OSDN Git Service

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