2 * Low-level exception handling code
4 * Copyright (C) 2012 ARM Ltd.
5 * Authors: Catalin Marinas <catalin.marinas@arm.com>
6 * Will Deacon <will.deacon@arm.com>
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.
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.
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/>.
21 #include <linux/init.h>
22 #include <linux/linkage.h>
24 #include <asm/assembler.h>
25 #include <asm/asm-offsets.h>
26 #include <asm/errno.h>
28 #include <asm/ptrace.h>
29 #include <asm/thread_info.h>
30 #include <asm/unistd.h>
41 .macro kernel_entry, el, regsize = 64
42 sub sp, sp, #S_FRAME_SIZE - S_LR // room for LR, SP, SPSR, ELR
44 mov w0, w0 // zero upper 32 bits of x0
64 add x21, sp, #S_FRAME_SIZE
68 stp lr, x21, [sp, #S_LR]
69 stp x22, x23, [sp, #S_PC]
72 * Set syscallno to -1 by default (overridden later if real syscall).
76 str x21, [sp, #S_SYSCALLNO]
80 * Registers that may be useful after this macro is invoked:
84 * x23 - aborted PSTATE
88 #ifdef CONFIG_MTK_COMPAT
89 .macro kernel_entry_compat
90 sub sp, sp, #S_FRAME_SIZE - S_X16 // room for LR, SP, SPSR, ELR
91 mov w0, w0 // zero upper 32 bits of x0
93 stp x14, x15, [sp, #-16]!
94 stp x12, x13, [sp, #-16]!
95 stp x10, x11, [sp, #-16]!
96 stp x8, x9, [sp, #-16]!
97 stp x6, x7, [sp, #-16]!
98 stp x4, x5, [sp, #-16]!
99 stp x2, x3, [sp, #-16]!
100 stp x0, x1, [sp, #-16]!
105 stp lr, x21, [sp, #S_LR]
106 stp x22, x23, [sp, #S_PC]
109 * Set syscallno to -1 by default (overridden later if real syscall).
112 str x21, [sp, #S_SYSCALLNO]
115 * Registers that may be useful after this macro is invoked:
119 * x23 - aborted PSTATE
124 .macro kernel_exit, el, ret = 0
125 ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
127 ldr x23, [sp, #S_SP] // load return stack pointer
130 ldr x1, [sp, #S_X1] // preserve x0 (syscall return)
135 pop x2, x3 // load the rest of the registers
139 msr elr_el1, x21 // set up the return data
154 ldr lr, [sp], #S_FRAME_SIZE - S_LR // load LR and restore SP
155 eret // return to kernel
158 #ifdef CONFIG_MTK_COMPAT
159 .macro kernel_exit_compat, ret = 0
160 ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
161 ldr x23, [sp, #S_SP] // load return stack pointer
163 ldr x1, [sp, #S_X1] // preserve x0 (syscall return)
166 ldp x0, x1, [sp], #16
168 ldp x2, x3, [sp], #16 // load the rest of the registers
169 ldp x4, x5, [sp], #16
170 ldp x6, x7, [sp], #16
171 ldp x8, x9, [sp], #16
172 msr elr_el1, x21 // set up the return data
175 ldp x10, x11, [sp], #16
176 ldp x12, x13, [sp], #16
177 ldp x14, x15, [sp], #16
180 ldp x16, x17, [sp], #16
181 ldp x18, x19, [sp], #16
182 ldp x20, x21, [sp], #16
183 ldp x22, x23, [sp], #16
184 ldp x24, x25, [sp], #16
185 ldp x26, x27, [sp], #16
186 ldp x28, x29, [sp], #16
187 ldr lr, [sp], #S_FRAME_SIZE - S_LR // load LR and restore SP
188 eret // return to kernel
189 // could not run here
191 1: add sp, sp, #S_X29-S_X15
192 ldr lr, [sp], #S_FRAME_SIZE - S_LR // load LR and restore SP
193 eret // return to kernel
197 .macro get_thread_info, rd
199 and \rd, \rd, #~(THREAD_SIZE - 1) // top of stack
203 * These are the registers used in the syscall handler, and allow us to
204 * have in theory up to 7 arguments to a function - x0 to x6.
206 * x7 is reserved for the system call number in 32-bit mode.
208 sc_nr .req x25 // number of system calls
209 scno .req x26 // syscall number
210 stbl .req x27 // syscall table pointer
211 tsk .req x28 // current thread_info
214 * Interrupt handling.
217 ldr x1, handle_arch_irq
230 ventry el1_sync_invalid // Synchronous EL1t
231 ventry el1_irq_invalid // IRQ EL1t
232 ventry el1_fiq_invalid // FIQ EL1t
233 ventry el1_error_invalid // Error EL1t
235 ventry el1_sync // Synchronous EL1h
236 ventry el1_irq // IRQ EL1h
237 ventry el1_fiq_invalid // FIQ EL1h
238 ventry el1_error_invalid // Error EL1h
240 ventry el0_sync // Synchronous 64-bit EL0
241 ventry el0_irq // IRQ 64-bit EL0
242 ventry el0_fiq_invalid // FIQ 64-bit EL0
243 ventry el0_error_invalid // Error 64-bit EL0
246 ventry el0_sync_compat // Synchronous 32-bit EL0
247 ventry el0_irq_compat // IRQ 32-bit EL0
248 ventry el0_fiq_invalid_compat // FIQ 32-bit EL0
249 ventry el0_error_invalid_compat // Error 32-bit EL0
251 ventry el0_sync_invalid // Synchronous 32-bit EL0
252 ventry el0_irq_invalid // IRQ 32-bit EL0
253 ventry el0_fiq_invalid // FIQ 32-bit EL0
254 ventry el0_error_invalid // Error 32-bit EL0
259 * Invalid mode handlers
261 .macro inv_entry, el, reason, regsize = 64
262 kernel_entry el, \regsize
270 inv_entry 0, BAD_SYNC
271 ENDPROC(el0_sync_invalid)
275 ENDPROC(el0_irq_invalid)
279 ENDPROC(el0_fiq_invalid)
282 inv_entry 0, BAD_ERROR
283 ENDPROC(el0_error_invalid)
286 el0_fiq_invalid_compat:
287 inv_entry 0, BAD_FIQ, 32
288 ENDPROC(el0_fiq_invalid_compat)
290 el0_error_invalid_compat:
291 inv_entry 0, BAD_ERROR, 32
292 ENDPROC(el0_error_invalid_compat)
296 inv_entry 1, BAD_SYNC
297 ENDPROC(el1_sync_invalid)
301 ENDPROC(el1_irq_invalid)
305 ENDPROC(el1_fiq_invalid)
308 inv_entry 1, BAD_ERROR
309 ENDPROC(el1_error_invalid)
318 and x20, x0, #0xffffffffffffc000
319 ldr w4, [x20, #TI_CPU_EXCP]
321 str w4, [x20, #TI_CPU_EXCP]
324 str x0, [x20, #TI_REGS_ON_EXCP]
327 b.lt el1_sync_nest_skip
328 bl aee_stop_nested_panic
330 mrs x1, esr_el1 // read the syndrome register
331 lsr x24, x1, #ESR_EL1_EC_SHIFT // exception class
332 cmp x24, #ESR_EL1_EC_DABT_EL1 // data abort in EL1
334 cmp x24, #ESR_EL1_EC_SYS64 // configurable trap
336 cmp x24, #ESR_EL1_EC_SP_ALIGN // stack alignment exception
338 cmp x24, #ESR_EL1_EC_PC_ALIGN // pc alignment exception
340 cmp x24, #ESR_EL1_EC_UNKNOWN // unknown exception in EL1
342 cmp x24, #ESR_EL1_EC_BREAKPT_EL1 // debug exception in EL1
347 * Data abort handling
350 enable_dbg_if_not_stepping x2
351 // re-enable interrupts if they were enabled in the aborted context
352 tbnz x23, #7, 1f // PSR_I_BIT
355 mov x2, sp // struct pt_regs
358 and x20, x5, #0xffffffffffffc000
359 ldr w4, [x20, #TI_CPU_EXCP]
361 str w4, [x20, #TI_CPU_EXCP]
363 // disable interrupts before pulling preserved data off the stack
368 * Stack or PC alignment exception handling
375 * Undefined instruction
382 * Debug exception handling
384 cmp x24, #ESR_EL1_EC_BRK64 // if BRK64
385 cinc x24, x24, eq // set bit '0'
386 tbz x24, #0, el1_inv // EL1 only
388 mov x2, sp // struct pt_regs
389 bl do_debug_exception
391 and x20, x5, #0xffffffffffffc000
392 ldr w4, [x20, #TI_CPU_EXCP]
394 str w4, [x20, #TI_CPU_EXCP]
398 // TODO: add support for undefined instructions in kernel mode
408 enable_dbg_if_not_stepping x0
409 #ifdef CONFIG_TRACE_IRQFLAGS
410 bl trace_hardirqs_off
412 #ifdef CONFIG_PREEMPT
414 ldr x24, [tsk, #TI_PREEMPT] // get preempt count
415 add x0, x24, #1 // increment it
416 str x0, [tsk, #TI_PREEMPT]
419 #ifdef CONFIG_PREEMPT
420 str x24, [tsk, #TI_PREEMPT] // restore preempt count
421 cbnz x24, 1f // preempt count != 0
422 ldr x0, [tsk, #TI_FLAGS] // get flags
423 tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
427 #ifdef CONFIG_TRACE_IRQFLAGS
433 #ifdef CONFIG_PREEMPT
437 bl preempt_schedule_irq // irq en/disable is done inside
438 ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
439 tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
449 mrs x25, esr_el1 // read the syndrome register
450 lsr x24, x25, #ESR_EL1_EC_SHIFT // exception class
451 cmp x24, #ESR_EL1_EC_SVC64 // SVC in 64-bit state
453 adr lr, ret_from_exception
454 cmp x24, #ESR_EL1_EC_DABT_EL0 // data abort in EL0
456 cmp x24, #ESR_EL1_EC_IABT_EL0 // instruction abort in EL0
458 cmp x24, #ESR_EL1_EC_FP_ASIMD // FP/ASIMD access
460 cmp x24, #ESR_EL1_EC_FP_EXC64 // FP/ASIMD exception
462 cmp x24, #ESR_EL1_EC_SYS64 // configurable trap
464 cmp x24, #ESR_EL1_EC_SP_ALIGN // stack alignment exception
466 cmp x24, #ESR_EL1_EC_PC_ALIGN // pc alignment exception
468 cmp x24, #ESR_EL1_EC_UNKNOWN // unknown exception in EL0
470 cmp x24, #ESR_EL1_EC_BREAKPT_EL0 // debug exception in EL0
477 #ifdef CONFIG_MTK_COMPAT
482 mrs x25, esr_el1 // read the syndrome register
483 lsr x24, x25, #ESR_EL1_EC_SHIFT // exception class
484 cmp x24, #ESR_EL1_EC_SVC32 // SVC in 32-bit state
486 adr lr, ret_from_exception
487 cmp x24, #ESR_EL1_EC_DABT_EL0 // data abort in EL0
489 cmp x24, #ESR_EL1_EC_IABT_EL0 // instruction abort in EL0
491 cmp x24, #ESR_EL1_EC_FP_ASIMD // FP/ASIMD access
493 cmp x24, #ESR_EL1_EC_FP_EXC32 // FP/ASIMD exception
495 cmp x24, #ESR_EL1_EC_UNKNOWN // unknown exception in EL0
497 cmp x24, #ESR_EL1_EC_CP15_32 // CP15 MRC/MCR trap
499 cmp x24, #ESR_EL1_EC_CP15_64 // CP15 MRRC/MCRR trap
501 cmp x24, #ESR_EL1_EC_CP14_MR // CP14 MRC/MCR trap
503 cmp x24, #ESR_EL1_EC_CP14_LS // CP14 LDC/STC trap
505 cmp x24, #ESR_EL1_EC_CP14_64 // CP14 MRRC/MCRR trap
507 cmp x24, #ESR_EL1_EC_BREAKPT_EL0 // debug exception in EL0
512 * AArch32 syscall handling
514 adr stbl, compat_sys_call_table // load compat syscall table pointer
515 uxtw scno, w7 // syscall number in w7 (r7)
516 mov sc_nr, #__NR_compat_syscalls
521 #ifdef CONFIG_MTK_COMPAT
531 * Data abort handling
534 bic x0, x0, #(0xff << 56)
538 // enable interrupts before calling the main handler
545 * Instruction abort handling
551 // enable interrupts before calling the main handler
553 orr x1, x25, #1 << 24 // use reserved ISS bit for instruction aborts
558 * Floating Point or Advanced SIMD access
565 * Floating Point or Advanced SIMD exception
572 * Stack or PC alignment exception handling
578 // enable interrupts before calling the main handler
585 * Undefined instruction
591 * Debug exception handling
593 tbnz x24, #0, el0_inv // EL0 only
613 #ifdef CONFIG_TRACE_IRQFLAGS
614 bl trace_hardirqs_off
617 #ifdef CONFIG_PREEMPT
618 ldr x24, [tsk, #TI_PREEMPT] // get preempt count
619 add x23, x24, #1 // increment it
620 str x23, [tsk, #TI_PREEMPT]
623 #ifdef CONFIG_PREEMPT
624 ldr x0, [tsk, #TI_PREEMPT]
625 str x24, [tsk, #TI_PREEMPT]
632 #ifdef CONFIG_TRACE_IRQFLAGS
639 * This is the return code to user mode for abort handlers
644 ENDPROC(ret_from_exception)
647 * Register switch for AArch64. The callee-saved registers need to be saved
648 * and restored. On entry:
649 * x0 = previous task_struct (must be preserved across the switch)
650 * x1 = next task_struct
651 * Previous and next are guaranteed not to be the same.
655 add x8, x0, #THREAD_CPU_CONTEXT
657 stp x19, x20, [x8], #16 // store callee-saved registers
658 stp x21, x22, [x8], #16
659 stp x23, x24, [x8], #16
660 stp x25, x26, [x8], #16
661 stp x27, x28, [x8], #16
662 stp x29, x9, [x8], #16
664 add x8, x1, #THREAD_CPU_CONTEXT
665 ldp x19, x20, [x8], #16 // restore callee-saved registers
666 ldp x21, x22, [x8], #16
667 ldp x23, x24, [x8], #16
668 ldp x25, x26, [x8], #16
669 ldp x27, x28, [x8], #16
670 ldp x29, x9, [x8], #16
674 ENDPROC(cpu_switch_to)
677 * This is the fast syscall return path. We do as little as possible here,
678 * and this includes saving x0 back into the kernel stack.
681 disable_irq // disable interrupts
682 ldr x1, [tsk, #TI_FLAGS]
683 and x2, x1, #_TIF_WORK_MASK
684 cbnz x2, fast_work_pending
685 tbz x1, #TIF_SINGLESTEP, fast_exit
689 #ifdef CONFIG_MTK_COMPAT
690 kernel_exit_compat ret = 1
692 kernel_exit 0, ret = 1
696 * Ok, we need to do extra processing, enter the slow path.
699 str x0, [sp, #S_X0] // returned x0
701 tbnz x1, #TIF_NEED_RESCHED, work_resched
702 /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
703 ldr x2, [sp, #S_PSTATE]
705 tst x2, #PSR_MODE_MASK // user mode regs?
706 b.ne no_work_pending // returning to kernel
707 enable_irq // enable interrupts for do_notify_resume()
715 * "slow" syscall return path.
718 disable_irq // disable interrupts
719 ldr x1, [tsk, #TI_FLAGS]
720 and x2, x1, #_TIF_WORK_MASK
721 cbnz x2, work_pending
722 tbz x1, #TIF_SINGLESTEP, no_work_pending
726 #ifdef CONFIG_MTK_COMPAT
727 kernel_exit_compat ret = 0
729 kernel_exit 0, ret = 0
734 * This is how we return from a fork.
738 cbz x19, 1f // not a kernel thread
741 1: get_thread_info tsk
743 ENDPROC(ret_from_fork)
750 adrp stbl, sys_call_table // load syscall table pointer
751 uxtw scno, w8 // syscall number in w8
752 mov sc_nr, #__NR_syscalls
753 el0_svc_naked: // compat entry point
754 stp x0, scno, [sp, #S_ORIG_X0] // save the original x0 and syscall number
761 ldr x16, [tsk, #TI_FLAGS] // check for syscall hooks
762 tst x16, #_TIF_SYSCALL_WORK
764 adr lr, ret_fast_syscall // return address
765 cmp scno, sc_nr // check upper syscall limit
767 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
768 br x16 // call sys_* routine
775 * This is the really slow path. We're going to be doing context
776 * switches, and waiting for our parent to respond.
780 bl syscall_trace_enter
781 adr lr, __sys_trace_return // return address
782 cmp w0, #RET_SKIP_SYSCALL_TRACE // skip syscall and tracing?
784 cmp w0, #RET_SKIP_SYSCALL // skip syscall?
785 b.eq __sys_trace_return_skipped
786 uxtw scno, w0 // syscall number (possibly new)
787 mov x1, sp // pointer to regs
788 cmp scno, sc_nr // check upper syscall limit
790 ldp x0, x1, [sp] // restore the syscall args
791 ldp x2, x3, [sp, #S_X2]
792 ldp x4, x5, [sp, #S_X4]
793 ldp x6, x7, [sp, #S_X6]
794 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
795 br x16 // call sys_* routine
798 str x0, [sp] // save returned x0
799 __sys_trace_return_skipped: // x0 already in regs[0]
801 bl syscall_trace_exit
805 * Special system call wrappers.
807 ENTRY(sys_rt_sigreturn_wrapper)
810 ENDPROC(sys_rt_sigreturn_wrapper)
812 ENTRY(handle_arch_irq)