cpuid.c 23 KB

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