traps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 0x%X ", regs->int_code);
  48. print_vma_addr("in ", regs->psw.addr & PSW_ADDR_INSN);
  49. printk("\n");
  50. show_regs(regs);
  51. }
  52. int is_valid_bugaddr(unsigned long addr)
  53. {
  54. return 1;
  55. }
  56. void do_report_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
  57. {
  58. siginfo_t info;
  59. if (user_mode(regs)) {
  60. info.si_signo = si_signo;
  61. info.si_errno = 0;
  62. info.si_code = si_code;
  63. info.si_addr = get_trap_ip(regs);
  64. force_sig_info(si_signo, &info, current);
  65. report_user_fault(regs, si_signo);
  66. } else {
  67. const struct exception_table_entry *fixup;
  68. fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  69. if (fixup)
  70. regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
  71. else {
  72. enum bug_trap_type btt;
  73. btt = report_bug(regs->psw.addr & PSW_ADDR_INSN, regs);
  74. if (btt == BUG_TRAP_TYPE_WARN)
  75. return;
  76. die(regs, str);
  77. }
  78. }
  79. }
  80. static void __kprobes do_trap(struct pt_regs *regs, int si_signo, int si_code,
  81. 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. void __kprobes do_per_trap(struct pt_regs *regs)
  89. {
  90. siginfo_t info;
  91. if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
  92. return;
  93. if (!current->ptrace)
  94. return;
  95. info.si_signo = SIGTRAP;
  96. info.si_errno = 0;
  97. info.si_code = TRAP_HWBKPT;
  98. info.si_addr =
  99. (void __force __user *) current->thread.per_event.address;
  100. force_sig_info(SIGTRAP, &info, current);
  101. }
  102. void default_trap_handler(struct pt_regs *regs)
  103. {
  104. if (user_mode(regs)) {
  105. report_user_fault(regs, SIGSEGV);
  106. do_exit(SIGSEGV);
  107. } else
  108. die(regs, "Unknown program exception");
  109. }
  110. #define DO_ERROR_INFO(name, signr, sicode, str) \
  111. void name(struct pt_regs *regs) \
  112. { \
  113. do_trap(regs, signr, sicode, str); \
  114. }
  115. DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR,
  116. "addressing exception")
  117. DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN,
  118. "execute exception")
  119. DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV,
  120. "fixpoint divide exception")
  121. DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF,
  122. "fixpoint overflow exception")
  123. DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF,
  124. "HFP overflow exception")
  125. DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND,
  126. "HFP underflow exception")
  127. DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES,
  128. "HFP significance exception")
  129. DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV,
  130. "HFP divide exception")
  131. DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV,
  132. "HFP square root exception")
  133. DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN,
  134. "operand exception")
  135. DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC,
  136. "privileged operation")
  137. DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN,
  138. "special operation exception")
  139. DO_ERROR_INFO(translation_exception, SIGILL, ILL_ILLOPN,
  140. "translation 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 __kprobes illegal_op(struct pt_regs *regs)
  165. {
  166. siginfo_t info;
  167. __u8 opcode[6];
  168. __u16 __user *location;
  169. int is_uprobe_insn = 0;
  170. int signal = 0;
  171. location = get_trap_ip(regs);
  172. if (user_mode(regs)) {
  173. if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
  174. return;
  175. if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
  176. if (current->ptrace) {
  177. info.si_signo = SIGTRAP;
  178. info.si_errno = 0;
  179. info.si_code = TRAP_BRKPT;
  180. info.si_addr = location;
  181. force_sig_info(SIGTRAP, &info, current);
  182. } else
  183. signal = SIGILL;
  184. #ifdef CONFIG_UPROBES
  185. } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) {
  186. is_uprobe_insn = 1;
  187. #endif
  188. #ifdef CONFIG_MATHEMU
  189. } else if (opcode[0] == 0xb3) {
  190. if (get_user(*((__u16 *) (opcode+2)), location+1))
  191. return;
  192. signal = math_emu_b3(opcode, regs);
  193. } else if (opcode[0] == 0xed) {
  194. if (get_user(*((__u32 *) (opcode+2)),
  195. (__u32 __user *)(location+1)))
  196. return;
  197. signal = math_emu_ed(opcode, regs);
  198. } else if (*((__u16 *) opcode) == 0xb299) {
  199. if (get_user(*((__u16 *) (opcode+2)), location+1))
  200. return;
  201. signal = math_emu_srnm(opcode, regs);
  202. } else if (*((__u16 *) opcode) == 0xb29c) {
  203. if (get_user(*((__u16 *) (opcode+2)), location+1))
  204. return;
  205. signal = math_emu_stfpc(opcode, regs);
  206. } else if (*((__u16 *) opcode) == 0xb29d) {
  207. if (get_user(*((__u16 *) (opcode+2)), location+1))
  208. return;
  209. signal = math_emu_lfpc(opcode, regs);
  210. #endif
  211. } else
  212. signal = SIGILL;
  213. }
  214. /*
  215. * We got either an illegal op in kernel mode, or user space trapped
  216. * on a uprobes illegal instruction. See if kprobes or uprobes picks
  217. * it up. If not, SIGILL.
  218. */
  219. if (is_uprobe_insn || !user_mode(regs)) {
  220. if (notify_die(DIE_BPT, "bpt", regs, 0,
  221. 3, SIGTRAP) != NOTIFY_STOP)
  222. signal = SIGILL;
  223. }
  224. #ifdef CONFIG_MATHEMU
  225. if (signal == SIGFPE)
  226. do_fp_trap(regs, current->thread.fp_regs.fpc);
  227. else if (signal == SIGSEGV)
  228. do_trap(regs, signal, SEGV_MAPERR, "user address fault");
  229. else
  230. #endif
  231. if (signal)
  232. do_trap(regs, signal, ILL_ILLOPC, "illegal operation");
  233. }
  234. #ifdef CONFIG_MATHEMU
  235. void specification_exception(struct pt_regs *regs)
  236. {
  237. __u8 opcode[6];
  238. __u16 __user *location = NULL;
  239. int signal = 0;
  240. location = (__u16 __user *) get_trap_ip(regs);
  241. if (user_mode(regs)) {
  242. get_user(*((__u16 *) opcode), location);
  243. switch (opcode[0]) {
  244. case 0x28: /* LDR Rx,Ry */
  245. signal = math_emu_ldr(opcode);
  246. break;
  247. case 0x38: /* LER Rx,Ry */
  248. signal = math_emu_ler(opcode);
  249. break;
  250. case 0x60: /* STD R,D(X,B) */
  251. get_user(*((__u16 *) (opcode+2)), location+1);
  252. signal = math_emu_std(opcode, regs);
  253. break;
  254. case 0x68: /* LD R,D(X,B) */
  255. get_user(*((__u16 *) (opcode+2)), location+1);
  256. signal = math_emu_ld(opcode, regs);
  257. break;
  258. case 0x70: /* STE R,D(X,B) */
  259. get_user(*((__u16 *) (opcode+2)), location+1);
  260. signal = math_emu_ste(opcode, regs);
  261. break;
  262. case 0x78: /* LE R,D(X,B) */
  263. get_user(*((__u16 *) (opcode+2)), location+1);
  264. signal = math_emu_le(opcode, regs);
  265. break;
  266. default:
  267. signal = SIGILL;
  268. break;
  269. }
  270. } else
  271. signal = SIGILL;
  272. if (signal == SIGFPE)
  273. do_fp_trap(regs, current->thread.fp_regs.fpc);
  274. else if (signal)
  275. do_trap(regs, signal, ILL_ILLOPN, "specification exception");
  276. }
  277. #else
  278. DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
  279. "specification exception");
  280. #endif
  281. #ifdef CONFIG_64BIT
  282. int alloc_vector_registers(struct task_struct *tsk)
  283. {
  284. __vector128 *vxrs;
  285. int i;
  286. /* Allocate vector register save area. */
  287. vxrs = kzalloc(sizeof(__vector128) * __NUM_VXRS,
  288. GFP_KERNEL|__GFP_REPEAT);
  289. if (!vxrs)
  290. return -ENOMEM;
  291. preempt_disable();
  292. if (tsk == current)
  293. save_fp_regs(tsk->thread.fp_regs.fprs);
  294. /* Copy the 16 floating point registers */
  295. for (i = 0; i < 16; i++)
  296. *(freg_t *) &vxrs[i] = tsk->thread.fp_regs.fprs[i];
  297. tsk->thread.vxrs = vxrs;
  298. if (tsk == current) {
  299. __ctl_set_bit(0, 17);
  300. restore_vx_regs(vxrs);
  301. }
  302. preempt_enable();
  303. return 0;
  304. }
  305. void vector_exception(struct pt_regs *regs)
  306. {
  307. int si_code, vic;
  308. if (!MACHINE_HAS_VX) {
  309. do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation");
  310. return;
  311. }
  312. /* get vector interrupt code from fpc */
  313. asm volatile("stfpc %0" : "=m" (current->thread.fp_regs.fpc));
  314. vic = (current->thread.fp_regs.fpc & 0xf00) >> 8;
  315. switch (vic) {
  316. case 1: /* invalid vector operation */
  317. si_code = FPE_FLTINV;
  318. break;
  319. case 2: /* division by zero */
  320. si_code = FPE_FLTDIV;
  321. break;
  322. case 3: /* overflow */
  323. si_code = FPE_FLTOVF;
  324. break;
  325. case 4: /* underflow */
  326. si_code = FPE_FLTUND;
  327. break;
  328. case 5: /* inexact */
  329. si_code = FPE_FLTRES;
  330. break;
  331. default: /* unknown cause */
  332. si_code = 0;
  333. }
  334. do_trap(regs, SIGFPE, si_code, "vector exception");
  335. }
  336. static int __init disable_vector_extension(char *str)
  337. {
  338. S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
  339. return 1;
  340. }
  341. __setup("novx", disable_vector_extension);
  342. #endif
  343. void data_exception(struct pt_regs *regs)
  344. {
  345. __u16 __user *location;
  346. int signal = 0;
  347. location = get_trap_ip(regs);
  348. if (MACHINE_HAS_IEEE)
  349. asm volatile("stfpc %0" : "=m" (current->thread.fp_regs.fpc));
  350. #ifdef CONFIG_MATHEMU
  351. else if (user_mode(regs)) {
  352. __u8 opcode[6];
  353. get_user(*((__u16 *) opcode), location);
  354. switch (opcode[0]) {
  355. case 0x28: /* LDR Rx,Ry */
  356. signal = math_emu_ldr(opcode);
  357. break;
  358. case 0x38: /* LER Rx,Ry */
  359. signal = math_emu_ler(opcode);
  360. break;
  361. case 0x60: /* STD R,D(X,B) */
  362. get_user(*((__u16 *) (opcode+2)), location+1);
  363. signal = math_emu_std(opcode, regs);
  364. break;
  365. case 0x68: /* LD R,D(X,B) */
  366. get_user(*((__u16 *) (opcode+2)), location+1);
  367. signal = math_emu_ld(opcode, regs);
  368. break;
  369. case 0x70: /* STE R,D(X,B) */
  370. get_user(*((__u16 *) (opcode+2)), location+1);
  371. signal = math_emu_ste(opcode, regs);
  372. break;
  373. case 0x78: /* LE R,D(X,B) */
  374. get_user(*((__u16 *) (opcode+2)), location+1);
  375. signal = math_emu_le(opcode, regs);
  376. break;
  377. case 0xb3:
  378. get_user(*((__u16 *) (opcode+2)), location+1);
  379. signal = math_emu_b3(opcode, regs);
  380. break;
  381. case 0xed:
  382. get_user(*((__u32 *) (opcode+2)),
  383. (__u32 __user *)(location+1));
  384. signal = math_emu_ed(opcode, regs);
  385. break;
  386. case 0xb2:
  387. if (opcode[1] == 0x99) {
  388. get_user(*((__u16 *) (opcode+2)), location+1);
  389. signal = math_emu_srnm(opcode, regs);
  390. } else if (opcode[1] == 0x9c) {
  391. get_user(*((__u16 *) (opcode+2)), location+1);
  392. signal = math_emu_stfpc(opcode, regs);
  393. } else if (opcode[1] == 0x9d) {
  394. get_user(*((__u16 *) (opcode+2)), location+1);
  395. signal = math_emu_lfpc(opcode, regs);
  396. } else
  397. signal = SIGILL;
  398. break;
  399. default:
  400. signal = SIGILL;
  401. break;
  402. }
  403. }
  404. #endif
  405. #ifdef CONFIG_64BIT
  406. /* Check for vector register enablement */
  407. if (MACHINE_HAS_VX && !current->thread.vxrs &&
  408. (current->thread.fp_regs.fpc & FPC_DXC_MASK) == 0xfe00) {
  409. alloc_vector_registers(current);
  410. /* Vector data exception is suppressing, rewind psw. */
  411. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  412. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  413. return;
  414. }
  415. #endif
  416. if (current->thread.fp_regs.fpc & FPC_DXC_MASK)
  417. signal = SIGFPE;
  418. else
  419. signal = SIGILL;
  420. if (signal == SIGFPE)
  421. do_fp_trap(regs, current->thread.fp_regs.fpc);
  422. else if (signal)
  423. do_trap(regs, signal, ILL_ILLOPN, "data exception");
  424. }
  425. void space_switch_exception(struct pt_regs *regs)
  426. {
  427. /* Set user psw back to home space mode. */
  428. if (user_mode(regs))
  429. regs->psw.mask |= PSW_ASC_HOME;
  430. /* Send SIGILL. */
  431. do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
  432. }
  433. void __kprobes kernel_stack_overflow(struct pt_regs * regs)
  434. {
  435. bust_spinlocks(1);
  436. printk("Kernel stack overflow.\n");
  437. show_regs(regs);
  438. bust_spinlocks(0);
  439. panic("Corrupt kernel stack, can't continue.");
  440. }
  441. void __init trap_init(void)
  442. {
  443. local_mcck_enable();
  444. }