xref: /linux/arch/x86/include/asm/svm.h (revision d08b48585309247d4d28051dd7a315eef5d1db26)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c2cedf7bSEduardo Habkost #ifndef __SVM_H
3c2cedf7bSEduardo Habkost #define __SVM_H
4c2cedf7bSEduardo Habkost 
5af170c50SDavid Howells #include <uapi/asm/svm.h>
69780d51dSBabu Moger #include <uapi/asm/kvm.h>
726bf264eSXiao Guangrong 
8c45ad722SBabu Moger /*
9c45ad722SBabu Moger  * 32-bit intercept words in the VMCB Control Area, starting
10c45ad722SBabu Moger  * at Byte offset 000h.
11c45ad722SBabu Moger  */
12c45ad722SBabu Moger 
13c45ad722SBabu Moger enum intercept_words {
1403bfeeb9SBabu Moger 	INTERCEPT_CR = 0,
1530abaa88SBabu Moger 	INTERCEPT_DR,
169780d51dSBabu Moger 	INTERCEPT_EXCEPTION,
17c62e2e94SBabu Moger 	INTERCEPT_WORD3,
18c62e2e94SBabu Moger 	INTERCEPT_WORD4,
194c44e8d6SBabu Moger 	INTERCEPT_WORD5,
20c45ad722SBabu Moger 	MAX_INTERCEPT,
21c45ad722SBabu Moger };
2226bf264eSXiao Guangrong 
23c2cedf7bSEduardo Habkost enum {
2403bfeeb9SBabu Moger 	/* Byte offset 000h (word 0) */
2503bfeeb9SBabu Moger 	INTERCEPT_CR0_READ = 0,
2603bfeeb9SBabu Moger 	INTERCEPT_CR3_READ = 3,
2703bfeeb9SBabu Moger 	INTERCEPT_CR4_READ = 4,
2803bfeeb9SBabu Moger 	INTERCEPT_CR8_READ = 8,
2903bfeeb9SBabu Moger 	INTERCEPT_CR0_WRITE = 16,
3003bfeeb9SBabu Moger 	INTERCEPT_CR3_WRITE = 16 + 3,
3103bfeeb9SBabu Moger 	INTERCEPT_CR4_WRITE = 16 + 4,
3203bfeeb9SBabu Moger 	INTERCEPT_CR8_WRITE = 16 + 8,
3330abaa88SBabu Moger 	/* Byte offset 004h (word 1) */
3430abaa88SBabu Moger 	INTERCEPT_DR0_READ = 32,
3530abaa88SBabu Moger 	INTERCEPT_DR1_READ,
3630abaa88SBabu Moger 	INTERCEPT_DR2_READ,
3730abaa88SBabu Moger 	INTERCEPT_DR3_READ,
3830abaa88SBabu Moger 	INTERCEPT_DR4_READ,
3930abaa88SBabu Moger 	INTERCEPT_DR5_READ,
4030abaa88SBabu Moger 	INTERCEPT_DR6_READ,
4130abaa88SBabu Moger 	INTERCEPT_DR7_READ,
4230abaa88SBabu Moger 	INTERCEPT_DR0_WRITE = 48,
4330abaa88SBabu Moger 	INTERCEPT_DR1_WRITE,
4430abaa88SBabu Moger 	INTERCEPT_DR2_WRITE,
4530abaa88SBabu Moger 	INTERCEPT_DR3_WRITE,
4630abaa88SBabu Moger 	INTERCEPT_DR4_WRITE,
4730abaa88SBabu Moger 	INTERCEPT_DR5_WRITE,
4830abaa88SBabu Moger 	INTERCEPT_DR6_WRITE,
4930abaa88SBabu Moger 	INTERCEPT_DR7_WRITE,
509780d51dSBabu Moger 	/* Byte offset 008h (word 2) */
519780d51dSBabu Moger 	INTERCEPT_EXCEPTION_OFFSET = 64,
52c62e2e94SBabu Moger 	/* Byte offset 00Ch (word 3) */
53c62e2e94SBabu Moger 	INTERCEPT_INTR = 96,
54c2cedf7bSEduardo Habkost 	INTERCEPT_NMI,
55c2cedf7bSEduardo Habkost 	INTERCEPT_SMI,
56c2cedf7bSEduardo Habkost 	INTERCEPT_INIT,
57c2cedf7bSEduardo Habkost 	INTERCEPT_VINTR,
58c2cedf7bSEduardo Habkost 	INTERCEPT_SELECTIVE_CR0,
59c2cedf7bSEduardo Habkost 	INTERCEPT_STORE_IDTR,
60c2cedf7bSEduardo Habkost 	INTERCEPT_STORE_GDTR,
61c2cedf7bSEduardo Habkost 	INTERCEPT_STORE_LDTR,
62c2cedf7bSEduardo Habkost 	INTERCEPT_STORE_TR,
63c2cedf7bSEduardo Habkost 	INTERCEPT_LOAD_IDTR,
64c2cedf7bSEduardo Habkost 	INTERCEPT_LOAD_GDTR,
65c2cedf7bSEduardo Habkost 	INTERCEPT_LOAD_LDTR,
66c2cedf7bSEduardo Habkost 	INTERCEPT_LOAD_TR,
67c2cedf7bSEduardo Habkost 	INTERCEPT_RDTSC,
68c2cedf7bSEduardo Habkost 	INTERCEPT_RDPMC,
69c2cedf7bSEduardo Habkost 	INTERCEPT_PUSHF,
70c2cedf7bSEduardo Habkost 	INTERCEPT_POPF,
71c2cedf7bSEduardo Habkost 	INTERCEPT_CPUID,
72c2cedf7bSEduardo Habkost 	INTERCEPT_RSM,
73c2cedf7bSEduardo Habkost 	INTERCEPT_IRET,
74c2cedf7bSEduardo Habkost 	INTERCEPT_INTn,
75c2cedf7bSEduardo Habkost 	INTERCEPT_INVD,
76c2cedf7bSEduardo Habkost 	INTERCEPT_PAUSE,
77c2cedf7bSEduardo Habkost 	INTERCEPT_HLT,
78c2cedf7bSEduardo Habkost 	INTERCEPT_INVLPG,
79c2cedf7bSEduardo Habkost 	INTERCEPT_INVLPGA,
80c2cedf7bSEduardo Habkost 	INTERCEPT_IOIO_PROT,
81c2cedf7bSEduardo Habkost 	INTERCEPT_MSR_PROT,
82c2cedf7bSEduardo Habkost 	INTERCEPT_TASK_SWITCH,
83c2cedf7bSEduardo Habkost 	INTERCEPT_FERR_FREEZE,
84c2cedf7bSEduardo Habkost 	INTERCEPT_SHUTDOWN,
85c62e2e94SBabu Moger 	/* Byte offset 010h (word 4) */
86c62e2e94SBabu Moger 	INTERCEPT_VMRUN = 128,
87c2cedf7bSEduardo Habkost 	INTERCEPT_VMMCALL,
88c2cedf7bSEduardo Habkost 	INTERCEPT_VMLOAD,
89c2cedf7bSEduardo Habkost 	INTERCEPT_VMSAVE,
90c2cedf7bSEduardo Habkost 	INTERCEPT_STGI,
91c2cedf7bSEduardo Habkost 	INTERCEPT_CLGI,
92c2cedf7bSEduardo Habkost 	INTERCEPT_SKINIT,
93c2cedf7bSEduardo Habkost 	INTERCEPT_RDTSCP,
94c2cedf7bSEduardo Habkost 	INTERCEPT_ICEBP,
95c2cedf7bSEduardo Habkost 	INTERCEPT_WBINVD,
96c2cedf7bSEduardo Habkost 	INTERCEPT_MONITOR,
97c2cedf7bSEduardo Habkost 	INTERCEPT_MWAIT,
98c2cedf7bSEduardo Habkost 	INTERCEPT_MWAIT_COND,
9981dd35d4SJoerg Roedel 	INTERCEPT_XSETBV,
1000cb8410bSJim Mattson 	INTERCEPT_RDPRU,
101376c6d28STom Lendacky 	TRAP_EFER_WRITE,
102376c6d28STom Lendacky 	TRAP_CR0_WRITE,
103376c6d28STom Lendacky 	TRAP_CR1_WRITE,
104376c6d28STom Lendacky 	TRAP_CR2_WRITE,
105376c6d28STom Lendacky 	TRAP_CR3_WRITE,
106376c6d28STom Lendacky 	TRAP_CR4_WRITE,
107376c6d28STom Lendacky 	TRAP_CR5_WRITE,
108376c6d28STom Lendacky 	TRAP_CR6_WRITE,
109376c6d28STom Lendacky 	TRAP_CR7_WRITE,
110376c6d28STom Lendacky 	TRAP_CR8_WRITE,
1114c44e8d6SBabu Moger 	/* Byte offset 014h (word 5) */
1124c44e8d6SBabu Moger 	INTERCEPT_INVLPGB = 160,
1134c44e8d6SBabu Moger 	INTERCEPT_INVLPGB_ILLEGAL,
1144c44e8d6SBabu Moger 	INTERCEPT_INVPCID,
1154c44e8d6SBabu Moger 	INTERCEPT_MCOMMIT,
1164c44e8d6SBabu Moger 	INTERCEPT_TLBSYNC,
117c2cedf7bSEduardo Habkost };
118c2cedf7bSEduardo Habkost 
119c2cedf7bSEduardo Habkost 
120c2cedf7bSEduardo Habkost struct __attribute__ ((__packed__)) vmcb_control_area {
121c45ad722SBabu Moger 	u32 intercepts[MAX_INTERCEPT];
122c62e2e94SBabu Moger 	u32 reserved_1[15 - MAX_INTERCEPT];
1231d8fb44aSBabu Moger 	u16 pause_filter_thresh;
124565d0998SMark Langsdorf 	u16 pause_filter_count;
125c2cedf7bSEduardo Habkost 	u64 iopm_base_pa;
126c2cedf7bSEduardo Habkost 	u64 msrpm_base_pa;
127c2cedf7bSEduardo Habkost 	u64 tsc_offset;
128c2cedf7bSEduardo Habkost 	u32 asid;
129c2cedf7bSEduardo Habkost 	u8 tlb_ctl;
130c2cedf7bSEduardo Habkost 	u8 reserved_2[3];
131c2cedf7bSEduardo Habkost 	u32 int_ctl;
132c2cedf7bSEduardo Habkost 	u32 int_vector;
133c2cedf7bSEduardo Habkost 	u32 int_state;
134c2cedf7bSEduardo Habkost 	u8 reserved_3[4];
135c2cedf7bSEduardo Habkost 	u32 exit_code;
136c2cedf7bSEduardo Habkost 	u32 exit_code_hi;
137c2cedf7bSEduardo Habkost 	u64 exit_info_1;
138c2cedf7bSEduardo Habkost 	u64 exit_info_2;
139c2cedf7bSEduardo Habkost 	u32 exit_int_info;
140c2cedf7bSEduardo Habkost 	u32 exit_int_info_err;
141c2cedf7bSEduardo Habkost 	u64 nested_ctl;
1423d5615e5SSuravee Suthikulpanit 	u64 avic_vapic_bar;
143291bd20dSTom Lendacky 	u64 ghcb_gpa;
144c2cedf7bSEduardo Habkost 	u32 event_inj;
145c2cedf7bSEduardo Habkost 	u32 event_inj_err;
146c2cedf7bSEduardo Habkost 	u64 nested_cr3;
1470dc92119SJanakarajan Natarajan 	u64 virt_ext;
1488d28fec4SRoedel, Joerg 	u32 clean;
1498d28fec4SRoedel, Joerg 	u32 reserved_5;
1506bc31bdcSAndre Przywara 	u64 next_rip;
151dc25e89eSAndre Przywara 	u8 insn_len;
152dc25e89eSAndre Przywara 	u8 insn_bytes[15];
1533d5615e5SSuravee Suthikulpanit 	u64 avic_backing_page;	/* Offset 0xe0 */
1543d5615e5SSuravee Suthikulpanit 	u8 reserved_6[8];	/* Offset 0xe8 */
1553d5615e5SSuravee Suthikulpanit 	u64 avic_logical_id;	/* Offset 0xf0 */
1563d5615e5SSuravee Suthikulpanit 	u64 avic_physical_id;	/* Offset 0xf8 */
157376c6d28STom Lendacky 	u8 reserved_7[8];
158376c6d28STom Lendacky 	u64 vmsa_pa;		/* Used for an SEV-ES guest */
15959d21d67SVineeth Pillai 	u8 reserved_8[720];
16059d21d67SVineeth Pillai 	/*
16159d21d67SVineeth Pillai 	 * Offset 0x3e0, 32 bytes reserved
16259d21d67SVineeth Pillai 	 * for use by hypervisor/software.
16359d21d67SVineeth Pillai 	 */
16459d21d67SVineeth Pillai 	u8 reserved_sw[32];
165c2cedf7bSEduardo Habkost };
166c2cedf7bSEduardo Habkost 
167c2cedf7bSEduardo Habkost 
168c2cedf7bSEduardo Habkost #define TLB_CONTROL_DO_NOTHING 0
169c2cedf7bSEduardo Habkost #define TLB_CONTROL_FLUSH_ALL_ASID 1
17038e5e92fSJoerg Roedel #define TLB_CONTROL_FLUSH_ASID 3
17138e5e92fSJoerg Roedel #define TLB_CONTROL_FLUSH_ASID_LOCAL 7
172c2cedf7bSEduardo Habkost 
173c2cedf7bSEduardo Habkost #define V_TPR_MASK 0x0f
174c2cedf7bSEduardo Habkost 
175c2cedf7bSEduardo Habkost #define V_IRQ_SHIFT 8
176c2cedf7bSEduardo Habkost #define V_IRQ_MASK (1 << V_IRQ_SHIFT)
177c2cedf7bSEduardo Habkost 
178640bd6e5SJanakarajan Natarajan #define V_GIF_SHIFT 9
179640bd6e5SJanakarajan Natarajan #define V_GIF_MASK (1 << V_GIF_SHIFT)
180640bd6e5SJanakarajan Natarajan 
181c2cedf7bSEduardo Habkost #define V_INTR_PRIO_SHIFT 16
182c2cedf7bSEduardo Habkost #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
183c2cedf7bSEduardo Habkost 
184c2cedf7bSEduardo Habkost #define V_IGN_TPR_SHIFT 20
185c2cedf7bSEduardo Habkost #define V_IGN_TPR_MASK (1 << V_IGN_TPR_SHIFT)
186c2cedf7bSEduardo Habkost 
1870f923e07SMaxim Levitsky #define V_IRQ_INJECTION_BITS_MASK (V_IRQ_MASK | V_INTR_PRIO_MASK | V_IGN_TPR_MASK)
1880f923e07SMaxim Levitsky 
189c2cedf7bSEduardo Habkost #define V_INTR_MASKING_SHIFT 24
190c2cedf7bSEduardo Habkost #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
191c2cedf7bSEduardo Habkost 
192640bd6e5SJanakarajan Natarajan #define V_GIF_ENABLE_SHIFT 25
193640bd6e5SJanakarajan Natarajan #define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT)
194640bd6e5SJanakarajan Natarajan 
19544a95daeSSuravee Suthikulpanit #define AVIC_ENABLE_SHIFT 31
19644a95daeSSuravee Suthikulpanit #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
19744a95daeSSuravee Suthikulpanit 
1984bdec12aSSuravee Suthikulpanit #define X2APIC_MODE_SHIFT 30
1994bdec12aSSuravee Suthikulpanit #define X2APIC_MODE_MASK (1 << X2APIC_MODE_SHIFT)
2004bdec12aSSuravee Suthikulpanit 
2018a77e909SJanakarajan Natarajan #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
20289c8a498SJanakarajan Natarajan #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
2038a77e909SJanakarajan Natarajan 
204f1c6366eSTom Lendacky #define SVM_INTERRUPT_SHADOW_MASK	BIT_ULL(0)
205f1c6366eSTom Lendacky #define SVM_GUEST_INTERRUPT_MASK	BIT_ULL(1)
206c2cedf7bSEduardo Habkost 
207c2cedf7bSEduardo Habkost #define SVM_IOIO_STR_SHIFT 2
208c2cedf7bSEduardo Habkost #define SVM_IOIO_REP_SHIFT 3
209c2cedf7bSEduardo Habkost #define SVM_IOIO_SIZE_SHIFT 4
210c2cedf7bSEduardo Habkost #define SVM_IOIO_ASIZE_SHIFT 7
211c2cedf7bSEduardo Habkost 
212c2cedf7bSEduardo Habkost #define SVM_IOIO_TYPE_MASK 1
213c2cedf7bSEduardo Habkost #define SVM_IOIO_STR_MASK (1 << SVM_IOIO_STR_SHIFT)
214c2cedf7bSEduardo Habkost #define SVM_IOIO_REP_MASK (1 << SVM_IOIO_REP_SHIFT)
215c2cedf7bSEduardo Habkost #define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT)
216c2cedf7bSEduardo Habkost #define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT)
217c2cedf7bSEduardo Habkost 
2184a810181SJoerg Roedel #define SVM_VM_CR_VALID_MASK	0x001fULL
2194a810181SJoerg Roedel #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
2204a810181SJoerg Roedel #define SVM_VM_CR_SVM_DIS_MASK  0x0010ULL
2214a810181SJoerg Roedel 
222cea3a19bSTom Lendacky #define SVM_NESTED_CTL_NP_ENABLE	BIT(0)
223ba7c3398STom Lendacky #define SVM_NESTED_CTL_SEV_ENABLE	BIT(1)
224376c6d28STom Lendacky #define SVM_NESTED_CTL_SEV_ES_ENABLE	BIT(2)
225cea3a19bSTom Lendacky 
22639150352SMaxim Levitsky 
227bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_RSVD	0xffffff0000000000ULL
228bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_MIN	0x0000000000000001ULL
229bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_MAX	0x000000ffffffffffULL
230bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_DEFAULT	0x0100000000ULL
231bb2aa78eSMaxim Levitsky 
232bb2aa78eSMaxim Levitsky 
23339150352SMaxim Levitsky /* AVIC */
2340dacc3dfSMaxim Levitsky #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK	(0xFFULL)
23539150352SMaxim Levitsky #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT			31
23639150352SMaxim Levitsky #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK		(1 << 31)
23739150352SMaxim Levitsky 
2384a204f78SSuravee Suthikulpanit #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK	GENMASK_ULL(11, 0)
23939150352SMaxim Levitsky #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK	(0xFFFFFFFFFFULL << 12)
24039150352SMaxim Levitsky #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK		(1ULL << 62)
24139150352SMaxim Levitsky #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK		(1ULL << 63)
2420dacc3dfSMaxim Levitsky #define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK		(0xFFULL)
24339150352SMaxim Levitsky 
2440dacc3dfSMaxim Levitsky #define AVIC_DOORBELL_PHYSICAL_ID_MASK			GENMASK_ULL(11, 0)
2450dacc3dfSMaxim Levitsky 
2460dacc3dfSMaxim Levitsky #define VMCB_AVIC_APIC_BAR_MASK				0xFFFFFFFFFF000ULL
24739150352SMaxim Levitsky 
24839150352SMaxim Levitsky #define AVIC_UNACCEL_ACCESS_WRITE_MASK		1
24939150352SMaxim Levitsky #define AVIC_UNACCEL_ACCESS_OFFSET_MASK		0xFF0
25039150352SMaxim Levitsky #define AVIC_UNACCEL_ACCESS_VECTOR_MASK		0xFFFFFFFF
25139150352SMaxim Levitsky 
25239150352SMaxim Levitsky enum avic_ipi_failure_cause {
25339150352SMaxim Levitsky 	AVIC_IPI_FAILURE_INVALID_INT_TYPE,
25439150352SMaxim Levitsky 	AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
25539150352SMaxim Levitsky 	AVIC_IPI_FAILURE_INVALID_TARGET,
25639150352SMaxim Levitsky 	AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
25739150352SMaxim Levitsky };
25839150352SMaxim Levitsky 
2594d1d7942SSuravee Suthikulpanit #define AVIC_PHYSICAL_MAX_INDEX_MASK	GENMASK_ULL(9, 0)
26039150352SMaxim Levitsky 
26139150352SMaxim Levitsky /*
262d2fe6bf5SSuravee Suthikulpanit  * For AVIC, the max index allowed for physical APIC ID
263d2fe6bf5SSuravee Suthikulpanit  * table is 0xff (255).
26439150352SMaxim Levitsky  */
265d2fe6bf5SSuravee Suthikulpanit #define AVIC_MAX_PHYSICAL_ID		0XFEULL
266d2fe6bf5SSuravee Suthikulpanit 
267d2fe6bf5SSuravee Suthikulpanit /*
268d2fe6bf5SSuravee Suthikulpanit  * For x2AVIC, the max index allowed for physical APIC ID
269d2fe6bf5SSuravee Suthikulpanit  * table is 0x1ff (511).
270d2fe6bf5SSuravee Suthikulpanit  */
271d2fe6bf5SSuravee Suthikulpanit #define X2AVIC_MAX_PHYSICAL_ID		0x1FFUL
27239150352SMaxim Levitsky 
27339150352SMaxim Levitsky #define AVIC_HPA_MASK	~((0xFFFULL << 52) | 0xFFF)
27439150352SMaxim Levitsky #define VMCB_AVIC_APIC_BAR_MASK		0xFFFFFFFFFF000ULL
27539150352SMaxim Levitsky 
27639150352SMaxim Levitsky 
277976bc5e2SBorislav Petkov struct vmcb_seg {
278c2cedf7bSEduardo Habkost 	u16 selector;
279c2cedf7bSEduardo Habkost 	u16 attrib;
280c2cedf7bSEduardo Habkost 	u32 limit;
281c2cedf7bSEduardo Habkost 	u64 base;
282976bc5e2SBorislav Petkov } __packed;
283c2cedf7bSEduardo Habkost 
2843dd2775bSTom Lendacky /* Save area definition for legacy and SEV-MEM guests */
285976bc5e2SBorislav Petkov struct vmcb_save_area {
286c2cedf7bSEduardo Habkost 	struct vmcb_seg es;
287c2cedf7bSEduardo Habkost 	struct vmcb_seg cs;
288c2cedf7bSEduardo Habkost 	struct vmcb_seg ss;
289c2cedf7bSEduardo Habkost 	struct vmcb_seg ds;
290c2cedf7bSEduardo Habkost 	struct vmcb_seg fs;
291c2cedf7bSEduardo Habkost 	struct vmcb_seg gs;
292c2cedf7bSEduardo Habkost 	struct vmcb_seg gdtr;
293c2cedf7bSEduardo Habkost 	struct vmcb_seg ldtr;
294c2cedf7bSEduardo Habkost 	struct vmcb_seg idtr;
295c2cedf7bSEduardo Habkost 	struct vmcb_seg tr;
296*d08b4858SCarlos Bilbao 	/* Reserved fields are named following their struct offset */
297*d08b4858SCarlos Bilbao 	u8 reserved_0xa0[42];
298046f773bSBrijesh Singh 	u8 vmpl;
299c2cedf7bSEduardo Habkost 	u8 cpl;
300*d08b4858SCarlos Bilbao 	u8 reserved_0xcc[4];
301c2cedf7bSEduardo Habkost 	u64 efer;
302*d08b4858SCarlos Bilbao 	u8 reserved_0xd8[112];
3033dd2775bSTom Lendacky 	u64 cr4;
3043dd2775bSTom Lendacky 	u64 cr3;
3053dd2775bSTom Lendacky 	u64 cr0;
3063dd2775bSTom Lendacky 	u64 dr7;
3073dd2775bSTom Lendacky 	u64 dr6;
3083dd2775bSTom Lendacky 	u64 rflags;
3093dd2775bSTom Lendacky 	u64 rip;
310*d08b4858SCarlos Bilbao 	u8 reserved_0x180[88];
3113dd2775bSTom Lendacky 	u64 rsp;
3123dd2775bSTom Lendacky 	u64 s_cet;
3133dd2775bSTom Lendacky 	u64 ssp;
3143dd2775bSTom Lendacky 	u64 isst_addr;
3153dd2775bSTom Lendacky 	u64 rax;
3163dd2775bSTom Lendacky 	u64 star;
3173dd2775bSTom Lendacky 	u64 lstar;
3183dd2775bSTom Lendacky 	u64 cstar;
3193dd2775bSTom Lendacky 	u64 sfmask;
3203dd2775bSTom Lendacky 	u64 kernel_gs_base;
3213dd2775bSTom Lendacky 	u64 sysenter_cs;
3223dd2775bSTom Lendacky 	u64 sysenter_esp;
3233dd2775bSTom Lendacky 	u64 sysenter_eip;
3243dd2775bSTom Lendacky 	u64 cr2;
325*d08b4858SCarlos Bilbao 	u8 reserved_0x248[32];
3263dd2775bSTom Lendacky 	u64 g_pat;
3273dd2775bSTom Lendacky 	u64 dbgctl;
3283dd2775bSTom Lendacky 	u64 br_from;
3293dd2775bSTom Lendacky 	u64 br_to;
3303dd2775bSTom Lendacky 	u64 last_excp_from;
3313dd2775bSTom Lendacky 	u64 last_excp_to;
332*d08b4858SCarlos Bilbao 	u8 reserved_0x298[72];
3333dd2775bSTom Lendacky 	u32 spec_ctrl;		/* Guest version of SPEC_CTRL at 0x2E0 */
3343dd2775bSTom Lendacky } __packed;
3353dd2775bSTom Lendacky 
3363dd2775bSTom Lendacky /* Save area definition for SEV-ES and SEV-SNP guests */
3373dd2775bSTom Lendacky struct sev_es_save_area {
3383dd2775bSTom Lendacky 	struct vmcb_seg es;
3393dd2775bSTom Lendacky 	struct vmcb_seg cs;
3403dd2775bSTom Lendacky 	struct vmcb_seg ss;
3413dd2775bSTom Lendacky 	struct vmcb_seg ds;
3423dd2775bSTom Lendacky 	struct vmcb_seg fs;
3433dd2775bSTom Lendacky 	struct vmcb_seg gs;
3443dd2775bSTom Lendacky 	struct vmcb_seg gdtr;
3453dd2775bSTom Lendacky 	struct vmcb_seg ldtr;
3463dd2775bSTom Lendacky 	struct vmcb_seg idtr;
3473dd2775bSTom Lendacky 	struct vmcb_seg tr;
3486d3b3d34STom Lendacky 	u64 vmpl0_ssp;
3496d3b3d34STom Lendacky 	u64 vmpl1_ssp;
3506d3b3d34STom Lendacky 	u64 vmpl2_ssp;
3516d3b3d34STom Lendacky 	u64 vmpl3_ssp;
3526d3b3d34STom Lendacky 	u64 u_cet;
353*d08b4858SCarlos Bilbao 	u8 reserved_0xc8[2];
3546d3b3d34STom Lendacky 	u8 vmpl;
3553dd2775bSTom Lendacky 	u8 cpl;
356*d08b4858SCarlos Bilbao 	u8 reserved_0xcc[4];
3573dd2775bSTom Lendacky 	u64 efer;
358*d08b4858SCarlos Bilbao 	u8 reserved_0xd8[104];
3593dd2775bSTom Lendacky 	u64 xss;
360c2cedf7bSEduardo Habkost 	u64 cr4;
361c2cedf7bSEduardo Habkost 	u64 cr3;
362c2cedf7bSEduardo Habkost 	u64 cr0;
363c2cedf7bSEduardo Habkost 	u64 dr7;
364c2cedf7bSEduardo Habkost 	u64 dr6;
365c2cedf7bSEduardo Habkost 	u64 rflags;
366c2cedf7bSEduardo Habkost 	u64 rip;
3676d3b3d34STom Lendacky 	u64 dr0;
3686d3b3d34STom Lendacky 	u64 dr1;
3696d3b3d34STom Lendacky 	u64 dr2;
3706d3b3d34STom Lendacky 	u64 dr3;
3716d3b3d34STom Lendacky 	u64 dr0_addr_mask;
3726d3b3d34STom Lendacky 	u64 dr1_addr_mask;
3736d3b3d34STom Lendacky 	u64 dr2_addr_mask;
3746d3b3d34STom Lendacky 	u64 dr3_addr_mask;
375*d08b4858SCarlos Bilbao 	u8 reserved_0x1c0[24];
376c2cedf7bSEduardo Habkost 	u64 rsp;
3776d3b3d34STom Lendacky 	u64 s_cet;
3786d3b3d34STom Lendacky 	u64 ssp;
3796d3b3d34STom Lendacky 	u64 isst_addr;
380c2cedf7bSEduardo Habkost 	u64 rax;
381c2cedf7bSEduardo Habkost 	u64 star;
382c2cedf7bSEduardo Habkost 	u64 lstar;
383c2cedf7bSEduardo Habkost 	u64 cstar;
384c2cedf7bSEduardo Habkost 	u64 sfmask;
385c2cedf7bSEduardo Habkost 	u64 kernel_gs_base;
386c2cedf7bSEduardo Habkost 	u64 sysenter_cs;
387c2cedf7bSEduardo Habkost 	u64 sysenter_esp;
388c2cedf7bSEduardo Habkost 	u64 sysenter_eip;
389c2cedf7bSEduardo Habkost 	u64 cr2;
390*d08b4858SCarlos Bilbao 	u8 reserved_0x248[32];
391c2cedf7bSEduardo Habkost 	u64 g_pat;
392c2cedf7bSEduardo Habkost 	u64 dbgctl;
393c2cedf7bSEduardo Habkost 	u64 br_from;
394c2cedf7bSEduardo Habkost 	u64 br_to;
395c2cedf7bSEduardo Habkost 	u64 last_excp_from;
396c2cedf7bSEduardo Habkost 	u64 last_excp_to;
397*d08b4858SCarlos Bilbao 	u8 reserved_0x298[80];
39886137773STom Lendacky 	u32 pkru;
399*d08b4858SCarlos Bilbao 	u32 tsc_aux;
400*d08b4858SCarlos Bilbao 	u8 reserved_0x2f0[24];
401d07f46f9STom Lendacky 	u64 rcx;
402d07f46f9STom Lendacky 	u64 rdx;
403d07f46f9STom Lendacky 	u64 rbx;
404*d08b4858SCarlos Bilbao 	u64 reserved_0x320;	/* rsp already available at 0x01d8 */
405d07f46f9STom Lendacky 	u64 rbp;
406d07f46f9STom Lendacky 	u64 rsi;
407d07f46f9STom Lendacky 	u64 rdi;
408d07f46f9STom Lendacky 	u64 r8;
409d07f46f9STom Lendacky 	u64 r9;
410d07f46f9STom Lendacky 	u64 r10;
411d07f46f9STom Lendacky 	u64 r11;
412d07f46f9STom Lendacky 	u64 r12;
413d07f46f9STom Lendacky 	u64 r13;
414d07f46f9STom Lendacky 	u64 r14;
415d07f46f9STom Lendacky 	u64 r15;
416*d08b4858SCarlos Bilbao 	u8 reserved_0x380[16];
4176d3b3d34STom Lendacky 	u64 guest_exit_info_1;
4186d3b3d34STom Lendacky 	u64 guest_exit_info_2;
4196d3b3d34STom Lendacky 	u64 guest_exit_int_info;
4206d3b3d34STom Lendacky 	u64 guest_nrip;
421046f773bSBrijesh Singh 	u64 sev_features;
4226d3b3d34STom Lendacky 	u64 vintr_ctrl;
4236d3b3d34STom Lendacky 	u64 guest_exit_code;
4246d3b3d34STom Lendacky 	u64 virtual_tom;
4256d3b3d34STom Lendacky 	u64 tlb_id;
4266d3b3d34STom Lendacky 	u64 pcpu_id;
4276d3b3d34STom Lendacky 	u64 event_inj;
428d07f46f9STom Lendacky 	u64 xcr0;
429*d08b4858SCarlos Bilbao 	u8 reserved_0x3f0[16];
4306d3b3d34STom Lendacky 
4316d3b3d34STom Lendacky 	/* Floating point area */
4326d3b3d34STom Lendacky 	u64 x87_dp;
4336d3b3d34STom Lendacky 	u32 mxcsr;
4346d3b3d34STom Lendacky 	u16 x87_ftw;
4356d3b3d34STom Lendacky 	u16 x87_fsw;
4366d3b3d34STom Lendacky 	u16 x87_fcw;
4376d3b3d34STom Lendacky 	u16 x87_fop;
4386d3b3d34STom Lendacky 	u16 x87_ds;
4396d3b3d34STom Lendacky 	u16 x87_cs;
4406d3b3d34STom Lendacky 	u64 x87_rip;
4416d3b3d34STom Lendacky 	u8 fpreg_x87[80];
4426d3b3d34STom Lendacky 	u8 fpreg_xmm[256];
4436d3b3d34STom Lendacky 	u8 fpreg_ymm[256];
444976bc5e2SBorislav Petkov } __packed;
445c2cedf7bSEduardo Habkost 
446a4690359STom Lendacky struct ghcb_save_area {
447*d08b4858SCarlos Bilbao 	u8 reserved_0x0[203];
448a4690359STom Lendacky 	u8 cpl;
449*d08b4858SCarlos Bilbao 	u8 reserved_0xcc[116];
450a4690359STom Lendacky 	u64 xss;
451*d08b4858SCarlos Bilbao 	u8 reserved_0x148[24];
452a4690359STom Lendacky 	u64 dr7;
453*d08b4858SCarlos Bilbao 	u8 reserved_0x168[16];
454a4690359STom Lendacky 	u64 rip;
455*d08b4858SCarlos Bilbao 	u8 reserved_0x180[88];
456a4690359STom Lendacky 	u64 rsp;
457*d08b4858SCarlos Bilbao 	u8 reserved_0x1e0[24];
458a4690359STom Lendacky 	u64 rax;
459*d08b4858SCarlos Bilbao 	u8 reserved_0x200[264];
460a4690359STom Lendacky 	u64 rcx;
461a4690359STom Lendacky 	u64 rdx;
462a4690359STom Lendacky 	u64 rbx;
463*d08b4858SCarlos Bilbao 	u8 reserved_0x320[8];
464a4690359STom Lendacky 	u64 rbp;
465a4690359STom Lendacky 	u64 rsi;
466a4690359STom Lendacky 	u64 rdi;
467a4690359STom Lendacky 	u64 r8;
468a4690359STom Lendacky 	u64 r9;
469a4690359STom Lendacky 	u64 r10;
470a4690359STom Lendacky 	u64 r11;
471a4690359STom Lendacky 	u64 r12;
472a4690359STom Lendacky 	u64 r13;
473a4690359STom Lendacky 	u64 r14;
474a4690359STom Lendacky 	u64 r15;
475*d08b4858SCarlos Bilbao 	u8 reserved_0x380[16];
476a4690359STom Lendacky 	u64 sw_exit_code;
477a4690359STom Lendacky 	u64 sw_exit_info_1;
478a4690359STom Lendacky 	u64 sw_exit_info_2;
479a4690359STom Lendacky 	u64 sw_scratch;
480*d08b4858SCarlos Bilbao 	u8 reserved_0x3b0[56];
481a4690359STom Lendacky 	u64 xcr0;
482a4690359STom Lendacky 	u8 valid_bitmap[16];
483a4690359STom Lendacky 	u64 x87_state_gpa;
484a4690359STom Lendacky } __packed;
485a4690359STom Lendacky 
4863dd2775bSTom Lendacky #define GHCB_SHARED_BUF_SIZE	2032
487d07f46f9STom Lendacky 
4883dd2775bSTom Lendacky struct ghcb {
489a4690359STom Lendacky 	struct ghcb_save_area save;
490a4690359STom Lendacky 	u8 reserved_save[2048 - sizeof(struct ghcb_save_area)];
4913dd2775bSTom Lendacky 
4923dd2775bSTom Lendacky 	u8 shared_buffer[GHCB_SHARED_BUF_SIZE];
493d07f46f9STom Lendacky 
494*d08b4858SCarlos Bilbao 	u8 reserved_0xff0[10];
495d07f46f9STom Lendacky 	u16 protocol_version;	/* negotiated SEV-ES/GHCB protocol version */
496d07f46f9STom Lendacky 	u32 ghcb_usage;
497d07f46f9STom Lendacky } __packed;
498d07f46f9STom Lendacky 
499d07f46f9STom Lendacky 
5003dd2775bSTom Lendacky #define EXPECTED_VMCB_SAVE_AREA_SIZE		740
501a4690359STom Lendacky #define EXPECTED_GHCB_SAVE_AREA_SIZE		1032
5026d3b3d34STom Lendacky #define EXPECTED_SEV_ES_SAVE_AREA_SIZE		1648
50359d21d67SVineeth Pillai #define EXPECTED_VMCB_CONTROL_AREA_SIZE		1024
504d07f46f9STom Lendacky #define EXPECTED_GHCB_SIZE			PAGE_SIZE
5057923ef4fSPaolo Bonzini 
506*d08b4858SCarlos Bilbao #define BUILD_BUG_RESERVED_OFFSET(x, y) \
507*d08b4858SCarlos Bilbao 	ASSERT_STRUCT_OFFSET(struct x, reserved ## _ ## y, y)
508*d08b4858SCarlos Bilbao 
5097923ef4fSPaolo Bonzini static inline void __unused_size_checks(void)
5107923ef4fSPaolo Bonzini {
511d07f46f9STom Lendacky 	BUILD_BUG_ON(sizeof(struct vmcb_save_area)	!= EXPECTED_VMCB_SAVE_AREA_SIZE);
512a4690359STom Lendacky 	BUILD_BUG_ON(sizeof(struct ghcb_save_area)	!= EXPECTED_GHCB_SAVE_AREA_SIZE);
5133dd2775bSTom Lendacky 	BUILD_BUG_ON(sizeof(struct sev_es_save_area)	!= EXPECTED_SEV_ES_SAVE_AREA_SIZE);
514d07f46f9STom Lendacky 	BUILD_BUG_ON(sizeof(struct vmcb_control_area)	!= EXPECTED_VMCB_CONTROL_AREA_SIZE);
515d07f46f9STom Lendacky 	BUILD_BUG_ON(sizeof(struct ghcb)		!= EXPECTED_GHCB_SIZE);
516*d08b4858SCarlos Bilbao 
517*d08b4858SCarlos Bilbao 	/* Check offsets of reserved fields */
518*d08b4858SCarlos Bilbao 
519*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xa0);
520*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xcc);
521*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xd8);
522*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x180);
523*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x248);
524*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x298);
525*d08b4858SCarlos Bilbao 
526*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0xc8);
527*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0xcc);
528*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0xd8);
529*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x1c0);
530*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x248);
531*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x298);
532*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x2f0);
533*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320);
534*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380);
535*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0);
536*d08b4858SCarlos Bilbao 
537*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x0);
538*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0xcc);
539*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x148);
540*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x168);
541*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x180);
542*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x1e0);
543*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x200);
544*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x320);
545*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x380);
546*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x3b0);
547*d08b4858SCarlos Bilbao 
548*d08b4858SCarlos Bilbao 	BUILD_BUG_RESERVED_OFFSET(ghcb, 0xff0);
5497923ef4fSPaolo Bonzini }
5507923ef4fSPaolo Bonzini 
551976bc5e2SBorislav Petkov struct vmcb {
552c2cedf7bSEduardo Habkost 	struct vmcb_control_area control;
553c2cedf7bSEduardo Habkost 	struct vmcb_save_area save;
554976bc5e2SBorislav Petkov } __packed;
555c2cedf7bSEduardo Habkost 
556c2cedf7bSEduardo Habkost #define SVM_CPUID_FUNC 0x8000000a
557c2cedf7bSEduardo Habkost 
558c2cedf7bSEduardo Habkost #define SVM_VM_CR_SVM_DISABLE 4
559c2cedf7bSEduardo Habkost 
560c2cedf7bSEduardo Habkost #define SVM_SELECTOR_S_SHIFT 4
561c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DPL_SHIFT 5
562c2cedf7bSEduardo Habkost #define SVM_SELECTOR_P_SHIFT 7
563c2cedf7bSEduardo Habkost #define SVM_SELECTOR_AVL_SHIFT 8
564c2cedf7bSEduardo Habkost #define SVM_SELECTOR_L_SHIFT 9
565c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DB_SHIFT 10
566c2cedf7bSEduardo Habkost #define SVM_SELECTOR_G_SHIFT 11
567c2cedf7bSEduardo Habkost 
568c2cedf7bSEduardo Habkost #define SVM_SELECTOR_TYPE_MASK (0xf)
569c2cedf7bSEduardo Habkost #define SVM_SELECTOR_S_MASK (1 << SVM_SELECTOR_S_SHIFT)
570c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DPL_MASK (3 << SVM_SELECTOR_DPL_SHIFT)
571c2cedf7bSEduardo Habkost #define SVM_SELECTOR_P_MASK (1 << SVM_SELECTOR_P_SHIFT)
572c2cedf7bSEduardo Habkost #define SVM_SELECTOR_AVL_MASK (1 << SVM_SELECTOR_AVL_SHIFT)
573c2cedf7bSEduardo Habkost #define SVM_SELECTOR_L_MASK (1 << SVM_SELECTOR_L_SHIFT)
574c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DB_MASK (1 << SVM_SELECTOR_DB_SHIFT)
575c2cedf7bSEduardo Habkost #define SVM_SELECTOR_G_MASK (1 << SVM_SELECTOR_G_SHIFT)
576c2cedf7bSEduardo Habkost 
577c2cedf7bSEduardo Habkost #define SVM_SELECTOR_WRITE_MASK (1 << 1)
578c2cedf7bSEduardo Habkost #define SVM_SELECTOR_READ_MASK SVM_SELECTOR_WRITE_MASK
579c2cedf7bSEduardo Habkost #define SVM_SELECTOR_CODE_MASK (1 << 3)
580c2cedf7bSEduardo Habkost 
581c2cedf7bSEduardo Habkost #define SVM_EVTINJ_VEC_MASK 0xff
582c2cedf7bSEduardo Habkost 
583c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_SHIFT 8
584c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
585c2cedf7bSEduardo Habkost 
586c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
587c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
588c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
589c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
590c2cedf7bSEduardo Habkost 
591c2cedf7bSEduardo Habkost #define SVM_EVTINJ_VALID (1 << 31)
592c2cedf7bSEduardo Habkost #define SVM_EVTINJ_VALID_ERR (1 << 11)
593c2cedf7bSEduardo Habkost 
594c2cedf7bSEduardo Habkost #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
59564a7ec06SGleb Natapov #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
596c2cedf7bSEduardo Habkost 
597c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR
598c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
599c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_EXEPT SVM_EVTINJ_TYPE_EXEPT
600c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_SOFT SVM_EVTINJ_TYPE_SOFT
601c2cedf7bSEduardo Habkost 
602c2cedf7bSEduardo Habkost #define SVM_EXITINTINFO_VALID SVM_EVTINJ_VALID
603c2cedf7bSEduardo Habkost #define SVM_EXITINTINFO_VALID_ERR SVM_EVTINJ_VALID_ERR
604c2cedf7bSEduardo Habkost 
605c2cedf7bSEduardo Habkost #define SVM_EXITINFOSHIFT_TS_REASON_IRET 36
606c2cedf7bSEduardo Habkost #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
607e269fb21SJan Kiszka #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
608c2cedf7bSEduardo Habkost 
6097ff76d58SAndre Przywara #define SVM_EXITINFO_REG_MASK 0x0F
6107ff76d58SAndre Przywara 
611dc77270fSAvi Kivity #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
612c2cedf7bSEduardo Habkost 
6133702c2f4SJoerg Roedel /* GHCB Accessor functions */
6143702c2f4SJoerg Roedel 
6153702c2f4SJoerg Roedel #define GHCB_BITMAP_IDX(field)							\
616a4690359STom Lendacky 	(offsetof(struct ghcb_save_area, field) / sizeof(u64))
6173702c2f4SJoerg Roedel 
6183702c2f4SJoerg Roedel #define DEFINE_GHCB_ACCESSORS(field)						\
6196b91ec4aSBorislav Petkov 	static __always_inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb) \
6203702c2f4SJoerg Roedel 	{									\
6213702c2f4SJoerg Roedel 		return test_bit(GHCB_BITMAP_IDX(field),				\
6223702c2f4SJoerg Roedel 				(unsigned long *)&ghcb->save.valid_bitmap);	\
6233702c2f4SJoerg Roedel 	}									\
6243702c2f4SJoerg Roedel 										\
6256b91ec4aSBorislav Petkov 	static __always_inline u64 ghcb_get_##field(struct ghcb *ghcb)		\
6260f60bde1STom Lendacky 	{									\
6270f60bde1STom Lendacky 		return ghcb->save.field;					\
6280f60bde1STom Lendacky 	}									\
6290f60bde1STom Lendacky 										\
6306b91ec4aSBorislav Petkov 	static __always_inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb) \
6310f60bde1STom Lendacky 	{									\
6320f60bde1STom Lendacky 		return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0;	\
6330f60bde1STom Lendacky 	}									\
6340f60bde1STom Lendacky 										\
6356b91ec4aSBorislav Petkov 	static __always_inline void ghcb_set_##field(struct ghcb *ghcb, u64 value) \
6363702c2f4SJoerg Roedel 	{									\
6373702c2f4SJoerg Roedel 		__set_bit(GHCB_BITMAP_IDX(field),				\
6383702c2f4SJoerg Roedel 			  (unsigned long *)&ghcb->save.valid_bitmap);		\
6393702c2f4SJoerg Roedel 		ghcb->save.field = value;					\
6403702c2f4SJoerg Roedel 	}
6413702c2f4SJoerg Roedel 
6423702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(cpl)
6433702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rip)
6443702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rsp)
6453702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rax)
6463702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rcx)
6473702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rdx)
6483702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rbx)
6493702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rbp)
6503702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rsi)
6513702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rdi)
6523702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r8)
6533702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r9)
6543702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r10)
6553702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r11)
6563702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r12)
6573702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r13)
6583702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r14)
6593702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r15)
6603702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_exit_code)
6613702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_exit_info_1)
6623702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_exit_info_2)
6633702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_scratch)
6643702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(xcr0)
6653702c2f4SJoerg Roedel 
666c2cedf7bSEduardo Habkost #endif
667