traps.c 7.6 KB

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