cpuid.c 16 KB

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