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