Subversion Repositories HelenOS

Rev

Rev 2407 | Rev 2410 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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