traps.c 7.8 KB

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