cpuid.c 22 KB

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