Merge branch 'core/urgent' into core/entry
[sfrench/cifs-2.6.git] / include / linux / entry-common.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_ENTRYCOMMON_H
3 #define __LINUX_ENTRYCOMMON_H
4
5 #include <linux/tracehook.h>
6 #include <linux/syscalls.h>
7 #include <linux/seccomp.h>
8 #include <linux/sched.h>
9
10 #include <asm/entry-common.h>
11
12 /*
13  * Define dummy _TIF work flags if not defined by the architecture or for
14  * disabled functionality.
15  */
16 #ifndef _TIF_SYSCALL_EMU
17 # define _TIF_SYSCALL_EMU               (0)
18 #endif
19
20 #ifndef _TIF_SYSCALL_TRACEPOINT
21 # define _TIF_SYSCALL_TRACEPOINT        (0)
22 #endif
23
24 #ifndef _TIF_SECCOMP
25 # define _TIF_SECCOMP                   (0)
26 #endif
27
28 #ifndef _TIF_SYSCALL_AUDIT
29 # define _TIF_SYSCALL_AUDIT             (0)
30 #endif
31
32 #ifndef _TIF_PATCH_PENDING
33 # define _TIF_PATCH_PENDING             (0)
34 #endif
35
36 #ifndef _TIF_UPROBE
37 # define _TIF_UPROBE                    (0)
38 #endif
39
40 #ifndef _TIF_NOTIFY_SIGNAL
41 # define _TIF_NOTIFY_SIGNAL             (0)
42 #endif
43
44 /*
45  * TIF flags handled in syscall_enter_from_user_mode()
46  */
47 #ifndef ARCH_SYSCALL_ENTER_WORK
48 # define ARCH_SYSCALL_ENTER_WORK        (0)
49 #endif
50
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)
55
56 /*
57  * TIF flags handled in syscall_exit_to_user_mode()
58  */
59 #ifndef ARCH_SYSCALL_EXIT_WORK
60 # define ARCH_SYSCALL_EXIT_WORK         (0)
61 #endif
62
63 #define SYSCALL_EXIT_WORK                                               \
64         (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT |                      \
65          _TIF_SYSCALL_TRACEPOINT | ARCH_SYSCALL_EXIT_WORK)
66
67 /*
68  * TIF flags handled in exit_to_user_mode_loop()
69  */
70 #ifndef ARCH_EXIT_TO_USER_MODE_WORK
71 # define ARCH_EXIT_TO_USER_MODE_WORK            (0)
72 #endif
73
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)
78
79 /**
80  * arch_check_user_regs - Architecture specific sanity check for user mode regs
81  * @regs:       Pointer to currents pt_regs
82  *
83  * Defaults to an empty implementation. Can be replaced by architecture
84  * specific code.
85  *
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.
89  */
90 static __always_inline void arch_check_user_regs(struct pt_regs *regs);
91
92 #ifndef arch_check_user_regs
93 static __always_inline void arch_check_user_regs(struct pt_regs *regs) {}
94 #endif
95
96 /**
97  * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry()
98  * @regs:       Pointer to currents pt_regs
99  *
100  * Returns: 0 on success or an error code to skip the syscall.
101  *
102  * Defaults to tracehook_report_syscall_entry(). Can be replaced by
103  * architecture specific code.
104  *
105  * Invoked from syscall_enter_from_user_mode()
106  */
107 static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs);
108
109 #ifndef arch_syscall_enter_tracehook
110 static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs)
111 {
112         return tracehook_report_syscall_entry(regs);
113 }
114 #endif
115
116 /**
117  * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
118  * @regs:       Pointer to currents pt_regs
119  *
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.
124  *
125  * This handles lockdep, RCU (context tracking) and tracing state.
126  *
127  * This is invoked when there is extra architecture specific functionality
128  * to be done between establishing state and handling user mode entry work.
129  */
130 void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);
131
132 /**
133  * syscall_enter_from_user_mode_work - Check and handle work before invoking
134  *                                     a syscall
135  * @regs:       Pointer to currents pt_regs
136  * @syscall:    The syscall number
137  *
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.
141  *
142  * Returns: The original or a modified syscall number
143  *
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.
148  *
149  * It handles the following work items:
150  *
151  *  1) TIF flag dependent invocations of arch_syscall_enter_tracehook(),
152  *     __secure_computing(), trace_sys_enter()
153  *  2) Invocation of audit_syscall_entry()
154  */
155 long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall);
156
157 /**
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
162  *
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.
167  *
168  * This is combination of syscall_enter_from_user_mode_prepare() and
169  * syscall_enter_from_user_mode_work().
170  *
171  * Returns: The original or a modified syscall number. See
172  * syscall_enter_from_user_mode_work() for further explanation.
173  */
174 long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall);
175
176 /**
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
179  *
180  * Defaults to local_irq_enable(). Can be supplied by architecture specific
181  * code.
182  */
183 static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
184
185 #ifndef local_irq_enable_exit_to_user
186 static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
187 {
188         local_irq_enable();
189 }
190 #endif
191
192 /**
193  * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
194  *
195  * Defaults to local_irq_disable(). Can be supplied by architecture specific
196  * code.
197  */
198 static inline void local_irq_disable_exit_to_user(void);
199
200 #ifndef local_irq_disable_exit_to_user
201 static inline void local_irq_disable_exit_to_user(void)
202 {
203         local_irq_disable();
204 }
205 #endif
206
207 /**
208  * arch_exit_to_user_mode_work - Architecture specific TIF work for exit
209  *                               to user mode.
210  * @regs:       Pointer to currents pt_regs
211  * @ti_work:    Cached TIF flags gathered with interrupts disabled
212  *
213  * Invoked from exit_to_user_mode_loop() with interrupt enabled
214  *
215  * Defaults to NOOP. Can be supplied by architecture specific code.
216  */
217 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
218                                                unsigned long ti_work);
219
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)
223 {
224 }
225 #endif
226
227 /**
228  * arch_exit_to_user_mode_prepare - Architecture specific preparation for
229  *                                  exit to user mode.
230  * @regs:       Pointer to currents pt_regs
231  * @ti_work:    Cached TIF flags gathered with interrupts disabled
232  *
233  * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last
234  * function before return. Defaults to NOOP.
235  */
236 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
237                                                   unsigned long ti_work);
238
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)
242 {
243 }
244 #endif
245
246 /**
247  * arch_exit_to_user_mode - Architecture specific final work before
248  *                          exit to user mode.
249  *
250  * Invoked from exit_to_user_mode() with interrupt disabled as the last
251  * function before return. Defaults to NOOP.
252  *
253  * This needs to be __always_inline because it is non-instrumentable code
254  * invoked after context tracking switched to user mode.
255  *
256  * An architecture implementation must not do anything complex, no locking
257  * etc. The main purpose is for speculation mitigations.
258  */
259 static __always_inline void arch_exit_to_user_mode(void);
260
261 #ifndef arch_exit_to_user_mode
262 static __always_inline void arch_exit_to_user_mode(void) { }
263 #endif
264
265 /**
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
269  *
270  * Invoked from exit_to_user_mode_loop().
271  */
272 void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal);
273
274 /**
275  * arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit()
276  * @regs:       Pointer to currents pt_regs
277  * @step:       Indicator for single step
278  *
279  * Defaults to tracehook_report_syscall_exit(). Can be replaced by
280  * architecture specific code.
281  *
282  * Invoked from syscall_exit_to_user_mode()
283  */
284 static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step);
285
286 #ifndef arch_syscall_exit_tracehook
287 static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step)
288 {
289         tracehook_report_syscall_exit(regs, step);
290 }
291 #endif
292
293 /**
294  * syscall_exit_to_user_mode - Handle work before returning to user mode
295  * @regs:       Pointer to currents pt_regs
296  *
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
300  * from fork code.
301  *
302  * The call order is:
303  *  1) One-time syscall exit work:
304  *      - rseq syscall exit
305  *      - audit
306  *      - syscall tracing
307  *      - tracehook (single stepping)
308  *
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
314  *
315  *  3) Final transition (lockdep, tracing, context tracking, RCU). Invokes
316  *     arch_exit_to_user_mode() to handle e.g. speculation mitigations
317  */
318 void syscall_exit_to_user_mode(struct pt_regs *regs);
319
320 /**
321  * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
322  * @regs:       Pointer to currents pt_regs
323  *
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.
328  *
329  * The function establishes state (lockdep, RCU (context tracking), tracing)
330  */
331 void irqentry_enter_from_user_mode(struct pt_regs *regs);
332
333 /**
334  * irqentry_exit_to_user_mode - Interrupt exit work
335  * @regs:       Pointer to current's pt_regs
336  *
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
340  * handling code.
341  *
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
344  * work.
345  */
346 void irqentry_exit_to_user_mode(struct pt_regs *regs);
347
348 #ifndef irqentry_state
349 typedef struct irqentry_state {
350         bool    exit_rcu;
351 } irqentry_state_t;
352 #endif
353
354 /**
355  * irqentry_enter - Handle state tracking on ordinary interrupt entries
356  * @regs:       Pointer to pt_regs of interrupted context
357  *
358  * Invokes:
359  *  - lockdep irqflag state tracking as low level ASM entry disabled
360  *    interrupts.
361  *
362  *  - Context tracking if the exception hit user mode.
363  *
364  *  - The hardirq tracer to keep the state consistent as low level ASM
365  *    entry disabled interrupts.
366  *
367  * As a precondition, this requires that the entry came from user mode,
368  * idle, or a kernel context in which RCU is watching.
369  *
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.
374  *
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.
378  *
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.
382  *
383  * Returns: An opaque object that must be passed to idtentry_exit()
384  */
385 irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs);
386
387 /**
388  * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt
389  *
390  * Conditional reschedule with additional sanity checks.
391  */
392 void irqentry_exit_cond_resched(void);
393
394 /**
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()
398  *
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.
402  *
403  * This is the last action before returning to the low level ASM code which
404  * just needs to return to the appropriate context.
405  *
406  * Counterpart to irqentry_enter().
407  */
408 void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state);
409
410 #endif