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