traps.c 7.5 KB

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