patch-5.15.79-rt54.patch
[platform/kernel/linux-rpi.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/static_call_types.h>
6 #include <linux/tracehook.h>
7 #include <linux/syscalls.h>
8 #include <linux/seccomp.h>
9 #include <linux/sched.h>
10
11 #include <asm/entry-common.h>
12
13 /*
14  * Define dummy _TIF work flags if not defined by the architecture or for
15  * disabled functionality.
16  */
17 #ifndef _TIF_PATCH_PENDING
18 # define _TIF_PATCH_PENDING             (0)
19 #endif
20
21 #ifndef _TIF_UPROBE
22 # define _TIF_UPROBE                    (0)
23 #endif
24
25 /*
26  * SYSCALL_WORK flags handled in syscall_enter_from_user_mode()
27  */
28 #ifndef ARCH_SYSCALL_WORK_ENTER
29 # define ARCH_SYSCALL_WORK_ENTER        (0)
30 #endif
31
32 /*
33  * SYSCALL_WORK flags handled in syscall_exit_to_user_mode()
34  */
35 #ifndef ARCH_SYSCALL_WORK_EXIT
36 # define ARCH_SYSCALL_WORK_EXIT         (0)
37 #endif
38
39 #define SYSCALL_WORK_ENTER      (SYSCALL_WORK_SECCOMP |                 \
40                                  SYSCALL_WORK_SYSCALL_TRACEPOINT |      \
41                                  SYSCALL_WORK_SYSCALL_TRACE |           \
42                                  SYSCALL_WORK_SYSCALL_EMU |             \
43                                  SYSCALL_WORK_SYSCALL_AUDIT |           \
44                                  SYSCALL_WORK_SYSCALL_USER_DISPATCH |   \
45                                  ARCH_SYSCALL_WORK_ENTER)
46 #define SYSCALL_WORK_EXIT       (SYSCALL_WORK_SYSCALL_TRACEPOINT |      \
47                                  SYSCALL_WORK_SYSCALL_TRACE |           \
48                                  SYSCALL_WORK_SYSCALL_AUDIT |           \
49                                  SYSCALL_WORK_SYSCALL_USER_DISPATCH |   \
50                                  SYSCALL_WORK_SYSCALL_EXIT_TRAP |       \
51                                  ARCH_SYSCALL_WORK_EXIT)
52
53 /*
54  * TIF flags handled in exit_to_user_mode_loop()
55  */
56 #ifndef ARCH_EXIT_TO_USER_MODE_WORK
57 # define ARCH_EXIT_TO_USER_MODE_WORK            (0)
58 #endif
59
60 #ifdef CONFIG_PREEMPT_LAZY
61 # define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
62 #else
63 # define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED)
64 #endif
65
66 #define EXIT_TO_USER_MODE_WORK                                          \
67         (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |           \
68          _TIF_NEED_RESCHED_MASK | _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL |     \
69          ARCH_EXIT_TO_USER_MODE_WORK)
70
71 /**
72  * arch_check_user_regs - Architecture specific sanity check for user mode regs
73  * @regs:       Pointer to currents pt_regs
74  *
75  * Defaults to an empty implementation. Can be replaced by architecture
76  * specific code.
77  *
78  * Invoked from syscall_enter_from_user_mode() in the non-instrumentable
79  * section. Use __always_inline so the compiler cannot push it out of line
80  * and make it instrumentable.
81  */
82 static __always_inline void arch_check_user_regs(struct pt_regs *regs);
83
84 #ifndef arch_check_user_regs
85 static __always_inline void arch_check_user_regs(struct pt_regs *regs) {}
86 #endif
87
88 /**
89  * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry()
90  * @regs:       Pointer to currents pt_regs
91  *
92  * Returns: 0 on success or an error code to skip the syscall.
93  *
94  * Defaults to tracehook_report_syscall_entry(). Can be replaced by
95  * architecture specific code.
96  *
97  * Invoked from syscall_enter_from_user_mode()
98  */
99 static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs);
100
101 #ifndef arch_syscall_enter_tracehook
102 static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs)
103 {
104         return tracehook_report_syscall_entry(regs);
105 }
106 #endif
107
108 /**
109  * enter_from_user_mode - Establish state when coming from user mode
110  *
111  * Syscall/interrupt entry disables interrupts, but user mode is traced as
112  * interrupts enabled. Also with NO_HZ_FULL RCU might be idle.
113  *
114  * 1) Tell lockdep that interrupts are disabled
115  * 2) Invoke context tracking if enabled to reactivate RCU
116  * 3) Trace interrupts off state
117  *
118  * Invoked from architecture specific syscall entry code with interrupts
119  * disabled. The calling code has to be non-instrumentable. When the
120  * function returns all state is correct and interrupts are still
121  * disabled. The subsequent functions can be instrumented.
122  *
123  * This is invoked when there is architecture specific functionality to be
124  * done between establishing state and enabling interrupts. The caller must
125  * enable interrupts before invoking syscall_enter_from_user_mode_work().
126  */
127 void enter_from_user_mode(struct pt_regs *regs);
128
129 /**
130  * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
131  * @regs:       Pointer to currents pt_regs
132  *
133  * Invoked from architecture specific syscall entry code with interrupts
134  * disabled. The calling code has to be non-instrumentable. When the
135  * function returns all state is correct, interrupts are enabled and the
136  * subsequent functions can be instrumented.
137  *
138  * This handles lockdep, RCU (context tracking) and tracing state, i.e.
139  * the functionality provided by enter_from_user_mode().
140  *
141  * This is invoked when there is extra architecture specific functionality
142  * to be done between establishing state and handling user mode entry work.
143  */
144 void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);
145
146 /**
147  * syscall_enter_from_user_mode_work - Check and handle work before invoking
148  *                                     a syscall
149  * @regs:       Pointer to currents pt_regs
150  * @syscall:    The syscall number
151  *
152  * Invoked from architecture specific syscall entry code with interrupts
153  * enabled after invoking syscall_enter_from_user_mode_prepare() and extra
154  * architecture specific work.
155  *
156  * Returns: The original or a modified syscall number
157  *
158  * If the returned syscall number is -1 then the syscall should be
159  * skipped. In this case the caller may invoke syscall_set_error() or
160  * syscall_set_return_value() first.  If neither of those are called and -1
161  * is returned, then the syscall will fail with ENOSYS.
162  *
163  * It handles the following work items:
164  *
165  *  1) syscall_work flag dependent invocations of
166  *     arch_syscall_enter_tracehook(), __secure_computing(), trace_sys_enter()
167  *  2) Invocation of audit_syscall_entry()
168  */
169 long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall);
170
171 /**
172  * syscall_enter_from_user_mode - Establish state and check and handle work
173  *                                before invoking a syscall
174  * @regs:       Pointer to currents pt_regs
175  * @syscall:    The syscall number
176  *
177  * Invoked from architecture specific syscall entry code with interrupts
178  * disabled. The calling code has to be non-instrumentable. When the
179  * function returns all state is correct, interrupts are enabled and the
180  * subsequent functions can be instrumented.
181  *
182  * This is combination of syscall_enter_from_user_mode_prepare() and
183  * syscall_enter_from_user_mode_work().
184  *
185  * Returns: The original or a modified syscall number. See
186  * syscall_enter_from_user_mode_work() for further explanation.
187  */
188 long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall);
189
190 /**
191  * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
192  * @ti_work:    Cached TIF flags gathered with interrupts disabled
193  *
194  * Defaults to local_irq_enable(). Can be supplied by architecture specific
195  * code.
196  */
197 static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
198
199 #ifndef local_irq_enable_exit_to_user
200 static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
201 {
202         local_irq_enable();
203 }
204 #endif
205
206 /**
207  * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
208  *
209  * Defaults to local_irq_disable(). Can be supplied by architecture specific
210  * code.
211  */
212 static inline void local_irq_disable_exit_to_user(void);
213
214 #ifndef local_irq_disable_exit_to_user
215 static inline void local_irq_disable_exit_to_user(void)
216 {
217         local_irq_disable();
218 }
219 #endif
220
221 /**
222  * arch_exit_to_user_mode_work - Architecture specific TIF work for exit
223  *                               to user mode.
224  * @regs:       Pointer to currents pt_regs
225  * @ti_work:    Cached TIF flags gathered with interrupts disabled
226  *
227  * Invoked from exit_to_user_mode_loop() with interrupt enabled
228  *
229  * Defaults to NOOP. Can be supplied by architecture specific code.
230  */
231 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
232                                                unsigned long ti_work);
233
234 #ifndef arch_exit_to_user_mode_work
235 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
236                                                unsigned long ti_work)
237 {
238 }
239 #endif
240
241 /**
242  * arch_exit_to_user_mode_prepare - Architecture specific preparation for
243  *                                  exit to user mode.
244  * @regs:       Pointer to currents pt_regs
245  * @ti_work:    Cached TIF flags gathered with interrupts disabled
246  *
247  * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last
248  * function before return. Defaults to NOOP.
249  */
250 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
251                                                   unsigned long ti_work);
252
253 #ifndef arch_exit_to_user_mode_prepare
254 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
255                                                   unsigned long ti_work)
256 {
257 }
258 #endif
259
260 /**
261  * arch_exit_to_user_mode - Architecture specific final work before
262  *                          exit to user mode.
263  *
264  * Invoked from exit_to_user_mode() with interrupt disabled as the last
265  * function before return. Defaults to NOOP.
266  *
267  * This needs to be __always_inline because it is non-instrumentable code
268  * invoked after context tracking switched to user mode.
269  *
270  * An architecture implementation must not do anything complex, no locking
271  * etc. The main purpose is for speculation mitigations.
272  */
273 static __always_inline void arch_exit_to_user_mode(void);
274
275 #ifndef arch_exit_to_user_mode
276 static __always_inline void arch_exit_to_user_mode(void) { }
277 #endif
278
279 /**
280  * arch_do_signal_or_restart -  Architecture specific signal delivery function
281  * @regs:       Pointer to currents pt_regs
282  * @has_signal: actual signal to handle
283  *
284  * Invoked from exit_to_user_mode_loop().
285  */
286 void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal);
287
288 /**
289  * arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit()
290  * @regs:       Pointer to currents pt_regs
291  * @step:       Indicator for single step
292  *
293  * Defaults to tracehook_report_syscall_exit(). Can be replaced by
294  * architecture specific code.
295  *
296  * Invoked from syscall_exit_to_user_mode()
297  */
298 static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step);
299
300 #ifndef arch_syscall_exit_tracehook
301 static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step)
302 {
303         tracehook_report_syscall_exit(regs, step);
304 }
305 #endif
306
307 /**
308  * exit_to_user_mode - Fixup state when exiting to user mode
309  *
310  * Syscall/interrupt exit enables interrupts, but the kernel state is
311  * interrupts disabled when this is invoked. Also tell RCU about it.
312  *
313  * 1) Trace interrupts on state
314  * 2) Invoke context tracking if enabled to adjust RCU state
315  * 3) Invoke architecture specific last minute exit code, e.g. speculation
316  *    mitigations, etc.: arch_exit_to_user_mode()
317  * 4) Tell lockdep that interrupts are enabled
318  *
319  * Invoked from architecture specific code when syscall_exit_to_user_mode()
320  * is not suitable as the last step before returning to userspace. Must be
321  * invoked with interrupts disabled and the caller must be
322  * non-instrumentable.
323  * The caller has to invoke syscall_exit_to_user_mode_work() before this.
324  */
325 void exit_to_user_mode(void);
326
327 /**
328  * syscall_exit_to_user_mode_work - Handle work before returning to user mode
329  * @regs:       Pointer to currents pt_regs
330  *
331  * Same as step 1 and 2 of syscall_exit_to_user_mode() but without calling
332  * exit_to_user_mode() to perform the final transition to user mode.
333  *
334  * Calling convention is the same as for syscall_exit_to_user_mode() and it
335  * returns with all work handled and interrupts disabled. The caller must
336  * invoke exit_to_user_mode() before actually switching to user mode to
337  * make the final state transitions. Interrupts must stay disabled between
338  * return from this function and the invocation of exit_to_user_mode().
339  */
340 void syscall_exit_to_user_mode_work(struct pt_regs *regs);
341
342 /**
343  * syscall_exit_to_user_mode - Handle work before returning to user mode
344  * @regs:       Pointer to currents pt_regs
345  *
346  * Invoked with interrupts enabled and fully valid regs. Returns with all
347  * work handled, interrupts disabled such that the caller can immediately
348  * switch to user mode. Called from architecture specific syscall and ret
349  * from fork code.
350  *
351  * The call order is:
352  *  1) One-time syscall exit work:
353  *      - rseq syscall exit
354  *      - audit
355  *      - syscall tracing
356  *      - tracehook (single stepping)
357  *
358  *  2) Preparatory work
359  *      - Exit to user mode loop (common TIF handling). Invokes
360  *        arch_exit_to_user_mode_work() for architecture specific TIF work
361  *      - Architecture specific one time work arch_exit_to_user_mode_prepare()
362  *      - Address limit and lockdep checks
363  *
364  *  3) Final transition (lockdep, tracing, context tracking, RCU), i.e. the
365  *     functionality in exit_to_user_mode().
366  *
367  * This is a combination of syscall_exit_to_user_mode_work() (1,2) and
368  * exit_to_user_mode(). This function is preferred unless there is a
369  * compelling architectural reason to use the separate functions.
370  */
371 void syscall_exit_to_user_mode(struct pt_regs *regs);
372
373 /**
374  * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
375  * @regs:       Pointer to currents pt_regs
376  *
377  * Invoked from architecture specific entry code with interrupts disabled.
378  * Can only be called when the interrupt entry came from user mode. The
379  * calling code must be non-instrumentable.  When the function returns all
380  * state is correct and the subsequent functions can be instrumented.
381  *
382  * The function establishes state (lockdep, RCU (context tracking), tracing)
383  */
384 void irqentry_enter_from_user_mode(struct pt_regs *regs);
385
386 /**
387  * irqentry_exit_to_user_mode - Interrupt exit work
388  * @regs:       Pointer to current's pt_regs
389  *
390  * Invoked with interrupts disabled and fully valid regs. Returns with all
391  * work handled, interrupts disabled such that the caller can immediately
392  * switch to user mode. Called from architecture specific interrupt
393  * handling code.
394  *
395  * The call order is #2 and #3 as described in syscall_exit_to_user_mode().
396  * Interrupt exit is not invoking #1 which is the syscall specific one time
397  * work.
398  */
399 void irqentry_exit_to_user_mode(struct pt_regs *regs);
400
401 #ifndef irqentry_state
402 /**
403  * struct irqentry_state - Opaque object for exception state storage
404  * @exit_rcu: Used exclusively in the irqentry_*() calls; signals whether the
405  *            exit path has to invoke rcu_irq_exit().
406  * @lockdep: Used exclusively in the irqentry_nmi_*() calls; ensures that
407  *           lockdep state is restored correctly on exit from nmi.
408  *
409  * This opaque object is filled in by the irqentry_*_enter() functions and
410  * must be passed back into the corresponding irqentry_*_exit() functions
411  * when the exception is complete.
412  *
413  * Callers of irqentry_*_[enter|exit]() must consider this structure opaque
414  * and all members private.  Descriptions of the members are provided to aid in
415  * the maintenance of the irqentry_*() functions.
416  */
417 typedef struct irqentry_state {
418         union {
419                 bool    exit_rcu;
420                 bool    lockdep;
421         };
422 } irqentry_state_t;
423 #endif
424
425 /**
426  * irqentry_enter - Handle state tracking on ordinary interrupt entries
427  * @regs:       Pointer to pt_regs of interrupted context
428  *
429  * Invokes:
430  *  - lockdep irqflag state tracking as low level ASM entry disabled
431  *    interrupts.
432  *
433  *  - Context tracking if the exception hit user mode.
434  *
435  *  - The hardirq tracer to keep the state consistent as low level ASM
436  *    entry disabled interrupts.
437  *
438  * As a precondition, this requires that the entry came from user mode,
439  * idle, or a kernel context in which RCU is watching.
440  *
441  * For kernel mode entries RCU handling is done conditional. If RCU is
442  * watching then the only RCU requirement is to check whether the tick has
443  * to be restarted. If RCU is not watching then rcu_irq_enter() has to be
444  * invoked on entry and rcu_irq_exit() on exit.
445  *
446  * Avoiding the rcu_irq_enter/exit() calls is an optimization but also
447  * solves the problem of kernel mode pagefaults which can schedule, which
448  * is not possible after invoking rcu_irq_enter() without undoing it.
449  *
450  * For user mode entries irqentry_enter_from_user_mode() is invoked to
451  * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
452  * would not be possible.
453  *
454  * Returns: An opaque object that must be passed to idtentry_exit()
455  */
456 irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs);
457
458 /**
459  * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt
460  *
461  * Conditional reschedule with additional sanity checks.
462  */
463 void irqentry_exit_cond_resched(void);
464 #ifdef CONFIG_PREEMPT_DYNAMIC
465 DECLARE_STATIC_CALL(irqentry_exit_cond_resched, irqentry_exit_cond_resched);
466 #endif
467
468 /**
469  * irqentry_exit - Handle return from exception that used irqentry_enter()
470  * @regs:       Pointer to pt_regs (exception entry regs)
471  * @state:      Return value from matching call to irqentry_enter()
472  *
473  * Depending on the return target (kernel/user) this runs the necessary
474  * preemption and work checks if possible and required and returns to
475  * the caller with interrupts disabled and no further work pending.
476  *
477  * This is the last action before returning to the low level ASM code which
478  * just needs to return to the appropriate context.
479  *
480  * Counterpart to irqentry_enter().
481  */
482 void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state);
483
484 /**
485  * irqentry_nmi_enter - Handle NMI entry
486  * @regs:       Pointer to currents pt_regs
487  *
488  * Similar to irqentry_enter() but taking care of the NMI constraints.
489  */
490 irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs);
491
492 /**
493  * irqentry_nmi_exit - Handle return from NMI handling
494  * @regs:       Pointer to pt_regs (NMI entry regs)
495  * @irq_state:  Return value from matching call to irqentry_nmi_enter()
496  *
497  * Last action before returning to the low level assembly code.
498  *
499  * Counterpart to irqentry_nmi_enter().
500  */
501 void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state);
502
503 #endif