Home | History | Annotate | Line # | Download | only in powerpc
linux_machdep.c revision 1.5.4.7
      1 /*	$NetBSD: linux_machdep.c,v 1.5.4.7 2002/06/20 03:43:02 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.7 2002/06/20 03:43:02 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(p, pack, stack)
    103 	struct proc *p;
    104 	struct exec_package *pack;
    105 	u_long stack;
    106 {
    107 	setregs(p, 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 proc *p = curproc;
    128 	struct trapframe *tf;
    129 	struct linux_sigregs frame;
    130 	struct linux_pt_regs linux_regs;
    131 	struct linux_sigcontext sc;
    132 	register_t fp;
    133 	int onstack;
    134 	int i;
    135 
    136 	tf = trapframe(p);
    137 
    138 	/*
    139 	 * Do we need to jump onto the signal stack?
    140 	 */
    141 	onstack =
    142 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    143 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    144 
    145 	/*
    146 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
    147 	 * not use it yet. XXX fix this.
    148 	 */
    149 	onstack=0;
    150 
    151 	/*
    152 	 * Allocate space for the signal handler context.
    153 	 */
    154 	if (onstack) {
    155 		fp = (register_t)
    156 		    ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
    157 		    p->p_sigctx.ps_sigstk.ss_size);
    158 	} else {
    159 		fp = tf->fixreg[1];
    160 	}
    161 #ifdef DEBUG_LINUX
    162 	printf("fp at start of linux_sendsig = %x\n", fp);
    163 #endif
    164 	fp -= sizeof(struct linux_sigregs);
    165 	fp &= ~0xf;
    166 
    167 	/*
    168 	 * Prepare a sigcontext for later.
    169 	 */
    170 	memset(&sc, 0, sizeof sc);
    171 	sc.lsignal = (int)native_to_linux_signo[sig];
    172 	sc.lhandler = (unsigned long)catcher;
    173 	native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
    174 	sc.lregs = (struct linux_pt_regs*)fp;
    175 
    176 	/*
    177 	 * Setup the signal stack frame as Linux does it in
    178 	 * arch/ppc/kernel/signal.c:setup_frame()
    179 	 *
    180 	 * Save register context.
    181 	 */
    182 	for (i = 0; i < 32; i++)
    183 		linux_regs.lgpr[i] = tf->fixreg[i];
    184 	linux_regs.lnip = tf->srr0;
    185 	linux_regs.lmsr = tf->srr1;
    186 	linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
    187 	linux_regs.lctr = tf->ctr;
    188 	linux_regs.llink = tf->lr;
    189 	linux_regs.lxer = tf->xer;
    190 	linux_regs.lccr = tf->cr;
    191 	linux_regs.lmq = 0;  			/* Unused, 601 only */
    192 	linux_regs.ltrap = tf->exc;
    193 	linux_regs.ldar = tf->dar;
    194 	linux_regs.ldsisr = tf->dsisr;
    195 	linux_regs.lresult = 0;
    196 
    197 	memset(&frame, 0, sizeof(frame));
    198 	memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
    199 
    200 	if (curproc == fpuproc)
    201 		save_fpu(curproc);
    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(p, 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(p, 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(p, v, retval)
    277 	struct proc *p;
    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 linux_rt_sigframe *scp, sigframe;
    285 	struct linux_sigregs sregs;
    286 	struct linux_pt_regs *lregs;
    287 	struct trapframe *tf;
    288 	sigset_t mask;
    289 	int i;
    290 
    291 	/*
    292 	 * The trampoline code hands us the context.
    293 	 * It is unsafe to keep track of it ourselves, in the event that a
    294 	 * program jumps out of a signal handler.
    295 	 */
    296 	scp = SCARG(uap, sfp);
    297 
    298 	/*
    299 	 * Get the context from user stack
    300 	 */
    301 	if (copyin((caddr_t)scp, &sigframe, sizeof(*scp)))
    302 		return (EFAULT);
    303 
    304 	/*
    305 	 * Make sure, fpu is sync'ed
    306 	 */
    307 	if (curproc == fpuproc)
    308 		save_fpu(curproc);
    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(p);
    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(p, v, retval)
    367 	struct proc *p;
    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 linux_sigcontext *scp, context;
    375 	struct linux_sigregs sregs;
    376 	struct linux_pt_regs *lregs;
    377 	struct trapframe *tf;
    378 	sigset_t mask;
    379 	int i;
    380 
    381 	/*
    382 	 * The trampoline code hands us the context.
    383 	 * It is unsafe to keep track of it ourselves, in the event that a
    384 	 * program jumps out of a signal handler.
    385 	 */
    386 	scp = SCARG(uap, scp);
    387 
    388 	/*
    389 	 * Get the context from user stack
    390 	 */
    391 	if (copyin(scp, &context, sizeof(*scp)))
    392 		return (EFAULT);
    393 
    394 	/*
    395 	 * Make sure, fpu is in sync
    396 	 */
    397 	if (curproc == fpuproc)
    398 		save_fpu(curproc);
    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(p);
    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 	return sys_ioctl(p, &bia, retval);
    508 }
    509 #if 0
    510 /*
    511  * Set I/O permissions for a process. Just set the maximum level
    512  * right away (ignoring the argument), otherwise we would have
    513  * to rely on I/O permission maps, which are not implemented.
    514  */
    515 int
    516 linux_sys_iopl(p, v, retval)
    517 	struct proc *p;
    518 	void *v;
    519 	register_t *retval;
    520 {
    521 	/*
    522 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    523 	 */
    524 #ifdef DEBUG_LINUX
    525 	printf("linux_sys_iopl: should not be here.\n");
    526 #endif
    527 	return 0;
    528 }
    529 #endif
    530 
    531 /*
    532  * See above. If a root process tries to set access to an I/O port,
    533  * just let it have the whole range.
    534  */
    535 int
    536 linux_sys_ioperm(p, v, retval)
    537 	struct proc *p;
    538 	void *v;
    539 	register_t *retval;
    540 {
    541 	/*
    542 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
    543 	 */
    544 #ifdef DEBUG_LINUX
    545 	printf("linux_sys_ioperm: should not be here.\n");
    546 #endif
    547 	return 0;
    548 }
    549 
    550 /*
    551  * wrapper linux_sys_new_uname() -> linux_sys_uname()
    552  */
    553 int
    554 linux_sys_new_uname(p, v, retval)
    555 	struct proc *p;
    556 	void *v;
    557 	register_t *retval;
    558 {
    559 	return linux_sys_uname(p, v, retval);
    560 }
    561 
    562 /*
    563  * wrapper linux_sys_new_select() -> linux_sys_select()
    564  */
    565 int
    566 linux_sys_new_select(p, v, retval)
    567 	struct proc *p;
    568 	void *v;
    569 	register_t *retval;
    570 {
    571 	return linux_sys_select(p, v, retval);
    572 }
    573