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