Home | History | Annotate | Line # | Download | only in powerpc
linux_machdep.c revision 1.5.4.8
      1 /*	$NetBSD: linux_machdep.c,v 1.5.4.8 2002/06/24 21:49:18 nathanw 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.5.4.8 2002/06/24 21:49:18 nathanw 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/map.h>
     47 #include <sys/proc.h>
     48 #include <sys/user.h>
     49 #include <sys/buf.h>
     50 #include <sys/reboot.h>
     51 #include <sys/conf.h>
     52 #include <sys/exec.h>
     53 #include <sys/file.h>
     54 #include <sys/callout.h>
     55 #include <sys/malloc.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/msgbuf.h>
     58 #include <sys/mount.h>
     59 #include <sys/vnode.h>
     60 #include <sys/device.h>
     61 #include <sys/sa.h>
     62 #include <sys/syscallargs.h>
     63 #include <sys/filedesc.h>
     64 #include <sys/exec_elf.h>
     65 #include <sys/disklabel.h>
     66 #include <sys/ioctl.h>
     67 #include <miscfs/specfs/specdev.h>
     68 
     69 #include <compat/linux/common/linux_types.h>
     70 #include <compat/linux/common/linux_signal.h>
     71 #include <compat/linux/common/linux_util.h>
     72 #include <compat/linux/common/linux_ioctl.h>
     73 #include <compat/linux/common/linux_hdio.h>
     74 #include <compat/linux/common/linux_exec.h>
     75 #include <compat/linux/common/linux_machdep.h>
     76 
     77 #include <compat/linux/linux_syscallargs.h>
     78 
     79 #include <machine/cpu.h>
     80 #include <machine/fpu.h>
     81 #include <machine/psl.h>
     82 #include <machine/reg.h>
     83 #include <machine/vmparam.h>
     84 
     85 /*
     86  * To see whether wscons is configured (for virtual console ioctl calls).
     87  */
     88 #if defined(_KERNEL_OPT)
     89 #include "wsdisplay.h"
     90 #endif
     91 #if (NWSDISPLAY > 0)
     92 #include <dev/wscons/wsconsio.h>
     93 #include <dev/wscons/wsdisplay_usl_io.h>
     94 #endif
     95 
     96 /*
     97  * Set set up registers on exec.
     98  * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
     99  * entry uses NetBSD's native setregs instead of linux_setregs
    100  */
    101 void
    102 linux_setregs(l, pack, stack)
    103 	struct lwp *l;
    104 	struct exec_package *pack;
    105 	u_long stack;
    106 {
    107 	setregs(l, pack, stack);
    108 }
    109 
    110 /*
    111  * Send an interrupt to process.
    112  *
    113  * Adapted from arch/powerpc/powerpc/sig_machdep.c:sendsig and
    114  * compat/linux/arch/i386/linux_machdep.c:linux_sendsig
    115  *
    116  * XXX Does not work well yet with RT signals
    117  *
    118  */
    119 
    120 void
    121 linux_sendsig(catcher, sig, mask, code)  /* XXX Check me */
    122 	sig_t catcher;
    123 	int sig;
    124 	sigset_t *mask;
    125 	u_long code;
    126 {
    127 	struct lwp *l = curlwp;
    128 	struct proc *p = l->l_proc;
    129 	struct trapframe *tf;
    130 	struct linux_sigregs frame;
    131 	struct linux_pt_regs linux_regs;
    132 	struct linux_sigcontext sc;
    133 	register_t fp;
    134 	int onstack;
    135 	int i;
    136 
    137 	tf = trapframe(l);
    138 
    139 	/*
    140 	 * Do we need to jump onto the signal stack?
    141 	 */
    142 	onstack =
    143 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    144 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    145 
    146 	/*
    147 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
    148 	 * not use it yet. XXX fix this.
    149 	 */
    150 	onstack=0;
    151 
    152 	/*
    153 	 * Allocate space for the signal handler context.
    154 	 */
    155 	if (onstack) {
    156 		fp = (register_t)
    157 		    ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
    158 		    p->p_sigctx.ps_sigstk.ss_size);
    159 	} else {
    160 		fp = tf->fixreg[1];
    161 	}
    162 #ifdef DEBUG_LINUX
    163 	printf("fp at start of linux_sendsig = %x\n", fp);
    164 #endif
    165 	fp -= sizeof(struct linux_sigregs);
    166 	fp &= ~0xf;
    167 
    168 	/*
    169 	 * Prepare a sigcontext for later.
    170 	 */
    171 	memset(&sc, 0, sizeof sc);
    172 	sc.lsignal = (int)native_to_linux_signo[sig];
    173 	sc.lhandler = (unsigned long)catcher;
    174 	native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
    175 	sc.lregs = (struct linux_pt_regs*)fp;
    176 
    177 	/*
    178 	 * Setup the signal stack frame as Linux does it in
    179 	 * arch/ppc/kernel/signal.c:setup_frame()
    180 	 *
    181 	 * Save register context.
    182 	 */
    183 	for (i = 0; i < 32; i++)
    184 		linux_regs.lgpr[i] = tf->fixreg[i];
    185 	linux_regs.lnip = tf->srr0;
    186 	linux_regs.lmsr = tf->srr1;
    187 	linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
    188 	linux_regs.lctr = tf->ctr;
    189 	linux_regs.llink = tf->lr;
    190 	linux_regs.lxer = tf->xer;
    191 	linux_regs.lccr = tf->cr;
    192 	linux_regs.lmq = 0;  			/* Unused, 601 only */
    193 	linux_regs.ltrap = tf->exc;
    194 	linux_regs.ldar = tf->dar;
    195 	linux_regs.ldsisr = tf->dsisr;
    196 	linux_regs.lresult = 0;
    197 
    198 	memset(&frame, 0, sizeof(frame));
    199 	memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
    200 
    201 	if (curlwp == fpuproc)
    202 		save_fpu(curlwp);
    203 	memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpr, sizeof(frame.lfp_regs));
    204 
    205 	/*
    206 	 * Copy Linux's signal trampoline on the user stack It should not
    207 	 * be used, but Linux binaries might expect it to be there.
    208 	 */
    209 	frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
    210 	frame.ltramp[1] = 0x44000002; /* sc */
    211 
    212 	/*
    213 	 * Move it to the user stack
    214 	 * There is a little trick here, about the LINUX_ABIGAP: the
    215 	 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
    216 	 * binaries. But the Linux kernel seems to do without it, and it
    217 	 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
    218 	 */
    219 	if (copyout(&frame, (caddr_t)fp, sizeof (frame) - LINUX_ABIGAP) != 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 		p->p_sigctx.ps_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 	 * Make sure, fpu is sync'ed
    308 	 */
    309 	if (curlwp == fpuproc)
    310 		save_fpu(curlwp);
    311 
    312 	/*
    313 	 *  Restore register context.
    314 	 */
    315 	if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
    316 		   &sregs, sizeof(sregs)))
    317 		return (EFAULT);
    318 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
    319 
    320 	tf = trapframe(l);
    321 #ifdef DEBUG_LINUX
    322 	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
    323 	    (unsigned long)tf, (unsigned long)scp);
    324 #endif
    325 
    326 	if ((lregs->lmsr & PSL_USERSTATIC) !=  (tf->srr1 & PSL_USERSTATIC))
    327 		return (EINVAL);
    328 
    329 	for (i = 0; i < 32; i++)
    330 		tf->fixreg[i] = lregs->lgpr[i];
    331 	tf->lr = lregs->llink;
    332 	tf->cr = lregs->lccr;
    333 	tf->xer = lregs->lxer;
    334 	tf->ctr = lregs->lctr;
    335 	tf->srr0 = lregs->lnip;
    336 	tf->srr1 = lregs->lmsr;
    337 
    338 	memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
    339 	       sizeof(curpcb->pcb_fpu.fpr));
    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 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    351 	else */
    352 		p->p_sigctx.ps_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(p, SIG_SETMASK, &mask, 0);
    359 
    360 	return (EJUSTRETURN);
    361 }
    362 
    363 
    364 /*
    365  * The following needs code review for potential security issues
    366  */
    367 int
    368 linux_sys_sigreturn(l, v, retval)
    369 	struct lwp *l;
    370 	void *v;
    371 	register_t *retval;
    372 {
    373 	struct linux_sys_sigreturn_args /* {
    374 		syscallarg(struct linux_sigcontext *) scp;
    375 	} */ *uap = v;
    376 	struct proc *p = l->l_proc;
    377 	struct linux_sigcontext *scp, context;
    378 	struct linux_sigregs sregs;
    379 	struct linux_pt_regs *lregs;
    380 	struct trapframe *tf;
    381 	sigset_t mask;
    382 	int i;
    383 
    384 	/*
    385 	 * The trampoline code hands us the context.
    386 	 * It is unsafe to keep track of it ourselves, in the event that a
    387 	 * program jumps out of a signal handler.
    388 	 */
    389 	scp = SCARG(uap, scp);
    390 
    391 	/*
    392 	 * Get the context from user stack
    393 	 */
    394 	if (copyin(scp, &context, sizeof(*scp)))
    395 		return (EFAULT);
    396 
    397 	/*
    398 	 * Make sure, fpu is in sync
    399 	 */
    400 	if (curlwp == fpuproc)
    401 		save_fpu(curlwp);
    402 
    403 	/*
    404 	 *  Restore register context.
    405 	 */
    406 	if (copyin((caddr_t)context.lregs, &sregs, sizeof(sregs)))
    407 		return (EFAULT);
    408 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
    409 
    410 	tf = trapframe(l);
    411 #ifdef DEBUG_LINUX
    412 	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
    413 	    (unsigned long)tf, (unsigned long)scp);
    414 #endif
    415 
    416 	if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
    417 		return (EINVAL);
    418 
    419 	for (i = 0; i < 32; i++)
    420 		tf->fixreg[i] = lregs->lgpr[i];
    421 	tf->lr = lregs->llink;
    422 	tf->cr = lregs->lccr;
    423 	tf->xer = lregs->lxer;
    424 	tf->ctr = lregs->lctr;
    425 	tf->srr0 = lregs->lnip;
    426 	tf->srr1 = lregs->lmsr;
    427 
    428 	memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
    429 	       sizeof(curpcb->pcb_fpu.fpr));
    430 
    431 	/*
    432 	 * Restore signal stack.
    433 	 *
    434 	 * XXX cannot find the onstack information in Linux sig context.
    435 	 * Is signal stack really supported on Linux?
    436 	 */
    437 #if 0
    438 	if (sc.sc_onstack & SS_ONSTACK)
    439 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    440 	else
    441 #endif
    442 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    443 
    444 	/* Restore signal mask. */
    445 	linux_old_extra_to_native_sigset(&mask, &context.lmask,
    446 	    &context._unused[3]);
    447 	(void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
    448 
    449 	return (EJUSTRETURN);
    450 }
    451 
    452 
    453 #if 0
    454 int
    455 linux_sys_modify_ldt(p, v, retval)
    456 	struct proc *p;
    457 	void *v;
    458 	register_t *retval;
    459 {
    460 	/*
    461 	 * This syscall is not implemented in Linux/PowerPC: we should not
    462 	 * be here
    463 	 */
    464 #ifdef DEBUG_LINUX
    465 	printf("linux_sys_modify_ldt: should not be here.\n");
    466 #endif
    467   return 0;
    468 }
    469 #endif
    470 
    471 /*
    472  * major device numbers remapping
    473  */
    474 dev_t
    475 linux_fakedev(dev, raw)
    476 	dev_t dev;
    477 	int raw;
    478 {
    479 	/* XXX write me */
    480 	return dev;
    481 }
    482 
    483 /*
    484  * We come here in a last attempt to satisfy a Linux ioctl() call
    485  */
    486 int
    487 linux_machdepioctl(p, v, retval)
    488 	struct proc *p;
    489 	void *v;
    490 	register_t *retval;
    491 {
    492 	struct linux_sys_ioctl_args /* {
    493 		syscallarg(int) fd;
    494 		syscallarg(u_long) com;
    495 		syscallarg(caddr_t) data;
    496 	} */ *uap = v;
    497 	struct sys_ioctl_args bia;
    498 	u_long com;
    499 
    500 	SCARG(&bia, fd) = SCARG(uap, fd);
    501 	SCARG(&bia, data) = SCARG(uap, data);
    502 	com = SCARG(uap, com);
    503 
    504 	switch (com) {
    505 	default:
    506 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
    507 		return EINVAL;
    508 	}
    509 	SCARG(&bia, com) = com;
    510 	/* XXX NJWLWP */
    511 	return sys_ioctl(curlwp, &bia, retval);
    512 }
    513 #if 0
    514 /*
    515  * Set I/O permissions for a process. Just set the maximum level
    516  * right away (ignoring the argument), otherwise we would have
    517  * to rely on I/O permission maps, which are not implemented.
    518  */
    519 int
    520 linux_sys_iopl(l, v, retval)
    521 	struct lwp *l;
    522 	void *v;
    523 	register_t *retval;
    524 {
    525 	/*
    526 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    527 	 */
    528 #ifdef DEBUG_LINUX
    529 	printf("linux_sys_iopl: should not be here.\n");
    530 #endif
    531 	return 0;
    532 }
    533 #endif
    534 
    535 /*
    536  * See above. If a root process tries to set access to an I/O port,
    537  * just let it have the whole range.
    538  */
    539 int
    540 linux_sys_ioperm(l, v, retval)
    541 	struct lwp *l;
    542 	void *v;
    543 	register_t *retval;
    544 {
    545 	/*
    546 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    547 	 */
    548 #ifdef DEBUG_LINUX
    549 	printf("linux_sys_ioperm: should not be here.\n");
    550 #endif
    551 	return 0;
    552 }
    553 
    554 /*
    555  * wrapper linux_sys_new_uname() -> linux_sys_uname()
    556  */
    557 int
    558 linux_sys_new_uname(l, v, retval)
    559 	struct lwp *l;
    560 	void *v;
    561 	register_t *retval;
    562 {
    563 	return linux_sys_uname(l, v, retval);
    564 }
    565 
    566 /*
    567  * wrapper linux_sys_new_select() -> linux_sys_select()
    568  */
    569 int
    570 linux_sys_new_select(l, v, retval)
    571 	struct lwp *l;
    572 	void *v;
    573 	register_t *retval;
    574 {
    575 	return linux_sys_select(l, v, retval);
    576 }
    577