cpuid.c 23 KB

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