sig_machdep.c revision 1.3
1/*	$NetBSD: sig_machdep.c,v 1.3 2000/04/03 10:23:24 tsubai Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "opt_compat_netbsd.h"
35
36#include <sys/param.h>
37#include <sys/mount.h>
38#include <sys/proc.h>
39#include <sys/syscallargs.h>
40#include <sys/systm.h>
41#include <sys/user.h>
42
43/*
44 * Send a signal to process.
45 */
46void
47sendsig(catcher, sig, mask, code)
48	sig_t catcher;
49	int sig;
50	sigset_t *mask;
51	u_long code;
52{
53	struct proc *p = curproc;
54	struct trapframe *tf;
55	struct sigframe *fp, frame;
56	struct sigacts *psp = p->p_sigacts;
57	int onstack;
58
59	tf = trapframe(p);
60
61	/* Do we need to jump onto the signal stack? */
62	onstack =
63	    (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
64	    (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
65
66	/* Allocate space for the signal handler context. */
67	if (onstack)
68		fp = (struct sigframe *)((caddr_t)psp->ps_sigstk.ss_sp +
69						  psp->ps_sigstk.ss_size);
70	else
71		fp = (struct sigframe *)tf->fixreg[1];
72	fp = (struct sigframe *)((int)(fp - 1) & ~0xf);
73
74	/* Build stack frame for signal trampoline. */
75	frame.sf_signum = sig;
76	frame.sf_code = code;
77
78	/* Save register context. */
79	bcopy(tf, &frame.sf_sc.sc_frame, sizeof *tf);
80
81	/* Save signal stack. */
82	frame.sf_sc.sc_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
83
84	/* Save signal mask. */
85	frame.sf_sc.sc_mask = *mask;
86
87#ifdef COMPAT_13
88	/*
89	 * XXX We always have to save an old style signal mask because
90	 * XXX we might be delivering a signal to a process which will
91	 * XXX escape from the signal in a non-standard way and invoke
92	 * XXX sigreturn() directly.
93	 */
94	native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13);
95#endif
96
97	if (copyout(&frame, fp, sizeof frame) != 0) {
98		/*
99		 * Process has trashed its stack; give it an illegal
100		 * instructoin to halt it in its tracks.
101		 */
102		sigexit(p, SIGILL);
103		/* NOTREACHED */
104	}
105
106	/*
107	 * Build context to run handler in.
108	 */
109	tf->fixreg[1] = (int)fp;
110	tf->lr = (int)catcher;
111	tf->fixreg[3] = (int)sig;
112	tf->fixreg[4] = (int)code;
113	tf->fixreg[5] = (int)&fp->sf_sc;
114	tf->srr0 = (int)psp->ps_sigcode;
115
116	/* Remember that we're now on the signal stack. */
117	if (onstack)
118		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
119}
120
121/*
122 * System call to cleanup state after a signal handler returns.
123 */
124int
125sys___sigreturn14(p, v, retval)
126	struct proc *p;
127	void *v;
128	register_t *retval;
129{
130	struct sys___sigreturn14_args /* {
131		syscallarg(struct sigcontext *) sigcntxp;
132	} */ *uap = v;
133	struct sigcontext sc;
134	struct trapframe *tf;
135	int error;
136
137	/*
138	 * The trampoline hands us the context.
139	 * It is unsafe to keep track of it ourselves, in the event that a
140	 * program jumps out of a signal hander.
141	 */
142	if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0)
143		return (error);
144
145	/* Restore the register context. */
146	tf = trapframe(p);
147	if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
148		return (EINVAL);
149	bcopy(&sc.sc_frame, tf, sizeof *tf);
150
151	/* Restore signal stack. */
152	if (sc.sc_onstack & SS_ONSTACK)
153		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
154	else
155		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
156
157	/* Restore signal mask. */
158	(void) sigprocmask1(p, SIG_SETMASK, &sc.sc_mask, 0);
159
160	return (EJUSTRETURN);
161}
162