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