Loading...
1/*
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
66static inline void do_trap_siginfo(int signo, int code,
67 unsigned long addr, struct task_struct *tsk)
68{
69 siginfo_t info;
70
71 info.si_signo = signo;
72 info.si_errno = 0;
73 info.si_code = code;
74 info.si_addr = (void __user *)addr;
75 force_sig_info(signo, &info, tsk);
76}
77
78void do_trap(struct pt_regs *regs, int signo, int code,
79 unsigned long addr, struct task_struct *tsk)
80{
81 if (show_unhandled_signals && unhandled_signal(tsk, signo)
82 && printk_ratelimit()) {
83 pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
84 tsk->comm, task_pid_nr(tsk), signo, code, addr);
85 print_vma_addr(KERN_CONT " in ", GET_IP(regs));
86 pr_cont("\n");
87 show_regs(regs);
88 }
89
90 do_trap_siginfo(signo, code, addr, tsk);
91}
92
93static void do_trap_error(struct pt_regs *regs, int signo, int code,
94 unsigned long addr, const char *str)
95{
96 if (user_mode(regs)) {
97 do_trap(regs, signo, code, addr, current);
98 } else {
99 if (!fixup_exception(regs))
100 die(regs, str);
101 }
102}
103
104#define DO_ERROR_INFO(name, signo, code, str) \
105asmlinkage void name(struct pt_regs *regs) \
106{ \
107 do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
108}
109
110DO_ERROR_INFO(do_trap_unknown,
111 SIGILL, ILL_ILLTRP, "unknown exception");
112DO_ERROR_INFO(do_trap_insn_misaligned,
113 SIGBUS, BUS_ADRALN, "instruction address misaligned");
114DO_ERROR_INFO(do_trap_insn_fault,
115 SIGSEGV, SEGV_ACCERR, "instruction access fault");
116DO_ERROR_INFO(do_trap_insn_illegal,
117 SIGILL, ILL_ILLOPC, "illegal instruction");
118DO_ERROR_INFO(do_trap_load_misaligned,
119 SIGBUS, BUS_ADRALN, "load address misaligned");
120DO_ERROR_INFO(do_trap_load_fault,
121 SIGSEGV, SEGV_ACCERR, "load access fault");
122DO_ERROR_INFO(do_trap_store_misaligned,
123 SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
124DO_ERROR_INFO(do_trap_store_fault,
125 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
126DO_ERROR_INFO(do_trap_ecall_u,
127 SIGILL, ILL_ILLTRP, "environment call from U-mode");
128DO_ERROR_INFO(do_trap_ecall_s,
129 SIGILL, ILL_ILLTRP, "environment call from S-mode");
130DO_ERROR_INFO(do_trap_ecall_m,
131 SIGILL, ILL_ILLTRP, "environment call from M-mode");
132
133asmlinkage void do_trap_break(struct pt_regs *regs)
134{
135#ifdef CONFIG_GENERIC_BUG
136 if (!user_mode(regs)) {
137 enum bug_trap_type type;
138
139 type = report_bug(regs->sepc, regs);
140 switch (type) {
141 case BUG_TRAP_TYPE_NONE:
142 break;
143 case BUG_TRAP_TYPE_WARN:
144 regs->sepc += sizeof(bug_insn_t);
145 return;
146 case BUG_TRAP_TYPE_BUG:
147 die(regs, "Kernel BUG");
148 }
149 }
150#endif /* CONFIG_GENERIC_BUG */
151
152 do_trap_siginfo(SIGTRAP, TRAP_BRKPT, regs->sepc, current);
153 regs->sepc += 0x4;
154}
155
156#ifdef CONFIG_GENERIC_BUG
157int is_valid_bugaddr(unsigned long pc)
158{
159 bug_insn_t insn;
160
161 if (pc < PAGE_OFFSET)
162 return 0;
163 if (probe_kernel_address((bug_insn_t __user *)pc, insn))
164 return 0;
165 return (insn == __BUG_INSN);
166}
167#endif /* CONFIG_GENERIC_BUG */
168
169void __init trap_init(void)
170{
171 /*
172 * Set sup0 scratch register to 0, indicating to exception vector
173 * that we are presently executing in the kernel
174 */
175 csr_write(sscratch, 0);
176 /* Set the exception vector address */
177 csr_write(stvec, &handle_exception);
178 /* Enable all interrupts */
179 csr_write(sie, -1);
180}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012 Regents of the University of California
4 */
5
6#include <linux/cpu.h>
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>
15#include <linux/mm.h>
16#include <linux/module.h>
17#include <linux/irq.h>
18
19#include <asm/processor.h>
20#include <asm/ptrace.h>
21#include <asm/csr.h>
22
23int show_unhandled_signals = 1;
24
25extern asmlinkage void handle_exception(void);
26
27static DEFINE_SPINLOCK(die_lock);
28
29void die(struct pt_regs *regs, const char *str)
30{
31 static int die_counter;
32 int ret;
33
34 oops_enter();
35
36 spin_lock_irq(&die_lock);
37 console_verbose();
38 bust_spinlocks(1);
39
40 pr_emerg("%s [#%d]\n", str, ++die_counter);
41 print_modules();
42 show_regs(regs);
43
44 ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
45
46 bust_spinlocks(0);
47 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
48 spin_unlock_irq(&die_lock);
49 oops_exit();
50
51 if (in_interrupt())
52 panic("Fatal exception in interrupt");
53 if (panic_on_oops)
54 panic("Fatal exception");
55 if (ret != NOTIFY_STOP)
56 do_exit(SIGSEGV);
57}
58
59void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
60{
61 struct task_struct *tsk = current;
62
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));
68 pr_cont("\n");
69 show_regs(regs);
70 }
71
72 force_sig_fault(signo, code, (void __user *)addr);
73}
74
75static void do_trap_error(struct pt_regs *regs, int signo, int code,
76 unsigned long addr, const char *str)
77{
78 if (user_mode(regs)) {
79 do_trap(regs, signo, code, addr);
80 } else {
81 if (!fixup_exception(regs))
82 die(regs, str);
83 }
84}
85
86#define DO_ERROR_INFO(name, signo, code, str) \
87asmlinkage __visible void name(struct pt_regs *regs) \
88{ \
89 do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
90}
91
92DO_ERROR_INFO(do_trap_unknown,
93 SIGILL, ILL_ILLTRP, "unknown exception");
94DO_ERROR_INFO(do_trap_insn_misaligned,
95 SIGBUS, BUS_ADRALN, "instruction address misaligned");
96DO_ERROR_INFO(do_trap_insn_fault,
97 SIGSEGV, SEGV_ACCERR, "instruction access fault");
98DO_ERROR_INFO(do_trap_insn_illegal,
99 SIGILL, ILL_ILLOPC, "illegal instruction");
100DO_ERROR_INFO(do_trap_load_misaligned,
101 SIGBUS, BUS_ADRALN, "load address misaligned");
102DO_ERROR_INFO(do_trap_load_fault,
103 SIGSEGV, SEGV_ACCERR, "load access fault");
104DO_ERROR_INFO(do_trap_store_misaligned,
105 SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
106DO_ERROR_INFO(do_trap_store_fault,
107 SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
108DO_ERROR_INFO(do_trap_ecall_u,
109 SIGILL, ILL_ILLTRP, "environment call from U-mode");
110DO_ERROR_INFO(do_trap_ecall_s,
111 SIGILL, ILL_ILLTRP, "environment call from S-mode");
112DO_ERROR_INFO(do_trap_ecall_m,
113 SIGILL, ILL_ILLTRP, "environment call from M-mode");
114
115static inline unsigned long get_break_insn_length(unsigned long pc)
116{
117 bug_insn_t insn;
118
119 if (probe_kernel_address((bug_insn_t *)pc, insn))
120 return 0;
121 return (((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) ? 4UL : 2UL);
122}
123
124asmlinkage __visible void do_trap_break(struct pt_regs *regs)
125{
126 if (user_mode(regs))
127 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->sepc);
128 else if (report_bug(regs->sepc, regs) == BUG_TRAP_TYPE_WARN)
129 regs->sepc += get_break_insn_length(regs->sepc);
130 else
131 die(regs, "Kernel BUG");
132}
133
134#ifdef CONFIG_GENERIC_BUG
135int is_valid_bugaddr(unsigned long pc)
136{
137 bug_insn_t insn;
138
139 if (pc < VMALLOC_START)
140 return 0;
141 if (probe_kernel_address((bug_insn_t *)pc, insn))
142 return 0;
143 if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
144 return (insn == __BUG_INSN_32);
145 else
146 return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
147}
148#endif /* CONFIG_GENERIC_BUG */
149
150void __init trap_init(void)
151{
152 /*
153 * Set sup0 scratch register to 0, indicating to exception vector
154 * that we are presently executing in the kernel
155 */
156 csr_write(CSR_SSCRATCH, 0);
157 /* Set the exception vector address */
158 csr_write(CSR_STVEC, &handle_exception);
159 /* Enable all interrupts */
160 csr_write(CSR_SIE, -1);
161}