entry-common.h 16 KB

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