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),
7 * Derived from "arch/i386/kernel/traps.c"
8 * Copyright (C) 1991, 1992 Linus Torvalds
12 * 'Traps.c' handles hardware traps and faults after we have saved some
15 #include <linux/kprobes.h>
16 #include <linux/kdebug.h>
17 #include <linux/module.h>
18 #include <linux/ptrace.h>
19 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <asm/switch_to.h>
25 int show_unhandled_signals
= 1;
27 static inline void __user
*get_trap_ip(struct pt_regs
*regs
)
30 unsigned long address
;
32 if (regs
->int_code
& 0x200)
33 address
= *(unsigned long *)(current
->thread
.trap_tdb
+ 24);
35 address
= regs
->psw
.addr
;
36 return (void __user
*)
37 ((address
- (regs
->int_code
>> 16)) & PSW_ADDR_INSN
);
39 return (void __user
*)
40 ((regs
->psw
.addr
- (regs
->int_code
>> 16)) & PSW_ADDR_INSN
);
44 static inline void report_user_fault(struct pt_regs
*regs
, int signr
)
46 if ((task_pid_nr(current
) > 1) && !show_unhandled_signals
)
48 if (!unhandled_signal(current
, signr
))
50 if (!printk_ratelimit())
52 printk("User process fault: interruption code 0x%X ", regs
->int_code
);
53 print_vma_addr("in ", regs
->psw
.addr
& PSW_ADDR_INSN
);
58 int is_valid_bugaddr(unsigned long addr
)
63 void do_report_trap(struct pt_regs
*regs
, int si_signo
, int si_code
, char *str
)
67 if (user_mode(regs
)) {
68 info
.si_signo
= si_signo
;
70 info
.si_code
= si_code
;
71 info
.si_addr
= get_trap_ip(regs
);
72 force_sig_info(si_signo
, &info
, current
);
73 report_user_fault(regs
, si_signo
);
75 const struct exception_table_entry
*fixup
;
76 fixup
= search_exception_tables(regs
->psw
.addr
& PSW_ADDR_INSN
);
78 regs
->psw
.addr
= extable_fixup(fixup
) | PSW_ADDR_AMODE
;
80 enum bug_trap_type btt
;
82 btt
= report_bug(regs
->psw
.addr
& PSW_ADDR_INSN
, regs
);
83 if (btt
== BUG_TRAP_TYPE_WARN
)
90 static void do_trap(struct pt_regs
*regs
, int si_signo
, int si_code
, char *str
)
92 if (notify_die(DIE_TRAP
, str
, regs
, 0,
93 regs
->int_code
, si_signo
) == NOTIFY_STOP
)
95 do_report_trap(regs
, si_signo
, si_code
, str
);
97 NOKPROBE_SYMBOL(do_trap
);
99 void do_per_trap(struct pt_regs
*regs
)
103 if (notify_die(DIE_SSTEP
, "sstep", regs
, 0, 0, SIGTRAP
) == NOTIFY_STOP
)
105 if (!current
->ptrace
)
107 info
.si_signo
= SIGTRAP
;
109 info
.si_code
= TRAP_HWBKPT
;
111 (void __force __user
*) current
->thread
.per_event
.address
;
112 force_sig_info(SIGTRAP
, &info
, current
);
114 NOKPROBE_SYMBOL(do_per_trap
);
116 void default_trap_handler(struct pt_regs
*regs
)
118 if (user_mode(regs
)) {
119 report_user_fault(regs
, SIGSEGV
);
122 die(regs
, "Unknown program exception");
125 #define DO_ERROR_INFO(name, signr, sicode, str) \
126 void name(struct pt_regs *regs) \
128 do_trap(regs, signr, sicode, str); \
131 DO_ERROR_INFO(addressing_exception
, SIGILL
, ILL_ILLADR
,
132 "addressing exception")
133 DO_ERROR_INFO(execute_exception
, SIGILL
, ILL_ILLOPN
,
135 DO_ERROR_INFO(divide_exception
, SIGFPE
, FPE_INTDIV
,
136 "fixpoint divide exception")
137 DO_ERROR_INFO(overflow_exception
, SIGFPE
, FPE_INTOVF
,
138 "fixpoint overflow exception")
139 DO_ERROR_INFO(hfp_overflow_exception
, SIGFPE
, FPE_FLTOVF
,
140 "HFP overflow exception")
141 DO_ERROR_INFO(hfp_underflow_exception
, SIGFPE
, FPE_FLTUND
,
142 "HFP underflow exception")
143 DO_ERROR_INFO(hfp_significance_exception
, SIGFPE
, FPE_FLTRES
,
144 "HFP significance exception")
145 DO_ERROR_INFO(hfp_divide_exception
, SIGFPE
, FPE_FLTDIV
,
146 "HFP divide exception")
147 DO_ERROR_INFO(hfp_sqrt_exception
, SIGFPE
, FPE_FLTINV
,
148 "HFP square root exception")
149 DO_ERROR_INFO(operand_exception
, SIGILL
, ILL_ILLOPN
,
151 DO_ERROR_INFO(privileged_op
, SIGILL
, ILL_PRVOPC
,
152 "privileged operation")
153 DO_ERROR_INFO(special_op_exception
, SIGILL
, ILL_ILLOPN
,
154 "special operation exception")
155 DO_ERROR_INFO(translation_exception
, SIGILL
, ILL_ILLOPN
,
156 "translation exception")
159 DO_ERROR_INFO(transaction_exception
, SIGILL
, ILL_ILLOPN
,
160 "transaction constraint exception")
163 static inline void do_fp_trap(struct pt_regs
*regs
, int fpc
)
166 /* FPC[2] is Data Exception Code */
167 if ((fpc
& 0x00000300) == 0) {
168 /* bits 6 and 7 of DXC are 0 iff IEEE exception */
169 if (fpc
& 0x8000) /* invalid fp operation */
170 si_code
= FPE_FLTINV
;
171 else if (fpc
& 0x4000) /* div by 0 */
172 si_code
= FPE_FLTDIV
;
173 else if (fpc
& 0x2000) /* overflow */
174 si_code
= FPE_FLTOVF
;
175 else if (fpc
& 0x1000) /* underflow */
176 si_code
= FPE_FLTUND
;
177 else if (fpc
& 0x0800) /* inexact */
178 si_code
= FPE_FLTRES
;
180 do_trap(regs
, SIGFPE
, si_code
, "floating point exception");
183 void illegal_op(struct pt_regs
*regs
)
187 __u16 __user
*location
;
188 int is_uprobe_insn
= 0;
191 location
= get_trap_ip(regs
);
193 if (user_mode(regs
)) {
194 if (get_user(*((__u16
*) opcode
), (__u16 __user
*) location
))
196 if (*((__u16
*) opcode
) == S390_BREAKPOINT_U16
) {
197 if (current
->ptrace
) {
198 info
.si_signo
= SIGTRAP
;
200 info
.si_code
= TRAP_BRKPT
;
201 info
.si_addr
= location
;
202 force_sig_info(SIGTRAP
, &info
, current
);
205 #ifdef CONFIG_UPROBES
206 } else if (*((__u16
*) opcode
) == UPROBE_SWBP_INSN
) {
209 #ifdef CONFIG_MATHEMU
210 } else if (opcode
[0] == 0xb3) {
211 if (get_user(*((__u16
*) (opcode
+2)), location
+1))
213 signal
= math_emu_b3(opcode
, regs
);
214 } else if (opcode
[0] == 0xed) {
215 if (get_user(*((__u32
*) (opcode
+2)),
216 (__u32 __user
*)(location
+1)))
218 signal
= math_emu_ed(opcode
, regs
);
219 } else if (*((__u16
*) opcode
) == 0xb299) {
220 if (get_user(*((__u16
*) (opcode
+2)), location
+1))
222 signal
= math_emu_srnm(opcode
, regs
);
223 } else if (*((__u16
*) opcode
) == 0xb29c) {
224 if (get_user(*((__u16
*) (opcode
+2)), location
+1))
226 signal
= math_emu_stfpc(opcode
, regs
);
227 } else if (*((__u16
*) opcode
) == 0xb29d) {
228 if (get_user(*((__u16
*) (opcode
+2)), location
+1))
230 signal
= math_emu_lfpc(opcode
, regs
);
236 * We got either an illegal op in kernel mode, or user space trapped
237 * on a uprobes illegal instruction. See if kprobes or uprobes picks
238 * it up. If not, SIGILL.
240 if (is_uprobe_insn
|| !user_mode(regs
)) {
241 if (notify_die(DIE_BPT
, "bpt", regs
, 0,
242 3, SIGTRAP
) != NOTIFY_STOP
)
246 #ifdef CONFIG_MATHEMU
247 if (signal
== SIGFPE
)
248 do_fp_trap(regs
, current
->thread
.fp_regs
.fpc
);
249 else if (signal
== SIGSEGV
)
250 do_trap(regs
, signal
, SEGV_MAPERR
, "user address fault");
254 do_trap(regs
, signal
, ILL_ILLOPC
, "illegal operation");
256 NOKPROBE_SYMBOL(illegal_op
);
258 #ifdef CONFIG_MATHEMU
259 void specification_exception(struct pt_regs
*regs
)
262 __u16 __user
*location
= NULL
;
265 location
= (__u16 __user
*) get_trap_ip(regs
);
267 if (user_mode(regs
)) {
268 get_user(*((__u16
*) opcode
), location
);
270 case 0x28: /* LDR Rx,Ry */
271 signal
= math_emu_ldr(opcode
);
273 case 0x38: /* LER Rx,Ry */
274 signal
= math_emu_ler(opcode
);
276 case 0x60: /* STD R,D(X,B) */
277 get_user(*((__u16
*) (opcode
+2)), location
+1);
278 signal
= math_emu_std(opcode
, regs
);
280 case 0x68: /* LD R,D(X,B) */
281 get_user(*((__u16
*) (opcode
+2)), location
+1);
282 signal
= math_emu_ld(opcode
, regs
);
284 case 0x70: /* STE R,D(X,B) */
285 get_user(*((__u16
*) (opcode
+2)), location
+1);
286 signal
= math_emu_ste(opcode
, regs
);
288 case 0x78: /* LE R,D(X,B) */
289 get_user(*((__u16
*) (opcode
+2)), location
+1);
290 signal
= math_emu_le(opcode
, regs
);
299 if (signal
== SIGFPE
)
300 do_fp_trap(regs
, current
->thread
.fp_regs
.fpc
);
302 do_trap(regs
, signal
, ILL_ILLOPN
, "specification exception");
305 DO_ERROR_INFO(specification_exception
, SIGILL
, ILL_ILLOPN
,
306 "specification exception");
310 int alloc_vector_registers(struct task_struct
*tsk
)
315 /* Allocate vector register save area. */
316 vxrs
= kzalloc(sizeof(__vector128
) * __NUM_VXRS
,
317 GFP_KERNEL
|__GFP_REPEAT
);
322 save_fp_regs(tsk
->thread
.fp_regs
.fprs
);
323 /* Copy the 16 floating point registers */
324 for (i
= 0; i
< 16; i
++)
325 *(freg_t
*) &vxrs
[i
] = tsk
->thread
.fp_regs
.fprs
[i
];
326 tsk
->thread
.vxrs
= vxrs
;
327 if (tsk
== current
) {
328 __ctl_set_bit(0, 17);
329 restore_vx_regs(vxrs
);
335 void vector_exception(struct pt_regs
*regs
)
339 if (!MACHINE_HAS_VX
) {
340 do_trap(regs
, SIGILL
, ILL_ILLOPN
, "illegal operation");
344 /* get vector interrupt code from fpc */
345 asm volatile("stfpc %0" : "=m" (current
->thread
.fp_regs
.fpc
));
346 vic
= (current
->thread
.fp_regs
.fpc
& 0xf00) >> 8;
348 case 1: /* invalid vector operation */
349 si_code
= FPE_FLTINV
;
351 case 2: /* division by zero */
352 si_code
= FPE_FLTDIV
;
354 case 3: /* overflow */
355 si_code
= FPE_FLTOVF
;
357 case 4: /* underflow */
358 si_code
= FPE_FLTUND
;
360 case 5: /* inexact */
361 si_code
= FPE_FLTRES
;
363 default: /* unknown cause */
366 do_trap(regs
, SIGFPE
, si_code
, "vector exception");
369 static int __init
disable_vector_extension(char *str
)
371 S390_lowcore
.machine_flags
&= ~MACHINE_FLAG_VX
;
374 __setup("novx", disable_vector_extension
);
377 void data_exception(struct pt_regs
*regs
)
379 __u16 __user
*location
;
382 location
= get_trap_ip(regs
);
384 if (MACHINE_HAS_IEEE
)
385 asm volatile("stfpc %0" : "=m" (current
->thread
.fp_regs
.fpc
));
387 #ifdef CONFIG_MATHEMU
388 else if (user_mode(regs
)) {
390 get_user(*((__u16
*) opcode
), location
);
392 case 0x28: /* LDR Rx,Ry */
393 signal
= math_emu_ldr(opcode
);
395 case 0x38: /* LER Rx,Ry */
396 signal
= math_emu_ler(opcode
);
398 case 0x60: /* STD R,D(X,B) */
399 get_user(*((__u16
*) (opcode
+2)), location
+1);
400 signal
= math_emu_std(opcode
, regs
);
402 case 0x68: /* LD R,D(X,B) */
403 get_user(*((__u16
*) (opcode
+2)), location
+1);
404 signal
= math_emu_ld(opcode
, regs
);
406 case 0x70: /* STE R,D(X,B) */
407 get_user(*((__u16
*) (opcode
+2)), location
+1);
408 signal
= math_emu_ste(opcode
, regs
);
410 case 0x78: /* LE R,D(X,B) */
411 get_user(*((__u16
*) (opcode
+2)), location
+1);
412 signal
= math_emu_le(opcode
, regs
);
415 get_user(*((__u16
*) (opcode
+2)), location
+1);
416 signal
= math_emu_b3(opcode
, regs
);
419 get_user(*((__u32
*) (opcode
+2)),
420 (__u32 __user
*)(location
+1));
421 signal
= math_emu_ed(opcode
, regs
);
424 if (opcode
[1] == 0x99) {
425 get_user(*((__u16
*) (opcode
+2)), location
+1);
426 signal
= math_emu_srnm(opcode
, regs
);
427 } else if (opcode
[1] == 0x9c) {
428 get_user(*((__u16
*) (opcode
+2)), location
+1);
429 signal
= math_emu_stfpc(opcode
, regs
);
430 } else if (opcode
[1] == 0x9d) {
431 get_user(*((__u16
*) (opcode
+2)), location
+1);
432 signal
= math_emu_lfpc(opcode
, regs
);
443 /* Check for vector register enablement */
444 if (MACHINE_HAS_VX
&& !current
->thread
.vxrs
&&
445 (current
->thread
.fp_regs
.fpc
& FPC_DXC_MASK
) == 0xfe00) {
446 alloc_vector_registers(current
);
447 /* Vector data exception is suppressing, rewind psw. */
448 regs
->psw
.addr
= __rewind_psw(regs
->psw
, regs
->int_code
>> 16);
449 clear_pt_regs_flag(regs
, PIF_PER_TRAP
);
454 if (current
->thread
.fp_regs
.fpc
& FPC_DXC_MASK
)
458 if (signal
== SIGFPE
)
459 do_fp_trap(regs
, current
->thread
.fp_regs
.fpc
);
461 do_trap(regs
, signal
, ILL_ILLOPN
, "data exception");
464 void space_switch_exception(struct pt_regs
*regs
)
466 /* Set user psw back to home space mode. */
468 regs
->psw
.mask
|= PSW_ASC_HOME
;
470 do_trap(regs
, SIGILL
, ILL_PRVOPC
, "space switch event");
473 void kernel_stack_overflow(struct pt_regs
*regs
)
476 printk("Kernel stack overflow.\n");
479 panic("Corrupt kernel stack, can't continue.");
481 NOKPROBE_SYMBOL(kernel_stack_overflow
);
483 void __init
trap_init(void)