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