1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2018, Google LLC. 4 */ 5 6 #ifndef SELFTEST_KVM_PROCESSOR_H 7 #define SELFTEST_KVM_PROCESSOR_H 8 9 #include <assert.h> 10 #include <stdint.h> 11 #include <syscall.h> 12 13 #include <asm/msr-index.h> 14 #include <asm/prctl.h> 15 16 #include <linux/kvm_para.h> 17 #include <linux/stringify.h> 18 19 #include "kvm_util.h" 20 #include "ucall_common.h" 21 22 extern bool host_cpu_is_intel; 23 extern bool host_cpu_is_amd; 24 extern uint64_t guest_tsc_khz; 25 26 #ifndef MAX_NR_CPUID_ENTRIES 27 #define MAX_NR_CPUID_ENTRIES 100 28 #endif 29 30 #define NONCANONICAL 0xaaaaaaaaaaaaaaaaull 31 32 /* Forced emulation prefix, used to invoke the emulator unconditionally. */ 33 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';" 34 35 #define NMI_VECTOR 0x02 36 37 #define X86_EFLAGS_FIXED (1u << 1) 38 39 #define X86_CR4_VME (1ul << 0) 40 #define X86_CR4_PVI (1ul << 1) 41 #define X86_CR4_TSD (1ul << 2) 42 #define X86_CR4_DE (1ul << 3) 43 #define X86_CR4_PSE (1ul << 4) 44 #define X86_CR4_PAE (1ul << 5) 45 #define X86_CR4_MCE (1ul << 6) 46 #define X86_CR4_PGE (1ul << 7) 47 #define X86_CR4_PCE (1ul << 8) 48 #define X86_CR4_OSFXSR (1ul << 9) 49 #define X86_CR4_OSXMMEXCPT (1ul << 10) 50 #define X86_CR4_UMIP (1ul << 11) 51 #define X86_CR4_LA57 (1ul << 12) 52 #define X86_CR4_VMXE (1ul << 13) 53 #define X86_CR4_SMXE (1ul << 14) 54 #define X86_CR4_FSGSBASE (1ul << 16) 55 #define X86_CR4_PCIDE (1ul << 17) 56 #define X86_CR4_OSXSAVE (1ul << 18) 57 #define X86_CR4_SMEP (1ul << 20) 58 #define X86_CR4_SMAP (1ul << 21) 59 #define X86_CR4_PKE (1ul << 22) 60 61 struct xstate_header { 62 u64 xstate_bv; 63 u64 xcomp_bv; 64 u64 reserved[6]; 65 } __attribute__((packed)); 66 67 struct xstate { 68 u8 i387[512]; 69 struct xstate_header header; 70 u8 extended_state_area[0]; 71 } __attribute__ ((packed, aligned (64))); 72 73 #define XFEATURE_MASK_FP BIT_ULL(0) 74 #define XFEATURE_MASK_SSE BIT_ULL(1) 75 #define XFEATURE_MASK_YMM BIT_ULL(2) 76 #define XFEATURE_MASK_BNDREGS BIT_ULL(3) 77 #define XFEATURE_MASK_BNDCSR BIT_ULL(4) 78 #define XFEATURE_MASK_OPMASK BIT_ULL(5) 79 #define XFEATURE_MASK_ZMM_Hi256 BIT_ULL(6) 80 #define XFEATURE_MASK_Hi16_ZMM BIT_ULL(7) 81 #define XFEATURE_MASK_PT BIT_ULL(8) 82 #define XFEATURE_MASK_PKRU BIT_ULL(9) 83 #define XFEATURE_MASK_PASID BIT_ULL(10) 84 #define XFEATURE_MASK_CET_USER BIT_ULL(11) 85 #define XFEATURE_MASK_CET_KERNEL BIT_ULL(12) 86 #define XFEATURE_MASK_LBR BIT_ULL(15) 87 #define XFEATURE_MASK_XTILE_CFG BIT_ULL(17) 88 #define XFEATURE_MASK_XTILE_DATA BIT_ULL(18) 89 90 #define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK | \ 91 XFEATURE_MASK_ZMM_Hi256 | \ 92 XFEATURE_MASK_Hi16_ZMM) 93 #define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILE_DATA | \ 94 XFEATURE_MASK_XTILE_CFG) 95 96 /* Note, these are ordered alphabetically to match kvm_cpuid_entry2. Eww. */ 97 enum cpuid_output_regs { 98 KVM_CPUID_EAX, 99 KVM_CPUID_EBX, 100 KVM_CPUID_ECX, 101 KVM_CPUID_EDX 102 }; 103 104 /* 105 * Pack the information into a 64-bit value so that each X86_FEATURE_XXX can be 106 * passed by value with no overhead. 107 */ 108 struct kvm_x86_cpu_feature { 109 u32 function; 110 u16 index; 111 u8 reg; 112 u8 bit; 113 }; 114 #define KVM_X86_CPU_FEATURE(fn, idx, gpr, __bit) \ 115 ({ \ 116 struct kvm_x86_cpu_feature feature = { \ 117 .function = fn, \ 118 .index = idx, \ 119 .reg = KVM_CPUID_##gpr, \ 120 .bit = __bit, \ 121 }; \ 122 \ 123 kvm_static_assert((fn & 0xc0000000) == 0 || \ 124 (fn & 0xc0000000) == 0x40000000 || \ 125 (fn & 0xc0000000) == 0x80000000 || \ 126 (fn & 0xc0000000) == 0xc0000000); \ 127 kvm_static_assert(idx < BIT(sizeof(feature.index) * BITS_PER_BYTE)); \ 128 feature; \ 129 }) 130 131 /* 132 * Basic Leafs, a.k.a. Intel defined 133 */ 134 #define X86_FEATURE_MWAIT KVM_X86_CPU_FEATURE(0x1, 0, ECX, 3) 135 #define X86_FEATURE_VMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 5) 136 #define X86_FEATURE_SMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 6) 137 #define X86_FEATURE_PDCM KVM_X86_CPU_FEATURE(0x1, 0, ECX, 15) 138 #define X86_FEATURE_PCID KVM_X86_CPU_FEATURE(0x1, 0, ECX, 17) 139 #define X86_FEATURE_X2APIC KVM_X86_CPU_FEATURE(0x1, 0, ECX, 21) 140 #define X86_FEATURE_MOVBE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 22) 141 #define X86_FEATURE_TSC_DEADLINE_TIMER KVM_X86_CPU_FEATURE(0x1, 0, ECX, 24) 142 #define X86_FEATURE_XSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 26) 143 #define X86_FEATURE_OSXSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 27) 144 #define X86_FEATURE_RDRAND KVM_X86_CPU_FEATURE(0x1, 0, ECX, 30) 145 #define X86_FEATURE_HYPERVISOR KVM_X86_CPU_FEATURE(0x1, 0, ECX, 31) 146 #define X86_FEATURE_PAE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 6) 147 #define X86_FEATURE_MCE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 7) 148 #define X86_FEATURE_APIC KVM_X86_CPU_FEATURE(0x1, 0, EDX, 9) 149 #define X86_FEATURE_CLFLUSH KVM_X86_CPU_FEATURE(0x1, 0, EDX, 19) 150 #define X86_FEATURE_XMM KVM_X86_CPU_FEATURE(0x1, 0, EDX, 25) 151 #define X86_FEATURE_XMM2 KVM_X86_CPU_FEATURE(0x1, 0, EDX, 26) 152 #define X86_FEATURE_FSGSBASE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 0) 153 #define X86_FEATURE_TSC_ADJUST KVM_X86_CPU_FEATURE(0x7, 0, EBX, 1) 154 #define X86_FEATURE_SGX KVM_X86_CPU_FEATURE(0x7, 0, EBX, 2) 155 #define X86_FEATURE_HLE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 4) 156 #define X86_FEATURE_SMEP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 7) 157 #define X86_FEATURE_INVPCID KVM_X86_CPU_FEATURE(0x7, 0, EBX, 10) 158 #define X86_FEATURE_RTM KVM_X86_CPU_FEATURE(0x7, 0, EBX, 11) 159 #define X86_FEATURE_MPX KVM_X86_CPU_FEATURE(0x7, 0, EBX, 14) 160 #define X86_FEATURE_SMAP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 20) 161 #define X86_FEATURE_PCOMMIT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 22) 162 #define X86_FEATURE_CLFLUSHOPT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 23) 163 #define X86_FEATURE_CLWB KVM_X86_CPU_FEATURE(0x7, 0, EBX, 24) 164 #define X86_FEATURE_UMIP KVM_X86_CPU_FEATURE(0x7, 0, ECX, 2) 165 #define X86_FEATURE_PKU KVM_X86_CPU_FEATURE(0x7, 0, ECX, 3) 166 #define X86_FEATURE_OSPKE KVM_X86_CPU_FEATURE(0x7, 0, ECX, 4) 167 #define X86_FEATURE_LA57 KVM_X86_CPU_FEATURE(0x7, 0, ECX, 16) 168 #define X86_FEATURE_RDPID KVM_X86_CPU_FEATURE(0x7, 0, ECX, 22) 169 #define X86_FEATURE_SGX_LC KVM_X86_CPU_FEATURE(0x7, 0, ECX, 30) 170 #define X86_FEATURE_SHSTK KVM_X86_CPU_FEATURE(0x7, 0, ECX, 7) 171 #define X86_FEATURE_IBT KVM_X86_CPU_FEATURE(0x7, 0, EDX, 20) 172 #define X86_FEATURE_AMX_TILE KVM_X86_CPU_FEATURE(0x7, 0, EDX, 24) 173 #define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26) 174 #define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29) 175 #define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31) 176 #define X86_FEATURE_XTILECFG KVM_X86_CPU_FEATURE(0xD, 0, EAX, 17) 177 #define X86_FEATURE_XTILEDATA KVM_X86_CPU_FEATURE(0xD, 0, EAX, 18) 178 #define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3) 179 #define X86_FEATURE_XFD KVM_X86_CPU_FEATURE(0xD, 1, EAX, 4) 180 #define X86_FEATURE_XTILEDATA_XFD KVM_X86_CPU_FEATURE(0xD, 18, ECX, 2) 181 182 /* 183 * Extended Leafs, a.k.a. AMD defined 184 */ 185 #define X86_FEATURE_SVM KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 2) 186 #define X86_FEATURE_PERFCTR_CORE KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 23) 187 #define X86_FEATURE_PERFCTR_NB KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 24) 188 #define X86_FEATURE_PERFCTR_LLC KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 28) 189 #define X86_FEATURE_NX KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 20) 190 #define X86_FEATURE_GBPAGES KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 26) 191 #define X86_FEATURE_RDTSCP KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 27) 192 #define X86_FEATURE_LM KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 29) 193 #define X86_FEATURE_INVTSC KVM_X86_CPU_FEATURE(0x80000007, 0, EDX, 8) 194 #define X86_FEATURE_RDPRU KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 4) 195 #define X86_FEATURE_AMD_IBPB KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 12) 196 #define X86_FEATURE_NPT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 0) 197 #define X86_FEATURE_LBRV KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 1) 198 #define X86_FEATURE_NRIPS KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 3) 199 #define X86_FEATURE_TSCRATEMSR KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 4) 200 #define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10) 201 #define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12) 202 #define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16) 203 #define X86_FEATURE_IDLE_HLT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 30) 204 #define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1) 205 #define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3) 206 #define X86_FEATURE_PERFMON_V2 KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 0) 207 #define X86_FEATURE_LBR_PMC_FREEZE KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 2) 208 209 /* 210 * KVM defined paravirt features. 211 */ 212 #define X86_FEATURE_KVM_CLOCKSOURCE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 0) 213 #define X86_FEATURE_KVM_NOP_IO_DELAY KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 1) 214 #define X86_FEATURE_KVM_MMU_OP KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 2) 215 #define X86_FEATURE_KVM_CLOCKSOURCE2 KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 3) 216 #define X86_FEATURE_KVM_ASYNC_PF KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 4) 217 #define X86_FEATURE_KVM_STEAL_TIME KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 5) 218 #define X86_FEATURE_KVM_PV_EOI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 6) 219 #define X86_FEATURE_KVM_PV_UNHALT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 7) 220 /* Bit 8 apparently isn't used?!?! */ 221 #define X86_FEATURE_KVM_PV_TLB_FLUSH KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 9) 222 #define X86_FEATURE_KVM_ASYNC_PF_VMEXIT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 10) 223 #define X86_FEATURE_KVM_PV_SEND_IPI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 11) 224 #define X86_FEATURE_KVM_POLL_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 12) 225 #define X86_FEATURE_KVM_PV_SCHED_YIELD KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 13) 226 #define X86_FEATURE_KVM_ASYNC_PF_INT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 14) 227 #define X86_FEATURE_KVM_MSI_EXT_DEST_ID KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 15) 228 #define X86_FEATURE_KVM_HC_MAP_GPA_RANGE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 16) 229 #define X86_FEATURE_KVM_MIGRATION_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 17) 230 231 /* 232 * Same idea as X86_FEATURE_XXX, but X86_PROPERTY_XXX retrieves a multi-bit 233 * value/property as opposed to a single-bit feature. Again, pack the info 234 * into a 64-bit value to pass by value with no overhead. 235 */ 236 struct kvm_x86_cpu_property { 237 u32 function; 238 u8 index; 239 u8 reg; 240 u8 lo_bit; 241 u8 hi_bit; 242 }; 243 #define KVM_X86_CPU_PROPERTY(fn, idx, gpr, low_bit, high_bit) \ 244 ({ \ 245 struct kvm_x86_cpu_property property = { \ 246 .function = fn, \ 247 .index = idx, \ 248 .reg = KVM_CPUID_##gpr, \ 249 .lo_bit = low_bit, \ 250 .hi_bit = high_bit, \ 251 }; \ 252 \ 253 kvm_static_assert(low_bit < high_bit); \ 254 kvm_static_assert((fn & 0xc0000000) == 0 || \ 255 (fn & 0xc0000000) == 0x40000000 || \ 256 (fn & 0xc0000000) == 0x80000000 || \ 257 (fn & 0xc0000000) == 0xc0000000); \ 258 kvm_static_assert(idx < BIT(sizeof(property.index) * BITS_PER_BYTE)); \ 259 property; \ 260 }) 261 262 #define X86_PROPERTY_MAX_BASIC_LEAF KVM_X86_CPU_PROPERTY(0, 0, EAX, 0, 31) 263 #define X86_PROPERTY_PMU_VERSION KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 0, 7) 264 #define X86_PROPERTY_PMU_NR_GP_COUNTERS KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 8, 15) 265 #define X86_PROPERTY_PMU_GP_COUNTERS_BIT_WIDTH KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 16, 23) 266 #define X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 24, 31) 267 #define X86_PROPERTY_PMU_EVENTS_MASK KVM_X86_CPU_PROPERTY(0xa, 0, EBX, 0, 7) 268 #define X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK KVM_X86_CPU_PROPERTY(0xa, 0, ECX, 0, 31) 269 #define X86_PROPERTY_PMU_NR_FIXED_COUNTERS KVM_X86_CPU_PROPERTY(0xa, 0, EDX, 0, 4) 270 #define X86_PROPERTY_PMU_FIXED_COUNTERS_BIT_WIDTH KVM_X86_CPU_PROPERTY(0xa, 0, EDX, 5, 12) 271 272 #define X86_PROPERTY_SUPPORTED_XCR0_LO KVM_X86_CPU_PROPERTY(0xd, 0, EAX, 0, 31) 273 #define X86_PROPERTY_XSTATE_MAX_SIZE_XCR0 KVM_X86_CPU_PROPERTY(0xd, 0, EBX, 0, 31) 274 #define X86_PROPERTY_XSTATE_MAX_SIZE KVM_X86_CPU_PROPERTY(0xd, 0, ECX, 0, 31) 275 #define X86_PROPERTY_SUPPORTED_XCR0_HI KVM_X86_CPU_PROPERTY(0xd, 0, EDX, 0, 31) 276 277 #define X86_PROPERTY_XSTATE_TILE_SIZE KVM_X86_CPU_PROPERTY(0xd, 18, EAX, 0, 31) 278 #define X86_PROPERTY_XSTATE_TILE_OFFSET KVM_X86_CPU_PROPERTY(0xd, 18, EBX, 0, 31) 279 #define X86_PROPERTY_AMX_MAX_PALETTE_TABLES KVM_X86_CPU_PROPERTY(0x1d, 0, EAX, 0, 31) 280 #define X86_PROPERTY_AMX_TOTAL_TILE_BYTES KVM_X86_CPU_PROPERTY(0x1d, 1, EAX, 0, 15) 281 #define X86_PROPERTY_AMX_BYTES_PER_TILE KVM_X86_CPU_PROPERTY(0x1d, 1, EAX, 16, 31) 282 #define X86_PROPERTY_AMX_BYTES_PER_ROW KVM_X86_CPU_PROPERTY(0x1d, 1, EBX, 0, 15) 283 #define X86_PROPERTY_AMX_NR_TILE_REGS KVM_X86_CPU_PROPERTY(0x1d, 1, EBX, 16, 31) 284 #define X86_PROPERTY_AMX_MAX_ROWS KVM_X86_CPU_PROPERTY(0x1d, 1, ECX, 0, 15) 285 286 #define X86_PROPERTY_MAX_KVM_LEAF KVM_X86_CPU_PROPERTY(0x40000000, 0, EAX, 0, 31) 287 288 #define X86_PROPERTY_MAX_EXT_LEAF KVM_X86_CPU_PROPERTY(0x80000000, 0, EAX, 0, 31) 289 #define X86_PROPERTY_MAX_PHY_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 0, 7) 290 #define X86_PROPERTY_MAX_VIRT_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 8, 15) 291 #define X86_PROPERTY_GUEST_MAX_PHY_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 16, 23) 292 #define X86_PROPERTY_SEV_C_BIT KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5) 293 #define X86_PROPERTY_PHYS_ADDR_REDUCTION KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 6, 11) 294 #define X86_PROPERTY_NR_PERFCTR_CORE KVM_X86_CPU_PROPERTY(0x80000022, 0, EBX, 0, 3) 295 #define X86_PROPERTY_NR_PERFCTR_NB KVM_X86_CPU_PROPERTY(0x80000022, 0, EBX, 10, 15) 296 297 #define X86_PROPERTY_MAX_CENTAUR_LEAF KVM_X86_CPU_PROPERTY(0xC0000000, 0, EAX, 0, 31) 298 299 /* 300 * Intel's architectural PMU events are bizarre. They have a "feature" bit 301 * that indicates the feature is _not_ supported, and a property that states 302 * the length of the bit mask of unsupported features. A feature is supported 303 * if the size of the bit mask is larger than the "unavailable" bit, and said 304 * bit is not set. Fixed counters also bizarre enumeration, but inverted from 305 * arch events for general purpose counters. Fixed counters are supported if a 306 * feature flag is set **OR** the total number of fixed counters is greater 307 * than index of the counter. 308 * 309 * Wrap the events for general purpose and fixed counters to simplify checking 310 * whether or not a given architectural event is supported. 311 */ 312 struct kvm_x86_pmu_feature { 313 struct kvm_x86_cpu_feature f; 314 }; 315 #define KVM_X86_PMU_FEATURE(__reg, __bit) \ 316 ({ \ 317 struct kvm_x86_pmu_feature feature = { \ 318 .f = KVM_X86_CPU_FEATURE(0xa, 0, __reg, __bit), \ 319 }; \ 320 \ 321 kvm_static_assert(KVM_CPUID_##__reg == KVM_CPUID_EBX || \ 322 KVM_CPUID_##__reg == KVM_CPUID_ECX); \ 323 feature; \ 324 }) 325 326 #define X86_PMU_FEATURE_CPU_CYCLES KVM_X86_PMU_FEATURE(EBX, 0) 327 #define X86_PMU_FEATURE_INSNS_RETIRED KVM_X86_PMU_FEATURE(EBX, 1) 328 #define X86_PMU_FEATURE_REFERENCE_CYCLES KVM_X86_PMU_FEATURE(EBX, 2) 329 #define X86_PMU_FEATURE_LLC_REFERENCES KVM_X86_PMU_FEATURE(EBX, 3) 330 #define X86_PMU_FEATURE_LLC_MISSES KVM_X86_PMU_FEATURE(EBX, 4) 331 #define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED KVM_X86_PMU_FEATURE(EBX, 5) 332 #define X86_PMU_FEATURE_BRANCHES_MISPREDICTED KVM_X86_PMU_FEATURE(EBX, 6) 333 #define X86_PMU_FEATURE_TOPDOWN_SLOTS KVM_X86_PMU_FEATURE(EBX, 7) 334 335 #define X86_PMU_FEATURE_INSNS_RETIRED_FIXED KVM_X86_PMU_FEATURE(ECX, 0) 336 #define X86_PMU_FEATURE_CPU_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 1) 337 #define X86_PMU_FEATURE_REFERENCE_TSC_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 2) 338 #define X86_PMU_FEATURE_TOPDOWN_SLOTS_FIXED KVM_X86_PMU_FEATURE(ECX, 3) 339 340 static inline unsigned int x86_family(unsigned int eax) 341 { 342 unsigned int x86; 343 344 x86 = (eax >> 8) & 0xf; 345 346 if (x86 == 0xf) 347 x86 += (eax >> 20) & 0xff; 348 349 return x86; 350 } 351 352 static inline unsigned int x86_model(unsigned int eax) 353 { 354 return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f); 355 } 356 357 /* Page table bitfield declarations */ 358 #define PTE_PRESENT_MASK BIT_ULL(0) 359 #define PTE_WRITABLE_MASK BIT_ULL(1) 360 #define PTE_USER_MASK BIT_ULL(2) 361 #define PTE_ACCESSED_MASK BIT_ULL(5) 362 #define PTE_DIRTY_MASK BIT_ULL(6) 363 #define PTE_LARGE_MASK BIT_ULL(7) 364 #define PTE_GLOBAL_MASK BIT_ULL(8) 365 #define PTE_NX_MASK BIT_ULL(63) 366 367 #define PHYSICAL_PAGE_MASK GENMASK_ULL(51, 12) 368 369 #define PAGE_SHIFT 12 370 #define PAGE_SIZE (1ULL << PAGE_SHIFT) 371 #define PAGE_MASK (~(PAGE_SIZE-1) & PHYSICAL_PAGE_MASK) 372 373 #define HUGEPAGE_SHIFT(x) (PAGE_SHIFT + (((x) - 1) * 9)) 374 #define HUGEPAGE_SIZE(x) (1UL << HUGEPAGE_SHIFT(x)) 375 #define HUGEPAGE_MASK(x) (~(HUGEPAGE_SIZE(x) - 1) & PHYSICAL_PAGE_MASK) 376 377 #define PTE_GET_PA(pte) ((pte) & PHYSICAL_PAGE_MASK) 378 #define PTE_GET_PFN(pte) (PTE_GET_PA(pte) >> PAGE_SHIFT) 379 380 /* General Registers in 64-Bit Mode */ 381 struct gpr64_regs { 382 u64 rax; 383 u64 rcx; 384 u64 rdx; 385 u64 rbx; 386 u64 rsp; 387 u64 rbp; 388 u64 rsi; 389 u64 rdi; 390 u64 r8; 391 u64 r9; 392 u64 r10; 393 u64 r11; 394 u64 r12; 395 u64 r13; 396 u64 r14; 397 u64 r15; 398 }; 399 400 struct desc64 { 401 uint16_t limit0; 402 uint16_t base0; 403 unsigned base1:8, type:4, s:1, dpl:2, p:1; 404 unsigned limit1:4, avl:1, l:1, db:1, g:1, base2:8; 405 uint32_t base3; 406 uint32_t zero1; 407 } __attribute__((packed)); 408 409 struct desc_ptr { 410 uint16_t size; 411 uint64_t address; 412 } __attribute__((packed)); 413 414 struct kvm_x86_state { 415 struct kvm_xsave *xsave; 416 struct kvm_vcpu_events events; 417 struct kvm_mp_state mp_state; 418 struct kvm_regs regs; 419 struct kvm_xcrs xcrs; 420 struct kvm_sregs sregs; 421 struct kvm_debugregs debugregs; 422 union { 423 struct kvm_nested_state nested; 424 char nested_[16384]; 425 }; 426 struct kvm_msrs msrs; 427 }; 428 429 static inline uint64_t get_desc64_base(const struct desc64 *desc) 430 { 431 return ((uint64_t)desc->base3 << 32) | 432 (desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24)); 433 } 434 435 static inline uint64_t rdtsc(void) 436 { 437 uint32_t eax, edx; 438 uint64_t tsc_val; 439 /* 440 * The lfence is to wait (on Intel CPUs) until all previous 441 * instructions have been executed. If software requires RDTSC to be 442 * executed prior to execution of any subsequent instruction, it can 443 * execute LFENCE immediately after RDTSC 444 */ 445 __asm__ __volatile__("lfence; rdtsc; lfence" : "=a"(eax), "=d"(edx)); 446 tsc_val = ((uint64_t)edx) << 32 | eax; 447 return tsc_val; 448 } 449 450 static inline uint64_t rdtscp(uint32_t *aux) 451 { 452 uint32_t eax, edx; 453 454 __asm__ __volatile__("rdtscp" : "=a"(eax), "=d"(edx), "=c"(*aux)); 455 return ((uint64_t)edx) << 32 | eax; 456 } 457 458 static inline uint64_t rdmsr(uint32_t msr) 459 { 460 uint32_t a, d; 461 462 __asm__ __volatile__("rdmsr" : "=a"(a), "=d"(d) : "c"(msr) : "memory"); 463 464 return a | ((uint64_t) d << 32); 465 } 466 467 static inline void wrmsr(uint32_t msr, uint64_t value) 468 { 469 uint32_t a = value; 470 uint32_t d = value >> 32; 471 472 __asm__ __volatile__("wrmsr" :: "a"(a), "d"(d), "c"(msr) : "memory"); 473 } 474 475 476 static inline uint16_t inw(uint16_t port) 477 { 478 uint16_t tmp; 479 480 __asm__ __volatile__("in %%dx, %%ax" 481 : /* output */ "=a" (tmp) 482 : /* input */ "d" (port)); 483 484 return tmp; 485 } 486 487 static inline uint16_t get_es(void) 488 { 489 uint16_t es; 490 491 __asm__ __volatile__("mov %%es, %[es]" 492 : /* output */ [es]"=rm"(es)); 493 return es; 494 } 495 496 static inline uint16_t get_cs(void) 497 { 498 uint16_t cs; 499 500 __asm__ __volatile__("mov %%cs, %[cs]" 501 : /* output */ [cs]"=rm"(cs)); 502 return cs; 503 } 504 505 static inline uint16_t get_ss(void) 506 { 507 uint16_t ss; 508 509 __asm__ __volatile__("mov %%ss, %[ss]" 510 : /* output */ [ss]"=rm"(ss)); 511 return ss; 512 } 513 514 static inline uint16_t get_ds(void) 515 { 516 uint16_t ds; 517 518 __asm__ __volatile__("mov %%ds, %[ds]" 519 : /* output */ [ds]"=rm"(ds)); 520 return ds; 521 } 522 523 static inline uint16_t get_fs(void) 524 { 525 uint16_t fs; 526 527 __asm__ __volatile__("mov %%fs, %[fs]" 528 : /* output */ [fs]"=rm"(fs)); 529 return fs; 530 } 531 532 static inline uint16_t get_gs(void) 533 { 534 uint16_t gs; 535 536 __asm__ __volatile__("mov %%gs, %[gs]" 537 : /* output */ [gs]"=rm"(gs)); 538 return gs; 539 } 540 541 static inline uint16_t get_tr(void) 542 { 543 uint16_t tr; 544 545 __asm__ __volatile__("str %[tr]" 546 : /* output */ [tr]"=rm"(tr)); 547 return tr; 548 } 549 550 static inline uint64_t get_cr0(void) 551 { 552 uint64_t cr0; 553 554 __asm__ __volatile__("mov %%cr0, %[cr0]" 555 : /* output */ [cr0]"=r"(cr0)); 556 return cr0; 557 } 558 559 static inline uint64_t get_cr3(void) 560 { 561 uint64_t cr3; 562 563 __asm__ __volatile__("mov %%cr3, %[cr3]" 564 : /* output */ [cr3]"=r"(cr3)); 565 return cr3; 566 } 567 568 static inline uint64_t get_cr4(void) 569 { 570 uint64_t cr4; 571 572 __asm__ __volatile__("mov %%cr4, %[cr4]" 573 : /* output */ [cr4]"=r"(cr4)); 574 return cr4; 575 } 576 577 static inline void set_cr4(uint64_t val) 578 { 579 __asm__ __volatile__("mov %0, %%cr4" : : "r" (val) : "memory"); 580 } 581 582 static inline void set_idt(const struct desc_ptr *idt_desc) 583 { 584 __asm__ __volatile__("lidt %0"::"m"(*idt_desc)); 585 } 586 587 static inline u64 xgetbv(u32 index) 588 { 589 u32 eax, edx; 590 591 __asm__ __volatile__("xgetbv;" 592 : "=a" (eax), "=d" (edx) 593 : "c" (index)); 594 return eax | ((u64)edx << 32); 595 } 596 597 static inline void xsetbv(u32 index, u64 value) 598 { 599 u32 eax = value; 600 u32 edx = value >> 32; 601 602 __asm__ __volatile__("xsetbv" :: "a" (eax), "d" (edx), "c" (index)); 603 } 604 605 static inline void wrpkru(u32 pkru) 606 { 607 /* Note, ECX and EDX are architecturally required to be '0'. */ 608 asm volatile(".byte 0x0f,0x01,0xef\n\t" 609 : : "a" (pkru), "c"(0), "d"(0)); 610 } 611 612 static inline struct desc_ptr get_gdt(void) 613 { 614 struct desc_ptr gdt; 615 __asm__ __volatile__("sgdt %[gdt]" 616 : /* output */ [gdt]"=m"(gdt)); 617 return gdt; 618 } 619 620 static inline struct desc_ptr get_idt(void) 621 { 622 struct desc_ptr idt; 623 __asm__ __volatile__("sidt %[idt]" 624 : /* output */ [idt]"=m"(idt)); 625 return idt; 626 } 627 628 static inline void outl(uint16_t port, uint32_t value) 629 { 630 __asm__ __volatile__("outl %%eax, %%dx" : : "d"(port), "a"(value)); 631 } 632 633 static inline void __cpuid(uint32_t function, uint32_t index, 634 uint32_t *eax, uint32_t *ebx, 635 uint32_t *ecx, uint32_t *edx) 636 { 637 *eax = function; 638 *ecx = index; 639 640 asm volatile("cpuid" 641 : "=a" (*eax), 642 "=b" (*ebx), 643 "=c" (*ecx), 644 "=d" (*edx) 645 : "0" (*eax), "2" (*ecx) 646 : "memory"); 647 } 648 649 static inline void cpuid(uint32_t function, 650 uint32_t *eax, uint32_t *ebx, 651 uint32_t *ecx, uint32_t *edx) 652 { 653 return __cpuid(function, 0, eax, ebx, ecx, edx); 654 } 655 656 static inline uint32_t this_cpu_fms(void) 657 { 658 uint32_t eax, ebx, ecx, edx; 659 660 cpuid(1, &eax, &ebx, &ecx, &edx); 661 return eax; 662 } 663 664 static inline uint32_t this_cpu_family(void) 665 { 666 return x86_family(this_cpu_fms()); 667 } 668 669 static inline uint32_t this_cpu_model(void) 670 { 671 return x86_model(this_cpu_fms()); 672 } 673 674 static inline bool this_cpu_vendor_string_is(const char *vendor) 675 { 676 const uint32_t *chunk = (const uint32_t *)vendor; 677 uint32_t eax, ebx, ecx, edx; 678 679 cpuid(0, &eax, &ebx, &ecx, &edx); 680 return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]); 681 } 682 683 static inline bool this_cpu_is_intel(void) 684 { 685 return this_cpu_vendor_string_is("GenuineIntel"); 686 } 687 688 /* 689 * Exclude early K5 samples with a vendor string of "AMDisbetter!" 690 */ 691 static inline bool this_cpu_is_amd(void) 692 { 693 return this_cpu_vendor_string_is("AuthenticAMD"); 694 } 695 696 static inline uint32_t __this_cpu_has(uint32_t function, uint32_t index, 697 uint8_t reg, uint8_t lo, uint8_t hi) 698 { 699 uint32_t gprs[4]; 700 701 __cpuid(function, index, 702 &gprs[KVM_CPUID_EAX], &gprs[KVM_CPUID_EBX], 703 &gprs[KVM_CPUID_ECX], &gprs[KVM_CPUID_EDX]); 704 705 return (gprs[reg] & GENMASK(hi, lo)) >> lo; 706 } 707 708 static inline bool this_cpu_has(struct kvm_x86_cpu_feature feature) 709 { 710 return __this_cpu_has(feature.function, feature.index, 711 feature.reg, feature.bit, feature.bit); 712 } 713 714 static inline uint32_t this_cpu_property(struct kvm_x86_cpu_property property) 715 { 716 return __this_cpu_has(property.function, property.index, 717 property.reg, property.lo_bit, property.hi_bit); 718 } 719 720 static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property) 721 { 722 uint32_t max_leaf; 723 724 switch (property.function & 0xc0000000) { 725 case 0: 726 max_leaf = this_cpu_property(X86_PROPERTY_MAX_BASIC_LEAF); 727 break; 728 case 0x40000000: 729 max_leaf = this_cpu_property(X86_PROPERTY_MAX_KVM_LEAF); 730 break; 731 case 0x80000000: 732 max_leaf = this_cpu_property(X86_PROPERTY_MAX_EXT_LEAF); 733 break; 734 case 0xc0000000: 735 max_leaf = this_cpu_property(X86_PROPERTY_MAX_CENTAUR_LEAF); 736 } 737 return max_leaf >= property.function; 738 } 739 740 static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature) 741 { 742 uint32_t nr_bits; 743 744 if (feature.f.reg == KVM_CPUID_EBX) { 745 nr_bits = this_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); 746 return nr_bits > feature.f.bit && !this_cpu_has(feature.f); 747 } 748 749 GUEST_ASSERT(feature.f.reg == KVM_CPUID_ECX); 750 nr_bits = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); 751 return nr_bits > feature.f.bit || this_cpu_has(feature.f); 752 } 753 754 static __always_inline uint64_t this_cpu_supported_xcr0(void) 755 { 756 if (!this_cpu_has_p(X86_PROPERTY_SUPPORTED_XCR0_LO)) 757 return 0; 758 759 return this_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_LO) | 760 ((uint64_t)this_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_HI) << 32); 761 } 762 763 typedef u32 __attribute__((vector_size(16))) sse128_t; 764 #define __sse128_u union { sse128_t vec; u64 as_u64[2]; u32 as_u32[4]; } 765 #define sse128_lo(x) ({ __sse128_u t; t.vec = x; t.as_u64[0]; }) 766 #define sse128_hi(x) ({ __sse128_u t; t.vec = x; t.as_u64[1]; }) 767 768 static inline void read_sse_reg(int reg, sse128_t *data) 769 { 770 switch (reg) { 771 case 0: 772 asm("movdqa %%xmm0, %0" : "=m"(*data)); 773 break; 774 case 1: 775 asm("movdqa %%xmm1, %0" : "=m"(*data)); 776 break; 777 case 2: 778 asm("movdqa %%xmm2, %0" : "=m"(*data)); 779 break; 780 case 3: 781 asm("movdqa %%xmm3, %0" : "=m"(*data)); 782 break; 783 case 4: 784 asm("movdqa %%xmm4, %0" : "=m"(*data)); 785 break; 786 case 5: 787 asm("movdqa %%xmm5, %0" : "=m"(*data)); 788 break; 789 case 6: 790 asm("movdqa %%xmm6, %0" : "=m"(*data)); 791 break; 792 case 7: 793 asm("movdqa %%xmm7, %0" : "=m"(*data)); 794 break; 795 default: 796 BUG(); 797 } 798 } 799 800 static inline void write_sse_reg(int reg, const sse128_t *data) 801 { 802 switch (reg) { 803 case 0: 804 asm("movdqa %0, %%xmm0" : : "m"(*data)); 805 break; 806 case 1: 807 asm("movdqa %0, %%xmm1" : : "m"(*data)); 808 break; 809 case 2: 810 asm("movdqa %0, %%xmm2" : : "m"(*data)); 811 break; 812 case 3: 813 asm("movdqa %0, %%xmm3" : : "m"(*data)); 814 break; 815 case 4: 816 asm("movdqa %0, %%xmm4" : : "m"(*data)); 817 break; 818 case 5: 819 asm("movdqa %0, %%xmm5" : : "m"(*data)); 820 break; 821 case 6: 822 asm("movdqa %0, %%xmm6" : : "m"(*data)); 823 break; 824 case 7: 825 asm("movdqa %0, %%xmm7" : : "m"(*data)); 826 break; 827 default: 828 BUG(); 829 } 830 } 831 832 static inline void cpu_relax(void) 833 { 834 asm volatile("rep; nop" ::: "memory"); 835 } 836 837 static inline void udelay(unsigned long usec) 838 { 839 uint64_t start, now, cycles; 840 841 GUEST_ASSERT(guest_tsc_khz); 842 cycles = guest_tsc_khz / 1000 * usec; 843 844 /* 845 * Deliberately don't PAUSE, a.k.a. cpu_relax(), so that the delay is 846 * as accurate as possible, e.g. doesn't trigger PAUSE-Loop VM-Exits. 847 */ 848 start = rdtsc(); 849 do { 850 now = rdtsc(); 851 } while (now - start < cycles); 852 } 853 854 #define ud2() \ 855 __asm__ __volatile__( \ 856 "ud2\n" \ 857 ) 858 859 #define hlt() \ 860 __asm__ __volatile__( \ 861 "hlt\n" \ 862 ) 863 864 struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu); 865 void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state); 866 void kvm_x86_state_cleanup(struct kvm_x86_state *state); 867 868 const struct kvm_msr_list *kvm_get_msr_index_list(void); 869 const struct kvm_msr_list *kvm_get_feature_msr_index_list(void); 870 bool kvm_msr_is_in_save_restore_list(uint32_t msr_index); 871 uint64_t kvm_get_feature_msr(uint64_t msr_index); 872 873 static inline void vcpu_msrs_get(struct kvm_vcpu *vcpu, 874 struct kvm_msrs *msrs) 875 { 876 int r = __vcpu_ioctl(vcpu, KVM_GET_MSRS, msrs); 877 878 TEST_ASSERT(r == msrs->nmsrs, 879 "KVM_GET_MSRS failed, r: %i (failed on MSR %x)", 880 r, r < 0 || r >= msrs->nmsrs ? -1 : msrs->entries[r].index); 881 } 882 static inline void vcpu_msrs_set(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs) 883 { 884 int r = __vcpu_ioctl(vcpu, KVM_SET_MSRS, msrs); 885 886 TEST_ASSERT(r == msrs->nmsrs, 887 "KVM_SET_MSRS failed, r: %i (failed on MSR %x)", 888 r, r < 0 || r >= msrs->nmsrs ? -1 : msrs->entries[r].index); 889 } 890 static inline void vcpu_debugregs_get(struct kvm_vcpu *vcpu, 891 struct kvm_debugregs *debugregs) 892 { 893 vcpu_ioctl(vcpu, KVM_GET_DEBUGREGS, debugregs); 894 } 895 static inline void vcpu_debugregs_set(struct kvm_vcpu *vcpu, 896 struct kvm_debugregs *debugregs) 897 { 898 vcpu_ioctl(vcpu, KVM_SET_DEBUGREGS, debugregs); 899 } 900 static inline void vcpu_xsave_get(struct kvm_vcpu *vcpu, 901 struct kvm_xsave *xsave) 902 { 903 vcpu_ioctl(vcpu, KVM_GET_XSAVE, xsave); 904 } 905 static inline void vcpu_xsave2_get(struct kvm_vcpu *vcpu, 906 struct kvm_xsave *xsave) 907 { 908 vcpu_ioctl(vcpu, KVM_GET_XSAVE2, xsave); 909 } 910 static inline void vcpu_xsave_set(struct kvm_vcpu *vcpu, 911 struct kvm_xsave *xsave) 912 { 913 vcpu_ioctl(vcpu, KVM_SET_XSAVE, xsave); 914 } 915 static inline void vcpu_xcrs_get(struct kvm_vcpu *vcpu, 916 struct kvm_xcrs *xcrs) 917 { 918 vcpu_ioctl(vcpu, KVM_GET_XCRS, xcrs); 919 } 920 static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs) 921 { 922 vcpu_ioctl(vcpu, KVM_SET_XCRS, xcrs); 923 } 924 925 const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid, 926 uint32_t function, uint32_t index); 927 const struct kvm_cpuid2 *kvm_get_supported_cpuid(void); 928 929 static inline uint32_t kvm_cpu_fms(void) 930 { 931 return get_cpuid_entry(kvm_get_supported_cpuid(), 0x1, 0)->eax; 932 } 933 934 static inline uint32_t kvm_cpu_family(void) 935 { 936 return x86_family(kvm_cpu_fms()); 937 } 938 939 static inline uint32_t kvm_cpu_model(void) 940 { 941 return x86_model(kvm_cpu_fms()); 942 } 943 944 bool kvm_cpuid_has(const struct kvm_cpuid2 *cpuid, 945 struct kvm_x86_cpu_feature feature); 946 947 static inline bool kvm_cpu_has(struct kvm_x86_cpu_feature feature) 948 { 949 return kvm_cpuid_has(kvm_get_supported_cpuid(), feature); 950 } 951 952 uint32_t kvm_cpuid_property(const struct kvm_cpuid2 *cpuid, 953 struct kvm_x86_cpu_property property); 954 955 static inline uint32_t kvm_cpu_property(struct kvm_x86_cpu_property property) 956 { 957 return kvm_cpuid_property(kvm_get_supported_cpuid(), property); 958 } 959 960 static __always_inline bool kvm_cpu_has_p(struct kvm_x86_cpu_property property) 961 { 962 uint32_t max_leaf; 963 964 switch (property.function & 0xc0000000) { 965 case 0: 966 max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_BASIC_LEAF); 967 break; 968 case 0x40000000: 969 max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_KVM_LEAF); 970 break; 971 case 0x80000000: 972 max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_EXT_LEAF); 973 break; 974 case 0xc0000000: 975 max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_CENTAUR_LEAF); 976 } 977 return max_leaf >= property.function; 978 } 979 980 static inline bool kvm_pmu_has(struct kvm_x86_pmu_feature feature) 981 { 982 uint32_t nr_bits; 983 984 if (feature.f.reg == KVM_CPUID_EBX) { 985 nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); 986 return nr_bits > feature.f.bit && !kvm_cpu_has(feature.f); 987 } 988 989 TEST_ASSERT_EQ(feature.f.reg, KVM_CPUID_ECX); 990 nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); 991 return nr_bits > feature.f.bit || kvm_cpu_has(feature.f); 992 } 993 994 static __always_inline uint64_t kvm_cpu_supported_xcr0(void) 995 { 996 if (!kvm_cpu_has_p(X86_PROPERTY_SUPPORTED_XCR0_LO)) 997 return 0; 998 999 return kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_LO) | 1000 ((uint64_t)kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_HI) << 32); 1001 } 1002 1003 static inline size_t kvm_cpuid2_size(int nr_entries) 1004 { 1005 return sizeof(struct kvm_cpuid2) + 1006 sizeof(struct kvm_cpuid_entry2) * nr_entries; 1007 } 1008 1009 /* 1010 * Allocate a "struct kvm_cpuid2* instance, with the 0-length arrary of 1011 * entries sized to hold @nr_entries. The caller is responsible for freeing 1012 * the struct. 1013 */ 1014 static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries) 1015 { 1016 struct kvm_cpuid2 *cpuid; 1017 1018 cpuid = malloc(kvm_cpuid2_size(nr_entries)); 1019 TEST_ASSERT(cpuid, "-ENOMEM when allocating kvm_cpuid2"); 1020 1021 cpuid->nent = nr_entries; 1022 1023 return cpuid; 1024 } 1025 1026 void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid); 1027 1028 static inline void vcpu_get_cpuid(struct kvm_vcpu *vcpu) 1029 { 1030 vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid); 1031 } 1032 1033 static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu, 1034 uint32_t function, 1035 uint32_t index) 1036 { 1037 TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first (or equivalent)"); 1038 1039 vcpu_get_cpuid(vcpu); 1040 1041 return (struct kvm_cpuid_entry2 *)get_cpuid_entry(vcpu->cpuid, 1042 function, index); 1043 } 1044 1045 static inline struct kvm_cpuid_entry2 *vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu, 1046 uint32_t function) 1047 { 1048 return __vcpu_get_cpuid_entry(vcpu, function, 0); 1049 } 1050 1051 static inline int __vcpu_set_cpuid(struct kvm_vcpu *vcpu) 1052 { 1053 int r; 1054 1055 TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first"); 1056 r = __vcpu_ioctl(vcpu, KVM_SET_CPUID2, vcpu->cpuid); 1057 if (r) 1058 return r; 1059 1060 /* On success, refresh the cache to pick up adjustments made by KVM. */ 1061 vcpu_get_cpuid(vcpu); 1062 return 0; 1063 } 1064 1065 static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu) 1066 { 1067 TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first"); 1068 vcpu_ioctl(vcpu, KVM_SET_CPUID2, vcpu->cpuid); 1069 1070 /* Refresh the cache to pick up adjustments made by KVM. */ 1071 vcpu_get_cpuid(vcpu); 1072 } 1073 1074 void vcpu_set_cpuid_property(struct kvm_vcpu *vcpu, 1075 struct kvm_x86_cpu_property property, 1076 uint32_t value); 1077 void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, uint8_t maxphyaddr); 1078 1079 void vcpu_clear_cpuid_entry(struct kvm_vcpu *vcpu, uint32_t function); 1080 1081 static inline bool vcpu_cpuid_has(struct kvm_vcpu *vcpu, 1082 struct kvm_x86_cpu_feature feature) 1083 { 1084 struct kvm_cpuid_entry2 *entry; 1085 1086 entry = __vcpu_get_cpuid_entry(vcpu, feature.function, feature.index); 1087 return *((&entry->eax) + feature.reg) & BIT(feature.bit); 1088 } 1089 1090 void vcpu_set_or_clear_cpuid_feature(struct kvm_vcpu *vcpu, 1091 struct kvm_x86_cpu_feature feature, 1092 bool set); 1093 1094 static inline void vcpu_set_cpuid_feature(struct kvm_vcpu *vcpu, 1095 struct kvm_x86_cpu_feature feature) 1096 { 1097 vcpu_set_or_clear_cpuid_feature(vcpu, feature, true); 1098 1099 } 1100 1101 static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu, 1102 struct kvm_x86_cpu_feature feature) 1103 { 1104 vcpu_set_or_clear_cpuid_feature(vcpu, feature, false); 1105 } 1106 1107 uint64_t vcpu_get_msr(struct kvm_vcpu *vcpu, uint64_t msr_index); 1108 int _vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, uint64_t msr_value); 1109 1110 /* 1111 * Assert on an MSR access(es) and pretty print the MSR name when possible. 1112 * Note, the caller provides the stringified name so that the name of macro is 1113 * printed, not the value the macro resolves to (due to macro expansion). 1114 */ 1115 #define TEST_ASSERT_MSR(cond, fmt, msr, str, args...) \ 1116 do { \ 1117 if (__builtin_constant_p(msr)) { \ 1118 TEST_ASSERT(cond, fmt, str, args); \ 1119 } else if (!(cond)) { \ 1120 char buf[16]; \ 1121 \ 1122 snprintf(buf, sizeof(buf), "MSR 0x%x", msr); \ 1123 TEST_ASSERT(cond, fmt, buf, args); \ 1124 } \ 1125 } while (0) 1126 1127 /* 1128 * Returns true if KVM should return the last written value when reading an MSR 1129 * from userspace, e.g. the MSR isn't a command MSR, doesn't emulate state that 1130 * is changing, etc. This is NOT an exhaustive list! The intent is to filter 1131 * out MSRs that are not durable _and_ that a selftest wants to write. 1132 */ 1133 static inline bool is_durable_msr(uint32_t msr) 1134 { 1135 return msr != MSR_IA32_TSC; 1136 } 1137 1138 #define vcpu_set_msr(vcpu, msr, val) \ 1139 do { \ 1140 uint64_t r, v = val; \ 1141 \ 1142 TEST_ASSERT_MSR(_vcpu_set_msr(vcpu, msr, v) == 1, \ 1143 "KVM_SET_MSRS failed on %s, value = 0x%lx", msr, #msr, v); \ 1144 if (!is_durable_msr(msr)) \ 1145 break; \ 1146 r = vcpu_get_msr(vcpu, msr); \ 1147 TEST_ASSERT_MSR(r == v, "Set %s to '0x%lx', got back '0x%lx'", msr, #msr, v, r);\ 1148 } while (0) 1149 1150 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits); 1151 void kvm_init_vm_address_properties(struct kvm_vm *vm); 1152 bool vm_is_unrestricted_guest(struct kvm_vm *vm); 1153 1154 struct ex_regs { 1155 uint64_t rax, rcx, rdx, rbx; 1156 uint64_t rbp, rsi, rdi; 1157 uint64_t r8, r9, r10, r11; 1158 uint64_t r12, r13, r14, r15; 1159 uint64_t vector; 1160 uint64_t error_code; 1161 uint64_t rip; 1162 uint64_t cs; 1163 uint64_t rflags; 1164 }; 1165 1166 struct idt_entry { 1167 uint16_t offset0; 1168 uint16_t selector; 1169 uint16_t ist : 3; 1170 uint16_t : 5; 1171 uint16_t type : 4; 1172 uint16_t : 1; 1173 uint16_t dpl : 2; 1174 uint16_t p : 1; 1175 uint16_t offset1; 1176 uint32_t offset2; uint32_t reserved; 1177 }; 1178 1179 void vm_install_exception_handler(struct kvm_vm *vm, int vector, 1180 void (*handler)(struct ex_regs *)); 1181 1182 /* If a toddler were to say "abracadabra". */ 1183 #define KVM_EXCEPTION_MAGIC 0xabacadabaULL 1184 1185 /* 1186 * KVM selftest exception fixup uses registers to coordinate with the exception 1187 * handler, versus the kernel's in-memory tables and KVM-Unit-Tests's in-memory 1188 * per-CPU data. Using only registers avoids having to map memory into the 1189 * guest, doesn't require a valid, stable GS.base, and reduces the risk of 1190 * for recursive faults when accessing memory in the handler. The downside to 1191 * using registers is that it restricts what registers can be used by the actual 1192 * instruction. But, selftests are 64-bit only, making register* pressure a 1193 * minor concern. Use r9-r11 as they are volatile, i.e. don't need to be saved 1194 * by the callee, and except for r11 are not implicit parameters to any 1195 * instructions. Ideally, fixup would use r8-r10 and thus avoid implicit 1196 * parameters entirely, but Hyper-V's hypercall ABI uses r8 and testing Hyper-V 1197 * is higher priority than testing non-faulting SYSCALL/SYSRET. 1198 * 1199 * Note, the fixup handler deliberately does not handle #DE, i.e. the vector 1200 * is guaranteed to be non-zero on fault. 1201 * 1202 * REGISTER INPUTS: 1203 * r9 = MAGIC 1204 * r10 = RIP 1205 * r11 = new RIP on fault 1206 * 1207 * REGISTER OUTPUTS: 1208 * r9 = exception vector (non-zero) 1209 * r10 = error code 1210 */ 1211 #define __KVM_ASM_SAFE(insn, fep) \ 1212 "mov $" __stringify(KVM_EXCEPTION_MAGIC) ", %%r9\n\t" \ 1213 "lea 1f(%%rip), %%r10\n\t" \ 1214 "lea 2f(%%rip), %%r11\n\t" \ 1215 fep "1: " insn "\n\t" \ 1216 "xor %%r9, %%r9\n\t" \ 1217 "2:\n\t" \ 1218 "mov %%r9b, %[vector]\n\t" \ 1219 "mov %%r10, %[error_code]\n\t" 1220 1221 #define KVM_ASM_SAFE(insn) __KVM_ASM_SAFE(insn, "") 1222 #define KVM_ASM_SAFE_FEP(insn) __KVM_ASM_SAFE(insn, KVM_FEP) 1223 1224 #define KVM_ASM_SAFE_OUTPUTS(v, ec) [vector] "=qm"(v), [error_code] "=rm"(ec) 1225 #define KVM_ASM_SAFE_CLOBBERS "r9", "r10", "r11" 1226 1227 #define kvm_asm_safe(insn, inputs...) \ 1228 ({ \ 1229 uint64_t ign_error_code; \ 1230 uint8_t vector; \ 1231 \ 1232 asm volatile(KVM_ASM_SAFE(insn) \ 1233 : KVM_ASM_SAFE_OUTPUTS(vector, ign_error_code) \ 1234 : inputs \ 1235 : KVM_ASM_SAFE_CLOBBERS); \ 1236 vector; \ 1237 }) 1238 1239 #define kvm_asm_safe_ec(insn, error_code, inputs...) \ 1240 ({ \ 1241 uint8_t vector; \ 1242 \ 1243 asm volatile(KVM_ASM_SAFE(insn) \ 1244 : KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ 1245 : inputs \ 1246 : KVM_ASM_SAFE_CLOBBERS); \ 1247 vector; \ 1248 }) 1249 1250 #define kvm_asm_safe_fep(insn, inputs...) \ 1251 ({ \ 1252 uint64_t ign_error_code; \ 1253 uint8_t vector; \ 1254 \ 1255 asm volatile(KVM_ASM_SAFE_FEP(insn) \ 1256 : KVM_ASM_SAFE_OUTPUTS(vector, ign_error_code) \ 1257 : inputs \ 1258 : KVM_ASM_SAFE_CLOBBERS); \ 1259 vector; \ 1260 }) 1261 1262 #define kvm_asm_safe_ec_fep(insn, error_code, inputs...) \ 1263 ({ \ 1264 uint8_t vector; \ 1265 \ 1266 asm volatile(KVM_ASM_SAFE_FEP(insn) \ 1267 : KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ 1268 : inputs \ 1269 : KVM_ASM_SAFE_CLOBBERS); \ 1270 vector; \ 1271 }) 1272 1273 #define BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \ 1274 static inline uint8_t insn##_safe ##_fep(uint32_t idx, uint64_t *val) \ 1275 { \ 1276 uint64_t error_code; \ 1277 uint8_t vector; \ 1278 uint32_t a, d; \ 1279 \ 1280 asm volatile(KVM_ASM_SAFE##_FEP(#insn) \ 1281 : "=a"(a), "=d"(d), \ 1282 KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ 1283 : "c"(idx) \ 1284 : KVM_ASM_SAFE_CLOBBERS); \ 1285 \ 1286 *val = (uint64_t)a | ((uint64_t)d << 32); \ 1287 return vector; \ 1288 } 1289 1290 /* 1291 * Generate {insn}_safe() and {insn}_safe_fep() helpers for instructions that 1292 * use ECX as in input index, and EDX:EAX as a 64-bit output. 1293 */ 1294 #define BUILD_READ_U64_SAFE_HELPERS(insn) \ 1295 BUILD_READ_U64_SAFE_HELPER(insn, , ) \ 1296 BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \ 1297 1298 BUILD_READ_U64_SAFE_HELPERS(rdmsr) 1299 BUILD_READ_U64_SAFE_HELPERS(rdpmc) 1300 BUILD_READ_U64_SAFE_HELPERS(xgetbv) 1301 1302 static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val) 1303 { 1304 return kvm_asm_safe("wrmsr", "a"(val & -1u), "d"(val >> 32), "c"(msr)); 1305 } 1306 1307 static inline uint8_t xsetbv_safe(uint32_t index, uint64_t value) 1308 { 1309 u32 eax = value; 1310 u32 edx = value >> 32; 1311 1312 return kvm_asm_safe("xsetbv", "a" (eax), "d" (edx), "c" (index)); 1313 } 1314 1315 bool kvm_is_tdp_enabled(void); 1316 1317 static inline bool kvm_is_pmu_enabled(void) 1318 { 1319 return get_kvm_param_bool("enable_pmu"); 1320 } 1321 1322 static inline bool kvm_is_forced_emulation_enabled(void) 1323 { 1324 return !!get_kvm_param_integer("force_emulation_prefix"); 1325 } 1326 1327 uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr, 1328 int *level); 1329 uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr); 1330 1331 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, 1332 uint64_t a3); 1333 uint64_t __xen_hypercall(uint64_t nr, uint64_t a0, void *a1); 1334 void xen_hypercall(uint64_t nr, uint64_t a0, void *a1); 1335 1336 static inline uint64_t __kvm_hypercall_map_gpa_range(uint64_t gpa, 1337 uint64_t size, uint64_t flags) 1338 { 1339 return kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> PAGE_SHIFT, flags, 0); 1340 } 1341 1342 static inline void kvm_hypercall_map_gpa_range(uint64_t gpa, uint64_t size, 1343 uint64_t flags) 1344 { 1345 uint64_t ret = __kvm_hypercall_map_gpa_range(gpa, size, flags); 1346 1347 GUEST_ASSERT(!ret); 1348 } 1349 1350 /* 1351 * Execute HLT in an STI interrupt shadow to ensure that a pending IRQ that's 1352 * intended to be a wake event arrives *after* HLT is executed. Modern CPUs, 1353 * except for a few oddballs that KVM is unlikely to run on, block IRQs for one 1354 * instruction after STI, *if* RFLAGS.IF=0 before STI. Note, Intel CPUs may 1355 * block other events beyond regular IRQs, e.g. may block NMIs and SMIs too. 1356 */ 1357 static inline void safe_halt(void) 1358 { 1359 asm volatile("sti; hlt"); 1360 } 1361 1362 /* 1363 * Enable interrupts and ensure that interrupts are evaluated upon return from 1364 * this function, i.e. execute a nop to consume the STi interrupt shadow. 1365 */ 1366 static inline void sti_nop(void) 1367 { 1368 asm volatile ("sti; nop"); 1369 } 1370 1371 /* 1372 * Enable interrupts for one instruction (nop), to allow the CPU to process all 1373 * interrupts that are already pending. 1374 */ 1375 static inline void sti_nop_cli(void) 1376 { 1377 asm volatile ("sti; nop; cli"); 1378 } 1379 1380 static inline void sti(void) 1381 { 1382 asm volatile("sti"); 1383 } 1384 1385 static inline void cli(void) 1386 { 1387 asm volatile ("cli"); 1388 } 1389 1390 void __vm_xsave_require_permission(uint64_t xfeature, const char *name); 1391 1392 #define vm_xsave_require_permission(xfeature) \ 1393 __vm_xsave_require_permission(xfeature, #xfeature) 1394 1395 enum pg_level { 1396 PG_LEVEL_NONE, 1397 PG_LEVEL_4K, 1398 PG_LEVEL_2M, 1399 PG_LEVEL_1G, 1400 PG_LEVEL_512G, 1401 PG_LEVEL_NUM 1402 }; 1403 1404 #define PG_LEVEL_SHIFT(_level) ((_level - 1) * 9 + 12) 1405 #define PG_LEVEL_SIZE(_level) (1ull << PG_LEVEL_SHIFT(_level)) 1406 1407 #define PG_SIZE_4K PG_LEVEL_SIZE(PG_LEVEL_4K) 1408 #define PG_SIZE_2M PG_LEVEL_SIZE(PG_LEVEL_2M) 1409 #define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G) 1410 1411 void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level); 1412 void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, 1413 uint64_t nr_bytes, int level); 1414 1415 /* 1416 * Basic CPU control in CR0 1417 */ 1418 #define X86_CR0_PE (1UL<<0) /* Protection Enable */ 1419 #define X86_CR0_MP (1UL<<1) /* Monitor Coprocessor */ 1420 #define X86_CR0_EM (1UL<<2) /* Emulation */ 1421 #define X86_CR0_TS (1UL<<3) /* Task Switched */ 1422 #define X86_CR0_ET (1UL<<4) /* Extension Type */ 1423 #define X86_CR0_NE (1UL<<5) /* Numeric Error */ 1424 #define X86_CR0_WP (1UL<<16) /* Write Protect */ 1425 #define X86_CR0_AM (1UL<<18) /* Alignment Mask */ 1426 #define X86_CR0_NW (1UL<<29) /* Not Write-through */ 1427 #define X86_CR0_CD (1UL<<30) /* Cache Disable */ 1428 #define X86_CR0_PG (1UL<<31) /* Paging */ 1429 1430 #define PFERR_PRESENT_BIT 0 1431 #define PFERR_WRITE_BIT 1 1432 #define PFERR_USER_BIT 2 1433 #define PFERR_RSVD_BIT 3 1434 #define PFERR_FETCH_BIT 4 1435 #define PFERR_PK_BIT 5 1436 #define PFERR_SGX_BIT 15 1437 #define PFERR_GUEST_FINAL_BIT 32 1438 #define PFERR_GUEST_PAGE_BIT 33 1439 #define PFERR_IMPLICIT_ACCESS_BIT 48 1440 1441 #define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT) 1442 #define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT) 1443 #define PFERR_USER_MASK BIT(PFERR_USER_BIT) 1444 #define PFERR_RSVD_MASK BIT(PFERR_RSVD_BIT) 1445 #define PFERR_FETCH_MASK BIT(PFERR_FETCH_BIT) 1446 #define PFERR_PK_MASK BIT(PFERR_PK_BIT) 1447 #define PFERR_SGX_MASK BIT(PFERR_SGX_BIT) 1448 #define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT) 1449 #define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) 1450 #define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) 1451 1452 bool sys_clocksource_is_based_on_tsc(void); 1453 1454 #endif /* SELFTEST_KVM_PROCESSOR_H */ 1455