^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/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/personality.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/tracehook.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/ucontext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/fpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/vdso.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct rt_sigframe {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct siginfo info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct ucontext uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #if IS_ENABLED(CONFIG_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static inline int restore_sigcontext_fpu(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct sigcontext __user *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long used_math_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) clear_used_math();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) __get_user_error(used_math_flag, &sc->used_math_flag, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!used_math_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) set_used_math();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #if IS_ENABLED(CONFIG_LAZY_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (current == last_task_used_math) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) last_task_used_math = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) disable_ptreg_fpu(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) clear_fpu(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return __copy_from_user(&tsk->thread.fpu, &sc->fpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sizeof(struct fpu_struct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static inline int setup_sigcontext_fpu(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct sigcontext __user *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) __put_user_error(used_math(), &sc->used_math_flag, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!used_math())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #if IS_ENABLED(CONFIG_LAZY_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (last_task_used_math == tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) save_fpu(last_task_used_math);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unlazy_fpu(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ret = __copy_to_user(&sc->fpu, &tsk->thread.fpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) sizeof(struct fpu_struct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int restore_sigframe(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct rt_sigframe __user * sf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) sigset_t set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) set_current_blocked(&set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) __get_user_error(regs->uregs[0], &sf->uc.uc_mcontext.nds32_r0, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) __get_user_error(regs->uregs[1], &sf->uc.uc_mcontext.nds32_r1, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) __get_user_error(regs->uregs[2], &sf->uc.uc_mcontext.nds32_r2, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) __get_user_error(regs->uregs[3], &sf->uc.uc_mcontext.nds32_r3, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __get_user_error(regs->uregs[4], &sf->uc.uc_mcontext.nds32_r4, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) __get_user_error(regs->uregs[5], &sf->uc.uc_mcontext.nds32_r5, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) __get_user_error(regs->uregs[6], &sf->uc.uc_mcontext.nds32_r6, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) __get_user_error(regs->uregs[7], &sf->uc.uc_mcontext.nds32_r7, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) __get_user_error(regs->uregs[8], &sf->uc.uc_mcontext.nds32_r8, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) __get_user_error(regs->uregs[9], &sf->uc.uc_mcontext.nds32_r9, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __get_user_error(regs->uregs[10], &sf->uc.uc_mcontext.nds32_r10, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __get_user_error(regs->uregs[11], &sf->uc.uc_mcontext.nds32_r11, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __get_user_error(regs->uregs[12], &sf->uc.uc_mcontext.nds32_r12, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) __get_user_error(regs->uregs[13], &sf->uc.uc_mcontext.nds32_r13, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __get_user_error(regs->uregs[14], &sf->uc.uc_mcontext.nds32_r14, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __get_user_error(regs->uregs[15], &sf->uc.uc_mcontext.nds32_r15, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) __get_user_error(regs->uregs[16], &sf->uc.uc_mcontext.nds32_r16, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) __get_user_error(regs->uregs[17], &sf->uc.uc_mcontext.nds32_r17, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) __get_user_error(regs->uregs[18], &sf->uc.uc_mcontext.nds32_r18, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) __get_user_error(regs->uregs[19], &sf->uc.uc_mcontext.nds32_r19, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) __get_user_error(regs->uregs[20], &sf->uc.uc_mcontext.nds32_r20, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __get_user_error(regs->uregs[21], &sf->uc.uc_mcontext.nds32_r21, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) __get_user_error(regs->uregs[22], &sf->uc.uc_mcontext.nds32_r22, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) __get_user_error(regs->uregs[23], &sf->uc.uc_mcontext.nds32_r23, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) __get_user_error(regs->uregs[24], &sf->uc.uc_mcontext.nds32_r24, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) __get_user_error(regs->uregs[25], &sf->uc.uc_mcontext.nds32_r25, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) __get_user_error(regs->fp, &sf->uc.uc_mcontext.nds32_fp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) __get_user_error(regs->gp, &sf->uc.uc_mcontext.nds32_gp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) __get_user_error(regs->lp, &sf->uc.uc_mcontext.nds32_lp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) __get_user_error(regs->sp, &sf->uc.uc_mcontext.nds32_sp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) __get_user_error(regs->ipc, &sf->uc.uc_mcontext.nds32_ipc, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #if defined(CONFIG_HWZOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) __get_user_error(regs->lc, &sf->uc.uc_mcontext.zol.nds32_lc, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) __get_user_error(regs->le, &sf->uc.uc_mcontext.zol.nds32_le, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) __get_user_error(regs->lb, &sf->uc.uc_mcontext.zol.nds32_lb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #if IS_ENABLED(CONFIG_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) err |= restore_sigcontext_fpu(regs, &sf->uc.uc_mcontext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * Avoid sys_rt_sigreturn() restarting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) forget_syscall(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct rt_sigframe __user *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Always make any pending restarted system calls return -EINTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) current->restart_block.fn = do_no_restart_syscall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Since we stacked the signal on a 64-bit boundary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * then 'sp' should be two-word aligned here. If it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * not, then the user is trying to mess with us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (regs->sp & 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) frame = (struct rt_sigframe __user *)regs->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!access_ok(frame, sizeof(*frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (restore_sigframe(regs, frame))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (restore_altstack(&frame->uc.uc_stack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return regs->uregs[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) badframe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) force_sig(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) setup_sigframe(struct rt_sigframe __user * sf, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sigset_t * set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) __put_user_error(regs->uregs[0], &sf->uc.uc_mcontext.nds32_r0, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) __put_user_error(regs->uregs[1], &sf->uc.uc_mcontext.nds32_r1, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) __put_user_error(regs->uregs[2], &sf->uc.uc_mcontext.nds32_r2, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) __put_user_error(regs->uregs[3], &sf->uc.uc_mcontext.nds32_r3, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) __put_user_error(regs->uregs[4], &sf->uc.uc_mcontext.nds32_r4, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) __put_user_error(regs->uregs[5], &sf->uc.uc_mcontext.nds32_r5, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) __put_user_error(regs->uregs[6], &sf->uc.uc_mcontext.nds32_r6, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) __put_user_error(regs->uregs[7], &sf->uc.uc_mcontext.nds32_r7, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) __put_user_error(regs->uregs[8], &sf->uc.uc_mcontext.nds32_r8, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) __put_user_error(regs->uregs[9], &sf->uc.uc_mcontext.nds32_r9, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) __put_user_error(regs->uregs[10], &sf->uc.uc_mcontext.nds32_r10, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) __put_user_error(regs->uregs[11], &sf->uc.uc_mcontext.nds32_r11, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) __put_user_error(regs->uregs[12], &sf->uc.uc_mcontext.nds32_r12, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) __put_user_error(regs->uregs[13], &sf->uc.uc_mcontext.nds32_r13, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) __put_user_error(regs->uregs[14], &sf->uc.uc_mcontext.nds32_r14, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) __put_user_error(regs->uregs[15], &sf->uc.uc_mcontext.nds32_r15, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) __put_user_error(regs->uregs[16], &sf->uc.uc_mcontext.nds32_r16, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) __put_user_error(regs->uregs[17], &sf->uc.uc_mcontext.nds32_r17, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) __put_user_error(regs->uregs[18], &sf->uc.uc_mcontext.nds32_r18, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) __put_user_error(regs->uregs[19], &sf->uc.uc_mcontext.nds32_r19, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) __put_user_error(regs->uregs[20], &sf->uc.uc_mcontext.nds32_r20, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) __put_user_error(regs->uregs[21], &sf->uc.uc_mcontext.nds32_r21, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) __put_user_error(regs->uregs[22], &sf->uc.uc_mcontext.nds32_r22, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) __put_user_error(regs->uregs[23], &sf->uc.uc_mcontext.nds32_r23, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) __put_user_error(regs->uregs[24], &sf->uc.uc_mcontext.nds32_r24, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) __put_user_error(regs->uregs[25], &sf->uc.uc_mcontext.nds32_r25, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) __put_user_error(regs->fp, &sf->uc.uc_mcontext.nds32_fp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) __put_user_error(regs->gp, &sf->uc.uc_mcontext.nds32_gp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) __put_user_error(regs->lp, &sf->uc.uc_mcontext.nds32_lp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) __put_user_error(regs->sp, &sf->uc.uc_mcontext.nds32_sp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) __put_user_error(regs->ipc, &sf->uc.uc_mcontext.nds32_ipc, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #if defined(CONFIG_HWZOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) __put_user_error(regs->lc, &sf->uc.uc_mcontext.zol.nds32_lc, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) __put_user_error(regs->le, &sf->uc.uc_mcontext.zol.nds32_le, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) __put_user_error(regs->lb, &sf->uc.uc_mcontext.zol.nds32_lb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #if IS_ENABLED(CONFIG_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) err |= setup_sigcontext_fpu(regs, &sf->uc.uc_mcontext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) __put_user_error(current->thread.error_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) &sf->uc.uc_mcontext.error_code, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) __put_user_error(current->thread.address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) &sf->uc.uc_mcontext.fault_address, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static inline void __user *get_sigframe(struct ksignal *ksig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct pt_regs *regs, int framesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) unsigned long sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Default to using normal stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) sp = regs->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * If we are on the alternate signal stack and would overflow it, don't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * Return an always-bogus address instead so we will die with SIGSEGV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return (void __user __force *)(-1UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /* This is the X/Open sanctioned signal stack switching. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) sp = (sigsp(sp, ksig) - framesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * nds32 mandates 8-byte alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) sp &= ~0x7UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return (void __user *)sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) setup_return(struct pt_regs *regs, struct ksignal *ksig, void __user * frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) unsigned long retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) retcode = VDSO_SYMBOL(current->mm->context.vdso, rt_sigtramp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) regs->uregs[0] = ksig->sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) regs->sp = (unsigned long)frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) regs->lp = retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) regs->ipc = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) setup_rt_frame(struct ksignal *ksig, sigset_t * set, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct rt_sigframe __user *frame =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) get_sigframe(ksig, regs, sizeof(*frame));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!access_ok(frame, sizeof(*frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) __put_user_error(0, &frame->uc.uc_flags, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) __put_user_error(NULL, &frame->uc.uc_link, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) err |= setup_sigframe(frame, regs, set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) setup_return(regs, ksig, frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) err |= copy_siginfo_to_user(&frame->info, &ksig->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) regs->uregs[1] = (unsigned long)&frame->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) regs->uregs[2] = (unsigned long)&frame->uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * OK, we're invoking a handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) sigset_t *oldset = sigmask_to_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (in_syscall(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Avoid additional syscall restarting via ret_slow_syscall. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) forget_syscall(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) switch (regs->uregs[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) case -ERESTART_RESTARTBLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) case -ERESTARTNOHAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) regs->uregs[0] = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) regs->uregs[0] = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) case -ERESTARTNOINTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) regs->uregs[0] = regs->orig_r0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) regs->ipc -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * Set up the stack frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ret = setup_rt_frame(ksig, oldset, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) signal_setup_done(ret, ksig, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * Note that 'init' is a special process: it doesn't get signals it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * want to handle. Thus you cannot kill init even with a SIGKILL even by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * mistake.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * Note that we go through the signals twice: once to check the signals that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * the kernel can handle, and then we build all the user-level signal handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * stack-frames in one go after that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static void do_signal(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct ksignal ksig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (get_signal(&ksig)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) handle_signal(&ksig, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * If we were from a system call, check for system call restarting...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (in_syscall(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* Restart the system call - no handlers present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* Avoid additional syscall restarting via ret_slow_syscall. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) forget_syscall(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) switch (regs->uregs[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) case -ERESTART_RESTARTBLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) regs->uregs[15] = __NR_restart_syscall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) case -ERESTARTNOHAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) case -ERESTARTNOINTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) regs->uregs[0] = regs->orig_r0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) regs->ipc -= 0x4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) restore_saved_sigmask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) asmlinkage void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) do_notify_resume(struct pt_regs *regs, unsigned int thread_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (thread_flags & _TIF_SIGPENDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) do_signal(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (thread_flags & _TIF_NOTIFY_RESUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) tracehook_notify_resume(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }