cpuid.c 24 KB

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