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>
27 #include <asm/thread_info.h>
28 #include <asm/unistd.h>
39 .macro kernel_entry, el, regsize = 64
40 sub sp, sp, #S_FRAME_SIZE - S_LR // room for LR, SP, SPSR, ELR
42 mov w0, w0 // zero upper 32 bits of x0
62 add x21, sp, #S_FRAME_SIZE
66 stp lr, x21, [sp, #S_LR]
67 stp x22, x23, [sp, #S_PC]
70 * Set syscallno to -1 by default (overridden later if real syscall).
74 str x21, [sp, #S_SYSCALLNO]
78 * Registers that may be useful after this macro is invoked:
82 * x23 - aborted PSTATE
86 .macro kernel_exit, el, ret = 0
87 ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
89 ldr x23, [sp, #S_SP] // load return stack pointer
92 ldr x1, [sp, #S_X1] // preserve x0 (syscall return)
97 pop x2, x3 // load the rest of the registers
101 msr elr_el1, x21 // set up the return data
116 ldr lr, [sp], #S_FRAME_SIZE - S_LR // load LR and restore SP
117 eret // return to kernel
120 .macro get_thread_info, rd
122 and \rd, \rd, #~((1 << 13) - 1) // top of 8K stack
126 * These are the registers used in the syscall handler, and allow us to
127 * have in theory up to 7 arguments to a function - x0 to x6.
129 * x7 is reserved for the system call number in 32-bit mode.
131 sc_nr .req x25 // number of system calls
132 scno .req x26 // syscall number
133 stbl .req x27 // syscall table pointer
134 tsk .req x28 // current thread_info
137 * Interrupt handling.
140 ldr x1, handle_arch_irq
157 ventry el1_sync_invalid // Synchronous EL1t
158 ventry el1_irq_invalid // IRQ EL1t
159 ventry el1_fiq_invalid // FIQ EL1t
160 ventry el1_error_invalid // Error EL1t
162 ventry el1_sync // Synchronous EL1h
163 ventry el1_irq // IRQ EL1h
164 ventry el1_fiq_invalid // FIQ EL1h
165 ventry el1_error_invalid // Error EL1h
167 ventry el0_sync // Synchronous 64-bit EL0
168 ventry el0_irq // IRQ 64-bit EL0
169 ventry el0_fiq_invalid // FIQ 64-bit EL0
170 ventry el0_error_invalid // Error 64-bit EL0
173 ventry el0_sync_compat // Synchronous 32-bit EL0
174 ventry el0_irq_compat // IRQ 32-bit EL0
175 ventry el0_fiq_invalid_compat // FIQ 32-bit EL0
176 ventry el0_error_invalid_compat // Error 32-bit EL0
178 ventry el0_sync_invalid // Synchronous 32-bit EL0
179 ventry el0_irq_invalid // IRQ 32-bit EL0
180 ventry el0_fiq_invalid // FIQ 32-bit EL0
181 ventry el0_error_invalid // Error 32-bit EL0
186 * Invalid mode handlers
188 .macro inv_entry, el, reason, regsize = 64
189 kernel_entry el, \regsize
197 inv_entry 0, BAD_SYNC
198 ENDPROC(el0_sync_invalid)
202 ENDPROC(el0_irq_invalid)
206 ENDPROC(el0_fiq_invalid)
209 inv_entry 0, BAD_ERROR
210 ENDPROC(el0_error_invalid)
213 el0_fiq_invalid_compat:
214 inv_entry 0, BAD_FIQ, 32
215 ENDPROC(el0_fiq_invalid_compat)
217 el0_error_invalid_compat:
218 inv_entry 0, BAD_ERROR, 32
219 ENDPROC(el0_error_invalid_compat)
223 inv_entry 1, BAD_SYNC
224 ENDPROC(el1_sync_invalid)
228 ENDPROC(el1_irq_invalid)
232 ENDPROC(el1_fiq_invalid)
235 inv_entry 1, BAD_ERROR
236 ENDPROC(el1_error_invalid)
244 mrs x1, esr_el1 // read the syndrome register
245 lsr x24, x1, #26 // exception class
246 cmp x24, #0x25 // data abort in EL1
248 cmp x24, #0x18 // configurable trap
250 cmp x24, #0x26 // stack alignment exception
252 cmp x24, #0x22 // pc alignment exception
254 cmp x24, #0x00 // unknown exception in EL1
256 cmp x24, #0x30 // debug exception in EL1
261 * Data abort handling
264 enable_dbg_if_not_stepping x2
265 // re-enable interrupts if they were enabled in the aborted context
266 tbnz x23, #7, 1f // PSR_I_BIT
269 mov x2, sp // struct pt_regs
272 // disable interrupts before pulling preserved data off the stack
277 * Stack or PC alignment exception handling
285 * Undefined instruction
291 * Debug exception handling
293 tbz x24, #0, el1_inv // EL1 only
295 mov x2, sp // struct pt_regs
296 bl do_debug_exception
300 // TODO: add support for undefined instructions in kernel mode
310 enable_dbg_if_not_stepping x0
311 #ifdef CONFIG_TRACE_IRQFLAGS
312 bl trace_hardirqs_off
314 #ifdef CONFIG_PREEMPT
316 ldr x24, [tsk, #TI_PREEMPT] // get preempt count
317 add x0, x24, #1 // increment it
318 str x0, [tsk, #TI_PREEMPT]
321 #ifdef CONFIG_PREEMPT
322 str x24, [tsk, #TI_PREEMPT] // restore preempt count
323 cbnz x24, 1f // preempt count != 0
324 ldr x0, [tsk, #TI_FLAGS] // get flags
325 tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
329 #ifdef CONFIG_TRACE_IRQFLAGS
335 #ifdef CONFIG_PREEMPT
339 bl preempt_schedule_irq // irq en/disable is done inside
340 ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
341 tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
351 mrs x25, esr_el1 // read the syndrome register
352 lsr x24, x25, #26 // exception class
353 cmp x24, #0x15 // SVC in 64-bit state
355 adr lr, ret_from_exception
356 cmp x24, #0x24 // data abort in EL0
358 cmp x24, #0x20 // instruction abort in EL0
360 cmp x24, #0x07 // FP/ASIMD access
362 cmp x24, #0x2c // FP/ASIMD exception
364 cmp x24, #0x18 // configurable trap
366 cmp x24, #0x26 // stack alignment exception
368 cmp x24, #0x22 // pc alignment exception
370 cmp x24, #0x00 // unknown exception in EL0
372 cmp x24, #0x30 // debug exception in EL0
380 mrs x25, esr_el1 // read the syndrome register
381 lsr x24, x25, #26 // exception class
382 cmp x24, #0x11 // SVC in 32-bit state
384 adr lr, ret_from_exception
385 cmp x24, #0x24 // data abort in EL0
387 cmp x24, #0x20 // instruction abort in EL0
389 cmp x24, #0x07 // FP/ASIMD access
391 cmp x24, #0x28 // FP/ASIMD exception
393 cmp x24, #0x00 // unknown exception in EL0
395 cmp x24, #0x30 // debug exception in EL0
400 * AArch32 syscall handling
402 adr stbl, compat_sys_call_table // load compat syscall table pointer
403 uxtw scno, w7 // syscall number in w7 (r7)
404 mov sc_nr, #__NR_compat_syscalls
415 * Data abort handling
421 // enable interrupts before calling the main handler
428 * Instruction abort handling
434 // enable interrupts before calling the main handler
436 orr x1, x25, #1 << 24 // use reserved ISS bit for instruction aborts
441 * Floating Point or Advanced SIMD access
448 * Floating Point or Advanced SIMD exception
455 * Stack or PC alignment exception handling
461 // enable interrupts before calling the main handler
468 * Undefined instruction
474 * Debug exception handling
476 tbnz x24, #0, el0_inv // EL0 only
496 #ifdef CONFIG_TRACE_IRQFLAGS
497 bl trace_hardirqs_off
500 #ifdef CONFIG_PREEMPT
501 ldr x24, [tsk, #TI_PREEMPT] // get preempt count
502 add x23, x24, #1 // increment it
503 str x23, [tsk, #TI_PREEMPT]
506 #ifdef CONFIG_PREEMPT
507 ldr x0, [tsk, #TI_PREEMPT]
508 str x24, [tsk, #TI_PREEMPT]
515 #ifdef CONFIG_TRACE_IRQFLAGS
522 * This is the return code to user mode for abort handlers
527 ENDPROC(ret_from_exception)
530 * Register switch for AArch64. The callee-saved registers need to be saved
531 * and restored. On entry:
532 * x0 = previous task_struct (must be preserved across the switch)
533 * x1 = next task_struct
534 * Previous and next are guaranteed not to be the same.
538 add x8, x0, #THREAD_CPU_CONTEXT
540 stp x19, x20, [x8], #16 // store callee-saved registers
541 stp x21, x22, [x8], #16
542 stp x23, x24, [x8], #16
543 stp x25, x26, [x8], #16
544 stp x27, x28, [x8], #16
545 stp x29, x9, [x8], #16
547 add x8, x1, #THREAD_CPU_CONTEXT
548 ldp x19, x20, [x8], #16 // restore callee-saved registers
549 ldp x21, x22, [x8], #16
550 ldp x23, x24, [x8], #16
551 ldp x25, x26, [x8], #16
552 ldp x27, x28, [x8], #16
553 ldp x29, x9, [x8], #16
557 ENDPROC(cpu_switch_to)
560 * This is the fast syscall return path. We do as little as possible here,
561 * and this includes saving x0 back into the kernel stack.
564 disable_irq // disable interrupts
565 ldr x1, [tsk, #TI_FLAGS]
566 and x2, x1, #_TIF_WORK_MASK
567 cbnz x2, fast_work_pending
568 tbz x1, #TIF_SINGLESTEP, fast_exit
572 kernel_exit 0, ret = 1
575 * Ok, we need to do extra processing, enter the slow path.
578 str x0, [sp, #S_X0] // returned x0
580 tbnz x1, #TIF_NEED_RESCHED, work_resched
581 /* TIF_SIGPENDING or TIF_NOTIFY_RESUME case */
582 ldr x2, [sp, #S_PSTATE]
584 tst x2, #PSR_MODE_MASK // user mode regs?
585 b.ne no_work_pending // returning to kernel
593 * "slow" syscall return path.
596 disable_irq // disable interrupts
597 ldr x1, [tsk, #TI_FLAGS]
598 and x2, x1, #_TIF_WORK_MASK
599 cbnz x2, work_pending
600 tbz x1, #TIF_SINGLESTEP, no_work_pending
604 kernel_exit 0, ret = 0
608 * This is how we return from a fork.
614 ENDPROC(ret_from_fork)
621 adrp stbl, sys_call_table // load syscall table pointer
622 uxtw scno, w8 // syscall number in w8
623 mov sc_nr, #__NR_syscalls
624 el0_svc_naked: // compat entry point
625 stp x0, scno, [sp, #S_ORIG_X0] // save the original x0 and syscall number
632 ldr x16, [tsk, #TI_FLAGS] // check for syscall tracing
633 tbnz x16, #TIF_SYSCALL_TRACE, __sys_trace // are we tracing syscalls?
634 adr lr, ret_fast_syscall // return address
635 cmp scno, sc_nr // check upper syscall limit
637 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
638 br x16 // call sys_* routine
645 * This is the really slow path. We're going to be doing context
646 * switches, and waiting for our parent to respond.
650 mov w0, #0 // trace entry
652 adr lr, __sys_trace_return // return address
653 uxtw scno, w0 // syscall number (possibly new)
654 mov x1, sp // pointer to regs
655 cmp scno, sc_nr // check upper syscall limit
657 ldp x0, x1, [sp] // restore the syscall args
658 ldp x2, x3, [sp, #S_X2]
659 ldp x4, x5, [sp, #S_X4]
660 ldp x6, x7, [sp, #S_X6]
661 ldr x16, [stbl, scno, lsl #3] // address in the syscall table
662 br x16 // call sys_* routine
665 str x0, [sp] // save returned x0
667 mov w0, #1 // trace exit
672 * Special system call wrappers.
674 ENTRY(sys_execve_wrapper)
677 ENDPROC(sys_execve_wrapper)
679 ENTRY(sys_clone_wrapper)
682 ENDPROC(sys_clone_wrapper)
684 ENTRY(sys_rt_sigreturn_wrapper)
687 ENDPROC(sys_rt_sigreturn_wrapper)
689 ENTRY(sys_sigaltstack_wrapper)
692 ENDPROC(sys_sigaltstack_wrapper)
694 ENTRY(handle_arch_irq)