• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/kexec.h>
19 
20 #include <asm/processor.h>
21 #include <asm/ptrace.h>
22 #include <asm/csr.h>
23 
24 int show_unhandled_signals = 1;
25 
26 extern asmlinkage void handle_exception(void);
27 
28 static DEFINE_SPINLOCK(die_lock);
29 
die(struct pt_regs * regs,const char * str)30 void die(struct pt_regs *regs, const char *str)
31 {
32 	static int die_counter;
33 	int ret;
34 	long cause;
35 	unsigned long flags;
36 
37 	oops_enter();
38 
39 	spin_lock_irqsave(&die_lock, flags);
40 	console_verbose();
41 	bust_spinlocks(1);
42 
43 	pr_emerg("%s [#%d]\n", str, ++die_counter);
44 	print_modules();
45 	if (regs)
46 		show_regs(regs);
47 
48 	cause = regs ? regs->cause : -1;
49 	ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
50 
51 	if (kexec_should_crash(current))
52 		crash_kexec(regs);
53 
54 	bust_spinlocks(0);
55 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
56 	spin_unlock_irqrestore(&die_lock, flags);
57 	oops_exit();
58 
59 	if (in_interrupt())
60 		panic("Fatal exception in interrupt");
61 	if (panic_on_oops)
62 		panic("Fatal exception");
63 	if (ret != NOTIFY_STOP)
64 		make_task_dead(SIGSEGV);
65 }
66 
do_trap(struct pt_regs * regs,int signo,int code,unsigned long addr)67 void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
68 {
69 	struct task_struct *tsk = current;
70 
71 	if (show_unhandled_signals && unhandled_signal(tsk, signo)
72 	    && printk_ratelimit()) {
73 		pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
74 			tsk->comm, task_pid_nr(tsk), signo, code, addr);
75 		print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
76 		pr_cont("\n");
77 		show_regs(regs);
78 	}
79 
80 	force_sig_fault(signo, code, (void __user *)addr);
81 }
82 
do_trap_error(struct pt_regs * regs,int signo,int code,unsigned long addr,const char * str)83 static void do_trap_error(struct pt_regs *regs, int signo, int code,
84 	unsigned long addr, const char *str)
85 {
86 	if (user_mode(regs)) {
87 		do_trap(regs, signo, code, addr);
88 	} else {
89 		if (!fixup_exception(regs))
90 			die(regs, str);
91 	}
92 }
93 
94 #define DO_ERROR_INFO(name, signo, code, str)				\
95 asmlinkage __visible void name(struct pt_regs *regs)			\
96 {									\
97 	do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
98 }
99 
100 DO_ERROR_INFO(do_trap_unknown,
101 	SIGILL, ILL_ILLTRP, "unknown exception");
102 DO_ERROR_INFO(do_trap_insn_misaligned,
103 	SIGBUS, BUS_ADRALN, "instruction address misaligned");
104 DO_ERROR_INFO(do_trap_insn_fault,
105 	SIGSEGV, SEGV_ACCERR, "instruction access fault");
106 DO_ERROR_INFO(do_trap_insn_illegal,
107 	SIGILL, ILL_ILLOPC, "illegal instruction");
108 DO_ERROR_INFO(do_trap_load_fault,
109 	SIGSEGV, SEGV_ACCERR, "load access fault");
110 #ifndef CONFIG_RISCV_M_MODE
111 DO_ERROR_INFO(do_trap_load_misaligned,
112 	SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
113 DO_ERROR_INFO(do_trap_store_misaligned,
114 	SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
115 #else
116 int handle_misaligned_load(struct pt_regs *regs);
117 int handle_misaligned_store(struct pt_regs *regs);
118 
do_trap_load_misaligned(struct pt_regs * regs)119 asmlinkage void do_trap_load_misaligned(struct pt_regs *regs)
120 {
121 	if (!handle_misaligned_load(regs))
122 		return;
123 	do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
124 		      "Oops - load address misaligned");
125 }
126 
do_trap_store_misaligned(struct pt_regs * regs)127 asmlinkage void do_trap_store_misaligned(struct pt_regs *regs)
128 {
129 	if (!handle_misaligned_store(regs))
130 		return;
131 	do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
132 		      "Oops - store (or AMO) address misaligned");
133 }
134 #endif
135 DO_ERROR_INFO(do_trap_store_fault,
136 	SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
137 DO_ERROR_INFO(do_trap_ecall_u,
138 	SIGILL, ILL_ILLTRP, "environment call from U-mode");
139 DO_ERROR_INFO(do_trap_ecall_s,
140 	SIGILL, ILL_ILLTRP, "environment call from S-mode");
141 DO_ERROR_INFO(do_trap_ecall_m,
142 	SIGILL, ILL_ILLTRP, "environment call from M-mode");
143 
get_break_insn_length(unsigned long pc)144 static inline unsigned long get_break_insn_length(unsigned long pc)
145 {
146 	bug_insn_t insn;
147 
148 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
149 		return 0;
150 
151 	return GET_INSN_LENGTH(insn);
152 }
153 
do_trap_break(struct pt_regs * regs)154 asmlinkage __visible void do_trap_break(struct pt_regs *regs)
155 {
156 	if (user_mode(regs))
157 		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
158 #ifdef CONFIG_KGDB
159 	else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
160 								== NOTIFY_STOP)
161 		return;
162 #endif
163 	else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
164 		regs->epc += get_break_insn_length(regs->epc);
165 	else
166 		die(regs, "Kernel BUG");
167 }
168 
169 #ifdef CONFIG_GENERIC_BUG
is_valid_bugaddr(unsigned long pc)170 int is_valid_bugaddr(unsigned long pc)
171 {
172 	bug_insn_t insn;
173 
174 	if (pc < VMALLOC_START)
175 		return 0;
176 	if (get_kernel_nofault(insn, (bug_insn_t *)pc))
177 		return 0;
178 	if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
179 		return (insn == __BUG_INSN_32);
180 	else
181 		return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
182 }
183 #endif /* CONFIG_GENERIC_BUG */
184 
185 /* stvec & scratch is already set from head.S */
trap_init(void)186 void trap_init(void)
187 {
188 }
189