xref: /linux/arch/x86/include/asm/svm.h (revision a4690359eaec985a1351786da887df1ba92440a0)
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 
1988a77e909SJanakarajan Natarajan #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
19989c8a498SJanakarajan Natarajan #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
2008a77e909SJanakarajan Natarajan 
201f1c6366eSTom Lendacky #define SVM_INTERRUPT_SHADOW_MASK	BIT_ULL(0)
202f1c6366eSTom Lendacky #define SVM_GUEST_INTERRUPT_MASK	BIT_ULL(1)
203c2cedf7bSEduardo Habkost 
204c2cedf7bSEduardo Habkost #define SVM_IOIO_STR_SHIFT 2
205c2cedf7bSEduardo Habkost #define SVM_IOIO_REP_SHIFT 3
206c2cedf7bSEduardo Habkost #define SVM_IOIO_SIZE_SHIFT 4
207c2cedf7bSEduardo Habkost #define SVM_IOIO_ASIZE_SHIFT 7
208c2cedf7bSEduardo Habkost 
209c2cedf7bSEduardo Habkost #define SVM_IOIO_TYPE_MASK 1
210c2cedf7bSEduardo Habkost #define SVM_IOIO_STR_MASK (1 << SVM_IOIO_STR_SHIFT)
211c2cedf7bSEduardo Habkost #define SVM_IOIO_REP_MASK (1 << SVM_IOIO_REP_SHIFT)
212c2cedf7bSEduardo Habkost #define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT)
213c2cedf7bSEduardo Habkost #define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT)
214c2cedf7bSEduardo Habkost 
2154a810181SJoerg Roedel #define SVM_VM_CR_VALID_MASK	0x001fULL
2164a810181SJoerg Roedel #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
2174a810181SJoerg Roedel #define SVM_VM_CR_SVM_DIS_MASK  0x0010ULL
2184a810181SJoerg Roedel 
219cea3a19bSTom Lendacky #define SVM_NESTED_CTL_NP_ENABLE	BIT(0)
220ba7c3398STom Lendacky #define SVM_NESTED_CTL_SEV_ENABLE	BIT(1)
221376c6d28STom Lendacky #define SVM_NESTED_CTL_SEV_ES_ENABLE	BIT(2)
222cea3a19bSTom Lendacky 
22339150352SMaxim Levitsky 
224bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_RSVD	0xffffff0000000000ULL
225bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_MIN	0x0000000000000001ULL
226bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_MAX	0x000000ffffffffffULL
227bb2aa78eSMaxim Levitsky #define SVM_TSC_RATIO_DEFAULT	0x0100000000ULL
228bb2aa78eSMaxim Levitsky 
229bb2aa78eSMaxim Levitsky 
23039150352SMaxim Levitsky /* AVIC */
2310dacc3dfSMaxim Levitsky #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK	(0xFFULL)
23239150352SMaxim Levitsky #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT			31
23339150352SMaxim Levitsky #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK		(1 << 31)
23439150352SMaxim Levitsky 
2354a204f78SSuravee Suthikulpanit #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK	GENMASK_ULL(11, 0)
23639150352SMaxim Levitsky #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK	(0xFFFFFFFFFFULL << 12)
23739150352SMaxim Levitsky #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK		(1ULL << 62)
23839150352SMaxim Levitsky #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK		(1ULL << 63)
2390dacc3dfSMaxim Levitsky #define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK		(0xFFULL)
24039150352SMaxim Levitsky 
2410dacc3dfSMaxim Levitsky #define AVIC_DOORBELL_PHYSICAL_ID_MASK			GENMASK_ULL(11, 0)
2420dacc3dfSMaxim Levitsky 
2430dacc3dfSMaxim Levitsky #define VMCB_AVIC_APIC_BAR_MASK				0xFFFFFFFFFF000ULL
24439150352SMaxim Levitsky 
24539150352SMaxim Levitsky #define AVIC_UNACCEL_ACCESS_WRITE_MASK		1
24639150352SMaxim Levitsky #define AVIC_UNACCEL_ACCESS_OFFSET_MASK		0xFF0
24739150352SMaxim Levitsky #define AVIC_UNACCEL_ACCESS_VECTOR_MASK		0xFFFFFFFF
24839150352SMaxim Levitsky 
24939150352SMaxim Levitsky enum avic_ipi_failure_cause {
25039150352SMaxim Levitsky 	AVIC_IPI_FAILURE_INVALID_INT_TYPE,
25139150352SMaxim Levitsky 	AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
25239150352SMaxim Levitsky 	AVIC_IPI_FAILURE_INVALID_TARGET,
25339150352SMaxim Levitsky 	AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
25439150352SMaxim Levitsky };
25539150352SMaxim Levitsky 
25639150352SMaxim Levitsky 
25739150352SMaxim Levitsky /*
25839150352SMaxim Levitsky  * 0xff is broadcast, so the max index allowed for physical APIC ID
25939150352SMaxim Levitsky  * table is 0xfe.  APIC IDs above 0xff are reserved.
26039150352SMaxim Levitsky  */
26139150352SMaxim Levitsky #define AVIC_MAX_PHYSICAL_ID_COUNT	0xff
26239150352SMaxim Levitsky 
26339150352SMaxim Levitsky #define AVIC_HPA_MASK	~((0xFFFULL << 52) | 0xFFF)
26439150352SMaxim Levitsky #define VMCB_AVIC_APIC_BAR_MASK		0xFFFFFFFFFF000ULL
26539150352SMaxim Levitsky 
26639150352SMaxim Levitsky 
267976bc5e2SBorislav Petkov struct vmcb_seg {
268c2cedf7bSEduardo Habkost 	u16 selector;
269c2cedf7bSEduardo Habkost 	u16 attrib;
270c2cedf7bSEduardo Habkost 	u32 limit;
271c2cedf7bSEduardo Habkost 	u64 base;
272976bc5e2SBorislav Petkov } __packed;
273c2cedf7bSEduardo Habkost 
2743dd2775bSTom Lendacky /* Save area definition for legacy and SEV-MEM guests */
275976bc5e2SBorislav Petkov struct vmcb_save_area {
276c2cedf7bSEduardo Habkost 	struct vmcb_seg es;
277c2cedf7bSEduardo Habkost 	struct vmcb_seg cs;
278c2cedf7bSEduardo Habkost 	struct vmcb_seg ss;
279c2cedf7bSEduardo Habkost 	struct vmcb_seg ds;
280c2cedf7bSEduardo Habkost 	struct vmcb_seg fs;
281c2cedf7bSEduardo Habkost 	struct vmcb_seg gs;
282c2cedf7bSEduardo Habkost 	struct vmcb_seg gdtr;
283c2cedf7bSEduardo Habkost 	struct vmcb_seg ldtr;
284c2cedf7bSEduardo Habkost 	struct vmcb_seg idtr;
285c2cedf7bSEduardo Habkost 	struct vmcb_seg tr;
286046f773bSBrijesh Singh 	u8 reserved_1[42];
287046f773bSBrijesh Singh 	u8 vmpl;
288c2cedf7bSEduardo Habkost 	u8 cpl;
289c2cedf7bSEduardo Habkost 	u8 reserved_2[4];
290c2cedf7bSEduardo Habkost 	u64 efer;
2913dd2775bSTom Lendacky 	u8 reserved_3[112];
2923dd2775bSTom Lendacky 	u64 cr4;
2933dd2775bSTom Lendacky 	u64 cr3;
2943dd2775bSTom Lendacky 	u64 cr0;
2953dd2775bSTom Lendacky 	u64 dr7;
2963dd2775bSTom Lendacky 	u64 dr6;
2973dd2775bSTom Lendacky 	u64 rflags;
2983dd2775bSTom Lendacky 	u64 rip;
2993dd2775bSTom Lendacky 	u8 reserved_4[88];
3003dd2775bSTom Lendacky 	u64 rsp;
3013dd2775bSTom Lendacky 	u64 s_cet;
3023dd2775bSTom Lendacky 	u64 ssp;
3033dd2775bSTom Lendacky 	u64 isst_addr;
3043dd2775bSTom Lendacky 	u64 rax;
3053dd2775bSTom Lendacky 	u64 star;
3063dd2775bSTom Lendacky 	u64 lstar;
3073dd2775bSTom Lendacky 	u64 cstar;
3083dd2775bSTom Lendacky 	u64 sfmask;
3093dd2775bSTom Lendacky 	u64 kernel_gs_base;
3103dd2775bSTom Lendacky 	u64 sysenter_cs;
3113dd2775bSTom Lendacky 	u64 sysenter_esp;
3123dd2775bSTom Lendacky 	u64 sysenter_eip;
3133dd2775bSTom Lendacky 	u64 cr2;
3143dd2775bSTom Lendacky 	u8 reserved_5[32];
3153dd2775bSTom Lendacky 	u64 g_pat;
3163dd2775bSTom Lendacky 	u64 dbgctl;
3173dd2775bSTom Lendacky 	u64 br_from;
3183dd2775bSTom Lendacky 	u64 br_to;
3193dd2775bSTom Lendacky 	u64 last_excp_from;
3203dd2775bSTom Lendacky 	u64 last_excp_to;
3213dd2775bSTom Lendacky 	u8 reserved_6[72];
3223dd2775bSTom Lendacky 	u32 spec_ctrl;		/* Guest version of SPEC_CTRL at 0x2E0 */
3233dd2775bSTom Lendacky } __packed;
3243dd2775bSTom Lendacky 
3253dd2775bSTom Lendacky /* Save area definition for SEV-ES and SEV-SNP guests */
3263dd2775bSTom Lendacky struct sev_es_save_area {
3273dd2775bSTom Lendacky 	struct vmcb_seg es;
3283dd2775bSTom Lendacky 	struct vmcb_seg cs;
3293dd2775bSTom Lendacky 	struct vmcb_seg ss;
3303dd2775bSTom Lendacky 	struct vmcb_seg ds;
3313dd2775bSTom Lendacky 	struct vmcb_seg fs;
3323dd2775bSTom Lendacky 	struct vmcb_seg gs;
3333dd2775bSTom Lendacky 	struct vmcb_seg gdtr;
3343dd2775bSTom Lendacky 	struct vmcb_seg ldtr;
3353dd2775bSTom Lendacky 	struct vmcb_seg idtr;
3363dd2775bSTom Lendacky 	struct vmcb_seg tr;
3373dd2775bSTom Lendacky 	u8 reserved_1[43];
3383dd2775bSTom Lendacky 	u8 cpl;
3393dd2775bSTom Lendacky 	u8 reserved_2[4];
3403dd2775bSTom Lendacky 	u64 efer;
34186137773STom Lendacky 	u8 reserved_3[104];
3423dd2775bSTom Lendacky 	u64 xss;
343c2cedf7bSEduardo Habkost 	u64 cr4;
344c2cedf7bSEduardo Habkost 	u64 cr3;
345c2cedf7bSEduardo Habkost 	u64 cr0;
346c2cedf7bSEduardo Habkost 	u64 dr7;
347c2cedf7bSEduardo Habkost 	u64 dr6;
348c2cedf7bSEduardo Habkost 	u64 rflags;
349c2cedf7bSEduardo Habkost 	u64 rip;
350c2cedf7bSEduardo Habkost 	u8 reserved_4[88];
351c2cedf7bSEduardo Habkost 	u64 rsp;
352c2cedf7bSEduardo Habkost 	u8 reserved_5[24];
353c2cedf7bSEduardo Habkost 	u64 rax;
354c2cedf7bSEduardo Habkost 	u64 star;
355c2cedf7bSEduardo Habkost 	u64 lstar;
356c2cedf7bSEduardo Habkost 	u64 cstar;
357c2cedf7bSEduardo Habkost 	u64 sfmask;
358c2cedf7bSEduardo Habkost 	u64 kernel_gs_base;
359c2cedf7bSEduardo Habkost 	u64 sysenter_cs;
360c2cedf7bSEduardo Habkost 	u64 sysenter_esp;
361c2cedf7bSEduardo Habkost 	u64 sysenter_eip;
362c2cedf7bSEduardo Habkost 	u64 cr2;
363c2cedf7bSEduardo Habkost 	u8 reserved_6[32];
364c2cedf7bSEduardo Habkost 	u64 g_pat;
365c2cedf7bSEduardo Habkost 	u64 dbgctl;
366c2cedf7bSEduardo Habkost 	u64 br_from;
367c2cedf7bSEduardo Habkost 	u64 br_to;
368c2cedf7bSEduardo Habkost 	u64 last_excp_from;
369c2cedf7bSEduardo Habkost 	u64 last_excp_to;
3703dd2775bSTom Lendacky 	u8 reserved_7[80];
37186137773STom Lendacky 	u32 pkru;
3723dd2775bSTom Lendacky 	u8 reserved_9[20];
3733dd2775bSTom Lendacky 	u64 reserved_10;	/* rax already available at 0x01f8 */
374d07f46f9STom Lendacky 	u64 rcx;
375d07f46f9STom Lendacky 	u64 rdx;
376d07f46f9STom Lendacky 	u64 rbx;
3773dd2775bSTom Lendacky 	u64 reserved_11;	/* rsp already available at 0x01d8 */
378d07f46f9STom Lendacky 	u64 rbp;
379d07f46f9STom Lendacky 	u64 rsi;
380d07f46f9STom Lendacky 	u64 rdi;
381d07f46f9STom Lendacky 	u64 r8;
382d07f46f9STom Lendacky 	u64 r9;
383d07f46f9STom Lendacky 	u64 r10;
384d07f46f9STom Lendacky 	u64 r11;
385d07f46f9STom Lendacky 	u64 r12;
386d07f46f9STom Lendacky 	u64 r13;
387d07f46f9STom Lendacky 	u64 r14;
388d07f46f9STom Lendacky 	u64 r15;
3893dd2775bSTom Lendacky 	u8 reserved_12[16];
390d07f46f9STom Lendacky 	u64 sw_exit_code;
391d07f46f9STom Lendacky 	u64 sw_exit_info_1;
392d07f46f9STom Lendacky 	u64 sw_exit_info_2;
393d07f46f9STom Lendacky 	u64 sw_scratch;
394046f773bSBrijesh Singh 	u64 sev_features;
3953dd2775bSTom Lendacky 	u8 reserved_13[48];
396d07f46f9STom Lendacky 	u64 xcr0;
397d07f46f9STom Lendacky 	u8 valid_bitmap[16];
398d07f46f9STom Lendacky 	u64 x87_state_gpa;
399976bc5e2SBorislav Petkov } __packed;
400c2cedf7bSEduardo Habkost 
401*a4690359STom Lendacky struct ghcb_save_area {
402*a4690359STom Lendacky 	u8 reserved_1[203];
403*a4690359STom Lendacky 	u8 cpl;
404*a4690359STom Lendacky 	u8 reserved_2[116];
405*a4690359STom Lendacky 	u64 xss;
406*a4690359STom Lendacky 	u8 reserved_3[24];
407*a4690359STom Lendacky 	u64 dr7;
408*a4690359STom Lendacky 	u8 reserved_4[16];
409*a4690359STom Lendacky 	u64 rip;
410*a4690359STom Lendacky 	u8 reserved_5[88];
411*a4690359STom Lendacky 	u64 rsp;
412*a4690359STom Lendacky 	u8 reserved_6[24];
413*a4690359STom Lendacky 	u64 rax;
414*a4690359STom Lendacky 	u8 reserved_7[264];
415*a4690359STom Lendacky 	u64 rcx;
416*a4690359STom Lendacky 	u64 rdx;
417*a4690359STom Lendacky 	u64 rbx;
418*a4690359STom Lendacky 	u8 reserved_8[8];
419*a4690359STom Lendacky 	u64 rbp;
420*a4690359STom Lendacky 	u64 rsi;
421*a4690359STom Lendacky 	u64 rdi;
422*a4690359STom Lendacky 	u64 r8;
423*a4690359STom Lendacky 	u64 r9;
424*a4690359STom Lendacky 	u64 r10;
425*a4690359STom Lendacky 	u64 r11;
426*a4690359STom Lendacky 	u64 r12;
427*a4690359STom Lendacky 	u64 r13;
428*a4690359STom Lendacky 	u64 r14;
429*a4690359STom Lendacky 	u64 r15;
430*a4690359STom Lendacky 	u8 reserved_9[16];
431*a4690359STom Lendacky 	u64 sw_exit_code;
432*a4690359STom Lendacky 	u64 sw_exit_info_1;
433*a4690359STom Lendacky 	u64 sw_exit_info_2;
434*a4690359STom Lendacky 	u64 sw_scratch;
435*a4690359STom Lendacky 	u8 reserved_10[56];
436*a4690359STom Lendacky 	u64 xcr0;
437*a4690359STom Lendacky 	u8 valid_bitmap[16];
438*a4690359STom Lendacky 	u64 x87_state_gpa;
439*a4690359STom Lendacky } __packed;
440*a4690359STom Lendacky 
4413dd2775bSTom Lendacky #define GHCB_SHARED_BUF_SIZE	2032
442d07f46f9STom Lendacky 
4433dd2775bSTom Lendacky struct ghcb {
444*a4690359STom Lendacky 	struct ghcb_save_area save;
445*a4690359STom Lendacky 	u8 reserved_save[2048 - sizeof(struct ghcb_save_area)];
4463dd2775bSTom Lendacky 
4473dd2775bSTom Lendacky 	u8 shared_buffer[GHCB_SHARED_BUF_SIZE];
448d07f46f9STom Lendacky 
449d07f46f9STom Lendacky 	u8 reserved_1[10];
450d07f46f9STom Lendacky 	u16 protocol_version;	/* negotiated SEV-ES/GHCB protocol version */
451d07f46f9STom Lendacky 	u32 ghcb_usage;
452d07f46f9STom Lendacky } __packed;
453d07f46f9STom Lendacky 
454d07f46f9STom Lendacky 
4553dd2775bSTom Lendacky #define EXPECTED_VMCB_SAVE_AREA_SIZE		740
456*a4690359STom Lendacky #define EXPECTED_GHCB_SAVE_AREA_SIZE		1032
4573dd2775bSTom Lendacky #define EXPECTED_SEV_ES_SAVE_AREA_SIZE		1032
45859d21d67SVineeth Pillai #define EXPECTED_VMCB_CONTROL_AREA_SIZE		1024
459d07f46f9STom Lendacky #define EXPECTED_GHCB_SIZE			PAGE_SIZE
4607923ef4fSPaolo Bonzini 
4617923ef4fSPaolo Bonzini static inline void __unused_size_checks(void)
4627923ef4fSPaolo Bonzini {
463d07f46f9STom Lendacky 	BUILD_BUG_ON(sizeof(struct vmcb_save_area)	!= EXPECTED_VMCB_SAVE_AREA_SIZE);
464*a4690359STom Lendacky 	BUILD_BUG_ON(sizeof(struct ghcb_save_area)	!= EXPECTED_GHCB_SAVE_AREA_SIZE);
4653dd2775bSTom Lendacky 	BUILD_BUG_ON(sizeof(struct sev_es_save_area)	!= EXPECTED_SEV_ES_SAVE_AREA_SIZE);
466d07f46f9STom Lendacky 	BUILD_BUG_ON(sizeof(struct vmcb_control_area)	!= EXPECTED_VMCB_CONTROL_AREA_SIZE);
467d07f46f9STom Lendacky 	BUILD_BUG_ON(sizeof(struct ghcb)		!= EXPECTED_GHCB_SIZE);
4687923ef4fSPaolo Bonzini }
4697923ef4fSPaolo Bonzini 
470976bc5e2SBorislav Petkov struct vmcb {
471c2cedf7bSEduardo Habkost 	struct vmcb_control_area control;
472c2cedf7bSEduardo Habkost 	struct vmcb_save_area save;
473976bc5e2SBorislav Petkov } __packed;
474c2cedf7bSEduardo Habkost 
475c2cedf7bSEduardo Habkost #define SVM_CPUID_FUNC 0x8000000a
476c2cedf7bSEduardo Habkost 
477c2cedf7bSEduardo Habkost #define SVM_VM_CR_SVM_DISABLE 4
478c2cedf7bSEduardo Habkost 
479c2cedf7bSEduardo Habkost #define SVM_SELECTOR_S_SHIFT 4
480c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DPL_SHIFT 5
481c2cedf7bSEduardo Habkost #define SVM_SELECTOR_P_SHIFT 7
482c2cedf7bSEduardo Habkost #define SVM_SELECTOR_AVL_SHIFT 8
483c2cedf7bSEduardo Habkost #define SVM_SELECTOR_L_SHIFT 9
484c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DB_SHIFT 10
485c2cedf7bSEduardo Habkost #define SVM_SELECTOR_G_SHIFT 11
486c2cedf7bSEduardo Habkost 
487c2cedf7bSEduardo Habkost #define SVM_SELECTOR_TYPE_MASK (0xf)
488c2cedf7bSEduardo Habkost #define SVM_SELECTOR_S_MASK (1 << SVM_SELECTOR_S_SHIFT)
489c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DPL_MASK (3 << SVM_SELECTOR_DPL_SHIFT)
490c2cedf7bSEduardo Habkost #define SVM_SELECTOR_P_MASK (1 << SVM_SELECTOR_P_SHIFT)
491c2cedf7bSEduardo Habkost #define SVM_SELECTOR_AVL_MASK (1 << SVM_SELECTOR_AVL_SHIFT)
492c2cedf7bSEduardo Habkost #define SVM_SELECTOR_L_MASK (1 << SVM_SELECTOR_L_SHIFT)
493c2cedf7bSEduardo Habkost #define SVM_SELECTOR_DB_MASK (1 << SVM_SELECTOR_DB_SHIFT)
494c2cedf7bSEduardo Habkost #define SVM_SELECTOR_G_MASK (1 << SVM_SELECTOR_G_SHIFT)
495c2cedf7bSEduardo Habkost 
496c2cedf7bSEduardo Habkost #define SVM_SELECTOR_WRITE_MASK (1 << 1)
497c2cedf7bSEduardo Habkost #define SVM_SELECTOR_READ_MASK SVM_SELECTOR_WRITE_MASK
498c2cedf7bSEduardo Habkost #define SVM_SELECTOR_CODE_MASK (1 << 3)
499c2cedf7bSEduardo Habkost 
500c2cedf7bSEduardo Habkost #define SVM_EVTINJ_VEC_MASK 0xff
501c2cedf7bSEduardo Habkost 
502c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_SHIFT 8
503c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
504c2cedf7bSEduardo Habkost 
505c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
506c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
507c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
508c2cedf7bSEduardo Habkost #define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
509c2cedf7bSEduardo Habkost 
510c2cedf7bSEduardo Habkost #define SVM_EVTINJ_VALID (1 << 31)
511c2cedf7bSEduardo Habkost #define SVM_EVTINJ_VALID_ERR (1 << 11)
512c2cedf7bSEduardo Habkost 
513c2cedf7bSEduardo Habkost #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
51464a7ec06SGleb Natapov #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
515c2cedf7bSEduardo Habkost 
516c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR
517c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
518c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_EXEPT SVM_EVTINJ_TYPE_EXEPT
519c2cedf7bSEduardo Habkost #define	SVM_EXITINTINFO_TYPE_SOFT SVM_EVTINJ_TYPE_SOFT
520c2cedf7bSEduardo Habkost 
521c2cedf7bSEduardo Habkost #define SVM_EXITINTINFO_VALID SVM_EVTINJ_VALID
522c2cedf7bSEduardo Habkost #define SVM_EXITINTINFO_VALID_ERR SVM_EVTINJ_VALID_ERR
523c2cedf7bSEduardo Habkost 
524c2cedf7bSEduardo Habkost #define SVM_EXITINFOSHIFT_TS_REASON_IRET 36
525c2cedf7bSEduardo Habkost #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
526e269fb21SJan Kiszka #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
527c2cedf7bSEduardo Habkost 
5287ff76d58SAndre Przywara #define SVM_EXITINFO_REG_MASK 0x0F
5297ff76d58SAndre Przywara 
530dc77270fSAvi Kivity #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
531c2cedf7bSEduardo Habkost 
5323702c2f4SJoerg Roedel /* GHCB Accessor functions */
5333702c2f4SJoerg Roedel 
5343702c2f4SJoerg Roedel #define GHCB_BITMAP_IDX(field)							\
535*a4690359STom Lendacky 	(offsetof(struct ghcb_save_area, field) / sizeof(u64))
5363702c2f4SJoerg Roedel 
5373702c2f4SJoerg Roedel #define DEFINE_GHCB_ACCESSORS(field)						\
5383702c2f4SJoerg Roedel 	static inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb)	\
5393702c2f4SJoerg Roedel 	{									\
5403702c2f4SJoerg Roedel 		return test_bit(GHCB_BITMAP_IDX(field),				\
5413702c2f4SJoerg Roedel 				(unsigned long *)&ghcb->save.valid_bitmap);	\
5423702c2f4SJoerg Roedel 	}									\
5433702c2f4SJoerg Roedel 										\
5440f60bde1STom Lendacky 	static inline u64 ghcb_get_##field(struct ghcb *ghcb)			\
5450f60bde1STom Lendacky 	{									\
5460f60bde1STom Lendacky 		return ghcb->save.field;					\
5470f60bde1STom Lendacky 	}									\
5480f60bde1STom Lendacky 										\
5490f60bde1STom Lendacky 	static inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb)	\
5500f60bde1STom Lendacky 	{									\
5510f60bde1STom Lendacky 		return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0;	\
5520f60bde1STom Lendacky 	}									\
5530f60bde1STom Lendacky 										\
5543702c2f4SJoerg Roedel 	static inline void ghcb_set_##field(struct ghcb *ghcb, u64 value)	\
5553702c2f4SJoerg Roedel 	{									\
5563702c2f4SJoerg Roedel 		__set_bit(GHCB_BITMAP_IDX(field),				\
5573702c2f4SJoerg Roedel 			  (unsigned long *)&ghcb->save.valid_bitmap);		\
5583702c2f4SJoerg Roedel 		ghcb->save.field = value;					\
5593702c2f4SJoerg Roedel 	}
5603702c2f4SJoerg Roedel 
5613702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(cpl)
5623702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rip)
5633702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rsp)
5643702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rax)
5653702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rcx)
5663702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rdx)
5673702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rbx)
5683702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rbp)
5693702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rsi)
5703702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(rdi)
5713702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r8)
5723702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r9)
5733702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r10)
5743702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r11)
5753702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r12)
5763702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r13)
5773702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r14)
5783702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(r15)
5793702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_exit_code)
5803702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_exit_info_1)
5813702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_exit_info_2)
5823702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(sw_scratch)
5833702c2f4SJoerg Roedel DEFINE_GHCB_ACCESSORS(xcr0)
5843702c2f4SJoerg Roedel 
585c2cedf7bSEduardo Habkost #endif
586