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