2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Vineetg: March 2009 (Supporting 2 levels of Interrupts)
9 * Stack switching code can no longer reliably rely on the fact that
10 * if we are NOT in user mode, stack is switched to kernel mode.
11 * e.g. L2 IRQ interrupted a L1 ISR which had not yet completed
12 * it's prologue including stack switching from user mode
14 * Vineetg: Aug 28th 2008: Bug #94984
15 * -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap
16 * Normally CPU does this automatically, however when doing FAKE rtie,
17 * we also need to explicitly do this. The problem in macros
18 * FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit
19 * was being "CLEARED" rather then "SET". Actually "SET" clears ZOL context
21 * Vineetg: May 5th 2008
22 * -Modified CALLEE_REG save/restore macros to handle the fact that
23 * r25 contains the kernel current task ptr
24 * - Defined Stack Switching Macro to be reused in all intr/excp hdlrs
25 * - Shaved off 11 instructions from RESTORE_ALL_INT1 by using the
26 * address Write back load ld.ab instead of seperate ld/add instn
28 * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
31 #ifndef __ASM_ARC_ENTRY_H
32 #define __ASM_ARC_ENTRY_H
35 #include <asm/unistd.h> /* For NR_syscalls defination */
36 #include <asm/asm-offsets.h>
37 #include <asm/arcregs.h>
38 #include <asm/ptrace.h>
39 #include <asm/processor.h> /* For VMALLOC_START */
40 #include <asm/thread_info.h> /* For THREAD_SIZE */
42 /* Note on the LD/ST addr modes with addr reg wback
46 * LD.a reg1, [reg2, x] => Pre Incr
47 * Eff Addr for load = [reg2 + x]
49 * LD.ab reg1, [reg2, x] => Post Incr
50 * Eff Addr for load = [reg2]
53 /*--------------------------------------------------------------
54 * Save caller saved registers (scratch registers) ( r0 - r12 )
55 * Registers are pushed / popped in the order defined in struct ptregs
57 *-------------------------------------------------------------*/
58 .macro SAVE_CALLER_SAVED
74 /*--------------------------------------------------------------
75 * Restore caller saved registers (scratch registers)
76 *-------------------------------------------------------------*/
77 .macro RESTORE_CALLER_SAVED
94 /*--------------------------------------------------------------
95 * Save callee saved registers (non scratch registers) ( r13 - r25 )
97 * User mode callee regs need to be saved in case of
98 * -fork and friends for replicating from parent to child
99 * -before going into do_signal( ) for ptrace/core-dump
100 * Special case handling is required for r25 in case it is used by kernel
101 * for caching task ptr. Low level exception/ISR save user mode r25
102 * into task->thread.user_r25. So it needs to be retrieved from there and
103 * saved into kernel stack with rest of callee reg-file
104 *-------------------------------------------------------------*/
105 .macro SAVE_CALLEE_SAVED_USER
119 #ifdef CONFIG_ARC_CURR_IN_REG
120 ; Retrieve orig r25 and save it on stack
121 ld r12, [r25, TASK_THREAD + THREAD_USER_R25]
127 /* move up by 1 word to "create" callee_regs->"stack_place_holder" */
131 /*--------------------------------------------------------------
132 * Save callee saved registers (non scratch registers) ( r13 - r25 )
133 * kernel mode callee regs needed to be saved in case of context switch
134 * If r25 is used for caching task pointer then that need not be saved
135 * as it can be re-created from current task global
136 *-------------------------------------------------------------*/
137 .macro SAVE_CALLEE_SAVED_KERNEL
150 #ifdef CONFIG_ARC_CURR_IN_REG
158 /*--------------------------------------------------------------
159 * RESTORE_CALLEE_SAVED_KERNEL:
160 * Loads callee (non scratch) Reg File by popping from Kernel mode stack.
161 * This is reverse of SAVE_CALLEE_SAVED,
164 * Ideally this shd only be called in switch_to for loading
165 * switched-IN task's CALLEE Reg File.
166 * For all other cases RESTORE_CALLEE_SAVED_FAST must be used
167 * which simply pops the stack w/o touching regs.
168 *-------------------------------------------------------------*/
169 .macro RESTORE_CALLEE_SAVED_KERNEL
172 #ifdef CONFIG_ARC_CURR_IN_REG
173 add sp, sp, 8 /* skip callee_reg gutter and user r25 placeholder */
175 add sp, sp, 4 /* skip "callee_regs->stack_place_holder" */
194 /*--------------------------------------------------------------
195 * RESTORE_CALLEE_SAVED_USER:
196 * This is called after do_signal where tracer might have changed callee regs
197 * thus we need to restore the reg file.
198 * Special case handling is required for r25 in case it is used by kernel
199 * for caching task ptr. Ptrace would have modified on-kernel-stack value of
200 * r25, which needs to be shoved back into task->thread.user_r25 where from
201 * Low level exception/ISR return code will retrieve to populate with rest of
203 *-------------------------------------------------------------*/
204 .macro RESTORE_CALLEE_SAVED_USER
206 add sp, sp, 4 /* skip "callee_regs->stack_place_holder" */
208 #ifdef CONFIG_ARC_CURR_IN_REG
210 st r12, [r25, TASK_THREAD + THREAD_USER_R25]
229 /*--------------------------------------------------------------
230 * Super FAST Restore callee saved regs by simply re-adjusting SP
231 *-------------------------------------------------------------*/
232 .macro DISCARD_CALLEE_SAVED_USER
236 /*--------------------------------------------------------------
237 * Restore User mode r25 saved in task_struct->thread.user_r25
238 *-------------------------------------------------------------*/
239 .macro RESTORE_USER_R25
240 ld r25, [r25, TASK_THREAD + THREAD_USER_R25]
243 /*-------------------------------------------------------------
244 * given a tsk struct, get to the base of it's kernel mode stack
245 * tsk->thread_info is really a PAGE, whose bottom hoists stack
246 * which grows upwards towards thread_info
247 *------------------------------------------------------------*/
249 .macro GET_TSK_STACK_BASE tsk, out
251 /* Get task->thread_info (this is essentially start of a PAGE) */
252 ld \out, [\tsk, TASK_THREAD_INFO]
254 /* Go to end of page where stack begins (grows upwards) */
255 add2 \out, \out, (THREAD_SIZE - 4)/4 /* one word GUTTER */
259 /*--------------------------------------------------------------
260 * Switch to Kernel Mode stack if SP points to User Mode stack
262 * Entry : r9 contains pre-IRQ/exception/trap status32
263 * Exit : SP is set to kernel mode stack pointer
264 * If CURR_IN_REG, r25 set to "current" task pointer
266 *-------------------------------------------------------------*/
268 .macro SWITCH_TO_KERNEL_STK
270 /* User Mode when this happened ? Yes: Proceed to switch stack */
271 bbit1 r9, STATUS_U_BIT, 88f
273 /* OK we were already in kernel mode when this event happened, thus can
274 * assume SP is kernel mode SP. _NO_ need to do any stack switching
277 #ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS
279 * If Level 2 Interrupts enabled, we may end up with a corner case:
280 * 1. User Task executing
281 * 2. L1 IRQ taken, ISR starts (CPU auto-switched to KERNEL mode)
282 * 3. But before it could switch SP from USER to KERNEL stack
283 * a L2 IRQ "Interrupts" L1
284 * Thay way although L2 IRQ happened in Kernel mode, stack is still
286 * To handle this, we may need to switch stack even if in kernel mode
287 * provided SP has values in range of USER mode stack ( < 0x7000_0000 )
289 brlo sp, VMALLOC_START, 88f
292 * We need to be a bit more cautious here. What if a kernel bug in
293 * L1 ISR, caused SP to go whaco (some small value which looks like
294 * USER stk) and then we take L2 ISR.
295 * Above brlo alone would treat it as a valid L1-L2 sceanrio
296 * instead of shouting alound
297 * The only feasible way is to make sure this L2 happened in
298 * L1 prelogue ONLY i.e. ilink2 is less than a pre-set marker in
299 * L1 ISR before it switches stack
304 /* Save Pre Intr/Exception KERNEL MODE SP on kernel stack
305 * safe-keeping not really needed, but it keeps the epilogue code
306 * (SP restore) simpler/uniform.
310 st.a sp, [sp, -12] ; Make room for orig_r0 and orig_r8
312 88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */
314 GET_CURR_TASK_ON_CPU r9
316 #ifdef CONFIG_ARC_CURR_IN_REG
318 /* If current task pointer cached in r25, time to
319 * -safekeep USER r25 in task->thread_struct->user_r25
320 * -load r25 with current task ptr
322 st.as r25, [r9, (TASK_THREAD + THREAD_USER_R25)/4]
326 /* With current tsk in r9, get it's kernel mode stack base */
327 GET_TSK_STACK_BASE r9, r9
329 #ifdef PT_REGS_CANARY
330 st 0xabcdabcd, [r9, 0]
333 /* Save Pre Intr/Exception User SP on kernel stack */
334 st.a sp, [r9, -12] ; Make room for orig_r0 and orig_r8
337 * SP should be set at the very end when we are done with everything
338 * In case of 2 levels of interrupt we depend on value of SP to assume
339 * that everything else is done (loading r25 etc)
342 /* set SP to point to kernel mode stack */
345 77: /* ----- Stack Switched to kernel Mode, Now save REG FILE ----- */
349 /*------------------------------------------------------------
350 * "FAKE" a rtie to return from CPU Exception context
351 * This is to re-enable Exceptions within exception
352 * Look at EV_ProtV to see how this is actually used
353 *-------------------------------------------------------------*/
355 .macro FAKE_RET_FROM_EXCPN reg
357 ld \reg, [sp, PT_status32]
358 bic \reg, \reg, (STATUS_U_MASK|STATUS_DE_MASK)
359 bset \reg, \reg, STATUS_L_BIT
369 * @reg [OUT] &thread_info of "current"
371 .macro GET_CURR_THR_INFO_FROM_SP reg
372 and \reg, sp, ~(THREAD_SIZE - 1)
376 * @reg [OUT] thread_info->flags of "current"
378 .macro GET_CURR_THR_INFO_FLAGS reg
379 GET_CURR_THR_INFO_FROM_SP \reg
380 ld \reg, [\reg, THREAD_INFO_FLAGS]
383 /*--------------------------------------------------------------
384 * For early Exception Prologue, a core reg is temporarily needed to
385 * code the rest of prolog (stack switching). This is done by stashing
386 * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP).
388 * Before saving the full regfile - this reg is restored back, only
389 * to be saved again on kernel mode stack, as part of ptregs.
390 *-------------------------------------------------------------*/
391 .macro EXCPN_PROLOG_FREEUP_REG reg
393 sr \reg, [ARC_REG_SCRATCH_DATA0]
395 st \reg, [@ex_saved_reg1]
399 .macro EXCPN_PROLOG_RESTORE_REG reg
401 lr \reg, [ARC_REG_SCRATCH_DATA0]
403 ld \reg, [@ex_saved_reg1]
407 /*--------------------------------------------------------------
408 * Save all registers used by Exceptions (TLB Miss, Prot-V, Mem err etc)
409 * Requires SP to be already switched to kernel mode Stack
410 * sp points to the next free element on the stack at exit of this macro.
411 * Registers are pushed / popped in the order defined in struct ptregs
413 * Note that syscalls are implemented via TRAP which is also a exception
414 * from CPU's point of view
415 *-------------------------------------------------------------*/
416 .macro SAVE_ALL_EXCEPTION marker
418 st \marker, [sp, 8] /* orig_r8 */
419 st r0, [sp, 4] /* orig_r0, needed only for sys calls */
421 /* Restore r9 used to code the early prologue */
422 EXCPN_PROLOG_RESTORE_REG r9
425 st.a r26, [sp, -4] /* gp */
432 st.a lp_count, [sp, -4]
440 #ifdef PT_REGS_CANARY
445 /* move up by 1 word to "create" pt_regs->"stack_place_holder" */
449 /*--------------------------------------------------------------
450 * Save scratch regs for exceptions
451 *-------------------------------------------------------------*/
453 SAVE_ALL_EXCEPTION orig_r8_IS_EXCPN
456 /*--------------------------------------------------------------
457 * Save scratch regs for sys calls
458 *-------------------------------------------------------------*/
461 * Setup pt_regs->orig_r8.
462 * Encode syscall number (r8) in upper short word of event type (r9)
463 * N.B. #1: This is already endian safe (see ptrace.h)
464 * #2: Only r9 can be used as scratch as it is already clobbered
465 * and it's contents are no longer needed by the latter part
466 * of exception prologue
469 or r9, r9, orig_r8_IS_SCALL
471 SAVE_ALL_EXCEPTION r9
474 /*--------------------------------------------------------------
475 * Restore all registers used by system call or Exceptions
476 * SP should always be pointing to the next free stack element
477 * when entering this macro.
481 * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
482 * for memory load operations. If used in that way interrupts are deffered
483 * by hardware and that is not good.
484 *-------------------------------------------------------------*/
485 .macro RESTORE_ALL_SYS
487 add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */
503 ld.ab r26, [sp, 4] /* gp */
506 ld sp, [sp] /* restore original sp */
507 /* orig_r0 and orig_r8 skipped automatically */
511 /*--------------------------------------------------------------
512 * Save all registers used by interrupt handlers.
513 *-------------------------------------------------------------*/
516 /* restore original r9 , saved in int1_saved_reg
517 * It will be saved on stack in macro: SAVE_CALLER_SAVED
520 lr r9, [ARC_REG_SCRATCH_DATA0]
522 ld r9, [@int1_saved_reg]
525 /* now we are ready to save the remaining context :) */
526 st orig_r8_IS_IRQ1, [sp, 8] /* Event Type */
527 st 0, [sp, 4] /* orig_r0 , N/A for IRQ */
529 st.a r26, [sp, -4] /* gp */
532 st.a ilink1, [sp, -4]
535 st.a lp_count, [sp, -4]
543 #ifdef PT_REGS_CANARY
547 /* move up by 1 word to "create" pt_regs->"stack_place_holder" */
553 /* TODO-vineetg: SMP we can't use global nor can we use
554 * SCRATCH0 as we do for int1 because while int1 is using
557 /* retsore original r9 , saved in sys_saved_r9 */
558 ld r9, [@int2_saved_reg]
560 /* now we are ready to save the remaining context :) */
561 st orig_r8_IS_IRQ2, [sp, 8] /* Event Type */
562 st 0, [sp, 4] /* orig_r0 , N/A for IRQ */
564 st.a r26, [sp, -4] /* gp */
567 st.a ilink2, [sp, -4]
570 st.a lp_count, [sp, -4]
578 #ifdef PT_REGS_CANARY
583 /* move up by 1 word to "create" pt_regs->"stack_place_holder" */
587 /*--------------------------------------------------------------
588 * Restore all registers used by interrupt handlers.
592 * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
593 * for memory load operations. If used in that way interrupts are deffered
594 * by hardware and that is not good.
595 *-------------------------------------------------------------*/
597 .macro RESTORE_ALL_INT1
598 add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */
600 ld.ab r9, [sp, 4] /* Actual reg file */
614 ld.ab r26, [sp, 4] /* gp */
617 ld sp, [sp] /* restore original sp */
618 /* orig_r0 and orig_r8 skipped automatically */
621 .macro RESTORE_ALL_INT2
622 add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */
638 ld.ab r26, [sp, 4] /* gp */
641 ld sp, [sp] /* restore original sp */
642 /* orig_r0 and orig_r8 skipped automatically */
647 /* Get CPU-ID of this core */
648 .macro GET_CPU_ID reg
656 /*-------------------------------------------------
657 * Retrieve the current running task on this CPU
658 * 1. Determine curr CPU id.
659 * 2. Use it to index into _current_task[ ]
661 .macro GET_CURR_TASK_ON_CPU reg
663 ld.as \reg, [@_current_task, \reg]
666 /*-------------------------------------------------
667 * Save a new task as the "current" task on this CPU
668 * 1. Determine curr CPU id.
669 * 2. Use it to index into _current_task[ ]
671 * Coded differently than GET_CURR_TASK_ON_CPU (which uses LD.AS)
672 * because ST r0, [r1, offset] can ONLY have s9 @offset
673 * while LD can take s9 (4 byte insn) or LIMM (8 byte insn)
676 .macro SET_CURR_TASK_ON_CPU tsk, tmp
678 add2 \tmp, @_current_task, \tmp
680 #ifdef CONFIG_ARC_CURR_IN_REG
687 #else /* Uniprocessor implementation of macros */
689 .macro GET_CURR_TASK_ON_CPU reg
690 ld \reg, [@_current_task]
693 .macro SET_CURR_TASK_ON_CPU tsk, tmp
694 st \tsk, [@_current_task]
695 #ifdef CONFIG_ARC_CURR_IN_REG
700 #endif /* SMP / UNI */
702 /* ------------------------------------------------------------------
703 * Get the ptr to some field of Current Task at @off in task struct
704 * -Uses r25 for Current task ptr if that is enabled
707 #ifdef CONFIG_ARC_CURR_IN_REG
709 .macro GET_CURR_TASK_FIELD_PTR off, reg
715 .macro GET_CURR_TASK_FIELD_PTR off, reg
716 GET_CURR_TASK_ON_CPU \reg
720 #endif /* CONFIG_ARC_CURR_IN_REG */
722 #endif /* __ASSEMBLY__ */
724 #endif /* __ASM_ARC_ENTRY_H */