blob: a633e3afb856f1d4ce4deedfc58b0982b10a0843 [file] [log] [blame]
Furquan Shaikh2af76f42014-04-28 16:39:40 -07001/*
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 Durbin4f89d972014-09-16 22:23:57 -050032#include <arch/barrier.h>
Furquan Shaikh2af76f42014-04-28 16:39:40 -070033#include <arch/cache.h>
34#include <arch/exception.h>
Furquan Shaikh4aa76162014-09-08 18:04:18 -070035#include <arch/transition.h>
Furquan Shaikh2af76f42014-04-28 16:39:40 -070036#include <console/console.h>
Furquan Shaikh69761cd2014-08-21 10:31:00 -070037#include <arch/lib_helpers.h>
Furquan Shaikh2af76f42014-04-28 16:39:40 -070038
Aaron Durbin4f89d972014-09-16 22:23:57 -050039static 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 Shaikh69761cd2014-08-21 10:31:00 -070056};
57
Furquan Shaikh4aa76162014-09-08 18:04:18 -070058static void print_regs(struct exc_state *exc_state)
Furquan Shaikh2af76f42014-04-28 16:39:40 -070059{
60 int i;
Furquan Shaikh4aa76162014-09-08 18:04:18 -070061 struct elx_state *elx = &exc_state->elx;
62 struct regs *regs = &exc_state->regs;
Furquan Shaikh2af76f42014-04-28 16:39:40 -070063
Furquan Shaikh4aa76162014-09-08 18:04:18 -070064 uint64_t elx_esr = raw_read_esr_current();
65 uint64_t elx_far = raw_read_far_current();
Furquan Shaikh2af76f42014-04-28 16:39:40 -070066
Furquan Shaikh4aa76162014-09-08 18:04:18 -070067 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 Shaikh69761cd2014-08-21 10:31:00 -070073}
74
Aaron Durbin4f89d972014-09-16 22:23:57 -050075
76static struct exception_handler *handlers[NUM_EXC_VIDS];
77
78
79int exception_handler_register(uint64_t vid, struct exception_handler *h)
Furquan Shaikh69761cd2014-08-21 10:31:00 -070080{
Aaron Durbin4f89d972014-09-16 22:23:57 -050081 if (vid >= NUM_EXC_VIDS)
82 return -1;
Furquan Shaikh69761cd2014-08-21 10:31:00 -070083
Aaron Durbin4f89d972014-09-16 22:23:57 -050084 /* Just place at head of queue. */
85 h->next = handlers[vid];
86 store_release(&handlers[vid], h);
87
88 return 0;
89}
90
91int 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 Shaikh2af76f42014-04-28 16:39:40 -0700107 }
Furquan Shaikh69761cd2014-08-21 10:31:00 -0700108
Aaron Durbin4f89d972014-09-16 22:23:57 -0500109 /* Not found */
110 return -1;
111}
Furquan Shaikh4aa76162014-09-08 18:04:18 -0700112
Aaron Durbin4f89d972014-09-16 22:23:57 -0500113static 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
121static 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
128static 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
146void 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
167static 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 Shaikh2af76f42014-04-28 16:39:40 -0700173}
174
Furquan Shaikh69761cd2014-08-21 10:31:00 -0700175static uint64_t test_exception(void)
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700176{
Aaron Durbin4f89d972014-09-16 22:23:57 -0500177 struct exception_handler sync_elx;
178 struct exception_handler sync_el0;
179 unsigned long long *a = (void *)0xffffffff00000000ULL;
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700180
Aaron Durbin4f89d972014-09-16 22:23:57 -0500181 sync_elx.handler = &test_exception_handler;
182 sync_el0.handler = &test_exception_handler;
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700183
Aaron Durbin4f89d972014-09-16 22:23:57 -0500184 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 Shaikh2af76f42014-04-28 16:39:40 -0700191
Furquan Shaikh69761cd2014-08-21 10:31:00 -0700192 return 0;
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700193}
194
Aaron Durbincc175762014-08-27 16:45:12 -0500195void exception_hwinit(void)
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700196{
Furquan Shaikh4aa76162014-09-08 18:04:18 -0700197 exc_set_vbar();
Aaron Durbincc175762014-08-27 16:45:12 -0500198}
199
200void exception_init(void)
201{
202 /* Load the exception table. */
203 exception_hwinit();
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700204
Furquan Shaikh69761cd2014-08-21 10:31:00 -0700205 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 Shaikh2af76f42014-04-28 16:39:40 -0700210}