xref: /netbsd-src/sys/arch/m68k/m68k/compat_16_machdep.c (revision 9bd176bc31f8bfe98877c43bf39dbe56a5d95a54)
1 /*    $NetBSD: compat_16_machdep.c,v 1.18 2021/10/27 04:14:59 thorpej Exp $   */
2 
3 /*
4  * Copyright (c) 1988 University of Utah.
5  * Copyright (c) 1982, 1986, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: Utah Hdr: machdep.c 1.74 92/12/20
37  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.18 2021/10/27 04:14:59 thorpej Exp $");
42 
43 #ifdef _KERNEL_OPT
44 #include "opt_compat_netbsd.h"
45 #endif
46 
47 #define __M68K_SIGNAL_PRIVATE
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/ras.h>
54 #include <sys/signal.h>
55 #include <sys/signalvar.h>
56 #include <sys/ucontext.h>
57 
58 #include <sys/mount.h>
59 #include <sys/syscallargs.h>
60 
61 #ifdef COMPAT_13
62 #include <compat/sys/signal.h>
63 #include <compat/sys/signalvar.h>
64 #endif
65 
66 #include <machine/cpu.h>
67 #include <machine/reg.h>
68 #include <machine/frame.h>
69 
70 extern  short exframesize[];
71 
72 #ifdef DEBUG
73 extern int sigdebug;
74 extern int sigpid;
75 #define SDB_FOLLOW	0x01
76 #define SDB_KSTACK	0x02
77 #define SDB_FPSTATE	0x04
78 #endif
79 
80 #ifdef COMPAT_16
81 /*
82  * Send an interrupt to process.
83  */
84 void
sendsig_sigcontext(const ksiginfo_t * ksi,const sigset_t * mask)85 sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
86 {
87 	struct lwp *l = curlwp;
88 	struct proc *p = l->l_proc;
89 	struct sigacts *ps = p->p_sigacts;
90 	struct frame *frame = (struct frame *)l->l_md.md_regs;
91 	int onstack, error;
92 	int sig = ksi->ksi_signo;
93 	u_long code = KSI_TRAPCODE(ksi);
94 	struct sigframe_sigcontext *fp = getframe(l, sig, &onstack), kf;
95 	sig_t catcher = SIGACTION(p, sig).sa_handler;
96 	short ft = frame->f_format;
97 
98 	fp--;
99 
100 #ifdef DEBUG
101 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
102 		printf("sendsig(%d): sig %d ssp %p usp %p scp %p ft %d\n",
103 		    p->p_pid, sig, &onstack, fp, &fp->sf_sc, ft);
104 #endif
105 
106 	/* Build stack frame for signal trampoline. */
107 	switch (ps->sa_sigdesc[sig].sd_vers) {
108 	case __SIGTRAMP_SIGCODE_VERSION:	/* legacy on-stack sigtramp */
109 		kf.sf_ra = (int)p->p_sigctx.ps_sigcode;
110 		break;
111 
112 	case __SIGTRAMP_SIGCONTEXT_VERSION:
113 		kf.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;
114 		break;
115 
116 	default:
117 		/* Don't know what trampoline version; kill it. */
118 		sigexit(l, SIGILL);
119 	}
120 
121 	kf.sf_signum = sig;
122 	kf.sf_code = code;
123 	kf.sf_scp = &fp->sf_sc;
124 
125 	/*
126 	 * Save necessary hardware state.  Currently this includes:
127 	 *	- general registers
128 	 *	- original exception frame (if not a "normal" frame)
129 	 *	- FP coprocessor state
130 	 */
131 	kf.sf_state.ss_flags = SS_USERREGS;
132 	memcpy(kf.sf_state.ss_frame.f_regs, frame->f_regs,
133 	    sizeof(frame->f_regs));
134 	if (ft >= FMT4) {
135 #ifdef DEBUG
136 		if (ft > 15 || exframesize[ft] < 0)
137 			panic("sendsig: bogus frame type");
138 #endif
139 		kf.sf_state.ss_flags |= SS_RTEFRAME;
140 		kf.sf_state.ss_frame.f_format = frame->f_format;
141 		kf.sf_state.ss_frame.f_vector = frame->f_vector;
142 		memcpy(&kf.sf_state.ss_frame.F_u, &frame->F_u,
143 		    (size_t) exframesize[ft]);
144 		/*
145 		 * Leave an indicator that we need to clean up the kernel
146 		 * stack.  We do this by setting the "pad word" above the
147 		 * hardware stack frame to the amount the stack must be
148 		 * adjusted by.
149 		 *
150 		 * N.B. we increment rather than just set f_stackadj in
151 		 * case we are called from syscall when processing a
152 		 * sigreturn.  In that case, f_stackadj may be non-zero.
153 		 */
154 		frame->f_stackadj += exframesize[ft];
155 		frame->f_format = frame->f_vector = 0;
156 #ifdef DEBUG
157 		if (sigdebug & SDB_FOLLOW)
158 			printf("sendsig(%d): copy out %d of frame %d\n",
159 			    p->p_pid, exframesize[ft], ft);
160 #endif
161 	}
162 
163 	if (fputype) {
164 		kf.sf_state.ss_flags |= SS_FPSTATE;
165 		m68881_save(&kf.sf_state.ss_fpstate);
166 	}
167 #ifdef DEBUG
168 	if ((sigdebug & SDB_FPSTATE) && *(char *)&kf.sf_state.ss_fpstate)
169 		printf("sendsig(%d): copy out FP state (%x) to %p\n",
170 		    p->p_pid, *(u_int *)&kf.sf_state.ss_fpstate,
171 		    &kf.sf_state.ss_fpstate);
172 #endif
173 
174 	/* Build the signal context to be used by sigreturn. */
175 	kf.sf_sc.sc_sp = frame->f_regs[SP];
176 	kf.sf_sc.sc_fp = frame->f_regs[A6];
177 	kf.sf_sc.sc_ap = (int)&fp->sf_state;
178 	kf.sf_sc.sc_pc = frame->f_pc;
179 	kf.sf_sc.sc_ps = frame->f_sr;
180 
181 	/* Save signal stack. */
182 	kf.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
183 
184 	/* Save signal mask. */
185 	kf.sf_sc.sc_mask = *mask;
186 
187 #ifdef COMPAT_13
188 	/*
189 	 * XXX We always have to save an old style signal mask because
190 	 * XXX we might be delivering a signal to a process which will
191 	 * XXX escape from the signal in a non-standard way and invoke
192 	 * XXX sigreturn() directly.
193 	 */
194 	native_sigset_to_sigset13(mask, &kf.sf_sc.__sc_mask13);
195 #endif
196 	sendsig_reset(l, sig);
197 	mutex_exit(p->p_lock);
198 	error = copyout(&kf, fp, sizeof(kf));
199 	mutex_enter(p->p_lock);
200 
201 	if (error != 0) {
202 #ifdef DEBUG
203 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
204 			printf("sendsig(%d): copyout failed on sig %d\n",
205 			    p->p_pid, sig);
206 #endif
207 		/*
208 		 * Process has trashed its stack; give it an illegal
209 		 * instruction to halt it in its tracks.
210 		 */
211 		sigexit(l, SIGILL);
212 		/* NOTREACHED */
213 	}
214 #ifdef DEBUG
215 	if (sigdebug & SDB_FOLLOW)
216 		printf("sendsig(%d): sig %d scp %p fp %p sc_sp %x sc_ap %x\n",
217 		    p->p_pid, sig, kf.sf_scp, fp,
218 		    kf.sf_sc.sc_sp, kf.sf_sc.sc_ap);
219 #endif
220 
221 	buildcontext(l, catcher, fp);
222 
223 	/* Remember that we're now on the signal stack. */
224 	if (onstack)
225 		l->l_sigstk.ss_flags |= SS_ONSTACK;
226 
227 #ifdef DEBUG
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 #endif
234 
235 #ifdef COMPAT_16
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 int
compat_16_sys___sigreturn14(struct lwp * l,const struct compat_16_sys___sigreturn14_args * uap,register_t * retval)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 proc *p = l->l_proc;
253 	struct sigcontext *scp;
254 	struct frame *frame;
255 	struct sigcontext tsigc;
256 	struct sigstate tstate;
257 	int rf, flags, error;
258 
259 	/*
260 	 * The trampoline code hands us the context.
261 	 * It is unsafe to keep track of it ourselves, in the event that a
262 	 * program jumps out of a signal handler.
263 	 */
264 	scp = SCARG(uap, sigcntxp);
265 #ifdef DEBUG
266 	if (sigdebug & SDB_FOLLOW)
267 		printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
268 #endif
269 	if ((int)scp & 1)
270 		return EINVAL;
271 
272 	if (copyin(scp, &tsigc, sizeof(tsigc)) != 0)
273 		return EFAULT;
274 	scp = &tsigc;
275 
276 	/* Make sure the user isn't pulling a fast one on us! */
277 	if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
278 		return EINVAL;
279 
280 	/* Restore register context. */
281 	frame = (struct frame *) l->l_md.md_regs;
282 
283 	/*
284 	 * Grab pointer to hardware state information.
285 	 * If zero, the user is probably doing a longjmp.
286 	 */
287 	if ((rf = scp->sc_ap) == 0)
288 		goto restore;
289 
290 	/*
291 	 * See if there is anything to do before we go to the
292 	 * expense of copying in close to 1/2K of data
293 	 */
294 	error = ufetch_int((void *)rf, (u_int *)&flags);
295 #ifdef DEBUG
296 	if (sigdebug & SDB_FOLLOW)
297 		printf("sigreturn(%d): sc_ap %x flags %x\n",
298 		    p->p_pid, rf, error ? -1 : flags);
299 #endif
300 	/* ufetch_int() failed (bogus sc_ap value). */
301 	if (error)
302 		return EINVAL;
303 
304 	if (flags == 0 || copyin((void *)rf, &tstate, sizeof(tstate)) != 0)
305 		goto restore;
306 #ifdef DEBUG
307 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
308 		printf("sigreturn(%d): ssp %p usp %x scp %p ft %d\n",
309 		    p->p_pid, &flags, scp->sc_sp, SCARG(uap, sigcntxp),
310 		    (flags & SS_RTEFRAME) ? tstate.ss_frame.f_format : -1);
311 #endif
312 	/*
313 	 * Restore long stack frames.  Note that we do not copy
314 	 * back the saved SR or PC, they were picked up above from
315 	 * the sigcontext structure.
316 	 */
317 	if (flags & SS_RTEFRAME) {
318 		register int sz;
319 
320 		/* grab frame type and validate */
321 		sz = tstate.ss_frame.f_format;
322 		if (sz > 15 || (sz = exframesize[sz]) < 0
323 				|| frame->f_stackadj < sz)
324 			return EINVAL;
325 		frame->f_stackadj -= sz;
326 		frame->f_format = tstate.ss_frame.f_format;
327 		frame->f_vector = tstate.ss_frame.f_vector;
328 		memcpy(&frame->F_u, &tstate.ss_frame.F_u, sz);
329 #ifdef DEBUG
330 		if (sigdebug & SDB_FOLLOW)
331 			printf("sigreturn(%d): copy in %d of frame type %d\n",
332 			    p->p_pid, sz, tstate.ss_frame.f_format);
333 #endif
334 	}
335 
336 	/*
337 	 * Restore most of the users registers except for A6 and SP
338 	 * which will be handled below.
339 	 */
340 	if (flags & SS_USERREGS)
341 		memcpy(frame->f_regs, tstate.ss_frame.f_regs,
342 		    sizeof(frame->f_regs) - (2 * NBPW));
343 
344 	/*
345 	 * Restore the original FP context
346 	 */
347 	if (fputype && (flags & SS_FPSTATE))
348 		m68881_restore(&tstate.ss_fpstate);
349 
350  restore:
351 	/*
352 	 * Restore the user supplied information.
353 	 * This should be at the last so that the error (EINVAL)
354 	 * is reported to the sigreturn caller, not to the
355 	 * jump destination.
356 	 */
357 
358 	frame->f_regs[SP] = scp->sc_sp;
359 	frame->f_regs[A6] = scp->sc_fp;
360 	frame->f_pc = scp->sc_pc;
361 	frame->f_sr = scp->sc_ps;
362 
363 	mutex_enter(p->p_lock);
364 
365 	/* Restore signal stack. */
366 	if (scp->sc_onstack & SS_ONSTACK)
367 		l->l_sigstk.ss_flags |= SS_ONSTACK;
368 	else
369 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
370 
371 	/* Restore signal mask. */
372 	(void) sigprocmask1(l, SIG_SETMASK, &scp->sc_mask, 0);
373 
374 	mutex_exit(p->p_lock);
375 
376 #ifdef DEBUG
377 	if ((sigdebug & SDB_FPSTATE) && *(char *)&tstate.ss_fpstate)
378 		printf("sigreturn(%d): copied in FP state (%x) at %p\n",
379 		    p->p_pid, *(u_int *)&tstate.ss_fpstate,
380 		    &tstate.ss_fpstate);
381 	if ((sigdebug & SDB_FOLLOW) ||
382 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
383 		printf("sigreturn(%d): returns\n", p->p_pid);
384 #endif
385 	return EJUSTRETURN;
386 }
387 #endif
388