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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/kvm_host.h> 14 #include "linux/lockdep.h" 15 #include <linux/export.h> 16 #include <linux/vmalloc.h> 17 #include <linux/uaccess.h> 18 #include <linux/sched/stat.h> 19 20 #include <asm/processor.h> 21 #include <asm/user.h> 22 #include <asm/fpu/xstate.h> 23 #include <asm/sgx.h> 24 #include <asm/cpuid/api.h> 25 #include "cpuid.h" 26 #include "lapic.h" 27 #include "mmu.h" 28 #include "trace.h" 29 #include "pmu.h" 30 #include "xen.h" 31 32 /* 33 * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be 34 * aligned to sizeof(unsigned long) because it's not accessed via bitops. 35 */ 36 u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly; 37 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_cpu_caps); 38 39 struct cpuid_xstate_sizes { 40 u32 eax; 41 u32 ebx; 42 u32 ecx; 43 }; 44 45 static struct cpuid_xstate_sizes xstate_sizes[XFEATURE_MAX] __ro_after_init; 46 47 void __init kvm_init_xstate_sizes(void) 48 { 49 u32 ign; 50 int i; 51 52 for (i = XFEATURE_YMM; i < ARRAY_SIZE(xstate_sizes); i++) { 53 struct cpuid_xstate_sizes *xs = &xstate_sizes[i]; 54 55 cpuid_count(0xD, i, &xs->eax, &xs->ebx, &xs->ecx, &ign); 56 } 57 } 58 59 u32 xstate_required_size(u64 xstate_bv, bool compacted) 60 { 61 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; 62 int i; 63 64 xstate_bv &= XFEATURE_MASK_EXTEND; 65 for (i = XFEATURE_YMM; i < ARRAY_SIZE(xstate_sizes) && xstate_bv; i++) { 66 struct cpuid_xstate_sizes *xs = &xstate_sizes[i]; 67 u32 offset; 68 69 if (!(xstate_bv & BIT_ULL(i))) 70 continue; 71 72 /* ECX[1]: 64B alignment in compacted form */ 73 if (compacted) 74 offset = (xs->ecx & 0x2) ? ALIGN(ret, 64) : ret; 75 else 76 offset = xs->ebx; 77 ret = max(ret, offset + xs->eax); 78 xstate_bv &= ~BIT_ULL(i); 79 } 80 81 return ret; 82 } 83 84 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2( 85 struct kvm_cpuid_entry2 *entries, int nent, u32 function, u64 index) 86 { 87 struct kvm_cpuid_entry2 *e; 88 int i; 89 90 /* 91 * KVM has a semi-arbitrary rule that querying the guest's CPUID model 92 * with IRQs disabled is disallowed. The CPUID model can legitimately 93 * have over one hundred entries, i.e. the lookup is slow, and IRQs are 94 * typically disabled in KVM only when KVM is in a performance critical 95 * path, e.g. the core VM-Enter/VM-Exit run loop. Nothing will break 96 * if this rule is violated, this assertion is purely to flag potential 97 * performance issues. If this fires, consider moving the lookup out 98 * of the hotpath, e.g. by caching information during CPUID updates. 99 */ 100 lockdep_assert_irqs_enabled(); 101 102 for (i = 0; i < nent; i++) { 103 e = &entries[i]; 104 105 if (e->function != function) 106 continue; 107 108 /* 109 * If the index isn't significant, use the first entry with a 110 * matching function. It's userspace's responsibility to not 111 * provide "duplicate" entries in all cases. 112 */ 113 if (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) || e->index == index) 114 return e; 115 116 117 /* 118 * Similarly, use the first matching entry if KVM is doing a 119 * lookup (as opposed to emulating CPUID) for a function that's 120 * architecturally defined as not having a significant index. 121 */ 122 if (index == KVM_CPUID_INDEX_NOT_SIGNIFICANT) { 123 /* 124 * Direct lookups from KVM should not diverge from what 125 * KVM defines internally (the architectural behavior). 126 */ 127 WARN_ON_ONCE(cpuid_function_is_indexed(function)); 128 return e; 129 } 130 } 131 132 return NULL; 133 } 134 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_find_cpuid_entry2); 135 136 static int kvm_check_cpuid(struct kvm_vcpu *vcpu) 137 { 138 struct kvm_cpuid_entry2 *best; 139 u64 xfeatures; 140 141 /* 142 * The existing code assumes virtual address is 48-bit or 57-bit in the 143 * canonical address checks; exit if it is ever changed. 144 */ 145 best = kvm_find_cpuid_entry(vcpu, 0x80000008); 146 if (best) { 147 int vaddr_bits = (best->eax & 0xff00) >> 8; 148 149 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0) 150 return -EINVAL; 151 } 152 153 /* 154 * Exposing dynamic xfeatures to the guest requires additional 155 * enabling in the FPU, e.g. to expand the guest XSAVE state size. 156 */ 157 best = kvm_find_cpuid_entry_index(vcpu, 0xd, 0); 158 if (!best) 159 return 0; 160 161 xfeatures = best->eax | ((u64)best->edx << 32); 162 xfeatures &= XFEATURE_MASK_USER_DYNAMIC; 163 if (!xfeatures) 164 return 0; 165 166 return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures); 167 } 168 169 static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu); 170 static void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu); 171 172 /* Check whether the supplied CPUID data is equal to what is already set for the vCPU. */ 173 static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, 174 int nent) 175 { 176 struct kvm_cpuid_entry2 *orig; 177 int i; 178 179 /* 180 * Apply runtime CPUID updates to the incoming CPUID entries to avoid 181 * false positives due mismatches on KVM-owned feature flags. 182 * 183 * Note! @e2 and @nent track the _old_ CPUID entries! 184 */ 185 kvm_update_cpuid_runtime(vcpu); 186 kvm_apply_cpuid_pv_features_quirk(vcpu); 187 188 if (nent != vcpu->arch.cpuid_nent) 189 return -EINVAL; 190 191 for (i = 0; i < nent; i++) { 192 orig = &vcpu->arch.cpuid_entries[i]; 193 if (e2[i].function != orig->function || 194 e2[i].index != orig->index || 195 e2[i].flags != orig->flags || 196 e2[i].eax != orig->eax || e2[i].ebx != orig->ebx || 197 e2[i].ecx != orig->ecx || e2[i].edx != orig->edx) 198 return -EINVAL; 199 } 200 201 return 0; 202 } 203 204 static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu, 205 const char *sig) 206 { 207 struct kvm_hypervisor_cpuid cpuid = {}; 208 struct kvm_cpuid_entry2 *entry; 209 u32 base; 210 211 for_each_possible_cpuid_base_hypervisor(base) { 212 entry = kvm_find_cpuid_entry(vcpu, base); 213 214 if (entry) { 215 u32 signature[3]; 216 217 signature[0] = entry->ebx; 218 signature[1] = entry->ecx; 219 signature[2] = entry->edx; 220 221 if (!memcmp(signature, sig, sizeof(signature))) { 222 cpuid.base = base; 223 cpuid.limit = entry->eax; 224 break; 225 } 226 } 227 } 228 229 return cpuid; 230 } 231 232 static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu) 233 { 234 struct kvm_hypervisor_cpuid kvm_cpuid; 235 struct kvm_cpuid_entry2 *best; 236 237 kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); 238 if (!kvm_cpuid.base) 239 return 0; 240 241 best = kvm_find_cpuid_entry(vcpu, kvm_cpuid.base | KVM_CPUID_FEATURES); 242 if (!best) 243 return 0; 244 245 if (kvm_hlt_in_guest(vcpu->kvm)) 246 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT); 247 248 return best->eax; 249 } 250 251 /* 252 * Calculate guest's supported XCR0 taking into account guest CPUID data and 253 * KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0). 254 */ 255 static u64 cpuid_get_supported_xcr0(struct kvm_vcpu *vcpu) 256 { 257 struct kvm_cpuid_entry2 *best; 258 259 best = kvm_find_cpuid_entry_index(vcpu, 0xd, 0); 260 if (!best) 261 return 0; 262 263 return (best->eax | ((u64)best->edx << 32)) & kvm_caps.supported_xcr0; 264 } 265 266 static u64 cpuid_get_supported_xss(struct kvm_vcpu *vcpu) 267 { 268 struct kvm_cpuid_entry2 *best; 269 270 best = kvm_find_cpuid_entry_index(vcpu, 0xd, 1); 271 if (!best) 272 return 0; 273 274 return (best->ecx | ((u64)best->edx << 32)) & kvm_caps.supported_xss; 275 } 276 277 static __always_inline void kvm_update_feature_runtime(struct kvm_vcpu *vcpu, 278 struct kvm_cpuid_entry2 *entry, 279 unsigned int x86_feature, 280 bool has_feature) 281 { 282 cpuid_entry_change(entry, x86_feature, has_feature); 283 guest_cpu_cap_change(vcpu, x86_feature, has_feature); 284 } 285 286 static void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) 287 { 288 struct kvm_cpuid_entry2 *best; 289 290 vcpu->arch.cpuid_dynamic_bits_dirty = false; 291 292 best = kvm_find_cpuid_entry(vcpu, 1); 293 if (best) { 294 kvm_update_feature_runtime(vcpu, best, X86_FEATURE_OSXSAVE, 295 kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)); 296 297 kvm_update_feature_runtime(vcpu, best, X86_FEATURE_APIC, 298 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE); 299 300 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) 301 kvm_update_feature_runtime(vcpu, best, X86_FEATURE_MWAIT, 302 vcpu->arch.ia32_misc_enable_msr & 303 MSR_IA32_MISC_ENABLE_MWAIT); 304 } 305 306 best = kvm_find_cpuid_entry_index(vcpu, 7, 0); 307 if (best) 308 kvm_update_feature_runtime(vcpu, best, X86_FEATURE_OSPKE, 309 kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE)); 310 311 312 best = kvm_find_cpuid_entry_index(vcpu, 0xD, 0); 313 if (best) 314 best->ebx = xstate_required_size(vcpu->arch.xcr0, false); 315 316 best = kvm_find_cpuid_entry_index(vcpu, 0xD, 1); 317 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) || 318 cpuid_entry_has(best, X86_FEATURE_XSAVEC))) 319 best->ebx = xstate_required_size(vcpu->arch.xcr0 | 320 vcpu->arch.ia32_xss, true); 321 } 322 323 static bool kvm_cpuid_has_hyperv(struct kvm_vcpu *vcpu) 324 { 325 #ifdef CONFIG_KVM_HYPERV 326 struct kvm_cpuid_entry2 *entry; 327 328 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_INTERFACE); 329 return entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX; 330 #else 331 return false; 332 #endif 333 } 334 335 static bool guest_cpuid_is_amd_or_hygon(struct kvm_vcpu *vcpu) 336 { 337 struct kvm_cpuid_entry2 *entry; 338 339 entry = kvm_find_cpuid_entry(vcpu, 0); 340 if (!entry) 341 return false; 342 343 return is_guest_vendor_amd(entry->ebx, entry->ecx, entry->edx) || 344 is_guest_vendor_hygon(entry->ebx, entry->ecx, entry->edx); 345 } 346 347 /* 348 * This isn't truly "unsafe", but except for the cpu_caps initialization code, 349 * all register lookups should use __cpuid_entry_get_reg(), which provides 350 * compile-time validation of the input. 351 */ 352 static u32 cpuid_get_reg_unsafe(struct kvm_cpuid_entry2 *entry, u32 reg) 353 { 354 switch (reg) { 355 case CPUID_EAX: 356 return entry->eax; 357 case CPUID_EBX: 358 return entry->ebx; 359 case CPUID_ECX: 360 return entry->ecx; 361 case CPUID_EDX: 362 return entry->edx; 363 default: 364 WARN_ON_ONCE(1); 365 return 0; 366 } 367 } 368 369 static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, 370 bool include_partially_emulated); 371 372 void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) 373 { 374 struct kvm_lapic *apic = vcpu->arch.apic; 375 struct kvm_cpuid_entry2 *best; 376 struct kvm_cpuid_entry2 *entry; 377 bool allow_gbpages; 378 int i; 379 380 memset(vcpu->arch.cpu_caps, 0, sizeof(vcpu->arch.cpu_caps)); 381 BUILD_BUG_ON(ARRAY_SIZE(reverse_cpuid) != NR_KVM_CPU_CAPS); 382 383 /* 384 * Reset guest capabilities to userspace's guest CPUID definition, i.e. 385 * honor userspace's definition for features that don't require KVM or 386 * hardware management/support (or that KVM simply doesn't care about). 387 */ 388 for (i = 0; i < NR_KVM_CPU_CAPS; i++) { 389 const struct cpuid_reg cpuid = reverse_cpuid[i]; 390 struct kvm_cpuid_entry2 emulated; 391 392 if (!cpuid.function) 393 continue; 394 395 entry = kvm_find_cpuid_entry_index(vcpu, cpuid.function, cpuid.index); 396 if (!entry) 397 continue; 398 399 cpuid_func_emulated(&emulated, cpuid.function, true); 400 401 /* 402 * A vCPU has a feature if it's supported by KVM and is enabled 403 * in guest CPUID. Note, this includes features that are 404 * supported by KVM but aren't advertised to userspace! 405 */ 406 vcpu->arch.cpu_caps[i] = kvm_cpu_caps[i] | 407 cpuid_get_reg_unsafe(&emulated, cpuid.reg); 408 vcpu->arch.cpu_caps[i] &= cpuid_get_reg_unsafe(entry, cpuid.reg); 409 } 410 411 kvm_update_cpuid_runtime(vcpu); 412 413 /* 414 * If TDP is enabled, let the guest use GBPAGES if they're supported in 415 * hardware. The hardware page walker doesn't let KVM disable GBPAGES, 416 * i.e. won't treat them as reserved, and KVM doesn't redo the GVA->GPA 417 * walk for performance and complexity reasons. Not to mention KVM 418 * _can't_ solve the problem because GVA->GPA walks aren't visible to 419 * KVM once a TDP translation is installed. Mimic hardware behavior so 420 * that KVM's is at least consistent, i.e. doesn't randomly inject #PF. 421 * If TDP is disabled, honor *only* guest CPUID as KVM has full control 422 * and can install smaller shadow pages if the host lacks 1GiB support. 423 */ 424 allow_gbpages = tdp_enabled ? boot_cpu_has(X86_FEATURE_GBPAGES) : 425 guest_cpu_cap_has(vcpu, X86_FEATURE_GBPAGES); 426 guest_cpu_cap_change(vcpu, X86_FEATURE_GBPAGES, allow_gbpages); 427 428 best = kvm_find_cpuid_entry(vcpu, 1); 429 if (best && apic) { 430 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER)) 431 apic->lapic_timer.timer_mode_mask = 3 << 17; 432 else 433 apic->lapic_timer.timer_mode_mask = 1 << 17; 434 435 kvm_apic_set_version(vcpu); 436 } 437 438 vcpu->arch.guest_supported_xcr0 = cpuid_get_supported_xcr0(vcpu); 439 vcpu->arch.guest_supported_xss = cpuid_get_supported_xss(vcpu); 440 441 vcpu->arch.pv_cpuid.features = kvm_apply_cpuid_pv_features_quirk(vcpu); 442 443 vcpu->arch.is_amd_compatible = guest_cpuid_is_amd_or_hygon(vcpu); 444 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); 445 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); 446 447 kvm_pmu_refresh(vcpu); 448 449 #define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f) 450 vcpu->arch.cr4_guest_rsvd_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_) | 451 __cr4_reserved_bits(guest_cpu_cap_has, vcpu); 452 #undef __kvm_cpu_cap_has 453 454 kvm_hv_set_cpuid(vcpu, kvm_cpuid_has_hyperv(vcpu)); 455 456 /* Invoke the vendor callback only after the above state is updated. */ 457 kvm_x86_call(vcpu_after_set_cpuid)(vcpu); 458 459 /* 460 * Except for the MMU, which needs to do its thing any vendor specific 461 * adjustments to the reserved GPA bits. 462 */ 463 kvm_mmu_after_set_cpuid(vcpu); 464 465 kvm_make_request(KVM_REQ_RECALC_INTERCEPTS, vcpu); 466 } 467 468 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu) 469 { 470 struct kvm_cpuid_entry2 *best; 471 472 best = kvm_find_cpuid_entry(vcpu, 0x80000000); 473 if (!best || best->eax < 0x80000008) 474 goto not_found; 475 best = kvm_find_cpuid_entry(vcpu, 0x80000008); 476 if (best) 477 return best->eax & 0xff; 478 not_found: 479 return 36; 480 } 481 482 int cpuid_query_maxguestphyaddr(struct kvm_vcpu *vcpu) 483 { 484 struct kvm_cpuid_entry2 *best; 485 486 best = kvm_find_cpuid_entry(vcpu, 0x80000000); 487 if (!best || best->eax < 0x80000008) 488 goto not_found; 489 best = kvm_find_cpuid_entry(vcpu, 0x80000008); 490 if (best) 491 return (best->eax >> 16) & 0xff; 492 not_found: 493 return 0; 494 } 495 496 /* 497 * This "raw" version returns the reserved GPA bits without any adjustments for 498 * encryption technologies that usurp bits. The raw mask should be used if and 499 * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs. 500 */ 501 u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu) 502 { 503 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63); 504 } 505 506 static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, 507 int nent) 508 { 509 u32 vcpu_caps[NR_KVM_CPU_CAPS]; 510 int r; 511 512 /* 513 * Swap the existing (old) entries with the incoming (new) entries in 514 * order to massage the new entries, e.g. to account for dynamic bits 515 * that KVM controls, without clobbering the current guest CPUID, which 516 * KVM needs to preserve in order to unwind on failure. 517 * 518 * Similarly, save the vCPU's current cpu_caps so that the capabilities 519 * can be updated alongside the CPUID entries when performing runtime 520 * updates. Full initialization is done if and only if the vCPU hasn't 521 * run, i.e. only if userspace is potentially changing CPUID features. 522 */ 523 swap(vcpu->arch.cpuid_entries, e2); 524 swap(vcpu->arch.cpuid_nent, nent); 525 526 memcpy(vcpu_caps, vcpu->arch.cpu_caps, sizeof(vcpu_caps)); 527 BUILD_BUG_ON(sizeof(vcpu_caps) != sizeof(vcpu->arch.cpu_caps)); 528 529 /* 530 * KVM does not correctly handle changing guest CPUID after KVM_RUN, as 531 * MAXPHYADDR, GBPAGES support, AMD reserved bit behavior, etc.. aren't 532 * tracked in kvm_mmu_page_role. As a result, KVM may miss guest page 533 * faults due to reusing SPs/SPTEs. In practice no sane VMM mucks with 534 * the core vCPU model on the fly. It would've been better to forbid any 535 * KVM_SET_CPUID{,2} calls after KVM_RUN altogether but unfortunately 536 * some VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do 537 * KVM_SET_CPUID{,2} again. To support this legacy behavior, check 538 * whether the supplied CPUID data is equal to what's already set. 539 */ 540 if (kvm_vcpu_has_run(vcpu)) { 541 r = kvm_cpuid_check_equal(vcpu, e2, nent); 542 if (r) 543 goto err; 544 goto success; 545 } 546 547 #ifdef CONFIG_KVM_HYPERV 548 if (kvm_cpuid_has_hyperv(vcpu)) { 549 r = kvm_hv_vcpu_init(vcpu); 550 if (r) 551 goto err; 552 } 553 #endif 554 555 r = kvm_check_cpuid(vcpu); 556 if (r) 557 goto err; 558 559 #ifdef CONFIG_KVM_XEN 560 vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE); 561 #endif 562 kvm_vcpu_after_set_cpuid(vcpu); 563 564 success: 565 kvfree(e2); 566 return 0; 567 568 err: 569 memcpy(vcpu->arch.cpu_caps, vcpu_caps, sizeof(vcpu_caps)); 570 swap(vcpu->arch.cpuid_entries, e2); 571 swap(vcpu->arch.cpuid_nent, nent); 572 return r; 573 } 574 575 /* when an old userspace process fills a new kernel module */ 576 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, 577 struct kvm_cpuid *cpuid, 578 struct kvm_cpuid_entry __user *entries) 579 { 580 int r, i; 581 struct kvm_cpuid_entry *e = NULL; 582 struct kvm_cpuid_entry2 *e2 = NULL; 583 584 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) 585 return -E2BIG; 586 587 if (cpuid->nent) { 588 e = vmemdup_array_user(entries, cpuid->nent, sizeof(*e)); 589 if (IS_ERR(e)) 590 return PTR_ERR(e); 591 592 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT); 593 if (!e2) { 594 r = -ENOMEM; 595 goto out_free_cpuid; 596 } 597 } 598 for (i = 0; i < cpuid->nent; i++) { 599 e2[i].function = e[i].function; 600 e2[i].eax = e[i].eax; 601 e2[i].ebx = e[i].ebx; 602 e2[i].ecx = e[i].ecx; 603 e2[i].edx = e[i].edx; 604 e2[i].index = 0; 605 e2[i].flags = 0; 606 e2[i].padding[0] = 0; 607 e2[i].padding[1] = 0; 608 e2[i].padding[2] = 0; 609 } 610 611 r = kvm_set_cpuid(vcpu, e2, cpuid->nent); 612 if (r) 613 kvfree(e2); 614 615 out_free_cpuid: 616 kvfree(e); 617 618 return r; 619 } 620 621 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, 622 struct kvm_cpuid2 *cpuid, 623 struct kvm_cpuid_entry2 __user *entries) 624 { 625 struct kvm_cpuid_entry2 *e2 = NULL; 626 int r; 627 628 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) 629 return -E2BIG; 630 631 if (cpuid->nent) { 632 e2 = vmemdup_array_user(entries, cpuid->nent, sizeof(*e2)); 633 if (IS_ERR(e2)) 634 return PTR_ERR(e2); 635 } 636 637 r = kvm_set_cpuid(vcpu, e2, cpuid->nent); 638 if (r) 639 kvfree(e2); 640 641 return r; 642 } 643 644 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, 645 struct kvm_cpuid2 *cpuid, 646 struct kvm_cpuid_entry2 __user *entries) 647 { 648 if (cpuid->nent < vcpu->arch.cpuid_nent) 649 return -E2BIG; 650 651 if (vcpu->arch.cpuid_dynamic_bits_dirty) 652 kvm_update_cpuid_runtime(vcpu); 653 654 if (copy_to_user(entries, vcpu->arch.cpuid_entries, 655 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) 656 return -EFAULT; 657 658 cpuid->nent = vcpu->arch.cpuid_nent; 659 return 0; 660 } 661 662 static __always_inline u32 raw_cpuid_get(struct cpuid_reg cpuid) 663 { 664 struct kvm_cpuid_entry2 entry; 665 u32 base; 666 667 /* 668 * KVM only supports features defined by Intel (0x0), AMD (0x80000000), 669 * and Centaur (0xc0000000). WARN if a feature for new vendor base is 670 * defined, as this and other code would need to be updated. 671 */ 672 base = cpuid.function & 0xffff0000; 673 if (WARN_ON_ONCE(base && base != 0x80000000 && base != 0xc0000000)) 674 return 0; 675 676 if (cpuid_eax(base) < cpuid.function) 677 return 0; 678 679 cpuid_count(cpuid.function, cpuid.index, 680 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx); 681 682 return *__cpuid_entry_get_reg(&entry, cpuid.reg); 683 } 684 685 /* 686 * For kernel-defined leafs, mask KVM's supported feature set with the kernel's 687 * capabilities as well as raw CPUID. For KVM-defined leafs, consult only raw 688 * CPUID, as KVM is the one and only authority (in the kernel). 689 */ 690 #define kvm_cpu_cap_init(leaf, feature_initializers...) \ 691 do { \ 692 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); \ 693 const u32 __maybe_unused kvm_cpu_cap_init_in_progress = leaf; \ 694 const u32 *kernel_cpu_caps = boot_cpu_data.x86_capability; \ 695 u32 kvm_cpu_cap_passthrough = 0; \ 696 u32 kvm_cpu_cap_synthesized = 0; \ 697 u32 kvm_cpu_cap_emulated = 0; \ 698 u32 kvm_cpu_cap_features = 0; \ 699 \ 700 feature_initializers \ 701 \ 702 kvm_cpu_caps[leaf] = kvm_cpu_cap_features; \ 703 \ 704 if (leaf < NCAPINTS) \ 705 kvm_cpu_caps[leaf] &= kernel_cpu_caps[leaf]; \ 706 \ 707 kvm_cpu_caps[leaf] |= kvm_cpu_cap_passthrough; \ 708 kvm_cpu_caps[leaf] &= (raw_cpuid_get(cpuid) | \ 709 kvm_cpu_cap_synthesized); \ 710 kvm_cpu_caps[leaf] |= kvm_cpu_cap_emulated; \ 711 } while (0) 712 713 /* 714 * Assert that the feature bit being declared, e.g. via F(), is in the CPUID 715 * word that's being initialized. Exempt 0x8000_0001.EDX usage of 0x1.EDX 716 * features, as AMD duplicated many 0x1.EDX features into 0x8000_0001.EDX. 717 */ 718 #define KVM_VALIDATE_CPU_CAP_USAGE(name) \ 719 do { \ 720 u32 __leaf = __feature_leaf(X86_FEATURE_##name); \ 721 \ 722 BUILD_BUG_ON(__leaf != kvm_cpu_cap_init_in_progress); \ 723 } while (0) 724 725 #define F(name) \ 726 ({ \ 727 KVM_VALIDATE_CPU_CAP_USAGE(name); \ 728 kvm_cpu_cap_features |= feature_bit(name); \ 729 }) 730 731 /* Scattered Flag - For features that are scattered by cpufeatures.h. */ 732 #define SCATTERED_F(name) \ 733 ({ \ 734 BUILD_BUG_ON(X86_FEATURE_##name >= MAX_CPU_FEATURES); \ 735 KVM_VALIDATE_CPU_CAP_USAGE(name); \ 736 if (boot_cpu_has(X86_FEATURE_##name)) \ 737 F(name); \ 738 }) 739 740 /* Features that KVM supports only on 64-bit kernels. */ 741 #define X86_64_F(name) \ 742 ({ \ 743 KVM_VALIDATE_CPU_CAP_USAGE(name); \ 744 if (IS_ENABLED(CONFIG_X86_64)) \ 745 F(name); \ 746 }) 747 748 /* 749 * Emulated Feature - For features that KVM emulates in software irrespective 750 * of host CPU/kernel support. 751 */ 752 #define EMULATED_F(name) \ 753 ({ \ 754 kvm_cpu_cap_emulated |= feature_bit(name); \ 755 F(name); \ 756 }) 757 758 /* 759 * Synthesized Feature - For features that are synthesized into boot_cpu_data, 760 * i.e. may not be present in the raw CPUID, but can still be advertised to 761 * userspace. Primarily used for mitigation related feature flags. 762 */ 763 #define SYNTHESIZED_F(name) \ 764 ({ \ 765 kvm_cpu_cap_synthesized |= feature_bit(name); \ 766 F(name); \ 767 }) 768 769 /* 770 * Passthrough Feature - For features that KVM supports based purely on raw 771 * hardware CPUID, i.e. that KVM virtualizes even if the host kernel doesn't 772 * use the feature. Simply force set the feature in KVM's capabilities, raw 773 * CPUID support will be factored in by kvm_cpu_cap_mask(). 774 */ 775 #define PASSTHROUGH_F(name) \ 776 ({ \ 777 kvm_cpu_cap_passthrough |= feature_bit(name); \ 778 F(name); \ 779 }) 780 781 /* 782 * Aliased Features - For features in 0x8000_0001.EDX that are duplicates of 783 * identical 0x1.EDX features, and thus are aliased from 0x1 to 0x8000_0001. 784 */ 785 #define ALIASED_1_EDX_F(name) \ 786 ({ \ 787 BUILD_BUG_ON(__feature_leaf(X86_FEATURE_##name) != CPUID_1_EDX); \ 788 BUILD_BUG_ON(kvm_cpu_cap_init_in_progress != CPUID_8000_0001_EDX); \ 789 kvm_cpu_cap_features |= feature_bit(name); \ 790 }) 791 792 /* 793 * Vendor Features - For features that KVM supports, but are added in later 794 * because they require additional vendor enabling. 795 */ 796 #define VENDOR_F(name) \ 797 ({ \ 798 KVM_VALIDATE_CPU_CAP_USAGE(name); \ 799 }) 800 801 /* 802 * Runtime Features - For features that KVM dynamically sets/clears at runtime, 803 * e.g. when CR4 changes, but which are never advertised to userspace. 804 */ 805 #define RUNTIME_F(name) \ 806 ({ \ 807 KVM_VALIDATE_CPU_CAP_USAGE(name); \ 808 }) 809 810 /* 811 * Undefine the MSR bit macro to avoid token concatenation issues when 812 * processing X86_FEATURE_SPEC_CTRL_SSBD. 813 */ 814 #undef SPEC_CTRL_SSBD 815 816 /* DS is defined by ptrace-abi.h on 32-bit builds. */ 817 #undef DS 818 819 void kvm_set_cpu_caps(void) 820 { 821 memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps)); 822 823 BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) > 824 sizeof(boot_cpu_data.x86_capability)); 825 826 kvm_cpu_cap_init(CPUID_1_ECX, 827 F(XMM3), 828 F(PCLMULQDQ), 829 VENDOR_F(DTES64), 830 /* 831 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not* 832 * advertised to guests via CPUID! MWAIT is also technically a 833 * runtime flag thanks to IA32_MISC_ENABLES; mark it as such so 834 * that KVM is aware that it's a known, unadvertised flag. 835 */ 836 RUNTIME_F(MWAIT), 837 /* DS-CPL */ 838 VENDOR_F(VMX), 839 /* SMX, EST */ 840 /* TM2 */ 841 F(SSSE3), 842 /* CNXT-ID */ 843 /* Reserved */ 844 F(FMA), 845 F(CX16), 846 /* xTPR Update */ 847 F(PDCM), 848 F(PCID), 849 /* Reserved, DCA */ 850 F(XMM4_1), 851 F(XMM4_2), 852 EMULATED_F(X2APIC), 853 F(MOVBE), 854 F(POPCNT), 855 EMULATED_F(TSC_DEADLINE_TIMER), 856 F(AES), 857 F(XSAVE), 858 RUNTIME_F(OSXSAVE), 859 F(AVX), 860 F(F16C), 861 F(RDRAND), 862 EMULATED_F(HYPERVISOR), 863 ); 864 865 kvm_cpu_cap_init(CPUID_1_EDX, 866 F(FPU), 867 F(VME), 868 F(DE), 869 F(PSE), 870 F(TSC), 871 F(MSR), 872 F(PAE), 873 F(MCE), 874 F(CX8), 875 F(APIC), 876 /* Reserved */ 877 F(SEP), 878 F(MTRR), 879 F(PGE), 880 F(MCA), 881 F(CMOV), 882 F(PAT), 883 F(PSE36), 884 /* PSN */ 885 F(CLFLUSH), 886 /* Reserved */ 887 VENDOR_F(DS), 888 /* ACPI */ 889 F(MMX), 890 F(FXSR), 891 F(XMM), 892 F(XMM2), 893 F(SELFSNOOP), 894 /* HTT, TM, Reserved, PBE */ 895 ); 896 897 kvm_cpu_cap_init(CPUID_7_0_EBX, 898 F(FSGSBASE), 899 EMULATED_F(TSC_ADJUST), 900 F(SGX), 901 F(BMI1), 902 F(HLE), 903 F(AVX2), 904 F(FDP_EXCPTN_ONLY), 905 F(SMEP), 906 F(BMI2), 907 F(ERMS), 908 F(INVPCID), 909 F(RTM), 910 F(ZERO_FCS_FDS), 911 VENDOR_F(MPX), 912 F(AVX512F), 913 F(AVX512DQ), 914 F(RDSEED), 915 F(ADX), 916 F(SMAP), 917 F(AVX512IFMA), 918 F(CLFLUSHOPT), 919 F(CLWB), 920 VENDOR_F(INTEL_PT), 921 F(AVX512PF), 922 F(AVX512ER), 923 F(AVX512CD), 924 F(SHA_NI), 925 F(AVX512BW), 926 F(AVX512VL), 927 ); 928 929 kvm_cpu_cap_init(CPUID_7_ECX, 930 F(AVX512VBMI), 931 PASSTHROUGH_F(LA57), 932 F(PKU), 933 RUNTIME_F(OSPKE), 934 F(RDPID), 935 F(AVX512_VPOPCNTDQ), 936 F(UMIP), 937 F(AVX512_VBMI2), 938 F(GFNI), 939 F(VAES), 940 F(VPCLMULQDQ), 941 F(AVX512_VNNI), 942 F(AVX512_BITALG), 943 F(CLDEMOTE), 944 F(MOVDIRI), 945 F(MOVDIR64B), 946 VENDOR_F(WAITPKG), 947 F(SGX_LC), 948 F(BUS_LOCK_DETECT), 949 X86_64_F(SHSTK), 950 ); 951 952 /* 953 * PKU not yet implemented for shadow paging and requires OSPKE 954 * to be set on the host. Clear it if that is not the case 955 */ 956 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE)) 957 kvm_cpu_cap_clear(X86_FEATURE_PKU); 958 959 /* 960 * Shadow Stacks aren't implemented in the Shadow MMU. Shadow Stack 961 * accesses require "magic" Writable=0,Dirty=1 protection, which KVM 962 * doesn't know how to emulate or map. 963 */ 964 if (!tdp_enabled) 965 kvm_cpu_cap_clear(X86_FEATURE_SHSTK); 966 967 kvm_cpu_cap_init(CPUID_7_EDX, 968 F(AVX512_4VNNIW), 969 F(AVX512_4FMAPS), 970 F(SPEC_CTRL), 971 F(SPEC_CTRL_SSBD), 972 EMULATED_F(ARCH_CAPABILITIES), 973 F(INTEL_STIBP), 974 F(MD_CLEAR), 975 F(AVX512_VP2INTERSECT), 976 F(FSRM), 977 F(SERIALIZE), 978 F(TSXLDTRK), 979 F(AVX512_FP16), 980 F(AMX_TILE), 981 F(AMX_INT8), 982 F(AMX_BF16), 983 F(FLUSH_L1D), 984 F(IBT), 985 ); 986 987 /* 988 * Disable support for IBT and SHSTK if KVM is configured to emulate 989 * accesses to reserved GPAs, as KVM's emulator doesn't support IBT or 990 * SHSTK, nor does KVM handle Shadow Stack #PFs (see above). 991 */ 992 if (allow_smaller_maxphyaddr) { 993 kvm_cpu_cap_clear(X86_FEATURE_SHSTK); 994 kvm_cpu_cap_clear(X86_FEATURE_IBT); 995 } 996 997 if (boot_cpu_has(X86_FEATURE_AMD_IBPB_RET) && 998 boot_cpu_has(X86_FEATURE_AMD_IBPB) && 999 boot_cpu_has(X86_FEATURE_AMD_IBRS)) 1000 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL); 1001 if (boot_cpu_has(X86_FEATURE_STIBP)) 1002 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP); 1003 if (boot_cpu_has(X86_FEATURE_AMD_SSBD)) 1004 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD); 1005 1006 kvm_cpu_cap_init(CPUID_7_1_EAX, 1007 F(SHA512), 1008 F(SM3), 1009 F(SM4), 1010 F(AVX_VNNI), 1011 F(AVX512_BF16), 1012 F(CMPCCXADD), 1013 F(FZRM), 1014 F(FSRS), 1015 F(FSRC), 1016 F(WRMSRNS), 1017 X86_64_F(LKGS), 1018 F(AMX_FP16), 1019 F(AVX_IFMA), 1020 F(LAM), 1021 ); 1022 1023 kvm_cpu_cap_init(CPUID_7_1_ECX, 1024 SCATTERED_F(MSR_IMM), 1025 ); 1026 1027 kvm_cpu_cap_init(CPUID_7_1_EDX, 1028 F(AVX_VNNI_INT8), 1029 F(AVX_NE_CONVERT), 1030 F(AMX_COMPLEX), 1031 F(AVX_VNNI_INT16), 1032 F(PREFETCHITI), 1033 F(AVX10), 1034 ); 1035 1036 kvm_cpu_cap_init(CPUID_7_2_EDX, 1037 F(INTEL_PSFD), 1038 F(IPRED_CTRL), 1039 F(RRSBA_CTRL), 1040 F(DDPD_U), 1041 F(BHI_CTRL), 1042 F(MCDT_NO), 1043 ); 1044 1045 kvm_cpu_cap_init(CPUID_D_1_EAX, 1046 F(XSAVEOPT), 1047 F(XSAVEC), 1048 F(XGETBV1), 1049 F(XSAVES), 1050 X86_64_F(XFD), 1051 ); 1052 1053 kvm_cpu_cap_init(CPUID_12_EAX, 1054 SCATTERED_F(SGX1), 1055 SCATTERED_F(SGX2), 1056 SCATTERED_F(SGX_EDECCSSA), 1057 ); 1058 1059 kvm_cpu_cap_init(CPUID_24_0_EBX, 1060 F(AVX10_128), 1061 F(AVX10_256), 1062 F(AVX10_512), 1063 ); 1064 1065 kvm_cpu_cap_init(CPUID_8000_0001_ECX, 1066 F(LAHF_LM), 1067 F(CMP_LEGACY), 1068 VENDOR_F(SVM), 1069 /* ExtApicSpace */ 1070 F(CR8_LEGACY), 1071 F(ABM), 1072 F(SSE4A), 1073 F(MISALIGNSSE), 1074 F(3DNOWPREFETCH), 1075 F(OSVW), 1076 /* IBS */ 1077 F(XOP), 1078 /* SKINIT, WDT, LWP */ 1079 F(FMA4), 1080 F(TBM), 1081 F(TOPOEXT), 1082 VENDOR_F(PERFCTR_CORE), 1083 ); 1084 1085 kvm_cpu_cap_init(CPUID_8000_0001_EDX, 1086 ALIASED_1_EDX_F(FPU), 1087 ALIASED_1_EDX_F(VME), 1088 ALIASED_1_EDX_F(DE), 1089 ALIASED_1_EDX_F(PSE), 1090 ALIASED_1_EDX_F(TSC), 1091 ALIASED_1_EDX_F(MSR), 1092 ALIASED_1_EDX_F(PAE), 1093 ALIASED_1_EDX_F(MCE), 1094 ALIASED_1_EDX_F(CX8), 1095 ALIASED_1_EDX_F(APIC), 1096 /* Reserved */ 1097 F(SYSCALL), 1098 ALIASED_1_EDX_F(MTRR), 1099 ALIASED_1_EDX_F(PGE), 1100 ALIASED_1_EDX_F(MCA), 1101 ALIASED_1_EDX_F(CMOV), 1102 ALIASED_1_EDX_F(PAT), 1103 ALIASED_1_EDX_F(PSE36), 1104 /* Reserved */ 1105 F(NX), 1106 /* Reserved */ 1107 F(MMXEXT), 1108 ALIASED_1_EDX_F(MMX), 1109 ALIASED_1_EDX_F(FXSR), 1110 F(FXSR_OPT), 1111 X86_64_F(GBPAGES), 1112 F(RDTSCP), 1113 /* Reserved */ 1114 X86_64_F(LM), 1115 F(3DNOWEXT), 1116 F(3DNOW), 1117 ); 1118 1119 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64)) 1120 kvm_cpu_cap_set(X86_FEATURE_GBPAGES); 1121 1122 kvm_cpu_cap_init(CPUID_8000_0007_EDX, 1123 SCATTERED_F(CONSTANT_TSC), 1124 ); 1125 1126 kvm_cpu_cap_init(CPUID_8000_0008_EBX, 1127 F(CLZERO), 1128 F(XSAVEERPTR), 1129 F(WBNOINVD), 1130 F(AMD_IBPB), 1131 F(AMD_IBRS), 1132 F(AMD_SSBD), 1133 F(VIRT_SSBD), 1134 F(AMD_SSB_NO), 1135 F(AMD_STIBP), 1136 F(AMD_STIBP_ALWAYS_ON), 1137 F(AMD_IBRS_SAME_MODE), 1138 PASSTHROUGH_F(EFER_LMSLE_MBZ), 1139 F(AMD_PSFD), 1140 F(AMD_IBPB_RET), 1141 ); 1142 1143 /* 1144 * AMD has separate bits for each SPEC_CTRL bit. 1145 * arch/x86/kernel/cpu/bugs.c is kind enough to 1146 * record that in cpufeatures so use them. 1147 */ 1148 if (boot_cpu_has(X86_FEATURE_IBPB)) { 1149 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB); 1150 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL) && 1151 !boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB)) 1152 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB_RET); 1153 } 1154 if (boot_cpu_has(X86_FEATURE_IBRS)) 1155 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS); 1156 if (boot_cpu_has(X86_FEATURE_STIBP)) 1157 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP); 1158 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD)) 1159 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD); 1160 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) 1161 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO); 1162 /* 1163 * The preference is to use SPEC CTRL MSR instead of the 1164 * VIRT_SPEC MSR. 1165 */ 1166 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) && 1167 !boot_cpu_has(X86_FEATURE_AMD_SSBD)) 1168 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD); 1169 1170 /* All SVM features required additional vendor module enabling. */ 1171 kvm_cpu_cap_init(CPUID_8000_000A_EDX, 1172 VENDOR_F(NPT), 1173 VENDOR_F(VMCBCLEAN), 1174 VENDOR_F(FLUSHBYASID), 1175 VENDOR_F(NRIPS), 1176 VENDOR_F(TSCRATEMSR), 1177 VENDOR_F(V_VMSAVE_VMLOAD), 1178 VENDOR_F(LBRV), 1179 VENDOR_F(PAUSEFILTER), 1180 VENDOR_F(PFTHRESHOLD), 1181 VENDOR_F(VGIF), 1182 VENDOR_F(VNMI), 1183 VENDOR_F(SVME_ADDR_CHK), 1184 ); 1185 1186 kvm_cpu_cap_init(CPUID_8000_001F_EAX, 1187 VENDOR_F(SME), 1188 VENDOR_F(SEV), 1189 /* VM_PAGE_FLUSH */ 1190 VENDOR_F(SEV_ES), 1191 F(SME_COHERENT), 1192 ); 1193 1194 kvm_cpu_cap_init(CPUID_8000_0021_EAX, 1195 F(NO_NESTED_DATA_BP), 1196 F(WRMSR_XX_BASE_NS), 1197 /* 1198 * Synthesize "LFENCE is serializing" into the AMD-defined entry 1199 * in KVM's supported CPUID, i.e. if the feature is reported as 1200 * supported by the kernel. LFENCE_RDTSC was a Linux-defined 1201 * synthetic feature long before AMD joined the bandwagon, e.g. 1202 * LFENCE is serializing on most CPUs that support SSE2. On 1203 * CPUs that don't support AMD's leaf, ANDing with the raw host 1204 * CPUID will drop the flags, and reporting support in AMD's 1205 * leaf can make it easier for userspace to detect the feature. 1206 */ 1207 SYNTHESIZED_F(LFENCE_RDTSC), 1208 /* SmmPgCfgLock */ 1209 /* 4: Resv */ 1210 SYNTHESIZED_F(VERW_CLEAR), 1211 F(NULL_SEL_CLR_BASE), 1212 /* UpperAddressIgnore */ 1213 F(AUTOIBRS), 1214 F(PREFETCHI), 1215 EMULATED_F(NO_SMM_CTL_MSR), 1216 /* PrefetchCtlMsr */ 1217 /* GpOnUserCpuid */ 1218 /* EPSF */ 1219 SYNTHESIZED_F(SBPB), 1220 SYNTHESIZED_F(IBPB_BRTYPE), 1221 SYNTHESIZED_F(SRSO_NO), 1222 F(SRSO_USER_KERNEL_NO), 1223 ); 1224 1225 kvm_cpu_cap_init(CPUID_8000_0021_ECX, 1226 SYNTHESIZED_F(TSA_SQ_NO), 1227 SYNTHESIZED_F(TSA_L1_NO), 1228 ); 1229 1230 kvm_cpu_cap_init(CPUID_8000_0022_EAX, 1231 F(PERFMON_V2), 1232 ); 1233 1234 if (!static_cpu_has_bug(X86_BUG_NULL_SEG)) 1235 kvm_cpu_cap_set(X86_FEATURE_NULL_SEL_CLR_BASE); 1236 1237 kvm_cpu_cap_init(CPUID_C000_0001_EDX, 1238 F(XSTORE), 1239 F(XSTORE_EN), 1240 F(XCRYPT), 1241 F(XCRYPT_EN), 1242 F(ACE2), 1243 F(ACE2_EN), 1244 F(PHE), 1245 F(PHE_EN), 1246 F(PMM), 1247 F(PMM_EN), 1248 ); 1249 1250 /* 1251 * Hide RDTSCP and RDPID if either feature is reported as supported but 1252 * probing MSR_TSC_AUX failed. This is purely a sanity check and 1253 * should never happen, but the guest will likely crash if RDTSCP or 1254 * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in 1255 * the past. For example, the sanity check may fire if this instance of 1256 * KVM is running as L1 on top of an older, broken KVM. 1257 */ 1258 if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) || 1259 kvm_cpu_cap_has(X86_FEATURE_RDPID)) && 1260 !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) { 1261 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP); 1262 kvm_cpu_cap_clear(X86_FEATURE_RDPID); 1263 } 1264 } 1265 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_set_cpu_caps); 1266 1267 #undef F 1268 #undef SCATTERED_F 1269 #undef X86_64_F 1270 #undef EMULATED_F 1271 #undef SYNTHESIZED_F 1272 #undef PASSTHROUGH_F 1273 #undef ALIASED_1_EDX_F 1274 #undef VENDOR_F 1275 #undef RUNTIME_F 1276 1277 struct kvm_cpuid_array { 1278 struct kvm_cpuid_entry2 *entries; 1279 int maxnent; 1280 int nent; 1281 }; 1282 1283 static struct kvm_cpuid_entry2 *get_next_cpuid(struct kvm_cpuid_array *array) 1284 { 1285 if (array->nent >= array->maxnent) 1286 return NULL; 1287 1288 return &array->entries[array->nent++]; 1289 } 1290 1291 static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array, 1292 u32 function, u32 index) 1293 { 1294 struct kvm_cpuid_entry2 *entry = get_next_cpuid(array); 1295 1296 if (!entry) 1297 return NULL; 1298 1299 memset(entry, 0, sizeof(*entry)); 1300 entry->function = function; 1301 entry->index = index; 1302 switch (function & 0xC0000000) { 1303 case 0x40000000: 1304 /* Hypervisor leaves are always synthesized by __do_cpuid_func. */ 1305 return entry; 1306 1307 case 0x80000000: 1308 /* 1309 * 0x80000021 is sometimes synthesized by __do_cpuid_func, which 1310 * would result in out-of-bounds calls to do_host_cpuid. 1311 */ 1312 { 1313 static int max_cpuid_80000000; 1314 if (!READ_ONCE(max_cpuid_80000000)) 1315 WRITE_ONCE(max_cpuid_80000000, cpuid_eax(0x80000000)); 1316 if (function > READ_ONCE(max_cpuid_80000000)) 1317 return entry; 1318 } 1319 break; 1320 1321 default: 1322 break; 1323 } 1324 1325 cpuid_count(entry->function, entry->index, 1326 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); 1327 1328 if (cpuid_function_is_indexed(function)) 1329 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; 1330 1331 return entry; 1332 } 1333 1334 static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, 1335 bool include_partially_emulated) 1336 { 1337 memset(entry, 0, sizeof(*entry)); 1338 1339 entry->function = func; 1340 entry->index = 0; 1341 entry->flags = 0; 1342 1343 switch (func) { 1344 case 0: 1345 entry->eax = 7; 1346 return 1; 1347 case 1: 1348 entry->ecx = feature_bit(MOVBE); 1349 /* 1350 * KVM allows userspace to enumerate MONITOR+MWAIT support to 1351 * the guest, but the MWAIT feature flag is never advertised 1352 * to userspace because MONITOR+MWAIT aren't virtualized by 1353 * hardware, can't be faithfully emulated in software (KVM 1354 * emulates them as NOPs), and allowing the guest to execute 1355 * them natively requires enabling a per-VM capability. 1356 */ 1357 if (include_partially_emulated) 1358 entry->ecx |= feature_bit(MWAIT); 1359 return 1; 1360 case 7: 1361 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; 1362 entry->eax = 0; 1363 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) 1364 entry->ecx = feature_bit(RDPID); 1365 return 1; 1366 default: 1367 return 0; 1368 } 1369 } 1370 1371 static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func) 1372 { 1373 if (array->nent >= array->maxnent) 1374 return -E2BIG; 1375 1376 array->nent += cpuid_func_emulated(&array->entries[array->nent], func, false); 1377 return 0; 1378 } 1379 1380 static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) 1381 { 1382 struct kvm_cpuid_entry2 *entry; 1383 int r, i, max_idx; 1384 1385 /* all calls to cpuid_count() should be made on the same cpu */ 1386 get_cpu(); 1387 1388 r = -E2BIG; 1389 1390 entry = do_host_cpuid(array, function, 0); 1391 if (!entry) 1392 goto out; 1393 1394 switch (function) { 1395 case 0: 1396 /* Limited to the highest leaf implemented in KVM. */ 1397 entry->eax = min(entry->eax, 0x24U); 1398 break; 1399 case 1: 1400 cpuid_entry_override(entry, CPUID_1_EDX); 1401 cpuid_entry_override(entry, CPUID_1_ECX); 1402 break; 1403 case 2: 1404 /* 1405 * On ancient CPUs, function 2 entries are STATEFUL. That is, 1406 * CPUID(function=2, index=0) may return different results each 1407 * time, with the least-significant byte in EAX enumerating the 1408 * number of times software should do CPUID(2, 0). 1409 * 1410 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less 1411 * idiotic. Intel's SDM states that EAX & 0xff "will always 1412 * return 01H. Software should ignore this value and not 1413 * interpret it as an informational descriptor", while AMD's 1414 * APM states that CPUID(2) is reserved. 1415 * 1416 * WARN if a frankenstein CPU that supports virtualization and 1417 * a stateful CPUID.0x2 is encountered. 1418 */ 1419 WARN_ON_ONCE((entry->eax & 0xff) > 1); 1420 break; 1421 /* functions 4 and 0x8000001d have additional index. */ 1422 case 4: 1423 case 0x8000001d: 1424 /* 1425 * Read entries until the cache type in the previous entry is 1426 * zero, i.e. indicates an invalid entry. 1427 */ 1428 for (i = 1; entry->eax & 0x1f; ++i) { 1429 entry = do_host_cpuid(array, function, i); 1430 if (!entry) 1431 goto out; 1432 } 1433 break; 1434 case 6: /* Thermal management */ 1435 entry->eax = 0x4; /* allow ARAT */ 1436 entry->ebx = 0; 1437 entry->ecx = 0; 1438 entry->edx = 0; 1439 break; 1440 /* function 7 has additional index. */ 1441 case 7: 1442 max_idx = entry->eax = min(entry->eax, 2u); 1443 cpuid_entry_override(entry, CPUID_7_0_EBX); 1444 cpuid_entry_override(entry, CPUID_7_ECX); 1445 cpuid_entry_override(entry, CPUID_7_EDX); 1446 1447 /* KVM only supports up to 0x7.2, capped above via min(). */ 1448 if (max_idx >= 1) { 1449 entry = do_host_cpuid(array, function, 1); 1450 if (!entry) 1451 goto out; 1452 1453 cpuid_entry_override(entry, CPUID_7_1_EAX); 1454 cpuid_entry_override(entry, CPUID_7_1_ECX); 1455 cpuid_entry_override(entry, CPUID_7_1_EDX); 1456 entry->ebx = 0; 1457 } 1458 if (max_idx >= 2) { 1459 entry = do_host_cpuid(array, function, 2); 1460 if (!entry) 1461 goto out; 1462 1463 cpuid_entry_override(entry, CPUID_7_2_EDX); 1464 entry->ecx = 0; 1465 entry->ebx = 0; 1466 entry->eax = 0; 1467 } 1468 break; 1469 case 0xa: { /* Architectural Performance Monitoring */ 1470 union cpuid10_eax eax = { }; 1471 union cpuid10_edx edx = { }; 1472 1473 if (!enable_pmu || !static_cpu_has(X86_FEATURE_ARCH_PERFMON)) { 1474 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1475 break; 1476 } 1477 1478 eax.split.version_id = kvm_pmu_cap.version; 1479 eax.split.num_counters = kvm_pmu_cap.num_counters_gp; 1480 eax.split.bit_width = kvm_pmu_cap.bit_width_gp; 1481 eax.split.mask_length = kvm_pmu_cap.events_mask_len; 1482 edx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed; 1483 edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed; 1484 1485 if (kvm_pmu_cap.version) 1486 edx.split.anythread_deprecated = 1; 1487 1488 entry->eax = eax.full; 1489 entry->ebx = kvm_pmu_cap.events_mask; 1490 entry->ecx = 0; 1491 entry->edx = edx.full; 1492 break; 1493 } 1494 case 0x1f: 1495 case 0xb: 1496 /* 1497 * No topology; a valid topology is indicated by the presence 1498 * of subleaf 1. 1499 */ 1500 entry->eax = entry->ebx = entry->ecx = 0; 1501 break; 1502 case 0xd: { 1503 u64 permitted_xcr0 = kvm_get_filtered_xcr0(); 1504 u64 permitted_xss = kvm_caps.supported_xss; 1505 1506 entry->eax &= permitted_xcr0; 1507 entry->ebx = xstate_required_size(permitted_xcr0, false); 1508 entry->ecx = entry->ebx; 1509 entry->edx &= permitted_xcr0 >> 32; 1510 if (!permitted_xcr0) 1511 break; 1512 1513 entry = do_host_cpuid(array, function, 1); 1514 if (!entry) 1515 goto out; 1516 1517 cpuid_entry_override(entry, CPUID_D_1_EAX); 1518 if (entry->eax & (feature_bit(XSAVES) | feature_bit(XSAVEC))) 1519 entry->ebx = xstate_required_size(permitted_xcr0 | permitted_xss, 1520 true); 1521 else { 1522 WARN_ON_ONCE(permitted_xss != 0); 1523 entry->ebx = 0; 1524 } 1525 entry->ecx &= permitted_xss; 1526 entry->edx &= permitted_xss >> 32; 1527 1528 for (i = 2; i < 64; ++i) { 1529 bool s_state; 1530 if (permitted_xcr0 & BIT_ULL(i)) 1531 s_state = false; 1532 else if (permitted_xss & BIT_ULL(i)) 1533 s_state = true; 1534 else 1535 continue; 1536 1537 entry = do_host_cpuid(array, function, i); 1538 if (!entry) 1539 goto out; 1540 1541 /* 1542 * The supported check above should have filtered out 1543 * invalid sub-leafs. Only valid sub-leafs should 1544 * reach this point, and they should have a non-zero 1545 * save state size. Furthermore, check whether the 1546 * processor agrees with permitted_xcr0/permitted_xss 1547 * on whether this is an XCR0- or IA32_XSS-managed area. 1548 */ 1549 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) { 1550 --array->nent; 1551 continue; 1552 } 1553 1554 if (!kvm_cpu_cap_has(X86_FEATURE_XFD)) 1555 entry->ecx &= ~BIT_ULL(2); 1556 entry->edx = 0; 1557 } 1558 break; 1559 } 1560 case 0x12: 1561 /* Intel SGX */ 1562 if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) { 1563 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1564 break; 1565 } 1566 1567 /* 1568 * Index 0: Sub-features, MISCSELECT (a.k.a extended features) 1569 * and max enclave sizes. The SGX sub-features and MISCSELECT 1570 * are restricted by kernel and KVM capabilities (like most 1571 * feature flags), while enclave size is unrestricted. 1572 */ 1573 cpuid_entry_override(entry, CPUID_12_EAX); 1574 entry->ebx &= SGX_MISC_EXINFO; 1575 1576 entry = do_host_cpuid(array, function, 1); 1577 if (!entry) 1578 goto out; 1579 1580 /* 1581 * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la 1582 * feature flags. Advertise all supported flags, including 1583 * privileged attributes that require explicit opt-in from 1584 * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is 1585 * expected to derive it from supported XCR0. 1586 */ 1587 entry->eax &= SGX_ATTR_PRIV_MASK | SGX_ATTR_UNPRIV_MASK; 1588 entry->ebx &= 0; 1589 break; 1590 /* Intel PT */ 1591 case 0x14: 1592 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) { 1593 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1594 break; 1595 } 1596 1597 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) { 1598 if (!do_host_cpuid(array, function, i)) 1599 goto out; 1600 } 1601 break; 1602 /* Intel AMX TILE */ 1603 case 0x1d: 1604 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) { 1605 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1606 break; 1607 } 1608 1609 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) { 1610 if (!do_host_cpuid(array, function, i)) 1611 goto out; 1612 } 1613 break; 1614 case 0x1e: /* TMUL information */ 1615 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) { 1616 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1617 break; 1618 } 1619 break; 1620 case 0x24: { 1621 u8 avx10_version; 1622 1623 if (!kvm_cpu_cap_has(X86_FEATURE_AVX10)) { 1624 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1625 break; 1626 } 1627 1628 /* 1629 * The AVX10 version is encoded in EBX[7:0]. Note, the version 1630 * is guaranteed to be >=1 if AVX10 is supported. Note #2, the 1631 * version needs to be captured before overriding EBX features! 1632 */ 1633 avx10_version = min_t(u8, entry->ebx & 0xff, 1); 1634 cpuid_entry_override(entry, CPUID_24_0_EBX); 1635 entry->ebx |= avx10_version; 1636 1637 entry->eax = 0; 1638 entry->ecx = 0; 1639 entry->edx = 0; 1640 break; 1641 } 1642 case KVM_CPUID_SIGNATURE: { 1643 const u32 *sigptr = (const u32 *)KVM_SIGNATURE; 1644 entry->eax = KVM_CPUID_FEATURES; 1645 entry->ebx = sigptr[0]; 1646 entry->ecx = sigptr[1]; 1647 entry->edx = sigptr[2]; 1648 break; 1649 } 1650 case KVM_CPUID_FEATURES: 1651 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) | 1652 (1 << KVM_FEATURE_NOP_IO_DELAY) | 1653 (1 << KVM_FEATURE_CLOCKSOURCE2) | 1654 (1 << KVM_FEATURE_ASYNC_PF) | 1655 (1 << KVM_FEATURE_PV_EOI) | 1656 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) | 1657 (1 << KVM_FEATURE_PV_UNHALT) | 1658 (1 << KVM_FEATURE_PV_TLB_FLUSH) | 1659 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) | 1660 (1 << KVM_FEATURE_PV_SEND_IPI) | 1661 (1 << KVM_FEATURE_POLL_CONTROL) | 1662 (1 << KVM_FEATURE_PV_SCHED_YIELD) | 1663 (1 << KVM_FEATURE_ASYNC_PF_INT); 1664 1665 if (sched_info_on()) 1666 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME); 1667 1668 entry->ebx = 0; 1669 entry->ecx = 0; 1670 entry->edx = 0; 1671 break; 1672 case 0x80000000: 1673 entry->eax = min(entry->eax, 0x80000022); 1674 /* 1675 * Serializing LFENCE is reported in a multitude of ways, and 1676 * NullSegClearsBase is not reported in CPUID on Zen2; help 1677 * userspace by providing the CPUID leaf ourselves. 1678 * 1679 * However, only do it if the host has CPUID leaf 0x8000001d. 1680 * QEMU thinks that it can query the host blindly for that 1681 * CPUID leaf if KVM reports that it supports 0x8000001d or 1682 * above. The processor merrily returns values from the 1683 * highest Intel leaf which QEMU tries to use as the guest's 1684 * 0x8000001d. Even worse, this can result in an infinite 1685 * loop if said highest leaf has no subleaves indexed by ECX. 1686 */ 1687 if (entry->eax >= 0x8000001d && 1688 (static_cpu_has(X86_FEATURE_LFENCE_RDTSC) 1689 || !static_cpu_has_bug(X86_BUG_NULL_SEG))) 1690 entry->eax = max(entry->eax, 0x80000021); 1691 break; 1692 case 0x80000001: 1693 entry->ebx &= ~GENMASK(27, 16); 1694 cpuid_entry_override(entry, CPUID_8000_0001_EDX); 1695 cpuid_entry_override(entry, CPUID_8000_0001_ECX); 1696 break; 1697 case 0x80000005: 1698 /* Pass host L1 cache and TLB info. */ 1699 break; 1700 case 0x80000006: 1701 /* Drop reserved bits, pass host L2 cache and TLB info. */ 1702 entry->edx &= ~GENMASK(17, 16); 1703 break; 1704 case 0x80000007: /* Advanced power management */ 1705 cpuid_entry_override(entry, CPUID_8000_0007_EDX); 1706 1707 /* mask against host */ 1708 entry->edx &= boot_cpu_data.x86_power; 1709 entry->eax = entry->ebx = entry->ecx = 0; 1710 break; 1711 case 0x80000008: { 1712 /* 1713 * GuestPhysAddrSize (EAX[23:16]) is intended for software 1714 * use. 1715 * 1716 * KVM's ABI is to report the effective MAXPHYADDR for the 1717 * guest in PhysAddrSize (phys_as), and the maximum 1718 * *addressable* GPA in GuestPhysAddrSize (g_phys_as). 1719 * 1720 * GuestPhysAddrSize is valid if and only if TDP is enabled, 1721 * in which case the max GPA that can be addressed by KVM may 1722 * be less than the max GPA that can be legally generated by 1723 * the guest, e.g. if MAXPHYADDR>48 but the CPU doesn't 1724 * support 5-level TDP. 1725 */ 1726 unsigned int virt_as = max((entry->eax >> 8) & 0xff, 48U); 1727 unsigned int phys_as, g_phys_as; 1728 1729 /* 1730 * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as 1731 * the guest operates in the same PA space as the host, i.e. 1732 * reductions in MAXPHYADDR for memory encryption affect shadow 1733 * paging, too. 1734 * 1735 * If TDP is enabled, use the raw bare metal MAXPHYADDR as 1736 * reductions to the HPAs do not affect GPAs. The max 1737 * addressable GPA is the same as the max effective GPA, except 1738 * that it's capped at 48 bits if 5-level TDP isn't supported 1739 * (hardware processes bits 51:48 only when walking the fifth 1740 * level page table). 1741 */ 1742 if (!tdp_enabled) { 1743 phys_as = boot_cpu_data.x86_phys_bits; 1744 g_phys_as = 0; 1745 } else { 1746 phys_as = entry->eax & 0xff; 1747 g_phys_as = phys_as; 1748 if (kvm_mmu_get_max_tdp_level() < 5) 1749 g_phys_as = min(g_phys_as, 48U); 1750 } 1751 1752 entry->eax = phys_as | (virt_as << 8) | (g_phys_as << 16); 1753 entry->ecx &= ~(GENMASK(31, 16) | GENMASK(11, 8)); 1754 entry->edx = 0; 1755 cpuid_entry_override(entry, CPUID_8000_0008_EBX); 1756 break; 1757 } 1758 case 0x8000000A: 1759 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) { 1760 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1761 break; 1762 } 1763 entry->eax = 1; /* SVM revision 1 */ 1764 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper 1765 ASID emulation to nested SVM */ 1766 entry->ecx = 0; /* Reserved */ 1767 cpuid_entry_override(entry, CPUID_8000_000A_EDX); 1768 break; 1769 case 0x80000019: 1770 entry->ecx = entry->edx = 0; 1771 break; 1772 case 0x8000001a: 1773 entry->eax &= GENMASK(2, 0); 1774 entry->ebx = entry->ecx = entry->edx = 0; 1775 break; 1776 case 0x8000001e: 1777 /* Do not return host topology information. */ 1778 entry->eax = entry->ebx = entry->ecx = 0; 1779 entry->edx = 0; /* reserved */ 1780 break; 1781 case 0x8000001F: 1782 if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) { 1783 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1784 } else { 1785 cpuid_entry_override(entry, CPUID_8000_001F_EAX); 1786 /* Clear NumVMPL since KVM does not support VMPL. */ 1787 entry->ebx &= ~GENMASK(31, 12); 1788 /* 1789 * Enumerate '0' for "PA bits reduction", the adjusted 1790 * MAXPHYADDR is enumerated directly (see 0x80000008). 1791 */ 1792 entry->ebx &= ~GENMASK(11, 6); 1793 } 1794 break; 1795 case 0x80000020: 1796 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1797 break; 1798 case 0x80000021: 1799 entry->ebx = entry->edx = 0; 1800 cpuid_entry_override(entry, CPUID_8000_0021_EAX); 1801 cpuid_entry_override(entry, CPUID_8000_0021_ECX); 1802 break; 1803 /* AMD Extended Performance Monitoring and Debug */ 1804 case 0x80000022: { 1805 union cpuid_0x80000022_ebx ebx = { }; 1806 1807 entry->ecx = entry->edx = 0; 1808 if (!enable_pmu || !kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2)) { 1809 entry->eax = entry->ebx = 0; 1810 break; 1811 } 1812 1813 cpuid_entry_override(entry, CPUID_8000_0022_EAX); 1814 1815 ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp; 1816 entry->ebx = ebx.full; 1817 break; 1818 } 1819 /*Add support for Centaur's CPUID instruction*/ 1820 case 0xC0000000: 1821 /*Just support up to 0xC0000004 now*/ 1822 entry->eax = min(entry->eax, 0xC0000004); 1823 break; 1824 case 0xC0000001: 1825 cpuid_entry_override(entry, CPUID_C000_0001_EDX); 1826 break; 1827 case 3: /* Processor serial number */ 1828 case 5: /* MONITOR/MWAIT */ 1829 case 0xC0000002: 1830 case 0xC0000003: 1831 case 0xC0000004: 1832 default: 1833 entry->eax = entry->ebx = entry->ecx = entry->edx = 0; 1834 break; 1835 } 1836 1837 r = 0; 1838 1839 out: 1840 put_cpu(); 1841 1842 return r; 1843 } 1844 1845 static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func, 1846 unsigned int type) 1847 { 1848 if (type == KVM_GET_EMULATED_CPUID) 1849 return __do_cpuid_func_emulated(array, func); 1850 1851 return __do_cpuid_func(array, func); 1852 } 1853 1854 #define CENTAUR_CPUID_SIGNATURE 0xC0000000 1855 1856 static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func, 1857 unsigned int type) 1858 { 1859 u32 limit; 1860 int r; 1861 1862 if (func == CENTAUR_CPUID_SIGNATURE && 1863 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR && 1864 boot_cpu_data.x86_vendor != X86_VENDOR_ZHAOXIN) 1865 return 0; 1866 1867 r = do_cpuid_func(array, func, type); 1868 if (r) 1869 return r; 1870 1871 limit = array->entries[array->nent - 1].eax; 1872 for (func = func + 1; func <= limit; ++func) { 1873 r = do_cpuid_func(array, func, type); 1874 if (r) 1875 break; 1876 } 1877 1878 return r; 1879 } 1880 1881 static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries, 1882 __u32 num_entries, unsigned int ioctl_type) 1883 { 1884 int i; 1885 __u32 pad[3]; 1886 1887 if (ioctl_type != KVM_GET_EMULATED_CPUID) 1888 return false; 1889 1890 /* 1891 * We want to make sure that ->padding is being passed clean from 1892 * userspace in case we want to use it for something in the future. 1893 * 1894 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we 1895 * have to give ourselves satisfied only with the emulated side. /me 1896 * sheds a tear. 1897 */ 1898 for (i = 0; i < num_entries; i++) { 1899 if (copy_from_user(pad, entries[i].padding, sizeof(pad))) 1900 return true; 1901 1902 if (pad[0] || pad[1] || pad[2]) 1903 return true; 1904 } 1905 return false; 1906 } 1907 1908 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, 1909 struct kvm_cpuid_entry2 __user *entries, 1910 unsigned int type) 1911 { 1912 static const u32 funcs[] = { 1913 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE, 1914 }; 1915 1916 struct kvm_cpuid_array array = { 1917 .nent = 0, 1918 }; 1919 int r, i; 1920 1921 if (cpuid->nent < 1) 1922 return -E2BIG; 1923 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) 1924 cpuid->nent = KVM_MAX_CPUID_ENTRIES; 1925 1926 if (sanity_check_entries(entries, cpuid->nent, type)) 1927 return -EINVAL; 1928 1929 array.entries = kvcalloc(cpuid->nent, sizeof(struct kvm_cpuid_entry2), GFP_KERNEL); 1930 if (!array.entries) 1931 return -ENOMEM; 1932 1933 array.maxnent = cpuid->nent; 1934 1935 for (i = 0; i < ARRAY_SIZE(funcs); i++) { 1936 r = get_cpuid_func(&array, funcs[i], type); 1937 if (r) 1938 goto out_free; 1939 } 1940 cpuid->nent = array.nent; 1941 1942 if (copy_to_user(entries, array.entries, 1943 array.nent * sizeof(struct kvm_cpuid_entry2))) 1944 r = -EFAULT; 1945 1946 out_free: 1947 kvfree(array.entries); 1948 return r; 1949 } 1950 1951 /* 1952 * Intel CPUID semantics treats any query for an out-of-range leaf as if the 1953 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics 1954 * returns all zeroes for any undefined leaf, whether or not the leaf is in 1955 * range. Centaur/VIA follows Intel semantics. 1956 * 1957 * A leaf is considered out-of-range if its function is higher than the maximum 1958 * supported leaf of its associated class or if its associated class does not 1959 * exist. 1960 * 1961 * There are three primary classes to be considered, with their respective 1962 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary 1963 * class exists if a guest CPUID entry for its <base> leaf exists. For a given 1964 * class, CPUID.<base>.EAX contains the max supported leaf for the class. 1965 * 1966 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff 1967 * - Hypervisor: 0x40000000 - 0x4fffffff 1968 * - Extended: 0x80000000 - 0xbfffffff 1969 * - Centaur: 0xc0000000 - 0xcfffffff 1970 * 1971 * The Hypervisor class is further subdivided into sub-classes that each act as 1972 * their own independent class associated with a 0x100 byte range. E.g. if Qemu 1973 * is advertising support for both HyperV and KVM, the resulting Hypervisor 1974 * CPUID sub-classes are: 1975 * 1976 * - HyperV: 0x40000000 - 0x400000ff 1977 * - KVM: 0x40000100 - 0x400001ff 1978 */ 1979 static struct kvm_cpuid_entry2 * 1980 get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index) 1981 { 1982 struct kvm_cpuid_entry2 *basic, *class; 1983 u32 function = *fn_ptr; 1984 1985 basic = kvm_find_cpuid_entry(vcpu, 0); 1986 if (!basic) 1987 return NULL; 1988 1989 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) || 1990 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx)) 1991 return NULL; 1992 1993 if (function >= 0x40000000 && function <= 0x4fffffff) 1994 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00); 1995 else if (function >= 0xc0000000) 1996 class = kvm_find_cpuid_entry(vcpu, 0xc0000000); 1997 else 1998 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000); 1999 2000 if (class && function <= class->eax) 2001 return NULL; 2002 2003 /* 2004 * Leaf specific adjustments are also applied when redirecting to the 2005 * max basic entry, e.g. if the max basic leaf is 0xb but there is no 2006 * entry for CPUID.0xb.index (see below), then the output value for EDX 2007 * needs to be pulled from CPUID.0xb.1. 2008 */ 2009 *fn_ptr = basic->eax; 2010 2011 /* 2012 * The class does not exist or the requested function is out of range; 2013 * the effective CPUID entry is the max basic leaf. Note, the index of 2014 * the original requested leaf is observed! 2015 */ 2016 return kvm_find_cpuid_entry_index(vcpu, basic->eax, index); 2017 } 2018 2019 bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, 2020 u32 *ecx, u32 *edx, bool exact_only) 2021 { 2022 u32 orig_function = *eax, function = *eax, index = *ecx; 2023 struct kvm_cpuid_entry2 *entry; 2024 bool exact, used_max_basic = false; 2025 2026 if (vcpu->arch.cpuid_dynamic_bits_dirty) 2027 kvm_update_cpuid_runtime(vcpu); 2028 2029 entry = kvm_find_cpuid_entry_index(vcpu, function, index); 2030 exact = !!entry; 2031 2032 if (!entry && !exact_only) { 2033 entry = get_out_of_range_cpuid_entry(vcpu, &function, index); 2034 used_max_basic = !!entry; 2035 } 2036 2037 if (entry) { 2038 *eax = entry->eax; 2039 *ebx = entry->ebx; 2040 *ecx = entry->ecx; 2041 *edx = entry->edx; 2042 if (function == 7 && index == 0) { 2043 u64 data; 2044 if ((*ebx & (feature_bit(RTM) | feature_bit(HLE))) && 2045 !kvm_msr_read(vcpu, MSR_IA32_TSX_CTRL, &data) && 2046 (data & TSX_CTRL_CPUID_CLEAR)) 2047 *ebx &= ~(feature_bit(RTM) | feature_bit(HLE)); 2048 } else if (function == 0x80000007) { 2049 if (kvm_hv_invtsc_suppressed(vcpu)) 2050 *edx &= ~feature_bit(CONSTANT_TSC); 2051 } else if (IS_ENABLED(CONFIG_KVM_XEN) && 2052 kvm_xen_is_tsc_leaf(vcpu, function)) { 2053 /* 2054 * Update guest TSC frequency information if necessary. 2055 * Ignore failures, there is no sane value that can be 2056 * provided if KVM can't get the TSC frequency. 2057 */ 2058 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) 2059 kvm_guest_time_update(vcpu); 2060 2061 if (index == 1) { 2062 *ecx = vcpu->arch.pvclock_tsc_mul; 2063 *edx = vcpu->arch.pvclock_tsc_shift; 2064 } else if (index == 2) { 2065 *eax = vcpu->arch.hw_tsc_khz; 2066 } 2067 } 2068 } else { 2069 *eax = *ebx = *ecx = *edx = 0; 2070 /* 2071 * When leaf 0BH or 1FH is defined, CL is pass-through 2072 * and EDX is always the x2APIC ID, even for undefined 2073 * subleaves. Index 1 will exist iff the leaf is 2074 * implemented, so we pass through CL iff leaf 1 2075 * exists. EDX can be copied from any existing index. 2076 */ 2077 if (function == 0xb || function == 0x1f) { 2078 entry = kvm_find_cpuid_entry_index(vcpu, function, 1); 2079 if (entry) { 2080 *ecx = index & 0xff; 2081 *edx = entry->edx; 2082 } 2083 } 2084 } 2085 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact, 2086 used_max_basic); 2087 return exact; 2088 } 2089 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_cpuid); 2090 2091 int kvm_emulate_cpuid(struct kvm_vcpu *vcpu) 2092 { 2093 u32 eax, ebx, ecx, edx; 2094 2095 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0)) 2096 return 1; 2097 2098 eax = kvm_rax_read(vcpu); 2099 ecx = kvm_rcx_read(vcpu); 2100 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false); 2101 kvm_rax_write(vcpu, eax); 2102 kvm_rbx_write(vcpu, ebx); 2103 kvm_rcx_write(vcpu, ecx); 2104 kvm_rdx_write(vcpu, edx); 2105 return kvm_skip_emulated_instruction(vcpu); 2106 } 2107 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_cpuid); 2108