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