traps.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <asm/opcode_constants.h>
  23. #include <asm/stack.h>
  24. #include <asm/traps.h>
  25. #include <arch/interrupts.h>
  26. #include <arch/spr_def.h>
  27. void __init trap_init(void)
  28. {
  29. /* Nothing needed here since we link code at .intrpt1 */
  30. }
  31. int unaligned_fixup = 1;
  32. static int __init setup_unaligned_fixup(char *str)
  33. {
  34. /*
  35. * Say "=-1" to completely disable it. If you just do "=0", we
  36. * will still parse the instruction, then fire a SIGBUS with
  37. * the correct address from inside the single_step code.
  38. */
  39. long val;
  40. if (strict_strtol(str, 0, &val) != 0)
  41. return 0;
  42. unaligned_fixup = val;
  43. pr_info("Fixups for unaligned data accesses are %s\n",
  44. unaligned_fixup >= 0 ?
  45. (unaligned_fixup ? "enabled" : "disabled") :
  46. "completely disabled");
  47. return 1;
  48. }
  49. __setup("unaligned_fixup=", setup_unaligned_fixup);
  50. #if CHIP_HAS_TILE_DMA()
  51. static int dma_disabled;
  52. static int __init nodma(char *str)
  53. {
  54. pr_info("User-space DMA is disabled\n");
  55. dma_disabled = 1;
  56. return 1;
  57. }
  58. __setup("nodma", nodma);
  59. /* How to decode SPR_GPV_REASON */
  60. #define IRET_ERROR (1U << 31)
  61. #define MT_ERROR (1U << 30)
  62. #define MF_ERROR (1U << 29)
  63. #define SPR_INDEX ((1U << 15) - 1)
  64. #define SPR_MPL_SHIFT 9 /* starting bit position for MPL encoded in SPR */
  65. /*
  66. * See if this GPV is just to notify the kernel of SPR use and we can
  67. * retry the user instruction after adjusting some MPLs suitably.
  68. */
  69. static int retry_gpv(unsigned int gpv_reason)
  70. {
  71. int mpl;
  72. if (gpv_reason & IRET_ERROR)
  73. return 0;
  74. BUG_ON((gpv_reason & (MT_ERROR|MF_ERROR)) == 0);
  75. mpl = (gpv_reason & SPR_INDEX) >> SPR_MPL_SHIFT;
  76. if (mpl == INT_DMA_NOTIFY && !dma_disabled) {
  77. /* User is turning on DMA. Allow it and retry. */
  78. printk(KERN_DEBUG "Process %d/%s is now enabled for DMA\n",
  79. current->pid, current->comm);
  80. BUG_ON(current->thread.tile_dma_state.enabled);
  81. current->thread.tile_dma_state.enabled = 1;
  82. grant_dma_mpls();
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. #endif /* CHIP_HAS_TILE_DMA() */
  88. #ifdef __tilegx__
  89. #define bundle_bits tilegx_bundle_bits
  90. #else
  91. #define bundle_bits tile_bundle_bits
  92. #endif
  93. extern bundle_bits bpt_code;
  94. asm(".pushsection .rodata.bpt_code,\"a\";"
  95. ".align 8;"
  96. "bpt_code: bpt;"
  97. ".size bpt_code,.-bpt_code;"
  98. ".popsection");
  99. static int special_ill(bundle_bits bundle, int *sigp, int *codep)
  100. {
  101. int sig, code, maxcode;
  102. if (bundle == bpt_code) {
  103. *sigp = SIGTRAP;
  104. *codep = TRAP_BRKPT;
  105. return 1;
  106. }
  107. /* If it's a "raise" bundle, then "ill" must be in pipe X1. */
  108. #ifdef __tilegx__
  109. if ((bundle & TILEGX_BUNDLE_MODE_MASK) != 0)
  110. return 0;
  111. if (get_Opcode_X1(bundle) != RRR_0_OPCODE_X1)
  112. return 0;
  113. if (get_RRROpcodeExtension_X1(bundle) != UNARY_RRR_0_OPCODE_X1)
  114. return 0;
  115. if (get_UnaryOpcodeExtension_X1(bundle) != ILL_UNARY_OPCODE_X1)
  116. return 0;
  117. #else
  118. if (bundle & TILE_BUNDLE_Y_ENCODING_MASK)
  119. return 0;
  120. if (get_Opcode_X1(bundle) != SHUN_0_OPCODE_X1)
  121. return 0;
  122. if (get_UnShOpcodeExtension_X1(bundle) != UN_0_SHUN_0_OPCODE_X1)
  123. return 0;
  124. if (get_UnOpcodeExtension_X1(bundle) != ILL_UN_0_SHUN_0_OPCODE_X1)
  125. return 0;
  126. #endif
  127. /* Check that the magic distinguishers are set to mean "raise". */
  128. if (get_Dest_X1(bundle) != 29 || get_SrcA_X1(bundle) != 37)
  129. return 0;
  130. /* There must be an "addli zero, zero, VAL" in X0. */
  131. if (get_Opcode_X0(bundle) != ADDLI_OPCODE_X0)
  132. return 0;
  133. if (get_Dest_X0(bundle) != TREG_ZERO)
  134. return 0;
  135. if (get_SrcA_X0(bundle) != TREG_ZERO)
  136. return 0;
  137. /*
  138. * Validate the proposed signal number and si_code value.
  139. * Note that we embed these in the static instruction itself
  140. * so that we perturb the register state as little as possible
  141. * at the time of the actual fault; it's unlikely you'd ever
  142. * need to dynamically choose which kind of fault to raise
  143. * from user space.
  144. */
  145. sig = get_Imm16_X0(bundle) & 0x3f;
  146. switch (sig) {
  147. case SIGILL:
  148. maxcode = NSIGILL;
  149. break;
  150. case SIGFPE:
  151. maxcode = NSIGFPE;
  152. break;
  153. case SIGSEGV:
  154. maxcode = NSIGSEGV;
  155. break;
  156. case SIGBUS:
  157. maxcode = NSIGBUS;
  158. break;
  159. case SIGTRAP:
  160. maxcode = NSIGTRAP;
  161. break;
  162. default:
  163. return 0;
  164. }
  165. code = (get_Imm16_X0(bundle) >> 6) & 0xf;
  166. if (code <= 0 || code > maxcode)
  167. return 0;
  168. /* Make it the requested signal. */
  169. *sigp = sig;
  170. *codep = code | __SI_FAULT;
  171. return 1;
  172. }
  173. void __kprobes do_trap(struct pt_regs *regs, int fault_num,
  174. unsigned long reason)
  175. {
  176. siginfo_t info = { 0 };
  177. int signo, code;
  178. unsigned long address;
  179. bundle_bits instr;
  180. /* Re-enable interrupts. */
  181. local_irq_enable();
  182. /*
  183. * If it hits in kernel mode and we can't fix it up, just exit the
  184. * current process and hope for the best.
  185. */
  186. if (!user_mode(regs)) {
  187. if (fixup_exception(regs)) /* only UNALIGN_DATA in practice */
  188. return;
  189. pr_alert("Kernel took bad trap %d at PC %#lx\n",
  190. fault_num, regs->pc);
  191. if (fault_num == INT_GPV)
  192. pr_alert("GPV_REASON is %#lx\n", reason);
  193. show_regs(regs);
  194. do_exit(SIGKILL); /* FIXME: implement i386 die() */
  195. return;
  196. }
  197. switch (fault_num) {
  198. case INT_ILL:
  199. if (copy_from_user(&instr, (void __user *)regs->pc,
  200. sizeof(instr))) {
  201. pr_err("Unreadable instruction for INT_ILL:"
  202. " %#lx\n", regs->pc);
  203. do_exit(SIGKILL);
  204. return;
  205. }
  206. if (!special_ill(instr, &signo, &code)) {
  207. signo = SIGILL;
  208. code = ILL_ILLOPC;
  209. }
  210. address = regs->pc;
  211. break;
  212. case INT_GPV:
  213. #if CHIP_HAS_TILE_DMA()
  214. if (retry_gpv(reason))
  215. return;
  216. #endif
  217. /*FALLTHROUGH*/
  218. case INT_UDN_ACCESS:
  219. case INT_IDN_ACCESS:
  220. #if CHIP_HAS_SN()
  221. case INT_SN_ACCESS:
  222. #endif
  223. signo = SIGILL;
  224. code = ILL_PRVREG;
  225. address = regs->pc;
  226. break;
  227. case INT_SWINT_3:
  228. case INT_SWINT_2:
  229. case INT_SWINT_0:
  230. signo = SIGILL;
  231. code = ILL_ILLTRP;
  232. address = regs->pc;
  233. break;
  234. case INT_UNALIGN_DATA:
  235. #ifndef __tilegx__ /* Emulated support for single step debugging */
  236. if (unaligned_fixup >= 0) {
  237. struct single_step_state *state =
  238. current_thread_info()->step_state;
  239. if (!state ||
  240. (void __user *)(regs->pc) != state->buffer) {
  241. single_step_once(regs);
  242. return;
  243. }
  244. }
  245. #endif
  246. signo = SIGBUS;
  247. code = BUS_ADRALN;
  248. address = 0;
  249. break;
  250. case INT_DOUBLE_FAULT:
  251. /*
  252. * For double fault, "reason" is actually passed as
  253. * SYSTEM_SAVE_K_2, the hypervisor's double-fault info, so
  254. * we can provide the original fault number rather than
  255. * the uninteresting "INT_DOUBLE_FAULT" so the user can
  256. * learn what actually struck while PL0 ICS was set.
  257. */
  258. fault_num = reason;
  259. signo = SIGILL;
  260. code = ILL_DBLFLT;
  261. address = regs->pc;
  262. break;
  263. #ifdef __tilegx__
  264. case INT_ILL_TRANS:
  265. signo = SIGSEGV;
  266. code = SEGV_MAPERR;
  267. if (reason & SPR_ILL_TRANS_REASON__I_STREAM_VA_RMASK)
  268. address = regs->pc;
  269. else
  270. address = 0; /* FIXME: GX: single-step for address */
  271. break;
  272. #endif
  273. default:
  274. panic("Unexpected do_trap interrupt number %d", fault_num);
  275. return;
  276. }
  277. info.si_signo = signo;
  278. info.si_code = code;
  279. info.si_addr = (void __user *)address;
  280. if (signo == SIGILL)
  281. info.si_trapno = fault_num;
  282. trace_unhandled_signal("trap", regs, address, signo);
  283. force_sig_info(signo, &info, current);
  284. }
  285. void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
  286. {
  287. _dump_stack(dummy, pc, lr, sp, r52);
  288. pr_emerg("Double fault: exiting\n");
  289. machine_halt();
  290. }