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