]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/unicore32/kernel/traps.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-artful-kernel.git] / arch / unicore32 / kernel / traps.c
1 /*
2 * linux/arch/unicore32/kernel/traps.c
3 *
4 * Code specific to PKUnity SoC and UniCore ISA
5 *
6 * Copyright (C) 2001-2010 GUAN Xue-tao
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * 'traps.c' handles hardware exceptions after we have saved some state.
13 * Mostly a debugging aid, but will probably kill the offending process.
14 */
15 #include <linux/module.h>
16 #include <linux/signal.h>
17 #include <linux/sched/signal.h>
18 #include <linux/sched/debug.h>
19 #include <linux/spinlock.h>
20 #include <linux/personality.h>
21 #include <linux/kallsyms.h>
22 #include <linux/kdebug.h>
23 #include <linux/uaccess.h>
24 #include <linux/delay.h>
25 #include <linux/hardirq.h>
26 #include <linux/init.h>
27 #include <linux/atomic.h>
28 #include <linux/unistd.h>
29
30 #include <asm/cacheflush.h>
31 #include <asm/traps.h>
32
33 #include "setup.h"
34
35 static void dump_mem(const char *, const char *, unsigned long, unsigned long);
36
37 void dump_backtrace_entry(unsigned long where,
38 unsigned long from, unsigned long frame)
39 {
40 #ifdef CONFIG_KALLSYMS
41 printk(KERN_DEFAULT "[<%08lx>] (%pS) from [<%08lx>] (%pS)\n",
42 where, (void *)where, from, (void *)from);
43 #else
44 printk(KERN_DEFAULT "Function entered at [<%08lx>] from [<%08lx>]\n",
45 where, from);
46 #endif
47 }
48
49 /*
50 * Stack pointers should always be within the kernels view of
51 * physical memory. If it is not there, then we can't dump
52 * out any information relating to the stack.
53 */
54 static int verify_stack(unsigned long sp)
55 {
56 if (sp < PAGE_OFFSET ||
57 (sp > (unsigned long)high_memory && high_memory != NULL))
58 return -EFAULT;
59
60 return 0;
61 }
62
63 /*
64 * Dump out the contents of some memory nicely...
65 */
66 static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
67 unsigned long top)
68 {
69 unsigned long first;
70 mm_segment_t fs;
71 int i;
72
73 /*
74 * We need to switch to kernel mode so that we can use __get_user
75 * to safely read from kernel space. Note that we now dump the
76 * code first, just in case the backtrace kills us.
77 */
78 fs = get_fs();
79 set_fs(KERNEL_DS);
80
81 printk(KERN_DEFAULT "%s%s(0x%08lx to 0x%08lx)\n",
82 lvl, str, bottom, top);
83
84 for (first = bottom & ~31; first < top; first += 32) {
85 unsigned long p;
86 char str[sizeof(" 12345678") * 8 + 1];
87
88 memset(str, ' ', sizeof(str));
89 str[sizeof(str) - 1] = '\0';
90
91 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
92 if (p >= bottom && p < top) {
93 unsigned long val;
94 if (__get_user(val, (unsigned long *)p) == 0)
95 sprintf(str + i * 9, " %08lx", val);
96 else
97 sprintf(str + i * 9, " ????????");
98 }
99 }
100 printk(KERN_DEFAULT "%s%04lx:%s\n", lvl, first & 0xffff, str);
101 }
102
103 set_fs(fs);
104 }
105
106 static void dump_instr(const char *lvl, struct pt_regs *regs)
107 {
108 unsigned long addr = instruction_pointer(regs);
109 const int width = 8;
110 mm_segment_t fs;
111 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
112 int i;
113
114 /*
115 * We need to switch to kernel mode so that we can use __get_user
116 * to safely read from kernel space. Note that we now dump the
117 * code first, just in case the backtrace kills us.
118 */
119 fs = get_fs();
120 set_fs(KERNEL_DS);
121
122 for (i = -4; i < 1; i++) {
123 unsigned int val, bad;
124
125 bad = __get_user(val, &((u32 *)addr)[i]);
126
127 if (!bad)
128 p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
129 width, val);
130 else {
131 p += sprintf(p, "bad PC value");
132 break;
133 }
134 }
135 printk(KERN_DEFAULT "%sCode: %s\n", lvl, str);
136
137 set_fs(fs);
138 }
139
140 static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
141 {
142 unsigned int fp, mode;
143 int ok = 1;
144
145 printk(KERN_DEFAULT "Backtrace: ");
146
147 if (!tsk)
148 tsk = current;
149
150 if (regs) {
151 fp = regs->UCreg_fp;
152 mode = processor_mode(regs);
153 } else if (tsk != current) {
154 fp = thread_saved_fp(tsk);
155 mode = 0x10;
156 } else {
157 asm("mov %0, fp" : "=r" (fp) : : "cc");
158 mode = 0x10;
159 }
160
161 if (!fp) {
162 printk("no frame pointer");
163 ok = 0;
164 } else if (verify_stack(fp)) {
165 printk("invalid frame pointer 0x%08x", fp);
166 ok = 0;
167 } else if (fp < (unsigned long)end_of_stack(tsk))
168 printk("frame pointer underflow");
169 printk("\n");
170
171 if (ok)
172 c_backtrace(fp, mode);
173 }
174
175 void show_stack(struct task_struct *tsk, unsigned long *sp)
176 {
177 dump_backtrace(NULL, tsk);
178 barrier();
179 }
180
181 static int __die(const char *str, int err, struct thread_info *thread,
182 struct pt_regs *regs)
183 {
184 struct task_struct *tsk = thread->task;
185 static int die_counter;
186 int ret;
187
188 printk(KERN_EMERG "Internal error: %s: %x [#%d]\n",
189 str, err, ++die_counter);
190
191 /* trap and error numbers are mostly meaningless on UniCore */
192 ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, \
193 SIGSEGV);
194 if (ret == NOTIFY_STOP)
195 return ret;
196
197 print_modules();
198 __show_regs(regs);
199 printk(KERN_EMERG "Process %.*s (pid: %d, stack limit = 0x%p)\n",
200 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
201
202 if (!user_mode(regs) || in_interrupt()) {
203 dump_mem(KERN_EMERG, "Stack: ", regs->UCreg_sp,
204 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
205 dump_backtrace(regs, tsk);
206 dump_instr(KERN_EMERG, regs);
207 }
208
209 return ret;
210 }
211
212 DEFINE_SPINLOCK(die_lock);
213
214 /*
215 * This function is protected against re-entrancy.
216 */
217 void die(const char *str, struct pt_regs *regs, int err)
218 {
219 struct thread_info *thread = current_thread_info();
220 int ret;
221
222 oops_enter();
223
224 spin_lock_irq(&die_lock);
225 console_verbose();
226 bust_spinlocks(1);
227 ret = __die(str, err, thread, regs);
228
229 bust_spinlocks(0);
230 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
231 spin_unlock_irq(&die_lock);
232 oops_exit();
233
234 if (in_interrupt())
235 panic("Fatal exception in interrupt");
236 if (panic_on_oops)
237 panic("Fatal exception");
238 if (ret != NOTIFY_STOP)
239 do_exit(SIGSEGV);
240 }
241
242 void uc32_notify_die(const char *str, struct pt_regs *regs,
243 struct siginfo *info, unsigned long err, unsigned long trap)
244 {
245 if (user_mode(regs)) {
246 current->thread.error_code = err;
247 current->thread.trap_no = trap;
248
249 force_sig_info(info->si_signo, info, current);
250 } else
251 die(str, regs, err);
252 }
253
254 /*
255 * bad_mode handles the impossible case in the vectors. If you see one of
256 * these, then it's extremely serious, and could mean you have buggy hardware.
257 * It never returns, and never tries to sync. We hope that we can at least
258 * dump out some state information...
259 */
260 asmlinkage void bad_mode(struct pt_regs *regs, unsigned int reason)
261 {
262 console_verbose();
263
264 printk(KERN_CRIT "Bad mode detected with reason 0x%x\n", reason);
265
266 die("Oops - bad mode", regs, 0);
267 local_irq_disable();
268 panic("bad mode");
269 }
270
271 void __pte_error(const char *file, int line, unsigned long val)
272 {
273 printk(KERN_DEFAULT "%s:%d: bad pte %08lx.\n", file, line, val);
274 }
275
276 void __pmd_error(const char *file, int line, unsigned long val)
277 {
278 printk(KERN_DEFAULT "%s:%d: bad pmd %08lx.\n", file, line, val);
279 }
280
281 void __pgd_error(const char *file, int line, unsigned long val)
282 {
283 printk(KERN_DEFAULT "%s:%d: bad pgd %08lx.\n", file, line, val);
284 }
285
286 asmlinkage void __div0(void)
287 {
288 printk(KERN_DEFAULT "Division by zero in kernel.\n");
289 dump_stack();
290 }
291 EXPORT_SYMBOL(__div0);
292
293 void abort(void)
294 {
295 BUG();
296
297 /* if that doesn't kill us, halt */
298 panic("Oops failed to kill thread");
299 }
300 EXPORT_SYMBOL(abort);
301
302 void __init trap_init(void)
303 {
304 return;
305 }
306
307 void __init early_trap_init(void)
308 {
309 unsigned long vectors = VECTORS_BASE;
310
311 /*
312 * Copy the vectors, stubs (in entry-unicore.S)
313 * into the vector page, mapped at 0xffff0000, and ensure these
314 * are visible to the instruction stream.
315 */
316 memcpy((void *)vectors,
317 __vectors_start,
318 __vectors_end - __vectors_start);
319 memcpy((void *)vectors + 0x200,
320 __stubs_start,
321 __stubs_end - __stubs_start);
322
323 early_signal_init();
324
325 flush_icache_range(vectors, vectors + PAGE_SIZE);
326 }