traps.c 10 KB

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