19d42c84fSVineet Gupta /* 29d42c84fSVineet Gupta * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 39d42c84fSVineet Gupta * 49d42c84fSVineet Gupta * This program is free software; you can redistribute it and/or modify 59d42c84fSVineet Gupta * it under the terms of the GNU General Public License version 2 as 69d42c84fSVineet Gupta * published by the Free Software Foundation. 79d42c84fSVineet Gupta * 8*4788a594SVineet Gupta * Vineetg: March 2009 (Supporting 2 levels of Interrupts) 9*4788a594SVineet Gupta * Stack switching code can no longer reliably rely on the fact that 10*4788a594SVineet Gupta * if we are NOT in user mode, stack is switched to kernel mode. 11*4788a594SVineet Gupta * e.g. L2 IRQ interrupted a L1 ISR which had not yet completed 12*4788a594SVineet Gupta * it's prologue including stack switching from user mode 13*4788a594SVineet Gupta * 149d42c84fSVineet Gupta * Vineetg: Aug 28th 2008: Bug #94984 159d42c84fSVineet Gupta * -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap 169d42c84fSVineet Gupta * Normally CPU does this automatically, however when doing FAKE rtie, 179d42c84fSVineet Gupta * we also need to explicitly do this. The problem in macros 189d42c84fSVineet Gupta * FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit 199d42c84fSVineet Gupta * was being "CLEARED" rather then "SET". Actually "SET" clears ZOL context 209d42c84fSVineet Gupta * 219d42c84fSVineet Gupta * Vineetg: May 5th 2008 22080c3747SVineet Gupta * -Modified CALLEE_REG save/restore macros to handle the fact that 23080c3747SVineet Gupta * r25 contains the kernel current task ptr 249d42c84fSVineet Gupta * - Defined Stack Switching Macro to be reused in all intr/excp hdlrs 259d42c84fSVineet Gupta * - Shaved off 11 instructions from RESTORE_ALL_INT1 by using the 269d42c84fSVineet Gupta * address Write back load ld.ab instead of seperate ld/add instn 279d42c84fSVineet Gupta * 289d42c84fSVineet Gupta * Amit Bhor, Sameer Dhavale: Codito Technologies 2004 299d42c84fSVineet Gupta */ 309d42c84fSVineet Gupta 319d42c84fSVineet Gupta #ifndef __ASM_ARC_ENTRY_H 329d42c84fSVineet Gupta #define __ASM_ARC_ENTRY_H 339d42c84fSVineet Gupta 349d42c84fSVineet Gupta #ifdef __ASSEMBLY__ 359d42c84fSVineet Gupta #include <asm/unistd.h> /* For NR_syscalls defination */ 369d42c84fSVineet Gupta #include <asm/asm-offsets.h> 379d42c84fSVineet Gupta #include <asm/arcregs.h> 389d42c84fSVineet Gupta #include <asm/ptrace.h> 39080c3747SVineet Gupta #include <asm/processor.h> /* For VMALLOC_START */ 409d42c84fSVineet Gupta #include <asm/thread_info.h> /* For THREAD_SIZE */ 419d42c84fSVineet Gupta 429d42c84fSVineet Gupta /* Note on the LD/ST addr modes with addr reg wback 439d42c84fSVineet Gupta * 449d42c84fSVineet Gupta * LD.a same as LD.aw 459d42c84fSVineet Gupta * 469d42c84fSVineet Gupta * LD.a reg1, [reg2, x] => Pre Incr 479d42c84fSVineet Gupta * Eff Addr for load = [reg2 + x] 489d42c84fSVineet Gupta * 499d42c84fSVineet Gupta * LD.ab reg1, [reg2, x] => Post Incr 509d42c84fSVineet Gupta * Eff Addr for load = [reg2] 519d42c84fSVineet Gupta */ 529d42c84fSVineet Gupta 539d42c84fSVineet Gupta /*-------------------------------------------------------------- 549d42c84fSVineet Gupta * Save caller saved registers (scratch registers) ( r0 - r12 ) 559d42c84fSVineet Gupta * Registers are pushed / popped in the order defined in struct ptregs 569d42c84fSVineet Gupta * in asm/ptrace.h 579d42c84fSVineet Gupta *-------------------------------------------------------------*/ 589d42c84fSVineet Gupta .macro SAVE_CALLER_SAVED 599d42c84fSVineet Gupta st.a r0, [sp, -4] 609d42c84fSVineet Gupta st.a r1, [sp, -4] 619d42c84fSVineet Gupta st.a r2, [sp, -4] 629d42c84fSVineet Gupta st.a r3, [sp, -4] 639d42c84fSVineet Gupta st.a r4, [sp, -4] 649d42c84fSVineet Gupta st.a r5, [sp, -4] 659d42c84fSVineet Gupta st.a r6, [sp, -4] 669d42c84fSVineet Gupta st.a r7, [sp, -4] 679d42c84fSVineet Gupta st.a r8, [sp, -4] 689d42c84fSVineet Gupta st.a r9, [sp, -4] 699d42c84fSVineet Gupta st.a r10, [sp, -4] 709d42c84fSVineet Gupta st.a r11, [sp, -4] 719d42c84fSVineet Gupta st.a r12, [sp, -4] 729d42c84fSVineet Gupta .endm 739d42c84fSVineet Gupta 749d42c84fSVineet Gupta /*-------------------------------------------------------------- 759d42c84fSVineet Gupta * Restore caller saved registers (scratch registers) 769d42c84fSVineet Gupta *-------------------------------------------------------------*/ 779d42c84fSVineet Gupta .macro RESTORE_CALLER_SAVED 789d42c84fSVineet Gupta ld.ab r12, [sp, 4] 799d42c84fSVineet Gupta ld.ab r11, [sp, 4] 809d42c84fSVineet Gupta ld.ab r10, [sp, 4] 819d42c84fSVineet Gupta ld.ab r9, [sp, 4] 829d42c84fSVineet Gupta ld.ab r8, [sp, 4] 839d42c84fSVineet Gupta ld.ab r7, [sp, 4] 849d42c84fSVineet Gupta ld.ab r6, [sp, 4] 859d42c84fSVineet Gupta ld.ab r5, [sp, 4] 869d42c84fSVineet Gupta ld.ab r4, [sp, 4] 879d42c84fSVineet Gupta ld.ab r3, [sp, 4] 889d42c84fSVineet Gupta ld.ab r2, [sp, 4] 899d42c84fSVineet Gupta ld.ab r1, [sp, 4] 909d42c84fSVineet Gupta ld.ab r0, [sp, 4] 919d42c84fSVineet Gupta .endm 929d42c84fSVineet Gupta 939d42c84fSVineet Gupta 949d42c84fSVineet Gupta /*-------------------------------------------------------------- 959d42c84fSVineet Gupta * Save callee saved registers (non scratch registers) ( r13 - r25 ) 969d42c84fSVineet Gupta * on kernel stack. 979d42c84fSVineet Gupta * User mode callee regs need to be saved in case of 989d42c84fSVineet Gupta * -fork and friends for replicating from parent to child 999d42c84fSVineet Gupta * -before going into do_signal( ) for ptrace/core-dump 1009d42c84fSVineet Gupta * Special case handling is required for r25 in case it is used by kernel 1019d42c84fSVineet Gupta * for caching task ptr. Low level exception/ISR save user mode r25 1029d42c84fSVineet Gupta * into task->thread.user_r25. So it needs to be retrieved from there and 1039d42c84fSVineet Gupta * saved into kernel stack with rest of callee reg-file 1049d42c84fSVineet Gupta *-------------------------------------------------------------*/ 1059d42c84fSVineet Gupta .macro SAVE_CALLEE_SAVED_USER 1069d42c84fSVineet Gupta st.a r13, [sp, -4] 1079d42c84fSVineet Gupta st.a r14, [sp, -4] 1089d42c84fSVineet Gupta st.a r15, [sp, -4] 1099d42c84fSVineet Gupta st.a r16, [sp, -4] 1109d42c84fSVineet Gupta st.a r17, [sp, -4] 1119d42c84fSVineet Gupta st.a r18, [sp, -4] 1129d42c84fSVineet Gupta st.a r19, [sp, -4] 1139d42c84fSVineet Gupta st.a r20, [sp, -4] 1149d42c84fSVineet Gupta st.a r21, [sp, -4] 1159d42c84fSVineet Gupta st.a r22, [sp, -4] 1169d42c84fSVineet Gupta st.a r23, [sp, -4] 1179d42c84fSVineet Gupta st.a r24, [sp, -4] 118080c3747SVineet Gupta 119080c3747SVineet Gupta #ifdef CONFIG_ARC_CURR_IN_REG 120080c3747SVineet Gupta ; Retrieve orig r25 and save it on stack 121080c3747SVineet Gupta ld r12, [r25, TASK_THREAD + THREAD_USER_R25] 122080c3747SVineet Gupta st.a r12, [sp, -4] 123080c3747SVineet Gupta #else 1249d42c84fSVineet Gupta st.a r25, [sp, -4] 125080c3747SVineet Gupta #endif 1269d42c84fSVineet Gupta 1279d42c84fSVineet Gupta /* move up by 1 word to "create" callee_regs->"stack_place_holder" */ 1289d42c84fSVineet Gupta sub sp, sp, 4 1299d42c84fSVineet Gupta .endm 1309d42c84fSVineet Gupta 1319d42c84fSVineet Gupta /*-------------------------------------------------------------- 1329d42c84fSVineet Gupta * Save callee saved registers (non scratch registers) ( r13 - r25 ) 1339d42c84fSVineet Gupta * kernel mode callee regs needed to be saved in case of context switch 1349d42c84fSVineet Gupta * If r25 is used for caching task pointer then that need not be saved 1359d42c84fSVineet Gupta * as it can be re-created from current task global 1369d42c84fSVineet Gupta *-------------------------------------------------------------*/ 1379d42c84fSVineet Gupta .macro SAVE_CALLEE_SAVED_KERNEL 1389d42c84fSVineet Gupta st.a r13, [sp, -4] 1399d42c84fSVineet Gupta st.a r14, [sp, -4] 1409d42c84fSVineet Gupta st.a r15, [sp, -4] 1419d42c84fSVineet Gupta st.a r16, [sp, -4] 1429d42c84fSVineet Gupta st.a r17, [sp, -4] 1439d42c84fSVineet Gupta st.a r18, [sp, -4] 1449d42c84fSVineet Gupta st.a r19, [sp, -4] 1459d42c84fSVineet Gupta st.a r20, [sp, -4] 1469d42c84fSVineet Gupta st.a r21, [sp, -4] 1479d42c84fSVineet Gupta st.a r22, [sp, -4] 1489d42c84fSVineet Gupta st.a r23, [sp, -4] 1499d42c84fSVineet Gupta st.a r24, [sp, -4] 150080c3747SVineet Gupta #ifdef CONFIG_ARC_CURR_IN_REG 151080c3747SVineet Gupta sub sp, sp, 8 152080c3747SVineet Gupta #else 1539d42c84fSVineet Gupta st.a r25, [sp, -4] 1549d42c84fSVineet Gupta sub sp, sp, 4 155080c3747SVineet Gupta #endif 1569d42c84fSVineet Gupta .endm 1579d42c84fSVineet Gupta 1589d42c84fSVineet Gupta /*-------------------------------------------------------------- 1599d42c84fSVineet Gupta * RESTORE_CALLEE_SAVED_KERNEL: 1609d42c84fSVineet Gupta * Loads callee (non scratch) Reg File by popping from Kernel mode stack. 1619d42c84fSVineet Gupta * This is reverse of SAVE_CALLEE_SAVED, 1629d42c84fSVineet Gupta * 1639d42c84fSVineet Gupta * NOTE: 1649d42c84fSVineet Gupta * Ideally this shd only be called in switch_to for loading 1659d42c84fSVineet Gupta * switched-IN task's CALLEE Reg File. 1669d42c84fSVineet Gupta * For all other cases RESTORE_CALLEE_SAVED_FAST must be used 1679d42c84fSVineet Gupta * which simply pops the stack w/o touching regs. 1689d42c84fSVineet Gupta *-------------------------------------------------------------*/ 1699d42c84fSVineet Gupta .macro RESTORE_CALLEE_SAVED_KERNEL 1709d42c84fSVineet Gupta 171080c3747SVineet Gupta 172080c3747SVineet Gupta #ifdef CONFIG_ARC_CURR_IN_REG 173080c3747SVineet Gupta add sp, sp, 8 /* skip callee_reg gutter and user r25 placeholder */ 174080c3747SVineet Gupta #else 1759d42c84fSVineet Gupta add sp, sp, 4 /* skip "callee_regs->stack_place_holder" */ 1769d42c84fSVineet Gupta ld.ab r25, [sp, 4] 177080c3747SVineet Gupta #endif 178080c3747SVineet Gupta 1799d42c84fSVineet Gupta ld.ab r24, [sp, 4] 1809d42c84fSVineet Gupta ld.ab r23, [sp, 4] 1819d42c84fSVineet Gupta ld.ab r22, [sp, 4] 1829d42c84fSVineet Gupta ld.ab r21, [sp, 4] 1839d42c84fSVineet Gupta ld.ab r20, [sp, 4] 1849d42c84fSVineet Gupta ld.ab r19, [sp, 4] 1859d42c84fSVineet Gupta ld.ab r18, [sp, 4] 1869d42c84fSVineet Gupta ld.ab r17, [sp, 4] 1879d42c84fSVineet Gupta ld.ab r16, [sp, 4] 1889d42c84fSVineet Gupta ld.ab r15, [sp, 4] 1899d42c84fSVineet Gupta ld.ab r14, [sp, 4] 1909d42c84fSVineet Gupta ld.ab r13, [sp, 4] 1919d42c84fSVineet Gupta 1929d42c84fSVineet Gupta .endm 1939d42c84fSVineet Gupta 1949d42c84fSVineet Gupta /*-------------------------------------------------------------- 195c3581039SVineet Gupta * RESTORE_CALLEE_SAVED_USER: 196c3581039SVineet Gupta * This is called after do_signal where tracer might have changed callee regs 197c3581039SVineet Gupta * thus we need to restore the reg file. 198c3581039SVineet Gupta * Special case handling is required for r25 in case it is used by kernel 199c3581039SVineet Gupta * for caching task ptr. Ptrace would have modified on-kernel-stack value of 200c3581039SVineet Gupta * r25, which needs to be shoved back into task->thread.user_r25 where from 201c3581039SVineet Gupta * Low level exception/ISR return code will retrieve to populate with rest of 202c3581039SVineet Gupta * callee reg-file. 203c3581039SVineet Gupta *-------------------------------------------------------------*/ 204c3581039SVineet Gupta .macro RESTORE_CALLEE_SAVED_USER 205c3581039SVineet Gupta 206c3581039SVineet Gupta add sp, sp, 4 /* skip "callee_regs->stack_place_holder" */ 207c3581039SVineet Gupta 208c3581039SVineet Gupta #ifdef CONFIG_ARC_CURR_IN_REG 209c3581039SVineet Gupta ld.ab r12, [sp, 4] 210c3581039SVineet Gupta st r12, [r25, TASK_THREAD + THREAD_USER_R25] 211c3581039SVineet Gupta #else 212c3581039SVineet Gupta ld.ab r25, [sp, 4] 213c3581039SVineet Gupta #endif 214c3581039SVineet Gupta 215c3581039SVineet Gupta ld.ab r24, [sp, 4] 216c3581039SVineet Gupta ld.ab r23, [sp, 4] 217c3581039SVineet Gupta ld.ab r22, [sp, 4] 218c3581039SVineet Gupta ld.ab r21, [sp, 4] 219c3581039SVineet Gupta ld.ab r20, [sp, 4] 220c3581039SVineet Gupta ld.ab r19, [sp, 4] 221c3581039SVineet Gupta ld.ab r18, [sp, 4] 222c3581039SVineet Gupta ld.ab r17, [sp, 4] 223c3581039SVineet Gupta ld.ab r16, [sp, 4] 224c3581039SVineet Gupta ld.ab r15, [sp, 4] 225c3581039SVineet Gupta ld.ab r14, [sp, 4] 226c3581039SVineet Gupta ld.ab r13, [sp, 4] 227c3581039SVineet Gupta .endm 228c3581039SVineet Gupta 229c3581039SVineet Gupta /*-------------------------------------------------------------- 2309d42c84fSVineet Gupta * Super FAST Restore callee saved regs by simply re-adjusting SP 2319d42c84fSVineet Gupta *-------------------------------------------------------------*/ 2329d42c84fSVineet Gupta .macro DISCARD_CALLEE_SAVED_USER 2339d42c84fSVineet Gupta add sp, sp, 14 * 4 2349d42c84fSVineet Gupta .endm 2359d42c84fSVineet Gupta 2369d42c84fSVineet Gupta /*-------------------------------------------------------------- 2379d42c84fSVineet Gupta * Restore User mode r25 saved in task_struct->thread.user_r25 2389d42c84fSVineet Gupta *-------------------------------------------------------------*/ 2399d42c84fSVineet Gupta .macro RESTORE_USER_R25 2409d42c84fSVineet Gupta ld r25, [r25, TASK_THREAD + THREAD_USER_R25] 2419d42c84fSVineet Gupta .endm 2429d42c84fSVineet Gupta 2439d42c84fSVineet Gupta /*------------------------------------------------------------- 2449d42c84fSVineet Gupta * given a tsk struct, get to the base of it's kernel mode stack 2459d42c84fSVineet Gupta * tsk->thread_info is really a PAGE, whose bottom hoists stack 2469d42c84fSVineet Gupta * which grows upwards towards thread_info 2479d42c84fSVineet Gupta *------------------------------------------------------------*/ 2489d42c84fSVineet Gupta 2499d42c84fSVineet Gupta .macro GET_TSK_STACK_BASE tsk, out 2509d42c84fSVineet Gupta 2519d42c84fSVineet Gupta /* Get task->thread_info (this is essentially start of a PAGE) */ 2529d42c84fSVineet Gupta ld \out, [\tsk, TASK_THREAD_INFO] 2539d42c84fSVineet Gupta 2549d42c84fSVineet Gupta /* Go to end of page where stack begins (grows upwards) */ 2559d42c84fSVineet Gupta add2 \out, \out, (THREAD_SIZE - 4)/4 /* one word GUTTER */ 2569d42c84fSVineet Gupta 2579d42c84fSVineet Gupta .endm 2589d42c84fSVineet Gupta 2599d42c84fSVineet Gupta /*-------------------------------------------------------------- 2609d42c84fSVineet Gupta * Switch to Kernel Mode stack if SP points to User Mode stack 2619d42c84fSVineet Gupta * 2629d42c84fSVineet Gupta * Entry : r9 contains pre-IRQ/exception/trap status32 2639d42c84fSVineet Gupta * Exit : SP is set to kernel mode stack pointer 264080c3747SVineet Gupta * If CURR_IN_REG, r25 set to "current" task pointer 2659d42c84fSVineet Gupta * Clobbers: r9 2669d42c84fSVineet Gupta *-------------------------------------------------------------*/ 2679d42c84fSVineet Gupta 2689d42c84fSVineet Gupta .macro SWITCH_TO_KERNEL_STK 2699d42c84fSVineet Gupta 2709d42c84fSVineet Gupta /* User Mode when this happened ? Yes: Proceed to switch stack */ 2719d42c84fSVineet Gupta bbit1 r9, STATUS_U_BIT, 88f 2729d42c84fSVineet Gupta 2739d42c84fSVineet Gupta /* OK we were already in kernel mode when this event happened, thus can 2749d42c84fSVineet Gupta * assume SP is kernel mode SP. _NO_ need to do any stack switching 2759d42c84fSVineet Gupta */ 2769d42c84fSVineet Gupta 277*4788a594SVineet Gupta #ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS 278*4788a594SVineet Gupta /* However.... 279*4788a594SVineet Gupta * If Level 2 Interrupts enabled, we may end up with a corner case: 280*4788a594SVineet Gupta * 1. User Task executing 281*4788a594SVineet Gupta * 2. L1 IRQ taken, ISR starts (CPU auto-switched to KERNEL mode) 282*4788a594SVineet Gupta * 3. But before it could switch SP from USER to KERNEL stack 283*4788a594SVineet Gupta * a L2 IRQ "Interrupts" L1 284*4788a594SVineet Gupta * Thay way although L2 IRQ happened in Kernel mode, stack is still 285*4788a594SVineet Gupta * not switched. 286*4788a594SVineet Gupta * To handle this, we may need to switch stack even if in kernel mode 287*4788a594SVineet Gupta * provided SP has values in range of USER mode stack ( < 0x7000_0000 ) 288*4788a594SVineet Gupta */ 289*4788a594SVineet Gupta brlo sp, VMALLOC_START, 88f 290*4788a594SVineet Gupta 291*4788a594SVineet Gupta /* TODO: vineetg: 292*4788a594SVineet Gupta * We need to be a bit more cautious here. What if a kernel bug in 293*4788a594SVineet Gupta * L1 ISR, caused SP to go whaco (some small value which looks like 294*4788a594SVineet Gupta * USER stk) and then we take L2 ISR. 295*4788a594SVineet Gupta * Above brlo alone would treat it as a valid L1-L2 sceanrio 296*4788a594SVineet Gupta * instead of shouting alound 297*4788a594SVineet Gupta * The only feasible way is to make sure this L2 happened in 298*4788a594SVineet Gupta * L1 prelogue ONLY i.e. ilink2 is less than a pre-set marker in 299*4788a594SVineet Gupta * L1 ISR before it switches stack 300*4788a594SVineet Gupta */ 301*4788a594SVineet Gupta 302*4788a594SVineet Gupta #endif 303*4788a594SVineet Gupta 3049d42c84fSVineet Gupta /* Save Pre Intr/Exception KERNEL MODE SP on kernel stack 3059d42c84fSVineet Gupta * safe-keeping not really needed, but it keeps the epilogue code 3069d42c84fSVineet Gupta * (SP restore) simpler/uniform. 3079d42c84fSVineet Gupta */ 3089d42c84fSVineet Gupta b.d 77f 3099d42c84fSVineet Gupta 3109d42c84fSVineet Gupta st.a sp, [sp, -12] ; Make room for orig_r0 and orig_r8 3119d42c84fSVineet Gupta 3129d42c84fSVineet Gupta 88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */ 3139d42c84fSVineet Gupta 3149d42c84fSVineet Gupta GET_CURR_TASK_ON_CPU r9 3159d42c84fSVineet Gupta 316080c3747SVineet Gupta #ifdef CONFIG_ARC_CURR_IN_REG 317080c3747SVineet Gupta 318080c3747SVineet Gupta /* If current task pointer cached in r25, time to 319080c3747SVineet Gupta * -safekeep USER r25 in task->thread_struct->user_r25 320080c3747SVineet Gupta * -load r25 with current task ptr 321080c3747SVineet Gupta */ 322080c3747SVineet Gupta st.as r25, [r9, (TASK_THREAD + THREAD_USER_R25)/4] 323080c3747SVineet Gupta mov r25, r9 324080c3747SVineet Gupta #endif 325080c3747SVineet Gupta 3269d42c84fSVineet Gupta /* With current tsk in r9, get it's kernel mode stack base */ 3279d42c84fSVineet Gupta GET_TSK_STACK_BASE r9, r9 3289d42c84fSVineet Gupta 3299d42c84fSVineet Gupta #ifdef PT_REGS_CANARY 3309d42c84fSVineet Gupta st 0xabcdabcd, [r9, 0] 3319d42c84fSVineet Gupta #endif 3329d42c84fSVineet Gupta 3339d42c84fSVineet Gupta /* Save Pre Intr/Exception User SP on kernel stack */ 3349d42c84fSVineet Gupta st.a sp, [r9, -12] ; Make room for orig_r0 and orig_r8 3359d42c84fSVineet Gupta 3369d42c84fSVineet Gupta /* CAUTION: 3379d42c84fSVineet Gupta * SP should be set at the very end when we are done with everything 3389d42c84fSVineet Gupta * In case of 2 levels of interrupt we depend on value of SP to assume 3399d42c84fSVineet Gupta * that everything else is done (loading r25 etc) 3409d42c84fSVineet Gupta */ 3419d42c84fSVineet Gupta 3429d42c84fSVineet Gupta /* set SP to point to kernel mode stack */ 3439d42c84fSVineet Gupta mov sp, r9 3449d42c84fSVineet Gupta 3459d42c84fSVineet Gupta 77: /* ----- Stack Switched to kernel Mode, Now save REG FILE ----- */ 3469d42c84fSVineet Gupta 3479d42c84fSVineet Gupta .endm 3489d42c84fSVineet Gupta 3499d42c84fSVineet Gupta /*------------------------------------------------------------ 3509d42c84fSVineet Gupta * "FAKE" a rtie to return from CPU Exception context 3519d42c84fSVineet Gupta * This is to re-enable Exceptions within exception 3529d42c84fSVineet Gupta * Look at EV_ProtV to see how this is actually used 3539d42c84fSVineet Gupta *-------------------------------------------------------------*/ 3549d42c84fSVineet Gupta 3559d42c84fSVineet Gupta .macro FAKE_RET_FROM_EXCPN reg 3569d42c84fSVineet Gupta 3579d42c84fSVineet Gupta ld \reg, [sp, PT_status32] 3589d42c84fSVineet Gupta bic \reg, \reg, (STATUS_U_MASK|STATUS_DE_MASK) 3599d42c84fSVineet Gupta bset \reg, \reg, STATUS_L_BIT 3609d42c84fSVineet Gupta sr \reg, [erstatus] 3619d42c84fSVineet Gupta mov \reg, 55f 3629d42c84fSVineet Gupta sr \reg, [eret] 3639d42c84fSVineet Gupta 3649d42c84fSVineet Gupta rtie 3659d42c84fSVineet Gupta 55: 3669d42c84fSVineet Gupta .endm 3679d42c84fSVineet Gupta 3689d42c84fSVineet Gupta /* 3699d42c84fSVineet Gupta * @reg [OUT] &thread_info of "current" 3709d42c84fSVineet Gupta */ 3719d42c84fSVineet Gupta .macro GET_CURR_THR_INFO_FROM_SP reg 3729d42c84fSVineet Gupta and \reg, sp, ~(THREAD_SIZE - 1) 3739d42c84fSVineet Gupta .endm 3749d42c84fSVineet Gupta 3759d42c84fSVineet Gupta /* 3769d42c84fSVineet Gupta * @reg [OUT] thread_info->flags of "current" 3779d42c84fSVineet Gupta */ 3789d42c84fSVineet Gupta .macro GET_CURR_THR_INFO_FLAGS reg 3799d42c84fSVineet Gupta GET_CURR_THR_INFO_FROM_SP \reg 3809d42c84fSVineet Gupta ld \reg, [\reg, THREAD_INFO_FLAGS] 3819d42c84fSVineet Gupta .endm 3829d42c84fSVineet Gupta 3839d42c84fSVineet Gupta /*-------------------------------------------------------------- 3849d42c84fSVineet Gupta * For early Exception Prologue, a core reg is temporarily needed to 3859d42c84fSVineet Gupta * code the rest of prolog (stack switching). This is done by stashing 3869d42c84fSVineet Gupta * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP). 3879d42c84fSVineet Gupta * 3889d42c84fSVineet Gupta * Before saving the full regfile - this reg is restored back, only 3899d42c84fSVineet Gupta * to be saved again on kernel mode stack, as part of ptregs. 3909d42c84fSVineet Gupta *-------------------------------------------------------------*/ 3919d42c84fSVineet Gupta .macro EXCPN_PROLOG_FREEUP_REG reg 3929d42c84fSVineet Gupta st \reg, [@ex_saved_reg1] 3939d42c84fSVineet Gupta .endm 3949d42c84fSVineet Gupta 3959d42c84fSVineet Gupta .macro EXCPN_PROLOG_RESTORE_REG reg 3969d42c84fSVineet Gupta ld \reg, [@ex_saved_reg1] 3979d42c84fSVineet Gupta .endm 3989d42c84fSVineet Gupta 3999d42c84fSVineet Gupta /*-------------------------------------------------------------- 4009d42c84fSVineet Gupta * Save all registers used by Exceptions (TLB Miss, Prot-V, Mem err etc) 4019d42c84fSVineet Gupta * Requires SP to be already switched to kernel mode Stack 4029d42c84fSVineet Gupta * sp points to the next free element on the stack at exit of this macro. 4039d42c84fSVineet Gupta * Registers are pushed / popped in the order defined in struct ptregs 4049d42c84fSVineet Gupta * in asm/ptrace.h 4059d42c84fSVineet Gupta * Note that syscalls are implemented via TRAP which is also a exception 4069d42c84fSVineet Gupta * from CPU's point of view 4079d42c84fSVineet Gupta *-------------------------------------------------------------*/ 4089d42c84fSVineet Gupta .macro SAVE_ALL_EXCEPTION marker 4099d42c84fSVineet Gupta 4105c39c0abSVineet Gupta st \marker, [sp, 8] 4115c39c0abSVineet Gupta st r0, [sp, 4] /* orig_r0, needed only for sys calls */ 4125c39c0abSVineet Gupta 4139d42c84fSVineet Gupta /* Restore r9 used to code the early prologue */ 4149d42c84fSVineet Gupta EXCPN_PROLOG_RESTORE_REG r9 4159d42c84fSVineet Gupta 4169d42c84fSVineet Gupta SAVE_CALLER_SAVED 4179d42c84fSVineet Gupta st.a r26, [sp, -4] /* gp */ 4189d42c84fSVineet Gupta st.a fp, [sp, -4] 4199d42c84fSVineet Gupta st.a blink, [sp, -4] 4209d42c84fSVineet Gupta lr r9, [eret] 4219d42c84fSVineet Gupta st.a r9, [sp, -4] 4229d42c84fSVineet Gupta lr r9, [erstatus] 4239d42c84fSVineet Gupta st.a r9, [sp, -4] 4249d42c84fSVineet Gupta st.a lp_count, [sp, -4] 4259d42c84fSVineet Gupta lr r9, [lp_end] 4269d42c84fSVineet Gupta st.a r9, [sp, -4] 4279d42c84fSVineet Gupta lr r9, [lp_start] 4289d42c84fSVineet Gupta st.a r9, [sp, -4] 4299d42c84fSVineet Gupta lr r9, [erbta] 4309d42c84fSVineet Gupta st.a r9, [sp, -4] 4319d42c84fSVineet Gupta 4329d42c84fSVineet Gupta #ifdef PT_REGS_CANARY 4339d42c84fSVineet Gupta mov r9, 0xdeadbeef 4349d42c84fSVineet Gupta st r9, [sp, -4] 4359d42c84fSVineet Gupta #endif 4369d42c84fSVineet Gupta 4379d42c84fSVineet Gupta /* move up by 1 word to "create" pt_regs->"stack_place_holder" */ 4389d42c84fSVineet Gupta sub sp, sp, 4 4399d42c84fSVineet Gupta .endm 4409d42c84fSVineet Gupta 4419d42c84fSVineet Gupta /*-------------------------------------------------------------- 4429d42c84fSVineet Gupta * Save scratch regs for exceptions 4439d42c84fSVineet Gupta *-------------------------------------------------------------*/ 4449d42c84fSVineet Gupta .macro SAVE_ALL_SYS 4455c39c0abSVineet Gupta SAVE_ALL_EXCEPTION orig_r8_IS_EXCPN 4469d42c84fSVineet Gupta .endm 4479d42c84fSVineet Gupta 4489d42c84fSVineet Gupta /*-------------------------------------------------------------- 4499d42c84fSVineet Gupta * Save scratch regs for sys calls 4509d42c84fSVineet Gupta *-------------------------------------------------------------*/ 4519d42c84fSVineet Gupta .macro SAVE_ALL_TRAP 4525c39c0abSVineet Gupta /* 4535c39c0abSVineet Gupta * Setup pt_regs->orig_r8. 4545c39c0abSVineet Gupta * Encode syscall number (r8) in upper short word of event type (r9) 4555c39c0abSVineet Gupta * N.B. #1: This is already endian safe (see ptrace.h) 4565c39c0abSVineet Gupta * #2: Only r9 can be used as scratch as it is already clobbered 4575c39c0abSVineet Gupta * and it's contents are no longer needed by the latter part 4585c39c0abSVineet Gupta * of exception prologue 4595c39c0abSVineet Gupta */ 4605c39c0abSVineet Gupta lsl r9, r8, 16 4615c39c0abSVineet Gupta or r9, r9, orig_r8_IS_SCALL 4625c39c0abSVineet Gupta 4635c39c0abSVineet Gupta SAVE_ALL_EXCEPTION r9 4649d42c84fSVineet Gupta .endm 4659d42c84fSVineet Gupta 4669d42c84fSVineet Gupta /*-------------------------------------------------------------- 4679d42c84fSVineet Gupta * Restore all registers used by system call or Exceptions 4689d42c84fSVineet Gupta * SP should always be pointing to the next free stack element 4699d42c84fSVineet Gupta * when entering this macro. 4709d42c84fSVineet Gupta * 4719d42c84fSVineet Gupta * NOTE: 4729d42c84fSVineet Gupta * 4739d42c84fSVineet Gupta * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg 4749d42c84fSVineet Gupta * for memory load operations. If used in that way interrupts are deffered 4759d42c84fSVineet Gupta * by hardware and that is not good. 4769d42c84fSVineet Gupta *-------------------------------------------------------------*/ 4779d42c84fSVineet Gupta .macro RESTORE_ALL_SYS 4789d42c84fSVineet Gupta 4799d42c84fSVineet Gupta add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */ 4809d42c84fSVineet Gupta 4819d42c84fSVineet Gupta ld.ab r9, [sp, 4] 4829d42c84fSVineet Gupta sr r9, [erbta] 4839d42c84fSVineet Gupta ld.ab r9, [sp, 4] 4849d42c84fSVineet Gupta sr r9, [lp_start] 4859d42c84fSVineet Gupta ld.ab r9, [sp, 4] 4869d42c84fSVineet Gupta sr r9, [lp_end] 4879d42c84fSVineet Gupta ld.ab r9, [sp, 4] 4889d42c84fSVineet Gupta mov lp_count, r9 4899d42c84fSVineet Gupta ld.ab r9, [sp, 4] 4909d42c84fSVineet Gupta sr r9, [erstatus] 4919d42c84fSVineet Gupta ld.ab r9, [sp, 4] 4929d42c84fSVineet Gupta sr r9, [eret] 4939d42c84fSVineet Gupta ld.ab blink, [sp, 4] 4949d42c84fSVineet Gupta ld.ab fp, [sp, 4] 4959d42c84fSVineet Gupta ld.ab r26, [sp, 4] /* gp */ 4969d42c84fSVineet Gupta RESTORE_CALLER_SAVED 4979d42c84fSVineet Gupta 4989d42c84fSVineet Gupta ld sp, [sp] /* restore original sp */ 4999d42c84fSVineet Gupta /* orig_r0 and orig_r8 skipped automatically */ 5009d42c84fSVineet Gupta .endm 5019d42c84fSVineet Gupta 5029d42c84fSVineet Gupta 5039d42c84fSVineet Gupta /*-------------------------------------------------------------- 5049d42c84fSVineet Gupta * Save all registers used by interrupt handlers. 5059d42c84fSVineet Gupta *-------------------------------------------------------------*/ 5069d42c84fSVineet Gupta .macro SAVE_ALL_INT1 5079d42c84fSVineet Gupta 5089d42c84fSVineet Gupta /* restore original r9 , saved in int1_saved_reg 5099d42c84fSVineet Gupta * It will be saved on stack in macro: SAVE_CALLER_SAVED 5109d42c84fSVineet Gupta */ 5119d42c84fSVineet Gupta ld r9, [@int1_saved_reg] 5129d42c84fSVineet Gupta 5139d42c84fSVineet Gupta /* now we are ready to save the remaining context :) */ 5145c39c0abSVineet Gupta st orig_r8_IS_IRQ1, [sp, 8] /* Event Type */ 5159d42c84fSVineet Gupta st 0, [sp, 4] /* orig_r0 , N/A for IRQ */ 5169d42c84fSVineet Gupta SAVE_CALLER_SAVED 5179d42c84fSVineet Gupta st.a r26, [sp, -4] /* gp */ 5189d42c84fSVineet Gupta st.a fp, [sp, -4] 5199d42c84fSVineet Gupta st.a blink, [sp, -4] 5209d42c84fSVineet Gupta st.a ilink1, [sp, -4] 5219d42c84fSVineet Gupta lr r9, [status32_l1] 5229d42c84fSVineet Gupta st.a r9, [sp, -4] 5239d42c84fSVineet Gupta st.a lp_count, [sp, -4] 5249d42c84fSVineet Gupta lr r9, [lp_end] 5259d42c84fSVineet Gupta st.a r9, [sp, -4] 5269d42c84fSVineet Gupta lr r9, [lp_start] 5279d42c84fSVineet Gupta st.a r9, [sp, -4] 5289d42c84fSVineet Gupta lr r9, [bta_l1] 5299d42c84fSVineet Gupta st.a r9, [sp, -4] 5309d42c84fSVineet Gupta 5319d42c84fSVineet Gupta #ifdef PT_REGS_CANARY 5329d42c84fSVineet Gupta mov r9, 0xdeadbee1 5339d42c84fSVineet Gupta st r9, [sp, -4] 5349d42c84fSVineet Gupta #endif 5359d42c84fSVineet Gupta /* move up by 1 word to "create" pt_regs->"stack_place_holder" */ 5369d42c84fSVineet Gupta sub sp, sp, 4 5379d42c84fSVineet Gupta .endm 5389d42c84fSVineet Gupta 539*4788a594SVineet Gupta .macro SAVE_ALL_INT2 540*4788a594SVineet Gupta 541*4788a594SVineet Gupta /* TODO-vineetg: SMP we can't use global nor can we use 542*4788a594SVineet Gupta * SCRATCH0 as we do for int1 because while int1 is using 543*4788a594SVineet Gupta * it, int2 can come 544*4788a594SVineet Gupta */ 545*4788a594SVineet Gupta /* retsore original r9 , saved in sys_saved_r9 */ 546*4788a594SVineet Gupta ld r9, [@int2_saved_reg] 547*4788a594SVineet Gupta 548*4788a594SVineet Gupta /* now we are ready to save the remaining context :) */ 549*4788a594SVineet Gupta st orig_r8_IS_IRQ2, [sp, 8] /* Event Type */ 550*4788a594SVineet Gupta st 0, [sp, 4] /* orig_r0 , N/A for IRQ */ 551*4788a594SVineet Gupta SAVE_CALLER_SAVED 552*4788a594SVineet Gupta st.a r26, [sp, -4] /* gp */ 553*4788a594SVineet Gupta st.a fp, [sp, -4] 554*4788a594SVineet Gupta st.a blink, [sp, -4] 555*4788a594SVineet Gupta st.a ilink2, [sp, -4] 556*4788a594SVineet Gupta lr r9, [status32_l2] 557*4788a594SVineet Gupta st.a r9, [sp, -4] 558*4788a594SVineet Gupta st.a lp_count, [sp, -4] 559*4788a594SVineet Gupta lr r9, [lp_end] 560*4788a594SVineet Gupta st.a r9, [sp, -4] 561*4788a594SVineet Gupta lr r9, [lp_start] 562*4788a594SVineet Gupta st.a r9, [sp, -4] 563*4788a594SVineet Gupta lr r9, [bta_l2] 564*4788a594SVineet Gupta st.a r9, [sp, -4] 565*4788a594SVineet Gupta 566*4788a594SVineet Gupta #ifdef PT_REGS_CANARY 567*4788a594SVineet Gupta mov r9, 0xdeadbee2 568*4788a594SVineet Gupta st r9, [sp, -4] 569*4788a594SVineet Gupta #endif 570*4788a594SVineet Gupta 571*4788a594SVineet Gupta /* move up by 1 word to "create" pt_regs->"stack_place_holder" */ 572*4788a594SVineet Gupta sub sp, sp, 4 573*4788a594SVineet Gupta .endm 574*4788a594SVineet Gupta 5759d42c84fSVineet Gupta /*-------------------------------------------------------------- 5769d42c84fSVineet Gupta * Restore all registers used by interrupt handlers. 5779d42c84fSVineet Gupta * 5789d42c84fSVineet Gupta * NOTE: 5799d42c84fSVineet Gupta * 5809d42c84fSVineet Gupta * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg 5819d42c84fSVineet Gupta * for memory load operations. If used in that way interrupts are deffered 5829d42c84fSVineet Gupta * by hardware and that is not good. 5839d42c84fSVineet Gupta *-------------------------------------------------------------*/ 5849d42c84fSVineet Gupta 5859d42c84fSVineet Gupta .macro RESTORE_ALL_INT1 5869d42c84fSVineet Gupta add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */ 5879d42c84fSVineet Gupta 5889d42c84fSVineet Gupta ld.ab r9, [sp, 4] /* Actual reg file */ 5899d42c84fSVineet Gupta sr r9, [bta_l1] 5909d42c84fSVineet Gupta ld.ab r9, [sp, 4] 5919d42c84fSVineet Gupta sr r9, [lp_start] 5929d42c84fSVineet Gupta ld.ab r9, [sp, 4] 5939d42c84fSVineet Gupta sr r9, [lp_end] 5949d42c84fSVineet Gupta ld.ab r9, [sp, 4] 5959d42c84fSVineet Gupta mov lp_count, r9 5969d42c84fSVineet Gupta ld.ab r9, [sp, 4] 5979d42c84fSVineet Gupta sr r9, [status32_l1] 5989d42c84fSVineet Gupta ld.ab r9, [sp, 4] 5999d42c84fSVineet Gupta mov ilink1, r9 6009d42c84fSVineet Gupta ld.ab blink, [sp, 4] 6019d42c84fSVineet Gupta ld.ab fp, [sp, 4] 6029d42c84fSVineet Gupta ld.ab r26, [sp, 4] /* gp */ 6039d42c84fSVineet Gupta RESTORE_CALLER_SAVED 6049d42c84fSVineet Gupta 6059d42c84fSVineet Gupta ld sp, [sp] /* restore original sp */ 6069d42c84fSVineet Gupta /* orig_r0 and orig_r8 skipped automatically */ 6079d42c84fSVineet Gupta .endm 6089d42c84fSVineet Gupta 609*4788a594SVineet Gupta .macro RESTORE_ALL_INT2 610*4788a594SVineet Gupta add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */ 611*4788a594SVineet Gupta 612*4788a594SVineet Gupta ld.ab r9, [sp, 4] 613*4788a594SVineet Gupta sr r9, [bta_l2] 614*4788a594SVineet Gupta ld.ab r9, [sp, 4] 615*4788a594SVineet Gupta sr r9, [lp_start] 616*4788a594SVineet Gupta ld.ab r9, [sp, 4] 617*4788a594SVineet Gupta sr r9, [lp_end] 618*4788a594SVineet Gupta ld.ab r9, [sp, 4] 619*4788a594SVineet Gupta mov lp_count, r9 620*4788a594SVineet Gupta ld.ab r9, [sp, 4] 621*4788a594SVineet Gupta sr r9, [status32_l2] 622*4788a594SVineet Gupta ld.ab r9, [sp, 4] 623*4788a594SVineet Gupta mov ilink2, r9 624*4788a594SVineet Gupta ld.ab blink, [sp, 4] 625*4788a594SVineet Gupta ld.ab fp, [sp, 4] 626*4788a594SVineet Gupta ld.ab r26, [sp, 4] /* gp */ 627*4788a594SVineet Gupta RESTORE_CALLER_SAVED 628*4788a594SVineet Gupta 629*4788a594SVineet Gupta ld sp, [sp] /* restore original sp */ 630*4788a594SVineet Gupta /* orig_r0 and orig_r8 skipped automatically */ 631*4788a594SVineet Gupta 632*4788a594SVineet Gupta .endm 633*4788a594SVineet Gupta 634*4788a594SVineet Gupta 6359d42c84fSVineet Gupta /* Get CPU-ID of this core */ 6369d42c84fSVineet Gupta .macro GET_CPU_ID reg 6379d42c84fSVineet Gupta lr \reg, [identity] 6389d42c84fSVineet Gupta lsr \reg, \reg, 8 6399d42c84fSVineet Gupta bmsk \reg, \reg, 7 6409d42c84fSVineet Gupta .endm 6419d42c84fSVineet Gupta 6429d42c84fSVineet Gupta .macro GET_CURR_TASK_ON_CPU reg 6439d42c84fSVineet Gupta ld \reg, [@_current_task] 6449d42c84fSVineet Gupta .endm 6459d42c84fSVineet Gupta 6469d42c84fSVineet Gupta .macro SET_CURR_TASK_ON_CPU tsk, tmp 6479d42c84fSVineet Gupta st \tsk, [@_current_task] 648080c3747SVineet Gupta #ifdef CONFIG_ARC_CURR_IN_REG 649080c3747SVineet Gupta mov r25, \tsk 650080c3747SVineet Gupta #endif 6519d42c84fSVineet Gupta .endm 6529d42c84fSVineet Gupta 6539d42c84fSVineet Gupta /* ------------------------------------------------------------------ 6549d42c84fSVineet Gupta * Get the ptr to some field of Current Task at @off in task struct 655080c3747SVineet Gupta * -Uses r25 for Current task ptr if that is enabled 6569d42c84fSVineet Gupta */ 6579d42c84fSVineet Gupta 658080c3747SVineet Gupta #ifdef CONFIG_ARC_CURR_IN_REG 659080c3747SVineet Gupta 660080c3747SVineet Gupta .macro GET_CURR_TASK_FIELD_PTR off, reg 661080c3747SVineet Gupta add \reg, r25, \off 662080c3747SVineet Gupta .endm 663080c3747SVineet Gupta 664080c3747SVineet Gupta #else 665080c3747SVineet Gupta 6669d42c84fSVineet Gupta .macro GET_CURR_TASK_FIELD_PTR off, reg 6679d42c84fSVineet Gupta GET_CURR_TASK_ON_CPU \reg 6689d42c84fSVineet Gupta add \reg, \reg, \off 6699d42c84fSVineet Gupta .endm 6709d42c84fSVineet Gupta 671080c3747SVineet Gupta #endif /* CONFIG_ARC_CURR_IN_REG */ 672080c3747SVineet Gupta 6739d42c84fSVineet Gupta #endif /* __ASSEMBLY__ */ 6749d42c84fSVineet Gupta 6759d42c84fSVineet Gupta #endif /* __ASM_ARC_ENTRY_H */ 676