Rev 4117 | 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 |
||
| 2410 | stepan | 33 | * @brief Exception handlers and exception initialization routines. |
| 2179 | stepan | 34 | */ |
| 35 | |||
| 36 | #include <arch/exception.h> |
||
| 37 | #include <arch/memstr.h> |
||
| 2235 | stepan | 38 | #include <arch/regutils.h> |
| 39 | #include <interrupt.h> |
||
| 2282 | jancik | 40 | #include <arch/mm/page_fault.h> |
| 3135 | jermar | 41 | #include <arch/barrier.h> |
| 4669 | pillai | 42 | #include <arch/machine.h> |
| 2284 | stepan | 43 | #include <print.h> |
| 2286 | stepan | 44 | #include <syscall/syscall.h> |
| 2179 | stepan | 45 | |
| 2407 | stepan | 46 | /** Offset used in calculation of exception handler's relative address. |
| 47 | * |
||
| 48 | * @see install_handler() |
||
| 49 | */ |
||
| 50 | #define PREFETCH_OFFSET 0x8 |
||
| 2329 | kebrt | 51 | |
| 2407 | stepan | 52 | /** LDR instruction's code */ |
| 2306 | kebrt | 53 | #define LDR_OPCODE 0xe59ff000 |
| 2179 | stepan | 54 | |
| 2407 | stepan | 55 | /** Number of exception vectors. */ |
| 56 | #define EXC_VECTORS 8 |
||
| 2329 | kebrt | 57 | |
| 2407 | stepan | 58 | /** Size of memory block occupied by exception vectors. */ |
| 59 | #define EXC_VECTORS_SIZE (EXC_VECTORS * 4) |
||
| 60 | |||
| 2235 | stepan | 61 | /** Updates specified exception vector to jump to given handler. |
| 2355 | stepan | 62 | * |
| 2329 | kebrt | 63 | * Addresses of handlers are stored in memory following exception vectors. |
| 2235 | stepan | 64 | */ |
| 3135 | jermar | 65 | static void install_handler(unsigned handler_addr, unsigned *vector) |
| 2235 | stepan | 66 | { |
| 67 | /* relative address (related to exc. vector) of the word |
||
| 68 | * where handler's address is stored |
||
| 69 | */ |
||
| 2611 | jermar | 70 | volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE - |
| 71 | PREFETCH_OFFSET; |
||
| 2179 | stepan | 72 | |
| 2235 | stepan | 73 | /* make it LDR instruction and store at exception vector */ |
| 74 | *vector = handler_address_ptr | LDR_OPCODE; |
||
| 3135 | jermar | 75 | smc_coherence(*vector); |
| 2179 | stepan | 76 | |
| 2235 | stepan | 77 | /* store handler's address */ |
| 78 | *(vector + EXC_VECTORS) = handler_addr; |
||
| 2284 | stepan | 79 | |
| 2179 | stepan | 80 | } |
| 81 | |||
| 2286 | stepan | 82 | /** Software Interrupt handler. |
| 83 | * |
||
| 84 | * Dispatches the syscall. |
||
| 85 | */ |
||
| 2304 | kebrt | 86 | static void swi_exception(int exc_no, istate_t *istate) |
| 2284 | stepan | 87 | { |
| 2464 | jermar | 88 | istate->r0 = syscall_handler(istate->r0, istate->r1, istate->r2, |
| 2611 | jermar | 89 | istate->r3, istate->r4, istate->r5, istate->r6); |
| 2284 | stepan | 90 | } |
| 91 | |||
| 2329 | kebrt | 92 | /** Fills exception vectors with appropriate exception handlers. */ |
| 2235 | stepan | 93 | void install_exception_handlers(void) |
| 94 | { |
||
| 2464 | jermar | 95 | install_handler((unsigned) reset_exception_entry, |
| 96 | (unsigned *) EXC_RESET_VEC); |
||
| 2235 | stepan | 97 | |
| 2464 | jermar | 98 | install_handler((unsigned) undef_instr_exception_entry, |
| 99 | (unsigned *) EXC_UNDEF_INSTR_VEC); |
||
| 2235 | stepan | 100 | |
| 2464 | jermar | 101 | install_handler((unsigned) swi_exception_entry, |
| 102 | (unsigned *) EXC_SWI_VEC); |
||
| 2235 | stepan | 103 | |
| 2464 | jermar | 104 | install_handler((unsigned) prefetch_abort_exception_entry, |
| 105 | (unsigned *) EXC_PREFETCH_ABORT_VEC); |
||
| 2235 | stepan | 106 | |
| 2464 | jermar | 107 | install_handler((unsigned) data_abort_exception_entry, |
| 108 | (unsigned *) EXC_DATA_ABORT_VEC); |
||
| 2235 | stepan | 109 | |
| 2464 | jermar | 110 | install_handler((unsigned) irq_exception_entry, |
| 111 | (unsigned *) EXC_IRQ_VEC); |
||
| 2235 | stepan | 112 | |
| 4117 | decky | 113 | install_handler((unsigned) fiq_exception_entry, |
| 2464 | jermar | 114 | (unsigned *) EXC_FIQ_VEC); |
| 2179 | stepan | 115 | } |
| 116 | |||
| 2284 | stepan | 117 | #ifdef HIGH_EXCEPTION_VECTORS |
| 2329 | kebrt | 118 | /** Activates use of high exception vectors addresses. */ |
| 2464 | jermar | 119 | static void high_vectors(void) |
| 2262 | stepan | 120 | { |
| 121 | uint32_t control_reg; |
||
| 122 | |||
| 4018 | decky | 123 | asm volatile ( |
| 124 | "mrc p15, 0, %[control_reg], c1, c1" |
||
| 125 | : [control_reg] "=r" (control_reg) |
||
| 126 | ); |
||
| 2262 | stepan | 127 | |
| 2464 | jermar | 128 | /* switch on the high vectors bit */ |
| 2262 | stepan | 129 | control_reg |= CP15_R1_HIGH_VECTORS_BIT; |
| 130 | |||
| 4018 | decky | 131 | asm volatile ( |
| 132 | "mcr p15, 0, %[control_reg], c1, c1" |
||
| 133 | :: [control_reg] "r" (control_reg) |
||
| 134 | ); |
||
| 2262 | stepan | 135 | } |
| 2284 | stepan | 136 | #endif |
| 2262 | stepan | 137 | |
| 4669 | pillai | 138 | /** Interrupt Exception handler. |
| 139 | * |
||
| 140 | * Determines the sources of interrupt and calls their handlers. |
||
| 141 | */ |
||
| 142 | static void irq_exception(int exc_no, istate_t *istate) |
||
| 143 | { |
||
| 144 | machine_irq_exception(exc_no, istate); |
||
| 145 | } |
||
| 146 | |||
| 2245 | stepan | 147 | /** Initializes exception handling. |
| 4018 | decky | 148 | * |
| 2245 | stepan | 149 | * Installs low-level exception handlers and then registers |
| 150 | * exceptions and their handlers to kernel exception dispatcher. |
||
| 151 | */ |
||
| 2235 | stepan | 152 | void exception_init(void) |
| 153 | { |
||
| 2262 | stepan | 154 | #ifdef HIGH_EXCEPTION_VECTORS |
| 155 | high_vectors(); |
||
| 156 | #endif |
||
| 2245 | stepan | 157 | install_exception_handlers(); |
| 158 | |||
| 2235 | stepan | 159 | exc_register(EXC_IRQ, "interrupt", (iroutine) irq_exception); |
| 2464 | jermar | 160 | exc_register(EXC_PREFETCH_ABORT, "prefetch abort", |
| 161 | (iroutine) prefetch_abort); |
||
| 2277 | jancik | 162 | exc_register(EXC_DATA_ABORT, "data abort", (iroutine) data_abort); |
| 2284 | stepan | 163 | exc_register(EXC_SWI, "software interrupt", (iroutine) swi_exception); |
| 2179 | stepan | 164 | } |
| 165 | |||
| 2326 | kebrt | 166 | /** Prints #istate_t structure content. |
| 167 | * |
||
| 168 | * @param istate Structure to be printed. |
||
| 169 | */ |
||
| 2304 | kebrt | 170 | void print_istate(istate_t *istate) |
| 171 | { |
||
| 4117 | decky | 172 | printf("istate dump:\n"); |
| 173 | |||
| 174 | printf(" r0: %x r1: %x r2: %x r3: %x\n", |
||
| 2464 | jermar | 175 | istate->r0, istate->r1, istate->r2, istate->r3); |
| 4117 | decky | 176 | printf(" r4: %x r5: %x r6: %x r7: %x\n", |
| 2464 | jermar | 177 | istate->r4, istate->r5, istate->r6, istate->r7); |
| 4117 | decky | 178 | printf(" r8: %x r8: %x r10: %x r11: %x\n", |
| 2464 | jermar | 179 | istate->r8, istate->r9, istate->r10, istate->r11); |
| 4117 | decky | 180 | printf(" r12: %x sp: %x lr: %x spsr: %x\n", |
| 2464 | jermar | 181 | istate->r12, istate->sp, istate->lr, istate->spsr); |
| 4117 | decky | 182 | |
| 183 | printf(" pc: %x\n", istate->pc); |
||
| 2304 | kebrt | 184 | } |
| 185 | |||
| 2179 | stepan | 186 | /** @} |
| 187 | */ |