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