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