traps.c 6.9 KB

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