traps.c 7.1 KB

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