cpuid.c 23 KB

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