Subversion Repositories HelenOS

Rev

Rev 2235 | Rev 2262 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2235 stepan 1
/*
2179 stepan 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 "aux_print/printf.h"
38
#include <arch/memstr.h>
2235 stepan 39
#include <arch/regutils.h>
40
#include <interrupt.h>
2245 stepan 41
#include <arch/drivers/gxemul.h>
2179 stepan 42
 
43
#define PREFETCH_OFFSET     0x8
44
#define BRANCH_OPCODE       0xea000000
2235 stepan 45
#define LDR_OPCODE      0xe59ff000
2179 stepan 46
#define VALID_BRANCH_MASK   0xff000000
2235 stepan 47
#define EXC_VECTORS_SIZE    0x20
48
#define EXC_VECTORS     0x8
2179 stepan 49
 
2235 stepan 50
 
51
#define SAVE_REGS_TO_STACK          \
52
    asm("stmfd sp!, {r0-r12, sp, lr}");     \
53
    asm("mrs r14, spsr");           \
54
    asm("stmfd sp!, {r14}");
55
 
56
#define CALL_EXC_DISPATCH(exception)        \
57
    asm("mov r0, %0" : : "i" (exception));  \
58
    asm("mov r1, sp");          \
59
    asm("bl exc_dispatch");     
60
 
61
/**Loads registers from the stack and resets SPSR before exitting exception
62
 * handler.
63
 */
64
#define LOAD_REGS_FROM_STACK            \
65
    asm("ldmfd sp!, {r14}");        \
66
    asm("msr spsr, r14");           \
67
    asm("ldmfd sp!, {r0-r12, sp, pc}^");
2179 stepan 68
 
2235 stepan 69
/** General exception handler.
70
 *  Stores registers, dispatches the exception,
71
 *  and finally restores registers and returns from exception processing.
72
 */
73
#define PROCESS_EXCEPTION(exception)        \
74
    SAVE_REGS_TO_STACK          \
75
    CALL_EXC_DISPATCH(exception)        \
76
    LOAD_REGS_FROM_STACK            
77
 
78
/** Updates specified exception vector to jump to given handler.
79
 * Addresses of handlers are stored in memory following exception vectors.
80
 */
81
static void install_handler (unsigned handler_addr, unsigned* vector)
82
{
83
    /* relative address (related to exc. vector) of the word
84
     * where handler's address is stored
85
    */
86
    volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE - PREFETCH_OFFSET;
2179 stepan 87
 
2235 stepan 88
    /* make it LDR instruction and store at exception vector */
89
    *vector = handler_address_ptr | LDR_OPCODE;
2179 stepan 90
 
2235 stepan 91
    /* store handler's address */
92
    *(vector + EXC_VECTORS) = handler_addr;
2179 stepan 93
}
94
 
2235 stepan 95
static void reset_exception_entry()
96
{
97
    PROCESS_EXCEPTION(EXC_RESET);
2179 stepan 98
}
99
 
2235 stepan 100
/** Low-level Software Interrupt Exception handler */
101
static void swi_exception_entry()
102
{
103
    PROCESS_EXCEPTION(EXC_SWI);
2179 stepan 104
}
105
 
2235 stepan 106
/** Low-level Undefined Instruction Exception handler */
107
static void undef_instr_exception_entry()
108
{
109
    PROCESS_EXCEPTION(EXC_UNDEF_INSTR);
110
}
111
 
112
/** Low-level Fast Interrupt Exception handler */
113
static void fiq_exception_entry()
114
{
115
    PROCESS_EXCEPTION(EXC_FIQ);
116
}
117
 
118
/** Low-level Prefetch Abort Exception handler */
119
static void prefetch_abort_exception_entry()
120
{
121
    asm("sub lr, lr, #4");
122
    PROCESS_EXCEPTION(EXC_PREFETCH_ABORT);
123
}
124
 
125
/** Low-level Data Abort Exception handler */
126
static void data_abort_exception_entry()
127
{
128
    asm("sub lr, lr, #8");
129
    PROCESS_EXCEPTION(EXC_DATA_ABORT);
130
}
131
 
132
 
133
/** Low-level Interrupt Exception handler */
134
static void irq_exception_entry()
135
{
136
    asm("sub lr, lr, #4");
137
    PROCESS_EXCEPTION(EXC_IRQ);
138
}
139
 
140
 
141
/** Interrupt Exception handler.
142
 * Determines the sources of interrupt, and calls their handlers.
143
 */
144
static void irq_exception(int exc_no, istate_t* istate)
145
{
2245 stepan 146
    uint32_t sources = gxemul_irqc_get_sources();
2235 stepan 147
    int i = 0;
2245 stepan 148
    for (; i < GXEMUL_IRQC_MAX_IRQ; i++) {
2235 stepan 149
        if (sources & (1 << i)) {
2245 stepan 150
            irq_t *irq = irq_dispatch_and_lock(i);
151
            if (irq) {
152
                /* The IRQ handler was found. */
153
                irq->handler(irq, irq->arg);
154
                spinlock_unlock(&irq->lock);
155
            } else {
156
                /* Spurious interrupt.*/
157
                aux_printf("cpu%d: spurious interrupt (inum=%d)\n", CPU->id, i);
158
            }
159
        }
160
    }
161
/* TODO remove after testing the above code
2235 stepan 162
            noirq = 0;
163
            if (i == CONSOLE_IRQ) {
164
                char readchar = *(char*)0x10000000;
165
                if (readchar == 0) {
166
                    aux_puts("?");
167
                }
168
                else {
169
                    aux_printf("%c", readchar);
170
                }
171
 
172
            }
173
            else if (i == TIMER_IRQ) {
174
                aux_printf("\n.\n");
2245 stepan 175
                //acknowledge
2235 stepan 176
                *(uint32_t*)0x15000110 = 0;
177
            }
178
        }
179
    }
180
 
181
    if (noirq)
2245 stepan 182
    aux_puts("IRQ exception without source\n");*/
2235 stepan 183
}
184
 
2179 stepan 185
/** Fills exception vectors with appropriate exception handlers.
186
*/
2235 stepan 187
void install_exception_handlers(void)
188
{
189
    install_handler((unsigned)reset_exception_entry,
190
             (unsigned*)EXC_RESET_VEC);
191
 
192
    install_handler((unsigned)undef_instr_exception_entry,
193
             (unsigned*)EXC_UNDEF_INSTR_VEC);
194
 
195
    install_handler((unsigned)swi_exception_entry,
196
             (unsigned*)EXC_SWI_VEC);
197
 
198
    install_handler((unsigned)prefetch_abort_exception_entry,
199
             (unsigned*)EXC_PREFETCH_ABORT_VEC);
200
 
201
    install_handler((unsigned)data_abort_exception_entry,
202
             (unsigned*)EXC_DATA_ABORT_VEC);
203
 
204
    install_handler((unsigned)irq_exception_entry,  
205
             (unsigned*)EXC_IRQ_VEC);
206
 
207
    install_handler((unsigned)fiq_exception_entry,
208
             (unsigned*)EXC_FIQ_VEC);
2179 stepan 209
}
210
 
2245 stepan 211
/** Initializes exception handling.
212
 *
213
 * Installs low-level exception handlers and then registers
214
 * exceptions and their handlers to kernel exception dispatcher.
215
 */
2235 stepan 216
void exception_init(void)
217
{
2245 stepan 218
    install_exception_handlers();
219
 
2235 stepan 220
    exc_register(EXC_IRQ, "interrupt", (iroutine) irq_exception);
221
    /* TODO add next */
2179 stepan 222
}
223
 
2235 stepan 224
/* TODO change soon, temporary hack. */
225
void setup_exception_stacks()
226
{
227
    /* switch to particular mode and set "sp" there */
228
    uint32_t cspr = current_status_reg_read();
229
    current_status_reg_control_write(
230
            (cspr & ~STATUS_REG_MODE_MASK) | IRQ_MODE
231
    );
232
    asm("ldr sp, =irq_stack");
233
    current_status_reg_control_write( cspr);
2245 stepan 234
 
235
    /* TODO if you want to test other exceptions than IRQ,
236
    make stack analogous to irq_stack (in start.S),
237
    and then set stack pointer here */
2235 stepan 238
}
239
 
2179 stepan 240
/** @}
241
 */