cpuid.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel-based Virtual Machine driver for Linux
  4. * cpuid support routines
  5. *
  6. * derived from arch/x86/kvm/x86.c
  7. *
  8. * Copyright 2011 Red Hat, Inc. and/or its affiliates.
  9. * Copyright IBM Corporation, 2008
  10. */
  11. #include <linux/kvm_host.h>
  12. #include <linux/export.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/sched/stat.h>
  16. #include <asm/processor.h>
  17. #include <asm/user.h>
  18. #include <asm/fpu/xstate.h>
  19. #include <asm/sgx.h>
  20. #include <asm/cpuid.h>
  21. #include "cpuid.h"
  22. #include "lapic.h"
  23. #include "mmu.h"
  24. #include "trace.h"
  25. #include "pmu.h"
  26. /*
  27. * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
  28. * aligned to sizeof(unsigned long) because it's not accessed via bitops.
  29. */
  30. u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
  31. EXPORT_SYMBOL_GPL(kvm_cpu_caps);
  32. u32 xstate_required_size(u64 xstate_bv, bool compacted)
  33. {
  34. int feature_bit = 0;
  35. u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
  36. xstate_bv &= XFEATURE_MASK_EXTEND;
  37. while (xstate_bv) {
  38. if (xstate_bv & 0x1) {
  39. u32 eax, ebx, ecx, edx, offset;
  40. cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
  41. /* ECX[1]: 64B alignment in compacted form */
  42. if (compacted)
  43. offset = (ecx & 0x2) ? ALIGN(ret, 64) : ret;
  44. else
  45. offset = ebx;
  46. ret = max(ret, offset + eax);
  47. }
  48. xstate_bv >>= 1;
  49. feature_bit++;
  50. }
  51. return ret;
  52. }
  53. /*
  54. * This one is tied to SSB in the user API, and not
  55. * visible in /proc/cpuinfo.
  56. */
  57. #define KVM_X86_FEATURE_PSFD (13*32+28) /* Predictive Store Forwarding Disable */
  58. #define F feature_bit
  59. #define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0)
  60. /*
  61. * Magic value used by KVM when querying userspace-provided CPUID entries and
  62. * doesn't care about the CPIUD index because the index of the function in
  63. * question is not significant. Note, this magic value must have at least one
  64. * bit set in bits[63:32] and must be consumed as a u64 by cpuid_entry2_find()
  65. * to avoid false positives when processing guest CPUID input.
  66. */
  67. #define KVM_CPUID_INDEX_NOT_SIGNIFICANT -1ull
  68. static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
  69. struct kvm_cpuid_entry2 *entries, int nent, u32 function, u64 index)
  70. {
  71. struct kvm_cpuid_entry2 *e;
  72. int i;
  73. for (i = 0; i < nent; i++) {
  74. e = &entries[i];
  75. if (e->function != function)
  76. continue;
  77. /*
  78. * If the index isn't significant, use the first entry with a
  79. * matching function. It's userspace's responsibilty to not
  80. * provide "duplicate" entries in all cases.
  81. */
  82. if (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) || e->index == index)
  83. return e;
  84. /*
  85. * Similarly, use the first matching entry if KVM is doing a
  86. * lookup (as opposed to emulating CPUID) for a function that's
  87. * architecturally defined as not having a significant index.
  88. */
  89. if (index == KVM_CPUID_INDEX_NOT_SIGNIFICANT) {
  90. /*
  91. * Direct lookups from KVM should not diverge from what
  92. * KVM defines internally (the architectural behavior).
  93. */
  94. WARN_ON_ONCE(cpuid_function_is_indexed(function));
  95. return e;
  96. }
  97. }
  98. return NULL;
  99. }
  100. static int kvm_check_cpuid(struct kvm_vcpu *vcpu,
  101. struct kvm_cpuid_entry2 *entries,
  102. int nent)
  103. {
  104. struct kvm_cpuid_entry2 *best;
  105. u64 xfeatures;
  106. /*
  107. * The existing code assumes virtual address is 48-bit or 57-bit in the
  108. * canonical address checks; exit if it is ever changed.
  109. */
  110. best = cpuid_entry2_find(entries, nent, 0x80000008,
  111. KVM_CPUID_INDEX_NOT_SIGNIFICANT);
  112. if (best) {
  113. int vaddr_bits = (best->eax & 0xff00) >> 8;
  114. if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
  115. return -EINVAL;
  116. }
  117. /*
  118. * Exposing dynamic xfeatures to the guest requires additional
  119. * enabling in the FPU, e.g. to expand the guest XSAVE state size.
  120. */
  121. best = cpuid_entry2_find(entries, nent, 0xd, 0);
  122. if (!best)
  123. return 0;
  124. xfeatures = best->eax | ((u64)best->edx << 32);
  125. xfeatures &= XFEATURE_MASK_USER_DYNAMIC;
  126. if (!xfeatures)
  127. return 0;
  128. return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures);
  129. }
  130. /* Check whether the supplied CPUID data is equal to what is already set for the vCPU. */
  131. static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
  132. int nent)
  133. {
  134. struct kvm_cpuid_entry2 *orig;
  135. int i;
  136. if (nent != vcpu->arch.cpuid_nent)
  137. return -EINVAL;
  138. for (i = 0; i < nent; i++) {
  139. orig = &vcpu->arch.cpuid_entries[i];
  140. if (e2[i].function != orig->function ||
  141. e2[i].index != orig->index ||
  142. e2[i].flags != orig->flags ||
  143. e2[i].eax != orig->eax || e2[i].ebx != orig->ebx ||
  144. e2[i].ecx != orig->ecx || e2[i].edx != orig->edx)
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. static void kvm_update_kvm_cpuid_base(struct kvm_vcpu *vcpu)
  150. {
  151. u32 function;
  152. struct kvm_cpuid_entry2 *entry;
  153. vcpu->arch.kvm_cpuid_base = 0;
  154. for_each_possible_hypervisor_cpuid_base(function) {
  155. entry = kvm_find_cpuid_entry(vcpu, function);
  156. if (entry) {
  157. u32 signature[3];
  158. signature[0] = entry->ebx;
  159. signature[1] = entry->ecx;
  160. signature[2] = entry->edx;
  161. BUILD_BUG_ON(sizeof(signature) > sizeof(KVM_SIGNATURE));
  162. if (!memcmp(signature, KVM_SIGNATURE, sizeof(signature))) {
  163. vcpu->arch.kvm_cpuid_base = function;
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu,
  170. struct kvm_cpuid_entry2 *entries, int nent)
  171. {
  172. u32 base = vcpu->arch.kvm_cpuid_base;
  173. if (!base)
  174. return NULL;
  175. return cpuid_entry2_find(entries, nent, base | KVM_CPUID_FEATURES,
  176. KVM_CPUID_INDEX_NOT_SIGNIFICANT);
  177. }
  178. static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
  179. {
  180. return __kvm_find_kvm_cpuid_features(vcpu, vcpu->arch.cpuid_entries,
  181. vcpu->arch.cpuid_nent);
  182. }
  183. void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
  184. {
  185. struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
  186. /*
  187. * save the feature bitmap to avoid cpuid lookup for every PV
  188. * operation
  189. */
  190. if (best)
  191. vcpu->arch.pv_cpuid.features = best->eax;
  192. }
  193. /*
  194. * Calculate guest's supported XCR0 taking into account guest CPUID data and
  195. * KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0).
  196. */
  197. static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent)
  198. {
  199. struct kvm_cpuid_entry2 *best;
  200. best = cpuid_entry2_find(entries, nent, 0xd, 0);
  201. if (!best)
  202. return 0;
  203. return (best->eax | ((u64)best->edx << 32)) & kvm_caps.supported_xcr0;
  204. }
  205. static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries,
  206. int nent)
  207. {
  208. struct kvm_cpuid_entry2 *best;
  209. u64 guest_supported_xcr0 = cpuid_get_supported_xcr0(entries, nent);
  210. best = cpuid_entry2_find(entries, nent, 1, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
  211. if (best) {
  212. /* Update OSXSAVE bit */
  213. if (boot_cpu_has(X86_FEATURE_XSAVE))
  214. cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
  215. kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
  216. cpuid_entry_change(best, X86_FEATURE_APIC,
  217. vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
  218. }
  219. best = cpuid_entry2_find(entries, nent, 7, 0);
  220. if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
  221. cpuid_entry_change(best, X86_FEATURE_OSPKE,
  222. kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
  223. best = cpuid_entry2_find(entries, nent, 0xD, 0);
  224. if (best)
  225. best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
  226. best = cpuid_entry2_find(entries, nent, 0xD, 1);
  227. if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
  228. cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
  229. best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
  230. best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent);
  231. if (kvm_hlt_in_guest(vcpu->kvm) && best &&
  232. (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
  233. best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
  234. if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
  235. best = cpuid_entry2_find(entries, nent, 0x1, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
  236. if (best)
  237. cpuid_entry_change(best, X86_FEATURE_MWAIT,
  238. vcpu->arch.ia32_misc_enable_msr &
  239. MSR_IA32_MISC_ENABLE_MWAIT);
  240. }
  241. /*
  242. * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate
  243. * the supported XSAVE Feature Request Mask (XFRM), i.e. the enclave's
  244. * requested XCR0 value. The enclave's XFRM must be a subset of XCRO
  245. * at the time of EENTER, thus adjust the allowed XFRM by the guest's
  246. * supported XCR0. Similar to XCR0 handling, FP and SSE are forced to
  247. * '1' even on CPUs that don't support XSAVE.
  248. */
  249. best = cpuid_entry2_find(entries, nent, 0x12, 0x1);
  250. if (best) {
  251. best->ecx &= guest_supported_xcr0 & 0xffffffff;
  252. best->edx &= guest_supported_xcr0 >> 32;
  253. best->ecx |= XFEATURE_MASK_FPSSE;
  254. }
  255. }
  256. void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
  257. {
  258. __kvm_update_cpuid_runtime(vcpu, vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent);
  259. }
  260. EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime);
  261. static bool kvm_cpuid_has_hyperv(struct kvm_cpuid_entry2 *entries, int nent)
  262. {
  263. struct kvm_cpuid_entry2 *entry;
  264. entry = cpuid_entry2_find(entries, nent, HYPERV_CPUID_INTERFACE,
  265. KVM_CPUID_INDEX_NOT_SIGNIFICANT);
  266. return entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX;
  267. }
  268. static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
  269. {
  270. struct kvm_lapic *apic = vcpu->arch.apic;
  271. struct kvm_cpuid_entry2 *best;
  272. best = kvm_find_cpuid_entry(vcpu, 1);
  273. if (best && apic) {
  274. if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
  275. apic->lapic_timer.timer_mode_mask = 3 << 17;
  276. else
  277. apic->lapic_timer.timer_mode_mask = 1 << 17;
  278. kvm_apic_set_version(vcpu);
  279. }
  280. vcpu->arch.guest_supported_xcr0 =
  281. cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent);
  282. kvm_update_pv_runtime(vcpu);
  283. vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
  284. vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
  285. kvm_pmu_refresh(vcpu);
  286. vcpu->arch.cr4_guest_rsvd_bits =
  287. __cr4_reserved_bits(guest_cpuid_has, vcpu);
  288. kvm_hv_set_cpuid(vcpu, kvm_cpuid_has_hyperv(vcpu->arch.cpuid_entries,
  289. vcpu->arch.cpuid_nent));
  290. /* Invoke the vendor callback only after the above state is updated. */
  291. static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu);
  292. /*
  293. * Except for the MMU, which needs to do its thing any vendor specific
  294. * adjustments to the reserved GPA bits.
  295. */
  296. kvm_mmu_after_set_cpuid(vcpu);
  297. }
  298. int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
  299. {
  300. struct kvm_cpuid_entry2 *best;
  301. best = kvm_find_cpuid_entry(vcpu, 0x80000000);
  302. if (!best || best->eax < 0x80000008)
  303. goto not_found;
  304. best = kvm_find_cpuid_entry(vcpu, 0x80000008);
  305. if (best)
  306. return best->eax & 0xff;
  307. not_found:
  308. return 36;
  309. }
  310. /*
  311. * This "raw" version returns the reserved GPA bits without any adjustments for
  312. * encryption technologies that usurp bits. The raw mask should be used if and
  313. * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs.
  314. */
  315. u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
  316. {
  317. return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
  318. }
  319. static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
  320. int nent)
  321. {
  322. int r;
  323. __kvm_update_cpuid_runtime(vcpu, e2, nent);
  324. /*
  325. * KVM does not correctly handle changing guest CPUID after KVM_RUN, as
  326. * MAXPHYADDR, GBPAGES support, AMD reserved bit behavior, etc.. aren't
  327. * tracked in kvm_mmu_page_role. As a result, KVM may miss guest page
  328. * faults due to reusing SPs/SPTEs. In practice no sane VMM mucks with
  329. * the core vCPU model on the fly. It would've been better to forbid any
  330. * KVM_SET_CPUID{,2} calls after KVM_RUN altogether but unfortunately
  331. * some VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do
  332. * KVM_SET_CPUID{,2} again. To support this legacy behavior, check
  333. * whether the supplied CPUID data is equal to what's already set.
  334. */
  335. if (vcpu->arch.last_vmentry_cpu != -1) {
  336. r = kvm_cpuid_check_equal(vcpu, e2, nent);
  337. if (r)
  338. return r;
  339. kvfree(e2);
  340. return 0;
  341. }
  342. if (kvm_cpuid_has_hyperv(e2, nent)) {
  343. r = kvm_hv_vcpu_init(vcpu);
  344. if (r)
  345. return r;
  346. }
  347. r = kvm_check_cpuid(vcpu, e2, nent);
  348. if (r)
  349. return r;
  350. kvfree(vcpu->arch.cpuid_entries);
  351. vcpu->arch.cpuid_entries = e2;
  352. vcpu->arch.cpuid_nent = nent;
  353. kvm_update_kvm_cpuid_base(vcpu);
  354. kvm_vcpu_after_set_cpuid(vcpu);
  355. return 0;
  356. }
  357. /* when an old userspace process fills a new kernel module */
  358. int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  359. struct kvm_cpuid *cpuid,
  360. struct kvm_cpuid_entry __user *entries)
  361. {
  362. int r, i;
  363. struct kvm_cpuid_entry *e = NULL;
  364. struct kvm_cpuid_entry2 *e2 = NULL;
  365. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  366. return -E2BIG;
  367. if (cpuid->nent) {
  368. e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
  369. if (IS_ERR(e))
  370. return PTR_ERR(e);
  371. e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
  372. if (!e2) {
  373. r = -ENOMEM;
  374. goto out_free_cpuid;
  375. }
  376. }
  377. for (i = 0; i < cpuid->nent; i++) {
  378. e2[i].function = e[i].function;
  379. e2[i].eax = e[i].eax;
  380. e2[i].ebx = e[i].ebx;
  381. e2[i].ecx = e[i].ecx;
  382. e2[i].edx = e[i].edx;
  383. e2[i].index = 0;
  384. e2[i].flags = 0;
  385. e2[i].padding[0] = 0;
  386. e2[i].padding[1] = 0;
  387. e2[i].padding[2] = 0;
  388. }
  389. r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
  390. if (r)
  391. kvfree(e2);
  392. out_free_cpuid:
  393. kvfree(e);
  394. return r;
  395. }
  396. int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  397. struct kvm_cpuid2 *cpuid,
  398. struct kvm_cpuid_entry2 __user *entries)
  399. {
  400. struct kvm_cpuid_entry2 *e2 = NULL;
  401. int r;
  402. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  403. return -E2BIG;
  404. if (cpuid->nent) {
  405. e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
  406. if (IS_ERR(e2))
  407. return PTR_ERR(e2);
  408. }
  409. r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
  410. if (r)
  411. kvfree(e2);
  412. return r;
  413. }
  414. int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  415. struct kvm_cpuid2 *cpuid,
  416. struct kvm_cpuid_entry2 __user *entries)
  417. {
  418. int r;
  419. r = -E2BIG;
  420. if (cpuid->nent < vcpu->arch.cpuid_nent)
  421. goto out;
  422. r = -EFAULT;
  423. if (copy_to_user(entries, vcpu->arch.cpuid_entries,
  424. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  425. goto out;
  426. return 0;
  427. out:
  428. cpuid->nent = vcpu->arch.cpuid_nent;
  429. return r;
  430. }
  431. /* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */
  432. static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
  433. {
  434. const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
  435. struct kvm_cpuid_entry2 entry;
  436. reverse_cpuid_check(leaf);
  437. cpuid_count(cpuid.function, cpuid.index,
  438. &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
  439. kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
  440. }
  441. static __always_inline
  442. void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask)
  443. {
  444. /* Use kvm_cpu_cap_mask for non-scattered leafs. */
  445. BUILD_BUG_ON(leaf < NCAPINTS);
  446. kvm_cpu_caps[leaf] = mask;
  447. __kvm_cpu_cap_mask(leaf);
  448. }
  449. static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
  450. {
  451. /* Use kvm_cpu_cap_init_scattered for scattered leafs. */
  452. BUILD_BUG_ON(leaf >= NCAPINTS);
  453. kvm_cpu_caps[leaf] &= mask;
  454. __kvm_cpu_cap_mask(leaf);
  455. }
  456. void kvm_set_cpu_caps(void)
  457. {
  458. #ifdef CONFIG_X86_64
  459. unsigned int f_gbpages = F(GBPAGES);
  460. unsigned int f_lm = F(LM);
  461. unsigned int f_xfd = F(XFD);
  462. #else
  463. unsigned int f_gbpages = 0;
  464. unsigned int f_lm = 0;
  465. unsigned int f_xfd = 0;
  466. #endif
  467. memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
  468. BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
  469. sizeof(boot_cpu_data.x86_capability));
  470. memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
  471. sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
  472. kvm_cpu_cap_mask(CPUID_1_ECX,
  473. /*
  474. * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
  475. * advertised to guests via CPUID!
  476. */
  477. F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
  478. 0 /* DS-CPL, VMX, SMX, EST */ |
  479. 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
  480. F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
  481. F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
  482. F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
  483. 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
  484. F(F16C) | F(RDRAND)
  485. );
  486. /* KVM emulates x2apic in software irrespective of host support. */
  487. kvm_cpu_cap_set(X86_FEATURE_X2APIC);
  488. kvm_cpu_cap_mask(CPUID_1_EDX,
  489. F(FPU) | F(VME) | F(DE) | F(PSE) |
  490. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  491. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
  492. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  493. F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
  494. 0 /* Reserved, DS, ACPI */ | F(MMX) |
  495. F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
  496. 0 /* HTT, TM, Reserved, PBE */
  497. );
  498. kvm_cpu_cap_mask(CPUID_7_0_EBX,
  499. F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) |
  500. F(FDP_EXCPTN_ONLY) | F(SMEP) | F(BMI2) | F(ERMS) | F(INVPCID) |
  501. F(RTM) | F(ZERO_FCS_FDS) | 0 /*MPX*/ | F(AVX512F) |
  502. F(AVX512DQ) | F(RDSEED) | F(ADX) | F(SMAP) | F(AVX512IFMA) |
  503. F(CLFLUSHOPT) | F(CLWB) | 0 /*INTEL_PT*/ | F(AVX512PF) |
  504. F(AVX512ER) | F(AVX512CD) | F(SHA_NI) | F(AVX512BW) |
  505. F(AVX512VL));
  506. kvm_cpu_cap_mask(CPUID_7_ECX,
  507. F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
  508. F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
  509. F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
  510. F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
  511. F(SGX_LC) | F(BUS_LOCK_DETECT)
  512. );
  513. /* Set LA57 based on hardware capability. */
  514. if (cpuid_ecx(7) & F(LA57))
  515. kvm_cpu_cap_set(X86_FEATURE_LA57);
  516. /*
  517. * PKU not yet implemented for shadow paging and requires OSPKE
  518. * to be set on the host. Clear it if that is not the case
  519. */
  520. if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
  521. kvm_cpu_cap_clear(X86_FEATURE_PKU);
  522. kvm_cpu_cap_mask(CPUID_7_EDX,
  523. F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
  524. F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
  525. F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
  526. F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16) |
  527. F(AMX_TILE) | F(AMX_INT8) | F(AMX_BF16)
  528. );
  529. /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
  530. kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
  531. kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
  532. if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
  533. kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
  534. if (boot_cpu_has(X86_FEATURE_STIBP))
  535. kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
  536. if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
  537. kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
  538. kvm_cpu_cap_mask(CPUID_7_1_EAX,
  539. F(AVX_VNNI) | F(AVX512_BF16)
  540. );
  541. kvm_cpu_cap_mask(CPUID_D_1_EAX,
  542. F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd
  543. );
  544. kvm_cpu_cap_init_scattered(CPUID_12_EAX,
  545. SF(SGX1) | SF(SGX2)
  546. );
  547. kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
  548. F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
  549. F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
  550. F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
  551. 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
  552. F(TOPOEXT) | 0 /* PERFCTR_CORE */
  553. );
  554. kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
  555. F(FPU) | F(VME) | F(DE) | F(PSE) |
  556. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  557. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
  558. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  559. F(PAT) | F(PSE36) | 0 /* Reserved */ |
  560. F(NX) | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
  561. F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
  562. 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
  563. );
  564. if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
  565. kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
  566. kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
  567. F(CLZERO) | F(XSAVEERPTR) |
  568. F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
  569. F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON) |
  570. __feature_bit(KVM_X86_FEATURE_PSFD)
  571. );
  572. /*
  573. * AMD has separate bits for each SPEC_CTRL bit.
  574. * arch/x86/kernel/cpu/bugs.c is kind enough to
  575. * record that in cpufeatures so use them.
  576. */
  577. if (boot_cpu_has(X86_FEATURE_IBPB))
  578. kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
  579. if (boot_cpu_has(X86_FEATURE_IBRS))
  580. kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
  581. if (boot_cpu_has(X86_FEATURE_STIBP))
  582. kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
  583. if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
  584. kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
  585. if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
  586. kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
  587. /*
  588. * The preference is to use SPEC CTRL MSR instead of the
  589. * VIRT_SPEC MSR.
  590. */
  591. if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
  592. !boot_cpu_has(X86_FEATURE_AMD_SSBD))
  593. kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
  594. /*
  595. * Hide all SVM features by default, SVM will set the cap bits for
  596. * features it emulates and/or exposes for L1.
  597. */
  598. kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
  599. kvm_cpu_cap_mask(CPUID_8000_001F_EAX,
  600. 0 /* SME */ | F(SEV) | 0 /* VM_PAGE_FLUSH */ | F(SEV_ES) |
  601. F(SME_COHERENT));
  602. kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
  603. F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
  604. F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
  605. F(PMM) | F(PMM_EN)
  606. );
  607. if (cpu_feature_enabled(X86_FEATURE_SRSO_NO))
  608. kvm_cpu_cap_set(X86_FEATURE_SRSO_NO);
  609. /*
  610. * Hide RDTSCP and RDPID if either feature is reported as supported but
  611. * probing MSR_TSC_AUX failed. This is purely a sanity check and
  612. * should never happen, but the guest will likely crash if RDTSCP or
  613. * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in
  614. * the past. For example, the sanity check may fire if this instance of
  615. * KVM is running as L1 on top of an older, broken KVM.
  616. */
  617. if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) ||
  618. kvm_cpu_cap_has(X86_FEATURE_RDPID)) &&
  619. !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) {
  620. kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
  621. kvm_cpu_cap_clear(X86_FEATURE_RDPID);
  622. }
  623. }
  624. EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
  625. struct kvm_cpuid_array {
  626. struct kvm_cpuid_entry2 *entries;
  627. int maxnent;
  628. int nent;
  629. };
  630. static struct kvm_cpuid_entry2 *get_next_cpuid(struct kvm_cpuid_array *array)
  631. {
  632. if (array->nent >= array->maxnent)
  633. return NULL;
  634. return &array->entries[array->nent++];
  635. }
  636. static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
  637. u32 function, u32 index)
  638. {
  639. struct kvm_cpuid_entry2 *entry = get_next_cpuid(array);
  640. if (!entry)
  641. return NULL;
  642. memset(entry, 0, sizeof(*entry));
  643. entry->function = function;
  644. entry->index = index;
  645. switch (function & 0xC0000000) {
  646. case 0x40000000:
  647. /* Hypervisor leaves are always synthesized by __do_cpuid_func. */
  648. return entry;
  649. case 0x80000000:
  650. /*
  651. * 0x80000021 is sometimes synthesized by __do_cpuid_func, which
  652. * would result in out-of-bounds calls to do_host_cpuid.
  653. */
  654. {
  655. static int max_cpuid_80000000;
  656. if (!READ_ONCE(max_cpuid_80000000))
  657. WRITE_ONCE(max_cpuid_80000000, cpuid_eax(0x80000000));
  658. if (function > READ_ONCE(max_cpuid_80000000))
  659. return entry;
  660. }
  661. break;
  662. default:
  663. break;
  664. }
  665. cpuid_count(entry->function, entry->index,
  666. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  667. if (cpuid_function_is_indexed(function))
  668. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  669. return entry;
  670. }
  671. static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
  672. {
  673. struct kvm_cpuid_entry2 *entry;
  674. if (array->nent >= array->maxnent)
  675. return -E2BIG;
  676. entry = &array->entries[array->nent];
  677. entry->function = func;
  678. entry->index = 0;
  679. entry->flags = 0;
  680. switch (func) {
  681. case 0:
  682. entry->eax = 7;
  683. ++array->nent;
  684. break;
  685. case 1:
  686. entry->ecx = F(MOVBE);
  687. ++array->nent;
  688. break;
  689. case 7:
  690. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  691. entry->eax = 0;
  692. if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
  693. entry->ecx = F(RDPID);
  694. ++array->nent;
  695. break;
  696. default:
  697. break;
  698. }
  699. return 0;
  700. }
  701. static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
  702. {
  703. struct kvm_cpuid_entry2 *entry;
  704. int r, i, max_idx;
  705. /* all calls to cpuid_count() should be made on the same cpu */
  706. get_cpu();
  707. r = -E2BIG;
  708. entry = do_host_cpuid(array, function, 0);
  709. if (!entry)
  710. goto out;
  711. switch (function) {
  712. case 0:
  713. /* Limited to the highest leaf implemented in KVM. */
  714. entry->eax = min(entry->eax, 0x1fU);
  715. break;
  716. case 1:
  717. cpuid_entry_override(entry, CPUID_1_EDX);
  718. cpuid_entry_override(entry, CPUID_1_ECX);
  719. break;
  720. case 2:
  721. /*
  722. * On ancient CPUs, function 2 entries are STATEFUL. That is,
  723. * CPUID(function=2, index=0) may return different results each
  724. * time, with the least-significant byte in EAX enumerating the
  725. * number of times software should do CPUID(2, 0).
  726. *
  727. * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
  728. * idiotic. Intel's SDM states that EAX & 0xff "will always
  729. * return 01H. Software should ignore this value and not
  730. * interpret it as an informational descriptor", while AMD's
  731. * APM states that CPUID(2) is reserved.
  732. *
  733. * WARN if a frankenstein CPU that supports virtualization and
  734. * a stateful CPUID.0x2 is encountered.
  735. */
  736. WARN_ON_ONCE((entry->eax & 0xff) > 1);
  737. break;
  738. /* functions 4 and 0x8000001d have additional index. */
  739. case 4:
  740. case 0x8000001d:
  741. /*
  742. * Read entries until the cache type in the previous entry is
  743. * zero, i.e. indicates an invalid entry.
  744. */
  745. for (i = 1; entry->eax & 0x1f; ++i) {
  746. entry = do_host_cpuid(array, function, i);
  747. if (!entry)
  748. goto out;
  749. }
  750. break;
  751. case 6: /* Thermal management */
  752. entry->eax = 0x4; /* allow ARAT */
  753. entry->ebx = 0;
  754. entry->ecx = 0;
  755. entry->edx = 0;
  756. break;
  757. /* function 7 has additional index. */
  758. case 7:
  759. entry->eax = min(entry->eax, 1u);
  760. cpuid_entry_override(entry, CPUID_7_0_EBX);
  761. cpuid_entry_override(entry, CPUID_7_ECX);
  762. cpuid_entry_override(entry, CPUID_7_EDX);
  763. /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
  764. if (entry->eax == 1) {
  765. entry = do_host_cpuid(array, function, 1);
  766. if (!entry)
  767. goto out;
  768. cpuid_entry_override(entry, CPUID_7_1_EAX);
  769. entry->ebx = 0;
  770. entry->ecx = 0;
  771. entry->edx = 0;
  772. }
  773. break;
  774. case 0xa: { /* Architectural Performance Monitoring */
  775. union cpuid10_eax eax;
  776. union cpuid10_edx edx;
  777. if (!static_cpu_has(X86_FEATURE_ARCH_PERFMON)) {
  778. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  779. break;
  780. }
  781. eax.split.version_id = kvm_pmu_cap.version;
  782. eax.split.num_counters = kvm_pmu_cap.num_counters_gp;
  783. eax.split.bit_width = kvm_pmu_cap.bit_width_gp;
  784. eax.split.mask_length = kvm_pmu_cap.events_mask_len;
  785. edx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed;
  786. edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
  787. if (kvm_pmu_cap.version)
  788. edx.split.anythread_deprecated = 1;
  789. edx.split.reserved1 = 0;
  790. edx.split.reserved2 = 0;
  791. entry->eax = eax.full;
  792. entry->ebx = kvm_pmu_cap.events_mask;
  793. entry->ecx = 0;
  794. entry->edx = edx.full;
  795. break;
  796. }
  797. case 0x1f:
  798. case 0xb:
  799. /*
  800. * No topology; a valid topology is indicated by the presence
  801. * of subleaf 1.
  802. */
  803. entry->eax = entry->ebx = entry->ecx = 0;
  804. break;
  805. case 0xd: {
  806. u64 permitted_xcr0 = kvm_caps.supported_xcr0 & xstate_get_guest_group_perm();
  807. u64 permitted_xss = kvm_caps.supported_xss;
  808. entry->eax &= permitted_xcr0;
  809. entry->ebx = xstate_required_size(permitted_xcr0, false);
  810. entry->ecx = entry->ebx;
  811. entry->edx &= permitted_xcr0 >> 32;
  812. if (!permitted_xcr0)
  813. break;
  814. entry = do_host_cpuid(array, function, 1);
  815. if (!entry)
  816. goto out;
  817. cpuid_entry_override(entry, CPUID_D_1_EAX);
  818. if (entry->eax & (F(XSAVES)|F(XSAVEC)))
  819. entry->ebx = xstate_required_size(permitted_xcr0 | permitted_xss,
  820. true);
  821. else {
  822. WARN_ON_ONCE(permitted_xss != 0);
  823. entry->ebx = 0;
  824. }
  825. entry->ecx &= permitted_xss;
  826. entry->edx &= permitted_xss >> 32;
  827. for (i = 2; i < 64; ++i) {
  828. bool s_state;
  829. if (permitted_xcr0 & BIT_ULL(i))
  830. s_state = false;
  831. else if (permitted_xss & BIT_ULL(i))
  832. s_state = true;
  833. else
  834. continue;
  835. entry = do_host_cpuid(array, function, i);
  836. if (!entry)
  837. goto out;
  838. /*
  839. * The supported check above should have filtered out
  840. * invalid sub-leafs. Only valid sub-leafs should
  841. * reach this point, and they should have a non-zero
  842. * save state size. Furthermore, check whether the
  843. * processor agrees with permitted_xcr0/permitted_xss
  844. * on whether this is an XCR0- or IA32_XSS-managed area.
  845. */
  846. if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
  847. --array->nent;
  848. continue;
  849. }
  850. if (!kvm_cpu_cap_has(X86_FEATURE_XFD))
  851. entry->ecx &= ~BIT_ULL(2);
  852. entry->edx = 0;
  853. }
  854. break;
  855. }
  856. case 0x12:
  857. /* Intel SGX */
  858. if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) {
  859. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  860. break;
  861. }
  862. /*
  863. * Index 0: Sub-features, MISCSELECT (a.k.a extended features)
  864. * and max enclave sizes. The SGX sub-features and MISCSELECT
  865. * are restricted by kernel and KVM capabilities (like most
  866. * feature flags), while enclave size is unrestricted.
  867. */
  868. cpuid_entry_override(entry, CPUID_12_EAX);
  869. entry->ebx &= SGX_MISC_EXINFO;
  870. entry = do_host_cpuid(array, function, 1);
  871. if (!entry)
  872. goto out;
  873. /*
  874. * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la
  875. * feature flags. Advertise all supported flags, including
  876. * privileged attributes that require explicit opt-in from
  877. * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is
  878. * expected to derive it from supported XCR0.
  879. */
  880. entry->eax &= SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT |
  881. SGX_ATTR_PROVISIONKEY | SGX_ATTR_EINITTOKENKEY |
  882. SGX_ATTR_KSS;
  883. entry->ebx &= 0;
  884. break;
  885. /* Intel PT */
  886. case 0x14:
  887. if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
  888. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  889. break;
  890. }
  891. for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
  892. if (!do_host_cpuid(array, function, i))
  893. goto out;
  894. }
  895. break;
  896. /* Intel AMX TILE */
  897. case 0x1d:
  898. if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
  899. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  900. break;
  901. }
  902. for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
  903. if (!do_host_cpuid(array, function, i))
  904. goto out;
  905. }
  906. break;
  907. case 0x1e: /* TMUL information */
  908. if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
  909. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  910. break;
  911. }
  912. break;
  913. case KVM_CPUID_SIGNATURE: {
  914. const u32 *sigptr = (const u32 *)KVM_SIGNATURE;
  915. entry->eax = KVM_CPUID_FEATURES;
  916. entry->ebx = sigptr[0];
  917. entry->ecx = sigptr[1];
  918. entry->edx = sigptr[2];
  919. break;
  920. }
  921. case KVM_CPUID_FEATURES:
  922. entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
  923. (1 << KVM_FEATURE_NOP_IO_DELAY) |
  924. (1 << KVM_FEATURE_CLOCKSOURCE2) |
  925. (1 << KVM_FEATURE_ASYNC_PF) |
  926. (1 << KVM_FEATURE_PV_EOI) |
  927. (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
  928. (1 << KVM_FEATURE_PV_UNHALT) |
  929. (1 << KVM_FEATURE_PV_TLB_FLUSH) |
  930. (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
  931. (1 << KVM_FEATURE_PV_SEND_IPI) |
  932. (1 << KVM_FEATURE_POLL_CONTROL) |
  933. (1 << KVM_FEATURE_PV_SCHED_YIELD) |
  934. (1 << KVM_FEATURE_ASYNC_PF_INT);
  935. if (sched_info_on())
  936. entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
  937. entry->ebx = 0;
  938. entry->ecx = 0;
  939. entry->edx = 0;
  940. break;
  941. case 0x80000000:
  942. entry->eax = min(entry->eax, 0x80000021);
  943. /*
  944. * Serializing LFENCE is reported in a multitude of ways, and
  945. * NullSegClearsBase is not reported in CPUID on Zen2; help
  946. * userspace by providing the CPUID leaf ourselves.
  947. *
  948. * However, only do it if the host has CPUID leaf 0x8000001d.
  949. * QEMU thinks that it can query the host blindly for that
  950. * CPUID leaf if KVM reports that it supports 0x8000001d or
  951. * above. The processor merrily returns values from the
  952. * highest Intel leaf which QEMU tries to use as the guest's
  953. * 0x8000001d. Even worse, this can result in an infinite
  954. * loop if said highest leaf has no subleaves indexed by ECX.
  955. */
  956. if (entry->eax >= 0x8000001d &&
  957. (static_cpu_has(X86_FEATURE_LFENCE_RDTSC)
  958. || !static_cpu_has_bug(X86_BUG_NULL_SEG)))
  959. entry->eax = max(entry->eax, 0x80000021);
  960. break;
  961. case 0x80000001:
  962. entry->ebx &= ~GENMASK(27, 16);
  963. cpuid_entry_override(entry, CPUID_8000_0001_EDX);
  964. cpuid_entry_override(entry, CPUID_8000_0001_ECX);
  965. break;
  966. case 0x80000006:
  967. /* Drop reserved bits, pass host L2 cache and TLB info. */
  968. entry->edx &= ~GENMASK(17, 16);
  969. break;
  970. case 0x80000007: /* Advanced power management */
  971. /* invariant TSC is CPUID.80000007H:EDX[8] */
  972. entry->edx &= (1 << 8);
  973. /* mask against host */
  974. entry->edx &= boot_cpu_data.x86_power;
  975. entry->eax = entry->ebx = entry->ecx = 0;
  976. break;
  977. case 0x80000008: {
  978. unsigned g_phys_as = (entry->eax >> 16) & 0xff;
  979. unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
  980. unsigned phys_as = entry->eax & 0xff;
  981. /*
  982. * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as
  983. * the guest operates in the same PA space as the host, i.e.
  984. * reductions in MAXPHYADDR for memory encryption affect shadow
  985. * paging, too.
  986. *
  987. * If TDP is enabled but an explicit guest MAXPHYADDR is not
  988. * provided, use the raw bare metal MAXPHYADDR as reductions to
  989. * the HPAs do not affect GPAs.
  990. */
  991. if (!tdp_enabled)
  992. g_phys_as = boot_cpu_data.x86_phys_bits;
  993. else if (!g_phys_as)
  994. g_phys_as = phys_as;
  995. entry->eax = g_phys_as | (virt_as << 8);
  996. entry->ecx &= ~(GENMASK(31, 16) | GENMASK(11, 8));
  997. entry->edx = 0;
  998. cpuid_entry_override(entry, CPUID_8000_0008_EBX);
  999. break;
  1000. }
  1001. case 0x8000000A:
  1002. if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
  1003. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  1004. break;
  1005. }
  1006. entry->eax = 1; /* SVM revision 1 */
  1007. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  1008. ASID emulation to nested SVM */
  1009. entry->ecx = 0; /* Reserved */
  1010. cpuid_entry_override(entry, CPUID_8000_000A_EDX);
  1011. break;
  1012. case 0x80000019:
  1013. entry->ecx = entry->edx = 0;
  1014. break;
  1015. case 0x8000001a:
  1016. entry->eax &= GENMASK(2, 0);
  1017. entry->ebx = entry->ecx = entry->edx = 0;
  1018. break;
  1019. case 0x8000001e:
  1020. /* Do not return host topology information. */
  1021. entry->eax = entry->ebx = entry->ecx = 0;
  1022. entry->edx = 0; /* reserved */
  1023. break;
  1024. case 0x8000001F:
  1025. if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) {
  1026. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  1027. } else {
  1028. cpuid_entry_override(entry, CPUID_8000_001F_EAX);
  1029. /* Clear NumVMPL since KVM does not support VMPL. */
  1030. entry->ebx &= ~GENMASK(31, 12);
  1031. /*
  1032. * Enumerate '0' for "PA bits reduction", the adjusted
  1033. * MAXPHYADDR is enumerated directly (see 0x80000008).
  1034. */
  1035. entry->ebx &= ~GENMASK(11, 6);
  1036. }
  1037. break;
  1038. case 0x80000020:
  1039. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  1040. break;
  1041. case 0x80000021:
  1042. entry->ebx = entry->ecx = entry->edx = 0;
  1043. /*
  1044. * Pass down these bits:
  1045. * EAX 0 NNDBP, Processor ignores nested data breakpoints
  1046. * EAX 2 LAS, LFENCE always serializing
  1047. * EAX 6 NSCB, Null selector clear base
  1048. *
  1049. * Other defined bits are for MSRs that KVM does not expose:
  1050. * EAX 3 SPCL, SMM page configuration lock
  1051. * EAX 13 PCMSR, Prefetch control MSR
  1052. */
  1053. entry->eax &= BIT(0) | BIT(2) | BIT(6);
  1054. if (static_cpu_has(X86_FEATURE_LFENCE_RDTSC))
  1055. entry->eax |= BIT(2);
  1056. if (!static_cpu_has_bug(X86_BUG_NULL_SEG))
  1057. entry->eax |= BIT(6);
  1058. break;
  1059. /*Add support for Centaur's CPUID instruction*/
  1060. case 0xC0000000:
  1061. /*Just support up to 0xC0000004 now*/
  1062. entry->eax = min(entry->eax, 0xC0000004);
  1063. break;
  1064. case 0xC0000001:
  1065. cpuid_entry_override(entry, CPUID_C000_0001_EDX);
  1066. break;
  1067. case 3: /* Processor serial number */
  1068. case 5: /* MONITOR/MWAIT */
  1069. case 0xC0000002:
  1070. case 0xC0000003:
  1071. case 0xC0000004:
  1072. default:
  1073. entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
  1074. break;
  1075. }
  1076. r = 0;
  1077. out:
  1078. put_cpu();
  1079. return r;
  1080. }
  1081. static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
  1082. unsigned int type)
  1083. {
  1084. if (type == KVM_GET_EMULATED_CPUID)
  1085. return __do_cpuid_func_emulated(array, func);
  1086. return __do_cpuid_func(array, func);
  1087. }
  1088. #define CENTAUR_CPUID_SIGNATURE 0xC0000000
  1089. static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
  1090. unsigned int type)
  1091. {
  1092. u32 limit;
  1093. int r;
  1094. if (func == CENTAUR_CPUID_SIGNATURE &&
  1095. boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
  1096. return 0;
  1097. r = do_cpuid_func(array, func, type);
  1098. if (r)
  1099. return r;
  1100. limit = array->entries[array->nent - 1].eax;
  1101. for (func = func + 1; func <= limit; ++func) {
  1102. r = do_cpuid_func(array, func, type);
  1103. if (r)
  1104. break;
  1105. }
  1106. return r;
  1107. }
  1108. static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
  1109. __u32 num_entries, unsigned int ioctl_type)
  1110. {
  1111. int i;
  1112. __u32 pad[3];
  1113. if (ioctl_type != KVM_GET_EMULATED_CPUID)
  1114. return false;
  1115. /*
  1116. * We want to make sure that ->padding is being passed clean from
  1117. * userspace in case we want to use it for something in the future.
  1118. *
  1119. * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
  1120. * have to give ourselves satisfied only with the emulated side. /me
  1121. * sheds a tear.
  1122. */
  1123. for (i = 0; i < num_entries; i++) {
  1124. if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
  1125. return true;
  1126. if (pad[0] || pad[1] || pad[2])
  1127. return true;
  1128. }
  1129. return false;
  1130. }
  1131. int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
  1132. struct kvm_cpuid_entry2 __user *entries,
  1133. unsigned int type)
  1134. {
  1135. static const u32 funcs[] = {
  1136. 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
  1137. };
  1138. struct kvm_cpuid_array array = {
  1139. .nent = 0,
  1140. };
  1141. int r, i;
  1142. if (cpuid->nent < 1)
  1143. return -E2BIG;
  1144. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1145. cpuid->nent = KVM_MAX_CPUID_ENTRIES;
  1146. if (sanity_check_entries(entries, cpuid->nent, type))
  1147. return -EINVAL;
  1148. array.entries = kvcalloc(cpuid->nent, sizeof(struct kvm_cpuid_entry2), GFP_KERNEL);
  1149. if (!array.entries)
  1150. return -ENOMEM;
  1151. array.maxnent = cpuid->nent;
  1152. for (i = 0; i < ARRAY_SIZE(funcs); i++) {
  1153. r = get_cpuid_func(&array, funcs[i], type);
  1154. if (r)
  1155. goto out_free;
  1156. }
  1157. cpuid->nent = array.nent;
  1158. if (copy_to_user(entries, array.entries,
  1159. array.nent * sizeof(struct kvm_cpuid_entry2)))
  1160. r = -EFAULT;
  1161. out_free:
  1162. kvfree(array.entries);
  1163. return r;
  1164. }
  1165. struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu,
  1166. u32 function, u32 index)
  1167. {
  1168. return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
  1169. function, index);
  1170. }
  1171. EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry_index);
  1172. struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
  1173. u32 function)
  1174. {
  1175. return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
  1176. function, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
  1177. }
  1178. EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
  1179. /*
  1180. * Intel CPUID semantics treats any query for an out-of-range leaf as if the
  1181. * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
  1182. * returns all zeroes for any undefined leaf, whether or not the leaf is in
  1183. * range. Centaur/VIA follows Intel semantics.
  1184. *
  1185. * A leaf is considered out-of-range if its function is higher than the maximum
  1186. * supported leaf of its associated class or if its associated class does not
  1187. * exist.
  1188. *
  1189. * There are three primary classes to be considered, with their respective
  1190. * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
  1191. * class exists if a guest CPUID entry for its <base> leaf exists. For a given
  1192. * class, CPUID.<base>.EAX contains the max supported leaf for the class.
  1193. *
  1194. * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
  1195. * - Hypervisor: 0x40000000 - 0x4fffffff
  1196. * - Extended: 0x80000000 - 0xbfffffff
  1197. * - Centaur: 0xc0000000 - 0xcfffffff
  1198. *
  1199. * The Hypervisor class is further subdivided into sub-classes that each act as
  1200. * their own independent class associated with a 0x100 byte range. E.g. if Qemu
  1201. * is advertising support for both HyperV and KVM, the resulting Hypervisor
  1202. * CPUID sub-classes are:
  1203. *
  1204. * - HyperV: 0x40000000 - 0x400000ff
  1205. * - KVM: 0x40000100 - 0x400001ff
  1206. */
  1207. static struct kvm_cpuid_entry2 *
  1208. get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
  1209. {
  1210. struct kvm_cpuid_entry2 *basic, *class;
  1211. u32 function = *fn_ptr;
  1212. basic = kvm_find_cpuid_entry(vcpu, 0);
  1213. if (!basic)
  1214. return NULL;
  1215. if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
  1216. is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
  1217. return NULL;
  1218. if (function >= 0x40000000 && function <= 0x4fffffff)
  1219. class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00);
  1220. else if (function >= 0xc0000000)
  1221. class = kvm_find_cpuid_entry(vcpu, 0xc0000000);
  1222. else
  1223. class = kvm_find_cpuid_entry(vcpu, function & 0x80000000);
  1224. if (class && function <= class->eax)
  1225. return NULL;
  1226. /*
  1227. * Leaf specific adjustments are also applied when redirecting to the
  1228. * max basic entry, e.g. if the max basic leaf is 0xb but there is no
  1229. * entry for CPUID.0xb.index (see below), then the output value for EDX
  1230. * needs to be pulled from CPUID.0xb.1.
  1231. */
  1232. *fn_ptr = basic->eax;
  1233. /*
  1234. * The class does not exist or the requested function is out of range;
  1235. * the effective CPUID entry is the max basic leaf. Note, the index of
  1236. * the original requested leaf is observed!
  1237. */
  1238. return kvm_find_cpuid_entry_index(vcpu, basic->eax, index);
  1239. }
  1240. bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
  1241. u32 *ecx, u32 *edx, bool exact_only)
  1242. {
  1243. u32 orig_function = *eax, function = *eax, index = *ecx;
  1244. struct kvm_cpuid_entry2 *entry;
  1245. bool exact, used_max_basic = false;
  1246. entry = kvm_find_cpuid_entry_index(vcpu, function, index);
  1247. exact = !!entry;
  1248. if (!entry && !exact_only) {
  1249. entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
  1250. used_max_basic = !!entry;
  1251. }
  1252. if (entry) {
  1253. *eax = entry->eax;
  1254. *ebx = entry->ebx;
  1255. *ecx = entry->ecx;
  1256. *edx = entry->edx;
  1257. if (function == 7 && index == 0) {
  1258. u64 data;
  1259. if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
  1260. (data & TSX_CTRL_CPUID_CLEAR))
  1261. *ebx &= ~(F(RTM) | F(HLE));
  1262. }
  1263. } else {
  1264. *eax = *ebx = *ecx = *edx = 0;
  1265. /*
  1266. * When leaf 0BH or 1FH is defined, CL is pass-through
  1267. * and EDX is always the x2APIC ID, even for undefined
  1268. * subleaves. Index 1 will exist iff the leaf is
  1269. * implemented, so we pass through CL iff leaf 1
  1270. * exists. EDX can be copied from any existing index.
  1271. */
  1272. if (function == 0xb || function == 0x1f) {
  1273. entry = kvm_find_cpuid_entry_index(vcpu, function, 1);
  1274. if (entry) {
  1275. *ecx = index & 0xff;
  1276. *edx = entry->edx;
  1277. }
  1278. }
  1279. }
  1280. trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
  1281. used_max_basic);
  1282. return exact;
  1283. }
  1284. EXPORT_SYMBOL_GPL(kvm_cpuid);
  1285. int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  1286. {
  1287. u32 eax, ebx, ecx, edx;
  1288. if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
  1289. return 1;
  1290. eax = kvm_rax_read(vcpu);
  1291. ecx = kvm_rcx_read(vcpu);
  1292. kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
  1293. kvm_rax_write(vcpu, eax);
  1294. kvm_rbx_write(vcpu, ebx);
  1295. kvm_rcx_write(vcpu, ecx);
  1296. kvm_rdx_write(vcpu, edx);
  1297. return kvm_skip_emulated_instruction(vcpu);
  1298. }
  1299. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);