cpuid.c 17 KB

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