traps.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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);
  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 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. void __init trap_init(void)
  94. {
  95. /* Nothing to do here */
  96. }
  97. /* Breakpoint handler */
  98. asmlinkage void breakpoint_c(struct pt_regs *fp)
  99. {
  100. /*
  101. * The breakpoint entry code has moved the PC on by 4 bytes, so we must
  102. * move it back. This could be done on the host but we do it here
  103. * because monitor.S of JTAG gdbserver does it too.
  104. */
  105. fp->ea -= 4;
  106. _exception(SIGTRAP, fp, TRAP_BRKPT, fp->ea);
  107. }
  108. #ifndef CONFIG_NIOS2_ALIGNMENT_TRAP
  109. /* Alignment exception handler */
  110. asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause)
  111. {
  112. unsigned long addr = RDCTL(CTL_BADADDR);
  113. cause >>= 2;
  114. fp->ea -= 4;
  115. if (fixup_exception(fp))
  116. return;
  117. if (!user_mode(fp)) {
  118. pr_alert("Unaligned access from kernel mode, this might be a hardware\n");
  119. pr_alert("problem, dump registers and restart the instruction\n");
  120. pr_alert(" BADADDR 0x%08lx\n", addr);
  121. pr_alert(" cause %d\n", cause);
  122. pr_alert(" op-code 0x%08lx\n", *(unsigned long *)(fp->ea));
  123. show_regs(fp);
  124. return;
  125. }
  126. _exception(SIGBUS, fp, BUS_ADRALN, addr);
  127. }
  128. #endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */
  129. /* Illegal instruction handler */
  130. asmlinkage void handle_illegal_c(struct pt_regs *fp)
  131. {
  132. fp->ea -= 4;
  133. _exception(SIGILL, fp, ILL_ILLOPC, fp->ea);
  134. }
  135. /* Supervisor instruction handler */
  136. asmlinkage void handle_supervisor_instr(struct pt_regs *fp)
  137. {
  138. fp->ea -= 4;
  139. _exception(SIGILL, fp, ILL_PRVOPC, fp->ea);
  140. }
  141. /* Division error handler */
  142. asmlinkage void handle_diverror_c(struct pt_regs *fp)
  143. {
  144. fp->ea -= 4;
  145. _exception(SIGFPE, fp, FPE_INTDIV, fp->ea);
  146. }
  147. /* Unhandled exception handler */
  148. asmlinkage void unhandled_exception(struct pt_regs *regs, int cause)
  149. {
  150. unsigned long addr = RDCTL(CTL_BADADDR);
  151. cause /= 4;
  152. pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n",
  153. cause, user_mode(regs) ? "user" : "kernel", addr);
  154. regs->ea -= 4;
  155. show_regs(regs);
  156. pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs->ea));
  157. }
  158. asmlinkage void handle_trap_1_c(struct pt_regs *fp)
  159. {
  160. _send_sig(SIGUSR1, 0, fp->ea);
  161. }
  162. asmlinkage void handle_trap_2_c(struct pt_regs *fp)
  163. {
  164. _send_sig(SIGUSR2, 0, fp->ea);
  165. }
  166. asmlinkage void handle_trap_3_c(struct pt_regs *fp)
  167. {
  168. _send_sig(SIGILL, ILL_ILLTRP, fp->ea);
  169. }