Merge branch 'master'
[deliverable/linux.git] / arch / ia64 / kernel / traps.c
1 /*
2 * Architecture-specific trap handling.
3 *
4 * Copyright (C) 1998-2003 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
6 *
7 * 05/12/00 grao <goutham.rao@intel.com> : added isr in siginfo for SIGFPE
8 */
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/tty.h>
15 #include <linux/vt_kern.h> /* For unblank_screen() */
16 #include <linux/module.h> /* for EXPORT_SYMBOL */
17 #include <linux/hardirq.h>
18 #include <linux/kprobes.h>
19
20 #include <asm/fpswa.h>
21 #include <asm/ia32.h>
22 #include <asm/intrinsics.h>
23 #include <asm/processor.h>
24 #include <asm/uaccess.h>
25 #include <asm/kdebug.h>
26
27 extern spinlock_t timerlist_lock;
28
29 fpswa_interface_t *fpswa_interface;
30 EXPORT_SYMBOL(fpswa_interface);
31
32 struct notifier_block *ia64die_chain;
33
34 int
35 register_die_notifier(struct notifier_block *nb)
36 {
37 return notifier_chain_register(&ia64die_chain, nb);
38 }
39 EXPORT_SYMBOL_GPL(register_die_notifier);
40
41 int
42 unregister_die_notifier(struct notifier_block *nb)
43 {
44 return notifier_chain_unregister(&ia64die_chain, nb);
45 }
46 EXPORT_SYMBOL_GPL(unregister_die_notifier);
47
48 void __init
49 trap_init (void)
50 {
51 if (ia64_boot_param->fpswa)
52 /* FPSWA fixup: make the interface pointer a kernel virtual address: */
53 fpswa_interface = __va(ia64_boot_param->fpswa);
54 }
55
56 /*
57 * Unlock any spinlocks which will prevent us from getting the message out (timerlist_lock
58 * is acquired through the console unblank code)
59 */
60 void
61 bust_spinlocks (int yes)
62 {
63 int loglevel_save = console_loglevel;
64
65 if (yes) {
66 oops_in_progress = 1;
67 return;
68 }
69
70 #ifdef CONFIG_VT
71 unblank_screen();
72 #endif
73 oops_in_progress = 0;
74 /*
75 * OK, the message is on the console. Now we call printk() without
76 * oops_in_progress set so that printk will give klogd a poke. Hold onto
77 * your hats...
78 */
79 console_loglevel = 15; /* NMI oopser may have shut the console up */
80 printk(" ");
81 console_loglevel = loglevel_save;
82 }
83
84 void
85 die (const char *str, struct pt_regs *regs, long err)
86 {
87 static struct {
88 spinlock_t lock;
89 u32 lock_owner;
90 int lock_owner_depth;
91 } die = {
92 .lock = SPIN_LOCK_UNLOCKED,
93 .lock_owner = -1,
94 .lock_owner_depth = 0
95 };
96 static int die_counter;
97 int cpu = get_cpu();
98
99 if (die.lock_owner != cpu) {
100 console_verbose();
101 spin_lock_irq(&die.lock);
102 die.lock_owner = cpu;
103 die.lock_owner_depth = 0;
104 bust_spinlocks(1);
105 }
106 put_cpu();
107
108 if (++die.lock_owner_depth < 3) {
109 printk("%s[%d]: %s %ld [%d]\n",
110 current->comm, current->pid, str, err, ++die_counter);
111 (void) notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
112 show_regs(regs);
113 } else
114 printk(KERN_ERR "Recursive die() failure, output suppressed\n");
115
116 bust_spinlocks(0);
117 die.lock_owner = -1;
118 spin_unlock_irq(&die.lock);
119 do_exit(SIGSEGV);
120 }
121
122 void
123 die_if_kernel (char *str, struct pt_regs *regs, long err)
124 {
125 if (!user_mode(regs))
126 die(str, regs, err);
127 }
128
129 void
130 __kprobes ia64_bad_break (unsigned long break_num, struct pt_regs *regs)
131 {
132 siginfo_t siginfo;
133 int sig, code;
134
135 /* SIGILL, SIGFPE, SIGSEGV, and SIGBUS want these field initialized: */
136 siginfo.si_addr = (void __user *) (regs->cr_iip + ia64_psr(regs)->ri);
137 siginfo.si_imm = break_num;
138 siginfo.si_flags = 0; /* clear __ISR_VALID */
139 siginfo.si_isr = 0;
140
141 switch (break_num) {
142 case 0: /* unknown error (used by GCC for __builtin_abort()) */
143 if (notify_die(DIE_BREAK, "break 0", regs, break_num, TRAP_BRKPT, SIGTRAP)
144 == NOTIFY_STOP)
145 return;
146 die_if_kernel("bugcheck!", regs, break_num);
147 sig = SIGILL; code = ILL_ILLOPC;
148 break;
149
150 case 1: /* integer divide by zero */
151 sig = SIGFPE; code = FPE_INTDIV;
152 break;
153
154 case 2: /* integer overflow */
155 sig = SIGFPE; code = FPE_INTOVF;
156 break;
157
158 case 3: /* range check/bounds check */
159 sig = SIGFPE; code = FPE_FLTSUB;
160 break;
161
162 case 4: /* null pointer dereference */
163 sig = SIGSEGV; code = SEGV_MAPERR;
164 break;
165
166 case 5: /* misaligned data */
167 sig = SIGSEGV; code = BUS_ADRALN;
168 break;
169
170 case 6: /* decimal overflow */
171 sig = SIGFPE; code = __FPE_DECOVF;
172 break;
173
174 case 7: /* decimal divide by zero */
175 sig = SIGFPE; code = __FPE_DECDIV;
176 break;
177
178 case 8: /* packed decimal error */
179 sig = SIGFPE; code = __FPE_DECERR;
180 break;
181
182 case 9: /* invalid ASCII digit */
183 sig = SIGFPE; code = __FPE_INVASC;
184 break;
185
186 case 10: /* invalid decimal digit */
187 sig = SIGFPE; code = __FPE_INVDEC;
188 break;
189
190 case 11: /* paragraph stack overflow */
191 sig = SIGSEGV; code = __SEGV_PSTKOVF;
192 break;
193
194 case 0x3f000 ... 0x3ffff: /* bundle-update in progress */
195 sig = SIGILL; code = __ILL_BNDMOD;
196 break;
197
198 default:
199 if (break_num < 0x40000 || break_num > 0x100000)
200 die_if_kernel("Bad break", regs, break_num);
201
202 if (break_num < 0x80000) {
203 sig = SIGILL; code = __ILL_BREAK;
204 } else {
205 if (notify_die(DIE_BREAK, "bad break", regs, break_num, TRAP_BRKPT, SIGTRAP)
206 == NOTIFY_STOP)
207 return;
208 sig = SIGTRAP; code = TRAP_BRKPT;
209 }
210 }
211 siginfo.si_signo = sig;
212 siginfo.si_errno = 0;
213 siginfo.si_code = code;
214 force_sig_info(sig, &siginfo, current);
215 }
216
217 /*
218 * disabled_fph_fault() is called when a user-level process attempts to access f32..f127
219 * and it doesn't own the fp-high register partition. When this happens, we save the
220 * current fph partition in the task_struct of the fpu-owner (if necessary) and then load
221 * the fp-high partition of the current task (if necessary). Note that the kernel has
222 * access to fph by the time we get here, as the IVT's "Disabled FP-Register" handler takes
223 * care of clearing psr.dfh.
224 */
225 static inline void
226 disabled_fph_fault (struct pt_regs *regs)
227 {
228 struct ia64_psr *psr = ia64_psr(regs);
229
230 /* first, grant user-level access to fph partition: */
231 psr->dfh = 0;
232
233 /*
234 * Make sure that no other task gets in on this processor
235 * while we're claiming the FPU
236 */
237 preempt_disable();
238 #ifndef CONFIG_SMP
239 {
240 struct task_struct *fpu_owner
241 = (struct task_struct *)ia64_get_kr(IA64_KR_FPU_OWNER);
242
243 if (ia64_is_local_fpu_owner(current)) {
244 preempt_enable_no_resched();
245 return;
246 }
247
248 if (fpu_owner)
249 ia64_flush_fph(fpu_owner);
250 }
251 #endif /* !CONFIG_SMP */
252 ia64_set_local_fpu_owner(current);
253 if ((current->thread.flags & IA64_THREAD_FPH_VALID) != 0) {
254 __ia64_load_fpu(current->thread.fph);
255 psr->mfh = 0;
256 } else {
257 __ia64_init_fpu();
258 /*
259 * Set mfh because the state in thread.fph does not match the state in
260 * the fph partition.
261 */
262 psr->mfh = 1;
263 }
264 preempt_enable_no_resched();
265 }
266
267 static inline int
268 fp_emulate (int fp_fault, void *bundle, long *ipsr, long *fpsr, long *isr, long *pr, long *ifs,
269 struct pt_regs *regs)
270 {
271 fp_state_t fp_state;
272 fpswa_ret_t ret;
273
274 if (!fpswa_interface)
275 return -1;
276
277 memset(&fp_state, 0, sizeof(fp_state_t));
278
279 /*
280 * compute fp_state. only FP registers f6 - f11 are used by the
281 * kernel, so set those bits in the mask and set the low volatile
282 * pointer to point to these registers.
283 */
284 fp_state.bitmask_low64 = 0xfc0; /* bit6..bit11 */
285
286 fp_state.fp_state_low_volatile = (fp_state_low_volatile_t *) &regs->f6;
287 /*
288 * unsigned long (*EFI_FPSWA) (
289 * unsigned long trap_type,
290 * void *Bundle,
291 * unsigned long *pipsr,
292 * unsigned long *pfsr,
293 * unsigned long *pisr,
294 * unsigned long *ppreds,
295 * unsigned long *pifs,
296 * void *fp_state);
297 */
298 ret = (*fpswa_interface->fpswa)((unsigned long) fp_fault, bundle,
299 (unsigned long *) ipsr, (unsigned long *) fpsr,
300 (unsigned long *) isr, (unsigned long *) pr,
301 (unsigned long *) ifs, &fp_state);
302
303 return ret.status;
304 }
305
306 /*
307 * Handle floating-point assist faults and traps.
308 */
309 static int
310 handle_fpu_swa (int fp_fault, struct pt_regs *regs, unsigned long isr)
311 {
312 long exception, bundle[2];
313 unsigned long fault_ip;
314 struct siginfo siginfo;
315 static int fpu_swa_count = 0;
316 static unsigned long last_time;
317
318 fault_ip = regs->cr_iip;
319 if (!fp_fault && (ia64_psr(regs)->ri == 0))
320 fault_ip -= 16;
321 if (copy_from_user(bundle, (void __user *) fault_ip, sizeof(bundle)))
322 return -1;
323
324 if (jiffies - last_time > 5*HZ)
325 fpu_swa_count = 0;
326 if ((fpu_swa_count < 4) && !(current->thread.flags & IA64_THREAD_FPEMU_NOPRINT)) {
327 last_time = jiffies;
328 ++fpu_swa_count;
329 printk(KERN_WARNING
330 "%s(%d): floating-point assist fault at ip %016lx, isr %016lx\n",
331 current->comm, current->pid, regs->cr_iip + ia64_psr(regs)->ri, isr);
332 }
333
334 exception = fp_emulate(fp_fault, bundle, &regs->cr_ipsr, &regs->ar_fpsr, &isr, &regs->pr,
335 &regs->cr_ifs, regs);
336 if (fp_fault) {
337 if (exception == 0) {
338 /* emulation was successful */
339 ia64_increment_ip(regs);
340 } else if (exception == -1) {
341 printk(KERN_ERR "handle_fpu_swa: fp_emulate() returned -1\n");
342 return -1;
343 } else {
344 /* is next instruction a trap? */
345 if (exception & 2) {
346 ia64_increment_ip(regs);
347 }
348 siginfo.si_signo = SIGFPE;
349 siginfo.si_errno = 0;
350 siginfo.si_code = __SI_FAULT; /* default code */
351 siginfo.si_addr = (void __user *) (regs->cr_iip + ia64_psr(regs)->ri);
352 if (isr & 0x11) {
353 siginfo.si_code = FPE_FLTINV;
354 } else if (isr & 0x22) {
355 /* denormal operand gets the same si_code as underflow
356 * see arch/i386/kernel/traps.c:math_error() */
357 siginfo.si_code = FPE_FLTUND;
358 } else if (isr & 0x44) {
359 siginfo.si_code = FPE_FLTDIV;
360 }
361 siginfo.si_isr = isr;
362 siginfo.si_flags = __ISR_VALID;
363 siginfo.si_imm = 0;
364 force_sig_info(SIGFPE, &siginfo, current);
365 }
366 } else {
367 if (exception == -1) {
368 printk(KERN_ERR "handle_fpu_swa: fp_emulate() returned -1\n");
369 return -1;
370 } else if (exception != 0) {
371 /* raise exception */
372 siginfo.si_signo = SIGFPE;
373 siginfo.si_errno = 0;
374 siginfo.si_code = __SI_FAULT; /* default code */
375 siginfo.si_addr = (void __user *) (regs->cr_iip + ia64_psr(regs)->ri);
376 if (isr & 0x880) {
377 siginfo.si_code = FPE_FLTOVF;
378 } else if (isr & 0x1100) {
379 siginfo.si_code = FPE_FLTUND;
380 } else if (isr & 0x2200) {
381 siginfo.si_code = FPE_FLTRES;
382 }
383 siginfo.si_isr = isr;
384 siginfo.si_flags = __ISR_VALID;
385 siginfo.si_imm = 0;
386 force_sig_info(SIGFPE, &siginfo, current);
387 }
388 }
389 return 0;
390 }
391
392 struct illegal_op_return {
393 unsigned long fkt, arg1, arg2, arg3;
394 };
395
396 struct illegal_op_return
397 ia64_illegal_op_fault (unsigned long ec, long arg1, long arg2, long arg3,
398 long arg4, long arg5, long arg6, long arg7,
399 struct pt_regs regs)
400 {
401 struct illegal_op_return rv;
402 struct siginfo si;
403 char buf[128];
404
405 #ifdef CONFIG_IA64_BRL_EMU
406 {
407 extern struct illegal_op_return ia64_emulate_brl (struct pt_regs *, unsigned long);
408
409 rv = ia64_emulate_brl(&regs, ec);
410 if (rv.fkt != (unsigned long) -1)
411 return rv;
412 }
413 #endif
414
415 sprintf(buf, "IA-64 Illegal operation fault");
416 die_if_kernel(buf, &regs, 0);
417
418 memset(&si, 0, sizeof(si));
419 si.si_signo = SIGILL;
420 si.si_code = ILL_ILLOPC;
421 si.si_addr = (void __user *) (regs.cr_iip + ia64_psr(&regs)->ri);
422 force_sig_info(SIGILL, &si, current);
423 rv.fkt = 0;
424 return rv;
425 }
426
427 void __kprobes
428 ia64_fault (unsigned long vector, unsigned long isr, unsigned long ifa,
429 unsigned long iim, unsigned long itir, long arg5, long arg6,
430 long arg7, struct pt_regs regs)
431 {
432 unsigned long code, error = isr, iip;
433 struct siginfo siginfo;
434 char buf[128];
435 int result, sig;
436 static const char *reason[] = {
437 "IA-64 Illegal Operation fault",
438 "IA-64 Privileged Operation fault",
439 "IA-64 Privileged Register fault",
440 "IA-64 Reserved Register/Field fault",
441 "Disabled Instruction Set Transition fault",
442 "Unknown fault 5", "Unknown fault 6", "Unknown fault 7", "Illegal Hazard fault",
443 "Unknown fault 9", "Unknown fault 10", "Unknown fault 11", "Unknown fault 12",
444 "Unknown fault 13", "Unknown fault 14", "Unknown fault 15"
445 };
446
447 if ((isr & IA64_ISR_NA) && ((isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH)) {
448 /*
449 * This fault was due to lfetch.fault, set "ed" bit in the psr to cancel
450 * the lfetch.
451 */
452 ia64_psr(&regs)->ed = 1;
453 return;
454 }
455
456 iip = regs.cr_iip + ia64_psr(&regs)->ri;
457
458 switch (vector) {
459 case 24: /* General Exception */
460 code = (isr >> 4) & 0xf;
461 sprintf(buf, "General Exception: %s%s", reason[code],
462 (code == 3) ? ((isr & (1UL << 37))
463 ? " (RSE access)" : " (data access)") : "");
464 if (code == 8) {
465 # ifdef CONFIG_IA64_PRINT_HAZARDS
466 printk("%s[%d]: possible hazard @ ip=%016lx (pr = %016lx)\n",
467 current->comm, current->pid,
468 regs.cr_iip + ia64_psr(&regs)->ri, regs.pr);
469 # endif
470 return;
471 }
472 break;
473
474 case 25: /* Disabled FP-Register */
475 if (isr & 2) {
476 disabled_fph_fault(&regs);
477 return;
478 }
479 sprintf(buf, "Disabled FPL fault---not supposed to happen!");
480 break;
481
482 case 26: /* NaT Consumption */
483 if (user_mode(&regs)) {
484 void __user *addr;
485
486 if (((isr >> 4) & 0xf) == 2) {
487 /* NaT page consumption */
488 sig = SIGSEGV;
489 code = SEGV_ACCERR;
490 addr = (void __user *) ifa;
491 } else {
492 /* register NaT consumption */
493 sig = SIGILL;
494 code = ILL_ILLOPN;
495 addr = (void __user *) (regs.cr_iip
496 + ia64_psr(&regs)->ri);
497 }
498 siginfo.si_signo = sig;
499 siginfo.si_code = code;
500 siginfo.si_errno = 0;
501 siginfo.si_addr = addr;
502 siginfo.si_imm = vector;
503 siginfo.si_flags = __ISR_VALID;
504 siginfo.si_isr = isr;
505 force_sig_info(sig, &siginfo, current);
506 return;
507 } else if (ia64_done_with_exception(&regs))
508 return;
509 sprintf(buf, "NaT consumption");
510 break;
511
512 case 31: /* Unsupported Data Reference */
513 if (user_mode(&regs)) {
514 siginfo.si_signo = SIGILL;
515 siginfo.si_code = ILL_ILLOPN;
516 siginfo.si_errno = 0;
517 siginfo.si_addr = (void __user *) iip;
518 siginfo.si_imm = vector;
519 siginfo.si_flags = __ISR_VALID;
520 siginfo.si_isr = isr;
521 force_sig_info(SIGILL, &siginfo, current);
522 return;
523 }
524 sprintf(buf, "Unsupported data reference");
525 break;
526
527 case 29: /* Debug */
528 case 35: /* Taken Branch Trap */
529 case 36: /* Single Step Trap */
530 if (fsys_mode(current, &regs)) {
531 extern char __kernel_syscall_via_break[];
532 /*
533 * Got a trap in fsys-mode: Taken Branch Trap and Single Step trap
534 * need special handling; Debug trap is not supposed to happen.
535 */
536 if (unlikely(vector == 29)) {
537 die("Got debug trap in fsys-mode---not supposed to happen!",
538 &regs, 0);
539 return;
540 }
541 /* re-do the system call via break 0x100000: */
542 regs.cr_iip = (unsigned long) __kernel_syscall_via_break;
543 ia64_psr(&regs)->ri = 0;
544 ia64_psr(&regs)->cpl = 3;
545 return;
546 }
547 switch (vector) {
548 case 29:
549 siginfo.si_code = TRAP_HWBKPT;
550 #ifdef CONFIG_ITANIUM
551 /*
552 * Erratum 10 (IFA may contain incorrect address) now has
553 * "NoFix" status. There are no plans for fixing this.
554 */
555 if (ia64_psr(&regs)->is == 0)
556 ifa = regs.cr_iip;
557 #endif
558 break;
559 case 35: siginfo.si_code = TRAP_BRANCH; ifa = 0; break;
560 case 36: siginfo.si_code = TRAP_TRACE; ifa = 0; break;
561 }
562 if (notify_die(DIE_FAULT, "ia64_fault", &regs, vector, siginfo.si_code, SIGTRAP)
563 == NOTIFY_STOP)
564 return;
565 siginfo.si_signo = SIGTRAP;
566 siginfo.si_errno = 0;
567 siginfo.si_addr = (void __user *) ifa;
568 siginfo.si_imm = 0;
569 siginfo.si_flags = __ISR_VALID;
570 siginfo.si_isr = isr;
571 force_sig_info(SIGTRAP, &siginfo, current);
572 return;
573
574 case 32: /* fp fault */
575 case 33: /* fp trap */
576 result = handle_fpu_swa((vector == 32) ? 1 : 0, &regs, isr);
577 if ((result < 0) || (current->thread.flags & IA64_THREAD_FPEMU_SIGFPE)) {
578 siginfo.si_signo = SIGFPE;
579 siginfo.si_errno = 0;
580 siginfo.si_code = FPE_FLTINV;
581 siginfo.si_addr = (void __user *) iip;
582 siginfo.si_flags = __ISR_VALID;
583 siginfo.si_isr = isr;
584 siginfo.si_imm = 0;
585 force_sig_info(SIGFPE, &siginfo, current);
586 }
587 return;
588
589 case 34:
590 if (isr & 0x2) {
591 /* Lower-Privilege Transfer Trap */
592 /*
593 * Just clear PSR.lp and then return immediately: all the
594 * interesting work (e.g., signal delivery is done in the kernel
595 * exit path).
596 */
597 ia64_psr(&regs)->lp = 0;
598 return;
599 } else {
600 /* Unimplemented Instr. Address Trap */
601 if (user_mode(&regs)) {
602 siginfo.si_signo = SIGILL;
603 siginfo.si_code = ILL_BADIADDR;
604 siginfo.si_errno = 0;
605 siginfo.si_flags = 0;
606 siginfo.si_isr = 0;
607 siginfo.si_imm = 0;
608 siginfo.si_addr = (void __user *) iip;
609 force_sig_info(SIGILL, &siginfo, current);
610 return;
611 }
612 sprintf(buf, "Unimplemented Instruction Address fault");
613 }
614 break;
615
616 case 45:
617 #ifdef CONFIG_IA32_SUPPORT
618 if (ia32_exception(&regs, isr) == 0)
619 return;
620 #endif
621 printk(KERN_ERR "Unexpected IA-32 exception (Trap 45)\n");
622 printk(KERN_ERR " iip - 0x%lx, ifa - 0x%lx, isr - 0x%lx\n",
623 iip, ifa, isr);
624 force_sig(SIGSEGV, current);
625 break;
626
627 case 46:
628 #ifdef CONFIG_IA32_SUPPORT
629 if (ia32_intercept(&regs, isr) == 0)
630 return;
631 #endif
632 printk(KERN_ERR "Unexpected IA-32 intercept trap (Trap 46)\n");
633 printk(KERN_ERR " iip - 0x%lx, ifa - 0x%lx, isr - 0x%lx, iim - 0x%lx\n",
634 iip, ifa, isr, iim);
635 force_sig(SIGSEGV, current);
636 return;
637
638 case 47:
639 sprintf(buf, "IA-32 Interruption Fault (int 0x%lx)", isr >> 16);
640 break;
641
642 default:
643 sprintf(buf, "Fault %lu", vector);
644 break;
645 }
646 die_if_kernel(buf, &regs, error);
647 force_sig(SIGILL, current);
648 }
This page took 0.046748 seconds and 6 git commands to generate.