xref: /openbmc/qemu/target/i386/kvm/kvm-cpu.c (revision 2df1eb27)
1 /*
2  * x86 KVM CPU type initialization
3  *
4  * Copyright 2021 SUSE LLC
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "cpu.h"
12 #include "host-cpu.h"
13 #include "kvm-cpu.h"
14 #include "qapi/error.h"
15 #include "sysemu/sysemu.h"
16 #include "hw/boards.h"
17 
18 #include "kvm_i386.h"
19 #include "hw/core/accel-cpu.h"
20 
21 static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
22 {
23     X86CPU *cpu = X86_CPU(cs);
24     CPUX86State *env = &cpu->env;
25 
26     /*
27      * The realize order is important, since x86_cpu_realize() checks if
28      * nothing else has been set by the user (or by accelerators) in
29      * cpu->ucode_rev and cpu->phys_bits, and updates the CPUID results in
30      * mwait.ecx.
31      * This accel realization code also assumes cpu features are already expanded.
32      *
33      * realize order:
34      *
35      * x86_cpu_realize():
36      *  -> x86_cpu_expand_features()
37      *  -> cpu_exec_realizefn():
38      *            -> accel_cpu_common_realize()
39      *               kvm_cpu_realizefn() -> host_cpu_realizefn()
40      *  -> cpu_common_realizefn()
41      *  -> check/update ucode_rev, phys_bits, mwait
42      */
43     if (cpu->max_features) {
44         if (enable_cpu_pm && kvm_has_waitpkg()) {
45             env->features[FEAT_7_0_ECX] |= CPUID_7_0_ECX_WAITPKG;
46         }
47         if (cpu->ucode_rev == 0) {
48             cpu->ucode_rev =
49                 kvm_arch_get_supported_msr_feature(kvm_state,
50                                                    MSR_IA32_UCODE_REV);
51         }
52     }
53     return host_cpu_realizefn(cs, errp);
54 }
55 
56 static bool lmce_supported(void)
57 {
58     uint64_t mce_cap = 0;
59 
60     if (kvm_ioctl(kvm_state, KVM_X86_GET_MCE_CAP_SUPPORTED, &mce_cap) < 0) {
61         return false;
62     }
63     return !!(mce_cap & MCG_LMCE_P);
64 }
65 
66 static void kvm_cpu_max_instance_init(X86CPU *cpu)
67 {
68     CPUX86State *env = &cpu->env;
69     KVMState *s = kvm_state;
70 
71     host_cpu_max_instance_init(cpu);
72 
73     if (lmce_supported()) {
74         object_property_set_bool(OBJECT(cpu), "lmce", true, &error_abort);
75     }
76 
77     env->cpuid_min_level =
78         kvm_arch_get_supported_cpuid(s, 0x0, 0, R_EAX);
79     env->cpuid_min_xlevel =
80         kvm_arch_get_supported_cpuid(s, 0x80000000, 0, R_EAX);
81     env->cpuid_min_xlevel2 =
82         kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_EAX);
83 }
84 
85 static void kvm_cpu_xsave_init(void)
86 {
87     static bool first = true;
88     uint32_t eax, ebx, ecx, edx;
89     int i;
90 
91     if (!first) {
92         return;
93     }
94     first = false;
95 
96     /* x87 and SSE states are in the legacy region of the XSAVE area. */
97     x86_ext_save_areas[XSTATE_FP_BIT].offset = 0;
98     x86_ext_save_areas[XSTATE_SSE_BIT].offset = 0;
99 
100     for (i = XSTATE_SSE_BIT + 1; i < XSAVE_STATE_AREA_COUNT; i++) {
101         ExtSaveArea *esa = &x86_ext_save_areas[i];
102 
103         if (!esa->size) {
104             continue;
105         }
106         if ((x86_cpu_get_supported_feature_word(esa->feature, false) & esa->bits)
107             != esa->bits) {
108             continue;
109         }
110         host_cpuid(0xd, i, &eax, &ebx, &ecx, &edx);
111         if (eax != 0) {
112             assert(esa->size == eax);
113             esa->offset = ebx;
114             esa->ecx = ecx;
115         }
116     }
117 }
118 
119 /*
120  * KVM-specific features that are automatically added/removed
121  * from cpudef models when KVM is enabled.
122  * Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
123  *
124  * NOTE: features can be enabled by default only if they were
125  *       already available in the oldest kernel version supported
126  *       by the KVM accelerator (see "OS requirements" section at
127  *       docs/system/target-i386.rst)
128  */
129 static PropValue kvm_default_props[] = {
130     { "kvmclock", "on" },
131     { "kvm-nopiodelay", "on" },
132     { "kvm-asyncpf", "on" },
133     { "kvm-steal-time", "on" },
134     { "kvm-pv-eoi", "on" },
135     { "kvmclock-stable-bit", "on" },
136     { "x2apic", "on" },
137     { "kvm-msi-ext-dest-id", "off" },
138     { "acpi", "off" },
139     { "monitor", "off" },
140     { "svm", "off" },
141     { NULL, NULL },
142 };
143 
144 /*
145  * Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
146  */
147 void x86_cpu_change_kvm_default(const char *prop, const char *value)
148 {
149     PropValue *pv;
150     for (pv = kvm_default_props; pv->prop; pv++) {
151         if (!strcmp(pv->prop, prop)) {
152             pv->value = value;
153             break;
154         }
155     }
156 
157     /*
158      * It is valid to call this function only for properties that
159      * are already present in the kvm_default_props table.
160      */
161     assert(pv->prop);
162 }
163 
164 static void kvm_cpu_instance_init(CPUState *cs)
165 {
166     X86CPU *cpu = X86_CPU(cs);
167     X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
168 
169     host_cpu_instance_init(cpu);
170 
171     if (xcc->model) {
172         /* only applies to builtin_x86_defs cpus */
173         if (!kvm_irqchip_in_kernel()) {
174             x86_cpu_change_kvm_default("x2apic", "off");
175         } else if (kvm_irqchip_is_split()) {
176             x86_cpu_change_kvm_default("kvm-msi-ext-dest-id", "on");
177         }
178 
179         /* Special cases not set in the X86CPUDefinition structs: */
180         x86_cpu_apply_props(cpu, kvm_default_props);
181     }
182 
183     if (cpu->max_features) {
184         kvm_cpu_max_instance_init(cpu);
185     }
186 
187     kvm_cpu_xsave_init();
188 }
189 
190 static void kvm_cpu_accel_class_init(ObjectClass *oc, void *data)
191 {
192     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
193 
194     acc->cpu_target_realize = kvm_cpu_realizefn;
195     acc->cpu_instance_init = kvm_cpu_instance_init;
196 }
197 static const TypeInfo kvm_cpu_accel_type_info = {
198     .name = ACCEL_CPU_NAME("kvm"),
199 
200     .parent = TYPE_ACCEL_CPU,
201     .class_init = kvm_cpu_accel_class_init,
202     .abstract = true,
203 };
204 static void kvm_cpu_accel_register_types(void)
205 {
206     type_register_static(&kvm_cpu_accel_type_info);
207 }
208 type_init(kvm_cpu_accel_register_types);
209