GNU Linux-libre 4.9.337-gnu1
[releases.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/arm-smccc.h>
22 #include <linux/init.h>
23 #include <linux/linkage.h>
24
25 #include <asm/alternative.h>
26 #include <asm/assembler.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/cpufeature.h>
29 #include <asm/errno.h>
30 #include <asm/esr.h>
31 #include <asm/irq.h>
32 #include <asm/memory.h>
33 #include <asm/mmu.h>
34 #include <asm/processor.h>
35 #include <asm/thread_info.h>
36 #include <asm/asm-uaccess.h>
37 #include <asm/unistd.h>
38 #include <asm/kernel-pgtable.h>
39
40 /*
41  * Context tracking subsystem.  Used to instrument transitions
42  * between user and kernel mode.
43  */
44         .macro ct_user_exit, syscall = 0
45 #ifdef CONFIG_CONTEXT_TRACKING
46         bl      context_tracking_user_exit
47         .if \syscall == 1
48         /*
49          * Save/restore needed during syscalls.  Restore syscall arguments from
50          * the values already saved on stack during kernel_entry.
51          */
52         ldp     x0, x1, [sp]
53         ldp     x2, x3, [sp, #S_X2]
54         ldp     x4, x5, [sp, #S_X4]
55         ldp     x6, x7, [sp, #S_X6]
56         .endif
57 #endif
58         .endm
59
60         .macro ct_user_enter
61 #ifdef CONFIG_CONTEXT_TRACKING
62         bl      context_tracking_user_enter
63 #endif
64         .endm
65
66 /*
67  * Bad Abort numbers
68  *-----------------
69  */
70 #define BAD_SYNC        0
71 #define BAD_IRQ         1
72 #define BAD_FIQ         2
73 #define BAD_ERROR       3
74
75         .macro kernel_ventry, el, label, regsize = 64
76         .align 7
77 .Lventry_start\@:
78         .if     \el == 0
79         /*
80          * This must be the first instruction of the EL0 vector entries. It is
81          * skipped by the trampoline vectors, to trigger the cleanup.
82          */
83         b       .Lskip_tramp_vectors_cleanup\@
84         .if     \regsize == 64
85         mrs     x30, tpidrro_el0
86         msr     tpidrro_el0, xzr
87         .else
88         mov     x30, xzr
89         .endif
90 .Lskip_tramp_vectors_cleanup\@:
91         .endif
92
93         sub     sp, sp, #S_FRAME_SIZE
94         b       el\()\el\()_\label
95 .org .Lventry_start\@ + 128     // Did we overflow the ventry slot?
96         .endm
97
98         .macro tramp_alias, dst, sym, tmp
99         mov_q   \dst, TRAMP_VALIAS
100         adr_l   \tmp, \sym
101         add     \dst, \dst, \tmp
102         adr_l   \tmp, .entry.tramp.text
103         sub     \dst, \dst, \tmp
104         .endm
105
106         // This macro corrupts x0-x3. It is the caller's duty
107         // to save/restore them if required.
108         .macro  apply_ssbd, state, targ, tmp1, tmp2
109 #ifdef CONFIG_ARM64_SSBD
110 alternative_cb  arm64_enable_wa2_handling
111         b       \targ
112 alternative_cb_end
113         ldr_this_cpu    \tmp2, arm64_ssbd_callback_required, \tmp1
114         cbz     \tmp2, \targ
115         ldr     \tmp2, [tsk, #TI_FLAGS]
116         tbnz    \tmp2, #TIF_SSBD, \targ
117         mov     w0, #ARM_SMCCC_ARCH_WORKAROUND_2
118         mov     w1, #\state
119 alternative_cb  arm64_update_smccc_conduit
120         nop                                     // Patched to SMC/HVC #0
121 alternative_cb_end
122 #endif
123         .endm
124
125         .macro  kernel_entry, el, regsize = 64
126         .if     \regsize == 32
127         mov     w0, w0                          // zero upper 32 bits of x0
128         .endif
129         stp     x0, x1, [sp, #16 * 0]
130         stp     x2, x3, [sp, #16 * 1]
131         stp     x4, x5, [sp, #16 * 2]
132         stp     x6, x7, [sp, #16 * 3]
133         stp     x8, x9, [sp, #16 * 4]
134         stp     x10, x11, [sp, #16 * 5]
135         stp     x12, x13, [sp, #16 * 6]
136         stp     x14, x15, [sp, #16 * 7]
137         stp     x16, x17, [sp, #16 * 8]
138         stp     x18, x19, [sp, #16 * 9]
139         stp     x20, x21, [sp, #16 * 10]
140         stp     x22, x23, [sp, #16 * 11]
141         stp     x24, x25, [sp, #16 * 12]
142         stp     x26, x27, [sp, #16 * 13]
143         stp     x28, x29, [sp, #16 * 14]
144
145         .if     \el == 0
146         mrs     x21, sp_el0
147         mov     tsk, sp
148         and     tsk, tsk, #~(THREAD_SIZE - 1)   // Ensure MDSCR_EL1.SS is clear,
149         ldr     x19, [tsk, #TI_FLAGS]           // since we can unmask debug
150         disable_step_tsk x19, x20               // exceptions when scheduling.
151
152         apply_ssbd 1, 1f, x22, x23
153
154 #ifdef CONFIG_ARM64_SSBD
155         ldp     x0, x1, [sp, #16 * 0]
156         ldp     x2, x3, [sp, #16 * 1]
157 #endif
158 1:
159
160         mov     x29, xzr                        // fp pointed to user-space
161         .else
162         add     x21, sp, #S_FRAME_SIZE
163         get_thread_info tsk
164         /* Save the task's original addr_limit and set USER_DS */
165         ldr     x20, [tsk, #TI_ADDR_LIMIT]
166         str     x20, [sp, #S_ORIG_ADDR_LIMIT]
167         mov     x20, #USER_DS
168         str     x20, [tsk, #TI_ADDR_LIMIT]
169         /* No need to reset PSTATE.UAO, hardware's already set it to 0 for us */
170         .endif /* \el == 0 */
171         mrs     x22, elr_el1
172         mrs     x23, spsr_el1
173         stp     lr, x21, [sp, #S_LR]
174         stp     x22, x23, [sp, #S_PC]
175
176         /*
177          * Set syscallno to -1 by default (overridden later if real syscall).
178          */
179         .if     \el == 0
180         mvn     x21, xzr
181         str     x21, [sp, #S_SYSCALLNO]
182         .endif
183
184         /*
185          * Set sp_el0 to current thread_info.
186          */
187         .if     \el == 0
188         msr     sp_el0, tsk
189         .endif
190
191         /*
192          * Registers that may be useful after this macro is invoked:
193          *
194          * x21 - aborted SP
195          * x22 - aborted PC
196          * x23 - aborted PSTATE
197         */
198         .endm
199
200         .macro  kernel_exit, el
201         .if     \el != 0
202         /* Restore the task's original addr_limit. */
203         ldr     x20, [sp, #S_ORIG_ADDR_LIMIT]
204         str     x20, [tsk, #TI_ADDR_LIMIT]
205
206         /* No need to restore UAO, it will be restored from SPSR_EL1 */
207         .endif
208
209         ldp     x21, x22, [sp, #S_PC]           // load ELR, SPSR
210         .if     \el == 0
211         ct_user_enter
212         ldr     x23, [sp, #S_SP]                // load return stack pointer
213         msr     sp_el0, x23
214         tst     x22, #PSR_MODE32_BIT            // native task?
215         b.eq    3f
216
217 #ifdef CONFIG_ARM64_ERRATUM_845719
218 alternative_if ARM64_WORKAROUND_845719
219 #ifdef CONFIG_PID_IN_CONTEXTIDR
220         mrs     x29, contextidr_el1
221         msr     contextidr_el1, x29
222 #else
223         msr contextidr_el1, xzr
224 #endif
225 alternative_else_nop_endif
226 #endif
227 3:
228         apply_ssbd 0, 5f, x0, x1
229 5:
230         .endif
231         msr     elr_el1, x21                    // set up the return data
232         msr     spsr_el1, x22
233         ldp     x0, x1, [sp, #16 * 0]
234         ldp     x2, x3, [sp, #16 * 1]
235         ldp     x4, x5, [sp, #16 * 2]
236         ldp     x6, x7, [sp, #16 * 3]
237         ldp     x8, x9, [sp, #16 * 4]
238         ldp     x10, x11, [sp, #16 * 5]
239         ldp     x12, x13, [sp, #16 * 6]
240         ldp     x14, x15, [sp, #16 * 7]
241         ldp     x16, x17, [sp, #16 * 8]
242         ldp     x18, x19, [sp, #16 * 9]
243         ldp     x20, x21, [sp, #16 * 10]
244         ldp     x22, x23, [sp, #16 * 11]
245         ldp     x24, x25, [sp, #16 * 12]
246         ldp     x26, x27, [sp, #16 * 13]
247         ldp     x28, x29, [sp, #16 * 14]
248
249         .if     \el == 0
250 alternative_if_not ARM64_UNMAP_KERNEL_AT_EL0
251         ldr     lr, [sp, #S_LR]
252         add     sp, sp, #S_FRAME_SIZE           // restore sp
253         eret
254 alternative_else_nop_endif
255 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
256         bne     4f
257         msr     far_el1, x29
258         tramp_alias     x30, tramp_exit_native, x29
259         br      x30
260 4:
261         tramp_alias     x30, tramp_exit_compat, x29
262         br      x30
263 #endif
264         .else
265         ldr     lr, [sp, #S_LR]
266         add     sp, sp, #S_FRAME_SIZE           // restore sp
267         eret
268         .endif
269         .endm
270
271         .macro  get_thread_info, rd
272         mrs     \rd, sp_el0
273         .endm
274
275         .macro  irq_stack_entry
276         mov     x19, sp                 // preserve the original sp
277
278         /*
279          * Compare sp with the current thread_info, if the top
280          * ~(THREAD_SIZE - 1) bits match, we are on a task stack, and
281          * should switch to the irq stack.
282          */
283         and     x25, x19, #~(THREAD_SIZE - 1)
284         cmp     x25, tsk
285         b.ne    9998f
286
287         adr_this_cpu x25, irq_stack, x26
288         mov     x26, #IRQ_STACK_START_SP
289         add     x26, x25, x26
290
291         /* switch to the irq stack */
292         mov     sp, x26
293
294         /*
295          * Add a dummy stack frame, this non-standard format is fixed up
296          * by unwind_frame()
297          */
298         stp     x29, x19, [sp, #-16]!
299         mov     x29, sp
300
301 9998:
302         .endm
303
304         /*
305          * x19 should be preserved between irq_stack_entry and
306          * irq_stack_exit.
307          */
308         .macro  irq_stack_exit
309         mov     sp, x19
310         .endm
311
312 /*
313  * These are the registers used in the syscall handler, and allow us to
314  * have in theory up to 7 arguments to a function - x0 to x6.
315  *
316  * x7 is reserved for the system call number in 32-bit mode.
317  */
318 sc_nr   .req    x25             // number of system calls
319 scno    .req    x26             // syscall number
320 stbl    .req    x27             // syscall table pointer
321 tsk     .req    x28             // current thread_info
322
323 /*
324  * Interrupt handling.
325  */
326         .macro  irq_handler
327         ldr_l   x1, handle_arch_irq
328         mov     x0, sp
329         irq_stack_entry
330         blr     x1
331         irq_stack_exit
332         .endm
333
334         .text
335
336 /*
337  * Exception vectors.
338  */
339         .pushsection ".entry.text", "ax"
340
341         .align  11
342 ENTRY(vectors)
343         kernel_ventry   1, sync_invalid                 // Synchronous EL1t
344         kernel_ventry   1, irq_invalid                  // IRQ EL1t
345         kernel_ventry   1, fiq_invalid                  // FIQ EL1t
346         kernel_ventry   1, error_invalid                // Error EL1t
347
348         kernel_ventry   1, sync                         // Synchronous EL1h
349         kernel_ventry   1, irq                          // IRQ EL1h
350         kernel_ventry   1, fiq_invalid                  // FIQ EL1h
351         kernel_ventry   1, error_invalid                // Error EL1h
352
353         kernel_ventry   0, sync                         // Synchronous 64-bit EL0
354         kernel_ventry   0, irq                          // IRQ 64-bit EL0
355         kernel_ventry   0, fiq_invalid                  // FIQ 64-bit EL0
356         kernel_ventry   0, error_invalid                // Error 64-bit EL0
357
358 #ifdef CONFIG_COMPAT
359         kernel_ventry   0, sync_compat, 32              // Synchronous 32-bit EL0
360         kernel_ventry   0, irq_compat, 32               // IRQ 32-bit EL0
361         kernel_ventry   0, fiq_invalid_compat, 32       // FIQ 32-bit EL0
362         kernel_ventry   0, error_invalid_compat, 32     // Error 32-bit EL0
363 #else
364         kernel_ventry   0, sync_invalid, 32             // Synchronous 32-bit EL0
365         kernel_ventry   0, irq_invalid, 32              // IRQ 32-bit EL0
366         kernel_ventry   0, fiq_invalid, 32              // FIQ 32-bit EL0
367         kernel_ventry   0, error_invalid, 32            // Error 32-bit EL0
368 #endif
369 END(vectors)
370
371 /*
372  * Invalid mode handlers
373  */
374         .macro  inv_entry, el, reason, regsize = 64
375         kernel_entry \el, \regsize
376         mov     x0, sp
377         mov     x1, #\reason
378         mrs     x2, esr_el1
379         b       bad_mode
380         .endm
381
382 el0_sync_invalid:
383         inv_entry 0, BAD_SYNC
384 ENDPROC(el0_sync_invalid)
385
386 el0_irq_invalid:
387         inv_entry 0, BAD_IRQ
388 ENDPROC(el0_irq_invalid)
389
390 el0_fiq_invalid:
391         inv_entry 0, BAD_FIQ
392 ENDPROC(el0_fiq_invalid)
393
394 el0_error_invalid:
395         inv_entry 0, BAD_ERROR
396 ENDPROC(el0_error_invalid)
397
398 #ifdef CONFIG_COMPAT
399 el0_fiq_invalid_compat:
400         inv_entry 0, BAD_FIQ, 32
401 ENDPROC(el0_fiq_invalid_compat)
402
403 el0_error_invalid_compat:
404         inv_entry 0, BAD_ERROR, 32
405 ENDPROC(el0_error_invalid_compat)
406 #endif
407
408 el1_sync_invalid:
409         inv_entry 1, BAD_SYNC
410 ENDPROC(el1_sync_invalid)
411
412 el1_irq_invalid:
413         inv_entry 1, BAD_IRQ
414 ENDPROC(el1_irq_invalid)
415
416 el1_fiq_invalid:
417         inv_entry 1, BAD_FIQ
418 ENDPROC(el1_fiq_invalid)
419
420 el1_error_invalid:
421         inv_entry 1, BAD_ERROR
422 ENDPROC(el1_error_invalid)
423
424 /*
425  * EL1 mode handlers.
426  */
427         .align  6
428 el1_sync:
429         kernel_entry 1
430         mrs     x1, esr_el1                     // read the syndrome register
431         lsr     x24, x1, #ESR_ELx_EC_SHIFT      // exception class
432         cmp     x24, #ESR_ELx_EC_DABT_CUR       // data abort in EL1
433         b.eq    el1_da
434         cmp     x24, #ESR_ELx_EC_IABT_CUR       // instruction abort in EL1
435         b.eq    el1_ia
436         cmp     x24, #ESR_ELx_EC_SYS64          // configurable trap
437         b.eq    el1_undef
438         cmp     x24, #ESR_ELx_EC_SP_ALIGN       // stack alignment exception
439         b.eq    el1_sp_pc
440         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
441         b.eq    el1_sp_pc
442         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL1
443         b.eq    el1_undef
444         cmp     x24, #ESR_ELx_EC_BREAKPT_CUR    // debug exception in EL1
445         b.ge    el1_dbg
446         b       el1_inv
447
448 el1_ia:
449         /*
450          * Fall through to the Data abort case
451          */
452 el1_da:
453         /*
454          * Data abort handling
455          */
456         mrs     x3, far_el1
457         enable_dbg
458         // re-enable interrupts if they were enabled in the aborted context
459         tbnz    x23, #7, 1f                     // PSR_I_BIT
460         enable_irq
461 1:
462         clear_address_tag x0, x3
463         mov     x2, sp                          // struct pt_regs
464         bl      do_mem_abort
465
466         // disable interrupts before pulling preserved data off the stack
467         disable_irq
468         kernel_exit 1
469 el1_sp_pc:
470         /*
471          * Stack or PC alignment exception handling
472          */
473         mrs     x0, far_el1
474         enable_dbg
475         mov     x2, sp
476         b       do_sp_pc_abort
477 el1_undef:
478         /*
479          * Undefined instruction
480          */
481         enable_dbg
482         mov     x0, sp
483         b       do_undefinstr
484 el1_dbg:
485         /*
486          * Debug exception handling
487          */
488         cmp     x24, #ESR_ELx_EC_BRK64          // if BRK64
489         cinc    x24, x24, eq                    // set bit '0'
490         tbz     x24, #0, el1_inv                // EL1 only
491         mrs     x0, far_el1
492         mov     x2, sp                          // struct pt_regs
493         bl      do_debug_exception
494         kernel_exit 1
495 el1_inv:
496         // TODO: add support for undefined instructions in kernel mode
497         enable_dbg
498         mov     x0, sp
499         mov     x2, x1
500         mov     x1, #BAD_SYNC
501         b       bad_mode
502 ENDPROC(el1_sync)
503
504         .align  6
505 el1_irq:
506         kernel_entry 1
507         enable_dbg
508 #ifdef CONFIG_TRACE_IRQFLAGS
509         bl      trace_hardirqs_off
510 #endif
511
512         irq_handler
513
514 #ifdef CONFIG_PREEMPT
515         ldr     w24, [tsk, #TI_PREEMPT]         // get preempt count
516         cbnz    w24, 1f                         // preempt count != 0
517         ldr     x0, [tsk, #TI_FLAGS]            // get flags
518         tbz     x0, #TIF_NEED_RESCHED, 1f       // needs rescheduling?
519         bl      el1_preempt
520 1:
521 #endif
522 #ifdef CONFIG_TRACE_IRQFLAGS
523         bl      trace_hardirqs_on
524 #endif
525         kernel_exit 1
526 ENDPROC(el1_irq)
527
528 #ifdef CONFIG_PREEMPT
529 el1_preempt:
530         mov     x24, lr
531 1:      bl      preempt_schedule_irq            // irq en/disable is done inside
532         ldr     x0, [tsk, #TI_FLAGS]            // get new tasks TI_FLAGS
533         tbnz    x0, #TIF_NEED_RESCHED, 1b       // needs rescheduling?
534         ret     x24
535 #endif
536
537 /*
538  * EL0 mode handlers.
539  */
540         .align  6
541 el0_sync:
542         kernel_entry 0
543         mrs     x25, esr_el1                    // read the syndrome register
544         lsr     x24, x25, #ESR_ELx_EC_SHIFT     // exception class
545         cmp     x24, #ESR_ELx_EC_SVC64          // SVC in 64-bit state
546         b.eq    el0_svc
547         cmp     x24, #ESR_ELx_EC_DABT_LOW       // data abort in EL0
548         b.eq    el0_da
549         cmp     x24, #ESR_ELx_EC_IABT_LOW       // instruction abort in EL0
550         b.eq    el0_ia
551         cmp     x24, #ESR_ELx_EC_FP_ASIMD       // FP/ASIMD access
552         b.eq    el0_fpsimd_acc
553         cmp     x24, #ESR_ELx_EC_FP_EXC64       // FP/ASIMD exception
554         b.eq    el0_fpsimd_exc
555         cmp     x24, #ESR_ELx_EC_SYS64          // configurable trap
556         b.eq    el0_sys
557         cmp     x24, #ESR_ELx_EC_SP_ALIGN       // stack alignment exception
558         b.eq    el0_sp_pc
559         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
560         b.eq    el0_sp_pc
561         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL0
562         b.eq    el0_undef
563         cmp     x24, #ESR_ELx_EC_BREAKPT_LOW    // debug exception in EL0
564         b.ge    el0_dbg
565         b       el0_inv
566
567 #ifdef CONFIG_COMPAT
568         .align  6
569 el0_sync_compat:
570         kernel_entry 0, 32
571         mrs     x25, esr_el1                    // read the syndrome register
572         lsr     x24, x25, #ESR_ELx_EC_SHIFT     // exception class
573         cmp     x24, #ESR_ELx_EC_SVC32          // SVC in 32-bit state
574         b.eq    el0_svc_compat
575         cmp     x24, #ESR_ELx_EC_DABT_LOW       // data abort in EL0
576         b.eq    el0_da
577         cmp     x24, #ESR_ELx_EC_IABT_LOW       // instruction abort in EL0
578         b.eq    el0_ia
579         cmp     x24, #ESR_ELx_EC_FP_ASIMD       // FP/ASIMD access
580         b.eq    el0_fpsimd_acc
581         cmp     x24, #ESR_ELx_EC_FP_EXC32       // FP/ASIMD exception
582         b.eq    el0_fpsimd_exc
583         cmp     x24, #ESR_ELx_EC_PC_ALIGN       // pc alignment exception
584         b.eq    el0_sp_pc
585         cmp     x24, #ESR_ELx_EC_UNKNOWN        // unknown exception in EL0
586         b.eq    el0_undef
587         cmp     x24, #ESR_ELx_EC_CP15_32        // CP15 MRC/MCR trap
588         b.eq    el0_undef
589         cmp     x24, #ESR_ELx_EC_CP15_64        // CP15 MRRC/MCRR trap
590         b.eq    el0_undef
591         cmp     x24, #ESR_ELx_EC_CP14_MR        // CP14 MRC/MCR trap
592         b.eq    el0_undef
593         cmp     x24, #ESR_ELx_EC_CP14_LS        // CP14 LDC/STC trap
594         b.eq    el0_undef
595         cmp     x24, #ESR_ELx_EC_CP14_64        // CP14 MRRC/MCRR trap
596         b.eq    el0_undef
597         cmp     x24, #ESR_ELx_EC_BREAKPT_LOW    // debug exception in EL0
598         b.ge    el0_dbg
599         b       el0_inv
600 el0_svc_compat:
601         /*
602          * AArch32 syscall handling
603          */
604         adrp    stbl, compat_sys_call_table     // load compat syscall table pointer
605         uxtw    scno, w7                        // syscall number in w7 (r7)
606         mov     sc_nr, #__NR_compat_syscalls
607         b       el0_svc_naked
608
609         .align  6
610 el0_irq_compat:
611         kernel_entry 0, 32
612         b       el0_irq_naked
613 #endif
614
615 el0_da:
616         /*
617          * Data abort handling
618          */
619         mrs     x26, far_el1
620         // enable interrupts before calling the main handler
621         enable_dbg_and_irq
622         ct_user_exit
623         clear_address_tag x0, x26
624         mov     x1, x25
625         mov     x2, sp
626         bl      do_mem_abort
627         b       ret_to_user
628 el0_ia:
629         /*
630          * Instruction abort handling
631          */
632         mrs     x26, far_el1
633         msr     daifclr, #(8 | 4 | 1)
634 #ifdef CONFIG_TRACE_IRQFLAGS
635         bl      trace_hardirqs_off
636 #endif
637         ct_user_exit
638         mov     x0, x26
639         mov     x1, x25
640         mov     x2, sp
641         bl      do_el0_ia_bp_hardening
642         b       ret_to_user
643 el0_fpsimd_acc:
644         /*
645          * Floating Point or Advanced SIMD access
646          */
647         enable_dbg
648         ct_user_exit
649         mov     x0, x25
650         mov     x1, sp
651         bl      do_fpsimd_acc
652         b       ret_to_user
653 el0_fpsimd_exc:
654         /*
655          * Floating Point or Advanced SIMD exception
656          */
657         enable_dbg
658         ct_user_exit
659         mov     x0, x25
660         mov     x1, sp
661         bl      do_fpsimd_exc
662         b       ret_to_user
663 el0_sp_pc:
664         /*
665          * Stack or PC alignment exception handling
666          */
667         mrs     x26, far_el1
668         enable_dbg
669 #ifdef CONFIG_TRACE_IRQFLAGS
670         bl      trace_hardirqs_off
671 #endif
672         ct_user_exit
673         mov     x0, x26
674         mov     x1, x25
675         mov     x2, sp
676         bl      do_sp_pc_abort
677         b       ret_to_user
678 el0_undef:
679         /*
680          * Undefined instruction
681          */
682         // enable interrupts before calling the main handler
683         enable_dbg_and_irq
684         ct_user_exit
685         mov     x0, sp
686         bl      do_undefinstr
687         b       ret_to_user
688 el0_sys:
689         /*
690          * System instructions, for trapped cache maintenance instructions
691          */
692         enable_dbg_and_irq
693         ct_user_exit
694         mov     x0, x25
695         mov     x1, sp
696         bl      do_sysinstr
697         b       ret_to_user
698 el0_dbg:
699         /*
700          * Debug exception handling
701          */
702         tbnz    x24, #0, el0_inv                // EL0 only
703         mrs     x0, far_el1
704         mov     x1, x25
705         mov     x2, sp
706         bl      do_debug_exception
707         enable_dbg
708         ct_user_exit
709         b       ret_to_user
710 el0_inv:
711         enable_dbg
712         ct_user_exit
713         mov     x0, sp
714         mov     x1, #BAD_SYNC
715         mov     x2, x25
716         bl      bad_el0_sync
717         b       ret_to_user
718 ENDPROC(el0_sync)
719
720         .align  6
721 el0_irq:
722         kernel_entry 0
723 el0_irq_naked:
724         enable_dbg
725 #ifdef CONFIG_TRACE_IRQFLAGS
726         bl      trace_hardirqs_off
727 #endif
728
729         ct_user_exit
730 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
731         tbz     x22, #55, 1f
732         bl      do_el0_irq_bp_hardening
733 1:
734 #endif
735         irq_handler
736
737 #ifdef CONFIG_TRACE_IRQFLAGS
738         bl      trace_hardirqs_on
739 #endif
740         b       ret_to_user
741 ENDPROC(el0_irq)
742
743 /*
744  * Register switch for AArch64. The callee-saved registers need to be saved
745  * and restored. On entry:
746  *   x0 = previous task_struct (must be preserved across the switch)
747  *   x1 = next task_struct
748  * Previous and next are guaranteed not to be the same.
749  *
750  */
751 ENTRY(cpu_switch_to)
752         mov     x10, #THREAD_CPU_CONTEXT
753         add     x8, x0, x10
754         mov     x9, sp
755         stp     x19, x20, [x8], #16             // store callee-saved registers
756         stp     x21, x22, [x8], #16
757         stp     x23, x24, [x8], #16
758         stp     x25, x26, [x8], #16
759         stp     x27, x28, [x8], #16
760         stp     x29, x9, [x8], #16
761         str     lr, [x8]
762         add     x8, x1, x10
763         ldp     x19, x20, [x8], #16             // restore callee-saved registers
764         ldp     x21, x22, [x8], #16
765         ldp     x23, x24, [x8], #16
766         ldp     x25, x26, [x8], #16
767         ldp     x27, x28, [x8], #16
768         ldp     x29, x9, [x8], #16
769         ldr     lr, [x8]
770         mov     sp, x9
771         and     x9, x9, #~(THREAD_SIZE - 1)
772         msr     sp_el0, x9
773         ret
774 ENDPROC(cpu_switch_to)
775
776 /*
777  * This is the fast syscall return path.  We do as little as possible here,
778  * and this includes saving x0 back into the kernel stack.
779  */
780 ret_fast_syscall:
781         disable_irq                             // disable interrupts
782         str     x0, [sp, #S_X0]                 // returned x0
783         ldr     x1, [tsk, #TI_FLAGS]            // re-check for syscall tracing
784         and     x2, x1, #_TIF_SYSCALL_WORK
785         cbnz    x2, ret_fast_syscall_trace
786         and     x2, x1, #_TIF_WORK_MASK
787         cbnz    x2, work_pending
788         enable_step_tsk x1, x2
789         kernel_exit 0
790 ret_fast_syscall_trace:
791         enable_irq                              // enable interrupts
792         b       __sys_trace_return_skipped      // we already saved x0
793
794 /*
795  * Ok, we need to do extra processing, enter the slow path.
796  */
797 work_pending:
798         mov     x0, sp                          // 'regs'
799         bl      do_notify_resume
800 #ifdef CONFIG_TRACE_IRQFLAGS
801         bl      trace_hardirqs_on               // enabled while in userspace
802 #endif
803         ldr     x1, [tsk, #TI_FLAGS]            // re-check for single-step
804         b       finish_ret_to_user
805 /*
806  * "slow" syscall return path.
807  */
808 ret_to_user:
809         disable_irq                             // disable interrupts
810         ldr     x1, [tsk, #TI_FLAGS]
811         and     x2, x1, #_TIF_WORK_MASK
812         cbnz    x2, work_pending
813 finish_ret_to_user:
814         enable_step_tsk x1, x2
815         kernel_exit 0
816 ENDPROC(ret_to_user)
817
818 /*
819  * This is how we return from a fork.
820  */
821 ENTRY(ret_from_fork)
822         bl      schedule_tail
823         cbz     x19, 1f                         // not a kernel thread
824         mov     x0, x20
825         blr     x19
826 1:      get_thread_info tsk
827         b       ret_to_user
828 ENDPROC(ret_from_fork)
829
830 /*
831  * SVC handler.
832  */
833         .align  6
834 el0_svc:
835         adrp    stbl, sys_call_table            // load syscall table pointer
836         uxtw    scno, w8                        // syscall number in w8
837         mov     sc_nr, #__NR_syscalls
838 el0_svc_naked:                                  // compat entry point
839         stp     x0, scno, [sp, #S_ORIG_X0]      // save the original x0 and syscall number
840         enable_dbg_and_irq
841         ct_user_exit 1
842
843         ldr     x16, [tsk, #TI_FLAGS]           // check for syscall hooks
844         tst     x16, #_TIF_SYSCALL_WORK
845         b.ne    __sys_trace
846         cmp     scno, sc_nr                     // check upper syscall limit
847         b.hs    ni_sys
848         mask_nospec64 scno, sc_nr, x19  // enforce bounds for syscall number
849         ldr     x16, [stbl, scno, lsl #3]       // address in the syscall table
850         blr     x16                             // call sys_* routine
851         b       ret_fast_syscall
852 ni_sys:
853         mov     x0, sp
854         bl      do_ni_syscall
855         b       ret_fast_syscall
856 ENDPROC(el0_svc)
857
858         /*
859          * This is the really slow path.  We're going to be doing context
860          * switches, and waiting for our parent to respond.
861          */
862 __sys_trace:
863         mov     w0, #-1                         // set default errno for
864         cmp     scno, x0                        // user-issued syscall(-1)
865         b.ne    1f
866         mov     x0, #-ENOSYS
867         str     x0, [sp, #S_X0]
868 1:      mov     x0, sp
869         bl      syscall_trace_enter
870         cmp     w0, #-1                         // skip the syscall?
871         b.eq    __sys_trace_return_skipped
872         uxtw    scno, w0                        // syscall number (possibly new)
873         mov     x1, sp                          // pointer to regs
874         cmp     scno, sc_nr                     // check upper syscall limit
875         b.hs    __ni_sys_trace
876         ldp     x0, x1, [sp]                    // restore the syscall args
877         ldp     x2, x3, [sp, #S_X2]
878         ldp     x4, x5, [sp, #S_X4]
879         ldp     x6, x7, [sp, #S_X6]
880         ldr     x16, [stbl, scno, lsl #3]       // address in the syscall table
881         blr     x16                             // call sys_* routine
882
883 __sys_trace_return:
884         str     x0, [sp, #S_X0]                 // save returned x0
885 __sys_trace_return_skipped:
886         mov     x0, sp
887         bl      syscall_trace_exit
888         b       ret_to_user
889
890 __ni_sys_trace:
891         mov     x0, sp
892         bl      do_ni_syscall
893         b       __sys_trace_return
894
895         .popsection                             // .entry.text
896
897         // Move from tramp_pg_dir to swapper_pg_dir
898         .macro tramp_map_kernel, tmp
899         mrs     \tmp, ttbr1_el1
900         sub     \tmp, \tmp, #SWAPPER_DIR_SIZE
901         bic     \tmp, \tmp, #USER_ASID_FLAG
902         msr     ttbr1_el1, \tmp
903         .endm
904
905         .macro tramp_unmap_kernel, tmp
906         mrs     \tmp, ttbr1_el1
907         add     \tmp, \tmp, #SWAPPER_DIR_SIZE
908         orr     \tmp, \tmp, #USER_ASID_FLAG
909         msr     ttbr1_el1, \tmp
910         /*
911          * We avoid running the post_ttbr_update_workaround here because
912          * it's only needed by Cavium ThunderX, which requires KPTI to be
913          * disabled.
914          */
915         .endm
916
917         .macro tramp_data_page  dst
918         adr_l   \dst, .entry.tramp.text
919         sub     \dst, \dst, PAGE_SIZE
920         .endm
921
922         .macro tramp_data_read_var      dst, var
923 #ifdef CONFIG_RANDOMIZE_BASE
924         tramp_data_page         \dst
925         add     \dst, \dst, #:lo12:__entry_tramp_data_\var
926         ldr     \dst, [\dst]
927 #else
928         ldr     \dst, =\var
929 #endif
930         .endm
931
932 #define BHB_MITIGATION_NONE     0
933 #define BHB_MITIGATION_LOOP     1
934 #define BHB_MITIGATION_FW       2
935 #define BHB_MITIGATION_INSN     3
936
937         .macro tramp_ventry, vector_start, regsize, kpti, bhb
938         .align  7
939 1:
940         .if     \regsize == 64
941         msr     tpidrro_el0, x30        // Restored in kernel_ventry
942         .endif
943
944         .if     \bhb == BHB_MITIGATION_LOOP
945         /*
946          * This sequence must appear before the first indirect branch. i.e. the
947          * ret out of tramp_ventry. It appears here because x30 is free.
948          */
949         __mitigate_spectre_bhb_loop     x30
950         .endif // \bhb == BHB_MITIGATION_LOOP
951
952         .if     \bhb == BHB_MITIGATION_INSN
953         clearbhb
954         isb
955         .endif // \bhb == BHB_MITIGATION_INSN
956
957         .if     \kpti == 1
958         /*
959          * Defend against branch aliasing attacks by pushing a dummy
960          * entry onto the return stack and using a RET instruction to
961          * enter the full-fat kernel vectors.
962          */
963         bl      2f
964         b       .
965 2:
966         tramp_map_kernel        x30
967         isb
968         tramp_data_read_var     x30, vectors
969         prfm    plil1strm, [x30, #(1b - \vector_start)]
970         msr     vbar_el1, x30
971         isb
972         .else
973         ldr     x30, =vectors
974         .endif // \kpti == 1
975
976         .if     \bhb == BHB_MITIGATION_FW
977         /*
978          * The firmware sequence must appear before the first indirect branch.
979          * i.e. the ret out of tramp_ventry. But it also needs the stack to be
980          * mapped to save/restore the registers the SMC clobbers.
981          */
982         __mitigate_spectre_bhb_fw
983         .endif // \bhb == BHB_MITIGATION_FW
984
985         add     x30, x30, #(1b - \vector_start + 4)
986         ret
987 .org 1b + 128   // Did we overflow the ventry slot?
988         .endm
989
990         .macro tramp_exit, regsize = 64
991         tramp_data_read_var     x30, this_cpu_vector
992 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
993         mrs     x29, tpidr_el1
994 alternative_else
995         mrs     x29, tpidr_el2
996 alternative_endif
997         ldr     x30, [x30, x29]
998
999         msr     vbar_el1, x30
1000         ldr     lr, [sp, #S_LR]
1001         tramp_unmap_kernel      x29
1002         .if     \regsize == 64
1003         mrs     x29, far_el1
1004         .endif
1005         add     sp, sp, #S_FRAME_SIZE           // restore sp
1006         eret
1007         .endm
1008
1009         .macro  generate_tramp_vector,  kpti, bhb
1010 .Lvector_start\@:
1011         .space  0x400
1012
1013         .rept   4
1014         tramp_ventry    .Lvector_start\@, 64, \kpti, \bhb
1015         .endr
1016         .rept   4
1017         tramp_ventry    .Lvector_start\@, 32, \kpti, \bhb
1018         .endr
1019         .endm
1020
1021 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
1022 /*
1023  * Exception vectors trampoline.
1024  * The order must match __bp_harden_el1_vectors and the
1025  * arm64_bp_harden_el1_vectors enum.
1026  */
1027         .pushsection ".entry.tramp.text", "ax"
1028         .align  11
1029 ENTRY(tramp_vectors)
1030 #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
1031         generate_tramp_vector   kpti=1, bhb=BHB_MITIGATION_LOOP
1032         generate_tramp_vector   kpti=1, bhb=BHB_MITIGATION_FW
1033         generate_tramp_vector   kpti=1, bhb=BHB_MITIGATION_INSN
1034 #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
1035         generate_tramp_vector   kpti=1, bhb=BHB_MITIGATION_NONE
1036 END(tramp_vectors)
1037
1038 ENTRY(tramp_exit_native)
1039         tramp_exit
1040 END(tramp_exit_native)
1041
1042 ENTRY(tramp_exit_compat)
1043         tramp_exit      32
1044 END(tramp_exit_compat)
1045
1046         .ltorg
1047         .popsection                             // .entry.tramp.text
1048 #ifdef CONFIG_RANDOMIZE_BASE
1049         .pushsection ".rodata", "a"
1050         .align PAGE_SHIFT
1051         .globl  __entry_tramp_data_start
1052 __entry_tramp_data_start:
1053 __entry_tramp_data_vectors:
1054         .quad   vectors
1055 #ifdef CONFIG_ARM_SDE_INTERFACE
1056 __entry_tramp_data___sdei_asm_trampoline_next_handler:
1057         .quad   __sdei_asm_handler
1058 #endif /* CONFIG_ARM_SDE_INTERFACE */
1059 __entry_tramp_data_this_cpu_vector:
1060         .quad   this_cpu_vector
1061         .popsection                             // .rodata
1062 #endif /* CONFIG_RANDOMIZE_BASE */
1063 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1064
1065 /*
1066  * Exception vectors for spectre mitigations on entry from EL1 when
1067  * kpti is not in use.
1068  */
1069         .macro generate_el1_vector, bhb
1070 .Lvector_start\@:
1071         kernel_ventry   1, sync_invalid                 // Synchronous EL1t
1072         kernel_ventry   1, irq_invalid                  // IRQ EL1t
1073         kernel_ventry   1, fiq_invalid                  // FIQ EL1t
1074         kernel_ventry   1, error_invalid                // Error EL1t
1075
1076         kernel_ventry   1, sync                         // Synchronous EL1h
1077         kernel_ventry   1, irq                          // IRQ EL1h
1078         kernel_ventry   1, fiq_invalid                  // FIQ EL1h
1079         kernel_ventry   1, error_invalid                // Error EL1h
1080
1081         .rept   4
1082         tramp_ventry    .Lvector_start\@, 64, 0, \bhb
1083         .endr
1084         .rept 4
1085         tramp_ventry    .Lvector_start\@, 32, 0, \bhb
1086         .endr
1087         .endm
1088
1089 /* The order must match tramp_vecs and the arm64_bp_harden_el1_vectors enum. */
1090         .pushsection ".entry.text", "ax"
1091         .align  11
1092 ENTRY(__bp_harden_el1_vectors)
1093 #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
1094         generate_el1_vector     bhb=BHB_MITIGATION_LOOP
1095         generate_el1_vector     bhb=BHB_MITIGATION_FW
1096         generate_el1_vector     bhb=BHB_MITIGATION_INSN
1097 #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
1098 END(__bp_harden_el1_vectors)
1099         .popsection
1100
1101 /*
1102  * Special system call wrappers.
1103  */
1104 ENTRY(sys_rt_sigreturn_wrapper)
1105         mov     x0, sp
1106         b       sys_rt_sigreturn
1107 ENDPROC(sys_rt_sigreturn_wrapper)