Coverage Report

Created: 2026-10-31 08:12

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