cpuid.c 18 KB

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