xref: /linux-6.15/include/linux/entry-common.h (revision 64c19ba2)
1142781e1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0 */
2142781e1SThomas Gleixner #ifndef __LINUX_ENTRYCOMMON_H
3142781e1SThomas Gleixner #define __LINUX_ENTRYCOMMON_H
4142781e1SThomas Gleixner 
5142781e1SThomas Gleixner #include <linux/tracehook.h>
6142781e1SThomas Gleixner #include <linux/syscalls.h>
7142781e1SThomas Gleixner #include <linux/seccomp.h>
8142781e1SThomas Gleixner #include <linux/sched.h>
9142781e1SThomas Gleixner 
10142781e1SThomas Gleixner #include <asm/entry-common.h>
11142781e1SThomas Gleixner 
12142781e1SThomas Gleixner /*
13142781e1SThomas Gleixner  * Define dummy _TIF work flags if not defined by the architecture or for
14142781e1SThomas Gleixner  * disabled functionality.
15142781e1SThomas Gleixner  */
16142781e1SThomas Gleixner #ifndef _TIF_SYSCALL_EMU
17142781e1SThomas Gleixner # define _TIF_SYSCALL_EMU		(0)
18142781e1SThomas Gleixner #endif
19142781e1SThomas Gleixner 
20142781e1SThomas Gleixner #ifndef _TIF_SYSCALL_AUDIT
21142781e1SThomas Gleixner # define _TIF_SYSCALL_AUDIT		(0)
22142781e1SThomas Gleixner #endif
23142781e1SThomas Gleixner 
24a9f3a74aSThomas Gleixner #ifndef _TIF_PATCH_PENDING
25a9f3a74aSThomas Gleixner # define _TIF_PATCH_PENDING		(0)
26a9f3a74aSThomas Gleixner #endif
27a9f3a74aSThomas Gleixner 
28a9f3a74aSThomas Gleixner #ifndef _TIF_UPROBE
29a9f3a74aSThomas Gleixner # define _TIF_UPROBE			(0)
30a9f3a74aSThomas Gleixner #endif
31a9f3a74aSThomas Gleixner 
3212db8b69SJens Axboe #ifndef _TIF_NOTIFY_SIGNAL
3312db8b69SJens Axboe # define _TIF_NOTIFY_SIGNAL		(0)
3412db8b69SJens Axboe #endif
3512db8b69SJens Axboe 
36142781e1SThomas Gleixner /*
37900ffe39SKees Cook  * TIF flags handled in syscall_enter_from_user_mode()
38142781e1SThomas Gleixner  */
39142781e1SThomas Gleixner #ifndef ARCH_SYSCALL_ENTER_WORK
40142781e1SThomas Gleixner # define ARCH_SYSCALL_ENTER_WORK	(0)
41142781e1SThomas Gleixner #endif
42142781e1SThomas Gleixner 
43142781e1SThomas Gleixner #define SYSCALL_ENTER_WORK						\
44*64c19ba2SGabriel Krisman Bertazi 	(_TIF_SYSCALL_AUDIT  |						\
45524666cbSGabriel Krisman Bertazi 	 _TIF_SYSCALL_EMU |						\
46142781e1SThomas Gleixner 	 ARCH_SYSCALL_ENTER_WORK)
47142781e1SThomas Gleixner 
48a9f3a74aSThomas Gleixner /*
49a9f3a74aSThomas Gleixner  * TIF flags handled in syscall_exit_to_user_mode()
50a9f3a74aSThomas Gleixner  */
51a9f3a74aSThomas Gleixner #ifndef ARCH_SYSCALL_EXIT_WORK
52a9f3a74aSThomas Gleixner # define ARCH_SYSCALL_EXIT_WORK		(0)
53a9f3a74aSThomas Gleixner #endif
54a9f3a74aSThomas Gleixner 
55a9f3a74aSThomas Gleixner #define SYSCALL_EXIT_WORK						\
56*64c19ba2SGabriel Krisman Bertazi 	(_TIF_SYSCALL_AUDIT |						\
57524666cbSGabriel Krisman Bertazi 	 ARCH_SYSCALL_EXIT_WORK)
58a9f3a74aSThomas Gleixner 
59524666cbSGabriel Krisman Bertazi #define SYSCALL_WORK_ENTER	(SYSCALL_WORK_SECCOMP |			\
60*64c19ba2SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_TRACEPOINT |	\
61*64c19ba2SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_TRACE)
62*64c19ba2SGabriel Krisman Bertazi #define SYSCALL_WORK_EXIT	(SYSCALL_WORK_SYSCALL_TRACEPOINT |	\
63*64c19ba2SGabriel Krisman Bertazi 				 SYSCALL_WORK_SYSCALL_TRACE)
64b86678cfSGabriel Krisman Bertazi 
65a9f3a74aSThomas Gleixner /*
66a9f3a74aSThomas Gleixner  * TIF flags handled in exit_to_user_mode_loop()
67a9f3a74aSThomas Gleixner  */
68a9f3a74aSThomas Gleixner #ifndef ARCH_EXIT_TO_USER_MODE_WORK
69a9f3a74aSThomas Gleixner # define ARCH_EXIT_TO_USER_MODE_WORK		(0)
70a9f3a74aSThomas Gleixner #endif
71a9f3a74aSThomas Gleixner 
72a9f3a74aSThomas Gleixner #define EXIT_TO_USER_MODE_WORK						\
73a9f3a74aSThomas Gleixner 	(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE |		\
7412db8b69SJens Axboe 	 _TIF_NEED_RESCHED | _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL |	\
75a9f3a74aSThomas Gleixner 	 ARCH_EXIT_TO_USER_MODE_WORK)
76a9f3a74aSThomas Gleixner 
77142781e1SThomas Gleixner /**
78142781e1SThomas Gleixner  * arch_check_user_regs - Architecture specific sanity check for user mode regs
79142781e1SThomas Gleixner  * @regs:	Pointer to currents pt_regs
80142781e1SThomas Gleixner  *
81142781e1SThomas Gleixner  * Defaults to an empty implementation. Can be replaced by architecture
82142781e1SThomas Gleixner  * specific code.
83142781e1SThomas Gleixner  *
84142781e1SThomas Gleixner  * Invoked from syscall_enter_from_user_mode() in the non-instrumentable
85142781e1SThomas Gleixner  * section. Use __always_inline so the compiler cannot push it out of line
86142781e1SThomas Gleixner  * and make it instrumentable.
87142781e1SThomas Gleixner  */
88142781e1SThomas Gleixner static __always_inline void arch_check_user_regs(struct pt_regs *regs);
89142781e1SThomas Gleixner 
90142781e1SThomas Gleixner #ifndef arch_check_user_regs
91142781e1SThomas Gleixner static __always_inline void arch_check_user_regs(struct pt_regs *regs) {}
92142781e1SThomas Gleixner #endif
93142781e1SThomas Gleixner 
94142781e1SThomas Gleixner /**
95142781e1SThomas Gleixner  * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry()
96142781e1SThomas Gleixner  * @regs:	Pointer to currents pt_regs
97142781e1SThomas Gleixner  *
98142781e1SThomas Gleixner  * Returns: 0 on success or an error code to skip the syscall.
99142781e1SThomas Gleixner  *
100142781e1SThomas Gleixner  * Defaults to tracehook_report_syscall_entry(). Can be replaced by
101142781e1SThomas Gleixner  * architecture specific code.
102142781e1SThomas Gleixner  *
103142781e1SThomas Gleixner  * Invoked from syscall_enter_from_user_mode()
104142781e1SThomas Gleixner  */
105142781e1SThomas Gleixner static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs);
106142781e1SThomas Gleixner 
107142781e1SThomas Gleixner #ifndef arch_syscall_enter_tracehook
108142781e1SThomas Gleixner static inline __must_check int arch_syscall_enter_tracehook(struct pt_regs *regs)
109142781e1SThomas Gleixner {
110142781e1SThomas Gleixner 	return tracehook_report_syscall_entry(regs);
111142781e1SThomas Gleixner }
112142781e1SThomas Gleixner #endif
113142781e1SThomas Gleixner 
114142781e1SThomas Gleixner /**
1154facb95bSThomas Gleixner  * syscall_enter_from_user_mode_prepare - Establish state and enable interrupts
1164facb95bSThomas Gleixner  * @regs:	Pointer to currents pt_regs
1174facb95bSThomas Gleixner  *
1184facb95bSThomas Gleixner  * Invoked from architecture specific syscall entry code with interrupts
1194facb95bSThomas Gleixner  * disabled. The calling code has to be non-instrumentable. When the
1204facb95bSThomas Gleixner  * function returns all state is correct, interrupts are enabled and the
1214facb95bSThomas Gleixner  * subsequent functions can be instrumented.
1224facb95bSThomas Gleixner  *
1234facb95bSThomas Gleixner  * This handles lockdep, RCU (context tracking) and tracing state.
1244facb95bSThomas Gleixner  *
1254facb95bSThomas Gleixner  * This is invoked when there is extra architecture specific functionality
1264facb95bSThomas Gleixner  * to be done between establishing state and handling user mode entry work.
1274facb95bSThomas Gleixner  */
1284facb95bSThomas Gleixner void syscall_enter_from_user_mode_prepare(struct pt_regs *regs);
1294facb95bSThomas Gleixner 
1304facb95bSThomas Gleixner /**
1314facb95bSThomas Gleixner  * syscall_enter_from_user_mode_work - Check and handle work before invoking
132142781e1SThomas Gleixner  *				       a syscall
133142781e1SThomas Gleixner  * @regs:	Pointer to currents pt_regs
134142781e1SThomas Gleixner  * @syscall:	The syscall number
135142781e1SThomas Gleixner  *
136142781e1SThomas Gleixner  * Invoked from architecture specific syscall entry code with interrupts
1374facb95bSThomas Gleixner  * enabled after invoking syscall_enter_from_user_mode_prepare() and extra
1384facb95bSThomas Gleixner  * architecture specific work.
139142781e1SThomas Gleixner  *
140142781e1SThomas Gleixner  * Returns: The original or a modified syscall number
141142781e1SThomas Gleixner  *
142142781e1SThomas Gleixner  * If the returned syscall number is -1 then the syscall should be
143142781e1SThomas Gleixner  * skipped. In this case the caller may invoke syscall_set_error() or
144142781e1SThomas Gleixner  * syscall_set_return_value() first.  If neither of those are called and -1
145142781e1SThomas Gleixner  * is returned, then the syscall will fail with ENOSYS.
146142781e1SThomas Gleixner  *
1474facb95bSThomas Gleixner  * It handles the following work items:
148142781e1SThomas Gleixner  *
1494facb95bSThomas Gleixner  *  1) TIF flag dependent invocations of arch_syscall_enter_tracehook(),
150142781e1SThomas Gleixner  *     __secure_computing(), trace_sys_enter()
1514facb95bSThomas Gleixner  *  2) Invocation of audit_syscall_entry()
1524facb95bSThomas Gleixner  */
1534facb95bSThomas Gleixner long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall);
1544facb95bSThomas Gleixner 
1554facb95bSThomas Gleixner /**
1564facb95bSThomas Gleixner  * syscall_enter_from_user_mode - Establish state and check and handle work
1574facb95bSThomas Gleixner  *				  before invoking a syscall
1584facb95bSThomas Gleixner  * @regs:	Pointer to currents pt_regs
1594facb95bSThomas Gleixner  * @syscall:	The syscall number
1604facb95bSThomas Gleixner  *
1614facb95bSThomas Gleixner  * Invoked from architecture specific syscall entry code with interrupts
1624facb95bSThomas Gleixner  * disabled. The calling code has to be non-instrumentable. When the
1634facb95bSThomas Gleixner  * function returns all state is correct, interrupts are enabled and the
1644facb95bSThomas Gleixner  * subsequent functions can be instrumented.
1654facb95bSThomas Gleixner  *
1664facb95bSThomas Gleixner  * This is combination of syscall_enter_from_user_mode_prepare() and
1674facb95bSThomas Gleixner  * syscall_enter_from_user_mode_work().
1684facb95bSThomas Gleixner  *
1694facb95bSThomas Gleixner  * Returns: The original or a modified syscall number. See
1704facb95bSThomas Gleixner  * syscall_enter_from_user_mode_work() for further explanation.
171142781e1SThomas Gleixner  */
172142781e1SThomas Gleixner long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall);
173142781e1SThomas Gleixner 
174142781e1SThomas Gleixner /**
175a9f3a74aSThomas Gleixner  * local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable()
176a9f3a74aSThomas Gleixner  * @ti_work:	Cached TIF flags gathered with interrupts disabled
177a9f3a74aSThomas Gleixner  *
178a9f3a74aSThomas Gleixner  * Defaults to local_irq_enable(). Can be supplied by architecture specific
179a9f3a74aSThomas Gleixner  * code.
180a9f3a74aSThomas Gleixner  */
181a9f3a74aSThomas Gleixner static inline void local_irq_enable_exit_to_user(unsigned long ti_work);
182a9f3a74aSThomas Gleixner 
183a9f3a74aSThomas Gleixner #ifndef local_irq_enable_exit_to_user
184a9f3a74aSThomas Gleixner static inline void local_irq_enable_exit_to_user(unsigned long ti_work)
185a9f3a74aSThomas Gleixner {
186a9f3a74aSThomas Gleixner 	local_irq_enable();
187a9f3a74aSThomas Gleixner }
188a9f3a74aSThomas Gleixner #endif
189a9f3a74aSThomas Gleixner 
190a9f3a74aSThomas Gleixner /**
191a9f3a74aSThomas Gleixner  * local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable()
192a9f3a74aSThomas Gleixner  *
193a9f3a74aSThomas Gleixner  * Defaults to local_irq_disable(). Can be supplied by architecture specific
194a9f3a74aSThomas Gleixner  * code.
195a9f3a74aSThomas Gleixner  */
196a9f3a74aSThomas Gleixner static inline void local_irq_disable_exit_to_user(void);
197a9f3a74aSThomas Gleixner 
198a9f3a74aSThomas Gleixner #ifndef local_irq_disable_exit_to_user
199a9f3a74aSThomas Gleixner static inline void local_irq_disable_exit_to_user(void)
200a9f3a74aSThomas Gleixner {
201a9f3a74aSThomas Gleixner 	local_irq_disable();
202a9f3a74aSThomas Gleixner }
203a9f3a74aSThomas Gleixner #endif
204a9f3a74aSThomas Gleixner 
205a9f3a74aSThomas Gleixner /**
206a9f3a74aSThomas Gleixner  * arch_exit_to_user_mode_work - Architecture specific TIF work for exit
207a9f3a74aSThomas Gleixner  *				 to user mode.
208a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
209a9f3a74aSThomas Gleixner  * @ti_work:	Cached TIF flags gathered with interrupts disabled
210a9f3a74aSThomas Gleixner  *
211a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode_loop() with interrupt enabled
212a9f3a74aSThomas Gleixner  *
213a9f3a74aSThomas Gleixner  * Defaults to NOOP. Can be supplied by architecture specific code.
214a9f3a74aSThomas Gleixner  */
215a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
216a9f3a74aSThomas Gleixner 					       unsigned long ti_work);
217a9f3a74aSThomas Gleixner 
218a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode_work
219a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
220a9f3a74aSThomas Gleixner 					       unsigned long ti_work)
221a9f3a74aSThomas Gleixner {
222a9f3a74aSThomas Gleixner }
223a9f3a74aSThomas Gleixner #endif
224a9f3a74aSThomas Gleixner 
225a9f3a74aSThomas Gleixner /**
226a9f3a74aSThomas Gleixner  * arch_exit_to_user_mode_prepare - Architecture specific preparation for
227a9f3a74aSThomas Gleixner  *				    exit to user mode.
228a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
229a9f3a74aSThomas Gleixner  * @ti_work:	Cached TIF flags gathered with interrupts disabled
230a9f3a74aSThomas Gleixner  *
231a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode_prepare() with interrupt disabled as the last
232a9f3a74aSThomas Gleixner  * function before return. Defaults to NOOP.
233a9f3a74aSThomas Gleixner  */
234a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
235a9f3a74aSThomas Gleixner 						  unsigned long ti_work);
236a9f3a74aSThomas Gleixner 
237a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode_prepare
238a9f3a74aSThomas Gleixner static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
239a9f3a74aSThomas Gleixner 						  unsigned long ti_work)
240a9f3a74aSThomas Gleixner {
241a9f3a74aSThomas Gleixner }
242a9f3a74aSThomas Gleixner #endif
243a9f3a74aSThomas Gleixner 
244a9f3a74aSThomas Gleixner /**
245a9f3a74aSThomas Gleixner  * arch_exit_to_user_mode - Architecture specific final work before
246a9f3a74aSThomas Gleixner  *			    exit to user mode.
247a9f3a74aSThomas Gleixner  *
248a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode() with interrupt disabled as the last
249a9f3a74aSThomas Gleixner  * function before return. Defaults to NOOP.
250a9f3a74aSThomas Gleixner  *
251a9f3a74aSThomas Gleixner  * This needs to be __always_inline because it is non-instrumentable code
252a9f3a74aSThomas Gleixner  * invoked after context tracking switched to user mode.
253a9f3a74aSThomas Gleixner  *
254a9f3a74aSThomas Gleixner  * An architecture implementation must not do anything complex, no locking
255a9f3a74aSThomas Gleixner  * etc. The main purpose is for speculation mitigations.
256a9f3a74aSThomas Gleixner  */
257a9f3a74aSThomas Gleixner static __always_inline void arch_exit_to_user_mode(void);
258a9f3a74aSThomas Gleixner 
259a9f3a74aSThomas Gleixner #ifndef arch_exit_to_user_mode
260a9f3a74aSThomas Gleixner static __always_inline void arch_exit_to_user_mode(void) { }
261a9f3a74aSThomas Gleixner #endif
262a9f3a74aSThomas Gleixner 
263a9f3a74aSThomas Gleixner /**
26412db8b69SJens Axboe  * arch_do_signal_or_restart -  Architecture specific signal delivery function
265a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
26612db8b69SJens Axboe  * @has_signal:	actual signal to handle
267a9f3a74aSThomas Gleixner  *
268a9f3a74aSThomas Gleixner  * Invoked from exit_to_user_mode_loop().
269a9f3a74aSThomas Gleixner  */
27012db8b69SJens Axboe void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal);
271a9f3a74aSThomas Gleixner 
272a9f3a74aSThomas Gleixner /**
273a9f3a74aSThomas Gleixner  * arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit()
274a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
275a9f3a74aSThomas Gleixner  * @step:	Indicator for single step
276a9f3a74aSThomas Gleixner  *
277a9f3a74aSThomas Gleixner  * Defaults to tracehook_report_syscall_exit(). Can be replaced by
278a9f3a74aSThomas Gleixner  * architecture specific code.
279a9f3a74aSThomas Gleixner  *
280a9f3a74aSThomas Gleixner  * Invoked from syscall_exit_to_user_mode()
281a9f3a74aSThomas Gleixner  */
282a9f3a74aSThomas Gleixner static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step);
283a9f3a74aSThomas Gleixner 
284a9f3a74aSThomas Gleixner #ifndef arch_syscall_exit_tracehook
285a9f3a74aSThomas Gleixner static inline void arch_syscall_exit_tracehook(struct pt_regs *regs, bool step)
286a9f3a74aSThomas Gleixner {
287a9f3a74aSThomas Gleixner 	tracehook_report_syscall_exit(regs, step);
288a9f3a74aSThomas Gleixner }
289a9f3a74aSThomas Gleixner #endif
290a9f3a74aSThomas Gleixner 
291a9f3a74aSThomas Gleixner /**
292a9f3a74aSThomas Gleixner  * syscall_exit_to_user_mode - Handle work before returning to user mode
293a9f3a74aSThomas Gleixner  * @regs:	Pointer to currents pt_regs
294a9f3a74aSThomas Gleixner  *
295a9f3a74aSThomas Gleixner  * Invoked with interrupts enabled and fully valid regs. Returns with all
296a9f3a74aSThomas Gleixner  * work handled, interrupts disabled such that the caller can immediately
297a9f3a74aSThomas Gleixner  * switch to user mode. Called from architecture specific syscall and ret
298a9f3a74aSThomas Gleixner  * from fork code.
299a9f3a74aSThomas Gleixner  *
300a9f3a74aSThomas Gleixner  * The call order is:
301a9f3a74aSThomas Gleixner  *  1) One-time syscall exit work:
302a9f3a74aSThomas Gleixner  *	- rseq syscall exit
303a9f3a74aSThomas Gleixner  *      - audit
304a9f3a74aSThomas Gleixner  *	- syscall tracing
305a9f3a74aSThomas Gleixner  *	- tracehook (single stepping)
306a9f3a74aSThomas Gleixner  *
307a9f3a74aSThomas Gleixner  *  2) Preparatory work
308a9f3a74aSThomas Gleixner  *	- Exit to user mode loop (common TIF handling). Invokes
309a9f3a74aSThomas Gleixner  *	  arch_exit_to_user_mode_work() for architecture specific TIF work
310a9f3a74aSThomas Gleixner  *	- Architecture specific one time work arch_exit_to_user_mode_prepare()
311a9f3a74aSThomas Gleixner  *	- Address limit and lockdep checks
312a9f3a74aSThomas Gleixner  *
313a9f3a74aSThomas Gleixner  *  3) Final transition (lockdep, tracing, context tracking, RCU). Invokes
314a9f3a74aSThomas Gleixner  *     arch_exit_to_user_mode() to handle e.g. speculation mitigations
315a9f3a74aSThomas Gleixner  */
316a9f3a74aSThomas Gleixner void syscall_exit_to_user_mode(struct pt_regs *regs);
317a9f3a74aSThomas Gleixner 
318a9f3a74aSThomas Gleixner /**
319142781e1SThomas Gleixner  * irqentry_enter_from_user_mode - Establish state before invoking the irq handler
320142781e1SThomas Gleixner  * @regs:	Pointer to currents pt_regs
321142781e1SThomas Gleixner  *
322142781e1SThomas Gleixner  * Invoked from architecture specific entry code with interrupts disabled.
323142781e1SThomas Gleixner  * Can only be called when the interrupt entry came from user mode. The
324142781e1SThomas Gleixner  * calling code must be non-instrumentable.  When the function returns all
325142781e1SThomas Gleixner  * state is correct and the subsequent functions can be instrumented.
326142781e1SThomas Gleixner  *
327142781e1SThomas Gleixner  * The function establishes state (lockdep, RCU (context tracking), tracing)
328142781e1SThomas Gleixner  */
329142781e1SThomas Gleixner void irqentry_enter_from_user_mode(struct pt_regs *regs);
330142781e1SThomas Gleixner 
331a9f3a74aSThomas Gleixner /**
332a9f3a74aSThomas Gleixner  * irqentry_exit_to_user_mode - Interrupt exit work
333a9f3a74aSThomas Gleixner  * @regs:	Pointer to current's pt_regs
334a9f3a74aSThomas Gleixner  *
335a9f3a74aSThomas Gleixner  * Invoked with interrupts disbled and fully valid regs. Returns with all
336a9f3a74aSThomas Gleixner  * work handled, interrupts disabled such that the caller can immediately
337a9f3a74aSThomas Gleixner  * switch to user mode. Called from architecture specific interrupt
338a9f3a74aSThomas Gleixner  * handling code.
339a9f3a74aSThomas Gleixner  *
340a9f3a74aSThomas Gleixner  * The call order is #2 and #3 as described in syscall_exit_to_user_mode().
341a9f3a74aSThomas Gleixner  * Interrupt exit is not invoking #1 which is the syscall specific one time
342a9f3a74aSThomas Gleixner  * work.
343a9f3a74aSThomas Gleixner  */
344a9f3a74aSThomas Gleixner void irqentry_exit_to_user_mode(struct pt_regs *regs);
345a9f3a74aSThomas Gleixner 
346a5497babSThomas Gleixner #ifndef irqentry_state
347b6be002bSThomas Gleixner /**
348b6be002bSThomas Gleixner  * struct irqentry_state - Opaque object for exception state storage
349b6be002bSThomas Gleixner  * @exit_rcu: Used exclusively in the irqentry_*() calls; signals whether the
350b6be002bSThomas Gleixner  *            exit path has to invoke rcu_irq_exit().
351b6be002bSThomas Gleixner  * @lockdep: Used exclusively in the irqentry_nmi_*() calls; ensures that
352b6be002bSThomas Gleixner  *           lockdep state is restored correctly on exit from nmi.
353b6be002bSThomas Gleixner  *
354b6be002bSThomas Gleixner  * This opaque object is filled in by the irqentry_*_enter() functions and
355b6be002bSThomas Gleixner  * must be passed back into the corresponding irqentry_*_exit() functions
356b6be002bSThomas Gleixner  * when the exception is complete.
357b6be002bSThomas Gleixner  *
358b6be002bSThomas Gleixner  * Callers of irqentry_*_[enter|exit]() must consider this structure opaque
359b6be002bSThomas Gleixner  * and all members private.  Descriptions of the members are provided to aid in
360b6be002bSThomas Gleixner  * the maintenance of the irqentry_*() functions.
361b6be002bSThomas Gleixner  */
362a5497babSThomas Gleixner typedef struct irqentry_state {
363b6be002bSThomas Gleixner 	union {
364a5497babSThomas Gleixner 		bool	exit_rcu;
365b6be002bSThomas Gleixner 		bool	lockdep;
366b6be002bSThomas Gleixner 	};
367a5497babSThomas Gleixner } irqentry_state_t;
368a5497babSThomas Gleixner #endif
369a5497babSThomas Gleixner 
370a5497babSThomas Gleixner /**
371a5497babSThomas Gleixner  * irqentry_enter - Handle state tracking on ordinary interrupt entries
372a5497babSThomas Gleixner  * @regs:	Pointer to pt_regs of interrupted context
373a5497babSThomas Gleixner  *
374a5497babSThomas Gleixner  * Invokes:
375a5497babSThomas Gleixner  *  - lockdep irqflag state tracking as low level ASM entry disabled
376a5497babSThomas Gleixner  *    interrupts.
377a5497babSThomas Gleixner  *
378a5497babSThomas Gleixner  *  - Context tracking if the exception hit user mode.
379a5497babSThomas Gleixner  *
380a5497babSThomas Gleixner  *  - The hardirq tracer to keep the state consistent as low level ASM
381a5497babSThomas Gleixner  *    entry disabled interrupts.
382a5497babSThomas Gleixner  *
383a5497babSThomas Gleixner  * As a precondition, this requires that the entry came from user mode,
384a5497babSThomas Gleixner  * idle, or a kernel context in which RCU is watching.
385a5497babSThomas Gleixner  *
386a5497babSThomas Gleixner  * For kernel mode entries RCU handling is done conditional. If RCU is
387a5497babSThomas Gleixner  * watching then the only RCU requirement is to check whether the tick has
388a5497babSThomas Gleixner  * to be restarted. If RCU is not watching then rcu_irq_enter() has to be
389a5497babSThomas Gleixner  * invoked on entry and rcu_irq_exit() on exit.
390a5497babSThomas Gleixner  *
391a5497babSThomas Gleixner  * Avoiding the rcu_irq_enter/exit() calls is an optimization but also
392a5497babSThomas Gleixner  * solves the problem of kernel mode pagefaults which can schedule, which
393a5497babSThomas Gleixner  * is not possible after invoking rcu_irq_enter() without undoing it.
394a5497babSThomas Gleixner  *
395a5497babSThomas Gleixner  * For user mode entries irqentry_enter_from_user_mode() is invoked to
396a5497babSThomas Gleixner  * establish the proper context for NOHZ_FULL. Otherwise scheduling on exit
397a5497babSThomas Gleixner  * would not be possible.
398a5497babSThomas Gleixner  *
399a5497babSThomas Gleixner  * Returns: An opaque object that must be passed to idtentry_exit()
400a5497babSThomas Gleixner  */
401a5497babSThomas Gleixner irqentry_state_t noinstr irqentry_enter(struct pt_regs *regs);
402a5497babSThomas Gleixner 
403a5497babSThomas Gleixner /**
404a5497babSThomas Gleixner  * irqentry_exit_cond_resched - Conditionally reschedule on return from interrupt
405a5497babSThomas Gleixner  *
406a5497babSThomas Gleixner  * Conditional reschedule with additional sanity checks.
407a5497babSThomas Gleixner  */
408a5497babSThomas Gleixner void irqentry_exit_cond_resched(void);
409a5497babSThomas Gleixner 
410a5497babSThomas Gleixner /**
411a5497babSThomas Gleixner  * irqentry_exit - Handle return from exception that used irqentry_enter()
412a5497babSThomas Gleixner  * @regs:	Pointer to pt_regs (exception entry regs)
413a5497babSThomas Gleixner  * @state:	Return value from matching call to irqentry_enter()
414a5497babSThomas Gleixner  *
415a5497babSThomas Gleixner  * Depending on the return target (kernel/user) this runs the necessary
41678a56e04SIra Weiny  * preemption and work checks if possible and required and returns to
417a5497babSThomas Gleixner  * the caller with interrupts disabled and no further work pending.
418a5497babSThomas Gleixner  *
419a5497babSThomas Gleixner  * This is the last action before returning to the low level ASM code which
420a5497babSThomas Gleixner  * just needs to return to the appropriate context.
421a5497babSThomas Gleixner  *
422a5497babSThomas Gleixner  * Counterpart to irqentry_enter().
423a5497babSThomas Gleixner  */
424a5497babSThomas Gleixner void noinstr irqentry_exit(struct pt_regs *regs, irqentry_state_t state);
425a5497babSThomas Gleixner 
426b6be002bSThomas Gleixner /**
427b6be002bSThomas Gleixner  * irqentry_nmi_enter - Handle NMI entry
428b6be002bSThomas Gleixner  * @regs:	Pointer to currents pt_regs
429b6be002bSThomas Gleixner  *
430b6be002bSThomas Gleixner  * Similar to irqentry_enter() but taking care of the NMI constraints.
431b6be002bSThomas Gleixner  */
432b6be002bSThomas Gleixner irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs);
433b6be002bSThomas Gleixner 
434b6be002bSThomas Gleixner /**
435b6be002bSThomas Gleixner  * irqentry_nmi_exit - Handle return from NMI handling
436b6be002bSThomas Gleixner  * @regs:	Pointer to pt_regs (NMI entry regs)
437b6be002bSThomas Gleixner  * @irq_state:	Return value from matching call to irqentry_nmi_enter()
438b6be002bSThomas Gleixner  *
43978a56e04SIra Weiny  * Last action before returning to the low level assembly code.
440b6be002bSThomas Gleixner  *
441b6be002bSThomas Gleixner  * Counterpart to irqentry_nmi_enter().
442b6be002bSThomas Gleixner  */
443b6be002bSThomas Gleixner void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state);
444b6be002bSThomas Gleixner 
445142781e1SThomas Gleixner #endif
446