traps.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 "entry.h"
  21. int show_unhandled_signals = 1;
  22. static inline void __user *get_trap_ip(struct pt_regs *regs)
  23. {
  24. #ifdef CONFIG_64BIT
  25. unsigned long address;
  26. if (regs->int_code & 0x200)
  27. address = *(unsigned long *)(current->thread.trap_tdb + 24);
  28. else
  29. address = regs->psw.addr;
  30. return (void __user *)
  31. ((address - (regs->int_code >> 16)) & PSW_ADDR_INSN);
  32. #else
  33. return (void __user *)
  34. ((regs->psw.addr - (regs->int_code >> 16)) & PSW_ADDR_INSN);
  35. #endif
  36. }
  37. static inline void report_user_fault(struct pt_regs *regs, int signr)
  38. {
  39. if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
  40. return;
  41. if (!unhandled_signal(current, signr))
  42. return;
  43. if (!printk_ratelimit())
  44. return;
  45. printk("User process fault: interruption code 0x%X ", regs->int_code);
  46. print_vma_addr("in ", regs->psw.addr & PSW_ADDR_INSN);
  47. printk("\n");
  48. show_regs(regs);
  49. }
  50. int is_valid_bugaddr(unsigned long addr)
  51. {
  52. return 1;
  53. }
  54. static void __kprobes do_trap(struct pt_regs *regs,
  55. int si_signo, int si_code, char *str)
  56. {
  57. siginfo_t info;
  58. if (notify_die(DIE_TRAP, str, regs, 0,
  59. regs->int_code, si_signo) == NOTIFY_STOP)
  60. return;
  61. if (user_mode(regs)) {
  62. info.si_signo = si_signo;
  63. info.si_errno = 0;
  64. info.si_code = si_code;
  65. info.si_addr = get_trap_ip(regs);
  66. force_sig_info(si_signo, &info, current);
  67. report_user_fault(regs, si_signo);
  68. } else {
  69. const struct exception_table_entry *fixup;
  70. fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  71. if (fixup)
  72. regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
  73. else {
  74. enum bug_trap_type btt;
  75. btt = report_bug(regs->psw.addr & PSW_ADDR_INSN, regs);
  76. if (btt == BUG_TRAP_TYPE_WARN)
  77. return;
  78. die(regs, str);
  79. }
  80. }
  81. }
  82. void __kprobes do_per_trap(struct pt_regs *regs)
  83. {
  84. siginfo_t info;
  85. if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
  86. return;
  87. if (!current->ptrace)
  88. return;
  89. info.si_signo = SIGTRAP;
  90. info.si_errno = 0;
  91. info.si_code = TRAP_HWBKPT;
  92. info.si_addr =
  93. (void __force __user *) current->thread.per_event.address;
  94. force_sig_info(SIGTRAP, &info, current);
  95. }
  96. void default_trap_handler(struct pt_regs *regs)
  97. {
  98. if (user_mode(regs)) {
  99. report_user_fault(regs, SIGSEGV);
  100. do_exit(SIGSEGV);
  101. } else
  102. die(regs, "Unknown program exception");
  103. }
  104. #define DO_ERROR_INFO(name, signr, sicode, str) \
  105. void name(struct pt_regs *regs) \
  106. { \
  107. do_trap(regs, signr, sicode, str); \
  108. }
  109. DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR,
  110. "addressing exception")
  111. DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN,
  112. "execute exception")
  113. DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV,
  114. "fixpoint divide exception")
  115. DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF,
  116. "fixpoint overflow exception")
  117. DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF,
  118. "HFP overflow exception")
  119. DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND,
  120. "HFP underflow exception")
  121. DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES,
  122. "HFP significance exception")
  123. DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV,
  124. "HFP divide exception")
  125. DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV,
  126. "HFP square root exception")
  127. DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN,
  128. "operand exception")
  129. DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC,
  130. "privileged operation")
  131. DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN,
  132. "special operation exception")
  133. DO_ERROR_INFO(translation_exception, SIGILL, ILL_ILLOPN,
  134. "translation exception")
  135. #ifdef CONFIG_64BIT
  136. DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN,
  137. "transaction constraint exception")
  138. #endif
  139. static inline void do_fp_trap(struct pt_regs *regs, int fpc)
  140. {
  141. int si_code = 0;
  142. /* FPC[2] is Data Exception Code */
  143. if ((fpc & 0x00000300) == 0) {
  144. /* bits 6 and 7 of DXC are 0 iff IEEE exception */
  145. if (fpc & 0x8000) /* invalid fp operation */
  146. si_code = FPE_FLTINV;
  147. else if (fpc & 0x4000) /* div by 0 */
  148. si_code = FPE_FLTDIV;
  149. else if (fpc & 0x2000) /* overflow */
  150. si_code = FPE_FLTOVF;
  151. else if (fpc & 0x1000) /* underflow */
  152. si_code = FPE_FLTUND;
  153. else if (fpc & 0x0800) /* inexact */
  154. si_code = FPE_FLTRES;
  155. }
  156. do_trap(regs, SIGFPE, si_code, "floating point exception");
  157. }
  158. void __kprobes illegal_op(struct pt_regs *regs)
  159. {
  160. siginfo_t info;
  161. __u8 opcode[6];
  162. __u16 __user *location;
  163. int signal = 0;
  164. location = get_trap_ip(regs);
  165. if (user_mode(regs)) {
  166. if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
  167. return;
  168. if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
  169. if (current->ptrace) {
  170. info.si_signo = SIGTRAP;
  171. info.si_errno = 0;
  172. info.si_code = TRAP_BRKPT;
  173. info.si_addr = location;
  174. force_sig_info(SIGTRAP, &info, current);
  175. } else
  176. signal = SIGILL;
  177. #ifdef CONFIG_MATHEMU
  178. } else if (opcode[0] == 0xb3) {
  179. if (get_user(*((__u16 *) (opcode+2)), location+1))
  180. return;
  181. signal = math_emu_b3(opcode, regs);
  182. } else if (opcode[0] == 0xed) {
  183. if (get_user(*((__u32 *) (opcode+2)),
  184. (__u32 __user *)(location+1)))
  185. return;
  186. signal = math_emu_ed(opcode, regs);
  187. } else if (*((__u16 *) opcode) == 0xb299) {
  188. if (get_user(*((__u16 *) (opcode+2)), location+1))
  189. return;
  190. signal = math_emu_srnm(opcode, regs);
  191. } else if (*((__u16 *) opcode) == 0xb29c) {
  192. if (get_user(*((__u16 *) (opcode+2)), location+1))
  193. return;
  194. signal = math_emu_stfpc(opcode, regs);
  195. } else if (*((__u16 *) opcode) == 0xb29d) {
  196. if (get_user(*((__u16 *) (opcode+2)), location+1))
  197. return;
  198. signal = math_emu_lfpc(opcode, regs);
  199. #endif
  200. } else
  201. signal = SIGILL;
  202. } else {
  203. /*
  204. * If we get an illegal op in kernel mode, send it through the
  205. * kprobes notifier. If kprobes doesn't pick it up, SIGILL
  206. */
  207. if (notify_die(DIE_BPT, "bpt", regs, 0,
  208. 3, SIGTRAP) != NOTIFY_STOP)
  209. signal = SIGILL;
  210. }
  211. #ifdef CONFIG_MATHEMU
  212. if (signal == SIGFPE)
  213. do_fp_trap(regs, current->thread.fp_regs.fpc);
  214. else if (signal == SIGSEGV)
  215. do_trap(regs, signal, SEGV_MAPERR, "user address fault");
  216. else
  217. #endif
  218. if (signal)
  219. do_trap(regs, signal, ILL_ILLOPC, "illegal operation");
  220. }
  221. #ifdef CONFIG_MATHEMU
  222. void specification_exception(struct pt_regs *regs)
  223. {
  224. __u8 opcode[6];
  225. __u16 __user *location = NULL;
  226. int signal = 0;
  227. location = (__u16 __user *) get_trap_ip(regs);
  228. if (user_mode(regs)) {
  229. get_user(*((__u16 *) opcode), location);
  230. switch (opcode[0]) {
  231. case 0x28: /* LDR Rx,Ry */
  232. signal = math_emu_ldr(opcode);
  233. break;
  234. case 0x38: /* LER Rx,Ry */
  235. signal = math_emu_ler(opcode);
  236. break;
  237. case 0x60: /* STD R,D(X,B) */
  238. get_user(*((__u16 *) (opcode+2)), location+1);
  239. signal = math_emu_std(opcode, regs);
  240. break;
  241. case 0x68: /* LD R,D(X,B) */
  242. get_user(*((__u16 *) (opcode+2)), location+1);
  243. signal = math_emu_ld(opcode, regs);
  244. break;
  245. case 0x70: /* STE R,D(X,B) */
  246. get_user(*((__u16 *) (opcode+2)), location+1);
  247. signal = math_emu_ste(opcode, regs);
  248. break;
  249. case 0x78: /* LE R,D(X,B) */
  250. get_user(*((__u16 *) (opcode+2)), location+1);
  251. signal = math_emu_le(opcode, regs);
  252. break;
  253. default:
  254. signal = SIGILL;
  255. break;
  256. }
  257. } else
  258. signal = SIGILL;
  259. if (signal == SIGFPE)
  260. do_fp_trap(regs, current->thread.fp_regs.fpc);
  261. else if (signal)
  262. do_trap(regs, signal, ILL_ILLOPN, "specification exception");
  263. }
  264. #else
  265. DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
  266. "specification exception");
  267. #endif
  268. void data_exception(struct pt_regs *regs)
  269. {
  270. __u16 __user *location;
  271. int signal = 0;
  272. location = get_trap_ip(regs);
  273. if (MACHINE_HAS_IEEE)
  274. asm volatile("stfpc %0" : "=m" (current->thread.fp_regs.fpc));
  275. #ifdef CONFIG_MATHEMU
  276. else if (user_mode(regs)) {
  277. __u8 opcode[6];
  278. get_user(*((__u16 *) opcode), location);
  279. switch (opcode[0]) {
  280. case 0x28: /* LDR Rx,Ry */
  281. signal = math_emu_ldr(opcode);
  282. break;
  283. case 0x38: /* LER Rx,Ry */
  284. signal = math_emu_ler(opcode);
  285. break;
  286. case 0x60: /* STD R,D(X,B) */
  287. get_user(*((__u16 *) (opcode+2)), location+1);
  288. signal = math_emu_std(opcode, regs);
  289. break;
  290. case 0x68: /* LD R,D(X,B) */
  291. get_user(*((__u16 *) (opcode+2)), location+1);
  292. signal = math_emu_ld(opcode, regs);
  293. break;
  294. case 0x70: /* STE R,D(X,B) */
  295. get_user(*((__u16 *) (opcode+2)), location+1);
  296. signal = math_emu_ste(opcode, regs);
  297. break;
  298. case 0x78: /* LE R,D(X,B) */
  299. get_user(*((__u16 *) (opcode+2)), location+1);
  300. signal = math_emu_le(opcode, regs);
  301. break;
  302. case 0xb3:
  303. get_user(*((__u16 *) (opcode+2)), location+1);
  304. signal = math_emu_b3(opcode, regs);
  305. break;
  306. case 0xed:
  307. get_user(*((__u32 *) (opcode+2)),
  308. (__u32 __user *)(location+1));
  309. signal = math_emu_ed(opcode, regs);
  310. break;
  311. case 0xb2:
  312. if (opcode[1] == 0x99) {
  313. get_user(*((__u16 *) (opcode+2)), location+1);
  314. signal = math_emu_srnm(opcode, regs);
  315. } else if (opcode[1] == 0x9c) {
  316. get_user(*((__u16 *) (opcode+2)), location+1);
  317. signal = math_emu_stfpc(opcode, regs);
  318. } else if (opcode[1] == 0x9d) {
  319. get_user(*((__u16 *) (opcode+2)), location+1);
  320. signal = math_emu_lfpc(opcode, regs);
  321. } else
  322. signal = SIGILL;
  323. break;
  324. default:
  325. signal = SIGILL;
  326. break;
  327. }
  328. }
  329. #endif
  330. if (current->thread.fp_regs.fpc & FPC_DXC_MASK)
  331. signal = SIGFPE;
  332. else
  333. signal = SIGILL;
  334. if (signal == SIGFPE)
  335. do_fp_trap(regs, current->thread.fp_regs.fpc);
  336. else if (signal)
  337. do_trap(regs, signal, ILL_ILLOPN, "data exception");
  338. }
  339. void space_switch_exception(struct pt_regs *regs)
  340. {
  341. /* Set user psw back to home space mode. */
  342. if (user_mode(regs))
  343. regs->psw.mask |= PSW_ASC_HOME;
  344. /* Send SIGILL. */
  345. do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
  346. }
  347. void __kprobes kernel_stack_overflow(struct pt_regs * regs)
  348. {
  349. bust_spinlocks(1);
  350. printk("Kernel stack overflow.\n");
  351. show_regs(regs);
  352. bust_spinlocks(0);
  353. panic("Corrupt kernel stack, can't continue.");
  354. }
  355. void __init trap_init(void)
  356. {
  357. local_mcck_enable();
  358. }