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