| 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 <exception.h> |
| 30 |
#include <libpayload.h> |
| 31 |
#include <stdint.h> |
| 32 |
|
| 33 |
u64 exception_stack[2*KiB] __attribute__((aligned(16))); |
| 34 |
u64 *exception_stack_end = exception_stack + ARRAY_SIZE(exception_stack); |
| 35 |
|
| 36 |
struct exception_handler_info |
| 37 |
{ |
| 38 |
const char *name; |
| 39 |
}; |
| 40 |
|
| 41 |
static exception_hook hook; |
| 42 |
struct exception_state exception_state; |
| 43 |
|
| 44 |
static struct exception_handler_info exceptions[EXC_COUNT] = { |
| 45 |
[EXC_SYNC_SP0] = { "_sync_sp_el0" }, |
| 46 |
[EXC_IRQ_SP0] = { "_irq_sp_el0" }, |
| 47 |
[EXC_FIQ_SP0] = { "_fiq_sp_el0" }, |
| 48 |
[EXC_SERROR_SP0] = {"_serror_sp_el0"}, |
| 49 |
[EXC_SYNC_SPX] = { "_sync_spx" }, |
| 50 |
[EXC_IRQ_SPX] = { "_irq_spx" }, |
| 51 |
[EXC_FIQ_SPX] = { "_fiq_spx" }, |
| 52 |
[EXC_SERROR_SPX] = {"_serror_spx"}, |
| 53 |
[EXC_SYNC_ELX_64] = { "_sync_elx_64" }, |
| 54 |
[EXC_IRQ_ELX_64] = { "_irq_elx_64" }, |
| 55 |
[EXC_FIQ_ELX_64] = { "_fiq_elx_64" }, |
| 56 |
[EXC_SERROR_ELX_64] = {"_serror_elx_64"}, |
| 57 |
[EXC_SYNC_ELX_32] = { "_sync_elx_32" }, |
| 58 |
[EXC_IRQ_ELX_32] = { "_irq_elx_32" }, |
| 59 |
[EXC_FIQ_ELX_32] = { "_fiq_elx_32" }, |
| 60 |
[EXC_SERROR_ELX_32] = {"_serror_elx_32"}, |
| 61 |
}; |
| 62 |
|
| 63 |
static void dump_stack(uintptr_t addr, size_t bytes) |
| 64 |
{ |
| 65 |
int i, j; |
| 66 |
const int words_per_line = 8; |
| 67 |
uint64_t *ptr = (void *)ALIGN_DOWN(addr, words_per_line * sizeof(*ptr)); |
| 68 |
|
| 69 |
printf("Dumping stack:\n"); |
| 70 |
for (i = bytes / sizeof(*ptr); i >= 0; i -= words_per_line) { |
| 71 |
printf("%p: ", ptr + i); |
| 72 |
for (j = i; j < i + words_per_line; j++) |
| 73 |
printf("%016llx ", *(ptr + j)); |
| 74 |
printf("\n"); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void print_regs(struct exception_state *state) |
| 79 |
{ |
| 80 |
int i; |
| 81 |
|
| 82 |
printf("ELR = 0x%016llx ESR = 0x%08llx\n", |
| 83 |
state->elr, state->esr); |
| 84 |
printf("FAR = 0x%016llx SPSR = 0x%08llx\n", |
| 85 |
raw_read_far_el2(), raw_read_spsr_el2()); |
| 86 |
for (i = 0; i < 30; i += 2) { |
| 87 |
printf("X%02d = 0x%016llx X%02d = 0x%016llx\n", |
| 88 |
i, state->regs[i], i + 1, state->regs[i + 1]); |
| 89 |
} |
| 90 |
printf("X30 = 0x%016llx SP = 0x%016llx\n", |
| 91 |
state->regs[30], state->sp); |
| 92 |
} |
| 93 |
|
| 94 |
void exception_dispatch(struct exception_state *state, int idx); |
| 95 |
void exception_dispatch(struct exception_state *state, int idx) |
| 96 |
{ |
| 97 |
if (idx >= EXC_COUNT) { |
| 98 |
printf("Bad exception index %d.\n", idx); |
| 99 |
} else { |
| 100 |
struct exception_handler_info *info = &exceptions[idx]; |
| 101 |
if (hook && hook(idx)) |
| 102 |
return; |
| 103 |
|
| 104 |
if (info->name) |
| 105 |
printf("exception %s\n", info->name); |
| 106 |
else |
| 107 |
printf("exception _not_used.\n"); |
| 108 |
} |
| 109 |
print_regs(state); |
| 110 |
|
| 111 |
dump_stack(state->sp - 32, 512); |
| 112 |
|
| 113 |
halt(); |
| 114 |
} |
| 115 |
|
| 116 |
void exception_init(void) |
| 117 |
{ |
| 118 |
extern uint64_t exception_table[]; |
| 119 |
raw_write_vbar_el2((uintptr_t)exception_table); |
| 120 |
exception_set_state_ptr(&exception_state); |
| 121 |
} |
| 122 |
|
| 123 |
void exception_install_hook(exception_hook h) |
| 124 |
{ |
| 125 |
die_if(hook, "Implement support for a list of hooks if you need it."); |
| 126 |
hook = h; |
| 127 |
} |