^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef ARCH_X86_KVM_CPUID_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define ARCH_X86_KVM_CPUID_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "x86.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <uapi/asm/kvm_para.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) extern u32 kvm_cpu_caps[NCAPINTS] __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) void kvm_set_cpu_caps(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) void kvm_update_pv_runtime(struct kvm_vcpu *vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) u32 function, u32 index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct kvm_cpuid_entry2 __user *entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned int type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct kvm_cpuid *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct kvm_cpuid_entry __user *entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct kvm_cpuid2 *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct kvm_cpuid_entry2 __user *entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct kvm_cpuid2 *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct kvm_cpuid_entry2 __user *entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u32 *ecx, u32 *edx, bool exact_only);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return vcpu->arch.maxphyaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static inline bool kvm_vcpu_is_illegal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return (gpa >= BIT_ULL(cpuid_maxphyaddr(vcpu)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct cpuid_reg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u32 function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static const struct cpuid_reg reverse_cpuid[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) [CPUID_1_EDX] = { 1, 0, CPUID_EDX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) [CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) [CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) [CPUID_1_ECX] = { 1, 0, CPUID_ECX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) [CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) [CPUID_8000_0001_ECX] = {0x80000001, 0, CPUID_ECX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) [CPUID_7_0_EBX] = { 7, 0, CPUID_EBX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) [CPUID_D_1_EAX] = { 0xd, 1, CPUID_EAX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) [CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) [CPUID_6_EAX] = { 6, 0, CPUID_EAX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) [CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) [CPUID_7_ECX] = { 7, 0, CPUID_ECX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) [CPUID_8000_0007_EBX] = {0x80000007, 0, CPUID_EBX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) [CPUID_7_EDX] = { 7, 0, CPUID_EDX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) [CPUID_7_1_EAX] = { 7, 1, CPUID_EAX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Reverse CPUID and its derivatives can only be used for hardware-defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * feature words, i.e. words whose bits directly correspond to a CPUID leaf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Retrieving a feature bit or masking guest CPUID from a Linux-defined word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * is nonsensical as the bit number/mask is an arbitrary software-defined value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * and can't be used by KVM to query/control guest capabilities. And obviously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * the leaf being queried must have an entry in the lookup table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static __always_inline void reverse_cpuid_check(unsigned int x86_leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) BUILD_BUG_ON(x86_leaf == CPUID_LNX_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) BUILD_BUG_ON(x86_leaf == CPUID_LNX_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) BUILD_BUG_ON(x86_leaf == CPUID_LNX_3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) BUILD_BUG_ON(x86_leaf == CPUID_LNX_4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * Retrieve the bit mask from an X86_FEATURE_* definition. Features contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * the hardware defined bit number (stored in bits 4:0) and a software defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * "word" (stored in bits 31:5). The word is used to index into arrays of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * bit masks that hold the per-cpu feature capabilities, e.g. this_cpu_has().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static __always_inline u32 __feature_bit(int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) reverse_cpuid_check(x86_feature / 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 1 << (x86_feature & 31);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define feature_bit(name) __feature_bit(X86_FEATURE_##name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned int x86_leaf = x86_feature / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) reverse_cpuid_check(x86_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return reverse_cpuid[x86_leaf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static __always_inline u32 *__cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u32 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) case CPUID_EAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return &entry->eax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) case CPUID_EBX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return &entry->ebx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) case CPUID_ECX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return &entry->ecx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) case CPUID_EDX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return &entry->edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) BUILD_BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static __always_inline u32 *cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return __cpuid_entry_get_reg(entry, cpuid.reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static __always_inline u32 cpuid_entry_get(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return *reg & __feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static __always_inline bool cpuid_entry_has(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return cpuid_entry_get(entry, x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static __always_inline void cpuid_entry_clear(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *reg &= ~__feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static __always_inline void cpuid_entry_set(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *reg |= __feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static __always_inline void cpuid_entry_change(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned int x86_feature,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) bool set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Open coded instead of using cpuid_entry_{clear,set}() to coerce the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * compiler into using CMOV instead of Jcc when possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *reg |= __feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *reg &= ~__feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static __always_inline void cpuid_entry_override(struct kvm_cpuid_entry2 *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) enum cpuid_leafs leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u32 *reg = cpuid_entry_get_reg(entry, leaf * 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) BUILD_BUG_ON(leaf >= ARRAY_SIZE(kvm_cpu_caps));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *reg = kvm_cpu_caps[leaf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static __always_inline u32 *guest_cpuid_get_register(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct kvm_cpuid_entry2 *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) entry = kvm_find_cpuid_entry(vcpu, cpuid.function, cpuid.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return __cpuid_entry_get_reg(entry, cpuid.reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) u32 *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) reg = guest_cpuid_get_register(vcpu, x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return *reg & __feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) u32 *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) reg = guest_cpuid_get_register(vcpu, x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) *reg &= ~__feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static inline bool guest_cpuid_is_amd_or_hygon(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) best = kvm_find_cpuid_entry(vcpu, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return best &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) (is_guest_vendor_amd(best->ebx, best->ecx, best->edx) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) is_guest_vendor_hygon(best->ebx, best->ecx, best->edx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static inline int guest_cpuid_family(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return x86_family(best->eax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static inline int guest_cpuid_model(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return x86_model(best->eax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct kvm_cpuid_entry2 *best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!best)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return x86_stepping(best->eax);
^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) static inline bool guest_has_spec_ctrl_msr(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static inline bool guest_has_pred_cmd_msr(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB));
^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) static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return vcpu->arch.msr_misc_features_enables &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) unsigned int x86_leaf = x86_feature / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) reverse_cpuid_check(x86_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) unsigned int x86_leaf = x86_feature / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) reverse_cpuid_check(x86_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static __always_inline u32 kvm_cpu_cap_get(unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) unsigned int x86_leaf = x86_feature / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) reverse_cpuid_check(x86_leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return kvm_cpu_caps[x86_leaf] & __feature_bit(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static __always_inline bool kvm_cpu_cap_has(unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return !!kvm_cpu_cap_get(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static __always_inline void kvm_cpu_cap_check_and_set(unsigned int x86_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (boot_cpu_has(x86_feature))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) kvm_cpu_cap_set(x86_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static inline bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) unsigned int kvm_feature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (!vcpu->arch.pv_cpuid.enforce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return vcpu->arch.pv_cpuid.features & (1u << kvm_feature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) #endif