Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / include / linux / entry-common.h
blobfc61d0205c97084acc89c8e45e088946f5e6d9b2
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_ENTRYCOMMON_H
3 #define __LINUX_ENTRYCOMMON_H
5 #include <linux/static_call_types.h>
6 #include <linux/ptrace.h>
7 #include <linux/syscalls.h>
8 #include <linux/seccomp.h>
9 #include <linux/sched.h>
10 #include <linux/context_tracking.h>
11 #include <linux/livepatch.h>
12 #include <linux/resume_user_mode.h>
13 #include <linux/tick.h>
14 #include <linux/kmsan.h>
16 #include <asm/entry-common.h>
19 * Define dummy _TIF work flags if not defined by the architecture or for
20 * disabled functionality.
22 #ifndef _TIF_PATCH_PENDING
23 # define _TIF_PATCH_PENDING (0)
24 #endif
26 #ifndef _TIF_UPROBE
27 # define _TIF_UPROBE (0)
28 #endif
31 * SYSCALL_WORK flags handled in syscall_enter_from_user_mode()
33 #ifndef ARCH_SYSCALL_WORK_ENTER
34 # define ARCH_SYSCALL_WORK_ENTER (0)
35 #endif
38 * SYSCALL_WORK flags handled in syscall_exit_to_user_mode()
40 #ifndef ARCH_SYSCALL_WORK_EXIT
41 # define ARCH_SYSCALL_WORK_EXIT (0)
42 #endif
44 #define SYSCALL_WORK_ENTER (SYSCALL_WORK_SECCOMP | \
45 SYSCALL_WORK_SYSCALL_TRACEPOINT | \
46 SYSCALL_WORK_SYSCALL_TRACE | \
47 SYSCALL_WORK_SYSCALL_EMU | \
48 SYSCALL_WORK_SYSCALL_AUDIT | \
49 SYSCALL_WORK_SYSCALL_USER_DISPATCH | \
50 ARCH_SYSCALL_WORK_ENTER)
51 #define SYSCALL_WORK_EXIT (SYSCALL_WORK_SYSCALL_TRACEPOINT | \
52 SYSCALL_WORK_SYSCALL_TRACE | \
53 SYSCALL_WORK_SYSCALL_AUDIT | \
54 SYSCALL_WORK_SYSCALL_USER_DISPATCH | \
55 SYSCALL_WORK_SYSCALL_EXIT_TRAP | \
56 ARCH_SYSCALL_WORK_EXIT)
59 * TIF flags handled in exit_to_user_mode_loop()
61 #ifndef ARCH_EXIT_TO_USER_MODE_WORK
62 # define ARCH_EXIT_TO_USER_MODE_WORK (0)
63 #endif
65 #define EXIT_TO_USER_MODE_WORK \
66 (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
67 _TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY | \
68 _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL | \
69 ARCH_EXIT_TO_USER_MODE_WORK)
71 /**
72 * arch_enter_from_user_mode - Architecture specific sanity check for user mode regs
73 * @regs: Pointer to currents pt_regs
75 * Defaults to an empty implementation. Can be replaced by architecture
76 * specific code.
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.
82 static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs);
84 #ifndef arch_enter_from_user_mode
85 static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs) {}
86 #endif
88 /**
89 * enter_from_user_mode - Establish state when coming from user mode
91 * Syscall/interrupt entry disables interrupts, but user mode is traced as
92 * interrupts enabled. Also with NO_HZ_FULL RCU might be idle.
94 * 1) Tell lockdep that interrupts are disabled
95 * 2) Invoke context tracking if enabled to reactivate RCU
96 * 3) Trace interrupts off state
98 * Invoked from architecture specific syscall entry code with interrupts
99 * disabled. The calling code has to be non-instrumentable. When the
100 * function returns all state is correct and interrupts are still
101 * disabled. The subsequent functions can be instrumented.
103 * This is invoked when there is architecture specific functionality to be
104 * done between establishing state and enabling interrupts. The caller must
105 * enable interrupts before invoking syscall_enter_from_user_mode_work().
107 static __always_inline void enter_from_user_mode(struct pt_regs *regs)
109 arch_enter_from_user_mode(regs);
110 lockdep_hardirqs_off(CALLER_ADDR0);
112 CT_WARN_ON(__ct_state() != CT_STATE_USER);
113 user_exit_irqoff();
115 instrumentation_begin();
116 kmsan_unpoison_entry_regs(regs);
117 trace_hardirqs_off_finish();
118 instrumentation_end();
122 * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
123 * @regs: Pointer to currents pt_regs
125 * Invoked from architecture specific syscall entry code with interrupts
126 * disabled. The calling code has to be non-instrumentable. When the
127 * function returns all state is correct, interrupts are enabled and the
128 * subsequent functions can be instrumented.
130 * This handles lockdep, RCU (context tracking) and tracing state, i.e.
131 * the functionality provided by enter_from_user_mode().
133 * This is invoked when there is extra architecture specific functionality
134 * to be done between establishing state and handling user mode entry work.
136 void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);
138 long syscall_trace_enter(struct pt_regs *regs, long syscall,
139 unsigned long work);
142 * syscall_enter_from_user_mode_work - Check and handle work before invoking
143 * a syscall
144 * @regs: Pointer to currents pt_regs
145 * @syscall: The syscall number
147 * Invoked from architecture specific syscall entry code with interrupts
148 * enabled after invoking syscall_enter_from_user_mode_prepare() and extra
149 * architecture specific work.
151 * Returns: The original or a modified syscall number
153 * If the returned syscall number is -1 then the syscall should be
154 * skipped. In this case the caller may invoke syscall_set_error() or
155 * syscall_set_return_value() first. If neither of those are called and -1
156 * is returned, then the syscall will fail with ENOSYS.
158 * It handles the following work items:
160 * 1) syscall_work flag dependent invocations of
161 * ptrace_report_syscall_entry(), __secure_computing(), trace_sys_enter()
162 * 2) Invocation of audit_syscall_entry()
164 static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall)
166 unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
168 if (work & SYSCALL_WORK_ENTER)
169 syscall = syscall_trace_enter(regs, syscall, work);
171 return syscall;
175 * syscall_enter_from_user_mode - Establish state and check and handle work
176 * before invoking a syscall
177 * @regs: Pointer to currents pt_regs
178 * @syscall: The syscall number
180 * Invoked from architecture specific syscall entry code with interrupts
181 * disabled. The calling code has to be non-instrumentable. When the
182 * function returns all state is correct, interrupts are enabled and the
183 * subsequent functions can be instrumented.
185 * This is combination of syscall_enter_from_user_mode_prepare() and
186 * syscall_enter_from_user_mode_work().
188 * Returns: The original or a modified syscall number. See
189 * syscall_enter_from_user_mode_work() for further explanation.
191 static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall)
193 long ret;
195 enter_from_user_mode(regs);
197 instrumentation_begin();
198 local_irq_enable();
199 ret = syscall_enter_from_user_mode_work(regs, syscall);
200 instrumentation_end();
202 return ret;
206 * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
207 * @ti_work: Cached TIF flags gathered with interrupts disabled
209 * Defaults to local_irq_enable(). Can be supplied by architecture specific
210 * code.
212 static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
214 #ifndef local_irq_enable_exit_to_user
215 static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
217 local_irq_enable();
219 #endif
222 * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
224 * Defaults to local_irq_disable(). Can be supplied by architecture specific
225 * code.
227 static inline void local_irq_disable_exit_to_user(void);
229 #ifndef local_irq_disable_exit_to_user
230 static inline void local_irq_disable_exit_to_user(void)
232 local_irq_disable();
234 #endif
237 * arch_exit_to_user_mode_work - Architecture specific TIF work for exit
238 * to user mode.
239 * @regs: Pointer to currents pt_regs
240 * @ti_work: Cached TIF flags gathered with interrupts disabled
242 * Invoked from exit_to_user_mode_loop() with interrupt enabled
244 * Defaults to NOOP. Can be supplied by architecture specific code.
246 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
247 unsigned long ti_work);
249 #ifndef arch_exit_to_user_mode_work
250 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
251 unsigned long ti_work)
254 #endif
257 * arch_exit_to_user_mode_prepare - Architecture specific preparation for
258 * exit to user mode.
259 * @regs: Pointer to currents pt_regs
260 * @ti_work: Cached TIF flags gathered with interrupts disabled
262 * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last
263 * function before return. Defaults to NOOP.
265 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
266 unsigned long ti_work);
268 #ifndef arch_exit_to_user_mode_prepare
269 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
270 unsigned long ti_work)
273 #endif
276 * arch_exit_to_user_mode - Architecture specific final work before
277 * exit to user mode.
279 * Invoked from exit_to_user_mode() with interrupt disabled as the last
280 * function before return. Defaults to NOOP.
282 * This needs to be __always_inline because it is non-instrumentable code
283 * invoked after context tracking switched to user mode.
285 * An architecture implementation must not do anything complex, no locking
286 * etc. The main purpose is for speculation mitigations.
288 static __always_inline void arch_exit_to_user_mode(void);
290 #ifndef arch_exit_to_user_mode
291 static __always_inline void arch_exit_to_user_mode(void) { }
292 #endif
295 * arch_do_signal_or_restart - Architecture specific signal delivery function
296 * @regs: Pointer to currents pt_regs
298 * Invoked from exit_to_user_mode_loop().
300 void arch_do_signal_or_restart(struct pt_regs *regs);
303 * exit_to_user_mode_loop - do any pending work before leaving to user space
305 unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
306 unsigned long ti_work);
309 * exit_to_user_mode_prepare - call exit_to_user_mode_loop() if required
310 * @regs: Pointer to pt_regs on entry stack
312 * 1) check that interrupts are disabled
313 * 2) call tick_nohz_user_enter_prepare()
314 * 3) call exit_to_user_mode_loop() if any flags from
315 * EXIT_TO_USER_MODE_WORK are set
316 * 4) check that interrupts are still disabled
318 static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs)
320 unsigned long ti_work;
322 lockdep_assert_irqs_disabled();
324 /* Flush pending rcuog wakeup before the last need_resched() check */
325 tick_nohz_user_enter_prepare();
327 ti_work = read_thread_flags();
328 if (unlikely(ti_work & EXIT_TO_USER_MODE_WORK))
329 ti_work = exit_to_user_mode_loop(regs, ti_work);
331 arch_exit_to_user_mode_prepare(regs, ti_work);
333 /* Ensure that kernel state is sane for a return to userspace */
334 kmap_assert_nomap();
335 lockdep_assert_irqs_disabled();
336 lockdep_sys_exit();
340 * exit_to_user_mode - Fixup state when exiting to user mode
342 * Syscall/interrupt exit enables interrupts, but the kernel state is
343 * interrupts disabled when this is invoked. Also tell RCU about it.
345 * 1) Trace interrupts on state
346 * 2) Invoke context tracking if enabled to adjust RCU state
347 * 3) Invoke architecture specific last minute exit code, e.g. speculation
348 * mitigations, etc.: arch_exit_to_user_mode()
349 * 4) Tell lockdep that interrupts are enabled
351 * Invoked from architecture specific code when syscall_exit_to_user_mode()
352 * is not suitable as the last step before returning to userspace. Must be
353 * invoked with interrupts disabled and the caller must be
354 * non-instrumentable.
355 * The caller has to invoke syscall_exit_to_user_mode_work() before this.
357 static __always_inline void exit_to_user_mode(void)
359 instrumentation_begin();
360 trace_hardirqs_on_prepare();
361 lockdep_hardirqs_on_prepare();
362 instrumentation_end();
364 user_enter_irqoff();
365 arch_exit_to_user_mode();
366 lockdep_hardirqs_on(CALLER_ADDR0);
370 * syscall_exit_to_user_mode_work - Handle work before returning to user mode
371 * @regs: Pointer to currents pt_regs
373 * Same as step 1 and 2 of syscall_exit_to_user_mode() but without calling
374 * exit_to_user_mode() to perform the final transition to user mode.
376 * Calling convention is the same as for syscall_exit_to_user_mode() and it
377 * returns with all work handled and interrupts disabled. The caller must
378 * invoke exit_to_user_mode() before actually switching to user mode to
379 * make the final state transitions. Interrupts must stay disabled between
380 * return from this function and the invocation of exit_to_user_mode().
382 void syscall_exit_to_user_mode_work(struct pt_regs *regs);
385 * syscall_exit_to_user_mode - Handle work before returning to user mode
386 * @regs: Pointer to currents pt_regs
388 * Invoked with interrupts enabled and fully valid regs. Returns with all
389 * work handled, interrupts disabled such that the caller can immediately
390 * switch to user mode. Called from architecture specific syscall and ret
391 * from fork code.
393 * The call order is:
394 * 1) One-time syscall exit work:
395 * - rseq syscall exit
396 * - audit
397 * - syscall tracing
398 * - ptrace (single stepping)
400 * 2) Preparatory work
401 * - Exit to user mode loop (common TIF handling). Invokes
402 * arch_exit_to_user_mode_work() for architecture specific TIF work
403 * - Architecture specific one time work arch_exit_to_user_mode_prepare()
404 * - Address limit and lockdep checks
406 * 3) Final transition (lockdep, tracing, context tracking, RCU), i.e. the
407 * functionality in exit_to_user_mode().
409 * This is a combination of syscall_exit_to_user_mode_work() (1,2) and
410 * exit_to_user_mode(). This function is preferred unless there is a
411 * compelling architectural reason to use the separate functions.
413 void syscall_exit_to_user_mode(struct pt_regs *regs);
416 * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
417 * @regs: Pointer to currents pt_regs
419 * Invoked from architecture specific entry code with interrupts disabled.
420 * Can only be called when the interrupt entry came from user mode. The
421 * calling code must be non-instrumentable. When the function returns all
422 * state is correct and the subsequent functions can be instrumented.
424 * The function establishes state (lockdep, RCU (context tracking), tracing)
426 void irqentry_enter_from_user_mode(struct pt_regs *regs);
429 * irqentry_exit_to_user_mode - Interrupt exit work
430 * @regs: Pointer to current's pt_regs
432 * Invoked with interrupts disabled and fully valid regs. Returns with all
433 * work handled, interrupts disabled such that the caller can immediately
434 * switch to user mode. Called from architecture specific interrupt
435 * handling code.
437 * The call order is #2 and #3 as described in syscall_exit_to_user_mode().
438 * Interrupt exit is not invoking #1 which is the syscall specific one time
439 * work.
441 void irqentry_exit_to_user_mode(struct pt_regs *regs);
443 #ifndef irqentry_state
445 * struct irqentry_state - Opaque object for exception state storage
446 * @exit_rcu: Used exclusively in the irqentry_*() calls; signals whether the
447 * exit path has to invoke ct_irq_exit().
448 * @lockdep: Used exclusively in the irqentry_nmi_*() calls; ensures that
449 * lockdep state is restored correctly on exit from nmi.
451 * This opaque object is filled in by the irqentry_*_enter() functions and
452 * must be passed back into the corresponding irqentry_*_exit() functions
453 * when the exception is complete.
455 * Callers of irqentry_*_[enter|exit]() must consider this structure opaque
456 * and all members private. Descriptions of the members are provided to aid in
457 * the maintenance of the irqentry_*() functions.
459 typedef struct irqentry_state {
460 union {
461 bool exit_rcu;
462 bool lockdep;
464 } irqentry_state_t;
465 #endif
468 * irqentry_enter - Handle state tracking on ordinary interrupt entries
469 * @regs: Pointer to pt_regs of interrupted context
471 * Invokes:
472 * - lockdep irqflag state tracking as low level ASM entry disabled
473 * interrupts.
475 * - Context tracking if the exception hit user mode.
477 * - The hardirq tracer to keep the state consistent as low level ASM
478 * entry disabled interrupts.
480 * As a precondition, this requires that the entry came from user mode,
481 * idle, or a kernel context in which RCU is watching.
483 * For kernel mode entries RCU handling is done conditional. If RCU is
484 * watching then the only RCU requirement is to check whether the tick has
485 * to be restarted. If RCU is not watching then ct_irq_enter() has to be
486 * invoked on entry and ct_irq_exit() on exit.
488 * Avoiding the ct_irq_enter/exit() calls is an optimization but also
489 * solves the problem of kernel mode pagefaults which can schedule, which
490 * is not possible after invoking ct_irq_enter() without undoing it.
492 * For user mode entries irqentry_enter_from_user_mode() is invoked to
493 * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
494 * would not be possible.
496 * Returns: An opaque object that must be passed to idtentry_exit()
498 irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs);
501 * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt
503 * Conditional reschedule with additional sanity checks.
505 void raw_irqentry_exit_cond_resched(void);
506 #ifdef CONFIG_PREEMPT_DYNAMIC
507 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
508 #define irqentry_exit_cond_resched_dynamic_enabled raw_irqentry_exit_cond_resched
509 #define irqentry_exit_cond_resched_dynamic_disabled NULL
510 DECLARE_STATIC_CALL(irqentry_exit_cond_resched, raw_irqentry_exit_cond_resched);
511 #define irqentry_exit_cond_resched() static_call(irqentry_exit_cond_resched)()
512 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
513 DECLARE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
514 void dynamic_irqentry_exit_cond_resched(void);
515 #define irqentry_exit_cond_resched() dynamic_irqentry_exit_cond_resched()
516 #endif
517 #else /* CONFIG_PREEMPT_DYNAMIC */
518 #define irqentry_exit_cond_resched() raw_irqentry_exit_cond_resched()
519 #endif /* CONFIG_PREEMPT_DYNAMIC */
522 * irqentry_exit - Handle return from exception that used irqentry_enter()
523 * @regs: Pointer to pt_regs (exception entry regs)
524 * @state: Return value from matching call to irqentry_enter()
526 * Depending on the return target (kernel/user) this runs the necessary
527 * preemption and work checks if possible and required and returns to
528 * the caller with interrupts disabled and no further work pending.
530 * This is the last action before returning to the low level ASM code which
531 * just needs to return to the appropriate context.
533 * Counterpart to irqentry_enter().
535 void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state);
538 * irqentry_nmi_enter - Handle NMI entry
539 * @regs: Pointer to currents pt_regs
541 * Similar to irqentry_enter() but taking care of the NMI constraints.
543 irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs);
546 * irqentry_nmi_exit - Handle return from NMI handling
547 * @regs: Pointer to pt_regs (NMI entry regs)
548 * @irq_state: Return value from matching call to irqentry_nmi_enter()
550 * Last action before returning to the low level assembly code.
552 * Counterpart to irqentry_nmi_enter().
554 void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state);
556 #endif