xref: /openbmc/linux/arch/xtensa/kernel/vectors.S (revision 93dc544c)
1/*
2 * arch/xtensa/kernel/vectors.S
3 *
4 * This file contains all exception vectors (user, kernel, and double),
5 * as well as the window vectors (overflow and underflow), and the debug
6 * vector. These are the primary vectors executed by the processor if an
7 * exception occurs.
8 *
9 * This file is subject to the terms and conditions of the GNU General
10 * Public License.  See the file "COPYING" in the main directory of
11 * this archive for more details.
12 *
13 * Copyright (C) 2005 Tensilica, Inc.
14 *
15 * Chris Zankel <chris@zankel.net>
16 *
17 */
18
19/*
20 * We use a two-level table approach. The user and kernel exception vectors
21 * use a first-level dispatch table to dispatch the exception to a registered
22 * fast handler or the default handler, if no fast handler was registered.
23 * The default handler sets up a C-stack and dispatches the exception to a
24 * registerd C handler in the second-level dispatch table.
25 *
26 * Fast handler entry condition:
27 *
28 *   a0:	trashed, original value saved on stack (PT_AREG0)
29 *   a1:	a1
30 *   a2:	new stack pointer, original value in depc
31 *   a3:	dispatch table
32 *   depc:	a2, original value saved on stack (PT_DEPC)
33 *   excsave_1:	a3
34 *
35 * The value for PT_DEPC saved to stack also functions as a boolean to
36 * indicate that the exception is either a double or a regular exception:
37 *
38 *   PT_DEPC	>= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception
39 *		<  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
40 *
41 * Note:  Neither the kernel nor the user exception handler generate literals.
42 *
43 */
44
45#include <linux/linkage.h>
46#include <asm/ptrace.h>
47#include <asm/ptrace.h>
48#include <asm/current.h>
49#include <asm/asm-offsets.h>
50#include <asm/pgtable.h>
51#include <asm/processor.h>
52#include <asm/page.h>
53#include <asm/thread_info.h>
54#include <asm/processor.h>
55
56#define WINDOW_VECTORS_SIZE   0x180
57
58
59/*
60 * User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)
61 *
62 * We get here when an exception occurred while we were in userland.
63 * We switch to the kernel stack and jump to the first level handler
64 * associated to the exception cause.
65 *
66 * Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already
67 *       decremented by PT_USER_SIZE.
68 */
69
70	.section .UserExceptionVector.text, "ax"
71
72ENTRY(_UserExceptionVector)
73
74	xsr	a3, EXCSAVE_1		# save a3 and get dispatch table
75	wsr	a2, DEPC		# save a2
76	l32i	a2, a3, EXC_TABLE_KSTK	# load kernel stack to a2
77	s32i	a0, a2, PT_AREG0	# save a0 to ESF
78	rsr	a0, EXCCAUSE		# retrieve exception cause
79	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
80	addx4	a0, a0, a3		# find entry in table
81	l32i	a0, a0, EXC_TABLE_FAST_USER	# load handler
82	jx	a0
83
84/*
85 * Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)
86 *
87 * We get this exception when we were already in kernel space.
88 * We decrement the current stack pointer (kernel) by PT_SIZE and
89 * jump to the first-level handler associated with the exception cause.
90 *
91 * Note: we need to preserve space for the spill region.
92 */
93
94	.section .KernelExceptionVector.text, "ax"
95
96ENTRY(_KernelExceptionVector)
97
98	xsr	a3, EXCSAVE_1		# save a3, and get dispatch table
99	wsr	a2, DEPC		# save a2
100	addi	a2, a1, -16-PT_SIZE	# adjust stack pointer
101	s32i	a0, a2, PT_AREG0	# save a0 to ESF
102	rsr	a0, EXCCAUSE		# retrieve exception cause
103	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
104	addx4	a0, a0, a3		# find entry in table
105	l32i	a0, a0, EXC_TABLE_FAST_KERNEL	# load handler address
106	jx	a0
107
108
109/*
110 * Double exception vector (Exceptions with PS.EXCM == 1)
111 * We get this exception when another exception occurs while were are
112 * already in an exception, such as window overflow/underflow exception,
113 * or 'expected' exceptions, for example memory exception when we were trying
114 * to read data from an invalid address in user space.
115 *
116 * Note that this vector is never invoked for level-1 interrupts, because such
117 * interrupts are disabled (masked) when PS.EXCM is set.
118 *
119 * We decode the exception and take the appropriate action.  However, the
120 * double exception vector is much more careful, because a lot more error
121 * cases go through the double exception vector than through the user and
122 * kernel exception vectors.
123 *
124 * Occasionally, the kernel expects a double exception to occur.  This usually
125 * happens when accessing user-space memory with the user's permissions
126 * (l32e/s32e instructions).  The kernel state, though, is not always suitable
127 * for immediate transfer of control to handle_double, where "normal" exception
128 * processing occurs. Also in kernel mode, TLB misses can occur if accessing
129 * vmalloc memory, possibly requiring repair in a double exception handler.
130 *
131 * The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as
132 * a boolean variable and a pointer to a fixup routine. If the variable
133 * EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of
134 * zero indicates to use the default kernel/user exception handler.
135 * There is only one exception, when the value is identical to the exc_table
136 * label, the kernel is in trouble. This mechanism is used to protect critical
137 * sections, mainly when the handler writes to the stack to assert the stack
138 * pointer is valid. Once the fixup/default handler leaves that area, the
139 * EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.
140 *
141 * Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the
142 * nonzero address of a fixup routine before it could cause a double exception
143 * and reset it before it returns.
144 *
145 * Some other things to take care of when a fast exception handler doesn't
146 * specify a particular fixup handler but wants to use the default handlers:
147 *
148 *  - The original stack pointer (in a1) must not be modified. The fast
149 *    exception handler should only use a2 as the stack pointer.
150 *
151 *  - If the fast handler manipulates the stack pointer (in a2), it has to
152 *    register a valid fixup handler and cannot use the default handlers.
153 *
154 *  - The handler can use any other generic register from a3 to a15, but it
155 *    must save the content of these registers to stack (PT_AREG3...PT_AREGx)
156 *
157 *  - These registers must be saved before a double exception can occur.
158 *
159 *  - If we ever implement handling signals while in double exceptions, the
160 *    number of registers a fast handler has saved (excluding a0 and a1) must
161 *    be written to  PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )
162 *
163 * The fixup handlers are special handlers:
164 *
165 *  - Fixup entry conditions differ from regular exceptions:
166 *
167 *	a0:	   DEPC
168 *	a1: 	   a1
169 *	a2:	   trashed, original value in EXC_TABLE_DOUBLE_A2
170 *	a3:	   exctable
171 *	depc:	   a0
172 *	excsave_1: a3
173 *
174 *  - When the kernel enters the fixup handler, it still assumes it is in a
175 *    critical section, so EXC_TABLE_FIXUP variable is set to exc_table.
176 *    The fixup handler, therefore, has to re-register itself as the fixup
177 *    handler before it returns from the double exception.
178 *
179 *  - Fixup handler can share the same exception frame with the fast handler.
180 *    The kernel stack pointer is not changed when entering the fixup handler.
181 *
182 *  - Fixup handlers can jump to the default kernel and user exception
183 *    handlers. Before it jumps, though, it has to setup a exception frame
184 *    on stack. Because the default handler resets the register fixup handler
185 *    the fixup handler must make sure that the default handler returns to
186 *    it instead of the exception address, so it can re-register itself as
187 *    the fixup handler.
188 *
189 * In case of a critical condition where the kernel cannot recover, we jump
190 * to unrecoverable_exception with the following entry conditions.
191 * All registers a0...a15 are unchanged from the last exception, except:
192 *
193 *	a0:	   last address before we jumped to the unrecoverable_exception.
194 *	excsave_1: a0
195 *
196 *
197 * See the handle_alloca_user and spill_registers routines for example clients.
198 *
199 * FIXME: Note: we currently don't allow signal handling coming from a double
200 *        exception, so the item markt with (*) is not required.
201 */
202
203	.section .DoubleExceptionVector.text, "ax"
204	.begin literal_prefix .DoubleExceptionVector
205
206ENTRY(_DoubleExceptionVector)
207
208	/* Deliberately destroy excsave (don't assume it's value was valid). */
209
210	wsr	a3, EXCSAVE_1		# save a3
211
212	/* Check for kernel double exception (usually fatal). */
213
214	rsr	a3, PS
215	_bbci.l	a3, PS_UM_BIT, .Lksp
216
217	/* Check if we are currently handling a window exception. */
218	/* Note: We don't need to indicate that we enter a critical section. */
219
220	xsr	a0, DEPC		# get DEPC, save a0
221
222	movi	a3, XCHAL_WINDOW_VECTORS_VADDR
223	_bltu	a0, a3, .Lfixup
224	addi	a3, a3, WINDOW_VECTORS_SIZE
225	_bgeu	a0, a3, .Lfixup
226
227	/* Window overflow/underflow exception. Get stack pointer. */
228
229	mov	a3, a2
230	movi	a2, exc_table
231	l32i	a2, a2, EXC_TABLE_KSTK
232
233	/* Check for overflow/underflow exception, jump if overflow. */
234
235	_bbci.l	a0, 6, .Lovfl
236
237	/* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3  */
238
239	/* Restart window underflow exception.
240	 * We return to the instruction in user space that caused the window
241	 * underflow exception. Therefore, we change window base to the value
242	 * before we entered the window underflow exception and prepare the
243	 * registers to return as if we were coming from a regular exception
244	 * by changing depc (in a0).
245	 * Note: We can trash the current window frame (a0...a3) and depc!
246	 */
247
248	wsr	a2, DEPC		# save stack pointer temporarily
249	rsr	a0, PS
250	extui	a0, a0, PS_OWB_SHIFT, 4
251	wsr	a0, WINDOWBASE
252	rsync
253
254	/* We are now in the previous window frame. Save registers again. */
255
256	xsr	a2, DEPC		# save a2 and get stack pointer
257	s32i	a0, a2, PT_AREG0
258
259	wsr	a3, EXCSAVE_1		# save a3
260	movi	a3, exc_table
261
262	rsr	a0, EXCCAUSE
263	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
264	addx4	a0, a0, a3
265	l32i	a0, a0, EXC_TABLE_FAST_USER
266	jx	a0
267
268.Lfixup:/* Check for a fixup handler or if we were in a critical section. */
269
270	/* a0: depc, a1: a1, a2: a2, a3: trashed, depc: a0, excsave1: a3 */
271
272	movi	a3, exc_table
273	s32i	a2, a3, EXC_TABLE_DOUBLE_SAVE	# temporary variable
274
275	/* Enter critical section. */
276
277	l32i	a2, a3, EXC_TABLE_FIXUP
278	s32i	a3, a3, EXC_TABLE_FIXUP
279	beq	a2, a3, .Lunrecoverable_fixup	# critical!
280	beqz	a2, .Ldflt			# no handler was registered
281
282	/* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */
283
284	jx	a2
285
286.Ldflt:	/* Get stack pointer. */
287
288	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
289	addi	a2, a3, -PT_USER_SIZE
290
291.Lovfl:	/* Jump to default handlers. */
292
293	/* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
294
295	xsr	a3, DEPC
296	s32i	a0, a2, PT_DEPC
297	s32i	a3, a2, PT_AREG0
298
299	/* a0: avail, a1: a1, a2: kstk, a3: avail, depc: a2, excsave: a3 */
300
301	movi	a3, exc_table
302	rsr	a0, EXCCAUSE
303	addx4	a0, a0, a3
304	l32i	a0, a0, EXC_TABLE_FAST_USER
305	jx	a0
306
307	/*
308	 * We only allow the ITLB miss exception if we are in kernel space.
309	 * All other exceptions are unexpected and thus unrecoverable!
310	 */
311
312	.extern fast_second_level_miss_double_kernel
313
314.Lksp:	/* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */
315
316	rsr	a3, EXCCAUSE
317	beqi	a3, EXCCAUSE_ITLB_MISS, 1f
318	addi	a3, a3, -EXCCAUSE_DTLB_MISS
319	bnez	a3, .Lunrecoverable
3201:	movi	a3, fast_second_level_miss_double_kernel
321	jx	a3
322
323	/* Critical! We can't handle this situation. PANIC! */
324
325	.extern unrecoverable_exception
326
327.Lunrecoverable_fixup:
328	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
329	xsr	a0, DEPC
330
331.Lunrecoverable:
332	rsr	a3, EXCSAVE_1
333	wsr	a0, EXCSAVE_1
334	movi	a0, unrecoverable_exception
335	callx0	a0
336
337	.end literal_prefix
338
339
340/*
341 * Debug interrupt vector
342 *
343 * There is not much space here, so simply jump to another handler.
344 * EXCSAVE[DEBUGLEVEL] has been set to that handler.
345 */
346
347	.section .DebugInterruptVector.text, "ax"
348
349ENTRY(_DebugInterruptVector)
350	xsr	a0, EXCSAVE + XCHAL_DEBUGLEVEL
351	jx	a0
352
353
354
355/* Window overflow and underflow handlers.
356 * The handlers must be 64 bytes apart, first starting with the underflow
357 * handlers underflow-4 to underflow-12, then the overflow handlers
358 * overflow-4 to overflow-12.
359 *
360 * Note: We rerun the underflow handlers if we hit an exception, so
361 *	 we try to access any page that would cause a page fault early.
362 */
363
364	.section		.WindowVectors.text, "ax"
365
366
367/* 4-Register Window Overflow Vector (Handler) */
368
369	.align 64
370.global _WindowOverflow4
371_WindowOverflow4:
372	s32e	a0, a5, -16
373	s32e	a1, a5, -12
374	s32e	a2, a5,  -8
375	s32e	a3, a5,  -4
376	rfwo
377
378
379/* 4-Register Window Underflow Vector (Handler) */
380
381	.align 64
382.global _WindowUnderflow4
383_WindowUnderflow4:
384	l32e	a0, a5, -16
385	l32e	a1, a5, -12
386	l32e	a2, a5,  -8
387	l32e	a3, a5,  -4
388	rfwu
389
390
391/* 8-Register Window Overflow Vector (Handler) */
392
393	.align 64
394.global _WindowOverflow8
395_WindowOverflow8:
396	s32e	a0, a9, -16
397	l32e	a0, a1, -12
398	s32e	a2, a9,  -8
399	s32e	a1, a9, -12
400	s32e	a3, a9,  -4
401	s32e	a4, a0, -32
402	s32e	a5, a0, -28
403	s32e	a6, a0, -24
404	s32e	a7, a0, -20
405	rfwo
406
407/* 8-Register Window Underflow Vector (Handler) */
408
409	.align 64
410.global _WindowUnderflow8
411_WindowUnderflow8:
412	l32e	a1, a9, -12
413	l32e	a0, a9, -16
414	l32e	a7, a1, -12
415	l32e	a2, a9,  -8
416	l32e	a4, a7, -32
417	l32e	a3, a9,  -4
418	l32e	a5, a7, -28
419	l32e	a6, a7, -24
420	l32e	a7, a7, -20
421	rfwu
422
423
424/* 12-Register Window Overflow Vector (Handler) */
425
426	.align 64
427.global _WindowOverflow12
428_WindowOverflow12:
429	s32e	a0,  a13, -16
430	l32e	a0,  a1,  -12
431	s32e	a1,  a13, -12
432	s32e	a2,  a13,  -8
433	s32e	a3,  a13,  -4
434	s32e	a4,  a0,  -48
435	s32e	a5,  a0,  -44
436	s32e	a6,  a0,  -40
437	s32e	a7,  a0,  -36
438	s32e	a8,  a0,  -32
439	s32e	a9,  a0,  -28
440	s32e	a10, a0,  -24
441	s32e	a11, a0,  -20
442	rfwo
443
444/* 12-Register Window Underflow Vector (Handler) */
445
446	.align 64
447.global _WindowUnderflow12
448_WindowUnderflow12:
449	l32e	a1,  a13, -12
450	l32e	a0,  a13, -16
451	l32e	a11, a1,  -12
452	l32e	a2,  a13,  -8
453	l32e	a4,  a11, -48
454	l32e	a8,  a11, -32
455	l32e	a3,  a13,  -4
456	l32e	a5,  a11, -44
457	l32e	a6,  a11, -40
458	l32e	a7,  a11, -36
459	l32e	a9,  a11, -28
460	l32e	a10, a11, -24
461	l32e	a11, a11, -20
462	rfwu
463
464	.text
465
466
467