^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Kernel-based Virtual Machine driver for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * cpuid support routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * derived from arch/x86/kvm/x86.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright 2011 Red Hat, Inc. and/or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright IBM Corporation, 2008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/fpu/xstate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "cpuid.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "lapic.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "mmu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "pmu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * aligned to sizeof(unsigned long) because it's not accessed via bitops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u32 kvm_cpu_caps[NCAPINTS] __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) EXPORT_SYMBOL_GPL(kvm_cpu_caps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static u32 xstate_required_size(u64 xstate_bv, bool compacted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int feature_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) xstate_bv &= XFEATURE_MASK_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) while (xstate_bv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (xstate_bv & 0x1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u32 eax, ebx, ecx, edx, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) offset = compacted ? ret : ebx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ret = max(ret, offset + eax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) xstate_bv >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) feature_bit++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define F feature_bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct kvm_cpuid_entry2 *entries, int nent, u32 function, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct kvm_cpuid_entry2 *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) for (i = 0; i < nent; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) e = &entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (e->function == function && (e->index == index ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) !(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * The existing code assumes virtual address is 48-bit or 57-bit in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * canonical address checks; exit if it is ever changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) best = cpuid_entry2_find(entries, nent, 0x80000008, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (best) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int vaddr_bits = (best->eax & 0xff00) >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * save the feature bitmap to avoid cpuid lookup for every PV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) vcpu->arch.pv_cpuid.features = best->eax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) best = kvm_find_cpuid_entry(vcpu, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (best) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Update OSXSAVE bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (boot_cpu_has(X86_FEATURE_XSAVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) cpuid_entry_change(best, X86_FEATURE_APIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) best = kvm_find_cpuid_entry(vcpu, 7, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) cpuid_entry_change(best, X86_FEATURE_OSPKE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (kvm_hlt_in_guest(vcpu->kvm) && best &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) cpuid_entry_change(best, X86_FEATURE_MWAIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) vcpu->arch.ia32_misc_enable_msr &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) MSR_IA32_MISC_ENABLE_MWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct kvm_lapic *apic = vcpu->arch.apic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) best = kvm_find_cpuid_entry(vcpu, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (best && apic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) apic->lapic_timer.timer_mode_mask = 3 << 17;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) apic->lapic_timer.timer_mode_mask = 1 << 17;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) kvm_apic_set_version(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) vcpu->arch.guest_supported_xcr0 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) vcpu->arch.guest_supported_xcr0 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) kvm_update_pv_runtime(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) kvm_mmu_reset_context(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) kvm_pmu_refresh(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) vcpu->arch.cr4_guest_rsvd_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) __cr4_reserved_bits(guest_cpuid_has, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) vcpu->arch.cr3_lm_rsvd_bits = rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* Invoke the vendor callback only after the above state is updated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) kvm_x86_ops.vcpu_after_set_cpuid(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int is_efer_nx(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return host_efer & EFER_NX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct kvm_cpuid_entry2 *e, *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) e = &vcpu->arch.cpuid_entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (e->function == 0x80000001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) entry = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (entry && cpuid_entry_has(entry, X86_FEATURE_NX) && !is_efer_nx()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) cpuid_entry_clear(entry, X86_FEATURE_NX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) printk(KERN_INFO "kvm: guest NX capability removed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!best || best->eax < 0x80000008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return best->eax & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) not_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return 36;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* when an old userspace process fills a new kernel module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct kvm_cpuid *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct kvm_cpuid_entry __user *entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int r, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct kvm_cpuid_entry *e = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct kvm_cpuid_entry2 *e2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (cpuid->nent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (IS_ERR(e))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return PTR_ERR(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!e2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) goto out_free_cpuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) for (i = 0; i < cpuid->nent; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) e2[i].function = e[i].function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) e2[i].eax = e[i].eax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) e2[i].ebx = e[i].ebx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) e2[i].ecx = e[i].ecx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) e2[i].edx = e[i].edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) e2[i].index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) e2[i].flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) e2[i].padding[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) e2[i].padding[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) e2[i].padding[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) r = kvm_check_cpuid(e2, cpuid->nent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) kvfree(e2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) goto out_free_cpuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) kvfree(vcpu->arch.cpuid_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) vcpu->arch.cpuid_entries = e2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) vcpu->arch.cpuid_nent = cpuid->nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) cpuid_fix_nx_cap(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kvm_update_cpuid_runtime(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) kvm_vcpu_after_set_cpuid(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) out_free_cpuid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) kvfree(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct kvm_cpuid2 *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct kvm_cpuid_entry2 __user *entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct kvm_cpuid_entry2 *e2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (cpuid->nent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (IS_ERR(e2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return PTR_ERR(e2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) r = kvm_check_cpuid(e2, cpuid->nent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) kvfree(e2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) kvfree(vcpu->arch.cpuid_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) vcpu->arch.cpuid_entries = e2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) vcpu->arch.cpuid_nent = cpuid->nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) kvm_update_cpuid_runtime(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) kvm_vcpu_after_set_cpuid(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct kvm_cpuid2 *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct kvm_cpuid_entry2 __user *entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) r = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (cpuid->nent < vcpu->arch.cpuid_nent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (copy_to_user(entries, vcpu->arch.cpuid_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) cpuid->nent = vcpu->arch.cpuid_nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct kvm_cpuid_entry2 entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) reverse_cpuid_check(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) kvm_cpu_caps[leaf] &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) cpuid_count(cpuid.function, cpuid.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) void kvm_set_cpu_caps(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) unsigned int f_nx = is_efer_nx() ? F(NX) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) unsigned int f_gbpages = F(GBPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) unsigned int f_lm = F(LM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) unsigned int f_gbpages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) unsigned int f_lm = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) BUILD_BUG_ON(sizeof(kvm_cpu_caps) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) sizeof(boot_cpu_data.x86_capability));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) sizeof(kvm_cpu_caps));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) kvm_cpu_cap_mask(CPUID_1_ECX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * advertised to guests via CPUID!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 0 /* DS-CPL, VMX, SMX, EST */ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) F(F16C) | F(RDRAND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /* KVM emulates x2apic in software irrespective of host support. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) kvm_cpu_cap_set(X86_FEATURE_X2APIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) kvm_cpu_cap_mask(CPUID_1_EDX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) F(FPU) | F(VME) | F(DE) | F(PSE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) F(TSC) | F(MSR) | F(PAE) | F(MCE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 0 /* Reserved, DS, ACPI */ | F(MMX) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 0 /* HTT, TM, Reserved, PBE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) kvm_cpu_cap_mask(CPUID_7_0_EBX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) F(BMI2) | F(ERMS) | 0 /*INVPCID*/ | F(RTM) | 0 /*MPX*/ | F(RDSEED) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | 0 /*INTEL_PT*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) kvm_cpu_cap_mask(CPUID_7_ECX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /* Set LA57 based on hardware capability. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (cpuid_ecx(7) & F(LA57))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) kvm_cpu_cap_set(X86_FEATURE_LA57);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * PKU not yet implemented for shadow paging and requires OSPKE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * to be set on the host. Clear it if that is not the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) kvm_cpu_cap_clear(X86_FEATURE_PKU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) kvm_cpu_cap_mask(CPUID_7_EDX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) F(SERIALIZE) | F(TSXLDTRK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (boot_cpu_has(X86_FEATURE_STIBP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) kvm_cpu_cap_mask(CPUID_7_1_EAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) F(AVX512_BF16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) kvm_cpu_cap_mask(CPUID_D_1_EAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) F(TOPOEXT) | F(PERFCTR_CORE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) F(FPU) | F(VME) | F(DE) | F(PSE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) F(TSC) | F(MSR) | F(PAE) | F(MCE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) F(PAT) | F(PSE36) | 0 /* Reserved */ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) F(CLZERO) | F(XSAVEERPTR) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * AMD has separate bits for each SPEC_CTRL bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * arch/x86/kernel/cpu/bugs.c is kind enough to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * record that in cpufeatures so use them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (boot_cpu_has(X86_FEATURE_IBPB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (boot_cpu_has(X86_FEATURE_IBRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (boot_cpu_has(X86_FEATURE_STIBP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * The preference is to use SPEC CTRL MSR instead of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * VIRT_SPEC MSR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) !boot_cpu_has(X86_FEATURE_AMD_SSBD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * Hide all SVM features by default, SVM will set the cap bits for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * features it emulates and/or exposes for L1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) F(PMM) | F(PMM_EN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct kvm_cpuid_array {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct kvm_cpuid_entry2 *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int maxnent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) u32 function, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct kvm_cpuid_entry2 *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (array->nent >= array->maxnent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) entry = &array->entries[array->nent++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) entry->function = function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) entry->index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) entry->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) cpuid_count(entry->function, entry->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) switch (function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) case 7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) case 0xb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) case 0xd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) case 0xf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) case 0x10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) case 0x12:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) case 0x14:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) case 0x17:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) case 0x18:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) case 0x1f:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) case 0x8000001d:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct kvm_cpuid_entry2 *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (array->nent >= array->maxnent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) entry = &array->entries[array->nent];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) entry->function = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) entry->index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) entry->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) switch (func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) entry->eax = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) ++array->nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) entry->ecx = F(MOVBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) ++array->nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) case 7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) entry->eax = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) entry->ecx = F(RDPID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) ++array->nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct kvm_cpuid_entry2 *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) int r, i, max_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /* all calls to cpuid_count() should be made on the same cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) get_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) r = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) entry = do_host_cpuid(array, function, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) switch (function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /* Limited to the highest leaf implemented in KVM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) entry->eax = min(entry->eax, 0x1fU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) cpuid_entry_override(entry, CPUID_1_EDX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) cpuid_entry_override(entry, CPUID_1_ECX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * On ancient CPUs, function 2 entries are STATEFUL. That is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * CPUID(function=2, index=0) may return different results each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * time, with the least-significant byte in EAX enumerating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * number of times software should do CPUID(2, 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * idiotic. Intel's SDM states that EAX & 0xff "will always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * return 01H. Software should ignore this value and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * interpret it as an informational descriptor", while AMD's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * APM states that CPUID(2) is reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * WARN if a frankenstein CPU that supports virtualization and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * a stateful CPUID.0x2 is encountered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) WARN_ON_ONCE((entry->eax & 0xff) > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /* functions 4 and 0x8000001d have additional index. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) case 0x8000001d:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * Read entries until the cache type in the previous entry is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * zero, i.e. indicates an invalid entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) for (i = 1; entry->eax & 0x1f; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) entry = do_host_cpuid(array, function, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) case 6: /* Thermal management */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) entry->eax = 0x4; /* allow ARAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) entry->ebx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) entry->ecx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) /* function 7 has additional index. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) case 7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) entry->eax = min(entry->eax, 1u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) cpuid_entry_override(entry, CPUID_7_0_EBX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) cpuid_entry_override(entry, CPUID_7_ECX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) cpuid_entry_override(entry, CPUID_7_EDX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (entry->eax == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) entry = do_host_cpuid(array, function, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) cpuid_entry_override(entry, CPUID_7_1_EAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) entry->ebx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) entry->ecx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) case 9:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) case 0xa: { /* Architectural Performance Monitoring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) struct x86_pmu_capability cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) union cpuid10_eax eax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) union cpuid10_edx edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) perf_get_x86_pmu_capability(&cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Only support guest architectural pmu on a host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * with architectural pmu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (!cap.version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) memset(&cap, 0, sizeof(cap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) eax.split.version_id = min(cap.version, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) eax.split.num_counters = cap.num_counters_gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) eax.split.bit_width = cap.bit_width_gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) eax.split.mask_length = cap.events_mask_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) edx.split.bit_width_fixed = cap.bit_width_fixed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (cap.version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) edx.split.anythread_deprecated = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) edx.split.reserved1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) edx.split.reserved2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) entry->eax = eax.full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) entry->ebx = cap.events_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) entry->ecx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) entry->edx = edx.full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * Per Intel's SDM, the 0x1f is a superset of 0xb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * thus they can be handled by common code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) case 0x1f:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) case 0xb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * Populate entries until the level type (ECX[15:8]) of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * the starting entry, filled by the primary do_host_cpuid().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) for (i = 1; entry->ecx & 0xff00; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) entry = do_host_cpuid(array, function, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) case 0xd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) entry->eax &= supported_xcr0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) entry->ebx = xstate_required_size(supported_xcr0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) entry->ecx = entry->ebx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) entry->edx &= supported_xcr0 >> 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (!supported_xcr0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) entry = do_host_cpuid(array, function, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) cpuid_entry_override(entry, CPUID_D_1_EAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (entry->eax & (F(XSAVES)|F(XSAVEC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) entry->ebx = xstate_required_size(supported_xcr0 | supported_xss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) WARN_ON_ONCE(supported_xss != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) entry->ebx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) entry->ecx &= supported_xss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) entry->edx &= supported_xss >> 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) for (i = 2; i < 64; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) bool s_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (supported_xcr0 & BIT_ULL(i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) s_state = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) else if (supported_xss & BIT_ULL(i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) s_state = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) entry = do_host_cpuid(array, function, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * The supported check above should have filtered out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * invalid sub-leafs. Only valid sub-leafs should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * reach this point, and they should have a non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * save state size. Furthermore, check whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * processor agrees with supported_xcr0/supported_xss
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * on whether this is an XCR0- or IA32_XSS-managed area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) --array->nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /* Intel PT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) case 0x14:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (!do_host_cpuid(array, function, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) case KVM_CPUID_SIGNATURE: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) static const char signature[12] = "KVMKVMKVM\0\0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) const u32 *sigptr = (const u32 *)signature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) entry->eax = KVM_CPUID_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) entry->ebx = sigptr[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) entry->ecx = sigptr[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) entry->edx = sigptr[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) case KVM_CPUID_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) (1 << KVM_FEATURE_NOP_IO_DELAY) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) (1 << KVM_FEATURE_CLOCKSOURCE2) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) (1 << KVM_FEATURE_ASYNC_PF) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) (1 << KVM_FEATURE_PV_EOI) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) (1 << KVM_FEATURE_PV_UNHALT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) (1 << KVM_FEATURE_PV_TLB_FLUSH) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) (1 << KVM_FEATURE_PV_SEND_IPI) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) (1 << KVM_FEATURE_POLL_CONTROL) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) (1 << KVM_FEATURE_PV_SCHED_YIELD) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) (1 << KVM_FEATURE_ASYNC_PF_INT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (sched_info_on())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) entry->ebx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) entry->ecx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) case 0x80000000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) entry->eax = min(entry->eax, 0x8000001f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) case 0x80000001:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) cpuid_entry_override(entry, CPUID_8000_0001_EDX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) cpuid_entry_override(entry, CPUID_8000_0001_ECX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) case 0x80000006:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) /* L2 cache and TLB: pass through host info. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) case 0x80000007: /* Advanced power management */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) /* invariant TSC is CPUID.80000007H:EDX[8] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) entry->edx &= (1 << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /* mask against host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) entry->edx &= boot_cpu_data.x86_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) entry->eax = entry->ebx = entry->ecx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) case 0x80000008: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) unsigned g_phys_as = (entry->eax >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) unsigned phys_as = entry->eax & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * Use bare metal's MAXPHADDR if the CPU doesn't report guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * MAXPHYADDR separately, or if TDP (NPT) is disabled, as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * guest version "applies only to guests using nested paging".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (!g_phys_as || !tdp_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) g_phys_as = phys_as;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) entry->eax = g_phys_as | (virt_as << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) cpuid_entry_override(entry, CPUID_8000_0008_EBX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) case 0x8000000A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) entry->eax = 1; /* SVM revision 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) ASID emulation to nested SVM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) entry->ecx = 0; /* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) cpuid_entry_override(entry, CPUID_8000_000A_EDX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) case 0x80000019:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) entry->ecx = entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) case 0x8000001a:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) case 0x8000001e:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) /* Support memory encryption cpuid if host supports it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) case 0x8000001F:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (!boot_cpu_has(X86_FEATURE_SEV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /*Add support for Centaur's CPUID instruction*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) case 0xC0000000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /*Just support up to 0xC0000004 now*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) entry->eax = min(entry->eax, 0xC0000004);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) case 0xC0000001:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) cpuid_entry_override(entry, CPUID_C000_0001_EDX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) case 3: /* Processor serial number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) case 5: /* MONITOR/MWAIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) case 0xC0000002:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) case 0xC0000003:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) case 0xC0000004:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) put_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) if (type == KVM_GET_EMULATED_CPUID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) return __do_cpuid_func_emulated(array, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) return __do_cpuid_func(array, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) #define CENTAUR_CPUID_SIGNATURE 0xC0000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) u32 limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (func == CENTAUR_CPUID_SIGNATURE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) r = do_cpuid_func(array, func, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) limit = array->entries[array->nent - 1].eax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) for (func = func + 1; func <= limit; ++func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) r = do_cpuid_func(array, func, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) __u32 num_entries, unsigned int ioctl_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) __u32 pad[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (ioctl_type != KVM_GET_EMULATED_CPUID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * We want to make sure that ->padding is being passed clean from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * userspace in case we want to use it for something in the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) * have to give ourselves satisfied only with the emulated side. /me
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * sheds a tear.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) for (i = 0; i < num_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (pad[0] || pad[1] || pad[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) struct kvm_cpuid_entry2 __user *entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) static const u32 funcs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) struct kvm_cpuid_array array = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) .nent = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) int r, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (cpuid->nent < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) cpuid->nent = KVM_MAX_CPUID_ENTRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) if (sanity_check_entries(entries, cpuid->nent, type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) cpuid->nent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) if (!array.entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) array.maxnent = cpuid->nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) for (i = 0; i < ARRAY_SIZE(funcs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) r = get_cpuid_func(&array, funcs[i], type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) cpuid->nent = array.nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (copy_to_user(entries, array.entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) array.nent * sizeof(struct kvm_cpuid_entry2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) vfree(array.entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) u32 function, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) function, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * Intel CPUID semantics treats any query for an out-of-range leaf as if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * returns all zeroes for any undefined leaf, whether or not the leaf is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * range. Centaur/VIA follows Intel semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) * A leaf is considered out-of-range if its function is higher than the maximum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) * supported leaf of its associated class or if its associated class does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) * exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) * There are three primary classes to be considered, with their respective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) * class exists if a guest CPUID entry for its <base> leaf exists. For a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) * class, CPUID.<base>.EAX contains the max supported leaf for the class.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * - Hypervisor: 0x40000000 - 0x4fffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * - Extended: 0x80000000 - 0xbfffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * - Centaur: 0xc0000000 - 0xcfffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * The Hypervisor class is further subdivided into sub-classes that each act as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * their own indepdent class associated with a 0x100 byte range. E.g. if Qemu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * is advertising support for both HyperV and KVM, the resulting Hypervisor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * CPUID sub-classes are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) * - HyperV: 0x40000000 - 0x400000ff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * - KVM: 0x40000100 - 0x400001ff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) static struct kvm_cpuid_entry2 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) struct kvm_cpuid_entry2 *basic, *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) u32 function = *fn_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) basic = kvm_find_cpuid_entry(vcpu, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (!basic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (function >= 0x40000000 && function <= 0x4fffffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) else if (function >= 0xc0000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) if (class && function <= class->eax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) * Leaf specific adjustments are also applied when redirecting to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) * max basic entry, e.g. if the max basic leaf is 0xb but there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * entry for CPUID.0xb.index (see below), then the output value for EDX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) * needs to be pulled from CPUID.0xb.1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) *fn_ptr = basic->eax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * The class does not exist or the requested function is out of range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) * the effective CPUID entry is the max basic leaf. Note, the index of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * the original requested leaf is observed!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) return kvm_find_cpuid_entry(vcpu, basic->eax, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) u32 *ecx, u32 *edx, bool exact_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) u32 orig_function = *eax, function = *eax, index = *ecx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) struct kvm_cpuid_entry2 *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) bool exact, used_max_basic = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) entry = kvm_find_cpuid_entry(vcpu, function, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) exact = !!entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) if (!entry && !exact_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) used_max_basic = !!entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) *eax = entry->eax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) *ebx = entry->ebx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) *ecx = entry->ecx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) *edx = entry->edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (function == 7 && index == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) u64 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) (data & TSX_CTRL_CPUID_CLEAR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) *ebx &= ~(F(RTM) | F(HLE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) *eax = *ebx = *ecx = *edx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) * When leaf 0BH or 1FH is defined, CL is pass-through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) * and EDX is always the x2APIC ID, even for undefined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) * subleaves. Index 1 will exist iff the leaf is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) * implemented, so we pass through CL iff leaf 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) * exists. EDX can be copied from any existing index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (function == 0xb || function == 0x1f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) entry = kvm_find_cpuid_entry(vcpu, function, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) *ecx = index & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) *edx = entry->edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) used_max_basic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) return exact;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) EXPORT_SYMBOL_GPL(kvm_cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) u32 eax, ebx, ecx, edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) eax = kvm_rax_read(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) ecx = kvm_rcx_read(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) kvm_rax_write(vcpu, eax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) kvm_rbx_write(vcpu, ebx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) kvm_rcx_write(vcpu, ecx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) kvm_rdx_write(vcpu, edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) return kvm_skip_emulated_instruction(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);