2 * Based on arch/arm/kernel/traps.c
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/bug.h>
21 #include <linux/signal.h>
22 #include <linux/personality.h>
23 #include <linux/kallsyms.h>
24 #include <linux/spinlock.h>
25 #include <linux/uaccess.h>
26 #include <linux/hardirq.h>
27 #include <linux/kdebug.h>
28 #include <linux/module.h>
29 #include <linux/kexec.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/sched/signal.h>
33 #include <linux/sched/debug.h>
34 #include <linux/sched/task_stack.h>
35 #include <linux/sizes.h>
36 #include <linux/syscalls.h>
37 #include <linux/mm_types.h>
39 #include <asm/atomic.h>
41 #include <asm/debug-monitors.h>
44 #include <asm/traps.h>
46 #include <asm/stack_pointer.h>
47 #include <asm/stacktrace.h>
48 #include <asm/exception.h>
49 #include <asm/system_misc.h>
50 #include <asm/sysreg.h>
52 static const char *handler
[]= {
59 int show_unhandled_signals
= 1;
61 static void dump_backtrace_entry(unsigned long where
)
63 printk(" %pS\n", (void *)where
);
66 static void __dump_instr(const char *lvl
, struct pt_regs
*regs
)
68 unsigned long addr
= instruction_pointer(regs
);
69 char str
[sizeof("00000000 ") * 5 + 2 + 1], *p
= str
;
72 for (i
= -4; i
< 1; i
++) {
73 unsigned int val
, bad
;
75 bad
= __get_user(val
, &((u32
*)addr
)[i
]);
78 p
+= sprintf(p
, i
== 0 ? "(%08x) " : "%08x ", val
);
80 p
+= sprintf(p
, "bad PC value");
84 printk("%sCode: %s\n", lvl
, str
);
87 static void dump_instr(const char *lvl
, struct pt_regs
*regs
)
89 if (!user_mode(regs
)) {
90 mm_segment_t fs
= get_fs();
92 __dump_instr(lvl
, regs
);
95 __dump_instr(lvl
, regs
);
99 void dump_backtrace(struct pt_regs
*regs
, struct task_struct
*tsk
)
101 struct stackframe frame
;
104 pr_debug("%s(regs = %p tsk = %p)\n", __func__
, regs
, tsk
);
109 if (!try_get_task_stack(tsk
))
112 if (tsk
== current
) {
113 frame
.fp
= (unsigned long)__builtin_frame_address(0);
114 frame
.pc
= (unsigned long)dump_backtrace
;
117 * task blocked in __switch_to
119 frame
.fp
= thread_saved_fp(tsk
);
120 frame
.pc
= thread_saved_pc(tsk
);
122 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
123 frame
.graph
= tsk
->curr_ret_stack
;
127 printk("Call trace:\n");
129 /* skip until specified stack frame */
131 dump_backtrace_entry(frame
.pc
);
132 } else if (frame
.fp
== regs
->regs
[29]) {
135 * Mostly, this is the case where this function is
136 * called in panic/abort. As exception handler's
137 * stack frame does not contain the corresponding pc
138 * at which an exception has taken place, use regs->pc
141 dump_backtrace_entry(regs
->pc
);
143 } while (!unwind_frame(tsk
, &frame
));
148 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
150 dump_backtrace(NULL
, tsk
);
154 #ifdef CONFIG_PREEMPT
155 #define S_PREEMPT " PREEMPT"
161 static int __die(const char *str
, int err
, struct pt_regs
*regs
)
163 struct task_struct
*tsk
= current
;
164 static int die_counter
;
167 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP
"\n",
168 str
, err
, ++die_counter
);
170 /* trap and error numbers are mostly meaningless on ARM */
171 ret
= notify_die(DIE_OOPS
, str
, regs
, err
, 0, SIGSEGV
);
172 if (ret
== NOTIFY_STOP
)
177 pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
178 TASK_COMM_LEN
, tsk
->comm
, task_pid_nr(tsk
),
181 if (!user_mode(regs
)) {
182 dump_backtrace(regs
, tsk
);
183 dump_instr(KERN_EMERG
, regs
);
189 static DEFINE_RAW_SPINLOCK(die_lock
);
192 * This function is protected against re-entrancy.
194 void die(const char *str
, struct pt_regs
*regs
, int err
)
199 raw_spin_lock_irqsave(&die_lock
, flags
);
205 ret
= __die(str
, err
, regs
);
207 if (regs
&& kexec_should_crash(current
))
211 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
215 panic("Fatal exception in interrupt");
217 panic("Fatal exception");
219 raw_spin_unlock_irqrestore(&die_lock
, flags
);
221 if (ret
!= NOTIFY_STOP
)
225 void arm64_notify_die(const char *str
, struct pt_regs
*regs
,
226 struct siginfo
*info
, int err
)
228 if (user_mode(regs
)) {
229 current
->thread
.fault_address
= 0;
230 current
->thread
.fault_code
= err
;
231 force_sig_info(info
->si_signo
, info
, current
);
237 void arm64_skip_faulting_instruction(struct pt_regs
*regs
, unsigned long size
)
242 * If we were single stepping, we want to get the step exception after
243 * we return from the trap.
245 user_fastforward_single_step(current
);
248 static LIST_HEAD(undef_hook
);
249 static DEFINE_RAW_SPINLOCK(undef_lock
);
251 void register_undef_hook(struct undef_hook
*hook
)
255 raw_spin_lock_irqsave(&undef_lock
, flags
);
256 list_add(&hook
->node
, &undef_hook
);
257 raw_spin_unlock_irqrestore(&undef_lock
, flags
);
260 void unregister_undef_hook(struct undef_hook
*hook
)
264 raw_spin_lock_irqsave(&undef_lock
, flags
);
265 list_del(&hook
->node
);
266 raw_spin_unlock_irqrestore(&undef_lock
, flags
);
269 static int call_undef_hook(struct pt_regs
*regs
)
271 struct undef_hook
*hook
;
274 int (*fn
)(struct pt_regs
*regs
, u32 instr
) = NULL
;
275 void __user
*pc
= (void __user
*)instruction_pointer(regs
);
277 if (!user_mode(regs
))
280 if (compat_thumb_mode(regs
)) {
281 /* 16-bit Thumb instruction */
283 if (get_user(instr_le
, (__le16 __user
*)pc
))
285 instr
= le16_to_cpu(instr_le
);
286 if (aarch32_insn_is_wide(instr
)) {
289 if (get_user(instr_le
, (__le16 __user
*)(pc
+ 2)))
291 instr2
= le16_to_cpu(instr_le
);
292 instr
= (instr
<< 16) | instr2
;
295 /* 32-bit ARM instruction */
297 if (get_user(instr_le
, (__le32 __user
*)pc
))
299 instr
= le32_to_cpu(instr_le
);
302 raw_spin_lock_irqsave(&undef_lock
, flags
);
303 list_for_each_entry(hook
, &undef_hook
, node
)
304 if ((instr
& hook
->instr_mask
) == hook
->instr_val
&&
305 (regs
->pstate
& hook
->pstate_mask
) == hook
->pstate_val
)
308 raw_spin_unlock_irqrestore(&undef_lock
, flags
);
310 return fn
? fn(regs
, instr
) : 1;
313 static void force_signal_inject(int signal
, int code
, struct pt_regs
*regs
,
314 unsigned long address
)
317 void __user
*pc
= (void __user
*)instruction_pointer(regs
);
322 desc
= "undefined instruction";
325 desc
= "illegal memory access";
332 if (unhandled_signal(current
, signal
) &&
333 show_unhandled_signals_ratelimited()) {
334 pr_info("%s[%d]: %s: pc=%p\n",
335 current
->comm
, task_pid_nr(current
), desc
, pc
);
336 dump_instr(KERN_INFO
, regs
);
339 info
.si_signo
= signal
;
344 arm64_notify_die(desc
, regs
, &info
, 0);
348 * Set up process info to signal segmentation fault - called on access error.
350 void arm64_notify_segfault(struct pt_regs
*regs
, unsigned long addr
)
354 down_read(¤t
->mm
->mmap_sem
);
355 if (find_vma(current
->mm
, addr
) == NULL
)
359 up_read(¤t
->mm
->mmap_sem
);
361 force_signal_inject(SIGSEGV
, code
, regs
, addr
);
364 asmlinkage
void __exception
do_undefinstr(struct pt_regs
*regs
)
366 /* check for AArch32 breakpoint instructions */
367 if (!aarch32_break_handler(regs
))
370 if (call_undef_hook(regs
) == 0)
373 force_signal_inject(SIGILL
, ILL_ILLOPC
, regs
, 0);
376 int cpu_enable_cache_maint_trap(void *__unused
)
378 config_sctlr_el1(SCTLR_EL1_UCI
, 0);
382 #define __user_cache_maint(insn, address, res) \
383 if (address >= user_addr_max()) { \
386 uaccess_ttbr0_enable(); \
388 "1: " insn ", %1\n" \
391 " .pushsection .fixup,\"ax\"\n" \
393 "3: mov %w0, %w2\n" \
396 _ASM_EXTABLE(1b, 3b) \
398 : "r" (address), "i" (-EFAULT)); \
399 uaccess_ttbr0_disable(); \
402 static void user_cache_maint_handler(unsigned int esr
, struct pt_regs
*regs
)
404 unsigned long address
;
405 int rt
= (esr
& ESR_ELx_SYS64_ISS_RT_MASK
) >> ESR_ELx_SYS64_ISS_RT_SHIFT
;
406 int crm
= (esr
& ESR_ELx_SYS64_ISS_CRM_MASK
) >> ESR_ELx_SYS64_ISS_CRM_SHIFT
;
409 address
= untagged_addr(pt_regs_read_reg(regs
, rt
));
412 case ESR_ELx_SYS64_ISS_CRM_DC_CVAU
: /* DC CVAU, gets promoted */
413 __user_cache_maint("dc civac", address
, ret
);
415 case ESR_ELx_SYS64_ISS_CRM_DC_CVAC
: /* DC CVAC, gets promoted */
416 __user_cache_maint("dc civac", address
, ret
);
418 case ESR_ELx_SYS64_ISS_CRM_DC_CVAP
: /* DC CVAP */
419 __user_cache_maint("sys 3, c7, c12, 1", address
, ret
);
421 case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC
: /* DC CIVAC */
422 __user_cache_maint("dc civac", address
, ret
);
424 case ESR_ELx_SYS64_ISS_CRM_IC_IVAU
: /* IC IVAU */
425 __user_cache_maint("ic ivau", address
, ret
);
428 force_signal_inject(SIGILL
, ILL_ILLOPC
, regs
, 0);
433 arm64_notify_segfault(regs
, address
);
435 arm64_skip_faulting_instruction(regs
, AARCH64_INSN_SIZE
);
438 static void ctr_read_handler(unsigned int esr
, struct pt_regs
*regs
)
440 int rt
= (esr
& ESR_ELx_SYS64_ISS_RT_MASK
) >> ESR_ELx_SYS64_ISS_RT_SHIFT
;
441 unsigned long val
= arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0
);
443 pt_regs_write_reg(regs
, rt
, val
);
445 arm64_skip_faulting_instruction(regs
, AARCH64_INSN_SIZE
);
448 static void cntvct_read_handler(unsigned int esr
, struct pt_regs
*regs
)
450 int rt
= (esr
& ESR_ELx_SYS64_ISS_RT_MASK
) >> ESR_ELx_SYS64_ISS_RT_SHIFT
;
452 pt_regs_write_reg(regs
, rt
, arch_counter_get_cntvct());
453 arm64_skip_faulting_instruction(regs
, AARCH64_INSN_SIZE
);
456 static void cntfrq_read_handler(unsigned int esr
, struct pt_regs
*regs
)
458 int rt
= (esr
& ESR_ELx_SYS64_ISS_RT_MASK
) >> ESR_ELx_SYS64_ISS_RT_SHIFT
;
460 pt_regs_write_reg(regs
, rt
, arch_timer_get_rate());
461 arm64_skip_faulting_instruction(regs
, AARCH64_INSN_SIZE
);
465 unsigned int esr_mask
;
466 unsigned int esr_val
;
467 void (*handler
)(unsigned int esr
, struct pt_regs
*regs
);
470 static struct sys64_hook sys64_hooks
[] = {
472 .esr_mask
= ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK
,
473 .esr_val
= ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL
,
474 .handler
= user_cache_maint_handler
,
477 /* Trap read access to CTR_EL0 */
478 .esr_mask
= ESR_ELx_SYS64_ISS_SYS_OP_MASK
,
479 .esr_val
= ESR_ELx_SYS64_ISS_SYS_CTR_READ
,
480 .handler
= ctr_read_handler
,
483 /* Trap read access to CNTVCT_EL0 */
484 .esr_mask
= ESR_ELx_SYS64_ISS_SYS_OP_MASK
,
485 .esr_val
= ESR_ELx_SYS64_ISS_SYS_CNTVCT
,
486 .handler
= cntvct_read_handler
,
489 /* Trap read access to CNTFRQ_EL0 */
490 .esr_mask
= ESR_ELx_SYS64_ISS_SYS_OP_MASK
,
491 .esr_val
= ESR_ELx_SYS64_ISS_SYS_CNTFRQ
,
492 .handler
= cntfrq_read_handler
,
497 asmlinkage
void __exception
do_sysinstr(unsigned int esr
, struct pt_regs
*regs
)
499 struct sys64_hook
*hook
;
501 for (hook
= sys64_hooks
; hook
->handler
; hook
++)
502 if ((hook
->esr_mask
& esr
) == hook
->esr_val
) {
503 hook
->handler(esr
, regs
);
508 * New SYS instructions may previously have been undefined at EL0. Fall
509 * back to our usual undefined instruction handler so that we handle
510 * these consistently.
515 long compat_arm_syscall(struct pt_regs
*regs
);
517 asmlinkage
long do_ni_syscall(struct pt_regs
*regs
)
521 if (is_compat_task()) {
522 ret
= compat_arm_syscall(regs
);
528 if (show_unhandled_signals_ratelimited()) {
529 pr_info("%s[%d]: syscall %d\n", current
->comm
,
530 task_pid_nr(current
), regs
->syscallno
);
531 dump_instr("", regs
);
536 return sys_ni_syscall();
539 static const char *esr_class_str
[] = {
540 [0 ... ESR_ELx_EC_MAX
] = "UNRECOGNIZED EC",
541 [ESR_ELx_EC_UNKNOWN
] = "Unknown/Uncategorized",
542 [ESR_ELx_EC_WFx
] = "WFI/WFE",
543 [ESR_ELx_EC_CP15_32
] = "CP15 MCR/MRC",
544 [ESR_ELx_EC_CP15_64
] = "CP15 MCRR/MRRC",
545 [ESR_ELx_EC_CP14_MR
] = "CP14 MCR/MRC",
546 [ESR_ELx_EC_CP14_LS
] = "CP14 LDC/STC",
547 [ESR_ELx_EC_FP_ASIMD
] = "ASIMD",
548 [ESR_ELx_EC_CP10_ID
] = "CP10 MRC/VMRS",
549 [ESR_ELx_EC_CP14_64
] = "CP14 MCRR/MRRC",
550 [ESR_ELx_EC_ILL
] = "PSTATE.IL",
551 [ESR_ELx_EC_SVC32
] = "SVC (AArch32)",
552 [ESR_ELx_EC_HVC32
] = "HVC (AArch32)",
553 [ESR_ELx_EC_SMC32
] = "SMC (AArch32)",
554 [ESR_ELx_EC_SVC64
] = "SVC (AArch64)",
555 [ESR_ELx_EC_HVC64
] = "HVC (AArch64)",
556 [ESR_ELx_EC_SMC64
] = "SMC (AArch64)",
557 [ESR_ELx_EC_SYS64
] = "MSR/MRS (AArch64)",
558 [ESR_ELx_EC_IMP_DEF
] = "EL3 IMP DEF",
559 [ESR_ELx_EC_IABT_LOW
] = "IABT (lower EL)",
560 [ESR_ELx_EC_IABT_CUR
] = "IABT (current EL)",
561 [ESR_ELx_EC_PC_ALIGN
] = "PC Alignment",
562 [ESR_ELx_EC_DABT_LOW
] = "DABT (lower EL)",
563 [ESR_ELx_EC_DABT_CUR
] = "DABT (current EL)",
564 [ESR_ELx_EC_SP_ALIGN
] = "SP Alignment",
565 [ESR_ELx_EC_FP_EXC32
] = "FP (AArch32)",
566 [ESR_ELx_EC_FP_EXC64
] = "FP (AArch64)",
567 [ESR_ELx_EC_SERROR
] = "SError",
568 [ESR_ELx_EC_BREAKPT_LOW
] = "Breakpoint (lower EL)",
569 [ESR_ELx_EC_BREAKPT_CUR
] = "Breakpoint (current EL)",
570 [ESR_ELx_EC_SOFTSTP_LOW
] = "Software Step (lower EL)",
571 [ESR_ELx_EC_SOFTSTP_CUR
] = "Software Step (current EL)",
572 [ESR_ELx_EC_WATCHPT_LOW
] = "Watchpoint (lower EL)",
573 [ESR_ELx_EC_WATCHPT_CUR
] = "Watchpoint (current EL)",
574 [ESR_ELx_EC_BKPT32
] = "BKPT (AArch32)",
575 [ESR_ELx_EC_VECTOR32
] = "Vector catch (AArch32)",
576 [ESR_ELx_EC_BRK64
] = "BRK (AArch64)",
579 const char *esr_get_class_string(u32 esr
)
581 return esr_class_str
[ESR_ELx_EC(esr
)];
585 * bad_mode handles the impossible case in the exception vector. This is always
588 asmlinkage
void bad_mode(struct pt_regs
*regs
, int reason
, unsigned int esr
)
592 pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
593 handler
[reason
], smp_processor_id(), esr
,
594 esr_get_class_string(esr
));
596 die("Oops - bad mode", regs
, 0);
602 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
603 * exceptions taken from EL0. Unlike bad_mode, this returns.
605 asmlinkage
void bad_el0_sync(struct pt_regs
*regs
, int reason
, unsigned int esr
)
608 void __user
*pc
= (void __user
*)instruction_pointer(regs
);
611 pr_crit("Bad EL0 synchronous exception detected on CPU%d, code 0x%08x -- %s\n",
612 smp_processor_id(), esr
, esr_get_class_string(esr
));
615 info
.si_signo
= SIGILL
;
617 info
.si_code
= ILL_ILLOPC
;
620 current
->thread
.fault_address
= 0;
621 current
->thread
.fault_code
= 0;
623 force_sig_info(info
.si_signo
, &info
, current
);
626 #ifdef CONFIG_VMAP_STACK
628 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE
/sizeof(long)], overflow_stack
)
631 asmlinkage
void handle_bad_stack(struct pt_regs
*regs
)
633 unsigned long tsk_stk
= (unsigned long)current
->stack
;
634 unsigned long irq_stk
= (unsigned long)this_cpu_read(irq_stack_ptr
);
635 unsigned long ovf_stk
= (unsigned long)this_cpu_ptr(overflow_stack
);
636 unsigned int esr
= read_sysreg(esr_el1
);
637 unsigned long far
= read_sysreg(far_el1
);
640 pr_emerg("Insufficient stack space to handle exception!");
642 pr_emerg("ESR: 0x%08x -- %s\n", esr
, esr_get_class_string(esr
));
643 pr_emerg("FAR: 0x%016lx\n", far
);
645 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
646 tsk_stk
, tsk_stk
+ THREAD_SIZE
);
647 pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
648 irq_stk
, irq_stk
+ THREAD_SIZE
);
649 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
650 ovf_stk
, ovf_stk
+ OVERFLOW_STACK_SIZE
);
655 * We use nmi_panic to limit the potential for recusive overflows, and
656 * to get a better stack trace.
658 nmi_panic(NULL
, "kernel stack overflow");
663 void __pte_error(const char *file
, int line
, unsigned long val
)
665 pr_err("%s:%d: bad pte %016lx.\n", file
, line
, val
);
668 void __pmd_error(const char *file
, int line
, unsigned long val
)
670 pr_err("%s:%d: bad pmd %016lx.\n", file
, line
, val
);
673 void __pud_error(const char *file
, int line
, unsigned long val
)
675 pr_err("%s:%d: bad pud %016lx.\n", file
, line
, val
);
678 void __pgd_error(const char *file
, int line
, unsigned long val
)
680 pr_err("%s:%d: bad pgd %016lx.\n", file
, line
, val
);
683 /* GENERIC_BUG traps */
685 int is_valid_bugaddr(unsigned long addr
)
688 * bug_handler() only called for BRK #BUG_BRK_IMM.
689 * So the answer is trivial -- any spurious instances with no
690 * bug table entry will be rejected by report_bug() and passed
691 * back to the debug-monitors code and handled as a fatal
692 * unexpected debug exception.
697 static int bug_handler(struct pt_regs
*regs
, unsigned int esr
)
700 return DBG_HOOK_ERROR
;
702 switch (report_bug(regs
->pc
, regs
)) {
703 case BUG_TRAP_TYPE_BUG
:
704 die("Oops - BUG", regs
, 0);
707 case BUG_TRAP_TYPE_WARN
:
711 /* unknown/unrecognised bug trap type */
712 return DBG_HOOK_ERROR
;
715 /* If thread survives, skip over the BUG instruction and continue: */
716 arm64_skip_faulting_instruction(regs
, AARCH64_INSN_SIZE
);
717 return DBG_HOOK_HANDLED
;
720 static struct break_hook bug_break_hook
= {
721 .esr_val
= 0xf2000000 | BUG_BRK_IMM
,
722 .esr_mask
= 0xffffffff,
727 * Initial handler for AArch64 BRK exceptions
728 * This handler only used until debug_traps_init().
730 int __init
early_brk64(unsigned long addr
, unsigned int esr
,
731 struct pt_regs
*regs
)
733 return bug_handler(regs
, esr
) != DBG_HOOK_HANDLED
;
736 /* This registration must happen early, before debug_traps_init(). */
737 void __init
trap_init(void)
739 register_break_hook(&bug_break_hook
);