xref: /linux/include/linux/entry-common.h (revision 3fd6c59042dbba50391e30862beac979491145fe)
1142781e1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0 */
2142781e1SThomas Gleixner #ifndef __LINUX_ENTRYCOMMON_H
3142781e1SThomas Gleixner #define __LINUX_ENTRYCOMMON_H
4142781e1SThomas Gleixner 
540607ee9SPeter Zijlstra (Intel) #include <linux/static_call_types.h>
6153474baSEric W. Biederman #include <linux/ptrace.h>
7142781e1SThomas Gleixner #include <linux/syscalls.h>
8142781e1SThomas Gleixner #include <linux/seccomp.h>
9142781e1SThomas Gleixner #include <linux/sched.h>
10d6801947SSven Schnelle #include <linux/context_tracking.h>
11d6801947SSven Schnelle #include <linux/livepatch.h>
12d6801947SSven Schnelle #include <linux/resume_user_mode.h>
13d6801947SSven Schnelle #include <linux/tick.h>
14caf4062eSSven Schnelle #include <linux/kmsan.h>
15142781e1SThomas Gleixner 
16142781e1SThomas Gleixner #include <asm/entry-common.h>
17142781e1SThomas Gleixner 
18142781e1SThomas Gleixner /*
19142781e1SThomas Gleixner  * Define dummy _TIF work flags if not defined by the architecture or for
20142781e1SThomas Gleixner  * disabled functionality.
21142781e1SThomas Gleixner  */
22a9f3a74aSThomas Gleixner #ifndef _TIF_PATCH_PENDING
23a9f3a74aSThomas Gleixner # define _TIF_PATCH_PENDING		(0)
24a9f3a74aSThomas Gleixner #endif
25a9f3a74aSThomas Gleixner 
26a9f3a74aSThomas Gleixner #ifndef _TIF_UPROBE
27a9f3a74aSThomas Gleixner # define _TIF_UPROBE			(0)
28a9f3a74aSThomas Gleixner #endif
29a9f3a74aSThomas Gleixner 
30142781e1SThomas Gleixner /*
3129915524SGabriel Krisman Bertazi  * SYSCALL_WORK flags handled in syscall_enter_from_user_mode()
32142781e1SThomas Gleixner  */
3329915524SGabriel Krisman Bertazi #ifndef ARCH_SYSCALL_WORK_ENTER
3429915524SGabriel Krisman Bertazi # define ARCH_SYSCALL_WORK_ENTER	(0)
35142781e1SThomas Gleixner #endif
36142781e1SThomas Gleixner 
37a9f3a74aSThomas Gleixner /*
3829915524SGabriel Krisman Bertazi  * SYSCALL_WORK flags handled in syscall_exit_to_user_mode()
39a9f3a74aSThomas Gleixner  */
4029915524SGabriel Krisman Bertazi #ifndef ARCH_SYSCALL_WORK_EXIT
4129915524SGabriel Krisman Bertazi # define ARCH_SYSCALL_WORK_EXIT		(0)
42a9f3a74aSThomas Gleixner #endif
43a9f3a74aSThomas Gleixner 
44524666cbSGabriel Krisman Bertazi #define SYSCALL_WORK_ENTER	(SYSCALL_WORK_SECCOMP |			\
4564c19ba2SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_TRACEPOINT |	\
4664eb35f7SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_TRACE |		\
47785dc4ebSGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_EMU |		\
4829915524SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_AUDIT |		\
4911894468SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_USER_DISPATCH |	\
5029915524SGabriel Krisman Bertazi 				 ARCH_SYSCALL_WORK_ENTER)
5164c19ba2SGabriel Krisman Bertazi #define SYSCALL_WORK_EXIT	(SYSCALL_WORK_SYSCALL_TRACEPOINT |	\
52785dc4ebSGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_TRACE |		\
5329915524SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_AUDIT |		\
5411894468SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_USER_DISPATCH |	\
556342adcaSGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_EXIT_TRAP	|	\
5629915524SGabriel Krisman Bertazi 				 ARCH_SYSCALL_WORK_EXIT)
57b86678cfSGabriel Krisman Bertazi 
58a9f3a74aSThomas Gleixner /*
59a9f3a74aSThomas Gleixner  * TIF flags handled in exit_to_user_mode_loop()
60a9f3a74aSThomas Gleixner  */
61a9f3a74aSThomas Gleixner #ifndef ARCH_EXIT_TO_USER_MODE_WORK
62a9f3a74aSThomas Gleixner # define ARCH_EXIT_TO_USER_MODE_WORK		(0)
63a9f3a74aSThomas Gleixner #endif
64a9f3a74aSThomas Gleixner 
65a9f3a74aSThomas Gleixner #define EXIT_TO_USER_MODE_WORK						\
66a9f3a74aSThomas Gleixner 	(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |		\
6712db8b69SJens Axboe 	 _TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY |			\
68a9f3a74aSThomas Gleixner 	 _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL |			\
69a9f3a74aSThomas Gleixner 	 ARCH_EXIT_TO_USER_MODE_WORK)
70142781e1SThomas Gleixner 
716d97af48SSven Schnelle /**
72142781e1SThomas Gleixner  * arch_enter_from_user_mode - Architecture specific sanity check for user mode regs
73142781e1SThomas Gleixner  * @regs:	Pointer to currents pt_regs
74142781e1SThomas Gleixner  *
75142781e1SThomas Gleixner  * Defaults to an empty implementation. Can be replaced by architecture
76142781e1SThomas Gleixner  * specific code.
77142781e1SThomas Gleixner  *
78142781e1SThomas Gleixner  * Invoked from syscall_enter_from_user_mode() in the non-instrumentable
79142781e1SThomas Gleixner  * section. Use __always_inline so the compiler cannot push it out of line
80142781e1SThomas Gleixner  * and make it instrumentable.
816d97af48SSven Schnelle  */
82142781e1SThomas Gleixner static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs);
836d97af48SSven Schnelle 
846d97af48SSven Schnelle #ifndef arch_enter_from_user_mode
arch_enter_from_user_mode(struct pt_regs * regs)85142781e1SThomas Gleixner static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs) {}
86142781e1SThomas Gleixner #endif
87142781e1SThomas Gleixner 
8896e2fbccSSven Schnelle /**
8996e2fbccSSven Schnelle  * enter_from_user_mode - Establish state when coming from user mode
9096e2fbccSSven Schnelle  *
9196e2fbccSSven Schnelle  * Syscall/interrupt entry disables interrupts, but user mode is traced as
9296e2fbccSSven Schnelle  * interrupts enabled. Also with NO_HZ_FULL RCU might be idle.
9396e2fbccSSven Schnelle  *
9496e2fbccSSven Schnelle  * 1) Tell lockdep that interrupts are disabled
9596e2fbccSSven Schnelle  * 2) Invoke context tracking if enabled to reactivate RCU
9696e2fbccSSven Schnelle  * 3) Trace interrupts off state
9796e2fbccSSven Schnelle  *
9896e2fbccSSven Schnelle  * Invoked from architecture specific syscall entry code with interrupts
9996e2fbccSSven Schnelle  * disabled. The calling code has to be non-instrumentable. When the
10096e2fbccSSven Schnelle  * function returns all state is correct and interrupts are still
10196e2fbccSSven Schnelle  * disabled. The subsequent functions can be instrumented.
10296e2fbccSSven Schnelle  *
10396e2fbccSSven Schnelle  * This is invoked when there is architecture specific functionality to be
10496e2fbccSSven Schnelle  * done between establishing state and enabling interrupts. The caller must
10596e2fbccSSven Schnelle  * enable interrupts before invoking syscall_enter_from_user_mode_work().
106caf4062eSSven Schnelle  */
enter_from_user_mode(struct pt_regs * regs)107caf4062eSSven Schnelle static __always_inline void enter_from_user_mode(struct pt_regs *regs)
108caf4062eSSven Schnelle {
109caf4062eSSven Schnelle 	arch_enter_from_user_mode(regs);
110caf4062eSSven Schnelle 	lockdep_hardirqs_off(CALLER_ADDR0);
111*d65d411cSValentin Schneider 
112caf4062eSSven Schnelle 	CT_WARN_ON(__ct_state() != CT_STATE_USER);
113caf4062eSSven Schnelle 	user_exit_irqoff();
114caf4062eSSven Schnelle 
115caf4062eSSven Schnelle 	instrumentation_begin();
116caf4062eSSven Schnelle 	kmsan_unpoison_entry_regs(regs);
117caf4062eSSven Schnelle 	trace_hardirqs_off_finish();
118caf4062eSSven Schnelle 	instrumentation_end();
11996e2fbccSSven Schnelle }
12096e2fbccSSven Schnelle 
1214facb95bSThomas Gleixner /**
1224facb95bSThomas Gleixner  * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
1234facb95bSThomas Gleixner  * @regs:	Pointer to currents pt_regs
1244facb95bSThomas Gleixner  *
1254facb95bSThomas Gleixner  * Invoked from architecture specific syscall entry code with interrupts
1264facb95bSThomas Gleixner  * disabled. The calling code has to be non-instrumentable. When the
1274facb95bSThomas Gleixner  * function returns all state is correct, interrupts are enabled and the
1284facb95bSThomas Gleixner  * subsequent functions can be instrumented.
12996e2fbccSSven Schnelle  *
13096e2fbccSSven Schnelle  * This handles lockdep, RCU (context tracking) and tracing state, i.e.
1314facb95bSThomas Gleixner  * the functionality provided by enter_from_user_mode().
1324facb95bSThomas Gleixner  *
1334facb95bSThomas Gleixner  * This is invoked when there is extra architecture specific functionality
1344facb95bSThomas Gleixner  * to be done between establishing state and handling user mode entry work.
1354facb95bSThomas Gleixner  */
1364facb95bSThomas Gleixner void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);
137221a1640SSven Schnelle 
138221a1640SSven Schnelle long syscall_trace_enter(struct pt_regs *regs, long syscall,
139221a1640SSven Schnelle 			 unsigned long work);
1404facb95bSThomas Gleixner 
1414facb95bSThomas Gleixner /**
142142781e1SThomas Gleixner  * syscall_enter_from_user_mode_work - Check and handle work before invoking
143142781e1SThomas Gleixner  *				       a syscall
144142781e1SThomas Gleixner  * @regs:	Pointer to currents pt_regs
145142781e1SThomas Gleixner  * @syscall:	The syscall number
146142781e1SThomas Gleixner  *
1474facb95bSThomas Gleixner  * Invoked from architecture specific syscall entry code with interrupts
1484facb95bSThomas Gleixner  * enabled after invoking syscall_enter_from_user_mode_prepare() and extra
149142781e1SThomas Gleixner  * architecture specific work.
150142781e1SThomas Gleixner  *
151142781e1SThomas Gleixner  * Returns: The original or a modified syscall number
152142781e1SThomas Gleixner  *
153142781e1SThomas Gleixner  * If the returned syscall number is -1 then the syscall should be
154142781e1SThomas Gleixner  * skipped. In this case the caller may invoke syscall_set_error() or
155142781e1SThomas Gleixner  * syscall_set_return_value() first.  If neither of those are called and -1
156142781e1SThomas Gleixner  * is returned, then the syscall will fail with ENOSYS.
1574facb95bSThomas Gleixner  *
158142781e1SThomas Gleixner  * It handles the following work items:
15929915524SGabriel Krisman Bertazi  *
1600cfcb2b9SEric W. Biederman  *  1) syscall_work flag dependent invocations of
1614facb95bSThomas Gleixner  *     ptrace_report_syscall_entry(), __secure_computing(), trace_sys_enter()
1624facb95bSThomas Gleixner  *  2) Invocation of audit_syscall_entry()
163221a1640SSven Schnelle  */
syscall_enter_from_user_mode_work(struct pt_regs * regs,long syscall)164221a1640SSven Schnelle static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall)
165221a1640SSven Schnelle {
166221a1640SSven Schnelle 	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
167221a1640SSven Schnelle 
168221a1640SSven Schnelle 	if (work & SYSCALL_WORK_ENTER)
169221a1640SSven Schnelle 		syscall = syscall_trace_enter(regs, syscall, work);
170221a1640SSven Schnelle 
171221a1640SSven Schnelle 	return syscall;
1724facb95bSThomas Gleixner }
1734facb95bSThomas Gleixner 
1744facb95bSThomas Gleixner /**
1754facb95bSThomas Gleixner  * syscall_enter_from_user_mode - Establish state and check and handle work
1764facb95bSThomas Gleixner  *				  before invoking a syscall
1774facb95bSThomas Gleixner  * @regs:	Pointer to currents pt_regs
1784facb95bSThomas Gleixner  * @syscall:	The syscall number
1794facb95bSThomas Gleixner  *
1804facb95bSThomas Gleixner  * Invoked from architecture specific syscall entry code with interrupts
1814facb95bSThomas Gleixner  * disabled. The calling code has to be non-instrumentable. When the
1824facb95bSThomas Gleixner  * function returns all state is correct, interrupts are enabled and the
1834facb95bSThomas Gleixner  * subsequent functions can be instrumented.
1844facb95bSThomas Gleixner  *
1854facb95bSThomas Gleixner  * This is combination of syscall_enter_from_user_mode_prepare() and
1864facb95bSThomas Gleixner  * syscall_enter_from_user_mode_work().
1874facb95bSThomas Gleixner  *
1884facb95bSThomas Gleixner  * Returns: The original or a modified syscall number. See
189142781e1SThomas Gleixner  * syscall_enter_from_user_mode_work() for further explanation.
190221a1640SSven Schnelle  */
syscall_enter_from_user_mode(struct pt_regs * regs,long syscall)191221a1640SSven Schnelle static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall)
192221a1640SSven Schnelle {
193221a1640SSven Schnelle 	long ret;
194221a1640SSven Schnelle 
195221a1640SSven Schnelle 	enter_from_user_mode(regs);
196221a1640SSven Schnelle 
197221a1640SSven Schnelle 	instrumentation_begin();
198221a1640SSven Schnelle 	local_irq_enable();
199221a1640SSven Schnelle 	ret = syscall_enter_from_user_mode_work(regs, syscall);
200221a1640SSven Schnelle 	instrumentation_end();
201221a1640SSven Schnelle 
202221a1640SSven Schnelle 	return ret;
203142781e1SThomas Gleixner }
204142781e1SThomas Gleixner 
205a9f3a74aSThomas Gleixner /**
206a9f3a74aSThomas Gleixner  * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
207a9f3a74aSThomas Gleixner  * @ti_work:	Cached TIF flags gathered with interrupts disabled
208a9f3a74aSThomas Gleixner  *
209a9f3a74aSThomas Gleixner  * Defaults to local_irq_enable(). Can be supplied by architecture specific
210a9f3a74aSThomas Gleixner  * code.
211a9f3a74aSThomas Gleixner  */
212a9f3a74aSThomas Gleixner static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
213a9f3a74aSThomas Gleixner 
214a9f3a74aSThomas Gleixner #ifndef local_irq_enable_exit_to_user
local_irq_enable_exit_to_user(unsigned long ti_work)215a9f3a74aSThomas Gleixner static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
216a9f3a74aSThomas Gleixner {
217a9f3a74aSThomas Gleixner 	local_irq_enable();
218a9f3a74aSThomas Gleixner }
219a9f3a74aSThomas Gleixner #endif
220a9f3a74aSThomas Gleixner 
221a9f3a74aSThomas Gleixner /**
222a9f3a74aSThomas Gleixner  * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
223a9f3a74aSThomas Gleixner  *
224a9f3a74aSThomas Gleixner  * Defaults to local_irq_disable(). Can be supplied by architecture specific
225a9f3a74aSThomas Gleixner  * code.
226a9f3a74aSThomas Gleixner  */
227a9f3a74aSThomas Gleixner static inline void local_irq_disable_exit_to_user(void);
228a9f3a74aSThomas Gleixner 
229a9f3a74aSThomas Gleixner #ifndef local_irq_disable_exit_to_user
local_irq_disable_exit_to_user(void)230a9f3a74aSThomas Gleixner static inline void local_irq_disable_exit_to_user(void)
231a9f3a74aSThomas Gleixner {
232a9f3a74aSThomas Gleixner 	local_irq_disable();
233a9f3a74aSThomas Gleixner }
234a9f3a74aSThomas Gleixner #endif
235a9f3a74aSThomas Gleixner 
236a9f3a74aSThomas Gleixner /**
237a9f3a74aSThomas Gleixner  * arch_exit_to_user_mode_work - Architecture specific TIF work for exit
238a9f3a74aSThomas Gleixner  *				 to user mode.
239a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
240a9f3a74aSThomas Gleixner  * @ti_work:	Cached TIF flags gathered with interrupts disabled
241a9f3a74aSThomas Gleixner  *
242a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode_loop() with interrupt enabled
243a9f3a74aSThomas Gleixner  *
244a9f3a74aSThomas Gleixner  * Defaults to NOOP. Can be supplied by architecture specific code.
245a9f3a74aSThomas Gleixner  */
246a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
247a9f3a74aSThomas Gleixner 					       unsigned long ti_work);
248a9f3a74aSThomas Gleixner 
249a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode_work
arch_exit_to_user_mode_work(struct pt_regs * regs,unsigned long ti_work)250a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
251a9f3a74aSThomas Gleixner 					       unsigned long ti_work)
252a9f3a74aSThomas Gleixner {
253a9f3a74aSThomas Gleixner }
254a9f3a74aSThomas Gleixner #endif
255a9f3a74aSThomas Gleixner 
256a9f3a74aSThomas Gleixner /**
257a9f3a74aSThomas Gleixner  * arch_exit_to_user_mode_prepare - Architecture specific preparation for
258a9f3a74aSThomas Gleixner  *				    exit to user mode.
259a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
260a9f3a74aSThomas Gleixner  * @ti_work:	Cached TIF flags gathered with interrupts disabled
261a9f3a74aSThomas Gleixner  *
262a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last
263a9f3a74aSThomas Gleixner  * function before return. Defaults to NOOP.
264a9f3a74aSThomas Gleixner  */
265a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
266a9f3a74aSThomas Gleixner 						  unsigned long ti_work);
267a9f3a74aSThomas Gleixner 
268a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode_prepare
arch_exit_to_user_mode_prepare(struct pt_regs * regs,unsigned long ti_work)269a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
270a9f3a74aSThomas Gleixner 						  unsigned long ti_work)
271a9f3a74aSThomas Gleixner {
272a9f3a74aSThomas Gleixner }
273a9f3a74aSThomas Gleixner #endif
274a9f3a74aSThomas Gleixner 
275a9f3a74aSThomas Gleixner /**
276a9f3a74aSThomas Gleixner  * arch_exit_to_user_mode - Architecture specific final work before
277a9f3a74aSThomas Gleixner  *			    exit to user mode.
278a9f3a74aSThomas Gleixner  *
279a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode() with interrupt disabled as the last
280a9f3a74aSThomas Gleixner  * function before return. Defaults to NOOP.
281a9f3a74aSThomas Gleixner  *
282a9f3a74aSThomas Gleixner  * This needs to be __always_inline because it is non-instrumentable code
283a9f3a74aSThomas Gleixner  * invoked after context tracking switched to user mode.
284a9f3a74aSThomas Gleixner  *
285a9f3a74aSThomas Gleixner  * An architecture implementation must not do anything complex, no locking
286a9f3a74aSThomas Gleixner  * etc. The main purpose is for speculation mitigations.
287a9f3a74aSThomas Gleixner  */
288a9f3a74aSThomas Gleixner static __always_inline void arch_exit_to_user_mode(void);
289a9f3a74aSThomas Gleixner 
290a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode
arch_exit_to_user_mode(void)291a9f3a74aSThomas Gleixner static __always_inline void arch_exit_to_user_mode(void) { }
292a9f3a74aSThomas Gleixner #endif
293a9f3a74aSThomas Gleixner 
29412db8b69SJens Axboe /**
295a9f3a74aSThomas Gleixner  * arch_do_signal_or_restart -  Architecture specific signal delivery function
296a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
297a9f3a74aSThomas Gleixner  *
298a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode_loop().
2998ba62d37SEric W. Biederman  */
300a9f3a74aSThomas Gleixner void arch_do_signal_or_restart(struct pt_regs *regs);
301a9f3a74aSThomas Gleixner 
302d6801947SSven Schnelle /**
303d6801947SSven Schnelle  * exit_to_user_mode_loop - do any pending work before leaving to user space
304d6801947SSven Schnelle  */
305d6801947SSven Schnelle unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
306d6801947SSven Schnelle 				     unsigned long ti_work);
307d6801947SSven Schnelle 
308d6801947SSven Schnelle /**
309d6801947SSven Schnelle  * exit_to_user_mode_prepare - call exit_to_user_mode_loop() if required
310d6801947SSven Schnelle  * @regs:	Pointer to pt_regs on entry stack
311d6801947SSven Schnelle  *
312d6801947SSven Schnelle  * 1) check that interrupts are disabled
313d6801947SSven Schnelle  * 2) call tick_nohz_user_enter_prepare()
314d6801947SSven Schnelle  * 3) call exit_to_user_mode_loop() if any flags from
315d6801947SSven Schnelle  *    EXIT_TO_USER_MODE_WORK are set
316d6801947SSven Schnelle  * 4) check that interrupts are still disabled
317d6801947SSven Schnelle  */
exit_to_user_mode_prepare(struct pt_regs * regs)318d6801947SSven Schnelle static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs)
319d6801947SSven Schnelle {
320d6801947SSven Schnelle 	unsigned long ti_work;
321d6801947SSven Schnelle 
322d6801947SSven Schnelle 	lockdep_assert_irqs_disabled();
323d6801947SSven Schnelle 
324d6801947SSven Schnelle 	/* Flush pending rcuog wakeup before the last need_resched() check */
325d6801947SSven Schnelle 	tick_nohz_user_enter_prepare();
326d6801947SSven Schnelle 
327d6801947SSven Schnelle 	ti_work = read_thread_flags();
328d6801947SSven Schnelle 	if (unlikely(ti_work & EXIT_TO_USER_MODE_WORK))
329d6801947SSven Schnelle 		ti_work = exit_to_user_mode_loop(regs, ti_work);
330d6801947SSven Schnelle 
331d6801947SSven Schnelle 	arch_exit_to_user_mode_prepare(regs, ti_work);
332d6801947SSven Schnelle 
333d6801947SSven Schnelle 	/* Ensure that kernel state is sane for a return to userspace */
334d6801947SSven Schnelle 	kmap_assert_nomap();
335d6801947SSven Schnelle 	lockdep_assert_irqs_disabled();
336d6801947SSven Schnelle 	lockdep_sys_exit();
337d6801947SSven Schnelle }
338d6801947SSven Schnelle 
339310de1a6SSven Schnelle /**
340310de1a6SSven Schnelle  * exit_to_user_mode - Fixup state when exiting to user mode
341310de1a6SSven Schnelle  *
342310de1a6SSven Schnelle  * Syscall/interrupt exit enables interrupts, but the kernel state is
343310de1a6SSven Schnelle  * interrupts disabled when this is invoked. Also tell RCU about it.
344310de1a6SSven Schnelle  *
345310de1a6SSven Schnelle  * 1) Trace interrupts on state
346310de1a6SSven Schnelle  * 2) Invoke context tracking if enabled to adjust RCU state
347310de1a6SSven Schnelle  * 3) Invoke architecture specific last minute exit code, e.g. speculation
348310de1a6SSven Schnelle  *    mitigations, etc.: arch_exit_to_user_mode()
349310de1a6SSven Schnelle  * 4) Tell lockdep that interrupts are enabled
350310de1a6SSven Schnelle  *
351310de1a6SSven Schnelle  * Invoked from architecture specific code when syscall_exit_to_user_mode()
352310de1a6SSven Schnelle  * is not suitable as the last step before returning to userspace. Must be
353310de1a6SSven Schnelle  * invoked with interrupts disabled and the caller must be
354c6156e1dSSven Schnelle  * non-instrumentable.
355310de1a6SSven Schnelle  * The caller has to invoke syscall_exit_to_user_mode_work() before this.
356d6801947SSven Schnelle  */
exit_to_user_mode(void)357d6801947SSven Schnelle static __always_inline void exit_to_user_mode(void)
358d6801947SSven Schnelle {
359d6801947SSven Schnelle 	instrumentation_begin();
360d6801947SSven Schnelle 	trace_hardirqs_on_prepare();
361d6801947SSven Schnelle 	lockdep_hardirqs_on_prepare();
362d6801947SSven Schnelle 	instrumentation_end();
363d6801947SSven Schnelle 
364d6801947SSven Schnelle 	user_enter_irqoff();
365d6801947SSven Schnelle 	arch_exit_to_user_mode();
366d6801947SSven Schnelle 	lockdep_hardirqs_on(CALLER_ADDR0);
367310de1a6SSven Schnelle }
368310de1a6SSven Schnelle 
369c6156e1dSSven Schnelle /**
370c6156e1dSSven Schnelle  * syscall_exit_to_user_mode_work - Handle work before returning to user mode
371c6156e1dSSven Schnelle  * @regs:	Pointer to currents pt_regs
372c6156e1dSSven Schnelle  *
373c6156e1dSSven Schnelle  * Same as step 1 and 2 of syscall_exit_to_user_mode() but without calling
374c6156e1dSSven Schnelle  * exit_to_user_mode() to perform the final transition to user mode.
375c6156e1dSSven Schnelle  *
376c6156e1dSSven Schnelle  * Calling convention is the same as for syscall_exit_to_user_mode() and it
377c6156e1dSSven Schnelle  * returns with all work handled and interrupts disabled. The caller must
378c6156e1dSSven Schnelle  * invoke exit_to_user_mode() before actually switching to user mode to
379c6156e1dSSven Schnelle  * make the final state transitions. Interrupts must stay disabled between
380c6156e1dSSven Schnelle  * return from this function and the invocation of exit_to_user_mode().
381c6156e1dSSven Schnelle  */
382c6156e1dSSven Schnelle void syscall_exit_to_user_mode_work(struct pt_regs *regs);
383c6156e1dSSven Schnelle 
384a9f3a74aSThomas Gleixner /**
385a9f3a74aSThomas Gleixner  * syscall_exit_to_user_mode - Handle work before returning to user mode
386a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
387a9f3a74aSThomas Gleixner  *
388a9f3a74aSThomas Gleixner  * Invoked with interrupts enabled and fully valid regs. Returns with all
389a9f3a74aSThomas Gleixner  * work handled, interrupts disabled such that the caller can immediately
390a9f3a74aSThomas Gleixner  * switch to user mode. Called from architecture specific syscall and ret
391a9f3a74aSThomas Gleixner  * from fork code.
392a9f3a74aSThomas Gleixner  *
393a9f3a74aSThomas Gleixner  * The call order is:
394a9f3a74aSThomas Gleixner  *  1) One-time syscall exit work:
395a9f3a74aSThomas Gleixner  *	- rseq syscall exit
396a9f3a74aSThomas Gleixner  *      - audit
3970cfcb2b9SEric W. Biederman  *	- syscall tracing
398a9f3a74aSThomas Gleixner  *	- ptrace (single stepping)
399a9f3a74aSThomas Gleixner  *
400a9f3a74aSThomas Gleixner  *  2) Preparatory work
401a9f3a74aSThomas Gleixner  *	- Exit to user mode loop (common TIF handling). Invokes
402a9f3a74aSThomas Gleixner  *	  arch_exit_to_user_mode_work() for architecture specific TIF work
403a9f3a74aSThomas Gleixner  *	- Architecture specific one time work arch_exit_to_user_mode_prepare()
404a9f3a74aSThomas Gleixner  *	- Address limit and lockdep checks
405310de1a6SSven Schnelle  *
406310de1a6SSven Schnelle  *  3) Final transition (lockdep, tracing, context tracking, RCU), i.e. the
407c6156e1dSSven Schnelle  *     functionality in exit_to_user_mode().
408c6156e1dSSven Schnelle  *
409c6156e1dSSven Schnelle  * This is a combination of syscall_exit_to_user_mode_work() (1,2) and
41097258ce9SIngo Molnar  * exit_to_user_mode(). This function is preferred unless there is a
411a9f3a74aSThomas Gleixner  * compelling architectural reason to use the separate functions.
412a9f3a74aSThomas Gleixner  */
413a9f3a74aSThomas Gleixner void syscall_exit_to_user_mode(struct pt_regs *regs);
414a9f3a74aSThomas Gleixner 
415142781e1SThomas Gleixner /**
416142781e1SThomas Gleixner  * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
417142781e1SThomas Gleixner  * @regs:	Pointer to currents pt_regs
418142781e1SThomas Gleixner  *
419142781e1SThomas Gleixner  * Invoked from architecture specific entry code with interrupts disabled.
420142781e1SThomas Gleixner  * Can only be called when the interrupt entry came from user mode. The
421142781e1SThomas Gleixner  * calling code must be non-instrumentable.  When the function returns all
422142781e1SThomas Gleixner  * state is correct and the subsequent functions can be instrumented.
423142781e1SThomas Gleixner  *
424142781e1SThomas Gleixner  * The function establishes state (lockdep, RCU (context tracking), tracing)
425142781e1SThomas Gleixner  */
426142781e1SThomas Gleixner void irqentry_enter_from_user_mode(struct pt_regs *regs);
427a9f3a74aSThomas Gleixner 
428a9f3a74aSThomas Gleixner /**
429a9f3a74aSThomas Gleixner  * irqentry_exit_to_user_mode - Interrupt exit work
430a9f3a74aSThomas Gleixner  * @regs:	Pointer to current's pt_regs
43197258ce9SIngo Molnar  *
432a9f3a74aSThomas Gleixner  * Invoked with interrupts disabled and fully valid regs. Returns with all
433a9f3a74aSThomas Gleixner  * work handled, interrupts disabled such that the caller can immediately
434a9f3a74aSThomas Gleixner  * switch to user mode. Called from architecture specific interrupt
435a9f3a74aSThomas Gleixner  * handling code.
436a9f3a74aSThomas Gleixner  *
437a9f3a74aSThomas Gleixner  * The call order is #2 and #3 as described in syscall_exit_to_user_mode().
438a9f3a74aSThomas Gleixner  * Interrupt exit is not invoking #1 which is the syscall specific one time
439a9f3a74aSThomas Gleixner  * work.
440a9f3a74aSThomas Gleixner  */
441a9f3a74aSThomas Gleixner void irqentry_exit_to_user_mode(struct pt_regs *regs);
442a5497babSThomas Gleixner 
443b6be002bSThomas Gleixner #ifndef irqentry_state
444b6be002bSThomas Gleixner /**
445b6be002bSThomas Gleixner  * struct irqentry_state - Opaque object for exception state storage
4466f0e6c15SFrederic Weisbecker  * @exit_rcu: Used exclusively in the irqentry_*() calls; signals whether the
447b6be002bSThomas Gleixner  *            exit path has to invoke ct_irq_exit().
448b6be002bSThomas Gleixner  * @lockdep: Used exclusively in the irqentry_nmi_*() calls; ensures that
449b6be002bSThomas Gleixner  *           lockdep state is restored correctly on exit from nmi.
450b6be002bSThomas Gleixner  *
451b6be002bSThomas Gleixner  * This opaque object is filled in by the irqentry_*_enter() functions and
452b6be002bSThomas Gleixner  * must be passed back into the corresponding irqentry_*_exit() functions
453b6be002bSThomas Gleixner  * when the exception is complete.
454b6be002bSThomas Gleixner  *
455b6be002bSThomas Gleixner  * Callers of irqentry_*_[enter|exit]() must consider this structure opaque
456b6be002bSThomas Gleixner  * and all members private.  Descriptions of the members are provided to aid in
457b6be002bSThomas Gleixner  * the maintenance of the irqentry_*() functions.
458a5497babSThomas Gleixner  */
459b6be002bSThomas Gleixner typedef struct irqentry_state {
460a5497babSThomas Gleixner 	union {
461b6be002bSThomas Gleixner 		bool	exit_rcu;
462b6be002bSThomas Gleixner 		bool	lockdep;
463a5497babSThomas Gleixner 	};
464a5497babSThomas Gleixner } irqentry_state_t;
465a5497babSThomas Gleixner #endif
466a5497babSThomas Gleixner 
467a5497babSThomas Gleixner /**
468a5497babSThomas Gleixner  * irqentry_enter - Handle state tracking on ordinary interrupt entries
469a5497babSThomas Gleixner  * @regs:	Pointer to pt_regs of interrupted context
470a5497babSThomas Gleixner  *
471a5497babSThomas Gleixner  * Invokes:
472a5497babSThomas Gleixner  *  - lockdep irqflag state tracking as low level ASM entry disabled
473a5497babSThomas Gleixner  *    interrupts.
474a5497babSThomas Gleixner  *
475a5497babSThomas Gleixner  *  - Context tracking if the exception hit user mode.
476a5497babSThomas Gleixner  *
477a5497babSThomas Gleixner  *  - The hardirq tracer to keep the state consistent as low level ASM
478a5497babSThomas Gleixner  *    entry disabled interrupts.
479a5497babSThomas Gleixner  *
480a5497babSThomas Gleixner  * As a precondition, this requires that the entry came from user mode,
481a5497babSThomas Gleixner  * idle, or a kernel context in which RCU is watching.
482a5497babSThomas Gleixner  *
483a5497babSThomas Gleixner  * For kernel mode entries RCU handling is done conditional. If RCU is
4846f0e6c15SFrederic Weisbecker  * watching then the only RCU requirement is to check whether the tick has
4856f0e6c15SFrederic Weisbecker  * to be restarted. If RCU is not watching then ct_irq_enter() has to be
486a5497babSThomas Gleixner  * invoked on entry and ct_irq_exit() on exit.
4876f0e6c15SFrederic Weisbecker  *
488a5497babSThomas Gleixner  * Avoiding the ct_irq_enter/exit() calls is an optimization but also
4896f0e6c15SFrederic Weisbecker  * solves the problem of kernel mode pagefaults which can schedule, which
490a5497babSThomas Gleixner  * is not possible after invoking ct_irq_enter() without undoing it.
491a5497babSThomas Gleixner  *
492a5497babSThomas Gleixner  * For user mode entries irqentry_enter_from_user_mode() is invoked to
493a5497babSThomas Gleixner  * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
494a5497babSThomas Gleixner  * would not be possible.
495a5497babSThomas Gleixner  *
496a5497babSThomas Gleixner  * Returns: An opaque object that must be passed to idtentry_exit()
497a5497babSThomas Gleixner  */
498a5497babSThomas Gleixner irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs);
499a5497babSThomas Gleixner 
500a5497babSThomas Gleixner /**
501a5497babSThomas Gleixner  * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt
502a5497babSThomas Gleixner  *
503a5497babSThomas Gleixner  * Conditional reschedule with additional sanity checks.
5044624a14fSMark Rutland  */
50540607ee9SPeter Zijlstra (Intel) void raw_irqentry_exit_cond_resched(void);
50699cf983cSMark Rutland #ifdef CONFIG_PREEMPT_DYNAMIC
5074624a14fSMark Rutland #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
5088a69fe0bSMark Rutland #define irqentry_exit_cond_resched_dynamic_enabled	raw_irqentry_exit_cond_resched
5094624a14fSMark Rutland #define irqentry_exit_cond_resched_dynamic_disabled	NULL
5104624a14fSMark Rutland DECLARE_STATIC_CALL(irqentry_exit_cond_resched, raw_irqentry_exit_cond_resched);
51199cf983cSMark Rutland #define irqentry_exit_cond_resched()	static_call(irqentry_exit_cond_resched)()
51299cf983cSMark Rutland #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
51399cf983cSMark Rutland DECLARE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
51499cf983cSMark Rutland void dynamic_irqentry_exit_cond_resched(void);
51540607ee9SPeter Zijlstra (Intel) #define irqentry_exit_cond_resched()	dynamic_irqentry_exit_cond_resched()
51699cf983cSMark Rutland #endif
51799cf983cSMark Rutland #else /* CONFIG_PREEMPT_DYNAMIC */
51899cf983cSMark Rutland #define irqentry_exit_cond_resched()	raw_irqentry_exit_cond_resched()
519a5497babSThomas Gleixner #endif /* CONFIG_PREEMPT_DYNAMIC */
520a5497babSThomas Gleixner 
521a5497babSThomas Gleixner /**
522a5497babSThomas Gleixner  * irqentry_exit - Handle return from exception that used irqentry_enter()
523a5497babSThomas Gleixner  * @regs:	Pointer to pt_regs (exception entry regs)
524a5497babSThomas Gleixner  * @state:	Return value from matching call to irqentry_enter()
525a5497babSThomas Gleixner  *
52678a56e04SIra Weiny  * Depending on the return target (kernel/user) this runs the necessary
527a5497babSThomas Gleixner  * preemption and work checks if possible and required and returns to
528a5497babSThomas Gleixner  * the caller with interrupts disabled and no further work pending.
529a5497babSThomas Gleixner  *
530a5497babSThomas Gleixner  * This is the last action before returning to the low level ASM code which
531a5497babSThomas Gleixner  * just needs to return to the appropriate context.
532a5497babSThomas Gleixner  *
533a5497babSThomas Gleixner  * Counterpart to irqentry_enter().
534a5497babSThomas Gleixner  */
535a5497babSThomas Gleixner void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state);
536b6be002bSThomas Gleixner 
537b6be002bSThomas Gleixner /**
538b6be002bSThomas Gleixner  * irqentry_nmi_enter - Handle NMI entry
539b6be002bSThomas Gleixner  * @regs:	Pointer to currents pt_regs
540b6be002bSThomas Gleixner  *
541b6be002bSThomas Gleixner  * Similar to irqentry_enter() but taking care of the NMI constraints.
542b6be002bSThomas Gleixner  */
543b6be002bSThomas Gleixner irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs);
544b6be002bSThomas Gleixner 
545b6be002bSThomas Gleixner /**
546b6be002bSThomas Gleixner  * irqentry_nmi_exit - Handle return from NMI handling
547b6be002bSThomas Gleixner  * @regs:	Pointer to pt_regs (NMI entry regs)
548b6be002bSThomas Gleixner  * @irq_state:	Return value from matching call to irqentry_nmi_enter()
54978a56e04SIra Weiny  *
550b6be002bSThomas Gleixner  * Last action before returning to the low level assembly code.
551b6be002bSThomas Gleixner  *
552b6be002bSThomas Gleixner  * Counterpart to irqentry_nmi_enter().
553b6be002bSThomas Gleixner  */
554b6be002bSThomas Gleixner void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state);
555142781e1SThomas Gleixner 
556 #endif
557