traps.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/module.h>
  18. #include <linux/reboot.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/ptrace.h>
  21. #include <asm/opcode-tile.h>
  22. #include <arch/interrupts.h>
  23. #include <arch/spr_def.h>
  24. void __init trap_init(void)
  25. {
  26. /* Nothing needed here since we link code at .intrpt1 */
  27. }
  28. int unaligned_fixup = 1;
  29. static int __init setup_unaligned_fixup(char *str)
  30. {
  31. /*
  32. * Say "=-1" to completely disable it. If you just do "=0", we
  33. * will still parse the instruction, then fire a SIGBUS with
  34. * the correct address from inside the single_step code.
  35. */
  36. long val;
  37. if (strict_strtol(str, 0, &val) != 0)
  38. return 0;
  39. unaligned_fixup = val;
  40. printk("Fixups for unaligned data accesses are %s\n",
  41. unaligned_fixup >= 0 ?
  42. (unaligned_fixup ? "enabled" : "disabled") :
  43. "completely disabled");
  44. return 1;
  45. }
  46. __setup("unaligned_fixup=", setup_unaligned_fixup);
  47. #if CHIP_HAS_TILE_DMA()
  48. static int dma_disabled;
  49. static int __init nodma(char *str)
  50. {
  51. printk("User-space DMA is disabled\n");
  52. dma_disabled = 1;
  53. return 1;
  54. }
  55. __setup("nodma", nodma);
  56. /* How to decode SPR_GPV_REASON */
  57. #define IRET_ERROR (1U << 31)
  58. #define MT_ERROR (1U << 30)
  59. #define MF_ERROR (1U << 29)
  60. #define SPR_INDEX ((1U << 15) - 1)
  61. #define SPR_MPL_SHIFT 9 /* starting bit position for MPL encoded in SPR */
  62. /*
  63. * See if this GPV is just to notify the kernel of SPR use and we can
  64. * retry the user instruction after adjusting some MPLs suitably.
  65. */
  66. static int retry_gpv(unsigned int gpv_reason)
  67. {
  68. int mpl;
  69. if (gpv_reason & IRET_ERROR)
  70. return 0;
  71. BUG_ON((gpv_reason & (MT_ERROR|MF_ERROR)) == 0);
  72. mpl = (gpv_reason & SPR_INDEX) >> SPR_MPL_SHIFT;
  73. if (mpl == INT_DMA_NOTIFY && !dma_disabled) {
  74. /* User is turning on DMA. Allow it and retry. */
  75. printk(KERN_DEBUG "Process %d/%s is now enabled for DMA\n",
  76. current->pid, current->comm);
  77. BUG_ON(current->thread.tile_dma_state.enabled);
  78. current->thread.tile_dma_state.enabled = 1;
  79. grant_dma_mpls();
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. #endif /* CHIP_HAS_TILE_DMA() */
  85. /* Defined inside do_trap(), below. */
  86. #ifdef __tilegx__
  87. extern tilegx_bundle_bits bpt_code;
  88. #else
  89. extern tile_bundle_bits bpt_code;
  90. #endif
  91. void __kprobes do_trap(struct pt_regs *regs, int fault_num,
  92. unsigned long reason)
  93. {
  94. siginfo_t info = { 0 };
  95. int signo, code;
  96. unsigned long address;
  97. __typeof__(bpt_code) instr;
  98. /* Re-enable interrupts. */
  99. local_irq_enable();
  100. /*
  101. * If it hits in kernel mode and we can't fix it up, just exit the
  102. * current process and hope for the best.
  103. */
  104. if (!user_mode(regs)) {
  105. if (fixup_exception(regs)) /* only UNALIGN_DATA in practice */
  106. return;
  107. printk(KERN_ALERT "Kernel took bad trap %d at PC %#lx\n",
  108. fault_num, regs->pc);
  109. if (fault_num == INT_GPV)
  110. printk(KERN_ALERT "GPV_REASON is %#lx\n", reason);
  111. show_regs(regs);
  112. do_exit(SIGKILL); /* FIXME: implement i386 die() */
  113. return;
  114. }
  115. switch (fault_num) {
  116. case INT_ILL:
  117. asm(".pushsection .rodata.bpt_code,\"a\";"
  118. ".align 8;"
  119. "bpt_code: bpt;"
  120. ".size bpt_code,.-bpt_code;"
  121. ".popsection");
  122. if (copy_from_user(&instr, (void *)regs->pc, sizeof(instr))) {
  123. printk(KERN_ERR "Unreadable instruction for INT_ILL:"
  124. " %#lx\n", regs->pc);
  125. do_exit(SIGKILL);
  126. return;
  127. }
  128. if (instr == bpt_code) {
  129. signo = SIGTRAP;
  130. code = TRAP_BRKPT;
  131. } else {
  132. signo = SIGILL;
  133. code = ILL_ILLOPC;
  134. }
  135. address = regs->pc;
  136. break;
  137. case INT_GPV:
  138. #if CHIP_HAS_TILE_DMA()
  139. if (retry_gpv(reason))
  140. return;
  141. #endif
  142. /*FALLTHROUGH*/
  143. case INT_UDN_ACCESS:
  144. case INT_IDN_ACCESS:
  145. #if CHIP_HAS_SN()
  146. case INT_SN_ACCESS:
  147. #endif
  148. signo = SIGILL;
  149. code = ILL_PRVREG;
  150. address = regs->pc;
  151. break;
  152. case INT_SWINT_3:
  153. case INT_SWINT_2:
  154. case INT_SWINT_0:
  155. signo = SIGILL;
  156. code = ILL_ILLTRP;
  157. address = regs->pc;
  158. break;
  159. case INT_UNALIGN_DATA:
  160. #ifndef __tilegx__ /* FIXME: GX: no single-step yet */
  161. if (unaligned_fixup >= 0) {
  162. struct single_step_state *state =
  163. current_thread_info()->step_state;
  164. if (!state || (void *)(regs->pc) != state->buffer) {
  165. single_step_once(regs);
  166. return;
  167. }
  168. }
  169. #endif
  170. signo = SIGBUS;
  171. code = BUS_ADRALN;
  172. address = 0;
  173. break;
  174. case INT_DOUBLE_FAULT:
  175. /*
  176. * For double fault, "reason" is actually passed as
  177. * SYSTEM_SAVE_1_2, the hypervisor's double-fault info, so
  178. * we can provide the original fault number rather than
  179. * the uninteresting "INT_DOUBLE_FAULT" so the user can
  180. * learn what actually struck while PL0 ICS was set.
  181. */
  182. fault_num = reason;
  183. signo = SIGILL;
  184. code = ILL_DBLFLT;
  185. address = regs->pc;
  186. break;
  187. #ifdef __tilegx__
  188. case INT_ILL_TRANS:
  189. signo = SIGSEGV;
  190. code = SEGV_MAPERR;
  191. if (reason & SPR_ILL_TRANS_REASON__I_STREAM_VA_RMASK)
  192. address = regs->pc;
  193. else
  194. address = 0; /* FIXME: GX: single-step for address */
  195. break;
  196. #endif
  197. default:
  198. panic("Unexpected do_trap interrupt number %d", fault_num);
  199. return;
  200. }
  201. info.si_signo = signo;
  202. info.si_code = code;
  203. info.si_addr = (void *)address;
  204. if (signo == SIGILL)
  205. info.si_trapno = fault_num;
  206. force_sig_info(signo, &info, current);
  207. }
  208. extern void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52);
  209. void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
  210. {
  211. _dump_stack(dummy, pc, lr, sp, r52);
  212. printk("Double fault: exiting\n");
  213. machine_halt();
  214. }