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