cpuid.c 24 KB

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