Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / frv / kernel / traps.c
blob7089c2428b3f14507ffbf1e356bc59f5da81aa75
1 /* traps.c: high-level exception handler for FR-V
3 * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/sched.h>
13 #include <linux/signal.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/types.h>
17 #include <linux/user.h>
18 #include <linux/string.h>
19 #include <linux/linkage.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
23 #include <asm/asm-offsets.h>
24 #include <asm/setup.h>
25 #include <asm/fpu.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/siginfo.h>
30 #include <asm/unaligned.h>
32 void show_backtrace(struct pt_regs *, unsigned long);
34 extern asmlinkage void __break_hijack_kernel_event(void);
36 /*****************************************************************************/
38 * instruction access error
40 asmlinkage void insn_access_error(unsigned long esfr1, unsigned long epcr0, unsigned long esr0)
42 siginfo_t info;
44 die_if_kernel("-- Insn Access Error --\n"
45 "EPCR0 : %08lx\n"
46 "ESR0 : %08lx\n",
47 epcr0, esr0);
49 info.si_signo = SIGSEGV;
50 info.si_code = SEGV_ACCERR;
51 info.si_errno = 0;
52 info.si_addr = (void *) ((epcr0 & EPCR0_V) ? (epcr0 & EPCR0_PC) : __frame->pc);
54 force_sig_info(info.si_signo, &info, current);
55 } /* end insn_access_error() */
57 /*****************************************************************************/
59 * handler for:
60 * - illegal instruction
61 * - privileged instruction
62 * - unsupported trap
63 * - debug exceptions
65 asmlinkage void illegal_instruction(unsigned long esfr1, unsigned long epcr0, unsigned long esr0)
67 siginfo_t info;
69 die_if_kernel("-- Illegal Instruction --\n"
70 "EPCR0 : %08lx\n"
71 "ESR0 : %08lx\n"
72 "ESFR1 : %08lx\n",
73 epcr0, esr0, esfr1);
75 info.si_errno = 0;
76 info.si_addr = (void *) ((epcr0 & EPCR0_V) ? (epcr0 & EPCR0_PC) : __frame->pc);
78 switch (__frame->tbr & TBR_TT) {
79 case TBR_TT_ILLEGAL_INSTR:
80 info.si_signo = SIGILL;
81 info.si_code = ILL_ILLOPC;
82 break;
83 case TBR_TT_PRIV_INSTR:
84 info.si_signo = SIGILL;
85 info.si_code = ILL_PRVOPC;
86 break;
87 case TBR_TT_TRAP2 ... TBR_TT_TRAP126:
88 info.si_signo = SIGILL;
89 info.si_code = ILL_ILLTRP;
90 break;
91 /* GDB uses "tira gr0, #1" as a breakpoint instruction. */
92 case TBR_TT_TRAP1:
93 case TBR_TT_BREAK:
94 info.si_signo = SIGTRAP;
95 info.si_code =
96 (__frame->__status & REG__STATUS_STEPPED) ? TRAP_TRACE : TRAP_BRKPT;
97 break;
100 force_sig_info(info.si_signo, &info, current);
101 } /* end illegal_instruction() */
103 /*****************************************************************************/
105 * handle atomic operations with errors
106 * - arguments in gr8, gr9, gr10
107 * - original memory value placed in gr5
108 * - replacement memory value placed in gr9
110 asmlinkage void atomic_operation(unsigned long esfr1, unsigned long epcr0,
111 unsigned long esr0)
113 static DEFINE_SPINLOCK(atomic_op_lock);
114 unsigned long x, y, z, *p;
115 mm_segment_t oldfs;
116 siginfo_t info;
117 int ret;
119 y = 0;
120 z = 0;
122 oldfs = get_fs();
123 if (!user_mode(__frame))
124 set_fs(KERNEL_DS);
126 switch (__frame->tbr & TBR_TT) {
127 /* TIRA gr0,#120
128 * u32 __atomic_user_cmpxchg32(u32 *ptr, u32 test, u32 new)
130 case TBR_TT_ATOMIC_CMPXCHG32:
131 p = (unsigned long *) __frame->gr8;
132 x = __frame->gr9;
133 y = __frame->gr10;
135 for (;;) {
136 ret = get_user(z, p);
137 if (ret < 0)
138 goto error;
140 if (z != x)
141 goto done;
143 spin_lock_irq(&atomic_op_lock);
145 if (__get_user(z, p) == 0) {
146 if (z != x)
147 goto done2;
149 if (__put_user(y, p) == 0)
150 goto done2;
151 goto error2;
154 spin_unlock_irq(&atomic_op_lock);
157 /* TIRA gr0,#121
158 * u32 __atomic_kernel_xchg32(void *v, u32 new)
160 case TBR_TT_ATOMIC_XCHG32:
161 p = (unsigned long *) __frame->gr8;
162 y = __frame->gr9;
164 for (;;) {
165 ret = get_user(z, p);
166 if (ret < 0)
167 goto error;
169 spin_lock_irq(&atomic_op_lock);
171 if (__get_user(z, p) == 0) {
172 if (__put_user(y, p) == 0)
173 goto done2;
174 goto error2;
177 spin_unlock_irq(&atomic_op_lock);
180 /* TIRA gr0,#122
181 * ulong __atomic_kernel_XOR_return(ulong i, ulong *v)
183 case TBR_TT_ATOMIC_XOR:
184 p = (unsigned long *) __frame->gr8;
185 x = __frame->gr9;
187 for (;;) {
188 ret = get_user(z, p);
189 if (ret < 0)
190 goto error;
192 spin_lock_irq(&atomic_op_lock);
194 if (__get_user(z, p) == 0) {
195 y = x ^ z;
196 if (__put_user(y, p) == 0)
197 goto done2;
198 goto error2;
201 spin_unlock_irq(&atomic_op_lock);
204 /* TIRA gr0,#123
205 * ulong __atomic_kernel_OR_return(ulong i, ulong *v)
207 case TBR_TT_ATOMIC_OR:
208 p = (unsigned long *) __frame->gr8;
209 x = __frame->gr9;
211 for (;;) {
212 ret = get_user(z, p);
213 if (ret < 0)
214 goto error;
216 spin_lock_irq(&atomic_op_lock);
218 if (__get_user(z, p) == 0) {
219 y = x ^ z;
220 if (__put_user(y, p) == 0)
221 goto done2;
222 goto error2;
225 spin_unlock_irq(&atomic_op_lock);
228 /* TIRA gr0,#124
229 * ulong __atomic_kernel_AND_return(ulong i, ulong *v)
231 case TBR_TT_ATOMIC_AND:
232 p = (unsigned long *) __frame->gr8;
233 x = __frame->gr9;
235 for (;;) {
236 ret = get_user(z, p);
237 if (ret < 0)
238 goto error;
240 spin_lock_irq(&atomic_op_lock);
242 if (__get_user(z, p) == 0) {
243 y = x & z;
244 if (__put_user(y, p) == 0)
245 goto done2;
246 goto error2;
249 spin_unlock_irq(&atomic_op_lock);
252 /* TIRA gr0,#125
253 * int __atomic_user_sub_return(atomic_t *v, int i)
255 case TBR_TT_ATOMIC_SUB:
256 p = (unsigned long *) __frame->gr8;
257 x = __frame->gr9;
259 for (;;) {
260 ret = get_user(z, p);
261 if (ret < 0)
262 goto error;
264 spin_lock_irq(&atomic_op_lock);
266 if (__get_user(z, p) == 0) {
267 y = z - x;
268 if (__put_user(y, p) == 0)
269 goto done2;
270 goto error2;
273 spin_unlock_irq(&atomic_op_lock);
276 /* TIRA gr0,#126
277 * int __atomic_user_add_return(atomic_t *v, int i)
279 case TBR_TT_ATOMIC_ADD:
280 p = (unsigned long *) __frame->gr8;
281 x = __frame->gr9;
283 for (;;) {
284 ret = get_user(z, p);
285 if (ret < 0)
286 goto error;
288 spin_lock_irq(&atomic_op_lock);
290 if (__get_user(z, p) == 0) {
291 y = z + x;
292 if (__put_user(y, p) == 0)
293 goto done2;
294 goto error2;
297 spin_unlock_irq(&atomic_op_lock);
300 default:
301 BUG();
304 done2:
305 spin_unlock_irq(&atomic_op_lock);
306 done:
307 if (!user_mode(__frame))
308 set_fs(oldfs);
309 __frame->gr5 = z;
310 __frame->gr9 = y;
311 return;
313 error2:
314 spin_unlock_irq(&atomic_op_lock);
315 error:
316 if (!user_mode(__frame))
317 set_fs(oldfs);
318 __frame->pc -= 4;
320 die_if_kernel("-- Atomic Op Error --\n");
322 info.si_signo = SIGSEGV;
323 info.si_code = SEGV_ACCERR;
324 info.si_errno = 0;
325 info.si_addr = (void *) __frame->pc;
327 force_sig_info(info.si_signo, &info, current);
330 /*****************************************************************************/
334 asmlinkage void media_exception(unsigned long msr0, unsigned long msr1)
336 siginfo_t info;
338 die_if_kernel("-- Media Exception --\n"
339 "MSR0 : %08lx\n"
340 "MSR1 : %08lx\n",
341 msr0, msr1);
343 info.si_signo = SIGFPE;
344 info.si_code = FPE_MDAOVF;
345 info.si_errno = 0;
346 info.si_addr = (void *) __frame->pc;
348 force_sig_info(info.si_signo, &info, current);
349 } /* end media_exception() */
351 /*****************************************************************************/
353 * instruction or data access exception
355 asmlinkage void memory_access_exception(unsigned long esr0,
356 unsigned long ear0,
357 unsigned long epcr0)
359 siginfo_t info;
361 #ifdef CONFIG_MMU
362 unsigned long fixup;
364 if ((esr0 & ESRx_EC) == ESRx_EC_DATA_ACCESS)
365 if (handle_misalignment(esr0, ear0, epcr0) == 0)
366 return;
368 if ((fixup = search_exception_table(__frame->pc)) != 0) {
369 __frame->pc = fixup;
370 return;
372 #endif
374 die_if_kernel("-- Memory Access Exception --\n"
375 "ESR0 : %08lx\n"
376 "EAR0 : %08lx\n"
377 "EPCR0 : %08lx\n",
378 esr0, ear0, epcr0);
380 info.si_signo = SIGSEGV;
381 info.si_code = SEGV_ACCERR;
382 info.si_errno = 0;
383 info.si_addr = NULL;
385 if ((esr0 & (ESRx_VALID | ESR0_EAV)) == (ESRx_VALID | ESR0_EAV))
386 info.si_addr = (void *) ear0;
388 force_sig_info(info.si_signo, &info, current);
390 } /* end memory_access_exception() */
392 /*****************************************************************************/
394 * data access error
395 * - double-word data load from CPU control area (0xFExxxxxx)
396 * - read performed on inactive or self-refreshing SDRAM
397 * - error notification from slave device
398 * - misaligned address
399 * - access to out of bounds memory region
400 * - user mode accessing privileged memory region
401 * - write to R/O memory region
403 asmlinkage void data_access_error(unsigned long esfr1, unsigned long esr15, unsigned long ear15)
405 siginfo_t info;
407 die_if_kernel("-- Data Access Error --\n"
408 "ESR15 : %08lx\n"
409 "EAR15 : %08lx\n",
410 esr15, ear15);
412 info.si_signo = SIGSEGV;
413 info.si_code = SEGV_ACCERR;
414 info.si_errno = 0;
415 info.si_addr = (void *)
416 (((esr15 & (ESRx_VALID|ESR15_EAV)) == (ESRx_VALID|ESR15_EAV)) ? ear15 : 0);
418 force_sig_info(info.si_signo, &info, current);
419 } /* end data_access_error() */
421 /*****************************************************************************/
423 * data store error - should only happen if accessing inactive or self-refreshing SDRAM
425 asmlinkage void data_store_error(unsigned long esfr1, unsigned long esr15)
427 die_if_kernel("-- Data Store Error --\n"
428 "ESR15 : %08lx\n",
429 esr15);
430 BUG();
431 } /* end data_store_error() */
433 /*****************************************************************************/
437 asmlinkage void division_exception(unsigned long esfr1, unsigned long esr0, unsigned long isr)
439 siginfo_t info;
441 die_if_kernel("-- Division Exception --\n"
442 "ESR0 : %08lx\n"
443 "ISR : %08lx\n",
444 esr0, isr);
446 info.si_signo = SIGFPE;
447 info.si_code = FPE_INTDIV;
448 info.si_errno = 0;
449 info.si_addr = (void *) __frame->pc;
451 force_sig_info(info.si_signo, &info, current);
452 } /* end division_exception() */
454 /*****************************************************************************/
458 asmlinkage void compound_exception(unsigned long esfr1,
459 unsigned long esr0, unsigned long esr14, unsigned long esr15,
460 unsigned long msr0, unsigned long msr1)
462 die_if_kernel("-- Compound Exception --\n"
463 "ESR0 : %08lx\n"
464 "ESR15 : %08lx\n"
465 "ESR15 : %08lx\n"
466 "MSR0 : %08lx\n"
467 "MSR1 : %08lx\n",
468 esr0, esr14, esr15, msr0, msr1);
469 BUG();
470 } /* end compound_exception() */
472 /*****************************************************************************/
474 * The architecture-independent backtrace generator
476 void dump_stack(void)
478 show_stack(NULL, NULL);
481 EXPORT_SYMBOL(dump_stack);
483 void show_stack(struct task_struct *task, unsigned long *sp)
487 void show_trace_task(struct task_struct *tsk)
489 printk("CONTEXT: stack=0x%lx frame=0x%p LR=0x%lx RET=0x%lx\n",
490 tsk->thread.sp, tsk->thread.frame, tsk->thread.lr, tsk->thread.sched_lr);
493 static const char *regnames[] = {
494 "PSR ", "ISR ", "CCR ", "CCCR",
495 "LR ", "LCR ", "PC ", "_stt",
496 "sys ", "GR8*", "GNE0", "GNE1",
497 "IACH", "IACL",
498 "TBR ", "SP ", "FP ", "GR3 ",
499 "GR4 ", "GR5 ", "GR6 ", "GR7 ",
500 "GR8 ", "GR9 ", "GR10", "GR11",
501 "GR12", "GR13", "GR14", "GR15",
502 "GR16", "GR17", "GR18", "GR19",
503 "GR20", "GR21", "GR22", "GR23",
504 "GR24", "GR25", "GR26", "GR27",
505 "EFRM", "CURR", "GR30", "BFRM"
508 void show_regs(struct pt_regs *regs)
510 unsigned long *reg;
511 int loop;
513 printk("\n");
515 printk("Frame: @%08lx [%s]\n",
516 (unsigned long) regs,
517 regs->psr & PSR_S ? "kernel" : "user");
519 reg = (unsigned long *) regs;
520 for (loop = 0; loop < NR_PT_REGS; loop++) {
521 printk("%s %08lx", regnames[loop + 0], reg[loop + 0]);
523 if (loop == NR_PT_REGS - 1 || loop % 5 == 4)
524 printk("\n");
525 else
526 printk(" | ");
529 printk("Process %s (pid: %d)\n", current->comm, current->pid);
532 void die_if_kernel(const char *str, ...)
534 char buffer[256];
535 va_list va;
537 if (user_mode(__frame))
538 return;
540 va_start(va, str);
541 vsprintf(buffer, str, va);
542 va_end(va);
544 console_verbose();
545 printk("\n===================================\n");
546 printk("%s\n", buffer);
547 show_backtrace(__frame, 0);
549 __break_hijack_kernel_event();
550 do_exit(SIGSEGV);
553 /*****************************************************************************/
555 * dump the contents of an exception frame
557 static void show_backtrace_regs(struct pt_regs *frame)
559 unsigned long *reg;
560 int loop;
562 /* print the registers for this frame */
563 printk("<-- %s Frame: @%p -->\n",
564 frame->psr & PSR_S ? "Kernel Mode" : "User Mode",
565 frame);
567 reg = (unsigned long *) frame;
568 for (loop = 0; loop < NR_PT_REGS; loop++) {
569 printk("%s %08lx", regnames[loop + 0], reg[loop + 0]);
571 if (loop == NR_PT_REGS - 1 || loop % 5 == 4)
572 printk("\n");
573 else
574 printk(" | ");
577 printk("--------\n");
578 } /* end show_backtrace_regs() */
580 /*****************************************************************************/
582 * generate a backtrace of the kernel stack
584 void show_backtrace(struct pt_regs *frame, unsigned long sp)
586 struct pt_regs *frame0;
587 unsigned long tos = 0, stop = 0, base;
588 int format;
590 base = ((((unsigned long) frame) + 8191) & ~8191) - sizeof(struct user_context);
591 frame0 = (struct pt_regs *) base;
593 if (sp) {
594 tos = sp;
595 stop = (unsigned long) frame;
598 printk("\nProcess %s (pid: %d)\n\n", current->comm, current->pid);
600 for (;;) {
601 /* dump stack segment between frames */
602 //printk("%08lx -> %08lx\n", tos, stop);
603 format = 0;
604 while (tos < stop) {
605 if (format == 0)
606 printk(" %04lx :", tos & 0xffff);
608 printk(" %08lx", *(unsigned long *) tos);
610 tos += 4;
611 format++;
612 if (format == 8) {
613 printk("\n");
614 format = 0;
618 if (format > 0)
619 printk("\n");
621 /* dump frame 0 outside of the loop */
622 if (frame == frame0)
623 break;
625 tos = frame->sp;
626 if (((unsigned long) frame) + sizeof(*frame) != tos) {
627 printk("-- TOS %08lx does not follow frame %p --\n",
628 tos, frame);
629 break;
632 show_backtrace_regs(frame);
634 /* dump the stack between this frame and the next */
635 stop = (unsigned long) frame->next_frame;
636 if (stop != base &&
637 (stop < tos ||
638 stop > base ||
639 (stop < base && stop + sizeof(*frame) > base) ||
640 stop & 3)) {
641 printk("-- next_frame %08lx is invalid (range %08lx-%08lx) --\n",
642 stop, tos, base);
643 break;
646 /* move to next frame */
647 frame = frame->next_frame;
650 /* we can always dump frame 0, even if the rest of the stack is corrupt */
651 show_backtrace_regs(frame0);
653 } /* end show_backtrace() */
655 /*****************************************************************************/
657 * initialise traps
659 void __init trap_init (void)
661 } /* end trap_init() */