traps.c 4.6 KB

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