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