traps.c 7.5 KB

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