/*1* arch/xtensa/kernel/vectors.S2*3* This file contains all exception vectors (user, kernel, and double),4* as well as the window vectors (overflow and underflow), and the debug5* vector. These are the primary vectors executed by the processor if an6* exception occurs.7*8* This file is subject to the terms and conditions of the GNU General9* Public License. See the file "COPYING" in the main directory of10* this archive for more details.11*12* Copyright (C) 2005 Tensilica, Inc.13*14* Chris Zankel <[email protected]>15*16*/1718/*19* We use a two-level table approach. The user and kernel exception vectors20* use a first-level dispatch table to dispatch the exception to a registered21* fast handler or the default handler, if no fast handler was registered.22* The default handler sets up a C-stack and dispatches the exception to a23* registerd C handler in the second-level dispatch table.24*25* Fast handler entry condition:26*27* a0: trashed, original value saved on stack (PT_AREG0)28* a1: a129* a2: new stack pointer, original value in depc30* a3: dispatch table31* depc: a2, original value saved on stack (PT_DEPC)32* excsave_1: a333*34* The value for PT_DEPC saved to stack also functions as a boolean to35* indicate that the exception is either a double or a regular exception:36*37* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception38* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception39*40* Note: Neither the kernel nor the user exception handler generate literals.41*42*/4344#include <linux/linkage.h>45#include <asm/ptrace.h>46#include <asm/current.h>47#include <asm/asm-offsets.h>48#include <asm/pgtable.h>49#include <asm/processor.h>50#include <asm/page.h>51#include <asm/thread_info.h>5253#define WINDOW_VECTORS_SIZE 0x180545556/*57* User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)58*59* We get here when an exception occurred while we were in userland.60* We switch to the kernel stack and jump to the first level handler61* associated to the exception cause.62*63* Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already64* decremented by PT_USER_SIZE.65*/6667.section .UserExceptionVector.text, "ax"6869ENTRY(_UserExceptionVector)7071xsr a3, EXCSAVE_1 # save a3 and get dispatch table72wsr a2, DEPC # save a273l32i a2, a3, EXC_TABLE_KSTK # load kernel stack to a274s32i a0, a2, PT_AREG0 # save a0 to ESF75rsr a0, EXCCAUSE # retrieve exception cause76s32i a0, a2, PT_DEPC # mark it as a regular exception77addx4 a0, a0, a3 # find entry in table78l32i a0, a0, EXC_TABLE_FAST_USER # load handler79jx a08081/*82* Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)83*84* We get this exception when we were already in kernel space.85* We decrement the current stack pointer (kernel) by PT_SIZE and86* jump to the first-level handler associated with the exception cause.87*88* Note: we need to preserve space for the spill region.89*/9091.section .KernelExceptionVector.text, "ax"9293ENTRY(_KernelExceptionVector)9495xsr a3, EXCSAVE_1 # save a3, and get dispatch table96wsr a2, DEPC # save a297addi a2, a1, -16-PT_SIZE # adjust stack pointer98s32i a0, a2, PT_AREG0 # save a0 to ESF99rsr a0, EXCCAUSE # retrieve exception cause100s32i a0, a2, PT_DEPC # mark it as a regular exception101addx4 a0, a0, a3 # find entry in table102l32i a0, a0, EXC_TABLE_FAST_KERNEL # load handler address103jx a0104105106/*107* Double exception vector (Exceptions with PS.EXCM == 1)108* We get this exception when another exception occurs while were are109* already in an exception, such as window overflow/underflow exception,110* or 'expected' exceptions, for example memory exception when we were trying111* to read data from an invalid address in user space.112*113* Note that this vector is never invoked for level-1 interrupts, because such114* interrupts are disabled (masked) when PS.EXCM is set.115*116* We decode the exception and take the appropriate action. However, the117* double exception vector is much more careful, because a lot more error118* cases go through the double exception vector than through the user and119* kernel exception vectors.120*121* Occasionally, the kernel expects a double exception to occur. This usually122* happens when accessing user-space memory with the user's permissions123* (l32e/s32e instructions). The kernel state, though, is not always suitable124* for immediate transfer of control to handle_double, where "normal" exception125* processing occurs. Also in kernel mode, TLB misses can occur if accessing126* vmalloc memory, possibly requiring repair in a double exception handler.127*128* The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as129* a boolean variable and a pointer to a fixup routine. If the variable130* EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of131* zero indicates to use the default kernel/user exception handler.132* There is only one exception, when the value is identical to the exc_table133* label, the kernel is in trouble. This mechanism is used to protect critical134* sections, mainly when the handler writes to the stack to assert the stack135* pointer is valid. Once the fixup/default handler leaves that area, the136* EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.137*138* Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the139* nonzero address of a fixup routine before it could cause a double exception140* and reset it before it returns.141*142* Some other things to take care of when a fast exception handler doesn't143* specify a particular fixup handler but wants to use the default handlers:144*145* - The original stack pointer (in a1) must not be modified. The fast146* exception handler should only use a2 as the stack pointer.147*148* - If the fast handler manipulates the stack pointer (in a2), it has to149* register a valid fixup handler and cannot use the default handlers.150*151* - The handler can use any other generic register from a3 to a15, but it152* must save the content of these registers to stack (PT_AREG3...PT_AREGx)153*154* - These registers must be saved before a double exception can occur.155*156* - If we ever implement handling signals while in double exceptions, the157* number of registers a fast handler has saved (excluding a0 and a1) must158* be written to PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )159*160* The fixup handlers are special handlers:161*162* - Fixup entry conditions differ from regular exceptions:163*164* a0: DEPC165* a1: a1166* a2: trashed, original value in EXC_TABLE_DOUBLE_A2167* a3: exctable168* depc: a0169* excsave_1: a3170*171* - When the kernel enters the fixup handler, it still assumes it is in a172* critical section, so EXC_TABLE_FIXUP variable is set to exc_table.173* The fixup handler, therefore, has to re-register itself as the fixup174* handler before it returns from the double exception.175*176* - Fixup handler can share the same exception frame with the fast handler.177* The kernel stack pointer is not changed when entering the fixup handler.178*179* - Fixup handlers can jump to the default kernel and user exception180* handlers. Before it jumps, though, it has to setup a exception frame181* on stack. Because the default handler resets the register fixup handler182* the fixup handler must make sure that the default handler returns to183* it instead of the exception address, so it can re-register itself as184* the fixup handler.185*186* In case of a critical condition where the kernel cannot recover, we jump187* to unrecoverable_exception with the following entry conditions.188* All registers a0...a15 are unchanged from the last exception, except:189*190* a0: last address before we jumped to the unrecoverable_exception.191* excsave_1: a0192*193*194* See the handle_alloca_user and spill_registers routines for example clients.195*196* FIXME: Note: we currently don't allow signal handling coming from a double197* exception, so the item markt with (*) is not required.198*/199200.section .DoubleExceptionVector.text, "ax"201.begin literal_prefix .DoubleExceptionVector202203ENTRY(_DoubleExceptionVector)204205/* Deliberately destroy excsave (don't assume it's value was valid). */206207wsr a3, EXCSAVE_1 # save a3208209/* Check for kernel double exception (usually fatal). */210211rsr a3, PS212_bbci.l a3, PS_UM_BIT, .Lksp213214/* Check if we are currently handling a window exception. */215/* Note: We don't need to indicate that we enter a critical section. */216217xsr a0, DEPC # get DEPC, save a0218219movi a3, XCHAL_WINDOW_VECTORS_VADDR220_bltu a0, a3, .Lfixup221addi a3, a3, WINDOW_VECTORS_SIZE222_bgeu a0, a3, .Lfixup223224/* Window overflow/underflow exception. Get stack pointer. */225226mov a3, a2227movi a2, exc_table228l32i a2, a2, EXC_TABLE_KSTK229230/* Check for overflow/underflow exception, jump if overflow. */231232_bbci.l a0, 6, .Lovfl233234/* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */235236/* Restart window underflow exception.237* We return to the instruction in user space that caused the window238* underflow exception. Therefore, we change window base to the value239* before we entered the window underflow exception and prepare the240* registers to return as if we were coming from a regular exception241* by changing depc (in a0).242* Note: We can trash the current window frame (a0...a3) and depc!243*/244245wsr a2, DEPC # save stack pointer temporarily246rsr a0, PS247extui a0, a0, PS_OWB_SHIFT, 4248wsr a0, WINDOWBASE249rsync250251/* We are now in the previous window frame. Save registers again. */252253xsr a2, DEPC # save a2 and get stack pointer254s32i a0, a2, PT_AREG0255256wsr a3, EXCSAVE_1 # save a3257movi a3, exc_table258259rsr a0, EXCCAUSE260s32i a0, a2, PT_DEPC # mark it as a regular exception261addx4 a0, a0, a3262l32i a0, a0, EXC_TABLE_FAST_USER263jx a0264265.Lfixup:/* Check for a fixup handler or if we were in a critical section. */266267/* a0: depc, a1: a1, a2: a2, a3: trashed, depc: a0, excsave1: a3 */268269movi a3, exc_table270s32i a2, a3, EXC_TABLE_DOUBLE_SAVE # temporary variable271272/* Enter critical section. */273274l32i a2, a3, EXC_TABLE_FIXUP275s32i a3, a3, EXC_TABLE_FIXUP276beq a2, a3, .Lunrecoverable_fixup # critical!277beqz a2, .Ldflt # no handler was registered278279/* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */280281jx a2282283.Ldflt: /* Get stack pointer. */284285l32i a3, a3, EXC_TABLE_DOUBLE_SAVE286addi a2, a3, -PT_USER_SIZE287288.Lovfl: /* Jump to default handlers. */289290/* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */291292xsr a3, DEPC293s32i a0, a2, PT_DEPC294s32i a3, a2, PT_AREG0295296/* a0: avail, a1: a1, a2: kstk, a3: avail, depc: a2, excsave: a3 */297298movi a3, exc_table299rsr a0, EXCCAUSE300addx4 a0, a0, a3301l32i a0, a0, EXC_TABLE_FAST_USER302jx a0303304/*305* We only allow the ITLB miss exception if we are in kernel space.306* All other exceptions are unexpected and thus unrecoverable!307*/308309#ifdef CONFIG_MMU310.extern fast_second_level_miss_double_kernel311312.Lksp: /* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */313314rsr a3, EXCCAUSE315beqi a3, EXCCAUSE_ITLB_MISS, 1f316addi a3, a3, -EXCCAUSE_DTLB_MISS317bnez a3, .Lunrecoverable3181: movi a3, fast_second_level_miss_double_kernel319jx a3320#else321.equ .Lksp, .Lunrecoverable322#endif323324/* Critical! We can't handle this situation. PANIC! */325326.extern unrecoverable_exception327328.Lunrecoverable_fixup:329l32i a2, a3, EXC_TABLE_DOUBLE_SAVE330xsr a0, DEPC331332.Lunrecoverable:333rsr a3, EXCSAVE_1334wsr a0, EXCSAVE_1335movi a0, unrecoverable_exception336callx0 a0337338.end literal_prefix339340341/*342* Debug interrupt vector343*344* There is not much space here, so simply jump to another handler.345* EXCSAVE[DEBUGLEVEL] has been set to that handler.346*/347348.section .DebugInterruptVector.text, "ax"349350ENTRY(_DebugInterruptVector)351xsr a0, EXCSAVE + XCHAL_DEBUGLEVEL352jx a0353354355356/* Window overflow and underflow handlers.357* The handlers must be 64 bytes apart, first starting with the underflow358* handlers underflow-4 to underflow-12, then the overflow handlers359* overflow-4 to overflow-12.360*361* Note: We rerun the underflow handlers if we hit an exception, so362* we try to access any page that would cause a page fault early.363*/364365.section .WindowVectors.text, "ax"366367368/* 4-Register Window Overflow Vector (Handler) */369370.align 64371.global _WindowOverflow4372_WindowOverflow4:373s32e a0, a5, -16374s32e a1, a5, -12375s32e a2, a5, -8376s32e a3, a5, -4377rfwo378379380/* 4-Register Window Underflow Vector (Handler) */381382.align 64383.global _WindowUnderflow4384_WindowUnderflow4:385l32e a0, a5, -16386l32e a1, a5, -12387l32e a2, a5, -8388l32e a3, a5, -4389rfwu390391392/* 8-Register Window Overflow Vector (Handler) */393394.align 64395.global _WindowOverflow8396_WindowOverflow8:397s32e a0, a9, -16398l32e a0, a1, -12399s32e a2, a9, -8400s32e a1, a9, -12401s32e a3, a9, -4402s32e a4, a0, -32403s32e a5, a0, -28404s32e a6, a0, -24405s32e a7, a0, -20406rfwo407408/* 8-Register Window Underflow Vector (Handler) */409410.align 64411.global _WindowUnderflow8412_WindowUnderflow8:413l32e a1, a9, -12414l32e a0, a9, -16415l32e a7, a1, -12416l32e a2, a9, -8417l32e a4, a7, -32418l32e a3, a9, -4419l32e a5, a7, -28420l32e a6, a7, -24421l32e a7, a7, -20422rfwu423424425/* 12-Register Window Overflow Vector (Handler) */426427.align 64428.global _WindowOverflow12429_WindowOverflow12:430s32e a0, a13, -16431l32e a0, a1, -12432s32e a1, a13, -12433s32e a2, a13, -8434s32e a3, a13, -4435s32e a4, a0, -48436s32e a5, a0, -44437s32e a6, a0, -40438s32e a7, a0, -36439s32e a8, a0, -32440s32e a9, a0, -28441s32e a10, a0, -24442s32e a11, a0, -20443rfwo444445/* 12-Register Window Underflow Vector (Handler) */446447.align 64448.global _WindowUnderflow12449_WindowUnderflow12:450l32e a1, a13, -12451l32e a0, a13, -16452l32e a11, a1, -12453l32e a2, a13, -8454l32e a4, a11, -48455l32e a8, a11, -32456l32e a3, a13, -4457l32e a5, a11, -44458l32e a6, a11, -40459l32e a7, a11, -36460l32e a9, a11, -28461l32e a10, a11, -24462l32e a11, a11, -20463rfwu464465.text466467468469470