• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 *  linux/arch/arm/kernel/entry-common.S
3 *
4 *  Copyright (C) 2000 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <asm/unistd.h>
12#include <asm/ftrace.h>
13#include <asm/unwind.h>
14
15#ifdef CONFIG_NEED_RET_TO_USER
16#include <mach/entry-macro.S>
17#else
18	.macro  arch_ret_to_user, tmp1, tmp2
19	.endm
20#endif
21
22#include "entry-header.S"
23
24
25	.align	5
26/*
27 * This is the fast syscall return path.  We do as little as
28 * possible here, and this includes saving r0 back into the SVC
29 * stack.
30 */
31ret_fast_syscall:
32 UNWIND(.fnstart	)
33 UNWIND(.cantunwind	)
34	disable_irq				@ disable interrupts
35	ldr	r1, [tsk, #TI_FLAGS]
36	tst	r1, #_TIF_WORK_MASK
37	bne	fast_work_pending
38#if defined(CONFIG_IRQSOFF_TRACER)
39	asm_trace_hardirqs_on
40#endif
41
42	/* perform architecture specific actions before user return */
43	arch_ret_to_user r1, lr
44
45	restore_user_regs fast = 1, offset = S_OFF
46 UNWIND(.fnend		)
47
48/*
49 * Ok, we need to do extra processing, enter the slow path.
50 */
51fast_work_pending:
52	str	r0, [sp, #S_R0+S_OFF]!		@ returned r0
53work_pending:
54	mov	r0, sp				@ 'regs'
55	mov	r2, why				@ 'syscall'
56	bl	do_work_pending
57	cmp	r0, #0
58	beq	no_work_pending
59	movlt	scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE)
60	ldmia	sp, {r0 - r6}			@ have to reload r0 - r6
61	b	local_restart			@ ... and off we go
62
63/*
64 * "slow" syscall return path.  "why" tells us if this was a real syscall.
65 */
66ENTRY(ret_to_user)
67ret_slow_syscall:
68	disable_irq				@ disable interrupts
69ENTRY(ret_to_user_from_irq)
70	ldr	r1, [tsk, #TI_FLAGS]
71	tst	r1, #_TIF_WORK_MASK
72	bne	work_pending
73no_work_pending:
74#if defined(CONFIG_IRQSOFF_TRACER)
75	asm_trace_hardirqs_on
76#endif
77	/* perform architecture specific actions before user return */
78	arch_ret_to_user r1, lr
79
80	restore_user_regs fast = 0, offset = 0
81ENDPROC(ret_to_user_from_irq)
82ENDPROC(ret_to_user)
83
84/*
85 * This is how we return from a fork.
86 */
87ENTRY(ret_from_fork)
88	bl	schedule_tail
89	get_thread_info tsk
90	ldr	r1, [tsk, #TI_FLAGS]		@ check for syscall tracing
91	mov	why, #1
92	tst	r1, #_TIF_SYSCALL_WORK		@ are we tracing syscalls?
93	beq	ret_slow_syscall
94	mov	r1, sp
95	mov	r0, #1				@ trace exit [IP = 1]
96	bl	syscall_trace
97	b	ret_slow_syscall
98ENDPROC(ret_from_fork)
99
100	.equ NR_syscalls,0
101#define CALL(x) .equ NR_syscalls,NR_syscalls+1
102#include "calls.S"
103#undef CALL
104#define CALL(x) .long x
105
106#ifdef CONFIG_FUNCTION_TRACER
107/*
108 * When compiling with -pg, gcc inserts a call to the mcount routine at the
109 * start of every function.  In mcount, apart from the function's address (in
110 * lr), we need to get hold of the function's caller's address.
111 *
112 * Older GCCs (pre-4.4) inserted a call to a routine called mcount like this:
113 *
114 *	bl	mcount
115 *
116 * These versions have the limitation that in order for the mcount routine to
117 * be able to determine the function's caller's address, an APCS-style frame
118 * pointer (which is set up with something like the code below) is required.
119 *
120 *	mov     ip, sp
121 *	push    {fp, ip, lr, pc}
122 *	sub     fp, ip, #4
123 *
124 * With EABI, these frame pointers are not available unless -mapcs-frame is
125 * specified, and if building as Thumb-2, not even then.
126 *
127 * Newer GCCs (4.4+) solve this problem by introducing a new version of mcount,
128 * with call sites like:
129 *
130 *	push	{lr}
131 *	bl	__gnu_mcount_nc
132 *
133 * With these compilers, frame pointers are not necessary.
134 *
135 * mcount can be thought of as a function called in the middle of a subroutine
136 * call.  As such, it needs to be transparent for both the caller and the
137 * callee: the original lr needs to be restored when leaving mcount, and no
138 * registers should be clobbered.  (In the __gnu_mcount_nc implementation, we
139 * clobber the ip register.  This is OK because the ARM calling convention
140 * allows it to be clobbered in subroutines and doesn't use it to hold
141 * parameters.)
142 *
143 * When using dynamic ftrace, we patch out the mcount call by a "mov r0, r0"
144 * for the mcount case, and a "pop {lr}" for the __gnu_mcount_nc case (see
145 * arch/arm/kernel/ftrace.c).
146 */
147
148#ifndef CONFIG_OLD_MCOUNT
149#if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 4))
150#error Ftrace requires CONFIG_FRAME_POINTER=y with GCC older than 4.4.0.
151#endif
152#endif
153
154.macro mcount_adjust_addr rd, rn
155	bic	\rd, \rn, #1		@ clear the Thumb bit if present
156	sub	\rd, \rd, #MCOUNT_INSN_SIZE
157.endm
158
159.macro __mcount suffix
160	mcount_enter
161	ldr	r0, =ftrace_trace_function
162	ldr	r2, [r0]
163	adr	r0, .Lftrace_stub
164	cmp	r0, r2
165	bne	1f
166
167#ifdef CONFIG_FUNCTION_GRAPH_TRACER
168	ldr     r1, =ftrace_graph_return
169	ldr     r2, [r1]
170	cmp     r0, r2
171	bne     ftrace_graph_caller\suffix
172
173	ldr     r1, =ftrace_graph_entry
174	ldr     r2, [r1]
175	ldr     r0, =ftrace_graph_entry_stub
176	cmp     r0, r2
177	bne     ftrace_graph_caller\suffix
178#endif
179
180	mcount_exit
181
1821: 	mcount_get_lr	r1			@ lr of instrumented func
183	mcount_adjust_addr	r0, lr		@ instrumented function
184	adr	lr, BSYM(2f)
185	mov	pc, r2
1862:	mcount_exit
187.endm
188
189.macro __ftrace_caller suffix
190	mcount_enter
191
192	mcount_get_lr	r1			@ lr of instrumented func
193	mcount_adjust_addr	r0, lr		@ instrumented function
194
195	.globl ftrace_call\suffix
196ftrace_call\suffix:
197	bl	ftrace_stub
198
199#ifdef CONFIG_FUNCTION_GRAPH_TRACER
200	.globl ftrace_graph_call\suffix
201ftrace_graph_call\suffix:
202	mov	r0, r0
203#endif
204
205	mcount_exit
206.endm
207
208.macro __ftrace_graph_caller
209	sub	r0, fp, #4		@ &lr of instrumented routine (&parent)
210#ifdef CONFIG_DYNAMIC_FTRACE
211	@ called from __ftrace_caller, saved in mcount_enter
212	ldr	r1, [sp, #16]		@ instrumented routine (func)
213	mcount_adjust_addr	r1, r1
214#else
215	@ called from __mcount, untouched in lr
216	mcount_adjust_addr	r1, lr	@ instrumented routine (func)
217#endif
218	mov	r2, fp			@ frame pointer
219	bl	prepare_ftrace_return
220	mcount_exit
221.endm
222
223#ifdef CONFIG_OLD_MCOUNT
224/*
225 * mcount
226 */
227
228.macro mcount_enter
229	stmdb	sp!, {r0-r3, lr}
230.endm
231
232.macro mcount_get_lr reg
233	ldr	\reg, [fp, #-4]
234.endm
235
236.macro mcount_exit
237	ldr	lr, [fp, #-4]
238	ldmia	sp!, {r0-r3, pc}
239.endm
240
241ENTRY(mcount)
242#ifdef CONFIG_DYNAMIC_FTRACE
243	stmdb	sp!, {lr}
244	ldr	lr, [fp, #-4]
245	ldmia	sp!, {pc}
246#else
247	__mcount _old
248#endif
249ENDPROC(mcount)
250
251#ifdef CONFIG_DYNAMIC_FTRACE
252ENTRY(ftrace_caller_old)
253	__ftrace_caller _old
254ENDPROC(ftrace_caller_old)
255#endif
256
257#ifdef CONFIG_FUNCTION_GRAPH_TRACER
258ENTRY(ftrace_graph_caller_old)
259	__ftrace_graph_caller
260ENDPROC(ftrace_graph_caller_old)
261#endif
262
263.purgem mcount_enter
264.purgem mcount_get_lr
265.purgem mcount_exit
266#endif
267
268/*
269 * __gnu_mcount_nc
270 */
271
272.macro mcount_enter
273	stmdb	sp!, {r0-r3, lr}
274.endm
275
276.macro mcount_get_lr reg
277	ldr	\reg, [sp, #20]
278.endm
279
280.macro mcount_exit
281	ldmia	sp!, {r0-r3, ip, lr}
282	mov	pc, ip
283.endm
284
285ENTRY(__gnu_mcount_nc)
286#ifdef CONFIG_DYNAMIC_FTRACE
287	mov	ip, lr
288	ldmia	sp!, {lr}
289	mov	pc, ip
290#else
291	__mcount
292#endif
293ENDPROC(__gnu_mcount_nc)
294
295#ifdef CONFIG_DYNAMIC_FTRACE
296ENTRY(ftrace_caller)
297	__ftrace_caller
298ENDPROC(ftrace_caller)
299#endif
300
301#ifdef CONFIG_FUNCTION_GRAPH_TRACER
302ENTRY(ftrace_graph_caller)
303	__ftrace_graph_caller
304ENDPROC(ftrace_graph_caller)
305#endif
306
307.purgem mcount_enter
308.purgem mcount_get_lr
309.purgem mcount_exit
310
311#ifdef CONFIG_FUNCTION_GRAPH_TRACER
312	.globl return_to_handler
313return_to_handler:
314	stmdb	sp!, {r0-r3}
315	mov	r0, fp			@ frame pointer
316	bl	ftrace_return_to_handler
317	mov	lr, r0			@ r0 has real ret addr
318	ldmia	sp!, {r0-r3}
319	mov	pc, lr
320#endif
321
322ENTRY(ftrace_stub)
323.Lftrace_stub:
324	mov	pc, lr
325ENDPROC(ftrace_stub)
326
327#endif /* CONFIG_FUNCTION_TRACER */
328
329/*=============================================================================
330 * SWI handler
331 *-----------------------------------------------------------------------------
332 */
333
334	/* If we're optimising for StrongARM the resulting code won't
335	   run on an ARM7 and we can save a couple of instructions.
336								--pb */
337#ifdef CONFIG_CPU_ARM710
338#define A710(code...) code
339.Larm710bug:
340	ldmia	sp, {r0 - lr}^			@ Get calling r0 - lr
341	mov	r0, r0
342	add	sp, sp, #S_FRAME_SIZE
343	subs	pc, lr, #4
344#else
345#define A710(code...)
346#endif
347
348	.align	5
349ENTRY(vector_swi)
350	sub	sp, sp, #S_FRAME_SIZE
351	stmia	sp, {r0 - r12}			@ Calling r0 - r12
352 ARM(	add	r8, sp, #S_PC		)
353 ARM(	stmdb	r8, {sp, lr}^		)	@ Calling sp, lr
354 THUMB(	mov	r8, sp			)
355 THUMB(	store_user_sp_lr r8, r10, S_SP	)	@ calling sp, lr
356	mrs	r8, spsr			@ called from non-FIQ mode, so ok.
357	str	lr, [sp, #S_PC]			@ Save calling PC
358	str	r8, [sp, #S_PSR]		@ Save CPSR
359	str	r0, [sp, #S_OLD_R0]		@ Save OLD_R0
360	zero_fp
361
362	/*
363	 * Get the system call number.
364	 */
365
366#if defined(CONFIG_OABI_COMPAT)
367
368	/*
369	 * If we have CONFIG_OABI_COMPAT then we need to look at the swi
370	 * value to determine if it is an EABI or an old ABI call.
371	 */
372#ifdef CONFIG_ARM_THUMB
373	tst	r8, #PSR_T_BIT
374	movne	r10, #0				@ no thumb OABI emulation
375	ldreq	r10, [lr, #-4]			@ get SWI instruction
376#else
377	ldr	r10, [lr, #-4]			@ get SWI instruction
378  A710(	and	ip, r10, #0x0f000000		@ check for SWI		)
379  A710(	teq	ip, #0x0f000000						)
380  A710(	bne	.Larm710bug						)
381#endif
382#ifdef CONFIG_CPU_ENDIAN_BE8
383	rev	r10, r10			@ little endian instruction
384#endif
385
386#elif defined(CONFIG_AEABI)
387
388	/*
389	 * Pure EABI user space always put syscall number into scno (r7).
390	 */
391  A710(	ldr	ip, [lr, #-4]			@ get SWI instruction	)
392  A710(	and	ip, ip, #0x0f000000		@ check for SWI		)
393  A710(	teq	ip, #0x0f000000						)
394  A710(	bne	.Larm710bug						)
395
396#elif defined(CONFIG_ARM_THUMB)
397
398	/* Legacy ABI only, possibly thumb mode. */
399	tst	r8, #PSR_T_BIT			@ this is SPSR from save_user_regs
400	addne	scno, r7, #__NR_SYSCALL_BASE	@ put OS number in
401	ldreq	scno, [lr, #-4]
402
403#else
404
405	/* Legacy ABI only. */
406	ldr	scno, [lr, #-4]			@ get SWI instruction
407  A710(	and	ip, scno, #0x0f000000		@ check for SWI		)
408  A710(	teq	ip, #0x0f000000						)
409  A710(	bne	.Larm710bug						)
410
411#endif
412
413#ifdef CONFIG_ALIGNMENT_TRAP
414	ldr	ip, __cr_alignment
415	ldr	ip, [ip]
416	mcr	p15, 0, ip, c1, c0		@ update control register
417#endif
418	enable_irq
419
420	get_thread_info tsk
421	adr	tbl, sys_call_table		@ load syscall table pointer
422
423#if defined(CONFIG_OABI_COMPAT)
424	/*
425	 * If the swi argument is zero, this is an EABI call and we do nothing.
426	 *
427	 * If this is an old ABI call, get the syscall number into scno and
428	 * get the old ABI syscall table address.
429	 */
430	bics	r10, r10, #0xff000000
431	eorne	scno, r10, #__NR_OABI_SYSCALL_BASE
432	ldrne	tbl, =sys_oabi_call_table
433#elif !defined(CONFIG_AEABI)
434	bic	scno, scno, #0xff000000		@ mask off SWI op-code
435	eor	scno, scno, #__NR_SYSCALL_BASE	@ check OS number
436#endif
437
438local_restart:
439	ldr	r10, [tsk, #TI_FLAGS]		@ check for syscall tracing
440	stmdb	sp!, {r4, r5}			@ push fifth and sixth args
441
442#ifdef CONFIG_SECCOMP
443	tst	r10, #_TIF_SECCOMP
444	bne	__sys_trace
445#endif
446
447	tst	r10, #_TIF_SYSCALL_WORK		@ are we tracing syscalls?
448	bne	__sys_trace
449
450	cmp	scno, #NR_syscalls		@ check upper syscall limit
451	adr	lr, BSYM(ret_fast_syscall)	@ return address
452	ldrcc	pc, [tbl, scno, lsl #2]		@ call sys_* routine
453
454	add	r1, sp, #S_OFF
4552:	mov	why, #0				@ no longer a real syscall
456	cmp	scno, #(__ARM_NR_BASE - __NR_SYSCALL_BASE)
457	eor	r0, scno, #__NR_SYSCALL_BASE	@ put OS number back
458	bcs	arm_syscall
459	b	sys_ni_syscall			@ not private func
460ENDPROC(vector_swi)
461
462	/*
463	 * This is the really slow path.  We're going to be doing
464	 * context switches, and waiting for our parent to respond.
465	 */
466__sys_trace:
467	mov	r2, scno
468	add	r1, sp, #S_OFF
469	mov	r0, #0				@ trace entry [IP = 0]
470	bl	syscall_trace
471
472	adr	lr, BSYM(__sys_trace_return)	@ return address
473	mov	scno, r0			@ syscall number (possibly new)
474	add	r1, sp, #S_R0 + S_OFF		@ pointer to regs
475	cmp	scno, #NR_syscalls		@ check upper syscall limit
476	ldmccia	r1, {r0 - r6}			@ have to reload r0 - r6
477	stmccia	sp, {r4, r5}			@ and update the stack args
478	ldrcc	pc, [tbl, scno, lsl #2]		@ call sys_* routine
479	b	2b
480
481__sys_trace_return:
482	str	r0, [sp, #S_R0 + S_OFF]!	@ save returned r0
483	mov	r2, scno
484	mov	r1, sp
485	mov	r0, #1				@ trace exit [IP = 1]
486	bl	syscall_trace
487	b	ret_slow_syscall
488
489	.align	5
490#ifdef CONFIG_ALIGNMENT_TRAP
491	.type	__cr_alignment, #object
492__cr_alignment:
493	.word	cr_alignment
494#endif
495	.ltorg
496
497/*
498 * This is the syscall table declaration for native ABI syscalls.
499 * With EABI a couple syscalls are obsolete and defined as sys_ni_syscall.
500 */
501#define ABI(native, compat) native
502#ifdef CONFIG_AEABI
503#define OBSOLETE(syscall) sys_ni_syscall
504#else
505#define OBSOLETE(syscall) syscall
506#endif
507
508	.type	sys_call_table, #object
509ENTRY(sys_call_table)
510#include "calls.S"
511#undef ABI
512#undef OBSOLETE
513
514/*============================================================================
515 * Special system call wrappers
516 */
517@ r0 = syscall number
518@ r8 = syscall table
519sys_syscall:
520		bic	scno, r0, #__NR_OABI_SYSCALL_BASE
521		cmp	scno, #__NR_syscall - __NR_SYSCALL_BASE
522		cmpne	scno, #NR_syscalls	@ check range
523		stmloia	sp, {r5, r6}		@ shuffle args
524		movlo	r0, r1
525		movlo	r1, r2
526		movlo	r2, r3
527		movlo	r3, r4
528		ldrlo	pc, [tbl, scno, lsl #2]
529		b	sys_ni_syscall
530ENDPROC(sys_syscall)
531
532sys_fork_wrapper:
533		add	r0, sp, #S_OFF
534		b	sys_fork
535ENDPROC(sys_fork_wrapper)
536
537sys_vfork_wrapper:
538		add	r0, sp, #S_OFF
539		b	sys_vfork
540ENDPROC(sys_vfork_wrapper)
541
542sys_execve_wrapper:
543		add	r3, sp, #S_OFF
544		b	sys_execve
545ENDPROC(sys_execve_wrapper)
546
547sys_clone_wrapper:
548		add	ip, sp, #S_OFF
549		str	ip, [sp, #4]
550		b	sys_clone
551ENDPROC(sys_clone_wrapper)
552
553sys_sigreturn_wrapper:
554		add	r0, sp, #S_OFF
555		mov	why, #0		@ prevent syscall restart handling
556		b	sys_sigreturn
557ENDPROC(sys_sigreturn_wrapper)
558
559sys_rt_sigreturn_wrapper:
560		add	r0, sp, #S_OFF
561		mov	why, #0		@ prevent syscall restart handling
562		b	sys_rt_sigreturn
563ENDPROC(sys_rt_sigreturn_wrapper)
564
565sys_sigaltstack_wrapper:
566		ldr	r2, [sp, #S_OFF + S_SP]
567		b	do_sigaltstack
568ENDPROC(sys_sigaltstack_wrapper)
569
570sys_statfs64_wrapper:
571		teq	r1, #88
572		moveq	r1, #84
573		b	sys_statfs64
574ENDPROC(sys_statfs64_wrapper)
575
576sys_fstatfs64_wrapper:
577		teq	r1, #88
578		moveq	r1, #84
579		b	sys_fstatfs64
580ENDPROC(sys_fstatfs64_wrapper)
581
582/*
583 * Note: off_4k (r5) is always units of 4K.  If we can't do the requested
584 * offset, we return EINVAL.
585 */
586sys_mmap2:
587#if PAGE_SHIFT > 12
588		tst	r5, #PGOFF_MASK
589		moveq	r5, r5, lsr #PAGE_SHIFT - 12
590		streq	r5, [sp, #4]
591		beq	sys_mmap_pgoff
592		mov	r0, #-EINVAL
593		mov	pc, lr
594#else
595		str	r5, [sp, #4]
596		b	sys_mmap_pgoff
597#endif
598ENDPROC(sys_mmap2)
599
600#ifdef CONFIG_OABI_COMPAT
601
602/*
603 * These are syscalls with argument register differences
604 */
605
606sys_oabi_pread64:
607		stmia	sp, {r3, r4}
608		b	sys_pread64
609ENDPROC(sys_oabi_pread64)
610
611sys_oabi_pwrite64:
612		stmia	sp, {r3, r4}
613		b	sys_pwrite64
614ENDPROC(sys_oabi_pwrite64)
615
616sys_oabi_truncate64:
617		mov	r3, r2
618		mov	r2, r1
619		b	sys_truncate64
620ENDPROC(sys_oabi_truncate64)
621
622sys_oabi_ftruncate64:
623		mov	r3, r2
624		mov	r2, r1
625		b	sys_ftruncate64
626ENDPROC(sys_oabi_ftruncate64)
627
628sys_oabi_readahead:
629		str	r3, [sp]
630		mov	r3, r2
631		mov	r2, r1
632		b	sys_readahead
633ENDPROC(sys_oabi_readahead)
634
635/*
636 * Let's declare a second syscall table for old ABI binaries
637 * using the compatibility syscall entries.
638 */
639#define ABI(native, compat) compat
640#define OBSOLETE(syscall) syscall
641
642	.type	sys_oabi_call_table, #object
643ENTRY(sys_oabi_call_table)
644#include "calls.S"
645#undef ABI
646#undef OBSOLETE
647
648#endif
649
650