| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <arch/cache.h> |
| 30 |
#include <arch/exception.h> |
| 31 |
#include <exception.h> |
| 32 |
#include <libpayload.h> |
| 33 |
#include <stdint.h> |
| 34 |
|
| 35 |
u32 exception_stack[0x400] __attribute__((aligned(8))); |
| 36 |
struct exception_state exception_state; |
| 37 |
|
| 38 |
static exception_hook hook; |
| 39 |
static const char *names[EXC_COUNT] = { |
| 40 |
[EXC_UNDEF] = "Undefined Instruction", |
| 41 |
[EXC_SWI] = "Software Interrupt", |
| 42 |
[EXC_PABORT] = "Prefetch Abort", |
| 43 |
[EXC_DABORT] = "Data Abort", |
| 44 |
[EXC_IRQ] = "Interrupt", |
| 45 |
[EXC_FIQ] = "Fast Interrupt", |
| 46 |
}; |
| 47 |
|
| 48 |
static void dump_stack(uintptr_t addr, size_t bytes) |
| 49 |
{ |
| 50 |
int i, j; |
| 51 |
const int line = 8; |
| 52 |
uint32_t *ptr = (uint32_t *)(addr & ~(line * sizeof(*ptr) - 1)); |
| 53 |
|
| 54 |
printf("Dumping stack:\n"); |
| 55 |
for (i = bytes / sizeof(*ptr); i >= 0; i -= line) { |
| 56 |
printf("%p: ", ptr + i); |
| 57 |
for (j = i; j < i + line; j++) |
| 58 |
printf("%08x ", *(ptr + j)); |
| 59 |
printf("\n"); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static void print_regs(void) |
| 64 |
{ |
| 65 |
int i; |
| 66 |
|
| 67 |
for (i = 0; i < 16; i++) { |
| 68 |
if (i == 15) |
| 69 |
printf("PC"); |
| 70 |
else if (i == 14) |
| 71 |
printf("LR"); |
| 72 |
else if (i == 13) |
| 73 |
printf("SP"); |
| 74 |
else if (i == 12) |
| 75 |
printf("IP"); |
| 76 |
else |
| 77 |
printf("R%d", i); |
| 78 |
printf(" = 0x%08x\n", exception_state.regs[i]); |
| 79 |
} |
| 80 |
printf("CPSR = 0x%08x\n", exception_state.cpsr); |
| 81 |
} |
| 82 |
|
| 83 |
void exception_dispatch(u32 idx) |
| 84 |
{ |
| 85 |
die_if(idx >= EXC_COUNT || !names[idx], "Bad exception index %u!", idx); |
| 86 |
|
| 87 |
if (hook && hook(idx)) |
| 88 |
return; |
| 89 |
|
| 90 |
printf("%s Exception\n", names[idx]); |
| 91 |
print_regs(); |
| 92 |
switch (idx) { |
| 93 |
case EXC_PABORT: |
| 94 |
printf("IFAR = %#.8x\n", read_ifar()); |
| 95 |
printf("IFSR = %#.8x\n", read_ifsr()); |
| 96 |
printf("AIFSR = %#.8x\n", read_aifsr()); |
| 97 |
break; |
| 98 |
case EXC_DABORT: |
| 99 |
printf("DFAR = %#.8x\n", read_dfar()); |
| 100 |
printf("DFSR = %#.8x\n", read_dfsr()); |
| 101 |
printf("ADFSR = %#.8x\n", read_adfsr()); |
| 102 |
break; |
| 103 |
}; |
| 104 |
dump_stack(exception_state.regs[13], 512); |
| 105 |
halt(); |
| 106 |
} |
| 107 |
|
| 108 |
void exception_init(void) |
| 109 |
{ |
| 110 |
uint32_t sctlr = read_sctlr(); |
| 111 |
|
| 112 |
sctlr &= ~SCTLR_TE; |
| 113 |
|
| 114 |
sctlr &= ~SCTLR_V; |
| 115 |
write_sctlr(sctlr); |
| 116 |
|
| 117 |
extern uint32_t exception_table[]; |
| 118 |
set_vbar((uintptr_t)exception_table); |
| 119 |
|
| 120 |
exception_stack_end = exception_stack + ARRAY_SIZE(exception_stack); |
| 121 |
exception_state_ptr = &exception_state; |
| 122 |
} |
| 123 |
|
| 124 |
void exception_install_hook(exception_hook h) |
| 125 |
{ |
| 126 |
die_if(hook, "Implement support for a list of hooks if you need it."); |
| 127 |
hook = h; |
| 128 |
} |