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