traps.c 7.5 KB

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