Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright 2013 Google Inc. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <types.h> |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 32 | #include <arch/barrier.h> |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 33 | #include <arch/cache.h> |
| 34 | #include <arch/exception.h> |
Furquan Shaikh | 4aa7616 | 2014-09-08 18:04:18 -0700 | [diff] [blame] | 35 | #include <arch/transition.h> |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 36 | #include <console/console.h> |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 37 | #include <arch/lib_helpers.h> |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 38 | |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 39 | static const char *exception_names[NUM_EXC_VIDS] = { |
| 40 | [EXC_VID_CUR_SP_EL0_SYNC] = "_sync_sp_el0", |
| 41 | [EXC_VID_CUR_SP_EL0_IRQ] = "_irq_sp_el0", |
| 42 | [EXC_VID_CUR_SP_EL0_FIRQ] = "_fiq_sp_el0", |
| 43 | [EXC_VID_CUR_SP_EL0_SERR] = "_serror_sp_el0", |
| 44 | [EXC_VID_CUR_SP_ELX_SYNC] = "_sync_sp_el3", |
| 45 | [EXC_VID_CUR_SP_ELX_IRQ] = "_irq_sp_el3", |
| 46 | [EXC_VID_CUR_SP_ELX_FIQ] = "_fiq_sp_el3", |
| 47 | [EXC_VID_CUR_SP_ELX_SERR] = "_serror_sp_el3", |
| 48 | [EXC_VID_LOW64_SYNC] = "_sync_elx_64", |
| 49 | [EXC_VID_LOW64_IRQ] = "_irq_elx_64", |
| 50 | [EXC_VID_LOW64_FIQ] = "_fiq_elx_64", |
| 51 | [EXC_VID_LOW64_SERR] = "_serror_elx_64", |
| 52 | [EXC_VID_LOW32_SYNC] = "_sync_elx_32", |
| 53 | [EXC_VID_LOW32_IRQ] = "_irq_elx_32", |
| 54 | [EXC_VID_LOW32_FIQ] = "_fiq_elx_32", |
| 55 | [EXC_VID_LOW32_SERR] = "_serror_elx_32" |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Furquan Shaikh | 4aa7616 | 2014-09-08 18:04:18 -0700 | [diff] [blame] | 58 | static void print_regs(struct exc_state *exc_state) |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 59 | { |
| 60 | int i; |
Furquan Shaikh | 4aa7616 | 2014-09-08 18:04:18 -0700 | [diff] [blame] | 61 | struct elx_state *elx = &exc_state->elx; |
| 62 | struct regs *regs = &exc_state->regs; |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 63 | |
Furquan Shaikh | 4aa7616 | 2014-09-08 18:04:18 -0700 | [diff] [blame] | 64 | uint64_t elx_esr = raw_read_esr_current(); |
| 65 | uint64_t elx_far = raw_read_far_current(); |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 66 | |
Furquan Shaikh | 4aa7616 | 2014-09-08 18:04:18 -0700 | [diff] [blame] | 67 | printk(BIOS_DEBUG, "ELR = 0x%016llx\n", elx->elr); |
| 68 | printk(BIOS_DEBUG, "ESR = 0x%016llx\n", elx_esr); |
| 69 | printk(BIOS_DEBUG, "SPSR = 0x%08llx\n", elx->spsr); |
| 70 | printk(BIOS_DEBUG, "FAR = 0x%016llx\n", elx_far); |
| 71 | for (i = X0_INDEX; i < XMAX_INDEX; i++) |
| 72 | printk(BIOS_DEBUG, "X%02d = 0x%016llx\n", i, regs->x[i]); |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 75 | |
| 76 | static struct exception_handler *handlers[NUM_EXC_VIDS]; |
| 77 | |
| 78 | |
| 79 | int exception_handler_register(uint64_t vid, struct exception_handler *h) |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 80 | { |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 81 | if (vid >= NUM_EXC_VIDS) |
| 82 | return -1; |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 83 | |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 84 | /* Just place at head of queue. */ |
| 85 | h->next = handlers[vid]; |
| 86 | store_release(&handlers[vid], h); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int exception_handler_unregister(uint64_t vid, struct exception_handler *h) |
| 92 | { |
| 93 | struct exception_handler *cur; |
| 94 | struct exception_handler **prev; |
| 95 | |
| 96 | if (vid >= NUM_EXC_VIDS) |
| 97 | return -1; |
| 98 | |
| 99 | prev = &handlers[vid]; |
| 100 | |
| 101 | for (cur = handlers[vid]; cur != NULL; cur = cur->next) { |
| 102 | if (cur != h) |
| 103 | continue; |
| 104 | /* Update previous pointer. */ |
| 105 | store_release(prev, cur->next); |
| 106 | return 0; |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 107 | } |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 108 | |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 109 | /* Not found */ |
| 110 | return -1; |
| 111 | } |
Furquan Shaikh | 4aa7616 | 2014-09-08 18:04:18 -0700 | [diff] [blame] | 112 | |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 113 | static void print_exception_info(struct exc_state *state, uint64_t idx) |
| 114 | { |
| 115 | if (idx < NUM_EXC_VIDS) |
| 116 | printk(BIOS_DEBUG, "exception %s\n", exception_names[idx]); |
| 117 | |
| 118 | print_regs(state); |
| 119 | } |
| 120 | |
| 121 | static void print_exception_and_die(struct exc_state *state, uint64_t idx) |
| 122 | { |
| 123 | print_exception_info(state, idx); |
| 124 | die("exception death"); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static int handle_exception(struct exc_state *state, uint64_t idx) |
| 129 | { |
| 130 | int ret = EXC_RET_ABORT; |
| 131 | |
| 132 | struct exception_handler *h; |
| 133 | |
| 134 | for (h = handlers[idx]; h != NULL; h = h->next) { |
| 135 | int hret; |
| 136 | |
| 137 | hret = h->handler(state, idx); |
| 138 | |
| 139 | if (hret > ret) |
| 140 | ret = hret; |
| 141 | } |
| 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | void exc_dispatch(struct exc_state *state, uint64_t idx) |
| 147 | { |
| 148 | int ret; |
| 149 | |
| 150 | if (idx >= NUM_EXC_VIDS) { |
| 151 | printk(BIOS_DEBUG, "Bad exception index %x.\n", (int)idx); |
| 152 | print_exception_and_die(state, idx); |
| 153 | } |
| 154 | |
| 155 | ret = handle_exception(state, idx); |
| 156 | |
| 157 | if (ret == EXC_RET_ABORT) |
| 158 | print_exception_and_die(state, idx); |
| 159 | |
| 160 | if (ret == EXC_RET_IGNORED || ret == EXC_RET_HANDLED_DUMP_STATE) |
| 161 | print_exception_info(state, idx); |
| 162 | |
| 163 | exc_exit(&state->regs); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | static int test_exception_handler(struct exc_state *state, uint64_t vector_id) |
| 168 | { |
| 169 | /* Update instruction pointer to next instrution. */ |
| 170 | state->elx.elr += sizeof(uint32_t); |
| 171 | raw_write_elr_current(state->elx.elr); |
| 172 | return EXC_RET_HANDLED; |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 175 | static uint64_t test_exception(void) |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 176 | { |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 177 | struct exception_handler sync_elx; |
| 178 | struct exception_handler sync_el0; |
| 179 | unsigned long long *a = (void *)0xffffffff00000000ULL; |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 180 | |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 181 | sync_elx.handler = &test_exception_handler; |
| 182 | sync_el0.handler = &test_exception_handler; |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 183 | |
Aaron Durbin | 4f89d97 | 2014-09-16 22:23:57 -0500 | [diff] [blame] | 184 | exception_handler_register(EXC_VID_CUR_SP_ELX_SYNC, &sync_elx); |
| 185 | exception_handler_register(EXC_VID_CUR_SP_EL0_SYNC, &sync_el0); |
| 186 | |
| 187 | force_read(*a); |
| 188 | |
| 189 | exception_handler_unregister(EXC_VID_CUR_SP_ELX_SYNC, &sync_elx); |
| 190 | exception_handler_unregister(EXC_VID_CUR_SP_EL0_SYNC, &sync_el0); |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 191 | |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 192 | return 0; |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Aaron Durbin | cc17576 | 2014-08-27 16:45:12 -0500 | [diff] [blame] | 195 | void exception_hwinit(void) |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 196 | { |
Furquan Shaikh | 4aa7616 | 2014-09-08 18:04:18 -0700 | [diff] [blame] | 197 | exc_set_vbar(); |
Aaron Durbin | cc17576 | 2014-08-27 16:45:12 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void exception_init(void) |
| 201 | { |
| 202 | /* Load the exception table. */ |
| 203 | exception_hwinit(); |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 204 | |
Furquan Shaikh | 69761cd | 2014-08-21 10:31:00 -0700 | [diff] [blame] | 205 | printk(BIOS_DEBUG, "ARM64: Exception handlers installed.\n"); |
| 206 | |
| 207 | printk(BIOS_DEBUG, "ARM64: Testing exception\n"); |
| 208 | test_exception(); |
| 209 | printk(BIOS_DEBUG, "ARM64: Done test exception\n"); |
Furquan Shaikh | 2af76f4 | 2014-04-28 16:39:40 -0700 | [diff] [blame] | 210 | } |