1 // SPDX-License-Identifier: GPL-2.0
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),
8 * Derived from "arch/i386/kernel/traps.c"
9 * Copyright (C) 1991, 1992 Linus Torvalds
13 * 'Traps.c' handles hardware traps and faults after we have saved some
16 #include <linux/kprobes.h>
17 #include <linux/kdebug.h>
18 #include <linux/extable.h>
19 #include <linux/ptrace.h>
20 #include <linux/sched.h>
21 #include <linux/sched/debug.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
25 #include <linux/cpu.h>
26 #include <asm/fpu/api.h>
29 static inline void __user
*get_trap_ip(struct pt_regs
*regs
)
31 unsigned long address
;
33 if (regs
->int_code
& 0x200)
34 address
= *(unsigned long *)(current
->thread
.trap_tdb
+ 24);
36 address
= regs
->psw
.addr
;
37 return (void __user
*) (address
- (regs
->int_code
>> 16));
40 int is_valid_bugaddr(unsigned long addr
)
45 void do_report_trap(struct pt_regs
*regs
, int si_signo
, int si_code
, char *str
)
47 if (user_mode(regs
)) {
48 force_sig_fault(si_signo
, si_code
, get_trap_ip(regs
));
49 report_user_fault(regs
, si_signo
, 0);
51 const struct exception_table_entry
*fixup
;
52 fixup
= s390_search_extables(regs
->psw
.addr
);
54 regs
->psw
.addr
= extable_fixup(fixup
);
61 static void do_trap(struct pt_regs
*regs
, int si_signo
, int si_code
, char *str
)
63 if (notify_die(DIE_TRAP
, str
, regs
, 0,
64 regs
->int_code
, si_signo
) == NOTIFY_STOP
)
66 do_report_trap(regs
, si_signo
, si_code
, str
);
68 NOKPROBE_SYMBOL(do_trap
);
70 void do_per_trap(struct pt_regs
*regs
)
72 if (notify_die(DIE_SSTEP
, "sstep", regs
, 0, 0, SIGTRAP
) == NOTIFY_STOP
)
76 force_sig_fault(SIGTRAP
, TRAP_HWBKPT
,
77 (void __force __user
*) current
->thread
.per_event
.address
);
79 NOKPROBE_SYMBOL(do_per_trap
);
81 void default_trap_handler(struct pt_regs
*regs
)
83 if (user_mode(regs
)) {
84 report_user_fault(regs
, SIGSEGV
, 0);
87 die(regs
, "Unknown program exception");
90 #define DO_ERROR_INFO(name, signr, sicode, str) \
91 void name(struct pt_regs *regs) \
93 do_trap(regs, signr, sicode, str); \
96 DO_ERROR_INFO(addressing_exception
, SIGILL
, ILL_ILLADR
,
97 "addressing exception")
98 DO_ERROR_INFO(execute_exception
, SIGILL
, ILL_ILLOPN
,
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
,
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")
123 static inline void do_fp_trap(struct pt_regs
*regs
, __u32 fpc
)
126 /* FPC[2] is Data Exception Code */
127 if ((fpc
& 0x00000300) == 0) {
128 /* bits 6 and 7 of DXC are 0 iff IEEE exception */
129 if (fpc
& 0x8000) /* invalid fp operation */
130 si_code
= FPE_FLTINV
;
131 else if (fpc
& 0x4000) /* div by 0 */
132 si_code
= FPE_FLTDIV
;
133 else if (fpc
& 0x2000) /* overflow */
134 si_code
= FPE_FLTOVF
;
135 else if (fpc
& 0x1000) /* underflow */
136 si_code
= FPE_FLTUND
;
137 else if (fpc
& 0x0800) /* inexact */
138 si_code
= FPE_FLTRES
;
140 do_trap(regs
, SIGFPE
, si_code
, "floating point exception");
143 void translation_exception(struct pt_regs
*regs
)
145 /* May never happen. */
146 panic("Translation exception");
149 void illegal_op(struct pt_regs
*regs
)
152 __u16 __user
*location
;
153 int is_uprobe_insn
= 0;
156 location
= get_trap_ip(regs
);
158 if (user_mode(regs
)) {
159 if (get_user(*((__u16
*) opcode
), (__u16 __user
*) location
))
161 if (*((__u16
*) opcode
) == S390_BREAKPOINT_U16
) {
163 force_sig_fault(SIGTRAP
, TRAP_BRKPT
, location
);
166 #ifdef CONFIG_UPROBES
167 } else if (*((__u16
*) opcode
) == UPROBE_SWBP_INSN
) {
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.
178 if (is_uprobe_insn
|| !user_mode(regs
)) {
179 if (notify_die(DIE_BPT
, "bpt", regs
, 0,
180 3, SIGTRAP
) != NOTIFY_STOP
)
184 do_trap(regs
, signal
, ILL_ILLOPC
, "illegal operation");
186 NOKPROBE_SYMBOL(illegal_op
);
188 DO_ERROR_INFO(specification_exception
, SIGILL
, ILL_ILLOPN
,
189 "specification exception");
191 void vector_exception(struct pt_regs
*regs
)
195 if (!MACHINE_HAS_VX
) {
196 do_trap(regs
, SIGILL
, ILL_ILLOPN
, "illegal operation");
200 /* get vector interrupt code from fpc */
202 vic
= (current
->thread
.fpu
.fpc
& 0xf00) >> 8;
204 case 1: /* invalid vector operation */
205 si_code
= FPE_FLTINV
;
207 case 2: /* division by zero */
208 si_code
= FPE_FLTDIV
;
210 case 3: /* overflow */
211 si_code
= FPE_FLTOVF
;
213 case 4: /* underflow */
214 si_code
= FPE_FLTUND
;
216 case 5: /* inexact */
217 si_code
= FPE_FLTRES
;
219 default: /* unknown cause */
222 do_trap(regs
, SIGFPE
, si_code
, "vector exception");
225 void data_exception(struct pt_regs
*regs
)
228 if (current
->thread
.fpu
.fpc
& FPC_DXC_MASK
)
229 do_fp_trap(regs
, current
->thread
.fpu
.fpc
);
231 do_trap(regs
, SIGILL
, ILL_ILLOPN
, "data exception");
234 void space_switch_exception(struct pt_regs
*regs
)
236 /* Set user psw back to home space mode. */
238 regs
->psw
.mask
|= PSW_ASC_HOME
;
240 do_trap(regs
, SIGILL
, ILL_PRVOPC
, "space switch event");
243 void monitor_event_exception(struct pt_regs
*regs
)
245 const struct exception_table_entry
*fixup
;
250 switch (report_bug(regs
->psw
.addr
- (regs
->int_code
>> 16), regs
)) {
251 case BUG_TRAP_TYPE_NONE
:
252 fixup
= s390_search_extables(regs
->psw
.addr
);
254 regs
->psw
.addr
= extable_fixup(fixup
);
256 case BUG_TRAP_TYPE_WARN
:
258 case BUG_TRAP_TYPE_BUG
:
259 die(regs
, "monitor event");
264 void kernel_stack_overflow(struct pt_regs
*regs
)
267 printk("Kernel stack overflow.\n");
270 panic("Corrupt kernel stack, can't continue.");
272 NOKPROBE_SYMBOL(kernel_stack_overflow
);
274 static void __init
test_monitor_call(void)
285 panic("Monitor call doesn't work!\n");
288 void __init
trap_init(void)
290 sort_extable(__start_dma_ex_table
, __stop_dma_ex_table
);