exceptions.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * HW exception handling
  3. *
  4. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  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/kernel.h>
  15. #include <linux/signal.h>
  16. #include <linux/sched.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/module.h>
  19. #include <asm/exceptions.h>
  20. #include <asm/entry.h> /* For KM CPU var */
  21. #include <linux/uaccess.h>
  22. #include <linux/errno.h>
  23. #include <linux/ptrace.h>
  24. #include <asm/current.h>
  25. #define MICROBLAZE_ILL_OPCODE_EXCEPTION 0x02
  26. #define MICROBLAZE_IBUS_EXCEPTION 0x03
  27. #define MICROBLAZE_DBUS_EXCEPTION 0x04
  28. #define MICROBLAZE_DIV_ZERO_EXCEPTION 0x05
  29. #define MICROBLAZE_FPU_EXCEPTION 0x06
  30. #define MICROBLAZE_PRIVILEGED_EXCEPTION 0x07
  31. static DEFINE_SPINLOCK(die_lock);
  32. void die(const char *str, struct pt_regs *fp, long err)
  33. {
  34. console_verbose();
  35. spin_lock_irq(&die_lock);
  36. printk(KERN_WARNING "Oops: %s, sig: %ld\n", str, err);
  37. show_regs(fp);
  38. spin_unlock_irq(&die_lock);
  39. /* do_exit() should take care of panic'ing from an interrupt
  40. * context so we don't handle it here
  41. */
  42. do_exit(err);
  43. }
  44. /* for user application debugging */
  45. void sw_exception(struct pt_regs *regs)
  46. {
  47. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
  48. }
  49. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  50. {
  51. siginfo_t info;
  52. if (kernel_mode(regs)) {
  53. die("Exception in kernel mode", regs, signr);
  54. }
  55. info.si_signo = signr;
  56. info.si_errno = 0;
  57. info.si_code = code;
  58. info.si_addr = (void __user *) addr;
  59. force_sig_info(signr, &info, current);
  60. }
  61. asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
  62. int fsr, int addr)
  63. {
  64. #ifdef CONFIG_MMU
  65. int code;
  66. addr = regs->pc;
  67. #endif
  68. #if 0
  69. printk(KERN_WARNING "Exception %02x in %s mode, FSR=%08x PC=%08x " \
  70. "ESR=%08x\n",
  71. type, user_mode(regs) ? "user" : "kernel", fsr,
  72. (unsigned int) regs->pc, (unsigned int) regs->esr);
  73. #endif
  74. switch (type & 0x1F) {
  75. case MICROBLAZE_ILL_OPCODE_EXCEPTION:
  76. if (user_mode(regs)) {
  77. pr_debug(KERN_WARNING "Illegal opcode exception " \
  78. "in user mode.\n");
  79. _exception(SIGILL, regs, ILL_ILLOPC, addr);
  80. return;
  81. }
  82. printk(KERN_WARNING "Illegal opcode exception " \
  83. "in kernel mode.\n");
  84. die("opcode exception", regs, SIGBUS);
  85. break;
  86. case MICROBLAZE_IBUS_EXCEPTION:
  87. if (user_mode(regs)) {
  88. pr_debug(KERN_WARNING "Instruction bus error " \
  89. "exception in user mode.\n");
  90. _exception(SIGBUS, regs, BUS_ADRERR, addr);
  91. return;
  92. }
  93. printk(KERN_WARNING "Instruction bus error exception " \
  94. "in kernel mode.\n");
  95. die("bus exception", regs, SIGBUS);
  96. break;
  97. case MICROBLAZE_DBUS_EXCEPTION:
  98. if (user_mode(regs)) {
  99. pr_debug(KERN_WARNING "Data bus error exception " \
  100. "in user mode.\n");
  101. _exception(SIGBUS, regs, BUS_ADRERR, addr);
  102. return;
  103. }
  104. printk(KERN_WARNING "Data bus error exception " \
  105. "in kernel mode.\n");
  106. die("bus exception", regs, SIGBUS);
  107. break;
  108. case MICROBLAZE_DIV_ZERO_EXCEPTION:
  109. if (user_mode(regs)) {
  110. pr_debug(KERN_WARNING "Divide by zero exception " \
  111. "in user mode\n");
  112. _exception(SIGILL, regs, FPE_INTDIV, addr);
  113. return;
  114. }
  115. printk(KERN_WARNING "Divide by zero exception " \
  116. "in kernel mode.\n");
  117. die("Divide by zero exception", regs, SIGBUS);
  118. break;
  119. case MICROBLAZE_FPU_EXCEPTION:
  120. pr_debug(KERN_WARNING "FPU exception\n");
  121. /* IEEE FP exception */
  122. /* I removed fsr variable and use code var for storing fsr */
  123. if (fsr & FSR_IO)
  124. fsr = FPE_FLTINV;
  125. else if (fsr & FSR_OF)
  126. fsr = FPE_FLTOVF;
  127. else if (fsr & FSR_UF)
  128. fsr = FPE_FLTUND;
  129. else if (fsr & FSR_DZ)
  130. fsr = FPE_FLTDIV;
  131. else if (fsr & FSR_DO)
  132. fsr = FPE_FLTRES;
  133. _exception(SIGFPE, regs, fsr, addr);
  134. break;
  135. #ifdef CONFIG_MMU
  136. case MICROBLAZE_PRIVILEGED_EXCEPTION:
  137. pr_debug(KERN_WARNING "Privileged exception\n");
  138. /* "brk r0,r0" - used as debug breakpoint - old toolchain */
  139. if (get_user(code, (unsigned long *)regs->pc) == 0
  140. && code == 0x980c0000) {
  141. _exception(SIGTRAP, regs, TRAP_BRKPT, addr);
  142. } else {
  143. _exception(SIGILL, regs, ILL_PRVOPC, addr);
  144. }
  145. break;
  146. #endif
  147. default:
  148. /* FIXME what to do in unexpected exception */
  149. printk(KERN_WARNING "Unexpected exception %02x "
  150. "PC=%08x in %s mode\n", type, (unsigned int) addr,
  151. kernel_mode(regs) ? "kernel" : "user");
  152. }
  153. return;
  154. }