cpuid.c 22 KB

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