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