xref: /netbsd-src/sys/arch/mips/mips/compat_16_machdep.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: compat_16_machdep.c,v 1.9 2007/03/04 06:00:12 christos 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: compat_16_machdep.c,v 1.9 2007/03/04 06:00:12 christos 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/syscallargs.h>
70 
71 #include <compat/sys/signal.h>
72 #include <compat/sys/signalvar.h>
73 
74 #include <machine/cpu.h>
75 
76 #include <mips/regnum.h>
77 #include <mips/frame.h>
78 
79 #ifdef DEBUG
80 int sigdebug = 0;
81 int sigpid = 0;
82 #define SDB_FOLLOW	0x01
83 #define SDB_KSTACK	0x02
84 #define SDB_FPSTATE	0x04
85 #endif
86 
87 /*
88  * Send a signal to process.
89  */
90 void
91 sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *returnmask)
92 {
93 	int sig = ksi->ksi_signo;
94 	struct lwp *l = curlwp;
95 	struct proc *p = l->l_proc;
96 	struct sigacts *ps = p->p_sigacts;
97 	int onstack, error;
98 	struct sigcontext *scp = getframe(l, sig, &onstack), ksc;
99 	struct frame *f;
100 	sig_t catcher = SIGACTION(p, sig).sa_handler;
101 
102 	f = (struct frame *)l->l_md.md_regs;
103 
104 	scp--;
105 
106 #ifdef DEBUG
107 	if ((sigdebug & SDB_FOLLOW) ||
108 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
109 		printf("sendsig(%d): sig %d ssp %p scp %p\n",
110 		       p->p_pid, sig, &onstack, scp);
111 #endif
112 
113 	/* Build stack frame for signal trampoline. */
114 	ksc.sc_pc = f->f_regs[_R_PC];
115 	ksc.mullo = f->f_regs[_R_MULLO];
116 	ksc.mulhi = f->f_regs[_R_MULHI];
117 
118 	/* Save register context. */
119 	ksc.sc_regs[_R_ZERO] = 0xACEDBADE;		/* magic number */
120 	memcpy(&ksc.sc_regs[1], &f->f_regs[1],
121 	    sizeof(ksc.sc_regs) - sizeof(ksc.sc_regs[0]));
122 
123 	/* Save the FP state, if necessary, then copy it. */
124 #ifndef SOFTFLOAT
125 	ksc.sc_fpused = l->l_md.md_flags & MDP_FPUSED;
126 	if (ksc.sc_fpused) {
127 		/* if FPU has current state, save it first */
128 		if (l == fpcurlwp)
129 			savefpregs(l);
130 		*(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs;
131 	}
132 #else
133 	*(struct fpreg *)ksc.sc_fpregs = l->l_addr->u_pcb.pcb_fpregs;
134 #endif
135 
136 	/* Save signal stack. */
137 	ksc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
138 
139 	/* Save signal mask. */
140 	ksc.sc_mask = *returnmask;
141 
142 #if defined(COMPAT_13) || defined(COMPAT_ULTRIX)
143 	/*
144 	 * XXX We always have to save an old style signal mask because
145 	 * XXX we might be delivering a signal to a process which will
146 	 * XXX escape from the signal in a non-standard way and invoke
147 	 * XXX sigreturn() directly.
148 	 */
149 	native_sigset_to_sigset13(returnmask, &ksc.__sc_mask13);
150 #endif
151 
152 	sendsig_reset(l, sig);
153 
154 	mutex_exit(&p->p_smutex);
155 	error = copyout(&ksc, (void *)scp, sizeof(ksc));
156 	mutex_enter(&p->p_smutex);
157 
158 	if (error != 0) {
159 		/*
160 		 * Process has trashed its stack; give it an illegal
161 		 * instruction to halt it in its tracks.
162 		 */
163 #ifdef DEBUG
164 		if ((sigdebug & SDB_FOLLOW) ||
165 		    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
166 			printf("sendsig(%d): copyout failed on sig %d\n",
167 			    p->p_pid, sig);
168 #endif
169 		sigexit(l, SIGILL);
170 		/* NOTREACHED */
171 	}
172 
173 	/*
174 	 * Set up the registers to directly invoke the signal
175 	 * handler.  The return address will be set up to point
176 	 * to the signal trampoline to bounce us back.
177 	 */
178 	f->f_regs[_R_A0] = sig;
179 	f->f_regs[_R_A1] = ksi->ksi_trap;
180 	f->f_regs[_R_A2] = (intptr_t)scp;
181 	f->f_regs[_R_A3] = (intptr_t)catcher;		/* XXX ??? */
182 
183 	f->f_regs[_R_PC] = (intptr_t)catcher;
184 	f->f_regs[_R_T9] = (intptr_t)catcher;
185 	f->f_regs[_R_SP] = (intptr_t)scp;
186 
187 	switch (ps->sa_sigdesc[sig].sd_vers) {
188 	case 0:		/* legacy on-stack sigtramp */
189 		f->f_regs[_R_RA] = (intptr_t)p->p_sigctx.ps_sigcode;
190 		break;
191 #ifdef COMPAT_16
192 	case 1:
193 		f->f_regs[_R_RA] = (intptr_t)ps->sa_sigdesc[sig].sd_tramp;
194 		break;
195 #endif
196 	default:
197 		/* Don't know what trampoline version; kill it. */
198 		sigexit(l, SIGILL);
199 	}
200 
201 	/* Remember that we're now on the signal stack. */
202 	if (onstack)
203 		l->l_sigstk.ss_flags |= SS_ONSTACK;
204 
205 #ifdef DEBUG
206 	if ((sigdebug & SDB_FOLLOW) ||
207 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
208 		printf("sendsig(%d): sig %d returns\n",
209 		       p->p_pid, sig);
210 #endif
211 }
212 
213 #ifdef COMPAT_16 /* not needed if COMPAT_ULTRIX only */
214 /*
215  * System call to cleanup state after a signal
216  * has been taken.  Reset signal mask and
217  * stack state from context left by sendsig (above).
218  * Return to previous pc and psl as specified by
219  * context left by sendsig. Check carefully to
220  * make sure that the user has not modified the
221  * psl to gain improper privileges or to cause
222  * a machine fault.
223  */
224 /* ARGSUSED */
225 int
226 compat_16_sys___sigreturn14(struct lwp *l, void *v, register_t *retval)
227 {
228 	struct compat_16_sys___sigreturn14_args /* {
229 		syscallarg(struct sigcontext *) sigcntxp;
230 	} */ *uap = v;
231 	struct sigcontext *scp, ksc;
232 	struct frame *f;
233 	struct proc *p = l->l_proc;
234 	int error;
235 
236 	/*
237 	 * The trampoline code hands us the context.
238 	 * It is unsafe to keep track of it ourselves, in the event that a
239 	 * program jumps out of a signal handler.
240 	 */
241 	scp = SCARG(uap, sigcntxp);
242 #ifdef DEBUG
243 	if (sigdebug & SDB_FOLLOW)
244 		printf("sigreturn: pid %d, scp %p\n", l->l_proc->p_pid, scp);
245 #endif
246 	if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0)
247 		return (error);
248 
249 	if ((u_int) ksc.sc_regs[_R_ZERO] != 0xacedbadeU)/* magic number */
250 		return (EINVAL);
251 
252 	/* Restore the register context. */
253 	f = (struct frame *)l->l_md.md_regs;
254 	f->f_regs[_R_PC] = ksc.sc_pc;
255 	f->f_regs[_R_MULLO] = ksc.mullo;
256 	f->f_regs[_R_MULHI] = ksc.mulhi;
257 	memcpy(&f->f_regs[1], &scp->sc_regs[1],
258 	    sizeof(scp->sc_regs) - sizeof(scp->sc_regs[0]));
259 #ifndef	SOFTFLOAT
260 	if (scp->sc_fpused) {
261 		/* Disable the FPU to fault in FP registers. */
262 		f->f_regs[_R_SR] &= ~MIPS_SR_COP_1_BIT;
263 		if (l == fpcurlwp)
264 			fpcurlwp = NULL;
265 		l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
266 	}
267 #else
268 	l->l_addr->u_pcb.pcb_fpregs = *(struct fpreg *)scp->sc_fpregs;
269 #endif
270 
271 	mutex_enter(&p->p_smutex);
272 	/* Restore signal stack. */
273 	if (ksc.sc_onstack & SS_ONSTACK)
274 		l->l_sigstk.ss_flags |= SS_ONSTACK;
275 	else
276 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
277 	/* Restore signal mask. */
278 	(void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0);
279 	mutex_exit(&p->p_smutex);
280 
281 	return (EJUSTRETURN);
282 }
283 #endif /* COMPAT_16 */
284