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