Home | History | Annotate | Line # | Download | only in alpha
linux_machdep.c revision 1.15.2.5
      1 /*	$NetBSD: linux_machdep.c,v 1.15.2.5 2001/11/14 19:13:02 nathanw 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 Eric Haszlakiewicz.
      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  * Based on sys/arch/i386/i386/linux_machdep.c:
     39  *	linux_machdep.c,v 1.42 1998/09/11 12:50:06 mycroft Exp
     40  *	written by Frank van der Linden
     41  *
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.15.2.5 2001/11/14 19:13:02 nathanw Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/signalvar.h>
     50 #include <sys/kernel.h>
     51 #include <sys/map.h>
     52 #include <sys/proc.h>
     53 #include <sys/user.h>
     54 #include <sys/buf.h>
     55 #include <sys/reboot.h>
     56 #include <sys/conf.h>
     57 #include <sys/exec.h>
     58 #include <sys/file.h>
     59 #include <sys/callout.h>
     60 #include <sys/malloc.h>
     61 #include <sys/mbuf.h>
     62 #include <sys/msgbuf.h>
     63 #include <sys/mount.h>
     64 #include <sys/vnode.h>
     65 #include <sys/device.h>
     66 #include <sys/syscallargs.h>
     67 #include <sys/filedesc.h>
     68 #include <sys/exec_elf.h>
     69 #include <sys/ioctl.h>
     70 
     71 #include <uvm/uvm_extern.h>
     72 
     73 #include <compat/linux/common/linux_types.h>
     74 #include <compat/linux/common/linux_signal.h>
     75 #include <compat/linux/common/linux_siginfo.h>
     76 #include <compat/linux/common/linux_util.h>
     77 #include <compat/linux/common/linux_ioctl.h>
     78 #include <compat/linux/common/linux_exec.h>
     79 #include <compat/linux/common/linux_machdep.h>
     80 #include <compat/linux/common/linux_emuldata.h>
     81 
     82 #include <compat/linux/linux_syscallargs.h>
     83 
     84 #include <machine/alpha.h>
     85 #include <machine/reg.h>
     86 
     87 #if defined(_KERNEL_OPT)
     88 #include "wsdisplay.h"
     89 #endif
     90 #if (NWSDISPLAY >0)
     91 #include <dev/wscons/wsdisplay_usl_io.h>
     92 #endif
     93 #ifdef DEBUG
     94 #include <machine/sigdebug.h>
     95 #endif
     96 
     97 /*
     98  * Deal with some alpha-specific things in the Linux emulation code.
     99  */
    100 
    101 void
    102 linux_setregs(l, epp, stack)
    103 	struct lwp *l;
    104 	struct exec_package *epp;
    105 	u_long stack;
    106 {
    107 #ifdef DEBUG
    108 	struct trapframe *tfp = l->l_md.md_tf;
    109 #endif
    110 
    111 	setregs(l, epp, stack);
    112 #ifdef DEBUG
    113 	/*
    114 	 * Linux has registers set to zero on entry; for DEBUG kernels
    115 	 * the alpha setregs() fills registers with 0xbabefacedeadbeef.
    116 	 */
    117 	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
    118 #endif
    119 }
    120 
    121 void setup_linux_rt_sigframe(tf, sig, mask)
    122 	struct trapframe *tf;
    123 	int sig;
    124 	sigset_t *mask;
    125 {
    126 	struct lwp *l = curproc;
    127 	struct proc *p = l->l_proc;
    128 	struct linux_rt_sigframe *sfp, sigframe;
    129 	int onstack;
    130 	int fsize, rndfsize;
    131 	extern char linux_rt_sigcode[], linux_rt_esigcode[];
    132 
    133 	/* Do we need to jump onto the signal stack? */
    134 	onstack = (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    135 		  (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    136 
    137 	/* Allocate space for the signal handler context.  */
    138 	fsize = sizeof(struct linux_rt_sigframe);
    139 	rndfsize = ((fsize + 15) / 16) * 16;
    140 
    141 	if (onstack)
    142 		sfp = (struct linux_rt_sigframe *)
    143 					((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
    144 						p->p_sigctx.ps_sigstk.ss_size);
    145 	else
    146 		sfp = (struct linux_rt_sigframe *)(alpha_pal_rdusp());
    147 	sfp = (struct linux_rt_sigframe *)((caddr_t)sfp - rndfsize);
    148 
    149 #ifdef DEBUG
    150 	if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
    151 		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
    152 		    sig, &onstack, sfp);
    153 #endif /* DEBUG */
    154 
    155 	/*
    156 	 * Build the signal context to be used by sigreturn.
    157 	 */
    158 	bzero(&sigframe.uc, sizeof(struct linux_ucontext));
    159 	sigframe.uc.uc_mcontext.sc_onstack = onstack;
    160 
    161 	/* Setup potentially partial signal mask in sc_mask. */
    162 	/* But get all of it in uc_sigmask */
    163 	native_to_linux_old_sigset(mask, &sigframe.uc.uc_mcontext.sc_mask);
    164 	native_to_linux_sigset(mask, &sigframe.uc.uc_sigmask);
    165 
    166 	sigframe.uc.uc_mcontext.sc_pc = tf->tf_regs[FRAME_PC];
    167 	sigframe.uc.uc_mcontext.sc_ps = ALPHA_PSL_USERMODE;
    168 	frametoreg(tf, (struct reg *)sigframe.uc.uc_mcontext.sc_regs);
    169 	sigframe.uc.uc_mcontext.sc_regs[R_SP] = alpha_pal_rdusp();
    170 
    171 	alpha_enable_fp(l, 1);
    172 	sigframe.uc.uc_mcontext.sc_fpcr = alpha_read_fpcr();
    173 	sigframe.uc.uc_mcontext.sc_fp_control = alpha_read_fp_c(l);
    174 	alpha_pal_wrfen(0);
    175 
    176 	sigframe.uc.uc_mcontext.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
    177 	sigframe.uc.uc_mcontext.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
    178 	sigframe.uc.uc_mcontext.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
    179 
    180 	/*
    181 	 * XXX XAX Create bogus siginfo data.  This can't really
    182 	 * XXX be fixed until NetBSD has realtime signals.
    183 	 * XXX Or we do the emuldata thing.
    184 	 * XXX -erh
    185 	 */
    186 	bzero(&sigframe.info, sizeof(struct linux_siginfo));
    187 	sigframe.info.lsi_signo = sig;
    188 	sigframe.info.lsi_code = LINUX_SI_USER;
    189 	sigframe.info.lsi_pid = p->p_pid;
    190 	sigframe.info.lsi_uid = p->p_ucred->cr_uid;	/* Use real uid here? */
    191 
    192 	if (copyout((caddr_t)&sigframe, (caddr_t)sfp, fsize) != 0) {
    193 #ifdef DEBUG
    194 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    195 			printf("sendsig(%d): copyout failed on sig %d\n",
    196 			    p->p_pid, sig);
    197 #endif
    198 		/*
    199 		 * Process has trashed its stack; give it an illegal
    200 		 * instruction to halt it in its tracks.
    201 		 */
    202 		sigexit(l, SIGILL);
    203 		/* NOTREACHED */
    204 	}
    205 
    206 	/* Pass pointers to siginfo and ucontext in the regs */
    207 	tf->tf_regs[FRAME_A1] = (unsigned long)&sfp->info;
    208 	tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->uc;
    209 
    210 	/* Address of trampoline code.  End up at this PC after mi_switch */
    211 	tf->tf_regs[FRAME_PC] =
    212 	    (u_int64_t)(PS_STRINGS - (linux_rt_esigcode - linux_rt_sigcode));
    213 
    214 	/* Adjust the stack */
    215 	alpha_pal_wrusp((unsigned long)sfp);
    216 
    217 	/* Remember that we're now on the signal stack. */
    218 	if (onstack)
    219 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    220 }
    221 
    222 void setup_linux_sigframe(tf, sig, mask)
    223 	struct trapframe *tf;
    224 	int sig;
    225 	sigset_t *mask;
    226 {
    227 	struct lwp *l = curproc;
    228 	struct proc *p = l->l_proc;
    229 	struct linux_sigframe *sfp, sigframe;
    230 	int onstack;
    231 	int fsize, rndfsize;
    232 	extern char linux_sigcode[], linux_esigcode[];
    233 
    234 	/* Do we need to jump onto the signal stack? */
    235 	onstack = (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    236 		  (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    237 
    238 	/* Allocate space for the signal handler context.  */
    239 	fsize = sizeof(struct linux_sigframe);
    240 	rndfsize = ((fsize + 15) / 16) * 16;
    241 
    242 	if (onstack)
    243 		sfp = (struct linux_sigframe *)
    244 					((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
    245 						p->p_sigctx.ps_sigstk.ss_size);
    246 	else
    247 		sfp = (struct linux_sigframe *)(alpha_pal_rdusp());
    248 	sfp = (struct linux_sigframe *)((caddr_t)sfp - rndfsize);
    249 
    250 #ifdef DEBUG
    251 	if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
    252 		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
    253 		    sig, &onstack, sfp);
    254 #endif /* DEBUG */
    255 
    256 	/*
    257 	 * Build the signal context to be used by sigreturn.
    258 	 */
    259 	bzero(&sigframe.sf_sc, sizeof(struct linux_ucontext));
    260 	sigframe.sf_sc.sc_onstack = onstack;
    261 	native_to_linux_old_sigset(mask, &sigframe.sf_sc.sc_mask);
    262 	sigframe.sf_sc.sc_pc = tf->tf_regs[FRAME_PC];
    263 	sigframe.sf_sc.sc_ps = ALPHA_PSL_USERMODE;
    264 	frametoreg(tf, (struct reg *)sigframe.sf_sc.sc_regs);
    265 	sigframe.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp();
    266 
    267 	if (l == fpcurproc) {
    268 	    alpha_pal_wrfen(1);
    269 	    savefpstate(&l->l_addr->u_pcb.pcb_fp);
    270 	    alpha_pal_wrfen(0);
    271 	    sigframe.sf_sc.sc_fpcr = l->l_addr->u_pcb.pcb_fp.fpr_cr;
    272 	    fpcurproc = NULL;
    273 	}
    274 	/* XXX ownedfp ? etc...? */
    275 
    276 	sigframe.sf_sc.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
    277 	sigframe.sf_sc.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
    278 	sigframe.sf_sc.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
    279 
    280 	if (copyout((caddr_t)&sigframe, (caddr_t)sfp, fsize) != 0) {
    281 #ifdef DEBUG
    282 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    283 			printf("sendsig(%d): copyout failed on sig %d\n",
    284 			    p->p_pid, sig);
    285 #endif
    286 		/*
    287 		 * Process has trashed its stack; give it an illegal
    288 		 * instruction to halt it in its tracks.
    289 		 */
    290 		sigexit(l, SIGILL);
    291 		/* NOTREACHED */
    292 	}
    293 
    294 	/* Pass pointers to sigcontext in the regs */
    295 	tf->tf_regs[FRAME_A1] = 0;
    296 	tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->sf_sc;
    297 
    298 	/* Address of trampoline code.  End up at this PC after mi_switch */
    299 	tf->tf_regs[FRAME_PC] =
    300 	    (u_int64_t)(PS_STRINGS - (linux_esigcode - linux_sigcode));
    301 
    302 	/* Adjust the stack */
    303 	alpha_pal_wrusp((unsigned long)sfp);
    304 
    305 	/* Remember that we're now on the signal stack. */
    306 	if (onstack)
    307 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    308 }
    309 
    310 /*
    311  * Send an interrupt to process.
    312  *
    313  * Stack is set up to allow sigcode stored
    314  * in u. to call routine, followed by kcall
    315  * to sigreturn routine below.  After sigreturn
    316  * resets the signal mask, the stack, and the
    317  * frame pointer, it returns to the user
    318  * specified pc, psl.
    319  */
    320 void
    321 linux_sendsig(catcher, sig, mask, code)
    322 	sig_t catcher;
    323 	int sig;
    324 	sigset_t *mask;
    325 	u_long code;
    326 {
    327 	struct lwp *l = curproc;
    328 	struct trapframe *tf = l->l_md.md_tf;
    329 #ifdef notyet
    330 	struct linux_emuldata *edp;
    331 
    332 	/* Setup the signal frame (and part of the trapframe) */
    333 	/*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/
    334 /*	XXX XAX this is broken now.  need someplace to store what
    335 	XXX XAX kind of signal handler a signal has.*/
    336 #if 0
    337 	edp = (struct linux_emuldata *)p->p_emuldata;
    338 #else
    339 	edp = 0;
    340 #endif
    341 	if (edp && sigismember(&edp->ps_siginfo, sig))
    342 		setup_linux_rt_sigframe(tf, sig, mask);
    343 	else
    344 #endif /* notyet */
    345 		setup_linux_sigframe(tf, sig, mask);
    346 
    347 	/* Signal handler for trampoline code */
    348 	tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
    349 	tf->tf_regs[FRAME_A0] = native_to_linux_sig[sig];
    350 
    351 	/*
    352 	 * Linux has a custom restorer option.  To support it we would
    353 	 * need to store an array of restorers and a sigcode block
    354 	 * which knew to use it.  Doesn't seem worth the trouble.
    355 	 * -erh
    356 	 */
    357 
    358 #ifdef DEBUG
    359 	if (sigdebug & SDB_FOLLOW)
    360 		printf("sendsig(%d): pc %lx, catcher %lx\n", l->l_proc->p_pid,
    361 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
    362 	if ((sigdebug & SDB_KSTACK) && l->l_proc->p_pid == sigpid)
    363 		printf("sendsig(%d): sig %d returns\n", l->l_proc->p_pid, sig);
    364 #endif
    365 }
    366 
    367 /*
    368  * System call to cleanup state after a signal
    369  * has been taken.  Reset signal mask and
    370  * stack state from context left by sendsig (above).
    371  * Return to previous pc as specified by context
    372  * left by sendsig.
    373  * Linux real-time signals use a different sigframe,
    374  * but the sigcontext is the same.
    375  */
    376 
    377 int
    378 linux_restore_sigcontext(struct lwp *l, struct linux_sigcontext context,
    379 			 sigset_t *mask)
    380 {
    381 
    382 	struct proc *p = l->l_proc;
    383 	/*
    384 	 * Linux doesn't (yet) have alternate signal stacks.
    385 	 * However, the OSF/1 sigcontext which they use has
    386 	 * an onstack member.  This could be needed in the future.
    387 	 */
    388 	if (context.sc_onstack & LINUX_SA_ONSTACK)
    389 	    p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    390 	else
    391 	    p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    392 
    393 	/* Reset the signal mask */
    394 	(void) sigprocmask1(p, SIG_SETMASK, mask, 0);
    395 
    396 	/*
    397 	 * Check for security violations.
    398 	 * Linux doesn't allow any changes to the PSL.
    399 	 */
    400 	if (context.sc_ps != ALPHA_PSL_USERMODE)
    401 	    return(EINVAL);
    402 
    403 	l->l_md.md_tf->tf_regs[FRAME_PC] = context.sc_pc;
    404 	l->l_md.md_tf->tf_regs[FRAME_PS] = context.sc_ps;
    405 
    406 	regtoframe((struct reg *)context.sc_regs, l->l_md.md_tf);
    407 	alpha_pal_wrusp(context.sc_regs[R_SP]);
    408 
    409 	if (l == fpcurproc)
    410 	    fpcurproc = NULL;
    411 
    412 	/* Restore fp regs and fpr_cr */
    413 	bcopy((struct fpreg *)context.sc_fpregs, &l->l_addr->u_pcb.pcb_fp,
    414 	    sizeof(struct fpreg));
    415 	/* XXX sc_ownedfp ? */
    416 	/* XXX sc_fp_control ? */
    417 
    418 #ifdef DEBUG
    419 	if (sigdebug & SDB_FOLLOW)
    420 		printf("linux_rt_sigreturn(%d): returns\n", p->p_pid);
    421 #endif
    422 	return (EJUSTRETURN);
    423 }
    424 
    425 int
    426 linux_sys_rt_sigreturn(l, v, retval)
    427 	struct lwp *l;
    428 	void *v;
    429 	register_t *retval;
    430 {
    431 	struct linux_sys_rt_sigreturn_args /* {
    432 		syscallarg(struct linux_rt_sigframe *) sfp;
    433 	} */ *uap = v;
    434 	struct linux_rt_sigframe *sfp, sigframe;
    435 	sigset_t mask;
    436 
    437 	/*
    438 	 * The trampoline code hands us the context.
    439 	 * It is unsafe to keep track of it ourselves, in the event that a
    440 	 * program jumps out of a signal handler.
    441 	 */
    442 
    443 	sfp = SCARG(uap, sfp);
    444 
    445 	if (ALIGN(sfp) != (u_int64_t)sfp)
    446 		return(EINVAL);
    447 
    448 	/*
    449 	 * Fetch the frame structure.
    450 	 */
    451 	if (copyin((caddr_t)sfp, &sigframe,
    452 			sizeof(struct linux_rt_sigframe)) != 0)
    453 		return (EFAULT);
    454 
    455 	/* Grab the signal mask */
    456 	linux_to_native_sigset(&sigframe.uc.uc_sigmask, &mask);
    457 
    458 	return(linux_restore_sigcontext(l, sigframe.uc.uc_mcontext, &mask));
    459 }
    460 
    461 
    462 int
    463 linux_sys_sigreturn(l, v, retval)
    464 	struct lwp *l;
    465 	void *v;
    466 	register_t *retval;
    467 {
    468 	struct linux_sys_sigreturn_args /* {
    469 		syscallarg(struct linux_sigframe *) sfp;
    470 	} */ *uap = v;
    471 	struct linux_sigframe *sfp, frame;
    472 	sigset_t mask;
    473 
    474 	/*
    475 	 * The trampoline code hands us the context.
    476 	 * It is unsafe to keep track of it ourselves, in the event that a
    477 	 * program jumps out of a signal handler.
    478 	 */
    479 
    480 	sfp = SCARG(uap, sfp);
    481 	if (ALIGN(sfp) != (u_int64_t)sfp)
    482 		return(EINVAL);
    483 
    484 	/*
    485 	 * Fetch the frame structure.
    486 	 */
    487 	if (copyin((caddr_t)sfp, &frame, sizeof(struct linux_sigframe)) != 0)
    488 		return(EFAULT);
    489 
    490 	/* Grab the signal mask. */
    491 	/* XXX use frame.extramask */
    492 	linux_old_to_native_sigset(frame.sf_sc.sc_mask, &mask);
    493 
    494 	return(linux_restore_sigcontext(l, frame.sf_sc, &mask));
    495 }
    496 
    497 /*
    498  * We come here in a last attempt to satisfy a Linux ioctl() call
    499  */
    500 /* XXX XAX update this, add maps, etc... */
    501 int
    502 linux_machdepioctl(p, v, retval)
    503 	struct proc *p;
    504 	void *v;
    505 	register_t *retval;
    506 {
    507 	struct linux_sys_ioctl_args /* {
    508 		syscallarg(int) fd;
    509 		syscallarg(u_long) com;
    510 		syscallarg(caddr_t) data;
    511 	} */ *uap = v;
    512 	struct sys_ioctl_args bia;
    513 	u_long com;
    514 
    515 	SCARG(&bia, fd) = SCARG(uap, fd);
    516 	SCARG(&bia, data) = SCARG(uap, data);
    517 	com = SCARG(uap, com);
    518 
    519 	switch (com) {
    520 	default:
    521 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
    522 		return EINVAL;
    523 	}
    524 	SCARG(&bia, com) = com;
    525 	/* XXX njwlwp */
    526 	return sys_ioctl(curproc, &bia, retval);
    527 }
    528 
    529 /* XXX XAX fix this */
    530 dev_t
    531 linux_fakedev(dev)
    532 	dev_t dev;
    533 {
    534 	return dev;
    535 }
    536 
    537