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