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