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/thread_info.h>
29 #include <asm/unistd.h>
30 #include <asm/unistd32.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 .macro kernel_exit, el, ret = 0
89 ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
91 ldr x23, [sp, #S_SP] // load return stack pointer
94 ldr x1, [sp, #S_X1] // preserve x0 (syscall return)
99 pop x2, x3 // load the rest of the registers
103 msr elr_el1, x21 // set up the return data
118 ldr lr, [sp], #S_FRAME_SIZE - S_LR // load LR and restore SP
119 eret // return to kernel
122 .macro get_thread_info, rd
124 and \rd, \rd, #~(THREAD_SIZE - 1) // top of stack
128 * These are the registers used in the syscall handler, and allow us to
129 * have in theory up to 7 arguments to a function - x0 to x6.
131 * x7 is reserved for the system call number in 32-bit mode.
133 sc_nr .req x25 // number of system calls
134 scno .req x26 // syscall number
135 stbl .req x27 // syscall table pointer
136 tsk .req x28 // current thread_info
139 * Interrupt handling.
142 ldr x1, handle_arch_irq
155 ventry el1_sync_invalid // Synchronous EL1t
156 ventry el1_irq_invalid // IRQ EL1t
157 ventry el1_fiq_invalid // FIQ EL1t
158 ventry el1_error_invalid // Error EL1t
160 ventry el1_sync // Synchronous EL1h
161 ventry el1_irq // IRQ EL1h
162 ventry el1_fiq_invalid // FIQ EL1h
163 ventry el1_error_invalid // Error EL1h
165 ventry el0_sync // Synchronous 64-bit EL0
166 ventry el0_irq // IRQ 64-bit EL0
167 ventry el0_fiq_invalid // FIQ 64-bit EL0
168 ventry el0_error_invalid // Error 64-bit EL0
171 ventry el0_sync_compat // Synchronous 32-bit EL0
172 ventry el0_irq_compat // IRQ 32-bit EL0
173 ventry el0_fiq_invalid_compat // FIQ 32-bit EL0
174 ventry el0_error_invalid_compat // Error 32-bit EL0
176 ventry el0_sync_invalid // Synchronous 32-bit EL0
177 ventry el0_irq_invalid // IRQ 32-bit EL0
178 ventry el0_fiq_invalid // FIQ 32-bit EL0
179 ventry el0_error_invalid // Error 32-bit EL0
184 * Invalid mode handlers
186 .macro inv_entry, el, reason, regsize = 64
187 kernel_entry el, \regsize
195 inv_entry 0, BAD_SYNC
196 ENDPROC(el0_sync_invalid)
200 ENDPROC(el0_irq_invalid)
204 ENDPROC(el0_fiq_invalid)
207 inv_entry 0, BAD_ERROR
208 ENDPROC(el0_error_invalid)
211 el0_fiq_invalid_compat:
212 inv_entry 0, BAD_FIQ, 32
213 ENDPROC(el0_fiq_invalid_compat)
215 el0_error_invalid_compat:
216 inv_entry 0, BAD_ERROR, 32
217 ENDPROC(el0_error_invalid_compat)
221 inv_entry 1, BAD_SYNC
222 ENDPROC(el1_sync_invalid)
226 ENDPROC(el1_irq_invalid)
230 ENDPROC(el1_fiq_invalid)
233 inv_entry 1, BAD_ERROR
234 ENDPROC(el1_error_invalid)
242 mrs x1, esr_el1 // read the syndrome register
243 lsr x24, x1, #ESR_EL1_EC_SHIFT // exception class
244 cmp x24, #ESR_EL1_EC_DABT_EL1 // data abort in EL1
246 cmp x24, #ESR_EL1_EC_SYS64 // configurable trap
248 cmp x24, #ESR_EL1_EC_SP_ALIGN // stack alignment exception
250 cmp x24, #ESR_EL1_EC_PC_ALIGN // pc alignment exception
252 cmp x24, #ESR_EL1_EC_UNKNOWN // unknown exception in EL1
254 cmp x24, #ESR_EL1_EC_BREAKPT_EL1 // debug exception in EL1
259 * Data abort handling
262 enable_dbg_if_not_stepping x2
263 // re-enable interrupts if they were enabled in the aborted context
264 tbnz x23, #7, 1f // PSR_I_BIT
267 mov x2, sp // struct pt_regs
270 // disable interrupts before pulling preserved data off the stack
275 * Stack or PC alignment exception handling
283 * Undefined instruction
289 * Debug exception handling
291 tbz x24, #0, el1_inv // EL1 only
293 mov x2, sp // struct pt_regs
294 bl do_debug_exception
298 // TODO: add support for undefined instructions in kernel mode
308 enable_dbg_if_not_stepping x0
309 #ifdef CONFIG_TRACE_IRQFLAGS
310 bl trace_hardirqs_off
312 #ifdef CONFIG_PREEMPT
314 ldr x24, [tsk, #TI_PREEMPT] // get preempt count
315 add x0, x24, #1 // increment it
316 str x0, [tsk, #TI_PREEMPT]
319 #ifdef CONFIG_PREEMPT
320 str x24, [tsk, #TI_PREEMPT] // restore preempt count
321 cbnz x24, 1f // preempt count != 0
322 ldr x0, [tsk, #TI_FLAGS] // get flags
323 tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
327 #ifdef CONFIG_TRACE_IRQFLAGS
333 #ifdef CONFIG_PREEMPT
337 bl preempt_schedule_irq // irq en/disable is done inside
338 ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
339 tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
349 mrs x25, esr_el1 // read the syndrome register
350 lsr x24, x25, #ESR_EL1_EC_SHIFT // exception class
351 cmp x24, #ESR_EL1_EC_SVC64 // SVC in 64-bit state
353 adr lr, ret_from_exception
354 cmp x24, #ESR_EL1_EC_DABT_EL0 // data abort in EL0
356 cmp x24, #ESR_EL1_EC_IABT_EL0 // instruction abort in EL0
358 cmp x24, #ESR_EL1_EC_FP_ASIMD // FP/ASIMD access
360 cmp x24, #ESR_EL1_EC_FP_EXC64 // FP/ASIMD exception
362 cmp x24, #ESR_EL1_EC_SYS64 // configurable trap
364 cmp x24, #ESR_EL1_EC_SP_ALIGN // stack alignment exception
366 cmp x24, #ESR_EL1_EC_PC_ALIGN // pc alignment exception
368 cmp x24, #ESR_EL1_EC_UNKNOWN // unknown exception in EL0
370 cmp x24, #ESR_EL1_EC_BREAKPT_EL0 // debug exception in EL0
378 mrs x25, esr_el1 // read the syndrome register
379 lsr x24, x25, #ESR_EL1_EC_SHIFT // exception class
380 cmp x24, #ESR_EL1_EC_SVC32 // SVC in 32-bit state
382 adr lr, ret_from_exception
383 cmp x24, #ESR_EL1_EC_DABT_EL0 // data abort in EL0
385 cmp x24, #ESR_EL1_EC_IABT_EL0 // instruction abort in EL0
387 cmp x24, #ESR_EL1_EC_FP_ASIMD // FP/ASIMD access
389 cmp x24, #ESR_EL1_EC_FP_EXC32 // FP/ASIMD exception
391 cmp x24, #ESR_EL1_EC_UNKNOWN // unknown exception in EL0
393 cmp x24, #ESR_EL1_EC_CP15_32 // CP15 MRC/MCR trap
395 cmp x24, #ESR_EL1_EC_CP15_64 // CP15 MRRC/MCRR trap
397 cmp x24, #ESR_EL1_EC_CP14_MR // CP14 MRC/MCR trap
399 cmp x24, #ESR_EL1_EC_CP14_LS // CP14 LDC/STC trap
401 cmp x24, #ESR_EL1_EC_CP14_64 // CP14 MRRC/MCRR trap
403 cmp x24, #ESR_EL1_EC_BREAKPT_EL0 // debug exception in EL0
408 * AArch32 syscall handling
410 adr stbl, compat_sys_call_table // load compat syscall table pointer
411 uxtw scno, w7 // syscall number in w7 (r7)
412 mov sc_nr, #__NR_compat_syscalls
423 * Data abort handling
429 // enable interrupts before calling the main handler
436 * Instruction abort handling
442 // enable interrupts before calling the main handler
444 orr x1, x25, #1 << 24 // use reserved ISS bit for instruction aborts
449 * Floating Point or Advanced SIMD access
456 * Floating Point or Advanced SIMD exception
463 * Stack or PC alignment exception handling
469 // enable interrupts before calling the main handler
476 * Undefined instruction
482 * Debug exception handling
484 tbnz x24, #0, el0_inv // EL0 only
504 #ifdef CONFIG_TRACE_IRQFLAGS
505 bl trace_hardirqs_off
508 #ifdef CONFIG_PREEMPT
509 ldr x24, [tsk, #TI_PREEMPT] // get preempt count
510 add x23, x24, #1 // increment it
511 str x23, [tsk, #TI_PREEMPT]
514 #ifdef CONFIG_PREEMPT
515 ldr x0, [tsk, #TI_PREEMPT]
516 str x24, [tsk, #TI_PREEMPT]
523 #ifdef CONFIG_TRACE_IRQFLAGS
530 * This is the return code to user mode for abort handlers
535 ENDPROC(ret_from_exception)
538 * Register switch for AArch64. The callee-saved registers need to be saved
539 * and restored. On entry:
540 * x0 = previous task_struct (must be preserved across the switch)
541 * x1 = next task_struct
542 * Previous and next are guaranteed not to be the same.
546 add x8, x0, #THREAD_CPU_CONTEXT
548 stp x19, x20, [x8], #16 // store callee-saved registers
549 stp x21, x22, [x8], #16
550 stp x23, x24, [x8], #16
551 stp x25, x26, [x8], #16
552 stp x27, x28, [x8], #16
553 stp x29, x9, [x8], #16
555 add x8, x1, #THREAD_CPU_CONTEXT
556 ldp x19, x20, [x8], #16 // restore callee-saved registers
557 ldp x21, x22, [x8], #16
558 ldp x23, x24, [x8], #16
559 ldp x25, x26, [x8], #16
560 ldp x27, x28, [x8], #16
561 ldp x29, x9, [x8], #16
565 ENDPROC(cpu_switch_to)
568 * This is the fast syscall return path. We do as little as possible here,
569 * and this includes saving x0 back into the kernel stack.
572 disable_irq // disable interrupts
573 ldr x1, [tsk, #TI_FLAGS]
574 and x2, x1, #_TIF_WORK_MASK
575 cbnz x2, fast_work_pending
576 tbz x1, #TIF_SINGLESTEP, fast_exit
580 kernel_exit 0, ret = 1
583 * Ok, we need to do extra processing, enter the slow path.
586 str x0, [sp, #S_X0] // returned x0
588 tbnz x1, #TIF_NEED_RESCHED, work_resched
589 /* TIF_SIGPENDING or TIF_NOTIFY_RESUME case */
590 ldr x2, [sp, #S_PSTATE]
592 tst x2, #PSR_MODE_MASK // user mode regs?
593 b.ne no_work_pending // returning to kernel
594 enable_irq // enable interrupts for do_notify_resume()
602 * "slow" syscall return path.
605 disable_irq // disable interrupts
606 ldr x1, [tsk, #TI_FLAGS]
607 and x2, x1, #_TIF_WORK_MASK
608 cbnz x2, work_pending
609 tbz x1, #TIF_SINGLESTEP, no_work_pending
613 kernel_exit 0, ret = 0
617 * This is how we return from a fork.
621 cbz x19, 1f // not a kernel thread
624 1: get_thread_info tsk
626 ENDPROC(ret_from_fork)
633 adrp stbl, sys_call_table // load syscall table pointer
634 uxtw scno, w8 // syscall number in w8
635 mov sc_nr, #__NR_syscalls
636 el0_svc_naked: // compat entry point
637 stp x0, scno, [sp, #S_ORIG_X0] // save the original x0 and syscall number
644 ldr x16, [tsk, #TI_FLAGS] // check for syscall tracing
645 tbnz x16, #TIF_SYSCALL_TRACE, __sys_trace // are we tracing syscalls?
646 adr lr, ret_fast_syscall // return address
647 cmp scno, sc_nr // check upper syscall limit
649 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
650 br x16 // call sys_* routine
657 * This is the really slow path. We're going to be doing context
658 * switches, and waiting for our parent to respond.
662 mov w0, #0 // trace entry
664 adr lr, __sys_trace_return // return address
665 uxtw scno, w0 // syscall number (possibly new)
666 mov x1, sp // pointer to regs
667 cmp scno, sc_nr // check upper syscall limit
669 ldp x0, x1, [sp] // restore the syscall args
670 ldp x2, x3, [sp, #S_X2]
671 ldp x4, x5, [sp, #S_X4]
672 ldp x6, x7, [sp, #S_X6]
673 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
674 br x16 // call sys_* routine
677 str x0, [sp] // save returned x0
679 mov w0, #1 // trace exit
684 * Special system call wrappers.
686 ENTRY(sys_rt_sigreturn_wrapper)
689 ENDPROC(sys_rt_sigreturn_wrapper)
691 ENTRY(handle_arch_irq)