xref: /openbmc/linux/arch/x86/include/asm/svm.h (revision f2a55d08)
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 #include <asm/hyperv-tlfs.h>
9 
10 /*
11  * 32-bit intercept words in the VMCB Control Area, starting
12  * at Byte offset 000h.
13  */
14 
15 enum intercept_words {
16 	INTERCEPT_CR = 0,
17 	INTERCEPT_DR,
18 	INTERCEPT_EXCEPTION,
19 	INTERCEPT_WORD3,
20 	INTERCEPT_WORD4,
21 	INTERCEPT_WORD5,
22 	MAX_INTERCEPT,
23 };
24 
25 enum {
26 	/* Byte offset 000h (word 0) */
27 	INTERCEPT_CR0_READ = 0,
28 	INTERCEPT_CR3_READ = 3,
29 	INTERCEPT_CR4_READ = 4,
30 	INTERCEPT_CR8_READ = 8,
31 	INTERCEPT_CR0_WRITE = 16,
32 	INTERCEPT_CR3_WRITE = 16 + 3,
33 	INTERCEPT_CR4_WRITE = 16 + 4,
34 	INTERCEPT_CR8_WRITE = 16 + 8,
35 	/* Byte offset 004h (word 1) */
36 	INTERCEPT_DR0_READ = 32,
37 	INTERCEPT_DR1_READ,
38 	INTERCEPT_DR2_READ,
39 	INTERCEPT_DR3_READ,
40 	INTERCEPT_DR4_READ,
41 	INTERCEPT_DR5_READ,
42 	INTERCEPT_DR6_READ,
43 	INTERCEPT_DR7_READ,
44 	INTERCEPT_DR0_WRITE = 48,
45 	INTERCEPT_DR1_WRITE,
46 	INTERCEPT_DR2_WRITE,
47 	INTERCEPT_DR3_WRITE,
48 	INTERCEPT_DR4_WRITE,
49 	INTERCEPT_DR5_WRITE,
50 	INTERCEPT_DR6_WRITE,
51 	INTERCEPT_DR7_WRITE,
52 	/* Byte offset 008h (word 2) */
53 	INTERCEPT_EXCEPTION_OFFSET = 64,
54 	/* Byte offset 00Ch (word 3) */
55 	INTERCEPT_INTR = 96,
56 	INTERCEPT_NMI,
57 	INTERCEPT_SMI,
58 	INTERCEPT_INIT,
59 	INTERCEPT_VINTR,
60 	INTERCEPT_SELECTIVE_CR0,
61 	INTERCEPT_STORE_IDTR,
62 	INTERCEPT_STORE_GDTR,
63 	INTERCEPT_STORE_LDTR,
64 	INTERCEPT_STORE_TR,
65 	INTERCEPT_LOAD_IDTR,
66 	INTERCEPT_LOAD_GDTR,
67 	INTERCEPT_LOAD_LDTR,
68 	INTERCEPT_LOAD_TR,
69 	INTERCEPT_RDTSC,
70 	INTERCEPT_RDPMC,
71 	INTERCEPT_PUSHF,
72 	INTERCEPT_POPF,
73 	INTERCEPT_CPUID,
74 	INTERCEPT_RSM,
75 	INTERCEPT_IRET,
76 	INTERCEPT_INTn,
77 	INTERCEPT_INVD,
78 	INTERCEPT_PAUSE,
79 	INTERCEPT_HLT,
80 	INTERCEPT_INVLPG,
81 	INTERCEPT_INVLPGA,
82 	INTERCEPT_IOIO_PROT,
83 	INTERCEPT_MSR_PROT,
84 	INTERCEPT_TASK_SWITCH,
85 	INTERCEPT_FERR_FREEZE,
86 	INTERCEPT_SHUTDOWN,
87 	/* Byte offset 010h (word 4) */
88 	INTERCEPT_VMRUN = 128,
89 	INTERCEPT_VMMCALL,
90 	INTERCEPT_VMLOAD,
91 	INTERCEPT_VMSAVE,
92 	INTERCEPT_STGI,
93 	INTERCEPT_CLGI,
94 	INTERCEPT_SKINIT,
95 	INTERCEPT_RDTSCP,
96 	INTERCEPT_ICEBP,
97 	INTERCEPT_WBINVD,
98 	INTERCEPT_MONITOR,
99 	INTERCEPT_MWAIT,
100 	INTERCEPT_MWAIT_COND,
101 	INTERCEPT_XSETBV,
102 	INTERCEPT_RDPRU,
103 	TRAP_EFER_WRITE,
104 	TRAP_CR0_WRITE,
105 	TRAP_CR1_WRITE,
106 	TRAP_CR2_WRITE,
107 	TRAP_CR3_WRITE,
108 	TRAP_CR4_WRITE,
109 	TRAP_CR5_WRITE,
110 	TRAP_CR6_WRITE,
111 	TRAP_CR7_WRITE,
112 	TRAP_CR8_WRITE,
113 	/* Byte offset 014h (word 5) */
114 	INTERCEPT_INVLPGB = 160,
115 	INTERCEPT_INVLPGB_ILLEGAL,
116 	INTERCEPT_INVPCID,
117 	INTERCEPT_MCOMMIT,
118 	INTERCEPT_TLBSYNC,
119 };
120 
121 
122 struct __attribute__ ((__packed__)) vmcb_control_area {
123 	u32 intercepts[MAX_INTERCEPT];
124 	u32 reserved_1[15 - MAX_INTERCEPT];
125 	u16 pause_filter_thresh;
126 	u16 pause_filter_count;
127 	u64 iopm_base_pa;
128 	u64 msrpm_base_pa;
129 	u64 tsc_offset;
130 	u32 asid;
131 	u8 tlb_ctl;
132 	u8 reserved_2[3];
133 	u32 int_ctl;
134 	u32 int_vector;
135 	u32 int_state;
136 	u8 reserved_3[4];
137 	u32 exit_code;
138 	u32 exit_code_hi;
139 	u64 exit_info_1;
140 	u64 exit_info_2;
141 	u32 exit_int_info;
142 	u32 exit_int_info_err;
143 	u64 nested_ctl;
144 	u64 avic_vapic_bar;
145 	u64 ghcb_gpa;
146 	u32 event_inj;
147 	u32 event_inj_err;
148 	u64 nested_cr3;
149 	u64 virt_ext;
150 	u32 clean;
151 	u32 reserved_5;
152 	u64 next_rip;
153 	u8 insn_len;
154 	u8 insn_bytes[15];
155 	u64 avic_backing_page;	/* Offset 0xe0 */
156 	u8 reserved_6[8];	/* Offset 0xe8 */
157 	u64 avic_logical_id;	/* Offset 0xf0 */
158 	u64 avic_physical_id;	/* Offset 0xf8 */
159 	u8 reserved_7[8];
160 	u64 vmsa_pa;		/* Used for an SEV-ES guest */
161 	u8 reserved_8[720];
162 	/*
163 	 * Offset 0x3e0, 32 bytes reserved
164 	 * for use by hypervisor/software.
165 	 */
166 	union {
167 		struct hv_vmcb_enlightenments hv_enlightenments;
168 		u8 reserved_sw[32];
169 	};
170 };
171 
172 
173 #define TLB_CONTROL_DO_NOTHING 0
174 #define TLB_CONTROL_FLUSH_ALL_ASID 1
175 #define TLB_CONTROL_FLUSH_ASID 3
176 #define TLB_CONTROL_FLUSH_ASID_LOCAL 7
177 
178 #define V_TPR_MASK 0x0f
179 
180 #define V_IRQ_SHIFT 8
181 #define V_IRQ_MASK (1 << V_IRQ_SHIFT)
182 
183 #define V_GIF_SHIFT 9
184 #define V_GIF_MASK (1 << V_GIF_SHIFT)
185 
186 #define V_NMI_PENDING_SHIFT 11
187 #define V_NMI_PENDING_MASK (1 << V_NMI_PENDING_SHIFT)
188 
189 #define V_NMI_BLOCKING_SHIFT 12
190 #define V_NMI_BLOCKING_MASK (1 << V_NMI_BLOCKING_SHIFT)
191 
192 #define V_INTR_PRIO_SHIFT 16
193 #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
194 
195 #define V_IGN_TPR_SHIFT 20
196 #define V_IGN_TPR_MASK (1 << V_IGN_TPR_SHIFT)
197 
198 #define V_IRQ_INJECTION_BITS_MASK (V_IRQ_MASK | V_INTR_PRIO_MASK | V_IGN_TPR_MASK)
199 
200 #define V_INTR_MASKING_SHIFT 24
201 #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
202 
203 #define V_GIF_ENABLE_SHIFT 25
204 #define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT)
205 
206 #define V_NMI_ENABLE_SHIFT 26
207 #define V_NMI_ENABLE_MASK (1 << V_NMI_ENABLE_SHIFT)
208 
209 #define AVIC_ENABLE_SHIFT 31
210 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
211 
212 #define X2APIC_MODE_SHIFT 30
213 #define X2APIC_MODE_MASK (1 << X2APIC_MODE_SHIFT)
214 
215 #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
216 #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
217 
218 #define SVM_INTERRUPT_SHADOW_MASK	BIT_ULL(0)
219 #define SVM_GUEST_INTERRUPT_MASK	BIT_ULL(1)
220 
221 #define SVM_IOIO_STR_SHIFT 2
222 #define SVM_IOIO_REP_SHIFT 3
223 #define SVM_IOIO_SIZE_SHIFT 4
224 #define SVM_IOIO_ASIZE_SHIFT 7
225 
226 #define SVM_IOIO_TYPE_MASK 1
227 #define SVM_IOIO_STR_MASK (1 << SVM_IOIO_STR_SHIFT)
228 #define SVM_IOIO_REP_MASK (1 << SVM_IOIO_REP_SHIFT)
229 #define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT)
230 #define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT)
231 
232 #define SVM_VM_CR_VALID_MASK	0x001fULL
233 #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
234 #define SVM_VM_CR_SVM_DIS_MASK  0x0010ULL
235 
236 #define SVM_NESTED_CTL_NP_ENABLE	BIT(0)
237 #define SVM_NESTED_CTL_SEV_ENABLE	BIT(1)
238 #define SVM_NESTED_CTL_SEV_ES_ENABLE	BIT(2)
239 
240 
241 #define SVM_TSC_RATIO_RSVD	0xffffff0000000000ULL
242 #define SVM_TSC_RATIO_MIN	0x0000000000000001ULL
243 #define SVM_TSC_RATIO_MAX	0x000000ffffffffffULL
244 #define SVM_TSC_RATIO_DEFAULT	0x0100000000ULL
245 
246 
247 /* AVIC */
248 #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK	(0xFFULL)
249 #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT			31
250 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK		(1 << 31)
251 
252 #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK	GENMASK_ULL(11, 0)
253 #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK	(0xFFFFFFFFFFULL << 12)
254 #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK		(1ULL << 62)
255 #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK		(1ULL << 63)
256 #define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK		(0xFFULL)
257 
258 #define AVIC_DOORBELL_PHYSICAL_ID_MASK			GENMASK_ULL(11, 0)
259 
260 #define VMCB_AVIC_APIC_BAR_MASK				0xFFFFFFFFFF000ULL
261 
262 #define AVIC_UNACCEL_ACCESS_WRITE_MASK		1
263 #define AVIC_UNACCEL_ACCESS_OFFSET_MASK		0xFF0
264 #define AVIC_UNACCEL_ACCESS_VECTOR_MASK		0xFFFFFFFF
265 
266 enum avic_ipi_failure_cause {
267 	AVIC_IPI_FAILURE_INVALID_INT_TYPE,
268 	AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
269 	AVIC_IPI_FAILURE_INVALID_TARGET,
270 	AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
271 };
272 
273 #define AVIC_PHYSICAL_MAX_INDEX_MASK	GENMASK_ULL(8, 0)
274 
275 /*
276  * For AVIC, the max index allowed for physical APIC ID table is 0xfe (254), as
277  * 0xff is a broadcast to all CPUs, i.e. can't be targeted individually.
278  */
279 #define AVIC_MAX_PHYSICAL_ID		0XFEULL
280 
281 /*
282  * For x2AVIC, the max index allowed for physical APIC ID table is 0x1ff (511).
283  */
284 #define X2AVIC_MAX_PHYSICAL_ID		0x1FFUL
285 
286 static_assert((AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == AVIC_MAX_PHYSICAL_ID);
287 static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_MAX_PHYSICAL_ID);
288 
289 #define AVIC_HPA_MASK	~((0xFFFULL << 52) | 0xFFF)
290 
291 #define SVM_SEV_FEAT_DEBUG_SWAP                        BIT(5)
292 
293 struct vmcb_seg {
294 	u16 selector;
295 	u16 attrib;
296 	u32 limit;
297 	u64 base;
298 } __packed;
299 
300 /* Save area definition for legacy and SEV-MEM guests */
301 struct vmcb_save_area {
302 	struct vmcb_seg es;
303 	struct vmcb_seg cs;
304 	struct vmcb_seg ss;
305 	struct vmcb_seg ds;
306 	struct vmcb_seg fs;
307 	struct vmcb_seg gs;
308 	struct vmcb_seg gdtr;
309 	struct vmcb_seg ldtr;
310 	struct vmcb_seg idtr;
311 	struct vmcb_seg tr;
312 	/* Reserved fields are named following their struct offset */
313 	u8 reserved_0xa0[42];
314 	u8 vmpl;
315 	u8 cpl;
316 	u8 reserved_0xcc[4];
317 	u64 efer;
318 	u8 reserved_0xd8[112];
319 	u64 cr4;
320 	u64 cr3;
321 	u64 cr0;
322 	u64 dr7;
323 	u64 dr6;
324 	u64 rflags;
325 	u64 rip;
326 	u8 reserved_0x180[88];
327 	u64 rsp;
328 	u64 s_cet;
329 	u64 ssp;
330 	u64 isst_addr;
331 	u64 rax;
332 	u64 star;
333 	u64 lstar;
334 	u64 cstar;
335 	u64 sfmask;
336 	u64 kernel_gs_base;
337 	u64 sysenter_cs;
338 	u64 sysenter_esp;
339 	u64 sysenter_eip;
340 	u64 cr2;
341 	u8 reserved_0x248[32];
342 	u64 g_pat;
343 	u64 dbgctl;
344 	u64 br_from;
345 	u64 br_to;
346 	u64 last_excp_from;
347 	u64 last_excp_to;
348 	u8 reserved_0x298[72];
349 	u64 spec_ctrl;		/* Guest version of SPEC_CTRL at 0x2E0 */
350 } __packed;
351 
352 /* Save area definition for SEV-ES and SEV-SNP guests */
353 struct sev_es_save_area {
354 	struct vmcb_seg es;
355 	struct vmcb_seg cs;
356 	struct vmcb_seg ss;
357 	struct vmcb_seg ds;
358 	struct vmcb_seg fs;
359 	struct vmcb_seg gs;
360 	struct vmcb_seg gdtr;
361 	struct vmcb_seg ldtr;
362 	struct vmcb_seg idtr;
363 	struct vmcb_seg tr;
364 	u64 vmpl0_ssp;
365 	u64 vmpl1_ssp;
366 	u64 vmpl2_ssp;
367 	u64 vmpl3_ssp;
368 	u64 u_cet;
369 	u8 reserved_0xc8[2];
370 	u8 vmpl;
371 	u8 cpl;
372 	u8 reserved_0xcc[4];
373 	u64 efer;
374 	u8 reserved_0xd8[104];
375 	u64 xss;
376 	u64 cr4;
377 	u64 cr3;
378 	u64 cr0;
379 	u64 dr7;
380 	u64 dr6;
381 	u64 rflags;
382 	u64 rip;
383 	u64 dr0;
384 	u64 dr1;
385 	u64 dr2;
386 	u64 dr3;
387 	u64 dr0_addr_mask;
388 	u64 dr1_addr_mask;
389 	u64 dr2_addr_mask;
390 	u64 dr3_addr_mask;
391 	u8 reserved_0x1c0[24];
392 	u64 rsp;
393 	u64 s_cet;
394 	u64 ssp;
395 	u64 isst_addr;
396 	u64 rax;
397 	u64 star;
398 	u64 lstar;
399 	u64 cstar;
400 	u64 sfmask;
401 	u64 kernel_gs_base;
402 	u64 sysenter_cs;
403 	u64 sysenter_esp;
404 	u64 sysenter_eip;
405 	u64 cr2;
406 	u8 reserved_0x248[32];
407 	u64 g_pat;
408 	u64 dbgctl;
409 	u64 br_from;
410 	u64 br_to;
411 	u64 last_excp_from;
412 	u64 last_excp_to;
413 	u8 reserved_0x298[80];
414 	u32 pkru;
415 	u32 tsc_aux;
416 	u8 reserved_0x2f0[24];
417 	u64 rcx;
418 	u64 rdx;
419 	u64 rbx;
420 	u64 reserved_0x320;	/* rsp already available at 0x01d8 */
421 	u64 rbp;
422 	u64 rsi;
423 	u64 rdi;
424 	u64 r8;
425 	u64 r9;
426 	u64 r10;
427 	u64 r11;
428 	u64 r12;
429 	u64 r13;
430 	u64 r14;
431 	u64 r15;
432 	u8 reserved_0x380[16];
433 	u64 guest_exit_info_1;
434 	u64 guest_exit_info_2;
435 	u64 guest_exit_int_info;
436 	u64 guest_nrip;
437 	u64 sev_features;
438 	u64 vintr_ctrl;
439 	u64 guest_exit_code;
440 	u64 virtual_tom;
441 	u64 tlb_id;
442 	u64 pcpu_id;
443 	u64 event_inj;
444 	u64 xcr0;
445 	u8 reserved_0x3f0[16];
446 
447 	/* Floating point area */
448 	u64 x87_dp;
449 	u32 mxcsr;
450 	u16 x87_ftw;
451 	u16 x87_fsw;
452 	u16 x87_fcw;
453 	u16 x87_fop;
454 	u16 x87_ds;
455 	u16 x87_cs;
456 	u64 x87_rip;
457 	u8 fpreg_x87[80];
458 	u8 fpreg_xmm[256];
459 	u8 fpreg_ymm[256];
460 } __packed;
461 
462 struct ghcb_save_area {
463 	u8 reserved_0x0[203];
464 	u8 cpl;
465 	u8 reserved_0xcc[116];
466 	u64 xss;
467 	u8 reserved_0x148[24];
468 	u64 dr7;
469 	u8 reserved_0x168[16];
470 	u64 rip;
471 	u8 reserved_0x180[88];
472 	u64 rsp;
473 	u8 reserved_0x1e0[24];
474 	u64 rax;
475 	u8 reserved_0x200[264];
476 	u64 rcx;
477 	u64 rdx;
478 	u64 rbx;
479 	u8 reserved_0x320[8];
480 	u64 rbp;
481 	u64 rsi;
482 	u64 rdi;
483 	u64 r8;
484 	u64 r9;
485 	u64 r10;
486 	u64 r11;
487 	u64 r12;
488 	u64 r13;
489 	u64 r14;
490 	u64 r15;
491 	u8 reserved_0x380[16];
492 	u64 sw_exit_code;
493 	u64 sw_exit_info_1;
494 	u64 sw_exit_info_2;
495 	u64 sw_scratch;
496 	u8 reserved_0x3b0[56];
497 	u64 xcr0;
498 	u8 valid_bitmap[16];
499 	u64 x87_state_gpa;
500 } __packed;
501 
502 #define GHCB_SHARED_BUF_SIZE	2032
503 
504 struct ghcb {
505 	struct ghcb_save_area save;
506 	u8 reserved_save[2048 - sizeof(struct ghcb_save_area)];
507 
508 	u8 shared_buffer[GHCB_SHARED_BUF_SIZE];
509 
510 	u8 reserved_0xff0[10];
511 	u16 protocol_version;	/* negotiated SEV-ES/GHCB protocol version */
512 	u32 ghcb_usage;
513 } __packed;
514 
515 
516 #define EXPECTED_VMCB_SAVE_AREA_SIZE		744
517 #define EXPECTED_GHCB_SAVE_AREA_SIZE		1032
518 #define EXPECTED_SEV_ES_SAVE_AREA_SIZE		1648
519 #define EXPECTED_VMCB_CONTROL_AREA_SIZE		1024
520 #define EXPECTED_GHCB_SIZE			PAGE_SIZE
521 
522 #define BUILD_BUG_RESERVED_OFFSET(x, y) \
523 	ASSERT_STRUCT_OFFSET(struct x, reserved ## _ ## y, y)
524 
525 static inline void __unused_size_checks(void)
526 {
527 	BUILD_BUG_ON(sizeof(struct vmcb_save_area)	!= EXPECTED_VMCB_SAVE_AREA_SIZE);
528 	BUILD_BUG_ON(sizeof(struct ghcb_save_area)	!= EXPECTED_GHCB_SAVE_AREA_SIZE);
529 	BUILD_BUG_ON(sizeof(struct sev_es_save_area)	!= EXPECTED_SEV_ES_SAVE_AREA_SIZE);
530 	BUILD_BUG_ON(sizeof(struct vmcb_control_area)	!= EXPECTED_VMCB_CONTROL_AREA_SIZE);
531 	BUILD_BUG_ON(sizeof(struct ghcb)		!= EXPECTED_GHCB_SIZE);
532 
533 	/* Check offsets of reserved fields */
534 
535 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xa0);
536 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xcc);
537 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0xd8);
538 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x180);
539 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x248);
540 	BUILD_BUG_RESERVED_OFFSET(vmcb_save_area, 0x298);
541 
542 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0xc8);
543 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0xcc);
544 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0xd8);
545 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x1c0);
546 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x248);
547 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x298);
548 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x2f0);
549 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320);
550 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380);
551 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0);
552 
553 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x0);
554 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0xcc);
555 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x148);
556 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x168);
557 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x180);
558 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x1e0);
559 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x200);
560 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x320);
561 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x380);
562 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x3b0);
563 
564 	BUILD_BUG_RESERVED_OFFSET(ghcb, 0xff0);
565 }
566 
567 struct vmcb {
568 	struct vmcb_control_area control;
569 	struct vmcb_save_area save;
570 } __packed;
571 
572 #define SVM_CPUID_FUNC 0x8000000a
573 
574 #define SVM_VM_CR_SVM_DISABLE 4
575 
576 #define SVM_SELECTOR_S_SHIFT 4
577 #define SVM_SELECTOR_DPL_SHIFT 5
578 #define SVM_SELECTOR_P_SHIFT 7
579 #define SVM_SELECTOR_AVL_SHIFT 8
580 #define SVM_SELECTOR_L_SHIFT 9
581 #define SVM_SELECTOR_DB_SHIFT 10
582 #define SVM_SELECTOR_G_SHIFT 11
583 
584 #define SVM_SELECTOR_TYPE_MASK (0xf)
585 #define SVM_SELECTOR_S_MASK (1 << SVM_SELECTOR_S_SHIFT)
586 #define SVM_SELECTOR_DPL_MASK (3 << SVM_SELECTOR_DPL_SHIFT)
587 #define SVM_SELECTOR_P_MASK (1 << SVM_SELECTOR_P_SHIFT)
588 #define SVM_SELECTOR_AVL_MASK (1 << SVM_SELECTOR_AVL_SHIFT)
589 #define SVM_SELECTOR_L_MASK (1 << SVM_SELECTOR_L_SHIFT)
590 #define SVM_SELECTOR_DB_MASK (1 << SVM_SELECTOR_DB_SHIFT)
591 #define SVM_SELECTOR_G_MASK (1 << SVM_SELECTOR_G_SHIFT)
592 
593 #define SVM_SELECTOR_WRITE_MASK (1 << 1)
594 #define SVM_SELECTOR_READ_MASK SVM_SELECTOR_WRITE_MASK
595 #define SVM_SELECTOR_CODE_MASK (1 << 3)
596 
597 #define SVM_EVTINJ_VEC_MASK 0xff
598 
599 #define SVM_EVTINJ_TYPE_SHIFT 8
600 #define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
601 
602 #define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
603 #define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
604 #define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
605 #define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
606 
607 #define SVM_EVTINJ_VALID (1 << 31)
608 #define SVM_EVTINJ_VALID_ERR (1 << 11)
609 
610 #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
611 #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
612 
613 #define	SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR
614 #define	SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
615 #define	SVM_EXITINTINFO_TYPE_EXEPT SVM_EVTINJ_TYPE_EXEPT
616 #define	SVM_EXITINTINFO_TYPE_SOFT SVM_EVTINJ_TYPE_SOFT
617 
618 #define SVM_EXITINTINFO_VALID SVM_EVTINJ_VALID
619 #define SVM_EXITINTINFO_VALID_ERR SVM_EVTINJ_VALID_ERR
620 
621 #define SVM_EXITINFOSHIFT_TS_REASON_IRET 36
622 #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
623 #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
624 
625 #define SVM_EXITINFO_REG_MASK 0x0F
626 
627 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
628 
629 /* GHCB Accessor functions */
630 
631 #define GHCB_BITMAP_IDX(field)							\
632 	(offsetof(struct ghcb_save_area, field) / sizeof(u64))
633 
634 #define DEFINE_GHCB_ACCESSORS(field)						\
635 	static __always_inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb) \
636 	{									\
637 		return test_bit(GHCB_BITMAP_IDX(field),				\
638 				(unsigned long *)&ghcb->save.valid_bitmap);	\
639 	}									\
640 										\
641 	static __always_inline u64 ghcb_get_##field(struct ghcb *ghcb)		\
642 	{									\
643 		return ghcb->save.field;					\
644 	}									\
645 										\
646 	static __always_inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb) \
647 	{									\
648 		return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0;	\
649 	}									\
650 										\
651 	static __always_inline void ghcb_set_##field(struct ghcb *ghcb, u64 value) \
652 	{									\
653 		__set_bit(GHCB_BITMAP_IDX(field),				\
654 			  (unsigned long *)&ghcb->save.valid_bitmap);		\
655 		ghcb->save.field = value;					\
656 	}
657 
658 DEFINE_GHCB_ACCESSORS(cpl)
659 DEFINE_GHCB_ACCESSORS(rip)
660 DEFINE_GHCB_ACCESSORS(rsp)
661 DEFINE_GHCB_ACCESSORS(rax)
662 DEFINE_GHCB_ACCESSORS(rcx)
663 DEFINE_GHCB_ACCESSORS(rdx)
664 DEFINE_GHCB_ACCESSORS(rbx)
665 DEFINE_GHCB_ACCESSORS(rbp)
666 DEFINE_GHCB_ACCESSORS(rsi)
667 DEFINE_GHCB_ACCESSORS(rdi)
668 DEFINE_GHCB_ACCESSORS(r8)
669 DEFINE_GHCB_ACCESSORS(r9)
670 DEFINE_GHCB_ACCESSORS(r10)
671 DEFINE_GHCB_ACCESSORS(r11)
672 DEFINE_GHCB_ACCESSORS(r12)
673 DEFINE_GHCB_ACCESSORS(r13)
674 DEFINE_GHCB_ACCESSORS(r14)
675 DEFINE_GHCB_ACCESSORS(r15)
676 DEFINE_GHCB_ACCESSORS(sw_exit_code)
677 DEFINE_GHCB_ACCESSORS(sw_exit_info_1)
678 DEFINE_GHCB_ACCESSORS(sw_exit_info_2)
679 DEFINE_GHCB_ACCESSORS(sw_scratch)
680 DEFINE_GHCB_ACCESSORS(xcr0)
681 
682 #endif
683