1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 *  linux/arch/arm/kernel/entry-common.S
4 *
5 *  Copyright (C) 2000 Russell King
6 */
7
8#include <asm/assembler.h>
9#include <asm/unistd.h>
10#include <asm/ftrace.h>
11#include <asm/unwind.h>
12#include <asm/memory.h>
13#ifdef CONFIG_AEABI
14#include <asm/unistd-oabi.h>
15#endif
16
17	.equ	NR_syscalls, __NR_syscalls
18
19#ifdef CONFIG_NEED_RET_TO_USER
20#include <mach/entry-macro.S>
21#else
22	.macro  arch_ret_to_user, tmp1, tmp2
23	.endm
24#endif
25
26#include "entry-header.S"
27
28saved_psr	.req	r8
29#if defined(CONFIG_TRACE_IRQFLAGS) || defined(CONFIG_CONTEXT_TRACKING)
30saved_pc	.req	r9
31#define TRACE(x...) x
32#else
33saved_pc	.req	lr
34#define TRACE(x...)
35#endif
36
37	.section .entry.text,"ax",%progbits
38	.align	5
39#if !(IS_ENABLED(CONFIG_TRACE_IRQFLAGS) || IS_ENABLED(CONFIG_CONTEXT_TRACKING) || \
40	IS_ENABLED(CONFIG_DEBUG_RSEQ))
41/*
42 * This is the fast syscall return path.  We do as little as possible here,
43 * such as avoiding writing r0 to the stack.  We only use this path if we
44 * have tracing, context tracking and rseq debug disabled - the overheads
45 * from those features make this path too inefficient.
46 */
47ret_fast_syscall:
48__ret_fast_syscall:
49 UNWIND(.fnstart	)
50 UNWIND(.cantunwind	)
51	disable_irq_notrace			@ disable interrupts
52	ldr	r1, [tsk, #TI_FLAGS]		@ re-check for syscall tracing
53	movs	r1, r1, lsl #16
54	bne	fast_work_pending
55
56
57	/* perform architecture specific actions before user return */
58	arch_ret_to_user r1, lr
59
60	restore_user_regs fast = 1, offset = S_OFF
61 UNWIND(.fnend		)
62ENDPROC(ret_fast_syscall)
63
64	/* Ok, we need to do extra processing, enter the slow path. */
65fast_work_pending:
66	str	r0, [sp, #S_R0+S_OFF]!		@ returned r0
67	/* fall through to work_pending */
68#else
69/*
70 * The "replacement" ret_fast_syscall for when tracing, context tracking,
71 * or rseq debug is enabled.  As we will need to call out to some C functions,
72 * we save r0 first to avoid needing to save registers around each C function
73 * call.
74 */
75ret_fast_syscall:
76__ret_fast_syscall:
77 UNWIND(.fnstart	)
78 UNWIND(.cantunwind	)
79	str	r0, [sp, #S_R0 + S_OFF]!	@ save returned r0
80#if IS_ENABLED(CONFIG_DEBUG_RSEQ)
81	/* do_rseq_syscall needs interrupts enabled. */
82	mov	r0, sp				@ 'regs'
83	bl	do_rseq_syscall
84#endif
85	disable_irq_notrace			@ disable interrupts
86	ldr	r1, [tsk, #TI_FLAGS]		@ re-check for syscall tracing
87	movs	r1, r1, lsl #16
88	beq	no_work_pending
89 UNWIND(.fnend		)
90ENDPROC(ret_fast_syscall)
91
92	/* Slower path - fall through to work_pending */
93#endif
94
95	tst	r1, #_TIF_SYSCALL_WORK
96	bne	__sys_trace_return_nosave
97slow_work_pending:
98	mov	r0, sp				@ 'regs'
99	mov	r2, why				@ 'syscall'
100	bl	do_work_pending
101	cmp	r0, #0
102	beq	no_work_pending
103	movlt	scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE)
104	ldmia	sp, {r0 - r6}			@ have to reload r0 - r6
105	b	local_restart			@ ... and off we go
106ENDPROC(ret_fast_syscall)
107
108/*
109 * "slow" syscall return path.  "why" tells us if this was a real syscall.
110 * IRQs may be enabled here, so always disable them.  Note that we use the
111 * "notrace" version to avoid calling into the tracing code unnecessarily.
112 * do_work_pending() will update this state if necessary.
113 */
114ENTRY(ret_to_user)
115ret_slow_syscall:
116#if IS_ENABLED(CONFIG_DEBUG_RSEQ)
117	/* do_rseq_syscall needs interrupts enabled. */
118	enable_irq_notrace			@ enable interrupts
119	mov	r0, sp				@ 'regs'
120	bl	do_rseq_syscall
121#endif
122	disable_irq_notrace			@ disable interrupts
123ENTRY(ret_to_user_from_irq)
124	ldr	r1, [tsk, #TI_FLAGS]
125	movs	r1, r1, lsl #16
126	bne	slow_work_pending
127no_work_pending:
128	asm_trace_hardirqs_on save = 0
129
130	/* perform architecture specific actions before user return */
131	arch_ret_to_user r1, lr
132	ct_user_enter save = 0
133
134	restore_user_regs fast = 0, offset = 0
135ENDPROC(ret_to_user_from_irq)
136ENDPROC(ret_to_user)
137
138/*
139 * This is how we return from a fork.
140 */
141ENTRY(ret_from_fork)
142	bl	schedule_tail
143	cmp	r5, #0
144	movne	r0, r4
145	badrne	lr, 1f
146	retne	r5
1471:	get_thread_info tsk
148	b	ret_slow_syscall
149ENDPROC(ret_from_fork)
150
151/*=============================================================================
152 * SWI handler
153 *-----------------------------------------------------------------------------
154 */
155
156	.align	5
157ENTRY(vector_swi)
158#ifdef CONFIG_CPU_V7M
159	v7m_exception_entry
160#else
161	sub	sp, sp, #PT_REGS_SIZE
162	stmia	sp, {r0 - r12}			@ Calling r0 - r12
163 ARM(	add	r8, sp, #S_PC		)
164 ARM(	stmdb	r8, {sp, lr}^		)	@ Calling sp, lr
165 THUMB(	mov	r8, sp			)
166 THUMB(	store_user_sp_lr r8, r10, S_SP	)	@ calling sp, lr
167	mrs	saved_psr, spsr			@ called from non-FIQ mode, so ok.
168 TRACE(	mov	saved_pc, lr		)
169	str	saved_pc, [sp, #S_PC]		@ Save calling PC
170	str	saved_psr, [sp, #S_PSR]		@ Save CPSR
171	str	r0, [sp, #S_OLD_R0]		@ Save OLD_R0
172#endif
173	reload_current r10, ip
174	zero_fp
175	alignment_trap r10, ip, __cr_alignment
176	asm_trace_hardirqs_on save=0
177	enable_irq_notrace
178	ct_user_exit save=0
179
180	/*
181	 * Get the system call number.
182	 */
183
184#if defined(CONFIG_OABI_COMPAT)
185
186	/*
187	 * If we have CONFIG_OABI_COMPAT then we need to look at the swi
188	 * value to determine if it is an EABI or an old ABI call.
189	 */
190#ifdef CONFIG_ARM_THUMB
191	tst	saved_psr, #PSR_T_BIT
192	movne	r10, #0				@ no thumb OABI emulation
193 USER(	ldreq	r10, [saved_pc, #-4]	)	@ get SWI instruction
194#else
195 USER(	ldr	r10, [saved_pc, #-4]	)	@ get SWI instruction
196#endif
197 ARM_BE8(rev	r10, r10)			@ little endian instruction
198
199#elif defined(CONFIG_AEABI)
200
201	/*
202	 * Pure EABI user space always put syscall number into scno (r7).
203	 */
204#elif defined(CONFIG_ARM_THUMB)
205	/* Legacy ABI only, possibly thumb mode. */
206	tst	saved_psr, #PSR_T_BIT		@ this is SPSR from save_user_regs
207	addne	scno, r7, #__NR_SYSCALL_BASE	@ put OS number in
208 USER(	ldreq	scno, [saved_pc, #-4]	)
209
210#else
211	/* Legacy ABI only. */
212 USER(	ldr	scno, [saved_pc, #-4]	)	@ get SWI instruction
213#endif
214
215	/* saved_psr and saved_pc are now dead */
216
217	uaccess_disable tbl
218	get_thread_info tsk
219
220	adr	tbl, sys_call_table		@ load syscall table pointer
221
222#if defined(CONFIG_OABI_COMPAT)
223	/*
224	 * If the swi argument is zero, this is an EABI call and we do nothing.
225	 *
226	 * If this is an old ABI call, get the syscall number into scno and
227	 * get the old ABI syscall table address.
228	 */
229	bics	r10, r10, #0xff000000
230	strne	r10, [tsk, #TI_ABI_SYSCALL]
231	streq	scno, [tsk, #TI_ABI_SYSCALL]
232	eorne	scno, r10, #__NR_OABI_SYSCALL_BASE
233	ldrne	tbl, =sys_oabi_call_table
234#elif !defined(CONFIG_AEABI)
235	bic	scno, scno, #0xff000000		@ mask off SWI op-code
236	str	scno, [tsk, #TI_ABI_SYSCALL]
237	eor	scno, scno, #__NR_SYSCALL_BASE	@ check OS number
238#else
239	str	scno, [tsk, #TI_ABI_SYSCALL]
240#endif
241	/*
242	 * Reload the registers that may have been corrupted on entry to
243	 * the syscall assembly (by tracing or context tracking.)
244	 */
245 TRACE(	ldmia	sp, {r0 - r3}		)
246
247local_restart:
248	ldr	r10, [tsk, #TI_FLAGS]		@ check for syscall tracing
249	stmdb	sp!, {r4, r5}			@ push fifth and sixth args
250
251	tst	r10, #_TIF_SYSCALL_WORK		@ are we tracing syscalls?
252	bne	__sys_trace
253
254	invoke_syscall tbl, scno, r10, __ret_fast_syscall
255
256	add	r1, sp, #S_OFF
2572:	cmp	scno, #(__ARM_NR_BASE - __NR_SYSCALL_BASE)
258	eor	r0, scno, #__NR_SYSCALL_BASE	@ put OS number back
259	bcs	arm_syscall
260	mov	why, #0				@ no longer a real syscall
261	b	sys_ni_syscall			@ not private func
262
263#if defined(CONFIG_OABI_COMPAT) || !defined(CONFIG_AEABI)
264	/*
265	 * We failed to handle a fault trying to access the page
266	 * containing the swi instruction, but we're not really in a
267	 * position to return -EFAULT. Instead, return back to the
268	 * instruction and re-enter the user fault handling path trying
269	 * to page it in. This will likely result in sending SEGV to the
270	 * current task.
271	 */
2729001:
273	sub	lr, saved_pc, #4
274	str	lr, [sp, #S_PC]
275	get_thread_info tsk
276	b	ret_fast_syscall
277#endif
278ENDPROC(vector_swi)
279
280	/*
281	 * This is the really slow path.  We're going to be doing
282	 * context switches, and waiting for our parent to respond.
283	 */
284__sys_trace:
285	add	r0, sp, #S_OFF
286	bl	syscall_trace_enter
287	mov	scno, r0
288	invoke_syscall tbl, scno, r10, __sys_trace_return, reload=1
289	cmp	scno, #-1			@ skip the syscall?
290	bne	2b
291	add	sp, sp, #S_OFF			@ restore stack
292
293__sys_trace_return_nosave:
294	enable_irq_notrace
295	mov	r0, sp
296	bl	syscall_trace_exit
297	b	ret_slow_syscall
298
299__sys_trace_return:
300	str	r0, [sp, #S_R0 + S_OFF]!	@ save returned r0
301	mov	r0, sp
302	bl	syscall_trace_exit
303	b	ret_slow_syscall
304
305	.align	5
306#ifdef CONFIG_ALIGNMENT_TRAP
307	.type	__cr_alignment, #object
308__cr_alignment:
309	.word	cr_alignment
310#endif
311	.ltorg
312
313	.macro	syscall_table_start, sym
314	.equ	__sys_nr, 0
315	.type	\sym, #object
316ENTRY(\sym)
317	.endm
318
319	.macro	syscall, nr, func
320	.ifgt	__sys_nr - \nr
321	.error	"Duplicated/unorded system call entry"
322	.endif
323	.rept	\nr - __sys_nr
324	.long	sys_ni_syscall
325	.endr
326	.long	\func
327	.equ	__sys_nr, \nr + 1
328	.endm
329
330	.macro	syscall_table_end, sym
331	.ifgt	__sys_nr - __NR_syscalls
332	.error	"System call table too big"
333	.endif
334	.rept	__NR_syscalls - __sys_nr
335	.long	sys_ni_syscall
336	.endr
337	.size	\sym, . - \sym
338	.endm
339
340#define __SYSCALL_WITH_COMPAT(nr, native, compat)	__SYSCALL(nr, native)
341#define __SYSCALL(nr, func) syscall nr, func
342
343/*
344 * This is the syscall table declaration for native ABI syscalls.
345 * With EABI a couple syscalls are obsolete and defined as sys_ni_syscall.
346 */
347	syscall_table_start sys_call_table
348#ifdef CONFIG_AEABI
349#include <calls-eabi.S>
350#else
351#include <calls-oabi.S>
352#endif
353	syscall_table_end sys_call_table
354
355/*============================================================================
356 * Special system call wrappers
357 */
358@ r0 = syscall number
359@ r8 = syscall table
360sys_syscall:
361		bic	scno, r0, #__NR_OABI_SYSCALL_BASE
362		cmp	scno, #__NR_syscall - __NR_SYSCALL_BASE
363		cmpne	scno, #NR_syscalls	@ check range
364#ifdef CONFIG_CPU_SPECTRE
365		movhs	scno, #0
366		csdb
367#endif
368		stmialo	sp, {r5, r6}		@ shuffle args
369		movlo	r0, r1
370		movlo	r1, r2
371		movlo	r2, r3
372		movlo	r3, r4
373		ldrlo	pc, [tbl, scno, lsl #2]
374		b	sys_ni_syscall
375ENDPROC(sys_syscall)
376
377sys_sigreturn_wrapper:
378		add	r0, sp, #S_OFF
379		mov	why, #0		@ prevent syscall restart handling
380		b	sys_sigreturn
381ENDPROC(sys_sigreturn_wrapper)
382
383sys_rt_sigreturn_wrapper:
384		add	r0, sp, #S_OFF
385		mov	why, #0		@ prevent syscall restart handling
386		b	sys_rt_sigreturn
387ENDPROC(sys_rt_sigreturn_wrapper)
388
389sys_statfs64_wrapper:
390		teq	r1, #88
391		moveq	r1, #84
392		b	sys_statfs64
393ENDPROC(sys_statfs64_wrapper)
394
395sys_fstatfs64_wrapper:
396		teq	r1, #88
397		moveq	r1, #84
398		b	sys_fstatfs64
399ENDPROC(sys_fstatfs64_wrapper)
400
401/*
402 * Note: off_4k (r5) is always units of 4K.  If we can't do the requested
403 * offset, we return EINVAL.
404 */
405sys_mmap2:
406		str	r5, [sp, #4]
407		b	sys_mmap_pgoff
408ENDPROC(sys_mmap2)
409
410#ifdef CONFIG_OABI_COMPAT
411
412/*
413 * These are syscalls with argument register differences
414 */
415
416sys_oabi_pread64:
417		stmia	sp, {r3, r4}
418		b	sys_pread64
419ENDPROC(sys_oabi_pread64)
420
421sys_oabi_pwrite64:
422		stmia	sp, {r3, r4}
423		b	sys_pwrite64
424ENDPROC(sys_oabi_pwrite64)
425
426sys_oabi_truncate64:
427		mov	r3, r2
428		mov	r2, r1
429		b	sys_truncate64
430ENDPROC(sys_oabi_truncate64)
431
432sys_oabi_ftruncate64:
433		mov	r3, r2
434		mov	r2, r1
435		b	sys_ftruncate64
436ENDPROC(sys_oabi_ftruncate64)
437
438sys_oabi_readahead:
439		str	r3, [sp]
440		mov	r3, r2
441		mov	r2, r1
442		b	sys_readahead
443ENDPROC(sys_oabi_readahead)
444
445/*
446 * Let's declare a second syscall table for old ABI binaries
447 * using the compatibility syscall entries.
448 */
449	syscall_table_start sys_oabi_call_table
450#undef __SYSCALL_WITH_COMPAT
451#define __SYSCALL_WITH_COMPAT(nr, native, compat)	__SYSCALL(nr, compat)
452#include <calls-oabi.S>
453	syscall_table_end sys_oabi_call_table
454
455#endif
456
457