1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Macros for Flexible Return and Event Delivery (FRED) 4 */ 5 6 #ifndef ASM_X86_FRED_H 7 #define ASM_X86_FRED_H 8 9 #include <linux/const.h> 10 11 #include <asm/asm.h> 12 #include <asm/msr.h> 13 #include <asm/trapnr.h> 14 15 /* 16 * FRED event return instruction opcodes for ERET{S,U}; supported in 17 * binutils >= 2.41. 18 */ 19 #define ERETS _ASM_BYTES(0xf2,0x0f,0x01,0xca) 20 #define ERETU _ASM_BYTES(0xf3,0x0f,0x01,0xca) 21 22 /* 23 * RSP is aligned to a 64-byte boundary before used to push a new stack frame 24 */ 25 #define FRED_STACK_FRAME_RSP_MASK _AT(unsigned long, (~0x3f)) 26 27 /* 28 * Used for the return address for call emulation during code patching, 29 * and measured in 64-byte cache lines. 30 */ 31 #define FRED_CONFIG_REDZONE_AMOUNT 1 32 #define FRED_CONFIG_REDZONE (_AT(unsigned long, FRED_CONFIG_REDZONE_AMOUNT) << 6) 33 #define FRED_CONFIG_INT_STKLVL(l) (_AT(unsigned long, l) << 9) 34 #define FRED_CONFIG_ENTRYPOINT(p) _AT(unsigned long, (p)) 35 36 #ifndef __ASSEMBLER__ 37 38 #ifdef CONFIG_X86_FRED 39 #include <linux/kernel.h> 40 #include <linux/sched/task_stack.h> 41 42 #include <asm/ptrace.h> 43 44 struct fred_info { 45 /* Event data: CR2, DR6, ... */ 46 unsigned long edata; 47 unsigned long resv; 48 }; 49 50 /* Full format of the FRED stack frame */ 51 struct fred_frame { 52 struct pt_regs regs; 53 struct fred_info info; 54 }; 55 56 static __always_inline struct fred_info *fred_info(struct pt_regs *regs) 57 { 58 return &container_of(regs, struct fred_frame, regs)->info; 59 } 60 61 static __always_inline unsigned long fred_event_data(struct pt_regs *regs) 62 { 63 return fred_info(regs)->edata; 64 } 65 66 void asm_fred_entrypoint_user(void); 67 void asm_fred_entrypoint_kernel(void); 68 void asm_fred_entry_from_kvm(struct fred_ss); 69 70 __visible void fred_entry_from_user(struct pt_regs *regs); 71 __visible void fred_entry_from_kernel(struct pt_regs *regs); 72 __visible void __fred_entry_from_kvm(struct pt_regs *regs); 73 74 /* Can be called from noinstr code, thus __always_inline */ 75 static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) 76 { 77 struct fred_ss ss = { 78 .ss =__KERNEL_DS, 79 .type = type, 80 .vector = vector, 81 .nmi = type == EVENT_TYPE_NMI, 82 .lm = 1, 83 }; 84 85 asm_fred_entry_from_kvm(ss); 86 } 87 88 void cpu_init_fred_exceptions(void); 89 void cpu_init_fred_rsps(void); 90 void fred_complete_exception_setup(void); 91 92 DECLARE_PER_CPU(unsigned long, fred_rsp0); 93 94 static __always_inline void fred_sync_rsp0(unsigned long rsp0) 95 { 96 __this_cpu_write(fred_rsp0, rsp0); 97 } 98 99 static __always_inline void fred_update_rsp0(void) 100 { 101 unsigned long rsp0 = (unsigned long) task_stack_page(current) + THREAD_SIZE; 102 103 if (cpu_feature_enabled(X86_FEATURE_FRED) && (__this_cpu_read(fred_rsp0) != rsp0)) { 104 wrmsrns(MSR_IA32_FRED_RSP0, rsp0); 105 __this_cpu_write(fred_rsp0, rsp0); 106 } 107 } 108 #else /* CONFIG_X86_FRED */ 109 static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { return 0; } 110 static inline void cpu_init_fred_exceptions(void) { } 111 static inline void cpu_init_fred_rsps(void) { } 112 static inline void fred_complete_exception_setup(void) { } 113 static inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) { } 114 static inline void fred_sync_rsp0(unsigned long rsp0) { } 115 static inline void fred_update_rsp0(void) { } 116 #endif /* CONFIG_X86_FRED */ 117 #endif /* !__ASSEMBLER__ */ 118 119 #endif /* ASM_X86_FRED_H */ 120