source: mainline/kernel/arch/arm32/src/exception.c@ cc250b3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc250b3 was cc250b3, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

Merge mainline changes

  • Property mode set to 100644
File size: 7.0 KB
Line 
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>
38#include <arch/machine_func.h>
39#include <interrupt.h>
40#include <arch/mm/page_fault.h>
41#include <arch/barrier.h>
42#include <print.h>
43#include <syscall/syscall.h>
44#include <stacktrace.h>
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 */
65static void install_handler(unsigned handler_addr, unsigned *vector)
66{
67 /* relative address (related to exc. vector) of the word
68 * where handler's address is stored
69 */
70 volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE -
71 PREFETCH_OFFSET;
72
73 /* make it LDR instruction and store at exception vector */
74 *vector = handler_address_ptr | LDR_OPCODE;
75 smc_coherence(*vector);
76
77 /* store handler's address */
78 *(vector + EXC_VECTORS) = handler_addr;
79
80}
81
82/** Software Interrupt handler.
83 *
84 * Dispatches the syscall.
85 *
86 */
87static void swi_exception(unsigned int exc_no, istate_t *istate)
88{
89 istate->r0 = syscall_handler(istate->r0, istate->r1, istate->r2,
90 istate->r3, istate->r4, istate->r5, istate->r6);
91}
92
93/** Fills exception vectors with appropriate exception handlers. */
94void 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
114 install_handler((unsigned) fiq_exception_entry,
115 (unsigned *) EXC_FIQ_VEC);
116}
117
118#ifdef HIGH_EXCEPTION_VECTORS
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).
132 */
133static void high_vectors(void)
134{
135 uint32_t control_reg = 0;
136 // TODO CHeck the armv6 way and implement it
137#if defined(PROCESSOR_armv7_a) | defined(ROCESSOR_armv6)
138 asm volatile (
139 "mrc p15, 0, %[control_reg], c1, c0"
140 : [control_reg] "=r" (control_reg)
141 );
142#elif defined(PROCESSOR_armv4) | defined(PROCESSOR_armv5)
143 asm volatile (
144 "mrc p15, 0, %[control_reg], c1, c0"
145 : [control_reg] "=r" (control_reg)
146 );
147#endif
148
149 /* switch on the high vectors bit */
150 control_reg |= CP15_R1_HIGH_VECTORS_BIT;
151
152#if defined(PROCESSOR_armv7_a) | defined(ROCESSOR_armv6)
153 asm volatile (
154 "mcr p15, 0, %[control_reg], c1, c0"
155 :: [control_reg] "r" (control_reg)
156 );
157#elif defined(PROCESSOR_armv4) | defined(PROCESSOR_armv5)
158 asm volatile (
159 "mcr p15, 0, %[control_reg], c1, c0"
160 :: [control_reg] "r" (control_reg)
161 );
162#endif
163}
164#endif
165
166/** Interrupt Exception handler.
167 *
168 * Determines the sources of interrupt and calls their handlers.
169 */
170static void irq_exception(unsigned int exc_no, istate_t *istate)
171{
172 machine_irq_exception(exc_no, istate);
173}
174
175/** Initializes exception handling.
176 *
177 * Installs low-level exception handlers and then registers
178 * exceptions and their handlers to kernel exception dispatcher.
179 */
180void exception_init(void)
181{
182 // TODO check for availability of high vectors for <= armv5
183#ifdef HIGH_EXCEPTION_VECTORS
184 high_vectors();
185#endif
186 install_exception_handlers();
187
188 exc_register(EXC_IRQ, "interrupt", true,
189 (iroutine_t) irq_exception);
190 exc_register(EXC_PREFETCH_ABORT, "prefetch abort", true,
191 (iroutine_t) prefetch_abort);
192 exc_register(EXC_DATA_ABORT, "data abort", true,
193 (iroutine_t) data_abort);
194 exc_register(EXC_SWI, "software interrupt", true,
195 (iroutine_t) swi_exception);
196}
197
198/** Prints #istate_t structure content.
199 *
200 * @param istate Structure to be printed.
201 */
202void istate_decode(istate_t *istate)
203{
204 printf("r0 =%0#10" PRIx32 "\tr1 =%0#10" PRIx32 "\t"
205 "r2 =%0#10" PRIx32 "\tr3 =%0#10" PRIx32 "\n",
206 istate->r0, istate->r1, istate->r2, istate->r3);
207 printf("r4 =%0#10" PRIx32 "\tr5 =%0#10" PRIx32 "\t"
208 "r6 =%0#10" PRIx32 "\tr7 =%0#10" PRIx32 "\n",
209 istate->r4, istate->r5, istate->r6, istate->r7);
210 printf("r8 =%0#10" PRIx32 "\tr9 =%0#10" PRIx32 "\t"
211 "r10=%0#10" PRIx32 "\tfp =%0#10" PRIx32 "\n",
212 istate->r8, istate->r9, istate->r10, istate->fp);
213 printf("r12=%0#10" PRIx32 "\tsp =%0#10" PRIx32 "\t"
214 "lr =%0#10" PRIx32 "\tspsr=%0#10" PRIx32 "\n",
215 istate->r12, istate->sp, istate->lr, istate->spsr);
216}
217
218/** @}
219 */
Note: See TracBrowser for help on using the repository browser.