xref: /openbmc/linux/arch/x86/include/asm/svm.h (revision 3a9a6f3d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SVM_H
3 #define __SVM_H
4 
5 #include <uapi/asm/svm.h>
6 #include <uapi/asm/kvm.h>
7 
8 /*
9  * 32-bit intercept words in the VMCB Control Area, starting
10  * at Byte offset 000h.
11  */
12 
13 enum intercept_words {
14 	INTERCEPT_CR = 0,
15 	INTERCEPT_DR,
16 	INTERCEPT_EXCEPTION,
17 	INTERCEPT_WORD3,
18 	INTERCEPT_WORD4,
19 	INTERCEPT_WORD5,
20 	MAX_INTERCEPT,
21 };
22 
23 enum {
24 	/* Byte offset 000h (word 0) */
25 	INTERCEPT_CR0_READ = 0,
26 	INTERCEPT_CR3_READ = 3,
27 	INTERCEPT_CR4_READ = 4,
28 	INTERCEPT_CR8_READ = 8,
29 	INTERCEPT_CR0_WRITE = 16,
30 	INTERCEPT_CR3_WRITE = 16 + 3,
31 	INTERCEPT_CR4_WRITE = 16 + 4,
32 	INTERCEPT_CR8_WRITE = 16 + 8,
33 	/* Byte offset 004h (word 1) */
34 	INTERCEPT_DR0_READ = 32,
35 	INTERCEPT_DR1_READ,
36 	INTERCEPT_DR2_READ,
37 	INTERCEPT_DR3_READ,
38 	INTERCEPT_DR4_READ,
39 	INTERCEPT_DR5_READ,
40 	INTERCEPT_DR6_READ,
41 	INTERCEPT_DR7_READ,
42 	INTERCEPT_DR0_WRITE = 48,
43 	INTERCEPT_DR1_WRITE,
44 	INTERCEPT_DR2_WRITE,
45 	INTERCEPT_DR3_WRITE,
46 	INTERCEPT_DR4_WRITE,
47 	INTERCEPT_DR5_WRITE,
48 	INTERCEPT_DR6_WRITE,
49 	INTERCEPT_DR7_WRITE,
50 	/* Byte offset 008h (word 2) */
51 	INTERCEPT_EXCEPTION_OFFSET = 64,
52 	/* Byte offset 00Ch (word 3) */
53 	INTERCEPT_INTR = 96,
54 	INTERCEPT_NMI,
55 	INTERCEPT_SMI,
56 	INTERCEPT_INIT,
57 	INTERCEPT_VINTR,
58 	INTERCEPT_SELECTIVE_CR0,
59 	INTERCEPT_STORE_IDTR,
60 	INTERCEPT_STORE_GDTR,
61 	INTERCEPT_STORE_LDTR,
62 	INTERCEPT_STORE_TR,
63 	INTERCEPT_LOAD_IDTR,
64 	INTERCEPT_LOAD_GDTR,
65 	INTERCEPT_LOAD_LDTR,
66 	INTERCEPT_LOAD_TR,
67 	INTERCEPT_RDTSC,
68 	INTERCEPT_RDPMC,
69 	INTERCEPT_PUSHF,
70 	INTERCEPT_POPF,
71 	INTERCEPT_CPUID,
72 	INTERCEPT_RSM,
73 	INTERCEPT_IRET,
74 	INTERCEPT_INTn,
75 	INTERCEPT_INVD,
76 	INTERCEPT_PAUSE,
77 	INTERCEPT_HLT,
78 	INTERCEPT_INVLPG,
79 	INTERCEPT_INVLPGA,
80 	INTERCEPT_IOIO_PROT,
81 	INTERCEPT_MSR_PROT,
82 	INTERCEPT_TASK_SWITCH,
83 	INTERCEPT_FERR_FREEZE,
84 	INTERCEPT_SHUTDOWN,
85 	/* Byte offset 010h (word 4) */
86 	INTERCEPT_VMRUN = 128,
87 	INTERCEPT_VMMCALL,
88 	INTERCEPT_VMLOAD,
89 	INTERCEPT_VMSAVE,
90 	INTERCEPT_STGI,
91 	INTERCEPT_CLGI,
92 	INTERCEPT_SKINIT,
93 	INTERCEPT_RDTSCP,
94 	INTERCEPT_ICEBP,
95 	INTERCEPT_WBINVD,
96 	INTERCEPT_MONITOR,
97 	INTERCEPT_MWAIT,
98 	INTERCEPT_MWAIT_COND,
99 	INTERCEPT_XSETBV,
100 	INTERCEPT_RDPRU,
101 	TRAP_EFER_WRITE,
102 	TRAP_CR0_WRITE,
103 	TRAP_CR1_WRITE,
104 	TRAP_CR2_WRITE,
105 	TRAP_CR3_WRITE,
106 	TRAP_CR4_WRITE,
107 	TRAP_CR5_WRITE,
108 	TRAP_CR6_WRITE,
109 	TRAP_CR7_WRITE,
110 	TRAP_CR8_WRITE,
111 	/* Byte offset 014h (word 5) */
112 	INTERCEPT_INVLPGB = 160,
113 	INTERCEPT_INVLPGB_ILLEGAL,
114 	INTERCEPT_INVPCID,
115 	INTERCEPT_MCOMMIT,
116 	INTERCEPT_TLBSYNC,
117 };
118 
119 
120 struct __attribute__ ((__packed__)) vmcb_control_area {
121 	u32 intercepts[MAX_INTERCEPT];
122 	u32 reserved_1[15 - MAX_INTERCEPT];
123 	u16 pause_filter_thresh;
124 	u16 pause_filter_count;
125 	u64 iopm_base_pa;
126 	u64 msrpm_base_pa;
127 	u64 tsc_offset;
128 	u32 asid;
129 	u8 tlb_ctl;
130 	u8 reserved_2[3];
131 	u32 int_ctl;
132 	u32 int_vector;
133 	u32 int_state;
134 	u8 reserved_3[4];
135 	u32 exit_code;
136 	u32 exit_code_hi;
137 	u64 exit_info_1;
138 	u64 exit_info_2;
139 	u32 exit_int_info;
140 	u32 exit_int_info_err;
141 	u64 nested_ctl;
142 	u64 avic_vapic_bar;
143 	u64 ghcb_gpa;
144 	u32 event_inj;
145 	u32 event_inj_err;
146 	u64 nested_cr3;
147 	u64 virt_ext;
148 	u32 clean;
149 	u32 reserved_5;
150 	u64 next_rip;
151 	u8 insn_len;
152 	u8 insn_bytes[15];
153 	u64 avic_backing_page;	/* Offset 0xe0 */
154 	u8 reserved_6[8];	/* Offset 0xe8 */
155 	u64 avic_logical_id;	/* Offset 0xf0 */
156 	u64 avic_physical_id;	/* Offset 0xf8 */
157 	u8 reserved_7[8];
158 	u64 vmsa_pa;		/* Used for an SEV-ES guest */
159 	u8 reserved_8[720];
160 	/*
161 	 * Offset 0x3e0, 32 bytes reserved
162 	 * for use by hypervisor/software.
163 	 */
164 	u8 reserved_sw[32];
165 };
166 
167 
168 #define TLB_CONTROL_DO_NOTHING 0
169 #define TLB_CONTROL_FLUSH_ALL_ASID 1
170 #define TLB_CONTROL_FLUSH_ASID 3
171 #define TLB_CONTROL_FLUSH_ASID_LOCAL 7
172 
173 #define V_TPR_MASK 0x0f
174 
175 #define V_IRQ_SHIFT 8
176 #define V_IRQ_MASK (1 << V_IRQ_SHIFT)
177 
178 #define V_GIF_SHIFT 9
179 #define V_GIF_MASK (1 << V_GIF_SHIFT)
180 
181 #define V_INTR_PRIO_SHIFT 16
182 #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
183 
184 #define V_IGN_TPR_SHIFT 20
185 #define V_IGN_TPR_MASK (1 << V_IGN_TPR_SHIFT)
186 
187 #define V_IRQ_INJECTION_BITS_MASK (V_IRQ_MASK | V_INTR_PRIO_MASK | V_IGN_TPR_MASK)
188 
189 #define V_INTR_MASKING_SHIFT 24
190 #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
191 
192 #define V_GIF_ENABLE_SHIFT 25
193 #define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT)
194 
195 #define AVIC_ENABLE_SHIFT 31
196 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
197 
198 #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
199 #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
200 
201 #define SVM_INTERRUPT_SHADOW_MASK	BIT_ULL(0)
202 #define SVM_GUEST_INTERRUPT_MASK	BIT_ULL(1)
203 
204 #define SVM_IOIO_STR_SHIFT 2
205 #define SVM_IOIO_REP_SHIFT 3
206 #define SVM_IOIO_SIZE_SHIFT 4
207 #define SVM_IOIO_ASIZE_SHIFT 7
208 
209 #define SVM_IOIO_TYPE_MASK 1
210 #define SVM_IOIO_STR_MASK (1 << SVM_IOIO_STR_SHIFT)
211 #define SVM_IOIO_REP_MASK (1 << SVM_IOIO_REP_SHIFT)
212 #define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT)
213 #define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT)
214 
215 #define SVM_VM_CR_VALID_MASK	0x001fULL
216 #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
217 #define SVM_VM_CR_SVM_DIS_MASK  0x0010ULL
218 
219 #define SVM_NESTED_CTL_NP_ENABLE	BIT(0)
220 #define SVM_NESTED_CTL_SEV_ENABLE	BIT(1)
221 #define SVM_NESTED_CTL_SEV_ES_ENABLE	BIT(2)
222 
223 
224 /* AVIC */
225 #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK	(0xFF)
226 #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT			31
227 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK		(1 << 31)
228 
229 #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK	(0xFFULL)
230 #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK	(0xFFFFFFFFFFULL << 12)
231 #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK		(1ULL << 62)
232 #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK		(1ULL << 63)
233 #define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK		(0xFF)
234 
235 #define AVIC_DOORBELL_PHYSICAL_ID_MASK			(0xFF)
236 
237 #define AVIC_UNACCEL_ACCESS_WRITE_MASK		1
238 #define AVIC_UNACCEL_ACCESS_OFFSET_MASK		0xFF0
239 #define AVIC_UNACCEL_ACCESS_VECTOR_MASK		0xFFFFFFFF
240 
241 enum avic_ipi_failure_cause {
242 	AVIC_IPI_FAILURE_INVALID_INT_TYPE,
243 	AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
244 	AVIC_IPI_FAILURE_INVALID_TARGET,
245 	AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
246 };
247 
248 
249 /*
250  * 0xff is broadcast, so the max index allowed for physical APIC ID
251  * table is 0xfe.  APIC IDs above 0xff are reserved.
252  */
253 #define AVIC_MAX_PHYSICAL_ID_COUNT	0xff
254 
255 #define AVIC_HPA_MASK	~((0xFFFULL << 52) | 0xFFF)
256 #define VMCB_AVIC_APIC_BAR_MASK		0xFFFFFFFFFF000ULL
257 
258 
259 struct vmcb_seg {
260 	u16 selector;
261 	u16 attrib;
262 	u32 limit;
263 	u64 base;
264 } __packed;
265 
266 struct vmcb_save_area {
267 	struct vmcb_seg es;
268 	struct vmcb_seg cs;
269 	struct vmcb_seg ss;
270 	struct vmcb_seg ds;
271 	struct vmcb_seg fs;
272 	struct vmcb_seg gs;
273 	struct vmcb_seg gdtr;
274 	struct vmcb_seg ldtr;
275 	struct vmcb_seg idtr;
276 	struct vmcb_seg tr;
277 	u8 reserved_1[43];
278 	u8 cpl;
279 	u8 reserved_2[4];
280 	u64 efer;
281 	u8 reserved_3[104];
282 	u64 xss;		/* Valid for SEV-ES only */
283 	u64 cr4;
284 	u64 cr3;
285 	u64 cr0;
286 	u64 dr7;
287 	u64 dr6;
288 	u64 rflags;
289 	u64 rip;
290 	u8 reserved_4[88];
291 	u64 rsp;
292 	u8 reserved_5[24];
293 	u64 rax;
294 	u64 star;
295 	u64 lstar;
296 	u64 cstar;
297 	u64 sfmask;
298 	u64 kernel_gs_base;
299 	u64 sysenter_cs;
300 	u64 sysenter_esp;
301 	u64 sysenter_eip;
302 	u64 cr2;
303 	u8 reserved_6[32];
304 	u64 g_pat;
305 	u64 dbgctl;
306 	u64 br_from;
307 	u64 br_to;
308 	u64 last_excp_from;
309 	u64 last_excp_to;
310 
311 	/*
312 	 * The following part of the save area is valid only for
313 	 * SEV-ES guests when referenced through the GHCB or for
314 	 * saving to the host save area.
315 	 */
316 	u8 reserved_7[72];
317 	u32 spec_ctrl;		/* Guest version of SPEC_CTRL at 0x2E0 */
318 	u8 reserved_7b[4];
319 	u32 pkru;
320 	u8 reserved_7a[20];
321 	u64 reserved_8;		/* rax already available at 0x01f8 */
322 	u64 rcx;
323 	u64 rdx;
324 	u64 rbx;
325 	u64 reserved_9;		/* rsp already available at 0x01d8 */
326 	u64 rbp;
327 	u64 rsi;
328 	u64 rdi;
329 	u64 r8;
330 	u64 r9;
331 	u64 r10;
332 	u64 r11;
333 	u64 r12;
334 	u64 r13;
335 	u64 r14;
336 	u64 r15;
337 	u8 reserved_10[16];
338 	u64 sw_exit_code;
339 	u64 sw_exit_info_1;
340 	u64 sw_exit_info_2;
341 	u64 sw_scratch;
342 	u8 reserved_11[56];
343 	u64 xcr0;
344 	u8 valid_bitmap[16];
345 	u64 x87_state_gpa;
346 } __packed;
347 
348 struct ghcb {
349 	struct vmcb_save_area save;
350 	u8 reserved_save[2048 - sizeof(struct vmcb_save_area)];
351 
352 	u8 shared_buffer[2032];
353 
354 	u8 reserved_1[10];
355 	u16 protocol_version;	/* negotiated SEV-ES/GHCB protocol version */
356 	u32 ghcb_usage;
357 } __packed;
358 
359 
360 #define EXPECTED_VMCB_SAVE_AREA_SIZE		1032
361 #define EXPECTED_VMCB_CONTROL_AREA_SIZE		1024
362 #define EXPECTED_GHCB_SIZE			PAGE_SIZE
363 
364 static inline void __unused_size_checks(void)
365 {
366 	BUILD_BUG_ON(sizeof(struct vmcb_save_area)	!= EXPECTED_VMCB_SAVE_AREA_SIZE);
367 	BUILD_BUG_ON(sizeof(struct vmcb_control_area)	!= EXPECTED_VMCB_CONTROL_AREA_SIZE);
368 	BUILD_BUG_ON(sizeof(struct ghcb)		!= EXPECTED_GHCB_SIZE);
369 }
370 
371 struct vmcb {
372 	struct vmcb_control_area control;
373 	struct vmcb_save_area save;
374 } __packed;
375 
376 #define SVM_CPUID_FUNC 0x8000000a
377 
378 #define SVM_VM_CR_SVM_DISABLE 4
379 
380 #define SVM_SELECTOR_S_SHIFT 4
381 #define SVM_SELECTOR_DPL_SHIFT 5
382 #define SVM_SELECTOR_P_SHIFT 7
383 #define SVM_SELECTOR_AVL_SHIFT 8
384 #define SVM_SELECTOR_L_SHIFT 9
385 #define SVM_SELECTOR_DB_SHIFT 10
386 #define SVM_SELECTOR_G_SHIFT 11
387 
388 #define SVM_SELECTOR_TYPE_MASK (0xf)
389 #define SVM_SELECTOR_S_MASK (1 << SVM_SELECTOR_S_SHIFT)
390 #define SVM_SELECTOR_DPL_MASK (3 << SVM_SELECTOR_DPL_SHIFT)
391 #define SVM_SELECTOR_P_MASK (1 << SVM_SELECTOR_P_SHIFT)
392 #define SVM_SELECTOR_AVL_MASK (1 << SVM_SELECTOR_AVL_SHIFT)
393 #define SVM_SELECTOR_L_MASK (1 << SVM_SELECTOR_L_SHIFT)
394 #define SVM_SELECTOR_DB_MASK (1 << SVM_SELECTOR_DB_SHIFT)
395 #define SVM_SELECTOR_G_MASK (1 << SVM_SELECTOR_G_SHIFT)
396 
397 #define SVM_SELECTOR_WRITE_MASK (1 << 1)
398 #define SVM_SELECTOR_READ_MASK SVM_SELECTOR_WRITE_MASK
399 #define SVM_SELECTOR_CODE_MASK (1 << 3)
400 
401 #define SVM_EVTINJ_VEC_MASK 0xff
402 
403 #define SVM_EVTINJ_TYPE_SHIFT 8
404 #define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
405 
406 #define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
407 #define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
408 #define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
409 #define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
410 
411 #define SVM_EVTINJ_VALID (1 << 31)
412 #define SVM_EVTINJ_VALID_ERR (1 << 11)
413 
414 #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
415 #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
416 
417 #define	SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR
418 #define	SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
419 #define	SVM_EXITINTINFO_TYPE_EXEPT SVM_EVTINJ_TYPE_EXEPT
420 #define	SVM_EXITINTINFO_TYPE_SOFT SVM_EVTINJ_TYPE_SOFT
421 
422 #define SVM_EXITINTINFO_VALID SVM_EVTINJ_VALID
423 #define SVM_EXITINTINFO_VALID_ERR SVM_EVTINJ_VALID_ERR
424 
425 #define SVM_EXITINFOSHIFT_TS_REASON_IRET 36
426 #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
427 #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
428 
429 #define SVM_EXITINFO_REG_MASK 0x0F
430 
431 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
432 
433 /* GHCB Accessor functions */
434 
435 #define GHCB_BITMAP_IDX(field)							\
436 	(offsetof(struct vmcb_save_area, field) / sizeof(u64))
437 
438 #define DEFINE_GHCB_ACCESSORS(field)						\
439 	static inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb)	\
440 	{									\
441 		return test_bit(GHCB_BITMAP_IDX(field),				\
442 				(unsigned long *)&ghcb->save.valid_bitmap);	\
443 	}									\
444 										\
445 	static inline u64 ghcb_get_##field(struct ghcb *ghcb)			\
446 	{									\
447 		return ghcb->save.field;					\
448 	}									\
449 										\
450 	static inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb)	\
451 	{									\
452 		return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0;	\
453 	}									\
454 										\
455 	static inline void ghcb_set_##field(struct ghcb *ghcb, u64 value)	\
456 	{									\
457 		__set_bit(GHCB_BITMAP_IDX(field),				\
458 			  (unsigned long *)&ghcb->save.valid_bitmap);		\
459 		ghcb->save.field = value;					\
460 	}
461 
462 DEFINE_GHCB_ACCESSORS(cpl)
463 DEFINE_GHCB_ACCESSORS(rip)
464 DEFINE_GHCB_ACCESSORS(rsp)
465 DEFINE_GHCB_ACCESSORS(rax)
466 DEFINE_GHCB_ACCESSORS(rcx)
467 DEFINE_GHCB_ACCESSORS(rdx)
468 DEFINE_GHCB_ACCESSORS(rbx)
469 DEFINE_GHCB_ACCESSORS(rbp)
470 DEFINE_GHCB_ACCESSORS(rsi)
471 DEFINE_GHCB_ACCESSORS(rdi)
472 DEFINE_GHCB_ACCESSORS(r8)
473 DEFINE_GHCB_ACCESSORS(r9)
474 DEFINE_GHCB_ACCESSORS(r10)
475 DEFINE_GHCB_ACCESSORS(r11)
476 DEFINE_GHCB_ACCESSORS(r12)
477 DEFINE_GHCB_ACCESSORS(r13)
478 DEFINE_GHCB_ACCESSORS(r14)
479 DEFINE_GHCB_ACCESSORS(r15)
480 DEFINE_GHCB_ACCESSORS(sw_exit_code)
481 DEFINE_GHCB_ACCESSORS(sw_exit_info_1)
482 DEFINE_GHCB_ACCESSORS(sw_exit_info_2)
483 DEFINE_GHCB_ACCESSORS(sw_scratch)
484 DEFINE_GHCB_ACCESSORS(xcr0)
485 
486 #endif
487