1 /* $NetBSD: compat_16_machdep.c,v 1.17 2011/06/07 00:48:29 matt 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.17 2011/06/07 00:48:29 matt 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  */
103 void
104 sendsig_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 	fpu_save();
136 	frame.sf_sc.sc_ownedfp = fpu_used_p(l);
137 	memcpy((struct fpreg *)frame.sf_sc.sc_fpregs, &pcb->pcb_fp,
138 	    sizeof(struct fpreg));
139 	frame.sf_sc.sc_fp_control = alpha_read_fp_c(l);
140 	memset(frame.sf_sc.sc_reserved, 0, sizeof frame.sf_sc.sc_reserved);
141 	memset(frame.sf_sc.sc_xxx, 0, sizeof frame.sf_sc.sc_xxx); /* XXX */
142 
143 	/* Save signal stack. */
144 	frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
145 
146 	/* Save signal mask. */
147 	frame.sf_sc.sc_mask = *mask;
148 
149 #if defined(COMPAT_13) || defined(COMPAT_OSF1)
150 	/*
151 	 * XXX We always have to save an old style signal mask because
152 	 * XXX we might be delivering a signal to a process which will
153 	 * XXX escape from the signal in a non-standard way and invoke
154 	 * XXX sigreturn() directly.
155 	 */
156 	{
157 		/* Note: it's a long in the stack frame. */
158 		sigset13_t mask13;
159 
160 		native_sigset_to_sigset13(mask, &mask13);
161 		frame.sf_sc.__sc_mask13 = mask13;
162 	}
163 #endif
164 
165 	sendsig_reset(l, sig);
166 	mutex_exit(p->p_lock);
167 	error = copyout(&frame, (void *)fp, sizeof(frame));
168 	mutex_enter(p->p_lock);
169 
170 	if (error != 0) {
171 		/*
172 		 * Process has trashed its stack; give it an illegal
173 		 * instruction to halt it in its tracks.
174 		 */
175 #ifdef DEBUG
176 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
177 			printf("sendsig_sigcontext(%d): copyout failed on sig %d\n",
178 			       p->p_pid, sig);
179 #endif
180 		sigexit(l, SIGILL);
181 		/* NOTREACHED */
182 	}
183 #ifdef DEBUG
184 	if (sigdebug & SDB_FOLLOW)
185 		printf("sendsig_sigcontext(%d): sig %d usp %p code %x\n",
186 		       p->p_pid, sig, fp, ksi->ksi_code);
187 #endif
188 
189 	/*
190 	 * Set up the registers to directly invoke the signal handler.  The
191 	 * signal trampoline is then used to return from the signal.  Note
192 	 * the trampoline version numbers are coordinated with machine-
193 	 * dependent code in libc.
194 	 */
195 	switch (ps->sa_sigdesc[sig].sd_vers) {
196 	case 0:		/* legacy on-stack sigtramp */
197 		buildcontext(l,(void *)catcher,
198 			     (void *)p->p_sigctx.ps_sigcode,
199 			     (void *)fp);
200 		break;
201 #ifdef COMPAT_16
202 	case 1:
203 		buildcontext(l,(void *)catcher,
204 			     (const void *)ps->sa_sigdesc[sig].sd_tramp,
205 			     (void *)fp);
206 		break;
207 #endif
208 	default:
209 		/* Don't know what trampoline version; kill it. */
210 		sigexit(l, SIGILL);
211 	}
212 
213 	/* sigcontext specific trap frame */
214 	tf->tf_regs[FRAME_A0] = sig;
215 
216 	/* tf->tf_regs[FRAME_A1] = ksi->ksi_code; */
217 	tf->tf_regs[FRAME_A1] = KSI_TRAPCODE(ksi);
218 	tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_sc;
219 
220 	/* Remember that we're now on the signal stack. */
221 	if (onstack)
222 		l->l_sigstk.ss_flags |= SS_ONSTACK;
223 
224 #ifdef DEBUG
225 	if (sigdebug & SDB_FOLLOW)
226 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
227 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
228 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
229 		printf("sendsig(%d): sig %d returns\n",
230 		    p->p_pid, sig);
231 #endif
232 }
233 
234 #ifdef COMPAT_16 /* not needed if COMPAT_OSF1 only */
235 /*
236  * System call to cleanup state after a signal
237  * has been taken.  Reset signal mask and
238  * stack state from context left by sendsig (above).
239  * Return to previous pc and psl as specified by
240  * context left by sendsig. Check carefully to
241  * make sure that the user has not modified the
242  * psl to gain improper privileges or to cause
243  * a machine fault.
244  */
245 /* ARGSUSED */
246 int
247 compat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
248 {
249 	/* {
250 		syscallarg(struct sigcontext *) sigcntxp;
251 	} */
252 	struct sigcontext *scp, ksc;
253 	struct proc *p = l->l_proc;
254 	struct pcb *pcb;
255 
256 	/*
257 	 * The trampoline code hands us the context.
258 	 * It is unsafe to keep track of it ourselves, in the event that a
259 	 * program jumps out of a signal handler.
260 	 */
261 	scp = SCARG(uap, sigcntxp);
262 #ifdef DEBUG
263 	if (sigdebug & SDB_FOLLOW)
264 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
265 #endif
266 	if (ALIGN(scp) != (u_int64_t)scp)
267 		return (EINVAL);
268 
269 	if (copyin((void *)scp, &ksc, sizeof(ksc)) != 0)
270 		return (EFAULT);
271 
272 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
273 		return (EINVAL);
274 
275 	/* Restore register context. */
276 	l->l_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
277 	l->l_md.md_tf->tf_regs[FRAME_PS] =
278 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
279 
280 	regtoframe((struct reg *)ksc.sc_regs, l->l_md.md_tf);
281 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
282 
283 	pcb = lwp_getpcb(l);
284 	fpu_discard();
285 	memcpy(&pcb->pcb_fp, (struct fpreg *)ksc.sc_fpregs,
286 	    sizeof(struct fpreg));
287 	pcb->pcb_fp.fpr_cr = ksc.sc_fpcr;
288 	l->l_md.md_flags = (ksc.sc_fp_control & MDLWP_FP_C)
289 	    | (ksc.sc_ownedfp ? MDLWP_FPUSED : 0);
290 
291 	mutex_enter(p->p_lock);
292 	/* Restore signal stack. */
293 	if (ksc.sc_onstack & SS_ONSTACK)
294 		l->l_sigstk.ss_flags |= SS_ONSTACK;
295 	else
296 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
297 	/* Restore signal mask. */
298 	(void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0);
299 	mutex_exit(p->p_lock);
300 
301 #ifdef DEBUG
302 	if (sigdebug & SDB_FOLLOW)
303 		printf("sigreturn(%d): returns\n", p->p_pid);
304 #endif
305 	return (EJUSTRETURN);
306 }
307 #endif /* COMPAT_16 */
308