xref: /openbmc/linux/arch/sh/kernel/traps.c (revision f7c35abe)
1 #include <linux/bug.h>
2 #include <linux/io.h>
3 #include <linux/types.h>
4 #include <linux/kdebug.h>
5 #include <linux/signal.h>
6 #include <linux/sched.h>
7 #include <linux/sched/debug.h>
8 #include <linux/sched/task_stack.h>
9 #include <linux/uaccess.h>
10 #include <linux/hardirq.h>
11 #include <linux/kernel.h>
12 #include <linux/kexec.h>
13 #include <linux/sched/signal.h>
14 
15 #include <linux/extable.h>
16 #include <linux/module.h>	/* print_modules */
17 #include <asm/unwinder.h>
18 #include <asm/traps.h>
19 
20 static DEFINE_SPINLOCK(die_lock);
21 
22 void die(const char *str, struct pt_regs *regs, long err)
23 {
24 	static int die_counter;
25 
26 	oops_enter();
27 
28 	spin_lock_irq(&die_lock);
29 	console_verbose();
30 	bust_spinlocks(1);
31 
32 	printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
33 	print_modules();
34 	show_regs(regs);
35 
36 	printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
37 			task_pid_nr(current), task_stack_page(current) + 1);
38 
39 	if (!user_mode(regs) || in_interrupt())
40 		dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
41 			 (unsigned long)task_stack_page(current));
42 
43 	notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
44 
45 	bust_spinlocks(0);
46 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
47 	spin_unlock_irq(&die_lock);
48 	oops_exit();
49 
50 	if (kexec_should_crash(current))
51 		crash_kexec(regs);
52 
53 	if (in_interrupt())
54 		panic("Fatal exception in interrupt");
55 
56 	if (panic_on_oops)
57 		panic("Fatal exception");
58 
59 	do_exit(SIGSEGV);
60 }
61 
62 void die_if_kernel(const char *str, struct pt_regs *regs, long err)
63 {
64 	if (!user_mode(regs))
65 		die(str, regs, err);
66 }
67 
68 /*
69  * try and fix up kernelspace address errors
70  * - userspace errors just cause EFAULT to be returned, resulting in SEGV
71  * - kernel/userspace interfaces cause a jump to an appropriate handler
72  * - other kernel errors are bad
73  */
74 void die_if_no_fixup(const char *str, struct pt_regs *regs, long err)
75 {
76 	if (!user_mode(regs)) {
77 		const struct exception_table_entry *fixup;
78 		fixup = search_exception_tables(regs->pc);
79 		if (fixup) {
80 			regs->pc = fixup->fixup;
81 			return;
82 		}
83 
84 		die(str, regs, err);
85 	}
86 }
87 
88 #ifdef CONFIG_GENERIC_BUG
89 static void handle_BUG(struct pt_regs *regs)
90 {
91 	const struct bug_entry *bug;
92 	unsigned long bugaddr = regs->pc;
93 	enum bug_trap_type tt;
94 
95 	if (!is_valid_bugaddr(bugaddr))
96 		goto invalid;
97 
98 	bug = find_bug(bugaddr);
99 
100 	/* Switch unwinders when unwind_stack() is called */
101 	if (bug->flags & BUGFLAG_UNWINDER)
102 		unwinder_faulted = 1;
103 
104 	tt = report_bug(bugaddr, regs);
105 	if (tt == BUG_TRAP_TYPE_WARN) {
106 		regs->pc += instruction_size(bugaddr);
107 		return;
108 	}
109 
110 invalid:
111 	die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
112 }
113 
114 int is_valid_bugaddr(unsigned long addr)
115 {
116 	insn_size_t opcode;
117 
118 	if (addr < PAGE_OFFSET)
119 		return 0;
120 	if (probe_kernel_address((insn_size_t *)addr, opcode))
121 		return 0;
122 	if (opcode == TRAPA_BUG_OPCODE)
123 		return 1;
124 
125 	return 0;
126 }
127 #endif
128 
129 /*
130  * Generic trap handler.
131  */
132 BUILD_TRAP_HANDLER(debug)
133 {
134 	TRAP_HANDLER_DECL;
135 
136 	/* Rewind */
137 	regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
138 
139 	if (notify_die(DIE_TRAP, "debug trap", regs, 0, vec & 0xff,
140 		       SIGTRAP) == NOTIFY_STOP)
141 		return;
142 
143 	force_sig(SIGTRAP, current);
144 }
145 
146 /*
147  * Special handler for BUG() traps.
148  */
149 BUILD_TRAP_HANDLER(bug)
150 {
151 	TRAP_HANDLER_DECL;
152 
153 	/* Rewind */
154 	regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
155 
156 	if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff,
157 		       SIGTRAP) == NOTIFY_STOP)
158 		return;
159 
160 #ifdef CONFIG_GENERIC_BUG
161 	if (__kernel_text_address(instruction_pointer(regs))) {
162 		insn_size_t insn = *(insn_size_t *)instruction_pointer(regs);
163 		if (insn == TRAPA_BUG_OPCODE)
164 			handle_BUG(regs);
165 		return;
166 	}
167 #endif
168 
169 	force_sig(SIGTRAP, current);
170 }
171 
172 BUILD_TRAP_HANDLER(nmi)
173 {
174 	unsigned int cpu = smp_processor_id();
175 	TRAP_HANDLER_DECL;
176 
177 	nmi_enter();
178 	nmi_count(cpu)++;
179 
180 	switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) {
181 	case NOTIFY_OK:
182 	case NOTIFY_STOP:
183 		break;
184 	case NOTIFY_BAD:
185 		die("Fatal Non-Maskable Interrupt", regs, SIGINT);
186 	default:
187 		printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n");
188 		break;
189 	}
190 
191 	nmi_exit();
192 }
193