Merge remote-tracking branch 'lts/linux-4.4.y' into linux-linaro-lsk-v4.4
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / entry.S
1 /*
2  * Low-level exception handling code
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  * Authors:     Catalin Marinas <catalin.marinas@arm.com>
6  *              Will Deacon <will.deacon@arm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <linux/init.h>
22 #include <linux/linkage.h>
23
24 #include <asm/alternative.h>
25 #include <asm/assembler.h>
26 #include <asm/asm-offsets.h>
27 #include <asm/cpufeature.h>
28 #include <asm/errno.h>
29 #include <asm/esr.h>
30 #include <asm/irq.h>
31 #include <asm/memory.h>
32 #include <asm/thread_info.h>
33 #include <asm/unistd.h>
34
35 /*
36  * Context tracking subsystem.  Used to instrument transitions
37  * between user and kernel mode.
38  */
39         .macro ct_user_exit, syscall = 0
40 #ifdef CONFIG_CONTEXT_TRACKING
41         bl      context_tracking_user_exit
42         .if \syscall == 1
43         /*
44          * Save/restore needed during syscalls.  Restore syscall arguments from
45          * the values already saved on stack during kernel_entry.
46          */
47         ldp     x0, x1, [sp]
48         ldp     x2, x3, [sp, #S_X2]
49         ldp     x4, x5, [sp, #S_X4]
50         ldp     x6, x7, [sp, #S_X6]
51         .endif
52 #endif
53         .endm
54
55         .macro ct_user_enter
56 #ifdef CONFIG_CONTEXT_TRACKING
57         bl      context_tracking_user_enter
58 #endif
59         .endm
60
61 /*
62  * Bad Abort numbers
63  *-----------------
64  */
65 #define BAD_SYNC        0
66 #define BAD_IRQ         1
67 #define BAD_FIQ         2
68 #define BAD_ERROR       3
69
70         .macro  kernel_entry, el, regsize = 64
71         sub     sp, sp, #S_FRAME_SIZE
72         .if     \regsize == 32
73         mov     w0, w0                          // zero upper 32 bits of x0
74         .endif
75         stp     x0, x1, [sp, #16 * 0]
76         stp     x2, x3, [sp, #16 * 1]
77         stp     x4, x5, [sp, #16 * 2]
78         stp     x6, x7, [sp, #16 * 3]
79         stp     x8, x9, [sp, #16 * 4]
80         stp     x10, x11, [sp, #16 * 5]
81         stp     x12, x13, [sp, #16 * 6]
82         stp     x14, x15, [sp, #16 * 7]
83         stp     x16, x17, [sp, #16 * 8]
84         stp     x18, x19, [sp, #16 * 9]
85         stp     x20, x21, [sp, #16 * 10]
86         stp     x22, x23, [sp, #16 * 11]
87         stp     x24, x25, [sp, #16 * 12]
88         stp     x26, x27, [sp, #16 * 13]
89         stp     x28, x29, [sp, #16 * 14]
90
91         .if     \el == 0
92         mrs     x21, sp_el0
93         mov     tsk, sp
94         and     tsk, tsk, #~(THREAD_SIZE - 1)   // Ensure MDSCR_EL1.SS is clear,
95         ldr     x19, [tsk, #TI_FLAGS]           // since we can unmask debug
96         disable_step_tsk x19, x20               // exceptions when scheduling.
97
98         mov     x29, xzr                        // fp pointed to user-space
99         .else
100         add     x21, sp, #S_FRAME_SIZE
101         get_thread_info tsk
102         /* Save the task's original addr_limit and set USER_DS (TASK_SIZE_64) */
103         ldr     x20, [tsk, #TI_ADDR_LIMIT]
104         str     x20, [sp, #S_ORIG_ADDR_LIMIT]
105         mov     x20, #TASK_SIZE_64
106         str     x20, [tsk, #TI_ADDR_LIMIT]
107         .endif /* \el == 0 */
108         mrs     x22, elr_el1
109         mrs     x23, spsr_el1
110         stp     lr, x21, [sp, #S_LR]
111         stp     x22, x23, [sp, #S_PC]
112
113         /*
114          * Set syscallno to -1 by default (overridden later if real syscall).
115          */
116         .if     \el == 0
117         mvn     x21, xzr
118         str     x21, [sp, #S_SYSCALLNO]
119         .endif
120
121         /*
122          * Set sp_el0 to current thread_info.
123          */
124         .if     \el == 0
125         msr     sp_el0, tsk
126         .endif
127
128         /*
129          * Registers that may be useful after this macro is invoked:
130          *
131          * x21 - aborted SP
132          * x22 - aborted PC
133          * x23 - aborted PSTATE
134         */
135         .endm
136
137         .macro  kernel_exit, el
138         .if     \el != 0
139         /* Restore the task's original addr_limit. */
140         ldr     x20, [sp, #S_ORIG_ADDR_LIMIT]
141         str     x20, [tsk, #TI_ADDR_LIMIT]
142         .endif
143
144         ldp     x21, x22, [sp, #S_PC]           // load ELR, SPSR
145         .if     \el == 0
146         ct_user_enter
147         ldr     x23, [sp, #S_SP]                // load return stack pointer
148         msr     sp_el0, x23
149 #ifdef CONFIG_ARM64_ERRATUM_845719
150 alternative_if_not ARM64_WORKAROUND_845719
151         nop
152         nop
153 #ifdef CONFIG_PID_IN_CONTEXTIDR
154         nop
155 #endif
156 alternative_else
157         tbz     x22, #4, 1f
158 #ifdef CONFIG_PID_IN_CONTEXTIDR
159         mrs     x29, contextidr_el1
160         msr     contextidr_el1, x29
161 #else
162         msr contextidr_el1, xzr
163 #endif
164 1:
165 alternative_endif
166 #endif
167         .endif
168         msr     elr_el1, x21                    // set up the return data
169         msr     spsr_el1, x22
170         ldp     x0, x1, [sp, #16 * 0]
171         ldp     x2, x3, [sp, #16 * 1]
172         ldp     x4, x5, [sp, #16 * 2]
173         ldp     x6, x7, [sp, #16 * 3]
174         ldp     x8, x9, [sp, #16 * 4]
175         ldp     x10, x11, [sp, #16 * 5]
176         ldp     x12, x13, [sp, #16 * 6]
177         ldp     x14, x15, [sp, #16 * 7]
178         ldp     x16, x17, [sp, #16 * 8]
179         ldp     x18, x19, [sp, #16 * 9]
180         ldp     x20, x21, [sp, #16 * 10]
181         ldp     x22, x23, [sp, #16 * 11]
182         ldp     x24, x25, [sp, #16 * 12]
183         ldp     x26, x27, [sp, #16 * 13]
184         ldp     x28, x29, [sp, #16 * 14]
185         ldr     lr, [sp, #S_LR]
186         add     sp, sp, #S_FRAME_SIZE           // restore sp
187         eret                                    // return to kernel
188         .endm
189
190         .macro  get_thread_info, rd
191         mrs     \rd, sp_el0
192         .endm
193
194         .macro  irq_stack_entry
195         mov     x19, sp                 // preserve the original sp
196
197         /*
198          * Compare sp with the current thread_info, if the top
199          * ~(THREAD_SIZE - 1) bits match, we are on a task stack, and
200          * should switch to the irq stack.
201          */
202         and     x25, x19, #~(THREAD_SIZE - 1)
203         cmp     x25, tsk
204         b.ne    9998f
205
206         this_cpu_ptr irq_stack, x25, x26
207         mov     x26, #IRQ_STACK_START_SP
208         add     x26, x25, x26
209
210         /* switch to the irq stack */
211         mov     sp, x26
212
213         /*
214          * Add a dummy stack frame, this non-standard format is fixed up
215          * by unwind_frame()
216          */
217         stp     x29, x19, [sp, #-16]!
218         mov     x29, sp
219
220 9998:
221         .endm
222
223         /*
224          * x19 should be preserved between irq_stack_entry and
225          * irq_stack_exit.
226          */
227         .macro  irq_stack_exit
228         mov     sp, x19
229         .endm
230
231 /*
232  * These are the registers used in the syscall handler, and allow us to
233  * have in theory up to 7 arguments to a function - x0 to x6.
234  *
235  * x7 is reserved for the system call number in 32-bit mode.
236  */
237 sc_nr   .req    x25             // number of system calls
238 scno    .req    x26             // syscall number
239 stbl    .req    x27             // syscall table pointer
240 tsk     .req    x28             // current thread_info
241
242 /*
243  * Interrupt handling.
244  */
245         .macro  irq_handler
246         ldr_l   x1, handle_arch_irq
247         mov     x0, sp
248         irq_stack_entry
249         blr     x1
250         irq_stack_exit
251         .endm
252
253         .text
254
255 /*
256  * Exception vectors.
257  */
258
259         .align  11
260 ENTRY(vectors)
261         ventry  el1_sync_invalid                // Synchronous EL1t
262         ventry  el1_irq_invalid                 // IRQ EL1t
263         ventry  el1_fiq_invalid                 // FIQ EL1t
264         ventry  el1_error_invalid               // Error EL1t
265
266         ventry  el1_sync                        // Synchronous EL1h
267         ventry  el1_irq                         // IRQ EL1h
268         ventry  el1_fiq_invalid                 // FIQ EL1h
269         ventry  el1_error_invalid               // Error EL1h
270
271         ventry  el0_sync                        // Synchronous 64-bit EL0
272         ventry  el0_irq                         // IRQ 64-bit EL0
273         ventry  el0_fiq_invalid                 // FIQ 64-bit EL0
274         ventry  el0_error_invalid               // Error 64-bit EL0
275
276 #ifdef CONFIG_COMPAT
277         ventry  el0_sync_compat                 // Synchronous 32-bit EL0
278         ventry  el0_irq_compat                  // IRQ 32-bit EL0
279         ventry  el0_fiq_invalid_compat          // FIQ 32-bit EL0
280         ventry  el0_error_invalid_compat        // Error 32-bit EL0
281 #else
282         ventry  el0_sync_invalid                // Synchronous 32-bit EL0
283         ventry  el0_irq_invalid                 // IRQ 32-bit EL0
284         ventry  el0_fiq_invalid                 // FIQ 32-bit EL0
285         ventry  el0_error_invalid               // Error 32-bit EL0
286 #endif
287 END(vectors)
288
289 /*
290  * Invalid mode handlers
291  */
292         .macro  inv_entry, el, reason, regsize = 64
293         kernel_entry el, \regsize
294         mov     x0, sp
295         mov     x1, #\reason
296         mrs     x2, esr_el1
297         b       bad_mode
298         .endm
299
300 el0_sync_invalid:
301         inv_entry 0, BAD_SYNC
302 ENDPROC(el0_sync_invalid)
303
304 el0_irq_invalid:
305         inv_entry 0, BAD_IRQ
306 ENDPROC(el0_irq_invalid)
307
308 el0_fiq_invalid:
309         inv_entry 0, BAD_FIQ
310 ENDPROC(el0_fiq_invalid)
311
312 el0_error_invalid:
313         inv_entry 0, BAD_ERROR
314 ENDPROC(el0_error_invalid)
315
316 #ifdef CONFIG_COMPAT
317 el0_fiq_invalid_compat:
318         inv_entry 0, BAD_FIQ, 32
319 ENDPROC(el0_fiq_invalid_compat)
320
321 el0_error_invalid_compat:
322         inv_entry 0, BAD_ERROR, 32
323 ENDPROC(el0_error_invalid_compat)
324 #endif
325
326 el1_sync_invalid:
327         inv_entry 1, BAD_SYNC
328 ENDPROC(el1_sync_invalid)
329
330 el1_irq_invalid:
331         inv_entry 1, BAD_IRQ
332 ENDPROC(el1_irq_invalid)
333
334 el1_fiq_invalid:
335         inv_entry 1, BAD_FIQ
336 ENDPROC(el1_fiq_invalid)
337
338 el1_error_invalid:
339         inv_entry 1, BAD_ERROR
340 ENDPROC(el1_error_invalid)
341
342 /*
343  * EL1 mode handlers.
344  */
345         .align  6
346 el1_sync:
347         kernel_entry 1
348         mrs     x1, esr_el1                     // read the syndrome register
349         lsr     x24, x1, #ESR_ELx_EC_SHIFT      // exception class
350         cmp     x24, #ESR_ELx_EC_DABT_CUR       // data abort in EL1
351         b.eq    el1_da
352         cmp     x24, #ESR_ELx_EC_SYS64          // configurable trap
353         b.eq    el1_undef
354         cmp     x24, #ESR_ELx_EC_SP_ALIGN       // stack alignment exception
355         b.eq    el1_sp_pc
356         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
357         b.eq    el1_sp_pc
358         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL1
359         b.eq    el1_undef
360         cmp     x24, #ESR_ELx_EC_BREAKPT_CUR    // debug exception in EL1
361         b.ge    el1_dbg
362         b       el1_inv
363 el1_da:
364         /*
365          * Data abort handling
366          */
367         mrs     x0, far_el1
368         enable_dbg
369         // re-enable interrupts if they were enabled in the aborted context
370         tbnz    x23, #7, 1f                     // PSR_I_BIT
371         enable_irq
372 1:
373         mov     x2, sp                          // struct pt_regs
374         bl      do_mem_abort
375
376         // disable interrupts before pulling preserved data off the stack
377         disable_irq
378         kernel_exit 1
379 el1_sp_pc:
380         /*
381          * Stack or PC alignment exception handling
382          */
383         mrs     x0, far_el1
384         enable_dbg
385         mov     x2, sp
386         b       do_sp_pc_abort
387 el1_undef:
388         /*
389          * Undefined instruction
390          */
391         enable_dbg
392         mov     x0, sp
393         b       do_undefinstr
394 el1_dbg:
395         /*
396          * Debug exception handling
397          */
398         cmp     x24, #ESR_ELx_EC_BRK64          // if BRK64
399         cinc    x24, x24, eq                    // set bit '0'
400         tbz     x24, #0, el1_inv                // EL1 only
401         mrs     x0, far_el1
402         mov     x2, sp                          // struct pt_regs
403         bl      do_debug_exception
404         kernel_exit 1
405 el1_inv:
406         // TODO: add support for undefined instructions in kernel mode
407         enable_dbg
408         mov     x0, sp
409         mov     x2, x1
410         mov     x1, #BAD_SYNC
411         b       bad_mode
412 ENDPROC(el1_sync)
413
414         .align  6
415 el1_irq:
416         kernel_entry 1
417         enable_dbg
418 #ifdef CONFIG_TRACE_IRQFLAGS
419         bl      trace_hardirqs_off
420 #endif
421
422         get_thread_info tsk
423         irq_handler
424
425 #ifdef CONFIG_PREEMPT
426         ldr     w24, [tsk, #TI_PREEMPT]         // get preempt count
427         cbnz    w24, 1f                         // preempt count != 0
428         ldr     x0, [tsk, #TI_FLAGS]            // get flags
429         tbz     x0, #TIF_NEED_RESCHED, 1f       // needs rescheduling?
430         bl      el1_preempt
431 1:
432 #endif
433 #ifdef CONFIG_TRACE_IRQFLAGS
434         bl      trace_hardirqs_on
435 #endif
436         kernel_exit 1
437 ENDPROC(el1_irq)
438
439 #ifdef CONFIG_PREEMPT
440 el1_preempt:
441         mov     x24, lr
442 1:      bl      preempt_schedule_irq            // irq en/disable is done inside
443         ldr     x0, [tsk, #TI_FLAGS]            // get new tasks TI_FLAGS
444         tbnz    x0, #TIF_NEED_RESCHED, 1b       // needs rescheduling?
445         ret     x24
446 #endif
447
448 /*
449  * EL0 mode handlers.
450  */
451         .align  6
452 el0_sync:
453         kernel_entry 0
454         mrs     x25, esr_el1                    // read the syndrome register
455         lsr     x24, x25, #ESR_ELx_EC_SHIFT     // exception class
456         cmp     x24, #ESR_ELx_EC_SVC64          // SVC in 64-bit state
457         b.eq    el0_svc
458         cmp     x24, #ESR_ELx_EC_DABT_LOW       // data abort in EL0
459         b.eq    el0_da
460         cmp     x24, #ESR_ELx_EC_IABT_LOW       // instruction abort in EL0
461         b.eq    el0_ia
462         cmp     x24, #ESR_ELx_EC_FP_ASIMD       // FP/ASIMD access
463         b.eq    el0_fpsimd_acc
464         cmp     x24, #ESR_ELx_EC_FP_EXC64       // FP/ASIMD exception
465         b.eq    el0_fpsimd_exc
466         cmp     x24, #ESR_ELx_EC_SYS64          // configurable trap
467         b.eq    el0_undef
468         cmp     x24, #ESR_ELx_EC_SP_ALIGN       // stack alignment exception
469         b.eq    el0_sp_pc
470         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
471         b.eq    el0_sp_pc
472         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL0
473         b.eq    el0_undef
474         cmp     x24, #ESR_ELx_EC_BREAKPT_LOW    // debug exception in EL0
475         b.ge    el0_dbg
476         b       el0_inv
477
478 #ifdef CONFIG_COMPAT
479         .align  6
480 el0_sync_compat:
481         kernel_entry 0, 32
482         mrs     x25, esr_el1                    // read the syndrome register
483         lsr     x24, x25, #ESR_ELx_EC_SHIFT     // exception class
484         cmp     x24, #ESR_ELx_EC_SVC32          // SVC in 32-bit state
485         b.eq    el0_svc_compat
486         cmp     x24, #ESR_ELx_EC_DABT_LOW       // data abort in EL0
487         b.eq    el0_da
488         cmp     x24, #ESR_ELx_EC_IABT_LOW       // instruction abort in EL0
489         b.eq    el0_ia
490         cmp     x24, #ESR_ELx_EC_FP_ASIMD       // FP/ASIMD access
491         b.eq    el0_fpsimd_acc
492         cmp     x24, #ESR_ELx_EC_FP_EXC32       // FP/ASIMD exception
493         b.eq    el0_fpsimd_exc
494         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
495         b.eq    el0_sp_pc
496         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL0
497         b.eq    el0_undef
498         cmp     x24, #ESR_ELx_EC_CP15_32        // CP15 MRC/MCR trap
499         b.eq    el0_undef
500         cmp     x24, #ESR_ELx_EC_CP15_64        // CP15 MRRC/MCRR trap
501         b.eq    el0_undef
502         cmp     x24, #ESR_ELx_EC_CP14_MR        // CP14 MRC/MCR trap
503         b.eq    el0_undef
504         cmp     x24, #ESR_ELx_EC_CP14_LS        // CP14 LDC/STC trap
505         b.eq    el0_undef
506         cmp     x24, #ESR_ELx_EC_CP14_64        // CP14 MRRC/MCRR trap
507         b.eq    el0_undef
508         cmp     x24, #ESR_ELx_EC_BREAKPT_LOW    // debug exception in EL0
509         b.ge    el0_dbg
510         b       el0_inv
511 el0_svc_compat:
512         /*
513          * AArch32 syscall handling
514          */
515         adrp    stbl, compat_sys_call_table     // load compat syscall table pointer
516         uxtw    scno, w7                        // syscall number in w7 (r7)
517         mov     sc_nr, #__NR_compat_syscalls
518         b       el0_svc_naked
519
520         .align  6
521 el0_irq_compat:
522         kernel_entry 0, 32
523         b       el0_irq_naked
524 #endif
525
526 el0_da:
527         /*
528          * Data abort handling
529          */
530         mrs     x26, far_el1
531         // enable interrupts before calling the main handler
532         enable_dbg_and_irq
533         ct_user_exit
534         bic     x0, x26, #(0xff << 56)
535         mov     x1, x25
536         mov     x2, sp
537         bl      do_mem_abort
538         b       ret_to_user
539 el0_ia:
540         /*
541          * Instruction abort handling
542          */
543         mrs     x26, far_el1
544         // enable interrupts before calling the main handler
545         enable_dbg_and_irq
546         ct_user_exit
547         mov     x0, x26
548         orr     x1, x25, #1 << 24               // use reserved ISS bit for instruction aborts
549         mov     x2, sp
550         bl      do_mem_abort
551         b       ret_to_user
552 el0_fpsimd_acc:
553         /*
554          * Floating Point or Advanced SIMD access
555          */
556         enable_dbg
557         ct_user_exit
558         mov     x0, x25
559         mov     x1, sp
560         bl      do_fpsimd_acc
561         b       ret_to_user
562 el0_fpsimd_exc:
563         /*
564          * Floating Point or Advanced SIMD exception
565          */
566         enable_dbg
567         ct_user_exit
568         mov     x0, x25
569         mov     x1, sp
570         bl      do_fpsimd_exc
571         b       ret_to_user
572 el0_sp_pc:
573         /*
574          * Stack or PC alignment exception handling
575          */
576         mrs     x26, far_el1
577         // enable interrupts before calling the main handler
578         enable_dbg_and_irq
579         ct_user_exit
580         mov     x0, x26
581         mov     x1, x25
582         mov     x2, sp
583         bl      do_sp_pc_abort
584         b       ret_to_user
585 el0_undef:
586         /*
587          * Undefined instruction
588          */
589         // enable interrupts before calling the main handler
590         enable_dbg_and_irq
591         ct_user_exit
592         mov     x0, sp
593         bl      do_undefinstr
594         b       ret_to_user
595 el0_dbg:
596         /*
597          * Debug exception handling
598          */
599         tbnz    x24, #0, el0_inv                // EL0 only
600         mrs     x0, far_el1
601         mov     x1, x25
602         mov     x2, sp
603         bl      do_debug_exception
604         enable_dbg
605         ct_user_exit
606         b       ret_to_user
607 el0_inv:
608         enable_dbg
609         ct_user_exit
610         mov     x0, sp
611         mov     x1, #BAD_SYNC
612         mov     x2, x25
613         bl      bad_mode
614         b       ret_to_user
615 ENDPROC(el0_sync)
616
617         .align  6
618 el0_irq:
619         kernel_entry 0
620 el0_irq_naked:
621         enable_dbg
622 #ifdef CONFIG_TRACE_IRQFLAGS
623         bl      trace_hardirqs_off
624 #endif
625
626         ct_user_exit
627         irq_handler
628
629 #ifdef CONFIG_TRACE_IRQFLAGS
630         bl      trace_hardirqs_on
631 #endif
632         b       ret_to_user
633 ENDPROC(el0_irq)
634
635 /*
636  * Register switch for AArch64. The callee-saved registers need to be saved
637  * and restored. On entry:
638  *   x0 = previous task_struct (must be preserved across the switch)
639  *   x1 = next task_struct
640  * Previous and next are guaranteed not to be the same.
641  *
642  */
643 ENTRY(cpu_switch_to)
644         mov     x10, #THREAD_CPU_CONTEXT
645         add     x8, x0, x10
646         mov     x9, sp
647         stp     x19, x20, [x8], #16             // store callee-saved registers
648         stp     x21, x22, [x8], #16
649         stp     x23, x24, [x8], #16
650         stp     x25, x26, [x8], #16
651         stp     x27, x28, [x8], #16
652         stp     x29, x9, [x8], #16
653         str     lr, [x8]
654         add     x8, x1, x10
655         ldp     x19, x20, [x8], #16             // restore callee-saved registers
656         ldp     x21, x22, [x8], #16
657         ldp     x23, x24, [x8], #16
658         ldp     x25, x26, [x8], #16
659         ldp     x27, x28, [x8], #16
660         ldp     x29, x9, [x8], #16
661         ldr     lr, [x8]
662         mov     sp, x9
663         and     x9, x9, #~(THREAD_SIZE - 1)
664         msr     sp_el0, x9
665         ret
666 ENDPROC(cpu_switch_to)
667
668 /*
669  * This is the fast syscall return path.  We do as little as possible here,
670  * and this includes saving x0 back into the kernel stack.
671  */
672 ret_fast_syscall:
673         disable_irq                             // disable interrupts
674         str     x0, [sp, #S_X0]                 // returned x0
675         ldr     x1, [tsk, #TI_FLAGS]            // re-check for syscall tracing
676         and     x2, x1, #_TIF_SYSCALL_WORK
677         cbnz    x2, ret_fast_syscall_trace
678         and     x2, x1, #_TIF_WORK_MASK
679         cbnz    x2, work_pending
680         enable_step_tsk x1, x2
681         kernel_exit 0
682 ret_fast_syscall_trace:
683         enable_irq                              // enable interrupts
684         b       __sys_trace_return_skipped      // we already saved x0
685
686 /*
687  * Ok, we need to do extra processing, enter the slow path.
688  */
689 work_pending:
690         tbnz    x1, #TIF_NEED_RESCHED, work_resched
691         /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
692         mov     x0, sp                          // 'regs'
693         enable_irq                              // enable interrupts for do_notify_resume()
694         bl      do_notify_resume
695         b       ret_to_user
696 work_resched:
697 #ifdef CONFIG_TRACE_IRQFLAGS
698         bl      trace_hardirqs_off              // the IRQs are off here, inform the tracing code
699 #endif
700         bl      schedule
701
702 /*
703  * "slow" syscall return path.
704  */
705 ret_to_user:
706         disable_irq                             // disable interrupts
707         ldr     x1, [tsk, #TI_FLAGS]
708         and     x2, x1, #_TIF_WORK_MASK
709         cbnz    x2, work_pending
710         enable_step_tsk x1, x2
711         kernel_exit 0
712 ENDPROC(ret_to_user)
713
714 /*
715  * This is how we return from a fork.
716  */
717 ENTRY(ret_from_fork)
718         bl      schedule_tail
719         cbz     x19, 1f                         // not a kernel thread
720         mov     x0, x20
721         blr     x19
722 1:      get_thread_info tsk
723         b       ret_to_user
724 ENDPROC(ret_from_fork)
725
726 /*
727  * SVC handler.
728  */
729         .align  6
730 el0_svc:
731         adrp    stbl, sys_call_table            // load syscall table pointer
732         uxtw    scno, w8                        // syscall number in w8
733         mov     sc_nr, #__NR_syscalls
734 el0_svc_naked:                                  // compat entry point
735         stp     x0, scno, [sp, #S_ORIG_X0]      // save the original x0 and syscall number
736         enable_dbg_and_irq
737         ct_user_exit 1
738
739         ldr     x16, [tsk, #TI_FLAGS]           // check for syscall hooks
740         tst     x16, #_TIF_SYSCALL_WORK
741         b.ne    __sys_trace
742         cmp     scno, sc_nr                     // check upper syscall limit
743         b.hs    ni_sys
744         ldr     x16, [stbl, scno, lsl #3]       // address in the syscall table
745         blr     x16                             // call sys_* routine
746         b       ret_fast_syscall
747 ni_sys:
748         mov     x0, sp
749         bl      do_ni_syscall
750         b       ret_fast_syscall
751 ENDPROC(el0_svc)
752
753         /*
754          * This is the really slow path.  We're going to be doing context
755          * switches, and waiting for our parent to respond.
756          */
757 __sys_trace:
758         mov     w0, #-1                         // set default errno for
759         cmp     scno, x0                        // user-issued syscall(-1)
760         b.ne    1f
761         mov     x0, #-ENOSYS
762         str     x0, [sp, #S_X0]
763 1:      mov     x0, sp
764         bl      syscall_trace_enter
765         cmp     w0, #-1                         // skip the syscall?
766         b.eq    __sys_trace_return_skipped
767         uxtw    scno, w0                        // syscall number (possibly new)
768         mov     x1, sp                          // pointer to regs
769         cmp     scno, sc_nr                     // check upper syscall limit
770         b.hs    __ni_sys_trace
771         ldp     x0, x1, [sp]                    // restore the syscall args
772         ldp     x2, x3, [sp, #S_X2]
773         ldp     x4, x5, [sp, #S_X4]
774         ldp     x6, x7, [sp, #S_X6]
775         ldr     x16, [stbl, scno, lsl #3]       // address in the syscall table
776         blr     x16                             // call sys_* routine
777
778 __sys_trace_return:
779         str     x0, [sp, #S_X0]                 // save returned x0
780 __sys_trace_return_skipped:
781         mov     x0, sp
782         bl      syscall_trace_exit
783         b       ret_to_user
784
785 __ni_sys_trace:
786         mov     x0, sp
787         bl      do_ni_syscall
788         b       __sys_trace_return
789
790 /*
791  * Special system call wrappers.
792  */
793 ENTRY(sys_rt_sigreturn_wrapper)
794         mov     x0, sp
795         b       sys_rt_sigreturn
796 ENDPROC(sys_rt_sigreturn_wrapper)