1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef ARCH_X86_KVM_CPUID_H
3 #define ARCH_X86_KVM_CPUID_H
6 #include "reverse_cpuid.h"
8 #include <asm/processor.h>
9 #include <uapi/asm/kvm_para.h>
11 extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
12 void kvm_set_cpu_caps(void);
14 void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu);
15 void kvm_update_pv_runtime(struct kvm_vcpu *vcpu);
16 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
17 u32 function, u32 index);
18 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
19 struct kvm_cpuid_entry2 __user *entries,
21 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
22 struct kvm_cpuid *cpuid,
23 struct kvm_cpuid_entry __user *entries);
24 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
25 struct kvm_cpuid2 *cpuid,
26 struct kvm_cpuid_entry2 __user *entries);
27 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
28 struct kvm_cpuid2 *cpuid,
29 struct kvm_cpuid_entry2 __user *entries);
30 bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
31 u32 *ecx, u32 *edx, bool exact_only);
33 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu);
34 u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu);
36 static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
38 return vcpu->arch.maxphyaddr;
41 static inline bool kvm_vcpu_is_legal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
43 return !(gpa & vcpu->arch.reserved_gpa_bits);
46 static inline bool kvm_vcpu_is_illegal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
48 return !kvm_vcpu_is_legal_gpa(vcpu, gpa);
51 static inline bool kvm_vcpu_is_legal_aligned_gpa(struct kvm_vcpu *vcpu,
52 gpa_t gpa, gpa_t alignment)
54 return IS_ALIGNED(gpa, alignment) && kvm_vcpu_is_legal_gpa(vcpu, gpa);
57 static inline bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
59 return kvm_vcpu_is_legal_aligned_gpa(vcpu, gpa, PAGE_SIZE);
62 static __always_inline void cpuid_entry_override(struct kvm_cpuid_entry2 *entry,
65 u32 *reg = cpuid_entry_get_reg(entry, leaf * 32);
67 BUILD_BUG_ON(leaf >= ARRAY_SIZE(kvm_cpu_caps));
68 *reg = kvm_cpu_caps[leaf];
71 static __always_inline u32 *guest_cpuid_get_register(struct kvm_vcpu *vcpu,
72 unsigned int x86_feature)
74 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
75 struct kvm_cpuid_entry2 *entry;
77 entry = kvm_find_cpuid_entry(vcpu, cpuid.function, cpuid.index);
81 return __cpuid_entry_get_reg(entry, cpuid.reg);
84 static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu,
85 unsigned int x86_feature)
89 reg = guest_cpuid_get_register(vcpu, x86_feature);
93 return *reg & __feature_bit(x86_feature);
96 static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu,
97 unsigned int x86_feature)
101 reg = guest_cpuid_get_register(vcpu, x86_feature);
103 *reg &= ~__feature_bit(x86_feature);
106 static inline bool guest_cpuid_is_amd_or_hygon(struct kvm_vcpu *vcpu)
108 struct kvm_cpuid_entry2 *best;
110 best = kvm_find_cpuid_entry(vcpu, 0, 0);
112 (is_guest_vendor_amd(best->ebx, best->ecx, best->edx) ||
113 is_guest_vendor_hygon(best->ebx, best->ecx, best->edx));
116 static inline bool guest_cpuid_is_intel(struct kvm_vcpu *vcpu)
118 struct kvm_cpuid_entry2 *best;
120 best = kvm_find_cpuid_entry(vcpu, 0, 0);
121 return best && is_guest_vendor_intel(best->ebx, best->ecx, best->edx);
124 static inline int guest_cpuid_family(struct kvm_vcpu *vcpu)
126 struct kvm_cpuid_entry2 *best;
128 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
132 return x86_family(best->eax);
135 static inline int guest_cpuid_model(struct kvm_vcpu *vcpu)
137 struct kvm_cpuid_entry2 *best;
139 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
143 return x86_model(best->eax);
146 static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
148 struct kvm_cpuid_entry2 *best;
150 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
154 return x86_stepping(best->eax);
157 static inline bool guest_has_spec_ctrl_msr(struct kvm_vcpu *vcpu)
159 return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) ||
160 guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) ||
161 guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) ||
162 guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD));
165 static inline bool guest_has_pred_cmd_msr(struct kvm_vcpu *vcpu)
167 return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) ||
168 guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB));
171 static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
173 return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
176 static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
178 return vcpu->arch.msr_misc_features_enables &
179 MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
182 static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
184 unsigned int x86_leaf = __feature_leaf(x86_feature);
186 reverse_cpuid_check(x86_leaf);
187 kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature);
190 static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature)
192 unsigned int x86_leaf = __feature_leaf(x86_feature);
194 reverse_cpuid_check(x86_leaf);
195 kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
198 static __always_inline u32 kvm_cpu_cap_get(unsigned int x86_feature)
200 unsigned int x86_leaf = __feature_leaf(x86_feature);
202 reverse_cpuid_check(x86_leaf);
203 return kvm_cpu_caps[x86_leaf] & __feature_bit(x86_feature);
206 static __always_inline bool kvm_cpu_cap_has(unsigned int x86_feature)
208 return !!kvm_cpu_cap_get(x86_feature);
211 static __always_inline void kvm_cpu_cap_check_and_set(unsigned int x86_feature)
213 if (boot_cpu_has(x86_feature))
214 kvm_cpu_cap_set(x86_feature);
217 static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu,
218 unsigned int kvm_feature)
220 if (!vcpu->arch.pv_cpuid.enforce)
223 return vcpu->arch.pv_cpuid.features & (1u << kvm_feature);