[6b781c0] | 1 | /*
|
---|
| 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 |
|
---|
[c5429fe] | 29 | /** @addtogroup kernel_arm32
|
---|
[6b781c0] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief Exception handlers and exception initialization routines.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <arch/exception.h>
|
---|
| 37 | #include <arch/regutils.h>
|
---|
[5ac77cc] | 38 | #include <arch/machine_func.h>
|
---|
[6b781c0] | 39 | #include <interrupt.h>
|
---|
| 40 | #include <arch/mm/page_fault.h>
|
---|
[a03b609] | 41 | #include <arch/cp15.h>
|
---|
[05882233] | 42 | #include <barrier.h>
|
---|
[bab75df6] | 43 | #include <stdio.h>
|
---|
[6b781c0] | 44 | #include <syscall/syscall.h>
|
---|
[15817089] | 45 | #include <stacktrace.h>
|
---|
[6b781c0] | 46 |
|
---|
| 47 | /** Offset used in calculation of exception handler's relative address.
|
---|
| 48 | *
|
---|
| 49 | * @see install_handler()
|
---|
| 50 | */
|
---|
| 51 | #define PREFETCH_OFFSET 0x8
|
---|
| 52 |
|
---|
| 53 | /** LDR instruction's code */
|
---|
| 54 | #define LDR_OPCODE 0xe59ff000
|
---|
| 55 |
|
---|
| 56 | /** Number of exception vectors. */
|
---|
| 57 | #define EXC_VECTORS 8
|
---|
| 58 |
|
---|
| 59 | /** Size of memory block occupied by exception vectors. */
|
---|
| 60 | #define EXC_VECTORS_SIZE (EXC_VECTORS * 4)
|
---|
| 61 |
|
---|
| 62 | /** Updates specified exception vector to jump to given handler.
|
---|
| 63 | *
|
---|
| 64 | * Addresses of handlers are stored in memory following exception vectors.
|
---|
| 65 | */
|
---|
[eeaf667] | 66 | static void install_handler(unsigned handler_addr, unsigned *vector)
|
---|
[6b781c0] | 67 | {
|
---|
[7c3fb9b] | 68 | /*
|
---|
| 69 | * Relative address (related to exc. vector) of the word
|
---|
[6b781c0] | 70 | * where handler's address is stored
|
---|
[ae7d03c] | 71 | */
|
---|
[9cc0d7c] | 72 | volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE -
|
---|
| 73 | PREFETCH_OFFSET;
|
---|
[a35b458] | 74 |
|
---|
[7c3fb9b] | 75 | /* Make it LDR instruction and store at exception vector */
|
---|
[6b781c0] | 76 | *vector = handler_address_ptr | LDR_OPCODE;
|
---|
[0abc2ae] | 77 | smc_coherence(vector, 4);
|
---|
[a35b458] | 78 |
|
---|
[7c3fb9b] | 79 | /* Store handler's address */
|
---|
[6b781c0] | 80 | *(vector + EXC_VECTORS) = handler_addr;
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /** Software Interrupt handler.
|
---|
| 85 | *
|
---|
| 86 | * Dispatches the syscall.
|
---|
[214ec25c] | 87 | *
|
---|
[6b781c0] | 88 | */
|
---|
[214ec25c] | 89 | static void swi_exception(unsigned int exc_no, istate_t *istate)
|
---|
[6b781c0] | 90 | {
|
---|
[af69a4b] | 91 | interrupts_enable();
|
---|
[6b781c0] | 92 | istate->r0 = syscall_handler(istate->r0, istate->r1, istate->r2,
|
---|
[9cc0d7c] | 93 | istate->r3, istate->r4, istate->r5, istate->r6);
|
---|
[af69a4b] | 94 | interrupts_disable();
|
---|
[6b781c0] | 95 | }
|
---|
| 96 |
|
---|
| 97 | /** Fills exception vectors with appropriate exception handlers. */
|
---|
| 98 | void install_exception_handlers(void)
|
---|
| 99 | {
|
---|
| 100 | install_handler((unsigned) reset_exception_entry,
|
---|
| 101 | (unsigned *) EXC_RESET_VEC);
|
---|
[a35b458] | 102 |
|
---|
[6b781c0] | 103 | install_handler((unsigned) undef_instr_exception_entry,
|
---|
| 104 | (unsigned *) EXC_UNDEF_INSTR_VEC);
|
---|
[a35b458] | 105 |
|
---|
[6b781c0] | 106 | install_handler((unsigned) swi_exception_entry,
|
---|
| 107 | (unsigned *) EXC_SWI_VEC);
|
---|
[a35b458] | 108 |
|
---|
[6b781c0] | 109 | install_handler((unsigned) prefetch_abort_exception_entry,
|
---|
| 110 | (unsigned *) EXC_PREFETCH_ABORT_VEC);
|
---|
[a35b458] | 111 |
|
---|
[6b781c0] | 112 | install_handler((unsigned) data_abort_exception_entry,
|
---|
| 113 | (unsigned *) EXC_DATA_ABORT_VEC);
|
---|
[a35b458] | 114 |
|
---|
[6b781c0] | 115 | install_handler((unsigned) irq_exception_entry,
|
---|
| 116 | (unsigned *) EXC_IRQ_VEC);
|
---|
[a35b458] | 117 |
|
---|
[00287cc] | 118 | install_handler((unsigned) fiq_exception_entry,
|
---|
[6b781c0] | 119 | (unsigned *) EXC_FIQ_VEC);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | #ifdef HIGH_EXCEPTION_VECTORS
|
---|
[c5b69a5e] | 123 | /** Activates use of high exception vectors addresses.
|
---|
| 124 | *
|
---|
| 125 | * "High vectors were introduced into some implementations of ARMv4 and are
|
---|
| 126 | * required in ARMv6 implementations. High vectors allow the exception vector
|
---|
| 127 | * locations to be moved from their normal address range 0x00000000-0x0000001C
|
---|
| 128 | * at the bottom of the 32-bit address space, to an alternative address range
|
---|
| 129 | * 0xFFFF0000-0xFFFF001C near the top of the address space. These alternative
|
---|
| 130 | * locations are known as the high vectors.
|
---|
| 131 | *
|
---|
| 132 | * Prior to ARMv6, it is IMPLEMENTATION DEFINED whether the high vectors are
|
---|
| 133 | * supported. When they are, a hardware configuration input selects whether
|
---|
| 134 | * the normal vectors or the high vectors are to be used from
|
---|
| 135 | * reset." ARM Architecture Reference Manual A2.6.11 (p. 64 in the PDF).
|
---|
[b51b1cd] | 136 | *
|
---|
| 137 | * ARM920T (gta02) TRM A2.3.5 (PDF p. 36) and ARM926EJ-S (icp) 2.3.2 (PDF p. 42)
|
---|
| 138 | * say that armv4 an armv5 chips that we support implement this.
|
---|
[c5b69a5e] | 139 | */
|
---|
[6b781c0] | 140 | static void high_vectors(void)
|
---|
| 141 | {
|
---|
[a03b609] | 142 | uint32_t control_reg = SCTLR_read();
|
---|
[a35b458] | 143 |
|
---|
[6b781c0] | 144 | /* switch on the high vectors bit */
|
---|
[a03b609] | 145 | control_reg |= SCTLR_HIGH_VECTORS_EN_FLAG;
|
---|
[a35b458] | 146 |
|
---|
[a03b609] | 147 | SCTLR_write(control_reg);
|
---|
[6b781c0] | 148 | }
|
---|
| 149 | #endif
|
---|
| 150 |
|
---|
[6ac14a70] | 151 | /** Interrupt Exception handler.
|
---|
| 152 | *
|
---|
| 153 | * Determines the sources of interrupt and calls their handlers.
|
---|
| 154 | */
|
---|
[214ec25c] | 155 | static void irq_exception(unsigned int exc_no, istate_t *istate)
|
---|
[6ac14a70] | 156 | {
|
---|
| 157 | machine_irq_exception(exc_no, istate);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[957ce9a5] | 160 | /** Undefined instruction exception handler.
|
---|
| 161 | *
|
---|
| 162 | * Calls scheduler_fpu_lazy_request
|
---|
| 163 | */
|
---|
| 164 | static void undef_insn_exception(unsigned int exc_no, istate_t *istate)
|
---|
| 165 | {
|
---|
[5481a22e] | 166 | #ifdef CONFIG_FPU
|
---|
| 167 | if (handle_if_fpu_exception()) {
|
---|
[48a209a] | 168 | /*
|
---|
| 169 | * Retry the failing instruction,
|
---|
| 170 | * ARM Architecture Reference Manual says on p.B1-1169
|
---|
| 171 | * that offset for undef instruction exception is 4
|
---|
| 172 | */
|
---|
| 173 | istate->pc -= 4;
|
---|
[5481a22e] | 174 | return;
|
---|
[0237380] | 175 | }
|
---|
[5481a22e] | 176 | #endif
|
---|
| 177 | fault_if_from_uspace(istate, "Undefined instruction.");
|
---|
| 178 | panic_badtrap(istate, exc_no, "Undefined instruction.");
|
---|
[957ce9a5] | 179 | }
|
---|
| 180 |
|
---|
[6b781c0] | 181 | /** Initializes exception handling.
|
---|
[e762b43] | 182 | *
|
---|
[6b781c0] | 183 | * Installs low-level exception handlers and then registers
|
---|
| 184 | * exceptions and their handlers to kernel exception dispatcher.
|
---|
| 185 | */
|
---|
| 186 | void exception_init(void)
|
---|
| 187 | {
|
---|
[c5b69a5e] | 188 | // TODO check for availability of high vectors for <= armv5
|
---|
[6b781c0] | 189 | #ifdef HIGH_EXCEPTION_VECTORS
|
---|
| 190 | high_vectors();
|
---|
| 191 | #endif
|
---|
| 192 | install_exception_handlers();
|
---|
[a35b458] | 193 |
|
---|
[957ce9a5] | 194 | exc_register(EXC_UNDEF_INSTR, "undefined instruction", true,
|
---|
| 195 | (iroutine_t) undef_insn_exception);
|
---|
[b3b7e14a] | 196 | exc_register(EXC_IRQ, "interrupt", true,
|
---|
| 197 | (iroutine_t) irq_exception);
|
---|
| 198 | exc_register(EXC_PREFETCH_ABORT, "prefetch abort", true,
|
---|
| 199 | (iroutine_t) prefetch_abort);
|
---|
| 200 | exc_register(EXC_DATA_ABORT, "data abort", true,
|
---|
| 201 | (iroutine_t) data_abort);
|
---|
| 202 | exc_register(EXC_SWI, "software interrupt", true,
|
---|
| 203 | (iroutine_t) swi_exception);
|
---|
[6b781c0] | 204 | }
|
---|
| 205 |
|
---|
| 206 | /** Prints #istate_t structure content.
|
---|
| 207 | *
|
---|
| 208 | * @param istate Structure to be printed.
|
---|
| 209 | */
|
---|
[22a28a69] | 210 | void istate_decode(istate_t *istate)
|
---|
[6b781c0] | 211 | {
|
---|
[a99a3d7] | 212 | printf("r0 =%0#10" PRIx32 "\tr1 =%0#10" PRIx32 "\t"
|
---|
| 213 | "r2 =%0#10" PRIx32 "\tr3 =%0#10" PRIx32 "\n",
|
---|
[6b781c0] | 214 | istate->r0, istate->r1, istate->r2, istate->r3);
|
---|
[a99a3d7] | 215 | printf("r4 =%0#10" PRIx32 "\tr5 =%0#10" PRIx32 "\t"
|
---|
| 216 | "r6 =%0#10" PRIx32 "\tr7 =%0#10" PRIx32 "\n",
|
---|
[6b781c0] | 217 | istate->r4, istate->r5, istate->r6, istate->r7);
|
---|
[a99a3d7] | 218 | printf("r8 =%0#10" PRIx32 "\tr9 =%0#10" PRIx32 "\t"
|
---|
| 219 | "r10=%0#10" PRIx32 "\tfp =%0#10" PRIx32 "\n",
|
---|
| 220 | istate->r8, istate->r9, istate->r10, istate->fp);
|
---|
| 221 | printf("r12=%0#10" PRIx32 "\tsp =%0#10" PRIx32 "\t"
|
---|
| 222 | "lr =%0#10" PRIx32 "\tspsr=%0#10" PRIx32 "\n",
|
---|
| 223 | istate->r12, istate->sp, istate->lr, istate->spsr);
|
---|
[6b781c0] | 224 | }
|
---|
| 225 |
|
---|
| 226 | /** @}
|
---|
| 227 | */
|
---|