]>
Commit | Line | Data |
---|---|---|
00b27a3e AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * cpuid support routines | |
4 | * | |
5 | * derived from arch/x86/kvm/x86.c | |
6 | * | |
7 | * Copyright 2011 Red Hat, Inc. and/or its affiliates. | |
8 | * Copyright IBM Corporation, 2008 | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
11 | * the COPYING file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <linux/kvm_host.h> | |
16 | #include <linux/module.h> | |
bb5a798a JK |
17 | #include <linux/vmalloc.h> |
18 | #include <linux/uaccess.h> | |
00b27a3e AK |
19 | #include <asm/user.h> |
20 | #include <asm/xsave.h> | |
21 | #include "cpuid.h" | |
22 | #include "lapic.h" | |
23 | #include "mmu.h" | |
24 | #include "trace.h" | |
25 | ||
26 | void kvm_update_cpuid(struct kvm_vcpu *vcpu) | |
27 | { | |
28 | struct kvm_cpuid_entry2 *best; | |
29 | struct kvm_lapic *apic = vcpu->arch.apic; | |
30 | ||
31 | best = kvm_find_cpuid_entry(vcpu, 1, 0); | |
32 | if (!best) | |
33 | return; | |
34 | ||
35 | /* Update OSXSAVE bit */ | |
36 | if (cpu_has_xsave && best->function == 0x1) { | |
37 | best->ecx &= ~(bit(X86_FEATURE_OSXSAVE)); | |
38 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) | |
39 | best->ecx |= bit(X86_FEATURE_OSXSAVE); | |
40 | } | |
41 | ||
42 | if (apic) { | |
43 | if (best->ecx & bit(X86_FEATURE_TSC_DEADLINE_TIMER)) | |
44 | apic->lapic_timer.timer_mode_mask = 3 << 17; | |
45 | else | |
46 | apic->lapic_timer.timer_mode_mask = 1 << 17; | |
47 | } | |
f5132b01 | 48 | |
d7876f1b PB |
49 | best = kvm_find_cpuid_entry(vcpu, 0xD, 0); |
50 | if (!best) | |
51 | vcpu->arch.guest_supported_xcr0 = 0; | |
52 | else | |
53 | vcpu->arch.guest_supported_xcr0 = | |
54 | (best->eax | ((u64)best->edx << 32)) & | |
55 | host_xcr0 & KVM_SUPPORTED_XCR0; | |
56 | ||
f5132b01 | 57 | kvm_pmu_cpuid_update(vcpu); |
00b27a3e AK |
58 | } |
59 | ||
60 | static int is_efer_nx(void) | |
61 | { | |
62 | unsigned long long efer = 0; | |
63 | ||
64 | rdmsrl_safe(MSR_EFER, &efer); | |
65 | return efer & EFER_NX; | |
66 | } | |
67 | ||
68 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) | |
69 | { | |
70 | int i; | |
71 | struct kvm_cpuid_entry2 *e, *entry; | |
72 | ||
73 | entry = NULL; | |
74 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { | |
75 | e = &vcpu->arch.cpuid_entries[i]; | |
76 | if (e->function == 0x80000001) { | |
77 | entry = e; | |
78 | break; | |
79 | } | |
80 | } | |
81 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { | |
82 | entry->edx &= ~(1 << 20); | |
83 | printk(KERN_INFO "kvm: guest NX capability removed\n"); | |
84 | } | |
85 | } | |
86 | ||
87 | /* when an old userspace process fills a new kernel module */ | |
88 | int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, | |
89 | struct kvm_cpuid *cpuid, | |
90 | struct kvm_cpuid_entry __user *entries) | |
91 | { | |
92 | int r, i; | |
93 | struct kvm_cpuid_entry *cpuid_entries; | |
94 | ||
95 | r = -E2BIG; | |
96 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
97 | goto out; | |
98 | r = -ENOMEM; | |
99 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); | |
100 | if (!cpuid_entries) | |
101 | goto out; | |
102 | r = -EFAULT; | |
103 | if (copy_from_user(cpuid_entries, entries, | |
104 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) | |
105 | goto out_free; | |
106 | for (i = 0; i < cpuid->nent; i++) { | |
107 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; | |
108 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; | |
109 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; | |
110 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; | |
111 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; | |
112 | vcpu->arch.cpuid_entries[i].index = 0; | |
113 | vcpu->arch.cpuid_entries[i].flags = 0; | |
114 | vcpu->arch.cpuid_entries[i].padding[0] = 0; | |
115 | vcpu->arch.cpuid_entries[i].padding[1] = 0; | |
116 | vcpu->arch.cpuid_entries[i].padding[2] = 0; | |
117 | } | |
118 | vcpu->arch.cpuid_nent = cpuid->nent; | |
119 | cpuid_fix_nx_cap(vcpu); | |
120 | r = 0; | |
121 | kvm_apic_set_version(vcpu); | |
122 | kvm_x86_ops->cpuid_update(vcpu); | |
123 | kvm_update_cpuid(vcpu); | |
124 | ||
125 | out_free: | |
126 | vfree(cpuid_entries); | |
127 | out: | |
128 | return r; | |
129 | } | |
130 | ||
131 | int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, | |
132 | struct kvm_cpuid2 *cpuid, | |
133 | struct kvm_cpuid_entry2 __user *entries) | |
134 | { | |
135 | int r; | |
136 | ||
137 | r = -E2BIG; | |
138 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
139 | goto out; | |
140 | r = -EFAULT; | |
141 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, | |
142 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) | |
143 | goto out; | |
144 | vcpu->arch.cpuid_nent = cpuid->nent; | |
145 | kvm_apic_set_version(vcpu); | |
146 | kvm_x86_ops->cpuid_update(vcpu); | |
147 | kvm_update_cpuid(vcpu); | |
148 | return 0; | |
149 | ||
150 | out: | |
151 | return r; | |
152 | } | |
153 | ||
154 | int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, | |
155 | struct kvm_cpuid2 *cpuid, | |
156 | struct kvm_cpuid_entry2 __user *entries) | |
157 | { | |
158 | int r; | |
159 | ||
160 | r = -E2BIG; | |
161 | if (cpuid->nent < vcpu->arch.cpuid_nent) | |
162 | goto out; | |
163 | r = -EFAULT; | |
164 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, | |
165 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) | |
166 | goto out; | |
167 | return 0; | |
168 | ||
169 | out: | |
170 | cpuid->nent = vcpu->arch.cpuid_nent; | |
171 | return r; | |
172 | } | |
173 | ||
174 | static void cpuid_mask(u32 *word, int wordnum) | |
175 | { | |
176 | *word &= boot_cpu_data.x86_capability[wordnum]; | |
177 | } | |
178 | ||
179 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |
180 | u32 index) | |
181 | { | |
182 | entry->function = function; | |
183 | entry->index = index; | |
184 | cpuid_count(entry->function, entry->index, | |
185 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); | |
186 | entry->flags = 0; | |
187 | } | |
188 | ||
189 | static bool supported_xcr0_bit(unsigned bit) | |
190 | { | |
191 | u64 mask = ((u64)1 << bit); | |
192 | ||
647e23bb | 193 | return mask & KVM_SUPPORTED_XCR0 & host_xcr0; |
00b27a3e AK |
194 | } |
195 | ||
196 | #define F(x) bit(X86_FEATURE_##x) | |
197 | ||
831bf664 | 198 | static int do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
00b27a3e AK |
199 | u32 index, int *nent, int maxnent) |
200 | { | |
831bf664 | 201 | int r; |
00b27a3e AK |
202 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
203 | #ifdef CONFIG_X86_64 | |
204 | unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL) | |
205 | ? F(GBPAGES) : 0; | |
206 | unsigned f_lm = F(LM); | |
207 | #else | |
208 | unsigned f_gbpages = 0; | |
209 | unsigned f_lm = 0; | |
210 | #endif | |
211 | unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0; | |
ad756a16 | 212 | unsigned f_invpcid = kvm_x86_ops->invpcid_supported() ? F(INVPCID) : 0; |
00b27a3e AK |
213 | |
214 | /* cpuid 1.edx */ | |
215 | const u32 kvm_supported_word0_x86_features = | |
216 | F(FPU) | F(VME) | F(DE) | F(PSE) | | |
217 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | | |
218 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | | |
219 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | | |
220 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) | | |
221 | 0 /* Reserved, DS, ACPI */ | F(MMX) | | |
222 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | | |
223 | 0 /* HTT, TM, Reserved, PBE */; | |
224 | /* cpuid 0x80000001.edx */ | |
225 | const u32 kvm_supported_word1_x86_features = | |
226 | F(FPU) | F(VME) | F(DE) | F(PSE) | | |
227 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | | |
228 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | | |
229 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | | |
230 | F(PAT) | F(PSE36) | 0 /* Reserved */ | | |
231 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | | |
232 | F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp | | |
233 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); | |
234 | /* cpuid 1.ecx */ | |
235 | const u32 kvm_supported_word4_x86_features = | |
236 | F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ | | |
237 | 0 /* DS-CPL, VMX, SMX, EST */ | | |
238 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | | |
fb215366 | 239 | F(FMA) | F(CX16) | 0 /* xTPR Update, PDCM */ | |
ad756a16 | 240 | F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) | |
00b27a3e AK |
241 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
242 | 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) | | |
243 | F(F16C) | F(RDRAND); | |
244 | /* cpuid 0x80000001.ecx */ | |
245 | const u32 kvm_supported_word6_x86_features = | |
246 | F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ | | |
247 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | | |
2b036c6b | 248 | F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) | |
00b27a3e AK |
249 | 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM); |
250 | ||
251 | /* cpuid 0xC0000001.edx */ | |
252 | const u32 kvm_supported_word5_x86_features = | |
253 | F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) | | |
254 | F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) | | |
255 | F(PMM) | F(PMM_EN); | |
256 | ||
257 | /* cpuid 7.0.ebx */ | |
258 | const u32 kvm_supported_word9_x86_features = | |
83c52915 | 259 | F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) | |
ad756a16 | 260 | F(BMI2) | F(ERMS) | f_invpcid | F(RTM); |
00b27a3e AK |
261 | |
262 | /* all calls to cpuid_count() should be made on the same cpu */ | |
263 | get_cpu(); | |
831bf664 SL |
264 | |
265 | r = -E2BIG; | |
266 | ||
267 | if (*nent >= maxnent) | |
268 | goto out; | |
269 | ||
00b27a3e AK |
270 | do_cpuid_1_ent(entry, function, index); |
271 | ++*nent; | |
272 | ||
273 | switch (function) { | |
274 | case 0: | |
275 | entry->eax = min(entry->eax, (u32)0xd); | |
276 | break; | |
277 | case 1: | |
278 | entry->edx &= kvm_supported_word0_x86_features; | |
279 | cpuid_mask(&entry->edx, 0); | |
280 | entry->ecx &= kvm_supported_word4_x86_features; | |
281 | cpuid_mask(&entry->ecx, 4); | |
282 | /* we support x2apic emulation even if host does not support | |
283 | * it since we emulate x2apic in software */ | |
284 | entry->ecx |= F(X2APIC); | |
285 | break; | |
286 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands | |
287 | * may return different values. This forces us to get_cpu() before | |
288 | * issuing the first command, and also to emulate this annoying behavior | |
289 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ | |
290 | case 2: { | |
291 | int t, times = entry->eax & 0xff; | |
292 | ||
293 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
294 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; | |
831bf664 SL |
295 | for (t = 1; t < times; ++t) { |
296 | if (*nent >= maxnent) | |
297 | goto out; | |
298 | ||
00b27a3e AK |
299 | do_cpuid_1_ent(&entry[t], function, 0); |
300 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
301 | ++*nent; | |
302 | } | |
303 | break; | |
304 | } | |
305 | /* function 4 has additional index. */ | |
306 | case 4: { | |
307 | int i, cache_type; | |
308 | ||
309 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
310 | /* read more entries until cache_type is zero */ | |
831bf664 SL |
311 | for (i = 1; ; ++i) { |
312 | if (*nent >= maxnent) | |
313 | goto out; | |
314 | ||
00b27a3e AK |
315 | cache_type = entry[i - 1].eax & 0x1f; |
316 | if (!cache_type) | |
317 | break; | |
318 | do_cpuid_1_ent(&entry[i], function, i); | |
319 | entry[i].flags |= | |
320 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
321 | ++*nent; | |
322 | } | |
323 | break; | |
324 | } | |
325 | case 7: { | |
326 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
bbbda795 | 327 | /* Mask ebx against host capability word 9 */ |
00b27a3e AK |
328 | if (index == 0) { |
329 | entry->ebx &= kvm_supported_word9_x86_features; | |
330 | cpuid_mask(&entry->ebx, 9); | |
ba904635 WA |
331 | // TSC_ADJUST is emulated |
332 | entry->ebx |= F(TSC_ADJUST); | |
00b27a3e AK |
333 | } else |
334 | entry->ebx = 0; | |
335 | entry->eax = 0; | |
336 | entry->ecx = 0; | |
337 | entry->edx = 0; | |
338 | break; | |
339 | } | |
340 | case 9: | |
341 | break; | |
a6c06ed1 GN |
342 | case 0xa: { /* Architectural Performance Monitoring */ |
343 | struct x86_pmu_capability cap; | |
344 | union cpuid10_eax eax; | |
345 | union cpuid10_edx edx; | |
346 | ||
347 | perf_get_x86_pmu_capability(&cap); | |
348 | ||
349 | /* | |
350 | * Only support guest architectural pmu on a host | |
351 | * with architectural pmu. | |
352 | */ | |
353 | if (!cap.version) | |
354 | memset(&cap, 0, sizeof(cap)); | |
355 | ||
356 | eax.split.version_id = min(cap.version, 2); | |
357 | eax.split.num_counters = cap.num_counters_gp; | |
358 | eax.split.bit_width = cap.bit_width_gp; | |
359 | eax.split.mask_length = cap.events_mask_len; | |
360 | ||
361 | edx.split.num_counters_fixed = cap.num_counters_fixed; | |
362 | edx.split.bit_width_fixed = cap.bit_width_fixed; | |
363 | edx.split.reserved = 0; | |
364 | ||
365 | entry->eax = eax.full; | |
366 | entry->ebx = cap.events_mask; | |
367 | entry->ecx = 0; | |
368 | entry->edx = edx.full; | |
369 | break; | |
370 | } | |
00b27a3e AK |
371 | /* function 0xb has additional index. */ |
372 | case 0xb: { | |
373 | int i, level_type; | |
374 | ||
375 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
376 | /* read more entries until level_type is zero */ | |
831bf664 SL |
377 | for (i = 1; ; ++i) { |
378 | if (*nent >= maxnent) | |
379 | goto out; | |
380 | ||
00b27a3e AK |
381 | level_type = entry[i - 1].ecx & 0xff00; |
382 | if (!level_type) | |
383 | break; | |
384 | do_cpuid_1_ent(&entry[i], function, i); | |
385 | entry[i].flags |= | |
386 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
387 | ++*nent; | |
388 | } | |
389 | break; | |
390 | } | |
391 | case 0xd: { | |
392 | int idx, i; | |
393 | ||
647e23bb PB |
394 | entry->eax &= host_xcr0 & KVM_SUPPORTED_XCR0; |
395 | entry->edx &= (host_xcr0 & KVM_SUPPORTED_XCR0) >> 32; | |
00b27a3e | 396 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
831bf664 SL |
397 | for (idx = 1, i = 1; idx < 64; ++idx) { |
398 | if (*nent >= maxnent) | |
399 | goto out; | |
400 | ||
00b27a3e AK |
401 | do_cpuid_1_ent(&entry[i], function, idx); |
402 | if (entry[i].eax == 0 || !supported_xcr0_bit(idx)) | |
403 | continue; | |
404 | entry[i].flags |= | |
405 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
406 | ++*nent; | |
407 | ++i; | |
408 | } | |
409 | break; | |
410 | } | |
411 | case KVM_CPUID_SIGNATURE: { | |
326d07cb MK |
412 | static const char signature[12] = "KVMKVMKVM\0\0"; |
413 | const u32 *sigptr = (const u32 *)signature; | |
57c22e5f | 414 | entry->eax = KVM_CPUID_FEATURES; |
00b27a3e AK |
415 | entry->ebx = sigptr[0]; |
416 | entry->ecx = sigptr[1]; | |
417 | entry->edx = sigptr[2]; | |
418 | break; | |
419 | } | |
420 | case KVM_CPUID_FEATURES: | |
421 | entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) | | |
422 | (1 << KVM_FEATURE_NOP_IO_DELAY) | | |
423 | (1 << KVM_FEATURE_CLOCKSOURCE2) | | |
424 | (1 << KVM_FEATURE_ASYNC_PF) | | |
ae7a2a3f | 425 | (1 << KVM_FEATURE_PV_EOI) | |
6aef266c SV |
426 | (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) | |
427 | (1 << KVM_FEATURE_PV_UNHALT); | |
00b27a3e AK |
428 | |
429 | if (sched_info_on()) | |
430 | entry->eax |= (1 << KVM_FEATURE_STEAL_TIME); | |
431 | ||
432 | entry->ebx = 0; | |
433 | entry->ecx = 0; | |
434 | entry->edx = 0; | |
435 | break; | |
436 | case 0x80000000: | |
437 | entry->eax = min(entry->eax, 0x8000001a); | |
438 | break; | |
439 | case 0x80000001: | |
440 | entry->edx &= kvm_supported_word1_x86_features; | |
441 | cpuid_mask(&entry->edx, 1); | |
442 | entry->ecx &= kvm_supported_word6_x86_features; | |
443 | cpuid_mask(&entry->ecx, 6); | |
444 | break; | |
445 | case 0x80000008: { | |
446 | unsigned g_phys_as = (entry->eax >> 16) & 0xff; | |
447 | unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U); | |
448 | unsigned phys_as = entry->eax & 0xff; | |
449 | ||
450 | if (!g_phys_as) | |
451 | g_phys_as = phys_as; | |
452 | entry->eax = g_phys_as | (virt_as << 8); | |
453 | entry->ebx = entry->edx = 0; | |
454 | break; | |
455 | } | |
456 | case 0x80000019: | |
457 | entry->ecx = entry->edx = 0; | |
458 | break; | |
459 | case 0x8000001a: | |
460 | break; | |
461 | case 0x8000001d: | |
462 | break; | |
463 | /*Add support for Centaur's CPUID instruction*/ | |
464 | case 0xC0000000: | |
465 | /*Just support up to 0xC0000004 now*/ | |
466 | entry->eax = min(entry->eax, 0xC0000004); | |
467 | break; | |
468 | case 0xC0000001: | |
469 | entry->edx &= kvm_supported_word5_x86_features; | |
470 | cpuid_mask(&entry->edx, 5); | |
471 | break; | |
472 | case 3: /* Processor serial number */ | |
473 | case 5: /* MONITOR/MWAIT */ | |
474 | case 6: /* Thermal management */ | |
00b27a3e AK |
475 | case 0x80000007: /* Advanced power management */ |
476 | case 0xC0000002: | |
477 | case 0xC0000003: | |
478 | case 0xC0000004: | |
479 | default: | |
480 | entry->eax = entry->ebx = entry->ecx = entry->edx = 0; | |
481 | break; | |
482 | } | |
483 | ||
484 | kvm_x86_ops->set_supported_cpuid(function, entry); | |
485 | ||
831bf664 SL |
486 | r = 0; |
487 | ||
488 | out: | |
00b27a3e | 489 | put_cpu(); |
831bf664 SL |
490 | |
491 | return r; | |
00b27a3e AK |
492 | } |
493 | ||
494 | #undef F | |
495 | ||
831bf664 SL |
496 | struct kvm_cpuid_param { |
497 | u32 func; | |
498 | u32 idx; | |
499 | bool has_leaf_count; | |
326d07cb | 500 | bool (*qualifier)(const struct kvm_cpuid_param *param); |
831bf664 SL |
501 | }; |
502 | ||
326d07cb | 503 | static bool is_centaur_cpu(const struct kvm_cpuid_param *param) |
831bf664 SL |
504 | { |
505 | return boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR; | |
506 | } | |
507 | ||
00b27a3e AK |
508 | int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
509 | struct kvm_cpuid_entry2 __user *entries) | |
510 | { | |
511 | struct kvm_cpuid_entry2 *cpuid_entries; | |
831bf664 | 512 | int limit, nent = 0, r = -E2BIG, i; |
00b27a3e | 513 | u32 func; |
326d07cb | 514 | static const struct kvm_cpuid_param param[] = { |
831bf664 SL |
515 | { .func = 0, .has_leaf_count = true }, |
516 | { .func = 0x80000000, .has_leaf_count = true }, | |
517 | { .func = 0xC0000000, .qualifier = is_centaur_cpu, .has_leaf_count = true }, | |
518 | { .func = KVM_CPUID_SIGNATURE }, | |
519 | { .func = KVM_CPUID_FEATURES }, | |
520 | }; | |
00b27a3e AK |
521 | |
522 | if (cpuid->nent < 1) | |
523 | goto out; | |
524 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
525 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; | |
526 | r = -ENOMEM; | |
527 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); | |
528 | if (!cpuid_entries) | |
529 | goto out; | |
530 | ||
831bf664 SL |
531 | r = 0; |
532 | for (i = 0; i < ARRAY_SIZE(param); i++) { | |
326d07cb | 533 | const struct kvm_cpuid_param *ent = ¶m[i]; |
00b27a3e | 534 | |
831bf664 SL |
535 | if (ent->qualifier && !ent->qualifier(ent)) |
536 | continue; | |
00b27a3e | 537 | |
831bf664 | 538 | r = do_cpuid_ent(&cpuid_entries[nent], ent->func, ent->idx, |
00b27a3e AK |
539 | &nent, cpuid->nent); |
540 | ||
831bf664 | 541 | if (r) |
00b27a3e AK |
542 | goto out_free; |
543 | ||
831bf664 SL |
544 | if (!ent->has_leaf_count) |
545 | continue; | |
546 | ||
00b27a3e | 547 | limit = cpuid_entries[nent - 1].eax; |
831bf664 SL |
548 | for (func = ent->func + 1; func <= limit && nent < cpuid->nent && r == 0; ++func) |
549 | r = do_cpuid_ent(&cpuid_entries[nent], func, ent->idx, | |
550 | &nent, cpuid->nent); | |
00b27a3e | 551 | |
831bf664 | 552 | if (r) |
00b27a3e AK |
553 | goto out_free; |
554 | } | |
555 | ||
00b27a3e AK |
556 | r = -EFAULT; |
557 | if (copy_to_user(entries, cpuid_entries, | |
558 | nent * sizeof(struct kvm_cpuid_entry2))) | |
559 | goto out_free; | |
560 | cpuid->nent = nent; | |
561 | r = 0; | |
562 | ||
563 | out_free: | |
564 | vfree(cpuid_entries); | |
565 | out: | |
566 | return r; | |
567 | } | |
568 | ||
569 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) | |
570 | { | |
571 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; | |
572 | int j, nent = vcpu->arch.cpuid_nent; | |
573 | ||
574 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; | |
575 | /* when no next entry is found, the current entry[i] is reselected */ | |
576 | for (j = i + 1; ; j = (j + 1) % nent) { | |
577 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; | |
578 | if (ej->function == e->function) { | |
579 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; | |
580 | return j; | |
581 | } | |
582 | } | |
583 | return 0; /* silence gcc, even though control never reaches here */ | |
584 | } | |
585 | ||
586 | /* find an entry with matching function, matching index (if needed), and that | |
587 | * should be read next (if it's stateful) */ | |
588 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, | |
589 | u32 function, u32 index) | |
590 | { | |
591 | if (e->function != function) | |
592 | return 0; | |
593 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) | |
594 | return 0; | |
595 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && | |
596 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) | |
597 | return 0; | |
598 | return 1; | |
599 | } | |
600 | ||
601 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, | |
602 | u32 function, u32 index) | |
603 | { | |
604 | int i; | |
605 | struct kvm_cpuid_entry2 *best = NULL; | |
606 | ||
607 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { | |
608 | struct kvm_cpuid_entry2 *e; | |
609 | ||
610 | e = &vcpu->arch.cpuid_entries[i]; | |
611 | if (is_matching_cpuid_entry(e, function, index)) { | |
612 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) | |
613 | move_to_next_stateful_cpuid_entry(vcpu, i); | |
614 | best = e; | |
615 | break; | |
616 | } | |
617 | } | |
618 | return best; | |
619 | } | |
620 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); | |
621 | ||
622 | int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) | |
623 | { | |
624 | struct kvm_cpuid_entry2 *best; | |
625 | ||
626 | best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0); | |
627 | if (!best || best->eax < 0x80000008) | |
628 | goto not_found; | |
629 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); | |
630 | if (best) | |
631 | return best->eax & 0xff; | |
632 | not_found: | |
633 | return 36; | |
634 | } | |
635 | ||
636 | /* | |
637 | * If no match is found, check whether we exceed the vCPU's limit | |
638 | * and return the content of the highest valid _standard_ leaf instead. | |
639 | * This is to satisfy the CPUID specification. | |
640 | */ | |
641 | static struct kvm_cpuid_entry2* check_cpuid_limit(struct kvm_vcpu *vcpu, | |
642 | u32 function, u32 index) | |
643 | { | |
644 | struct kvm_cpuid_entry2 *maxlevel; | |
645 | ||
646 | maxlevel = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0); | |
647 | if (!maxlevel || maxlevel->eax >= function) | |
648 | return NULL; | |
649 | if (function & 0x80000000) { | |
650 | maxlevel = kvm_find_cpuid_entry(vcpu, 0, 0); | |
651 | if (!maxlevel) | |
652 | return NULL; | |
653 | } | |
654 | return kvm_find_cpuid_entry(vcpu, maxlevel->eax, index); | |
655 | } | |
656 | ||
62046e5a | 657 | void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx) |
00b27a3e | 658 | { |
62046e5a | 659 | u32 function = *eax, index = *ecx; |
00b27a3e AK |
660 | struct kvm_cpuid_entry2 *best; |
661 | ||
00b27a3e AK |
662 | best = kvm_find_cpuid_entry(vcpu, function, index); |
663 | ||
664 | if (!best) | |
665 | best = check_cpuid_limit(vcpu, function, index); | |
666 | ||
667 | if (best) { | |
62046e5a AK |
668 | *eax = best->eax; |
669 | *ebx = best->ebx; | |
670 | *ecx = best->ecx; | |
671 | *edx = best->edx; | |
672 | } else | |
673 | *eax = *ebx = *ecx = *edx = 0; | |
674 | } | |
66f7b72e | 675 | EXPORT_SYMBOL_GPL(kvm_cpuid); |
62046e5a AK |
676 | |
677 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) | |
678 | { | |
679 | u32 function, eax, ebx, ecx, edx; | |
680 | ||
681 | function = eax = kvm_register_read(vcpu, VCPU_REGS_RAX); | |
682 | ecx = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
683 | kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx); | |
684 | kvm_register_write(vcpu, VCPU_REGS_RAX, eax); | |
685 | kvm_register_write(vcpu, VCPU_REGS_RBX, ebx); | |
686 | kvm_register_write(vcpu, VCPU_REGS_RCX, ecx); | |
687 | kvm_register_write(vcpu, VCPU_REGS_RDX, edx); | |
00b27a3e | 688 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
62046e5a | 689 | trace_kvm_cpuid(function, eax, ebx, ecx, edx); |
00b27a3e AK |
690 | } |
691 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |