3 * Copyright IBM Corp. 1999, 2000
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 __kprobes do_trap(struct pt_regs *regs, int si_signo, int si_code,
93 if (notify_die(DIE_TRAP, str, regs, 0,
94 regs->int_code, si_signo) == NOTIFY_STOP)
96 do_report_trap(regs, si_signo, si_code, str);
99 void __kprobes 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);
115 void default_trap_handler(struct pt_regs *regs)
117 if (user_mode(regs)) {
118 report_user_fault(regs, SIGSEGV);
121 die(regs, "Unknown program exception");
124 #define DO_ERROR_INFO(name, signr, sicode, str) \
125 void name(struct pt_regs *regs) \
127 do_trap(regs, signr, sicode, str); \
130 DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR,
131 "addressing exception")
132 DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN,
134 DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV,
135 "fixpoint divide exception")
136 DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF,
137 "fixpoint overflow exception")
138 DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF,
139 "HFP overflow exception")
140 DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND,
141 "HFP underflow exception")
142 DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES,
143 "HFP significance exception")
144 DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV,
145 "HFP divide exception")
146 DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV,
147 "HFP square root exception")
148 DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN,
150 DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC,
151 "privileged operation")
152 DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN,
153 "special operation exception")
154 DO_ERROR_INFO(translation_exception, SIGILL, ILL_ILLOPN,
155 "translation exception")
158 DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN,
159 "transaction constraint exception")
162 static inline void do_fp_trap(struct pt_regs *regs, int fpc)
165 /* FPC[2] is Data Exception Code */
166 if ((fpc & 0x00000300) == 0) {
167 /* bits 6 and 7 of DXC are 0 iff IEEE exception */
168 if (fpc & 0x8000) /* invalid fp operation */
169 si_code = FPE_FLTINV;
170 else if (fpc & 0x4000) /* div by 0 */
171 si_code = FPE_FLTDIV;
172 else if (fpc & 0x2000) /* overflow */
173 si_code = FPE_FLTOVF;
174 else if (fpc & 0x1000) /* underflow */
175 si_code = FPE_FLTUND;
176 else if (fpc & 0x0800) /* inexact */
177 si_code = FPE_FLTRES;
179 do_trap(regs, SIGFPE, si_code, "floating point exception");
182 void __kprobes illegal_op(struct pt_regs *regs)
186 __u16 __user *location;
187 int is_uprobe_insn = 0;
190 location = get_trap_ip(regs);
192 if (user_mode(regs)) {
193 if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
195 if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
196 if (current->ptrace) {
197 info.si_signo = SIGTRAP;
199 info.si_code = TRAP_BRKPT;
200 info.si_addr = location;
201 force_sig_info(SIGTRAP, &info, current);
204 #ifdef CONFIG_UPROBES
205 } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) {
208 #ifdef CONFIG_MATHEMU
209 } else if (opcode[0] == 0xb3) {
210 if (get_user(*((__u16 *) (opcode+2)), location+1))
212 signal = math_emu_b3(opcode, regs);
213 } else if (opcode[0] == 0xed) {
214 if (get_user(*((__u32 *) (opcode+2)),
215 (__u32 __user *)(location+1)))
217 signal = math_emu_ed(opcode, regs);
218 } else if (*((__u16 *) opcode) == 0xb299) {
219 if (get_user(*((__u16 *) (opcode+2)), location+1))
221 signal = math_emu_srnm(opcode, regs);
222 } else if (*((__u16 *) opcode) == 0xb29c) {
223 if (get_user(*((__u16 *) (opcode+2)), location+1))
225 signal = math_emu_stfpc(opcode, regs);
226 } else if (*((__u16 *) opcode) == 0xb29d) {
227 if (get_user(*((__u16 *) (opcode+2)), location+1))
229 signal = math_emu_lfpc(opcode, regs);
235 * We got either an illegal op in kernel mode, or user space trapped
236 * on a uprobes illegal instruction. See if kprobes or uprobes picks
237 * it up. If not, SIGILL.
239 if (is_uprobe_insn || !user_mode(regs)) {
240 if (notify_die(DIE_BPT, "bpt", regs, 0,
241 3, SIGTRAP) != NOTIFY_STOP)
245 #ifdef CONFIG_MATHEMU
246 if (signal == SIGFPE)
247 do_fp_trap(regs, current->thread.fp_regs.fpc);
248 else if (signal == SIGSEGV)
249 do_trap(regs, signal, SEGV_MAPERR, "user address fault");
253 do_trap(regs, signal, ILL_ILLOPC, "illegal operation");
257 #ifdef CONFIG_MATHEMU
258 void specification_exception(struct pt_regs *regs)
261 __u16 __user *location = NULL;
264 location = (__u16 __user *) get_trap_ip(regs);
266 if (user_mode(regs)) {
267 get_user(*((__u16 *) opcode), location);
269 case 0x28: /* LDR Rx,Ry */
270 signal = math_emu_ldr(opcode);
272 case 0x38: /* LER Rx,Ry */
273 signal = math_emu_ler(opcode);
275 case 0x60: /* STD R,D(X,B) */
276 get_user(*((__u16 *) (opcode+2)), location+1);
277 signal = math_emu_std(opcode, regs);
279 case 0x68: /* LD R,D(X,B) */
280 get_user(*((__u16 *) (opcode+2)), location+1);
281 signal = math_emu_ld(opcode, regs);
283 case 0x70: /* STE R,D(X,B) */
284 get_user(*((__u16 *) (opcode+2)), location+1);
285 signal = math_emu_ste(opcode, regs);
287 case 0x78: /* LE R,D(X,B) */
288 get_user(*((__u16 *) (opcode+2)), location+1);
289 signal = math_emu_le(opcode, regs);
298 if (signal == SIGFPE)
299 do_fp_trap(regs, current->thread.fp_regs.fpc);
301 do_trap(regs, signal, ILL_ILLOPN, "specification exception");
304 DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
305 "specification exception");
309 int alloc_vector_registers(struct task_struct *tsk)
314 /* Allocate vector register save area. */
315 vxrs = kzalloc(sizeof(__vector128) * __NUM_VXRS,
316 GFP_KERNEL|__GFP_REPEAT);
321 save_fp_regs(tsk->thread.fp_regs.fprs);
322 /* Copy the 16 floating point registers */
323 for (i = 0; i < 16; i++)
324 *(freg_t *) &vxrs[i] = tsk->thread.fp_regs.fprs[i];
325 tsk->thread.vxrs = vxrs;
326 if (tsk == current) {
327 __ctl_set_bit(0, 17);
328 restore_vx_regs(vxrs);
334 void vector_exception(struct pt_regs *regs)
338 if (!MACHINE_HAS_VX) {
339 do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation");
343 /* get vector interrupt code from fpc */
344 asm volatile("stfpc %0" : "=m" (current->thread.fp_regs.fpc));
345 vic = (current->thread.fp_regs.fpc & 0xf00) >> 8;
347 case 1: /* invalid vector operation */
348 si_code = FPE_FLTINV;
350 case 2: /* division by zero */
351 si_code = FPE_FLTDIV;
353 case 3: /* overflow */
354 si_code = FPE_FLTOVF;
356 case 4: /* underflow */
357 si_code = FPE_FLTUND;
359 case 5: /* inexact */
360 si_code = FPE_FLTRES;
362 default: /* unknown cause */
365 do_trap(regs, SIGFPE, si_code, "vector exception");
368 static int __init disable_vector_extension(char *str)
370 S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
373 __setup("novx", disable_vector_extension);
376 void data_exception(struct pt_regs *regs)
378 __u16 __user *location;
381 location = get_trap_ip(regs);
383 if (MACHINE_HAS_IEEE)
384 asm volatile("stfpc %0" : "=m" (current->thread.fp_regs.fpc));
386 #ifdef CONFIG_MATHEMU
387 else if (user_mode(regs)) {
389 get_user(*((__u16 *) opcode), location);
391 case 0x28: /* LDR Rx,Ry */
392 signal = math_emu_ldr(opcode);
394 case 0x38: /* LER Rx,Ry */
395 signal = math_emu_ler(opcode);
397 case 0x60: /* STD R,D(X,B) */
398 get_user(*((__u16 *) (opcode+2)), location+1);
399 signal = math_emu_std(opcode, regs);
401 case 0x68: /* LD R,D(X,B) */
402 get_user(*((__u16 *) (opcode+2)), location+1);
403 signal = math_emu_ld(opcode, regs);
405 case 0x70: /* STE R,D(X,B) */
406 get_user(*((__u16 *) (opcode+2)), location+1);
407 signal = math_emu_ste(opcode, regs);
409 case 0x78: /* LE R,D(X,B) */
410 get_user(*((__u16 *) (opcode+2)), location+1);
411 signal = math_emu_le(opcode, regs);
414 get_user(*((__u16 *) (opcode+2)), location+1);
415 signal = math_emu_b3(opcode, regs);
418 get_user(*((__u32 *) (opcode+2)),
419 (__u32 __user *)(location+1));
420 signal = math_emu_ed(opcode, regs);
423 if (opcode[1] == 0x99) {
424 get_user(*((__u16 *) (opcode+2)), location+1);
425 signal = math_emu_srnm(opcode, regs);
426 } else if (opcode[1] == 0x9c) {
427 get_user(*((__u16 *) (opcode+2)), location+1);
428 signal = math_emu_stfpc(opcode, regs);
429 } else if (opcode[1] == 0x9d) {
430 get_user(*((__u16 *) (opcode+2)), location+1);
431 signal = math_emu_lfpc(opcode, regs);
442 /* Check for vector register enablement */
443 if (MACHINE_HAS_VX && !current->thread.vxrs &&
444 (current->thread.fp_regs.fpc & FPC_DXC_MASK) == 0xfe00) {
445 alloc_vector_registers(current);
446 /* Vector data exception is suppressing, rewind psw. */
447 regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
448 clear_pt_regs_flag(regs, PIF_PER_TRAP);
453 if (current->thread.fp_regs.fpc & FPC_DXC_MASK)
457 if (signal == SIGFPE)
458 do_fp_trap(regs, current->thread.fp_regs.fpc);
460 do_trap(regs, signal, ILL_ILLOPN, "data exception");
463 void space_switch_exception(struct pt_regs *regs)
465 /* Set user psw back to home space mode. */
467 regs->psw.mask |= PSW_ASC_HOME;
469 do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
472 void __kprobes kernel_stack_overflow(struct pt_regs * regs)
475 printk("Kernel stack overflow.\n");
478 panic("Corrupt kernel stack, can't continue.");
481 void __init trap_init(void)