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