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