1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef ARCH_X86_KVM_CPUID_H 3 #define ARCH_X86_KVM_CPUID_H 4 5 #include "reverse_cpuid.h" 6 #include <asm/cpu.h> 7 #include <asm/processor.h> 8 #include <uapi/asm/kvm_para.h> 9 10 extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly; 11 void kvm_set_cpu_caps(void); 12 13 void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu); 14 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu, 15 u32 function, u32 index); 16 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, 17 u32 function); 18 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, 19 struct kvm_cpuid_entry2 __user *entries, 20 unsigned int type); 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); 32 33 void __init kvm_init_xstate_sizes(void); 34 u32 xstate_required_size(u64 xstate_bv, bool compacted); 35 36 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu); 37 u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu); 38 39 static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) 40 { 41 return vcpu->arch.maxphyaddr; 42 } 43 44 static inline bool kvm_vcpu_is_legal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa) 45 { 46 return !(gpa & vcpu->arch.reserved_gpa_bits); 47 } 48 49 static inline bool kvm_vcpu_is_legal_aligned_gpa(struct kvm_vcpu *vcpu, 50 gpa_t gpa, gpa_t alignment) 51 { 52 return IS_ALIGNED(gpa, alignment) && kvm_vcpu_is_legal_gpa(vcpu, gpa); 53 } 54 55 static inline bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) 56 { 57 return kvm_vcpu_is_legal_aligned_gpa(vcpu, gpa, PAGE_SIZE); 58 } 59 60 static __always_inline void cpuid_entry_override(struct kvm_cpuid_entry2 *entry, 61 unsigned int leaf) 62 { 63 u32 *reg = cpuid_entry_get_reg(entry, leaf * 32); 64 65 BUILD_BUG_ON(leaf >= ARRAY_SIZE(kvm_cpu_caps)); 66 *reg = kvm_cpu_caps[leaf]; 67 } 68 69 static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, 70 unsigned int x86_feature) 71 { 72 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature); 73 struct kvm_cpuid_entry2 *entry; 74 u32 *reg; 75 76 /* 77 * XSAVES is a special snowflake. Due to lack of a dedicated intercept 78 * on SVM, KVM must assume that XSAVES (and thus XRSTORS) is usable by 79 * the guest if the host supports XSAVES and *XSAVE* is exposed to the 80 * guest. Because the guest can execute XSAVES and XRSTORS, i.e. can 81 * indirectly consume XSS, KVM must ensure XSS is zeroed when running 82 * the guest, i.e. must set XSAVES in vCPU capabilities. But to reject 83 * direct XSS reads and writes (to minimize the virtualization hole and 84 * honor userspace's CPUID), KVM needs to check the raw guest CPUID, 85 * not KVM's view of guest capabilities. 86 * 87 * For all other features, guest capabilities are accurate. Expand 88 * this allowlist with extreme vigilance. 89 */ 90 BUILD_BUG_ON(x86_feature != X86_FEATURE_XSAVES); 91 92 entry = kvm_find_cpuid_entry_index(vcpu, cpuid.function, cpuid.index); 93 if (!entry) 94 return NULL; 95 96 reg = __cpuid_entry_get_reg(entry, cpuid.reg); 97 if (!reg) 98 return false; 99 100 return *reg & __feature_bit(x86_feature); 101 } 102 103 static inline bool guest_cpuid_is_amd_compatible(struct kvm_vcpu *vcpu) 104 { 105 return vcpu->arch.is_amd_compatible; 106 } 107 108 static inline bool guest_cpuid_is_intel_compatible(struct kvm_vcpu *vcpu) 109 { 110 return !guest_cpuid_is_amd_compatible(vcpu); 111 } 112 113 static inline int guest_cpuid_family(struct kvm_vcpu *vcpu) 114 { 115 struct kvm_cpuid_entry2 *best; 116 117 best = kvm_find_cpuid_entry(vcpu, 0x1); 118 if (!best) 119 return -1; 120 121 return x86_family(best->eax); 122 } 123 124 static inline int guest_cpuid_model(struct kvm_vcpu *vcpu) 125 { 126 struct kvm_cpuid_entry2 *best; 127 128 best = kvm_find_cpuid_entry(vcpu, 0x1); 129 if (!best) 130 return -1; 131 132 return x86_model(best->eax); 133 } 134 135 static inline bool cpuid_model_is_consistent(struct kvm_vcpu *vcpu) 136 { 137 return boot_cpu_data.x86_model == guest_cpuid_model(vcpu); 138 } 139 140 static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu) 141 { 142 struct kvm_cpuid_entry2 *best; 143 144 best = kvm_find_cpuid_entry(vcpu, 0x1); 145 if (!best) 146 return -1; 147 148 return x86_stepping(best->eax); 149 } 150 151 static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu) 152 { 153 return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT; 154 } 155 156 static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu) 157 { 158 return vcpu->arch.msr_misc_features_enables & 159 MSR_MISC_FEATURES_ENABLES_CPUID_FAULT; 160 } 161 162 static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature) 163 { 164 unsigned int x86_leaf = __feature_leaf(x86_feature); 165 166 kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature); 167 } 168 169 static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature) 170 { 171 unsigned int x86_leaf = __feature_leaf(x86_feature); 172 173 kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature); 174 } 175 176 static __always_inline u32 kvm_cpu_cap_get(unsigned int x86_feature) 177 { 178 unsigned int x86_leaf = __feature_leaf(x86_feature); 179 180 return kvm_cpu_caps[x86_leaf] & __feature_bit(x86_feature); 181 } 182 183 static __always_inline bool kvm_cpu_cap_has(unsigned int x86_feature) 184 { 185 return !!kvm_cpu_cap_get(x86_feature); 186 } 187 188 static __always_inline void kvm_cpu_cap_check_and_set(unsigned int x86_feature) 189 { 190 if (boot_cpu_has(x86_feature)) 191 kvm_cpu_cap_set(x86_feature); 192 } 193 194 static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu, 195 unsigned int kvm_feature) 196 { 197 if (!vcpu->arch.pv_cpuid.enforce) 198 return true; 199 200 return vcpu->arch.pv_cpuid.features & (1u << kvm_feature); 201 } 202 203 static __always_inline void guest_cpu_cap_set(struct kvm_vcpu *vcpu, 204 unsigned int x86_feature) 205 { 206 unsigned int x86_leaf = __feature_leaf(x86_feature); 207 208 vcpu->arch.cpu_caps[x86_leaf] |= __feature_bit(x86_feature); 209 } 210 211 static __always_inline void guest_cpu_cap_clear(struct kvm_vcpu *vcpu, 212 unsigned int x86_feature) 213 { 214 unsigned int x86_leaf = __feature_leaf(x86_feature); 215 216 vcpu->arch.cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature); 217 } 218 219 static __always_inline void guest_cpu_cap_change(struct kvm_vcpu *vcpu, 220 unsigned int x86_feature, 221 bool guest_has_cap) 222 { 223 if (guest_has_cap) 224 guest_cpu_cap_set(vcpu, x86_feature); 225 else 226 guest_cpu_cap_clear(vcpu, x86_feature); 227 } 228 229 static __always_inline bool guest_cpu_cap_has(struct kvm_vcpu *vcpu, 230 unsigned int x86_feature) 231 { 232 unsigned int x86_leaf = __feature_leaf(x86_feature); 233 234 /* 235 * Except for MWAIT, querying dynamic feature bits is disallowed, so 236 * that KVM can defer runtime updates until the next CPUID emulation. 237 */ 238 BUILD_BUG_ON(x86_feature == X86_FEATURE_APIC || 239 x86_feature == X86_FEATURE_OSXSAVE || 240 x86_feature == X86_FEATURE_OSPKE); 241 242 return vcpu->arch.cpu_caps[x86_leaf] & __feature_bit(x86_feature); 243 } 244 245 static inline bool kvm_vcpu_is_legal_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) 246 { 247 if (guest_cpu_cap_has(vcpu, X86_FEATURE_LAM)) 248 cr3 &= ~(X86_CR3_LAM_U48 | X86_CR3_LAM_U57); 249 250 return kvm_vcpu_is_legal_gpa(vcpu, cr3); 251 } 252 253 static inline bool guest_has_spec_ctrl_msr(struct kvm_vcpu *vcpu) 254 { 255 return (guest_cpu_cap_has(vcpu, X86_FEATURE_SPEC_CTRL) || 256 guest_cpu_cap_has(vcpu, X86_FEATURE_AMD_STIBP) || 257 guest_cpu_cap_has(vcpu, X86_FEATURE_AMD_IBRS) || 258 guest_cpu_cap_has(vcpu, X86_FEATURE_AMD_SSBD)); 259 } 260 261 static inline bool guest_has_pred_cmd_msr(struct kvm_vcpu *vcpu) 262 { 263 return (guest_cpu_cap_has(vcpu, X86_FEATURE_SPEC_CTRL) || 264 guest_cpu_cap_has(vcpu, X86_FEATURE_AMD_IBPB) || 265 guest_cpu_cap_has(vcpu, X86_FEATURE_SBPB)); 266 } 267 268 #endif 269