1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Regents of the University of California
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/sched/debug.h>
11 #include <linux/sched/signal.h>
12 #include <linux/signal.h>
13 #include <linux/kdebug.h>
14 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/irq.h>
19 #include <asm/processor.h>
20 #include <asm/ptrace.h>
23 int show_unhandled_signals
= 1;
25 extern asmlinkage
void handle_exception(void);
27 static DEFINE_SPINLOCK(die_lock
);
29 void die(struct pt_regs
*regs
, const char *str
)
31 static int die_counter
;
36 spin_lock_irq(&die_lock
);
40 pr_emerg("%s [#%d]\n", str
, ++die_counter
);
44 ret
= notify_die(DIE_OOPS
, str
, regs
, 0, regs
->cause
, SIGSEGV
);
47 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
48 spin_unlock_irq(&die_lock
);
52 panic("Fatal exception in interrupt");
54 panic("Fatal exception");
55 if (ret
!= NOTIFY_STOP
)
59 void do_trap(struct pt_regs
*regs
, int signo
, int code
, unsigned long addr
)
61 struct task_struct
*tsk
= current
;
63 if (show_unhandled_signals
&& unhandled_signal(tsk
, signo
)
64 && printk_ratelimit()) {
65 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT
,
66 tsk
->comm
, task_pid_nr(tsk
), signo
, code
, addr
);
67 print_vma_addr(KERN_CONT
" in ", instruction_pointer(regs
));
72 force_sig_fault(signo
, code
, (void __user
*)addr
);
75 static void do_trap_error(struct pt_regs
*regs
, int signo
, int code
,
76 unsigned long addr
, const char *str
)
78 if (user_mode(regs
)) {
79 do_trap(regs
, signo
, code
, addr
);
81 if (!fixup_exception(regs
))
86 #define DO_ERROR_INFO(name, signo, code, str) \
87 asmlinkage __visible void name(struct pt_regs *regs) \
89 do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
92 DO_ERROR_INFO(do_trap_unknown
,
93 SIGILL
, ILL_ILLTRP
, "unknown exception");
94 DO_ERROR_INFO(do_trap_insn_misaligned
,
95 SIGBUS
, BUS_ADRALN
, "instruction address misaligned");
96 DO_ERROR_INFO(do_trap_insn_fault
,
97 SIGSEGV
, SEGV_ACCERR
, "instruction access fault");
98 DO_ERROR_INFO(do_trap_insn_illegal
,
99 SIGILL
, ILL_ILLOPC
, "illegal instruction");
100 DO_ERROR_INFO(do_trap_load_fault
,
101 SIGSEGV
, SEGV_ACCERR
, "load access fault");
102 #ifndef CONFIG_RISCV_M_MODE
103 DO_ERROR_INFO(do_trap_load_misaligned
,
104 SIGBUS
, BUS_ADRALN
, "Oops - load address misaligned");
105 DO_ERROR_INFO(do_trap_store_misaligned
,
106 SIGBUS
, BUS_ADRALN
, "Oops - store (or AMO) address misaligned");
108 int handle_misaligned_load(struct pt_regs
*regs
);
109 int handle_misaligned_store(struct pt_regs
*regs
);
111 asmlinkage
void do_trap_load_misaligned(struct pt_regs
*regs
)
113 if (!handle_misaligned_load(regs
))
115 do_trap_error(regs
, SIGBUS
, BUS_ADRALN
, regs
->epc
,
116 "Oops - load address misaligned");
119 asmlinkage
void do_trap_store_misaligned(struct pt_regs
*regs
)
121 if (!handle_misaligned_store(regs
))
123 do_trap_error(regs
, SIGBUS
, BUS_ADRALN
, regs
->epc
,
124 "Oops - store (or AMO) address misaligned");
127 DO_ERROR_INFO(do_trap_store_fault
,
128 SIGSEGV
, SEGV_ACCERR
, "store (or AMO) access fault");
129 DO_ERROR_INFO(do_trap_ecall_u
,
130 SIGILL
, ILL_ILLTRP
, "environment call from U-mode");
131 DO_ERROR_INFO(do_trap_ecall_s
,
132 SIGILL
, ILL_ILLTRP
, "environment call from S-mode");
133 DO_ERROR_INFO(do_trap_ecall_m
,
134 SIGILL
, ILL_ILLTRP
, "environment call from M-mode");
136 static inline unsigned long get_break_insn_length(unsigned long pc
)
140 if (get_kernel_nofault(insn
, (bug_insn_t
*)pc
))
143 return GET_INSN_LENGTH(insn
);
146 asmlinkage __visible
void do_trap_break(struct pt_regs
*regs
)
149 force_sig_fault(SIGTRAP
, TRAP_BRKPT
, (void __user
*)regs
->epc
);
151 else if (notify_die(DIE_TRAP
, "EBREAK", regs
, 0, regs
->cause
, SIGTRAP
)
155 else if (report_bug(regs
->epc
, regs
) == BUG_TRAP_TYPE_WARN
)
156 regs
->epc
+= get_break_insn_length(regs
->epc
);
158 die(regs
, "Kernel BUG");
161 #ifdef CONFIG_GENERIC_BUG
162 int is_valid_bugaddr(unsigned long pc
)
166 if (pc
< VMALLOC_START
)
168 if (get_kernel_nofault(insn
, (bug_insn_t
*)pc
))
170 if ((insn
& __INSN_LENGTH_MASK
) == __INSN_LENGTH_32
)
171 return (insn
== __BUG_INSN_32
);
173 return ((insn
& __COMPRESSED_INSN_MASK
) == __BUG_INSN_16
);
175 #endif /* CONFIG_GENERIC_BUG */
177 /* stvec & scratch is already set from head.S */