^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * OpenRISC signal.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Linux architectural port borrowing liberally from similar works of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * others. All original copyrights apply as per the original source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * declaration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Modifications for the OpenRISC architecture:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
^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) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/tracehook.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <asm/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <asm/ucontext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define DEBUG_SIG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct rt_sigframe {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct siginfo info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct ucontext uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned char retcode[16]; /* trampoline code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int restore_sigcontext(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct sigcontext __user *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Always make any pending restarted system calls return -EINTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) current->restart_block.fn = do_no_restart_syscall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Restore the regs from &sc->regs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * (sc is already checked since the sigframe was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * checked in sys_sigreturn previously)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) err |= __copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) err |= __copy_from_user(®s->pc, &sc->regs.pc, sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) err |= __copy_from_user(®s->sr, &sc->regs.sr, sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* make sure the SM-bit is cleared so user-mode cannot fool us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) regs->sr &= ~SPR_SR_SM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) regs->orig_gpr11 = -1; /* Avoid syscall restart checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* TODO: the other ports use regs->orig_XX to disable syscall checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * after this completes, but we don't use that mechanism. maybe we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * use it now ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) sigset_t set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * Since we stacked the signal on a dword boundary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * then frame should be dword aligned here. If it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * not, then the user is trying to mess with us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (((unsigned long)frame) & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!access_ok(frame, sizeof(*frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^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) if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (restore_altstack(&frame->uc.uc_stack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) goto badframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return regs->gpr[11];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) badframe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) force_sig(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Set up a signal frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* copy the regs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* There should be no need to save callee-saved registers here...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * ...but we save them anyway. Revisit this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) err |= __copy_to_user(&sc->regs.pc, ®s->pc, sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) err |= __copy_to_user(&sc->regs.sr, ®s->sr, sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static inline unsigned long align_sigframe(unsigned long sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return sp & ~3UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * Work out where the signal frame should go. It's either on the user stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * or the alternate stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static inline void __user *get_sigframe(struct ksignal *ksig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct pt_regs *regs, size_t frame_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) unsigned long sp = regs->sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* redzone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) sp -= STACK_FRAME_OVERHEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) sp = sigsp(sp, ksig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) sp = align_sigframe(sp - frame_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return (void __user *)sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* grab and setup a signal frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * basically we stack a lot of state info, and arrange for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * user-mode program to return to the kernel using either a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * trampoline which performs the syscall sigreturn, or a provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * user-mode trampoline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct rt_sigframe __user *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) unsigned long return_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) frame = get_sigframe(ksig, regs, sizeof(*frame));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!access_ok(frame, sizeof(*frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Create siginfo. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (ksig->ka.sa.sa_flags & SA_SIGINFO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) err |= copy_siginfo_to_user(&frame->info, &ksig->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Create the ucontext. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) err |= __put_user(0, &frame->uc.uc_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) err |= __put_user(NULL, &frame->uc.uc_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* trampoline - the desired return ip is the retcode itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return_ip = (unsigned long)&frame->retcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* This is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) l.ori r11,r0,__NR_sigreturn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) l.sys 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) err |= __put_user(0xa960, (short __user *)(frame->retcode + 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err |= __put_user(__NR_rt_sigreturn, (short __user *)(frame->retcode + 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) err |= __put_user(0x20000001, (unsigned long __user *)(frame->retcode + 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) err |= __put_user(0x15000000, (unsigned long __user *)(frame->retcode + 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* Set up registers for signal handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) regs->pc = (unsigned long)ksig->ka.sa.sa_handler; /* what we enter NOW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) regs->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) regs->gpr[3] = (unsigned long)ksig->sig; /* arg 1: signo */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) regs->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) regs->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* actually move the usp to reflect the stacked frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) regs->sp = (unsigned long)frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) handle_signal(struct ksignal *ksig, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * Note that 'init' is a special process: it doesn't get signals it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * want to handle. Thus you cannot kill init even with a SIGKILL even by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * mistake.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * Also note that the regs structure given here as an argument, is the latest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * pushed pt_regs. It may or may not be the same as the first pushed registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * when the initial usermode->kernelmode transition took place. Therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * we can use user_mode(regs) to see if we came directly from kernel or user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * mode below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int do_signal(struct pt_regs *regs, int syscall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct ksignal ksig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) unsigned long continue_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned long restart_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) unsigned long retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int restart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (syscall) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) continue_addr = regs->pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) restart_addr = continue_addr - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) retval = regs->gpr[11];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * Setup syscall restart here so that a debugger will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * see the already changed PC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) switch (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) case -ERESTART_RESTARTBLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) restart = -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) case -ERESTARTNOHAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) case -ERESTARTNOINTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) restart++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) regs->gpr[11] = regs->orig_gpr11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) regs->pc = restart_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * Get the signal to deliver. During the call to get_signal the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * debugger may change all our registers so we may need to revert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * the decision to restart the syscall; specifically, if the PC is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * changed, don't restart the syscall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (get_signal(&ksig)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (unlikely(restart) && regs->pc == restart_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (retval == -ERESTARTNOHAND ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) retval == -ERESTART_RESTARTBLOCK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) || (retval == -ERESTARTSYS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /* No automatic restart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) regs->gpr[11] = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) regs->pc = continue_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) handle_signal(&ksig, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* no handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) restore_saved_sigmask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * Restore pt_regs PC as syscall restart will be handled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * kernel without return to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (unlikely(restart) && regs->pc == restart_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) regs->pc = continue_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) asmlinkage int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (likely(thread_flags & _TIF_NEED_RESCHED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (unlikely(!user_mode(regs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (thread_flags & _TIF_SIGPENDING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int restart = do_signal(regs, syscall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (unlikely(restart)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * Restart without handlers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * Deal with it without leaving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * the kernel space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) syscall = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) tracehook_notify_resume(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) thread_flags = current_thread_info()->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) } while (thread_flags & _TIF_WORK_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }