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