traps.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * OpenRISC traps.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * Here we handle the break vectors not used by the system call
  18. * mechanism, as well as some general stack/register dumping
  19. * things.
  20. *
  21. */
  22. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/kmod.h>
  27. #include <linux/string.h>
  28. #include <linux/errno.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/timer.h>
  31. #include <linux/mm.h>
  32. #include <linux/kallsyms.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/segment.h>
  35. #include <asm/io.h>
  36. #include <asm/pgtable.h>
  37. extern char _etext, _stext;
  38. int kstack_depth_to_print = 0x180;
  39. static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
  40. {
  41. return p > (void *)tinfo && p < (void *)tinfo + THREAD_SIZE - 3;
  42. }
  43. void show_trace(struct task_struct *task, unsigned long *stack)
  44. {
  45. struct thread_info *context;
  46. unsigned long addr;
  47. context = (struct thread_info *)
  48. ((unsigned long)stack & (~(THREAD_SIZE - 1)));
  49. while (valid_stack_ptr(context, stack)) {
  50. addr = *stack++;
  51. if (__kernel_text_address(addr)) {
  52. printk(" [<%08lx>]", addr);
  53. print_symbol(" %s", addr);
  54. printk("\n");
  55. }
  56. }
  57. printk(" =======================\n");
  58. }
  59. /* displays a short stack trace */
  60. void show_stack(struct task_struct *task, unsigned long *esp)
  61. {
  62. unsigned long addr, *stack;
  63. int i;
  64. if (esp == NULL)
  65. esp = (unsigned long *)&esp;
  66. stack = esp;
  67. printk("Stack dump [0x%08lx]:\n", (unsigned long)esp);
  68. for (i = 0; i < kstack_depth_to_print; i++) {
  69. if (kstack_end(stack))
  70. break;
  71. if (__get_user(addr, stack)) {
  72. /* This message matches "failing address" marked
  73. s390 in ksymoops, so lines containing it will
  74. not be filtered out by ksymoops. */
  75. printk("Failing address 0x%lx\n", (unsigned long)stack);
  76. break;
  77. }
  78. stack++;
  79. printk("sp + %02d: 0x%08lx\n", i * 4, addr);
  80. }
  81. printk("\n");
  82. show_trace(task, esp);
  83. return;
  84. }
  85. void show_trace_task(struct task_struct *tsk)
  86. {
  87. /*
  88. * TODO: SysRq-T trace dump...
  89. */
  90. }
  91. void show_registers(struct pt_regs *regs)
  92. {
  93. int i;
  94. int in_kernel = 1;
  95. unsigned long esp;
  96. esp = (unsigned long)(&regs->sp);
  97. if (user_mode(regs))
  98. in_kernel = 0;
  99. printk("CPU #: %d\n"
  100. " PC: %08lx SR: %08lx SP: %08lx\n",
  101. smp_processor_id(), regs->pc, regs->sr, regs->sp);
  102. printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
  103. 0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
  104. printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
  105. regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
  106. printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
  107. regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
  108. printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
  109. regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
  110. printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
  111. regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
  112. printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
  113. regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
  114. printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
  115. regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
  116. printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
  117. regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
  118. printk(" RES: %08lx oGPR11: %08lx\n",
  119. regs->gpr[11], regs->orig_gpr11);
  120. printk("Process %s (pid: %d, stackpage=%08lx)\n",
  121. current->comm, current->pid, (unsigned long)current);
  122. /*
  123. * When in-kernel, we also print out the stack and code at the
  124. * time of the fault..
  125. */
  126. if (in_kernel) {
  127. printk("\nStack: ");
  128. show_stack(NULL, (unsigned long *)esp);
  129. printk("\nCode: ");
  130. if (regs->pc < PAGE_OFFSET)
  131. goto bad;
  132. for (i = -24; i < 24; i++) {
  133. unsigned char c;
  134. if (__get_user(c, &((unsigned char *)regs->pc)[i])) {
  135. bad:
  136. printk(" Bad PC value.");
  137. break;
  138. }
  139. if (i == 0)
  140. printk("(%02x) ", c);
  141. else
  142. printk("%02x ", c);
  143. }
  144. }
  145. printk("\n");
  146. }
  147. void nommu_dump_state(struct pt_regs *regs,
  148. unsigned long ea, unsigned long vector)
  149. {
  150. int i;
  151. unsigned long addr, stack = regs->sp;
  152. printk("\n\r[nommu_dump_state] :: ea %lx, vector %lx\n\r", ea, vector);
  153. printk("CPU #: %d\n"
  154. " PC: %08lx SR: %08lx SP: %08lx\n",
  155. 0, regs->pc, regs->sr, regs->sp);
  156. printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
  157. 0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
  158. printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
  159. regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
  160. printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
  161. regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
  162. printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
  163. regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
  164. printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
  165. regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
  166. printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
  167. regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
  168. printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
  169. regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
  170. printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
  171. regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
  172. printk(" RES: %08lx oGPR11: %08lx\n",
  173. regs->gpr[11], regs->orig_gpr11);
  174. printk("Process %s (pid: %d, stackpage=%08lx)\n",
  175. ((struct task_struct *)(__pa(current)))->comm,
  176. ((struct task_struct *)(__pa(current)))->pid,
  177. (unsigned long)current);
  178. printk("\nStack: ");
  179. printk("Stack dump [0x%08lx]:\n", (unsigned long)stack);
  180. for (i = 0; i < kstack_depth_to_print; i++) {
  181. if (((long)stack & (THREAD_SIZE - 1)) == 0)
  182. break;
  183. stack++;
  184. printk("%lx :: sp + %02d: 0x%08lx\n", stack, i * 4,
  185. *((unsigned long *)(__pa(stack))));
  186. }
  187. printk("\n");
  188. printk("Call Trace: ");
  189. i = 1;
  190. while (((long)stack & (THREAD_SIZE - 1)) != 0) {
  191. addr = *((unsigned long *)__pa(stack));
  192. stack++;
  193. if (kernel_text_address(addr)) {
  194. if (i && ((i % 6) == 0))
  195. printk("\n ");
  196. printk(" [<%08lx>]", addr);
  197. i++;
  198. }
  199. }
  200. printk("\n");
  201. printk("\nCode: ");
  202. for (i = -24; i < 24; i++) {
  203. unsigned char c;
  204. c = ((unsigned char *)(__pa(regs->pc)))[i];
  205. if (i == 0)
  206. printk("(%02x) ", c);
  207. else
  208. printk("%02x ", c);
  209. }
  210. printk("\n");
  211. }
  212. /* This is normally the 'Oops' routine */
  213. void die(const char *str, struct pt_regs *regs, long err)
  214. {
  215. console_verbose();
  216. printk("\n%s#: %04lx\n", str, err & 0xffff);
  217. show_registers(regs);
  218. #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
  219. printk("\n\nUNHANDLED_EXCEPTION: entering infinite loop\n");
  220. /* shut down interrupts */
  221. local_irq_disable();
  222. __asm__ __volatile__("l.nop 1");
  223. do {} while (1);
  224. #endif
  225. do_exit(SIGSEGV);
  226. }
  227. /* This is normally the 'Oops' routine */
  228. void die_if_kernel(const char *str, struct pt_regs *regs, long err)
  229. {
  230. if (user_mode(regs))
  231. return;
  232. die(str, regs, err);
  233. }
  234. void unhandled_exception(struct pt_regs *regs, int ea, int vector)
  235. {
  236. printk("Unable to handle exception at EA =0x%x, vector 0x%x",
  237. ea, vector);
  238. die("Oops", regs, 9);
  239. }
  240. void __init trap_init(void)
  241. {
  242. /* Nothing needs to be done */
  243. }
  244. asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
  245. {
  246. siginfo_t info;
  247. memset(&info, 0, sizeof(info));
  248. info.si_signo = SIGTRAP;
  249. info.si_code = TRAP_TRACE;
  250. info.si_addr = (void *)address;
  251. force_sig_info(SIGTRAP, &info, current);
  252. regs->pc += 4;
  253. }
  254. asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
  255. {
  256. siginfo_t info;
  257. if (user_mode(regs)) {
  258. /* Send a SIGSEGV */
  259. info.si_signo = SIGSEGV;
  260. info.si_errno = 0;
  261. /* info.si_code has been set above */
  262. info.si_addr = (void *)address;
  263. force_sig_info(SIGSEGV, &info, current);
  264. } else {
  265. printk("KERNEL: Unaligned Access 0x%.8lx\n", address);
  266. show_registers(regs);
  267. die("Die:", regs, address);
  268. }
  269. }
  270. asmlinkage void do_bus_fault(struct pt_regs *regs, unsigned long address)
  271. {
  272. siginfo_t info;
  273. if (user_mode(regs)) {
  274. /* Send a SIGBUS */
  275. info.si_signo = SIGBUS;
  276. info.si_errno = 0;
  277. info.si_code = BUS_ADRERR;
  278. info.si_addr = (void *)address;
  279. force_sig_info(SIGBUS, &info, current);
  280. } else { /* Kernel mode */
  281. printk("KERNEL: Bus error (SIGBUS) 0x%.8lx\n", address);
  282. show_registers(regs);
  283. die("Die:", regs, address);
  284. }
  285. }
  286. asmlinkage void do_illegal_instruction(struct pt_regs *regs,
  287. unsigned long address)
  288. {
  289. siginfo_t info;
  290. if (user_mode(regs)) {
  291. /* Send a SIGILL */
  292. info.si_signo = SIGILL;
  293. info.si_errno = 0;
  294. info.si_code = ILL_ILLOPC;
  295. info.si_addr = (void *)address;
  296. force_sig_info(SIGBUS, &info, current);
  297. } else { /* Kernel mode */
  298. printk("KERNEL: Illegal instruction (SIGILL) 0x%.8lx\n",
  299. address);
  300. show_registers(regs);
  301. die("Die:", regs, address);
  302. }
  303. }