/* * arch/v850/kernel/entry.S -- Low-level system-call handling, trap handlers, * and context-switching * * Copyright (C) 2001,02,03 NEC Electronics Corporation * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org> * * This file is subject to the terms and conditions of the GNU General * Public License. See the file COPYING in the main directory of this * archive for more details. * * Written by Miles Bader <miles@gnu.org> */ #include <linux/sys.h> #include <asm/entry.h>#include <asm/current.h>#include <asm/thread_info.h>#include <asm/clinkage.h>#include <asm/processor.h>#include <asm/irq.h>#include <asm/errno.h> #include <asm/asm-offsets.h> /* Make a slightly more convenient alias for C_SYMBOL_NAME. */#define CSYM C_SYMBOL_NAME /* The offset of the struct pt_regs in a state-save-frame on the stack. */#define PTO STATE_SAVE_PT_OFFSET /* Save argument registers to the state-save-frame pointed to by EP. */#define SAVE_ARG_REGS \ sst.w r6, PTO+PT_GPR(6)[ep]; \ sst.w r7, PTO+PT_GPR(7)[ep]; \ sst.w r8, PTO+PT_GPR(8)[ep]; \ sst.w r9, PTO+PT_GPR(9)[ep]/* Restore argument registers from the state-save-frame pointed to by EP. */#define RESTORE_ARG_REGS \ sld.w PTO+PT_GPR(6)[ep], r6; \ sld.w PTO+PT_GPR(7)[ep], r7; \ sld.w PTO+PT_GPR(8)[ep], r8; \ sld.w PTO+PT_GPR(9)[ep], r9 /* Save value return registers to the state-save-frame pointed to by EP. */#define SAVE_RVAL_REGS \ sst.w r10, PTO+PT_GPR(10)[ep]; \ sst.w r11, PTO+PT_GPR(11)[ep]/* Restore value return registers from the state-save-frame pointed to by EP. */#define RESTORE_RVAL_REGS \ sld.w PTO+PT_GPR(10)[ep], r10; \ sld.w PTO+PT_GPR(11)[ep], r11 #define SAVE_CALL_CLOBBERED_REGS_BEFORE_ARGS \ sst.w r1, PTO+PT_GPR(1)[ep]; \ sst.w r5, PTO+PT_GPR(5)[ep]#define SAVE_CALL_CLOBBERED_REGS_AFTER_RVAL \ sst.w r12, PTO+PT_GPR(12)[ep]; \ sst.w r13, PTO+PT_GPR(13)[ep]; \ sst.w r14, PTO+PT_GPR(14)[ep]; \ sst.w r15, PTO+PT_GPR(15)[ep]; \ sst.w r16, PTO+PT_GPR(16)[ep]; \ sst.w r17, PTO+PT_GPR(17)[ep]; \ sst.w r18, PTO+PT_GPR(18)[ep]; \ sst.w r19, PTO+PT_GPR(19)[ep]#define RESTORE_CALL_CLOBBERED_REGS_BEFORE_ARGS \ sld.w PTO+PT_GPR(1)[ep], r1; \ sld.w PTO+PT_GPR(5)[ep], r5#define RESTORE_CALL_CLOBBERED_REGS_AFTER_RVAL \ sld.w PTO+PT_GPR(12)[ep], r12; \ sld.w PTO+PT_GPR(13)[ep], r13; \ sld.w PTO+PT_GPR(14)[ep], r14; \ sld.w PTO+PT_GPR(15)[ep], r15; \ sld.w PTO+PT_GPR(16)[ep], r16; \ sld.w PTO+PT_GPR(17)[ep], r17; \ sld.w PTO+PT_GPR(18)[ep], r18; \ sld.w PTO+PT_GPR(19)[ep], r19 /* Save `call clobbered' registers to the state-save-frame pointed to by EP. */#define SAVE_CALL_CLOBBERED_REGS \ SAVE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ SAVE_ARG_REGS; \ SAVE_RVAL_REGS; \ SAVE_CALL_CLOBBERED_REGS_AFTER_RVAL/* Restore `call clobbered' registers from the state-save-frame pointed to by EP. */#define RESTORE_CALL_CLOBBERED_REGS \ RESTORE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ RESTORE_ARG_REGS; \ RESTORE_RVAL_REGS; \ RESTORE_CALL_CLOBBERED_REGS_AFTER_RVAL /* Save `call clobbered' registers except for the return-value registers to the state-save-frame pointed to by EP. */#define SAVE_CALL_CLOBBERED_REGS_NO_RVAL \ SAVE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ SAVE_ARG_REGS; \ SAVE_CALL_CLOBBERED_REGS_AFTER_RVAL/* Restore `call clobbered' registers except for the return-value registers from the state-save-frame pointed to by EP. */#define RESTORE_CALL_CLOBBERED_REGS_NO_RVAL \ RESTORE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ RESTORE_ARG_REGS; \ RESTORE_CALL_CLOBBERED_REGS_AFTER_RVAL /* Save `call saved' registers to the state-save-frame pointed to by EP. */#define SAVE_CALL_SAVED_REGS \ sst.w r2, PTO+PT_GPR(2)[ep]; \ sst.w r20, PTO+PT_GPR(20)[ep]; \ sst.w r21, PTO+PT_GPR(21)[ep]; \ sst.w r22, PTO+PT_GPR(22)[ep]; \ sst.w r23, PTO+PT_GPR(23)[ep]; \ sst.w r24, PTO+PT_GPR(24)[ep]; \ sst.w r25, PTO+PT_GPR(25)[ep]; \ sst.w r26, PTO+PT_GPR(26)[ep]; \ sst.w r27, PTO+PT_GPR(27)[ep]; \ sst.w r28, PTO+PT_GPR(28)[ep]; \ sst.w r29, PTO+PT_GPR(29)[ep]/* Restore `call saved' registers from the state-save-frame pointed to by EP. */#define RESTORE_CALL_SAVED_REGS \ sld.w PTO+PT_GPR(2)[ep], r2; \ sld.w PTO+PT_GPR(20)[ep], r20; \ sld.w PTO+PT_GPR(21)[ep], r21; \ sld.w PTO+PT_GPR(22)[ep], r22; \ sld.w PTO+PT_GPR(23)[ep], r23; \ sld.w PTO+PT_GPR(24)[ep], r24; \ sld.w PTO+PT_GPR(25)[ep], r25; \ sld.w PTO+PT_GPR(26)[ep], r26; \ sld.w PTO+PT_GPR(27)[ep], r27; \ sld.w PTO+PT_GPR(28)[ep], r28; \ sld.w PTO+PT_GPR(29)[ep], r29 /* Save the PC stored in the special register SAVEREG to the state-save-frame pointed to by EP. r19 is clobbered. */#define SAVE_PC(savereg) \ stsr SR_ ## savereg, r19; \ sst.w r19, PTO+PT_PC[ep]/* Restore the PC from the state-save-frame pointed to by EP, to the special register SAVEREG. LP is clobbered (it is used as a scratch register because the POP_STATE macro restores it, and this macro is usually used inside POP_STATE). */#define RESTORE_PC(savereg) \ sld.w PTO+PT_PC[ep], lp; \ ldsr lp, SR_ ## savereg/* Save the PSW register stored in the special register SAVREG to the state-save-frame pointed to by EP. r19 is clobbered. */#define SAVE_PSW(savereg) \ stsr SR_ ## savereg, r19; \ sst.w r19, PTO+PT_PSW[ep]/* Restore the PSW register from the state-save-frame pointed to by EP, to the special register SAVEREG. LP is clobbered (it is used as a scratch register because the POP_STATE macro restores it, and this macro is usually used inside POP_STATE). */#define RESTORE_PSW(savereg) \ sld.w PTO+PT_PSW[ep], lp; \ ldsr lp, SR_ ## savereg /* Save CTPC/CTPSW/CTBP registers to the state-save-frame pointed to by REG. r19 is clobbered. */#define SAVE_CT_REGS \ stsr SR_CTPC, r19; \ sst.w r19, PTO+PT_CTPC[ep]; \ stsr SR_CTPSW, r19; \ sst.w r19, PTO+PT_CTPSW[ep]; \ stsr SR_CTBP, r19; \ sst.w r19, PTO+PT_CTBP[ep]/* Restore CTPC/CTPSW/CTBP registers from the state-save-frame pointed to by EP. LP is clobbered (it is used as a scratch register because the POP_STATE macro restores it, and this macro is usually used inside POP_STATE). */#define RESTORE_CT_REGS \ sld.w PTO+PT_CTPC[ep], lp; \ ldsr lp, SR_CTPC; \ sld.w PTO+PT_CTPSW[ep], lp; \ ldsr lp, SR_CTPSW; \ sld.w PTO+PT_CTBP[ep], lp; \ ldsr lp, SR_CTBP /* Push register state, except for the stack pointer, on the stack in the form of a state-save-frame (plus some extra padding), in preparation for a system call. This macro makes sure that the EP, GP, and LP registers are saved, and TYPE identifies the set of extra registers to be saved as well. Also copies (the new value of) SP to EP. */#define PUSH_STATE(type) \ addi -STATE_SAVE_SIZE, sp, sp; /* Make room on the stack. */ \ st.w ep, PTO+PT_GPR(GPR_EP)[sp]; \ mov sp, ep; \ sst.w gp, PTO+PT_GPR(GPR_GP)[ep]; \ sst.w lp, PTO+PT_GPR(GPR_LP)[ep]; \ type ## _STATE_SAVER/* Pop a register state pushed by PUSH_STATE, except for the stack pointer, from the stack. */#define POP_STATE(type) \ mov sp, ep; \ type ## _STATE_RESTORER; \ sld.w PTO+PT_GPR(GPR_GP)[ep], gp; \ sld.w PTO+PT_GPR(GPR_LP)[ep], lp; \ sld.w PTO+PT_GPR(GPR_EP)[ep], ep; \ addi STATE_SAVE_SIZE, sp, sp /* Clean up our stack space. */ /* Switch to the kernel stack if necessary, and push register state on the stack in the form of a state-save-frame. Also load the current task pointer if switching from user mode. The stack-pointer (r3) should have already been saved to the memory location SP_SAVE_LOC (the reason for this is that the interrupt vectors may be beyond a 22-bit signed offset jump from the actual interrupt handler, and this allows them to save the stack-pointer and use that register to do an indirect jump). This macro makes sure that `special' registers, system registers, and the stack pointer are saved; TYPE identifies the set of extra registers to be saved as well. SYSCALL_NUM is the register in which the system-call number this state is for is stored (r0 if this isn't a system call). Interrupts should already be disabled when calling this. */#define SAVE_STATE(type, syscall_num, sp_save_loc) \ tst1 0, KM; /* See if already in kernel mode. */ \ bz 1f; \ ld.w sp_save_loc, sp; /* ... yes, use saved SP. */ \ br 2f; \1: ld.w KSP, sp; /* ... no, switch to kernel stack. */ \2: PUSH_STATE(type); \ ld.b KM, r19; /* Remember old kernel-mode. */ \ sst.w r19, PTO+PT_KERNEL_MODE[ep]; \ ld.w sp_save_loc, r19; /* Remember old SP. */ \ sst.w r19, PTO+PT_GPR(GPR_SP)[ep]; \ mov 1, r19; /* Now definitely in kernel-mode. */ \ st.b r19, KM; \ GET_CURRENT_TASK(CURRENT_TASK); /* Fetch the current task pointer. */ \ /* Save away the syscall number. */ \ sst.w syscall_num, PTO+PT_CUR_SYSCALL[ep] /* Save register state not normally saved by PUSH_STATE for TYPE, to the state-save-frame on the stack; also copies SP to EP. r19 may be trashed. */#define SAVE_EXTRA_STATE(type) \ mov sp, ep; \ type ## _EXTRA_STATE_SAVER/* Restore register state not normally restored by POP_STATE for TYPE, from the state-save-frame on the stack; also copies SP to EP. r19 may be trashed. */#define RESTORE_EXTRA_STATE(type) \ mov sp, ep; \ type ## _EXTRA_STATE_RESTORER /* Save any call-clobbered registers not normally saved by PUSH_STATE for TYPE, to the state-save-frame on the stack. EP may be trashed, but is not guaranteed to contain a copy of SP (unlike after most SAVE_... macros). r19 may be trashed. */#define SAVE_EXTRA_STATE_FOR_SCHEDULE(type) \ type ## _SCHEDULE_EXTRA_STATE_SAVER/* Restore any call-clobbered registers not normally restored by POP_STATE for TYPE, to the state-save-frame on the stack. EP may be trashed, but is not guaranteed to contain a copy of SP (unlike after most RESTORE_... macros). r19 may be trashed. */#define RESTORE_EXTRA_STATE_FOR_SCHEDULE(type) \ type ## _SCHEDULE_EXTRA_STATE_RESTORER /* These are extra_state_saver/restorer values for a user trap. Note that we save the argument registers so that restarted syscalls will function properly (otherwise it wouldn't be necessary), and we must _not_ restore the return-value registers (so that traps can return a value!), but call-clobbered registers are not saved at all, as the caller of the syscall function should have saved them. */ #define TRAP_RET reti/* Traps don't save call-clobbered registers (but do still save arg regs). We preserve PSw to keep long-term state, namely interrupt status (for traps from kernel-mode), and the single-step flag (for user traps). */#define TRAP_STATE_SAVER \ SAVE_ARG_REGS; \ SAVE_PC(EIPC); \ SAVE_PSW(EIPSW)/* When traps return, they just leave call-clobbered registers (except for arg regs) with whatever value they have from the kernel. Traps don't preserve the PSW, but we zero EIPSW to ensure it doesn't contain anything dangerous (in particular, the single-step flag). */#define TRAP_STATE_RESTORER \ RESTORE_ARG_REGS; \ RESTORE_PC(EIPC); \ RESTORE_PSW(EIPSW)/* Save registers not normally saved by traps. We need to save r12, even though it's nominally call-clobbered, because it's used when restarting a system call (the signal-handling path uses SAVE_EXTRA_STATE, and expects r12 to be restored when the trap returns). */#define TRAP_EXTRA_STATE_SAVER \ SAVE_RVAL_REGS; \ sst.w r12, PTO+PT_GPR(12)[ep]; \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS#define TRAP_EXTRA_STATE_RESTORER \ RESTORE_RVAL_REGS; \ sld.w PTO+PT_GPR(12)[ep], r12; \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS/* Save registers prior to calling scheduler (just before trap returns). We have to save the return-value registers to preserve the trap's return value. Note that ..._SCHEDULE_EXTRA_STATE_SAVER, unlike most ..._SAVER macros, is required to setup EP itself if EP is needed (this is because in many cases, the macro is empty). */#define TRAP_SCHEDULE_EXTRA_STATE_SAVER \ mov sp, ep; \ SAVE_RVAL_REGS/* Note that ..._SCHEDULE_EXTRA_STATE_RESTORER, unlike most ..._RESTORER macros, is required to setup EP itself if EP is needed (this is because in many cases, the macro is empty). */#define TRAP_SCHEDULE_EXTRA_STATE_RESTORER \ mov sp, ep; \ RESTORE_RVAL_REGS /* Register saving/restoring for maskable interrupts. */#define IRQ_RET reti#define IRQ_STATE_SAVER \ SAVE_CALL_CLOBBERED_REGS; \ SAVE_PC(EIPC); \ SAVE_PSW(EIPSW)#define IRQ_STATE_RESTORER \ RESTORE_CALL_CLOBBERED_REGS; \ RESTORE_PC(EIPC); \ RESTORE_PSW(EIPSW)#define IRQ_EXTRA_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS#define IRQ_EXTRA_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS#define IRQ_SCHEDULE_EXTRA_STATE_SAVER /* nothing */#define IRQ_SCHEDULE_EXTRA_STATE_RESTORER /* nothing */ /* Register saving/restoring for non-maskable interrupts. */#define NMI_RET reti#define NMI_STATE_SAVER \ SAVE_CALL_CLOBBERED_REGS; \ SAVE_PC(FEPC); \ SAVE_PSW(FEPSW);#define NMI_STATE_RESTORER \ RESTORE_CALL_CLOBBERED_REGS; \ RESTORE_PC(FEPC); \ RESTORE_PSW(FEPSW);#define NMI_EXTRA_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS#define NMI_EXTRA_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS#define NMI_SCHEDULE_EXTRA_STATE_SAVER /* nothing */#define NMI_SCHEDULE_EXTRA_STATE_RESTORER /* nothing */ /* Register saving/restoring for debug traps. */#define DBTRAP_RET .long 0x014607E0 /* `dbret', but gas doesn't support it. */#define DBTRAP_STATE_SAVER \ SAVE_CALL_CLOBBERED_REGS; \ SAVE_PC(DBPC); \ SAVE_PSW(DBPSW)#define DBTRAP_STATE_RESTORER \ RESTORE_CALL_CLOBBERED_REGS; \ RESTORE_PC(DBPC); \ RESTORE_PSW(DBPSW)#define DBTRAP_EXTRA_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS#define DBTRAP_EXTRA_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS#define DBTRAP_SCHEDULE_EXTRA_STATE_SAVER /* nothing */#define DBTRAP_SCHEDULE_EXTRA_STATE_RESTORER /* nothing */ /* Register saving/restoring for a context switch. We don't need to save too many registers, because context-switching looks like a function call (via the function `switch_thread'), so callers will save any call-clobbered registers themselves. We do need to save the CT regs, as they're normally not saved during kernel entry (the kernel doesn't use them). We save PSW so that interrupt-status state will correctly follow each thread (mostly NMI vs. normal-IRQ/trap), though for the most part it doesn't matter since threads are always in almost exactly the same processor state during a context switch. The stack pointer and return value are handled by switch_thread itself. */#define SWITCH_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_PSW(PSW); \ SAVE_CT_REGS#define SWITCH_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_PSW(PSW); \ RESTORE_CT_REGS /* Restore register state from the state-save-frame on the stack, switch back to the user stack if necessary, and return from the trap/interrupt. EXTRA_STATE_RESTORER is a sequence of assembly language statements to restore anything not restored by this macro. Only registers not saved by the C compiler are restored (that is, R3(sp), R4(gp), R31(lp), and anything restored by EXTRA_STATE_RESTORER). */#define RETURN(type) \ ld.b PTO+PT_KERNEL_MODE[sp], r19; \ di; /* Disable interrupts */ \ cmp r19, r0; /* See if returning to kernel mode, */\ bne 2f; /* ... if so, skip resched &c. */ \ \ /* We're returning to user mode, so check for various conditions that \ trigger rescheduling. */ \ GET_CURRENT_THREAD(r18); \ ld.w TI_FLAGS[r18], r19; \ andi _TIF_NEED_RESCHED, r19, r0; \ bnz 3f; /* Call the scheduler. */ \5: andi _TIF_SIGPENDING, r19, r18; \ ld.w TASK_PTRACE[CURRENT_TASK], r19; /* ptrace flags */ \ or r18, r19; /* see if either is non-zero */ \ bnz 4f; /* if so, handle them */ \ \/* Return to user state. */ \1: st.b r0, KM; /* Now officially in user state. */ \ \/* Final return. The stack-pointer fiddling is not needed when returning \ to kernel-mode, but they don't hurt, and this way we can share the \ (sometimes rather lengthy) POP_STATE macro. */ \2: POP_STATE(type); \ st.w sp, KSP; /* Save the kernel stack pointer. */ \ ld.w PT_GPR(GPR_SP)-PT_SIZE[sp], sp; /* Restore stack pointer. */ \ type ## _RET; /* Return from the trap/interrupt. */ \ \/* Call the scheduler before returning from a syscall/trap. */ \3: SAVE_EXTRA_STATE_FOR_SCHEDULE(type); /* Prepare to call scheduler. */ \ jarl call_scheduler, lp; /* Call scheduler */ \ di; /* The scheduler enables interrupts */\ RESTORE_EXTRA_STATE_FOR_SCHEDULE(type); \ GET_CURRENT_THREAD(r18); \ ld.w TI_FLAGS[r18], r19; \ br 5b; /* Continue with return path. */ \ \/* Handle a signal or ptraced process return. \ r18 should be non-zero if there are pending signals. */ \4: /* Not all registers are saved by the normal trap/interrupt entry \ points (for instance, call-saved registers (because the normal \ C-compiler calling sequence in the kernel makes sure they're \ preserved), and call-clobbered registers in the case of \ traps), but signal handlers may want to examine or change the \ complete register state. Here we save anything not saved by \ the normal entry sequence, so that it may be safely restored \ (in a possibly modified form) after do_signal returns. */ \ SAVE_EXTRA_STATE(type); /* Save state not saved by entry. */ \ jarl handle_signal_or_ptrace_return, lp; \ RESTORE_EXTRA_STATE(type); /* Restore extra regs. */ \ br 1b /* Jump to the appropriate function for the system call number in r12 (r12 is not preserved), or return an error if r12 is not valid. The LP register should point to the location where the called function should return. [note that MAKE_SYS_CALL uses label 1] */#define MAKE_SYS_CALL \ /* Figure out which function to use for this system call. */ \ shl 2, r12; \ /* See if the system call number is valid. */ \ addi lo(CSYM(sys_call_table) - sys_call_table_end), r12, r0; \ bnh 1f; \ mov hilo(CSYM(sys_call_table)), r19; \ add r19, r12; \ ld.w 0[r12], r12; \ /* Make the system call. */ \ jmp [r12]; \ /* The syscall number is invalid, return an error. */ \1: addi -ENOSYS, r0, r10; \ jmp [lp] .text /* * User trap. * * Trap 0 system calls are also handled here. * * The stack-pointer (r3) should have already been saved to the memory * location ENTRY_SP (the reason for this is that the interrupt vectors may be * beyond a 22-bit signed offset jump from the actual interrupt handler, and * this allows them to save the stack-pointer and use that register to do an * indirect jump). * * Syscall protocol: * Syscall number in r12, args in r6-r9 * Return value in r10 */G_ENTRY(trap): SAVE_STATE (TRAP, r12, ENTRY_SP) // Save registers. stsr SR_ECR, r19 // Find out which trap it was. ei // Enable interrupts. mov hilo(ret_from_trap), lp // where the trap should return // The following two shifts (1) clear out extraneous NMI data in the // upper 16-bits, (2) convert the 0x40 - 0x5f range of trap ECR // numbers into the (0-31) << 2 range we want, (3) set the flags. shl 27, r19 // chop off all high bits shr 25, r19 // scale back down and then << 2 bnz 2f // See if not trap 0.Lines 1–500 of 1121. Load more
/* * arch/v850/kernel/entry.S -- Low-level system-call handling, trap handlers, * and context-switching * * Copyright (C) 2001,02,03 NEC Electronics Corporation * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org> * * This file is subject to the terms and conditions of the GNU General * Public License. See the file COPYING in the main directory of this * archive for more details. * * Written by Miles Bader <miles@gnu.org> */ #include <linux/sys.h> #include <asm/entry.h> #include <asm/current.h> #include <asm/thread_info.h> #include <asm/clinkage.h> #include <asm/processor.h> #include <asm/irq.h> #include <asm/errno.h> #include <asm/asm-offsets.h> /* Make a slightly more convenient alias for C_SYMBOL_NAME. */ #define CSYM C_SYMBOL_NAME /* The offset of the struct pt_regs in a state-save-frame on the stack. */ #define PTO STATE_SAVE_PT_OFFSET /* Save argument registers to the state-save-frame pointed to by EP. */ #define SAVE_ARG_REGS \ sst.w r6, PTO+PT_GPR(6)[ep]; \ sst.w r7, PTO+PT_GPR(7)[ep]; \ sst.w r8, PTO+PT_GPR(8)[ep]; \ sst.w r9, PTO+PT_GPR(9)[ep] /* Restore argument registers from the state-save-frame pointed to by EP. */ #define RESTORE_ARG_REGS \ sld.w PTO+PT_GPR(6)[ep], r6; \ sld.w PTO+PT_GPR(7)[ep], r7; \ sld.w PTO+PT_GPR(8)[ep], r8; \ sld.w PTO+PT_GPR(9)[ep], r9 /* Save value return registers to the state-save-frame pointed to by EP. */ #define SAVE_RVAL_REGS \ sst.w r10, PTO+PT_GPR(10)[ep]; \ sst.w r11, PTO+PT_GPR(11)[ep] /* Restore value return registers from the state-save-frame pointed to by EP. */ #define RESTORE_RVAL_REGS \ sld.w PTO+PT_GPR(10)[ep], r10; \ sld.w PTO+PT_GPR(11)[ep], r11 #define SAVE_CALL_CLOBBERED_REGS_BEFORE_ARGS \ sst.w r1, PTO+PT_GPR(1)[ep]; \ sst.w r5, PTO+PT_GPR(5)[ep] #define SAVE_CALL_CLOBBERED_REGS_AFTER_RVAL \ sst.w r12, PTO+PT_GPR(12)[ep]; \ sst.w r13, PTO+PT_GPR(13)[ep]; \ sst.w r14, PTO+PT_GPR(14)[ep]; \ sst.w r15, PTO+PT_GPR(15)[ep]; \ sst.w r16, PTO+PT_GPR(16)[ep]; \ sst.w r17, PTO+PT_GPR(17)[ep]; \ sst.w r18, PTO+PT_GPR(18)[ep]; \ sst.w r19, PTO+PT_GPR(19)[ep] #define RESTORE_CALL_CLOBBERED_REGS_BEFORE_ARGS \ sld.w PTO+PT_GPR(1)[ep], r1; \ sld.w PTO+PT_GPR(5)[ep], r5 #define RESTORE_CALL_CLOBBERED_REGS_AFTER_RVAL \ sld.w PTO+PT_GPR(12)[ep], r12; \ sld.w PTO+PT_GPR(13)[ep], r13; \ sld.w PTO+PT_GPR(14)[ep], r14; \ sld.w PTO+PT_GPR(15)[ep], r15; \ sld.w PTO+PT_GPR(16)[ep], r16; \ sld.w PTO+PT_GPR(17)[ep], r17; \ sld.w PTO+PT_GPR(18)[ep], r18; \ sld.w PTO+PT_GPR(19)[ep], r19 /* Save `call clobbered' registers to the state-save-frame pointed to by EP. */ #define SAVE_CALL_CLOBBERED_REGS \ SAVE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ SAVE_ARG_REGS; \ SAVE_RVAL_REGS; \ SAVE_CALL_CLOBBERED_REGS_AFTER_RVAL /* Restore `call clobbered' registers from the state-save-frame pointed to by EP. */ #define RESTORE_CALL_CLOBBERED_REGS \ RESTORE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ RESTORE_ARG_REGS; \ RESTORE_RVAL_REGS; \ RESTORE_CALL_CLOBBERED_REGS_AFTER_RVAL /* Save `call clobbered' registers except for the return-value registers to the state-save-frame pointed to by EP. */ #define SAVE_CALL_CLOBBERED_REGS_NO_RVAL \ SAVE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ SAVE_ARG_REGS; \ SAVE_CALL_CLOBBERED_REGS_AFTER_RVAL /* Restore `call clobbered' registers except for the return-value registers from the state-save-frame pointed to by EP. */ #define RESTORE_CALL_CLOBBERED_REGS_NO_RVAL \ RESTORE_CALL_CLOBBERED_REGS_BEFORE_ARGS; \ RESTORE_ARG_REGS; \ RESTORE_CALL_CLOBBERED_REGS_AFTER_RVAL /* Save `call saved' registers to the state-save-frame pointed to by EP. */ #define SAVE_CALL_SAVED_REGS \ sst.w r2, PTO+PT_GPR(2)[ep]; \ sst.w r20, PTO+PT_GPR(20)[ep]; \ sst.w r21, PTO+PT_GPR(21)[ep]; \ sst.w r22, PTO+PT_GPR(22)[ep]; \ sst.w r23, PTO+PT_GPR(23)[ep]; \ sst.w r24, PTO+PT_GPR(24)[ep]; \ sst.w r25, PTO+PT_GPR(25)[ep]; \ sst.w r26, PTO+PT_GPR(26)[ep]; \ sst.w r27, PTO+PT_GPR(27)[ep]; \ sst.w r28, PTO+PT_GPR(28)[ep]; \ sst.w r29, PTO+PT_GPR(29)[ep] /* Restore `call saved' registers from the state-save-frame pointed to by EP. */ #define RESTORE_CALL_SAVED_REGS \ sld.w PTO+PT_GPR(2)[ep], r2; \ sld.w PTO+PT_GPR(20)[ep], r20; \ sld.w PTO+PT_GPR(21)[ep], r21; \ sld.w PTO+PT_GPR(22)[ep], r22; \ sld.w PTO+PT_GPR(23)[ep], r23; \ sld.w PTO+PT_GPR(24)[ep], r24; \ sld.w PTO+PT_GPR(25)[ep], r25; \ sld.w PTO+PT_GPR(26)[ep], r26; \ sld.w PTO+PT_GPR(27)[ep], r27; \ sld.w PTO+PT_GPR(28)[ep], r28; \ sld.w PTO+PT_GPR(29)[ep], r29 /* Save the PC stored in the special register SAVEREG to the state-save-frame pointed to by EP. r19 is clobbered. */ #define SAVE_PC(savereg) \ stsr SR_ ## savereg, r19; \ sst.w r19, PTO+PT_PC[ep] /* Restore the PC from the state-save-frame pointed to by EP, to the special register SAVEREG. LP is clobbered (it is used as a scratch register because the POP_STATE macro restores it, and this macro is usually used inside POP_STATE). */ #define RESTORE_PC(savereg) \ sld.w PTO+PT_PC[ep], lp; \ ldsr lp, SR_ ## savereg /* Save the PSW register stored in the special register SAVREG to the state-save-frame pointed to by EP. r19 is clobbered. */ #define SAVE_PSW(savereg) \ stsr SR_ ## savereg, r19; \ sst.w r19, PTO+PT_PSW[ep] /* Restore the PSW register from the state-save-frame pointed to by EP, to the special register SAVEREG. LP is clobbered (it is used as a scratch register because the POP_STATE macro restores it, and this macro is usually used inside POP_STATE). */ #define RESTORE_PSW(savereg) \ sld.w PTO+PT_PSW[ep], lp; \ ldsr lp, SR_ ## savereg /* Save CTPC/CTPSW/CTBP registers to the state-save-frame pointed to by REG. r19 is clobbered. */ #define SAVE_CT_REGS \ stsr SR_CTPC, r19; \ sst.w r19, PTO+PT_CTPC[ep]; \ stsr SR_CTPSW, r19; \ sst.w r19, PTO+PT_CTPSW[ep]; \ stsr SR_CTBP, r19; \ sst.w r19, PTO+PT_CTBP[ep] /* Restore CTPC/CTPSW/CTBP registers from the state-save-frame pointed to by EP. LP is clobbered (it is used as a scratch register because the POP_STATE macro restores it, and this macro is usually used inside POP_STATE). */ #define RESTORE_CT_REGS \ sld.w PTO+PT_CTPC[ep], lp; \ ldsr lp, SR_CTPC; \ sld.w PTO+PT_CTPSW[ep], lp; \ ldsr lp, SR_CTPSW; \ sld.w PTO+PT_CTBP[ep], lp; \ ldsr lp, SR_CTBP /* Push register state, except for the stack pointer, on the stack in the form of a state-save-frame (plus some extra padding), in preparation for a system call. This macro makes sure that the EP, GP, and LP registers are saved, and TYPE identifies the set of extra registers to be saved as well. Also copies (the new value of) SP to EP. */ #define PUSH_STATE(type) \ addi -STATE_SAVE_SIZE, sp, sp; /* Make room on the stack. */ \ st.w ep, PTO+PT_GPR(GPR_EP)[sp]; \ mov sp, ep; \ sst.w gp, PTO+PT_GPR(GPR_GP)[ep]; \ sst.w lp, PTO+PT_GPR(GPR_LP)[ep]; \ type ## _STATE_SAVER /* Pop a register state pushed by PUSH_STATE, except for the stack pointer, from the stack. */ #define POP_STATE(type) \ mov sp, ep; \ type ## _STATE_RESTORER; \ sld.w PTO+PT_GPR(GPR_GP)[ep], gp; \ sld.w PTO+PT_GPR(GPR_LP)[ep], lp; \ sld.w PTO+PT_GPR(GPR_EP)[ep], ep; \ addi STATE_SAVE_SIZE, sp, sp /* Clean up our stack space. */ /* Switch to the kernel stack if necessary, and push register state on the stack in the form of a state-save-frame. Also load the current task pointer if switching from user mode. The stack-pointer (r3) should have already been saved to the memory location SP_SAVE_LOC (the reason for this is that the interrupt vectors may be beyond a 22-bit signed offset jump from the actual interrupt handler, and this allows them to save the stack-pointer and use that register to do an indirect jump). This macro makes sure that `special' registers, system registers, and the stack pointer are saved; TYPE identifies the set of extra registers to be saved as well. SYSCALL_NUM is the register in which the system-call number this state is for is stored (r0 if this isn't a system call). Interrupts should already be disabled when calling this. */ #define SAVE_STATE(type, syscall_num, sp_save_loc) \ tst1 0, KM; /* See if already in kernel mode. */ \ bz 1f; \ ld.w sp_save_loc, sp; /* ... yes, use saved SP. */ \ br 2f; \ 1: ld.w KSP, sp; /* ... no, switch to kernel stack. */ \ 2: PUSH_STATE(type); \ ld.b KM, r19; /* Remember old kernel-mode. */ \ sst.w r19, PTO+PT_KERNEL_MODE[ep]; \ ld.w sp_save_loc, r19; /* Remember old SP. */ \ sst.w r19, PTO+PT_GPR(GPR_SP)[ep]; \ mov 1, r19; /* Now definitely in kernel-mode. */ \ st.b r19, KM; \ GET_CURRENT_TASK(CURRENT_TASK); /* Fetch the current task pointer. */ \ /* Save away the syscall number. */ \ sst.w syscall_num, PTO+PT_CUR_SYSCALL[ep] /* Save register state not normally saved by PUSH_STATE for TYPE, to the state-save-frame on the stack; also copies SP to EP. r19 may be trashed. */ #define SAVE_EXTRA_STATE(type) \ mov sp, ep; \ type ## _EXTRA_STATE_SAVER /* Restore register state not normally restored by POP_STATE for TYPE, from the state-save-frame on the stack; also copies SP to EP. r19 may be trashed. */ #define RESTORE_EXTRA_STATE(type) \ mov sp, ep; \ type ## _EXTRA_STATE_RESTORER /* Save any call-clobbered registers not normally saved by PUSH_STATE for TYPE, to the state-save-frame on the stack. EP may be trashed, but is not guaranteed to contain a copy of SP (unlike after most SAVE_... macros). r19 may be trashed. */ #define SAVE_EXTRA_STATE_FOR_SCHEDULE(type) \ type ## _SCHEDULE_EXTRA_STATE_SAVER /* Restore any call-clobbered registers not normally restored by POP_STATE for TYPE, to the state-save-frame on the stack. EP may be trashed, but is not guaranteed to contain a copy of SP (unlike after most RESTORE_... macros). r19 may be trashed. */ #define RESTORE_EXTRA_STATE_FOR_SCHEDULE(type) \ type ## _SCHEDULE_EXTRA_STATE_RESTORER /* These are extra_state_saver/restorer values for a user trap. Note that we save the argument registers so that restarted syscalls will function properly (otherwise it wouldn't be necessary), and we must _not_ restore the return-value registers (so that traps can return a value!), but call-clobbered registers are not saved at all, as the caller of the syscall function should have saved them. */ #define TRAP_RET reti /* Traps don't save call-clobbered registers (but do still save arg regs). We preserve PSw to keep long-term state, namely interrupt status (for traps from kernel-mode), and the single-step flag (for user traps). */ #define TRAP_STATE_SAVER \ SAVE_ARG_REGS; \ SAVE_PC(EIPC); \ SAVE_PSW(EIPSW) /* When traps return, they just leave call-clobbered registers (except for arg regs) with whatever value they have from the kernel. Traps don't preserve the PSW, but we zero EIPSW to ensure it doesn't contain anything dangerous (in particular, the single-step flag). */ #define TRAP_STATE_RESTORER \ RESTORE_ARG_REGS; \ RESTORE_PC(EIPC); \ RESTORE_PSW(EIPSW) /* Save registers not normally saved by traps. We need to save r12, even though it's nominally call-clobbered, because it's used when restarting a system call (the signal-handling path uses SAVE_EXTRA_STATE, and expects r12 to be restored when the trap returns). */ #define TRAP_EXTRA_STATE_SAVER \ SAVE_RVAL_REGS; \ sst.w r12, PTO+PT_GPR(12)[ep]; \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS #define TRAP_EXTRA_STATE_RESTORER \ RESTORE_RVAL_REGS; \ sld.w PTO+PT_GPR(12)[ep], r12; \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS /* Save registers prior to calling scheduler (just before trap returns). We have to save the return-value registers to preserve the trap's return value. Note that ..._SCHEDULE_EXTRA_STATE_SAVER, unlike most ..._SAVER macros, is required to setup EP itself if EP is needed (this is because in many cases, the macro is empty). */ #define TRAP_SCHEDULE_EXTRA_STATE_SAVER \ mov sp, ep; \ SAVE_RVAL_REGS /* Note that ..._SCHEDULE_EXTRA_STATE_RESTORER, unlike most ..._RESTORER macros, is required to setup EP itself if EP is needed (this is because in many cases, the macro is empty). */ #define TRAP_SCHEDULE_EXTRA_STATE_RESTORER \ mov sp, ep; \ RESTORE_RVAL_REGS /* Register saving/restoring for maskable interrupts. */ #define IRQ_RET reti #define IRQ_STATE_SAVER \ SAVE_CALL_CLOBBERED_REGS; \ SAVE_PC(EIPC); \ SAVE_PSW(EIPSW) #define IRQ_STATE_RESTORER \ RESTORE_CALL_CLOBBERED_REGS; \ RESTORE_PC(EIPC); \ RESTORE_PSW(EIPSW) #define IRQ_EXTRA_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS #define IRQ_EXTRA_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS #define IRQ_SCHEDULE_EXTRA_STATE_SAVER /* nothing */ #define IRQ_SCHEDULE_EXTRA_STATE_RESTORER /* nothing */ /* Register saving/restoring for non-maskable interrupts. */ #define NMI_RET reti #define NMI_STATE_SAVER \ SAVE_CALL_CLOBBERED_REGS; \ SAVE_PC(FEPC); \ SAVE_PSW(FEPSW); #define NMI_STATE_RESTORER \ RESTORE_CALL_CLOBBERED_REGS; \ RESTORE_PC(FEPC); \ RESTORE_PSW(FEPSW); #define NMI_EXTRA_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS #define NMI_EXTRA_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS #define NMI_SCHEDULE_EXTRA_STATE_SAVER /* nothing */ #define NMI_SCHEDULE_EXTRA_STATE_RESTORER /* nothing */ /* Register saving/restoring for debug traps. */ #define DBTRAP_RET .long 0x014607E0 /* `dbret', but gas doesn't support it. */ #define DBTRAP_STATE_SAVER \ SAVE_CALL_CLOBBERED_REGS; \ SAVE_PC(DBPC); \ SAVE_PSW(DBPSW) #define DBTRAP_STATE_RESTORER \ RESTORE_CALL_CLOBBERED_REGS; \ RESTORE_PC(DBPC); \ RESTORE_PSW(DBPSW) #define DBTRAP_EXTRA_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_CT_REGS #define DBTRAP_EXTRA_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_CT_REGS #define DBTRAP_SCHEDULE_EXTRA_STATE_SAVER /* nothing */ #define DBTRAP_SCHEDULE_EXTRA_STATE_RESTORER /* nothing */ /* Register saving/restoring for a context switch. We don't need to save too many registers, because context-switching looks like a function call (via the function `switch_thread'), so callers will save any call-clobbered registers themselves. We do need to save the CT regs, as they're normally not saved during kernel entry (the kernel doesn't use them). We save PSW so that interrupt-status state will correctly follow each thread (mostly NMI vs. normal-IRQ/trap), though for the most part it doesn't matter since threads are always in almost exactly the same processor state during a context switch. The stack pointer and return value are handled by switch_thread itself. */ #define SWITCH_STATE_SAVER \ SAVE_CALL_SAVED_REGS; \ SAVE_PSW(PSW); \ SAVE_CT_REGS #define SWITCH_STATE_RESTORER \ RESTORE_CALL_SAVED_REGS; \ RESTORE_PSW(PSW); \ RESTORE_CT_REGS /* Restore register state from the state-save-frame on the stack, switch back to the user stack if necessary, and return from the trap/interrupt. EXTRA_STATE_RESTORER is a sequence of assembly language statements to restore anything not restored by this macro. Only registers not saved by the C compiler are restored (that is, R3(sp), R4(gp), R31(lp), and anything restored by EXTRA_STATE_RESTORER). */ #define RETURN(type) \ ld.b PTO+PT_KERNEL_MODE[sp], r19; \ di; /* Disable interrupts */ \ cmp r19, r0; /* See if returning to kernel mode, */\ bne 2f; /* ... if so, skip resched &c. */ \ \ /* We're returning to user mode, so check for various conditions that \ trigger rescheduling. */ \ GET_CURRENT_THREAD(r18); \ ld.w TI_FLAGS[r18], r19; \ andi _TIF_NEED_RESCHED, r19, r0; \ bnz 3f; /* Call the scheduler. */ \ 5: andi _TIF_SIGPENDING, r19, r18; \ ld.w TASK_PTRACE[CURRENT_TASK], r19; /* ptrace flags */ \ or r18, r19; /* see if either is non-zero */ \ bnz 4f; /* if so, handle them */ \ \ /* Return to user state. */ \ 1: st.b r0, KM; /* Now officially in user state. */ \ \ /* Final return. The stack-pointer fiddling is not needed when returning \ to kernel-mode, but they don't hurt, and this way we can share the \ (sometimes rather lengthy) POP_STATE macro. */ \ 2: POP_STATE(type); \ st.w sp, KSP; /* Save the kernel stack pointer. */ \ ld.w PT_GPR(GPR_SP)-PT_SIZE[sp], sp; /* Restore stack pointer. */ \ type ## _RET; /* Return from the trap/interrupt. */ \ \ /* Call the scheduler before returning from a syscall/trap. */ \ 3: SAVE_EXTRA_STATE_FOR_SCHEDULE(type); /* Prepare to call scheduler. */ \ jarl call_scheduler, lp; /* Call scheduler */ \ di; /* The scheduler enables interrupts */\ RESTORE_EXTRA_STATE_FOR_SCHEDULE(type); \ GET_CURRENT_THREAD(r18); \ ld.w TI_FLAGS[r18], r19; \ br 5b; /* Continue with return path. */ \ \ /* Handle a signal or ptraced process return. \ r18 should be non-zero if there are pending signals. */ \ 4: /* Not all registers are saved by the normal trap/interrupt entry \ points (for instance, call-saved registers (because the normal \ C-compiler calling sequence in the kernel makes sure they're \ preserved), and call-clobbered registers in the case of \ traps), but signal handlers may want to examine or change the \ complete register state. Here we save anything not saved by \ the normal entry sequence, so that it may be safely restored \ (in a possibly modified form) after do_signal returns. */ \ SAVE_EXTRA_STATE(type); /* Save state not saved by entry. */ \ jarl handle_signal_or_ptrace_return, lp; \ RESTORE_EXTRA_STATE(type); /* Restore extra regs. */ \ br 1b /* Jump to the appropriate function for the system call number in r12 (r12 is not preserved), or return an error if r12 is not valid. The LP register should point to the location where the called function should return. [note that MAKE_SYS_CALL uses label 1] */ #define MAKE_SYS_CALL \ /* Figure out which function to use for this system call. */ \ shl 2, r12; \ /* See if the system call number is valid. */ \ addi lo(CSYM(sys_call_table) - sys_call_table_end), r12, r0; \ bnh 1f; \ mov hilo(CSYM(sys_call_table)), r19; \ add r19, r12; \ ld.w 0[r12], r12; \ /* Make the system call. */ \ jmp [r12]; \ /* The syscall number is invalid, return an error. */ \ 1: addi -ENOSYS, r0, r10; \ jmp [lp] .text /* * User trap. * * Trap 0 system calls are also handled here. * * The stack-pointer (r3) should have already been saved to the memory * location ENTRY_SP (the reason for this is that the interrupt vectors may be * beyond a 22-bit signed offset jump from the actual interrupt handler, and * this allows them to save the stack-pointer and use that register to do an * indirect jump). * * Syscall protocol: * Syscall number in r12, args in r6-r9 * Return value in r10 */ G_ENTRY(trap): SAVE_STATE (TRAP, r12, ENTRY_SP) // Save registers. stsr SR_ECR, r19 // Find out which trap it was. ei // Enable interrupts. mov hilo(ret_from_trap), lp // where the trap should return // The following two shifts (1) clear out extraneous NMI data in the // upper 16-bits, (2) convert the 0x40 - 0x5f range of trap ECR // numbers into the (0-31) << 2 range we want, (3) set the flags. shl 27, r19 // chop off all high bits shr 25, r19 // scale back down and then << 2 bnz 2f // See if not trap 0.