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