blob: b99d9dd21fd0bfdbe837969d22b01f4f220cc184 [file] [log] [blame]
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07001/*
2 * Copyright (C) 2012 Regents of the University of California
3 *
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.
7 *
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.
12 */
13
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>
22#include <linux/mm.h>
23#include <linux/module.h>
24#include <linux/irq.h>
25
26#include <asm/processor.h>
27#include <asm/ptrace.h>
28#include <asm/csr.h>
29
30int show_unhandled_signals = 1;
31
32extern asmlinkage void handle_exception(void);
33
34static DEFINE_SPINLOCK(die_lock);
35
36void die(struct pt_regs *regs, const char *str)
37{
38 static int die_counter;
39 int ret;
40
41 oops_enter();
42
43 spin_lock_irq(&die_lock);
44 console_verbose();
45 bust_spinlocks(1);
46
47 pr_emerg("%s [#%d]\n", str, ++die_counter);
48 print_modules();
49 show_regs(regs);
50
51 ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
52
53 bust_spinlocks(0);
54 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
55 spin_unlock_irq(&die_lock);
56 oops_exit();
57
58 if (in_interrupt())
59 panic("Fatal exception in interrupt");
60 if (panic_on_oops)
61 panic("Fatal exception");
62 if (ret != NOTIFY_STOP)
63 do_exit(SIGSEGV);
64}
65
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070066void do_trap(struct pt_regs *regs, int signo, int code,
67 unsigned long addr, struct task_struct *tsk)
68{
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));
74 pr_cont("\n");
75 show_regs(regs);
76 }
77
Eric W. Biederman7ff3a762018-04-24 10:26:11 -050078 force_sig_fault(signo, code, (void __user *)addr, tsk);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070079}
80
81static void do_trap_error(struct pt_regs *regs, int signo, int code,
82 unsigned long addr, const char *str)
83{
84 if (user_mode(regs)) {
85 do_trap(regs, signo, code, addr, current);
86 } else {
87 if (!fixup_exception(regs))
88 die(regs, str);
89 }
90}
91
92#define DO_ERROR_INFO(name, signo, code, str) \
93asmlinkage void name(struct pt_regs *regs) \
94{ \
95 do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
96}
97
98DO_ERROR_INFO(do_trap_unknown,
99 SIGILL, ILL_ILLTRP, "unknown exception");
100DO_ERROR_INFO(do_trap_insn_misaligned,
101 SIGBUS, BUS_ADRALN, "instruction address misaligned");
102DO_ERROR_INFO(do_trap_insn_fault,
103 SIGSEGV, SEGV_ACCERR, "instruction access fault");
104DO_ERROR_INFO(do_trap_insn_illegal,
105 SIGILL, ILL_ILLOPC, "illegal instruction");
106DO_ERROR_INFO(do_trap_load_misaligned,
107 SIGBUS, BUS_ADRALN, "load address misaligned");
108DO_ERROR_INFO(do_trap_load_fault,
109 SIGSEGV, SEGV_ACCERR, "load access fault");
110DO_ERROR_INFO(do_trap_store_misaligned,
111 SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
112DO_ERROR_INFO(do_trap_store_fault,
113 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
114DO_ERROR_INFO(do_trap_ecall_u,
115 SIGILL, ILL_ILLTRP, "environment call from U-mode");
116DO_ERROR_INFO(do_trap_ecall_s,
117 SIGILL, ILL_ILLTRP, "environment call from S-mode");
118DO_ERROR_INFO(do_trap_ecall_m,
119 SIGILL, ILL_ILLTRP, "environment call from M-mode");
120
121asmlinkage void do_trap_break(struct pt_regs *regs)
122{
123#ifdef CONFIG_GENERIC_BUG
124 if (!user_mode(regs)) {
125 enum bug_trap_type type;
126
127 type = report_bug(regs->sepc, regs);
128 switch (type) {
129 case BUG_TRAP_TYPE_NONE:
130 break;
131 case BUG_TRAP_TYPE_WARN:
132 regs->sepc += sizeof(bug_insn_t);
133 return;
134 case BUG_TRAP_TYPE_BUG:
135 die(regs, "Kernel BUG");
136 }
137 }
138#endif /* CONFIG_GENERIC_BUG */
139
Eric W. Biederman7ff3a762018-04-24 10:26:11 -0500140 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700141 regs->sepc += 0x4;
142}
143
144#ifdef CONFIG_GENERIC_BUG
145int is_valid_bugaddr(unsigned long pc)
146{
147 bug_insn_t insn;
148
149 if (pc < PAGE_OFFSET)
150 return 0;
151 if (probe_kernel_address((bug_insn_t __user *)pc, insn))
152 return 0;
153 return (insn == __BUG_INSN);
154}
155#endif /* CONFIG_GENERIC_BUG */
156
157void __init trap_init(void)
158{
159 /*
160 * Set sup0 scratch register to 0, indicating to exception vector
161 * that we are presently executing in the kernel
162 */
163 csr_write(sscratch, 0);
164 /* Set the exception vector address */
165 csr_write(stvec, &handle_exception);
166 /* Enable all interrupts */
167 csr_write(sie, -1);
168}