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