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