traps.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Hardware exception handling
  3. *
  4. * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
  5. * Copyright (C) 2004 Microtronix Datacom Ltd.
  6. * Copyright (C) 2001 Vic Phillips
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file COPYING in the main directory of this
  10. * archive for more details.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/signal.h>
  15. #include <linux/export.h>
  16. #include <linux/mm.h>
  17. #include <linux/ptrace.h>
  18. #include <asm/traps.h>
  19. #include <asm/sections.h>
  20. #include <asm/uaccess.h>
  21. static DEFINE_SPINLOCK(die_lock);
  22. static void _send_sig(int signo, int code, unsigned long addr)
  23. {
  24. siginfo_t info;
  25. info.si_signo = signo;
  26. info.si_errno = 0;
  27. info.si_code = code;
  28. info.si_addr = (void __user *) addr;
  29. force_sig_info(signo, &info, current);
  30. }
  31. void die(const char *str, struct pt_regs *regs, long err)
  32. {
  33. console_verbose();
  34. spin_lock_irq(&die_lock);
  35. pr_warn("Oops: %s, sig: %ld\n", str, err);
  36. show_regs(regs);
  37. spin_unlock_irq(&die_lock);
  38. /*
  39. * do_exit() should take care of panic'ing from an interrupt
  40. * context so we don't handle it here
  41. */
  42. do_exit(err);
  43. }
  44. void _exception(int signo, struct pt_regs *regs, int code, unsigned long addr)
  45. {
  46. if (!user_mode(regs))
  47. die("Exception in kernel mode", regs, signo);
  48. _send_sig(signo, code, addr);
  49. }
  50. /*
  51. * The show_stack is an external API which we do not use ourselves.
  52. */
  53. int kstack_depth_to_print = 48;
  54. void show_stack(struct task_struct *task, unsigned long *stack)
  55. {
  56. unsigned long *endstack, addr;
  57. int i;
  58. if (!stack) {
  59. if (task)
  60. stack = (unsigned long *)task->thread.ksp;
  61. else
  62. stack = (unsigned long *)&stack;
  63. }
  64. addr = (unsigned long) stack;
  65. endstack = (unsigned long *) PAGE_ALIGN(addr);
  66. pr_emerg("Stack from %08lx:", (unsigned long)stack);
  67. for (i = 0; i < kstack_depth_to_print; i++) {
  68. if (stack + 1 > endstack)
  69. break;
  70. if (i % 8 == 0)
  71. pr_emerg("\n ");
  72. pr_emerg(" %08lx", *stack++);
  73. }
  74. pr_emerg("\nCall Trace:");
  75. i = 0;
  76. while (stack + 1 <= endstack) {
  77. addr = *stack++;
  78. /*
  79. * If the address is either in the text segment of the
  80. * kernel, or in the region which contains vmalloc'ed
  81. * memory, it *may* be the address of a calling
  82. * routine; if so, print it so that someone tracing
  83. * down the cause of the crash will be able to figure
  84. * out the call path that was taken.
  85. */
  86. if (((addr >= (unsigned long) _stext) &&
  87. (addr <= (unsigned long) _etext))) {
  88. if (i % 4 == 0)
  89. pr_emerg("\n ");
  90. pr_emerg(" [<%08lx>]", addr);
  91. i++;
  92. }
  93. }
  94. pr_emerg("\n");
  95. }
  96. void __init trap_init(void)
  97. {
  98. /* Nothing to do here */
  99. }
  100. /* Breakpoint handler */
  101. asmlinkage void breakpoint_c(struct pt_regs *fp)
  102. {
  103. /*
  104. * The breakpoint entry code has moved the PC on by 4 bytes, so we must
  105. * move it back. This could be done on the host but we do it here
  106. * because monitor.S of JTAG gdbserver does it too.
  107. */
  108. fp->ea -= 4;
  109. _exception(SIGTRAP, fp, TRAP_BRKPT, fp->ea);
  110. }
  111. #ifndef CONFIG_NIOS2_ALIGNMENT_TRAP
  112. /* Alignment exception handler */
  113. asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause)
  114. {
  115. unsigned long addr = RDCTL(CTL_BADADDR);
  116. cause >>= 2;
  117. fp->ea -= 4;
  118. if (fixup_exception(fp))
  119. return;
  120. if (!user_mode(fp)) {
  121. pr_alert("Unaligned access from kernel mode, this might be a hardware\n");
  122. pr_alert("problem, dump registers and restart the instruction\n");
  123. pr_alert(" BADADDR 0x%08lx\n", addr);
  124. pr_alert(" cause %d\n", cause);
  125. pr_alert(" op-code 0x%08lx\n", *(unsigned long *)(fp->ea));
  126. show_regs(fp);
  127. return;
  128. }
  129. _exception(SIGBUS, fp, BUS_ADRALN, addr);
  130. }
  131. #endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */
  132. /* Illegal instruction handler */
  133. asmlinkage void handle_illegal_c(struct pt_regs *fp)
  134. {
  135. fp->ea -= 4;
  136. _exception(SIGILL, fp, ILL_ILLOPC, fp->ea);
  137. }
  138. /* Supervisor instruction handler */
  139. asmlinkage void handle_supervisor_instr(struct pt_regs *fp)
  140. {
  141. fp->ea -= 4;
  142. _exception(SIGILL, fp, ILL_PRVOPC, fp->ea);
  143. }
  144. /* Division error handler */
  145. asmlinkage void handle_diverror_c(struct pt_regs *fp)
  146. {
  147. fp->ea -= 4;
  148. _exception(SIGFPE, fp, FPE_INTDIV, fp->ea);
  149. }
  150. /* Unhandled exception handler */
  151. asmlinkage void unhandled_exception(struct pt_regs *regs, int cause)
  152. {
  153. unsigned long addr = RDCTL(CTL_BADADDR);
  154. cause /= 4;
  155. pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n",
  156. cause, user_mode(regs) ? "user" : "kernel", addr);
  157. regs->ea -= 4;
  158. show_regs(regs);
  159. pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs->ea));
  160. }
  161. asmlinkage void handle_trap_1_c(struct pt_regs *fp)
  162. {
  163. _send_sig(SIGUSR1, 0, fp->ea);
  164. }
  165. asmlinkage void handle_trap_2_c(struct pt_regs *fp)
  166. {
  167. _send_sig(SIGUSR2, 0, fp->ea);
  168. }
  169. asmlinkage void handle_trap_3_c(struct pt_regs *fp)
  170. {
  171. _send_sig(SIGILL, ILL_ILLTRP, fp->ea);
  172. }