cpuid.c 20 KB

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