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_update_cpuid_runtime(struct kvm_vcpu *vcpu); 14 void kvm_update_pv_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 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 u32 *guest_cpuid_get_register(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 75 entry = kvm_find_cpuid_entry_index(vcpu, cpuid.function, cpuid.index); 76 if (!entry) 77 return NULL; 78 79 return __cpuid_entry_get_reg(entry, cpuid.reg); 80 } 81 82 static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, 83 unsigned int x86_feature) 84 { 85 u32 *reg; 86 87 reg = guest_cpuid_get_register(vcpu, x86_feature); 88 if (!reg) 89 return false; 90 91 return *reg & __feature_bit(x86_feature); 92 } 93 94 static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu, 95 unsigned int x86_feature) 96 { 97 u32 *reg; 98 99 reg = guest_cpuid_get_register(vcpu, x86_feature); 100 if (reg) 101 *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 guest_has_spec_ctrl_msr(struct kvm_vcpu *vcpu) 153 { 154 return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) || 155 guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) || 156 guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) || 157 guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)); 158 } 159 160 static inline bool guest_has_pred_cmd_msr(struct kvm_vcpu *vcpu) 161 { 162 return (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) || 163 guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB) || 164 guest_cpuid_has(vcpu, X86_FEATURE_SBPB)); 165 } 166 167 static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu) 168 { 169 return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT; 170 } 171 172 static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu) 173 { 174 return vcpu->arch.msr_misc_features_enables & 175 MSR_MISC_FEATURES_ENABLES_CPUID_FAULT; 176 } 177 178 static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature) 179 { 180 unsigned int x86_leaf = __feature_leaf(x86_feature); 181 182 reverse_cpuid_check(x86_leaf); 183 kvm_cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature); 184 } 185 186 static __always_inline void kvm_cpu_cap_set(unsigned int x86_feature) 187 { 188 unsigned int x86_leaf = __feature_leaf(x86_feature); 189 190 reverse_cpuid_check(x86_leaf); 191 kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature); 192 } 193 194 static __always_inline u32 kvm_cpu_cap_get(unsigned int x86_feature) 195 { 196 unsigned int x86_leaf = __feature_leaf(x86_feature); 197 198 reverse_cpuid_check(x86_leaf); 199 return kvm_cpu_caps[x86_leaf] & __feature_bit(x86_feature); 200 } 201 202 static __always_inline bool kvm_cpu_cap_has(unsigned int x86_feature) 203 { 204 return !!kvm_cpu_cap_get(x86_feature); 205 } 206 207 static __always_inline void kvm_cpu_cap_check_and_set(unsigned int x86_feature) 208 { 209 if (boot_cpu_has(x86_feature)) 210 kvm_cpu_cap_set(x86_feature); 211 } 212 213 static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu, 214 unsigned int kvm_feature) 215 { 216 if (!vcpu->arch.pv_cpuid.enforce) 217 return true; 218 219 return vcpu->arch.pv_cpuid.features & (1u << kvm_feature); 220 } 221 222 enum kvm_governed_features { 223 #define KVM_GOVERNED_FEATURE(x) KVM_GOVERNED_##x, 224 #include "governed_features.h" 225 KVM_NR_GOVERNED_FEATURES 226 }; 227 228 static __always_inline int kvm_governed_feature_index(unsigned int x86_feature) 229 { 230 switch (x86_feature) { 231 #define KVM_GOVERNED_FEATURE(x) case x: return KVM_GOVERNED_##x; 232 #include "governed_features.h" 233 default: 234 return -1; 235 } 236 } 237 238 static __always_inline bool kvm_is_governed_feature(unsigned int x86_feature) 239 { 240 return kvm_governed_feature_index(x86_feature) >= 0; 241 } 242 243 static __always_inline void kvm_governed_feature_set(struct kvm_vcpu *vcpu, 244 unsigned int x86_feature) 245 { 246 BUILD_BUG_ON(!kvm_is_governed_feature(x86_feature)); 247 248 __set_bit(kvm_governed_feature_index(x86_feature), 249 vcpu->arch.governed_features.enabled); 250 } 251 252 static __always_inline void kvm_governed_feature_check_and_set(struct kvm_vcpu *vcpu, 253 unsigned int x86_feature) 254 { 255 if (kvm_cpu_cap_has(x86_feature) && guest_cpuid_has(vcpu, x86_feature)) 256 kvm_governed_feature_set(vcpu, x86_feature); 257 } 258 259 static __always_inline bool guest_can_use(struct kvm_vcpu *vcpu, 260 unsigned int x86_feature) 261 { 262 BUILD_BUG_ON(!kvm_is_governed_feature(x86_feature)); 263 264 return test_bit(kvm_governed_feature_index(x86_feature), 265 vcpu->arch.governed_features.enabled); 266 } 267 268 static inline bool kvm_vcpu_is_legal_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) 269 { 270 if (guest_can_use(vcpu, X86_FEATURE_LAM)) 271 cr3 &= ~(X86_CR3_LAM_U48 | X86_CR3_LAM_U57); 272 273 return kvm_vcpu_is_legal_gpa(vcpu, cr3); 274 } 275 276 #endif 277