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