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