Rev 2329 | Rev 2344 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2235 | stepan | 1 | /* |
2179 | stepan | 2 | * Copyright (c) 2007 Petr Stepan |
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - 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 | * - 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 ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup arm32 |
||
30 | * @{ |
||
31 | */ |
||
32 | /** @file |
||
33 | @brief Exception handlers and exception initialization routines. |
||
34 | */ |
||
35 | |||
2329 | kebrt | 36 | |
2179 | stepan | 37 | #include <arch/exception.h> |
2326 | kebrt | 38 | #include <arch/debug/print.h> |
2179 | stepan | 39 | #include <arch/memstr.h> |
2235 | stepan | 40 | #include <arch/regutils.h> |
41 | #include <interrupt.h> |
||
2306 | kebrt | 42 | #include <arch/machine.h> |
2282 | jancik | 43 | #include <arch/mm/page_fault.h> |
2284 | stepan | 44 | #include <print.h> |
2286 | stepan | 45 | #include <syscall/syscall.h> |
2179 | stepan | 46 | |
2329 | kebrt | 47 | |
2306 | kebrt | 48 | #define PREFETCH_OFFSET 0x8 |
49 | #define BRANCH_OPCODE 0xea000000 |
||
50 | #define LDR_OPCODE 0xe59ff000 |
||
51 | #define VALID_BRANCH_MASK 0xff000000 |
||
52 | #define EXC_VECTORS_SIZE 0x20 |
||
53 | #define EXC_VECTORS 0x8 |
||
2179 | stepan | 54 | |
2329 | kebrt | 55 | |
2284 | stepan | 56 | extern uintptr_t supervisor_sp; |
57 | extern uintptr_t exc_stack; |
||
2235 | stepan | 58 | |
2329 | kebrt | 59 | |
2284 | stepan | 60 | inline static void setup_stack_and_save_regs() |
61 | { |
||
62 | asm volatile( "ldr r13, =exc_stack \n\ |
||
63 | stmfd r13!, {r0} \n\ |
||
64 | mrs r0, spsr \n\ |
||
65 | and r0, r0, #0x1f \n\ |
||
66 | cmp r0, #0x10 \n\ |
||
67 | bne 1f \n\ |
||
68 | \n\ |
||
69 | @prev mode was usermode \n\ |
||
70 | ldmfd r13!, {r0} \n\ |
||
71 | ldr r13, =supervisor_sp \n\ |
||
2298 | stepan | 72 | ldr r13, [r13] \n\ |
2286 | stepan | 73 | stmfd r13!, {lr} \n\ |
74 | stmfd r13!, {r0-r12} \n\ |
||
2284 | stepan | 75 | stmfd r13!, {r13, lr}^ \n\ |
76 | mrs r0, spsr \n\ |
||
77 | stmfd r13!, {r0} \n\ |
||
78 | b 2f \n\ |
||
79 | \n\ |
||
80 | @prev mode was not usermode \n\ |
||
81 | 1: \n\ |
||
82 | stmfd r13!, {r1, r2, r3} \n\ |
||
83 | mrs r1, cpsr \n\ |
||
84 | mov r2, lr \n\ |
||
85 | bic r1, r1, #0x1f \n\ |
||
86 | orr r1, r1, r0 \n\ |
||
87 | mrs r0, cpsr \n\ |
||
88 | msr cpsr_c, r1 \n\ |
||
89 | \n\ |
||
90 | mov r3, r13 \n\ |
||
91 | stmfd r13!, {r2} \n\ |
||
2286 | stepan | 92 | mov r2, lr \n\ |
2284 | stepan | 93 | stmfd r13!, {r4-r12} \n\ |
94 | mov r1, r13 \n\ |
||
2298 | stepan | 95 | @following two lines are for debugging \n\ |
96 | mov sp, #0 \n\ |
||
2286 | stepan | 97 | mov lr, #0 \n\ |
2284 | stepan | 98 | msr cpsr_c, r0 \n\ |
99 | \n\ |
||
100 | ldmfd r13!, {r4, r5, r6, r7} \n\ |
||
101 | stmfd r1!, {r4, r5, r6} \n\ |
||
102 | stmfd r1!, {r7} \n\ |
||
103 | stmfd r1!, {r2} \n\ |
||
104 | stmfd r1!, {r3} \n\ |
||
105 | mrs r0, spsr \n\ |
||
106 | stmfd r1!, {r0} \n\ |
||
107 | mov r13, r1 \n\ |
||
108 | 2:" |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | |||
113 | inline static void load_regs() |
||
114 | { |
||
115 | asm volatile( "ldmfd r13!, {r0} \n\ |
||
116 | msr spsr, r0 \n\ |
||
117 | and r0, r0, #0x1f \n\ |
||
118 | cmp r0, #0x10 \n\ |
||
119 | bne 3f \n\ |
||
120 | \n\ |
||
121 | @return to user mode \n\ |
||
122 | ldmfd r13!, {r13, lr}^ \n\ |
||
123 | b 4f \n\ |
||
124 | \n\ |
||
125 | @return to non-user mode \n\ |
||
126 | 3: \n\ |
||
127 | ldmfd r13!, {r1, r2} \n\ |
||
128 | mrs r3, cpsr \n\ |
||
129 | bic r3, r3, #0x1f \n\ |
||
130 | orr r3, r3, r0 \n\ |
||
131 | mrs r0, cpsr \n\ |
||
132 | msr cpsr_c, r3 \n\ |
||
133 | \n\ |
||
134 | mov r13, r1 \n\ |
||
135 | mov lr, r2 \n\ |
||
136 | msr cpsr_c, r0 \n\ |
||
137 | \n\ |
||
138 | @actual return \n\ |
||
2298 | stepan | 139 | 4: ldmfd r13, {r0-r12, pc}^" |
2284 | stepan | 140 | ); |
141 | } |
||
142 | |||
143 | |||
2286 | stepan | 144 | |
145 | /*#define SAVE_REGS_TO_STACK \ |
||
2284 | stepan | 146 | asm("stmfd r13!, {r0-r12, r13, lr}"); \ |
2235 | stepan | 147 | asm("mrs r14, spsr"); \ |
2284 | stepan | 148 | asm("stmfd r13!, {r14}"); |
2286 | stepan | 149 | */ |
2235 | stepan | 150 | |
2284 | stepan | 151 | |
2235 | stepan | 152 | #define CALL_EXC_DISPATCH(exception) \ |
153 | asm("mov r0, %0" : : "i" (exception)); \ |
||
2284 | stepan | 154 | asm("mov r1, r13"); \ |
2235 | stepan | 155 | asm("bl exc_dispatch"); |
156 | |||
2284 | stepan | 157 | |
2235 | stepan | 158 | /**Loads registers from the stack and resets SPSR before exitting exception |
159 | * handler. |
||
2286 | stepan | 160 | |
2235 | stepan | 161 | #define LOAD_REGS_FROM_STACK \ |
2284 | stepan | 162 | asm("ldmfd r13!, {r14}"); \ |
2235 | stepan | 163 | asm("msr spsr, r14"); \ |
2284 | stepan | 164 | asm("ldmfd r13!, {r0-r12, r13, pc}^"); |
2286 | stepan | 165 | */ |
2284 | stepan | 166 | |
2179 | stepan | 167 | |
2235 | stepan | 168 | /** General exception handler. |
169 | * Stores registers, dispatches the exception, |
||
170 | * and finally restores registers and returns from exception processing. |
||
2329 | kebrt | 171 | * |
172 | * @param exception Exception number. |
||
2235 | stepan | 173 | */ |
174 | #define PROCESS_EXCEPTION(exception) \ |
||
2284 | stepan | 175 | setup_stack_and_save_regs(); \ |
2235 | stepan | 176 | CALL_EXC_DISPATCH(exception) \ |
2284 | stepan | 177 | load_regs(); |
2235 | stepan | 178 | |
2284 | stepan | 179 | /* #define PROCESS_EXCEPTION(exception) \ |
180 | SAVE_REGS_TO_STACK \ |
||
181 | CALL_EXC_DISPATCH(exception) \ |
||
182 | LOAD_REGS_FROM_STACK*/ |
||
183 | |||
2329 | kebrt | 184 | |
2235 | stepan | 185 | /** Updates specified exception vector to jump to given handler. |
2329 | kebrt | 186 | * Addresses of handlers are stored in memory following exception vectors. |
2235 | stepan | 187 | */ |
188 | static void install_handler (unsigned handler_addr, unsigned* vector) |
||
189 | { |
||
190 | /* relative address (related to exc. vector) of the word |
||
191 | * where handler's address is stored |
||
192 | */ |
||
193 | volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE - PREFETCH_OFFSET; |
||
2179 | stepan | 194 | |
2235 | stepan | 195 | /* make it LDR instruction and store at exception vector */ |
196 | *vector = handler_address_ptr | LDR_OPCODE; |
||
2179 | stepan | 197 | |
2235 | stepan | 198 | /* store handler's address */ |
199 | *(vector + EXC_VECTORS) = handler_addr; |
||
2284 | stepan | 200 | |
2179 | stepan | 201 | } |
202 | |||
2284 | stepan | 203 | |
2329 | kebrt | 204 | /** Low-level Reset Exception handler. */ |
2235 | stepan | 205 | static void reset_exception_entry() |
206 | { |
||
207 | PROCESS_EXCEPTION(EXC_RESET); |
||
2179 | stepan | 208 | } |
209 | |||
2329 | kebrt | 210 | |
211 | /** Low-level Software Interrupt Exception handler. */ |
||
2235 | stepan | 212 | static void swi_exception_entry() |
213 | { |
||
214 | PROCESS_EXCEPTION(EXC_SWI); |
||
2179 | stepan | 215 | } |
216 | |||
2329 | kebrt | 217 | |
218 | /** Low-level Undefined Instruction Exception handler. */ |
||
2235 | stepan | 219 | static void undef_instr_exception_entry() |
220 | { |
||
221 | PROCESS_EXCEPTION(EXC_UNDEF_INSTR); |
||
222 | } |
||
223 | |||
2329 | kebrt | 224 | |
225 | /** Low-level Fast Interrupt Exception handler. */ |
||
2235 | stepan | 226 | static void fiq_exception_entry() |
227 | { |
||
228 | PROCESS_EXCEPTION(EXC_FIQ); |
||
229 | } |
||
230 | |||
2329 | kebrt | 231 | |
232 | /** Low-level Prefetch Abort Exception handler. */ |
||
2235 | stepan | 233 | static void prefetch_abort_exception_entry() |
234 | { |
||
235 | asm("sub lr, lr, #4"); |
||
236 | PROCESS_EXCEPTION(EXC_PREFETCH_ABORT); |
||
237 | } |
||
238 | |||
2329 | kebrt | 239 | |
240 | /** Low-level Data Abort Exception handler. */ |
||
2235 | stepan | 241 | static void data_abort_exception_entry() |
242 | { |
||
243 | asm("sub lr, lr, #8"); |
||
244 | PROCESS_EXCEPTION(EXC_DATA_ABORT); |
||
245 | } |
||
246 | |||
247 | |||
2329 | kebrt | 248 | /** Low-level Interrupt Exception handler. */ |
2235 | stepan | 249 | static void irq_exception_entry() |
250 | { |
||
251 | asm("sub lr, lr, #4"); |
||
252 | PROCESS_EXCEPTION(EXC_IRQ); |
||
253 | } |
||
254 | |||
2329 | kebrt | 255 | |
2286 | stepan | 256 | /** Software Interrupt handler. |
257 | * |
||
258 | * Dispatches the syscall. |
||
259 | */ |
||
2304 | kebrt | 260 | static void swi_exception(int exc_no, istate_t *istate) |
2284 | stepan | 261 | { |
2341 | kebrt | 262 | /* |
2304 | kebrt | 263 | dprintf("SYSCALL: r0-r4: %x, %x, %x, %x, %x; pc: %x\n", istate->r0, |
2298 | stepan | 264 | istate->r1, istate->r2, istate->r3, istate->r4, istate->pc); |
2341 | kebrt | 265 | */ |
2298 | stepan | 266 | |
2286 | stepan | 267 | istate->r0 = syscall_handler( |
268 | istate->r0, |
||
269 | istate->r1, |
||
270 | istate->r2, |
||
271 | istate->r3, |
||
272 | istate->r4); |
||
2284 | stepan | 273 | } |
274 | |||
2329 | kebrt | 275 | |
2235 | stepan | 276 | /** Interrupt Exception handler. |
2286 | stepan | 277 | * |
2235 | stepan | 278 | * Determines the sources of interrupt, and calls their handlers. |
279 | */ |
||
2304 | kebrt | 280 | static void irq_exception(int exc_no, istate_t *istate) |
2235 | stepan | 281 | { |
2306 | kebrt | 282 | machine_irq_exception(exc_no, istate); |
2235 | stepan | 283 | } |
284 | |||
2329 | kebrt | 285 | |
286 | /** Fills exception vectors with appropriate exception handlers. */ |
||
2235 | stepan | 287 | void install_exception_handlers(void) |
288 | { |
||
289 | install_handler((unsigned)reset_exception_entry, |
||
290 | (unsigned*)EXC_RESET_VEC); |
||
291 | |||
292 | install_handler((unsigned)undef_instr_exception_entry, |
||
293 | (unsigned*)EXC_UNDEF_INSTR_VEC); |
||
294 | |||
295 | install_handler((unsigned)swi_exception_entry, |
||
296 | (unsigned*)EXC_SWI_VEC); |
||
297 | |||
298 | install_handler((unsigned)prefetch_abort_exception_entry, |
||
299 | (unsigned*)EXC_PREFETCH_ABORT_VEC); |
||
300 | |||
301 | install_handler((unsigned)data_abort_exception_entry, |
||
302 | (unsigned*)EXC_DATA_ABORT_VEC); |
||
303 | |||
2284 | stepan | 304 | install_handler((unsigned)irq_exception_entry, |
2235 | stepan | 305 | (unsigned*)EXC_IRQ_VEC); |
306 | |||
307 | install_handler((unsigned)fiq_exception_entry, |
||
308 | (unsigned*)EXC_FIQ_VEC); |
||
2179 | stepan | 309 | } |
310 | |||
2329 | kebrt | 311 | |
2284 | stepan | 312 | #ifdef HIGH_EXCEPTION_VECTORS |
2329 | kebrt | 313 | /** Activates use of high exception vectors addresses. */ |
314 | static void high_vectors() |
||
2262 | stepan | 315 | { |
316 | uint32_t control_reg; |
||
317 | |||
318 | asm volatile( "mrc p15, 0, %0, c1, c1": "=r" (control_reg)); |
||
319 | |||
320 | //switch on the high vectors bit |
||
321 | control_reg |= CP15_R1_HIGH_VECTORS_BIT; |
||
322 | |||
323 | asm volatile( "mcr p15, 0, %0, c1, c1" : : "r" (control_reg)); |
||
324 | } |
||
2284 | stepan | 325 | #endif |
2262 | stepan | 326 | |
2326 | kebrt | 327 | |
2245 | stepan | 328 | /** Initializes exception handling. |
329 | * |
||
330 | * Installs low-level exception handlers and then registers |
||
331 | * exceptions and their handlers to kernel exception dispatcher. |
||
332 | */ |
||
2235 | stepan | 333 | void exception_init(void) |
334 | { |
||
2262 | stepan | 335 | #ifdef HIGH_EXCEPTION_VECTORS |
336 | high_vectors(); |
||
337 | #endif |
||
2245 | stepan | 338 | install_exception_handlers(); |
339 | |||
2235 | stepan | 340 | exc_register(EXC_IRQ, "interrupt", (iroutine) irq_exception); |
2277 | jancik | 341 | exc_register(EXC_PREFETCH_ABORT, "prefetch abort", (iroutine) prefetch_abort); |
342 | exc_register(EXC_DATA_ABORT, "data abort", (iroutine) data_abort); |
||
2284 | stepan | 343 | exc_register(EXC_SWI, "software interrupt", (iroutine) swi_exception); |
344 | /* TODO add next */ |
||
2179 | stepan | 345 | } |
346 | |||
2326 | kebrt | 347 | |
2329 | kebrt | 348 | /** Sets stack pointers in all supported exception modes. */ |
2235 | stepan | 349 | void setup_exception_stacks() |
350 | { |
||
2284 | stepan | 351 | /* switch to particular mode and set "r13" there */ |
2262 | stepan | 352 | |
2284 | stepan | 353 | uint32_t cspr = current_status_reg_read(); |
2262 | stepan | 354 | |
2284 | stepan | 355 | /* IRQ stack */ |
356 | current_status_reg_control_write( |
||
357 | (cspr & ~STATUS_REG_MODE_MASK) | IRQ_MODE |
||
358 | ); |
||
359 | asm("ldr r13, =exc_stack"); |
||
2262 | stepan | 360 | |
2284 | stepan | 361 | /* abort stack */ |
362 | current_status_reg_control_write( |
||
363 | (cspr & ~STATUS_REG_MODE_MASK) | ABORT_MODE |
||
364 | ); |
||
365 | asm("ldr r13, =exc_stack"); |
||
2262 | stepan | 366 | |
2284 | stepan | 367 | /* TODO if you want to test other exceptions than IRQ, |
368 | make stack analogous to irq_stack (in start.S), |
||
369 | and then set stack pointer here */ |
||
2262 | stepan | 370 | |
2286 | stepan | 371 | current_status_reg_control_write(cspr); |
2235 | stepan | 372 | } |
373 | |||
2304 | kebrt | 374 | |
2326 | kebrt | 375 | /** Prints #istate_t structure content. |
376 | * |
||
377 | * @param istate Structure to be printed. |
||
378 | */ |
||
2304 | kebrt | 379 | void print_istate(istate_t *istate) |
380 | { |
||
381 | dprintf("istate dump:\n"); |
||
382 | |||
383 | dprintf(" r0: %x r1: %x r2: %x r3: %x\n", |
||
384 | istate->r0, istate->r1, istate->r2, istate->r3); |
||
385 | dprintf(" r4: %x r5: %x r6: %x r7: %x\n", |
||
386 | istate->r4, istate->r5, istate->r6, istate->r7); |
||
387 | dprintf(" r8: %x r8: %x r10: %x r11: %x\n", |
||
388 | istate->r8, istate->r9, istate->r10, istate->r11); |
||
389 | dprintf(" r12: %x sp: %x lr: %x spsr: %x\n", |
||
390 | istate->r12, istate->sp, istate->lr, istate->spsr); |
||
391 | |||
392 | dprintf(" pc: %x\n", istate->pc); |
||
393 | } |
||
394 | |||
395 | |||
2179 | stepan | 396 | /** @} |
397 | */ |