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