Home | History | Annotate | Line # | Download | only in alpha
linux_machdep.c revision 1.43.2.1
      1 /*	$NetBSD: linux_machdep.c,v 1.43.2.1 2008/05/10 23:48:51 wrstuden 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  * Based on sys/arch/i386/i386/linux_machdep.c:
     32  *	linux_machdep.c,v 1.42 1998/09/11 12:50:06 mycroft Exp
     33  *	written by Frank van der Linden
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.43.2.1 2008/05/10 23:48:51 wrstuden Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/signalvar.h>
     43 #include <sys/kernel.h>
     44 #include <sys/proc.h>
     45 #include <sys/user.h>
     46 #include <sys/buf.h>
     47 #include <sys/reboot.h>
     48 #include <sys/conf.h>
     49 #include <sys/exec.h>
     50 #include <sys/file.h>
     51 #include <sys/callout.h>
     52 #include <sys/malloc.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/msgbuf.h>
     55 #include <sys/mount.h>
     56 #include <sys/vnode.h>
     57 #include <sys/device.h>
     58 #include <sys/sa.h>
     59 #include <sys/syscallargs.h>
     60 #include <sys/filedesc.h>
     61 #include <sys/exec_elf.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/kauth.h>
     64 
     65 #include <uvm/uvm_extern.h>
     66 
     67 #include <compat/linux/common/linux_types.h>
     68 #include <compat/linux/common/linux_signal.h>
     69 #include <compat/linux/common/linux_siginfo.h>
     70 #include <compat/linux/common/linux_util.h>
     71 #include <compat/linux/common/linux_ioctl.h>
     72 #include <compat/linux/common/linux_exec.h>
     73 #include <compat/linux/common/linux_machdep.h>
     74 #include <compat/linux/common/linux_emuldata.h>
     75 
     76 #include <compat/linux/linux_syscallargs.h>
     77 
     78 #include <machine/alpha.h>
     79 #include <machine/reg.h>
     80 
     81 #if defined(_KERNEL_OPT)
     82 #include "wsdisplay.h"
     83 #endif
     84 #if (NWSDISPLAY >0)
     85 #include <dev/wscons/wsdisplay_usl_io.h>
     86 #endif
     87 #ifdef DEBUG
     88 #include <machine/sigdebug.h>
     89 #endif
     90 
     91 /*
     92  * Deal with some alpha-specific things in the Linux emulation code.
     93  */
     94 
     95 void
     96 linux_setregs(struct lwp *l, struct exec_package *epp, u_long stack)
     97 {
     98 #ifdef DEBUG
     99 	struct trapframe *tfp = l->l_md.md_tf;
    100 #endif
    101 
    102 	setregs(l, epp, stack);
    103 #ifdef DEBUG
    104 	/*
    105 	 * Linux has registers set to zero on entry; for DEBUG kernels
    106 	 * the alpha setregs() fills registers with 0xbabefacedeadbeef.
    107 	 */
    108 	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
    109 #endif
    110 }
    111 
    112 void
    113 setup_linux_rt_sigframe(struct trapframe *tf, int sig, const sigset_t *mask)
    114 {
    115 	struct lwp *l = curlwp;
    116 	struct proc *p = l->l_proc;
    117 	struct linux_rt_sigframe *sfp, sigframe;
    118 	int onstack, error;
    119 	int fsize, rndfsize;
    120 	extern char linux_rt_sigcode[], linux_rt_esigcode[];
    121 
    122 	/* Do we need to jump onto the signal stack? */
    123 	onstack = (l->l_sigstk->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    124 		  (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    125 
    126 	/* Allocate space for the signal handler context.  */
    127 	fsize = sizeof(struct linux_rt_sigframe);
    128 	rndfsize = ((fsize + 15) / 16) * 16;
    129 
    130 	if (onstack)
    131 		sfp = (struct linux_rt_sigframe *)
    132 		    ((char *)l->l_sigstk->ss_sp + l->l_sigstk->ss_size);
    133 	else
    134 		sfp = (struct linux_rt_sigframe *)(alpha_pal_rdusp());
    135 	sfp = (struct linux_rt_sigframe *)((char *)sfp - rndfsize);
    136 
    137 #ifdef DEBUG
    138 	if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
    139 		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
    140 		    sig, &onstack, sfp);
    141 #endif /* DEBUG */
    142 
    143 	/*
    144 	 * Build the signal context to be used by sigreturn.
    145 	 */
    146 	memset(&sigframe.uc, 0, sizeof(struct linux_ucontext));
    147 	sigframe.uc.uc_mcontext.sc_onstack = onstack;
    148 
    149 	/* Setup potentially partial signal mask in sc_mask. */
    150 	/* But get all of it in uc_sigmask */
    151 	native_to_linux_old_sigset(&sigframe.uc.uc_mcontext.sc_mask, mask);
    152 	native_to_linux_sigset(&sigframe.uc.uc_sigmask, mask);
    153 
    154 	sigframe.uc.uc_mcontext.sc_pc = tf->tf_regs[FRAME_PC];
    155 	sigframe.uc.uc_mcontext.sc_ps = ALPHA_PSL_USERMODE;
    156 	frametoreg(tf, (struct reg *)sigframe.uc.uc_mcontext.sc_regs);
    157 	sigframe.uc.uc_mcontext.sc_regs[R_SP] = alpha_pal_rdusp();
    158 
    159 	alpha_enable_fp(l, 1);
    160 	sigframe.uc.uc_mcontext.sc_fpcr = alpha_read_fpcr();
    161 	sigframe.uc.uc_mcontext.sc_fp_control = alpha_read_fp_c(l);
    162 	alpha_pal_wrfen(0);
    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 	memset(&sigframe.info, 0, 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 = kauth_cred_geteuid(l->l_cred);	/* Use real uid here? */
    179 
    180 	sendsig_reset(l, sig);
    181 	mutex_exit(p->p_lock);
    182 	error = copyout((void *)&sigframe, (void *)sfp, fsize);
    183 	mutex_enter(p->p_lock);
    184 
    185 	if (error != 0) {
    186 #ifdef DEBUG
    187 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
    188 			printf("sendsig(%d): copyout failed on sig %d\n",
    189 			    p->p_pid, sig);
    190 #endif
    191 		/*
    192 		 * Process has trashed its stack; give it an illegal
    193 		 * instruction to halt it in its tracks.
    194 		 */
    195 		sigexit(l, SIGILL);
    196 		/* NOTREACHED */
    197 	}
    198 
    199 	/* Pass pointers to siginfo and ucontext in the regs */
    200 	tf->tf_regs[FRAME_A1] = (unsigned long)&sfp->info;
    201 	tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->uc;
    202 
    203 	/* Address of trampoline code.  End up at this PC after mi_switch */
    204 	tf->tf_regs[FRAME_PC] =
    205 	    (u_int64_t)(p->p_psstr - (linux_rt_esigcode - linux_rt_sigcode));
    206 
    207 	/* Adjust the stack */
    208 	alpha_pal_wrusp((unsigned long)sfp);
    209 
    210 	/* Remember that we're now on the signal stack. */
    211 	if (onstack)
    212 		l->l_sigstk->ss_flags |= SS_ONSTACK;
    213 }
    214 
    215 void setup_linux_sigframe(tf, sig, mask)
    216 	struct trapframe *tf;
    217 	int sig;
    218 	const sigset_t *mask;
    219 {
    220 	struct lwp *l = curlwp;
    221 	struct proc *p = l->l_proc;
    222 	struct linux_sigframe *sfp, sigframe;
    223 	int onstack, error;
    224 	int fsize, rndfsize;
    225 	extern char linux_sigcode[], linux_esigcode[];
    226 
    227 	/* Do we need to jump onto the signal stack? */
    228 	onstack = (l->l_sigstk->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    229 		  (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    230 
    231 	/* Allocate space for the signal handler context.  */
    232 	fsize = sizeof(struct linux_sigframe);
    233 	rndfsize = ((fsize + 15) / 16) * 16;
    234 
    235 	if (onstack)
    236 		sfp = (struct linux_sigframe *)
    237 		    ((char *)l->l_sigstk->ss_sp + l->l_sigstk->ss_size);
    238 	else
    239 		sfp = (struct linux_sigframe *)(alpha_pal_rdusp());
    240 	sfp = (struct linux_sigframe *)((char *)sfp - rndfsize);
    241 
    242 #ifdef DEBUG
    243 	if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
    244 		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
    245 		    sig, &onstack, sfp);
    246 #endif /* DEBUG */
    247 
    248 	/*
    249 	 * Build the signal context to be used by sigreturn.
    250 	 */
    251 	memset(&sigframe.sf_sc, 0, sizeof(struct linux_sigcontext));
    252 	sigframe.sf_sc.sc_onstack = onstack;
    253 	native_to_linux_old_sigset(&sigframe.sf_sc.sc_mask, mask);
    254 	sigframe.sf_sc.sc_pc = tf->tf_regs[FRAME_PC];
    255 	sigframe.sf_sc.sc_ps = ALPHA_PSL_USERMODE;
    256 	frametoreg(tf, (struct reg *)sigframe.sf_sc.sc_regs);
    257 	sigframe.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp();
    258 
    259 	if (l == fpcurlwp) {
    260 	    alpha_pal_wrfen(1);
    261 	    savefpstate(&l->l_addr->u_pcb.pcb_fp);
    262 	    alpha_pal_wrfen(0);
    263 	    sigframe.sf_sc.sc_fpcr = l->l_addr->u_pcb.pcb_fp.fpr_cr;
    264 	    fpcurlwp = NULL;
    265 	}
    266 	/* XXX ownedfp ? etc...? */
    267 
    268 	sigframe.sf_sc.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
    269 	sigframe.sf_sc.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
    270 	sigframe.sf_sc.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
    271 
    272 	sendsig_reset(l, sig);
    273 	mutex_exit(p->p_lock);
    274 	error = copyout((void *)&sigframe, (void *)sfp, fsize);
    275 	mutex_enter(p->p_lock);
    276 
    277 	if (error != 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(l, 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 		l->l_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(const ksiginfo_t *ksi, const sigset_t *mask)
    319 {
    320 	struct lwp *l = curlwp;
    321 	struct proc *p = l->l_proc;
    322 	struct trapframe *tf = l->l_md.md_tf;
    323 	const int sig = ksi->ksi_signo;
    324 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    325 #ifdef notyet
    326 	struct linux_emuldata *edp;
    327 
    328 	/* Setup the signal frame (and part of the trapframe) */
    329 	/*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/
    330 /*	XXX XAX this is broken now.  need someplace to store what
    331 	XXX XAX kind of signal handler a signal has.*/
    332 #if 0
    333 	edp = (struct linux_emuldata *)p->p_emuldata;
    334 #else
    335 	edp = 0;
    336 #endif
    337 	if (edp && sigismember(&edp->ps_siginfo, sig))
    338 		setup_linux_rt_sigframe(tf, sig, mask);
    339 	else
    340 #endif /* notyet */
    341 		setup_linux_sigframe(tf, sig, mask);
    342 
    343 	/* Signal handler for trampoline code */
    344 	tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
    345 	tf->tf_regs[FRAME_A0] = native_to_linux_signo[sig];
    346 
    347 	/*
    348 	 * Linux has a custom restorer option.  To support it we would
    349 	 * need to store an array of restorers and a sigcode block
    350 	 * which knew to use it.  Doesn't seem worth the trouble.
    351 	 * -erh
    352 	 */
    353 
    354 #ifdef DEBUG
    355 	if (sigdebug & SDB_FOLLOW)
    356 		printf("sendsig(%d): pc %lx, catcher %lx\n", l->l_proc->p_pid,
    357 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
    358 	if ((sigdebug & SDB_KSTACK) && l->l_proc->p_pid == sigpid)
    359 		printf("sendsig(%d): sig %d returns\n", l->l_proc->p_pid, sig);
    360 #endif
    361 }
    362 
    363 /*
    364  * System call to cleanup state after a signal
    365  * has been taken.  Reset signal mask and
    366  * stack state from context left by sendsig (above).
    367  * Return to previous pc as specified by context
    368  * left by sendsig.
    369  * Linux real-time signals use a different sigframe,
    370  * but the sigcontext is the same.
    371  */
    372 
    373 int
    374 linux_restore_sigcontext(struct lwp *l, struct linux_sigcontext context,
    375 			 sigset_t *mask)
    376 {
    377 	struct proc *p = l->l_proc;
    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 	mutex_enter(p->p_lock);
    385 	if (context.sc_onstack & LINUX_SA_ONSTACK)
    386 	    l->l_sigstk->ss_flags |= SS_ONSTACK;
    387 	else
    388 	    l->l_sigstk->ss_flags &= ~SS_ONSTACK;
    389 
    390 	/* Reset the signal mask */
    391 	(void) sigprocmask1(l, SIG_SETMASK, mask, 0);
    392 	mutex_exit(p->p_lock);
    393 
    394 	/*
    395 	 * Check for security violations.
    396 	 * Linux doesn't allow any changes to the PSL.
    397 	 */
    398 	if (context.sc_ps != ALPHA_PSL_USERMODE)
    399 	    return(EINVAL);
    400 
    401 	l->l_md.md_tf->tf_regs[FRAME_PC] = context.sc_pc;
    402 	l->l_md.md_tf->tf_regs[FRAME_PS] = context.sc_ps;
    403 
    404 	regtoframe((struct reg *)context.sc_regs, l->l_md.md_tf);
    405 	alpha_pal_wrusp(context.sc_regs[R_SP]);
    406 
    407 	if (l == fpcurlwp)
    408 	    fpcurlwp = NULL;
    409 
    410 	/* Restore fp regs and fpr_cr */
    411 	bcopy((struct fpreg *)context.sc_fpregs, &l->l_addr->u_pcb.pcb_fp,
    412 	    sizeof(struct fpreg));
    413 	/* XXX sc_ownedfp ? */
    414 	/* XXX sc_fp_control ? */
    415 
    416 #ifdef DEBUG
    417 	if (sigdebug & SDB_FOLLOW)
    418 		printf("linux_rt_sigreturn(%d): returns\n", p->p_pid);
    419 #endif
    420 	return (EJUSTRETURN);
    421 }
    422 
    423 int
    424 linux_sys_rt_sigreturn(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval)
    425 {
    426 	/* {
    427 		syscallarg(struct linux_rt_sigframe *) sfp;
    428 	} */
    429 	struct linux_rt_sigframe *sfp, sigframe;
    430 	sigset_t mask;
    431 
    432 	/*
    433 	 * The trampoline code hands us the context.
    434 	 * It is unsafe to keep track of it ourselves, in the event that a
    435 	 * program jumps out of a signal handler.
    436 	 */
    437 
    438 	sfp = SCARG(uap, sfp);
    439 
    440 	if (ALIGN(sfp) != (u_int64_t)sfp)
    441 		return(EINVAL);
    442 
    443 	/*
    444 	 * Fetch the frame structure.
    445 	 */
    446 	if (copyin((void *)sfp, &sigframe,
    447 			sizeof(struct linux_rt_sigframe)) != 0)
    448 		return (EFAULT);
    449 
    450 	/* Grab the signal mask */
    451 	linux_to_native_sigset(&mask, &sigframe.uc.uc_sigmask);
    452 
    453 	return(linux_restore_sigcontext(l, sigframe.uc.uc_mcontext, &mask));
    454 }
    455 
    456 
    457 int
    458 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
    459 {
    460 	/* {
    461 		syscallarg(struct linux_sigframe *) sfp;
    462 	} */
    463 	struct linux_sigframe *sfp, frame;
    464 	sigset_t mask;
    465 
    466 	/*
    467 	 * The trampoline code hands us the context.
    468 	 * It is unsafe to keep track of it ourselves, in the event that a
    469 	 * program jumps out of a signal handler.
    470 	 */
    471 
    472 	sfp = SCARG(uap, sfp);
    473 	if (ALIGN(sfp) != (u_int64_t)sfp)
    474 		return(EINVAL);
    475 
    476 	/*
    477 	 * Fetch the frame structure.
    478 	 */
    479 	if (copyin((void *)sfp, &frame, sizeof(struct linux_sigframe)) != 0)
    480 		return(EFAULT);
    481 
    482 	/* Grab the signal mask. */
    483 	/* XXX use frame.extramask */
    484 	linux_old_to_native_sigset(&mask, frame.sf_sc.sc_mask);
    485 
    486 	return(linux_restore_sigcontext(l, frame.sf_sc, &mask));
    487 }
    488 
    489 /*
    490  * We come here in a last attempt to satisfy a Linux ioctl() call
    491  */
    492 /* XXX XAX update this, add maps, etc... */
    493 int
    494 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
    495 {
    496 	/* {
    497 		syscallarg(int) fd;
    498 		syscallarg(u_long) com;
    499 		syscallarg(void *) data;
    500 	} */
    501 	struct sys_ioctl_args bia;
    502 	u_long com;
    503 
    504 	SCARG(&bia, fd) = SCARG(uap, fd);
    505 	SCARG(&bia, data) = SCARG(uap, data);
    506 	com = SCARG(uap, com);
    507 
    508 	switch (com) {
    509 	default:
    510 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
    511 		return EINVAL;
    512 	}
    513 	SCARG(&bia, com) = com;
    514 	return sys_ioctl(l, &bia, retval);
    515 }
    516 
    517 /* XXX XAX fix this */
    518 dev_t
    519 linux_fakedev(dev_t dev, int raw)
    520 {
    521 	return dev;
    522 }
    523 
    524 int
    525 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    526 {
    527 	return 0;
    528 }
    529