2 * Copyright (C) 2012 Regents of the University of California
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched/signal.h>
19 #include <linux/signal.h>
20 #include <linux/kdebug.h>
21 #include <linux/uaccess.h>
23 #include <linux/module.h>
24 #include <linux/irq.h>
26 #include <asm/processor.h>
27 #include <asm/ptrace.h>
30 int show_unhandled_signals = 1;
32 extern asmlinkage void handle_exception(void);
34 static DEFINE_SPINLOCK(die_lock);
36 void die(struct pt_regs *regs, const char *str)
38 static int die_counter;
43 spin_lock_irq(&die_lock);
47 pr_emerg("%s [#%d]\n", str, ++die_counter);
51 ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
54 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
55 spin_unlock_irq(&die_lock);
59 panic("Fatal exception in interrupt");
61 panic("Fatal exception");
62 if (ret != NOTIFY_STOP)
66 void do_trap(struct pt_regs *regs, int signo, int code,
67 unsigned long addr, struct task_struct *tsk)
69 if (show_unhandled_signals && unhandled_signal(tsk, signo)
70 && printk_ratelimit()) {
71 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
72 tsk->comm, task_pid_nr(tsk), signo, code, addr);
73 print_vma_addr(KERN_CONT " in ", GET_IP(regs));
78 force_sig_fault(signo, code, (void __user *)addr, tsk);
81 static void do_trap_error(struct pt_regs *regs, int signo, int code,
82 unsigned long addr, const char *str)
84 if (user_mode(regs)) {
85 do_trap(regs, signo, code, addr, current);
87 if (!fixup_exception(regs))
92 #define DO_ERROR_INFO(name, signo, code, str) \
93 asmlinkage void name(struct pt_regs *regs) \
95 do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
98 DO_ERROR_INFO(do_trap_unknown,
99 SIGILL, ILL_ILLTRP, "unknown exception");
100 DO_ERROR_INFO(do_trap_insn_misaligned,
101 SIGBUS, BUS_ADRALN, "instruction address misaligned");
102 DO_ERROR_INFO(do_trap_insn_fault,
103 SIGSEGV, SEGV_ACCERR, "instruction access fault");
104 DO_ERROR_INFO(do_trap_insn_illegal,
105 SIGILL, ILL_ILLOPC, "illegal instruction");
106 DO_ERROR_INFO(do_trap_load_misaligned,
107 SIGBUS, BUS_ADRALN, "load address misaligned");
108 DO_ERROR_INFO(do_trap_load_fault,
109 SIGSEGV, SEGV_ACCERR, "load access fault");
110 DO_ERROR_INFO(do_trap_store_misaligned,
111 SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
112 DO_ERROR_INFO(do_trap_store_fault,
113 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
114 DO_ERROR_INFO(do_trap_ecall_u,
115 SIGILL, ILL_ILLTRP, "environment call from U-mode");
116 DO_ERROR_INFO(do_trap_ecall_s,
117 SIGILL, ILL_ILLTRP, "environment call from S-mode");
118 DO_ERROR_INFO(do_trap_ecall_m,
119 SIGILL, ILL_ILLTRP, "environment call from M-mode");
121 asmlinkage void do_trap_break(struct pt_regs *regs)
123 #ifdef CONFIG_GENERIC_BUG
124 if (!user_mode(regs)) {
125 enum bug_trap_type type;
127 type = report_bug(regs->sepc, regs);
129 case BUG_TRAP_TYPE_NONE:
131 case BUG_TRAP_TYPE_WARN:
132 regs->sepc += sizeof(bug_insn_t);
134 case BUG_TRAP_TYPE_BUG:
135 die(regs, "Kernel BUG");
138 #endif /* CONFIG_GENERIC_BUG */
140 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current);
144 #ifdef CONFIG_GENERIC_BUG
145 int is_valid_bugaddr(unsigned long pc)
149 if (pc < PAGE_OFFSET)
151 if (probe_kernel_address((bug_insn_t *)pc, insn))
153 return (insn == __BUG_INSN);
155 #endif /* CONFIG_GENERIC_BUG */
157 void __init trap_init(void)
160 * Set sup0 scratch register to 0, indicating to exception vector
161 * that we are presently executing in the kernel
163 csr_write(sscratch, 0);
164 /* Set the exception vector address */
165 csr_write(stvec, &handle_exception);
166 /* Enable all interrupts */