cpuid.c 21 KB

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