exceptions.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * HW exception handling
  3. *
  4. * Copyright (C) 2008-2009 Michal Simek <[email protected]>
  5. * Copyright (C) 2008 PetaLogix
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. */
  11. /*
  12. * This file handles the architecture-dependent parts of hardware exceptions
  13. */
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/signal.h>
  17. #include <linux/sched.h>
  18. #include <linux/sched/debug.h>
  19. #include <linux/kallsyms.h>
  20. #include <asm/exceptions.h>
  21. #include <asm/entry.h> /* For KM CPU var */
  22. #include <linux/uaccess.h>
  23. #include <linux/errno.h>
  24. #include <linux/ptrace.h>
  25. #include <asm/current.h>
  26. #include <asm/cacheflush.h>
  27. #define MICROBLAZE_ILL_OPCODE_EXCEPTION 0x02
  28. #define MICROBLAZE_IBUS_EXCEPTION 0x03
  29. #define MICROBLAZE_DBUS_EXCEPTION 0x04
  30. #define MICROBLAZE_DIV_ZERO_EXCEPTION 0x05
  31. #define MICROBLAZE_FPU_EXCEPTION 0x06
  32. #define MICROBLAZE_PRIVILEGED_EXCEPTION 0x07
  33. static DEFINE_SPINLOCK(die_lock);
  34. void die(const char *str, struct pt_regs *fp, long err)
  35. {
  36. console_verbose();
  37. spin_lock_irq(&die_lock);
  38. pr_warn("Oops: %s, sig: %ld\n", str, err);
  39. show_regs(fp);
  40. spin_unlock_irq(&die_lock);
  41. /* make_task_dead() should take care of panic'ing from an interrupt
  42. * context so we don't handle it here
  43. */
  44. make_task_dead(err);
  45. }
  46. /* for user application debugging */
  47. asmlinkage void sw_exception(struct pt_regs *regs)
  48. {
  49. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
  50. flush_dcache_range(regs->r16, regs->r16 + 0x4);
  51. flush_icache_range(regs->r16, regs->r16 + 0x4);
  52. }
  53. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  54. {
  55. if (kernel_mode(regs))
  56. die("Exception in kernel mode", regs, signr);
  57. force_sig_fault(signr, code, (void __user *)addr);
  58. }
  59. asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
  60. int fsr, int addr)
  61. {
  62. addr = regs->pc;
  63. #if 0
  64. pr_warn("Exception %02x in %s mode, FSR=%08x PC=%08x ESR=%08x\n",
  65. type, user_mode(regs) ? "user" : "kernel", fsr,
  66. (unsigned int) regs->pc, (unsigned int) regs->esr);
  67. #endif
  68. switch (type & 0x1F) {
  69. case MICROBLAZE_ILL_OPCODE_EXCEPTION:
  70. if (user_mode(regs)) {
  71. pr_debug("Illegal opcode exception in user mode\n");
  72. _exception(SIGILL, regs, ILL_ILLOPC, addr);
  73. return;
  74. }
  75. pr_warn("Illegal opcode exception in kernel mode.\n");
  76. die("opcode exception", regs, SIGBUS);
  77. break;
  78. case MICROBLAZE_IBUS_EXCEPTION:
  79. if (user_mode(regs)) {
  80. pr_debug("Instruction bus error exception in user mode\n");
  81. _exception(SIGBUS, regs, BUS_ADRERR, addr);
  82. return;
  83. }
  84. pr_warn("Instruction bus error exception in kernel mode.\n");
  85. die("bus exception", regs, SIGBUS);
  86. break;
  87. case MICROBLAZE_DBUS_EXCEPTION:
  88. if (user_mode(regs)) {
  89. pr_debug("Data bus error exception in user mode\n");
  90. _exception(SIGBUS, regs, BUS_ADRERR, addr);
  91. return;
  92. }
  93. pr_warn("Data bus error exception in kernel mode.\n");
  94. die("bus exception", regs, SIGBUS);
  95. break;
  96. case MICROBLAZE_DIV_ZERO_EXCEPTION:
  97. if (user_mode(regs)) {
  98. pr_debug("Divide by zero exception in user mode\n");
  99. _exception(SIGFPE, regs, FPE_INTDIV, addr);
  100. return;
  101. }
  102. pr_warn("Divide by zero exception in kernel mode.\n");
  103. die("Divide by zero exception", regs, SIGBUS);
  104. break;
  105. case MICROBLAZE_FPU_EXCEPTION:
  106. pr_debug("FPU exception\n");
  107. /* IEEE FP exception */
  108. /* I removed fsr variable and use code var for storing fsr */
  109. if (fsr & FSR_IO)
  110. fsr = FPE_FLTINV;
  111. else if (fsr & FSR_OF)
  112. fsr = FPE_FLTOVF;
  113. else if (fsr & FSR_UF)
  114. fsr = FPE_FLTUND;
  115. else if (fsr & FSR_DZ)
  116. fsr = FPE_FLTDIV;
  117. else if (fsr & FSR_DO)
  118. fsr = FPE_FLTRES;
  119. _exception(SIGFPE, regs, fsr, addr);
  120. break;
  121. case MICROBLAZE_PRIVILEGED_EXCEPTION:
  122. pr_debug("Privileged exception\n");
  123. _exception(SIGILL, regs, ILL_PRVOPC, addr);
  124. break;
  125. default:
  126. /* FIXME what to do in unexpected exception */
  127. pr_warn("Unexpected exception %02x PC=%08x in %s mode\n",
  128. type, (unsigned int) addr,
  129. kernel_mode(regs) ? "kernel" : "user");
  130. }
  131. return;
  132. }