1 /*	$NetBSD: linux_machdep.c,v 1.44 2022/08/07 20:14:01 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by ITOH Yasufumi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.44 2022/08/07 20:14:01 tsutsui Exp $");
34 
35 #define COMPAT_LINUX 1
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/exec.h>
42 #include <sys/ioctl.h>
43 #include <sys/mount.h>
44 #include <sys/signal.h>
45 #include <sys/signalvar.h>
46 #include <sys/syscallargs.h>
47 #include <sys/kauth.h>
48 
49 #include <sys/cpu.h>
50 #include <machine/reg.h>
51 
52 #include <compat/linux/common/linux_types.h>
53 #include <compat/linux/common/linux_signal.h>
54 #include <compat/linux/common/linux_ioctl.h>
55 #include <compat/linux/common/linux_exec.h>
56 #include <compat/linux/common/linux_machdep.h>
57 
58 #include <compat/linux/linux_syscall.h>
59 #include <compat/linux/linux_syscallargs.h>
60 
61 /* XXX should be in an include file somewhere */
62 #define CC_PURGE	1
63 #define CC_FLUSH	2
64 #define CC_IPURGE	4
65 #define CC_EXTPURGE	0x80000000
66 /* XXX end should be */
67 
68 extern short exframesize[];
69 
70 #ifdef DEBUG
71 extern int sigdebug;
72 extern int sigpid;
73 #define SDB_FOLLOW	0x01
74 #define SDB_KSTACK	0x02
75 #define SDB_FPSTATE	0x04
76 #endif
77 
78 void setup_linux_sigframe(struct frame *frame, int sig,
79     const sigset_t *mask, void *usp);
80 void setup_linux_rt_sigframe(struct frame *frame, int sig,
81     const sigset_t *mask, void *usp, struct lwp *l);
82 
83 /*
84  * Deal with some m68k-specific things in the Linux emulation code.
85  */
86 
87 /*
88  * Setup registers on program execution.
89  */
90 void
linux_setregs(struct lwp * l,struct exec_package * epp,vaddr_t stack)91 linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
92 {
93 
94 	setregs(l, epp, stack);
95 }
96 
97 /*
98  * Setup signal frame for old signal interface.
99  */
100 void
setup_linux_sigframe(struct frame * frame,int sig,const sigset_t * mask,void * usp)101 setup_linux_sigframe(struct frame *frame, int sig, const sigset_t *mask, void *usp)
102 {
103 	struct lwp *l = curlwp;
104 	struct proc *p = l->l_proc;
105 	struct linux_sigframe *fp, kf;
106 	short ft;
107 	int error;
108 
109 	ft = frame->f_format;
110 
111 	/* Allocate space for the signal handler context on the user stack. */
112 	fp = (struct linux_sigframe *) usp;
113 	fp--;
114 
115 #ifdef DEBUG
116 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
117 		printf("setup_linux_sigframe(%d): sig %d ssp %p usp %p scp %p ft %d\n",
118 		       p->p_pid, sig, &ft, fp, &fp->sf_c.c_sc, ft);
119 #endif
120 
121 	memset(&kf, 0, sizeof(kf));
122 
123 	/* Build stack frame. */
124 	kf.sf_psigtramp = fp->sf_sigtramp;	/* return addr for handler */
125 	kf.sf_signum = native_to_linux_signo[sig];
126 	kf.sf_code = frame->f_vector;		/* Does anyone use it? */
127 	kf.sf_scp = &fp->sf_c.c_sc;
128 
129 	/* The sigtramp code is on the stack frame on Linux/m68k. */
130 	kf.sf_sigtramp[0] = LINUX_SF_SIGTRAMP0;
131 	kf.sf_sigtramp[1] = LINUX_SF_SIGTRAMP1;
132 
133 	/*
134 	 * Save necessary hardware state.  Currently this includes:
135 	 *	- scratch registers
136 	 *	- original exception frame (if not a "normal" frame)
137 	 *	- FP coprocessor state
138 	 */
139 	kf.sf_c.c_sc.sc_d0 = frame->f_regs[D0];
140 	kf.sf_c.c_sc.sc_d1 = frame->f_regs[D1];
141 	kf.sf_c.c_sc.sc_a0 = frame->f_regs[A0];
142 	kf.sf_c.c_sc.sc_a1 = frame->f_regs[A1];
143 
144 	/* Clear for security (and initialize ss_format). */
145 	memset(&kf.sf_c.c_sc.sc_ss, 0, sizeof kf.sf_c.c_sc.sc_ss);
146 
147 	if (ft >= FMT4) {
148 #ifdef DEBUG
149 		if (ft > 15 || exframesize[ft] < 0)
150 			panic("setup_linux_sigframe: bogus frame type");
151 #endif
152 		kf.sf_c.c_sc.sc_ss.ss_format = ft;
153 		kf.sf_c.c_sc.sc_ss.ss_vector = frame->f_vector;
154 		memcpy(&kf.sf_c.c_sc.sc_ss.ss_frame, &frame->F_u,
155 			(size_t) exframesize[ft]);
156 		/*
157 		 * Leave an indicator that we need to clean up the kernel
158 		 * stack.  We do this by setting the "pad word" above the
159 		 * hardware stack frame to the amount the stack must be
160 		 * adjusted by.
161 		 *
162 		 * N.B. we increment rather than just set f_stackadj in
163 		 * case we are called from syscall when processing a
164 		 * sigreturn.  In that case, f_stackadj may be non-zero.
165 		 */
166 		frame->f_stackadj += exframesize[ft];
167 		frame->f_format = frame->f_vector = 0;
168 #ifdef DEBUG
169 		if (sigdebug & SDB_FOLLOW)
170 			printf("setup_linux_sigframe(%d): copy out %d of frame %d\n",
171 			       p->p_pid, exframesize[ft], ft);
172 #endif
173 	}
174 
175 	switch (fputype) {
176 	case FPU_NONE:
177 		break;
178 #ifdef M68060
179 	case FPU_68060:
180 		__asm("fsave %0" : "=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.FPF_u1)
181 			: : "memory");
182 		if (((struct fpframe060 *)&kf.sf_c.c_sc.sc_ss.ss_fpstate.FPF_u1)
183 					->fpf6_frmfmt != FPF6_FMT_NULL) {
184 			__asm("fmovem %%fp0-%%fp1,%0" :
185 				"=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.fpf_regs[0][0]));
186 			/*
187 			 * On 060,  "fmovem fpcr/fpsr/fpi,<ea>"  is
188 			 * emulated by software and slow.
189 			 */
190 			__asm("fmovem %%fpcr,%0; fmovem %%fpsr,%1; fmovem %%fpi,%2" :
191 				"=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.fpf_fpcr),
192 				"=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.fpf_fpsr),
193 				"=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.fpf_fpiar));
194 		}
195 		break;
196 #endif
197 	default:
198 		__asm("fsave %0" : "=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.FPF_u1)
199 			: : "memory");
200 		if (kf.sf_c.c_sc.sc_ss.ss_fpstate.fpf_version) {
201 			__asm("fmovem %%fp0-%%fp1,%0; fmovem %%fpcr/%%fpsr/%%fpi,%1" :
202 				"=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.fpf_regs[0][0]),
203 				"=m" (kf.sf_c.c_sc.sc_ss.ss_fpstate.fpf_fpcr)
204 				: : "memory");
205 		}
206 		break;
207 	}
208 #ifdef DEBUG
209 	if ((sigdebug & SDB_FPSTATE) && *(char *)&kf.sf_c.c_sc.sc_ss.ss_fpstate)
210 		printf("setup_linux_sigframe(%d): copy out FP state (%x) to %p\n",
211 		       p->p_pid, *(u_int *)&kf.sf_c.c_sc.sc_ss.ss_fpstate,
212 		       &kf.sf_c.c_sc.sc_ss.ss_fpstate);
213 #endif
214 
215 	/* Build the signal context to be used by sigreturn. */
216 #if LINUX__NSIG_WORDS > 1
217 	native_to_linux_old_extra_sigset(&kf.sf_c.c_sc.sc_mask,
218 	    kf.sf_c.c_extrasigmask, mask);
219 #else
220 	native_to_linux_old_sigset(&kf.sf_c.c_sc.sc_mask, mask);
221 #endif
222 	kf.sf_c.c_sc.sc_sp = frame->f_regs[SP];
223 	kf.sf_c.c_sc.sc_pc = frame->f_pc;
224 	kf.sf_c.c_sc.sc_ps = frame->f_sr;
225 	sendsig_reset(l, sig);
226 
227 	mutex_exit(p->p_lock);
228 	error = copyout(&kf, fp, sizeof(struct linux_sigframe));
229 	mutex_enter(p->p_lock);
230 
231 	if (error) {
232 #ifdef DEBUG
233 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
234 			printf("setup_linux_sigframe(%d): copyout failed on sig %d\n",
235 			       p->p_pid, sig);
236 #endif
237 		/*
238 		 * Process has trashed its stack; give it a segmentation
239 		 * violation to halt it in its tracks.
240 		 */
241 		sigexit(l, SIGSEGV);
242 		/* NOTREACHED */
243 	}
244 
245 	/*
246 	 * The signal trampoline is on the signal frame.
247 	 * Clear the instruction cache in case of cached.
248 	 */
249 	cachectl1(CC_EXTPURGE | CC_IPURGE,
250 			(vaddr_t) fp->sf_sigtramp, sizeof fp->sf_sigtramp, p);
251 
252 	/* Set up the user stack pointer. */
253 	frame->f_regs[SP] = (int)fp;
254 
255 #ifdef DEBUG
256 	if (sigdebug & SDB_FOLLOW)
257 		printf("setup_linux_sigframe(%d): sig %d scp %p fp %p sc_sp %x\n",
258 		       p->p_pid, sig, kf.sf_scp, fp, kf.sf_c.c_sc.sc_sp);
259 #endif
260 }
261 
262 /*
263  * Setup signal frame for new RT signal interface.
264  */
265 void
setup_linux_rt_sigframe(struct frame * frame,int sig,const sigset_t * mask,void * usp,struct lwp * l)266 setup_linux_rt_sigframe(struct frame *frame, int sig, const sigset_t *mask, void *usp, struct lwp *l)
267 {
268 	struct proc *p = l->l_proc;
269 	struct linux_rt_sigframe *fp, kf;
270 	int error;
271 	short ft;
272 
273 	ft = frame->f_format;
274 
275 	/* Allocate space for the signal handler context on the user stack. */
276 	fp = (struct linux_rt_sigframe *) usp;
277 	fp--;
278 
279 #ifdef DEBUG
280 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
281 		printf("setup_linux_rt_sigframe(%d): sig %d ssp %p usp %p ucp %p ft %d\n",
282 		       p->p_pid, sig, &ft, fp, &fp->sf_uc, ft);
283 #endif
284 
285 	memset(&kf, 0, sizeof(kf));
286 
287 	/* Build stack frame. */
288 	kf.sf_psigtramp = fp->sf_sigtramp;	/* return addr for handler */
289 	kf.sf_signum = native_to_linux_signo[sig];
290 	kf.sf_pinfo = &fp->sf_info;
291 	kf.sf_puc = &fp->sf_uc;
292 
293 	/* The sigtramp code is on the stack frame on Linux/m68k. */
294 	kf.sf_sigtramp[0] = LINUX_RT_SF_SIGTRAMP0;
295 	kf.sf_sigtramp[1] = LINUX_RT_SF_SIGTRAMP1;
296 
297 	/* clear for security (and initialize uc_flags, ss_format, etc.). */
298 	memset(&kf.sf_uc, 0, sizeof(struct linux_ucontext));
299 
300 	/*
301 	 * Save necessary hardware state.  Currently this includes:
302 	 *	- general registers
303 	 *	- original exception frame (if not a "normal" frame)
304 	 *	- FP coprocessor state
305 	 */
306 	/* version of mcontext */
307 	kf.sf_uc.uc_mc.mc_version = LINUX_MCONTEXT_VERSION;
308 
309 	/* general registers and pc/sr */
310 	memcpy(kf.sf_uc.uc_mc.mc_gregs.gr_regs, frame->f_regs, sizeof(u_int)*16);
311 	kf.sf_uc.uc_mc.mc_gregs.gr_pc = frame->f_pc;
312 	kf.sf_uc.uc_mc.mc_gregs.gr_sr = frame->f_sr;
313 
314 	if (ft >= FMT4) {
315 #ifdef DEBUG
316 		if (ft > 15 || exframesize[ft] < 0)
317 			panic("setup_linux_rt_sigframe: bogus frame type");
318 #endif
319 		kf.sf_uc.uc_ss.ss_format = ft;
320 		kf.sf_uc.uc_ss.ss_vector = frame->f_vector;
321 		memcpy(&kf.sf_uc.uc_ss.ss_frame, &frame->F_u,
322 			(size_t) exframesize[ft]);
323 		/*
324 		 * Leave an indicator that we need to clean up the kernel
325 		 * stack.  We do this by setting the "pad word" above the
326 		 * hardware stack frame to the amount the stack must be
327 		 * adjusted by.
328 		 *
329 		 * N.B. we increment rather than just set f_stackadj in
330 		 * case we are called from syscall when processing a
331 		 * sigreturn.  In that case, f_stackadj may be non-zero.
332 		 */
333 		frame->f_stackadj += exframesize[ft];
334 		frame->f_format = frame->f_vector = 0;
335 #ifdef DEBUG
336 		if (sigdebug & SDB_FOLLOW)
337 			printf("setup_linux_rt_sigframe(%d): copy out %d of frame %d\n",
338 			       p->p_pid, exframesize[ft], ft);
339 #endif
340 	}
341 
342 	switch (fputype) {
343 	case FPU_NONE:
344 		break;
345 #ifdef M68060
346 	case FPU_68060:
347 		__asm("fsave %0" : "=m" (kf.sf_uc.uc_ss.ss_fpstate));
348 				/* See note below. */
349 		if (((struct fpframe060 *) &kf.sf_uc.uc_ss.ss_fpstate.FPF_u1)
350 					->fpf6_frmfmt != FPF6_FMT_NULL) {
351 			__asm("fmovem %%fp0-%%fp7,%0" :
352 				"=m" (kf.sf_uc.uc_mc.mc_fpregs.fpr_regs[0][0]));
353 			/*
354 			 * On 060,  "fmovem fpcr/fpsr/fpi,<ea>"  is
355 			 * emulated by software and slow.
356 			 */
357 			__asm("fmovem %%fpcr,%0; fmovem %%fpsr,%1; fmovem %%fpi,%2" :
358 				"=m" (kf.sf_uc.uc_mc.mc_fpregs.fpr_fpcr),
359 				"=m" (kf.sf_uc.uc_mc.mc_fpregs.fpr_fpsr),
360 				"=m" (kf.sf_uc.uc_mc.mc_fpregs.fpr_fpiar));
361 		}
362 		break;
363 #endif
364 	default:
365 		/*
366 		 * NOTE:  We give whole of the  "struct linux_rt_fpframe"
367 		 * to the __asm("fsave") argument; not the FPF_u1 element only.
368 		 * Unlike the non-RT version of this structure,
369 		 * this contains only the FPU state used by "fsave"
370 		 * (and whole of the information is in the structure).
371 		 * This gives the correct dependency information to the __asm(),
372 		 * and no "memory" is required to the ``clobberd'' list.
373 		 */
374 		__asm("fsave %0" : "=m" (kf.sf_uc.uc_ss.ss_fpstate));
375 		if (kf.sf_uc.uc_ss.ss_fpstate.fpf_version) {
376 			__asm("fmovem %%fp0-%%fp7,%0; fmovem %%fpcr/%%fpsr/%%fpi,%1" :
377 				"=m" (kf.sf_uc.uc_mc.mc_fpregs.fpr_regs[0][0]),
378 				"=m" (kf.sf_uc.uc_mc.mc_fpregs.fpr_fpcr)
379 				: : "memory");
380 		}
381 		break;
382 	}
383 #ifdef DEBUG
384 	if ((sigdebug & SDB_FPSTATE) && *(char *)&kf.sf_uc.uc_ss.ss_fpstate)
385 		printf("setup_linux_rt_sigframe(%d): copy out FP state (%x) to %p\n",
386 		       p->p_pid, *(u_int *)&kf.sf_uc.uc_ss.ss_fpstate,
387 		       &kf.sf_uc.uc_ss.ss_fpstate);
388 #endif
389 
390 	/*
391 	 * XXX XAX Create bogus siginfo data.  This can't really
392 	 * XXX be fixed until NetBSD has realtime signals.
393 	 * XXX Or we do the emuldata thing.
394 	 * XXX -erh
395 	 */
396 	memset(&kf.sf_info, 0, sizeof(struct linux_siginfo));
397 	kf.sf_info.lsi_signo = sig;
398 	kf.sf_info.lsi_code = LINUX_SI_USER;
399 	kf.sf_info.lsi_pid = p->p_pid;
400 	kf.sf_info.lsi_uid = kauth_cred_geteuid(l->l_cred);	/* Use real uid here? */
401 
402 	/* Build the signal context to be used by sigreturn. */
403 	native_to_linux_sigset(&kf.sf_uc.uc_sigmask, mask);
404 	kf.sf_uc.uc_stack.ss_sp = l->l_sigstk.ss_sp;
405 	kf.sf_uc.uc_stack.ss_flags =
406 		(l->l_sigstk.ss_flags & SS_ONSTACK ? LINUX_SS_ONSTACK : 0) |
407 		(l->l_sigstk.ss_flags & SS_DISABLE ? LINUX_SS_DISABLE : 0);
408 	kf.sf_uc.uc_stack.ss_size = l->l_sigstk.ss_size;
409 	sendsig_reset(l, sig);
410 
411 	mutex_exit(p->p_lock);
412 	error = copyout(&kf, fp, sizeof(struct linux_rt_sigframe));
413 	mutex_enter(p->p_lock);
414 
415 	if (error) {
416 #ifdef DEBUG
417 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
418 			printf("setup_linux_rt_sigframe(%d): copyout failed on sig %d\n",
419 			       p->p_pid, sig);
420 #endif
421 		/*
422 		 * Process has trashed its stack; give it a segmentation
423 		 * violation to halt it in its tracks.
424 		 */
425 		sigexit(l, SIGSEGV);
426 		/* NOTREACHED */
427 	}
428 
429 	/*
430 	 * The signal trampoline is on the signal frame.
431 	 * Clear the instruction cache in case of cached.
432 	 */
433 	cachectl1(CC_EXTPURGE | CC_IPURGE,
434 			(vaddr_t) fp->sf_sigtramp, sizeof fp->sf_sigtramp, p);
435 
436 	/* Set up the user stack pointer. */
437 	frame->f_regs[SP] = (int)fp;
438 
439 #ifdef DEBUG
440 	if (sigdebug & SDB_FOLLOW)
441 		printf("setup_linux_rt_sigframe(%d): sig %d puc %p fp %p sc_sp %x\n",
442 		       p->p_pid, sig, kf.sf_puc, fp,
443 		       kf.sf_uc.uc_mc.mc_gregs.gr_regs[SP]);
444 #endif
445 }
446 
447 /*
448  * Send an interrupt to Linux process.
449  */
450 void
linux_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)451 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
452 {
453 	/* u_long code = ksi->ksi_trap; */
454 	int sig = ksi->ksi_signo;
455 	struct lwp *l = curlwp;
456 	struct proc *p = l->l_proc;
457 	struct frame *frame = (struct frame *)l->l_md.md_regs;
458 	int onstack;
459 	/* user stack for signal context */
460 	void *usp = getframe(l, sig, &onstack);
461 	sig_t catcher = SIGACTION(p, sig).sa_handler;
462 
463 	/* Setup the signal frame (and part of the trapframe). */
464 	if (SIGACTION(p, sig).sa_flags & SA_SIGINFO)
465 		setup_linux_rt_sigframe(frame, sig, mask, usp, l);
466 	else
467 		setup_linux_sigframe(frame, sig, mask, usp);
468 
469 	/* Call the signal handler. */
470 	frame->f_pc = (u_int) catcher;
471 
472 	/* Remember that we're now on the signal stack. */
473 	if (onstack)
474 		l->l_sigstk.ss_flags |= SS_ONSTACK;
475 
476 #ifdef DEBUG
477 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
478 		printf("linux_sendsig(%d): sig %d returns\n",
479 		       p->p_pid, sig);
480 #endif
481 }
482 
483 /*
484  * The linux_sys_sigreturn and linux_sys_rt_sigreturn
485  * system calls cleanup state after a signal
486  * has been taken.  Reset signal mask and stack
487  * state from context left by linux_sendsig (above).
488  * Return to previous pc and psl as specified by
489  * context left by linux_sendsig. Check carefully to
490  * make sure that the user has not modified the
491  * psl to gain improper privileges or to cause
492  * a machine fault.
493  *
494  * Note that the sigreturn system calls of Linux/m68k
495  * do not return on errors, but issue segmentation
496  * violation and terminate the process.
497  */
498 /* ARGSUSED */
499 int
linux_sys_sigreturn(struct lwp * l,const void * v,register_t * retval)500 linux_sys_sigreturn(struct lwp *l, const void *v, register_t *retval)
501 {
502 	struct proc *p = l->l_proc;
503 	struct frame *frame;
504 	struct linux_sigc2 tsigc2;	/* extra mask and sigcontext */
505 	struct linux_sigcontext *scp;	/* pointer to sigcontext */
506 	sigset_t mask;
507 	int sz = 0;			/* extra frame size */
508 	int usp;
509 
510 	/*
511 	 * sigreturn of Linux/m68k takes no arguments.
512 	 * The user stack points at struct linux_sigc2.
513 	 */
514 	frame = (struct frame *) l->l_md.md_regs;
515 	usp = frame->f_regs[SP];
516 	if (usp & 1)
517 		goto bad;
518 
519 #ifdef DEBUG
520 	if (sigdebug & SDB_FOLLOW)
521 		printf("linux_sys_sigreturn: pid %d, usp %p\n",
522 			p->p_pid, (void *) usp);
523 #endif
524 
525 	/* Grab whole of the sigcontext. */
526 	if (copyin((void *) usp, &tsigc2, sizeof tsigc2)) {
527 bad:
528 		mutex_enter(p->p_lock);
529 		sigexit(l, SIGSEGV);
530 	}
531 
532 	scp = &tsigc2.c_sc;
533 
534 	/*
535 	 * Check kernel stack and re-enter to syscall() if needed.
536 	 */
537 	if ((sz = scp->sc_ss.ss_format) != 0) {
538 		if ((sz = exframesize[sz]) < 0)
539 			goto bad;
540 		if (sz && frame->f_stackadj == 0) {
541 			/*
542 			 * Extra stack space is required but not allocated.
543 			 * Allocate and re-enter syscall().
544 			 */
545 			reenter_syscall(frame, sz);
546 			/* NOTREACHED */
547 		}
548 	}
549 #ifdef DEBUG
550 	/* reenter_syscall() doesn't adjust stack. */
551 	if (sz != frame->f_stackadj)
552 		panic("linux_sys_sigreturn: adj: %d != %d",
553 			sz, frame->f_stackadj);
554 #endif
555 
556 	mutex_enter(p->p_lock);
557 
558 	/* Restore signal stack. */
559 	l->l_sigstk.ss_flags &= ~SS_ONSTACK;
560 
561 	/* Restore signal mask. */
562 #if LINUX__NSIG_WORDS > 1
563 	linux_old_extra_to_native_sigset(&mask, &scp->sc_mask,
564 					 tsigc2.c_extrasigmask);
565 #else
566 	linux_old_to_native_sigset(&scp->sc_mask, &mask);
567 #endif
568 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
569 
570 	mutex_exit(p->p_lock);
571 
572 	/*
573 	 * Restore the user supplied information.
574 	 */
575 	frame->f_regs[SP] = scp->sc_sp;
576 	frame->f_regs[D0] = scp->sc_d0;
577 	frame->f_regs[D1] = scp->sc_d1;
578 	frame->f_regs[A0] = scp->sc_a0;
579 	frame->f_regs[A1] = scp->sc_a1;
580 	frame->f_pc = scp->sc_pc;
581 	/* Privileged bits of  sr  are silently ignored on Linux/m68k. */
582 	frame->f_sr = scp->sc_ps & ~(PSL_MBZ|PSL_IPL|PSL_S);
583 	/*
584 	 * Other registers are assumed to be unchanged,
585 	 * and not restored.
586 	 */
587 
588 	/*
589 	 * Restore long stack frames.  Note that we do not copy
590 	 * back the saved SR or PC, they were picked up above from
591 	 * the sigcontext structure.
592 	 */
593 	if (scp->sc_ss.ss_format) {
594 		frame->f_format = scp->sc_ss.ss_format;
595 		frame->f_vector = scp->sc_ss.ss_vector;
596 		if (frame->f_stackadj < sz)	/* just in case... */
597 			goto bad;
598 		frame->f_stackadj -= sz;
599 		memcpy(&frame->F_u, &scp->sc_ss.ss_frame, sz);
600 #ifdef DEBUG
601 		if (sigdebug & SDB_FOLLOW)
602 			printf("linux_sys_sigreturn(%d): copy in %d of frame type %d\n",
603 			       p->p_pid, sz, scp->sc_ss.ss_format);
604 #endif
605 	}
606 
607 	/*
608 	 * Finally we restore the original FP context.
609 	 */
610 	switch (fputype) {
611 	case FPU_NONE:
612 		break;
613 #ifdef M68060
614 	case FPU_68060:
615 		if (((struct fpframe060*)&scp->sc_ss.ss_fpstate.FPF_u1)
616 					->fpf6_frmfmt != FPF6_FMT_NULL) {
617 			/*
618 			 * On 060,  "fmovem <ea>,fpcr/fpsr/fpi"  is
619 			 * emulated by software and slow.
620 			 */
621 			__asm("fmovem %0,%%fpcr; fmovem %1,%%fpsr; fmovem %2,%%fpi"::
622 				"m" (scp->sc_ss.ss_fpstate.fpf_fpcr),
623 				"m" (scp->sc_ss.ss_fpstate.fpf_fpsr),
624 				"m" (scp->sc_ss.ss_fpstate.fpf_fpiar));
625 			__asm("fmovem %0,%%fp0-%%fp1" : :
626 				"m" (scp->sc_ss.ss_fpstate.fpf_regs[0][0]));
627 		}
628 		__asm("frestore %0" : : "m" (scp->sc_ss.ss_fpstate.FPF_u1));
629 		break;
630 #endif
631 	default:
632 		if (scp->sc_ss.ss_fpstate.fpf_version) {
633 			__asm("fmovem %0,%%fpcr/%%fpsr/%%fpi; fmovem %1,%%fp0-%%fp1"::
634 				"m" (scp->sc_ss.ss_fpstate.fpf_fpcr),
635 				"m" (scp->sc_ss.ss_fpstate.fpf_regs[0][0]));
636 		}
637 		__asm("frestore %0" : : "m" (scp->sc_ss.ss_fpstate.FPF_u1));
638 		break;
639 	}
640 
641 #ifdef DEBUG
642 	if ((sigdebug & SDB_FPSTATE) && *(char *)&scp->sc_ss.ss_fpstate)
643 		printf("linux_sys_sigreturn(%d): copied in FP state (%x) at %p\n",
644 		       p->p_pid, *(u_int *)&scp->sc_ss.ss_fpstate,
645 		       &scp->sc_ss.ss_fpstate);
646 	if ((sigdebug & SDB_FOLLOW) ||
647 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
648 		printf("linux_sys_sigreturn(%d): returns\n", p->p_pid);
649 #endif
650 
651 	return EJUSTRETURN;
652 }
653 
654 /* ARGSUSED */
655 int
linux_sys_rt_sigreturn(struct lwp * l,const void * v,register_t * retval)656 linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
657 {
658 	struct proc *p = l->l_proc;
659 	struct frame *frame;
660 	struct linux_ucontext *ucp;	/* ucontext in user space */
661 	struct linux_ucontext tuc;	/* copy of *ucp */
662 	sigset_t mask;
663 	int sz = 0, error;		/* extra frame size */
664 
665 	/*
666 	 * rt_sigreturn of Linux/m68k takes no arguments.
667 	 * usp + 4 is a pointer to siginfo structure,
668 	 * usp + 8 is a pointer to ucontext structure.
669 	 */
670 	frame = (struct frame *) l->l_md.md_regs;
671 	error = copyin((char *)frame->f_regs[SP] + 8, (void *)&ucp,
672 	    sizeof(void *));
673 	if (error || (int) ucp & 1)
674 		goto bad;		/* error or odd address */
675 
676 #ifdef DEBUG
677 	if (sigdebug & SDB_FOLLOW)
678 		printf("linux_rt_sigreturn: pid %d, ucp %p\n", p->p_pid, ucp);
679 #endif
680 
681 	/* Grab whole of the ucontext. */
682 	if (copyin(ucp, &tuc, sizeof tuc)) {
683 bad:
684 		mutex_enter(p->p_lock);
685 		sigexit(l, SIGSEGV);
686 	}
687 
688 	/*
689 	 * Check kernel stack and re-enter to syscall() if needed.
690 	 */
691 	if ((sz = tuc.uc_ss.ss_format) != 0) {
692 		if ((sz = exframesize[sz]) < 0)
693 			goto bad;
694 		if (sz && frame->f_stackadj == 0) {
695 			/*
696 			 * Extra stack space is required but not allocated.
697 			 * Allocate and re-enter syscall().
698 			 */
699 			reenter_syscall(frame, sz);
700 			/* NOTREACHED */
701 		}
702 	}
703 #ifdef DEBUG
704 	/* reenter_syscall() doesn't adjust stack. */
705 	if (sz != frame->f_stackadj)
706 		panic("linux_sys_rt_sigreturn: adj: %d != %d",
707 			sz, frame->f_stackadj);
708 #endif
709 
710 	if (tuc.uc_mc.mc_version != LINUX_MCONTEXT_VERSION)
711 		goto bad;
712 
713 	mutex_enter(p->p_lock);
714 
715 	/* Restore signal stack. */
716 	l->l_sigstk.ss_flags =
717 		(l->l_sigstk.ss_flags & ~SS_ONSTACK) |
718 		(tuc.uc_stack.ss_flags & LINUX_SS_ONSTACK ? SS_ONSTACK : 0);
719 
720 	/* Restore signal mask. */
721 	linux_to_native_sigset(&mask, &tuc.uc_sigmask);
722 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
723 
724 	mutex_exit(p->p_lock);
725 
726 	/*
727 	 * Restore the user supplied information.
728 	 */
729 	memcpy(frame->f_regs, tuc.uc_mc.mc_gregs.gr_regs, sizeof(u_int)*16);
730 	frame->f_pc = tuc.uc_mc.mc_gregs.gr_pc;
731 	/* Privileged bits of  sr  are silently ignored on Linux/m68k. */
732 	frame->f_sr = tuc.uc_mc.mc_gregs.gr_sr & ~(PSL_MBZ|PSL_IPL|PSL_S);
733 
734 	/*
735 	 * Restore long stack frames.  Note that we do not copy
736 	 * back the saved SR or PC, they were picked up above from
737 	 * the ucontext structure.
738 	 */
739 	if (tuc.uc_ss.ss_format) {
740 		frame->f_format = tuc.uc_ss.ss_format;
741 		frame->f_vector = tuc.uc_ss.ss_vector;
742 		if (frame->f_stackadj < sz)	/* just in case... */
743 			goto bad;
744 		frame->f_stackadj -= sz;
745 		memcpy(&frame->F_u, &tuc.uc_ss.ss_frame, sz);
746 #ifdef DEBUG
747 		if (sigdebug & SDB_FOLLOW)
748 			printf("linux_sys_rt_sigreturn(%d): copy in %d of frame type %d\n",
749 			       p->p_pid, sz, tuc.uc_ss.ss_format);
750 #endif
751 	}
752 
753 	/*
754 	 * Finally we restore the original FP context.
755 	 */
756 	switch (fputype) {
757 	case FPU_NONE:
758 		break;
759 #ifdef M68060
760 	case FPU_68060:
761 		if (((struct fpframe060*)&tuc.uc_ss.ss_fpstate.FPF_u1)
762 					->fpf6_frmfmt != FPF6_FMT_NULL) {
763 			/*
764 			 * On 060,  "fmovem <ea>,fpcr/fpsr/fpi"  is
765 			 * emulated by software and slow.
766 			 */
767 			__asm("fmovem %0,%%fpcr; fmovem %1,%%fpsr; fmovem %2,%%fpi"::
768 				"m" (tuc.uc_mc.mc_fpregs.fpr_fpcr),
769 				"m" (tuc.uc_mc.mc_fpregs.fpr_fpsr),
770 				"m" (tuc.uc_mc.mc_fpregs.fpr_fpiar));
771 			__asm("fmovem %0,%%fp0-%%fp1" : :
772 				"m" (tuc.uc_mc.mc_fpregs.fpr_regs[0][0]));
773 		}
774 		__asm("frestore %0" : : "m" (tuc.uc_ss.ss_fpstate.FPF_u1));
775 		break;
776 #endif
777 	default:
778 		if (tuc.uc_ss.ss_fpstate.fpf_version) {
779 			__asm("fmovem %0,%%fpcr/%%fpsr/%%fpi; fmovem %1,%%fp0-%%fp1"::
780 				"m" (tuc.uc_mc.mc_fpregs.fpr_fpcr),
781 				"m" (tuc.uc_mc.mc_fpregs.fpr_regs[0][0]));
782 		}
783 		__asm("frestore %0" : : "m" (tuc.uc_ss.ss_fpstate.FPF_u1));
784 		break;
785 	}
786 
787 #ifdef DEBUG
788 	if ((sigdebug & SDB_FPSTATE) && *(char *)&tuc.uc_ss.ss_fpstate)
789 		printf("linux_rt_sigreturn(%d): copied in FP state (%x) at %p\n",
790 		       p->p_pid, *(u_int *)&tuc.uc_ss.ss_fpstate,
791 		       &tuc.uc_ss.ss_fpstate);
792 	if ((sigdebug & SDB_FOLLOW) ||
793 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
794 		printf("linux_rt_sigreturn(%d): returns\n", p->p_pid);
795 #endif
796 
797 	return EJUSTRETURN;
798 }
799 
800 /*
801  * MPU cache operation of Linux/m68k,
802  * mainly used for dynamic linking.
803  */
804 
805 /* scope */
806 #define LINUX_FLUSH_SCOPE_LINE	1	/* a cache line */
807 #define LINUX_FLUSH_SCOPE_PAGE	2	/* a page */
808 #define LINUX_FLUSH_SCOPE_ALL	3	/* the whole cache */
809 /* cache */
810 #define LINUX_FLUSH_CACHE_DATA	1	/* flush and purge data cache */
811 #define LINUX_FLUSH_CACHE_INSN	2	/* purge instruction cache */
812 #define LINUX_FLUSH_CACHE_BOTH	3	/* both */
813 
814 /* ARGSUSED */
815 int
linux_sys_cacheflush(struct lwp * l,const struct linux_sys_cacheflush_args * uap,register_t * retval)816 linux_sys_cacheflush(struct lwp *l, const struct linux_sys_cacheflush_args *uap, register_t *retval)
817 {
818 	/* {
819 		syscallarg(unsigned long)	addr;
820 		syscallarg(int)			scope;
821 		syscallarg(int)			cache;
822 		syscallarg(unsigned long)	len;
823 	} */
824 	struct proc *p = l->l_proc;
825 	int scope, cache;
826 	vaddr_t addr;
827 	int len;
828 	int error;
829 
830 	scope = SCARG(uap, scope);
831 	cache = SCARG(uap, cache);
832 
833 	if (scope < LINUX_FLUSH_SCOPE_LINE || scope > LINUX_FLUSH_SCOPE_ALL
834 				|| cache & ~LINUX_FLUSH_CACHE_BOTH)
835 		return EINVAL;
836 
837 #if defined(M68040) || defined(M68060)
838 	addr = (vaddr_t) SCARG(uap, addr);
839 	len = (int) SCARG(uap, len);
840 #else
841 	/*
842 	 * We always flush entire cache on 68020/030
843 	 * and these values are not used afterwards.
844 	 */
845 	addr = 0;
846 	len = 0;
847 #endif
848 
849 	/*
850 	 * LINUX_FLUSH_SCOPE_ALL (flush whole cache) is limited to super users.
851 	 */
852 	if (scope == LINUX_FLUSH_SCOPE_ALL) {
853 		if ((error = kauth_authorize_machdep(l->l_cred,
854 		    KAUTH_MACHDEP_CACHEFLUSH, NULL, NULL, NULL, NULL)) != 0)
855 			return error;
856 #if defined(M68040) || defined(M68060)
857 		/* entire cache */
858 		len = INT_MAX;
859 #endif
860 	}
861 
862 	error = 0;
863 	if (cache & LINUX_FLUSH_CACHE_DATA)
864 		if ((error = cachectl1(CC_EXTPURGE|CC_PURGE, addr, len, p)) !=0)
865 			return error;
866 	if (cache & LINUX_FLUSH_CACHE_INSN)
867 		error = cachectl1(CC_EXTPURGE|CC_IPURGE, addr, len, p);
868 
869 	return error;
870 }
871 
872 /*
873  * Convert NetBSD's devices to Linux's.
874  */
875 dev_t
linux_fakedev(dev_t dev,int raw)876 linux_fakedev(dev_t dev, int raw)
877 {
878 
879 	/* do nothing for now */
880 	return dev;
881 }
882 
883 /*
884  * We come here in a last attempt to satisfy a Linux ioctl() call.
885  */
886 int
linux_machdepioctl(struct lwp * l,const struct linux_sys_ioctl_args * uap,register_t * retval)887 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
888 {
889 	/* {
890 		syscallarg(int) fd;
891 		syscallarg(u_long) com;
892 		syscallarg(void *) data;
893 	} */
894 	struct sys_ioctl_args bia;
895 	u_long com;
896 
897 	SCARG(&bia, fd) = SCARG(uap, fd);
898 	SCARG(&bia, data) = SCARG(uap, data);
899 	com = SCARG(uap, com);
900 
901 	switch (com) {
902 
903 	/* do nothing for now */
904 
905 	default:
906 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
907 		return EINVAL;
908 	}
909 	SCARG(&bia, com) = com;
910 	return sys_ioctl(l, &bia, retval);
911 }
912 
913 int
linux_usertrap(struct lwp * l,vaddr_t trapaddr,void * arg)914 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
915 {
916 	return 0;
917 }
918