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