xref: /openbmc/linux/arch/arm/kernel/entry-common.S (revision 0ad53fe3)
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	zero_fp
174	alignment_trap r10, ip, __cr_alignment
175	asm_trace_hardirqs_on save=0
176	enable_irq_notrace
177	ct_user_exit save=0
178
179	/*
180	 * Get the system call number.
181	 */
182
183#if defined(CONFIG_OABI_COMPAT)
184
185	/*
186	 * If we have CONFIG_OABI_COMPAT then we need to look at the swi
187	 * value to determine if it is an EABI or an old ABI call.
188	 */
189#ifdef CONFIG_ARM_THUMB
190	tst	saved_psr, #PSR_T_BIT
191	movne	r10, #0				@ no thumb OABI emulation
192 USER(	ldreq	r10, [saved_pc, #-4]	)	@ get SWI instruction
193#else
194 USER(	ldr	r10, [saved_pc, #-4]	)	@ get SWI instruction
195#endif
196 ARM_BE8(rev	r10, r10)			@ little endian instruction
197
198#elif defined(CONFIG_AEABI)
199
200	/*
201	 * Pure EABI user space always put syscall number into scno (r7).
202	 */
203#elif defined(CONFIG_ARM_THUMB)
204	/* Legacy ABI only, possibly thumb mode. */
205	tst	saved_psr, #PSR_T_BIT		@ this is SPSR from save_user_regs
206	addne	scno, r7, #__NR_SYSCALL_BASE	@ put OS number in
207 USER(	ldreq	scno, [saved_pc, #-4]	)
208
209#else
210	/* Legacy ABI only. */
211 USER(	ldr	scno, [saved_pc, #-4]	)	@ get SWI instruction
212#endif
213
214	/* saved_psr and saved_pc are now dead */
215
216	uaccess_disable tbl
217	get_thread_info tsk
218
219	adr	tbl, sys_call_table		@ load syscall table pointer
220
221#if defined(CONFIG_OABI_COMPAT)
222	/*
223	 * If the swi argument is zero, this is an EABI call and we do nothing.
224	 *
225	 * If this is an old ABI call, get the syscall number into scno and
226	 * get the old ABI syscall table address.
227	 */
228	bics	r10, r10, #0xff000000
229	strne	r10, [tsk, #TI_ABI_SYSCALL]
230	streq	scno, [tsk, #TI_ABI_SYSCALL]
231	eorne	scno, r10, #__NR_OABI_SYSCALL_BASE
232	ldrne	tbl, =sys_oabi_call_table
233#elif !defined(CONFIG_AEABI)
234	bic	scno, scno, #0xff000000		@ mask off SWI op-code
235	str	scno, [tsk, #TI_ABI_SYSCALL]
236	eor	scno, scno, #__NR_SYSCALL_BASE	@ check OS number
237#else
238	str	scno, [tsk, #TI_ABI_SYSCALL]
239#endif
240	/*
241	 * Reload the registers that may have been corrupted on entry to
242	 * the syscall assembly (by tracing or context tracking.)
243	 */
244 TRACE(	ldmia	sp, {r0 - r3}		)
245
246local_restart:
247	ldr	r10, [tsk, #TI_FLAGS]		@ check for syscall tracing
248	stmdb	sp!, {r4, r5}			@ push fifth and sixth args
249
250	tst	r10, #_TIF_SYSCALL_WORK		@ are we tracing syscalls?
251	bne	__sys_trace
252
253	invoke_syscall tbl, scno, r10, __ret_fast_syscall
254
255	add	r1, sp, #S_OFF
2562:	cmp	scno, #(__ARM_NR_BASE - __NR_SYSCALL_BASE)
257	eor	r0, scno, #__NR_SYSCALL_BASE	@ put OS number back
258	bcs	arm_syscall
259	mov	why, #0				@ no longer a real syscall
260	b	sys_ni_syscall			@ not private func
261
262#if defined(CONFIG_OABI_COMPAT) || !defined(CONFIG_AEABI)
263	/*
264	 * We failed to handle a fault trying to access the page
265	 * containing the swi instruction, but we're not really in a
266	 * position to return -EFAULT. Instead, return back to the
267	 * instruction and re-enter the user fault handling path trying
268	 * to page it in. This will likely result in sending SEGV to the
269	 * current task.
270	 */
2719001:
272	sub	lr, saved_pc, #4
273	str	lr, [sp, #S_PC]
274	get_thread_info tsk
275	b	ret_fast_syscall
276#endif
277ENDPROC(vector_swi)
278
279	/*
280	 * This is the really slow path.  We're going to be doing
281	 * context switches, and waiting for our parent to respond.
282	 */
283__sys_trace:
284	add	r0, sp, #S_OFF
285	bl	syscall_trace_enter
286	mov	scno, r0
287	invoke_syscall tbl, scno, r10, __sys_trace_return, reload=1
288	cmp	scno, #-1			@ skip the syscall?
289	bne	2b
290	add	sp, sp, #S_OFF			@ restore stack
291
292__sys_trace_return_nosave:
293	enable_irq_notrace
294	mov	r0, sp
295	bl	syscall_trace_exit
296	b	ret_slow_syscall
297
298__sys_trace_return:
299	str	r0, [sp, #S_R0 + S_OFF]!	@ save returned r0
300	mov	r0, sp
301	bl	syscall_trace_exit
302	b	ret_slow_syscall
303
304	.align	5
305#ifdef CONFIG_ALIGNMENT_TRAP
306	.type	__cr_alignment, #object
307__cr_alignment:
308	.word	cr_alignment
309#endif
310	.ltorg
311
312	.macro	syscall_table_start, sym
313	.equ	__sys_nr, 0
314	.type	\sym, #object
315ENTRY(\sym)
316	.endm
317
318	.macro	syscall, nr, func
319	.ifgt	__sys_nr - \nr
320	.error	"Duplicated/unorded system call entry"
321	.endif
322	.rept	\nr - __sys_nr
323	.long	sys_ni_syscall
324	.endr
325	.long	\func
326	.equ	__sys_nr, \nr + 1
327	.endm
328
329	.macro	syscall_table_end, sym
330	.ifgt	__sys_nr - __NR_syscalls
331	.error	"System call table too big"
332	.endif
333	.rept	__NR_syscalls - __sys_nr
334	.long	sys_ni_syscall
335	.endr
336	.size	\sym, . - \sym
337	.endm
338
339#define __SYSCALL_WITH_COMPAT(nr, native, compat)	__SYSCALL(nr, native)
340#define __SYSCALL(nr, func) syscall nr, func
341
342/*
343 * This is the syscall table declaration for native ABI syscalls.
344 * With EABI a couple syscalls are obsolete and defined as sys_ni_syscall.
345 */
346	syscall_table_start sys_call_table
347#ifdef CONFIG_AEABI
348#include <calls-eabi.S>
349#else
350#include <calls-oabi.S>
351#endif
352	syscall_table_end sys_call_table
353
354/*============================================================================
355 * Special system call wrappers
356 */
357@ r0 = syscall number
358@ r8 = syscall table
359sys_syscall:
360		bic	scno, r0, #__NR_OABI_SYSCALL_BASE
361		cmp	scno, #__NR_syscall - __NR_SYSCALL_BASE
362		cmpne	scno, #NR_syscalls	@ check range
363#ifdef CONFIG_CPU_SPECTRE
364		movhs	scno, #0
365		csdb
366#endif
367		stmialo	sp, {r5, r6}		@ shuffle args
368		movlo	r0, r1
369		movlo	r1, r2
370		movlo	r2, r3
371		movlo	r3, r4
372		ldrlo	pc, [tbl, scno, lsl #2]
373		b	sys_ni_syscall
374ENDPROC(sys_syscall)
375
376sys_sigreturn_wrapper:
377		add	r0, sp, #S_OFF
378		mov	why, #0		@ prevent syscall restart handling
379		b	sys_sigreturn
380ENDPROC(sys_sigreturn_wrapper)
381
382sys_rt_sigreturn_wrapper:
383		add	r0, sp, #S_OFF
384		mov	why, #0		@ prevent syscall restart handling
385		b	sys_rt_sigreturn
386ENDPROC(sys_rt_sigreturn_wrapper)
387
388sys_statfs64_wrapper:
389		teq	r1, #88
390		moveq	r1, #84
391		b	sys_statfs64
392ENDPROC(sys_statfs64_wrapper)
393
394sys_fstatfs64_wrapper:
395		teq	r1, #88
396		moveq	r1, #84
397		b	sys_fstatfs64
398ENDPROC(sys_fstatfs64_wrapper)
399
400/*
401 * Note: off_4k (r5) is always units of 4K.  If we can't do the requested
402 * offset, we return EINVAL.
403 */
404sys_mmap2:
405		str	r5, [sp, #4]
406		b	sys_mmap_pgoff
407ENDPROC(sys_mmap2)
408
409#ifdef CONFIG_OABI_COMPAT
410
411/*
412 * These are syscalls with argument register differences
413 */
414
415sys_oabi_pread64:
416		stmia	sp, {r3, r4}
417		b	sys_pread64
418ENDPROC(sys_oabi_pread64)
419
420sys_oabi_pwrite64:
421		stmia	sp, {r3, r4}
422		b	sys_pwrite64
423ENDPROC(sys_oabi_pwrite64)
424
425sys_oabi_truncate64:
426		mov	r3, r2
427		mov	r2, r1
428		b	sys_truncate64
429ENDPROC(sys_oabi_truncate64)
430
431sys_oabi_ftruncate64:
432		mov	r3, r2
433		mov	r2, r1
434		b	sys_ftruncate64
435ENDPROC(sys_oabi_ftruncate64)
436
437sys_oabi_readahead:
438		str	r3, [sp]
439		mov	r3, r2
440		mov	r2, r1
441		b	sys_readahead
442ENDPROC(sys_oabi_readahead)
443
444/*
445 * Let's declare a second syscall table for old ABI binaries
446 * using the compatibility syscall entries.
447 */
448	syscall_table_start sys_oabi_call_table
449#undef __SYSCALL_WITH_COMPAT
450#define __SYSCALL_WITH_COMPAT(nr, native, compat)	__SYSCALL(nr, compat)
451#include <calls-oabi.S>
452	syscall_table_end sys_oabi_call_table
453
454#endif
455
456