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