Linux Audio

Check our new training course

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