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