Subversion Repositories HelenOS

Rev

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