/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * OpenRISC entry.S * * Linux architectural port borrowing liberally from similar works of * others. All original copyrights apply as per the original source * declaration. * * Modifications for the OpenRISC architecture: * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com> * Copyright (C) 2005 Gyorgy Jeney <nog@bsemi.com> * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
*/
/* * We need to disable interrupts at beginning of RESTORE_ALL * since interrupt might come in after we've loaded EPC return address * and overwrite EPC with address somewhere in RESTORE_ALL * which is of course wrong!
*/
/* clobbers 'reg' */
#define CLEAR_LWA_FLAG(reg) \
l.movhi reg,hi(lwa_flag) ;\
l.ori reg,reg,lo(lwa_flag) ;\
l.sw 0(reg),r0 /* * NOTE: one should never assume that SPR_EPC, SPR_ESR, SPR_EEAR * contain the same values as when exception we're handling * occured. in fact they never do. if you need them use * values saved on stack (for SPR_EPC, SPR_ESR) or content * of r4 (for SPR_EEAR). for details look at EXCEPTION_HANDLE() * in 'arch/openrisc/kernel/head.S'
*/
EXCEPTION_ENTRY(_data_page_fault_handler)
CLEAR_LWA_FLAG(r3) /* set up parameters for do_page_fault */
l.ori r5,r0,0x300 // exception vector
1:
l.addi r3,r1,0 // pt_regs /* r4 set be EXCEPTION_HANDLE */ // effective address of fault
#ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
l.lwz r6,PT_PC(r3) // address of an offending insn
l.lwz r6,0(r6) // instruction that caused pf
/* * Syscalls are a special type of exception in that they are * _explicitly_ invoked by userspace and can therefore be * held to conform to the same ABI as normal functions with * respect to whether registers are preserved across the call * or not.
*/
/* Upon syscall entry we just save the callee-saved registers * and not the call-clobbered ones.
*/
ENTRY(_sys_call_handler) /* r1, EPCR, ESR a already saved */
l.sw PT_GPR2(r1),r2 /* r3-r8 must be saved because syscall restart relies * on us being able to restart the syscall args... technically * they should be clobbered, otherwise
*/
l.sw PT_GPR3(r1),r3 /* * r4 already saved * r4 holds the EEAR address of the fault, use it as screatch reg and * then load the original r4
*/
CLEAR_LWA_FLAG(r4)
l.lwz r4,PT_GPR4(r1)
l.sw PT_GPR5(r1),r5
l.sw PT_GPR6(r1),r6
l.sw PT_GPR7(r1),r7
l.sw PT_GPR8(r1),r8
l.sw PT_GPR9(r1),r9 /* r10 already saved */
l.sw PT_GPR11(r1),r11 /* orig_gpr11 must be set for syscalls */
l.sw PT_ORIG_GPR11(r1),r11 /* r12,r13 already saved */
/* r14-r28 (even) aren't touched by the syscall fast path below * so we don't need to save them. However, the functions that return * to userspace via a call to switch() DO need to save these because * switch() effectively clobbers them... saving these registers for * such functions is handled in their syscall wrappers (see fork, vfork, * and clone, below).
/* r30 is the only register we clobber in the fast path */ /* r30 already saved */ /* l.sw PT_GPR30(r1),r30 */
_syscall_check_trace_enter: /* syscalls run with interrupts enabled */
TRACE_IRQS_ON_SYSCALL
ENABLE_INTERRUPTS(r29) // enable interrupts, r29 is temp
/* If TIF_SYSCALL_TRACE is set, then we want to do syscall tracing */
l.lwz r30,TI_FLAGS(r10)
l.andi r30,r30,_TIF_SYSCALL_TRACE
l.sfne r30,r0
l.bf _syscall_trace_enter
l.nop
_syscall_check: /* Ensure that the syscall number is reasonable */
l.sfgeui r11,__NR_syscalls
l.bf _syscall_badsys
l.nop
_syscall_return: /* All syscalls return here... just pay attention to ret_from_fork * which does it in a round-about way.
*/
l.sw PT_GPR11(r1),r11 // save return value
_syscall_check_trace_leave: /* r30 is a callee-saved register so this should still hold the * _TIF_SYSCALL_TRACE flag from _syscall_check_trace_enter above... * _syscall_trace_leave expects syscall result to be in pt_regs->r11.
*/
l.sfne r30,r0
l.bf _syscall_trace_leave
l.nop
/* This is where the exception-return code begins... interrupts need to be * disabled the rest of the way here because we can't afford to miss any
* interrupts that set NEED_RESCHED or SIGNALPENDING... really true? */
_syscall_check_work:
#ifdef CONFIG_DEBUG_RSEQ
l.jal rseq_syscall
l.ori r3,r1,0
#endif /* Here we need to disable interrupts */
DISABLE_INTERRUPTS(r27,r29)
TRACE_IRQS_OFF
l.lwz r30,TI_FLAGS(r10)
l.andi r30,r30,_TIF_WORK_MASK
l.sfne r30,r0
l.bnf _syscall_resume_userspace
l.nop
/* Work pending follows a different return path, so we need to * make sure that all the call-saved registers get into pt_regs * before branching...
*/
l.sw PT_GPR14(r1),r14
l.sw PT_GPR16(r1),r16
l.sw PT_GPR18(r1),r18
l.sw PT_GPR20(r1),r20
l.sw PT_GPR22(r1),r22
l.sw PT_GPR24(r1),r24
l.sw PT_GPR26(r1),r26
l.sw PT_GPR28(r1),r28
/* _work_pending needs to be called with interrupts disabled */
l.j _work_pending
l.nop
/* This is the hot path for returning to userspace from a syscall. If there's * work to be done and the branch to _work_pending was taken above, then the * return to userspace will be done via the normal exception return path... * that path restores _all_ registers and will overwrite the "clobbered" * registers with whatever garbage is in pt_regs -- that's OK because those * registers are clobbered anyway and because the extra work is insignificant * in the context of the extra work that _work_pending is doing.
/* Once again, syscalls are special and only guarantee to preserve the
* same registers as a normal function call */
/* The assumption here is that the registers r14-r28 (even) are untouched and * don't need to be restored... be sure that that's really the case!
*/
/* This is still too much... we should only be restoring what we actually * clobbered... we should even be using 'scratch' (odd) regs above so that * we don't need to restore anything, hardly...
*/
l.lwz r2,PT_GPR2(r1)
/* Restore args */ /* r3-r8 are technically clobbered, but syscall restart needs these * to be restored...
*/
l.lwz r3,PT_GPR3(r1)
l.lwz r4,PT_GPR4(r1)
l.lwz r5,PT_GPR5(r1)
l.lwz r6,PT_GPR6(r1)
l.lwz r7,PT_GPR7(r1)
l.lwz r8,PT_GPR8(r1)
/* r30 is the only register we clobber in the fast path */
l.lwz r30,PT_GPR30(r1)
/* Here we use r13-r19 (odd) as scratch regs */
l.lwz r13,PT_PC(r1)
l.lwz r15,PT_SR(r1)
l.lwz r1,PT_SP(r1) /* Interrupts need to be disabled for setting EPCR and ESR * so that another interrupt doesn't come in here and clobber
* them before we can use them for our l.rfe */
DISABLE_INTERRUPTS(r17,r19)
l.mtspr r0,r13,SPR_EPCR_BASE
l.mtspr r0,r15,SPR_ESR_BASE
l.rfe
/* End of hot path! * Keep the below tracing and error handling out of the hot path...
*/
_syscall_trace_enter: /* Here we pass pt_regs to do_syscall_trace_enter. Make sure * that function is really getting all the info it needs as * pt_regs isn't a complete set of userspace regs, just the * ones relevant to the syscall... * * Note use of delay slot for setting argument.
*/
l.jal do_syscall_trace_enter
l.addi r3,r1,0
/* Restore arguments (not preserved across do_syscall_trace_enter) * so that we can do the syscall for real and return to the syscall * hot path.
*/
l.lwz r11,PT_GPR11(r1)
l.lwz r3,PT_GPR3(r1)
l.lwz r4,PT_GPR4(r1)
l.lwz r5,PT_GPR5(r1)
l.lwz r6,PT_GPR6(r1)
l.lwz r7,PT_GPR7(r1)
_syscall_badsys: /* Here we effectively pretend to have executed an imaginary * syscall that returns -ENOSYS and then return to the regular * syscall hot path. * Note that "return value" is set in the delay slot...
*/
l.j _syscall_return
l.addi r11,r0,-ENOSYS
/******* END SYSCALL HANDLING *******/
/* ---[ 0xd00: Floating Point exception ]-------------------------------- */
/* Check if we are a kernel thread */
l.sfeqi r20,0
l.bf 1f
l.nop
/* ...we are a kernel thread so invoke the requested callback */
l.jalr r20
l.or r3,r22,r0
1: /* _syscall_returns expect r11 to contain return value */
l.lwz r11,PT_GPR11(r1)
/* The syscall fast path return expects call-saved registers * r14-r28 to be untouched, so we restore them here as they * will have been effectively clobbered when arriving here * via the call to switch()
*/
l.lwz r14,PT_GPR14(r1)
l.lwz r16,PT_GPR16(r1)
l.lwz r18,PT_GPR18(r1)
l.lwz r20,PT_GPR20(r1)
l.lwz r22,PT_GPR22(r1)
l.lwz r24,PT_GPR24(r1)
l.lwz r26,PT_GPR26(r1)
l.lwz r28,PT_GPR28(r1)
/* * This routine switches between two different tasks. The process * state of one is saved on its kernel stack. Then the state * of the other is restored from its kernel stack. The memory * management hardware is updated to the second process's state. * Finally, we can return to the second process, via the 'return'. * * Note: there are two ways to get to the "going out" portion * of this code; either by coming in via the entry (_switch) * or via "fork" which must set up an environment equivalent * to the "_switch" path. If you change this (or in particular, the * SAVE_REGS macro), you'll have to change the fork code also.
*/
/* _switch MUST never lay on page boundry, cause it runs from * effective addresses and beeing interrupted by iTLB miss would kill it. * dTLB miss seems to never accour in the bad place since data accesses * are from task structures which are always page aligned. * * The problem happens in RESTORE_ALL where we first set the EPCR * register, then load the previous register values and only at the end call * the l.rfe instruction. If get TLB miss in beetwen the EPCR register gets * garbled and we end up calling l.rfe with the wrong EPCR. (same probably * holds for ESR) * * To avoid this problems it is sufficient to align _switch to * some nice round number smaller than it's size...
*/
/* ABI rules apply here... we either enter _switch via schedule() or via * an imaginary call to which we shall return at return_from_fork. Either * way, we are a function call and only need to preserve the callee-saved * registers when we return. As such, we don't need to save the registers * on the stack that we won't be returning as they were...
*/
.align 0x400
ENTRY(_switch) /* We don't store SR as _switch only gets called in a context where
* the SR will be the same going in and coming out... */
/* Set up new pt_regs struct for saving task state */
l.addi r1,r1,-(INT_FRAME_SIZE)
/* No need to store r1/PT_SP as it goes into KSP below */
l.sw PT_GPR2(r1),r2
l.sw PT_GPR9(r1),r9
/* Save callee-saved registers to the new pt_regs */
l.sw PT_GPR14(r1),r14
l.sw PT_GPR16(r1),r16
l.sw PT_GPR18(r1),r18
l.sw PT_GPR20(r1),r20
l.sw PT_GPR22(r1),r22
l.sw PT_GPR24(r1),r24
l.sw PT_GPR26(r1),r26
l.sw PT_GPR28(r1),r28
l.sw PT_GPR30(r1),r30
l.addi r11,r10,0 /* Save old 'current' to 'last' return value*/
/* We use thread_info->ksp for storing the address of the above * structure so that we can get back to it later... we don't want * to lose the value of thread_info->ksp, though, so store it as * pt_regs->sp so that we can easily restore it when we are made * live again...
*/
/* Save the old value of thread_info->ksp as pt_regs->sp */
l.lwz r29,TI_KSP(r10)
l.sw PT_SP(r1),r29
/* Swap kernel stack pointers */
l.sw TI_KSP(r10),r1 /* Save old stack pointer */
l.or r10,r4,r0 /* Set up new current_thread_info */
l.lwz r1,TI_KSP(r10) /* Load new stack pointer */
/* Restore the old value of thread_info->ksp */
l.lwz r29,PT_SP(r1)
l.sw TI_KSP(r10),r29
/* ...and restore the registers, except r11 because the return value * has already been set above.
*/
l.lwz r2,PT_GPR2(r1)
l.lwz r9,PT_GPR9(r1) /* No need to restore r10 */ /* ...and do not restore r11 */
/* Unwind stack to pre-switch state */
l.addi r1,r1,(INT_FRAME_SIZE)
/* Return via the link-register back to where we 'came from', where * that may be either schedule(), ret_from_fork(), or * ret_from_kernel_thread(). If we are returning to a new thread, * we are expected to have set up the arg to schedule_tail already, * hence we do so here unconditionally:
*/
l.lwz r3,TI_TASK(r3) /* Load 'prev' as schedule_tail arg */
l.jr r9
l.nop
/* These all use the delay slot for setting the argument register, so the * jump is always happening after the l.addi instruction. * * These are all just wrappers that don't touch the link-register r9, so the * return from the "real" syscall function will return back to the syscall * code that did the l.jal that brought us here.
*/
/* fork requires that we save all the callee-saved registers because they * are all effectively clobbered by the call to _switch. Here we store * all the registers that aren't touched by the syscall fast path and thus * weren't saved there.
*/
/* This is a catch-all syscall for atomic instructions for the OpenRISC 1000. * The functions takes a variable number of parameters depending on which * particular flavour of atomic you want... parameter 1 is a flag identifying * the atomic in question. Currently, this function implements the * following variants: * * XCHG: * @flag: 1 * @ptr1: * @ptr2: * Atomically exchange the values in pointers 1 and 2. *
*/
ENTRY(sys_or1k_atomic) /* FIXME: This ignores r3 and always does an XCHG */
DISABLE_INTERRUPTS(r17,r19)
l.lwz r29,0(r4)
l.lwz r27,0(r5)
l.sw 0(r4),r27
l.sw 0(r5),r29
ENABLE_INTERRUPTS(r17)
l.jr r9
l.or r11,r0,r0