1 /* $NetBSD: compat_16_machdep.c,v 1.8 2005/09/14 21:48:14 he 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/sa.h>
83 #include <sys/systm.h>
84 #include <sys/syscall.h>
85 #include <sys/syscallargs.h>
86 #include <sys/user.h>
87 
88 #if defined(COMPAT_13) || defined(COMPAT_OSF1)
89 #include <compat/sys/signal.h>
90 #include <compat/sys/signalvar.h>
91 #endif
92 
93 #include <machine/cpu.h>
94 #include <machine/reg.h>
95 
96 __KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.8 2005/09/14 21:48:14 he Exp $");
97 
98 
99 #ifdef DEBUG
100 #include <machine/sigdebug.h>
101 #endif
102 
103 #include <machine/alpha.h>
104 
105 #include "ksyms.h"
106 
107 /*
108  * Send an interrupt to process, old style
109  */
110 void
111 sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
112 {
113 	struct lwp *l = curlwp;
114 	struct proc *p = l->l_proc;
115 	struct sigacts *ps = p->p_sigacts;
116 	int onstack, sig = ksi->ksi_signo;
117 	struct sigframe_sigcontext *fp, frame;
118 	struct trapframe *tf;
119 	sig_t catcher = SIGACTION(p, sig).sa_handler;
120 
121 	tf = l->l_md.md_tf;
122 	fp = getframe(l, sig, &onstack), frame;
123 	fp--;
124 
125 #ifdef DEBUG
126 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
127 		printf("sendsig_sigcontext(%d): sig %d ssp %p usp %p\n",
128 		       p->p_pid, sig, &onstack, fp);
129 #endif
130 
131 	/* Build stack frame for signal trampoline. */
132 	frame.sf_sc.sc_pc = tf->tf_regs[FRAME_PC];
133 	frame.sf_sc.sc_ps = tf->tf_regs[FRAME_PS];
134 
135 	/* Save register context. */
136 	frametoreg(tf, (struct reg *)frame.sf_sc.sc_regs);
137 	frame.sf_sc.sc_regs[R_ZERO] = 0xACEDBADE;	/* magic number */
138 	frame.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp();
139 
140  	/* save the floating-point state, if necessary, then copy it. */
141 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
142 		fpusave_proc(l, 1);
143 	frame.sf_sc.sc_ownedfp = l->l_md.md_flags & MDP_FPUSED;
144 	memcpy((struct fpreg *)frame.sf_sc.sc_fpregs, &l->l_addr->u_pcb.pcb_fp,
145 	    sizeof(struct fpreg));
146 	frame.sf_sc.sc_fp_control = alpha_read_fp_c(l);
147 	memset(frame.sf_sc.sc_reserved, 0, sizeof frame.sf_sc.sc_reserved);
148 	memset(frame.sf_sc.sc_xxx, 0, sizeof frame.sf_sc.sc_xxx); /* XXX */
149 
150 	/* Save signal stack. */
151 	frame.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
152 
153 	/* Save signal mask. */
154 	frame.sf_sc.sc_mask = *mask;
155 
156 #if defined(COMPAT_13) || defined(COMPAT_OSF1)
157 	/*
158 	 * XXX We always have to save an old style signal mask because
159 	 * XXX we might be delivering a signal to a process which will
160 	 * XXX escape from the signal in a non-standard way and invoke
161 	 * XXX sigreturn() directly.
162 	 */
163 	{
164 		/* Note: it's a long in the stack frame. */
165 		sigset13_t mask13;
166 
167 		native_sigset_to_sigset13(mask, &mask13);
168 		frame.sf_sc.__sc_mask13 = mask13;
169 	}
170 #endif
171 
172 	if (copyout(&frame, (caddr_t)fp, sizeof(frame)) != 0) {
173 		/*
174 		 * Process has trashed its stack; give it an illegal
175 		 * instruction to halt it in its tracks.
176 		 */
177 #ifdef DEBUG
178 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
179 			printf("sendsig_sigcontext(%d): copyout failed on sig %d\n",
180 			       p->p_pid, sig);
181 #endif
182 		sigexit(l, SIGILL);
183 		/* NOTREACHED */
184 	}
185 #ifdef DEBUG
186 	if (sigdebug & SDB_FOLLOW)
187 		printf("sendsig_sigcontext(%d): sig %d usp %p code %x\n",
188 		       p->p_pid, sig, fp, ksi->ksi_code);
189 #endif
190 
191 	/*
192 	 * Set up the registers to directly invoke the signal handler.  The
193 	 * signal trampoline is then used to return from the signal.  Note
194 	 * the trampoline version numbers are coordinated with machine-
195 	 * dependent code in libc.
196 	 */
197 	switch (ps->sa_sigdesc[sig].sd_vers) {
198 	case 0:		/* legacy on-stack sigtramp */
199 		buildcontext(l,(void *)catcher,
200 			     (void *)p->p_sigctx.ps_sigcode,
201 			     (void *)fp);
202 		break;
203 #ifdef COMPAT_16
204 	case 1:
205 		buildcontext(l,(void *)catcher,
206 			     (const void *)ps->sa_sigdesc[sig].sd_tramp,
207 			     (void *)fp);
208 		break;
209 #endif
210 	default:
211 		/* Don't know what trampoline version; kill it. */
212 		sigexit(l, SIGILL);
213 	}
214 
215 	/* sigcontext specific trap frame */
216 	tf->tf_regs[FRAME_A0] = sig;
217 
218 	/* tf->tf_regs[FRAME_A1] = ksi->ksi_code; */
219 	tf->tf_regs[FRAME_A1] = KSI_TRAPCODE(ksi);
220 	tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_sc;
221 
222 	/* Remember that we're now on the signal stack. */
223 	if (onstack)
224 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
225 
226 #ifdef DEBUG
227 	if (sigdebug & SDB_FOLLOW)
228 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
229 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
230 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
231 		printf("sendsig(%d): sig %d returns\n",
232 		    p->p_pid, sig);
233 #endif
234 }
235 
236 #ifdef COMPAT_16 /* not needed if COMPAT_OSF1 only */
237 /*
238  * System call to cleanup state after a signal
239  * has been taken.  Reset signal mask and
240  * stack state from context left by sendsig (above).
241  * Return to previous pc and psl as specified by
242  * context left by sendsig. Check carefully to
243  * make sure that the user has not modified the
244  * psl to gain improper privileges or to cause
245  * a machine fault.
246  */
247 /* ARGSUSED */
248 int
249 compat_16_sys___sigreturn14(l, v, retval)
250 	struct lwp *l;
251 	void *v;
252 	register_t *retval;
253 {
254 	struct compat_16_sys___sigreturn14_args /* {
255 		syscallarg(struct sigcontext *) sigcntxp;
256 	} */ *uap = v;
257 	struct sigcontext *scp, ksc;
258 	struct proc *p = l->l_proc;
259 
260 	/*
261 	 * The trampoline code hands us the context.
262 	 * It is unsafe to keep track of it ourselves, in the event that a
263 	 * program jumps out of a signal handler.
264 	 */
265 	scp = SCARG(uap, sigcntxp);
266 #ifdef DEBUG
267 	if (sigdebug & SDB_FOLLOW)
268 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
269 #endif
270 	if (ALIGN(scp) != (u_int64_t)scp)
271 		return (EINVAL);
272 
273 	if (copyin((caddr_t)scp, &ksc, sizeof(ksc)) != 0)
274 		return (EFAULT);
275 
276 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
277 		return (EINVAL);
278 
279 	/* Restore register context. */
280 	l->l_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
281 	l->l_md.md_tf->tf_regs[FRAME_PS] =
282 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
283 
284 	regtoframe((struct reg *)ksc.sc_regs, l->l_md.md_tf);
285 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
286 
287 	/* XXX ksc.sc_ownedfp ? */
288 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
289 		fpusave_proc(l, 0);
290 	memcpy(&l->l_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
291 	    sizeof(struct fpreg));
292 	l->l_addr->u_pcb.pcb_fp.fpr_cr = ksc.sc_fpcr;
293 	l->l_md.md_flags = ksc.sc_fp_control & MDP_FP_C;
294 
295 	/* Restore signal stack. */
296 	if (ksc.sc_onstack & SS_ONSTACK)
297 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
298 	else
299 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
300 
301 	/* Restore signal mask. */
302 	(void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
303 
304 #ifdef DEBUG
305 	if (sigdebug & SDB_FOLLOW)
306 		printf("sigreturn(%d): returns\n", p->p_pid);
307 #endif
308 	return (EJUSTRETURN);
309 }
310 #endif /* COMPAT_16 */
311