traps.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <asm/setup.h>
  24. #include <arch/interrupts.h>
  25. #include <arch/spr_def.h>
  26. #include <arch/opcode.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 & TILEPRO_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. static const char *const int_name[] = {
  174. [INT_MEM_ERROR] = "Memory error",
  175. [INT_ILL] = "Illegal instruction",
  176. [INT_GPV] = "General protection violation",
  177. [INT_UDN_ACCESS] = "UDN access",
  178. [INT_IDN_ACCESS] = "IDN access",
  179. #if CHIP_HAS_SN()
  180. [INT_SN_ACCESS] = "SN access",
  181. #endif
  182. [INT_SWINT_3] = "Software interrupt 3",
  183. [INT_SWINT_2] = "Software interrupt 2",
  184. [INT_SWINT_0] = "Software interrupt 0",
  185. [INT_UNALIGN_DATA] = "Unaligned data",
  186. [INT_DOUBLE_FAULT] = "Double fault",
  187. #ifdef __tilegx__
  188. [INT_ILL_TRANS] = "Illegal virtual address",
  189. #endif
  190. };
  191. void __kprobes do_trap(struct pt_regs *regs, int fault_num,
  192. unsigned long reason)
  193. {
  194. siginfo_t info = { 0 };
  195. int signo, code;
  196. unsigned long address = 0;
  197. bundle_bits instr;
  198. /* Re-enable interrupts. */
  199. local_irq_enable();
  200. /*
  201. * If it hits in kernel mode and we can't fix it up, just exit the
  202. * current process and hope for the best.
  203. */
  204. if (!user_mode(regs)) {
  205. const char *name;
  206. if (fixup_exception(regs)) /* only UNALIGN_DATA in practice */
  207. return;
  208. if (fault_num >= 0 &&
  209. fault_num < sizeof(int_name)/sizeof(int_name[0]) &&
  210. int_name[fault_num] != NULL)
  211. name = int_name[fault_num];
  212. else
  213. name = "Unknown interrupt";
  214. pr_alert("Kernel took bad trap %d (%s) at PC %#lx\n",
  215. fault_num, name, regs->pc);
  216. if (fault_num == INT_GPV)
  217. pr_alert("GPV_REASON is %#lx\n", reason);
  218. show_regs(regs);
  219. do_exit(SIGKILL); /* FIXME: implement i386 die() */
  220. return;
  221. }
  222. switch (fault_num) {
  223. case INT_MEM_ERROR:
  224. signo = SIGBUS;
  225. code = BUS_OBJERR;
  226. break;
  227. case INT_ILL:
  228. if (copy_from_user(&instr, (void __user *)regs->pc,
  229. sizeof(instr))) {
  230. pr_err("Unreadable instruction for INT_ILL:"
  231. " %#lx\n", regs->pc);
  232. do_exit(SIGKILL);
  233. return;
  234. }
  235. if (!special_ill(instr, &signo, &code)) {
  236. signo = SIGILL;
  237. code = ILL_ILLOPC;
  238. }
  239. address = regs->pc;
  240. break;
  241. case INT_GPV:
  242. #if CHIP_HAS_TILE_DMA()
  243. if (retry_gpv(reason))
  244. return;
  245. #endif
  246. /*FALLTHROUGH*/
  247. case INT_UDN_ACCESS:
  248. case INT_IDN_ACCESS:
  249. #if CHIP_HAS_SN()
  250. case INT_SN_ACCESS:
  251. #endif
  252. signo = SIGILL;
  253. code = ILL_PRVREG;
  254. address = regs->pc;
  255. break;
  256. case INT_SWINT_3:
  257. case INT_SWINT_2:
  258. case INT_SWINT_0:
  259. signo = SIGILL;
  260. code = ILL_ILLTRP;
  261. address = regs->pc;
  262. break;
  263. case INT_UNALIGN_DATA:
  264. #ifndef __tilegx__ /* Emulated support for single step debugging */
  265. if (unaligned_fixup >= 0) {
  266. struct single_step_state *state =
  267. current_thread_info()->step_state;
  268. if (!state ||
  269. (void __user *)(regs->pc) != state->buffer) {
  270. single_step_once(regs);
  271. return;
  272. }
  273. }
  274. #endif
  275. signo = SIGBUS;
  276. code = BUS_ADRALN;
  277. address = 0;
  278. break;
  279. case INT_DOUBLE_FAULT:
  280. /*
  281. * For double fault, "reason" is actually passed as
  282. * SYSTEM_SAVE_K_2, the hypervisor's double-fault info, so
  283. * we can provide the original fault number rather than
  284. * the uninteresting "INT_DOUBLE_FAULT" so the user can
  285. * learn what actually struck while PL0 ICS was set.
  286. */
  287. fault_num = reason;
  288. signo = SIGILL;
  289. code = ILL_DBLFLT;
  290. address = regs->pc;
  291. break;
  292. #ifdef __tilegx__
  293. case INT_ILL_TRANS: {
  294. /* Avoid a hardware erratum with the return address stack. */
  295. fill_ra_stack();
  296. signo = SIGSEGV;
  297. code = SEGV_MAPERR;
  298. if (reason & SPR_ILL_TRANS_REASON__I_STREAM_VA_RMASK)
  299. address = regs->pc;
  300. else
  301. address = 0; /* FIXME: GX: single-step for address */
  302. break;
  303. }
  304. #endif
  305. default:
  306. panic("Unexpected do_trap interrupt number %d", fault_num);
  307. return;
  308. }
  309. info.si_signo = signo;
  310. info.si_code = code;
  311. info.si_addr = (void __user *)address;
  312. if (signo == SIGILL)
  313. info.si_trapno = fault_num;
  314. if (signo != SIGTRAP)
  315. trace_unhandled_signal("trap", regs, address, signo);
  316. force_sig_info(signo, &info, current);
  317. }
  318. void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
  319. {
  320. _dump_stack(dummy, pc, lr, sp, r52);
  321. pr_emerg("Double fault: exiting\n");
  322. machine_halt();
  323. }