^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright (C) 2005-2017 Andes Technology Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/linkage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/assembler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/nds32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/asm-offsets.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <asm/thread_info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/fpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #ifdef CONFIG_HWZOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) .macro pop_zol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) mtusr $r14, $LB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) mtusr $r15, $LE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) mtusr $r16, $LC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .macro restore_user_regs_first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) setgie.d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) isb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #if defined(CONFIG_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) addi $sp, $sp, OSP_OFFSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) lmw.adm $r12, [$sp], $r25, #0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) sethi $p0, hi20(has_fpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) lbsi $p0, [$p0+lo12(has_fpu)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) beqz $p0, 2f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) mtsr $r25, $FUCOP_CTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) addi $sp, $sp, FUCOP_CTL_OFFSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) lmw.adm $r12, [$sp], $r24, #0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) mtsr $r12, $SP_USR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) mtsr $r13, $IPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #ifdef CONFIG_HWZOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) pop_zol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) mtsr $r19, $PSW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) mtsr $r20, $IPSW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) mtsr $r21, $P_IPSW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) mtsr $r22, $P_IPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) mtsr $r23, $P_P0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) mtsr $r24, $P_P1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) lmw.adm $sp, [$sp], $sp, #0xe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .macro restore_user_regs_last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pop $p0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) cmovn $sp, $p0, $p0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) iret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) nop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .macro restore_user_regs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) restore_user_regs_first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) lmw.adm $r0, [$sp], $r25, #0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) addi $sp, $sp, OSP_OFFSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) restore_user_regs_last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .macro fast_restore_user_regs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) restore_user_regs_first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) lmw.adm $r1, [$sp], $r25, #0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) addi $sp, $sp, OSP_OFFSET-4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) restore_user_regs_last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #ifdef CONFIG_PREEMPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .macro preempt_stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .macro preempt_stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) setgie.d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) isb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define resume_kernel no_work_pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ENTRY(ret_from_exception)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) preempt_stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ENTRY(ret_from_intr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * judge Kernel or user mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) lwi $p0, [$sp+(#IPSW_OFFSET)] ! Check if in nested interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) andi $p0, $p0, #PSW_mskINTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) bnez $p0, resume_kernel ! done with iret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) j resume_userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * This is the fast syscall return path. We do as little as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * possible here, and this includes saving $r0 back into the SVC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * fixed: tsk - $r25, syscall # - $r7, syscall table pointer - $r8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ENTRY(ret_fast_syscall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) gie_disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) lwi $r1, [tsk+#TSK_TI_FLAGS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) andi $p1, $r1, #_TIF_WORK_MASK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) bnez $p1, fast_work_pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) fast_restore_user_regs ! iret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Ok, we need to do extra processing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * enter the slow path returning from syscall, while pending work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fast_work_pending:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) swi $r0, [$sp+(#R0_OFFSET)] ! what is different from ret_from_exception
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) work_pending:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) andi $p1, $r1, #_TIF_NEED_RESCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) bnez $p1, work_resched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) andi $p1, $r1, #_TIF_SIGPENDING|#_TIF_NOTIFY_RESUME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) beqz $p1, no_work_pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) move $r0, $sp ! 'regs'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) gie_enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bal do_notify_resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) b ret_slow_syscall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) work_resched:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) bal schedule ! path, return to user mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * "slow" syscall return path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ENTRY(resume_userspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ENTRY(ret_slow_syscall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) gie_disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) lwi $p0, [$sp+(#IPSW_OFFSET)] ! Check if in nested interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) andi $p0, $p0, #PSW_mskINTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) bnez $p0, no_work_pending ! done with iret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) lwi $r1, [tsk+#TSK_TI_FLAGS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) andi $p1, $r1, #_TIF_WORK_MASK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) bnez $p1, work_pending ! handle work_resched, sig_pend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) no_work_pending:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #ifdef CONFIG_TRACE_IRQFLAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) lwi $p0, [$sp+(#IPSW_OFFSET)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) andi $p0, $p0, #0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) la $r10, __trace_hardirqs_off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) la $r9, __trace_hardirqs_on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) cmovz $r9, $p0, $r10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) jral $r9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) restore_user_regs ! return from iret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * preemptive kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #ifdef CONFIG_PREEMPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) resume_kernel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) gie_disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) lwi $t0, [tsk+#TSK_TI_PREEMPT]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) bnez $t0, no_work_pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) lwi $t0, [tsk+#TSK_TI_FLAGS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) andi $p1, $t0, #_TIF_NEED_RESCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) beqz $p1, no_work_pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) lwi $t0, [$sp+(#IPSW_OFFSET)] ! Interrupts off?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) andi $t0, $t0, #1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) beqz $t0, no_work_pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) jal preempt_schedule_irq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) b no_work_pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * This is how we return from a fork.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ENTRY(ret_from_fork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) bal schedule_tail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) beqz $r6, 1f ! r6 stores fn for kernel thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) move $r0, $r7 ! prepare kernel thread arg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) jral $r6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) lwi $r1, [tsk+#TSK_TI_FLAGS] ! check for syscall tracing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) andi $p1, $r1, #_TIF_WORK_SYSCALL_LEAVE ! are we tracing syscalls?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) beqz $p1, ret_slow_syscall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) move $r0, $sp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) bal syscall_trace_leave
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) b ret_slow_syscall