sig_machdep.c revision 1.1
1/*	$NetBSD: sig_machdep.c,v 1.1 1999/11/17 14:56:11 kleink 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 <sys/param.h>
35#include <sys/mount.h>
36#include <sys/proc.h>
37#include <sys/syscallargs.h>
38#include <sys/systm.h>
39#include <sys/user.h>
40
41/*
42 * Send a signal to process.
43 */
44void
45sendsig(catcher, sig, mask, code)
46	sig_t catcher;
47	int sig;
48	sigset_t *mask;
49	u_long code;
50{
51	struct proc *p = curproc;
52	struct trapframe *tf;
53	struct sigframe *fp, frame;
54	struct sigacts *psp = p->p_sigacts;
55	int onstack;
56
57	tf = trapframe(p);
58
59	/* Do we need to jump onto the signal stack? */
60	onstack =
61	    (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
62	    (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
63
64	/* Allocate space for the signal handler context. */
65	if (onstack)
66		fp = (struct sigframe *)((caddr_t)psp->ps_sigstk.ss_sp +
67						  psp->ps_sigstk.ss_size);
68	else
69		fp = (struct sigframe *)tf->fixreg[1];
70	fp = (struct sigframe *)((int)(fp - 1) & ~0xf);
71
72	/* Build stack frame for signal trampoline. */
73	frame.sf_signum = sig;
74	frame.sf_code = code;
75
76	/* Save register context. */
77	bcopy(tf, &frame.sf_sc.sc_frame, sizeof *tf);
78
79	/* Save signal stack. */
80	frame.sf_sc.sc_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
81
82	/* Save signal mask. */
83	frame.sf_sc.sc_mask = *mask;
84
85#ifdef COMPAT_13
86	/*
87	 * XXX We always have to save an old style signal mask because
88	 * XXX we might be delivering a signal to a process which will
89	 * XXX escape from the signal in a non-standard way and invoke
90	 * XXX sigreturn() directly.
91	 */
92	native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13);
93#endif
94
95	if (copyout(&frame, fp, sizeof frame) != 0) {
96		/*
97		 * Process has trashed its stack; give it an illegal
98		 * instructoin to halt it in its tracks.
99		 */
100		sigexit(p, SIGILL);
101		/* NOTREACHED */
102	}
103
104	/*
105	 * Build context to run handler in.
106	 */
107	tf->fixreg[1] = (int)fp;
108	tf->lr = (int)catcher;
109	tf->fixreg[3] = (int)sig;
110	tf->fixreg[4] = (int)code;
111	tf->fixreg[5] = (int)&frame.sf_sc;
112	tf->srr0 = (int)psp->ps_sigcode;
113
114	/* Remember that we're now on the signal stack. */
115	if (onstack)
116		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
117}
118
119/*
120 * System call to cleanup state after a signal handler returns.
121 */
122int
123sys___sigreturn14(p, v, retval)
124	struct proc *p;
125	void *v;
126	register_t *retval;
127{
128	struct sys___sigreturn14_args /* {
129		syscallarg(struct sigcontext *) sigcntxp;
130	} */ *uap = v;
131	struct sigcontext sc;
132	struct trapframe *tf;
133	int error;
134
135	/*
136	 * The trampoline hands us the context.
137	 * It is unsafe to keep track of it ourselves, in the event that a
138	 * program jumps out of a signal hander.
139	 */
140	if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0)
141		return (error);
142
143	/* Restore the register context. */
144	tf = trapframe(p);
145	if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
146		return (EINVAL);
147	bcopy(&sc.sc_frame, tf, sizeof *tf);
148
149	/* Restore signal stack. */
150	if (sc.sc_onstack & SS_ONSTACK)
151		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
152	else
153		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
154
155	/* Restore signal mask. */
156	(void) sigprocmask1(p, SIG_SETMASK, &sc.sc_mask, 0);
157
158	return (EJUSTRETURN);
159}
160