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