cpuid.c 22 KB

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