Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_ENTRYCOMMON_H
0003 #define __LINUX_ENTRYCOMMON_H
0004 
0005 #include <linux/static_call_types.h>
0006 #include <linux/ptrace.h>
0007 #include <linux/syscalls.h>
0008 #include <linux/seccomp.h>
0009 #include <linux/sched.h>
0010 
0011 #include <asm/entry-common.h>
0012 
0013 /*
0014  * Define dummy _TIF work flags if not defined by the architecture or for
0015  * disabled functionality.
0016  */
0017 #ifndef _TIF_PATCH_PENDING
0018 # define _TIF_PATCH_PENDING     (0)
0019 #endif
0020 
0021 #ifndef _TIF_UPROBE
0022 # define _TIF_UPROBE            (0)
0023 #endif
0024 
0025 /*
0026  * SYSCALL_WORK flags handled in syscall_enter_from_user_mode()
0027  */
0028 #ifndef ARCH_SYSCALL_WORK_ENTER
0029 # define ARCH_SYSCALL_WORK_ENTER    (0)
0030 #endif
0031 
0032 /*
0033  * SYSCALL_WORK flags handled in syscall_exit_to_user_mode()
0034  */
0035 #ifndef ARCH_SYSCALL_WORK_EXIT
0036 # define ARCH_SYSCALL_WORK_EXIT     (0)
0037 #endif
0038 
0039 #define SYSCALL_WORK_ENTER  (SYSCALL_WORK_SECCOMP |         \
0040                  SYSCALL_WORK_SYSCALL_TRACEPOINT |  \
0041                  SYSCALL_WORK_SYSCALL_TRACE |       \
0042                  SYSCALL_WORK_SYSCALL_EMU |     \
0043                  SYSCALL_WORK_SYSCALL_AUDIT |       \
0044                  SYSCALL_WORK_SYSCALL_USER_DISPATCH |   \
0045                  ARCH_SYSCALL_WORK_ENTER)
0046 #define SYSCALL_WORK_EXIT   (SYSCALL_WORK_SYSCALL_TRACEPOINT |  \
0047                  SYSCALL_WORK_SYSCALL_TRACE |       \
0048                  SYSCALL_WORK_SYSCALL_AUDIT |       \
0049                  SYSCALL_WORK_SYSCALL_USER_DISPATCH |   \
0050                  SYSCALL_WORK_SYSCALL_EXIT_TRAP |   \
0051                  ARCH_SYSCALL_WORK_EXIT)
0052 
0053 /*
0054  * TIF flags handled in exit_to_user_mode_loop()
0055  */
0056 #ifndef ARCH_EXIT_TO_USER_MODE_WORK
0057 # define ARCH_EXIT_TO_USER_MODE_WORK        (0)
0058 #endif
0059 
0060 #define EXIT_TO_USER_MODE_WORK                      \
0061     (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |       \
0062      _TIF_NEED_RESCHED | _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL |  \
0063      ARCH_EXIT_TO_USER_MODE_WORK)
0064 
0065 /**
0066  * arch_enter_from_user_mode - Architecture specific sanity check for user mode regs
0067  * @regs:   Pointer to currents pt_regs
0068  *
0069  * Defaults to an empty implementation. Can be replaced by architecture
0070  * specific code.
0071  *
0072  * Invoked from syscall_enter_from_user_mode() in the non-instrumentable
0073  * section. Use __always_inline so the compiler cannot push it out of line
0074  * and make it instrumentable.
0075  */
0076 static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs);
0077 
0078 #ifndef arch_enter_from_user_mode
0079 static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs) {}
0080 #endif
0081 
0082 /**
0083  * enter_from_user_mode - Establish state when coming from user mode
0084  *
0085  * Syscall/interrupt entry disables interrupts, but user mode is traced as
0086  * interrupts enabled. Also with NO_HZ_FULL RCU might be idle.
0087  *
0088  * 1) Tell lockdep that interrupts are disabled
0089  * 2) Invoke context tracking if enabled to reactivate RCU
0090  * 3) Trace interrupts off state
0091  *
0092  * Invoked from architecture specific syscall entry code with interrupts
0093  * disabled. The calling code has to be non-instrumentable. When the
0094  * function returns all state is correct and interrupts are still
0095  * disabled. The subsequent functions can be instrumented.
0096  *
0097  * This is invoked when there is architecture specific functionality to be
0098  * done between establishing state and enabling interrupts. The caller must
0099  * enable interrupts before invoking syscall_enter_from_user_mode_work().
0100  */
0101 void enter_from_user_mode(struct pt_regs *regs);
0102 
0103 /**
0104  * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
0105  * @regs:   Pointer to currents pt_regs
0106  *
0107  * Invoked from architecture specific syscall entry code with interrupts
0108  * disabled. The calling code has to be non-instrumentable. When the
0109  * function returns all state is correct, interrupts are enabled and the
0110  * subsequent functions can be instrumented.
0111  *
0112  * This handles lockdep, RCU (context tracking) and tracing state, i.e.
0113  * the functionality provided by enter_from_user_mode().
0114  *
0115  * This is invoked when there is extra architecture specific functionality
0116  * to be done between establishing state and handling user mode entry work.
0117  */
0118 void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);
0119 
0120 /**
0121  * syscall_enter_from_user_mode_work - Check and handle work before invoking
0122  *                     a syscall
0123  * @regs:   Pointer to currents pt_regs
0124  * @syscall:    The syscall number
0125  *
0126  * Invoked from architecture specific syscall entry code with interrupts
0127  * enabled after invoking syscall_enter_from_user_mode_prepare() and extra
0128  * architecture specific work.
0129  *
0130  * Returns: The original or a modified syscall number
0131  *
0132  * If the returned syscall number is -1 then the syscall should be
0133  * skipped. In this case the caller may invoke syscall_set_error() or
0134  * syscall_set_return_value() first.  If neither of those are called and -1
0135  * is returned, then the syscall will fail with ENOSYS.
0136  *
0137  * It handles the following work items:
0138  *
0139  *  1) syscall_work flag dependent invocations of
0140  *     ptrace_report_syscall_entry(), __secure_computing(), trace_sys_enter()
0141  *  2) Invocation of audit_syscall_entry()
0142  */
0143 long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall);
0144 
0145 /**
0146  * syscall_enter_from_user_mode - Establish state and check and handle work
0147  *                before invoking a syscall
0148  * @regs:   Pointer to currents pt_regs
0149  * @syscall:    The syscall number
0150  *
0151  * Invoked from architecture specific syscall entry code with interrupts
0152  * disabled. The calling code has to be non-instrumentable. When the
0153  * function returns all state is correct, interrupts are enabled and the
0154  * subsequent functions can be instrumented.
0155  *
0156  * This is combination of syscall_enter_from_user_mode_prepare() and
0157  * syscall_enter_from_user_mode_work().
0158  *
0159  * Returns: The original or a modified syscall number. See
0160  * syscall_enter_from_user_mode_work() for further explanation.
0161  */
0162 long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall);
0163 
0164 /**
0165  * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
0166  * @ti_work:    Cached TIF flags gathered with interrupts disabled
0167  *
0168  * Defaults to local_irq_enable(). Can be supplied by architecture specific
0169  * code.
0170  */
0171 static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
0172 
0173 #ifndef local_irq_enable_exit_to_user
0174 static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
0175 {
0176     local_irq_enable();
0177 }
0178 #endif
0179 
0180 /**
0181  * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
0182  *
0183  * Defaults to local_irq_disable(). Can be supplied by architecture specific
0184  * code.
0185  */
0186 static inline void local_irq_disable_exit_to_user(void);
0187 
0188 #ifndef local_irq_disable_exit_to_user
0189 static inline void local_irq_disable_exit_to_user(void)
0190 {
0191     local_irq_disable();
0192 }
0193 #endif
0194 
0195 /**
0196  * arch_exit_to_user_mode_work - Architecture specific TIF work for exit
0197  *               to user mode.
0198  * @regs:   Pointer to currents pt_regs
0199  * @ti_work:    Cached TIF flags gathered with interrupts disabled
0200  *
0201  * Invoked from exit_to_user_mode_loop() with interrupt enabled
0202  *
0203  * Defaults to NOOP. Can be supplied by architecture specific code.
0204  */
0205 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
0206                            unsigned long ti_work);
0207 
0208 #ifndef arch_exit_to_user_mode_work
0209 static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
0210                            unsigned long ti_work)
0211 {
0212 }
0213 #endif
0214 
0215 /**
0216  * arch_exit_to_user_mode_prepare - Architecture specific preparation for
0217  *                  exit to user mode.
0218  * @regs:   Pointer to currents pt_regs
0219  * @ti_work:    Cached TIF flags gathered with interrupts disabled
0220  *
0221  * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last
0222  * function before return. Defaults to NOOP.
0223  */
0224 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
0225                           unsigned long ti_work);
0226 
0227 #ifndef arch_exit_to_user_mode_prepare
0228 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
0229                           unsigned long ti_work)
0230 {
0231 }
0232 #endif
0233 
0234 /**
0235  * arch_exit_to_user_mode - Architecture specific final work before
0236  *              exit to user mode.
0237  *
0238  * Invoked from exit_to_user_mode() with interrupt disabled as the last
0239  * function before return. Defaults to NOOP.
0240  *
0241  * This needs to be __always_inline because it is non-instrumentable code
0242  * invoked after context tracking switched to user mode.
0243  *
0244  * An architecture implementation must not do anything complex, no locking
0245  * etc. The main purpose is for speculation mitigations.
0246  */
0247 static __always_inline void arch_exit_to_user_mode(void);
0248 
0249 #ifndef arch_exit_to_user_mode
0250 static __always_inline void arch_exit_to_user_mode(void) { }
0251 #endif
0252 
0253 /**
0254  * arch_do_signal_or_restart -  Architecture specific signal delivery function
0255  * @regs:   Pointer to currents pt_regs
0256  * @has_signal: actual signal to handle
0257  *
0258  * Invoked from exit_to_user_mode_loop().
0259  */
0260 void arch_do_signal_or_restart(struct pt_regs *regs);
0261 
0262 /**
0263  * exit_to_user_mode - Fixup state when exiting to user mode
0264  *
0265  * Syscall/interrupt exit enables interrupts, but the kernel state is
0266  * interrupts disabled when this is invoked. Also tell RCU about it.
0267  *
0268  * 1) Trace interrupts on state
0269  * 2) Invoke context tracking if enabled to adjust RCU state
0270  * 3) Invoke architecture specific last minute exit code, e.g. speculation
0271  *    mitigations, etc.: arch_exit_to_user_mode()
0272  * 4) Tell lockdep that interrupts are enabled
0273  *
0274  * Invoked from architecture specific code when syscall_exit_to_user_mode()
0275  * is not suitable as the last step before returning to userspace. Must be
0276  * invoked with interrupts disabled and the caller must be
0277  * non-instrumentable.
0278  * The caller has to invoke syscall_exit_to_user_mode_work() before this.
0279  */
0280 void exit_to_user_mode(void);
0281 
0282 /**
0283  * syscall_exit_to_user_mode_work - Handle work before returning to user mode
0284  * @regs:   Pointer to currents pt_regs
0285  *
0286  * Same as step 1 and 2 of syscall_exit_to_user_mode() but without calling
0287  * exit_to_user_mode() to perform the final transition to user mode.
0288  *
0289  * Calling convention is the same as for syscall_exit_to_user_mode() and it
0290  * returns with all work handled and interrupts disabled. The caller must
0291  * invoke exit_to_user_mode() before actually switching to user mode to
0292  * make the final state transitions. Interrupts must stay disabled between
0293  * return from this function and the invocation of exit_to_user_mode().
0294  */
0295 void syscall_exit_to_user_mode_work(struct pt_regs *regs);
0296 
0297 /**
0298  * syscall_exit_to_user_mode - Handle work before returning to user mode
0299  * @regs:   Pointer to currents pt_regs
0300  *
0301  * Invoked with interrupts enabled and fully valid regs. Returns with all
0302  * work handled, interrupts disabled such that the caller can immediately
0303  * switch to user mode. Called from architecture specific syscall and ret
0304  * from fork code.
0305  *
0306  * The call order is:
0307  *  1) One-time syscall exit work:
0308  *  - rseq syscall exit
0309  *      - audit
0310  *  - syscall tracing
0311  *  - ptrace (single stepping)
0312  *
0313  *  2) Preparatory work
0314  *  - Exit to user mode loop (common TIF handling). Invokes
0315  *    arch_exit_to_user_mode_work() for architecture specific TIF work
0316  *  - Architecture specific one time work arch_exit_to_user_mode_prepare()
0317  *  - Address limit and lockdep checks
0318  *
0319  *  3) Final transition (lockdep, tracing, context tracking, RCU), i.e. the
0320  *     functionality in exit_to_user_mode().
0321  *
0322  * This is a combination of syscall_exit_to_user_mode_work() (1,2) and
0323  * exit_to_user_mode(). This function is preferred unless there is a
0324  * compelling architectural reason to use the separate functions.
0325  */
0326 void syscall_exit_to_user_mode(struct pt_regs *regs);
0327 
0328 /**
0329  * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
0330  * @regs:   Pointer to currents pt_regs
0331  *
0332  * Invoked from architecture specific entry code with interrupts disabled.
0333  * Can only be called when the interrupt entry came from user mode. The
0334  * calling code must be non-instrumentable.  When the function returns all
0335  * state is correct and the subsequent functions can be instrumented.
0336  *
0337  * The function establishes state (lockdep, RCU (context tracking), tracing)
0338  */
0339 void irqentry_enter_from_user_mode(struct pt_regs *regs);
0340 
0341 /**
0342  * irqentry_exit_to_user_mode - Interrupt exit work
0343  * @regs:   Pointer to current's pt_regs
0344  *
0345  * Invoked with interrupts disabled and fully valid regs. Returns with all
0346  * work handled, interrupts disabled such that the caller can immediately
0347  * switch to user mode. Called from architecture specific interrupt
0348  * handling code.
0349  *
0350  * The call order is #2 and #3 as described in syscall_exit_to_user_mode().
0351  * Interrupt exit is not invoking #1 which is the syscall specific one time
0352  * work.
0353  */
0354 void irqentry_exit_to_user_mode(struct pt_regs *regs);
0355 
0356 #ifndef irqentry_state
0357 /**
0358  * struct irqentry_state - Opaque object for exception state storage
0359  * @exit_rcu: Used exclusively in the irqentry_*() calls; signals whether the
0360  *            exit path has to invoke ct_irq_exit().
0361  * @lockdep: Used exclusively in the irqentry_nmi_*() calls; ensures that
0362  *           lockdep state is restored correctly on exit from nmi.
0363  *
0364  * This opaque object is filled in by the irqentry_*_enter() functions and
0365  * must be passed back into the corresponding irqentry_*_exit() functions
0366  * when the exception is complete.
0367  *
0368  * Callers of irqentry_*_[enter|exit]() must consider this structure opaque
0369  * and all members private.  Descriptions of the members are provided to aid in
0370  * the maintenance of the irqentry_*() functions.
0371  */
0372 typedef struct irqentry_state {
0373     union {
0374         bool    exit_rcu;
0375         bool    lockdep;
0376     };
0377 } irqentry_state_t;
0378 #endif
0379 
0380 /**
0381  * irqentry_enter - Handle state tracking on ordinary interrupt entries
0382  * @regs:   Pointer to pt_regs of interrupted context
0383  *
0384  * Invokes:
0385  *  - lockdep irqflag state tracking as low level ASM entry disabled
0386  *    interrupts.
0387  *
0388  *  - Context tracking if the exception hit user mode.
0389  *
0390  *  - The hardirq tracer to keep the state consistent as low level ASM
0391  *    entry disabled interrupts.
0392  *
0393  * As a precondition, this requires that the entry came from user mode,
0394  * idle, or a kernel context in which RCU is watching.
0395  *
0396  * For kernel mode entries RCU handling is done conditional. If RCU is
0397  * watching then the only RCU requirement is to check whether the tick has
0398  * to be restarted. If RCU is not watching then ct_irq_enter() has to be
0399  * invoked on entry and ct_irq_exit() on exit.
0400  *
0401  * Avoiding the ct_irq_enter/exit() calls is an optimization but also
0402  * solves the problem of kernel mode pagefaults which can schedule, which
0403  * is not possible after invoking ct_irq_enter() without undoing it.
0404  *
0405  * For user mode entries irqentry_enter_from_user_mode() is invoked to
0406  * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
0407  * would not be possible.
0408  *
0409  * Returns: An opaque object that must be passed to idtentry_exit()
0410  */
0411 irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs);
0412 
0413 /**
0414  * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt
0415  *
0416  * Conditional reschedule with additional sanity checks.
0417  */
0418 void raw_irqentry_exit_cond_resched(void);
0419 #ifdef CONFIG_PREEMPT_DYNAMIC
0420 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
0421 #define irqentry_exit_cond_resched_dynamic_enabled  raw_irqentry_exit_cond_resched
0422 #define irqentry_exit_cond_resched_dynamic_disabled NULL
0423 DECLARE_STATIC_CALL(irqentry_exit_cond_resched, raw_irqentry_exit_cond_resched);
0424 #define irqentry_exit_cond_resched()    static_call(irqentry_exit_cond_resched)()
0425 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
0426 DECLARE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
0427 void dynamic_irqentry_exit_cond_resched(void);
0428 #define irqentry_exit_cond_resched()    dynamic_irqentry_exit_cond_resched()
0429 #endif
0430 #else /* CONFIG_PREEMPT_DYNAMIC */
0431 #define irqentry_exit_cond_resched()    raw_irqentry_exit_cond_resched()
0432 #endif /* CONFIG_PREEMPT_DYNAMIC */
0433 
0434 /**
0435  * irqentry_exit - Handle return from exception that used irqentry_enter()
0436  * @regs:   Pointer to pt_regs (exception entry regs)
0437  * @state:  Return value from matching call to irqentry_enter()
0438  *
0439  * Depending on the return target (kernel/user) this runs the necessary
0440  * preemption and work checks if possible and required and returns to
0441  * the caller with interrupts disabled and no further work pending.
0442  *
0443  * This is the last action before returning to the low level ASM code which
0444  * just needs to return to the appropriate context.
0445  *
0446  * Counterpart to irqentry_enter().
0447  */
0448 void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state);
0449 
0450 /**
0451  * irqentry_nmi_enter - Handle NMI entry
0452  * @regs:   Pointer to currents pt_regs
0453  *
0454  * Similar to irqentry_enter() but taking care of the NMI constraints.
0455  */
0456 irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs);
0457 
0458 /**
0459  * irqentry_nmi_exit - Handle return from NMI handling
0460  * @regs:   Pointer to pt_regs (NMI entry regs)
0461  * @irq_state:  Return value from matching call to irqentry_nmi_enter()
0462  *
0463  * Last action before returning to the low level assembly code.
0464  *
0465  * Counterpart to irqentry_nmi_enter().
0466  */
0467 void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state);
0468 
0469 #endif