traps.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/fpu/api.h>
  22. #include "entry.h"
  23. static inline void __user *get_trap_ip(struct pt_regs *regs)
  24. {
  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 *) (address - (regs->int_code >> 16));
  31. }
  32. int is_valid_bugaddr(unsigned long addr)
  33. {
  34. return 1;
  35. }
  36. void do_report_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
  37. {
  38. siginfo_t info;
  39. if (user_mode(regs)) {
  40. info.si_signo = si_signo;
  41. info.si_errno = 0;
  42. info.si_code = si_code;
  43. info.si_addr = get_trap_ip(regs);
  44. force_sig_info(si_signo, &info, current);
  45. report_user_fault(regs, si_signo, 0);
  46. } else {
  47. const struct exception_table_entry *fixup;
  48. fixup = search_exception_tables(regs->psw.addr);
  49. if (fixup)
  50. regs->psw.addr = extable_fixup(fixup);
  51. else {
  52. enum bug_trap_type btt;
  53. btt = report_bug(regs->psw.addr, regs);
  54. if (btt == BUG_TRAP_TYPE_WARN)
  55. return;
  56. die(regs, str);
  57. }
  58. }
  59. }
  60. static void do_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
  61. {
  62. if (notify_die(DIE_TRAP, str, regs, 0,
  63. regs->int_code, si_signo) == NOTIFY_STOP)
  64. return;
  65. do_report_trap(regs, si_signo, si_code, str);
  66. }
  67. NOKPROBE_SYMBOL(do_trap);
  68. void do_per_trap(struct pt_regs *regs)
  69. {
  70. siginfo_t info;
  71. if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
  72. return;
  73. if (!current->ptrace)
  74. return;
  75. info.si_signo = SIGTRAP;
  76. info.si_errno = 0;
  77. info.si_code = TRAP_HWBKPT;
  78. info.si_addr =
  79. (void __force __user *) current->thread.per_event.address;
  80. force_sig_info(SIGTRAP, &info, current);
  81. }
  82. NOKPROBE_SYMBOL(do_per_trap);
  83. void default_trap_handler(struct pt_regs *regs)
  84. {
  85. if (user_mode(regs)) {
  86. report_user_fault(regs, SIGSEGV, 0);
  87. do_exit(SIGSEGV);
  88. } else
  89. die(regs, "Unknown program exception");
  90. }
  91. #define DO_ERROR_INFO(name, signr, sicode, str) \
  92. void name(struct pt_regs *regs) \
  93. { \
  94. do_trap(regs, signr, sicode, str); \
  95. }
  96. DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR,
  97. "addressing exception")
  98. DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN,
  99. "execute exception")
  100. DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV,
  101. "fixpoint divide exception")
  102. DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF,
  103. "fixpoint overflow exception")
  104. DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF,
  105. "HFP overflow exception")
  106. DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND,
  107. "HFP underflow exception")
  108. DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES,
  109. "HFP significance exception")
  110. DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV,
  111. "HFP divide exception")
  112. DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV,
  113. "HFP square root exception")
  114. DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN,
  115. "operand exception")
  116. DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC,
  117. "privileged operation")
  118. DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN,
  119. "special operation exception")
  120. DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN,
  121. "transaction constraint exception")
  122. static inline void do_fp_trap(struct pt_regs *regs, __u32 fpc)
  123. {
  124. int si_code = 0;
  125. /* FPC[2] is Data Exception Code */
  126. if ((fpc & 0x00000300) == 0) {
  127. /* bits 6 and 7 of DXC are 0 iff IEEE exception */
  128. if (fpc & 0x8000) /* invalid fp operation */
  129. si_code = FPE_FLTINV;
  130. else if (fpc & 0x4000) /* div by 0 */
  131. si_code = FPE_FLTDIV;
  132. else if (fpc & 0x2000) /* overflow */
  133. si_code = FPE_FLTOVF;
  134. else if (fpc & 0x1000) /* underflow */
  135. si_code = FPE_FLTUND;
  136. else if (fpc & 0x0800) /* inexact */
  137. si_code = FPE_FLTRES;
  138. }
  139. do_trap(regs, SIGFPE, si_code, "floating point exception");
  140. }
  141. void translation_exception(struct pt_regs *regs)
  142. {
  143. /* May never happen. */
  144. panic("Translation exception");
  145. }
  146. void illegal_op(struct pt_regs *regs)
  147. {
  148. siginfo_t info;
  149. __u8 opcode[6];
  150. __u16 __user *location;
  151. int is_uprobe_insn = 0;
  152. int signal = 0;
  153. location = get_trap_ip(regs);
  154. if (user_mode(regs)) {
  155. if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
  156. return;
  157. if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
  158. if (current->ptrace) {
  159. info.si_signo = SIGTRAP;
  160. info.si_errno = 0;
  161. info.si_code = TRAP_BRKPT;
  162. info.si_addr = location;
  163. force_sig_info(SIGTRAP, &info, current);
  164. } else
  165. signal = SIGILL;
  166. #ifdef CONFIG_UPROBES
  167. } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) {
  168. is_uprobe_insn = 1;
  169. #endif
  170. } else
  171. signal = SIGILL;
  172. }
  173. /*
  174. * We got either an illegal op in kernel mode, or user space trapped
  175. * on a uprobes illegal instruction. See if kprobes or uprobes picks
  176. * it up. If not, SIGILL.
  177. */
  178. if (is_uprobe_insn || !user_mode(regs)) {
  179. if (notify_die(DIE_BPT, "bpt", regs, 0,
  180. 3, SIGTRAP) != NOTIFY_STOP)
  181. signal = SIGILL;
  182. }
  183. if (signal)
  184. do_trap(regs, signal, ILL_ILLOPC, "illegal operation");
  185. }
  186. NOKPROBE_SYMBOL(illegal_op);
  187. DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
  188. "specification exception");
  189. void vector_exception(struct pt_regs *regs)
  190. {
  191. int si_code, vic;
  192. if (!MACHINE_HAS_VX) {
  193. do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation");
  194. return;
  195. }
  196. /* get vector interrupt code from fpc */
  197. save_fpu_regs();
  198. vic = (current->thread.fpu.fpc & 0xf00) >> 8;
  199. switch (vic) {
  200. case 1: /* invalid vector operation */
  201. si_code = FPE_FLTINV;
  202. break;
  203. case 2: /* division by zero */
  204. si_code = FPE_FLTDIV;
  205. break;
  206. case 3: /* overflow */
  207. si_code = FPE_FLTOVF;
  208. break;
  209. case 4: /* underflow */
  210. si_code = FPE_FLTUND;
  211. break;
  212. case 5: /* inexact */
  213. si_code = FPE_FLTRES;
  214. break;
  215. default: /* unknown cause */
  216. si_code = 0;
  217. }
  218. do_trap(regs, SIGFPE, si_code, "vector exception");
  219. }
  220. void data_exception(struct pt_regs *regs)
  221. {
  222. int signal = 0;
  223. save_fpu_regs();
  224. if (current->thread.fpu.fpc & FPC_DXC_MASK)
  225. signal = SIGFPE;
  226. else
  227. signal = SIGILL;
  228. if (signal == SIGFPE)
  229. do_fp_trap(regs, current->thread.fpu.fpc);
  230. else if (signal)
  231. do_trap(regs, signal, ILL_ILLOPN, "data exception");
  232. }
  233. void space_switch_exception(struct pt_regs *regs)
  234. {
  235. /* Set user psw back to home space mode. */
  236. if (user_mode(regs))
  237. regs->psw.mask |= PSW_ASC_HOME;
  238. /* Send SIGILL. */
  239. do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
  240. }
  241. void kernel_stack_overflow(struct pt_regs *regs)
  242. {
  243. bust_spinlocks(1);
  244. printk("Kernel stack overflow.\n");
  245. show_regs(regs);
  246. bust_spinlocks(0);
  247. panic("Corrupt kernel stack, can't continue.");
  248. }
  249. NOKPROBE_SYMBOL(kernel_stack_overflow);
  250. void __init trap_init(void)
  251. {
  252. local_mcck_enable();
  253. }