Subversion Repositories HelenOS

Rev

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

Rev 2282 Rev 2284
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
#include <arch/exception.h>
36
#include <arch/exception.h>
37
#include <arch/debug_print/print.h>
37
#include <arch/debug_print/print.h>
38
#include <arch/memstr.h>
38
#include <arch/memstr.h>
39
#include <arch/regutils.h>
39
#include <arch/regutils.h>
40
#include <interrupt.h>
40
#include <interrupt.h>
41
#include <arch/drivers/gxemul.h>
41
#include <arch/drivers/gxemul.h>
42
#include <arch/mm/page_fault.h>
42
#include <arch/mm/page_fault.h>
-
 
43
#include <print.h>
43
 
44
 
44
#define PREFETCH_OFFSET     0x8
45
#define PREFETCH_OFFSET     0x8
45
#define BRANCH_OPCODE       0xea000000
46
#define BRANCH_OPCODE       0xea000000
46
#define LDR_OPCODE      0xe59ff000
47
#define LDR_OPCODE      0xe59ff000
47
#define VALID_BRANCH_MASK   0xff000000
48
#define VALID_BRANCH_MASK   0xff000000
48
#define EXC_VECTORS_SIZE    0x20
49
#define EXC_VECTORS_SIZE    0x20
49
#define EXC_VECTORS     0x8
50
#define EXC_VECTORS     0x8
50
 
51
 
-
 
52
extern uintptr_t supervisor_sp;
-
 
53
extern uintptr_t exc_stack;
-
 
54
 
-
 
55
inline static void setup_stack_and_save_regs()
-
 
56
{
-
 
57
asm volatile(   "ldr r13, =exc_stack        \n\
-
 
58
    stmfd r13!, {r0}            \n\
-
 
59
    mrs r0, spsr                \n\
-
 
60
    and r0, r0, #0x1f           \n\
-
 
61
    cmp r0, #0x10               \n\
-
 
62
    bne 1f                  \n\
-
 
63
                        \n\
-
 
64
    @prev mode was usermode         \n\
-
 
65
    ldmfd r13!, {r0}            \n\
-
 
66
    ldr r13, =supervisor_sp         \n\
-
 
67
    stmfd r13!, {r0-r12, r13, lr}       \n\
-
 
68
    stmfd r13!, {r13, lr}^          \n\
-
 
69
    mrs r0, spsr                \n\
-
 
70
    stmfd r13!, {r0}            \n\
-
 
71
    b 2f                    \n\
-
 
72
                        \n\
-
 
73
    @prev mode was not usermode     \n\
-
 
74
1:                      \n\
-
 
75
    stmfd r13!, {r1, r2, r3}        \n\
-
 
76
    mrs r1, cpsr                \n\
-
 
77
    mov r2, lr              \n\
-
 
78
    bic r1, r1, #0x1f           \n\
-
 
79
    orr r1, r1, r0              \n\
-
 
80
    mrs r0, cpsr                \n\
-
 
81
    msr cpsr_c, r1              \n\
-
 
82
                        \n\
-
 
83
    mov r3, r13             \n\
-
 
84
    stmfd r13!, {r2}            \n\
-
 
85
    stmfd r13!, {r3}            \n\
-
 
86
    stmfd r13!, {r4-r12}            \n\
-
 
87
    mov r2, lr              \n\
-
 
88
    mov r1, r13             \n\
-
 
89
    msr cpsr_c, r0              \n\
-
 
90
                        \n\
-
 
91
    ldmfd r13!, {r4, r5, r6, r7}        \n\
-
 
92
    stmfd r1!, {r4, r5, r6}         \n\
-
 
93
    stmfd r1!, {r7}             \n\
-
 
94
    stmfd r1!, {r2}             \n\
-
 
95
    stmfd r1!, {r3}             \n\
-
 
96
    mrs r0, spsr                \n\
-
 
97
    stmfd r1!, {r0}             \n\
-
 
98
    mov r13, r1             \n\
-
 
99
2:"
-
 
100
);
-
 
101
}
-
 
102
 
-
 
103
 
-
 
104
inline static void load_regs()
-
 
105
{
-
 
106
asm volatile(   "ldmfd r13!, {r0}       \n\
-
 
107
    msr spsr, r0                \n\
-
 
108
    and r0, r0, #0x1f           \n\
-
 
109
    cmp r0, #0x10               \n\
-
 
110
    bne 3f                  \n\
-
 
111
                        \n\
-
 
112
    @return to user mode            \n\
-
 
113
    ldmfd r13!, {r13, lr}^          \n\
-
 
114
    b 4f                    \n\
-
 
115
                        \n\
-
 
116
    @return to non-user mode        \n\
-
 
117
3:                      \n\
-
 
118
    ldmfd r13!, {r1, r2}            \n\
-
 
119
    mrs r3, cpsr                \n\
-
 
120
    bic r3, r3, #0x1f           \n\
-
 
121
    orr r3, r3, r0              \n\
-
 
122
    mrs r0, cpsr                \n\
-
 
123
    msr cpsr_c, r3              \n\
-
 
124
                        \n\
-
 
125
    mov r13, r1             \n\
-
 
126
    mov lr, r2              \n\
-
 
127
    msr cpsr_c, r0              \n\
-
 
128
                        \n\
-
 
129
    @actual return              \n\
-
 
130
4:                      \n\
-
 
131
    ldmfd r13!, {r0-r12, r13, pc}^"
-
 
132
);
-
 
133
}
-
 
134
 
51
 
135
 
52
#define SAVE_REGS_TO_STACK          \
136
#define SAVE_REGS_TO_STACK          \
53
    asm("stmfd sp!, {r0-r12, sp, lr}");     \
137
    asm("stmfd r13!, {r0-r12, r13, lr}");   \
54
    asm("mrs r14, spsr");           \
138
    asm("mrs r14, spsr");           \
55
    asm("stmfd sp!, {r14}");
139
    asm("stmfd r13!, {r14}");
-
 
140
 
-
 
141
 
56
 
142
 
57
#define CALL_EXC_DISPATCH(exception)        \
143
#define CALL_EXC_DISPATCH(exception)        \
58
    asm("mov r0, %0" : : "i" (exception));  \
144
    asm("mov r0, %0" : : "i" (exception));  \
59
    asm("mov r1, sp");          \
145
    asm("mov r1, r13");         \
60
    asm("bl exc_dispatch");     
146
    asm("bl exc_dispatch");     
61
 
147
 
-
 
148
 
62
/**Loads registers from the stack and resets SPSR before exitting exception
149
/**Loads registers from the stack and resets SPSR before exitting exception
63
 * handler.
150
 * handler.
64
 */
151
 */
65
#define LOAD_REGS_FROM_STACK            \
152
#define LOAD_REGS_FROM_STACK            \
66
    asm("ldmfd sp!, {r14}");        \
153
    asm("ldmfd r13!, {r14}");       \
67
    asm("msr spsr, r14");           \
154
    asm("msr spsr, r14");           \
68
    asm("ldmfd sp!, {r0-r12, sp, pc}^");
155
    asm("ldmfd r13!, {r0-r12, r13, pc}^");
-
 
156
 
-
 
157
 
69
   
158
   
70
/** General exception handler.
159
/** General exception handler.
71
 *  Stores registers, dispatches the exception,
160
 *  Stores registers, dispatches the exception,
72
 *  and finally restores registers and returns from exception processing.
161
 *  and finally restores registers and returns from exception processing.
73
 */
162
 */
-
 
163
 
74
#define PROCESS_EXCEPTION(exception)        \
164
#define PROCESS_EXCEPTION(exception)        \
75
    SAVE_REGS_TO_STACK          \
165
    setup_stack_and_save_regs();        \
76
    CALL_EXC_DISPATCH(exception)        \
166
    CALL_EXC_DISPATCH(exception)        \
-
 
167
    load_regs();
-
 
168
 
-
 
169
/* #define PROCESS_EXCEPTION(exception)     \
-
 
170
    SAVE_REGS_TO_STACK      \
-
 
171
    CALL_EXC_DISPATCH(exception)        \
77
    LOAD_REGS_FROM_STACK            
172
    LOAD_REGS_FROM_STACK*/
78
 
173
 
79
/** Updates specified exception vector to jump to given handler.
174
/** Updates specified exception vector to jump to given handler.
80
 * Addresses of handlers are stored in memory following exception vectors.
175
 * Addresses of handlers are stored in memory following exception vectors.
81
 */
176
 */
82
static void install_handler (unsigned handler_addr, unsigned* vector)
177
static void install_handler (unsigned handler_addr, unsigned* vector)
83
{
178
{
84
    /* relative address (related to exc. vector) of the word
179
    /* relative address (related to exc. vector) of the word
85
     * where handler's address is stored
180
     * where handler's address is stored
86
    */
181
    */
87
    volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE - PREFETCH_OFFSET;
182
    volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE - PREFETCH_OFFSET;
88
   
183
   
89
    /* make it LDR instruction and store at exception vector */
184
    /* make it LDR instruction and store at exception vector */
90
    *vector = handler_address_ptr | LDR_OPCODE;
185
    *vector = handler_address_ptr | LDR_OPCODE;
91
   
186
   
92
    /* store handler's address */
187
    /* store handler's address */
93
    *(vector + EXC_VECTORS) = handler_addr;
188
    *(vector + EXC_VECTORS) = handler_addr;
-
 
189
 
94
}
190
}
95
 
191
 
-
 
192
 
96
static void reset_exception_entry()
193
static void reset_exception_entry()
97
{
194
{
98
    PROCESS_EXCEPTION(EXC_RESET);
195
    PROCESS_EXCEPTION(EXC_RESET);
99
}
196
}
100
 
197
 
101
/** Low-level Software Interrupt Exception handler */
198
/** Low-level Software Interrupt Exception handler */
102
static void swi_exception_entry()
199
static void swi_exception_entry()
103
{
200
{
104
    PROCESS_EXCEPTION(EXC_SWI);
201
    PROCESS_EXCEPTION(EXC_SWI);
105
}
202
}
106
 
203
 
107
/** Low-level Undefined Instruction Exception handler */
204
/** Low-level Undefined Instruction Exception handler */
108
static void undef_instr_exception_entry()
205
static void undef_instr_exception_entry()
109
{
206
{
110
    PROCESS_EXCEPTION(EXC_UNDEF_INSTR);
207
    PROCESS_EXCEPTION(EXC_UNDEF_INSTR);
111
}
208
}
112
 
209
 
113
/** Low-level Fast Interrupt Exception handler */
210
/** Low-level Fast Interrupt Exception handler */
114
static void fiq_exception_entry()
211
static void fiq_exception_entry()
115
{
212
{
116
    PROCESS_EXCEPTION(EXC_FIQ);
213
    PROCESS_EXCEPTION(EXC_FIQ);
117
}
214
}
118
 
215
 
119
/** Low-level Prefetch Abort Exception handler */
216
/** Low-level Prefetch Abort Exception handler */
120
static void prefetch_abort_exception_entry()
217
static void prefetch_abort_exception_entry()
121
{
218
{
122
    asm("sub lr, lr, #4");
219
    asm("sub lr, lr, #4");
123
    PROCESS_EXCEPTION(EXC_PREFETCH_ABORT);
220
    PROCESS_EXCEPTION(EXC_PREFETCH_ABORT);
124
}
221
}
125
 
222
 
126
/** Low-level Data Abort Exception handler */
223
/** Low-level Data Abort Exception handler */
127
static void data_abort_exception_entry()
224
static void data_abort_exception_entry()
128
{
225
{
129
    asm("sub lr, lr, #8");
226
    asm("sub lr, lr, #8");
130
    PROCESS_EXCEPTION(EXC_DATA_ABORT);
227
    PROCESS_EXCEPTION(EXC_DATA_ABORT);
131
}
228
}
132
 
229
 
133
 
230
 
134
/** Low-level Interrupt Exception handler */
231
/** Low-level Interrupt Exception handler */
135
static void irq_exception_entry()
232
static void irq_exception_entry()
136
{
233
{
137
    asm("sub lr, lr, #4");
234
    asm("sub lr, lr, #4");
-
 
235
//  SAVE_REGS_TO_STACK      
-
 
236
//  CALL_EXC_DISPATCH(EXC_IRQ)      
-
 
237
//  LOAD_REGS_FROM_STACK;
138
    PROCESS_EXCEPTION(EXC_IRQ);
238
    PROCESS_EXCEPTION(EXC_IRQ);
139
}
239
}
140
 
240
 
-
 
241
// static void prefetch_abort_exception(int exc_no, istate_t* istate)
-
 
242
// {
-
 
243
//  dputs("(PREFETCH|DATA) ABORT exception caught, not processed.\n");
-
 
244
// }
-
 
245
 
-
 
246
static void swi_exception(int exc_no, istate_t* istate)
-
 
247
{
-
 
248
    dprintf("\nIstate dump:\n");
-
 
249
    dprintf("    r0:%X    r1:%X    r2:%X    r3:%X\n", istate->r0,  istate->r1, istate->r2,  istate->r3);
-
 
250
    dprintf("    r4:%X    r5:%X    r6:%X    r7:%X\n", istate->r4,  istate->r5, istate->r6,  istate->r7);
-
 
251
    dprintf("    r8:%X    r9:%X   r10:%X     r11:%X\n", istate->r8,  istate->r9, istate->r10, istate->r11);
-
 
252
    dprintf("      r12:%X    r13:%X    lr:%X  spsr:%X\n", istate->r12, istate->sp, istate->lr,  istate->spsr);
-
 
253
    dprintf("   prev_lr:%X    prev_sp:%X\n", istate->prev_lr, istate->prev_sp);
-
 
254
}
141
 
255
 
142
/** Interrupt Exception handler.
256
/** Interrupt Exception handler.
143
 * Determines the sources of interrupt, and calls their handlers.
257
 * Determines the sources of interrupt, and calls their handlers.
144
 */
258
 */
145
static void irq_exception(int exc_no, istate_t* istate)
259
static void irq_exception(int exc_no, istate_t* istate)
146
{
260
{
147
// TODO: move somewhere to gxemul.c and use machine_irq_exception (or some similar
261
// TODO: move somewhere to gxemul.c and use machine_irq_exception (or some similar
148
// name) to avoid using MACHINE == MACHINE_GXEMUL_TESTARM
262
// name) to avoid using MACHINE == MACHINE_GXEMUL_TESTARM
149
#if MACHINE == MACHINE_GXEMUL_TESTARM
263
#if MACHINE == MACHINE_GXEMUL_TESTARM
150
    uint32_t sources = gxemul_irqc_get_sources();
264
    uint32_t sources = gxemul_irqc_get_sources();
151
    int i = 0;
265
    int i = 0;
152
    for (; i < GXEMUL_IRQC_MAX_IRQ; i++) {
266
    for (; i < GXEMUL_IRQC_MAX_IRQ; i++) {
153
        if (sources & (1 << i)) {
267
        if (sources & (1 << i)) {
154
            irq_t *irq = irq_dispatch_and_lock(i);
268
            irq_t *irq = irq_dispatch_and_lock(i);
155
            if (irq) {
269
            if (irq) {
156
                /* The IRQ handler was found. */
270
                /* The IRQ handler was found. */
157
                irq->handler(irq, irq->arg);
271
                irq->handler(irq, irq->arg);
158
                spinlock_unlock(&irq->lock);
272
                spinlock_unlock(&irq->lock);
159
            } else {
273
            } else {
160
                /* Spurious interrupt.*/
274
                /* Spurious interrupt.*/
161
                dprintf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, i);
275
                dprintf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, i);
162
            }
276
            }
163
        }
277
        }
164
    }
278
    }
165
#endif
279
#endif
166
/* TODO remove after testing the above code
280
/* TODO remove after testing the above code
167
            noirq = 0;
281
            noirq = 0;
168
            if (i == CONSOLE_IRQ) {
282
            if (i == CONSOLE_IRQ) {
169
                char readchar = *(char*)0x10000000;
283
                char readchar = *(char*)0x10000000;
170
                if (readchar == 0) {
284
                if (readchar == 0) {
171
                    aux_puts("?");
285
                    aux_puts("?");
172
                }
286
                }
173
                else {
287
                else {
174
                    dprintf("%c", readchar);
288
                    dprintf("%c", readchar);
175
                }
289
                }
176
               
290
               
177
            }
291
            }
178
            else if (i == TIMER_IRQ) {
292
            else if (i == TIMER_IRQ) {
179
                dprintf("\n.\n");
293
                dprintf("\n.\n");
180
                //acknowledge
294
                //acknowledge
181
                *(uint32_t*)0x15000110 = 0;
295
                *(uint32_t*)0x15000110 = 0;
182
            }
296
            }
183
        }
297
        }
184
    }
298
    }
185
 
299
 
186
    if (noirq)
300
    if (noirq)
187
    aux_puts("IRQ exception without source\n");*/
301
    aux_puts("IRQ exception without source\n");*/
188
}
302
}
189
 
303
 
190
/** Fills exception vectors with appropriate exception handlers.
304
/** Fills exception vectors with appropriate exception handlers.
191
*/
305
*/
192
void install_exception_handlers(void)
306
void install_exception_handlers(void)
193
{
307
{
194
    install_handler((unsigned)reset_exception_entry,
308
    install_handler((unsigned)reset_exception_entry,
195
             (unsigned*)EXC_RESET_VEC);
309
             (unsigned*)EXC_RESET_VEC);
196
   
310
   
197
    install_handler((unsigned)undef_instr_exception_entry,
311
    install_handler((unsigned)undef_instr_exception_entry,
198
             (unsigned*)EXC_UNDEF_INSTR_VEC);
312
             (unsigned*)EXC_UNDEF_INSTR_VEC);
199
   
313
   
200
    install_handler((unsigned)swi_exception_entry,
314
    install_handler((unsigned)swi_exception_entry,
201
             (unsigned*)EXC_SWI_VEC);
315
             (unsigned*)EXC_SWI_VEC);
202
   
316
   
203
    install_handler((unsigned)prefetch_abort_exception_entry,
317
    install_handler((unsigned)prefetch_abort_exception_entry,
204
             (unsigned*)EXC_PREFETCH_ABORT_VEC);
318
             (unsigned*)EXC_PREFETCH_ABORT_VEC);
205
   
319
   
206
    install_handler((unsigned)data_abort_exception_entry,
320
    install_handler((unsigned)data_abort_exception_entry,
207
             (unsigned*)EXC_DATA_ABORT_VEC);
321
             (unsigned*)EXC_DATA_ABORT_VEC);
208
   
322
   
209
    install_handler((unsigned)irq_exception_entry,  
323
    install_handler((unsigned)irq_exception_entry,
210
             (unsigned*)EXC_IRQ_VEC);
324
             (unsigned*)EXC_IRQ_VEC);
211
   
325
   
212
    install_handler((unsigned)fiq_exception_entry,
326
    install_handler((unsigned)fiq_exception_entry,
213
             (unsigned*)EXC_FIQ_VEC);
327
             (unsigned*)EXC_FIQ_VEC);
214
}
328
}
215
 
329
 
-
 
330
#ifdef HIGH_EXCEPTION_VECTORS
216
/** Activates using high exception vectors addresses. */
331
/** Activates using high exception vectors addresses. */
217
 static void high_vectors()
332
 static void high_vectors()
218
{
333
{
219
    uint32_t control_reg;
334
    uint32_t control_reg;
220
   
335
   
221
    asm volatile( "mrc p15, 0, %0, c1, c1": "=r" (control_reg));
336
    asm volatile( "mrc p15, 0, %0, c1, c1": "=r" (control_reg));
222
   
337
   
223
    //switch on the high vectors bit
338
    //switch on the high vectors bit
224
    control_reg |= CP15_R1_HIGH_VECTORS_BIT;
339
    control_reg |= CP15_R1_HIGH_VECTORS_BIT;
225
   
340
   
226
    asm volatile( "mcr p15, 0, %0, c1, c1" : : "r" (control_reg));
341
    asm volatile( "mcr p15, 0, %0, c1, c1" : : "r" (control_reg));
227
}
342
}
228
 
343
#endif
229
 
344
 
230
/** Initializes exception handling.
345
/** Initializes exception handling.
231
 *
346
 *
232
 * Installs low-level exception handlers and then registers
347
 * Installs low-level exception handlers and then registers
233
 * exceptions and their handlers to kernel exception dispatcher.
348
 * exceptions and their handlers to kernel exception dispatcher.
234
 */
349
 */
235
void exception_init(void)
350
void exception_init(void)
236
{
351
{
237
#ifdef HIGH_EXCEPTION_VECTORS
352
#ifdef HIGH_EXCEPTION_VECTORS
238
    high_vectors();
353
    high_vectors();
239
#endif
354
#endif
240
 
-
 
241
    install_exception_handlers();
355
    install_exception_handlers();
242
   
356
   
243
    exc_register(EXC_IRQ, "interrupt", (iroutine) irq_exception);
357
    exc_register(EXC_IRQ, "interrupt", (iroutine) irq_exception);
244
    exc_register(EXC_PREFETCH_ABORT, "prefetch abort", (iroutine) prefetch_abort);
358
    exc_register(EXC_PREFETCH_ABORT, "prefetch abort", (iroutine) prefetch_abort);
245
    exc_register(EXC_DATA_ABORT, "data abort", (iroutine) data_abort);
359
    exc_register(EXC_DATA_ABORT, "data abort", (iroutine) data_abort);
-
 
360
    exc_register(EXC_SWI, "software interrupt", (iroutine) swi_exception);
246
    /* TODO add next */
361
    /* TODO add next */
247
}
362
}
248
 
363
 
249
/* TODO change soon, temporary hack. */
364
/** Sets stack pointers in all supported exception modes.
-
 
365
 *
-
 
366
 * @param stack_ptr stack pointer
-
 
367
 */
250
void setup_exception_stacks()
368
void setup_exception_stacks()
251
{
369
{
252
    /* switch to particular mode and set "sp" there */
370
        /* switch to particular mode and set "r13" there */
253
 
371
 
254
    uint32_t cspr = current_status_reg_read();
372
        uint32_t cspr = current_status_reg_read();
255
 
373
 
256
    /* IRQ stack */
374
        /* IRQ stack */
257
    current_status_reg_control_write(
375
        current_status_reg_control_write(
258
            (cspr & ~STATUS_REG_MODE_MASK) | IRQ_MODE
376
                        (cspr & ~STATUS_REG_MODE_MASK) | IRQ_MODE
259
    );
377
        );
260
    asm("ldr sp, =irq_stack");
378
        asm("ldr r13, =exc_stack");
261
 
379
 
262
    /* abort stack */
380
        /* abort stack */
263
    current_status_reg_control_write(
381
        current_status_reg_control_write(
264
            (cspr & ~STATUS_REG_MODE_MASK) | ABORT_MODE
382
                        (cspr & ~STATUS_REG_MODE_MASK) | ABORT_MODE
265
    );
383
        );
266
    asm("ldr sp, =abort_stack");
384
        asm("ldr r13, =exc_stack");
267
 
385
 
268
    /* TODO if you want to test other exceptions than IRQ,
386
        /* TODO if you want to test other exceptions than IRQ,
269
    make stack analogous to irq_stack (in start.S),
387
        make stack analogous to irq_stack (in start.S),
270
    and then set stack pointer here */
388
        and then set stack pointer here */
271
 
389
 
272
    current_status_reg_control_write( cspr);
390
        current_status_reg_control_write( cspr);
273
 
391
 
274
}
392
}
275
 
393
 
276
/** @}
394
/** @}
277
 */
395
 */
278
 
396