traps.c 4.6 KB

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