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