1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <uapi/asm/svm.h>
6 #include <uapi/asm/kvm.h>
8 #include <asm/hyperv-tlfs.h>
11 * 32-bit intercept words in the VMCB Control Area, starting
12 * at Byte offset 000h.
15 enum intercept_words {
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,
44 INTERCEPT_DR0_WRITE = 48,
52 /* Byte offset 008h (word 2) */
53 INTERCEPT_EXCEPTION_OFFSET = 64,
54 /* Byte offset 00Ch (word 3) */
60 INTERCEPT_SELECTIVE_CR0,
84 INTERCEPT_TASK_SWITCH,
85 INTERCEPT_FERR_FREEZE,
87 /* Byte offset 010h (word 4) */
88 INTERCEPT_VMRUN = 128,
100 INTERCEPT_MWAIT_COND,
113 /* Byte offset 014h (word 5) */
114 INTERCEPT_INVLPGB = 160,
115 INTERCEPT_INVLPGB_ILLEGAL,
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;
142 u32 exit_int_info_err;
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 */
160 u64 vmsa_pa; /* Used for an SEV-ES guest */
163 * Offset 0x3e0, 32 bytes reserved
164 * for use by hypervisor/software.
167 struct hv_enlightenments hv_enlightenments;
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
178 #define V_TPR_MASK 0x0f
180 #define V_IRQ_SHIFT 8
181 #define V_IRQ_MASK (1 << V_IRQ_SHIFT)
183 #define V_GIF_SHIFT 9
184 #define V_GIF_MASK (1 << V_GIF_SHIFT)
186 #define V_INTR_PRIO_SHIFT 16
187 #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
189 #define V_IGN_TPR_SHIFT 20
190 #define V_IGN_TPR_MASK (1 << V_IGN_TPR_SHIFT)
192 #define V_IRQ_INJECTION_BITS_MASK (V_IRQ_MASK | V_INTR_PRIO_MASK | V_IGN_TPR_MASK)
194 #define V_INTR_MASKING_SHIFT 24
195 #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
197 #define V_GIF_ENABLE_SHIFT 25
198 #define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT)
200 #define AVIC_ENABLE_SHIFT 31
201 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
203 #define X2APIC_MODE_SHIFT 30
204 #define X2APIC_MODE_MASK (1 << X2APIC_MODE_SHIFT)
206 #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
207 #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
209 #define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0)
210 #define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1)
212 #define SVM_IOIO_STR_SHIFT 2
213 #define SVM_IOIO_REP_SHIFT 3
214 #define SVM_IOIO_SIZE_SHIFT 4
215 #define SVM_IOIO_ASIZE_SHIFT 7
217 #define SVM_IOIO_TYPE_MASK 1
218 #define SVM_IOIO_STR_MASK (1 << SVM_IOIO_STR_SHIFT)
219 #define SVM_IOIO_REP_MASK (1 << SVM_IOIO_REP_SHIFT)
220 #define SVM_IOIO_SIZE_MASK (7 << SVM_IOIO_SIZE_SHIFT)
221 #define SVM_IOIO_ASIZE_MASK (7 << SVM_IOIO_ASIZE_SHIFT)
223 #define SVM_VM_CR_VALID_MASK 0x001fULL
224 #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
225 #define SVM_VM_CR_SVM_DIS_MASK 0x0010ULL
227 #define SVM_NESTED_CTL_NP_ENABLE BIT(0)
228 #define SVM_NESTED_CTL_SEV_ENABLE BIT(1)
229 #define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2)
232 #define SVM_TSC_RATIO_RSVD 0xffffff0000000000ULL
233 #define SVM_TSC_RATIO_MIN 0x0000000000000001ULL
234 #define SVM_TSC_RATIO_MAX 0x000000ffffffffffULL
235 #define SVM_TSC_RATIO_DEFAULT 0x0100000000ULL
239 #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFFULL)
240 #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31
241 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
243 #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK GENMASK_ULL(11, 0)
244 #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
245 #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
246 #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
247 #define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK (0xFFULL)
249 #define AVIC_DOORBELL_PHYSICAL_ID_MASK GENMASK_ULL(11, 0)
251 #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
253 #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
254 #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
255 #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
257 enum avic_ipi_failure_cause {
258 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
259 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
260 AVIC_IPI_FAILURE_INVALID_TARGET,
261 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
264 #define AVIC_PHYSICAL_MAX_INDEX_MASK GENMASK_ULL(8, 0)
267 * For AVIC, the max index allowed for physical APIC ID table is 0xfe (254), as
268 * 0xff is a broadcast to all CPUs, i.e. can't be targeted individually.
270 #define AVIC_MAX_PHYSICAL_ID 0XFEULL
273 * For x2AVIC, the max index allowed for physical APIC ID table is 0x1ff (511).
275 #define X2AVIC_MAX_PHYSICAL_ID 0x1FFUL
277 static_assert((AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == AVIC_MAX_PHYSICAL_ID);
278 static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_MAX_PHYSICAL_ID);
280 #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
281 #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
291 /* Save area definition for legacy and SEV-MEM guests */
292 struct vmcb_save_area {
299 struct vmcb_seg gdtr;
300 struct vmcb_seg ldtr;
301 struct vmcb_seg idtr;
339 u32 spec_ctrl; /* Guest version of SPEC_CTRL at 0x2E0 */
342 /* Save area definition for SEV-ES and SEV-SNP guests */
343 struct sev_es_save_area {
350 struct vmcb_seg gdtr;
351 struct vmcb_seg ldtr;
352 struct vmcb_seg idtr;
406 u64 reserved_9; /* rax already available at 0x01f8 */
410 u64 reserved_10; /* rsp already available at 0x01d8 */
423 u64 guest_exit_info_1;
424 u64 guest_exit_info_2;
425 u64 guest_exit_int_info;
437 /* Floating point area */
452 struct ghcb_save_area {
492 #define GHCB_SHARED_BUF_SIZE 2032
495 struct ghcb_save_area save;
496 u8 reserved_save[2048 - sizeof(struct ghcb_save_area)];
498 u8 shared_buffer[GHCB_SHARED_BUF_SIZE];
501 u16 protocol_version; /* negotiated SEV-ES/GHCB protocol version */
506 #define EXPECTED_VMCB_SAVE_AREA_SIZE 740
507 #define EXPECTED_GHCB_SAVE_AREA_SIZE 1032
508 #define EXPECTED_SEV_ES_SAVE_AREA_SIZE 1648
509 #define EXPECTED_VMCB_CONTROL_AREA_SIZE 1024
510 #define EXPECTED_GHCB_SIZE PAGE_SIZE
512 static inline void __unused_size_checks(void)
514 BUILD_BUG_ON(sizeof(struct vmcb_save_area) != EXPECTED_VMCB_SAVE_AREA_SIZE);
515 BUILD_BUG_ON(sizeof(struct ghcb_save_area) != EXPECTED_GHCB_SAVE_AREA_SIZE);
516 BUILD_BUG_ON(sizeof(struct sev_es_save_area) != EXPECTED_SEV_ES_SAVE_AREA_SIZE);
517 BUILD_BUG_ON(sizeof(struct vmcb_control_area) != EXPECTED_VMCB_CONTROL_AREA_SIZE);
518 BUILD_BUG_ON(sizeof(struct ghcb) != EXPECTED_GHCB_SIZE);
522 struct vmcb_control_area control;
523 struct vmcb_save_area save;
526 #define SVM_CPUID_FUNC 0x8000000a
528 #define SVM_VM_CR_SVM_DISABLE 4
530 #define SVM_SELECTOR_S_SHIFT 4
531 #define SVM_SELECTOR_DPL_SHIFT 5
532 #define SVM_SELECTOR_P_SHIFT 7
533 #define SVM_SELECTOR_AVL_SHIFT 8
534 #define SVM_SELECTOR_L_SHIFT 9
535 #define SVM_SELECTOR_DB_SHIFT 10
536 #define SVM_SELECTOR_G_SHIFT 11
538 #define SVM_SELECTOR_TYPE_MASK (0xf)
539 #define SVM_SELECTOR_S_MASK (1 << SVM_SELECTOR_S_SHIFT)
540 #define SVM_SELECTOR_DPL_MASK (3 << SVM_SELECTOR_DPL_SHIFT)
541 #define SVM_SELECTOR_P_MASK (1 << SVM_SELECTOR_P_SHIFT)
542 #define SVM_SELECTOR_AVL_MASK (1 << SVM_SELECTOR_AVL_SHIFT)
543 #define SVM_SELECTOR_L_MASK (1 << SVM_SELECTOR_L_SHIFT)
544 #define SVM_SELECTOR_DB_MASK (1 << SVM_SELECTOR_DB_SHIFT)
545 #define SVM_SELECTOR_G_MASK (1 << SVM_SELECTOR_G_SHIFT)
547 #define SVM_SELECTOR_WRITE_MASK (1 << 1)
548 #define SVM_SELECTOR_READ_MASK SVM_SELECTOR_WRITE_MASK
549 #define SVM_SELECTOR_CODE_MASK (1 << 3)
551 #define SVM_EVTINJ_VEC_MASK 0xff
553 #define SVM_EVTINJ_TYPE_SHIFT 8
554 #define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
556 #define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
557 #define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
558 #define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
559 #define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
561 #define SVM_EVTINJ_VALID (1 << 31)
562 #define SVM_EVTINJ_VALID_ERR (1 << 11)
564 #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
565 #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
567 #define SVM_EXITINTINFO_TYPE_INTR SVM_EVTINJ_TYPE_INTR
568 #define SVM_EXITINTINFO_TYPE_NMI SVM_EVTINJ_TYPE_NMI
569 #define SVM_EXITINTINFO_TYPE_EXEPT SVM_EVTINJ_TYPE_EXEPT
570 #define SVM_EXITINTINFO_TYPE_SOFT SVM_EVTINJ_TYPE_SOFT
572 #define SVM_EXITINTINFO_VALID SVM_EVTINJ_VALID
573 #define SVM_EXITINTINFO_VALID_ERR SVM_EVTINJ_VALID_ERR
575 #define SVM_EXITINFOSHIFT_TS_REASON_IRET 36
576 #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
577 #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
579 #define SVM_EXITINFO_REG_MASK 0x0F
581 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
583 /* GHCB Accessor functions */
585 #define GHCB_BITMAP_IDX(field) \
586 (offsetof(struct ghcb_save_area, field) / sizeof(u64))
588 #define DEFINE_GHCB_ACCESSORS(field) \
589 static __always_inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb) \
591 return test_bit(GHCB_BITMAP_IDX(field), \
592 (unsigned long *)&ghcb->save.valid_bitmap); \
595 static __always_inline u64 ghcb_get_##field(struct ghcb *ghcb) \
597 return ghcb->save.field; \
600 static __always_inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb) \
602 return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0; \
605 static __always_inline void ghcb_set_##field(struct ghcb *ghcb, u64 value) \
607 __set_bit(GHCB_BITMAP_IDX(field), \
608 (unsigned long *)&ghcb->save.valid_bitmap); \
609 ghcb->save.field = value; \
612 DEFINE_GHCB_ACCESSORS(cpl)
613 DEFINE_GHCB_ACCESSORS(rip)
614 DEFINE_GHCB_ACCESSORS(rsp)
615 DEFINE_GHCB_ACCESSORS(rax)
616 DEFINE_GHCB_ACCESSORS(rcx)
617 DEFINE_GHCB_ACCESSORS(rdx)
618 DEFINE_GHCB_ACCESSORS(rbx)
619 DEFINE_GHCB_ACCESSORS(rbp)
620 DEFINE_GHCB_ACCESSORS(rsi)
621 DEFINE_GHCB_ACCESSORS(rdi)
622 DEFINE_GHCB_ACCESSORS(r8)
623 DEFINE_GHCB_ACCESSORS(r9)
624 DEFINE_GHCB_ACCESSORS(r10)
625 DEFINE_GHCB_ACCESSORS(r11)
626 DEFINE_GHCB_ACCESSORS(r12)
627 DEFINE_GHCB_ACCESSORS(r13)
628 DEFINE_GHCB_ACCESSORS(r14)
629 DEFINE_GHCB_ACCESSORS(r15)
630 DEFINE_GHCB_ACCESSORS(sw_exit_code)
631 DEFINE_GHCB_ACCESSORS(sw_exit_info_1)
632 DEFINE_GHCB_ACCESSORS(sw_exit_info_2)
633 DEFINE_GHCB_ACCESSORS(sw_scratch)
634 DEFINE_GHCB_ACCESSORS(xcr0)