1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_ENTRYCOMMON_H
3 #define __LINUX_ENTRYCOMMON_H
5 #include <linux/tracehook.h>
6 #include <linux/syscalls.h>
7 #include <linux/seccomp.h>
8 #include <linux/sched.h>
10 #include <asm/entry-common.h>
13 * Define dummy _TIF work flags if not defined by the architecture or for
14 * disabled functionality.
16 #ifndef _TIF_SYSCALL_EMU
17 # define _TIF_SYSCALL_EMU (0)
20 #ifndef _TIF_SYSCALL_TRACEPOINT
21 # define _TIF_SYSCALL_TRACEPOINT (0)
25 # define _TIF_SECCOMP (0)
28 #ifndef _TIF_SYSCALL_AUDIT
29 # define _TIF_SYSCALL_AUDIT (0)
32 #ifndef _TIF_PATCH_PENDING
33 # define _TIF_PATCH_PENDING (0)
37 # define _TIF_UPROBE (0)
40 #ifndef _TIF_NOTIFY_SIGNAL
41 # define _TIF_NOTIFY_SIGNAL (0)
45 * TIF flags handled in syscall_enter_from_user_mode()
47 #ifndef ARCH_SYSCALL_ENTER_WORK
48 # define ARCH_SYSCALL_ENTER_WORK (0)
51 #define SYSCALL_ENTER_WORK \
52 (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \
53 _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_EMU | \
54 ARCH_SYSCALL_ENTER_WORK)
57 * TIF flags handled in syscall_exit_to_user_mode()
59 #ifndef ARCH_SYSCALL_EXIT_WORK
60 # define ARCH_SYSCALL_EXIT_WORK (0)
63 #define SYSCALL_EXIT_WORK \
64 (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
65 _TIF_SYSCALL_TRACEPOINT | ARCH_SYSCALL_EXIT_WORK)
68 * TIF flags handled in exit_to_user_mode_loop()
70 #ifndef ARCH_EXIT_TO_USER_MODE_WORK
71 # define ARCH_EXIT_TO_USER_MODE_WORK (0)
74 #define EXIT_TO_USER_MODE_WORK \
75 (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
76 _TIF_NEED_RESCHED | _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL | \
77 ARCH_EXIT_TO_USER_MODE_WORK)
80 * arch_check_user_regs - Architecture specific sanity check for user mode regs
81 * @regs: Pointer to currents pt_regs
83 * Defaults to an empty implementation. Can be replaced by architecture
86 * Invoked from syscall_enter_from_user_mode() in the non-instrumentable
87 * section. Use __always_inline so the compiler cannot push it out of line
88 * and make it instrumentable.
90 static __always_inline void arch_check_user_regs(struct pt_regs *regs);
92 #ifndef arch_check_user_regs
93 static __always_inline void arch_check_user_regs(struct pt_regs *regs) {}
97 * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry()
98 * @regs: Pointer to currents pt_regs
100 * Returns: 0 on success or an error code to skip the syscall.
102 * Defaults to tracehook_report_syscall_entry(). Can be replaced by
103 * architecture specific code.
105 * Invoked from syscall_enter_from_user_mode()
107 static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs);
109 #ifndef arch_syscall_enter_tracehook
110 static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs)
112 return tracehook_report_syscall_entry(regs);
117 * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
118 * @regs: Pointer to currents pt_regs
120 * Invoked from architecture specific syscall entry code with interrupts
121 * disabled. The calling code has to be non-instrumentable. When the
122 * function returns all state is correct, interrupts are enabled and the
123 * subsequent functions can be instrumented.
125 * This handles lockdep, RCU (context tracking) and tracing state.
127 * This is invoked when there is extra architecture specific functionality
128 * to be done between establishing state and handling user mode entry work.
130 void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);
133 * syscall_enter_from_user_mode_work - Check and handle work before invoking
135 * @regs: Pointer to currents pt_regs
136 * @syscall: The syscall number
138 * Invoked from architecture specific syscall entry code with interrupts
139 * enabled after invoking syscall_enter_from_user_mode_prepare() and extra
140 * architecture specific work.
142 * Returns: The original or a modified syscall number
144 * If the returned syscall number is -1 then the syscall should be
145 * skipped. In this case the caller may invoke syscall_set_error() or
146 * syscall_set_return_value() first. If neither of those are called and -1
147 * is returned, then the syscall will fail with ENOSYS.
149 * It handles the following work items:
151 * 1) TIF flag dependent invocations of arch_syscall_enter_tracehook(),
152 * __secure_computing(), trace_sys_enter()
153 * 2) Invocation of audit_syscall_entry()
155 long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall);
158 * syscall_enter_from_user_mode - Establish state and check and handle work
159 * before invoking a syscall
160 * @regs: Pointer to currents pt_regs
161 * @syscall: The syscall number
163 * Invoked from architecture specific syscall entry code with interrupts
164 * disabled. The calling code has to be non-instrumentable. When the
165 * function returns all state is correct, interrupts are enabled and the
166 * subsequent functions can be instrumented.
168 * This is combination of syscall_enter_from_user_mode_prepare() and
169 * syscall_enter_from_user_mode_work().
171 * Returns: The original or a modified syscall number. See
172 * syscall_enter_from_user_mode_work() for further explanation.
174 long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall);
177 * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
178 * @ti_work: Cached TIF flags gathered with interrupts disabled
180 * Defaults to local_irq_enable(). Can be supplied by architecture specific
183 static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
185 #ifndef local_irq_enable_exit_to_user
186 static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
193 * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
195 * Defaults to local_irq_disable(). Can be supplied by architecture specific
198 static inline void local_irq_disable_exit_to_user(void);
200 #ifndef local_irq_disable_exit_to_user
201 static inline void local_irq_disable_exit_to_user(void)
208 * arch_exit_to_user_mode_work - Architecture specific TIF work for exit
210 * @regs: Pointer to currents pt_regs
211 * @ti_work: Cached TIF flags gathered with interrupts disabled
213 * Invoked from exit_to_user_mode_loop() with interrupt enabled
215 * Defaults to NOOP. Can be supplied by architecture specific code.
217 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
218 unsigned long ti_work);
220 #ifndef arch_exit_to_user_mode_work
221 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
222 unsigned long ti_work)
228 * arch_exit_to_user_mode_prepare - Architecture specific preparation for
230 * @regs: Pointer to currents pt_regs
231 * @ti_work: Cached TIF flags gathered with interrupts disabled
233 * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last
234 * function before return. Defaults to NOOP.
236 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
237 unsigned long ti_work);
239 #ifndef arch_exit_to_user_mode_prepare
240 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
241 unsigned long ti_work)
247 * arch_exit_to_user_mode - Architecture specific final work before
250 * Invoked from exit_to_user_mode() with interrupt disabled as the last
251 * function before return. Defaults to NOOP.
253 * This needs to be __always_inline because it is non-instrumentable code
254 * invoked after context tracking switched to user mode.
256 * An architecture implementation must not do anything complex, no locking
257 * etc. The main purpose is for speculation mitigations.
259 static __always_inline void arch_exit_to_user_mode(void);
261 #ifndef arch_exit_to_user_mode
262 static __always_inline void arch_exit_to_user_mode(void) { }
266 * arch_do_signal_or_restart - Architecture specific signal delivery function
267 * @regs: Pointer to currents pt_regs
268 * @has_signal: actual signal to handle
270 * Invoked from exit_to_user_mode_loop().
272 void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal);
275 * arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit()
276 * @regs: Pointer to currents pt_regs
277 * @step: Indicator for single step
279 * Defaults to tracehook_report_syscall_exit(). Can be replaced by
280 * architecture specific code.
282 * Invoked from syscall_exit_to_user_mode()
284 static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step);
286 #ifndef arch_syscall_exit_tracehook
287 static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step)
289 tracehook_report_syscall_exit(regs, step);
294 * syscall_exit_to_user_mode - Handle work before returning to user mode
295 * @regs: Pointer to currents pt_regs
297 * Invoked with interrupts enabled and fully valid regs. Returns with all
298 * work handled, interrupts disabled such that the caller can immediately
299 * switch to user mode. Called from architecture specific syscall and ret
303 * 1) One-time syscall exit work:
304 * - rseq syscall exit
307 * - tracehook (single stepping)
309 * 2) Preparatory work
310 * - Exit to user mode loop (common TIF handling). Invokes
311 * arch_exit_to_user_mode_work() for architecture specific TIF work
312 * - Architecture specific one time work arch_exit_to_user_mode_prepare()
313 * - Address limit and lockdep checks
315 * 3) Final transition (lockdep, tracing, context tracking, RCU). Invokes
316 * arch_exit_to_user_mode() to handle e.g. speculation mitigations
318 void syscall_exit_to_user_mode(struct pt_regs *regs);
321 * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
322 * @regs: Pointer to currents pt_regs
324 * Invoked from architecture specific entry code with interrupts disabled.
325 * Can only be called when the interrupt entry came from user mode. The
326 * calling code must be non-instrumentable. When the function returns all
327 * state is correct and the subsequent functions can be instrumented.
329 * The function establishes state (lockdep, RCU (context tracking), tracing)
331 void irqentry_enter_from_user_mode(struct pt_regs *regs);
334 * irqentry_exit_to_user_mode - Interrupt exit work
335 * @regs: Pointer to current's pt_regs
337 * Invoked with interrupts disbled and fully valid regs. Returns with all
338 * work handled, interrupts disabled such that the caller can immediately
339 * switch to user mode. Called from architecture specific interrupt
342 * The call order is #2 and #3 as described in syscall_exit_to_user_mode().
343 * Interrupt exit is not invoking #1 which is the syscall specific one time
346 void irqentry_exit_to_user_mode(struct pt_regs *regs);
348 #ifndef irqentry_state
349 typedef struct irqentry_state {
355 * irqentry_enter - Handle state tracking on ordinary interrupt entries
356 * @regs: Pointer to pt_regs of interrupted context
359 * - lockdep irqflag state tracking as low level ASM entry disabled
362 * - Context tracking if the exception hit user mode.
364 * - The hardirq tracer to keep the state consistent as low level ASM
365 * entry disabled interrupts.
367 * As a precondition, this requires that the entry came from user mode,
368 * idle, or a kernel context in which RCU is watching.
370 * For kernel mode entries RCU handling is done conditional. If RCU is
371 * watching then the only RCU requirement is to check whether the tick has
372 * to be restarted. If RCU is not watching then rcu_irq_enter() has to be
373 * invoked on entry and rcu_irq_exit() on exit.
375 * Avoiding the rcu_irq_enter/exit() calls is an optimization but also
376 * solves the problem of kernel mode pagefaults which can schedule, which
377 * is not possible after invoking rcu_irq_enter() without undoing it.
379 * For user mode entries irqentry_enter_from_user_mode() is invoked to
380 * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
381 * would not be possible.
383 * Returns: An opaque object that must be passed to idtentry_exit()
385 irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs);
388 * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt
390 * Conditional reschedule with additional sanity checks.
392 void irqentry_exit_cond_resched(void);
395 * irqentry_exit - Handle return from exception that used irqentry_enter()
396 * @regs: Pointer to pt_regs (exception entry regs)
397 * @state: Return value from matching call to irqentry_enter()
399 * Depending on the return target (kernel/user) this runs the necessary
400 * preemption and work checks if possible and reguired and returns to
401 * the caller with interrupts disabled and no further work pending.
403 * This is the last action before returning to the low level ASM code which
404 * just needs to return to the appropriate context.
406 * Counterpart to irqentry_enter().
408 void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state);