cpuid.c 20 KB

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