| 1 | /* |
| 2 | * i386 CPUID, CPU class, definitions, models: system-only code |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "qemu/osdep.h" |
| 21 | #include "qemu/error-report.h" |
| 22 | #include "system/kvm.h" |
| 23 | #include "cpu.h" |
| 24 | #include "qapi/error.h" |
| 25 | #include "qapi/qapi-visit-run-state.h" |
| 26 | #include "qobject/qdict.h" |
| 27 | #include "qapi/qobject-input-visitor.h" |
| 28 | #include "qom/qom-qobject.h" |
| 29 | #include "qapi/qapi-commands-machine.h" |
| 30 | |
| 31 | #include "cpu-internal.h" |
| 32 | |
| 33 | /* Return a QDict containing keys for all properties that can be included |
| 34 | * in static expansion of CPU models. All properties set by x86_cpu_load_model() |
| 35 | * must be included in the dictionary. |
| 36 | */ |
| 37 | static QDict *x86_cpu_static_props(void) |
| 38 | { |
| 39 | FeatureWord w; |
| 40 | int i; |
| 41 | static const char *props[] = { |
| 42 | "min-level", |
| 43 | "min-xlevel", |
| 44 | "family", |
| 45 | "model", |
| 46 | "stepping", |
| 47 | "model-id", |
| 48 | "vendor", |
| 49 | "lmce", |
| 50 | NULL, |
| 51 | }; |
| 52 | static QDict *d; |
| 53 | |
| 54 | if (d) { |
| 55 | return d; |
| 56 | } |
| 57 | |
| 58 | d = qdict_new(); |
| 59 | for (i = 0; props[i]; i++) { |
| 60 | qdict_put_null(d, props[i]); |
| 61 | } |
| 62 | |
| 63 | for (w = 0; w < FEATURE_WORDS; w++) { |
| 64 | FeatureWordInfo *fi = &feature_word_info[w]; |
| 65 | int bit; |
| 66 | for (bit = 0; bit < 64; bit++) { |
| 67 | if (!fi->feat_names[bit]) { |
| 68 | continue; |
| 69 | } |
| 70 | qdict_put_null(d, fi->feat_names[bit]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return d; |
| 75 | } |
| 76 | |
| 77 | /* Add an entry to @props dict, with the value for property. */ |
| 78 | static void x86_cpu_expand_prop(X86CPU *cpu, QDict *props, const char *prop) |
| 79 | { |
| 80 | QObject *value = object_property_get_qobject(OBJECT(cpu), prop, |
| 81 | &error_abort); |
| 82 | |
| 83 | qdict_put_obj(props, prop, value); |
| 84 | } |
| 85 | |
| 86 | /* Convert CPU model data from X86CPU object to a property dictionary |
| 87 | * that can recreate exactly the same CPU model. |
| 88 | */ |
| 89 | static void x86_cpu_to_dict(X86CPU *cpu, QDict *props) |
| 90 | { |
| 91 | QDict *sprops = x86_cpu_static_props(); |
| 92 | const QDictEntry *e; |
| 93 | |
| 94 | for (e = qdict_first(sprops); e; e = qdict_next(sprops, e)) { |
| 95 | const char *prop = qdict_entry_key(e); |
| 96 | x86_cpu_expand_prop(cpu, props, prop); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* Convert CPU model data from X86CPU object to a property dictionary |
| 101 | * that can recreate exactly the same CPU model, including every |
| 102 | * writable QOM property. |
| 103 | */ |
| 104 | static void x86_cpu_to_dict_full(X86CPU *cpu, QDict *props) |
| 105 | { |
| 106 | ObjectPropertyIterator iter; |
| 107 | ObjectProperty *prop; |
| 108 | |
| 109 | object_property_iter_init(&iter, OBJECT(cpu)); |
| 110 | while ((prop = object_property_iter_next(&iter))) { |
| 111 | /* skip read-only or write-only properties */ |
| 112 | if (!prop->get || !prop->set) { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | /* "hotplugged" is the only property that is configurable |
| 117 | * on the command-line but will be set differently on CPUs |
| 118 | * created using "-cpu ... -smp ..." and by CPUs created |
| 119 | * on the fly by x86_cpu_from_model() for querying. Skip it. |
| 120 | */ |
| 121 | if (!strcmp(prop->name, "hotplugged")) { |
| 122 | continue; |
| 123 | } |
| 124 | x86_cpu_expand_prop(cpu, props, prop->name); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void object_apply_props(Object *obj, QObject *props, |
| 129 | const char *props_arg_name, Error **errp) |
| 130 | { |
| 131 | Visitor *visitor; |
| 132 | QDict *qdict; |
| 133 | const QDictEntry *prop; |
| 134 | |
| 135 | visitor = qobject_input_visitor_new(props); |
| 136 | if (!visit_start_struct(visitor, props_arg_name, NULL, 0, errp)) { |
| 137 | visit_free(visitor); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | qdict = qobject_to(QDict, props); |
| 142 | for (prop = qdict_first(qdict); prop; prop = qdict_next(qdict, prop)) { |
| 143 | if (!object_property_set(obj, qdict_entry_key(prop), |
| 144 | visitor, errp)) { |
| 145 | goto out; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | visit_check_struct(visitor, errp); |
| 150 | out: |
| 151 | visit_end_struct(visitor, NULL); |
| 152 | visit_free(visitor); |
| 153 | } |
| 154 | |
| 155 | /* Create X86CPU object according to model+props specification */ |
| 156 | static X86CPU *x86_cpu_from_model(const char *model, QObject *props, |
| 157 | const char *props_arg_name, Error **errp) |
| 158 | { |
| 159 | X86CPU *xc = NULL; |
| 160 | X86CPUClass *xcc; |
| 161 | Error *err = NULL; |
| 162 | |
| 163 | xcc = X86_CPU_CLASS(cpu_class_by_name(TYPE_X86_CPU, model)); |
| 164 | if (xcc == NULL) { |
| 165 | error_setg(&err, "CPU model '%s' not found", model); |
| 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | xc = X86_CPU(object_new_with_class(OBJECT_CLASS(xcc))); |
| 170 | if (props) { |
| 171 | object_apply_props(OBJECT(xc), props, props_arg_name, &err); |
| 172 | if (err) { |
| 173 | goto out; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | x86_cpu_expand_features(xc, &err); |
| 178 | if (err) { |
| 179 | goto out; |
| 180 | } |
| 181 | |
| 182 | out: |
| 183 | if (err) { |
| 184 | error_propagate(errp, err); |
| 185 | object_unref(OBJECT(xc)); |
| 186 | xc = NULL; |
| 187 | } |
| 188 | return xc; |
| 189 | } |
| 190 | |
| 191 | CpuModelExpansionInfo * |
| 192 | qmp_query_cpu_model_expansion(CpuModelExpansionType type, |
| 193 | CpuModelInfo *model, |
| 194 | Error **errp) |
| 195 | { |
| 196 | X86CPU *xc = NULL; |
| 197 | Error *err = NULL; |
| 198 | CpuModelExpansionInfo *ret = g_new0(CpuModelExpansionInfo, 1); |
| 199 | QDict *props = NULL; |
| 200 | const char *base_name; |
| 201 | |
| 202 | xc = x86_cpu_from_model(model->name, model->props, "model.props", &err); |
| 203 | if (err) { |
| 204 | goto out; |
| 205 | } |
| 206 | |
| 207 | props = qdict_new(); |
| 208 | ret->model = g_new0(CpuModelInfo, 1); |
| 209 | ret->model->props = QOBJECT(props); |
| 210 | |
| 211 | switch (type) { |
| 212 | case CPU_MODEL_EXPANSION_TYPE_STATIC: |
| 213 | /* Static expansion will be based on "base" only */ |
| 214 | base_name = "base"; |
| 215 | x86_cpu_to_dict(xc, props); |
| 216 | break; |
| 217 | case CPU_MODEL_EXPANSION_TYPE_FULL: |
| 218 | /* As we don't return every single property, full expansion needs |
| 219 | * to keep the original model name+props, and add extra |
| 220 | * properties on top of that. |
| 221 | */ |
| 222 | base_name = model->name; |
| 223 | x86_cpu_to_dict_full(xc, props); |
| 224 | break; |
| 225 | default: |
| 226 | error_setg(&err, "Unsupported expansion type"); |
| 227 | goto out; |
| 228 | } |
| 229 | |
| 230 | x86_cpu_to_dict(xc, props); |
| 231 | |
| 232 | ret->model->name = g_strdup(base_name); |
| 233 | |
| 234 | out: |
| 235 | object_unref(OBJECT(xc)); |
| 236 | if (err) { |
| 237 | error_propagate(errp, err); |
| 238 | qapi_free_CpuModelExpansionInfo(ret); |
| 239 | ret = NULL; |
| 240 | } |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | void cpu_clear_apic_feature(CPUX86State *env) |
| 245 | { |
| 246 | env->features[FEAT_1_EDX] &= ~CPUID_APIC; |
| 247 | } |
| 248 | |
| 249 | void cpu_set_apic_feature(CPUX86State *env) |
| 250 | { |
| 251 | env->features[FEAT_1_EDX] |= CPUID_APIC; |
| 252 | } |
| 253 | |
| 254 | bool cpu_has_x2apic_feature(CPUX86State *env) |
| 255 | { |
| 256 | return env->features[FEAT_1_ECX] & CPUID_EXT_X2APIC; |
| 257 | } |
| 258 | |
| 259 | bool cpu_is_bsp(X86CPU *cpu) |
| 260 | { |
| 261 | return cpu_get_apic_base(cpu->apic_state) & MSR_IA32_APICBASE_BSP; |
| 262 | } |
| 263 | |
| 264 | /* TODO: remove me, when reset over QOM tree is implemented */ |
| 265 | void x86_cpu_machine_reset_cb(void *opaque) |
| 266 | { |
| 267 | X86CPU *cpu = opaque; |
| 268 | cpu_reset(CPU(cpu)); |
| 269 | } |
| 270 | |
| 271 | GuestPanicInformation *x86_cpu_get_crash_info(CPUState *cs) |
| 272 | { |
| 273 | X86CPU *cpu = X86_CPU(cs); |
| 274 | CPUX86State *env = &cpu->env; |
| 275 | GuestPanicInformation *panic_info = NULL; |
| 276 | |
| 277 | #ifdef CONFIG_KVM |
| 278 | if (kvm_enabled()) { |
| 279 | struct kvm_run *run = cs->kvm_run; |
| 280 | |
| 281 | if (run->exit_reason == KVM_EXIT_SYSTEM_EVENT && |
| 282 | run->system_event.type == KVM_SYSTEM_EVENT_SEV_TERM) { |
| 283 | panic_info = g_new0(GuestPanicInformation, 1); |
| 284 | |
| 285 | panic_info->type = GUEST_PANIC_INFORMATION_TYPE_SEV; |
| 286 | /* There should always be one data item, otherwise use zeroes. */ |
| 287 | if (run->system_event.ndata > 0) { |
| 288 | panic_info->u.sev.set = (run->system_event.data[0] >> 12) & 0xf; |
| 289 | panic_info->u.sev.code = (run->system_event.data[0] >> 16) & 0xff; |
| 290 | } else { |
| 291 | warn_report("Hypervisor did not provide any data for SEV-ES termination"); |
| 292 | } |
| 293 | } |
| 294 | } else |
| 295 | #endif |
| 296 | |
| 297 | if (hyperv_feat_enabled(cpu, HYPERV_FEAT_CRASH)) { |
| 298 | panic_info = g_new0(GuestPanicInformation, 1); |
| 299 | |
| 300 | panic_info->type = GUEST_PANIC_INFORMATION_TYPE_HYPER_V; |
| 301 | |
| 302 | assert(HV_CRASH_PARAMS >= 5); |
| 303 | panic_info->u.hyper_v.arg1 = env->msr_hv_crash_params[0]; |
| 304 | panic_info->u.hyper_v.arg2 = env->msr_hv_crash_params[1]; |
| 305 | panic_info->u.hyper_v.arg3 = env->msr_hv_crash_params[2]; |
| 306 | panic_info->u.hyper_v.arg4 = env->msr_hv_crash_params[3]; |
| 307 | panic_info->u.hyper_v.arg5 = env->msr_hv_crash_params[4]; |
| 308 | } |
| 309 | |
| 310 | return panic_info; |
| 311 | } |
| 312 | void x86_cpu_get_crash_info_qom(Object *obj, Visitor *v, |
| 313 | const char *name, void *opaque, |
| 314 | Error **errp) |
| 315 | { |
| 316 | CPUState *cs = CPU(obj); |
| 317 | GuestPanicInformation *panic_info; |
| 318 | |
| 319 | if (!cs->crash_occurred) { |
| 320 | error_setg(errp, "No crash occurred"); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | panic_info = x86_cpu_get_crash_info(cs); |
| 325 | if (panic_info == NULL) { |
| 326 | error_setg(errp, "No crash information"); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | visit_type_GuestPanicInformation(v, "crash-information", &panic_info, |
| 331 | errp); |
| 332 | qapi_free_GuestPanicInformation(panic_info); |
| 333 | } |
| 334 | |
| 335 | uint64_t cpu_x86_get_msr_core_thread_count(X86CPU *cpu) |
| 336 | { |
| 337 | CPUX86State *env = &cpu->env; |
| 338 | uint64_t val; |
| 339 | |
| 340 | val = x86_threads_per_pkg(&env->topo_info); /* thread count, bits 15..0 */ |
| 341 | val |= x86_cores_per_pkg(&env->topo_info) << 16; /* core count, bits 31..16 */ |
| 342 | |
| 343 | return val; |
| 344 | } |