1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/arch/arm/kernel/entry-common.S
5 * Copyright (C) 2000 Russell King
8 #include <asm/assembler.h>
9 #include <asm/unistd.h>
10 #include <asm/ftrace.h>
11 #include <asm/unwind.h>
14 #include <asm/unistd-oabi.h>
17 .equ NR_syscalls, __NR_syscalls
19 #include "entry-header.S"
22 #if defined(CONFIG_TRACE_IRQFLAGS) || defined(CONFIG_CONTEXT_TRACKING_USER)
30 .section .entry.text,"ax",%progbits
32 #if !(IS_ENABLED(CONFIG_TRACE_IRQFLAGS) || IS_ENABLED(CONFIG_CONTEXT_TRACKING_USER) || \
33 IS_ENABLED(CONFIG_DEBUG_RSEQ))
35 * This is the fast syscall return path. We do as little as possible here,
36 * such as avoiding writing r0 to the stack. We only use this path if we
37 * have tracing, context tracking and rseq debug disabled - the overheads
38 * from those features make this path too inefficient.
44 disable_irq_notrace @ disable interrupts
45 ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
49 restore_user_regs fast = 1, offset = S_OFF
51 ENDPROC(ret_fast_syscall)
53 /* Ok, we need to do extra processing, enter the slow path. */
55 str r0, [sp, #S_R0+S_OFF]! @ returned r0
56 /* fall through to work_pending */
59 * The "replacement" ret_fast_syscall for when tracing, context tracking,
60 * or rseq debug is enabled. As we will need to call out to some C functions,
61 * we save r0 first to avoid needing to save registers around each C function
68 str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
69 #if IS_ENABLED(CONFIG_DEBUG_RSEQ)
70 /* do_rseq_syscall needs interrupts enabled. */
74 disable_irq_notrace @ disable interrupts
75 ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
79 ENDPROC(ret_fast_syscall)
81 /* Slower path - fall through to work_pending */
84 tst r1, #_TIF_SYSCALL_WORK
85 bne __sys_trace_return_nosave
88 mov r2, why @ 'syscall'
92 movlt scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE)
93 str scno, [tsk, #TI_ABI_SYSCALL] @ make sure tracers see update
94 ldmia sp, {r0 - r6} @ have to reload r0 - r6
95 b local_restart @ ... and off we go
96 ENDPROC(ret_fast_syscall)
99 * "slow" syscall return path. "why" tells us if this was a real syscall.
100 * IRQs may be enabled here, so always disable them. Note that we use the
101 * "notrace" version to avoid calling into the tracing code unnecessarily.
102 * do_work_pending() will update this state if necessary.
106 #if IS_ENABLED(CONFIG_DEBUG_RSEQ)
107 /* do_rseq_syscall needs interrupts enabled. */
108 enable_irq_notrace @ enable interrupts
112 disable_irq_notrace @ disable interrupts
113 ENTRY(ret_to_user_from_irq)
114 ldr r1, [tsk, #TI_FLAGS]
116 bne slow_work_pending
118 asm_trace_hardirqs_on save = 0
120 ct_user_enter save = 0
122 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
123 bl stackleak_erase_on_task_stack
125 restore_user_regs fast = 0, offset = 0
126 ENDPROC(ret_to_user_from_irq)
130 * This is how we return from a fork.
138 1: get_thread_info tsk
140 ENDPROC(ret_from_fork)
142 /*=============================================================================
144 *-----------------------------------------------------------------------------
148 #ifdef CONFIG_HARDEN_BRANCH_HISTORY
149 ENTRY(vector_bhb_loop8_swi)
150 sub sp, sp, #PT_REGS_SIZE
159 ENDPROC(vector_bhb_loop8_swi)
162 ENTRY(vector_bhb_bpiall_swi)
163 sub sp, sp, #PT_REGS_SIZE
165 mcr p15, 0, r8, c7, c5, 6 @ BPIALL
168 ENDPROC(vector_bhb_bpiall_swi)
172 #ifdef CONFIG_CPU_V7M
175 sub sp, sp, #PT_REGS_SIZE
176 stmia sp, {r0 - r12} @ Calling r0 - r12
178 ARM( add r8, sp, #S_PC )
179 ARM( stmdb r8, {sp, lr}^ ) @ Calling sp, lr
181 THUMB( store_user_sp_lr r8, r10, S_SP ) @ calling sp, lr
182 mrs saved_psr, spsr @ called from non-FIQ mode, so ok.
183 TRACE( mov saved_pc, lr )
184 str saved_pc, [sp, #S_PC] @ Save calling PC
185 str saved_psr, [sp, #S_PSR] @ Save CPSR
186 str r0, [sp, #S_OLD_R0] @ Save OLD_R0
188 reload_current r10, ip
190 alignment_trap r10, ip, cr_alignment
191 asm_trace_hardirqs_on save=0
196 * Get the system call number.
199 #if defined(CONFIG_OABI_COMPAT)
202 * If we have CONFIG_OABI_COMPAT then we need to look at the swi
203 * value to determine if it is an EABI or an old ABI call.
205 #ifdef CONFIG_ARM_THUMB
206 tst saved_psr, #PSR_T_BIT
207 movne r10, #0 @ no thumb OABI emulation
208 USER( ldreq r10, [saved_pc, #-4] ) @ get SWI instruction
210 USER( ldr r10, [saved_pc, #-4] ) @ get SWI instruction
212 ARM_BE8(rev r10, r10) @ little endian instruction
214 #elif defined(CONFIG_AEABI)
217 * Pure EABI user space always put syscall number into scno (r7).
219 #elif defined(CONFIG_ARM_THUMB)
220 /* Legacy ABI only, possibly thumb mode. */
221 tst saved_psr, #PSR_T_BIT @ this is SPSR from save_user_regs
222 addne scno, r7, #__NR_SYSCALL_BASE @ put OS number in
223 USER( ldreq scno, [saved_pc, #-4] )
226 /* Legacy ABI only. */
227 USER( ldr scno, [saved_pc, #-4] ) @ get SWI instruction
230 /* saved_psr and saved_pc are now dead */
235 adr tbl, sys_call_table @ load syscall table pointer
237 #if defined(CONFIG_OABI_COMPAT)
239 * If the swi argument is zero, this is an EABI call and we do nothing.
241 * If this is an old ABI call, get the syscall number into scno and
242 * get the old ABI syscall table address.
244 bics r10, r10, #0xff000000
245 strne r10, [tsk, #TI_ABI_SYSCALL]
246 streq scno, [tsk, #TI_ABI_SYSCALL]
247 eorne scno, r10, #__NR_OABI_SYSCALL_BASE
248 ldrne tbl, =sys_oabi_call_table
249 #elif !defined(CONFIG_AEABI)
250 bic scno, scno, #0xff000000 @ mask off SWI op-code
251 str scno, [tsk, #TI_ABI_SYSCALL]
252 eor scno, scno, #__NR_SYSCALL_BASE @ check OS number
254 str scno, [tsk, #TI_ABI_SYSCALL]
257 * Reload the registers that may have been corrupted on entry to
258 * the syscall assembly (by tracing or context tracking.)
260 TRACE( ldmia sp, {r0 - r3} )
263 ldr r10, [tsk, #TI_FLAGS] @ check for syscall tracing
264 stmdb sp!, {r4, r5} @ push fifth and sixth args
266 tst r10, #_TIF_SYSCALL_WORK @ are we tracing syscalls?
269 invoke_syscall tbl, scno, r10, __ret_fast_syscall
272 2: cmp scno, #(__ARM_NR_BASE - __NR_SYSCALL_BASE)
273 eor r0, scno, #__NR_SYSCALL_BASE @ put OS number back
275 mov why, #0 @ no longer a real syscall
276 b sys_ni_syscall @ not private func
278 #if defined(CONFIG_OABI_COMPAT) || !defined(CONFIG_AEABI)
280 * We failed to handle a fault trying to access the page
281 * containing the swi instruction, but we're not really in a
282 * position to return -EFAULT. Instead, return back to the
283 * instruction and re-enter the user fault handling path trying
284 * to page it in. This will likely result in sending SEGV to the
297 * This is the really slow path. We're going to be doing
298 * context switches, and waiting for our parent to respond.
302 bl syscall_trace_enter
304 invoke_syscall tbl, scno, r10, __sys_trace_return, reload=1
305 cmp scno, #-1 @ skip the syscall?
307 add sp, sp, #S_OFF @ restore stack
309 __sys_trace_return_nosave:
312 bl syscall_trace_exit
316 str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
318 bl syscall_trace_exit
321 .macro syscall_table_start, sym
327 .macro syscall, nr, func
329 .error "Duplicated/unorded system call entry"
335 .equ __sys_nr, \nr + 1
338 .macro syscall_table_end, sym
339 .ifgt __sys_nr - __NR_syscalls
340 .error "System call table too big"
342 .rept __NR_syscalls - __sys_nr
348 #define __SYSCALL_WITH_COMPAT(nr, native, compat) __SYSCALL(nr, native)
349 #define __SYSCALL(nr, func) syscall nr, func
352 * This is the syscall table declaration for native ABI syscalls.
353 * With EABI a couple syscalls are obsolete and defined as sys_ni_syscall.
355 syscall_table_start sys_call_table
357 #include <calls-eabi.S>
359 #include <calls-oabi.S>
361 syscall_table_end sys_call_table
363 /*============================================================================
364 * Special system call wrappers
366 @ r0 = syscall number
369 bic scno, r0, #__NR_OABI_SYSCALL_BASE
370 cmp scno, #__NR_syscall - __NR_SYSCALL_BASE
371 cmpne scno, #NR_syscalls @ check range
372 #ifdef CONFIG_CPU_SPECTRE
376 stmialo sp, {r5, r6} @ shuffle args
381 ldrlo pc, [tbl, scno, lsl #2]
385 sys_sigreturn_wrapper:
387 mov why, #0 @ prevent syscall restart handling
389 ENDPROC(sys_sigreturn_wrapper)
391 sys_rt_sigreturn_wrapper:
393 mov why, #0 @ prevent syscall restart handling
395 ENDPROC(sys_rt_sigreturn_wrapper)
397 sys_statfs64_wrapper:
401 ENDPROC(sys_statfs64_wrapper)
403 sys_fstatfs64_wrapper:
407 ENDPROC(sys_fstatfs64_wrapper)
410 * Note: off_4k (r5) is always units of 4K. If we can't do the requested
411 * offset, we return EINVAL.
418 #ifdef CONFIG_OABI_COMPAT
421 * These are syscalls with argument register differences
427 ENDPROC(sys_oabi_pread64)
432 ENDPROC(sys_oabi_pwrite64)
438 ENDPROC(sys_oabi_truncate64)
440 sys_oabi_ftruncate64:
444 ENDPROC(sys_oabi_ftruncate64)
451 ENDPROC(sys_oabi_readahead)
454 * Let's declare a second syscall table for old ABI binaries
455 * using the compatibility syscall entries.
457 syscall_table_start sys_oabi_call_table
458 #undef __SYSCALL_WITH_COMPAT
459 #define __SYSCALL_WITH_COMPAT(nr, native, compat) __SYSCALL(nr, compat)
460 #include <calls-oabi.S>
461 syscall_table_end sys_oabi_call_table