/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)3* Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)4*5* Vineetg: March 2009 (Supporting 2 levels of Interrupts)6* Stack switching code can no longer reliably rely on the fact that7* if we are NOT in user mode, stack is switched to kernel mode.8* e.g. L2 IRQ interrupted a L1 ISR which had not yet completed9* its prologue including stack switching from user mode10*11* Vineetg: Aug 28th 2008: Bug #9498412* -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap13* Normally CPU does this automatically, however when doing FAKE rtie,14* we also need to explicitly do this. The problem in macros15* FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit16* was being "CLEARED" rather then "SET". Actually "SET" clears ZOL context17*18* Vineetg: May 5th 200819* -Modified CALLEE_REG save/restore macros to handle the fact that20* r25 contains the kernel current task ptr21* - Defined Stack Switching Macro to be reused in all intr/excp hdlrs22* - Shaved off 11 instructions from RESTORE_ALL_INT1 by using the23* address Write back load ld.ab instead of separate ld/add instn24*25* Amit Bhor, Sameer Dhavale: Codito Technologies 200426*/2728#ifndef __ASM_ARC_ENTRY_COMPACT_H29#define __ASM_ARC_ENTRY_COMPACT_H3031#include <asm/asm-offsets.h>32#include <asm/irqflags-compact.h>33#include <asm/thread_info.h> /* For THREAD_SIZE */3435/* Note on the LD/ST addr modes with addr reg wback36*37* LD.a same as LD.aw38*39* LD.a reg1, [reg2, x] => Pre Incr40* Eff Addr for load = [reg2 + x]41*42* LD.ab reg1, [reg2, x] => Post Incr43* Eff Addr for load = [reg2]44*/4546.macro PUSHAX aux47lr r9, [\aux]48push r949.endm5051.macro POPAX aux52pop r953sr r9, [\aux]54.endm5556.macro SAVE_R0_TO_R1257push r058push r159push r260push r361push r462push r563push r664push r765push r866push r967push r1068push r1169push r1270.endm7172.macro RESTORE_R12_TO_R073pop r1274pop r1175pop r1076pop r977pop r878pop r779pop r680pop r581pop r482pop r383pop r284pop r185pop r086.endm8788.macro SAVE_ABI_CALLEE_REGS89push r1390push r1491push r1592push r1693push r1794push r1895push r1996push r2097push r2198push r2299push r23100push r24101push r25102.endm103104.macro RESTORE_ABI_CALLEE_REGS105pop r25106pop r24107pop r23108pop r22109pop r21110pop r20111pop r19112pop r18113pop r17114pop r16115pop r15116pop r14117pop r13118.endm119120/*--------------------------------------------------------------121* Switch to Kernel Mode stack if SP points to User Mode stack122*123* Entry : r9 contains pre-IRQ/exception/trap status32124* Exit : SP set to K mode stack125* SP at the time of entry (K/U) saved @ pt_regs->sp126* Clobbers: r9127*-------------------------------------------------------------*/128129.macro SWITCH_TO_KERNEL_STK130131/* User Mode when this happened ? Yes: Proceed to switch stack */132bbit1 r9, STATUS_U_BIT, 88f133134/* OK we were already in kernel mode when this event happened, thus can135* assume SP is kernel mode SP. _NO_ need to do any stack switching136*/137138#ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS139/* However....140* If Level 2 Interrupts enabled, we may end up with a corner case:141* 1. User Task executing142* 2. L1 IRQ taken, ISR starts (CPU auto-switched to KERNEL mode)143* 3. But before it could switch SP from USER to KERNEL stack144* a L2 IRQ "Interrupts" L1145* That way although L2 IRQ happened in Kernel mode, stack is still146* not switched.147* To handle this, we may need to switch stack even if in kernel mode148* provided SP has values in range of USER mode stack ( < 0x7000_0000 )149*/150brlo sp, VMALLOC_START, 88f151152/* TODO: vineetg:153* We need to be a bit more cautious here. What if a kernel bug in154* L1 ISR, caused SP to go whaco (some small value which looks like155* USER stk) and then we take L2 ISR.156* Above brlo alone would treat it as a valid L1-L2 scenario157* instead of shouting around158* The only feasible way is to make sure this L2 happened in159* L1 prelogue ONLY i.e. ilink2 is less than a pre-set marker in160* L1 ISR before it switches stack161*/162163#endif164165/*------Intr/Ecxp happened in kernel mode, SP already setup ------ */166/* save it nevertheless @ pt_regs->sp for uniformity */167168b.d 66f169st sp, [sp, PT_sp - SZ_PT_REGS]17017188: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */172173GET_CURR_TASK_ON_CPU r9174175/* With current tsk in r9, get its kernel mode stack base */176GET_TSK_STACK_BASE r9, r9177178/* save U mode SP @ pt_regs->sp */179st sp, [r9, PT_sp - SZ_PT_REGS]180181/* final SP switch */182mov sp, r918366:184.endm185186/*------------------------------------------------------------187* "FAKE" a rtie to return from CPU Exception context188* This is to re-enable Exceptions within exception189* Look at EV_ProtV to see how this is actually used190*-------------------------------------------------------------*/191192.macro FAKE_RET_FROM_EXCPN193194lr r9, [status32]195bclr r9, r9, STATUS_AE_BIT196or r9, r9, (STATUS_E1_MASK|STATUS_E2_MASK)197sr r9, [erstatus]198mov r9, 55f199sr r9, [eret]200rtie20155:202.endm203204/*--------------------------------------------------------------205* For early Exception/ISR Prologue, a core reg is temporarily needed to206* code the rest of prolog (stack switching). This is done by stashing207* it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP).208*209* Before saving the full regfile - this reg is restored back, only210* to be saved again on kernel mode stack, as part of pt_regs.211*-------------------------------------------------------------*/212.macro PROLOG_FREEUP_REG reg, mem213st \reg, [\mem]214.endm215216.macro PROLOG_RESTORE_REG reg, mem217ld \reg, [\mem]218.endm219220/*--------------------------------------------------------------221* Exception Entry prologue222* -Switches stack to K mode (if not already)223* -Saves the register file224*225* After this it is safe to call the "C" handlers226*-------------------------------------------------------------*/227.macro EXCEPTION_PROLOGUE_KEEP_AE228229/* Need at least 1 reg to code the early exception prologue */230PROLOG_FREEUP_REG r9, @ex_saved_reg1231232/* U/K mode at time of exception (stack not switched if already K) */233lr r9, [erstatus]234235/* ARC700 doesn't provide auto-stack switching */236SWITCH_TO_KERNEL_STK237238st.a r0, [sp, -8] /* orig_r0 needed for syscall (skip ECR slot) */239sub sp, sp, 4 /* skip pt_regs->sp, already saved above */240241/* Restore r9 used to code the early prologue */242PROLOG_RESTORE_REG r9, @ex_saved_reg1243244/* now we are ready to save the regfile */245SAVE_R0_TO_R12246PUSH gp247PUSH fp248PUSH blink249PUSHAX eret250PUSHAX erstatus251PUSH lp_count252PUSHAX lp_end253PUSHAX lp_start254PUSHAX erbta255256lr r10, [ecr]257st r10, [sp, PT_event]258259#ifdef CONFIG_ARC_CURR_IN_REG260/* gp already saved on stack: now load with "current" */261GET_CURR_TASK_ON_CPU gp262#endif263; OUTPUT: r10 has ECR expected by EV_Trap264.endm265266.macro EXCEPTION_PROLOGUE267268EXCEPTION_PROLOGUE_KEEP_AE ; return ECR in r10269270lr r0, [efa]271mov r1, sp272273FAKE_RET_FROM_EXCPN ; clobbers r9274.endm275276/*--------------------------------------------------------------277* Restore all registers used by system call or Exceptions278* SP should always be pointing to the next free stack element279* when entering this macro.280*281* NOTE:282*283* It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg284* for memory load operations. If used in that way interrupts are deferred285* by hardware and that is not good.286*-------------------------------------------------------------*/287.macro EXCEPTION_EPILOGUE288289POPAX erbta290POPAX lp_start291POPAX lp_end292293POP r9294mov lp_count, r9 ;LD to lp_count is not allowed295296POPAX erstatus297POPAX eret298POP blink299POP fp300POP gp301RESTORE_R12_TO_R0302303ld sp, [sp] /* restore original sp */304/* orig_r0, ECR skipped automatically */305.endm306307/* Dummy ECR values for Interrupts */308#define event_IRQ1 0x0031abcd309#define event_IRQ2 0x0032abcd310311.macro INTERRUPT_PROLOGUE LVL312313/* free up r9 as scratchpad */314PROLOG_FREEUP_REG r9, @int\LVL\()_saved_reg315316/* Which mode (user/kernel) was the system in when intr occurred */317lr r9, [status32_l\LVL\()]318319SWITCH_TO_KERNEL_STK320321322st.a 0x003\LVL\()abcd, [sp, -4] /* Dummy ECR */323sub sp, sp, 8 /* skip orig_r0 (not needed)324skip pt_regs->sp, already saved above */325326/* Restore r9 used to code the early prologue */327PROLOG_RESTORE_REG r9, @int\LVL\()_saved_reg328329SAVE_R0_TO_R12330PUSH gp331PUSH fp332PUSH blink333PUSH ilink\LVL\()334PUSHAX status32_l\LVL\()335PUSH lp_count336PUSHAX lp_end337PUSHAX lp_start338PUSHAX bta_l\LVL\()339340#ifdef CONFIG_ARC_CURR_IN_REG341/* gp already saved on stack: now load with "current" */342GET_CURR_TASK_ON_CPU gp343#endif344.endm345346/*--------------------------------------------------------------347* Restore all registers used by interrupt handlers.348*349* NOTE:350*351* It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg352* for memory load operations. If used in that way interrupts are deferred353* by hardware and that is not good.354*-------------------------------------------------------------*/355.macro INTERRUPT_EPILOGUE LVL356357POPAX bta_l\LVL\()358POPAX lp_start359POPAX lp_end360361POP r9362mov lp_count, r9 ;LD to lp_count is not allowed363364POPAX status32_l\LVL\()365POP ilink\LVL\()366POP blink367POP fp368POP gp369RESTORE_R12_TO_R0370371ld sp, [sp] /* restore original sp; orig_r0, ECR skipped implicitly */372.endm373374/* Get thread_info of "current" tsk */375.macro GET_CURR_THR_INFO_FROM_SP reg376bic \reg, sp, (THREAD_SIZE - 1)377.endm378379/* Get CPU-ID of this core */380.macro GET_CPU_ID reg381lr \reg, [identity]382lsr \reg, \reg, 8383bmsk \reg, \reg, 7384.endm385386#endif /* __ASM_ARC_ENTRY_COMPACT_H */387388389