2 * arch/xtensa/kernel/vectors.S
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
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.
13 * Copyright (C) 2005 Tensilica, Inc.
15 * Chris Zankel <chris@zankel.net>
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.
26 * Fast handler entry condition:
28 * a0: trashed, original value saved on stack (PT_AREG0)
30 * a2: new stack pointer, original value in depc
32 * depc: a2, original value saved on stack (PT_DEPC)
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:
38 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception
39 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
41 * Note: Neither the kernel nor the user exception handler generate literals.
45 #include <linux/linkage.h>
46 #include <asm/ptrace.h>
47 #include <asm/current.h>
48 #include <asm/asm-offsets.h>
49 #include <asm/pgtable.h>
50 #include <asm/processor.h>
52 #include <asm/thread_info.h>
54 #define WINDOW_VECTORS_SIZE 0x180
58 * User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)
60 * We get here when an exception occurred while we were in userland.
61 * We switch to the kernel stack and jump to the first level handler
62 * associated to the exception cause.
64 * Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already
65 * decremented by PT_USER_SIZE.
68 .section .UserExceptionVector.text, "ax"
70 ENTRY(_UserExceptionVector)
72 xsr a3, excsave1 # save a3 and get dispatch table
73 wsr a2, depc # save a2
74 l32i a2, a3, EXC_TABLE_KSTK # load kernel stack to a2
75 s32i a0, a2, PT_AREG0 # save a0 to ESF
76 rsr a0, exccause # retrieve exception cause
77 s32i a0, a2, PT_DEPC # mark it as a regular exception
78 addx4 a0, a0, a3 # find entry in table
79 l32i a0, a0, EXC_TABLE_FAST_USER # load handler
82 ENDPROC(_UserExceptionVector)
85 * Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)
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.
91 * Note: we need to preserve space for the spill region.
94 .section .KernelExceptionVector.text, "ax"
96 ENTRY(_KernelExceptionVector)
98 xsr a3, excsave1 # 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
108 ENDPROC(_KernelExceptionVector)
111 * Double exception vector (Exceptions with PS.EXCM == 1)
112 * We get this exception when another exception occurs while were are
113 * already in an exception, such as window overflow/underflow exception,
114 * or 'expected' exceptions, for example memory exception when we were trying
115 * to read data from an invalid address in user space.
117 * Note that this vector is never invoked for level-1 interrupts, because such
118 * interrupts are disabled (masked) when PS.EXCM is set.
120 * We decode the exception and take the appropriate action. However, the
121 * double exception vector is much more careful, because a lot more error
122 * cases go through the double exception vector than through the user and
123 * kernel exception vectors.
125 * Occasionally, the kernel expects a double exception to occur. This usually
126 * happens when accessing user-space memory with the user's permissions
127 * (l32e/s32e instructions). The kernel state, though, is not always suitable
128 * for immediate transfer of control to handle_double, where "normal" exception
129 * processing occurs. Also in kernel mode, TLB misses can occur if accessing
130 * vmalloc memory, possibly requiring repair in a double exception handler.
132 * The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as
133 * a boolean variable and a pointer to a fixup routine. If the variable
134 * EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of
135 * zero indicates to use the default kernel/user exception handler.
136 * There is only one exception, when the value is identical to the exc_table
137 * label, the kernel is in trouble. This mechanism is used to protect critical
138 * sections, mainly when the handler writes to the stack to assert the stack
139 * pointer is valid. Once the fixup/default handler leaves that area, the
140 * EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.
142 * Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the
143 * nonzero address of a fixup routine before it could cause a double exception
144 * and reset it before it returns.
146 * Some other things to take care of when a fast exception handler doesn't
147 * specify a particular fixup handler but wants to use the default handlers:
149 * - The original stack pointer (in a1) must not be modified. The fast
150 * exception handler should only use a2 as the stack pointer.
152 * - If the fast handler manipulates the stack pointer (in a2), it has to
153 * register a valid fixup handler and cannot use the default handlers.
155 * - The handler can use any other generic register from a3 to a15, but it
156 * must save the content of these registers to stack (PT_AREG3...PT_AREGx)
158 * - These registers must be saved before a double exception can occur.
160 * - If we ever implement handling signals while in double exceptions, the
161 * number of registers a fast handler has saved (excluding a0 and a1) must
162 * be written to PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )
164 * The fixup handlers are special handlers:
166 * - Fixup entry conditions differ from regular exceptions:
170 * a2: trashed, original value in EXC_TABLE_DOUBLE_A2
175 * - When the kernel enters the fixup handler, it still assumes it is in a
176 * critical section, so EXC_TABLE_FIXUP variable is set to exc_table.
177 * The fixup handler, therefore, has to re-register itself as the fixup
178 * handler before it returns from the double exception.
180 * - Fixup handler can share the same exception frame with the fast handler.
181 * The kernel stack pointer is not changed when entering the fixup handler.
183 * - Fixup handlers can jump to the default kernel and user exception
184 * handlers. Before it jumps, though, it has to setup a exception frame
185 * on stack. Because the default handler resets the register fixup handler
186 * the fixup handler must make sure that the default handler returns to
187 * it instead of the exception address, so it can re-register itself as
190 * In case of a critical condition where the kernel cannot recover, we jump
191 * to unrecoverable_exception with the following entry conditions.
192 * All registers a0...a15 are unchanged from the last exception, except:
194 * a0: last address before we jumped to the unrecoverable_exception.
198 * See the handle_alloca_user and spill_registers routines for example clients.
200 * FIXME: Note: we currently don't allow signal handling coming from a double
201 * exception, so the item markt with (*) is not required.
204 .section .DoubleExceptionVector.text, "ax"
205 .begin literal_prefix .DoubleExceptionVector
207 ENTRY(_DoubleExceptionVector)
209 /* Deliberately destroy excsave (don't assume it's value was valid). */
211 wsr a3, excsave1 # save a3
213 /* Check for kernel double exception (usually fatal). */
216 _bbci.l a3, PS_UM_BIT, .Lksp
218 /* Check if we are currently handling a window exception. */
219 /* Note: We don't need to indicate that we enter a critical section. */
221 xsr a0, depc # get DEPC, save a0
223 movi a3, XCHAL_WINDOW_VECTORS_VADDR
224 _bltu a0, a3, .Lfixup
225 addi a3, a3, WINDOW_VECTORS_SIZE
226 _bgeu a0, a3, .Lfixup
228 /* Window overflow/underflow exception. Get stack pointer. */
231 /* This explicit literal and the following references to it are made
232 * in order to fit DoubleExceptionVector.literals into the available
233 * 16-byte gap before DoubleExceptionVector.text in the absence of
234 * link time relaxation. See kernel/vmlinux.lds.S
236 .literal .Lexc_table, exc_table
238 l32i a2, a2, EXC_TABLE_KSTK
240 /* Check for overflow/underflow exception, jump if overflow. */
242 _bbci.l a0, 6, .Lovfl
244 /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
246 /* Restart window underflow exception.
247 * We return to the instruction in user space that caused the window
248 * underflow exception. Therefore, we change window base to the value
249 * before we entered the window underflow exception and prepare the
250 * registers to return as if we were coming from a regular exception
251 * by changing depc (in a0).
252 * Note: We can trash the current window frame (a0...a3) and depc!
255 wsr a2, depc # save stack pointer temporarily
257 extui a0, a0, PS_OWB_SHIFT, 4
261 /* We are now in the previous window frame. Save registers again. */
263 xsr a2, depc # save a2 and get stack pointer
264 s32i a0, a2, PT_AREG0
266 wsr a3, excsave1 # save a3
270 s32i a0, a2, PT_DEPC # mark it as a regular exception
272 l32i a0, a0, EXC_TABLE_FAST_USER
275 .Lfixup:/* Check for a fixup handler or if we were in a critical section. */
277 /* a0: depc, a1: a1, a2: a2, a3: trashed, depc: a0, excsave1: a3 */
280 s32i a2, a3, EXC_TABLE_DOUBLE_SAVE # temporary variable
282 /* Enter critical section. */
284 l32i a2, a3, EXC_TABLE_FIXUP
285 s32i a3, a3, EXC_TABLE_FIXUP
286 beq a2, a3, .Lunrecoverable_fixup # critical!
287 beqz a2, .Ldflt # no handler was registered
289 /* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */
293 .Ldflt: /* Get stack pointer. */
295 l32i a3, a3, EXC_TABLE_DOUBLE_SAVE
296 addi a2, a3, -PT_USER_SIZE
298 .Lovfl: /* Jump to default handlers. */
300 /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
304 s32i a3, a2, PT_AREG0
306 /* a0: avail, a1: a1, a2: kstk, a3: avail, depc: a2, excsave: a3 */
311 l32i a0, a0, EXC_TABLE_FAST_USER
315 * We only allow the ITLB miss exception if we are in kernel space.
316 * All other exceptions are unexpected and thus unrecoverable!
320 .extern fast_second_level_miss_double_kernel
322 .Lksp: /* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */
325 beqi a3, EXCCAUSE_ITLB_MISS, 1f
326 addi a3, a3, -EXCCAUSE_DTLB_MISS
327 bnez a3, .Lunrecoverable
328 1: movi a3, fast_second_level_miss_double_kernel
331 .equ .Lksp, .Lunrecoverable
334 /* Critical! We can't handle this situation. PANIC! */
336 .extern unrecoverable_exception
338 .Lunrecoverable_fixup:
339 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
345 movi a0, unrecoverable_exception
350 ENDPROC(_DoubleExceptionVector)
353 * Debug interrupt vector
355 * There is not much space here, so simply jump to another handler.
356 * EXCSAVE[DEBUGLEVEL] has been set to that handler.
359 .section .DebugInterruptVector.text, "ax"
361 ENTRY(_DebugInterruptVector)
363 xsr a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
366 ENDPROC(_DebugInterruptVector)
369 /* Window overflow and underflow handlers.
370 * The handlers must be 64 bytes apart, first starting with the underflow
371 * handlers underflow-4 to underflow-12, then the overflow handlers
372 * overflow-4 to overflow-12.
374 * Note: We rerun the underflow handlers if we hit an exception, so
375 * we try to access any page that would cause a page fault early.
378 #define ENTRY_ALIGN64(name) \
383 .section .WindowVectors.text, "ax"
386 /* 4-Register Window Overflow Vector (Handler) */
388 ENTRY_ALIGN64(_WindowOverflow4)
396 ENDPROC(_WindowOverflow4)
399 /* 4-Register Window Underflow Vector (Handler) */
401 ENTRY_ALIGN64(_WindowUnderflow4)
409 ENDPROC(_WindowUnderflow4)
411 /* 8-Register Window Overflow Vector (Handler) */
413 ENTRY_ALIGN64(_WindowOverflow8)
426 ENDPROC(_WindowOverflow8)
428 /* 8-Register Window Underflow Vector (Handler) */
430 ENTRY_ALIGN64(_WindowUnderflow8)
443 ENDPROC(_WindowUnderflow8)
445 /* 12-Register Window Overflow Vector (Handler) */
447 ENTRY_ALIGN64(_WindowOverflow12)
464 ENDPROC(_WindowOverflow12)
466 /* 12-Register Window Underflow Vector (Handler) */
468 ENTRY_ALIGN64(_WindowUnderflow12)
485 ENDPROC(_WindowUnderflow12)