Subversion Repositories HelenOS

Rev

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