/*1* Low-level exception handling2*3* This file is subject to the terms and conditions of the GNU General Public4* License. See the file "COPYING" in the main directory of this archive5* for more details.6*7* Copyright (C) 2004 - 2008 by Tensilica Inc.8* Copyright (C) 2015 Cadence Design Systems Inc.9*10* Chris Zankel <[email protected]>11*12*/1314#include <linux/linkage.h>15#include <linux/pgtable.h>16#include <asm/asm-offsets.h>17#include <asm/asmmacro.h>18#include <asm/processor.h>19#include <asm/coprocessor.h>20#include <asm/thread_info.h>21#include <asm/asm-uaccess.h>22#include <asm/unistd.h>23#include <asm/ptrace.h>24#include <asm/current.h>25#include <asm/page.h>26#include <asm/signal.h>27#include <asm/tlbflush.h>28#include <variant/tie-asm.h>2930/*31* Macro to find first bit set in WINDOWBASE from the left + 132*33* 100....0 -> 134* 010....0 -> 235* 000....1 -> WSBITS36*/3738.macro ffs_ws bit mask3940#if XCHAL_HAVE_NSA41nsau \bit, \mask # 32-WSBITS ... 31 (32 iff 0)42addi \bit, \bit, WSBITS - 32 + 1 # uppest bit set -> return 143#else44movi \bit, WSBITS45#if WSBITS > 1646_bltui \mask, 0x10000, 99f47addi \bit, \bit, -1648extui \mask, \mask, 16, 1649#endif50#if WSBITS > 85199: _bltui \mask, 0x100, 99f52addi \bit, \bit, -853srli \mask, \mask, 854#endif5599: _bltui \mask, 0x10, 99f56addi \bit, \bit, -457srli \mask, \mask, 45899: _bltui \mask, 0x4, 99f59addi \bit, \bit, -260srli \mask, \mask, 26199: _bltui \mask, 0x2, 99f62addi \bit, \bit, -16399:6465#endif66.endm676869.macro irq_save flags tmp70#if XTENSA_FAKE_NMI71#if defined(CONFIG_DEBUG_KERNEL) && (LOCKLEVEL | TOPLEVEL) >= XCHAL_DEBUGLEVEL72rsr \flags, ps73extui \tmp, \flags, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH74bgei \tmp, LOCKLEVEL, 99f75rsil \tmp, LOCKLEVEL7699:77#else78movi \tmp, LOCKLEVEL79rsr \flags, ps80or \flags, \flags, \tmp81xsr \flags, ps82rsync83#endif84#else85rsil \flags, LOCKLEVEL86#endif87.endm8889/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */9091/*92* First-level exception handler for user exceptions.93* Save some special registers, extra states and all registers in the AR94* register file that were in use in the user task, and jump to the common95* exception code.96* We save SAR (used to calculate WMASK), and WB and WS (we don't have to97* save them for kernel exceptions).98*99* Entry condition for user_exception:100*101* a0: trashed, original value saved on stack (PT_AREG0)102* a1: a1103* a2: new stack pointer, original value in depc104* a3: a3105* depc: a2, original value saved on stack (PT_DEPC)106* excsave1: dispatch table107*108* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC109* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception110*111* Entry condition for _user_exception:112*113* a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC114* excsave has been restored, and115* stack pointer (a1) has been set.116*117* Note: _user_exception might be at an odd address. Don't use call0..call12118*/119.literal_position120121ENTRY(user_exception)122123/* Save a1, a2, a3, and set SP. */124125rsr a0, depc126s32i a1, a2, PT_AREG1127s32i a0, a2, PT_AREG2128s32i a3, a2, PT_AREG3129mov a1, a2130131.globl _user_exception132_user_exception:133134/* Save SAR and turn off single stepping */135136movi a2, 0137wsr a2, depc # terminate user stack trace with 0138rsr a3, sar139xsr a2, icountlevel140s32i a3, a1, PT_SAR141s32i a2, a1, PT_ICOUNTLEVEL142143#if XCHAL_HAVE_THREADPTR144rur a2, threadptr145s32i a2, a1, PT_THREADPTR146#endif147148/* Rotate ws so that the current windowbase is at bit0. */149/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */150151#if defined(USER_SUPPORT_WINDOWED)152rsr a2, windowbase153rsr a3, windowstart154ssr a2155s32i a2, a1, PT_WINDOWBASE156s32i a3, a1, PT_WINDOWSTART157slli a2, a3, 32-WSBITS158src a2, a3, a2159srli a2, a2, 32-WSBITS160s32i a2, a1, PT_WMASK # needed for restoring registers161#else162movi a2, 0163movi a3, 1164s32i a2, a1, PT_WINDOWBASE165s32i a3, a1, PT_WINDOWSTART166s32i a3, a1, PT_WMASK167#endif168169/* Save only live registers. */170171UABI_W _bbsi.l a2, 1, .Lsave_window_registers172s32i a4, a1, PT_AREG4173s32i a5, a1, PT_AREG5174s32i a6, a1, PT_AREG6175s32i a7, a1, PT_AREG7176UABI_W _bbsi.l a2, 2, .Lsave_window_registers177s32i a8, a1, PT_AREG8178s32i a9, a1, PT_AREG9179s32i a10, a1, PT_AREG10180s32i a11, a1, PT_AREG11181UABI_W _bbsi.l a2, 3, .Lsave_window_registers182s32i a12, a1, PT_AREG12183s32i a13, a1, PT_AREG13184s32i a14, a1, PT_AREG14185s32i a15, a1, PT_AREG15186187#if defined(USER_SUPPORT_WINDOWED)188/* If only one valid frame skip saving regs. */189190beqi a2, 1, common_exception191192/* Save the remaining registers.193* We have to save all registers up to the first '1' from194* the right, except the current frame (bit 0).195* Assume a2 is: 001001000110001196* All register frames starting from the top field to the marked '1'197* must be saved.198*/199.Lsave_window_registers:200addi a3, a2, -1 # eliminate '1' in bit 0: yyyyxxww0201neg a3, a3 # yyyyxxww0 -> YYYYXXWW1+1202and a3, a3, a2 # max. only one bit is set203204/* Find number of frames to save */205206ffs_ws a0, a3 # number of frames to the '1' from left207208/* Store information into WMASK:209* bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,210* bits 4...: number of valid 4-register frames211*/212213slli a3, a0, 4 # number of frames to save in bits 8..4214extui a2, a2, 0, 4 # mask for the first 16 registers215or a2, a3, a2216s32i a2, a1, PT_WMASK # needed when we restore the reg-file217218/* Save 4 registers at a time */2192201: rotw -1221s32i a0, a5, PT_AREG_END - 16222s32i a1, a5, PT_AREG_END - 12223s32i a2, a5, PT_AREG_END - 8224s32i a3, a5, PT_AREG_END - 4225addi a0, a4, -1226addi a1, a5, -16227_bnez a0, 1b228229/* WINDOWBASE still in SAR! */230231rsr a2, sar # original WINDOWBASE232movi a3, 1233ssl a2234sll a3, a3235wsr a3, windowstart # set corresponding WINDOWSTART bit236wsr a2, windowbase # and WINDOWSTART237rsync238239/* We are back to the original stack pointer (a1) */240#endif241/* Now, jump to the common exception handler. */242243j common_exception244245ENDPROC(user_exception)246247/*248* First-level exit handler for kernel exceptions249* Save special registers and the live window frame.250* Note: Even though we changes the stack pointer, we don't have to do a251* MOVSP here, as we do that when we return from the exception.252* (See comment in the kernel exception exit code)253*254* Entry condition for kernel_exception:255*256* a0: trashed, original value saved on stack (PT_AREG0)257* a1: a1258* a2: new stack pointer, original in DEPC259* a3: a3260* depc: a2, original value saved on stack (PT_DEPC)261* excsave_1: dispatch table262*263* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC264* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception265*266* Entry condition for _kernel_exception:267*268* a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC269* excsave has been restored, and270* stack pointer (a1) has been set.271*272* Note: _kernel_exception might be at an odd address. Don't use call0..call12273*/274275ENTRY(kernel_exception)276277/* Save a1, a2, a3, and set SP. */278279rsr a0, depc # get a2280s32i a1, a2, PT_AREG1281s32i a0, a2, PT_AREG2282s32i a3, a2, PT_AREG3283mov a1, a2284285.globl _kernel_exception286_kernel_exception:287288/* Save SAR and turn off single stepping */289290movi a2, 0291rsr a3, sar292xsr a2, icountlevel293s32i a3, a1, PT_SAR294s32i a2, a1, PT_ICOUNTLEVEL295296#if defined(__XTENSA_WINDOWED_ABI__)297/* Rotate ws so that the current windowbase is at bit0. */298/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */299300rsr a2, windowbase # don't need to save these, we only301rsr a3, windowstart # need shifted windowstart: windowmask302ssr a2303slli a2, a3, 32-WSBITS304src a2, a3, a2305srli a2, a2, 32-WSBITS306s32i a2, a1, PT_WMASK # needed for kernel_exception_exit307#endif308309/* Save only the live window-frame */310311KABI_W _bbsi.l a2, 1, 1f312s32i a4, a1, PT_AREG4313s32i a5, a1, PT_AREG5314s32i a6, a1, PT_AREG6315s32i a7, a1, PT_AREG7316KABI_W _bbsi.l a2, 2, 1f317s32i a8, a1, PT_AREG8318s32i a9, a1, PT_AREG9319s32i a10, a1, PT_AREG10320s32i a11, a1, PT_AREG11321KABI_W _bbsi.l a2, 3, 1f322s32i a12, a1, PT_AREG12323s32i a13, a1, PT_AREG13324s32i a14, a1, PT_AREG14325s32i a15, a1, PT_AREG15326327#ifdef __XTENSA_WINDOWED_ABI__328_bnei a2, 1, 1f329/* Copy spill slots of a0 and a1 to imitate movsp330* in order to keep exception stack continuous331*/332l32i a3, a1, PT_KERNEL_SIZE333l32i a0, a1, PT_KERNEL_SIZE + 4334s32e a3, a1, -16335s32e a0, a1, -12336#endif3371:338l32i a0, a1, PT_AREG0 # restore saved a0339wsr a0, depc340341/*342* This is the common exception handler.343* We get here from the user exception handler or simply by falling through344* from the kernel exception handler.345* Save the remaining special registers, switch to kernel mode, and jump346* to the second-level exception handler.347*348*/349350common_exception:351352/* Save some registers, disable loops and clear the syscall flag. */353354rsr a2, debugcause355rsr a3, epc1356s32i a2, a1, PT_DEBUGCAUSE357s32i a3, a1, PT_PC358359movi a2, NO_SYSCALL360rsr a3, excvaddr361s32i a2, a1, PT_SYSCALL362movi a2, 0363s32i a3, a1, PT_EXCVADDR364#if XCHAL_HAVE_LOOPS365xsr a2, lcount366s32i a2, a1, PT_LCOUNT367#endif368369#if XCHAL_HAVE_EXCLUSIVE370/* Clear exclusive access monitor set by interrupted code */371clrex372#endif373374/* It is now save to restore the EXC_TABLE_FIXUP variable. */375376rsr a2, exccause377movi a3, 0378rsr a0, excsave1379s32i a2, a1, PT_EXCCAUSE380s32i a3, a0, EXC_TABLE_FIXUP381382/* All unrecoverable states are saved on stack, now, and a1 is valid.383* Now we can allow exceptions again. In case we've got an interrupt384* PS.INTLEVEL is set to LOCKLEVEL disabling furhter interrupts,385* otherwise it's left unchanged.386*387* Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)388*/389390rsr a3, ps391s32i a3, a1, PT_PS # save ps392393#if XTENSA_FAKE_NMI394/* Correct PS needs to be saved in the PT_PS:395* - in case of exception or level-1 interrupt it's in the PS,396* and is already saved.397* - in case of medium level interrupt it's in the excsave2.398*/399movi a0, EXCCAUSE_MAPPED_NMI400extui a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH401beq a2, a0, .Lmedium_level_irq402bnei a2, EXCCAUSE_LEVEL1_INTERRUPT, .Lexception403beqz a3, .Llevel1_irq # level-1 IRQ sets ps.intlevel to 0404405.Lmedium_level_irq:406rsr a0, excsave2407s32i a0, a1, PT_PS # save medium-level interrupt ps408bgei a3, LOCKLEVEL, .Lexception409410.Llevel1_irq:411movi a3, LOCKLEVEL412413.Lexception:414KABI_W movi a0, PS_WOE_MASK415KABI_W or a3, a3, a0416#else417addi a2, a2, -EXCCAUSE_LEVEL1_INTERRUPT418movi a0, LOCKLEVEL419extui a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH420# a3 = PS.INTLEVEL421moveqz a3, a0, a2 # a3 = LOCKLEVEL iff interrupt422KABI_W movi a2, PS_WOE_MASK423KABI_W or a3, a3, a2424#endif425426/* restore return address (or 0 if return to userspace) */427rsr a0, depc428wsr a3, ps429rsync # PS.WOE => rsync => overflow430431/* Save lbeg, lend */432#if XCHAL_HAVE_LOOPS433rsr a4, lbeg434rsr a3, lend435s32i a4, a1, PT_LBEG436s32i a3, a1, PT_LEND437#endif438439/* Save SCOMPARE1 */440441#if XCHAL_HAVE_S32C1I442rsr a3, scompare1443s32i a3, a1, PT_SCOMPARE1444#endif445446/* Save optional registers. */447448save_xtregs_opt a1 a3 a4 a5 a6 a7 PT_XTREGS_OPT449450#ifdef CONFIG_TRACE_IRQFLAGS451rsr abi_tmp0, ps452extui abi_tmp0, abi_tmp0, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH453beqz abi_tmp0, 1f454abi_call trace_hardirqs_off4551:456#endif457#ifdef CONFIG_CONTEXT_TRACKING_USER458l32i abi_tmp0, a1, PT_PS459bbci.l abi_tmp0, PS_UM_BIT, 1f460abi_call user_exit_callable4611:462#endif463464/* Go to second-level dispatcher. Set up parameters to pass to the465* exception handler and call the exception handler.466*/467468l32i abi_arg1, a1, PT_EXCCAUSE # pass EXCCAUSE469rsr abi_tmp0, excsave1470addx4 abi_tmp0, abi_arg1, abi_tmp0471l32i abi_tmp0, abi_tmp0, EXC_TABLE_DEFAULT # load handler472mov abi_arg0, a1 # pass stack frame473474/* Call the second-level handler */475476abi_callx abi_tmp0477478/* Jump here for exception exit */479.global common_exception_return480common_exception_return:481482#if XTENSA_FAKE_NMI483l32i abi_tmp0, a1, PT_EXCCAUSE484movi abi_tmp1, EXCCAUSE_MAPPED_NMI485l32i abi_saved1, a1, PT_PS486beq abi_tmp0, abi_tmp1, .Lrestore_state487#endif488.Ltif_loop:489irq_save abi_tmp0, abi_tmp1490#ifdef CONFIG_TRACE_IRQFLAGS491abi_call trace_hardirqs_off492#endif493494/* Jump if we are returning from kernel exceptions. */495496l32i abi_saved1, a1, PT_PS497GET_THREAD_INFO(abi_tmp0, a1)498l32i abi_saved0, abi_tmp0, TI_FLAGS499_bbci.l abi_saved1, PS_UM_BIT, .Lexit_tif_loop_kernel500501/* Specific to a user exception exit:502* We need to check some flags for signal handling and rescheduling,503* and have to restore WB and WS, extra states, and all registers504* in the register file that were in use in the user task.505* Note that we don't disable interrupts here.506*/507508_bbsi.l abi_saved0, TIF_NEED_RESCHED, .Lresched509movi abi_tmp0, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NOTIFY_SIGNAL510bnone abi_saved0, abi_tmp0, .Lexit_tif_loop_user511512l32i abi_tmp0, a1, PT_DEPC513bgeui abi_tmp0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lrestore_state514515/* Call do_signal() */516517#ifdef CONFIG_TRACE_IRQFLAGS518abi_call trace_hardirqs_on519#endif520rsil abi_tmp0, 0521mov abi_arg0, a1522abi_call do_notify_resume # int do_notify_resume(struct pt_regs*)523j .Ltif_loop524525.Lresched:526#ifdef CONFIG_TRACE_IRQFLAGS527abi_call trace_hardirqs_on528#endif529rsil abi_tmp0, 0530abi_call schedule # void schedule (void)531j .Ltif_loop532533.Lexit_tif_loop_kernel:534#ifdef CONFIG_PREEMPTION535_bbci.l abi_saved0, TIF_NEED_RESCHED, .Lrestore_state536537/* Check current_thread_info->preempt_count */538539l32i abi_tmp1, abi_tmp0, TI_PRE_COUNT540bnez abi_tmp1, .Lrestore_state541abi_call preempt_schedule_irq542#endif543j .Lrestore_state544545.Lexit_tif_loop_user:546#ifdef CONFIG_CONTEXT_TRACKING_USER547abi_call user_enter_callable548#endif549#ifdef CONFIG_HAVE_HW_BREAKPOINT550_bbci.l abi_saved0, TIF_DB_DISABLED, 1f551abi_call restore_dbreak5521:553#endif554#ifdef CONFIG_DEBUG_TLB_SANITY555l32i abi_tmp0, a1, PT_DEPC556bgeui abi_tmp0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lrestore_state557abi_call check_tlb_sanity558#endif559560.Lrestore_state:561#ifdef CONFIG_TRACE_IRQFLAGS562extui abi_tmp0, abi_saved1, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH563bgei abi_tmp0, LOCKLEVEL, 1f564abi_call trace_hardirqs_on5651:566#endif567/*568* Restore optional registers.569* abi_arg* are used as temporary registers here.570*/571572load_xtregs_opt a1 abi_tmp0 abi_arg0 abi_arg1 abi_arg2 abi_arg3 PT_XTREGS_OPT573574/* Restore SCOMPARE1 */575576#if XCHAL_HAVE_S32C1I577l32i abi_tmp0, a1, PT_SCOMPARE1578wsr abi_tmp0, scompare1579#endif580wsr abi_saved1, ps /* disable interrupts */581_bbci.l abi_saved1, PS_UM_BIT, kernel_exception_exit582583user_exception_exit:584585/* Restore the state of the task and return from the exception. */586587#if defined(USER_SUPPORT_WINDOWED)588/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */589590l32i a2, a1, PT_WINDOWBASE591l32i a3, a1, PT_WINDOWSTART592wsr a1, depc # use DEPC as temp storage593wsr a3, windowstart # restore WINDOWSTART594ssr a2 # preserve user's WB in the SAR595wsr a2, windowbase # switch to user's saved WB596rsync597rsr a1, depc # restore stack pointer598l32i a2, a1, PT_WMASK # register frames saved (in bits 4...9)599rotw -1 # we restore a4..a7600_bltui a6, 16, .Lclear_regs # only have to restore current window?601602/* The working registers are a0 and a3. We are restoring to603* a4..a7. Be careful not to destroy what we have just restored.604* Note: wmask has the format YYYYM:605* Y: number of registers saved in groups of 4606* M: 4 bit mask of first 16 registers607*/608609mov a2, a6610mov a3, a56116121: rotw -1 # a0..a3 become a4..a7613addi a3, a7, -4*4 # next iteration614addi a2, a6, -16 # decrementing Y in WMASK615l32i a4, a3, PT_AREG_END + 0616l32i a5, a3, PT_AREG_END + 4617l32i a6, a3, PT_AREG_END + 8618l32i a7, a3, PT_AREG_END + 12619_bgeui a2, 16, 1b620621/* Clear unrestored registers (don't leak anything to user-land */622623.Lclear_regs:624rsr a0, windowbase625rsr a3, sar626sub a3, a0, a3627beqz a3, 2f628extui a3, a3, 0, WBBITS6296301: rotw -1631addi a3, a7, -1632movi a4, 0633movi a5, 0634movi a6, 0635movi a7, 0636bgei a3, 1, 1b637638/* We are back were we were when we started.639* Note: a2 still contains WMASK (if we've returned to the original640* frame where we had loaded a2), or at least the lower 4 bits641* (if we have restored WSBITS-1 frames).642*/6432:644#else645movi a2, 1646#endif647#if XCHAL_HAVE_THREADPTR648l32i a3, a1, PT_THREADPTR649wur a3, threadptr650#endif651652j common_exception_exit653654/* This is the kernel exception exit.655* We avoided to do a MOVSP when we entered the exception, but we656* have to do it here.657*/658659kernel_exception_exit:660661#if defined(__XTENSA_WINDOWED_ABI__)662/* Check if we have to do a movsp.663*664* We only have to do a movsp if the previous window-frame has665* been spilled to the *temporary* exception stack instead of the666* task's stack. This is the case if the corresponding bit in667* WINDOWSTART for the previous window-frame was set before668* (not spilled) but is zero now (spilled).669* If this bit is zero, all other bits except the one for the670* current window frame are also zero. So, we can use a simple test:671* 'and' WINDOWSTART and WINDOWSTART-1:672*673* (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*674*675* The result is zero only if one bit was set.676*677* (Note: We might have gone through several task switches before678* we come back to the current task, so WINDOWBASE might be679* different from the time the exception occurred.)680*/681682/* Test WINDOWSTART before and after the exception.683* We actually have WMASK, so we only have to test if it is 1 or not.684*/685686l32i a2, a1, PT_WMASK687_beqi a2, 1, common_exception_exit # Spilled before exception,jump688689/* Test WINDOWSTART now. If spilled, do the movsp */690691rsr a3, windowstart692addi a0, a3, -1693and a3, a3, a0694_bnez a3, common_exception_exit695696/* Do a movsp (we returned from a call4, so we have at least a0..a7) */697698addi a0, a1, -16699l32i a3, a0, 0700l32i a4, a0, 4701s32i a3, a1, PT_KERNEL_SIZE + 0702s32i a4, a1, PT_KERNEL_SIZE + 4703l32i a3, a0, 8704l32i a4, a0, 12705s32i a3, a1, PT_KERNEL_SIZE + 8706s32i a4, a1, PT_KERNEL_SIZE + 12707708/* Common exception exit.709* We restore the special register and the current window frame, and710* return from the exception.711*712* Note: We expect a2 to hold PT_WMASK713*/714#else715movi a2, 1716#endif717718common_exception_exit:719720/* Restore address registers. */721722_bbsi.l a2, 1, 1f723l32i a4, a1, PT_AREG4724l32i a5, a1, PT_AREG5725l32i a6, a1, PT_AREG6726l32i a7, a1, PT_AREG7727_bbsi.l a2, 2, 1f728l32i a8, a1, PT_AREG8729l32i a9, a1, PT_AREG9730l32i a10, a1, PT_AREG10731l32i a11, a1, PT_AREG11732_bbsi.l a2, 3, 1f733l32i a12, a1, PT_AREG12734l32i a13, a1, PT_AREG13735l32i a14, a1, PT_AREG14736l32i a15, a1, PT_AREG15737738/* Restore PC, SAR */7397401: l32i a2, a1, PT_PC741l32i a3, a1, PT_SAR742wsr a2, epc1743wsr a3, sar744745/* Restore LBEG, LEND, LCOUNT */746#if XCHAL_HAVE_LOOPS747l32i a2, a1, PT_LBEG748l32i a3, a1, PT_LEND749wsr a2, lbeg750l32i a2, a1, PT_LCOUNT751wsr a3, lend752wsr a2, lcount753#endif754755/* We control single stepping through the ICOUNTLEVEL register. */756757l32i a2, a1, PT_ICOUNTLEVEL758movi a3, -2759wsr a2, icountlevel760wsr a3, icount761762/* Check if it was double exception. */763764l32i a0, a1, PT_DEPC765l32i a3, a1, PT_AREG3766l32i a2, a1, PT_AREG2767_bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f768769/* Restore a0...a3 and return */770771l32i a0, a1, PT_AREG0772l32i a1, a1, PT_AREG1773rfe7747751: wsr a0, depc776l32i a0, a1, PT_AREG0777l32i a1, a1, PT_AREG1778rfde779780ENDPROC(kernel_exception)781782/*783* Debug exception handler.784*785* Currently, we don't support KGDB, so only user application can be debugged.786*787* When we get here, a0 is trashed and saved to excsave[debuglevel]788*/789790.literal_position791792ENTRY(debug_exception)793794rsr a0, SREG_EPS + XCHAL_DEBUGLEVEL795bbsi.l a0, PS_EXCM_BIT, .Ldebug_exception_in_exception # exception mode796797/* Set EPC1 and EXCCAUSE */798799wsr a2, depc # save a2 temporarily800rsr a2, SREG_EPC + XCHAL_DEBUGLEVEL801wsr a2, epc1802803movi a2, EXCCAUSE_MAPPED_DEBUG804wsr a2, exccause805806/* Restore PS to the value before the debug exc but with PS.EXCM set.*/807808movi a2, 1 << PS_EXCM_BIT809or a2, a0, a2810wsr a2, ps811812/* Switch to kernel/user stack, restore jump vector, and save a0 */813814bbsi.l a2, PS_UM_BIT, .Ldebug_exception_user # jump if user mode815addi a2, a1, -16 - PT_KERNEL_SIZE # assume kernel stack816817.Ldebug_exception_continue:818l32i a0, a3, DT_DEBUG_SAVE819s32i a1, a2, PT_AREG1820s32i a0, a2, PT_AREG0821movi a0, 0822s32i a0, a2, PT_DEPC # mark it as a regular exception823xsr a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL824xsr a0, depc825s32i a3, a2, PT_AREG3826s32i a0, a2, PT_AREG2827mov a1, a2828829/* Debug exception is handled as an exception, so interrupts will830* likely be enabled in the common exception handler. Disable831* preemption if we have HW breakpoints to preserve DEBUGCAUSE.DBNUM832* meaning.833*/834#if defined(CONFIG_PREEMPT_COUNT) && defined(CONFIG_HAVE_HW_BREAKPOINT)835GET_THREAD_INFO(a2, a1)836l32i a3, a2, TI_PRE_COUNT837addi a3, a3, 1838s32i a3, a2, TI_PRE_COUNT839#endif840841rsr a2, ps842bbsi.l a2, PS_UM_BIT, _user_exception843j _kernel_exception844845.Ldebug_exception_user:846rsr a2, excsave1847l32i a2, a2, EXC_TABLE_KSTK # load kernel stack pointer848j .Ldebug_exception_continue849850.Ldebug_exception_in_exception:851#ifdef CONFIG_HAVE_HW_BREAKPOINT852/* Debug exception while in exception mode. This may happen when853* window overflow/underflow handler or fast exception handler hits854* data breakpoint, in which case save and disable all data855* breakpoints, single-step faulting instruction and restore data856* breakpoints.857*/858859bbci.l a0, PS_UM_BIT, .Ldebug_exception_in_exception # jump if kernel mode860861rsr a0, debugcause862bbsi.l a0, DEBUGCAUSE_DBREAK_BIT, .Ldebug_save_dbreak863864.set _index, 0865.rept XCHAL_NUM_DBREAK866l32i a0, a3, DT_DBREAKC_SAVE + _index * 4867wsr a0, SREG_DBREAKC + _index868.set _index, _index + 1869.endr870871l32i a0, a3, DT_ICOUNT_LEVEL_SAVE872wsr a0, icountlevel873874l32i a0, a3, DT_ICOUNT_SAVE875xsr a0, icount876877l32i a0, a3, DT_DEBUG_SAVE878xsr a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL879rfi XCHAL_DEBUGLEVEL880881.Ldebug_save_dbreak:882.set _index, 0883.rept XCHAL_NUM_DBREAK884movi a0, 0885xsr a0, SREG_DBREAKC + _index886s32i a0, a3, DT_DBREAKC_SAVE + _index * 4887.set _index, _index + 1888.endr889890movi a0, XCHAL_EXCM_LEVEL + 1891xsr a0, icountlevel892s32i a0, a3, DT_ICOUNT_LEVEL_SAVE893894movi a0, 0xfffffffe895xsr a0, icount896s32i a0, a3, DT_ICOUNT_SAVE897898l32i a0, a3, DT_DEBUG_SAVE899xsr a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL900rfi XCHAL_DEBUGLEVEL901#else902/* Debug exception while in exception mode. Should not happen. */903j .Ldebug_exception_in_exception // FIXME!!904#endif905906ENDPROC(debug_exception)907908/*909* We get here in case of an unrecoverable exception.910* The only thing we can do is to be nice and print a panic message.911* We only produce a single stack frame for panic, so ???912*913*914* Entry conditions:915*916* - a0 contains the caller address; original value saved in excsave1.917* - the original a0 contains a valid return address (backtrace) or 0.918* - a2 contains a valid stackpointer919*920* Notes:921*922* - If the stack pointer could be invalid, the caller has to setup a923* dummy stack pointer (e.g. the stack of the init_task)924*925* - If the return address could be invalid, the caller has to set it926* to 0, so the backtrace would stop.927*928*/929.align 4930unrecoverable_text:931.ascii "Unrecoverable error in exception handler\0"932933.literal_position934935ENTRY(unrecoverable_exception)936937#if XCHAL_HAVE_WINDOWED938movi a0, 1939movi a1, 0940941wsr a0, windowstart942wsr a1, windowbase943rsync944#endif945946movi a1, KERNEL_PS_WOE_MASK | LOCKLEVEL947wsr a1, ps948rsync949950movi a1, init_task951movi a0, 0952addi a1, a1, PT_REGS_OFFSET953954movi abi_arg0, unrecoverable_text955abi_call panic9569571: j 1b958959ENDPROC(unrecoverable_exception)960961/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */962963__XTENSA_HANDLER964.literal_position965966#ifdef SUPPORT_WINDOWED967/*968* Fast-handler for alloca exceptions969*970* The ALLOCA handler is entered when user code executes the MOVSP971* instruction and the caller's frame is not in the register file.972*973* This algorithm was taken from the Ross Morley's RTOS Porting Layer:974*975* /home/ross/rtos/porting/XtensaRTOS-PortingLayer-20090507/xtensa_vectors.S976*977* It leverages the existing window spill/fill routines and their support for978* double exceptions. The 'movsp' instruction will only cause an exception if979* the next window needs to be loaded. In fact this ALLOCA exception may be980* replaced at some point by changing the hardware to do a underflow exception981* of the proper size instead.982*983* This algorithm simply backs out the register changes started by the user984* exception handler, makes it appear that we have started a window underflow985* by rotating the window back and then setting the old window base (OWB) in986* the 'ps' register with the rolled back window base. The 'movsp' instruction987* will be re-executed and this time since the next window frames is in the988* active AR registers it won't cause an exception.989*990* If the WindowUnderflow code gets a TLB miss the page will get mapped991* the partial WindowUnderflow will be handled in the double exception992* handler.993*994* Entry condition:995*996* a0: trashed, original value saved on stack (PT_AREG0)997* a1: a1998* a2: new stack pointer, original in DEPC999* a3: a31000* depc: a2, original value saved on stack (PT_DEPC)1001* excsave_1: dispatch table1002*1003* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC1004* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception1005*/10061007ENTRY(fast_alloca)1008rsr a0, windowbase1009rotw -11010rsr a2, ps1011extui a3, a2, PS_OWB_SHIFT, PS_OWB_WIDTH1012xor a3, a3, a41013l32i a4, a6, PT_AREG01014l32i a1, a6, PT_DEPC1015rsr a6, depc1016wsr a1, depc1017slli a3, a3, PS_OWB_SHIFT1018xor a2, a2, a31019wsr a2, ps1020rsync10211022_bbci.l a4, 31, 4f1023rotw -11024_bbci.l a8, 30, 8f1025rotw -11026j _WindowUnderflow1210278: j _WindowUnderflow810284: j _WindowUnderflow41029ENDPROC(fast_alloca)1030#endif10311032#ifdef CONFIG_USER_ABI_CALL0_PROBE1033/*1034* fast illegal instruction handler.1035*1036* This is used to fix up user PS.WOE on the exception caused1037* by the first opcode related to register window. If PS.WOE is1038* already set it goes directly to the common user exception handler.1039*1040* Entry condition:1041*1042* a0: trashed, original value saved on stack (PT_AREG0)1043* a1: a11044* a2: new stack pointer, original in DEPC1045* a3: a31046* depc: a2, original value saved on stack (PT_DEPC)1047* excsave_1: dispatch table1048*/10491050ENTRY(fast_illegal_instruction_user)10511052rsr a0, ps1053bbsi.l a0, PS_WOE_BIT, 1f1054s32i a3, a2, PT_AREG31055movi a3, PS_WOE_MASK1056or a0, a0, a31057wsr a0, ps1058#ifdef CONFIG_USER_ABI_CALL0_PROBE1059GET_THREAD_INFO(a3, a2)1060rsr a0, epc11061s32i a0, a3, TI_PS_WOE_FIX_ADDR1062#endif1063l32i a3, a2, PT_AREG31064l32i a0, a2, PT_AREG01065rsr a2, depc1066rfe10671:1068call0 user_exception10691070ENDPROC(fast_illegal_instruction_user)1071#endif10721073/*1074* fast system calls.1075*1076* WARNING: The kernel doesn't save the entire user context before1077* handling a fast system call. These functions are small and short,1078* usually offering some functionality not available to user tasks.1079*1080* BE CAREFUL TO PRESERVE THE USER'S CONTEXT.1081*1082* Entry condition:1083*1084* a0: trashed, original value saved on stack (PT_AREG0)1085* a1: a11086* a2: new stack pointer, original in DEPC1087* a3: a31088* depc: a2, original value saved on stack (PT_DEPC)1089* excsave_1: dispatch table1090*/10911092ENTRY(fast_syscall_user)10931094/* Skip syscall. */10951096rsr a0, epc11097addi a0, a0, 31098wsr a0, epc110991100l32i a0, a2, PT_DEPC1101bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable11021103rsr a0, depc # get syscall-nr1104_beqz a0, fast_syscall_spill_registers1105_beqi a0, __NR_xtensa, fast_syscall_xtensa11061107call0 user_exception11081109ENDPROC(fast_syscall_user)11101111ENTRY(fast_syscall_unrecoverable)11121113/* Restore all states. */11141115l32i a0, a2, PT_AREG0 # restore a01116xsr a2, depc # restore a2, depc11171118wsr a0, excsave11119call0 unrecoverable_exception11201121ENDPROC(fast_syscall_unrecoverable)11221123/*1124* sysxtensa syscall handler1125*1126* int sysxtensa (SYS_XTENSA_ATOMIC_SET, ptr, val, unused);1127* int sysxtensa (SYS_XTENSA_ATOMIC_ADD, ptr, val, unused);1128* int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val, unused);1129* int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);1130* a2 a6 a3 a4 a51131*1132* Entry condition:1133*1134* a0: a2 (syscall-nr), original value saved on stack (PT_AREG0)1135* a1: a11136* a2: new stack pointer, original in a0 and DEPC1137* a3: a31138* a4..a15: unchanged1139* depc: a2, original value saved on stack (PT_DEPC)1140* excsave_1: dispatch table1141*1142* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC1143* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception1144*1145* Note: we don't have to save a2; a2 holds the return value1146*/11471148.literal_position11491150#ifdef CONFIG_FAST_SYSCALL_XTENSA11511152ENTRY(fast_syscall_xtensa)11531154s32i a7, a2, PT_AREG7 # we need an additional register1155movi a7, 4 # sizeof(unsigned int)1156access_ok a3, a7, a0, a2, .Leac # a0: scratch reg, a2: sp11571158_bgeui a6, SYS_XTENSA_COUNT, .Lill1159_bnei a6, SYS_XTENSA_ATOMIC_CMP_SWP, .Lnswp11601161/* Fall through for ATOMIC_CMP_SWP. */11621163.Lswp: /* Atomic compare and swap */11641165EX(.Leac) l32i a0, a3, 0 # read old value1166bne a0, a4, 1f # same as old value? jump1167EX(.Leac) s32i a5, a3, 0 # different, modify value1168l32i a7, a2, PT_AREG7 # restore a71169l32i a0, a2, PT_AREG0 # restore a01170movi a2, 1 # and return 11171rfe117211731: l32i a7, a2, PT_AREG7 # restore a71174l32i a0, a2, PT_AREG0 # restore a01175movi a2, 0 # return 0 (note that we cannot set1176rfe11771178.Lnswp: /* Atomic set, add, and exg_add. */11791180EX(.Leac) l32i a7, a3, 0 # orig1181addi a6, a6, -SYS_XTENSA_ATOMIC_SET1182add a0, a4, a7 # + arg1183moveqz a0, a4, a6 # set1184addi a6, a6, SYS_XTENSA_ATOMIC_SET1185EX(.Leac) s32i a0, a3, 0 # write new value11861187mov a0, a21188mov a2, a71189l32i a7, a0, PT_AREG7 # restore a71190l32i a0, a0, PT_AREG0 # restore a01191rfe11921193.Leac: l32i a7, a2, PT_AREG7 # restore a71194l32i a0, a2, PT_AREG0 # restore a01195movi a2, -EFAULT1196rfe11971198.Lill: l32i a7, a2, PT_AREG7 # restore a71199l32i a0, a2, PT_AREG0 # restore a01200movi a2, -EINVAL1201rfe12021203ENDPROC(fast_syscall_xtensa)12041205#else /* CONFIG_FAST_SYSCALL_XTENSA */12061207ENTRY(fast_syscall_xtensa)12081209l32i a0, a2, PT_AREG0 # restore a01210movi a2, -ENOSYS1211rfe12121213ENDPROC(fast_syscall_xtensa)12141215#endif /* CONFIG_FAST_SYSCALL_XTENSA */121612171218/* fast_syscall_spill_registers.1219*1220* Entry condition:1221*1222* a0: trashed, original value saved on stack (PT_AREG0)1223* a1: a11224* a2: new stack pointer, original in DEPC1225* a3: a31226* depc: a2, original value saved on stack (PT_DEPC)1227* excsave_1: dispatch table1228*1229* Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.1230*/12311232#if defined(CONFIG_FAST_SYSCALL_SPILL_REGISTERS) && \1233defined(USER_SUPPORT_WINDOWED)12341235ENTRY(fast_syscall_spill_registers)12361237/* Register a FIXUP handler (pass current wb as a parameter) */12381239xsr a3, excsave11240movi a0, fast_syscall_spill_registers_fixup1241s32i a0, a3, EXC_TABLE_FIXUP1242rsr a0, windowbase1243s32i a0, a3, EXC_TABLE_PARAM1244xsr a3, excsave1 # restore a3 and excsave_112451246/* Save a3, a4 and SAR on stack. */12471248rsr a0, sar1249s32i a3, a2, PT_AREG31250s32i a0, a2, PT_SAR12511252/* The spill routine might clobber a4, a7, a8, a11, a12, and a15. */12531254s32i a4, a2, PT_AREG41255s32i a7, a2, PT_AREG71256s32i a8, a2, PT_AREG81257s32i a11, a2, PT_AREG111258s32i a12, a2, PT_AREG121259s32i a15, a2, PT_AREG1512601261/*1262* Rotate ws so that the current windowbase is at bit 0.1263* Assume ws = xxxwww1yy (www1 current window frame).1264* Rotate ws right so that a4 = yyxxxwww1.1265*/12661267rsr a0, windowbase1268rsr a3, windowstart # a3 = xxxwww1yy1269ssr a0 # holds WB1270slli a0, a3, WSBITS1271or a3, a3, a0 # a3 = xxxwww1yyxxxwww1yy1272srl a3, a3 # a3 = 00xxxwww1yyxxxwww112731274/* We are done if there are no more than the current register frame. */12751276extui a3, a3, 1, WSBITS-1 # a3 = 0yyxxxwww1277movi a0, (1 << (WSBITS-1))1278_beqz a3, .Lnospill # only one active frame? jump12791280/* We want 1 at the top, so that we return to the current windowbase */12811282or a3, a3, a0 # 1yyxxxwww12831284/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */12851286wsr a3, windowstart # save shifted windowstart1287neg a0, a31288and a3, a0, a3 # first bit set from right: 00001000012891290ffs_ws a0, a3 # a0: shifts to skip empty frames1291movi a3, WSBITS1292sub a0, a3, a0 # WSBITS-a0:number of 0-bits from right1293ssr a0 # save in SAR for later.12941295rsr a3, windowbase1296add a3, a3, a01297wsr a3, windowbase1298rsync12991300rsr a3, windowstart1301srl a3, a3 # shift windowstart13021303/* WB is now just one frame below the oldest frame in the register1304window. WS is shifted so the oldest frame is in bit 0, thus, WB1305and WS differ by one 4-register frame. */13061307/* Save frames. Depending what call was used (call4, call8, call12),1308* we have to save 4,8. or 12 registers.1309*/131013111312.Lloop: _bbsi.l a3, 1, .Lc41313_bbci.l a3, 2, .Lc1213141315.Lc8: s32e a4, a13, -161316l32e a4, a5, -121317s32e a8, a4, -321318s32e a5, a13, -121319s32e a6, a13, -81320s32e a7, a13, -41321s32e a9, a4, -281322s32e a10, a4, -241323s32e a11, a4, -201324srli a11, a3, 2 # shift windowbase by 21325rotw 21326_bnei a3, 1, .Lloop1327j .Lexit13281329.Lc4: s32e a4, a9, -161330s32e a5, a9, -121331s32e a6, a9, -81332s32e a7, a9, -413331334srli a7, a3, 11335rotw 11336_bnei a3, 1, .Lloop1337j .Lexit13381339.Lc12: _bbci.l a3, 3, .Linvalid_mask # bit 2 shouldn't be zero!13401341/* 12-register frame (call12) */13421343l32e a0, a5, -121344s32e a8, a0, -481345mov a8, a013461347s32e a9, a8, -441348s32e a10, a8, -401349s32e a11, a8, -361350s32e a12, a8, -321351s32e a13, a8, -281352s32e a14, a8, -241353s32e a15, a8, -201354srli a15, a3, 313551356/* The stack pointer for a4..a7 is out of reach, so we rotate the1357* window, grab the stackpointer, and rotate back.1358* Alternatively, we could also use the following approach, but that1359* makes the fixup routine much more complicated:1360* rotw 11361* s32e a0, a13, -161362* ...1363* rotw 21364*/13651366rotw 11367mov a4, a131368rotw -113691370s32e a4, a8, -161371s32e a5, a8, -121372s32e a6, a8, -81373s32e a7, a8, -413741375rotw 313761377_beqi a3, 1, .Lexit1378j .Lloop13791380.Lexit:13811382/* Done. Do the final rotation and set WS */13831384rotw 11385rsr a3, windowbase1386ssl a31387movi a3, 11388sll a3, a31389wsr a3, windowstart1390.Lnospill:13911392/* Advance PC, restore registers and SAR, and return from exception. */13931394l32i a3, a2, PT_SAR1395l32i a0, a2, PT_AREG01396wsr a3, sar1397l32i a3, a2, PT_AREG313981399/* Restore clobbered registers. */14001401l32i a4, a2, PT_AREG41402l32i a7, a2, PT_AREG71403l32i a8, a2, PT_AREG81404l32i a11, a2, PT_AREG111405l32i a12, a2, PT_AREG121406l32i a15, a2, PT_AREG1514071408movi a2, 01409rfe14101411.Linvalid_mask:14121413/* We get here because of an unrecoverable error in the window1414* registers, so set up a dummy frame and kill the user application.1415* Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.1416*/14171418movi a0, 11419movi a1, 014201421wsr a0, windowstart1422wsr a1, windowbase1423rsync14241425movi a0, 014261427rsr a3, excsave11428l32i a1, a3, EXC_TABLE_KSTK14291430movi a4, KERNEL_PS_WOE_MASK | LOCKLEVEL1431wsr a4, ps1432rsync14331434movi abi_arg0, SIGSEGV1435abi_call make_task_dead14361437/* shouldn't return, so panic */14381439wsr a0, excsave11440call0 unrecoverable_exception # should not return14411: j 1b144214431444ENDPROC(fast_syscall_spill_registers)14451446/* Fixup handler.1447*1448* We get here if the spill routine causes an exception, e.g. tlb miss.1449* We basically restore WINDOWBASE and WINDOWSTART to the condition when1450* we entered the spill routine and jump to the user exception handler.1451*1452* Note that we only need to restore the bits in windowstart that have not1453* been spilled yet by the _spill_register routine. Luckily, a3 contains a1454* rotated windowstart with only those bits set for frames that haven't been1455* spilled yet. Because a3 is rotated such that bit 0 represents the register1456* frame for the current windowbase - 1, we need to rotate a3 left by the1457* value of the current windowbase + 1 and move it to windowstart.1458*1459* a0: value of depc, original value in depc1460* a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE1461* a3: exctable, original value in excsave11462*/14631464ENTRY(fast_syscall_spill_registers_fixup)14651466rsr a2, windowbase # get current windowbase (a2 is saved)1467xsr a0, depc # restore depc and a01468ssl a2 # set shift (32 - WB)14691470/* We need to make sure the current registers (a0-a3) are preserved.1471* To do this, we simply set the bit for the current window frame1472* in WS, so that the exception handlers save them to the task stack.1473*1474* Note: we use a3 to set the windowbase, so we take a special care1475* of it, saving it in the original _spill_registers frame across1476* the exception handler call.1477*/14781479xsr a3, excsave1 # get spill-mask1480slli a3, a3, 1 # shift left by one1481addi a3, a3, 1 # set the bit for the current window frame14821483slli a2, a3, 32-WSBITS1484src a2, a3, a2 # a2 = xxwww1yyxxxwww1yy......1485wsr a2, windowstart # set corrected windowstart14861487srli a3, a3, 11488rsr a2, excsave11489l32i a2, a2, EXC_TABLE_DOUBLE_SAVE # restore a21490xsr a2, excsave11491s32i a3, a2, EXC_TABLE_DOUBLE_SAVE # save a31492l32i a3, a2, EXC_TABLE_PARAM # original WB (in user task)1493xsr a2, excsave114941495/* Return to the original (user task) WINDOWBASE.1496* We leave the following frame behind:1497* a0, a1, a2 same1498* a3: trashed (saved in EXC_TABLE_DOUBLE_SAVE)1499* depc: depc (we have to return to that address)1500* excsave_1: exctable1501*/15021503wsr a3, windowbase1504rsync15051506/* We are now in the original frame when we entered _spill_registers:1507* a0: return address1508* a1: used, stack pointer1509* a2: kernel stack pointer1510* a3: available1511* depc: exception address1512* excsave: exctable1513* Note: This frame might be the same as above.1514*/15151516/* Setup stack pointer. */15171518addi a2, a2, -PT_USER_SIZE1519s32i a0, a2, PT_AREG015201521/* Make sure we return to this fixup handler. */15221523movi a3, fast_syscall_spill_registers_fixup_return1524s32i a3, a2, PT_DEPC # setup depc15251526/* Jump to the exception handler. */15271528rsr a3, excsave11529rsr a0, exccause1530addx4 a0, a0, a3 # find entry in table1531l32i a0, a0, EXC_TABLE_FAST_USER # load handler1532l32i a3, a3, EXC_TABLE_DOUBLE_SAVE1533jx a015341535ENDPROC(fast_syscall_spill_registers_fixup)15361537ENTRY(fast_syscall_spill_registers_fixup_return)15381539/* When we return here, all registers have been restored (a2: DEPC) */15401541wsr a2, depc # exception address15421543/* Restore fixup handler. */15441545rsr a2, excsave11546s32i a3, a2, EXC_TABLE_DOUBLE_SAVE1547movi a3, fast_syscall_spill_registers_fixup1548s32i a3, a2, EXC_TABLE_FIXUP1549rsr a3, windowbase1550s32i a3, a2, EXC_TABLE_PARAM1551l32i a2, a2, EXC_TABLE_KSTK15521553/* Load WB at the time the exception occurred. */15541555rsr a3, sar # WB is still in SAR1556neg a3, a31557wsr a3, windowbase1558rsync15591560rsr a3, excsave11561l32i a3, a3, EXC_TABLE_DOUBLE_SAVE15621563rfde15641565ENDPROC(fast_syscall_spill_registers_fixup_return)15661567#else /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */15681569ENTRY(fast_syscall_spill_registers)15701571l32i a0, a2, PT_AREG0 # restore a01572movi a2, -ENOSYS1573rfe15741575ENDPROC(fast_syscall_spill_registers)15761577#endif /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */15781579#ifdef CONFIG_MMU1580/*1581* We should never get here. Bail out!1582*/15831584ENTRY(fast_second_level_miss_double_kernel)158515861:1587call0 unrecoverable_exception # should not return15881: j 1b15891590ENDPROC(fast_second_level_miss_double_kernel)15911592/* First-level entry handler for user, kernel, and double 2nd-level1593* TLB miss exceptions. Note that for now, user and kernel miss1594* exceptions share the same entry point and are handled identically.1595*1596* An old, less-efficient C version of this function used to exist.1597* We include it below, interleaved as comments, for reference.1598*1599* Entry condition:1600*1601* a0: trashed, original value saved on stack (PT_AREG0)1602* a1: a11603* a2: new stack pointer, original in DEPC1604* a3: a31605* depc: a2, original value saved on stack (PT_DEPC)1606* excsave_1: dispatch table1607*1608* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC1609* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception1610*/16111612ENTRY(fast_second_level_miss)16131614/* Save a1 and a3. Note: we don't expect a double exception. */16151616s32i a1, a2, PT_AREG11617s32i a3, a2, PT_AREG316181619/* We need to map the page of PTEs for the user task. Find1620* the pointer to that page. Also, it's possible for tsk->mm1621* to be NULL while tsk->active_mm is nonzero if we faulted on1622* a vmalloc address. In that rare case, we must use1623* active_mm instead to avoid a fault in this handler. See1624*1625* http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html1626* (or search Internet on "mm vs. active_mm")1627*1628* if (!mm)1629* mm = tsk->active_mm;1630* pgd = pgd_offset (mm, regs->excvaddr);1631* pmd = pmd_offset (pgd, regs->excvaddr);1632* pmdval = *pmd;1633*/16341635GET_CURRENT(a1,a2)1636l32i a0, a1, TASK_MM # tsk->mm1637beqz a0, .Lfast_second_level_miss_no_mm16381639.Lfast_second_level_miss_continue:1640rsr a3, excvaddr # fault address1641_PGD_OFFSET(a0, a3, a1)1642l32i a0, a0, 0 # read pmdval1643beqz a0, .Lfast_second_level_miss_no_pmd16441645/* Read ptevaddr and convert to top of page-table page.1646*1647* vpnval = read_ptevaddr_register() & PAGE_MASK;1648* vpnval += DTLB_WAY_PGTABLE;1649* pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);1650* write_dtlb_entry (pteval, vpnval);1651*1652* The messy computation for 'pteval' above really simplifies1653* into the following:1654*1655* pteval = ((pmdval - PAGE_OFFSET + PHYS_OFFSET) & PAGE_MASK)1656* | PAGE_DIRECTORY1657*/16581659movi a1, (PHYS_OFFSET - PAGE_OFFSET) & 0xffffffff1660add a0, a0, a1 # pmdval - PAGE_OFFSET1661extui a1, a0, 0, PAGE_SHIFT # ... & PAGE_MASK1662xor a0, a0, a116631664movi a1, _PAGE_DIRECTORY1665or a0, a0, a1 # ... | PAGE_DIRECTORY16661667/*1668* We utilize all three wired-ways (7-9) to hold pmd translations.1669* Memory regions are mapped to the DTLBs according to bits 28 and 29.1670* This allows to map the three most common regions to three different1671* DTLBs:1672* 0,1 -> way 7 program (0040.0000) and virtual (c000.0000)1673* 2 -> way 8 shared libaries (2000.0000)1674* 3 -> way 0 stack (3000.0000)1675*/16761677extui a3, a3, 28, 2 # addr. bit 28 and 29 0,1,2,31678rsr a1, ptevaddr1679addx2 a3, a3, a3 # -> 0,3,6,91680srli a1, a1, PAGE_SHIFT1681extui a3, a3, 2, 2 # -> 0,0,1,21682slli a1, a1, PAGE_SHIFT # ptevaddr & PAGE_MASK1683addi a3, a3, DTLB_WAY_PGD1684add a1, a1, a3 # ... + way_number16851686.Lfast_second_level_miss_wdtlb:1687wdtlb a0, a11688dsync16891690/* Exit critical section. */1691.Lfast_second_level_miss_skip_wdtlb:1692rsr a3, excsave11693movi a0, 01694s32i a0, a3, EXC_TABLE_FIXUP16951696/* Restore the working registers, and return. */16971698l32i a0, a2, PT_AREG01699l32i a1, a2, PT_AREG11700l32i a3, a2, PT_AREG31701l32i a2, a2, PT_DEPC17021703bgeui a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f17041705/* Restore excsave1 and return. */17061707rsr a2, depc1708rfe17091710/* Return from double exception. */171117121: xsr a2, depc1713esync1714rfde17151716.Lfast_second_level_miss_no_mm:1717l32i a0, a1, TASK_ACTIVE_MM # unlikely case mm == 01718bnez a0, .Lfast_second_level_miss_continue17191720/* Even more unlikely case active_mm == 0.1721* We can get here with NMI in the middle of context_switch that1722* touches vmalloc area.1723*/1724movi a0, init_mm1725j .Lfast_second_level_miss_continue17261727.Lfast_second_level_miss_no_pmd:1728#if (DCACHE_WAY_SIZE > PAGE_SIZE)17291730/* Special case for cache aliasing.1731* We (should) only get here if a clear_user_page, copy_user_page1732* or the aliased cache flush functions got preemptively interrupted1733* by another task. Re-establish temporary mapping to the1734* TLBTEMP_BASE areas.1735*/17361737/* We shouldn't be in a double exception */17381739l32i a0, a2, PT_DEPC1740bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lfast_second_level_miss_slow17411742/* Make sure the exception originated in the special functions */17431744movi a0, __tlbtemp_mapping_start1745rsr a3, epc11746bltu a3, a0, .Lfast_second_level_miss_slow1747movi a0, __tlbtemp_mapping_end1748bgeu a3, a0, .Lfast_second_level_miss_slow17491750/* Check if excvaddr was in one of the TLBTEMP_BASE areas. */17511752movi a3, TLBTEMP_BASE_11753rsr a0, excvaddr1754bltu a0, a3, .Lfast_second_level_miss_slow17551756addi a1, a0, -TLBTEMP_SIZE1757bgeu a1, a3, .Lfast_second_level_miss_slow17581759/* Check if we have to restore an ITLB mapping. */17601761movi a1, __tlbtemp_mapping_itlb1762rsr a3, epc11763sub a3, a3, a117641765/* Calculate VPN */17661767movi a1, PAGE_MASK1768and a1, a1, a017691770/* Jump for ITLB entry */17711772bgez a3, 1f17731774/* We can use up to two TLBTEMP areas, one for src and one for dst. */17751776extui a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 11777add a1, a3, a117781779/* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */17801781mov a0, a61782movnez a0, a7, a31783j .Lfast_second_level_miss_wdtlb17841785/* ITLB entry. We only use dst in a6. */178617871: witlb a6, a11788isync1789j .Lfast_second_level_miss_skip_wdtlb179017911792#endif // DCACHE_WAY_SIZE > PAGE_SIZE17931794/* Invalid PGD, default exception handling */1795.Lfast_second_level_miss_slow:17961797rsr a1, depc1798s32i a1, a2, PT_AREG21799mov a1, a218001801rsr a2, ps1802bbsi.l a2, PS_UM_BIT, 1f1803call0 _kernel_exception18041: call0 _user_exception18051806ENDPROC(fast_second_level_miss)18071808/*1809* StoreProhibitedException1810*1811* Update the pte and invalidate the itlb mapping for this pte.1812*1813* Entry condition:1814*1815* a0: trashed, original value saved on stack (PT_AREG0)1816* a1: a11817* a2: new stack pointer, original in DEPC1818* a3: a31819* depc: a2, original value saved on stack (PT_DEPC)1820* excsave_1: dispatch table1821*1822* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC1823* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception1824*/18251826ENTRY(fast_store_prohibited)18271828/* Save a1 and a3. */18291830s32i a1, a2, PT_AREG11831s32i a3, a2, PT_AREG318321833GET_CURRENT(a1,a2)1834l32i a0, a1, TASK_MM # tsk->mm1835beqz a0, .Lfast_store_no_mm18361837.Lfast_store_continue:1838rsr a1, excvaddr # fault address1839_PGD_OFFSET(a0, a1, a3)1840l32i a0, a0, 01841beqz a0, .Lfast_store_slow18421843/*1844* Note that we test _PAGE_WRITABLE_BIT only if PTE is present1845* and is not PAGE_NONE. See pgtable.h for possible PTE layouts.1846*/18471848_PTE_OFFSET(a0, a1, a3)1849l32i a3, a0, 0 # read pteval1850movi a1, _PAGE_CA_INVALID1851ball a3, a1, .Lfast_store_slow1852bbci.l a3, _PAGE_WRITABLE_BIT, .Lfast_store_slow18531854movi a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE1855or a3, a3, a11856rsr a1, excvaddr1857s32i a3, a0, 018581859/* We need to flush the cache if we have page coloring. */1860#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK1861dhwb a0, 01862#endif1863pdtlb a0, a11864wdtlb a3, a018651866/* Exit critical section. */18671868movi a0, 01869rsr a3, excsave11870s32i a0, a3, EXC_TABLE_FIXUP18711872/* Restore the working registers, and return. */18731874l32i a3, a2, PT_AREG31875l32i a1, a2, PT_AREG11876l32i a0, a2, PT_AREG01877l32i a2, a2, PT_DEPC18781879bgeui a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f1880rsr a2, depc1881rfe18821883/* Double exception. Restore FIXUP handler and return. */188418851: xsr a2, depc1886esync1887rfde18881889.Lfast_store_no_mm:1890l32i a0, a1, TASK_ACTIVE_MM # unlikely case mm == 01891j .Lfast_store_continue18921893/* If there was a problem, handle fault in C */1894.Lfast_store_slow:1895rsr a1, excvaddr1896pdtlb a0, a11897bbci.l a0, DTLB_HIT_BIT, 1f1898idtlb a018991:1900rsr a3, depc # still holds a21901s32i a3, a2, PT_AREG21902mov a1, a219031904rsr a2, ps1905bbsi.l a2, PS_UM_BIT, 1f1906call0 _kernel_exception19071: call0 _user_exception19081909ENDPROC(fast_store_prohibited)19101911#endif /* CONFIG_MMU */19121913.text1914/*1915* System Calls.1916*1917* void system_call (struct pt_regs* regs, int exccause)1918* a2 a31919*/1920.literal_position19211922ENTRY(system_call)19231924#if defined(__XTENSA_WINDOWED_ABI__)1925abi_entry_default1926#elif defined(__XTENSA_CALL0_ABI__)1927abi_entry(12)19281929s32i a0, sp, 01930s32i abi_saved0, sp, 41931s32i abi_saved1, sp, 81932mov abi_saved0, a21933#else1934#error Unsupported Xtensa ABI1935#endif19361937/* regs->syscall = regs->areg[2] */19381939l32i a7, abi_saved0, PT_AREG21940s32i a7, abi_saved0, PT_SYSCALL19411942GET_THREAD_INFO(a4, a1)1943l32i abi_saved1, a4, TI_FLAGS1944movi a4, _TIF_WORK_MASK1945and abi_saved1, abi_saved1, a41946beqz abi_saved1, 1f19471948mov abi_arg0, abi_saved01949abi_call do_syscall_trace_enter1950beqz abi_rv, .Lsyscall_exit1951l32i a7, abi_saved0, PT_SYSCALL195219531:1954/* syscall = sys_call_table[syscall_nr] */19551956movi a4, sys_call_table1957movi a5, __NR_syscalls1958movi abi_rv, -ENOSYS1959bgeu a7, a5, 1f19601961addx4 a4, a7, a41962l32i abi_tmp0, a4, 019631964/* Load args: arg0 - arg5 are passed via regs. */19651966l32i abi_arg0, abi_saved0, PT_AREG61967l32i abi_arg1, abi_saved0, PT_AREG31968l32i abi_arg2, abi_saved0, PT_AREG41969l32i abi_arg3, abi_saved0, PT_AREG51970l32i abi_arg4, abi_saved0, PT_AREG81971l32i abi_arg5, abi_saved0, PT_AREG919721973abi_callx abi_tmp0197419751: /* regs->areg[2] = return_value */19761977s32i abi_rv, abi_saved0, PT_AREG21978bnez abi_saved1, 1f1979.Lsyscall_exit:1980#if defined(__XTENSA_WINDOWED_ABI__)1981abi_ret_default1982#elif defined(__XTENSA_CALL0_ABI__)1983l32i a0, sp, 01984l32i abi_saved0, sp, 41985l32i abi_saved1, sp, 81986abi_ret(12)1987#else1988#error Unsupported Xtensa ABI1989#endif199019911:1992mov abi_arg0, abi_saved01993abi_call do_syscall_trace_leave1994j .Lsyscall_exit19951996ENDPROC(system_call)19971998/*1999* Spill live registers on the kernel stack macro.2000*2001* Entry condition: ps.woe is set, ps.excm is cleared2002* Exit condition: windowstart has single bit set2003* May clobber: a12, a132004*/2005.macro spill_registers_kernel20062007#if XCHAL_NUM_AREGS > 162008call12 1f2009_j 2f2010retw2011.align 420121:2013_entry a1, 482014addi a12, a0, 32015#if XCHAL_NUM_AREGS > 322016.rept (XCHAL_NUM_AREGS - 32) / 122017_entry a1, 482018mov a12, a02019.endr2020#endif2021_entry a1, 162022#if XCHAL_NUM_AREGS % 12 == 02023mov a8, a82024#elif XCHAL_NUM_AREGS % 12 == 42025mov a12, a122026#elif XCHAL_NUM_AREGS % 12 == 82027mov a4, a42028#endif2029retw20302:2031#else2032mov a12, a122033#endif2034.endm20352036/*2037* Task switch.2038*2039* struct task* _switch_to (struct task* prev, struct task* next)2040* a2 a2 a32041*/20422043ENTRY(_switch_to)20442045#if defined(__XTENSA_WINDOWED_ABI__)2046abi_entry(XTENSA_SPILL_STACK_RESERVE)2047#elif defined(__XTENSA_CALL0_ABI__)2048abi_entry(16)20492050s32i a12, sp, 02051s32i a13, sp, 42052s32i a14, sp, 82053s32i a15, sp, 122054#else2055#error Unsupported Xtensa ABI2056#endif2057mov a11, a3 # and 'next' (a3)20582059l32i a4, a2, TASK_THREAD_INFO2060l32i a5, a3, TASK_THREAD_INFO20612062save_xtregs_user a4 a6 a8 a9 a12 a13 THREAD_XTREGS_USER20632064#if THREAD_RA > 1020 || THREAD_SP > 10202065addi a10, a2, TASK_THREAD2066s32i a0, a10, THREAD_RA - TASK_THREAD # save return address2067s32i a1, a10, THREAD_SP - TASK_THREAD # save stack pointer2068#else2069s32i a0, a2, THREAD_RA # save return address2070s32i a1, a2, THREAD_SP # save stack pointer2071#endif20722073#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)2074movi a6, __stack_chk_guard2075l32i a8, a3, TASK_STACK_CANARY2076s32i a8, a6, 02077#endif20782079/* Disable ints while we manipulate the stack pointer. */20802081irq_save a14, a32082rsync20832084/* Switch CPENABLE */20852086#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)2087l32i a3, a5, THREAD_CPENABLE2088#ifdef CONFIG_SMP2089beqz a3, 1f2090memw # pairs with memw (2) in fast_coprocessor2091l32i a6, a5, THREAD_CP_OWNER_CPU2092l32i a7, a5, THREAD_CPU2093beq a6, a7, 1f # load 0 into CPENABLE if current CPU is not the owner2094movi a3, 020951:2096#endif2097wsr a3, cpenable2098#endif20992100#if XCHAL_HAVE_EXCLUSIVE2101l32i a3, a5, THREAD_ATOMCTL82102getex a32103s32i a3, a4, THREAD_ATOMCTL82104#endif21052106/* Flush register file. */21072108#if defined(__XTENSA_WINDOWED_ABI__)2109spill_registers_kernel2110#endif21112112/* Set kernel stack (and leave critical section)2113* Note: It's save to set it here. The stack will not be overwritten2114* because the kernel stack will only be loaded again after2115* we return from kernel space.2116*/21172118rsr a3, excsave1 # exc_table2119addi a7, a5, PT_REGS_OFFSET2120s32i a7, a3, EXC_TABLE_KSTK21212122/* restore context of the task 'next' */21232124l32i a0, a11, THREAD_RA # restore return address2125l32i a1, a11, THREAD_SP # restore stack pointer21262127load_xtregs_user a5 a6 a8 a9 a12 a13 THREAD_XTREGS_USER21282129wsr a14, ps2130rsync21312132#if defined(__XTENSA_WINDOWED_ABI__)2133abi_ret(XTENSA_SPILL_STACK_RESERVE)2134#elif defined(__XTENSA_CALL0_ABI__)2135l32i a12, sp, 02136l32i a13, sp, 42137l32i a14, sp, 82138l32i a15, sp, 122139abi_ret(16)2140#else2141#error Unsupported Xtensa ABI2142#endif21432144ENDPROC(_switch_to)21452146ENTRY(ret_from_fork)21472148/* void schedule_tail (struct task_struct *prev)2149* Note: prev is still in abi_arg0 (return value from fake call frame)2150*/2151abi_call schedule_tail21522153mov abi_arg0, a12154abi_call do_syscall_trace_leave2155j common_exception_return21562157ENDPROC(ret_from_fork)21582159/*2160* Kernel thread creation helper2161* On entry, set up by copy_thread: abi_saved0 = thread_fn,2162* abi_saved1 = thread_fn arg. Left from _switch_to: abi_arg0 = prev2163*/2164ENTRY(ret_from_kernel_thread)21652166abi_call schedule_tail2167mov abi_arg0, abi_saved12168abi_callx abi_saved02169j common_exception_return21702171ENDPROC(ret_from_kernel_thread)21722173#ifdef CONFIG_HIBERNATION21742175.section .bss, "aw"2176.align 42177.Lsaved_regs:2178#if defined(__XTENSA_WINDOWED_ABI__)2179.fill 2, 42180#elif defined(__XTENSA_CALL0_ABI__)2181.fill 6, 42182#else2183#error Unsupported Xtensa ABI2184#endif2185.align XCHAL_NCP_SA_ALIGN2186.Lsaved_user_regs:2187.fill XTREGS_USER_SIZE, 121882189.previous21902191ENTRY(swsusp_arch_suspend)21922193abi_entry_default21942195movi a2, .Lsaved_regs2196movi a3, .Lsaved_user_regs2197s32i a0, a2, 02198s32i a1, a2, 42199save_xtregs_user a3 a4 a5 a6 a7 a8 02200#if defined(__XTENSA_WINDOWED_ABI__)2201spill_registers_kernel2202#elif defined(__XTENSA_CALL0_ABI__)2203s32i a12, a2, 82204s32i a13, a2, 122205s32i a14, a2, 162206s32i a15, a2, 202207#else2208#error Unsupported Xtensa ABI2209#endif2210abi_call swsusp_save2211mov a2, abi_rv2212abi_ret_default22132214ENDPROC(swsusp_arch_suspend)22152216ENTRY(swsusp_arch_resume)22172218abi_entry_default22192220#if defined(__XTENSA_WINDOWED_ABI__)2221spill_registers_kernel2222#endif22232224movi a2, restore_pblist2225l32i a2, a2, 022262227.Lcopy_pbe:2228l32i a3, a2, PBE_ADDRESS2229l32i a4, a2, PBE_ORIG_ADDRESS22302231__loopi a3, a9, PAGE_SIZE, 162232l32i a5, a3, 02233l32i a6, a3, 42234l32i a7, a3, 82235l32i a8, a3, 122236addi a3, a3, 162237s32i a5, a4, 02238s32i a6, a4, 42239s32i a7, a4, 82240s32i a8, a4, 122241addi a4, a4, 162242__endl a3, a922432244l32i a2, a2, PBE_NEXT2245bnez a2, .Lcopy_pbe22462247movi a2, .Lsaved_regs2248movi a3, .Lsaved_user_regs2249l32i a0, a2, 02250l32i a1, a2, 42251load_xtregs_user a3 a4 a5 a6 a7 a8 02252#if defined(__XTENSA_CALL0_ABI__)2253l32i a12, a2, 82254l32i a13, a2, 122255l32i a14, a2, 162256l32i a15, a2, 202257#endif2258movi a2, 02259abi_ret_default22602261ENDPROC(swsusp_arch_resume)22622263#endif226422652266