Home | History | Annotate | Line # | Download | only in powerpc
linux_machdep.c revision 1.31.20.2
      1 /*	$NetBSD: linux_machdep.c,v 1.31.20.2 2007/01/30 13:51:32 ad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1995, 2000, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Emmanuel Dreyfus.
      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 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.31.20.2 2007/01/30 13:51:32 ad Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/signalvar.h>
     45 #include <sys/kernel.h>
     46 #include <sys/proc.h>
     47 #include <sys/user.h>
     48 #include <sys/buf.h>
     49 #include <sys/reboot.h>
     50 #include <sys/conf.h>
     51 #include <sys/exec.h>
     52 #include <sys/file.h>
     53 #include <sys/callout.h>
     54 #include <sys/malloc.h>
     55 #include <sys/mbuf.h>
     56 #include <sys/msgbuf.h>
     57 #include <sys/mount.h>
     58 #include <sys/vnode.h>
     59 #include <sys/device.h>
     60 #include <sys/syscallargs.h>
     61 #include <sys/filedesc.h>
     62 #include <sys/exec_elf.h>
     63 #include <sys/disklabel.h>
     64 #include <sys/ioctl.h>
     65 #include <miscfs/specfs/specdev.h>
     66 
     67 #include <compat/linux/common/linux_types.h>
     68 #include <compat/linux/common/linux_signal.h>
     69 #include <compat/linux/common/linux_util.h>
     70 #include <compat/linux/common/linux_ioctl.h>
     71 #include <compat/linux/common/linux_hdio.h>
     72 #include <compat/linux/common/linux_exec.h>
     73 #include <compat/linux/common/linux_machdep.h>
     74 
     75 #include <compat/linux/linux_syscallargs.h>
     76 
     77 #include <machine/cpu.h>
     78 #include <machine/fpu.h>
     79 #include <machine/psl.h>
     80 #include <machine/reg.h>
     81 #include <machine/vmparam.h>
     82 
     83 /*
     84  * To see whether wscons is configured (for virtual console ioctl calls).
     85  */
     86 #if defined(_KERNEL_OPT)
     87 #include "wsdisplay.h"
     88 #endif
     89 #if (NWSDISPLAY > 0)
     90 #include <dev/wscons/wsconsio.h>
     91 #include <dev/wscons/wsdisplay_usl_io.h>
     92 #endif
     93 
     94 /*
     95  * Set set up registers on exec.
     96  * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
     97  * entry uses NetBSD's native setregs instead of linux_setregs
     98  */
     99 void
    100 linux_setregs(l, pack, stack)
    101 	struct lwp *l;
    102 	struct exec_package *pack;
    103 	u_long stack;
    104 {
    105 	setregs(l, pack, stack);
    106 }
    107 
    108 /*
    109  * Send an interrupt to process.
    110  *
    111  * Adapted from arch/powerpc/powerpc/sig_machdep.c:sendsig and
    112  * compat/linux/arch/i386/linux_machdep.c:linux_sendsig
    113  *
    114  * XXX Does not work well yet with RT signals
    115  *
    116  */
    117 
    118 void
    119 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
    120 {
    121 	const int sig = ksi->ksi_signo;
    122 	struct lwp *l = curlwp;
    123 	struct proc *p = l->l_proc;
    124 	struct trapframe *tf;
    125 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    126 	struct linux_sigregs frame;
    127 	struct linux_pt_regs linux_regs;
    128 	struct linux_sigcontext sc;
    129 	register_t fp;
    130 	int onstack, error;
    131 	int i;
    132 
    133 	tf = trapframe(l);
    134 
    135 	/*
    136 	 * Do we need to jump onto the signal stack?
    137 	 */
    138 	onstack =
    139 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    140 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    141 
    142 	/*
    143 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
    144 	 * not use it yet. XXX fix this.
    145 	 */
    146 	onstack=0;
    147 
    148 	/*
    149 	 * Allocate space for the signal handler context.
    150 	 */
    151 	if (onstack) {
    152 		fp = (register_t)
    153 		    ((caddr_t)l->l_sigstk.ss_sp +
    154 		    l->l_sigstk.ss_size);
    155 	} else {
    156 		fp = tf->fixreg[1];
    157 	}
    158 #ifdef DEBUG_LINUX
    159 	printf("fp at start of linux_sendsig = %x\n", fp);
    160 #endif
    161 	fp -= sizeof(struct linux_sigregs);
    162 	fp &= ~0xf;
    163 
    164 	/*
    165 	 * Prepare a sigcontext for later.
    166 	 */
    167 	memset(&sc, 0, sizeof sc);
    168 	sc.lsignal = (int)native_to_linux_signo[sig];
    169 	sc.lhandler = (unsigned long)catcher;
    170 	native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
    171 	sc.lregs = (struct linux_pt_regs*)fp;
    172 
    173 	/*
    174 	 * Setup the signal stack frame as Linux does it in
    175 	 * arch/ppc/kernel/signal.c:setup_frame()
    176 	 *
    177 	 * Save register context.
    178 	 */
    179 	for (i = 0; i < 32; i++)
    180 		linux_regs.lgpr[i] = tf->fixreg[i];
    181 	linux_regs.lnip = tf->srr0;
    182 	linux_regs.lmsr = tf->srr1 & PSL_USERSRR1;
    183 	linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
    184 	linux_regs.lctr = tf->ctr;
    185 	linux_regs.llink = tf->lr;
    186 	linux_regs.lxer = tf->xer;
    187 	linux_regs.lccr = tf->cr;
    188 	linux_regs.lmq = 0;  			/* Unused, 601 only */
    189 	linux_regs.ltrap = tf->exc;
    190 	linux_regs.ldar = tf->dar;
    191 	linux_regs.ldsisr = tf->dsisr;
    192 	linux_regs.lresult = 0;
    193 
    194 	memset(&frame, 0, sizeof(frame));
    195 	memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
    196 
    197 	save_fpu_lwp(curlwp, FPU_SAVE);
    198 	memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpreg, sizeof(frame.lfp_regs));
    199 
    200 	/*
    201 	 * Copy Linux's signal trampoline on the user stack It should not
    202 	 * be used, but Linux binaries might expect it to be there.
    203 	 */
    204 	frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
    205 	frame.ltramp[1] = 0x44000002; /* sc */
    206 
    207 	/*
    208 	 * Move it to the user stack
    209 	 * There is a little trick here, about the LINUX_ABIGAP: the
    210 	 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
    211 	 * binaries. But the Linux kernel seems to do without it, and it
    212 	 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
    213 	 */
    214 	sendsig_reset(l, sig);
    215 	mutex_exit(&p->p_smutex);
    216 	error = copyout(&frame, (caddr_t)fp, sizeof (frame) - LINUX_ABIGAP);
    217 	mutex_enter(&p->p_smutex);
    218 
    219 	if (error != 0) {
    220 		/*
    221 		 * Process has trashed its stack; give it an illegal
    222 		 * instruction to halt it in its tracks.
    223 		 */
    224 		sigexit(l, SIGILL);
    225 		/* NOTREACHED */
    226 	}
    227 
    228 	/*
    229 	 * Add a sigcontext on the stack
    230 	 */
    231 	fp -= sizeof(struct linux_sigcontext);
    232 	if (copyout(&sc, (caddr_t)fp, sizeof (struct linux_sigcontext)) != 0) {
    233 		/*
    234 		 * Process has trashed its stack; give it an illegal
    235 		 * instruction to halt it in its tracks.
    236 		 */
    237 		sigexit(l, SIGILL);
    238 		/* NOTREACHED */
    239 	}
    240 
    241 	/*
    242 	 * Set the registers according to how the Linux process expects them.
    243 	 * "Mind the gap" Linux expects a gap here.
    244 	 */
    245 	tf->fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
    246 	tf->lr = (int)catcher;
    247 	tf->fixreg[3] = (int)native_to_linux_signo[sig];
    248 	tf->fixreg[4] = fp;
    249 	tf->srr0 = (int)p->p_sigctx.ps_sigcode;
    250 
    251 #ifdef DEBUG_LINUX
    252 	printf("fp at end of linux_sendsig = %x\n", fp);
    253 #endif
    254 	/*
    255 	 * Remember that we're now on the signal stack.
    256 	 */
    257 	if (onstack)
    258 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    259 #ifdef DEBUG_LINUX
    260 	printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
    261 #endif
    262 }
    263 
    264 /*
    265  * System call to cleanup state after a signal
    266  * has been taken.  Reset signal mask and
    267  * stack state from context left by sendsig (above).
    268  * Return to previous pc and psl as specified by
    269  * context left by sendsig. Check carefully to
    270  * make sure that the user has not modified the
    271  * psl to gain improper privileges or to cause
    272  * a machine fault.
    273  *
    274  * XXX not tested
    275  */
    276 int
    277 linux_sys_rt_sigreturn(l, v, retval)
    278 	struct lwp *l;
    279 	void *v;
    280 	register_t *retval;
    281 {
    282 	struct linux_sys_rt_sigreturn_args /* {
    283 		syscallarg(struct linux_rt_sigframe *) sfp;
    284 	} */ *uap = v;
    285 	struct proc *p = l->l_proc;
    286 	struct linux_rt_sigframe *scp, sigframe;
    287 	struct linux_sigregs sregs;
    288 	struct linux_pt_regs *lregs;
    289 	struct trapframe *tf;
    290 	sigset_t mask;
    291 	int i;
    292 
    293 	/*
    294 	 * The trampoline code hands us the context.
    295 	 * It is unsafe to keep track of it ourselves, in the event that a
    296 	 * program jumps out of a signal handler.
    297 	 */
    298 	scp = SCARG(uap, sfp);
    299 
    300 	/*
    301 	 * Get the context from user stack
    302 	 */
    303 	if (copyin((caddr_t)scp, &sigframe, sizeof(*scp)))
    304 		return (EFAULT);
    305 
    306 	/*
    307 	 *  Restore register context.
    308 	 */
    309 	if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
    310 		   &sregs, sizeof(sregs)))
    311 		return (EFAULT);
    312 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
    313 
    314 	tf = trapframe(l);
    315 #ifdef DEBUG_LINUX
    316 	    (unsigned long)tf, (unsigned long)scp);
    317 #endif
    318 
    319 	if (!PSL_USEROK_P(lregs->lmsr))
    320 		return (EINVAL);
    321 
    322 	for (i = 0; i < 32; i++)
    323 		tf->fixreg[i] = lregs->lgpr[i];
    324 	tf->lr = lregs->llink;
    325 	tf->cr = lregs->lccr;
    326 	tf->xer = lregs->lxer;
    327 	tf->ctr = lregs->lctr;
    328 	tf->srr0 = lregs->lnip;
    329 	tf->srr1 = lregs->lmsr;
    330 
    331 	/*
    332 	 * Make sure the fpu state is discarded
    333 	 */
    334 	save_fpu_lwp(curlwp, FPU_DISCARD);
    335 
    336 	memcpy(curpcb->pcb_fpu.fpreg, (caddr_t)&sregs.lfp_regs,
    337 	       sizeof(curpcb->pcb_fpu.fpreg));
    338 
    339 	mutex_enter(&p->p_smutex);
    340 
    341 	/*
    342 	 * Restore signal stack.
    343 	 *
    344 	 * XXX cannot find the onstack information in Linux sig context.
    345 	 * Is signal stack really supported on Linux?
    346 	 *
    347 	 * It seems to be supported in libc6...
    348 	 */
    349 	/* if (sc.sc_onstack & SS_ONSTACK)
    350 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    351 	else */
    352 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    353 
    354 	/*
    355 	 * Grab the signal mask
    356 	 */
    357 	linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
    358 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
    359 
    360 	mutex_exit(&p->p_smutex);
    361 
    362 	return (EJUSTRETURN);
    363 }
    364 
    365 
    366 /*
    367  * The following needs code review for potential security issues
    368  */
    369 int
    370 linux_sys_sigreturn(l, v, retval)
    371 	struct lwp *l;
    372 	void *v;
    373 	register_t *retval;
    374 {
    375 	struct linux_sys_sigreturn_args /* {
    376 		syscallarg(struct linux_sigcontext *) scp;
    377 	} */ *uap = v;
    378 	struct proc *p = l->l_proc;
    379 	struct linux_sigcontext *scp, context;
    380 	struct linux_sigregs sregs;
    381 	struct linux_pt_regs *lregs;
    382 	struct trapframe *tf;
    383 	sigset_t mask;
    384 	int i;
    385 
    386 	/*
    387 	 * The trampoline code hands us the context.
    388 	 * It is unsafe to keep track of it ourselves, in the event that a
    389 	 * program jumps out of a signal handler.
    390 	 */
    391 	scp = SCARG(uap, scp);
    392 
    393 	/*
    394 	 * Get the context from user stack
    395 	 */
    396 	if (copyin(scp, &context, sizeof(*scp)))
    397 		return (EFAULT);
    398 
    399 	/*
    400 	 *  Restore register context.
    401 	 */
    402 	if (copyin((caddr_t)context.lregs, &sregs, sizeof(sregs)))
    403 		return (EFAULT);
    404 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
    405 
    406 	tf = trapframe(l);
    407 #ifdef DEBUG_LINUX
    408 	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
    409 	    (unsigned long)tf, (unsigned long)scp);
    410 #endif
    411 
    412 	if (!PSL_USEROK_P(lregs->lmsr))
    413 		return (EINVAL);
    414 
    415 	for (i = 0; i < 32; i++)
    416 		tf->fixreg[i] = lregs->lgpr[i];
    417 	tf->lr = lregs->llink;
    418 	tf->cr = lregs->lccr;
    419 	tf->xer = lregs->lxer;
    420 	tf->ctr = lregs->lctr;
    421 	tf->srr0 = lregs->lnip;
    422 	tf->srr1 = lregs->lmsr;
    423 
    424 	/*
    425 	 * Make sure the fpu state is discarded
    426 	 */
    427 	save_fpu_lwp(curlwp, FPU_DISCARD);
    428 
    429 	memcpy(curpcb->pcb_fpu.fpreg, (caddr_t)&sregs.lfp_regs,
    430 	       sizeof(curpcb->pcb_fpu.fpreg));
    431 
    432 	mutex_enter(&p->p_smutex);
    433 
    434 	/*
    435 	 * Restore signal stack.
    436 	 *
    437 	 * XXX cannot find the onstack information in Linux sig context.
    438 	 * Is signal stack really supported on Linux?
    439 	 */
    440 #if 0
    441 	if (sc.sc_onstack & SS_ONSTACK)
    442 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    443 	else
    444 #endif
    445 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    446 
    447 	/* Restore signal mask. */
    448 	linux_old_extra_to_native_sigset(&mask, &context.lmask,
    449 	    &context._unused[3]);
    450 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
    451 
    452 	mutex_exit(&p->p_smutex);
    453 
    454 	return (EJUSTRETURN);
    455 }
    456 
    457 
    458 #if 0
    459 int
    460 linux_sys_modify_ldt(p, v, retval)
    461 	struct proc *p;
    462 	void *v;
    463 	register_t *retval;
    464 {
    465 	/*
    466 	 * This syscall is not implemented in Linux/PowerPC: we should not
    467 	 * be here
    468 	 */
    469 #ifdef DEBUG_LINUX
    470 	printf("linux_sys_modify_ldt: should not be here.\n");
    471 #endif
    472   return 0;
    473 }
    474 #endif
    475 
    476 /*
    477  * major device numbers remapping
    478  */
    479 dev_t
    480 linux_fakedev(dev, raw)
    481 	dev_t dev;
    482 	int raw;
    483 {
    484 	/* XXX write me */
    485 	return dev;
    486 }
    487 
    488 /*
    489  * We come here in a last attempt to satisfy a Linux ioctl() call
    490  */
    491 int
    492 linux_machdepioctl(l, v, retval)
    493 	struct lwp *l;
    494 	void *v;
    495 	register_t *retval;
    496 {
    497 	struct linux_sys_ioctl_args /* {
    498 		syscallarg(int) fd;
    499 		syscallarg(u_long) com;
    500 		syscallarg(caddr_t) data;
    501 	} */ *uap = v;
    502 	struct sys_ioctl_args bia;
    503 	u_long com;
    504 
    505 	SCARG(&bia, fd) = SCARG(uap, fd);
    506 	SCARG(&bia, data) = SCARG(uap, data);
    507 	com = SCARG(uap, com);
    508 
    509 	switch (com) {
    510 	default:
    511 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
    512 		return EINVAL;
    513 	}
    514 	SCARG(&bia, com) = com;
    515 	/* XXX NJWLWP */
    516 	return sys_ioctl(curlwp, &bia, retval);
    517 }
    518 #if 0
    519 /*
    520  * Set I/O permissions for a process. Just set the maximum level
    521  * right away (ignoring the argument), otherwise we would have
    522  * to rely on I/O permission maps, which are not implemented.
    523  */
    524 int
    525 linux_sys_iopl(l, v, retval)
    526 	struct lwp *l;
    527 	void *v;
    528 	register_t *retval;
    529 {
    530 	/*
    531 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    532 	 */
    533 #ifdef DEBUG_LINUX
    534 	printf("linux_sys_iopl: should not be here.\n");
    535 #endif
    536 	return 0;
    537 }
    538 #endif
    539 
    540 /*
    541  * See above. If a root process tries to set access to an I/O port,
    542  * just let it have the whole range.
    543  */
    544 int
    545 linux_sys_ioperm(l, v, retval)
    546 	struct lwp *l;
    547 	void *v;
    548 	register_t *retval;
    549 {
    550 	/*
    551 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    552 	 */
    553 #ifdef DEBUG_LINUX
    554 	printf("linux_sys_ioperm: should not be here.\n");
    555 #endif
    556 	return 0;
    557 }
    558 
    559 /*
    560  * wrapper linux_sys_new_uname() -> linux_sys_uname()
    561  */
    562 int
    563 linux_sys_new_uname(l, v, retval)
    564 	struct lwp *l;
    565 	void *v;
    566 	register_t *retval;
    567 {
    568 	return linux_sys_uname(l, v, retval);
    569 }
    570 
    571 /*
    572  * wrapper linux_sys_new_select() -> linux_sys_select()
    573  */
    574 int
    575 linux_sys_new_select(l, v, retval)
    576 	struct lwp *l;
    577 	void *v;
    578 	register_t *retval;
    579 {
    580 	return linux_sys_select(l, v, retval);
    581 }
    582 
    583 int
    584 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    585 {
    586 	return 0;
    587 }
    588