traps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999, 2000
  4. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  5. * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
  6. *
  7. * Derived from "arch/i386/kernel/traps.c"
  8. * Copyright (C) 1991, 1992 Linus Torvalds
  9. */
  10. /*
  11. * 'Traps.c' handles hardware traps and faults after we have saved some
  12. * state in 'asm.s'.
  13. */
  14. #include <linux/kprobes.h>
  15. #include <linux/kdebug.h>
  16. #include <linux/module.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/slab.h>
  21. #include <asm/switch_to.h>
  22. #include "entry.h"
  23. int show_unhandled_signals = 1;
  24. static inline void __user *get_trap_ip(struct pt_regs *regs)
  25. {
  26. #ifdef CONFIG_64BIT
  27. unsigned long address;
  28. if (regs->int_code & 0x200)
  29. address = *(unsigned long *)(current->thread.trap_tdb + 24);
  30. else
  31. address = regs->psw.addr;
  32. return (void __user *)
  33. ((address - (regs->int_code >> 16)) & PSW_ADDR_INSN);
  34. #else
  35. return (void __user *)
  36. ((regs->psw.addr - (regs->int_code >> 16)) & PSW_ADDR_INSN);
  37. #endif
  38. }
  39. static inline void report_user_fault(struct pt_regs *regs, int signr)
  40. {
  41. if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
  42. return;
  43. if (!unhandled_signal(current, signr))
  44. return;
  45. if (!printk_ratelimit())
  46. return;
  47. printk("User process fault: interruption code %04x ilc:%d ",
  48. regs->int_code & 0xffff, regs->int_code >> 17);
  49. print_vma_addr("in ", regs->psw.addr & PSW_ADDR_INSN);
  50. printk("\n");
  51. show_regs(regs);
  52. }
  53. int is_valid_bugaddr(unsigned long addr)
  54. {
  55. return 1;
  56. }
  57. void do_report_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
  58. {
  59. siginfo_t info;
  60. if (user_mode(regs)) {
  61. info.si_signo = si_signo;
  62. info.si_errno = 0;
  63. info.si_code = si_code;
  64. info.si_addr = get_trap_ip(regs);
  65. force_sig_info(si_signo, &info, current);
  66. report_user_fault(regs, si_signo);
  67. } else {
  68. const struct exception_table_entry *fixup;
  69. fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  70. if (fixup)
  71. regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
  72. else {
  73. enum bug_trap_type btt;
  74. btt = report_bug(regs->psw.addr & PSW_ADDR_INSN, regs);
  75. if (btt == BUG_TRAP_TYPE_WARN)
  76. return;
  77. die(regs, str);
  78. }
  79. }
  80. }
  81. static void do_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
  82. {
  83. if (notify_die(DIE_TRAP, str, regs, 0,
  84. regs->int_code, si_signo) == NOTIFY_STOP)
  85. return;
  86. do_report_trap(regs, si_signo, si_code, str);
  87. }
  88. NOKPROBE_SYMBOL(do_trap);
  89. void do_per_trap(struct pt_regs *regs)
  90. {
  91. siginfo_t info;
  92. if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
  93. return;
  94. if (!current->ptrace)
  95. return;
  96. info.si_signo = SIGTRAP;
  97. info.si_errno = 0;
  98. info.si_code = TRAP_HWBKPT;
  99. info.si_addr =
  100. (void __force __user *) current->thread.per_event.address;
  101. force_sig_info(SIGTRAP, &info, current);
  102. }
  103. NOKPROBE_SYMBOL(do_per_trap);
  104. void default_trap_handler(struct pt_regs *regs)
  105. {
  106. if (user_mode(regs)) {
  107. report_user_fault(regs, SIGSEGV);
  108. do_exit(SIGSEGV);
  109. } else
  110. die(regs, "Unknown program exception");
  111. }
  112. #define DO_ERROR_INFO(name, signr, sicode, str) \
  113. void name(struct pt_regs *regs) \
  114. { \
  115. do_trap(regs, signr, sicode, str); \
  116. }
  117. DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR,
  118. "addressing exception")
  119. DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN,
  120. "execute exception")
  121. DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV,
  122. "fixpoint divide exception")
  123. DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF,
  124. "fixpoint overflow exception")
  125. DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF,
  126. "HFP overflow exception")
  127. DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND,
  128. "HFP underflow exception")
  129. DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES,
  130. "HFP significance exception")
  131. DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV,
  132. "HFP divide exception")
  133. DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV,
  134. "HFP square root exception")
  135. DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN,
  136. "operand exception")
  137. DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC,
  138. "privileged operation")
  139. DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN,
  140. "special operation exception")
  141. #ifdef CONFIG_64BIT
  142. DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN,
  143. "transaction constraint exception")
  144. #endif
  145. static inline void do_fp_trap(struct pt_regs *regs, int fpc)
  146. {
  147. int si_code = 0;
  148. /* FPC[2] is Data Exception Code */
  149. if ((fpc & 0x00000300) == 0) {
  150. /* bits 6 and 7 of DXC are 0 iff IEEE exception */
  151. if (fpc & 0x8000) /* invalid fp operation */
  152. si_code = FPE_FLTINV;
  153. else if (fpc & 0x4000) /* div by 0 */
  154. si_code = FPE_FLTDIV;
  155. else if (fpc & 0x2000) /* overflow */
  156. si_code = FPE_FLTOVF;
  157. else if (fpc & 0x1000) /* underflow */
  158. si_code = FPE_FLTUND;
  159. else if (fpc & 0x0800) /* inexact */
  160. si_code = FPE_FLTRES;
  161. }
  162. do_trap(regs, SIGFPE, si_code, "floating point exception");
  163. }
  164. void translation_exception(struct pt_regs *regs)
  165. {
  166. /* May never happen. */
  167. die(regs, "Translation exception");
  168. }
  169. void illegal_op(struct pt_regs *regs)
  170. {
  171. siginfo_t info;
  172. __u8 opcode[6];
  173. __u16 __user *location;
  174. int is_uprobe_insn = 0;
  175. int signal = 0;
  176. location = get_trap_ip(regs);
  177. if (user_mode(regs)) {
  178. if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
  179. return;
  180. if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
  181. if (current->ptrace) {
  182. info.si_signo = SIGTRAP;
  183. info.si_errno = 0;
  184. info.si_code = TRAP_BRKPT;
  185. info.si_addr = location;
  186. force_sig_info(SIGTRAP, &info, current);
  187. } else
  188. signal = SIGILL;
  189. #ifdef CONFIG_UPROBES
  190. } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) {
  191. is_uprobe_insn = 1;
  192. #endif
  193. #ifdef CONFIG_MATHEMU
  194. } else if (opcode[0] == 0xb3) {
  195. if (get_user(*((__u16 *) (opcode+2)), location+1))
  196. return;
  197. signal = math_emu_b3(opcode, regs);
  198. } else if (opcode[0] == 0xed) {
  199. if (get_user(*((__u32 *) (opcode+2)),
  200. (__u32 __user *)(location+1)))
  201. return;
  202. signal = math_emu_ed(opcode, regs);
  203. } else if (*((__u16 *) opcode) == 0xb299) {
  204. if (get_user(*((__u16 *) (opcode+2)), location+1))
  205. return;
  206. signal = math_emu_srnm(opcode, regs);
  207. } else if (*((__u16 *) opcode) == 0xb29c) {
  208. if (get_user(*((__u16 *) (opcode+2)), location+1))
  209. return;
  210. signal = math_emu_stfpc(opcode, regs);
  211. } else if (*((__u16 *) opcode) == 0xb29d) {
  212. if (get_user(*((__u16 *) (opcode+2)), location+1))
  213. return;
  214. signal = math_emu_lfpc(opcode, regs);
  215. #endif
  216. } else
  217. signal = SIGILL;
  218. }
  219. /*
  220. * We got either an illegal op in kernel mode, or user space trapped
  221. * on a uprobes illegal instruction. See if kprobes or uprobes picks
  222. * it up. If not, SIGILL.
  223. */
  224. if (is_uprobe_insn || !user_mode(regs)) {
  225. if (notify_die(DIE_BPT, "bpt", regs, 0,
  226. 3, SIGTRAP) != NOTIFY_STOP)
  227. signal = SIGILL;
  228. }
  229. #ifdef CONFIG_MATHEMU
  230. if (signal == SIGFPE)
  231. do_fp_trap(regs, current->thread.fp_regs.fpc);
  232. else if (signal == SIGSEGV)
  233. do_trap(regs, signal, SEGV_MAPERR, "user address fault");
  234. else
  235. #endif
  236. if (signal)
  237. do_trap(regs, signal, ILL_ILLOPC, "illegal operation");
  238. }
  239. NOKPROBE_SYMBOL(illegal_op);
  240. #ifdef CONFIG_MATHEMU
  241. void specification_exception(struct pt_regs *regs)
  242. {
  243. __u8 opcode[6];
  244. __u16 __user *location = NULL;
  245. int signal = 0;
  246. location = (__u16 __user *) get_trap_ip(regs);
  247. if (user_mode(regs)) {
  248. get_user(*((__u16 *) opcode), location);
  249. switch (opcode[0]) {
  250. case 0x28: /* LDR Rx,Ry */
  251. signal = math_emu_ldr(opcode);
  252. break;
  253. case 0x38: /* LER Rx,Ry */
  254. signal = math_emu_ler(opcode);
  255. break;
  256. case 0x60: /* STD R,D(X,B) */
  257. get_user(*((__u16 *) (opcode+2)), location+1);
  258. signal = math_emu_std(opcode, regs);
  259. break;
  260. case 0x68: /* LD R,D(X,B) */
  261. get_user(*((__u16 *) (opcode+2)), location+1);
  262. signal = math_emu_ld(opcode, regs);
  263. break;
  264. case 0x70: /* STE R,D(X,B) */
  265. get_user(*((__u16 *) (opcode+2)), location+1);
  266. signal = math_emu_ste(opcode, regs);
  267. break;
  268. case 0x78: /* LE R,D(X,B) */
  269. get_user(*((__u16 *) (opcode+2)), location+1);
  270. signal = math_emu_le(opcode, regs);
  271. break;
  272. default:
  273. signal = SIGILL;
  274. break;
  275. }
  276. } else
  277. signal = SIGILL;
  278. if (signal == SIGFPE)
  279. do_fp_trap(regs, current->thread.fp_regs.fpc);
  280. else if (signal)
  281. do_trap(regs, signal, ILL_ILLOPN, "specification exception");
  282. }
  283. #else
  284. DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
  285. "specification exception");
  286. #endif
  287. #ifdef CONFIG_64BIT
  288. int alloc_vector_registers(struct task_struct *tsk)
  289. {
  290. __vector128 *vxrs;
  291. int i;
  292. /* Allocate vector register save area. */
  293. vxrs = kzalloc(sizeof(__vector128) * __NUM_VXRS,
  294. GFP_KERNEL|__GFP_REPEAT);
  295. if (!vxrs)
  296. return -ENOMEM;
  297. preempt_disable();
  298. if (tsk == current)
  299. save_fp_regs(tsk->thread.fp_regs.fprs);
  300. /* Copy the 16 floating point registers */
  301. for (i = 0; i < 16; i++)
  302. *(freg_t *) &vxrs[i] = tsk->thread.fp_regs.fprs[i];
  303. tsk->thread.vxrs = vxrs;
  304. if (tsk == current) {
  305. __ctl_set_bit(0, 17);
  306. restore_vx_regs(vxrs);
  307. }
  308. preempt_enable();
  309. return 0;
  310. }
  311. void vector_exception(struct pt_regs *regs)
  312. {
  313. int si_code, vic;
  314. if (!MACHINE_HAS_VX) {
  315. do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation");
  316. return;
  317. }
  318. /* get vector interrupt code from fpc */
  319. asm volatile("stfpc %0" : "=m" (current->thread.fp_regs.fpc));
  320. vic = (current->thread.fp_regs.fpc & 0xf00) >> 8;
  321. switch (vic) {
  322. case 1: /* invalid vector operation */
  323. si_code = FPE_FLTINV;
  324. break;
  325. case 2: /* division by zero */
  326. si_code = FPE_FLTDIV;
  327. break;
  328. case 3: /* overflow */
  329. si_code = FPE_FLTOVF;
  330. break;
  331. case 4: /* underflow */
  332. si_code = FPE_FLTUND;
  333. break;
  334. case 5: /* inexact */
  335. si_code = FPE_FLTRES;
  336. break;
  337. default: /* unknown cause */
  338. si_code = 0;
  339. }
  340. do_trap(regs, SIGFPE, si_code, "vector exception");
  341. }
  342. static int __init disable_vector_extension(char *str)
  343. {
  344. S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
  345. return 1;
  346. }
  347. __setup("novx", disable_vector_extension);
  348. #endif
  349. void data_exception(struct pt_regs *regs)
  350. {
  351. __u16 __user *location;
  352. int signal = 0;
  353. location = get_trap_ip(regs);
  354. if (MACHINE_HAS_IEEE)
  355. asm volatile("stfpc %0" : "=m" (current->thread.fp_regs.fpc));
  356. #ifdef CONFIG_MATHEMU
  357. else if (user_mode(regs)) {
  358. __u8 opcode[6];
  359. get_user(*((__u16 *) opcode), location);
  360. switch (opcode[0]) {
  361. case 0x28: /* LDR Rx,Ry */
  362. signal = math_emu_ldr(opcode);
  363. break;
  364. case 0x38: /* LER Rx,Ry */
  365. signal = math_emu_ler(opcode);
  366. break;
  367. case 0x60: /* STD R,D(X,B) */
  368. get_user(*((__u16 *) (opcode+2)), location+1);
  369. signal = math_emu_std(opcode, regs);
  370. break;
  371. case 0x68: /* LD R,D(X,B) */
  372. get_user(*((__u16 *) (opcode+2)), location+1);
  373. signal = math_emu_ld(opcode, regs);
  374. break;
  375. case 0x70: /* STE R,D(X,B) */
  376. get_user(*((__u16 *) (opcode+2)), location+1);
  377. signal = math_emu_ste(opcode, regs);
  378. break;
  379. case 0x78: /* LE R,D(X,B) */
  380. get_user(*((__u16 *) (opcode+2)), location+1);
  381. signal = math_emu_le(opcode, regs);
  382. break;
  383. case 0xb3:
  384. get_user(*((__u16 *) (opcode+2)), location+1);
  385. signal = math_emu_b3(opcode, regs);
  386. break;
  387. case 0xed:
  388. get_user(*((__u32 *) (opcode+2)),
  389. (__u32 __user *)(location+1));
  390. signal = math_emu_ed(opcode, regs);
  391. break;
  392. case 0xb2:
  393. if (opcode[1] == 0x99) {
  394. get_user(*((__u16 *) (opcode+2)), location+1);
  395. signal = math_emu_srnm(opcode, regs);
  396. } else if (opcode[1] == 0x9c) {
  397. get_user(*((__u16 *) (opcode+2)), location+1);
  398. signal = math_emu_stfpc(opcode, regs);
  399. } else if (opcode[1] == 0x9d) {
  400. get_user(*((__u16 *) (opcode+2)), location+1);
  401. signal = math_emu_lfpc(opcode, regs);
  402. } else
  403. signal = SIGILL;
  404. break;
  405. default:
  406. signal = SIGILL;
  407. break;
  408. }
  409. }
  410. #endif
  411. #ifdef CONFIG_64BIT
  412. /* Check for vector register enablement */
  413. if (MACHINE_HAS_VX && !current->thread.vxrs &&
  414. (current->thread.fp_regs.fpc & FPC_DXC_MASK) == 0xfe00) {
  415. alloc_vector_registers(current);
  416. /* Vector data exception is suppressing, rewind psw. */
  417. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  418. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  419. return;
  420. }
  421. #endif
  422. if (current->thread.fp_regs.fpc & FPC_DXC_MASK)
  423. signal = SIGFPE;
  424. else
  425. signal = SIGILL;
  426. if (signal == SIGFPE)
  427. do_fp_trap(regs, current->thread.fp_regs.fpc);
  428. else if (signal)
  429. do_trap(regs, signal, ILL_ILLOPN, "data exception");
  430. }
  431. void space_switch_exception(struct pt_regs *regs)
  432. {
  433. /* Set user psw back to home space mode. */
  434. if (user_mode(regs))
  435. regs->psw.mask |= PSW_ASC_HOME;
  436. /* Send SIGILL. */
  437. do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
  438. }
  439. void kernel_stack_overflow(struct pt_regs *regs)
  440. {
  441. bust_spinlocks(1);
  442. printk("Kernel stack overflow.\n");
  443. show_regs(regs);
  444. bust_spinlocks(0);
  445. panic("Corrupt kernel stack, can't continue.");
  446. }
  447. NOKPROBE_SYMBOL(kernel_stack_overflow);
  448. void __init trap_init(void)
  449. {
  450. local_mcck_enable();
  451. }