167730e6cSSean Christopherson /* SPDX-License-Identifier: GPL-2.0-only */ 267730e6cSSean Christopherson /* 367730e6cSSean Christopherson * Copyright (C) 2018, Google LLC. 467730e6cSSean Christopherson */ 567730e6cSSean Christopherson 667730e6cSSean Christopherson #ifndef SELFTEST_KVM_PROCESSOR_H 767730e6cSSean Christopherson #define SELFTEST_KVM_PROCESSOR_H 867730e6cSSean Christopherson 967730e6cSSean Christopherson #include <assert.h> 1067730e6cSSean Christopherson #include <stdint.h> 1167730e6cSSean Christopherson #include <syscall.h> 1267730e6cSSean Christopherson 1367730e6cSSean Christopherson #include <asm/msr-index.h> 1467730e6cSSean Christopherson #include <asm/prctl.h> 1567730e6cSSean Christopherson 1667730e6cSSean Christopherson #include <linux/kvm_para.h> 1767730e6cSSean Christopherson #include <linux/stringify.h> 1867730e6cSSean Christopherson 1967730e6cSSean Christopherson #include "kvm_util.h" 2067730e6cSSean Christopherson #include "ucall_common.h" 2167730e6cSSean Christopherson 2267730e6cSSean Christopherson extern bool host_cpu_is_intel; 2367730e6cSSean Christopherson extern bool host_cpu_is_amd; 2467730e6cSSean Christopherson extern uint64_t guest_tsc_khz; 2567730e6cSSean Christopherson 2667730e6cSSean Christopherson #ifndef MAX_NR_CPUID_ENTRIES 2767730e6cSSean Christopherson #define MAX_NR_CPUID_ENTRIES 100 2867730e6cSSean Christopherson #endif 2967730e6cSSean Christopherson 3067730e6cSSean Christopherson /* Forced emulation prefix, used to invoke the emulator unconditionally. */ 3167730e6cSSean Christopherson #define KVM_FEP "ud2; .byte 'k', 'v', 'm';" 3267730e6cSSean Christopherson 3367730e6cSSean Christopherson #define NMI_VECTOR 0x02 3467730e6cSSean Christopherson 3567730e6cSSean Christopherson #define X86_EFLAGS_FIXED (1u << 1) 3667730e6cSSean Christopherson 3767730e6cSSean Christopherson #define X86_CR4_VME (1ul << 0) 3867730e6cSSean Christopherson #define X86_CR4_PVI (1ul << 1) 3967730e6cSSean Christopherson #define X86_CR4_TSD (1ul << 2) 4067730e6cSSean Christopherson #define X86_CR4_DE (1ul << 3) 4167730e6cSSean Christopherson #define X86_CR4_PSE (1ul << 4) 4267730e6cSSean Christopherson #define X86_CR4_PAE (1ul << 5) 4367730e6cSSean Christopherson #define X86_CR4_MCE (1ul << 6) 4467730e6cSSean Christopherson #define X86_CR4_PGE (1ul << 7) 4567730e6cSSean Christopherson #define X86_CR4_PCE (1ul << 8) 4667730e6cSSean Christopherson #define X86_CR4_OSFXSR (1ul << 9) 4767730e6cSSean Christopherson #define X86_CR4_OSXMMEXCPT (1ul << 10) 4867730e6cSSean Christopherson #define X86_CR4_UMIP (1ul << 11) 4967730e6cSSean Christopherson #define X86_CR4_LA57 (1ul << 12) 5067730e6cSSean Christopherson #define X86_CR4_VMXE (1ul << 13) 5167730e6cSSean Christopherson #define X86_CR4_SMXE (1ul << 14) 5267730e6cSSean Christopherson #define X86_CR4_FSGSBASE (1ul << 16) 5367730e6cSSean Christopherson #define X86_CR4_PCIDE (1ul << 17) 5467730e6cSSean Christopherson #define X86_CR4_OSXSAVE (1ul << 18) 5567730e6cSSean Christopherson #define X86_CR4_SMEP (1ul << 20) 5667730e6cSSean Christopherson #define X86_CR4_SMAP (1ul << 21) 5767730e6cSSean Christopherson #define X86_CR4_PKE (1ul << 22) 5867730e6cSSean Christopherson 5967730e6cSSean Christopherson struct xstate_header { 6067730e6cSSean Christopherson u64 xstate_bv; 6167730e6cSSean Christopherson u64 xcomp_bv; 6267730e6cSSean Christopherson u64 reserved[6]; 6367730e6cSSean Christopherson } __attribute__((packed)); 6467730e6cSSean Christopherson 6567730e6cSSean Christopherson struct xstate { 6667730e6cSSean Christopherson u8 i387[512]; 6767730e6cSSean Christopherson struct xstate_header header; 6867730e6cSSean Christopherson u8 extended_state_area[0]; 6967730e6cSSean Christopherson } __attribute__ ((packed, aligned (64))); 7067730e6cSSean Christopherson 7167730e6cSSean Christopherson #define XFEATURE_MASK_FP BIT_ULL(0) 7267730e6cSSean Christopherson #define XFEATURE_MASK_SSE BIT_ULL(1) 7367730e6cSSean Christopherson #define XFEATURE_MASK_YMM BIT_ULL(2) 7467730e6cSSean Christopherson #define XFEATURE_MASK_BNDREGS BIT_ULL(3) 7567730e6cSSean Christopherson #define XFEATURE_MASK_BNDCSR BIT_ULL(4) 7667730e6cSSean Christopherson #define XFEATURE_MASK_OPMASK BIT_ULL(5) 7767730e6cSSean Christopherson #define XFEATURE_MASK_ZMM_Hi256 BIT_ULL(6) 7867730e6cSSean Christopherson #define XFEATURE_MASK_Hi16_ZMM BIT_ULL(7) 7967730e6cSSean Christopherson #define XFEATURE_MASK_PT BIT_ULL(8) 8067730e6cSSean Christopherson #define XFEATURE_MASK_PKRU BIT_ULL(9) 8167730e6cSSean Christopherson #define XFEATURE_MASK_PASID BIT_ULL(10) 8267730e6cSSean Christopherson #define XFEATURE_MASK_CET_USER BIT_ULL(11) 8367730e6cSSean Christopherson #define XFEATURE_MASK_CET_KERNEL BIT_ULL(12) 8467730e6cSSean Christopherson #define XFEATURE_MASK_LBR BIT_ULL(15) 8567730e6cSSean Christopherson #define XFEATURE_MASK_XTILE_CFG BIT_ULL(17) 8667730e6cSSean Christopherson #define XFEATURE_MASK_XTILE_DATA BIT_ULL(18) 8767730e6cSSean Christopherson 8867730e6cSSean Christopherson #define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK | \ 8967730e6cSSean Christopherson XFEATURE_MASK_ZMM_Hi256 | \ 9067730e6cSSean Christopherson XFEATURE_MASK_Hi16_ZMM) 9167730e6cSSean Christopherson #define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILE_DATA | \ 9267730e6cSSean Christopherson XFEATURE_MASK_XTILE_CFG) 9367730e6cSSean Christopherson 9467730e6cSSean Christopherson /* Note, these are ordered alphabetically to match kvm_cpuid_entry2. Eww. */ 9567730e6cSSean Christopherson enum cpuid_output_regs { 9667730e6cSSean Christopherson KVM_CPUID_EAX, 9767730e6cSSean Christopherson KVM_CPUID_EBX, 9867730e6cSSean Christopherson KVM_CPUID_ECX, 9967730e6cSSean Christopherson KVM_CPUID_EDX 10067730e6cSSean Christopherson }; 10167730e6cSSean Christopherson 10267730e6cSSean Christopherson /* 10367730e6cSSean Christopherson * Pack the information into a 64-bit value so that each X86_FEATURE_XXX can be 10467730e6cSSean Christopherson * passed by value with no overhead. 10567730e6cSSean Christopherson */ 10667730e6cSSean Christopherson struct kvm_x86_cpu_feature { 10767730e6cSSean Christopherson u32 function; 10867730e6cSSean Christopherson u16 index; 10967730e6cSSean Christopherson u8 reg; 11067730e6cSSean Christopherson u8 bit; 11167730e6cSSean Christopherson }; 11267730e6cSSean Christopherson #define KVM_X86_CPU_FEATURE(fn, idx, gpr, __bit) \ 11367730e6cSSean Christopherson ({ \ 11467730e6cSSean Christopherson struct kvm_x86_cpu_feature feature = { \ 11567730e6cSSean Christopherson .function = fn, \ 11667730e6cSSean Christopherson .index = idx, \ 11767730e6cSSean Christopherson .reg = KVM_CPUID_##gpr, \ 11867730e6cSSean Christopherson .bit = __bit, \ 11967730e6cSSean Christopherson }; \ 12067730e6cSSean Christopherson \ 12167730e6cSSean Christopherson kvm_static_assert((fn & 0xc0000000) == 0 || \ 12267730e6cSSean Christopherson (fn & 0xc0000000) == 0x40000000 || \ 12367730e6cSSean Christopherson (fn & 0xc0000000) == 0x80000000 || \ 12467730e6cSSean Christopherson (fn & 0xc0000000) == 0xc0000000); \ 12567730e6cSSean Christopherson kvm_static_assert(idx < BIT(sizeof(feature.index) * BITS_PER_BYTE)); \ 12667730e6cSSean Christopherson feature; \ 12767730e6cSSean Christopherson }) 12867730e6cSSean Christopherson 12967730e6cSSean Christopherson /* 13067730e6cSSean Christopherson * Basic Leafs, a.k.a. Intel defined 13167730e6cSSean Christopherson */ 13267730e6cSSean Christopherson #define X86_FEATURE_MWAIT KVM_X86_CPU_FEATURE(0x1, 0, ECX, 3) 13367730e6cSSean Christopherson #define X86_FEATURE_VMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 5) 13467730e6cSSean Christopherson #define X86_FEATURE_SMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 6) 13567730e6cSSean Christopherson #define X86_FEATURE_PDCM KVM_X86_CPU_FEATURE(0x1, 0, ECX, 15) 13667730e6cSSean Christopherson #define X86_FEATURE_PCID KVM_X86_CPU_FEATURE(0x1, 0, ECX, 17) 13767730e6cSSean Christopherson #define X86_FEATURE_X2APIC KVM_X86_CPU_FEATURE(0x1, 0, ECX, 21) 13867730e6cSSean Christopherson #define X86_FEATURE_MOVBE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 22) 13967730e6cSSean Christopherson #define X86_FEATURE_TSC_DEADLINE_TIMER KVM_X86_CPU_FEATURE(0x1, 0, ECX, 24) 14067730e6cSSean Christopherson #define X86_FEATURE_XSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 26) 14167730e6cSSean Christopherson #define X86_FEATURE_OSXSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 27) 14267730e6cSSean Christopherson #define X86_FEATURE_RDRAND KVM_X86_CPU_FEATURE(0x1, 0, ECX, 30) 14367730e6cSSean Christopherson #define X86_FEATURE_HYPERVISOR KVM_X86_CPU_FEATURE(0x1, 0, ECX, 31) 14467730e6cSSean Christopherson #define X86_FEATURE_PAE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 6) 14567730e6cSSean Christopherson #define X86_FEATURE_MCE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 7) 14667730e6cSSean Christopherson #define X86_FEATURE_APIC KVM_X86_CPU_FEATURE(0x1, 0, EDX, 9) 14767730e6cSSean Christopherson #define X86_FEATURE_CLFLUSH KVM_X86_CPU_FEATURE(0x1, 0, EDX, 19) 14867730e6cSSean Christopherson #define X86_FEATURE_XMM KVM_X86_CPU_FEATURE(0x1, 0, EDX, 25) 14967730e6cSSean Christopherson #define X86_FEATURE_XMM2 KVM_X86_CPU_FEATURE(0x1, 0, EDX, 26) 15067730e6cSSean Christopherson #define X86_FEATURE_FSGSBASE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 0) 15167730e6cSSean Christopherson #define X86_FEATURE_TSC_ADJUST KVM_X86_CPU_FEATURE(0x7, 0, EBX, 1) 15267730e6cSSean Christopherson #define X86_FEATURE_SGX KVM_X86_CPU_FEATURE(0x7, 0, EBX, 2) 15367730e6cSSean Christopherson #define X86_FEATURE_HLE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 4) 15467730e6cSSean Christopherson #define X86_FEATURE_SMEP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 7) 15567730e6cSSean Christopherson #define X86_FEATURE_INVPCID KVM_X86_CPU_FEATURE(0x7, 0, EBX, 10) 15667730e6cSSean Christopherson #define X86_FEATURE_RTM KVM_X86_CPU_FEATURE(0x7, 0, EBX, 11) 15767730e6cSSean Christopherson #define X86_FEATURE_MPX KVM_X86_CPU_FEATURE(0x7, 0, EBX, 14) 15867730e6cSSean Christopherson #define X86_FEATURE_SMAP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 20) 15967730e6cSSean Christopherson #define X86_FEATURE_PCOMMIT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 22) 16067730e6cSSean Christopherson #define X86_FEATURE_CLFLUSHOPT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 23) 16167730e6cSSean Christopherson #define X86_FEATURE_CLWB KVM_X86_CPU_FEATURE(0x7, 0, EBX, 24) 16267730e6cSSean Christopherson #define X86_FEATURE_UMIP KVM_X86_CPU_FEATURE(0x7, 0, ECX, 2) 16367730e6cSSean Christopherson #define X86_FEATURE_PKU KVM_X86_CPU_FEATURE(0x7, 0, ECX, 3) 16467730e6cSSean Christopherson #define X86_FEATURE_OSPKE KVM_X86_CPU_FEATURE(0x7, 0, ECX, 4) 16567730e6cSSean Christopherson #define X86_FEATURE_LA57 KVM_X86_CPU_FEATURE(0x7, 0, ECX, 16) 16667730e6cSSean Christopherson #define X86_FEATURE_RDPID KVM_X86_CPU_FEATURE(0x7, 0, ECX, 22) 16767730e6cSSean Christopherson #define X86_FEATURE_SGX_LC KVM_X86_CPU_FEATURE(0x7, 0, ECX, 30) 16867730e6cSSean Christopherson #define X86_FEATURE_SHSTK KVM_X86_CPU_FEATURE(0x7, 0, ECX, 7) 16967730e6cSSean Christopherson #define X86_FEATURE_IBT KVM_X86_CPU_FEATURE(0x7, 0, EDX, 20) 17067730e6cSSean Christopherson #define X86_FEATURE_AMX_TILE KVM_X86_CPU_FEATURE(0x7, 0, EDX, 24) 17167730e6cSSean Christopherson #define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26) 17267730e6cSSean Christopherson #define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29) 17367730e6cSSean Christopherson #define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31) 17467730e6cSSean Christopherson #define X86_FEATURE_XTILECFG KVM_X86_CPU_FEATURE(0xD, 0, EAX, 17) 17567730e6cSSean Christopherson #define X86_FEATURE_XTILEDATA KVM_X86_CPU_FEATURE(0xD, 0, EAX, 18) 17667730e6cSSean Christopherson #define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3) 17767730e6cSSean Christopherson #define X86_FEATURE_XFD KVM_X86_CPU_FEATURE(0xD, 1, EAX, 4) 17867730e6cSSean Christopherson #define X86_FEATURE_XTILEDATA_XFD KVM_X86_CPU_FEATURE(0xD, 18, ECX, 2) 17967730e6cSSean Christopherson 18067730e6cSSean Christopherson /* 18167730e6cSSean Christopherson * Extended Leafs, a.k.a. AMD defined 18267730e6cSSean Christopherson */ 18367730e6cSSean Christopherson #define X86_FEATURE_SVM KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 2) 18467730e6cSSean Christopherson #define X86_FEATURE_NX KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 20) 18567730e6cSSean Christopherson #define X86_FEATURE_GBPAGES KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 26) 18667730e6cSSean Christopherson #define X86_FEATURE_RDTSCP KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 27) 18767730e6cSSean Christopherson #define X86_FEATURE_LM KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 29) 18867730e6cSSean Christopherson #define X86_FEATURE_INVTSC KVM_X86_CPU_FEATURE(0x80000007, 0, EDX, 8) 18967730e6cSSean Christopherson #define X86_FEATURE_RDPRU KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 4) 19067730e6cSSean Christopherson #define X86_FEATURE_AMD_IBPB KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 12) 19167730e6cSSean Christopherson #define X86_FEATURE_NPT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 0) 19267730e6cSSean Christopherson #define X86_FEATURE_LBRV KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 1) 19367730e6cSSean Christopherson #define X86_FEATURE_NRIPS KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 3) 19467730e6cSSean Christopherson #define X86_FEATURE_TSCRATEMSR KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 4) 19567730e6cSSean Christopherson #define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10) 19667730e6cSSean Christopherson #define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12) 19767730e6cSSean Christopherson #define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16) 19867730e6cSSean Christopherson #define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1) 19967730e6cSSean Christopherson #define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3) 20067730e6cSSean Christopherson 20167730e6cSSean Christopherson /* 20267730e6cSSean Christopherson * KVM defined paravirt features. 20367730e6cSSean Christopherson */ 20467730e6cSSean Christopherson #define X86_FEATURE_KVM_CLOCKSOURCE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 0) 20567730e6cSSean Christopherson #define X86_FEATURE_KVM_NOP_IO_DELAY KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 1) 20667730e6cSSean Christopherson #define X86_FEATURE_KVM_MMU_OP KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 2) 20767730e6cSSean Christopherson #define X86_FEATURE_KVM_CLOCKSOURCE2 KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 3) 20867730e6cSSean Christopherson #define X86_FEATURE_KVM_ASYNC_PF KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 4) 20967730e6cSSean Christopherson #define X86_FEATURE_KVM_STEAL_TIME KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 5) 21067730e6cSSean Christopherson #define X86_FEATURE_KVM_PV_EOI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 6) 21167730e6cSSean Christopherson #define X86_FEATURE_KVM_PV_UNHALT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 7) 21267730e6cSSean Christopherson /* Bit 8 apparently isn't used?!?! */ 21367730e6cSSean Christopherson #define X86_FEATURE_KVM_PV_TLB_FLUSH KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 9) 21467730e6cSSean Christopherson #define X86_FEATURE_KVM_ASYNC_PF_VMEXIT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 10) 21567730e6cSSean Christopherson #define X86_FEATURE_KVM_PV_SEND_IPI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 11) 21667730e6cSSean Christopherson #define X86_FEATURE_KVM_POLL_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 12) 21767730e6cSSean Christopherson #define X86_FEATURE_KVM_PV_SCHED_YIELD KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 13) 21867730e6cSSean Christopherson #define X86_FEATURE_KVM_ASYNC_PF_INT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 14) 21967730e6cSSean Christopherson #define X86_FEATURE_KVM_MSI_EXT_DEST_ID KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 15) 22067730e6cSSean Christopherson #define X86_FEATURE_KVM_HC_MAP_GPA_RANGE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 16) 22167730e6cSSean Christopherson #define X86_FEATURE_KVM_MIGRATION_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 17) 22267730e6cSSean Christopherson 22367730e6cSSean Christopherson /* 22467730e6cSSean Christopherson * Same idea as X86_FEATURE_XXX, but X86_PROPERTY_XXX retrieves a multi-bit 22567730e6cSSean Christopherson * value/property as opposed to a single-bit feature. Again, pack the info 22667730e6cSSean Christopherson * into a 64-bit value to pass by value with no overhead. 22767730e6cSSean Christopherson */ 22867730e6cSSean Christopherson struct kvm_x86_cpu_property { 22967730e6cSSean Christopherson u32 function; 23067730e6cSSean Christopherson u8 index; 23167730e6cSSean Christopherson u8 reg; 23267730e6cSSean Christopherson u8 lo_bit; 23367730e6cSSean Christopherson u8 hi_bit; 23467730e6cSSean Christopherson }; 23567730e6cSSean Christopherson #define KVM_X86_CPU_PROPERTY(fn, idx, gpr, low_bit, high_bit) \ 23667730e6cSSean Christopherson ({ \ 23767730e6cSSean Christopherson struct kvm_x86_cpu_property property = { \ 23867730e6cSSean Christopherson .function = fn, \ 23967730e6cSSean Christopherson .index = idx, \ 24067730e6cSSean Christopherson .reg = KVM_CPUID_##gpr, \ 24167730e6cSSean Christopherson .lo_bit = low_bit, \ 24267730e6cSSean Christopherson .hi_bit = high_bit, \ 24367730e6cSSean Christopherson }; \ 24467730e6cSSean Christopherson \ 24567730e6cSSean Christopherson kvm_static_assert(low_bit < high_bit); \ 24667730e6cSSean Christopherson kvm_static_assert((fn & 0xc0000000) == 0 || \ 24767730e6cSSean Christopherson (fn & 0xc0000000) == 0x40000000 || \ 24867730e6cSSean Christopherson (fn & 0xc0000000) == 0x80000000 || \ 24967730e6cSSean Christopherson (fn & 0xc0000000) == 0xc0000000); \ 25067730e6cSSean Christopherson kvm_static_assert(idx < BIT(sizeof(property.index) * BITS_PER_BYTE)); \ 25167730e6cSSean Christopherson property; \ 25267730e6cSSean Christopherson }) 25367730e6cSSean Christopherson 25467730e6cSSean Christopherson #define X86_PROPERTY_MAX_BASIC_LEAF KVM_X86_CPU_PROPERTY(0, 0, EAX, 0, 31) 25567730e6cSSean Christopherson #define X86_PROPERTY_PMU_VERSION KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 0, 7) 25667730e6cSSean Christopherson #define X86_PROPERTY_PMU_NR_GP_COUNTERS KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 8, 15) 25767730e6cSSean Christopherson #define X86_PROPERTY_PMU_GP_COUNTERS_BIT_WIDTH KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 16, 23) 25867730e6cSSean Christopherson #define X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 24, 31) 25967730e6cSSean Christopherson #define X86_PROPERTY_PMU_EVENTS_MASK KVM_X86_CPU_PROPERTY(0xa, 0, EBX, 0, 7) 26067730e6cSSean Christopherson #define X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK KVM_X86_CPU_PROPERTY(0xa, 0, ECX, 0, 31) 26167730e6cSSean Christopherson #define X86_PROPERTY_PMU_NR_FIXED_COUNTERS KVM_X86_CPU_PROPERTY(0xa, 0, EDX, 0, 4) 26267730e6cSSean Christopherson #define X86_PROPERTY_PMU_FIXED_COUNTERS_BIT_WIDTH KVM_X86_CPU_PROPERTY(0xa, 0, EDX, 5, 12) 26367730e6cSSean Christopherson 26467730e6cSSean Christopherson #define X86_PROPERTY_SUPPORTED_XCR0_LO KVM_X86_CPU_PROPERTY(0xd, 0, EAX, 0, 31) 26567730e6cSSean Christopherson #define X86_PROPERTY_XSTATE_MAX_SIZE_XCR0 KVM_X86_CPU_PROPERTY(0xd, 0, EBX, 0, 31) 26667730e6cSSean Christopherson #define X86_PROPERTY_XSTATE_MAX_SIZE KVM_X86_CPU_PROPERTY(0xd, 0, ECX, 0, 31) 26767730e6cSSean Christopherson #define X86_PROPERTY_SUPPORTED_XCR0_HI KVM_X86_CPU_PROPERTY(0xd, 0, EDX, 0, 31) 26867730e6cSSean Christopherson 26967730e6cSSean Christopherson #define X86_PROPERTY_XSTATE_TILE_SIZE KVM_X86_CPU_PROPERTY(0xd, 18, EAX, 0, 31) 27067730e6cSSean Christopherson #define X86_PROPERTY_XSTATE_TILE_OFFSET KVM_X86_CPU_PROPERTY(0xd, 18, EBX, 0, 31) 27167730e6cSSean Christopherson #define X86_PROPERTY_AMX_MAX_PALETTE_TABLES KVM_X86_CPU_PROPERTY(0x1d, 0, EAX, 0, 31) 27267730e6cSSean Christopherson #define X86_PROPERTY_AMX_TOTAL_TILE_BYTES KVM_X86_CPU_PROPERTY(0x1d, 1, EAX, 0, 15) 27367730e6cSSean Christopherson #define X86_PROPERTY_AMX_BYTES_PER_TILE KVM_X86_CPU_PROPERTY(0x1d, 1, EAX, 16, 31) 27467730e6cSSean Christopherson #define X86_PROPERTY_AMX_BYTES_PER_ROW KVM_X86_CPU_PROPERTY(0x1d, 1, EBX, 0, 15) 27567730e6cSSean Christopherson #define X86_PROPERTY_AMX_NR_TILE_REGS KVM_X86_CPU_PROPERTY(0x1d, 1, EBX, 16, 31) 27667730e6cSSean Christopherson #define X86_PROPERTY_AMX_MAX_ROWS KVM_X86_CPU_PROPERTY(0x1d, 1, ECX, 0, 15) 27767730e6cSSean Christopherson 27867730e6cSSean Christopherson #define X86_PROPERTY_MAX_KVM_LEAF KVM_X86_CPU_PROPERTY(0x40000000, 0, EAX, 0, 31) 27967730e6cSSean Christopherson 28067730e6cSSean Christopherson #define X86_PROPERTY_MAX_EXT_LEAF KVM_X86_CPU_PROPERTY(0x80000000, 0, EAX, 0, 31) 28167730e6cSSean Christopherson #define X86_PROPERTY_MAX_PHY_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 0, 7) 28267730e6cSSean Christopherson #define X86_PROPERTY_MAX_VIRT_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 8, 15) 28367730e6cSSean Christopherson #define X86_PROPERTY_GUEST_MAX_PHY_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 16, 23) 28467730e6cSSean Christopherson #define X86_PROPERTY_SEV_C_BIT KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5) 28567730e6cSSean Christopherson #define X86_PROPERTY_PHYS_ADDR_REDUCTION KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 6, 11) 28667730e6cSSean Christopherson 28767730e6cSSean Christopherson #define X86_PROPERTY_MAX_CENTAUR_LEAF KVM_X86_CPU_PROPERTY(0xC0000000, 0, EAX, 0, 31) 28867730e6cSSean Christopherson 28967730e6cSSean Christopherson /* 29067730e6cSSean Christopherson * Intel's architectural PMU events are bizarre. They have a "feature" bit 29167730e6cSSean Christopherson * that indicates the feature is _not_ supported, and a property that states 29267730e6cSSean Christopherson * the length of the bit mask of unsupported features. A feature is supported 29367730e6cSSean Christopherson * if the size of the bit mask is larger than the "unavailable" bit, and said 29467730e6cSSean Christopherson * bit is not set. Fixed counters also bizarre enumeration, but inverted from 29567730e6cSSean Christopherson * arch events for general purpose counters. Fixed counters are supported if a 29667730e6cSSean Christopherson * feature flag is set **OR** the total number of fixed counters is greater 29767730e6cSSean Christopherson * than index of the counter. 29867730e6cSSean Christopherson * 29967730e6cSSean Christopherson * Wrap the events for general purpose and fixed counters to simplify checking 30067730e6cSSean Christopherson * whether or not a given architectural event is supported. 30167730e6cSSean Christopherson */ 30267730e6cSSean Christopherson struct kvm_x86_pmu_feature { 30367730e6cSSean Christopherson struct kvm_x86_cpu_feature f; 30467730e6cSSean Christopherson }; 30567730e6cSSean Christopherson #define KVM_X86_PMU_FEATURE(__reg, __bit) \ 30667730e6cSSean Christopherson ({ \ 30767730e6cSSean Christopherson struct kvm_x86_pmu_feature feature = { \ 30867730e6cSSean Christopherson .f = KVM_X86_CPU_FEATURE(0xa, 0, __reg, __bit), \ 30967730e6cSSean Christopherson }; \ 31067730e6cSSean Christopherson \ 31167730e6cSSean Christopherson kvm_static_assert(KVM_CPUID_##__reg == KVM_CPUID_EBX || \ 31267730e6cSSean Christopherson KVM_CPUID_##__reg == KVM_CPUID_ECX); \ 31367730e6cSSean Christopherson feature; \ 31467730e6cSSean Christopherson }) 31567730e6cSSean Christopherson 31667730e6cSSean Christopherson #define X86_PMU_FEATURE_CPU_CYCLES KVM_X86_PMU_FEATURE(EBX, 0) 31767730e6cSSean Christopherson #define X86_PMU_FEATURE_INSNS_RETIRED KVM_X86_PMU_FEATURE(EBX, 1) 31867730e6cSSean Christopherson #define X86_PMU_FEATURE_REFERENCE_CYCLES KVM_X86_PMU_FEATURE(EBX, 2) 31967730e6cSSean Christopherson #define X86_PMU_FEATURE_LLC_REFERENCES KVM_X86_PMU_FEATURE(EBX, 3) 32067730e6cSSean Christopherson #define X86_PMU_FEATURE_LLC_MISSES KVM_X86_PMU_FEATURE(EBX, 4) 32167730e6cSSean Christopherson #define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED KVM_X86_PMU_FEATURE(EBX, 5) 32267730e6cSSean Christopherson #define X86_PMU_FEATURE_BRANCHES_MISPREDICTED KVM_X86_PMU_FEATURE(EBX, 6) 32367730e6cSSean Christopherson #define X86_PMU_FEATURE_TOPDOWN_SLOTS KVM_X86_PMU_FEATURE(EBX, 7) 32467730e6cSSean Christopherson 32567730e6cSSean Christopherson #define X86_PMU_FEATURE_INSNS_RETIRED_FIXED KVM_X86_PMU_FEATURE(ECX, 0) 32667730e6cSSean Christopherson #define X86_PMU_FEATURE_CPU_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 1) 32767730e6cSSean Christopherson #define X86_PMU_FEATURE_REFERENCE_TSC_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 2) 32867730e6cSSean Christopherson #define X86_PMU_FEATURE_TOPDOWN_SLOTS_FIXED KVM_X86_PMU_FEATURE(ECX, 3) 32967730e6cSSean Christopherson 33067730e6cSSean Christopherson static inline unsigned int x86_family(unsigned int eax) 33167730e6cSSean Christopherson { 33267730e6cSSean Christopherson unsigned int x86; 33367730e6cSSean Christopherson 33467730e6cSSean Christopherson x86 = (eax >> 8) & 0xf; 33567730e6cSSean Christopherson 33667730e6cSSean Christopherson if (x86 == 0xf) 33767730e6cSSean Christopherson x86 += (eax >> 20) & 0xff; 33867730e6cSSean Christopherson 33967730e6cSSean Christopherson return x86; 34067730e6cSSean Christopherson } 34167730e6cSSean Christopherson 34267730e6cSSean Christopherson static inline unsigned int x86_model(unsigned int eax) 34367730e6cSSean Christopherson { 34467730e6cSSean Christopherson return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f); 34567730e6cSSean Christopherson } 34667730e6cSSean Christopherson 34767730e6cSSean Christopherson /* Page table bitfield declarations */ 34867730e6cSSean Christopherson #define PTE_PRESENT_MASK BIT_ULL(0) 34967730e6cSSean Christopherson #define PTE_WRITABLE_MASK BIT_ULL(1) 35067730e6cSSean Christopherson #define PTE_USER_MASK BIT_ULL(2) 35167730e6cSSean Christopherson #define PTE_ACCESSED_MASK BIT_ULL(5) 35267730e6cSSean Christopherson #define PTE_DIRTY_MASK BIT_ULL(6) 35367730e6cSSean Christopherson #define PTE_LARGE_MASK BIT_ULL(7) 35467730e6cSSean Christopherson #define PTE_GLOBAL_MASK BIT_ULL(8) 35567730e6cSSean Christopherson #define PTE_NX_MASK BIT_ULL(63) 35667730e6cSSean Christopherson 35767730e6cSSean Christopherson #define PHYSICAL_PAGE_MASK GENMASK_ULL(51, 12) 35867730e6cSSean Christopherson 35967730e6cSSean Christopherson #define PAGE_SHIFT 12 36067730e6cSSean Christopherson #define PAGE_SIZE (1ULL << PAGE_SHIFT) 36167730e6cSSean Christopherson #define PAGE_MASK (~(PAGE_SIZE-1) & PHYSICAL_PAGE_MASK) 36267730e6cSSean Christopherson 36367730e6cSSean Christopherson #define HUGEPAGE_SHIFT(x) (PAGE_SHIFT + (((x) - 1) * 9)) 36467730e6cSSean Christopherson #define HUGEPAGE_SIZE(x) (1UL << HUGEPAGE_SHIFT(x)) 36567730e6cSSean Christopherson #define HUGEPAGE_MASK(x) (~(HUGEPAGE_SIZE(x) - 1) & PHYSICAL_PAGE_MASK) 36667730e6cSSean Christopherson 36767730e6cSSean Christopherson #define PTE_GET_PA(pte) ((pte) & PHYSICAL_PAGE_MASK) 36867730e6cSSean Christopherson #define PTE_GET_PFN(pte) (PTE_GET_PA(pte) >> PAGE_SHIFT) 36967730e6cSSean Christopherson 37067730e6cSSean Christopherson /* General Registers in 64-Bit Mode */ 37167730e6cSSean Christopherson struct gpr64_regs { 37267730e6cSSean Christopherson u64 rax; 37367730e6cSSean Christopherson u64 rcx; 37467730e6cSSean Christopherson u64 rdx; 37567730e6cSSean Christopherson u64 rbx; 37667730e6cSSean Christopherson u64 rsp; 37767730e6cSSean Christopherson u64 rbp; 37867730e6cSSean Christopherson u64 rsi; 37967730e6cSSean Christopherson u64 rdi; 38067730e6cSSean Christopherson u64 r8; 38167730e6cSSean Christopherson u64 r9; 38267730e6cSSean Christopherson u64 r10; 38367730e6cSSean Christopherson u64 r11; 38467730e6cSSean Christopherson u64 r12; 38567730e6cSSean Christopherson u64 r13; 38667730e6cSSean Christopherson u64 r14; 38767730e6cSSean Christopherson u64 r15; 38867730e6cSSean Christopherson }; 38967730e6cSSean Christopherson 39067730e6cSSean Christopherson struct desc64 { 39167730e6cSSean Christopherson uint16_t limit0; 39267730e6cSSean Christopherson uint16_t base0; 39367730e6cSSean Christopherson unsigned base1:8, type:4, s:1, dpl:2, p:1; 39467730e6cSSean Christopherson unsigned limit1:4, avl:1, l:1, db:1, g:1, base2:8; 39567730e6cSSean Christopherson uint32_t base3; 39667730e6cSSean Christopherson uint32_t zero1; 39767730e6cSSean Christopherson } __attribute__((packed)); 39867730e6cSSean Christopherson 39967730e6cSSean Christopherson struct desc_ptr { 40067730e6cSSean Christopherson uint16_t size; 40167730e6cSSean Christopherson uint64_t address; 40267730e6cSSean Christopherson } __attribute__((packed)); 40367730e6cSSean Christopherson 40467730e6cSSean Christopherson struct kvm_x86_state { 40567730e6cSSean Christopherson struct kvm_xsave *xsave; 40667730e6cSSean Christopherson struct kvm_vcpu_events events; 40767730e6cSSean Christopherson struct kvm_mp_state mp_state; 40867730e6cSSean Christopherson struct kvm_regs regs; 40967730e6cSSean Christopherson struct kvm_xcrs xcrs; 41067730e6cSSean Christopherson struct kvm_sregs sregs; 41167730e6cSSean Christopherson struct kvm_debugregs debugregs; 41267730e6cSSean Christopherson union { 41367730e6cSSean Christopherson struct kvm_nested_state nested; 41467730e6cSSean Christopherson char nested_[16384]; 41567730e6cSSean Christopherson }; 41667730e6cSSean Christopherson struct kvm_msrs msrs; 41767730e6cSSean Christopherson }; 41867730e6cSSean Christopherson 41967730e6cSSean Christopherson static inline uint64_t get_desc64_base(const struct desc64 *desc) 42067730e6cSSean Christopherson { 42167730e6cSSean Christopherson return ((uint64_t)desc->base3 << 32) | 42267730e6cSSean Christopherson (desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24)); 42367730e6cSSean Christopherson } 42467730e6cSSean Christopherson 42567730e6cSSean Christopherson static inline uint64_t rdtsc(void) 42667730e6cSSean Christopherson { 42767730e6cSSean Christopherson uint32_t eax, edx; 42867730e6cSSean Christopherson uint64_t tsc_val; 42967730e6cSSean Christopherson /* 43067730e6cSSean Christopherson * The lfence is to wait (on Intel CPUs) until all previous 43167730e6cSSean Christopherson * instructions have been executed. If software requires RDTSC to be 43267730e6cSSean Christopherson * executed prior to execution of any subsequent instruction, it can 43367730e6cSSean Christopherson * execute LFENCE immediately after RDTSC 43467730e6cSSean Christopherson */ 43567730e6cSSean Christopherson __asm__ __volatile__("lfence; rdtsc; lfence" : "=a"(eax), "=d"(edx)); 43667730e6cSSean Christopherson tsc_val = ((uint64_t)edx) << 32 | eax; 43767730e6cSSean Christopherson return tsc_val; 43867730e6cSSean Christopherson } 43967730e6cSSean Christopherson 44067730e6cSSean Christopherson static inline uint64_t rdtscp(uint32_t *aux) 44167730e6cSSean Christopherson { 44267730e6cSSean Christopherson uint32_t eax, edx; 44367730e6cSSean Christopherson 44467730e6cSSean Christopherson __asm__ __volatile__("rdtscp" : "=a"(eax), "=d"(edx), "=c"(*aux)); 44567730e6cSSean Christopherson return ((uint64_t)edx) << 32 | eax; 44667730e6cSSean Christopherson } 44767730e6cSSean Christopherson 44867730e6cSSean Christopherson static inline uint64_t rdmsr(uint32_t msr) 44967730e6cSSean Christopherson { 45067730e6cSSean Christopherson uint32_t a, d; 45167730e6cSSean Christopherson 45267730e6cSSean Christopherson __asm__ __volatile__("rdmsr" : "=a"(a), "=d"(d) : "c"(msr) : "memory"); 45367730e6cSSean Christopherson 45467730e6cSSean Christopherson return a | ((uint64_t) d << 32); 45567730e6cSSean Christopherson } 45667730e6cSSean Christopherson 45767730e6cSSean Christopherson static inline void wrmsr(uint32_t msr, uint64_t value) 45867730e6cSSean Christopherson { 45967730e6cSSean Christopherson uint32_t a = value; 46067730e6cSSean Christopherson uint32_t d = value >> 32; 46167730e6cSSean Christopherson 46267730e6cSSean Christopherson __asm__ __volatile__("wrmsr" :: "a"(a), "d"(d), "c"(msr) : "memory"); 46367730e6cSSean Christopherson } 46467730e6cSSean Christopherson 46567730e6cSSean Christopherson 46667730e6cSSean Christopherson static inline uint16_t inw(uint16_t port) 46767730e6cSSean Christopherson { 46867730e6cSSean Christopherson uint16_t tmp; 46967730e6cSSean Christopherson 47067730e6cSSean Christopherson __asm__ __volatile__("in %%dx, %%ax" 47167730e6cSSean Christopherson : /* output */ "=a" (tmp) 47267730e6cSSean Christopherson : /* input */ "d" (port)); 47367730e6cSSean Christopherson 47467730e6cSSean Christopherson return tmp; 47567730e6cSSean Christopherson } 47667730e6cSSean Christopherson 47767730e6cSSean Christopherson static inline uint16_t get_es(void) 47867730e6cSSean Christopherson { 47967730e6cSSean Christopherson uint16_t es; 48067730e6cSSean Christopherson 48167730e6cSSean Christopherson __asm__ __volatile__("mov %%es, %[es]" 48267730e6cSSean Christopherson : /* output */ [es]"=rm"(es)); 48367730e6cSSean Christopherson return es; 48467730e6cSSean Christopherson } 48567730e6cSSean Christopherson 48667730e6cSSean Christopherson static inline uint16_t get_cs(void) 48767730e6cSSean Christopherson { 48867730e6cSSean Christopherson uint16_t cs; 48967730e6cSSean Christopherson 49067730e6cSSean Christopherson __asm__ __volatile__("mov %%cs, %[cs]" 49167730e6cSSean Christopherson : /* output */ [cs]"=rm"(cs)); 49267730e6cSSean Christopherson return cs; 49367730e6cSSean Christopherson } 49467730e6cSSean Christopherson 49567730e6cSSean Christopherson static inline uint16_t get_ss(void) 49667730e6cSSean Christopherson { 49767730e6cSSean Christopherson uint16_t ss; 49867730e6cSSean Christopherson 49967730e6cSSean Christopherson __asm__ __volatile__("mov %%ss, %[ss]" 50067730e6cSSean Christopherson : /* output */ [ss]"=rm"(ss)); 50167730e6cSSean Christopherson return ss; 50267730e6cSSean Christopherson } 50367730e6cSSean Christopherson 50467730e6cSSean Christopherson static inline uint16_t get_ds(void) 50567730e6cSSean Christopherson { 50667730e6cSSean Christopherson uint16_t ds; 50767730e6cSSean Christopherson 50867730e6cSSean Christopherson __asm__ __volatile__("mov %%ds, %[ds]" 50967730e6cSSean Christopherson : /* output */ [ds]"=rm"(ds)); 51067730e6cSSean Christopherson return ds; 51167730e6cSSean Christopherson } 51267730e6cSSean Christopherson 51367730e6cSSean Christopherson static inline uint16_t get_fs(void) 51467730e6cSSean Christopherson { 51567730e6cSSean Christopherson uint16_t fs; 51667730e6cSSean Christopherson 51767730e6cSSean Christopherson __asm__ __volatile__("mov %%fs, %[fs]" 51867730e6cSSean Christopherson : /* output */ [fs]"=rm"(fs)); 51967730e6cSSean Christopherson return fs; 52067730e6cSSean Christopherson } 52167730e6cSSean Christopherson 52267730e6cSSean Christopherson static inline uint16_t get_gs(void) 52367730e6cSSean Christopherson { 52467730e6cSSean Christopherson uint16_t gs; 52567730e6cSSean Christopherson 52667730e6cSSean Christopherson __asm__ __volatile__("mov %%gs, %[gs]" 52767730e6cSSean Christopherson : /* output */ [gs]"=rm"(gs)); 52867730e6cSSean Christopherson return gs; 52967730e6cSSean Christopherson } 53067730e6cSSean Christopherson 53167730e6cSSean Christopherson static inline uint16_t get_tr(void) 53267730e6cSSean Christopherson { 53367730e6cSSean Christopherson uint16_t tr; 53467730e6cSSean Christopherson 53567730e6cSSean Christopherson __asm__ __volatile__("str %[tr]" 53667730e6cSSean Christopherson : /* output */ [tr]"=rm"(tr)); 53767730e6cSSean Christopherson return tr; 53867730e6cSSean Christopherson } 53967730e6cSSean Christopherson 54067730e6cSSean Christopherson static inline uint64_t get_cr0(void) 54167730e6cSSean Christopherson { 54267730e6cSSean Christopherson uint64_t cr0; 54367730e6cSSean Christopherson 54467730e6cSSean Christopherson __asm__ __volatile__("mov %%cr0, %[cr0]" 54567730e6cSSean Christopherson : /* output */ [cr0]"=r"(cr0)); 54667730e6cSSean Christopherson return cr0; 54767730e6cSSean Christopherson } 54867730e6cSSean Christopherson 54967730e6cSSean Christopherson static inline uint64_t get_cr3(void) 55067730e6cSSean Christopherson { 55167730e6cSSean Christopherson uint64_t cr3; 55267730e6cSSean Christopherson 55367730e6cSSean Christopherson __asm__ __volatile__("mov %%cr3, %[cr3]" 55467730e6cSSean Christopherson : /* output */ [cr3]"=r"(cr3)); 55567730e6cSSean Christopherson return cr3; 55667730e6cSSean Christopherson } 55767730e6cSSean Christopherson 55867730e6cSSean Christopherson static inline uint64_t get_cr4(void) 55967730e6cSSean Christopherson { 56067730e6cSSean Christopherson uint64_t cr4; 56167730e6cSSean Christopherson 56267730e6cSSean Christopherson __asm__ __volatile__("mov %%cr4, %[cr4]" 56367730e6cSSean Christopherson : /* output */ [cr4]"=r"(cr4)); 56467730e6cSSean Christopherson return cr4; 56567730e6cSSean Christopherson } 56667730e6cSSean Christopherson 56767730e6cSSean Christopherson static inline void set_cr4(uint64_t val) 56867730e6cSSean Christopherson { 56967730e6cSSean Christopherson __asm__ __volatile__("mov %0, %%cr4" : : "r" (val) : "memory"); 57067730e6cSSean Christopherson } 57167730e6cSSean Christopherson 57267730e6cSSean Christopherson static inline u64 xgetbv(u32 index) 57367730e6cSSean Christopherson { 57467730e6cSSean Christopherson u32 eax, edx; 57567730e6cSSean Christopherson 57667730e6cSSean Christopherson __asm__ __volatile__("xgetbv;" 57767730e6cSSean Christopherson : "=a" (eax), "=d" (edx) 57867730e6cSSean Christopherson : "c" (index)); 57967730e6cSSean Christopherson return eax | ((u64)edx << 32); 58067730e6cSSean Christopherson } 58167730e6cSSean Christopherson 58267730e6cSSean Christopherson static inline void xsetbv(u32 index, u64 value) 58367730e6cSSean Christopherson { 58467730e6cSSean Christopherson u32 eax = value; 58567730e6cSSean Christopherson u32 edx = value >> 32; 58667730e6cSSean Christopherson 58767730e6cSSean Christopherson __asm__ __volatile__("xsetbv" :: "a" (eax), "d" (edx), "c" (index)); 58867730e6cSSean Christopherson } 58967730e6cSSean Christopherson 59067730e6cSSean Christopherson static inline void wrpkru(u32 pkru) 59167730e6cSSean Christopherson { 59267730e6cSSean Christopherson /* Note, ECX and EDX are architecturally required to be '0'. */ 59367730e6cSSean Christopherson asm volatile(".byte 0x0f,0x01,0xef\n\t" 59467730e6cSSean Christopherson : : "a" (pkru), "c"(0), "d"(0)); 59567730e6cSSean Christopherson } 59667730e6cSSean Christopherson 59767730e6cSSean Christopherson static inline struct desc_ptr get_gdt(void) 59867730e6cSSean Christopherson { 59967730e6cSSean Christopherson struct desc_ptr gdt; 60067730e6cSSean Christopherson __asm__ __volatile__("sgdt %[gdt]" 60167730e6cSSean Christopherson : /* output */ [gdt]"=m"(gdt)); 60267730e6cSSean Christopherson return gdt; 60367730e6cSSean Christopherson } 60467730e6cSSean Christopherson 60567730e6cSSean Christopherson static inline struct desc_ptr get_idt(void) 60667730e6cSSean Christopherson { 60767730e6cSSean Christopherson struct desc_ptr idt; 60867730e6cSSean Christopherson __asm__ __volatile__("sidt %[idt]" 60967730e6cSSean Christopherson : /* output */ [idt]"=m"(idt)); 61067730e6cSSean Christopherson return idt; 61167730e6cSSean Christopherson } 61267730e6cSSean Christopherson 61367730e6cSSean Christopherson static inline void outl(uint16_t port, uint32_t value) 61467730e6cSSean Christopherson { 61567730e6cSSean Christopherson __asm__ __volatile__("outl %%eax, %%dx" : : "d"(port), "a"(value)); 61667730e6cSSean Christopherson } 61767730e6cSSean Christopherson 61867730e6cSSean Christopherson static inline void __cpuid(uint32_t function, uint32_t index, 61967730e6cSSean Christopherson uint32_t *eax, uint32_t *ebx, 62067730e6cSSean Christopherson uint32_t *ecx, uint32_t *edx) 62167730e6cSSean Christopherson { 62267730e6cSSean Christopherson *eax = function; 62367730e6cSSean Christopherson *ecx = index; 62467730e6cSSean Christopherson 62567730e6cSSean Christopherson asm volatile("cpuid" 62667730e6cSSean Christopherson : "=a" (*eax), 62767730e6cSSean Christopherson "=b" (*ebx), 62867730e6cSSean Christopherson "=c" (*ecx), 62967730e6cSSean Christopherson "=d" (*edx) 63067730e6cSSean Christopherson : "0" (*eax), "2" (*ecx) 63167730e6cSSean Christopherson : "memory"); 63267730e6cSSean Christopherson } 63367730e6cSSean Christopherson 63467730e6cSSean Christopherson static inline void cpuid(uint32_t function, 63567730e6cSSean Christopherson uint32_t *eax, uint32_t *ebx, 63667730e6cSSean Christopherson uint32_t *ecx, uint32_t *edx) 63767730e6cSSean Christopherson { 63867730e6cSSean Christopherson return __cpuid(function, 0, eax, ebx, ecx, edx); 63967730e6cSSean Christopherson } 64067730e6cSSean Christopherson 64167730e6cSSean Christopherson static inline uint32_t this_cpu_fms(void) 64267730e6cSSean Christopherson { 64367730e6cSSean Christopherson uint32_t eax, ebx, ecx, edx; 64467730e6cSSean Christopherson 64567730e6cSSean Christopherson cpuid(1, &eax, &ebx, &ecx, &edx); 64667730e6cSSean Christopherson return eax; 64767730e6cSSean Christopherson } 64867730e6cSSean Christopherson 64967730e6cSSean Christopherson static inline uint32_t this_cpu_family(void) 65067730e6cSSean Christopherson { 65167730e6cSSean Christopherson return x86_family(this_cpu_fms()); 65267730e6cSSean Christopherson } 65367730e6cSSean Christopherson 65467730e6cSSean Christopherson static inline uint32_t this_cpu_model(void) 65567730e6cSSean Christopherson { 65667730e6cSSean Christopherson return x86_model(this_cpu_fms()); 65767730e6cSSean Christopherson } 65867730e6cSSean Christopherson 65967730e6cSSean Christopherson static inline bool this_cpu_vendor_string_is(const char *vendor) 66067730e6cSSean Christopherson { 66167730e6cSSean Christopherson const uint32_t *chunk = (const uint32_t *)vendor; 66267730e6cSSean Christopherson uint32_t eax, ebx, ecx, edx; 66367730e6cSSean Christopherson 66467730e6cSSean Christopherson cpuid(0, &eax, &ebx, &ecx, &edx); 66567730e6cSSean Christopherson return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]); 66667730e6cSSean Christopherson } 66767730e6cSSean Christopherson 66867730e6cSSean Christopherson static inline bool this_cpu_is_intel(void) 66967730e6cSSean Christopherson { 67067730e6cSSean Christopherson return this_cpu_vendor_string_is("GenuineIntel"); 67167730e6cSSean Christopherson } 67267730e6cSSean Christopherson 67367730e6cSSean Christopherson /* 67467730e6cSSean Christopherson * Exclude early K5 samples with a vendor string of "AMDisbetter!" 67567730e6cSSean Christopherson */ 67667730e6cSSean Christopherson static inline bool this_cpu_is_amd(void) 67767730e6cSSean Christopherson { 67867730e6cSSean Christopherson return this_cpu_vendor_string_is("AuthenticAMD"); 67967730e6cSSean Christopherson } 68067730e6cSSean Christopherson 68167730e6cSSean Christopherson static inline uint32_t __this_cpu_has(uint32_t function, uint32_t index, 68267730e6cSSean Christopherson uint8_t reg, uint8_t lo, uint8_t hi) 68367730e6cSSean Christopherson { 68467730e6cSSean Christopherson uint32_t gprs[4]; 68567730e6cSSean Christopherson 68667730e6cSSean Christopherson __cpuid(function, index, 68767730e6cSSean Christopherson &gprs[KVM_CPUID_EAX], &gprs[KVM_CPUID_EBX], 68867730e6cSSean Christopherson &gprs[KVM_CPUID_ECX], &gprs[KVM_CPUID_EDX]); 68967730e6cSSean Christopherson 69067730e6cSSean Christopherson return (gprs[reg] & GENMASK(hi, lo)) >> lo; 69167730e6cSSean Christopherson } 69267730e6cSSean Christopherson 69367730e6cSSean Christopherson static inline bool this_cpu_has(struct kvm_x86_cpu_feature feature) 69467730e6cSSean Christopherson { 69567730e6cSSean Christopherson return __this_cpu_has(feature.function, feature.index, 69667730e6cSSean Christopherson feature.reg, feature.bit, feature.bit); 69767730e6cSSean Christopherson } 69867730e6cSSean Christopherson 69967730e6cSSean Christopherson static inline uint32_t this_cpu_property(struct kvm_x86_cpu_property property) 70067730e6cSSean Christopherson { 70167730e6cSSean Christopherson return __this_cpu_has(property.function, property.index, 70267730e6cSSean Christopherson property.reg, property.lo_bit, property.hi_bit); 70367730e6cSSean Christopherson } 70467730e6cSSean Christopherson 70567730e6cSSean Christopherson static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property) 70667730e6cSSean Christopherson { 70767730e6cSSean Christopherson uint32_t max_leaf; 70867730e6cSSean Christopherson 70967730e6cSSean Christopherson switch (property.function & 0xc0000000) { 71067730e6cSSean Christopherson case 0: 71167730e6cSSean Christopherson max_leaf = this_cpu_property(X86_PROPERTY_MAX_BASIC_LEAF); 71267730e6cSSean Christopherson break; 71367730e6cSSean Christopherson case 0x40000000: 71467730e6cSSean Christopherson max_leaf = this_cpu_property(X86_PROPERTY_MAX_KVM_LEAF); 71567730e6cSSean Christopherson break; 71667730e6cSSean Christopherson case 0x80000000: 71767730e6cSSean Christopherson max_leaf = this_cpu_property(X86_PROPERTY_MAX_EXT_LEAF); 71867730e6cSSean Christopherson break; 71967730e6cSSean Christopherson case 0xc0000000: 72067730e6cSSean Christopherson max_leaf = this_cpu_property(X86_PROPERTY_MAX_CENTAUR_LEAF); 72167730e6cSSean Christopherson } 72267730e6cSSean Christopherson return max_leaf >= property.function; 72367730e6cSSean Christopherson } 72467730e6cSSean Christopherson 72567730e6cSSean Christopherson static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature) 72667730e6cSSean Christopherson { 72767730e6cSSean Christopherson uint32_t nr_bits; 72867730e6cSSean Christopherson 72967730e6cSSean Christopherson if (feature.f.reg == KVM_CPUID_EBX) { 73067730e6cSSean Christopherson nr_bits = this_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); 73167730e6cSSean Christopherson return nr_bits > feature.f.bit && !this_cpu_has(feature.f); 73267730e6cSSean Christopherson } 73367730e6cSSean Christopherson 73467730e6cSSean Christopherson GUEST_ASSERT(feature.f.reg == KVM_CPUID_ECX); 73567730e6cSSean Christopherson nr_bits = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); 73667730e6cSSean Christopherson return nr_bits > feature.f.bit || this_cpu_has(feature.f); 73767730e6cSSean Christopherson } 73867730e6cSSean Christopherson 73967730e6cSSean Christopherson static __always_inline uint64_t this_cpu_supported_xcr0(void) 74067730e6cSSean Christopherson { 74167730e6cSSean Christopherson if (!this_cpu_has_p(X86_PROPERTY_SUPPORTED_XCR0_LO)) 74267730e6cSSean Christopherson return 0; 74367730e6cSSean Christopherson 74467730e6cSSean Christopherson return this_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_LO) | 74567730e6cSSean Christopherson ((uint64_t)this_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_HI) << 32); 74667730e6cSSean Christopherson } 74767730e6cSSean Christopherson 74867730e6cSSean Christopherson typedef u32 __attribute__((vector_size(16))) sse128_t; 74967730e6cSSean Christopherson #define __sse128_u union { sse128_t vec; u64 as_u64[2]; u32 as_u32[4]; } 75067730e6cSSean Christopherson #define sse128_lo(x) ({ __sse128_u t; t.vec = x; t.as_u64[0]; }) 75167730e6cSSean Christopherson #define sse128_hi(x) ({ __sse128_u t; t.vec = x; t.as_u64[1]; }) 75267730e6cSSean Christopherson 75367730e6cSSean Christopherson static inline void read_sse_reg(int reg, sse128_t *data) 75467730e6cSSean Christopherson { 75567730e6cSSean Christopherson switch (reg) { 75667730e6cSSean Christopherson case 0: 75767730e6cSSean Christopherson asm("movdqa %%xmm0, %0" : "=m"(*data)); 75867730e6cSSean Christopherson break; 75967730e6cSSean Christopherson case 1: 76067730e6cSSean Christopherson asm("movdqa %%xmm1, %0" : "=m"(*data)); 76167730e6cSSean Christopherson break; 76267730e6cSSean Christopherson case 2: 76367730e6cSSean Christopherson asm("movdqa %%xmm2, %0" : "=m"(*data)); 76467730e6cSSean Christopherson break; 76567730e6cSSean Christopherson case 3: 76667730e6cSSean Christopherson asm("movdqa %%xmm3, %0" : "=m"(*data)); 76767730e6cSSean Christopherson break; 76867730e6cSSean Christopherson case 4: 76967730e6cSSean Christopherson asm("movdqa %%xmm4, %0" : "=m"(*data)); 77067730e6cSSean Christopherson break; 77167730e6cSSean Christopherson case 5: 77267730e6cSSean Christopherson asm("movdqa %%xmm5, %0" : "=m"(*data)); 77367730e6cSSean Christopherson break; 77467730e6cSSean Christopherson case 6: 77567730e6cSSean Christopherson asm("movdqa %%xmm6, %0" : "=m"(*data)); 77667730e6cSSean Christopherson break; 77767730e6cSSean Christopherson case 7: 77867730e6cSSean Christopherson asm("movdqa %%xmm7, %0" : "=m"(*data)); 77967730e6cSSean Christopherson break; 78067730e6cSSean Christopherson default: 78167730e6cSSean Christopherson BUG(); 78267730e6cSSean Christopherson } 78367730e6cSSean Christopherson } 78467730e6cSSean Christopherson 78567730e6cSSean Christopherson static inline void write_sse_reg(int reg, const sse128_t *data) 78667730e6cSSean Christopherson { 78767730e6cSSean Christopherson switch (reg) { 78867730e6cSSean Christopherson case 0: 78967730e6cSSean Christopherson asm("movdqa %0, %%xmm0" : : "m"(*data)); 79067730e6cSSean Christopherson break; 79167730e6cSSean Christopherson case 1: 79267730e6cSSean Christopherson asm("movdqa %0, %%xmm1" : : "m"(*data)); 79367730e6cSSean Christopherson break; 79467730e6cSSean Christopherson case 2: 79567730e6cSSean Christopherson asm("movdqa %0, %%xmm2" : : "m"(*data)); 79667730e6cSSean Christopherson break; 79767730e6cSSean Christopherson case 3: 79867730e6cSSean Christopherson asm("movdqa %0, %%xmm3" : : "m"(*data)); 79967730e6cSSean Christopherson break; 80067730e6cSSean Christopherson case 4: 80167730e6cSSean Christopherson asm("movdqa %0, %%xmm4" : : "m"(*data)); 80267730e6cSSean Christopherson break; 80367730e6cSSean Christopherson case 5: 80467730e6cSSean Christopherson asm("movdqa %0, %%xmm5" : : "m"(*data)); 80567730e6cSSean Christopherson break; 80667730e6cSSean Christopherson case 6: 80767730e6cSSean Christopherson asm("movdqa %0, %%xmm6" : : "m"(*data)); 80867730e6cSSean Christopherson break; 80967730e6cSSean Christopherson case 7: 81067730e6cSSean Christopherson asm("movdqa %0, %%xmm7" : : "m"(*data)); 81167730e6cSSean Christopherson break; 81267730e6cSSean Christopherson default: 81367730e6cSSean Christopherson BUG(); 81467730e6cSSean Christopherson } 81567730e6cSSean Christopherson } 81667730e6cSSean Christopherson 81767730e6cSSean Christopherson static inline void cpu_relax(void) 81867730e6cSSean Christopherson { 81967730e6cSSean Christopherson asm volatile("rep; nop" ::: "memory"); 82067730e6cSSean Christopherson } 82167730e6cSSean Christopherson 82267730e6cSSean Christopherson static inline void udelay(unsigned long usec) 82367730e6cSSean Christopherson { 82467730e6cSSean Christopherson uint64_t start, now, cycles; 82567730e6cSSean Christopherson 82667730e6cSSean Christopherson GUEST_ASSERT(guest_tsc_khz); 82767730e6cSSean Christopherson cycles = guest_tsc_khz / 1000 * usec; 82867730e6cSSean Christopherson 82967730e6cSSean Christopherson /* 83067730e6cSSean Christopherson * Deliberately don't PAUSE, a.k.a. cpu_relax(), so that the delay is 83167730e6cSSean Christopherson * as accurate as possible, e.g. doesn't trigger PAUSE-Loop VM-Exits. 83267730e6cSSean Christopherson */ 83367730e6cSSean Christopherson start = rdtsc(); 83467730e6cSSean Christopherson do { 83567730e6cSSean Christopherson now = rdtsc(); 83667730e6cSSean Christopherson } while (now - start < cycles); 83767730e6cSSean Christopherson } 83867730e6cSSean Christopherson 83967730e6cSSean Christopherson #define ud2() \ 84067730e6cSSean Christopherson __asm__ __volatile__( \ 84167730e6cSSean Christopherson "ud2\n" \ 84267730e6cSSean Christopherson ) 84367730e6cSSean Christopherson 84467730e6cSSean Christopherson #define hlt() \ 84567730e6cSSean Christopherson __asm__ __volatile__( \ 84667730e6cSSean Christopherson "hlt\n" \ 84767730e6cSSean Christopherson ) 84867730e6cSSean Christopherson 84967730e6cSSean Christopherson struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu); 85067730e6cSSean Christopherson void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state); 85167730e6cSSean Christopherson void kvm_x86_state_cleanup(struct kvm_x86_state *state); 85267730e6cSSean Christopherson 85367730e6cSSean Christopherson const struct kvm_msr_list *kvm_get_msr_index_list(void); 85467730e6cSSean Christopherson const struct kvm_msr_list *kvm_get_feature_msr_index_list(void); 85567730e6cSSean Christopherson bool kvm_msr_is_in_save_restore_list(uint32_t msr_index); 85667730e6cSSean Christopherson uint64_t kvm_get_feature_msr(uint64_t msr_index); 85767730e6cSSean Christopherson 85867730e6cSSean Christopherson static inline void vcpu_msrs_get(struct kvm_vcpu *vcpu, 85967730e6cSSean Christopherson struct kvm_msrs *msrs) 86067730e6cSSean Christopherson { 86167730e6cSSean Christopherson int r = __vcpu_ioctl(vcpu, KVM_GET_MSRS, msrs); 86267730e6cSSean Christopherson 86367730e6cSSean Christopherson TEST_ASSERT(r == msrs->nmsrs, 86467730e6cSSean Christopherson "KVM_GET_MSRS failed, r: %i (failed on MSR %x)", 86567730e6cSSean Christopherson r, r < 0 || r >= msrs->nmsrs ? -1 : msrs->entries[r].index); 86667730e6cSSean Christopherson } 86767730e6cSSean Christopherson static inline void vcpu_msrs_set(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs) 86867730e6cSSean Christopherson { 86967730e6cSSean Christopherson int r = __vcpu_ioctl(vcpu, KVM_SET_MSRS, msrs); 87067730e6cSSean Christopherson 87167730e6cSSean Christopherson TEST_ASSERT(r == msrs->nmsrs, 87267730e6cSSean Christopherson "KVM_SET_MSRS failed, r: %i (failed on MSR %x)", 87367730e6cSSean Christopherson r, r < 0 || r >= msrs->nmsrs ? -1 : msrs->entries[r].index); 87467730e6cSSean Christopherson } 87567730e6cSSean Christopherson static inline void vcpu_debugregs_get(struct kvm_vcpu *vcpu, 87667730e6cSSean Christopherson struct kvm_debugregs *debugregs) 87767730e6cSSean Christopherson { 87867730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_GET_DEBUGREGS, debugregs); 87967730e6cSSean Christopherson } 88067730e6cSSean Christopherson static inline void vcpu_debugregs_set(struct kvm_vcpu *vcpu, 88167730e6cSSean Christopherson struct kvm_debugregs *debugregs) 88267730e6cSSean Christopherson { 88367730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_SET_DEBUGREGS, debugregs); 88467730e6cSSean Christopherson } 88567730e6cSSean Christopherson static inline void vcpu_xsave_get(struct kvm_vcpu *vcpu, 88667730e6cSSean Christopherson struct kvm_xsave *xsave) 88767730e6cSSean Christopherson { 88867730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_GET_XSAVE, xsave); 88967730e6cSSean Christopherson } 89067730e6cSSean Christopherson static inline void vcpu_xsave2_get(struct kvm_vcpu *vcpu, 89167730e6cSSean Christopherson struct kvm_xsave *xsave) 89267730e6cSSean Christopherson { 89367730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_GET_XSAVE2, xsave); 89467730e6cSSean Christopherson } 89567730e6cSSean Christopherson static inline void vcpu_xsave_set(struct kvm_vcpu *vcpu, 89667730e6cSSean Christopherson struct kvm_xsave *xsave) 89767730e6cSSean Christopherson { 89867730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_SET_XSAVE, xsave); 89967730e6cSSean Christopherson } 90067730e6cSSean Christopherson static inline void vcpu_xcrs_get(struct kvm_vcpu *vcpu, 90167730e6cSSean Christopherson struct kvm_xcrs *xcrs) 90267730e6cSSean Christopherson { 90367730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_GET_XCRS, xcrs); 90467730e6cSSean Christopherson } 90567730e6cSSean Christopherson static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs) 90667730e6cSSean Christopherson { 90767730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_SET_XCRS, xcrs); 90867730e6cSSean Christopherson } 90967730e6cSSean Christopherson 91067730e6cSSean Christopherson const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid, 91167730e6cSSean Christopherson uint32_t function, uint32_t index); 91267730e6cSSean Christopherson const struct kvm_cpuid2 *kvm_get_supported_cpuid(void); 91367730e6cSSean Christopherson 91467730e6cSSean Christopherson static inline uint32_t kvm_cpu_fms(void) 91567730e6cSSean Christopherson { 91667730e6cSSean Christopherson return get_cpuid_entry(kvm_get_supported_cpuid(), 0x1, 0)->eax; 91767730e6cSSean Christopherson } 91867730e6cSSean Christopherson 91967730e6cSSean Christopherson static inline uint32_t kvm_cpu_family(void) 92067730e6cSSean Christopherson { 92167730e6cSSean Christopherson return x86_family(kvm_cpu_fms()); 92267730e6cSSean Christopherson } 92367730e6cSSean Christopherson 92467730e6cSSean Christopherson static inline uint32_t kvm_cpu_model(void) 92567730e6cSSean Christopherson { 92667730e6cSSean Christopherson return x86_model(kvm_cpu_fms()); 92767730e6cSSean Christopherson } 92867730e6cSSean Christopherson 92967730e6cSSean Christopherson bool kvm_cpuid_has(const struct kvm_cpuid2 *cpuid, 93067730e6cSSean Christopherson struct kvm_x86_cpu_feature feature); 93167730e6cSSean Christopherson 93267730e6cSSean Christopherson static inline bool kvm_cpu_has(struct kvm_x86_cpu_feature feature) 93367730e6cSSean Christopherson { 93467730e6cSSean Christopherson return kvm_cpuid_has(kvm_get_supported_cpuid(), feature); 93567730e6cSSean Christopherson } 93667730e6cSSean Christopherson 93767730e6cSSean Christopherson uint32_t kvm_cpuid_property(const struct kvm_cpuid2 *cpuid, 93867730e6cSSean Christopherson struct kvm_x86_cpu_property property); 93967730e6cSSean Christopherson 94067730e6cSSean Christopherson static inline uint32_t kvm_cpu_property(struct kvm_x86_cpu_property property) 94167730e6cSSean Christopherson { 94267730e6cSSean Christopherson return kvm_cpuid_property(kvm_get_supported_cpuid(), property); 94367730e6cSSean Christopherson } 94467730e6cSSean Christopherson 94567730e6cSSean Christopherson static __always_inline bool kvm_cpu_has_p(struct kvm_x86_cpu_property property) 94667730e6cSSean Christopherson { 94767730e6cSSean Christopherson uint32_t max_leaf; 94867730e6cSSean Christopherson 94967730e6cSSean Christopherson switch (property.function & 0xc0000000) { 95067730e6cSSean Christopherson case 0: 95167730e6cSSean Christopherson max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_BASIC_LEAF); 95267730e6cSSean Christopherson break; 95367730e6cSSean Christopherson case 0x40000000: 95467730e6cSSean Christopherson max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_KVM_LEAF); 95567730e6cSSean Christopherson break; 95667730e6cSSean Christopherson case 0x80000000: 95767730e6cSSean Christopherson max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_EXT_LEAF); 95867730e6cSSean Christopherson break; 95967730e6cSSean Christopherson case 0xc0000000: 96067730e6cSSean Christopherson max_leaf = kvm_cpu_property(X86_PROPERTY_MAX_CENTAUR_LEAF); 96167730e6cSSean Christopherson } 96267730e6cSSean Christopherson return max_leaf >= property.function; 96367730e6cSSean Christopherson } 96467730e6cSSean Christopherson 96567730e6cSSean Christopherson static inline bool kvm_pmu_has(struct kvm_x86_pmu_feature feature) 96667730e6cSSean Christopherson { 96767730e6cSSean Christopherson uint32_t nr_bits; 96867730e6cSSean Christopherson 96967730e6cSSean Christopherson if (feature.f.reg == KVM_CPUID_EBX) { 97067730e6cSSean Christopherson nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH); 97167730e6cSSean Christopherson return nr_bits > feature.f.bit && !kvm_cpu_has(feature.f); 97267730e6cSSean Christopherson } 97367730e6cSSean Christopherson 97467730e6cSSean Christopherson TEST_ASSERT_EQ(feature.f.reg, KVM_CPUID_ECX); 97567730e6cSSean Christopherson nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); 97667730e6cSSean Christopherson return nr_bits > feature.f.bit || kvm_cpu_has(feature.f); 97767730e6cSSean Christopherson } 97867730e6cSSean Christopherson 97967730e6cSSean Christopherson static __always_inline uint64_t kvm_cpu_supported_xcr0(void) 98067730e6cSSean Christopherson { 98167730e6cSSean Christopherson if (!kvm_cpu_has_p(X86_PROPERTY_SUPPORTED_XCR0_LO)) 98267730e6cSSean Christopherson return 0; 98367730e6cSSean Christopherson 98467730e6cSSean Christopherson return kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_LO) | 98567730e6cSSean Christopherson ((uint64_t)kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_HI) << 32); 98667730e6cSSean Christopherson } 98767730e6cSSean Christopherson 98867730e6cSSean Christopherson static inline size_t kvm_cpuid2_size(int nr_entries) 98967730e6cSSean Christopherson { 99067730e6cSSean Christopherson return sizeof(struct kvm_cpuid2) + 99167730e6cSSean Christopherson sizeof(struct kvm_cpuid_entry2) * nr_entries; 99267730e6cSSean Christopherson } 99367730e6cSSean Christopherson 99467730e6cSSean Christopherson /* 99567730e6cSSean Christopherson * Allocate a "struct kvm_cpuid2* instance, with the 0-length arrary of 99667730e6cSSean Christopherson * entries sized to hold @nr_entries. The caller is responsible for freeing 99767730e6cSSean Christopherson * the struct. 99867730e6cSSean Christopherson */ 99967730e6cSSean Christopherson static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries) 100067730e6cSSean Christopherson { 100167730e6cSSean Christopherson struct kvm_cpuid2 *cpuid; 100267730e6cSSean Christopherson 100367730e6cSSean Christopherson cpuid = malloc(kvm_cpuid2_size(nr_entries)); 100467730e6cSSean Christopherson TEST_ASSERT(cpuid, "-ENOMEM when allocating kvm_cpuid2"); 100567730e6cSSean Christopherson 100667730e6cSSean Christopherson cpuid->nent = nr_entries; 100767730e6cSSean Christopherson 100867730e6cSSean Christopherson return cpuid; 100967730e6cSSean Christopherson } 101067730e6cSSean Christopherson 101167730e6cSSean Christopherson void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid); 101267730e6cSSean Christopherson 1013*a2a791e8SSean Christopherson static inline void vcpu_get_cpuid(struct kvm_vcpu *vcpu) 1014*a2a791e8SSean Christopherson { 1015*a2a791e8SSean Christopherson vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid); 1016*a2a791e8SSean Christopherson } 1017*a2a791e8SSean Christopherson 101867730e6cSSean Christopherson static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu, 101967730e6cSSean Christopherson uint32_t function, 102067730e6cSSean Christopherson uint32_t index) 102167730e6cSSean Christopherson { 102208833719SSean Christopherson TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first (or equivalent)"); 102308833719SSean Christopherson 1024*a2a791e8SSean Christopherson vcpu_get_cpuid(vcpu); 1025*a2a791e8SSean Christopherson 102667730e6cSSean Christopherson return (struct kvm_cpuid_entry2 *)get_cpuid_entry(vcpu->cpuid, 102767730e6cSSean Christopherson function, index); 102867730e6cSSean Christopherson } 102967730e6cSSean Christopherson 103067730e6cSSean Christopherson static inline struct kvm_cpuid_entry2 *vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu, 103167730e6cSSean Christopherson uint32_t function) 103267730e6cSSean Christopherson { 103367730e6cSSean Christopherson return __vcpu_get_cpuid_entry(vcpu, function, 0); 103467730e6cSSean Christopherson } 103567730e6cSSean Christopherson 103667730e6cSSean Christopherson static inline int __vcpu_set_cpuid(struct kvm_vcpu *vcpu) 103767730e6cSSean Christopherson { 103867730e6cSSean Christopherson int r; 103967730e6cSSean Christopherson 104067730e6cSSean Christopherson TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first"); 104167730e6cSSean Christopherson r = __vcpu_ioctl(vcpu, KVM_SET_CPUID2, vcpu->cpuid); 104267730e6cSSean Christopherson if (r) 104367730e6cSSean Christopherson return r; 104467730e6cSSean Christopherson 104567730e6cSSean Christopherson /* On success, refresh the cache to pick up adjustments made by KVM. */ 1046*a2a791e8SSean Christopherson vcpu_get_cpuid(vcpu); 104767730e6cSSean Christopherson return 0; 104867730e6cSSean Christopherson } 104967730e6cSSean Christopherson 105067730e6cSSean Christopherson static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu) 105167730e6cSSean Christopherson { 105267730e6cSSean Christopherson TEST_ASSERT(vcpu->cpuid, "Must do vcpu_init_cpuid() first"); 105367730e6cSSean Christopherson vcpu_ioctl(vcpu, KVM_SET_CPUID2, vcpu->cpuid); 105467730e6cSSean Christopherson 105567730e6cSSean Christopherson /* Refresh the cache to pick up adjustments made by KVM. */ 1056*a2a791e8SSean Christopherson vcpu_get_cpuid(vcpu); 105767730e6cSSean Christopherson } 105867730e6cSSean Christopherson 105967730e6cSSean Christopherson void vcpu_set_cpuid_property(struct kvm_vcpu *vcpu, 106067730e6cSSean Christopherson struct kvm_x86_cpu_property property, 106167730e6cSSean Christopherson uint32_t value); 106267730e6cSSean Christopherson void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, uint8_t maxphyaddr); 106367730e6cSSean Christopherson 106467730e6cSSean Christopherson void vcpu_clear_cpuid_entry(struct kvm_vcpu *vcpu, uint32_t function); 106567730e6cSSean Christopherson 106667730e6cSSean Christopherson static inline bool vcpu_cpuid_has(struct kvm_vcpu *vcpu, 106767730e6cSSean Christopherson struct kvm_x86_cpu_feature feature) 106867730e6cSSean Christopherson { 106967730e6cSSean Christopherson struct kvm_cpuid_entry2 *entry; 107067730e6cSSean Christopherson 107167730e6cSSean Christopherson entry = __vcpu_get_cpuid_entry(vcpu, feature.function, feature.index); 107267730e6cSSean Christopherson return *((&entry->eax) + feature.reg) & BIT(feature.bit); 107367730e6cSSean Christopherson } 107467730e6cSSean Christopherson 107567730e6cSSean Christopherson void vcpu_set_or_clear_cpuid_feature(struct kvm_vcpu *vcpu, 107667730e6cSSean Christopherson struct kvm_x86_cpu_feature feature, 107767730e6cSSean Christopherson bool set); 107867730e6cSSean Christopherson 107967730e6cSSean Christopherson static inline void vcpu_set_cpuid_feature(struct kvm_vcpu *vcpu, 108067730e6cSSean Christopherson struct kvm_x86_cpu_feature feature) 108167730e6cSSean Christopherson { 108267730e6cSSean Christopherson vcpu_set_or_clear_cpuid_feature(vcpu, feature, true); 108367730e6cSSean Christopherson 108467730e6cSSean Christopherson } 108567730e6cSSean Christopherson 108667730e6cSSean Christopherson static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu, 108767730e6cSSean Christopherson struct kvm_x86_cpu_feature feature) 108867730e6cSSean Christopherson { 108967730e6cSSean Christopherson vcpu_set_or_clear_cpuid_feature(vcpu, feature, false); 109067730e6cSSean Christopherson } 109167730e6cSSean Christopherson 109267730e6cSSean Christopherson uint64_t vcpu_get_msr(struct kvm_vcpu *vcpu, uint64_t msr_index); 109367730e6cSSean Christopherson int _vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, uint64_t msr_value); 109467730e6cSSean Christopherson 109567730e6cSSean Christopherson /* 109667730e6cSSean Christopherson * Assert on an MSR access(es) and pretty print the MSR name when possible. 109767730e6cSSean Christopherson * Note, the caller provides the stringified name so that the name of macro is 109867730e6cSSean Christopherson * printed, not the value the macro resolves to (due to macro expansion). 109967730e6cSSean Christopherson */ 110067730e6cSSean Christopherson #define TEST_ASSERT_MSR(cond, fmt, msr, str, args...) \ 110167730e6cSSean Christopherson do { \ 110267730e6cSSean Christopherson if (__builtin_constant_p(msr)) { \ 110367730e6cSSean Christopherson TEST_ASSERT(cond, fmt, str, args); \ 110467730e6cSSean Christopherson } else if (!(cond)) { \ 110567730e6cSSean Christopherson char buf[16]; \ 110667730e6cSSean Christopherson \ 110767730e6cSSean Christopherson snprintf(buf, sizeof(buf), "MSR 0x%x", msr); \ 110867730e6cSSean Christopherson TEST_ASSERT(cond, fmt, buf, args); \ 110967730e6cSSean Christopherson } \ 111067730e6cSSean Christopherson } while (0) 111167730e6cSSean Christopherson 111267730e6cSSean Christopherson /* 111367730e6cSSean Christopherson * Returns true if KVM should return the last written value when reading an MSR 111467730e6cSSean Christopherson * from userspace, e.g. the MSR isn't a command MSR, doesn't emulate state that 111567730e6cSSean Christopherson * is changing, etc. This is NOT an exhaustive list! The intent is to filter 111667730e6cSSean Christopherson * out MSRs that are not durable _and_ that a selftest wants to write. 111767730e6cSSean Christopherson */ 111867730e6cSSean Christopherson static inline bool is_durable_msr(uint32_t msr) 111967730e6cSSean Christopherson { 112067730e6cSSean Christopherson return msr != MSR_IA32_TSC; 112167730e6cSSean Christopherson } 112267730e6cSSean Christopherson 112367730e6cSSean Christopherson #define vcpu_set_msr(vcpu, msr, val) \ 112467730e6cSSean Christopherson do { \ 112567730e6cSSean Christopherson uint64_t r, v = val; \ 112667730e6cSSean Christopherson \ 112767730e6cSSean Christopherson TEST_ASSERT_MSR(_vcpu_set_msr(vcpu, msr, v) == 1, \ 112867730e6cSSean Christopherson "KVM_SET_MSRS failed on %s, value = 0x%lx", msr, #msr, v); \ 112967730e6cSSean Christopherson if (!is_durable_msr(msr)) \ 113067730e6cSSean Christopherson break; \ 113167730e6cSSean Christopherson r = vcpu_get_msr(vcpu, msr); \ 113267730e6cSSean Christopherson TEST_ASSERT_MSR(r == v, "Set %s to '0x%lx', got back '0x%lx'", msr, #msr, v, r);\ 113367730e6cSSean Christopherson } while (0) 113467730e6cSSean Christopherson 113567730e6cSSean Christopherson void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits); 113667730e6cSSean Christopherson void kvm_init_vm_address_properties(struct kvm_vm *vm); 113767730e6cSSean Christopherson bool vm_is_unrestricted_guest(struct kvm_vm *vm); 113867730e6cSSean Christopherson 113967730e6cSSean Christopherson struct ex_regs { 114067730e6cSSean Christopherson uint64_t rax, rcx, rdx, rbx; 114167730e6cSSean Christopherson uint64_t rbp, rsi, rdi; 114267730e6cSSean Christopherson uint64_t r8, r9, r10, r11; 114367730e6cSSean Christopherson uint64_t r12, r13, r14, r15; 114467730e6cSSean Christopherson uint64_t vector; 114567730e6cSSean Christopherson uint64_t error_code; 114667730e6cSSean Christopherson uint64_t rip; 114767730e6cSSean Christopherson uint64_t cs; 114867730e6cSSean Christopherson uint64_t rflags; 114967730e6cSSean Christopherson }; 115067730e6cSSean Christopherson 115167730e6cSSean Christopherson struct idt_entry { 115267730e6cSSean Christopherson uint16_t offset0; 115367730e6cSSean Christopherson uint16_t selector; 115467730e6cSSean Christopherson uint16_t ist : 3; 115567730e6cSSean Christopherson uint16_t : 5; 115667730e6cSSean Christopherson uint16_t type : 4; 115767730e6cSSean Christopherson uint16_t : 1; 115867730e6cSSean Christopherson uint16_t dpl : 2; 115967730e6cSSean Christopherson uint16_t p : 1; 116067730e6cSSean Christopherson uint16_t offset1; 116167730e6cSSean Christopherson uint32_t offset2; uint32_t reserved; 116267730e6cSSean Christopherson }; 116367730e6cSSean Christopherson 116467730e6cSSean Christopherson void vm_install_exception_handler(struct kvm_vm *vm, int vector, 116567730e6cSSean Christopherson void (*handler)(struct ex_regs *)); 116667730e6cSSean Christopherson 116767730e6cSSean Christopherson /* If a toddler were to say "abracadabra". */ 116867730e6cSSean Christopherson #define KVM_EXCEPTION_MAGIC 0xabacadabaULL 116967730e6cSSean Christopherson 117067730e6cSSean Christopherson /* 117167730e6cSSean Christopherson * KVM selftest exception fixup uses registers to coordinate with the exception 117267730e6cSSean Christopherson * handler, versus the kernel's in-memory tables and KVM-Unit-Tests's in-memory 117367730e6cSSean Christopherson * per-CPU data. Using only registers avoids having to map memory into the 117467730e6cSSean Christopherson * guest, doesn't require a valid, stable GS.base, and reduces the risk of 117567730e6cSSean Christopherson * for recursive faults when accessing memory in the handler. The downside to 117667730e6cSSean Christopherson * using registers is that it restricts what registers can be used by the actual 117767730e6cSSean Christopherson * instruction. But, selftests are 64-bit only, making register* pressure a 117867730e6cSSean Christopherson * minor concern. Use r9-r11 as they are volatile, i.e. don't need to be saved 117967730e6cSSean Christopherson * by the callee, and except for r11 are not implicit parameters to any 118067730e6cSSean Christopherson * instructions. Ideally, fixup would use r8-r10 and thus avoid implicit 118167730e6cSSean Christopherson * parameters entirely, but Hyper-V's hypercall ABI uses r8 and testing Hyper-V 118267730e6cSSean Christopherson * is higher priority than testing non-faulting SYSCALL/SYSRET. 118367730e6cSSean Christopherson * 118467730e6cSSean Christopherson * Note, the fixup handler deliberately does not handle #DE, i.e. the vector 118567730e6cSSean Christopherson * is guaranteed to be non-zero on fault. 118667730e6cSSean Christopherson * 118767730e6cSSean Christopherson * REGISTER INPUTS: 118867730e6cSSean Christopherson * r9 = MAGIC 118967730e6cSSean Christopherson * r10 = RIP 119067730e6cSSean Christopherson * r11 = new RIP on fault 119167730e6cSSean Christopherson * 119267730e6cSSean Christopherson * REGISTER OUTPUTS: 119367730e6cSSean Christopherson * r9 = exception vector (non-zero) 119467730e6cSSean Christopherson * r10 = error code 119567730e6cSSean Christopherson */ 119667730e6cSSean Christopherson #define __KVM_ASM_SAFE(insn, fep) \ 119767730e6cSSean Christopherson "mov $" __stringify(KVM_EXCEPTION_MAGIC) ", %%r9\n\t" \ 119867730e6cSSean Christopherson "lea 1f(%%rip), %%r10\n\t" \ 119967730e6cSSean Christopherson "lea 2f(%%rip), %%r11\n\t" \ 120067730e6cSSean Christopherson fep "1: " insn "\n\t" \ 120167730e6cSSean Christopherson "xor %%r9, %%r9\n\t" \ 120267730e6cSSean Christopherson "2:\n\t" \ 120367730e6cSSean Christopherson "mov %%r9b, %[vector]\n\t" \ 120467730e6cSSean Christopherson "mov %%r10, %[error_code]\n\t" 120567730e6cSSean Christopherson 120667730e6cSSean Christopherson #define KVM_ASM_SAFE(insn) __KVM_ASM_SAFE(insn, "") 120767730e6cSSean Christopherson #define KVM_ASM_SAFE_FEP(insn) __KVM_ASM_SAFE(insn, KVM_FEP) 120867730e6cSSean Christopherson 120967730e6cSSean Christopherson #define KVM_ASM_SAFE_OUTPUTS(v, ec) [vector] "=qm"(v), [error_code] "=rm"(ec) 121067730e6cSSean Christopherson #define KVM_ASM_SAFE_CLOBBERS "r9", "r10", "r11" 121167730e6cSSean Christopherson 121267730e6cSSean Christopherson #define kvm_asm_safe(insn, inputs...) \ 121367730e6cSSean Christopherson ({ \ 121467730e6cSSean Christopherson uint64_t ign_error_code; \ 121567730e6cSSean Christopherson uint8_t vector; \ 121667730e6cSSean Christopherson \ 121767730e6cSSean Christopherson asm volatile(KVM_ASM_SAFE(insn) \ 121867730e6cSSean Christopherson : KVM_ASM_SAFE_OUTPUTS(vector, ign_error_code) \ 121967730e6cSSean Christopherson : inputs \ 122067730e6cSSean Christopherson : KVM_ASM_SAFE_CLOBBERS); \ 122167730e6cSSean Christopherson vector; \ 122267730e6cSSean Christopherson }) 122367730e6cSSean Christopherson 122467730e6cSSean Christopherson #define kvm_asm_safe_ec(insn, error_code, inputs...) \ 122567730e6cSSean Christopherson ({ \ 122667730e6cSSean Christopherson uint8_t vector; \ 122767730e6cSSean Christopherson \ 122867730e6cSSean Christopherson asm volatile(KVM_ASM_SAFE(insn) \ 122967730e6cSSean Christopherson : KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ 123067730e6cSSean Christopherson : inputs \ 123167730e6cSSean Christopherson : KVM_ASM_SAFE_CLOBBERS); \ 123267730e6cSSean Christopherson vector; \ 123367730e6cSSean Christopherson }) 123467730e6cSSean Christopherson 123567730e6cSSean Christopherson #define kvm_asm_safe_fep(insn, inputs...) \ 123667730e6cSSean Christopherson ({ \ 123767730e6cSSean Christopherson uint64_t ign_error_code; \ 123867730e6cSSean Christopherson uint8_t vector; \ 123967730e6cSSean Christopherson \ 124067730e6cSSean Christopherson asm volatile(KVM_ASM_SAFE(insn) \ 124167730e6cSSean Christopherson : KVM_ASM_SAFE_OUTPUTS(vector, ign_error_code) \ 124267730e6cSSean Christopherson : inputs \ 124367730e6cSSean Christopherson : KVM_ASM_SAFE_CLOBBERS); \ 124467730e6cSSean Christopherson vector; \ 124567730e6cSSean Christopherson }) 124667730e6cSSean Christopherson 124767730e6cSSean Christopherson #define kvm_asm_safe_ec_fep(insn, error_code, inputs...) \ 124867730e6cSSean Christopherson ({ \ 124967730e6cSSean Christopherson uint8_t vector; \ 125067730e6cSSean Christopherson \ 125167730e6cSSean Christopherson asm volatile(KVM_ASM_SAFE_FEP(insn) \ 125267730e6cSSean Christopherson : KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ 125367730e6cSSean Christopherson : inputs \ 125467730e6cSSean Christopherson : KVM_ASM_SAFE_CLOBBERS); \ 125567730e6cSSean Christopherson vector; \ 125667730e6cSSean Christopherson }) 125767730e6cSSean Christopherson 125867730e6cSSean Christopherson #define BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \ 125967730e6cSSean Christopherson static inline uint8_t insn##_safe ##_fep(uint32_t idx, uint64_t *val) \ 126067730e6cSSean Christopherson { \ 126167730e6cSSean Christopherson uint64_t error_code; \ 126267730e6cSSean Christopherson uint8_t vector; \ 126367730e6cSSean Christopherson uint32_t a, d; \ 126467730e6cSSean Christopherson \ 126567730e6cSSean Christopherson asm volatile(KVM_ASM_SAFE##_FEP(#insn) \ 126667730e6cSSean Christopherson : "=a"(a), "=d"(d), \ 126767730e6cSSean Christopherson KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ 126867730e6cSSean Christopherson : "c"(idx) \ 126967730e6cSSean Christopherson : KVM_ASM_SAFE_CLOBBERS); \ 127067730e6cSSean Christopherson \ 127167730e6cSSean Christopherson *val = (uint64_t)a | ((uint64_t)d << 32); \ 127267730e6cSSean Christopherson return vector; \ 127367730e6cSSean Christopherson } 127467730e6cSSean Christopherson 127567730e6cSSean Christopherson /* 127667730e6cSSean Christopherson * Generate {insn}_safe() and {insn}_safe_fep() helpers for instructions that 127767730e6cSSean Christopherson * use ECX as in input index, and EDX:EAX as a 64-bit output. 127867730e6cSSean Christopherson */ 127967730e6cSSean Christopherson #define BUILD_READ_U64_SAFE_HELPERS(insn) \ 128067730e6cSSean Christopherson BUILD_READ_U64_SAFE_HELPER(insn, , ) \ 128167730e6cSSean Christopherson BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \ 128267730e6cSSean Christopherson 128367730e6cSSean Christopherson BUILD_READ_U64_SAFE_HELPERS(rdmsr) 128467730e6cSSean Christopherson BUILD_READ_U64_SAFE_HELPERS(rdpmc) 128567730e6cSSean Christopherson BUILD_READ_U64_SAFE_HELPERS(xgetbv) 128667730e6cSSean Christopherson 128767730e6cSSean Christopherson static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val) 128867730e6cSSean Christopherson { 128967730e6cSSean Christopherson return kvm_asm_safe("wrmsr", "a"(val & -1u), "d"(val >> 32), "c"(msr)); 129067730e6cSSean Christopherson } 129167730e6cSSean Christopherson 129267730e6cSSean Christopherson static inline uint8_t xsetbv_safe(uint32_t index, uint64_t value) 129367730e6cSSean Christopherson { 129467730e6cSSean Christopherson u32 eax = value; 129567730e6cSSean Christopherson u32 edx = value >> 32; 129667730e6cSSean Christopherson 129767730e6cSSean Christopherson return kvm_asm_safe("xsetbv", "a" (eax), "d" (edx), "c" (index)); 129867730e6cSSean Christopherson } 129967730e6cSSean Christopherson 130067730e6cSSean Christopherson bool kvm_is_tdp_enabled(void); 130167730e6cSSean Christopherson 130267730e6cSSean Christopherson static inline bool kvm_is_pmu_enabled(void) 130367730e6cSSean Christopherson { 130467730e6cSSean Christopherson return get_kvm_param_bool("enable_pmu"); 130567730e6cSSean Christopherson } 130667730e6cSSean Christopherson 130767730e6cSSean Christopherson static inline bool kvm_is_forced_emulation_enabled(void) 130867730e6cSSean Christopherson { 130967730e6cSSean Christopherson return !!get_kvm_param_integer("force_emulation_prefix"); 131067730e6cSSean Christopherson } 131167730e6cSSean Christopherson 131267730e6cSSean Christopherson uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr, 131367730e6cSSean Christopherson int *level); 131467730e6cSSean Christopherson uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr); 131567730e6cSSean Christopherson 131667730e6cSSean Christopherson uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, 131767730e6cSSean Christopherson uint64_t a3); 131867730e6cSSean Christopherson uint64_t __xen_hypercall(uint64_t nr, uint64_t a0, void *a1); 131967730e6cSSean Christopherson void xen_hypercall(uint64_t nr, uint64_t a0, void *a1); 132067730e6cSSean Christopherson 132167730e6cSSean Christopherson static inline uint64_t __kvm_hypercall_map_gpa_range(uint64_t gpa, 132267730e6cSSean Christopherson uint64_t size, uint64_t flags) 132367730e6cSSean Christopherson { 132467730e6cSSean Christopherson return kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> PAGE_SHIFT, flags, 0); 132567730e6cSSean Christopherson } 132667730e6cSSean Christopherson 132767730e6cSSean Christopherson static inline void kvm_hypercall_map_gpa_range(uint64_t gpa, uint64_t size, 132867730e6cSSean Christopherson uint64_t flags) 132967730e6cSSean Christopherson { 133067730e6cSSean Christopherson uint64_t ret = __kvm_hypercall_map_gpa_range(gpa, size, flags); 133167730e6cSSean Christopherson 133267730e6cSSean Christopherson GUEST_ASSERT(!ret); 133367730e6cSSean Christopherson } 133467730e6cSSean Christopherson 133567730e6cSSean Christopherson void __vm_xsave_require_permission(uint64_t xfeature, const char *name); 133667730e6cSSean Christopherson 133767730e6cSSean Christopherson #define vm_xsave_require_permission(xfeature) \ 133867730e6cSSean Christopherson __vm_xsave_require_permission(xfeature, #xfeature) 133967730e6cSSean Christopherson 134067730e6cSSean Christopherson enum pg_level { 134167730e6cSSean Christopherson PG_LEVEL_NONE, 134267730e6cSSean Christopherson PG_LEVEL_4K, 134367730e6cSSean Christopherson PG_LEVEL_2M, 134467730e6cSSean Christopherson PG_LEVEL_1G, 134567730e6cSSean Christopherson PG_LEVEL_512G, 134667730e6cSSean Christopherson PG_LEVEL_NUM 134767730e6cSSean Christopherson }; 134867730e6cSSean Christopherson 134967730e6cSSean Christopherson #define PG_LEVEL_SHIFT(_level) ((_level - 1) * 9 + 12) 135067730e6cSSean Christopherson #define PG_LEVEL_SIZE(_level) (1ull << PG_LEVEL_SHIFT(_level)) 135167730e6cSSean Christopherson 135267730e6cSSean Christopherson #define PG_SIZE_4K PG_LEVEL_SIZE(PG_LEVEL_4K) 135367730e6cSSean Christopherson #define PG_SIZE_2M PG_LEVEL_SIZE(PG_LEVEL_2M) 135467730e6cSSean Christopherson #define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G) 135567730e6cSSean Christopherson 135667730e6cSSean Christopherson void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level); 135767730e6cSSean Christopherson void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, 135867730e6cSSean Christopherson uint64_t nr_bytes, int level); 135967730e6cSSean Christopherson 136067730e6cSSean Christopherson /* 136167730e6cSSean Christopherson * Basic CPU control in CR0 136267730e6cSSean Christopherson */ 136367730e6cSSean Christopherson #define X86_CR0_PE (1UL<<0) /* Protection Enable */ 136467730e6cSSean Christopherson #define X86_CR0_MP (1UL<<1) /* Monitor Coprocessor */ 136567730e6cSSean Christopherson #define X86_CR0_EM (1UL<<2) /* Emulation */ 136667730e6cSSean Christopherson #define X86_CR0_TS (1UL<<3) /* Task Switched */ 136767730e6cSSean Christopherson #define X86_CR0_ET (1UL<<4) /* Extension Type */ 136867730e6cSSean Christopherson #define X86_CR0_NE (1UL<<5) /* Numeric Error */ 136967730e6cSSean Christopherson #define X86_CR0_WP (1UL<<16) /* Write Protect */ 137067730e6cSSean Christopherson #define X86_CR0_AM (1UL<<18) /* Alignment Mask */ 137167730e6cSSean Christopherson #define X86_CR0_NW (1UL<<29) /* Not Write-through */ 137267730e6cSSean Christopherson #define X86_CR0_CD (1UL<<30) /* Cache Disable */ 137367730e6cSSean Christopherson #define X86_CR0_PG (1UL<<31) /* Paging */ 137467730e6cSSean Christopherson 137567730e6cSSean Christopherson #define PFERR_PRESENT_BIT 0 137667730e6cSSean Christopherson #define PFERR_WRITE_BIT 1 137767730e6cSSean Christopherson #define PFERR_USER_BIT 2 137867730e6cSSean Christopherson #define PFERR_RSVD_BIT 3 137967730e6cSSean Christopherson #define PFERR_FETCH_BIT 4 138067730e6cSSean Christopherson #define PFERR_PK_BIT 5 138167730e6cSSean Christopherson #define PFERR_SGX_BIT 15 138267730e6cSSean Christopherson #define PFERR_GUEST_FINAL_BIT 32 138367730e6cSSean Christopherson #define PFERR_GUEST_PAGE_BIT 33 138467730e6cSSean Christopherson #define PFERR_IMPLICIT_ACCESS_BIT 48 138567730e6cSSean Christopherson 138667730e6cSSean Christopherson #define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT) 138767730e6cSSean Christopherson #define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT) 138867730e6cSSean Christopherson #define PFERR_USER_MASK BIT(PFERR_USER_BIT) 138967730e6cSSean Christopherson #define PFERR_RSVD_MASK BIT(PFERR_RSVD_BIT) 139067730e6cSSean Christopherson #define PFERR_FETCH_MASK BIT(PFERR_FETCH_BIT) 139167730e6cSSean Christopherson #define PFERR_PK_MASK BIT(PFERR_PK_BIT) 139267730e6cSSean Christopherson #define PFERR_SGX_MASK BIT(PFERR_SGX_BIT) 139367730e6cSSean Christopherson #define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT) 139467730e6cSSean Christopherson #define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) 139567730e6cSSean Christopherson #define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) 139667730e6cSSean Christopherson 139767730e6cSSean Christopherson bool sys_clocksource_is_based_on_tsc(void); 139867730e6cSSean Christopherson 139967730e6cSSean Christopherson #endif /* SELFTEST_KVM_PROCESSOR_H */ 1400