cpuid.c 19 KB

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