1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <uapi/asm/svm.h>
6 #include <uapi/asm/kvm.h>
9 * 32-bit intercept words in the VMCB Control Area, starting
10 * at Byte offset 000h.
13 enum intercept_words
{
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,
42 INTERCEPT_DR0_WRITE
= 48,
50 /* Byte offset 008h (word 2) */
51 INTERCEPT_EXCEPTION_OFFSET
= 64,
52 /* Byte offset 00Ch (word 3) */
58 INTERCEPT_SELECTIVE_CR0
,
82 INTERCEPT_TASK_SWITCH
,
83 INTERCEPT_FERR_FREEZE
,
85 /* Byte offset 010h (word 4) */
86 INTERCEPT_VMRUN
= 128,
111 /* Byte offset 014h (word 5) */
112 INTERCEPT_INVLPGB
= 160,
113 INTERCEPT_INVLPGB_ILLEGAL
,
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
;
140 u32 exit_int_info_err
;
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 */
158 u64 vmsa_pa
; /* Used for an SEV-ES guest */
162 #define TLB_CONTROL_DO_NOTHING 0
163 #define TLB_CONTROL_FLUSH_ALL_ASID 1
164 #define TLB_CONTROL_FLUSH_ASID 3
165 #define TLB_CONTROL_FLUSH_ASID_LOCAL 7
167 #define V_TPR_MASK 0x0f
169 #define V_IRQ_SHIFT 8
170 #define V_IRQ_MASK (1 << V_IRQ_SHIFT)
172 #define V_GIF_SHIFT 9
173 #define V_GIF_MASK (1 << V_GIF_SHIFT)
175 #define V_INTR_PRIO_SHIFT 16
176 #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
178 #define V_IGN_TPR_SHIFT 20
179 #define V_IGN_TPR_MASK (1 << V_IGN_TPR_SHIFT)
181 #define V_INTR_MASKING_SHIFT 24
182 #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
184 #define V_GIF_ENABLE_SHIFT 25
185 #define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT)
187 #define AVIC_ENABLE_SHIFT 31
188 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
190 #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
191 #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
193 #define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0)
194 #define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1)
196 #define SVM_IOIO_STR_SHIFT 2
197 #define SVM_IOIO_REP_SHIFT 3
198 #define SVM_IOIO_SIZE_SHIFT 4
199 #define SVM_IOIO_ASIZE_SHIFT 7
201 #define SVM_IOIO_TYPE_MASK 1
202 #define SVM_IOIO_STR_MASK (1 << SVM_IOIO_STR_SHIFT)
203 #define SVM_IOIO_REP_MASK (1 << SVM_IOIO_REP_SHIFT)
204 #define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT)
205 #define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT)
207 #define SVM_VM_CR_VALID_MASK 0x001fULL
208 #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
209 #define SVM_VM_CR_SVM_DIS_MASK 0x0010ULL
211 #define SVM_NESTED_CTL_NP_ENABLE BIT(0)
212 #define SVM_NESTED_CTL_SEV_ENABLE BIT(1)
213 #define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2)
222 struct vmcb_save_area
{
229 struct vmcb_seg gdtr
;
230 struct vmcb_seg ldtr
;
231 struct vmcb_seg idtr
;
238 u64 xss
; /* Valid for SEV-ES only */
268 * The following part of the save area is valid only for
269 * SEV-ES guests when referenced through the GHCB or for
270 * saving to the host save area.
275 u64 reserved_8
; /* rax already available at 0x01f8 */
279 u64 reserved_9
; /* rsp already available at 0x01d8 */
303 struct vmcb_save_area save
;
304 u8 reserved_save
[2048 - sizeof(struct vmcb_save_area
)];
306 u8 shared_buffer
[2032];
309 u16 protocol_version
; /* negotiated SEV-ES/GHCB protocol version */
314 #define EXPECTED_VMCB_SAVE_AREA_SIZE 1032
315 #define EXPECTED_VMCB_CONTROL_AREA_SIZE 272
316 #define EXPECTED_GHCB_SIZE PAGE_SIZE
318 static inline void __unused_size_checks(void)
320 BUILD_BUG_ON(sizeof(struct vmcb_save_area
) != EXPECTED_VMCB_SAVE_AREA_SIZE
);
321 BUILD_BUG_ON(sizeof(struct vmcb_control_area
) != EXPECTED_VMCB_CONTROL_AREA_SIZE
);
322 BUILD_BUG_ON(sizeof(struct ghcb
) != EXPECTED_GHCB_SIZE
);
326 struct vmcb_control_area control
;
327 u8 reserved_control
[1024 - sizeof(struct vmcb_control_area
)];
328 struct vmcb_save_area save
;
331 #define SVM_CPUID_FUNC 0x8000000a
333 #define SVM_VM_CR_SVM_DISABLE 4
335 #define SVM_SELECTOR_S_SHIFT 4
336 #define SVM_SELECTOR_DPL_SHIFT 5
337 #define SVM_SELECTOR_P_SHIFT 7
338 #define SVM_SELECTOR_AVL_SHIFT 8
339 #define SVM_SELECTOR_L_SHIFT 9
340 #define SVM_SELECTOR_DB_SHIFT 10
341 #define SVM_SELECTOR_G_SHIFT 11
343 #define SVM_SELECTOR_TYPE_MASK (0xf)
344 #define SVM_SELECTOR_S_MASK (1 << SVM_SELECTOR_S_SHIFT)
345 #define SVM_SELECTOR_DPL_MASK (3 << SVM_SELECTOR_DPL_SHIFT)
346 #define SVM_SELECTOR_P_MASK (1 << SVM_SELECTOR_P_SHIFT)
347 #define SVM_SELECTOR_AVL_MASK (1 << SVM_SELECTOR_AVL_SHIFT)
348 #define SVM_SELECTOR_L_MASK (1 << SVM_SELECTOR_L_SHIFT)
349 #define SVM_SELECTOR_DB_MASK (1 << SVM_SELECTOR_DB_SHIFT)
350 #define SVM_SELECTOR_G_MASK (1 << SVM_SELECTOR_G_SHIFT)
352 #define SVM_SELECTOR_WRITE_MASK (1 << 1)
353 #define SVM_SELECTOR_READ_MASK SVM_SELECTOR_WRITE_MASK
354 #define SVM_SELECTOR_CODE_MASK (1 << 3)
356 #define SVM_EVTINJ_VEC_MASK 0xff
358 #define SVM_EVTINJ_TYPE_SHIFT 8
359 #define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
361 #define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
362 #define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
363 #define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
364 #define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
366 #define SVM_EVTINJ_VALID (1 << 31)
367 #define SVM_EVTINJ_VALID_ERR (1 << 11)
369 #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
370 #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
372 #define SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR
373 #define SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
374 #define SVM_EXITINTINFO_TYPE_EXEPT SVM_EVTINJ_TYPE_EXEPT
375 #define SVM_EXITINTINFO_TYPE_SOFT SVM_EVTINJ_TYPE_SOFT
377 #define SVM_EXITINTINFO_VALID SVM_EVTINJ_VALID
378 #define SVM_EXITINTINFO_VALID_ERR SVM_EVTINJ_VALID_ERR
380 #define SVM_EXITINFOSHIFT_TS_REASON_IRET 36
381 #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
382 #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
384 #define SVM_EXITINFO_REG_MASK 0x0F
386 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
388 /* GHCB Accessor functions */
390 #define GHCB_BITMAP_IDX(field) \
391 (offsetof(struct vmcb_save_area, field) / sizeof(u64))
393 #define DEFINE_GHCB_ACCESSORS(field) \
394 static inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb) \
396 return test_bit(GHCB_BITMAP_IDX(field), \
397 (unsigned long *)&ghcb->save.valid_bitmap); \
400 static inline u64 ghcb_get_##field(struct ghcb *ghcb) \
402 return ghcb->save.field; \
405 static inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb) \
407 return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0; \
410 static inline void ghcb_set_##field(struct ghcb *ghcb, u64 value) \
412 __set_bit(GHCB_BITMAP_IDX(field), \
413 (unsigned long *)&ghcb->save.valid_bitmap); \
414 ghcb->save.field = value; \
417 DEFINE_GHCB_ACCESSORS(cpl
)
418 DEFINE_GHCB_ACCESSORS(rip
)
419 DEFINE_GHCB_ACCESSORS(rsp
)
420 DEFINE_GHCB_ACCESSORS(rax
)
421 DEFINE_GHCB_ACCESSORS(rcx
)
422 DEFINE_GHCB_ACCESSORS(rdx
)
423 DEFINE_GHCB_ACCESSORS(rbx
)
424 DEFINE_GHCB_ACCESSORS(rbp
)
425 DEFINE_GHCB_ACCESSORS(rsi
)
426 DEFINE_GHCB_ACCESSORS(rdi
)
427 DEFINE_GHCB_ACCESSORS(r8
)
428 DEFINE_GHCB_ACCESSORS(r9
)
429 DEFINE_GHCB_ACCESSORS(r10
)
430 DEFINE_GHCB_ACCESSORS(r11
)
431 DEFINE_GHCB_ACCESSORS(r12
)
432 DEFINE_GHCB_ACCESSORS(r13
)
433 DEFINE_GHCB_ACCESSORS(r14
)
434 DEFINE_GHCB_ACCESSORS(r15
)
435 DEFINE_GHCB_ACCESSORS(sw_exit_code
)
436 DEFINE_GHCB_ACCESSORS(sw_exit_info_1
)
437 DEFINE_GHCB_ACCESSORS(sw_exit_info_2
)
438 DEFINE_GHCB_ACCESSORS(sw_scratch
)
439 DEFINE_GHCB_ACCESSORS(xcr0
)