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