Home | History | Annotate | Line # | Download | only in powerpc
linux_machdep.c revision 1.5.4.10
      1 /*	$NetBSD: linux_machdep.c,v 1.5.4.10 2002/08/02 21:04:23 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.10 2002/08/02 21:04:23 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(sig, mask, code)  /* XXX Check me */
    122 	int sig;
    123 	sigset_t *mask;
    124 	u_long code;
    125 {
    126 	struct lwp *l = curlwp;
    127 	struct proc *p = l->l_proc;
    128 	struct trapframe *tf;
    129 	sig_t catcher = SIGACTION(p, sig).sa_handler;
    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 	save_fpu_lwp(curlwp);
    202 	memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpr, sizeof(frame.lfp_regs));
    203 
    204 	/*
    205 	 * Copy Linux's signal trampoline on the user stack It should not
    206 	 * be used, but Linux binaries might expect it to be there.
    207 	 */
    208 	frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
    209 	frame.ltramp[1] = 0x44000002; /* sc */
    210 
    211 	/*
    212 	 * Move it to the user stack
    213 	 * There is a little trick here, about the LINUX_ABIGAP: the
    214 	 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
    215 	 * binaries. But the Linux kernel seems to do without it, and it
    216 	 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
    217 	 */
    218 	if (copyout(&frame, (caddr_t)fp, sizeof (frame) - LINUX_ABIGAP) != 0) {
    219 		/*
    220 		 * Process has trashed its stack; give it an illegal
    221 		 * instruction to halt it in its tracks.
    222 		 */
    223 		sigexit(l, SIGILL);
    224 		/* NOTREACHED */
    225 	}
    226 
    227 	/*
    228 	 * Add a sigcontext on the stack
    229 	 */
    230 	fp -= sizeof(struct linux_sigcontext);
    231 	if (copyout(&sc, (caddr_t)fp, sizeof (struct linux_sigcontext)) != 0) {
    232 		/*
    233 		 * Process has trashed its stack; give it an illegal
    234 		 * instruction to halt it in its tracks.
    235 		 */
    236 		sigexit(l, SIGILL);
    237 		/* NOTREACHED */
    238 	}
    239 
    240 	/*
    241 	 * Set the registers according to how the Linux process expects them.
    242 	 * "Mind the gap" Linux expects a gap here.
    243 	 */
    244 	tf->fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
    245 	tf->lr = (int)catcher;
    246 	tf->fixreg[3] = (int)native_to_linux_signo[sig];
    247 	tf->fixreg[4] = fp;
    248 	tf->srr0 = (int)p->p_sigctx.ps_sigcode;
    249 
    250 #ifdef DEBUG_LINUX
    251 	printf("fp at end of linux_sendsig = %x\n", fp);
    252 #endif
    253 	/*
    254 	 * Remember that we're now on the signal stack.
    255 	 */
    256 	if (onstack)
    257 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    258 #ifdef DEBUG_LINUX
    259 	printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
    260 #endif
    261 }
    262 
    263 /*
    264  * System call to cleanup state after a signal
    265  * has been taken.  Reset signal mask and
    266  * stack state from context left by sendsig (above).
    267  * Return to previous pc and psl as specified by
    268  * context left by sendsig. Check carefully to
    269  * make sure that the user has not modified the
    270  * psl to gain improper privileges or to cause
    271  * a machine fault.
    272  *
    273  * XXX not tested
    274  */
    275 int
    276 linux_sys_rt_sigreturn(l, v, retval)
    277 	struct lwp *l;
    278 	void *v;
    279 	register_t *retval;
    280 {
    281 	struct linux_sys_rt_sigreturn_args /* {
    282 		syscallarg(struct linux_rt_sigframe *) sfp;
    283 	} */ *uap = v;
    284 	struct proc *p = l->l_proc;
    285 	struct linux_rt_sigframe *scp, sigframe;
    286 	struct linux_sigregs sregs;
    287 	struct linux_pt_regs *lregs;
    288 	struct trapframe *tf;
    289 	sigset_t mask;
    290 	int i;
    291 
    292 	/*
    293 	 * The trampoline code hands us the context.
    294 	 * It is unsafe to keep track of it ourselves, in the event that a
    295 	 * program jumps out of a signal handler.
    296 	 */
    297 	scp = SCARG(uap, sfp);
    298 
    299 	/*
    300 	 * Get the context from user stack
    301 	 */
    302 	if (copyin((caddr_t)scp, &sigframe, sizeof(*scp)))
    303 		return (EFAULT);
    304 
    305 	/*
    306 	 * Make sure, fpu is sync'ed
    307 	 */
    308 	save_fpu_lwp(curlwp);
    309 
    310 	/*
    311 	 *  Restore register context.
    312 	 */
    313 	if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
    314 		   &sregs, sizeof(sregs)))
    315 		return (EFAULT);
    316 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
    317 
    318 	tf = trapframe(l);
    319 #ifdef DEBUG_LINUX
    320 	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
    321 	    (unsigned long)tf, (unsigned long)scp);
    322 #endif
    323 
    324 	if ((lregs->lmsr & PSL_USERSTATIC) !=  (tf->srr1 & PSL_USERSTATIC))
    325 		return (EINVAL);
    326 
    327 	for (i = 0; i < 32; i++)
    328 		tf->fixreg[i] = lregs->lgpr[i];
    329 	tf->lr = lregs->llink;
    330 	tf->cr = lregs->lccr;
    331 	tf->xer = lregs->lxer;
    332 	tf->ctr = lregs->lctr;
    333 	tf->srr0 = lregs->lnip;
    334 	tf->srr1 = lregs->lmsr;
    335 
    336 	memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
    337 	       sizeof(curpcb->pcb_fpu.fpr));
    338 
    339 	/*
    340 	 * Restore signal stack.
    341 	 *
    342 	 * XXX cannot find the onstack information in Linux sig context.
    343 	 * Is signal stack really supported on Linux?
    344 	 *
    345 	 * It seems to be supported in libc6...
    346 	 */
    347 	/* if (sc.sc_onstack & SS_ONSTACK)
    348 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    349 	else */
    350 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    351 
    352 	/*
    353 	 * Grab the signal mask
    354 	 */
    355 	linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
    356 	(void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
    357 
    358 	return (EJUSTRETURN);
    359 }
    360 
    361 
    362 /*
    363  * The following needs code review for potential security issues
    364  */
    365 int
    366 linux_sys_sigreturn(l, v, retval)
    367 	struct lwp *l;
    368 	void *v;
    369 	register_t *retval;
    370 {
    371 	struct linux_sys_sigreturn_args /* {
    372 		syscallarg(struct linux_sigcontext *) scp;
    373 	} */ *uap = v;
    374 	struct proc *p = l->l_proc;
    375 	struct linux_sigcontext *scp, context;
    376 	struct linux_sigregs sregs;
    377 	struct linux_pt_regs *lregs;
    378 	struct trapframe *tf;
    379 	sigset_t mask;
    380 	int i;
    381 
    382 	/*
    383 	 * The trampoline code hands us the context.
    384 	 * It is unsafe to keep track of it ourselves, in the event that a
    385 	 * program jumps out of a signal handler.
    386 	 */
    387 	scp = SCARG(uap, scp);
    388 
    389 	/*
    390 	 * Get the context from user stack
    391 	 */
    392 	if (copyin(scp, &context, sizeof(*scp)))
    393 		return (EFAULT);
    394 
    395 	/*
    396 	 * Make sure, fpu is in sync
    397 	 */
    398 	save_fpu_lwp(curlwp);
    399 
    400 	/*
    401 	 *  Restore register context.
    402 	 */
    403 	if (copyin((caddr_t)context.lregs, &sregs, sizeof(sregs)))
    404 		return (EFAULT);
    405 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
    406 
    407 	tf = trapframe(l);
    408 #ifdef DEBUG_LINUX
    409 	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
    410 	    (unsigned long)tf, (unsigned long)scp);
    411 #endif
    412 
    413 	if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
    414 		return (EINVAL);
    415 
    416 	for (i = 0; i < 32; i++)
    417 		tf->fixreg[i] = lregs->lgpr[i];
    418 	tf->lr = lregs->llink;
    419 	tf->cr = lregs->lccr;
    420 	tf->xer = lregs->lxer;
    421 	tf->ctr = lregs->lctr;
    422 	tf->srr0 = lregs->lnip;
    423 	tf->srr1 = lregs->lmsr;
    424 
    425 	memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
    426 	       sizeof(curpcb->pcb_fpu.fpr));
    427 
    428 	/*
    429 	 * Restore signal stack.
    430 	 *
    431 	 * XXX cannot find the onstack information in Linux sig context.
    432 	 * Is signal stack really supported on Linux?
    433 	 */
    434 #if 0
    435 	if (sc.sc_onstack & SS_ONSTACK)
    436 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
    437 	else
    438 #endif
    439 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
    440 
    441 	/* Restore signal mask. */
    442 	linux_old_extra_to_native_sigset(&mask, &context.lmask,
    443 	    &context._unused[3]);
    444 	(void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
    445 
    446 	return (EJUSTRETURN);
    447 }
    448 
    449 
    450 #if 0
    451 int
    452 linux_sys_modify_ldt(p, v, retval)
    453 	struct proc *p;
    454 	void *v;
    455 	register_t *retval;
    456 {
    457 	/*
    458 	 * This syscall is not implemented in Linux/PowerPC: we should not
    459 	 * be here
    460 	 */
    461 #ifdef DEBUG_LINUX
    462 	printf("linux_sys_modify_ldt: should not be here.\n");
    463 #endif
    464   return 0;
    465 }
    466 #endif
    467 
    468 /*
    469  * major device numbers remapping
    470  */
    471 dev_t
    472 linux_fakedev(dev, raw)
    473 	dev_t dev;
    474 	int raw;
    475 {
    476 	/* XXX write me */
    477 	return dev;
    478 }
    479 
    480 /*
    481  * We come here in a last attempt to satisfy a Linux ioctl() call
    482  */
    483 int
    484 linux_machdepioctl(p, v, retval)
    485 	struct proc *p;
    486 	void *v;
    487 	register_t *retval;
    488 {
    489 	struct linux_sys_ioctl_args /* {
    490 		syscallarg(int) fd;
    491 		syscallarg(u_long) com;
    492 		syscallarg(caddr_t) data;
    493 	} */ *uap = v;
    494 	struct sys_ioctl_args bia;
    495 	u_long com;
    496 
    497 	SCARG(&bia, fd) = SCARG(uap, fd);
    498 	SCARG(&bia, data) = SCARG(uap, data);
    499 	com = SCARG(uap, com);
    500 
    501 	switch (com) {
    502 	default:
    503 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
    504 		return EINVAL;
    505 	}
    506 	SCARG(&bia, com) = com;
    507 	/* XXX NJWLWP */
    508 	return sys_ioctl(curlwp, &bia, retval);
    509 }
    510 #if 0
    511 /*
    512  * Set I/O permissions for a process. Just set the maximum level
    513  * right away (ignoring the argument), otherwise we would have
    514  * to rely on I/O permission maps, which are not implemented.
    515  */
    516 int
    517 linux_sys_iopl(l, v, retval)
    518 	struct lwp *l;
    519 	void *v;
    520 	register_t *retval;
    521 {
    522 	/*
    523 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    524 	 */
    525 #ifdef DEBUG_LINUX
    526 	printf("linux_sys_iopl: should not be here.\n");
    527 #endif
    528 	return 0;
    529 }
    530 #endif
    531 
    532 /*
    533  * See above. If a root process tries to set access to an I/O port,
    534  * just let it have the whole range.
    535  */
    536 int
    537 linux_sys_ioperm(l, v, retval)
    538 	struct lwp *l;
    539 	void *v;
    540 	register_t *retval;
    541 {
    542 	/*
    543 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    544 	 */
    545 #ifdef DEBUG_LINUX
    546 	printf("linux_sys_ioperm: should not be here.\n");
    547 #endif
    548 	return 0;
    549 }
    550 
    551 /*
    552  * wrapper linux_sys_new_uname() -> linux_sys_uname()
    553  */
    554 int
    555 linux_sys_new_uname(l, v, retval)
    556 	struct lwp *l;
    557 	void *v;
    558 	register_t *retval;
    559 {
    560 	return linux_sys_uname(l, v, retval);
    561 }
    562 
    563 /*
    564  * wrapper linux_sys_new_select() -> linux_sys_select()
    565  */
    566 int
    567 linux_sys_new_select(l, v, retval)
    568 	struct lwp *l;
    569 	void *v;
    570 	register_t *retval;
    571 {
    572 	return linux_sys_select(l, v, retval);
    573 }
    574