Subversion Repositories HelenOS

Rev

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