xref: /openbmc/qemu/target/i386/cpu-sysemu.c (revision 86e372ad)
1 /*
2  *  i386 CPUID, CPU class, definitions, models: sysemu-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 "cpu.h"
22 #include "sysemu/kvm.h"
23 #include "sysemu/xen.h"
24 #include "sysemu/whpx.h"
25 #include "qapi/error.h"
26 #include "qapi/qapi-visit-run-state.h"
27 #include "qapi/qmp/qdict.h"
28 #include "qapi/qobject-input-visitor.h"
29 #include "qom/qom-qobject.h"
30 #include "qapi/qapi-commands-machine-target.h"
31 #include "hw/qdev-properties.h"
32 
33 #include "exec/address-spaces.h"
34 #include "hw/i386/apic_internal.h"
35 
36 #include "cpu-internal.h"
37 
38 /* Return a QDict containing keys for all properties that can be included
39  * in static expansion of CPU models. All properties set by x86_cpu_load_model()
40  * must be included in the dictionary.
41  */
42 static QDict *x86_cpu_static_props(void)
43 {
44     FeatureWord w;
45     int i;
46     static const char *props[] = {
47         "min-level",
48         "min-xlevel",
49         "family",
50         "model",
51         "stepping",
52         "model-id",
53         "vendor",
54         "lmce",
55         NULL,
56     };
57     static QDict *d;
58 
59     if (d) {
60         return d;
61     }
62 
63     d = qdict_new();
64     for (i = 0; props[i]; i++) {
65         qdict_put_null(d, props[i]);
66     }
67 
68     for (w = 0; w < FEATURE_WORDS; w++) {
69         FeatureWordInfo *fi = &feature_word_info[w];
70         int bit;
71         for (bit = 0; bit < 64; bit++) {
72             if (!fi->feat_names[bit]) {
73                 continue;
74             }
75             qdict_put_null(d, fi->feat_names[bit]);
76         }
77     }
78 
79     return d;
80 }
81 
82 /* Add an entry to @props dict, with the value for property. */
83 static void x86_cpu_expand_prop(X86CPU *cpu, QDict *props, const char *prop)
84 {
85     QObject *value = object_property_get_qobject(OBJECT(cpu), prop,
86                                                  &error_abort);
87 
88     qdict_put_obj(props, prop, value);
89 }
90 
91 /* Convert CPU model data from X86CPU object to a property dictionary
92  * that can recreate exactly the same CPU model.
93  */
94 static void x86_cpu_to_dict(X86CPU *cpu, QDict *props)
95 {
96     QDict *sprops = x86_cpu_static_props();
97     const QDictEntry *e;
98 
99     for (e = qdict_first(sprops); e; e = qdict_next(sprops, e)) {
100         const char *prop = qdict_entry_key(e);
101         x86_cpu_expand_prop(cpu, props, prop);
102     }
103 }
104 
105 /* Convert CPU model data from X86CPU object to a property dictionary
106  * that can recreate exactly the same CPU model, including every
107  * writable QOM property.
108  */
109 static void x86_cpu_to_dict_full(X86CPU *cpu, QDict *props)
110 {
111     ObjectPropertyIterator iter;
112     ObjectProperty *prop;
113 
114     object_property_iter_init(&iter, OBJECT(cpu));
115     while ((prop = object_property_iter_next(&iter))) {
116         /* skip read-only or write-only properties */
117         if (!prop->get || !prop->set) {
118             continue;
119         }
120 
121         /* "hotplugged" is the only property that is configurable
122          * on the command-line but will be set differently on CPUs
123          * created using "-cpu ... -smp ..." and by CPUs created
124          * on the fly by x86_cpu_from_model() for querying. Skip it.
125          */
126         if (!strcmp(prop->name, "hotplugged")) {
127             continue;
128         }
129         x86_cpu_expand_prop(cpu, props, prop->name);
130     }
131 }
132 
133 static void object_apply_props(Object *obj, QObject *props,
134                                const char *props_arg_name, Error **errp)
135 {
136     Visitor *visitor;
137     QDict *qdict;
138     const QDictEntry *prop;
139 
140     visitor = qobject_input_visitor_new(props);
141     if (!visit_start_struct(visitor, props_arg_name, NULL, 0, errp)) {
142         visit_free(visitor);
143         return;
144     }
145 
146     qdict = qobject_to(QDict, props);
147     for (prop = qdict_first(qdict); prop; prop = qdict_next(qdict, prop)) {
148         if (!object_property_set(obj, qdict_entry_key(prop),
149                                  visitor, errp)) {
150             goto out;
151         }
152     }
153 
154     visit_check_struct(visitor, errp);
155 out:
156     visit_end_struct(visitor, NULL);
157     visit_free(visitor);
158 }
159 
160 /* Create X86CPU object according to model+props specification */
161 static X86CPU *x86_cpu_from_model(const char *model, QObject *props,
162                                   const char *props_arg_name, Error **errp)
163 {
164     X86CPU *xc = NULL;
165     X86CPUClass *xcc;
166     Error *err = NULL;
167 
168     xcc = X86_CPU_CLASS(cpu_class_by_name(TYPE_X86_CPU, model));
169     if (xcc == NULL) {
170         error_setg(&err, "CPU model '%s' not found", model);
171         goto out;
172     }
173 
174     xc = X86_CPU(object_new_with_class(OBJECT_CLASS(xcc)));
175     if (props) {
176         object_apply_props(OBJECT(xc), props, props_arg_name, &err);
177         if (err) {
178             goto out;
179         }
180     }
181 
182     x86_cpu_expand_features(xc, &err);
183     if (err) {
184         goto out;
185     }
186 
187 out:
188     if (err) {
189         error_propagate(errp, err);
190         object_unref(OBJECT(xc));
191         xc = NULL;
192     }
193     return xc;
194 }
195 
196 CpuModelExpansionInfo *
197 qmp_query_cpu_model_expansion(CpuModelExpansionType type,
198                                                       CpuModelInfo *model,
199                                                       Error **errp)
200 {
201     X86CPU *xc = NULL;
202     Error *err = NULL;
203     CpuModelExpansionInfo *ret = g_new0(CpuModelExpansionInfo, 1);
204     QDict *props = NULL;
205     const char *base_name;
206 
207     xc = x86_cpu_from_model(model->name, model->props, "model.props", &err);
208     if (err) {
209         goto out;
210     }
211 
212     props = qdict_new();
213     ret->model = g_new0(CpuModelInfo, 1);
214     ret->model->props = QOBJECT(props);
215 
216     switch (type) {
217     case CPU_MODEL_EXPANSION_TYPE_STATIC:
218         /* Static expansion will be based on "base" only */
219         base_name = "base";
220         x86_cpu_to_dict(xc, props);
221     break;
222     case CPU_MODEL_EXPANSION_TYPE_FULL:
223         /* As we don't return every single property, full expansion needs
224          * to keep the original model name+props, and add extra
225          * properties on top of that.
226          */
227         base_name = model->name;
228         x86_cpu_to_dict_full(xc, props);
229     break;
230     default:
231         error_setg(&err, "Unsupported expansion type");
232         goto out;
233     }
234 
235     x86_cpu_to_dict(xc, props);
236 
237     ret->model->name = g_strdup(base_name);
238 
239 out:
240     object_unref(OBJECT(xc));
241     if (err) {
242         error_propagate(errp, err);
243         qapi_free_CpuModelExpansionInfo(ret);
244         ret = NULL;
245     }
246     return ret;
247 }
248 
249 void cpu_clear_apic_feature(CPUX86State *env)
250 {
251     env->features[FEAT_1_EDX] &= ~CPUID_APIC;
252 }
253 
254 void cpu_set_apic_feature(CPUX86State *env)
255 {
256     env->features[FEAT_1_EDX] |= CPUID_APIC;
257 }
258 
259 bool cpu_has_x2apic_feature(CPUX86State *env)
260 {
261     return env->features[FEAT_1_ECX] & CPUID_EXT_X2APIC;
262 }
263 
264 bool cpu_is_bsp(X86CPU *cpu)
265 {
266     return cpu_get_apic_base(cpu->apic_state) & MSR_IA32_APICBASE_BSP;
267 }
268 
269 /* TODO: remove me, when reset over QOM tree is implemented */
270 void x86_cpu_machine_reset_cb(void *opaque)
271 {
272     X86CPU *cpu = opaque;
273     cpu_reset(CPU(cpu));
274 }
275 
276 APICCommonClass *apic_get_class(Error **errp)
277 {
278     const char *apic_type = "apic";
279 
280     /* TODO: in-kernel irqchip for hvf */
281     if (kvm_enabled()) {
282         if (!kvm_irqchip_in_kernel()) {
283             error_setg(errp, "KVM does not support userspace APIC");
284             return NULL;
285         }
286         apic_type = "kvm-apic";
287     } else if (xen_enabled()) {
288         apic_type = "xen-apic";
289     } else if (whpx_apic_in_platform()) {
290         apic_type = "whpx-apic";
291     }
292 
293     return APIC_COMMON_CLASS(object_class_by_name(apic_type));
294 }
295 
296 void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
297 {
298     APICCommonState *apic;
299     APICCommonClass *apic_class = apic_get_class(errp);
300 
301     if (!apic_class) {
302         return;
303     }
304 
305     cpu->apic_state = DEVICE(object_new_with_class(OBJECT_CLASS(apic_class)));
306     object_property_add_child(OBJECT(cpu), "lapic",
307                               OBJECT(cpu->apic_state));
308     object_unref(OBJECT(cpu->apic_state));
309 
310     /* TODO: convert to link<> */
311     apic = APIC_COMMON(cpu->apic_state);
312     apic->cpu = cpu;
313     apic->apicbase = APIC_DEFAULT_ADDRESS | MSR_IA32_APICBASE_ENABLE;
314 
315     /*
316      * apic_common_set_id needs to check if the CPU has x2APIC
317      * feature in case APIC ID >= 255, so we need to set apic->cpu
318      * before setting APIC ID
319      */
320     qdev_prop_set_uint32(cpu->apic_state, "id", cpu->apic_id);
321 }
322 
323 void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
324 {
325     APICCommonState *apic;
326     static bool apic_mmio_map_once;
327 
328     if (cpu->apic_state == NULL) {
329         return;
330     }
331     qdev_realize(DEVICE(cpu->apic_state), NULL, errp);
332 
333     /* Map APIC MMIO area */
334     apic = APIC_COMMON(cpu->apic_state);
335     if (!apic_mmio_map_once) {
336         memory_region_add_subregion_overlap(get_system_memory(),
337                                             apic->apicbase &
338                                             MSR_IA32_APICBASE_BASE,
339                                             &apic->io_memory,
340                                             0x1000);
341         apic_mmio_map_once = true;
342      }
343 }
344 
345 GuestPanicInformation *x86_cpu_get_crash_info(CPUState *cs)
346 {
347     X86CPU *cpu = X86_CPU(cs);
348     CPUX86State *env = &cpu->env;
349     GuestPanicInformation *panic_info = NULL;
350 
351     if (hyperv_feat_enabled(cpu, HYPERV_FEAT_CRASH)) {
352         panic_info = g_new0(GuestPanicInformation, 1);
353 
354         panic_info->type = GUEST_PANIC_INFORMATION_TYPE_HYPER_V;
355 
356         assert(HV_CRASH_PARAMS >= 5);
357         panic_info->u.hyper_v.arg1 = env->msr_hv_crash_params[0];
358         panic_info->u.hyper_v.arg2 = env->msr_hv_crash_params[1];
359         panic_info->u.hyper_v.arg3 = env->msr_hv_crash_params[2];
360         panic_info->u.hyper_v.arg4 = env->msr_hv_crash_params[3];
361         panic_info->u.hyper_v.arg5 = env->msr_hv_crash_params[4];
362     }
363 
364     return panic_info;
365 }
366 void x86_cpu_get_crash_info_qom(Object *obj, Visitor *v,
367                                 const char *name, void *opaque,
368                                 Error **errp)
369 {
370     CPUState *cs = CPU(obj);
371     GuestPanicInformation *panic_info;
372 
373     if (!cs->crash_occurred) {
374         error_setg(errp, "No crash occurred");
375         return;
376     }
377 
378     panic_info = x86_cpu_get_crash_info(cs);
379     if (panic_info == NULL) {
380         error_setg(errp, "No crash information");
381         return;
382     }
383 
384     visit_type_GuestPanicInformation(v, "crash-information", &panic_info,
385                                      errp);
386     qapi_free_GuestPanicInformation(panic_info);
387 }
388 
389