traps.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/kdebug.h>
  18. #include <linux/module.h>
  19. #include <linux/reboot.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/ptrace.h>
  22. #include <asm/stack.h>
  23. #include <asm/traps.h>
  24. #include <asm/setup.h>
  25. #include <arch/interrupts.h>
  26. #include <arch/spr_def.h>
  27. #include <arch/opcode.h>
  28. void __init trap_init(void)
  29. {
  30. /* Nothing needed here since we link code at .intrpt */
  31. }
  32. int unaligned_fixup = 1;
  33. static int __init setup_unaligned_fixup(char *str)
  34. {
  35. /*
  36. * Say "=-1" to completely disable it. If you just do "=0", we
  37. * will still parse the instruction, then fire a SIGBUS with
  38. * the correct address from inside the single_step code.
  39. */
  40. if (kstrtoint(str, 0, &unaligned_fixup) != 0)
  41. return 0;
  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. extern tile_bundle_bits bpt_code;
  88. asm(".pushsection .rodata.bpt_code,\"a\";"
  89. ".align 8;"
  90. "bpt_code: bpt;"
  91. ".size bpt_code,.-bpt_code;"
  92. ".popsection");
  93. static int special_ill(tile_bundle_bits bundle, int *sigp, int *codep)
  94. {
  95. int sig, code, maxcode;
  96. if (bundle == bpt_code) {
  97. *sigp = SIGTRAP;
  98. *codep = TRAP_BRKPT;
  99. return 1;
  100. }
  101. /* If it's a "raise" bundle, then "ill" must be in pipe X1. */
  102. #ifdef __tilegx__
  103. if ((bundle & TILEGX_BUNDLE_MODE_MASK) != 0)
  104. return 0;
  105. if (get_Opcode_X1(bundle) != RRR_0_OPCODE_X1)
  106. return 0;
  107. if (get_RRROpcodeExtension_X1(bundle) != UNARY_RRR_0_OPCODE_X1)
  108. return 0;
  109. if (get_UnaryOpcodeExtension_X1(bundle) != ILL_UNARY_OPCODE_X1)
  110. return 0;
  111. #else
  112. if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)
  113. return 0;
  114. if (get_Opcode_X1(bundle) != SHUN_0_OPCODE_X1)
  115. return 0;
  116. if (get_UnShOpcodeExtension_X1(bundle) != UN_0_SHUN_0_OPCODE_X1)
  117. return 0;
  118. if (get_UnOpcodeExtension_X1(bundle) != ILL_UN_0_SHUN_0_OPCODE_X1)
  119. return 0;
  120. #endif
  121. /* Check that the magic distinguishers are set to mean "raise". */
  122. if (get_Dest_X1(bundle) != 29 || get_SrcA_X1(bundle) != 37)
  123. return 0;
  124. /* There must be an "addli zero, zero, VAL" in X0. */
  125. if (get_Opcode_X0(bundle) != ADDLI_OPCODE_X0)
  126. return 0;
  127. if (get_Dest_X0(bundle) != TREG_ZERO)
  128. return 0;
  129. if (get_SrcA_X0(bundle) != TREG_ZERO)
  130. return 0;
  131. /*
  132. * Validate the proposed signal number and si_code value.
  133. * Note that we embed these in the static instruction itself
  134. * so that we perturb the register state as little as possible
  135. * at the time of the actual fault; it's unlikely you'd ever
  136. * need to dynamically choose which kind of fault to raise
  137. * from user space.
  138. */
  139. sig = get_Imm16_X0(bundle) & 0x3f;
  140. switch (sig) {
  141. case SIGILL:
  142. maxcode = NSIGILL;
  143. break;
  144. case SIGFPE:
  145. maxcode = NSIGFPE;
  146. break;
  147. case SIGSEGV:
  148. maxcode = NSIGSEGV;
  149. break;
  150. case SIGBUS:
  151. maxcode = NSIGBUS;
  152. break;
  153. case SIGTRAP:
  154. maxcode = NSIGTRAP;
  155. break;
  156. default:
  157. return 0;
  158. }
  159. code = (get_Imm16_X0(bundle) >> 6) & 0xf;
  160. if (code <= 0 || code > maxcode)
  161. return 0;
  162. /* Make it the requested signal. */
  163. *sigp = sig;
  164. *codep = code | __SI_FAULT;
  165. return 1;
  166. }
  167. static const char *const int_name[] = {
  168. [INT_MEM_ERROR] = "Memory error",
  169. [INT_ILL] = "Illegal instruction",
  170. [INT_GPV] = "General protection violation",
  171. [INT_UDN_ACCESS] = "UDN access",
  172. [INT_IDN_ACCESS] = "IDN access",
  173. #if CHIP_HAS_SN()
  174. [INT_SN_ACCESS] = "SN access",
  175. #endif
  176. [INT_SWINT_3] = "Software interrupt 3",
  177. [INT_SWINT_2] = "Software interrupt 2",
  178. [INT_SWINT_0] = "Software interrupt 0",
  179. [INT_UNALIGN_DATA] = "Unaligned data",
  180. [INT_DOUBLE_FAULT] = "Double fault",
  181. #ifdef __tilegx__
  182. [INT_ILL_TRANS] = "Illegal virtual address",
  183. #endif
  184. };
  185. static int do_bpt(struct pt_regs *regs)
  186. {
  187. unsigned long bundle, bcode, bpt;
  188. bundle = *(unsigned long *)instruction_pointer(regs);
  189. /*
  190. * bpt shoule be { bpt; nop }, which is 0x286a44ae51485000ULL.
  191. * we encode the unused least significant bits for other purpose.
  192. */
  193. bpt = bundle & ~((1ULL << 12) - 1);
  194. if (bpt != TILE_BPT_BUNDLE)
  195. return 0;
  196. bcode = bundle & ((1ULL << 12) - 1);
  197. /*
  198. * notify the kprobe handlers, if instruction is likely to
  199. * pertain to them.
  200. */
  201. switch (bcode) {
  202. /* breakpoint_insn */
  203. case 0:
  204. notify_die(DIE_BREAK, "debug", regs, bundle,
  205. INT_ILL, SIGTRAP);
  206. break;
  207. /* compiled_bpt */
  208. case DIE_COMPILED_BPT:
  209. notify_die(DIE_COMPILED_BPT, "debug", regs, bundle,
  210. INT_ILL, SIGTRAP);
  211. break;
  212. /* breakpoint2_insn */
  213. case DIE_SSTEPBP:
  214. notify_die(DIE_SSTEPBP, "single_step", regs, bundle,
  215. INT_ILL, SIGTRAP);
  216. break;
  217. default:
  218. return 0;
  219. }
  220. return 1;
  221. }
  222. void __kprobes do_trap(struct pt_regs *regs, int fault_num,
  223. unsigned long reason)
  224. {
  225. siginfo_t info = { 0 };
  226. int signo, code;
  227. unsigned long address = 0;
  228. tile_bundle_bits instr;
  229. int is_kernel = !user_mode(regs);
  230. /* Handle breakpoints, etc. */
  231. if (is_kernel && fault_num == INT_ILL && do_bpt(regs))
  232. return;
  233. /* Re-enable interrupts, if they were previously enabled. */
  234. if (!(regs->flags & PT_FLAGS_DISABLE_IRQ))
  235. local_irq_enable();
  236. /*
  237. * If it hits in kernel mode and we can't fix it up, just exit the
  238. * current process and hope for the best.
  239. */
  240. if (is_kernel) {
  241. const char *name;
  242. char buf[100];
  243. if (fixup_exception(regs)) /* ILL_TRANS or UNALIGN_DATA */
  244. return;
  245. if (fault_num >= 0 &&
  246. fault_num < sizeof(int_name)/sizeof(int_name[0]) &&
  247. int_name[fault_num] != NULL)
  248. name = int_name[fault_num];
  249. else
  250. name = "Unknown interrupt";
  251. if (fault_num == INT_GPV)
  252. snprintf(buf, sizeof(buf), "; GPV_REASON %#lx", reason);
  253. #ifdef __tilegx__
  254. else if (fault_num == INT_ILL_TRANS)
  255. snprintf(buf, sizeof(buf), "; address %#lx", reason);
  256. #endif
  257. else
  258. buf[0] = '\0';
  259. pr_alert("Kernel took bad trap %d (%s) at PC %#lx%s\n",
  260. fault_num, name, regs->pc, buf);
  261. show_regs(regs);
  262. do_exit(SIGKILL); /* FIXME: implement i386 die() */
  263. return;
  264. }
  265. switch (fault_num) {
  266. case INT_MEM_ERROR:
  267. signo = SIGBUS;
  268. code = BUS_OBJERR;
  269. break;
  270. case INT_ILL:
  271. if (copy_from_user(&instr, (void __user *)regs->pc,
  272. sizeof(instr))) {
  273. pr_err("Unreadable instruction for INT_ILL:"
  274. " %#lx\n", regs->pc);
  275. do_exit(SIGKILL);
  276. return;
  277. }
  278. if (!special_ill(instr, &signo, &code)) {
  279. signo = SIGILL;
  280. code = ILL_ILLOPC;
  281. }
  282. address = regs->pc;
  283. break;
  284. case INT_GPV:
  285. #if CHIP_HAS_TILE_DMA()
  286. if (retry_gpv(reason))
  287. return;
  288. #endif
  289. /*FALLTHROUGH*/
  290. case INT_UDN_ACCESS:
  291. case INT_IDN_ACCESS:
  292. #if CHIP_HAS_SN()
  293. case INT_SN_ACCESS:
  294. #endif
  295. signo = SIGILL;
  296. code = ILL_PRVREG;
  297. address = regs->pc;
  298. break;
  299. case INT_SWINT_3:
  300. case INT_SWINT_2:
  301. case INT_SWINT_0:
  302. signo = SIGILL;
  303. code = ILL_ILLTRP;
  304. address = regs->pc;
  305. break;
  306. case INT_UNALIGN_DATA:
  307. #ifndef __tilegx__ /* Emulated support for single step debugging */
  308. if (unaligned_fixup >= 0) {
  309. struct single_step_state *state =
  310. current_thread_info()->step_state;
  311. if (!state ||
  312. (void __user *)(regs->pc) != state->buffer) {
  313. single_step_once(regs);
  314. return;
  315. }
  316. }
  317. #endif
  318. signo = SIGBUS;
  319. code = BUS_ADRALN;
  320. address = 0;
  321. break;
  322. case INT_DOUBLE_FAULT:
  323. /*
  324. * For double fault, "reason" is actually passed as
  325. * SYSTEM_SAVE_K_2, the hypervisor's double-fault info, so
  326. * we can provide the original fault number rather than
  327. * the uninteresting "INT_DOUBLE_FAULT" so the user can
  328. * learn what actually struck while PL0 ICS was set.
  329. */
  330. fault_num = reason;
  331. signo = SIGILL;
  332. code = ILL_DBLFLT;
  333. address = regs->pc;
  334. break;
  335. #ifdef __tilegx__
  336. case INT_ILL_TRANS: {
  337. /* Avoid a hardware erratum with the return address stack. */
  338. fill_ra_stack();
  339. signo = SIGSEGV;
  340. address = reason;
  341. code = SEGV_MAPERR;
  342. break;
  343. }
  344. #endif
  345. default:
  346. panic("Unexpected do_trap interrupt number %d", fault_num);
  347. return;
  348. }
  349. info.si_signo = signo;
  350. info.si_code = code;
  351. info.si_addr = (void __user *)address;
  352. if (signo == SIGILL)
  353. info.si_trapno = fault_num;
  354. if (signo != SIGTRAP)
  355. trace_unhandled_signal("trap", regs, address, signo);
  356. force_sig_info(signo, &info, current);
  357. }
  358. void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
  359. {
  360. _dump_stack(dummy, pc, lr, sp, r52);
  361. pr_emerg("Double fault: exiting\n");
  362. machine_halt();
  363. }