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