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