cpuid.c 21 KB

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