compat_16_machdep.c revision 1.13
1/* $NetBSD: compat_16_machdep.c,v 1.13 2008/04/24 18:39:20 ad Exp $ */
2
3/*-
4 * Copyright (c) 2003 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 G. 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 (c) 1994, 1995, 1996 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Author: Chris G. Demetriou
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
59 *  School of Computer Science
60 *  Carnegie Mellon University
61 *  Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 */
66
67#include "opt_ddb.h"
68#include "opt_kgdb.h"
69#include "opt_multiprocessor.h"
70#include "opt_dec_3000_300.h"
71#include "opt_dec_3000_500.h"
72#include "opt_compat_osf1.h"
73#include "opt_compat_netbsd.h"
74#include "opt_execfmt.h"
75
76#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
77#include <sys/types.h>
78#include <sys/param.h>
79#include <sys/signal.h>
80#include <sys/mount.h>
81#include <sys/proc.h>
82#include <sys/systm.h>
83#include <sys/syscall.h>
84#include <sys/syscallargs.h>
85#include <sys/user.h>
86
87#if defined(COMPAT_13) || defined(COMPAT_OSF1)
88#include <compat/sys/signal.h>
89#include <compat/sys/signalvar.h>
90#endif
91
92#include <machine/cpu.h>
93#include <machine/reg.h>
94
95__KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.13 2008/04/24 18:39:20 ad Exp $");
96
97
98#ifdef DEBUG
99#include <machine/sigdebug.h>
100#endif
101
102#include <machine/alpha.h>
103
104#include "ksyms.h"
105
106/*
107 * Send an interrupt to process, old style
108 */
109void
110sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
111{
112	struct lwp *l = curlwp;
113	struct proc *p = l->l_proc;
114	struct sigacts *ps = p->p_sigacts;
115	int onstack, sig = ksi->ksi_signo, error;
116	struct sigframe_sigcontext *fp, frame;
117	struct trapframe *tf;
118	sig_t catcher = SIGACTION(p, sig).sa_handler;
119
120	tf = l->l_md.md_tf;
121	fp = getframe(l, sig, &onstack), frame;
122	fp--;
123
124#ifdef DEBUG
125	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
126		printf("sendsig_sigcontext(%d): sig %d ssp %p usp %p\n",
127		       p->p_pid, sig, &onstack, fp);
128#endif
129
130	/* Build stack frame for signal trampoline. */
131	frame.sf_sc.sc_pc = tf->tf_regs[FRAME_PC];
132	frame.sf_sc.sc_ps = tf->tf_regs[FRAME_PS];
133
134	/* Save register context. */
135	frametoreg(tf, (struct reg *)frame.sf_sc.sc_regs);
136	frame.sf_sc.sc_regs[R_ZERO] = 0xACEDBADE;	/* magic number */
137	frame.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp();
138
139 	/* save the floating-point state, if necessary, then copy it. */
140	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
141		fpusave_proc(l, 1);
142	frame.sf_sc.sc_ownedfp = l->l_md.md_flags & MDP_FPUSED;
143	memcpy((struct fpreg *)frame.sf_sc.sc_fpregs, &l->l_addr->u_pcb.pcb_fp,
144	    sizeof(struct fpreg));
145	frame.sf_sc.sc_fp_control = alpha_read_fp_c(l);
146	memset(frame.sf_sc.sc_reserved, 0, sizeof frame.sf_sc.sc_reserved);
147	memset(frame.sf_sc.sc_xxx, 0, sizeof frame.sf_sc.sc_xxx); /* XXX */
148
149	/* Save signal stack. */
150	frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
151
152	/* Save signal mask. */
153	frame.sf_sc.sc_mask = *mask;
154
155#if defined(COMPAT_13) || defined(COMPAT_OSF1)
156	/*
157	 * XXX We always have to save an old style signal mask because
158	 * XXX we might be delivering a signal to a process which will
159	 * XXX escape from the signal in a non-standard way and invoke
160	 * XXX sigreturn() directly.
161	 */
162	{
163		/* Note: it's a long in the stack frame. */
164		sigset13_t mask13;
165
166		native_sigset_to_sigset13(mask, &mask13);
167		frame.sf_sc.__sc_mask13 = mask13;
168	}
169#endif
170
171	sendsig_reset(l, sig);
172	mutex_exit(p->p_lock);
173	error = copyout(&frame, (void *)fp, sizeof(frame));
174	mutex_enter(p->p_lock);
175
176	if (error != 0) {
177		/*
178		 * Process has trashed its stack; give it an illegal
179		 * instruction to halt it in its tracks.
180		 */
181#ifdef DEBUG
182		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
183			printf("sendsig_sigcontext(%d): copyout failed on sig %d\n",
184			       p->p_pid, sig);
185#endif
186		sigexit(l, SIGILL);
187		/* NOTREACHED */
188	}
189#ifdef DEBUG
190	if (sigdebug & SDB_FOLLOW)
191		printf("sendsig_sigcontext(%d): sig %d usp %p code %x\n",
192		       p->p_pid, sig, fp, ksi->ksi_code);
193#endif
194
195	/*
196	 * Set up the registers to directly invoke the signal handler.  The
197	 * signal trampoline is then used to return from the signal.  Note
198	 * the trampoline version numbers are coordinated with machine-
199	 * dependent code in libc.
200	 */
201	switch (ps->sa_sigdesc[sig].sd_vers) {
202	case 0:		/* legacy on-stack sigtramp */
203		buildcontext(l,(void *)catcher,
204			     (void *)p->p_sigctx.ps_sigcode,
205			     (void *)fp);
206		break;
207#ifdef COMPAT_16
208	case 1:
209		buildcontext(l,(void *)catcher,
210			     (const void *)ps->sa_sigdesc[sig].sd_tramp,
211			     (void *)fp);
212		break;
213#endif
214	default:
215		/* Don't know what trampoline version; kill it. */
216		sigexit(l, SIGILL);
217	}
218
219	/* sigcontext specific trap frame */
220	tf->tf_regs[FRAME_A0] = sig;
221
222	/* tf->tf_regs[FRAME_A1] = ksi->ksi_code; */
223	tf->tf_regs[FRAME_A1] = KSI_TRAPCODE(ksi);
224	tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_sc;
225
226	/* Remember that we're now on the signal stack. */
227	if (onstack)
228		l->l_sigstk.ss_flags |= SS_ONSTACK;
229
230#ifdef DEBUG
231	if (sigdebug & SDB_FOLLOW)
232		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
233		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
234	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
235		printf("sendsig(%d): sig %d returns\n",
236		    p->p_pid, sig);
237#endif
238}
239
240#ifdef COMPAT_16 /* not needed if COMPAT_OSF1 only */
241/*
242 * System call to cleanup state after a signal
243 * has been taken.  Reset signal mask and
244 * stack state from context left by sendsig (above).
245 * Return to previous pc and psl as specified by
246 * context left by sendsig. Check carefully to
247 * make sure that the user has not modified the
248 * psl to gain improper privileges or to cause
249 * a machine fault.
250 */
251/* ARGSUSED */
252int
253compat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
254{
255	/* {
256		syscallarg(struct sigcontext *) sigcntxp;
257	} */
258	struct sigcontext *scp, ksc;
259	struct proc *p = l->l_proc;
260
261	/*
262	 * The trampoline code hands us the context.
263	 * It is unsafe to keep track of it ourselves, in the event that a
264	 * program jumps out of a signal handler.
265	 */
266	scp = SCARG(uap, sigcntxp);
267#ifdef DEBUG
268	if (sigdebug & SDB_FOLLOW)
269	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
270#endif
271	if (ALIGN(scp) != (u_int64_t)scp)
272		return (EINVAL);
273
274	if (copyin((void *)scp, &ksc, sizeof(ksc)) != 0)
275		return (EFAULT);
276
277	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
278		return (EINVAL);
279
280	/* Restore register context. */
281	l->l_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
282	l->l_md.md_tf->tf_regs[FRAME_PS] =
283	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
284
285	regtoframe((struct reg *)ksc.sc_regs, l->l_md.md_tf);
286	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
287
288	/* XXX ksc.sc_ownedfp ? */
289	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
290		fpusave_proc(l, 0);
291	memcpy(&l->l_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
292	    sizeof(struct fpreg));
293	l->l_addr->u_pcb.pcb_fp.fpr_cr = ksc.sc_fpcr;
294	l->l_md.md_flags = ksc.sc_fp_control & MDP_FP_C;
295
296	mutex_enter(p->p_lock);
297	/* Restore signal stack. */
298	if (ksc.sc_onstack & SS_ONSTACK)
299		l->l_sigstk.ss_flags |= SS_ONSTACK;
300	else
301		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
302	/* Restore signal mask. */
303	(void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0);
304	mutex_exit(p->p_lock);
305
306#ifdef DEBUG
307	if (sigdebug & SDB_FOLLOW)
308		printf("sigreturn(%d): returns\n", p->p_pid);
309#endif
310	return (EJUSTRETURN);
311}
312#endif /* COMPAT_16 */
313