Diff below moves the various sigexit() from all MD sendsig() to the MI
trapsignal().  Apart from the obvious code simplification, this will
help with locking as sigexit() does not return.

ok?

Index: arch/alpha/alpha/machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/alpha/alpha/machdep.c,v
retrieving revision 1.193
diff -u -p -r1.193 machdep.c
--- arch/alpha/alpha/machdep.c  26 Aug 2020 03:29:05 -0000      1.193
+++ arch/alpha/alpha/machdep.c  15 Sep 2020 08:34:45 -0000
@@ -1381,7 +1381,7 @@ regdump(framep)
 /*
  * Send an interrupt to process.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -1443,20 +1443,13 @@ sendsig(sig_t catcher, int sig, sigset_t
        if (psp->ps_siginfo & sigmask(sig)) {
                sip = (void *)scp + kscsize;
                if (copyout(ksip, (caddr_t)sip, fsize - kscsize) != 0)
-                       goto trash;
+                       return 1;
        } else
                sip = NULL;
 
        ksc.sc_cookie = (long)scp ^ p->p_p->ps_sigcookie;
-       if (copyout((caddr_t)&ksc, (caddr_t)scp, kscsize) != 0) {
-trash:
-               /*
-                * Process has trashed its stack; give it an illegal
-                * instruction to halt it in its tracks.
-                */
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
-       }
+       if (copyout((caddr_t)&ksc, (caddr_t)scp, kscsize) != 0)
+               return 1;
 
        /*
         * Set up the registers to return to sigcode.
@@ -1467,6 +1460,8 @@ trash:
        frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
        frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;         /* t12 is pv */
        alpha_pal_wrusp((unsigned long)scp);
+
+       return 0;
 }
 
 /*
Index: arch/amd64/amd64/machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/amd64/amd64/machdep.c,v
retrieving revision 1.269
diff -u -p -r1.269 machdep.c
--- arch/amd64/amd64/machdep.c  20 Aug 2020 15:12:35 -0000      1.269
+++ arch/amd64/amd64/machdep.c  15 Sep 2020 08:35:30 -0000
@@ -566,7 +566,7 @@ cpu_sysctl(int *name, u_int namelen, voi
  * signal mask, the stack, and the frame pointer, it returns to the
  * user specified pc.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -618,7 +618,7 @@ sendsig(sig_t catcher, int sig, sigset_t
        sp -= fpu_save_len;
        ksc.sc_fpstate = (struct fxsave64 *)sp;
        if (copyout(sfp, (void *)sp, fpu_save_len))
-               sigexit(p, SIGILL);
+               return 1;
 
        /* Now reset the FPU state in PCB */
        memcpy(&p->p_addr->u_pcb.pcb_savefpu,
@@ -630,13 +630,13 @@ sendsig(sig_t catcher, int sig, sigset_t
                sss += (sizeof(*ksip) + 15) & ~15;
 
                if (copyout(ksip, (void *)sip, sizeof(*ksip)))
-                       sigexit(p, SIGILL);
+                       return 1;
        }
        scp = sp - sss;
 
        ksc.sc_cookie = (long)scp ^ p->p_p->ps_sigcookie;
        if (copyout(&ksc, (void *)scp, sizeof(ksc)))
-               sigexit(p, SIGILL);
+               return 1;
 
        /*
         * Build context to run handler in.
@@ -654,6 +654,8 @@ sendsig(sig_t catcher, int sig, sigset_t
 
        /* The reset state _is_ the userspace state for this thread now */
        curcpu()->ci_flags |= CPUF_USERXSTATE;
+
+       return 0;
 }
 
 /*
Index: arch/arm/arm/sig_machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/arm/arm/sig_machdep.c,v
retrieving revision 1.18
diff -u -p -r1.18 sig_machdep.c
--- arch/arm/arm/sig_machdep.c  10 Jul 2018 04:19:59 -0000      1.18
+++ arch/arm/arm/sig_machdep.c  15 Sep 2020 08:36:11 -0000
@@ -74,7 +74,7 @@ process_frame(struct proc *p)
  * signal mask, the stack, and the frame pointer, it returns to the
  * user specified pc.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -145,14 +145,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        }
 
        frame.sf_sc.sc_cookie = (long)&fp->sf_sc ^ p->p_p->ps_sigcookie;
-       if (copyout(&frame, fp, sizeof(frame)) != 0) {
-               /*
-                * Process has trashed its stack; give it an illegal
-                * instruction to halt it in its tracks.
-                */
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
-       }
+       if (copyout(&frame, fp, sizeof(frame)) != 0)
+               return 1;
 
        /*
         * Build context to run handler in.  We invoke the handler
@@ -163,8 +157,10 @@ sendsig(sig_t catcher, int sig, sigset_t
        tf->tf_r2 = (register_t)frame.sf_scp;
        tf->tf_pc = (register_t)frame.sf_handler;
        tf->tf_usr_sp = (register_t)fp;
-       
+
        tf->tf_usr_lr = p->p_p->ps_sigcode;
+
+       return 0;
 }
 
 /*
Index: arch/arm64/arm64/sig_machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/arm64/arm64/sig_machdep.c,v
retrieving revision 1.6
diff -u -p -r1.6 sig_machdep.c
--- arch/arm64/arm64/sig_machdep.c      10 Jul 2018 04:19:59 -0000      1.6
+++ arch/arm64/arm64/sig_machdep.c      15 Sep 2020 08:36:37 -0000
@@ -94,7 +94,7 @@ process_frame(struct proc *p)
  * signal mask, the stack, and the frame pointer, it returns to the
  * user specified pc.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -143,14 +143,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        }
 
        frame.sf_sc.sc_cookie = (long)&fp->sf_sc ^ p->p_p->ps_sigcookie;
-       if (copyout(&frame, fp, sizeof(frame)) != 0) {
-               /*
-                * Process has trashed its stack; give it an illegal
-                * instruction to halt it in its tracks.
-                */
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
-       }
+       if (copyout(&frame, fp, sizeof(frame)) != 0)
+               return 1;
 
        /*
         * Build context to run handler in.  We invoke the handler
@@ -163,6 +157,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        tf->tf_sp = (register_t)fp;
 
        tf->tf_elr = p->p_p->ps_sigcode;
+
+       return 0;
 }
 
 /*
Index: arch/hppa/hppa/machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/hppa/hppa/machdep.c,v
retrieving revision 1.260
diff -u -p -r1.260 machdep.c
--- arch/hppa/hppa/machdep.c    14 Jun 2020 20:29:13 -0000      1.260
+++ arch/hppa/hppa/machdep.c    15 Sep 2020 08:37:03 -0000
@@ -1201,7 +1201,7 @@ setregs(struct proc *p, struct exec_pack
 /*
  * Send an interrupt to process.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -1273,7 +1273,7 @@ sendsig(sig_t catcher, int sig, sigset_t
            sizeof(ksc.sc_fpregs));
 
        if (setstack(tf, scp + sss, tf->tf_r3))
-               sigexit(p, SIGILL);
+               return 1;
 
        tf->tf_arg0 = sig;
        tf->tf_arg1 = sip;
@@ -1287,12 +1287,14 @@ sendsig(sig_t catcher, int sig, sigset_t
 
        ksc.sc_cookie = (long)scp ^ p->p_p->ps_sigcookie;
        if (copyout(&ksc, (void *)scp, sizeof(ksc)))
-               sigexit(p, SIGILL);
+               return 1;
 
        if (sip) {
                if (copyout(ksip, (void *)sip, sizeof *ksip))
-                       sigexit(p, SIGILL);
+                       return 1;
        }
+
+       return 0;
 }
 
 int
Index: arch/i386/i386/machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/i386/i386/machdep.c,v
retrieving revision 1.639
diff -u -p -r1.639 machdep.c
--- arch/i386/i386/machdep.c    13 Sep 2020 05:57:28 -0000      1.639
+++ arch/i386/i386/machdep.c    15 Sep 2020 08:37:35 -0000
@@ -2443,7 +2443,7 @@ pentium_cpuspeed(int *freq)
  * frame pointer, it returns to the user
  * specified pc, psl.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -2475,7 +2475,7 @@ sendsig(sig_t catcher, int sig, sigset_t
                frame.sf_sc.sc_fpstate = (void *)sp;
                if (copyout(&p->p_addr->u_pcb.pcb_savefpu,
                    (void *)sp, sizeof(union savefpu)))
-                       sigexit(p, SIGILL);
+                       return 1;
 
                /* Signal handlers get a completely clean FP state */
                p->p_md.md_flags &= ~MDP_USEDFPU;
@@ -2516,14 +2516,8 @@ sendsig(sig_t catcher, int sig, sigset_t
 
        /* XXX don't copyout siginfo if not needed? */
        frame.sf_sc.sc_cookie = (long)&fp->sf_sc ^ p->p_p->ps_sigcookie;
-       if (copyout(&frame, fp, sizeof(frame)) != 0) {
-               /*
-                * Process has trashed its stack; give it an illegal
-                * instruction to halt it in its tracks.
-                */
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
-       }
+       if (copyout(&frame, fp, sizeof(frame)) != 0)
+               return 1;
 
        /*
         * Build context to run handler in.
@@ -2537,6 +2531,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        tf->tf_eflags &= ~(PSL_T|PSL_D|PSL_VM|PSL_AC);
        tf->tf_esp = (int)fp;
        tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
+
+       return 0;
 }
 
 /*
Index: arch/m88k/m88k/sig_machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/m88k/m88k/sig_machdep.c,v
retrieving revision 1.29
diff -u -p -r1.29 sig_machdep.c
--- arch/m88k/m88k/sig_machdep.c        10 Jul 2018 04:19:59 -0000      1.29
+++ arch/m88k/m88k/sig_machdep.c        15 Sep 2020 08:38:02 -0000
@@ -103,7 +103,7 @@ pid_t sigpid = 0;
 /*
  * Send an interrupt to process.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -152,14 +152,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        bcopy((const void *)&tf->tf_regs, (void *)&sf.sf_sc.sc_regs,
            sizeof(sf.sf_sc.sc_regs));
 
-       if (copyout((caddr_t)&sf, (caddr_t)fp, fsize)) {
-               /*
-                * Process has trashed its stack; give it an illegal
-                * instruction to halt it in its tracks.
-                */
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
-       }
+       if (copyout((caddr_t)&sf, (caddr_t)fp, fsize))
+               return 1;
 
        /*
         * Set up registers for the signal handler invocation.
@@ -186,6 +180,8 @@ sendsig(sig_t catcher, int sig, sigset_t
            ((sigdebug & SDB_KSTACK) && p->p_p->ps_pid == sigpid))
                printf("sendsig(%d): sig %d returns\n", p->p_p->ps_pid, sig);
 #endif
+
+       return 0;
 }
 
 /*
Index: arch/macppc/macppc/machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/macppc/macppc/machdep.c,v
retrieving revision 1.192
diff -u -p -r1.192 machdep.c
--- arch/macppc/macppc/machdep.c        5 Jun 2020 14:25:05 -0000       1.192
+++ arch/macppc/macppc/machdep.c        15 Sep 2020 08:38:26 -0000
@@ -442,7 +442,7 @@ setregs(struct proc *p, struct exec_pack
 /*
  * Send a signal to process.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -480,7 +480,7 @@ sendsig(sig_t catcher, int sig, sigset_t
        }
        frame.sf_sc.sc_cookie = (long)&fp->sf_sc ^ p->p_p->ps_sigcookie;
        if (copyout(&frame, fp, sizeof frame) != 0)
-               sigexit(p, SIGILL);
+               return 1;
 
        tf->fixreg[1] = (int)fp;
        tf->lr = (int)catcher;
@@ -494,6 +494,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        syncicache(pa, (p->p_p->ps_emul->e_esigcode -
            p->p_p->ps_emul->e_sigcode));
 #endif
+
+       return 0;
 }
 
 /*
Index: arch/mips64/mips64/sendsig.c
===================================================================
RCS file: /cvs/src/sys/arch/mips64/mips64/sendsig.c,v
retrieving revision 1.33
diff -u -p -r1.33 sendsig.c
--- arch/mips64/mips64/sendsig.c        2 Aug 2019 07:41:57 -0000       1.33
+++ arch/mips64/mips64/sendsig.c        15 Sep 2020 08:38:58 -0000
@@ -91,7 +91,7 @@ struct sigframe {
 /*
  * Send an interrupt to process.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct cpu_info *ci = curcpu();
@@ -139,19 +139,13 @@ sendsig(sig_t catcher, int sig, sigset_t
 
        if (psp->ps_siginfo & sigmask(sig)) {
                if (copyout(ksip, (caddr_t)&fp->sf_si, sizeof *ksip))
-                       goto bail;
+                       return 1;
        }
 
        ksc.sc_cookie = (long)&fp->sf_sc ^ p->p_p->ps_sigcookie;
-       if (copyout((caddr_t)&ksc, (caddr_t)&fp->sf_sc, sizeof(ksc))) {
-bail:
-               /*
-                * Process has trashed its stack; give it an illegal
-                * instruction to halt it in its tracks.
-                */
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
-       }
+       if (copyout((caddr_t)&ksc, (caddr_t)&fp->sf_sc, sizeof(ksc)))
+               return 1;
+
        /*
         * Build the argument list for the signal handler.
         */
@@ -165,6 +159,8 @@ bail:
        regs->sp = (register_t)fp;
 
        regs->ra = p->p_p->ps_sigcode;
+
+       return 0;
 }
 
 /*
Index: arch/sh/sh/sh_machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/sh/sh/sh_machdep.c,v
retrieving revision 1.51
diff -u -p -r1.51 sh_machdep.c
--- arch/sh/sh/sh_machdep.c     16 May 2020 14:44:45 -0000      1.51
+++ arch/sh/sh/sh_machdep.c     15 Sep 2020 08:39:17 -0000
@@ -446,7 +446,7 @@ struct sigframe {
 /*
  * Send an interrupt to process.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -485,14 +485,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        frame.sf_uc.sc_mask = mask;
 
        frame.sf_uc.sc_cookie = (long)&fp->sf_uc ^ p->p_p->ps_sigcookie;
-       if (copyout(&frame, fp, sizeof(frame)) != 0) {
-               /*
-                * Process has trashed its stack; give it an illegal
-                * instruction to halt it in its tracks.
-                */
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
-       }
+       if (copyout(&frame, fp, sizeof(frame)) != 0)
+               return 1;
 
        tf->tf_r4 = sig;                /* "signum" argument for handler */
        tf->tf_r5 = (int)sip;           /* "sip" argument for handler */
@@ -500,6 +494,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        tf->tf_spc = (int)catcher;
        tf->tf_r15 = (int)fp;
        tf->tf_pr = (int)p->p_p->ps_sigcode;
+
+       return 0;
 }
 
 /*
Index: arch/powerpc64/powerpc64/machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/powerpc64/powerpc64/machdep.c,v
retrieving revision 1.61
diff -u -p -r1.61 machdep.c
--- arch/powerpc64/powerpc64/machdep.c  15 Sep 2020 07:47:24 -0000      1.61
+++ arch/powerpc64/powerpc64/machdep.c  15 Sep 2020 10:42:49 -0000
@@ -745,7 +745,7 @@ setregs(struct proc *p, struct exec_pack
        pcb->pcb_flags = 0;
 }
 
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -806,7 +806,7 @@ sendsig(sig_t catcher, int sig, sigset_t
 
        frame.sf_sc.sc_cookie = (long)&fp->sf_sc ^ p->p_p->ps_sigcookie;
        if (copyout(&frame, fp, sizeof(frame)))
-               sigexit(p, SIGILL);
+               return 1;
 
        /*
         * Build context to run handler in.
@@ -818,6 +818,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        tf->fixreg[12] = (register_t)catcher;
 
        tf->srr0 = p->p_p->ps_sigcode;
+
+       return 0;
 }
 
 int
Index: arch/sparc64/sparc64/machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/sparc64/sparc64/machdep.c,v
retrieving revision 1.198
diff -u -p -r1.198 machdep.c
--- arch/sparc64/sparc64/machdep.c      23 Jun 2020 01:21:29 -0000      1.198
+++ arch/sparc64/sparc64/machdep.c      15 Sep 2020 08:49:01 -0000
@@ -402,7 +402,7 @@ cpu_sysctl(int *name, u_int namelen, voi
 /*
  * Send an interrupt to process.
  */
-void
+int
 sendsig(sig_t catcher, int sig, sigset_t mask, const siginfo_t *ksip)
 {
        struct proc *p = curproc;
@@ -477,8 +477,7 @@ sendsig(sig_t catcher, int sig, sigset_t
                printf("sendsig: stack was trashed trying to send sig %d, "
                    "sending SIGILL\n", sig);
 #endif
-               sigexit(p, SIGILL);
-               /* NOTREACHED */
+               return 1;
        }
 
        /*
@@ -490,6 +489,8 @@ sendsig(sig_t catcher, int sig, sigset_t
        tf->tf_pc = addr;
        tf->tf_npc = addr + 4;
        tf->tf_out[6] = newsp - STACK_OFFSET;
+
+       return 0;
 }
 
 /*
Index: kern/kern_sig.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_sig.c,v
retrieving revision 1.262
diff -u -p -r1.262 kern_sig.c
--- kern/kern_sig.c     13 Sep 2020 13:33:37 -0000      1.262
+++ kern/kern_sig.c     16 Sep 2020 08:21:33 -0000
@@ -462,6 +462,8 @@ sys_sigprocmask(struct proc *p, void *v,
        int error = 0;
        sigset_t mask;
 
+       KASSERT(p == curproc);
+
        *retval = p->p_sigmask;
        mask = SCARG(uap, mask) &~ sigcantmask;
 
@@ -822,7 +824,10 @@ trapsignal(struct proc *p, int signum, u
                            p->p_sigmask, code, &si);
                }
 #endif
-               sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, &si);
+               if (sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, &si)) {
+                       sigexit(p, SIGILL);
+                       /* NOTREACHED */
+               }
                postsig_done(p, signum, ps);
        } else {
                p->p_sisig = signum;
@@ -1450,7 +1455,10 @@ postsig(struct proc *p, int signum)
                        p->p_sigval.sival_ptr = NULL;
                }
 
-               sendsig(action, signum, returnmask, &si);
+               if (sendsig(action, signum, returnmask, &si)) {
+                       sigexit(p, SIGILL);
+                       /* NOTREACHED */
+               }
                postsig_done(p, signum, ps);
                splx(s);
        }
Index: sys/signalvar.h
===================================================================
RCS file: /cvs/src/sys/sys/signalvar.h,v
retrieving revision 1.43
diff -u -p -r1.43 signalvar.h
--- sys/signalvar.h     13 Sep 2020 13:33:37 -0000      1.43
+++ sys/signalvar.h     16 Sep 2020 08:21:33 -0000
@@ -140,6 +140,6 @@ void        sigactsfree(struct process *);
 /*
  * Machine-dependent functions:
  */
-void   sendsig(sig_t _catcher, int _sig, sigset_t _mask, const siginfo_t *_si);
+int    sendsig(sig_t _catcher, int _sig, sigset_t _mask, const siginfo_t *_si);
 #endif /* _KERNEL */
 #endif /* !_SYS_SIGNALVAR_H_ */

Reply via email to