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