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