exceptions.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <asm/cacheflush.h>
  26. #define MICROBLAZE_ILL_OPCODE_EXCEPTION 0x02
  27. #define MICROBLAZE_IBUS_EXCEPTION 0x03
  28. #define MICROBLAZE_DBUS_EXCEPTION 0x04
  29. #define MICROBLAZE_DIV_ZERO_EXCEPTION 0x05
  30. #define MICROBLAZE_FPU_EXCEPTION 0x06
  31. #define MICROBLAZE_PRIVILEGED_EXCEPTION 0x07
  32. static DEFINE_SPINLOCK(die_lock);
  33. void die(const char *str, struct pt_regs *fp, long err)
  34. {
  35. console_verbose();
  36. spin_lock_irq(&die_lock);
  37. printk(KERN_WARNING "Oops: %s, sig: %ld\n", str, err);
  38. show_regs(fp);
  39. spin_unlock_irq(&die_lock);
  40. /* do_exit() should take care of panic'ing from an interrupt
  41. * context so we don't handle it here
  42. */
  43. do_exit(err);
  44. }
  45. /* for user application debugging */
  46. asmlinkage void sw_exception(struct pt_regs *regs)
  47. {
  48. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->r16);
  49. flush_dcache_range(regs->r16, regs->r16 + 0x4);
  50. flush_icache_range(regs->r16, regs->r16 + 0x4);
  51. }
  52. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  53. {
  54. siginfo_t info;
  55. if (kernel_mode(regs)) {
  56. die("Exception in kernel mode", regs, signr);
  57. }
  58. info.si_signo = signr;
  59. info.si_errno = 0;
  60. info.si_code = code;
  61. info.si_addr = (void __user *) addr;
  62. force_sig_info(signr, &info, current);
  63. }
  64. asmlinkage void full_exception(struct pt_regs *regs, unsigned int type,
  65. int fsr, int addr)
  66. {
  67. #ifdef CONFIG_MMU
  68. addr = regs->pc;
  69. #endif
  70. #if 0
  71. printk(KERN_WARNING "Exception %02x in %s mode, FSR=%08x PC=%08x " \
  72. "ESR=%08x\n",
  73. type, user_mode(regs) ? "user" : "kernel", fsr,
  74. (unsigned int) regs->pc, (unsigned int) regs->esr);
  75. #endif
  76. switch (type & 0x1F) {
  77. case MICROBLAZE_ILL_OPCODE_EXCEPTION:
  78. if (user_mode(regs)) {
  79. pr_debug("Illegal opcode exception in user mode\n");
  80. _exception(SIGILL, regs, ILL_ILLOPC, addr);
  81. return;
  82. }
  83. printk(KERN_WARNING "Illegal opcode exception " \
  84. "in kernel mode.\n");
  85. die("opcode exception", regs, SIGBUS);
  86. break;
  87. case MICROBLAZE_IBUS_EXCEPTION:
  88. if (user_mode(regs)) {
  89. pr_debug("Instruction bus error 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("Data bus error exception in user mode\n");
  100. _exception(SIGBUS, regs, BUS_ADRERR, addr);
  101. return;
  102. }
  103. printk(KERN_WARNING "Data bus error exception " \
  104. "in kernel mode.\n");
  105. die("bus exception", regs, SIGBUS);
  106. break;
  107. case MICROBLAZE_DIV_ZERO_EXCEPTION:
  108. if (user_mode(regs)) {
  109. pr_debug("Divide by zero exception in user mode\n");
  110. _exception(SIGILL, regs, FPE_INTDIV, addr);
  111. return;
  112. }
  113. printk(KERN_WARNING "Divide by zero exception " \
  114. "in kernel mode.\n");
  115. die("Divide by zero exception", regs, SIGBUS);
  116. break;
  117. case MICROBLAZE_FPU_EXCEPTION:
  118. pr_debug("FPU exception\n");
  119. /* IEEE FP exception */
  120. /* I removed fsr variable and use code var for storing fsr */
  121. if (fsr & FSR_IO)
  122. fsr = FPE_FLTINV;
  123. else if (fsr & FSR_OF)
  124. fsr = FPE_FLTOVF;
  125. else if (fsr & FSR_UF)
  126. fsr = FPE_FLTUND;
  127. else if (fsr & FSR_DZ)
  128. fsr = FPE_FLTDIV;
  129. else if (fsr & FSR_DO)
  130. fsr = FPE_FLTRES;
  131. _exception(SIGFPE, regs, fsr, addr);
  132. break;
  133. #ifdef CONFIG_MMU
  134. case MICROBLAZE_PRIVILEGED_EXCEPTION:
  135. pr_debug("Privileged exception\n");
  136. _exception(SIGILL, regs, ILL_PRVOPC, addr);
  137. break;
  138. #endif
  139. default:
  140. /* FIXME what to do in unexpected exception */
  141. printk(KERN_WARNING "Unexpected exception %02x "
  142. "PC=%08x in %s mode\n", type, (unsigned int) addr,
  143. kernel_mode(regs) ? "kernel" : "user");
  144. }
  145. return;
  146. }