| 1 |
|
| 2 |
|
| 3 |
#include <types.h> |
| 4 |
#include <arch/barrier.h> |
| 5 |
#include <arch/exception.h> |
| 6 |
#include <arch/transition.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <console/uart.h> |
| 9 |
#include <arch/lib_helpers.h> |
| 10 |
|
| 11 |
uint8_t exception_stack[2*KiB] __attribute__((aligned(16))); |
| 12 |
|
| 13 |
static const char *exception_names[NUM_EXC_VIDS] = { |
| 14 |
[EXC_VID_CUR_SP_EL0_SYNC] = "_sync_sp_el0", |
| 15 |
[EXC_VID_CUR_SP_EL0_IRQ] = "_irq_sp_el0", |
| 16 |
[EXC_VID_CUR_SP_EL0_FIRQ] = "_fiq_sp_el0", |
| 17 |
[EXC_VID_CUR_SP_EL0_SERR] = "_serror_sp_el0", |
| 18 |
[EXC_VID_CUR_SP_ELX_SYNC] = "_sync_sp_el3", |
| 19 |
[EXC_VID_CUR_SP_ELX_IRQ] = "_irq_sp_el3", |
| 20 |
[EXC_VID_CUR_SP_ELX_FIQ] = "_fiq_sp_el3", |
| 21 |
[EXC_VID_CUR_SP_ELX_SERR] = "_serror_sp_el3", |
| 22 |
[EXC_VID_LOW64_SYNC] = "_sync_elx_64", |
| 23 |
[EXC_VID_LOW64_IRQ] = "_irq_elx_64", |
| 24 |
[EXC_VID_LOW64_FIQ] = "_fiq_elx_64", |
| 25 |
[EXC_VID_LOW64_SERR] = "_serror_elx_64", |
| 26 |
[EXC_VID_LOW32_SYNC] = "_sync_elx_32", |
| 27 |
[EXC_VID_LOW32_IRQ] = "_irq_elx_32", |
| 28 |
[EXC_VID_LOW32_FIQ] = "_fiq_elx_32", |
| 29 |
[EXC_VID_LOW32_SERR] = "_serror_elx_32" |
| 30 |
}; |
| 31 |
|
| 32 |
static void dump_stack(uintptr_t addr, size_t bytes) |
| 33 |
{ |
| 34 |
int i, j; |
| 35 |
const int words_per_line = 8; |
| 36 |
uint64_t *ptr = (void *)ALIGN_DOWN(addr, words_per_line * sizeof(*ptr)); |
| 37 |
|
| 38 |
printk(BIOS_DEBUG, "Dumping stack:\n"); |
| 39 |
for (i = bytes / sizeof(*ptr); i >= 0; i -= words_per_line) { |
| 40 |
printk(BIOS_DEBUG, "%p: ", ptr + i); |
| 41 |
for (j = i; j < i + words_per_line; j++) |
| 42 |
printk(BIOS_DEBUG, "%016llx ", *(ptr + j)); |
| 43 |
printk(BIOS_DEBUG, "\n"); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
static void print_regs(struct exc_state *exc_state) |
| 48 |
{ |
| 49 |
int i; |
| 50 |
struct elx_state *elx = &exc_state->elx; |
| 51 |
struct regs *regs = &exc_state->regs; |
| 52 |
|
| 53 |
printk(BIOS_DEBUG, "ELR = 0x%016llx ESR = 0x%08llx\n", |
| 54 |
elx->elr, raw_read_esr()); |
| 55 |
printk(BIOS_DEBUG, "FAR = 0x%016llx SPSR = 0x%08llx\n", |
| 56 |
raw_read_far(), raw_read_spsr()); |
| 57 |
|
| 58 |
for (i = 0; i < 30; i += 2) { |
| 59 |
printk(BIOS_DEBUG, |
| 60 |
"X%02d = 0x%016llx X%02d = 0x%016llx\n", |
| 61 |
i, regs->x[i], i + 1, regs->x[i + 1]); |
| 62 |
} |
| 63 |
printk(BIOS_DEBUG, "X30 = 0x%016llx SP = 0x%016llx\n", |
| 64 |
regs->x[30], regs->sp); |
| 65 |
} |
| 66 |
|
| 67 |
static struct exception_handler *handlers[NUM_EXC_VIDS]; |
| 68 |
|
| 69 |
enum cb_err exception_handler_register(uint64_t vid, struct exception_handler *h) |
| 70 |
{ |
| 71 |
if (vid >= NUM_EXC_VIDS) |
| 72 |
return CB_ERR; |
| 73 |
|
| 74 |
|
| 75 |
h->next = handlers[vid]; |
| 76 |
store_release(&handlers[vid], h); |
| 77 |
|
| 78 |
return CB_SUCCESS; |
| 79 |
} |
| 80 |
|
| 81 |
enum cb_err exception_handler_unregister(uint64_t vid, struct exception_handler *h) |
| 82 |
{ |
| 83 |
struct exception_handler *cur; |
| 84 |
struct exception_handler **prev; |
| 85 |
|
| 86 |
if (vid >= NUM_EXC_VIDS) |
| 87 |
return CB_ERR; |
| 88 |
|
| 89 |
prev = &handlers[vid]; |
| 90 |
|
| 91 |
for (cur = handlers[vid]; cur != NULL; cur = cur->next) { |
| 92 |
if (cur != h) |
| 93 |
continue; |
| 94 |
|
| 95 |
store_release(prev, cur->next); |
| 96 |
return CB_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
return CB_ERR; |
| 101 |
} |
| 102 |
|
| 103 |
static void print_exception_info(struct exc_state *state, uint64_t idx) |
| 104 |
{ |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
__uart_tx_byte('\r'); |
| 111 |
__uart_tx_byte('\n'); |
| 112 |
__uart_tx_byte('!'); |
| 113 |
__uart_tx_byte('E'); |
| 114 |
__uart_tx_byte('X'); |
| 115 |
__uart_tx_byte('C'); |
| 116 |
__uart_tx_byte('E'); |
| 117 |
__uart_tx_byte('P'); |
| 118 |
__uart_tx_byte('T'); |
| 119 |
__uart_tx_byte('!'); |
| 120 |
|
| 121 |
printk(BIOS_DEBUG, "\nexception %s\n", |
| 122 |
idx < NUM_EXC_VIDS ? exception_names[idx] : "_unknown"); |
| 123 |
|
| 124 |
print_regs(state); |
| 125 |
|
| 126 |
dump_stack(state->regs.sp - 32, 512); |
| 127 |
} |
| 128 |
|
| 129 |
static void print_exception_and_die(struct exc_state *state, uint64_t idx) |
| 130 |
{ |
| 131 |
print_exception_info(state, idx); |
| 132 |
die("exception death"); |
| 133 |
} |
| 134 |
|
| 135 |
static int handle_exception(struct exc_state *state, uint64_t idx) |
| 136 |
{ |
| 137 |
int ret = EXC_RET_ABORT; |
| 138 |
|
| 139 |
struct exception_handler *h; |
| 140 |
|
| 141 |
for (h = handlers[idx]; h != NULL; h = h->next) { |
| 142 |
int hret; |
| 143 |
|
| 144 |
hret = h->handler(state, idx); |
| 145 |
|
| 146 |
if (hret > ret) |
| 147 |
ret = hret; |
| 148 |
} |
| 149 |
|
| 150 |
return ret; |
| 151 |
} |
| 152 |
|
| 153 |
void exc_dispatch(struct exc_state *state, uint64_t idx) |
| 154 |
{ |
| 155 |
int ret; |
| 156 |
|
| 157 |
if (idx >= NUM_EXC_VIDS) { |
| 158 |
printk(BIOS_DEBUG, "Bad exception index %x.\n", (int)idx); |
| 159 |
print_exception_and_die(state, idx); |
| 160 |
} |
| 161 |
|
| 162 |
ret = handle_exception(state, idx); |
| 163 |
|
| 164 |
if (ret == EXC_RET_ABORT) |
| 165 |
print_exception_and_die(state, idx); |
| 166 |
|
| 167 |
if (ret == EXC_RET_IGNORED || ret == EXC_RET_HANDLED_DUMP_STATE) |
| 168 |
print_exception_info(state, idx); |
| 169 |
|
| 170 |
exc_exit(&state->regs); |
| 171 |
} |
| 172 |
|
| 173 |
static int test_exception_handler(struct exc_state *state, uint64_t vector_id) |
| 174 |
{ |
| 175 |
|
| 176 |
state->elx.elr += sizeof(uint32_t); |
| 177 |
raw_write_elr(state->elx.elr); |
| 178 |
|
| 179 |
return EXC_RET_HANDLED; |
| 180 |
} |
| 181 |
|
| 182 |
static uint64_t test_exception(void) |
| 183 |
{ |
| 184 |
struct exception_handler sync_elx; |
| 185 |
struct exception_handler sync_el0; |
| 186 |
unsigned long long *a = (void *)0xffffffff00000000ULL; |
| 187 |
|
| 188 |
sync_elx.handler = &test_exception_handler; |
| 189 |
sync_el0.handler = &test_exception_handler; |
| 190 |
|
| 191 |
exception_handler_register(EXC_VID_CUR_SP_ELX_SYNC, &sync_elx); |
| 192 |
exception_handler_register(EXC_VID_CUR_SP_EL0_SYNC, &sync_el0); |
| 193 |
|
| 194 |
force_read(*a); |
| 195 |
|
| 196 |
exception_handler_unregister(EXC_VID_CUR_SP_ELX_SYNC, &sync_elx); |
| 197 |
exception_handler_unregister(EXC_VID_CUR_SP_EL0_SYNC, &sync_el0); |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
void exception_init(void) |
| 203 |
{ |
| 204 |
|
| 205 |
exception_init_asm(exception_stack + ARRAY_SIZE(exception_stack)); |
| 206 |
|
| 207 |
printk(BIOS_DEBUG, "ARM64: Exception handlers installed.\n"); |
| 208 |
|
| 209 |
|
| 210 |
if (CONFIG(FATAL_ASSERTS)) { |
| 211 |
printk(BIOS_DEBUG, "ARM64: Testing exception\n"); |
| 212 |
test_exception(); |
| 213 |
printk(BIOS_DEBUG, "ARM64: Done test exception\n"); |
| 214 |
} |
| 215 |
} |