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