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