xref: /netbsd-src/sys/arch/mips/mips/sig_machdep.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: sig_machdep.c,v 1.4 2003/01/17 23:36:18 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center and by Chris Demetriou.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright 1996 The Board of Trustees of The Leland Stanford
42  * Junior University. All Rights Reserved.
43  *
44  * Permission to use, copy, modify, and distribute this
45  * software and its documentation for any purpose and without
46  * fee is hereby granted, provided that the above copyright
47  * notice appear in all copies.  Stanford University
48  * makes no representations about the suitability of this
49  * software for any purpose.  It is provided "as is" without
50  * express or implied warranty.
51  */
52 
53 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
54 
55 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.4 2003/01/17 23:36:18 thorpej Exp $");
56 
57 #include "opt_cputype.h"
58 #include "opt_compat_netbsd.h"
59 #include "opt_compat_ultrix.h"
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/proc.h>
65 #include <sys/user.h>
66 #include <sys/signal.h>
67 #include <sys/signalvar.h>
68 #include <sys/mount.h>
69 #include <sys/sa.h>
70 #include <sys/syscallargs.h>
71 
72 #include <machine/cpu.h>
73 
74 #include <mips/regnum.h>
75 
76 #ifdef DEBUG
77 int sigdebug = 0;
78 int sigpid = 0;
79 #define SDB_FOLLOW	0x01
80 #define SDB_KSTACK	0x02
81 #define SDB_FPSTATE	0x04
82 #endif
83 
84 /*
85  * Send a signal to process.
86  */
87 void
88 sendsig(int sig, sigset_t *returnmask, u_long code)
89 {
90 	struct lwp *l = curlwp;
91 	struct proc *p = l->l_proc;
92 	struct sigacts *ps = p->p_sigacts;
93 	struct sigcontext *scp, ksc;
94 	struct frame *f;
95 	int onstack;
96 	sig_t catcher = SIGACTION(p, sig).sa_handler;
97 
98 	f = (struct frame *)l->l_md.md_regs;
99 
100 	/* Do we need to jump onto the signal stack? */
101 	onstack =
102 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
103 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
104 
105 	/* Allocate space for the signal handler context. */
106 	if (onstack)
107 		scp = (struct sigcontext *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp
108 		    + p->p_sigctx.ps_sigstk.ss_size);
109 	else
110 		/* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
111 		scp = (struct sigcontext *)(u_int32_t)f->f_regs[SP];
112 	scp--;
113 
114 #ifdef DEBUG
115 	if ((sigdebug & SDB_FOLLOW) ||
116 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
117 		printf("sendsig(%d): sig %d ssp %p scp %p\n",
118 		       p->p_pid, sig, &onstack, scp);
119 #endif
120 
121 	/* Build stack frame for signal trampoline. */
122 	ksc.sc_pc = f->f_regs[PC];
123 	ksc.mullo = f->f_regs[MULLO];
124 	ksc.mulhi = f->f_regs[MULHI];
125 
126 	/* Save register context. */
127 	ksc.sc_regs[ZERO] = 0xACEDBADE;		/* magic number */
128 	memcpy(&ksc.sc_regs[1], &f->f_regs[1],
129 	    sizeof(ksc.sc_regs) - sizeof(ksc.sc_regs[0]));
130 
131 	/* Save the FP state, if necessary, then copy it. */
132 #ifndef SOFTFLOAT
133 	ksc.sc_fpused = l->l_md.md_flags & MDP_FPUSED;
134 	if (ksc.sc_fpused) {
135 		/* if FPU has current state, save it first */
136 		if (l == fpcurlwp)
137 			savefpregs(l);
138 		*(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs;
139 	}
140 #else
141 	*(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs;
142 #endif
143 
144 	/* Save signal stack. */
145 	ksc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
146 
147 	/* Save signal mask. */
148 	ksc.sc_mask = *returnmask;
149 
150 #if defined(COMPAT_13) || defined(COMPAT_ULTRIX)
151 	/*
152 	 * XXX We always have to save an old style signal mask because
153 	 * XXX we might be delivering a signal to a process which will
154 	 * XXX escape from the signal in a non-standard way and invoke
155 	 * XXX sigreturn() directly.
156 	 */
157 	native_sigset_to_sigset13(returnmask, &ksc.__sc_mask13);
158 #endif
159 
160 	if (copyout(&ksc, (caddr_t)scp, sizeof(ksc))) {
161 		/*
162 		 * Process has trashed its stack; give it an illegal
163 		 * instruction to halt it in its tracks.
164 		 */
165 #ifdef DEBUG
166 		if ((sigdebug & SDB_FOLLOW) ||
167 		    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
168 			printf("sendsig(%d): copyout failed on sig %d\n",
169 			    p->p_pid, sig);
170 #endif
171 		sigexit(l, SIGILL);
172 		/* NOTREACHED */
173 	}
174 
175 	/*
176 	 * Set up the registers to directly invoke the signal
177 	 * handler.  The return address will be set up to point
178 	 * to the signal trampoline to bounce us back.
179 	 */
180 	f->f_regs[A0] = sig;
181 	f->f_regs[A1] = code;
182 	f->f_regs[A2] = (int)scp;
183 	f->f_regs[A3] = (int)catcher;		/* XXX ??? */
184 
185 	f->f_regs[PC] = (int)catcher;
186 	f->f_regs[T9] = (int)catcher;
187 	f->f_regs[SP] = (int)scp;
188 
189 	switch (ps->sa_sigdesc[sig].sd_vers) {
190 #if 1 /* COMPAT_16 */
191 	case 0:		/* legacy on-stack sigtramp */
192 		f->f_regs[RA] = (int)p->p_sigctx.ps_sigcode;
193 		break;
194 #endif /* COMPAT_16 */
195 
196 	case 1:
197 		f->f_regs[RA] = (int)ps->sa_sigdesc[sig].sd_tramp;
198 		break;
199 
200 	default:
201 		/* Don't know what trampoline version; kill it. */
202 		sigexit(l, SIGILL);
203 	}
204 
205 	/* Remember that we're now on the signal stack. */
206 	if (onstack)
207 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
208 
209 #ifdef DEBUG
210 	if ((sigdebug & SDB_FOLLOW) ||
211 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
212 		printf("sendsig(%d): sig %d returns\n",
213 		       p->p_pid, sig);
214 #endif
215 }
216 
217 /*
218  * System call to cleanup state after a signal
219  * has been taken.  Reset signal mask and
220  * stack state from context left by sendsig (above).
221  * Return to previous pc and psl as specified by
222  * context left by sendsig. Check carefully to
223  * make sure that the user has not modified the
224  * psl to gain improper privileges or to cause
225  * a machine fault.
226  */
227 /* ARGSUSED */
228 int
229 sys___sigreturn14(struct lwp *l, void *v, register_t *retval)
230 {
231 	struct sys___sigreturn14_args /* {
232 		syscallarg(struct sigcontext *) sigcntxp;
233 	} */ *uap = v;
234 	struct sigcontext *scp, ksc;
235 	struct frame *f;
236 	struct proc *p = l->l_proc;
237 	int error;
238 
239 	/*
240 	 * The trampoline code hands us the context.
241 	 * It is unsafe to keep track of it ourselves, in the event that a
242 	 * program jumps out of a signal handler.
243 	 */
244 	scp = SCARG(uap, sigcntxp);
245 #ifdef DEBUG
246 	if (sigdebug & SDB_FOLLOW)
247 		printf("sigreturn: pid %d, scp %p\n", l->l_proc->p_pid, scp);
248 #endif
249 	if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0)
250 		return (error);
251 
252 	if ((u_int) ksc.sc_regs[ZERO] != 0xacedbadeU)	/* magic number */
253 		return (EINVAL);
254 
255 	/* Restore the register context. */
256 	f = (struct frame *)l->l_md.md_regs;
257 	f->f_regs[PC] = ksc.sc_pc;
258 	f->f_regs[MULLO] = ksc.mullo;
259 	f->f_regs[MULHI] = ksc.mulhi;
260 	memcpy(&f->f_regs[1], &scp->sc_regs[1],
261 	    sizeof(scp->sc_regs) - sizeof(scp->sc_regs[0]));
262 #ifndef	SOFTFLOAT
263 	if (scp->sc_fpused) {
264 		/* Disable the FPU to fault in FP registers. */
265 		f->f_regs[SR] &= ~MIPS_SR_COP_1_BIT;
266 		if (l == fpcurlwp) {
267 			fpcurlwp = (struct lwp *)0;
268 		}
269 		l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
270 	}
271 #else
272 	l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
273 #endif
274 
275 	/* Restore signal stack. */
276 	if (ksc.sc_onstack & SS_ONSTACK)
277 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
278 	else
279 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
280 
281 	/* Restore signal mask. */
282 	(void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
283 
284 	return (EJUSTRETURN);
285 }
286