s390/kprobes: make use of NOKPROBE_SYMBOL()
[linux-2.6/btrfs-unstable.git] / arch / s390 / kernel / traps.c
blob1e1b866b296693f6c90df5321057bec8c4f89263
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),
7 * Derived from "arch/i386/kernel/traps.c"
8 * Copyright (C) 1991, 1992 Linus Torvalds
9 */
12 * 'Traps.c' handles hardware traps and faults after we have saved some
13 * state in 'asm.s'.
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>
20 #include <linux/mm.h>
21 #include <linux/slab.h>
22 #include <asm/switch_to.h>
23 #include "entry.h"
25 int show_unhandled_signals = 1;
27 static inline void __user *get_trap_ip(struct pt_regs *regs)
29 #ifdef CONFIG_64BIT
30 unsigned long address;
32 if (regs->int_code & 0x200)
33 address = *(unsigned long *)(current->thread.trap_tdb + 24);
34 else
35 address = regs->psw.addr;
36 return (void __user *)
37 ((address - (regs->int_code >> 16)) & PSW_ADDR_INSN);
38 #else
39 return (void __user *)
40 ((regs->psw.addr - (regs->int_code >> 16)) & PSW_ADDR_INSN);
41 #endif
44 static inline void report_user_fault(struct pt_regs *regs, int signr)
46 if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
47 return;
48 if (!unhandled_signal(current, signr))
49 return;
50 if (!printk_ratelimit())
51 return;
52 printk("User process fault: interruption code 0x%X ", regs->int_code);
53 print_vma_addr("in ", regs->psw.addr & PSW_ADDR_INSN);
54 printk("\n");
55 show_regs(regs);
58 int is_valid_bugaddr(unsigned long addr)
60 return 1;
63 void do_report_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
65 siginfo_t info;
67 if (user_mode(regs)) {
68 info.si_signo = si_signo;
69 info.si_errno = 0;
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);
74 } else {
75 const struct exception_table_entry *fixup;
76 fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
77 if (fixup)
78 regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
79 else {
80 enum bug_trap_type btt;
82 btt = report_bug(regs->psw.addr & PSW_ADDR_INSN, regs);
83 if (btt == BUG_TRAP_TYPE_WARN)
84 return;
85 die(regs, str);
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)
94 return;
95 do_report_trap(regs, si_signo, si_code, str);
97 NOKPROBE_SYMBOL(do_trap);
99 void do_per_trap(struct pt_regs *regs)
101 siginfo_t info;
103 if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
104 return;
105 if (!current->ptrace)
106 return;
107 info.si_signo = SIGTRAP;
108 info.si_errno = 0;
109 info.si_code = TRAP_HWBKPT;
110 info.si_addr =
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);
120 do_exit(SIGSEGV);
121 } else
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,
134 "execute exception")
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,
150 "operand exception")
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")
158 #ifdef CONFIG_64BIT
159 DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN,
160 "transaction constraint exception")
161 #endif
163 static inline void do_fp_trap(struct pt_regs *regs, int fpc)
165 int si_code = 0;
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)
185 siginfo_t info;
186 __u8 opcode[6];
187 __u16 __user *location;
188 int is_uprobe_insn = 0;
189 int signal = 0;
191 location = get_trap_ip(regs);
193 if (user_mode(regs)) {
194 if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
195 return;
196 if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
197 if (current->ptrace) {
198 info.si_signo = SIGTRAP;
199 info.si_errno = 0;
200 info.si_code = TRAP_BRKPT;
201 info.si_addr = location;
202 force_sig_info(SIGTRAP, &info, current);
203 } else
204 signal = SIGILL;
205 #ifdef CONFIG_UPROBES
206 } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) {
207 is_uprobe_insn = 1;
208 #endif
209 #ifdef CONFIG_MATHEMU
210 } else if (opcode[0] == 0xb3) {
211 if (get_user(*((__u16 *) (opcode+2)), location+1))
212 return;
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)))
217 return;
218 signal = math_emu_ed(opcode, regs);
219 } else if (*((__u16 *) opcode) == 0xb299) {
220 if (get_user(*((__u16 *) (opcode+2)), location+1))
221 return;
222 signal = math_emu_srnm(opcode, regs);
223 } else if (*((__u16 *) opcode) == 0xb29c) {
224 if (get_user(*((__u16 *) (opcode+2)), location+1))
225 return;
226 signal = math_emu_stfpc(opcode, regs);
227 } else if (*((__u16 *) opcode) == 0xb29d) {
228 if (get_user(*((__u16 *) (opcode+2)), location+1))
229 return;
230 signal = math_emu_lfpc(opcode, regs);
231 #endif
232 } else
233 signal = SIGILL;
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)
243 signal = SIGILL;
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");
251 else
252 #endif
253 if (signal)
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)
261 __u8 opcode[6];
262 __u16 __user *location = NULL;
263 int signal = 0;
265 location = (__u16 __user *) get_trap_ip(regs);
267 if (user_mode(regs)) {
268 get_user(*((__u16 *) opcode), location);
269 switch (opcode[0]) {
270 case 0x28: /* LDR Rx,Ry */
271 signal = math_emu_ldr(opcode);
272 break;
273 case 0x38: /* LER Rx,Ry */
274 signal = math_emu_ler(opcode);
275 break;
276 case 0x60: /* STD R,D(X,B) */
277 get_user(*((__u16 *) (opcode+2)), location+1);
278 signal = math_emu_std(opcode, regs);
279 break;
280 case 0x68: /* LD R,D(X,B) */
281 get_user(*((__u16 *) (opcode+2)), location+1);
282 signal = math_emu_ld(opcode, regs);
283 break;
284 case 0x70: /* STE R,D(X,B) */
285 get_user(*((__u16 *) (opcode+2)), location+1);
286 signal = math_emu_ste(opcode, regs);
287 break;
288 case 0x78: /* LE R,D(X,B) */
289 get_user(*((__u16 *) (opcode+2)), location+1);
290 signal = math_emu_le(opcode, regs);
291 break;
292 default:
293 signal = SIGILL;
294 break;
296 } else
297 signal = SIGILL;
299 if (signal == SIGFPE)
300 do_fp_trap(regs, current->thread.fp_regs.fpc);
301 else if (signal)
302 do_trap(regs, signal, ILL_ILLOPN, "specification exception");
304 #else
305 DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
306 "specification exception");
307 #endif
309 #ifdef CONFIG_64BIT
310 int alloc_vector_registers(struct task_struct *tsk)
312 __vector128 *vxrs;
313 int i;
315 /* Allocate vector register save area. */
316 vxrs = kzalloc(sizeof(__vector128) * __NUM_VXRS,
317 GFP_KERNEL|__GFP_REPEAT);
318 if (!vxrs)
319 return -ENOMEM;
320 preempt_disable();
321 if (tsk == current)
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);
331 preempt_enable();
332 return 0;
335 void vector_exception(struct pt_regs *regs)
337 int si_code, vic;
339 if (!MACHINE_HAS_VX) {
340 do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation");
341 return;
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;
347 switch (vic) {
348 case 1: /* invalid vector operation */
349 si_code = FPE_FLTINV;
350 break;
351 case 2: /* division by zero */
352 si_code = FPE_FLTDIV;
353 break;
354 case 3: /* overflow */
355 si_code = FPE_FLTOVF;
356 break;
357 case 4: /* underflow */
358 si_code = FPE_FLTUND;
359 break;
360 case 5: /* inexact */
361 si_code = FPE_FLTRES;
362 break;
363 default: /* unknown cause */
364 si_code = 0;
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;
372 return 1;
374 __setup("novx", disable_vector_extension);
375 #endif
377 void data_exception(struct pt_regs *regs)
379 __u16 __user *location;
380 int signal = 0;
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)) {
389 __u8 opcode[6];
390 get_user(*((__u16 *) opcode), location);
391 switch (opcode[0]) {
392 case 0x28: /* LDR Rx,Ry */
393 signal = math_emu_ldr(opcode);
394 break;
395 case 0x38: /* LER Rx,Ry */
396 signal = math_emu_ler(opcode);
397 break;
398 case 0x60: /* STD R,D(X,B) */
399 get_user(*((__u16 *) (opcode+2)), location+1);
400 signal = math_emu_std(opcode, regs);
401 break;
402 case 0x68: /* LD R,D(X,B) */
403 get_user(*((__u16 *) (opcode+2)), location+1);
404 signal = math_emu_ld(opcode, regs);
405 break;
406 case 0x70: /* STE R,D(X,B) */
407 get_user(*((__u16 *) (opcode+2)), location+1);
408 signal = math_emu_ste(opcode, regs);
409 break;
410 case 0x78: /* LE R,D(X,B) */
411 get_user(*((__u16 *) (opcode+2)), location+1);
412 signal = math_emu_le(opcode, regs);
413 break;
414 case 0xb3:
415 get_user(*((__u16 *) (opcode+2)), location+1);
416 signal = math_emu_b3(opcode, regs);
417 break;
418 case 0xed:
419 get_user(*((__u32 *) (opcode+2)),
420 (__u32 __user *)(location+1));
421 signal = math_emu_ed(opcode, regs);
422 break;
423 case 0xb2:
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);
433 } else
434 signal = SIGILL;
435 break;
436 default:
437 signal = SIGILL;
438 break;
441 #endif
442 #ifdef CONFIG_64BIT
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);
450 return;
452 #endif
454 if (current->thread.fp_regs.fpc & FPC_DXC_MASK)
455 signal = SIGFPE;
456 else
457 signal = SIGILL;
458 if (signal == SIGFPE)
459 do_fp_trap(regs, current->thread.fp_regs.fpc);
460 else if (signal)
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. */
467 if (user_mode(regs))
468 regs->psw.mask |= PSW_ASC_HOME;
469 /* Send SIGILL. */
470 do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
473 void kernel_stack_overflow(struct pt_regs *regs)
475 bust_spinlocks(1);
476 printk("Kernel stack overflow.\n");
477 show_regs(regs);
478 bust_spinlocks(0);
479 panic("Corrupt kernel stack, can't continue.");
481 NOKPROBE_SYMBOL(kernel_stack_overflow);
483 void __init trap_init(void)
485 local_mcck_enable();