162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com) 462306a36Sopenharmony_ci * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Vineetg: March 2009 (Supporting 2 levels of Interrupts) 762306a36Sopenharmony_ci * Stack switching code can no longer reliably rely on the fact that 862306a36Sopenharmony_ci * if we are NOT in user mode, stack is switched to kernel mode. 962306a36Sopenharmony_ci * e.g. L2 IRQ interrupted a L1 ISR which had not yet completed 1062306a36Sopenharmony_ci * it's prologue including stack switching from user mode 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * Vineetg: Aug 28th 2008: Bug #94984 1362306a36Sopenharmony_ci * -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap 1462306a36Sopenharmony_ci * Normally CPU does this automatically, however when doing FAKE rtie, 1562306a36Sopenharmony_ci * we also need to explicitly do this. The problem in macros 1662306a36Sopenharmony_ci * FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit 1762306a36Sopenharmony_ci * was being "CLEARED" rather then "SET". Actually "SET" clears ZOL context 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * Vineetg: May 5th 2008 2062306a36Sopenharmony_ci * -Modified CALLEE_REG save/restore macros to handle the fact that 2162306a36Sopenharmony_ci * r25 contains the kernel current task ptr 2262306a36Sopenharmony_ci * - Defined Stack Switching Macro to be reused in all intr/excp hdlrs 2362306a36Sopenharmony_ci * - Shaved off 11 instructions from RESTORE_ALL_INT1 by using the 2462306a36Sopenharmony_ci * address Write back load ld.ab instead of separate ld/add instn 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * Amit Bhor, Sameer Dhavale: Codito Technologies 2004 2762306a36Sopenharmony_ci */ 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#ifndef __ASM_ARC_ENTRY_COMPACT_H 3062306a36Sopenharmony_ci#define __ASM_ARC_ENTRY_COMPACT_H 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci#include <asm/asm-offsets.h> 3362306a36Sopenharmony_ci#include <asm/irqflags-compact.h> 3462306a36Sopenharmony_ci#include <asm/thread_info.h> /* For THREAD_SIZE */ 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci/*-------------------------------------------------------------- 3762306a36Sopenharmony_ci * Switch to Kernel Mode stack if SP points to User Mode stack 3862306a36Sopenharmony_ci * 3962306a36Sopenharmony_ci * Entry : r9 contains pre-IRQ/exception/trap status32 4062306a36Sopenharmony_ci * Exit : SP set to K mode stack 4162306a36Sopenharmony_ci * SP at the time of entry (K/U) saved @ pt_regs->sp 4262306a36Sopenharmony_ci * Clobbers: r9 4362306a36Sopenharmony_ci *-------------------------------------------------------------*/ 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci.macro SWITCH_TO_KERNEL_STK 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci /* User Mode when this happened ? Yes: Proceed to switch stack */ 4862306a36Sopenharmony_ci bbit1 r9, STATUS_U_BIT, 88f 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci /* OK we were already in kernel mode when this event happened, thus can 5162306a36Sopenharmony_ci * assume SP is kernel mode SP. _NO_ need to do any stack switching 5262306a36Sopenharmony_ci */ 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci#ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS 5562306a36Sopenharmony_ci /* However.... 5662306a36Sopenharmony_ci * If Level 2 Interrupts enabled, we may end up with a corner case: 5762306a36Sopenharmony_ci * 1. User Task executing 5862306a36Sopenharmony_ci * 2. L1 IRQ taken, ISR starts (CPU auto-switched to KERNEL mode) 5962306a36Sopenharmony_ci * 3. But before it could switch SP from USER to KERNEL stack 6062306a36Sopenharmony_ci * a L2 IRQ "Interrupts" L1 6162306a36Sopenharmony_ci * Thay way although L2 IRQ happened in Kernel mode, stack is still 6262306a36Sopenharmony_ci * not switched. 6362306a36Sopenharmony_ci * To handle this, we may need to switch stack even if in kernel mode 6462306a36Sopenharmony_ci * provided SP has values in range of USER mode stack ( < 0x7000_0000 ) 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_ci brlo sp, VMALLOC_START, 88f 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci /* TODO: vineetg: 6962306a36Sopenharmony_ci * We need to be a bit more cautious here. What if a kernel bug in 7062306a36Sopenharmony_ci * L1 ISR, caused SP to go whaco (some small value which looks like 7162306a36Sopenharmony_ci * USER stk) and then we take L2 ISR. 7262306a36Sopenharmony_ci * Above brlo alone would treat it as a valid L1-L2 scenario 7362306a36Sopenharmony_ci * instead of shouting around 7462306a36Sopenharmony_ci * The only feasible way is to make sure this L2 happened in 7562306a36Sopenharmony_ci * L1 prelogue ONLY i.e. ilink2 is less than a pre-set marker in 7662306a36Sopenharmony_ci * L1 ISR before it switches stack 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci#endif 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci /*------Intr/Ecxp happened in kernel mode, SP already setup ------ */ 8262306a36Sopenharmony_ci /* save it nevertheless @ pt_regs->sp for uniformity */ 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci b.d 66f 8562306a36Sopenharmony_ci st sp, [sp, PT_sp - SZ_PT_REGS] 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */ 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci GET_CURR_TASK_ON_CPU r9 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci /* With current tsk in r9, get it's kernel mode stack base */ 9262306a36Sopenharmony_ci GET_TSK_STACK_BASE r9, r9 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci /* save U mode SP @ pt_regs->sp */ 9562306a36Sopenharmony_ci st sp, [r9, PT_sp - SZ_PT_REGS] 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci /* final SP switch */ 9862306a36Sopenharmony_ci mov sp, r9 9962306a36Sopenharmony_ci66: 10062306a36Sopenharmony_ci.endm 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci/*------------------------------------------------------------ 10362306a36Sopenharmony_ci * "FAKE" a rtie to return from CPU Exception context 10462306a36Sopenharmony_ci * This is to re-enable Exceptions within exception 10562306a36Sopenharmony_ci * Look at EV_ProtV to see how this is actually used 10662306a36Sopenharmony_ci *-------------------------------------------------------------*/ 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci.macro FAKE_RET_FROM_EXCPN 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci lr r9, [status32] 11162306a36Sopenharmony_ci bclr r9, r9, STATUS_AE_BIT 11262306a36Sopenharmony_ci or r9, r9, (STATUS_E1_MASK|STATUS_E2_MASK) 11362306a36Sopenharmony_ci sr r9, [erstatus] 11462306a36Sopenharmony_ci mov r9, 55f 11562306a36Sopenharmony_ci sr r9, [eret] 11662306a36Sopenharmony_ci rtie 11762306a36Sopenharmony_ci55: 11862306a36Sopenharmony_ci.endm 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci/*-------------------------------------------------------------- 12162306a36Sopenharmony_ci * For early Exception/ISR Prologue, a core reg is temporarily needed to 12262306a36Sopenharmony_ci * code the rest of prolog (stack switching). This is done by stashing 12362306a36Sopenharmony_ci * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP). 12462306a36Sopenharmony_ci * 12562306a36Sopenharmony_ci * Before saving the full regfile - this reg is restored back, only 12662306a36Sopenharmony_ci * to be saved again on kernel mode stack, as part of pt_regs. 12762306a36Sopenharmony_ci *-------------------------------------------------------------*/ 12862306a36Sopenharmony_ci.macro PROLOG_FREEUP_REG reg, mem 12962306a36Sopenharmony_ci st \reg, [\mem] 13062306a36Sopenharmony_ci.endm 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci.macro PROLOG_RESTORE_REG reg, mem 13362306a36Sopenharmony_ci ld \reg, [\mem] 13462306a36Sopenharmony_ci.endm 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci/*-------------------------------------------------------------- 13762306a36Sopenharmony_ci * Exception Entry prologue 13862306a36Sopenharmony_ci * -Switches stack to K mode (if not already) 13962306a36Sopenharmony_ci * -Saves the register file 14062306a36Sopenharmony_ci * 14162306a36Sopenharmony_ci * After this it is safe to call the "C" handlers 14262306a36Sopenharmony_ci *-------------------------------------------------------------*/ 14362306a36Sopenharmony_ci.macro EXCEPTION_PROLOGUE_KEEP_AE 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci /* Need at least 1 reg to code the early exception prologue */ 14662306a36Sopenharmony_ci PROLOG_FREEUP_REG r9, @ex_saved_reg1 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci /* U/K mode at time of exception (stack not switched if already K) */ 14962306a36Sopenharmony_ci lr r9, [erstatus] 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci /* ARC700 doesn't provide auto-stack switching */ 15262306a36Sopenharmony_ci SWITCH_TO_KERNEL_STK 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci st.a r0, [sp, -8] /* orig_r0 needed for syscall (skip ECR slot) */ 15562306a36Sopenharmony_ci sub sp, sp, 4 /* skip pt_regs->sp, already saved above */ 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci /* Restore r9 used to code the early prologue */ 15862306a36Sopenharmony_ci PROLOG_RESTORE_REG r9, @ex_saved_reg1 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci /* now we are ready to save the regfile */ 16162306a36Sopenharmony_ci SAVE_R0_TO_R12 16262306a36Sopenharmony_ci PUSH gp 16362306a36Sopenharmony_ci PUSH fp 16462306a36Sopenharmony_ci PUSH blink 16562306a36Sopenharmony_ci PUSHAX eret 16662306a36Sopenharmony_ci PUSHAX erstatus 16762306a36Sopenharmony_ci PUSH lp_count 16862306a36Sopenharmony_ci PUSHAX lp_end 16962306a36Sopenharmony_ci PUSHAX lp_start 17062306a36Sopenharmony_ci PUSHAX erbta 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci lr r10, [ecr] 17362306a36Sopenharmony_ci st r10, [sp, PT_event] 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci#ifdef CONFIG_ARC_CURR_IN_REG 17662306a36Sopenharmony_ci /* gp already saved on stack: now load with "current" */ 17762306a36Sopenharmony_ci GET_CURR_TASK_ON_CPU gp 17862306a36Sopenharmony_ci#endif 17962306a36Sopenharmony_ci ; OUTPUT: r10 has ECR expected by EV_Trap 18062306a36Sopenharmony_ci.endm 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci.macro EXCEPTION_PROLOGUE 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci EXCEPTION_PROLOGUE_KEEP_AE ; return ECR in r10 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci lr r0, [efa] 18762306a36Sopenharmony_ci mov r1, sp 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci FAKE_RET_FROM_EXCPN ; clobbers r9 19062306a36Sopenharmony_ci.endm 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci/*-------------------------------------------------------------- 19362306a36Sopenharmony_ci * Restore all registers used by system call or Exceptions 19462306a36Sopenharmony_ci * SP should always be pointing to the next free stack element 19562306a36Sopenharmony_ci * when entering this macro. 19662306a36Sopenharmony_ci * 19762306a36Sopenharmony_ci * NOTE: 19862306a36Sopenharmony_ci * 19962306a36Sopenharmony_ci * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg 20062306a36Sopenharmony_ci * for memory load operations. If used in that way interrupts are deffered 20162306a36Sopenharmony_ci * by hardware and that is not good. 20262306a36Sopenharmony_ci *-------------------------------------------------------------*/ 20362306a36Sopenharmony_ci.macro EXCEPTION_EPILOGUE 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci POPAX erbta 20662306a36Sopenharmony_ci POPAX lp_start 20762306a36Sopenharmony_ci POPAX lp_end 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci POP r9 21062306a36Sopenharmony_ci mov lp_count, r9 ;LD to lp_count is not allowed 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci POPAX erstatus 21362306a36Sopenharmony_ci POPAX eret 21462306a36Sopenharmony_ci POP blink 21562306a36Sopenharmony_ci POP fp 21662306a36Sopenharmony_ci POP gp 21762306a36Sopenharmony_ci RESTORE_R12_TO_R0 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci ld sp, [sp] /* restore original sp */ 22062306a36Sopenharmony_ci /* orig_r0, ECR skipped automatically */ 22162306a36Sopenharmony_ci.endm 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci/* Dummy ECR values for Interrupts */ 22462306a36Sopenharmony_ci#define event_IRQ1 0x0031abcd 22562306a36Sopenharmony_ci#define event_IRQ2 0x0032abcd 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci.macro INTERRUPT_PROLOGUE LVL 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci /* free up r9 as scratchpad */ 23062306a36Sopenharmony_ci PROLOG_FREEUP_REG r9, @int\LVL\()_saved_reg 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci /* Which mode (user/kernel) was the system in when intr occurred */ 23362306a36Sopenharmony_ci lr r9, [status32_l\LVL\()] 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci SWITCH_TO_KERNEL_STK 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci PUSH 0x003\LVL\()abcd /* Dummy ECR */ 23962306a36Sopenharmony_ci sub sp, sp, 8 /* skip orig_r0 (not needed) 24062306a36Sopenharmony_ci skip pt_regs->sp, already saved above */ 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci /* Restore r9 used to code the early prologue */ 24362306a36Sopenharmony_ci PROLOG_RESTORE_REG r9, @int\LVL\()_saved_reg 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci SAVE_R0_TO_R12 24662306a36Sopenharmony_ci PUSH gp 24762306a36Sopenharmony_ci PUSH fp 24862306a36Sopenharmony_ci PUSH blink 24962306a36Sopenharmony_ci PUSH ilink\LVL\() 25062306a36Sopenharmony_ci PUSHAX status32_l\LVL\() 25162306a36Sopenharmony_ci PUSH lp_count 25262306a36Sopenharmony_ci PUSHAX lp_end 25362306a36Sopenharmony_ci PUSHAX lp_start 25462306a36Sopenharmony_ci PUSHAX bta_l\LVL\() 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci#ifdef CONFIG_ARC_CURR_IN_REG 25762306a36Sopenharmony_ci /* gp already saved on stack: now load with "current" */ 25862306a36Sopenharmony_ci GET_CURR_TASK_ON_CPU gp 25962306a36Sopenharmony_ci#endif 26062306a36Sopenharmony_ci.endm 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci/*-------------------------------------------------------------- 26362306a36Sopenharmony_ci * Restore all registers used by interrupt handlers. 26462306a36Sopenharmony_ci * 26562306a36Sopenharmony_ci * NOTE: 26662306a36Sopenharmony_ci * 26762306a36Sopenharmony_ci * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg 26862306a36Sopenharmony_ci * for memory load operations. If used in that way interrupts are deffered 26962306a36Sopenharmony_ci * by hardware and that is not good. 27062306a36Sopenharmony_ci *-------------------------------------------------------------*/ 27162306a36Sopenharmony_ci.macro INTERRUPT_EPILOGUE LVL 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci POPAX bta_l\LVL\() 27462306a36Sopenharmony_ci POPAX lp_start 27562306a36Sopenharmony_ci POPAX lp_end 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci POP r9 27862306a36Sopenharmony_ci mov lp_count, r9 ;LD to lp_count is not allowed 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci POPAX status32_l\LVL\() 28162306a36Sopenharmony_ci POP ilink\LVL\() 28262306a36Sopenharmony_ci POP blink 28362306a36Sopenharmony_ci POP fp 28462306a36Sopenharmony_ci POP gp 28562306a36Sopenharmony_ci RESTORE_R12_TO_R0 28662306a36Sopenharmony_ci 28762306a36Sopenharmony_ci ld sp, [sp] /* restore original sp; orig_r0, ECR skipped implicitly */ 28862306a36Sopenharmony_ci.endm 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci/* Get thread_info of "current" tsk */ 29162306a36Sopenharmony_ci.macro GET_CURR_THR_INFO_FROM_SP reg 29262306a36Sopenharmony_ci bic \reg, sp, (THREAD_SIZE - 1) 29362306a36Sopenharmony_ci.endm 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci/* Get CPU-ID of this core */ 29662306a36Sopenharmony_ci.macro GET_CPU_ID reg 29762306a36Sopenharmony_ci lr \reg, [identity] 29862306a36Sopenharmony_ci lsr \reg, \reg, 8 29962306a36Sopenharmony_ci bmsk \reg, \reg, 7 30062306a36Sopenharmony_ci.endm 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci#endif /* __ASM_ARC_ENTRY_COMPACT_H */ 303