Home | History | Annotate | Line # | Download | only in alpha
linux_machdep.c revision 1.12
      1 /*	$NetBSD: linux_machdep.c,v 1.12 2000/11/29 22:05:36 jdolecek 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 
     67 #include <uvm/uvm_extern.h>
     68 
     69 #include <compat/linux/common/linux_types.h>
     70 #include <compat/linux/common/linux_signal.h>
     71 #include <compat/linux/common/linux_siginfo.h>
     72 #include <compat/linux/common/linux_util.h>
     73 #include <compat/linux/common/linux_ioctl.h>
     74 #include <compat/linux/common/linux_exec.h>
     75 #include <compat/linux/common/linux_machdep.h>
     76 #include <compat/linux/common/linux_emuldata.h>
     77 
     78 #include <compat/linux/linux_syscallargs.h>
     79 
     80 #include <machine/alpha.h>
     81 #include <machine/reg.h>
     82 
     83 #if defined(_KERNEL) && !defined(_LKM)
     84 #include "wsdisplay.h"
     85 #endif
     86 #if (NWSDISPLAY >0)
     87 #include <sys/ioctl.h>
     88 #include <dev/wscons/wsdisplay_usl_io.h>
     89 #endif
     90 
     91 /*
     92  * Deal with some alpha-specific things in the Linux emulation code.
     93  */
     94 
     95 void
     96 linux_setregs(p, epp, stack)
     97 	struct proc *p;
     98 	struct exec_package *epp;
     99 	u_long stack;
    100 {
    101 /* XXX XAX I think this is ok. not sure though. */
    102 	setregs(p, epp, stack);
    103 }
    104 
    105 void setup_linux_rt_sigframe(tf, sig, mask)
    106 	struct trapframe *tf;
    107 	int sig;
    108 	sigset_t *mask;
    109 {
    110 	struct proc *p = curproc;
    111 	struct linux_rt_sigframe *sfp, sigframe;
    112 	struct sigacts *psp = p->p_sigacts;
    113 	int onstack;
    114 	int fsize, rndfsize;
    115 	extern char linux_rt_sigcode[], linux_rt_esigcode[];
    116 
    117 	/* Do we need to jump onto the signal stack? */
    118 	onstack = (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    119 		  (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
    120 
    121 	/* Allocate space for the signal handler context.  */
    122 	fsize = sizeof(struct linux_rt_sigframe);
    123 	rndfsize = ((fsize + 15) / 16) * 16;
    124 
    125 	if (onstack)
    126 		sfp = (struct linux_rt_sigframe *)
    127 					((caddr_t)psp->ps_sigstk.ss_sp +
    128 						  psp->ps_sigstk.ss_size);
    129 	else
    130 		sfp = (struct linux_rt_sigframe *)(alpha_pal_rdusp());
    131 	sfp = (struct linux_rt_sigframe *)((caddr_t)sfp - rndfsize);
    132 
    133 #ifdef DEBUG
    134 	if ((sigdebug & SDB_KSTACK) && p->p_pid = sigpid)
    135 		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
    136 		    sig, &onstack, sfp);
    137 #endif /* DEBUG */
    138 
    139 	/*
    140 	 * Build the signal context to be used by sigreturn.
    141 	 */
    142 	bzero(&sigframe.uc, sizeof(struct linux_ucontext));
    143 	sigframe.uc.uc_mcontext.sc_onstack = onstack;
    144 
    145 	/* Setup potentially partial signal mask in sc_mask. */
    146 	/* But get all of it in uc_sigmask */
    147 	native_to_linux_old_sigset(mask, &sigframe.uc.uc_mcontext.sc_mask);
    148 	native_to_linux_sigset(mask, &sigframe.uc.uc_sigmask);
    149 
    150 	sigframe.uc.uc_mcontext.sc_pc = tf->tf_regs[FRAME_PC];
    151 	sigframe.uc.uc_mcontext.sc_ps = ALPHA_PSL_USERMODE;
    152 	frametoreg(tf, (struct reg *)sigframe.uc.uc_mcontext.sc_regs);
    153 	sigframe.uc.uc_mcontext.sc_regs[R_SP] = alpha_pal_rdusp();
    154 
    155 	if (p == fpcurproc) {
    156 	    alpha_pal_wrfen(1);
    157 	    savefpstate(&p->p_addr->u_pcb.pcb_fp);
    158 	    alpha_pal_wrfen(0);
    159 	    sigframe.uc.uc_mcontext.sc_fpcr = p->p_addr->u_pcb.pcb_fp.fpr_cr;
    160 	    fpcurproc = NULL;
    161 	}
    162 	/* XXX ownedfp ? etc...? */
    163 
    164 	sigframe.uc.uc_mcontext.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
    165 	sigframe.uc.uc_mcontext.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
    166 	sigframe.uc.uc_mcontext.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
    167 
    168 	/*
    169 	 * XXX XAX Create bogus siginfo data.  This can't really
    170 	 * XXX be fixed until NetBSD has realtime signals.
    171 	 * XXX Or we do the emuldata thing.
    172 	 * XXX -erh
    173 	 */
    174 	bzero(&sigframe.info, sizeof(struct linux_siginfo));
    175 	sigframe.info.lsi_signo = sig;
    176 	sigframe.info.lsi_code = LINUX_SI_USER;
    177 	sigframe.info.lsi_pid = p->p_pid;
    178 	sigframe.info.lsi_uid = p->p_ucred->cr_uid;	/* Use real uid here? */
    179 
    180 	if (copyout((caddr_t)&sigframe, (caddr_t)sfp, fsize) != 0) {
    181 #ifdef DEBUG
    182 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    183 			printf("sendsig(%d): copyout failed on sig %d\n",
    184 			    p->p_pid, sig);
    185 #endif
    186 		/*
    187 		 * Process has trashed its stack; give it an illegal
    188 		 * instruction to halt it in its tracks.
    189 		 */
    190 		sigexit(p, SIGILL);
    191 		/* NOTREACHED */
    192 	}
    193 
    194 	/* Pass pointers to siginfo and ucontext in the regs */
    195 	tf->tf_regs[FRAME_A1] = (unsigned long)&sfp->info;
    196 	tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->uc;
    197 
    198 	/* Address of trampoline code.  End up at this PC after mi_switch */
    199 	tf->tf_regs[FRAME_PC] =
    200 	    (u_int64_t)(PS_STRINGS - (linux_rt_esigcode - linux_rt_sigcode));
    201 
    202 	/* Adjust the stack */
    203 	alpha_pal_wrusp((unsigned long)sfp);
    204 
    205 	/* Remember that we're now on the signal stack. */
    206 	if (onstack)
    207 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
    208 }
    209 
    210 void setup_linux_sigframe(tf, sig, mask)
    211 	struct trapframe *tf;
    212 	int sig;
    213 	sigset_t *mask;
    214 {
    215 	struct proc *p = curproc;
    216 	struct linux_sigframe *sfp, sigframe;
    217 	struct sigacts *psp = p->p_sigacts;
    218 	int onstack;
    219 	int fsize, rndfsize;
    220 	extern char linux_sigcode[], linux_esigcode[];
    221 
    222 	/* Do we need to jump onto the signal stack? */
    223 	onstack = (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    224 		  (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
    225 
    226 	/* Allocate space for the signal handler context.  */
    227 	fsize = sizeof(struct linux_sigframe);
    228 	rndfsize = ((fsize + 15) / 16) * 16;
    229 
    230 	if (onstack)
    231 		sfp = (struct linux_sigframe *)
    232 					((caddr_t)psp->ps_sigstk.ss_sp +
    233 						  psp->ps_sigstk.ss_size);
    234 	else
    235 		sfp = (struct linux_sigframe *)(alpha_pal_rdusp());
    236 	sfp = (struct linux_sigframe *)((caddr_t)sfp - rndfsize);
    237 
    238 #ifdef DEBUG
    239 	if ((sigdebug & SDB_KSTACK) && p->p_pid = sigpid)
    240 		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
    241 		    sig, &onstack, sfp);
    242 #endif /* DEBUG */
    243 
    244 	/*
    245 	 * Build the signal context to be used by sigreturn.
    246 	 */
    247 	bzero(&sigframe.sf_sc, sizeof(struct linux_ucontext));
    248 	sigframe.sf_sc.sc_onstack = onstack;
    249 	native_to_linux_old_sigset(mask, &sigframe.sf_sc.sc_mask);
    250 	sigframe.sf_sc.sc_pc = tf->tf_regs[FRAME_PC];
    251 	sigframe.sf_sc.sc_ps = ALPHA_PSL_USERMODE;
    252 	frametoreg(tf, (struct reg *)sigframe.sf_sc.sc_regs);
    253 	sigframe.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp();
    254 
    255 	if (p == fpcurproc) {
    256 	    alpha_pal_wrfen(1);
    257 	    savefpstate(&p->p_addr->u_pcb.pcb_fp);
    258 	    alpha_pal_wrfen(0);
    259 	    sigframe.sf_sc.sc_fpcr = p->p_addr->u_pcb.pcb_fp.fpr_cr;
    260 	    fpcurproc = NULL;
    261 	}
    262 	/* XXX ownedfp ? etc...? */
    263 
    264 	sigframe.sf_sc.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
    265 	sigframe.sf_sc.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
    266 	sigframe.sf_sc.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
    267 
    268 	if (copyout((caddr_t)&sigframe, (caddr_t)sfp, fsize) != 0) {
    269 #ifdef DEBUG
    270 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    271 			printf("sendsig(%d): copyout failed on sig %d\n",
    272 			    p->p_pid, sig);
    273 #endif
    274 		/*
    275 		 * Process has trashed its stack; give it an illegal
    276 		 * instruction to halt it in its tracks.
    277 		 */
    278 		sigexit(p, SIGILL);
    279 		/* NOTREACHED */
    280 	}
    281 
    282 	/* Pass pointers to sigcontext in the regs */
    283 	tf->tf_regs[FRAME_A1] = 0;
    284 	tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->sf_sc;
    285 
    286 	/* Address of trampoline code.  End up at this PC after mi_switch */
    287 	tf->tf_regs[FRAME_PC] =
    288 	    (u_int64_t)(PS_STRINGS - (linux_esigcode - linux_sigcode));
    289 
    290 	/* Adjust the stack */
    291 	alpha_pal_wrusp((unsigned long)sfp);
    292 
    293 	/* Remember that we're now on the signal stack. */
    294 	if (onstack)
    295 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
    296 }
    297 
    298 /*
    299  * Send an interrupt to process.
    300  *
    301  * Stack is set up to allow sigcode stored
    302  * in u. to call routine, followed by kcall
    303  * to sigreturn routine below.  After sigreturn
    304  * resets the signal mask, the stack, and the
    305  * frame pointer, it returns to the user
    306  * specified pc, psl.
    307  */
    308 void
    309 linux_sendsig(catcher, sig, mask, code)
    310 	sig_t catcher;
    311 	int sig;
    312 	sigset_t *mask;
    313 	u_long code;
    314 {
    315 	struct proc *p = curproc;
    316 	struct trapframe *tf = p->p_md.md_tf;
    317 #ifdef notyet
    318 	struct linux_emuldata *edp;
    319 
    320 	/* Setup the signal frame (and part of the trapframe) */
    321 	/*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/
    322 /*	XXX XAX this is broken now.  need someplace to store what
    323 	XXX XAX kind of signal handler a signal has.*/
    324 #if 0
    325 	edp = (struct linux_emuldata *)p->p_emuldata;
    326 #else
    327 	edp = 0;
    328 #endif
    329 	if (edp && sigismember(&edp->ps_siginfo, sig))
    330 		setup_linux_rt_sigframe(tf, sig, mask);
    331 	else
    332 #endif /* notyet */
    333 		setup_linux_sigframe(tf, sig, mask);
    334 
    335 	/* Signal handler for trampoline code */
    336 	tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
    337 	tf->tf_regs[FRAME_A0] = native_to_linux_sig[sig];
    338 
    339 	/*
    340 	 * Linux has a custom restorer option.  To support it we would
    341 	 * need to store an array of restorers and a sigcode block
    342 	 * which knew to use it.  Doesn't seem worth the trouble.
    343 	 * -erh
    344 	 */
    345 
    346 #ifdef DEBUG
    347 	if (sigdebug & SDB_FOLLOW)
    348 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
    349 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
    350 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    351 		printf("sendsig(%d): sig %d returns\n", p->p_pid, sig);
    352 #endif
    353 }
    354 
    355 /*
    356  * System call to cleanup state after a signal
    357  * has been taken.  Reset signal mask and
    358  * stack state from context left by sendsig (above).
    359  * Return to previous pc as specified by context
    360  * left by sendsig.
    361  * Linux real-time signals use a different sigframe,
    362  * but the sigcontext is the same.
    363  */
    364 
    365 int
    366 linux_restore_sigcontext(struct proc *p, struct linux_sigcontext context,
    367 			 sigset_t *mask)
    368 {
    369 
    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_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
    377 	else
    378 	    p->p_sigacts->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 	p->p_md.md_tf->tf_regs[FRAME_PC] = context.sc_pc;
    391 	p->p_md.md_tf->tf_regs[FRAME_PS] = context.sc_ps;
    392 
    393 	regtoframe((struct reg *)context.sc_regs, p->p_md.md_tf);
    394 	alpha_pal_wrusp(context.sc_regs[R_SP]);
    395 
    396 	if (p == fpcurproc)
    397 	    fpcurproc = NULL;
    398 
    399 	/* Restore fp regs and fpr_cr */
    400 	bcopy((struct fpreg *)context.sc_fpregs, &p->p_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->pid);
    408 #endif
    409 	return (EJUSTRETURN);
    410 }
    411 
    412 int
    413 linux_sys_rt_sigreturn(p, v, retval)
    414 	struct proc *p;
    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(p, sigframe.uc.uc_mcontext, &mask));
    446 }
    447 
    448 
    449 int
    450 linux_sys_sigreturn(p, v, retval)
    451 	struct proc *p;
    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(p, 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 	return sys_ioctl(p, &bia, retval);
    513 }
    514 
    515 /* XXX XAX fix this */
    516 dev_t
    517 linux_fakedev(dev)
    518 	dev_t dev;
    519 {
    520 	return dev;
    521 }
    522 
    523