cpuid.c 17 KB

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