1142781e1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0 */ 2142781e1SThomas Gleixner #ifndef __LINUX_ENTRYCOMMON_H 3142781e1SThomas Gleixner #define __LINUX_ENTRYCOMMON_H 4142781e1SThomas Gleixner 5142781e1SThomas Gleixner #include <linux/tracehook.h> 6142781e1SThomas Gleixner #include <linux/syscalls.h> 7142781e1SThomas Gleixner #include <linux/seccomp.h> 8142781e1SThomas Gleixner #include <linux/sched.h> 9142781e1SThomas Gleixner 10142781e1SThomas Gleixner #include <asm/entry-common.h> 11142781e1SThomas Gleixner 12142781e1SThomas Gleixner /* 13142781e1SThomas Gleixner * Define dummy _TIF work flags if not defined by the architecture or for 14142781e1SThomas Gleixner * disabled functionality. 15142781e1SThomas Gleixner */ 16142781e1SThomas Gleixner #ifndef _TIF_SYSCALL_EMU 17142781e1SThomas Gleixner # define _TIF_SYSCALL_EMU (0) 18142781e1SThomas Gleixner #endif 19142781e1SThomas Gleixner 20142781e1SThomas Gleixner #ifndef _TIF_SYSCALL_TRACEPOINT 21142781e1SThomas Gleixner # define _TIF_SYSCALL_TRACEPOINT (0) 22142781e1SThomas Gleixner #endif 23142781e1SThomas Gleixner 24142781e1SThomas Gleixner #ifndef _TIF_SECCOMP 25142781e1SThomas Gleixner # define _TIF_SECCOMP (0) 26142781e1SThomas Gleixner #endif 27142781e1SThomas Gleixner 28142781e1SThomas Gleixner #ifndef _TIF_SYSCALL_AUDIT 29142781e1SThomas Gleixner # define _TIF_SYSCALL_AUDIT (0) 30142781e1SThomas Gleixner #endif 31142781e1SThomas Gleixner 32*a9f3a74aSThomas Gleixner #ifndef _TIF_PATCH_PENDING 33*a9f3a74aSThomas Gleixner # define _TIF_PATCH_PENDING (0) 34*a9f3a74aSThomas Gleixner #endif 35*a9f3a74aSThomas Gleixner 36*a9f3a74aSThomas Gleixner #ifndef _TIF_UPROBE 37*a9f3a74aSThomas Gleixner # define _TIF_UPROBE (0) 38*a9f3a74aSThomas Gleixner #endif 39*a9f3a74aSThomas Gleixner 40142781e1SThomas Gleixner /* 41142781e1SThomas Gleixner * TIF flags handled in syscall_enter_from_usermode() 42142781e1SThomas Gleixner */ 43142781e1SThomas Gleixner #ifndef ARCH_SYSCALL_ENTER_WORK 44142781e1SThomas Gleixner # define ARCH_SYSCALL_ENTER_WORK (0) 45142781e1SThomas Gleixner #endif 46142781e1SThomas Gleixner 47142781e1SThomas Gleixner #define SYSCALL_ENTER_WORK \ 48142781e1SThomas Gleixner (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \ 49142781e1SThomas Gleixner _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_EMU | \ 50142781e1SThomas Gleixner ARCH_SYSCALL_ENTER_WORK) 51142781e1SThomas Gleixner 52*a9f3a74aSThomas Gleixner /* 53*a9f3a74aSThomas Gleixner * TIF flags handled in syscall_exit_to_user_mode() 54*a9f3a74aSThomas Gleixner */ 55*a9f3a74aSThomas Gleixner #ifndef ARCH_SYSCALL_EXIT_WORK 56*a9f3a74aSThomas Gleixner # define ARCH_SYSCALL_EXIT_WORK (0) 57*a9f3a74aSThomas Gleixner #endif 58*a9f3a74aSThomas Gleixner 59*a9f3a74aSThomas Gleixner #define SYSCALL_EXIT_WORK \ 60*a9f3a74aSThomas Gleixner (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \ 61*a9f3a74aSThomas Gleixner _TIF_SYSCALL_TRACEPOINT | ARCH_SYSCALL_EXIT_WORK) 62*a9f3a74aSThomas Gleixner 63*a9f3a74aSThomas Gleixner /* 64*a9f3a74aSThomas Gleixner * TIF flags handled in exit_to_user_mode_loop() 65*a9f3a74aSThomas Gleixner */ 66*a9f3a74aSThomas Gleixner #ifndef ARCH_EXIT_TO_USER_MODE_WORK 67*a9f3a74aSThomas Gleixner # define ARCH_EXIT_TO_USER_MODE_WORK (0) 68*a9f3a74aSThomas Gleixner #endif 69*a9f3a74aSThomas Gleixner 70*a9f3a74aSThomas Gleixner #define EXIT_TO_USER_MODE_WORK \ 71*a9f3a74aSThomas Gleixner (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \ 72*a9f3a74aSThomas Gleixner _TIF_NEED_RESCHED | _TIF_PATCH_PENDING | \ 73*a9f3a74aSThomas Gleixner ARCH_EXIT_TO_USER_MODE_WORK) 74*a9f3a74aSThomas Gleixner 75142781e1SThomas Gleixner /** 76142781e1SThomas Gleixner * arch_check_user_regs - Architecture specific sanity check for user mode regs 77142781e1SThomas Gleixner * @regs: Pointer to currents pt_regs 78142781e1SThomas Gleixner * 79142781e1SThomas Gleixner * Defaults to an empty implementation. Can be replaced by architecture 80142781e1SThomas Gleixner * specific code. 81142781e1SThomas Gleixner * 82142781e1SThomas Gleixner * Invoked from syscall_enter_from_user_mode() in the non-instrumentable 83142781e1SThomas Gleixner * section. Use __always_inline so the compiler cannot push it out of line 84142781e1SThomas Gleixner * and make it instrumentable. 85142781e1SThomas Gleixner */ 86142781e1SThomas Gleixner static __always_inline void arch_check_user_regs(struct pt_regs *regs); 87142781e1SThomas Gleixner 88142781e1SThomas Gleixner #ifndef arch_check_user_regs 89142781e1SThomas Gleixner static __always_inline void arch_check_user_regs(struct pt_regs *regs) {} 90142781e1SThomas Gleixner #endif 91142781e1SThomas Gleixner 92142781e1SThomas Gleixner /** 93142781e1SThomas Gleixner * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry() 94142781e1SThomas Gleixner * @regs: Pointer to currents pt_regs 95142781e1SThomas Gleixner * 96142781e1SThomas Gleixner * Returns: 0 on success or an error code to skip the syscall. 97142781e1SThomas Gleixner * 98142781e1SThomas Gleixner * Defaults to tracehook_report_syscall_entry(). Can be replaced by 99142781e1SThomas Gleixner * architecture specific code. 100142781e1SThomas Gleixner * 101142781e1SThomas Gleixner * Invoked from syscall_enter_from_user_mode() 102142781e1SThomas Gleixner */ 103142781e1SThomas Gleixner static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs); 104142781e1SThomas Gleixner 105142781e1SThomas Gleixner #ifndef arch_syscall_enter_tracehook 106142781e1SThomas Gleixner static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs) 107142781e1SThomas Gleixner { 108142781e1SThomas Gleixner return tracehook_report_syscall_entry(regs); 109142781e1SThomas Gleixner } 110142781e1SThomas Gleixner #endif 111142781e1SThomas Gleixner 112142781e1SThomas Gleixner /** 113142781e1SThomas Gleixner * syscall_enter_from_user_mode - Check and handle work before invoking 114142781e1SThomas Gleixner * a syscall 115142781e1SThomas Gleixner * @regs: Pointer to currents pt_regs 116142781e1SThomas Gleixner * @syscall: The syscall number 117142781e1SThomas Gleixner * 118142781e1SThomas Gleixner * Invoked from architecture specific syscall entry code with interrupts 119142781e1SThomas Gleixner * disabled. The calling code has to be non-instrumentable. When the 120142781e1SThomas Gleixner * function returns all state is correct and the subsequent functions can be 121142781e1SThomas Gleixner * instrumented. 122142781e1SThomas Gleixner * 123142781e1SThomas Gleixner * Returns: The original or a modified syscall number 124142781e1SThomas Gleixner * 125142781e1SThomas Gleixner * If the returned syscall number is -1 then the syscall should be 126142781e1SThomas Gleixner * skipped. In this case the caller may invoke syscall_set_error() or 127142781e1SThomas Gleixner * syscall_set_return_value() first. If neither of those are called and -1 128142781e1SThomas Gleixner * is returned, then the syscall will fail with ENOSYS. 129142781e1SThomas Gleixner * 130142781e1SThomas Gleixner * The following functionality is handled here: 131142781e1SThomas Gleixner * 132142781e1SThomas Gleixner * 1) Establish state (lockdep, RCU (context tracking), tracing) 133142781e1SThomas Gleixner * 2) TIF flag dependent invocations of arch_syscall_enter_tracehook(), 134142781e1SThomas Gleixner * __secure_computing(), trace_sys_enter() 135142781e1SThomas Gleixner * 3) Invocation of audit_syscall_entry() 136142781e1SThomas Gleixner */ 137142781e1SThomas Gleixner long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall); 138142781e1SThomas Gleixner 139142781e1SThomas Gleixner /** 140*a9f3a74aSThomas Gleixner * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable() 141*a9f3a74aSThomas Gleixner * @ti_work: Cached TIF flags gathered with interrupts disabled 142*a9f3a74aSThomas Gleixner * 143*a9f3a74aSThomas Gleixner * Defaults to local_irq_enable(). Can be supplied by architecture specific 144*a9f3a74aSThomas Gleixner * code. 145*a9f3a74aSThomas Gleixner */ 146*a9f3a74aSThomas Gleixner static inline void local_irq_enable_exit_to_user(unsigned long ti_work); 147*a9f3a74aSThomas Gleixner 148*a9f3a74aSThomas Gleixner #ifndef local_irq_enable_exit_to_user 149*a9f3a74aSThomas Gleixner static inline void local_irq_enable_exit_to_user(unsigned long ti_work) 150*a9f3a74aSThomas Gleixner { 151*a9f3a74aSThomas Gleixner local_irq_enable(); 152*a9f3a74aSThomas Gleixner } 153*a9f3a74aSThomas Gleixner #endif 154*a9f3a74aSThomas Gleixner 155*a9f3a74aSThomas Gleixner /** 156*a9f3a74aSThomas Gleixner * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable() 157*a9f3a74aSThomas Gleixner * 158*a9f3a74aSThomas Gleixner * Defaults to local_irq_disable(). Can be supplied by architecture specific 159*a9f3a74aSThomas Gleixner * code. 160*a9f3a74aSThomas Gleixner */ 161*a9f3a74aSThomas Gleixner static inline void local_irq_disable_exit_to_user(void); 162*a9f3a74aSThomas Gleixner 163*a9f3a74aSThomas Gleixner #ifndef local_irq_disable_exit_to_user 164*a9f3a74aSThomas Gleixner static inline void local_irq_disable_exit_to_user(void) 165*a9f3a74aSThomas Gleixner { 166*a9f3a74aSThomas Gleixner local_irq_disable(); 167*a9f3a74aSThomas Gleixner } 168*a9f3a74aSThomas Gleixner #endif 169*a9f3a74aSThomas Gleixner 170*a9f3a74aSThomas Gleixner /** 171*a9f3a74aSThomas Gleixner * arch_exit_to_user_mode_work - Architecture specific TIF work for exit 172*a9f3a74aSThomas Gleixner * to user mode. 173*a9f3a74aSThomas Gleixner * @regs: Pointer to currents pt_regs 174*a9f3a74aSThomas Gleixner * @ti_work: Cached TIF flags gathered with interrupts disabled 175*a9f3a74aSThomas Gleixner * 176*a9f3a74aSThomas Gleixner * Invoked from exit_to_user_mode_loop() with interrupt enabled 177*a9f3a74aSThomas Gleixner * 178*a9f3a74aSThomas Gleixner * Defaults to NOOP. Can be supplied by architecture specific code. 179*a9f3a74aSThomas Gleixner */ 180*a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_work(struct pt_regs *regs, 181*a9f3a74aSThomas Gleixner unsigned long ti_work); 182*a9f3a74aSThomas Gleixner 183*a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode_work 184*a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_work(struct pt_regs *regs, 185*a9f3a74aSThomas Gleixner unsigned long ti_work) 186*a9f3a74aSThomas Gleixner { 187*a9f3a74aSThomas Gleixner } 188*a9f3a74aSThomas Gleixner #endif 189*a9f3a74aSThomas Gleixner 190*a9f3a74aSThomas Gleixner /** 191*a9f3a74aSThomas Gleixner * arch_exit_to_user_mode_prepare - Architecture specific preparation for 192*a9f3a74aSThomas Gleixner * exit to user mode. 193*a9f3a74aSThomas Gleixner * @regs: Pointer to currents pt_regs 194*a9f3a74aSThomas Gleixner * @ti_work: Cached TIF flags gathered with interrupts disabled 195*a9f3a74aSThomas Gleixner * 196*a9f3a74aSThomas Gleixner * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last 197*a9f3a74aSThomas Gleixner * function before return. Defaults to NOOP. 198*a9f3a74aSThomas Gleixner */ 199*a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs, 200*a9f3a74aSThomas Gleixner unsigned long ti_work); 201*a9f3a74aSThomas Gleixner 202*a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode_prepare 203*a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs, 204*a9f3a74aSThomas Gleixner unsigned long ti_work) 205*a9f3a74aSThomas Gleixner { 206*a9f3a74aSThomas Gleixner } 207*a9f3a74aSThomas Gleixner #endif 208*a9f3a74aSThomas Gleixner 209*a9f3a74aSThomas Gleixner /** 210*a9f3a74aSThomas Gleixner * arch_exit_to_user_mode - Architecture specific final work before 211*a9f3a74aSThomas Gleixner * exit to user mode. 212*a9f3a74aSThomas Gleixner * 213*a9f3a74aSThomas Gleixner * Invoked from exit_to_user_mode() with interrupt disabled as the last 214*a9f3a74aSThomas Gleixner * function before return. Defaults to NOOP. 215*a9f3a74aSThomas Gleixner * 216*a9f3a74aSThomas Gleixner * This needs to be __always_inline because it is non-instrumentable code 217*a9f3a74aSThomas Gleixner * invoked after context tracking switched to user mode. 218*a9f3a74aSThomas Gleixner * 219*a9f3a74aSThomas Gleixner * An architecture implementation must not do anything complex, no locking 220*a9f3a74aSThomas Gleixner * etc. The main purpose is for speculation mitigations. 221*a9f3a74aSThomas Gleixner */ 222*a9f3a74aSThomas Gleixner static __always_inline void arch_exit_to_user_mode(void); 223*a9f3a74aSThomas Gleixner 224*a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode 225*a9f3a74aSThomas Gleixner static __always_inline void arch_exit_to_user_mode(void) { } 226*a9f3a74aSThomas Gleixner #endif 227*a9f3a74aSThomas Gleixner 228*a9f3a74aSThomas Gleixner /** 229*a9f3a74aSThomas Gleixner * arch_do_signal - Architecture specific signal delivery function 230*a9f3a74aSThomas Gleixner * @regs: Pointer to currents pt_regs 231*a9f3a74aSThomas Gleixner * 232*a9f3a74aSThomas Gleixner * Invoked from exit_to_user_mode_loop(). 233*a9f3a74aSThomas Gleixner */ 234*a9f3a74aSThomas Gleixner void arch_do_signal(struct pt_regs *regs); 235*a9f3a74aSThomas Gleixner 236*a9f3a74aSThomas Gleixner /** 237*a9f3a74aSThomas Gleixner * arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit() 238*a9f3a74aSThomas Gleixner * @regs: Pointer to currents pt_regs 239*a9f3a74aSThomas Gleixner * @step: Indicator for single step 240*a9f3a74aSThomas Gleixner * 241*a9f3a74aSThomas Gleixner * Defaults to tracehook_report_syscall_exit(). Can be replaced by 242*a9f3a74aSThomas Gleixner * architecture specific code. 243*a9f3a74aSThomas Gleixner * 244*a9f3a74aSThomas Gleixner * Invoked from syscall_exit_to_user_mode() 245*a9f3a74aSThomas Gleixner */ 246*a9f3a74aSThomas Gleixner static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step); 247*a9f3a74aSThomas Gleixner 248*a9f3a74aSThomas Gleixner #ifndef arch_syscall_exit_tracehook 249*a9f3a74aSThomas Gleixner static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step) 250*a9f3a74aSThomas Gleixner { 251*a9f3a74aSThomas Gleixner tracehook_report_syscall_exit(regs, step); 252*a9f3a74aSThomas Gleixner } 253*a9f3a74aSThomas Gleixner #endif 254*a9f3a74aSThomas Gleixner 255*a9f3a74aSThomas Gleixner /** 256*a9f3a74aSThomas Gleixner * syscall_exit_to_user_mode - Handle work before returning to user mode 257*a9f3a74aSThomas Gleixner * @regs: Pointer to currents pt_regs 258*a9f3a74aSThomas Gleixner * 259*a9f3a74aSThomas Gleixner * Invoked with interrupts enabled and fully valid regs. Returns with all 260*a9f3a74aSThomas Gleixner * work handled, interrupts disabled such that the caller can immediately 261*a9f3a74aSThomas Gleixner * switch to user mode. Called from architecture specific syscall and ret 262*a9f3a74aSThomas Gleixner * from fork code. 263*a9f3a74aSThomas Gleixner * 264*a9f3a74aSThomas Gleixner * The call order is: 265*a9f3a74aSThomas Gleixner * 1) One-time syscall exit work: 266*a9f3a74aSThomas Gleixner * - rseq syscall exit 267*a9f3a74aSThomas Gleixner * - audit 268*a9f3a74aSThomas Gleixner * - syscall tracing 269*a9f3a74aSThomas Gleixner * - tracehook (single stepping) 270*a9f3a74aSThomas Gleixner * 271*a9f3a74aSThomas Gleixner * 2) Preparatory work 272*a9f3a74aSThomas Gleixner * - Exit to user mode loop (common TIF handling). Invokes 273*a9f3a74aSThomas Gleixner * arch_exit_to_user_mode_work() for architecture specific TIF work 274*a9f3a74aSThomas Gleixner * - Architecture specific one time work arch_exit_to_user_mode_prepare() 275*a9f3a74aSThomas Gleixner * - Address limit and lockdep checks 276*a9f3a74aSThomas Gleixner * 277*a9f3a74aSThomas Gleixner * 3) Final transition (lockdep, tracing, context tracking, RCU). Invokes 278*a9f3a74aSThomas Gleixner * arch_exit_to_user_mode() to handle e.g. speculation mitigations 279*a9f3a74aSThomas Gleixner */ 280*a9f3a74aSThomas Gleixner void syscall_exit_to_user_mode(struct pt_regs *regs); 281*a9f3a74aSThomas Gleixner 282*a9f3a74aSThomas Gleixner /** 283142781e1SThomas Gleixner * irqentry_enter_from_user_mode - Establish state before invoking the irq handler 284142781e1SThomas Gleixner * @regs: Pointer to currents pt_regs 285142781e1SThomas Gleixner * 286142781e1SThomas Gleixner * Invoked from architecture specific entry code with interrupts disabled. 287142781e1SThomas Gleixner * Can only be called when the interrupt entry came from user mode. The 288142781e1SThomas Gleixner * calling code must be non-instrumentable. When the function returns all 289142781e1SThomas Gleixner * state is correct and the subsequent functions can be instrumented. 290142781e1SThomas Gleixner * 291142781e1SThomas Gleixner * The function establishes state (lockdep, RCU (context tracking), tracing) 292142781e1SThomas Gleixner */ 293142781e1SThomas Gleixner void irqentry_enter_from_user_mode(struct pt_regs *regs); 294142781e1SThomas Gleixner 295*a9f3a74aSThomas Gleixner /** 296*a9f3a74aSThomas Gleixner * irqentry_exit_to_user_mode - Interrupt exit work 297*a9f3a74aSThomas Gleixner * @regs: Pointer to current's pt_regs 298*a9f3a74aSThomas Gleixner * 299*a9f3a74aSThomas Gleixner * Invoked with interrupts disbled and fully valid regs. Returns with all 300*a9f3a74aSThomas Gleixner * work handled, interrupts disabled such that the caller can immediately 301*a9f3a74aSThomas Gleixner * switch to user mode. Called from architecture specific interrupt 302*a9f3a74aSThomas Gleixner * handling code. 303*a9f3a74aSThomas Gleixner * 304*a9f3a74aSThomas Gleixner * The call order is #2 and #3 as described in syscall_exit_to_user_mode(). 305*a9f3a74aSThomas Gleixner * Interrupt exit is not invoking #1 which is the syscall specific one time 306*a9f3a74aSThomas Gleixner * work. 307*a9f3a74aSThomas Gleixner */ 308*a9f3a74aSThomas Gleixner void irqentry_exit_to_user_mode(struct pt_regs *regs); 309*a9f3a74aSThomas Gleixner 310142781e1SThomas Gleixner #endif 311