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 - 2008 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 <linux/pgtable.h>
47 #include <asm/asmmacro.h>
48 #include <asm/ptrace.h>
49 #include <asm/current.h>
50 #include <asm/asm-offsets.h>
51 #include <asm/processor.h>
53 #include <asm/thread_info.h>
54 #include <asm/vectors.h>
56 #define WINDOW_VECTORS_SIZE 0x180
60 * User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)
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.
66 * Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already
67 * decremented by PT_USER_SIZE.
70 .section .UserExceptionVector.text, "ax"
72 ENTRY(_UserExceptionVector)
74 xsr a3, excsave1 # 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 xsr a3, excsave1 # restore a3 and dispatch table
85 ENDPROC(_UserExceptionVector)
88 * Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)
90 * We get this exception when we were already in kernel space.
91 * We decrement the current stack pointer (kernel) by PT_KERNEL_SIZE and
92 * jump to the first-level handler associated with the exception cause.
94 * Note: we need to preserve space for the spill region.
97 .section .KernelExceptionVector.text, "ax"
99 ENTRY(_KernelExceptionVector)
101 xsr a3, excsave1 # save a3, and get dispatch table
102 wsr a2, depc # save a2
103 addi a2, a1, -16 - PT_KERNEL_SIZE # adjust stack pointer
104 s32i a0, a2, PT_AREG0 # save a0 to ESF
105 rsr a0, exccause # retrieve exception cause
106 s32i a0, a2, PT_DEPC # mark it as a regular exception
107 addx4 a0, a0, a3 # find entry in table
108 l32i a0, a0, EXC_TABLE_FAST_KERNEL # load handler address
109 xsr a3, excsave1 # restore a3 and dispatch table
112 ENDPROC(_KernelExceptionVector)
115 * Double exception vector (Exceptions with PS.EXCM == 1)
116 * We get this exception when another exception occurs while were are
117 * already in an exception, such as window overflow/underflow exception,
118 * or 'expected' exceptions, for example memory exception when we were trying
119 * to read data from an invalid address in user space.
121 * Note that this vector is never invoked for level-1 interrupts, because such
122 * interrupts are disabled (masked) when PS.EXCM is set.
124 * We decode the exception and take the appropriate action. However, the
125 * double exception vector is much more careful, because a lot more error
126 * cases go through the double exception vector than through the user and
127 * kernel exception vectors.
129 * Occasionally, the kernel expects a double exception to occur. This usually
130 * happens when accessing user-space memory with the user's permissions
131 * (l32e/s32e instructions). The kernel state, though, is not always suitable
132 * for immediate transfer of control to handle_double, where "normal" exception
133 * processing occurs. Also in kernel mode, TLB misses can occur if accessing
134 * vmalloc memory, possibly requiring repair in a double exception handler.
136 * The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as
137 * a boolean variable and a pointer to a fixup routine. If the variable
138 * EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of
139 * zero indicates to use the default kernel/user exception handler.
140 * There is only one exception, when the value is identical to the exc_table
141 * label, the kernel is in trouble. This mechanism is used to protect critical
142 * sections, mainly when the handler writes to the stack to assert the stack
143 * pointer is valid. Once the fixup/default handler leaves that area, the
144 * EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.
146 * Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the
147 * nonzero address of a fixup routine before it could cause a double exception
148 * and reset it before it returns.
150 * Some other things to take care of when a fast exception handler doesn't
151 * specify a particular fixup handler but wants to use the default handlers:
153 * - The original stack pointer (in a1) must not be modified. The fast
154 * exception handler should only use a2 as the stack pointer.
156 * - If the fast handler manipulates the stack pointer (in a2), it has to
157 * register a valid fixup handler and cannot use the default handlers.
159 * - The handler can use any other generic register from a3 to a15, but it
160 * must save the content of these registers to stack (PT_AREG3...PT_AREGx)
162 * - These registers must be saved before a double exception can occur.
164 * - If we ever implement handling signals while in double exceptions, the
165 * number of registers a fast handler has saved (excluding a0 and a1) must
166 * be written to PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )
168 * The fixup handlers are special handlers:
170 * - Fixup entry conditions differ from regular exceptions:
174 * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
179 * - When the kernel enters the fixup handler, it still assumes it is in a
180 * critical section, so EXC_TABLE_FIXUP variable is set to exc_table.
181 * The fixup handler, therefore, has to re-register itself as the fixup
182 * handler before it returns from the double exception.
184 * - Fixup handler can share the same exception frame with the fast handler.
185 * The kernel stack pointer is not changed when entering the fixup handler.
187 * - Fixup handlers can jump to the default kernel and user exception
188 * handlers. Before it jumps, though, it has to setup a exception frame
189 * on stack. Because the default handler resets the register fixup handler
190 * the fixup handler must make sure that the default handler returns to
191 * it instead of the exception address, so it can re-register itself as
194 * In case of a critical condition where the kernel cannot recover, we jump
195 * to unrecoverable_exception with the following entry conditions.
196 * All registers a0...a15 are unchanged from the last exception, except:
198 * a0: last address before we jumped to the unrecoverable_exception.
202 * See the handle_alloca_user and spill_registers routines for example clients.
204 * FIXME: Note: we currently don't allow signal handling coming from a double
205 * exception, so the item markt with (*) is not required.
208 .section .DoubleExceptionVector.text, "ax"
210 ENTRY(_DoubleExceptionVector)
213 s32i a2, a3, EXC_TABLE_DOUBLE_SAVE
215 /* Check for kernel double exception (usually fatal). */
218 _bbsi.l a2, PS_UM_BIT, 1f
224 /* Check if we are currently handling a window exception. */
225 /* Note: We don't need to indicate that we enter a critical section. */
227 xsr a0, depc # get DEPC, save a0
229 #ifdef SUPPORT_WINDOWED
230 movi a2, WINDOW_VECTORS_VADDR
231 _bltu a0, a2, .Lfixup
232 addi a2, a2, WINDOW_VECTORS_SIZE
233 _bgeu a0, a2, .Lfixup
235 /* Window overflow/underflow exception. Get stack pointer. */
237 l32i a2, a3, EXC_TABLE_KSTK
239 /* Check for overflow/underflow exception, jump if overflow. */
241 bbci.l a0, 6, _DoubleExceptionVector_WindowOverflow
244 * Restart window underflow exception.
248 * a2 = new sp based on KSTK from exc_table
250 * excsave_1 = orig a3
252 * We return to the instruction in user space that caused the window
253 * underflow exception. Therefore, we change window base to the value
254 * before we entered the window underflow exception and prepare the
255 * registers to return as if we were coming from a regular exception
256 * by changing depc (in a0).
257 * Note: We can trash the current window frame (a0...a3) and depc!
259 _DoubleExceptionVector_WindowUnderflow:
261 wsr a2, depc # save stack pointer temporarily
263 extui a0, a0, PS_OWB_SHIFT, PS_OWB_WIDTH
267 /* We are now in the previous window frame. Save registers again. */
269 xsr a2, depc # save a2 and get stack pointer
270 s32i a0, a2, PT_AREG0
273 s32i a0, a2, PT_DEPC # mark it as a regular exception
276 l32i a0, a0, EXC_TABLE_FAST_USER
284 * We only allow the ITLB miss exception if we are in kernel space.
285 * All other exceptions are unexpected and thus unrecoverable!
289 .extern fast_second_level_miss_double_kernel
291 .Lksp: /* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */
294 beqi a3, EXCCAUSE_ITLB_MISS, 1f
295 addi a3, a3, -EXCCAUSE_DTLB_MISS
296 bnez a3, .Lunrecoverable
297 1: movi a3, fast_second_level_miss_double_kernel
300 .equ .Lksp, .Lunrecoverable
303 /* Critical! We can't handle this situation. PANIC! */
305 .extern unrecoverable_exception
307 .Lunrecoverable_fixup:
308 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
314 call0 unrecoverable_exception
316 .Lfixup:/* Check for a fixup handler or if we were in a critical section. */
318 /* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave1: a3 */
320 /* Enter critical section. */
322 l32i a2, a3, EXC_TABLE_FIXUP
323 s32i a3, a3, EXC_TABLE_FIXUP
324 beq a2, a3, .Lunrecoverable_fixup # critical section
325 beqz a2, .Ldflt # no handler was registered
327 /* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */
331 .Ldflt: /* Get stack pointer. */
333 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
334 addi a2, a2, -PT_USER_SIZE
336 /* a0: depc, a1: a1, a2: kstk, a3: exctable, depc: a0, excsave: a3 */
339 l32i a0, a3, EXC_TABLE_DOUBLE_SAVE
341 s32i a0, a2, PT_AREG0
343 /* a0: avail, a1: a1, a2: kstk, a3: exctable, depc: a2, excsave: a3 */
348 l32i a0, a0, EXC_TABLE_FAST_USER
351 #ifdef SUPPORT_WINDOWED
353 * Restart window OVERFLOW exception.
357 * a2 = new sp based on KSTK from exc_table
359 * excsave_1 = orig a3
361 * We return to the instruction in user space that caused the window
362 * overflow exception. Therefore, we change window base to the value
363 * before we entered the window overflow exception and prepare the
364 * registers to return as if we were coming from a regular exception
365 * by changing DEPC (in a0).
367 * NOTE: We CANNOT trash the current window frame (a0...a3), but we
370 * The tricky part here is that overflow8 and overflow12 handlers
371 * save a0, then clobber a0. To restart the handler, we have to restore
372 * a0 if the double exception was past the point where a0 was clobbered.
374 * To keep things simple, we take advantage of the fact all overflow
375 * handlers save a0 in their very first instruction. If DEPC was past
376 * that instruction, we can safely restore a0 from where it was saved
379 * a0: depc, a1: a1, a2: kstk, a3: exc_table, depc: a0, excsave1: a3
381 _DoubleExceptionVector_WindowOverflow:
382 extui a2, a0, 0, 6 # get offset into 64-byte vector handler
383 beqz a2, 1f # if at start of vector, don't restore
386 bbsi.l a0, 8, 1f # don't restore except for overflow 8 and 12
389 * This fixup handler is for the extremely unlikely case where the
390 * overflow handler's reference thru a0 gets a hardware TLB refill
391 * that bumps out the (distinct, aliasing) TLB entry that mapped its
392 * prior references thru a9/a13, and where our reference now thru
393 * a9/a13 gets a 2nd-level miss exception (not hardware TLB refill).
395 movi a2, window_overflow_restore_a0_fixup
396 s32i a2, a3, EXC_TABLE_FIXUP
397 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
403 * Restore a0 as saved by _WindowOverflow8().
407 wsr a0, depc # replace the saved a0
412 * Restore a0 as saved by _WindowOverflow12().
416 wsr a0, depc # replace the saved a0
420 s32i a0, a3, EXC_TABLE_FIXUP
421 s32i a2, a3, EXC_TABLE_DOUBLE_SAVE
424 * Restore WindowBase while leaving all address registers restored.
425 * We have to use ROTW for this, because WSR.WINDOWBASE requires
426 * an address register (which would prevent restore).
428 * Window Base goes from 0 ... 7 (Module 8)
429 * Window Start is 8 bits; Ex: (0b1010 1010):0x55 from series of call4s
433 extui a0, a0, PS_OWB_SHIFT, PS_OWB_WIDTH
438 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
447 * We are now in the user code's original window frame.
448 * Process the exception as a user exception as if it was
449 * taken by the user code.
451 * This is similar to the user exception vector,
452 * except that PT_DEPC isn't set to EXCCAUSE.
457 l32i a2, a3, EXC_TABLE_KSTK
458 s32i a0, a2, PT_AREG0
463 _DoubleExceptionVector_handle_exception:
464 addi a0, a0, -EXCCAUSE_UNALIGNED
467 l32i a0, a0, EXC_TABLE_FAST_USER + 4 * EXCCAUSE_UNALIGNED
471 movi a0, user_exception
486 ENDPROC(_DoubleExceptionVector)
488 #ifdef SUPPORT_WINDOWED
491 * Fixup handler for TLB miss in double exception handler for window owerflow.
492 * We get here with windowbase set to the window that was being spilled and
493 * a0 trashed. a0 bit 7 determines if this is a call8 (bit clear) or call12
496 * We do the following here:
497 * - go to the original window retaining a0 value;
498 * - set up exception stack to return back to appropriate a0 restore code
499 * (we'll need to rotate window back and there's no place to save this
500 * information, use different return address for that);
501 * - handle the exception;
502 * - go to the window that was being spilled;
503 * - set up window_overflow_restore_a0_fixup as a fixup routine;
505 * - restore the original window;
506 * - reset the default fixup routine;
507 * - return to user. By the time we get to this fixup handler all information
508 * about the conditions of the original double exception that happened in
509 * the window overflow handler is lost, so we just return to userspace to
510 * retry overflow from start.
512 * a0: value of depc, original value in depc
513 * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
514 * a3: exctable, original value in excsave1
520 ENTRY(window_overflow_restore_a0_fixup)
523 extui a0, a0, PS_OWB_SHIFT, PS_OWB_WIDTH
527 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
530 _beqi a0, 1, .Lhandle_1
531 _beqi a0, 3, .Lhandle_3
533 .macro overflow_fixup_handle_exception_pane n
540 l32i a2, a3, EXC_TABLE_KSTK
541 s32i a0, a2, PT_AREG0
543 movi a0, .Lrestore_\n
546 j _DoubleExceptionVector_handle_exception
550 overflow_fixup_handle_exception_pane 2
552 overflow_fixup_handle_exception_pane 1
554 overflow_fixup_handle_exception_pane 3
556 .macro overflow_fixup_restore_a0_pane n
559 /* Need to preserve a0 value here to be able to handle exception
560 * that may occur on a0 reload from stack. It may occur because
561 * TLB miss handler may not be atomic and pointer to page table
562 * may be lost before we get here. There are no free registers,
563 * so we need to use EXC_TABLE_DOUBLE_SAVE area.
566 s32i a2, a3, EXC_TABLE_DOUBLE_SAVE
567 movi a2, window_overflow_restore_a0_fixup
568 s32i a2, a3, EXC_TABLE_FIXUP
569 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
582 overflow_fixup_restore_a0_pane 2
586 s32i a2, a3, EXC_TABLE_DOUBLE_SAVE
588 s32i a2, a3, EXC_TABLE_FIXUP
589 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
594 overflow_fixup_restore_a0_pane 1
595 j .Lset_default_fixup
597 overflow_fixup_restore_a0_pane 3
598 j .Lset_default_fixup
600 ENDPROC(window_overflow_restore_a0_fixup)
605 * Debug interrupt vector
607 * There is not much space here, so simply jump to another handler.
608 * EXCSAVE[DEBUGLEVEL] has been set to that handler.
611 .section .DebugInterruptVector.text, "ax"
613 ENTRY(_DebugInterruptVector)
615 xsr a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
616 s32i a0, a3, DT_DEBUG_SAVE
617 l32i a0, a3, DT_DEBUG_EXCEPTION
620 ENDPROC(_DebugInterruptVector)
625 * Medium priority level interrupt vectors
627 * Each takes less than 16 (0x10) bytes, no literals, by placing
628 * the extra 8 bytes that would otherwise be required in the window
629 * vectors area where there is space. With relocatable vectors,
630 * all vectors are within ~ 4 kB range of each other, so we can
631 * simply jump (J) to another vector without having to use JX.
633 * common_exception code gets current IRQ level in PS.INTLEVEL
634 * and preserves it for the IRQ handling time.
637 .macro irq_entry_level level
639 .if XCHAL_EXCM_LEVEL >= \level
640 .section .Level\level\()InterruptVector.text, "ax"
641 ENTRY(_Level\level\()InterruptVector)
645 .if \level <= LOCKLEVEL
646 movi a0, EXCCAUSE_LEVEL1_INTERRUPT
648 movi a0, EXCCAUSE_MAPPED_NMI
652 # branch to user or kernel vector
653 j _SimulateUserKernelVectorException
664 #if XCHAL_EXCM_LEVEL >= 2
666 * Continuation of medium priority interrupt dispatch code.
667 * On entry here, a0 contains PS, and EPC2 contains saved a0:
671 _SimulateUserKernelVectorException:
672 addi a0, a0, (1 << PS_EXCM_BIT)
676 bbsi.l a0, PS_UM_BIT, 1f # branch if user mode
677 xsr a0, excsave2 # restore a0
678 j _KernelExceptionVector # simulate kernel vector exception
679 1: xsr a0, excsave2 # restore a0
680 j _UserExceptionVector # simulate user vector exception
684 /* Window overflow and underflow handlers.
685 * The handlers must be 64 bytes apart, first starting with the underflow
686 * handlers underflow-4 to underflow-12, then the overflow handlers
687 * overflow-4 to overflow-12.
689 * Note: We rerun the underflow handlers if we hit an exception, so
690 * we try to access any page that would cause a page fault early.
693 #define ENTRY_ALIGN64(name) \
698 .section .WindowVectors.text, "ax"
701 #ifdef SUPPORT_WINDOWED
703 /* 4-Register Window Overflow Vector (Handler) */
705 ENTRY_ALIGN64(_WindowOverflow4)
713 ENDPROC(_WindowOverflow4)
715 /* 4-Register Window Underflow Vector (Handler) */
717 ENTRY_ALIGN64(_WindowUnderflow4)
725 ENDPROC(_WindowUnderflow4)
727 /* 8-Register Window Overflow Vector (Handler) */
729 ENTRY_ALIGN64(_WindowOverflow8)
742 ENDPROC(_WindowOverflow8)
744 /* 8-Register Window Underflow Vector (Handler) */
746 ENTRY_ALIGN64(_WindowUnderflow8)
759 ENDPROC(_WindowUnderflow8)
761 /* 12-Register Window Overflow Vector (Handler) */
763 ENTRY_ALIGN64(_WindowOverflow12)
780 ENDPROC(_WindowOverflow12)
782 /* 12-Register Window Underflow Vector (Handler) */
784 ENTRY_ALIGN64(_WindowUnderflow12)
801 ENDPROC(_WindowUnderflow12)