traps.c 8.8 KB

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