Stefan Kristiansson | 272f84b | 2011-11-26 19:04:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011, Stefan Kristiansson <stefan.kristiansson@saunalahti.fi> |
| 3 | * (C) Copyright 2011, Julius Baxter <julius@opencores.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 18 | * MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
| 22 | #include <stdio_dev.h> |
| 23 | #include <asm/system.h> |
| 24 | |
| 25 | static const char * const excp_table[] = { |
| 26 | "Unknown exception", |
| 27 | "Reset", |
| 28 | "Bus Error", |
| 29 | "Data Page Fault", |
| 30 | "Instruction Page Fault", |
| 31 | "Tick Timer", |
| 32 | "Alignment", |
| 33 | "Illegal Instruction", |
| 34 | "External Interrupt", |
| 35 | "D-TLB Miss", |
| 36 | "I-TLB Miss", |
| 37 | "Range", |
| 38 | "System Call", |
| 39 | "Floating Point", |
| 40 | "Trap", |
| 41 | }; |
| 42 | |
| 43 | static void (*handlers[32])(void); |
| 44 | |
| 45 | void exception_install_handler(int exception, void (*handler)(void)) |
| 46 | { |
| 47 | if (exception < 0 || exception > 31) |
| 48 | return; |
| 49 | |
| 50 | handlers[exception] = handler; |
| 51 | } |
| 52 | |
| 53 | void exception_free_handler(int exception) |
| 54 | { |
| 55 | if (exception < 0 || exception > 31) |
| 56 | return; |
| 57 | |
| 58 | handlers[exception] = 0; |
| 59 | } |
| 60 | |
| 61 | static void exception_hang(int vect) |
| 62 | { |
| 63 | printf("Unhandled exception at 0x%x ", vect & 0xff00); |
| 64 | |
| 65 | vect = ((vect >> 8) & 0xff); |
| 66 | if (vect < ARRAY_SIZE(excp_table)) |
| 67 | printf("(%s)\n", excp_table[vect]); |
| 68 | else |
| 69 | printf("(%s)\n", excp_table[0]); |
| 70 | |
| 71 | printf("EPCR: 0x%08lx\n", mfspr(SPR_EPCR_BASE)); |
| 72 | printf("EEAR: 0x%08lx\n", mfspr(SPR_EEAR_BASE)); |
| 73 | printf("ESR: 0x%08lx\n", mfspr(SPR_ESR_BASE)); |
| 74 | hang(); |
| 75 | } |
| 76 | |
| 77 | void exception_handler(int vect) |
| 78 | { |
| 79 | int exception = vect >> 8; |
| 80 | |
| 81 | if (handlers[exception]) |
| 82 | handlers[exception](); |
| 83 | else |
| 84 | exception_hang(vect); |
| 85 | } |