Home | History | Annotate | Line # | Download | only in arm
linux_machdep.c revision 1.26
      1 /*	$NetBSD: linux_machdep.c,v 1.26 2008/04/24 18:39:22 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 2000 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.
      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 
     41 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.26 2008/04/24 18:39:22 ad Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/signalvar.h>
     46 #include <sys/kernel.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/syscallargs.h>
     62 #include <sys/filedesc.h>
     63 #include <sys/exec_elf.h>
     64 #include <sys/disklabel.h>
     65 #include <sys/ioctl.h>
     66 #include <miscfs/specfs/specdev.h>
     67 
     68 #include <compat/linux/common/linux_types.h>
     69 #include <compat/linux/common/linux_signal.h>
     70 #include <compat/linux/common/linux_util.h>
     71 #include <compat/linux/common/linux_ioctl.h>
     72 #include <compat/linux/common/linux_hdio.h>
     73 #include <compat/linux/common/linux_exec.h>
     74 #include <compat/linux/common/linux_machdep.h>
     75 #include <compat/linux/linux_syscallargs.h>
     76 
     77 void
     78 linux_setregs(struct lwp *l, struct exec_package *epp, u_long stack)
     79 {
     80 
     81 	setregs(l, epp, stack);
     82 }
     83 
     84 void
     85 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
     86 {
     87 	struct lwp *l = curlwp;
     88 	struct proc *p = l->l_proc;
     89 	struct trapframe *tf;
     90 	struct linux_sigframe *fp, frame;
     91 	int onstack, error;
     92 	const int sig = ksi->ksi_signo;
     93 	sig_t catcher = SIGACTION(p, sig).sa_handler;
     94 
     95 	tf = process_frame(l);
     96 
     97 	/*
     98 	 * The Linux version of this code is in
     99 	 * linux/arch/arm/kernel/signal.c.
    100 	 */
    101 
    102 	/* Do we need to jump onto the signal stack? */
    103 	onstack =
    104 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    105 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    106 
    107 	/* Allocate space for the signal handler context. */
    108 	if (onstack)
    109 		fp = (struct linux_sigframe *)((char *)l->l_sigstk.ss_sp +
    110 					  l->l_sigstk.ss_size);
    111 	else
    112 		fp = (struct linux_sigframe *)tf->tf_usr_sp;
    113 	fp--;
    114 
    115 	/* Build stack frame for signal trampoline. */
    116 
    117 	/* Save register context. */
    118 	frame.sf_sc.sc_r0     = tf->tf_r0;
    119 	frame.sf_sc.sc_r1     = tf->tf_r1;
    120 	frame.sf_sc.sc_r2     = tf->tf_r2;
    121 	frame.sf_sc.sc_r3     = tf->tf_r3;
    122 	frame.sf_sc.sc_r4     = tf->tf_r4;
    123 	frame.sf_sc.sc_r5     = tf->tf_r5;
    124 	frame.sf_sc.sc_r6     = tf->tf_r6;
    125 	frame.sf_sc.sc_r7     = tf->tf_r7;
    126 	frame.sf_sc.sc_r8     = tf->tf_r8;
    127 	frame.sf_sc.sc_r9     = tf->tf_r9;
    128 	frame.sf_sc.sc_r10    = tf->tf_r10;
    129 	frame.sf_sc.sc_r11    = tf->tf_r11;
    130 	frame.sf_sc.sc_r12    = tf->tf_r12;
    131 	frame.sf_sc.sc_sp     = tf->tf_usr_sp;
    132 	frame.sf_sc.sc_lr     = tf->tf_usr_lr;
    133 	frame.sf_sc.sc_pc     = tf->tf_pc;
    134 	frame.sf_sc.sc_cpsr   = tf->tf_spsr;
    135 
    136 	/* Save signal stack. */
    137 	/* Linux doesn't save the onstack flag in sigframe */
    138 
    139 	/* Save signal mask. */
    140 	native_to_linux_old_extra_sigset(&frame.sf_sc.sc_mask,
    141 	    frame.sf_extramask, mask);
    142 
    143 	/* Other state (mostly faked) */
    144 	/*
    145 	 * trapno should indicate the trap that caused the signal:
    146 	 * 6  -> undefined instruction
    147 	 * 11 -> address exception
    148 	 * 14 -> data/prefetch abort
    149 	 */
    150 	frame.sf_sc.sc_trapno = 0;
    151 	frame.sf_sc.sc_error_code = 0;
    152 	frame.sf_sc.sc_fault_address = (u_int32_t) ksi->ksi_addr;
    153 	sendsig_reset(l, sig);
    154 
    155 	mutex_exit(p->p_lock);
    156 	error = copyout(&frame, fp, sizeof(frame));
    157 	mutex_enter(p->p_lock);
    158 
    159 	if (error != 0) {
    160 		/*
    161 		 * Process has trashed its stack; give it an illegal
    162 		 * instruction to halt it in its tracks.
    163 		 */
    164 		sigexit(l, SIGILL);
    165 		/* NOTREACHED */
    166 	}
    167 	/*
    168 	 * Build context to run handler in.
    169 	 */
    170 	tf->tf_r0 = native_to_linux_signo[sig];
    171 	tf->tf_r1 = 0; /* XXX Should be a siginfo_t */
    172 	tf->tf_r2 = 0;
    173 	tf->tf_r3 = (register_t)catcher;
    174 	tf->tf_usr_sp = (register_t)fp;
    175 	tf->tf_pc = (register_t)p->p_sigctx.ps_sigcode;
    176 
    177 	/* Remember that we're now on the signal stack. */
    178 	if (onstack)
    179 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    180 
    181 }
    182 
    183 #if 0
    184 /*
    185  * System call to cleanup state after a signal
    186  * has been taken.  Reset signal mask and
    187  * stack state from context left by sendsig (above).
    188  * Return to previous pc and psl as specified by
    189  * context left by sendsig. Check carefully to
    190  * make sure that the user has not modified the
    191  * psl to gain improper privileges or to cause
    192  * a machine fault.
    193  */
    194 int
    195 linux_sys_rt_sigreturn(struct proc *p, void *v, register_t *retval)
    196 {
    197 	/* XXX XAX write me */
    198 	return(ENOSYS);
    199 }
    200 #endif
    201 
    202 int
    203 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *v,
    204 	register_t *retval)
    205 {
    206 	struct linux_sigframe *sfp, frame;
    207 	struct proc *p = l->l_proc;
    208 	struct trapframe *tf;
    209 	sigset_t mask;
    210 
    211 	tf = process_frame(l);
    212 
    213 	/*
    214 	 * The trampoline code hands us the context.
    215 	 * It is unsafe to keep track of it ourselves, in the event that a
    216 	 * program jumps out of a signal handler.
    217 	 */
    218 	sfp = (struct linux_sigframe *)tf->tf_usr_sp;
    219 	if (copyin((void *)sfp, &frame, sizeof(*sfp)) != 0)
    220 		return (EFAULT);
    221 
    222 	/*
    223 	 * Make sure the processor mode has not been tampered with and
    224 	 * interrupts have not been disabled.
    225 	 */
    226 	if (!VALID_R15_PSR(frame.sf_sc.sc_pc, frame.sf_sc.sc_cpsr))
    227 		return EINVAL;
    228 
    229 	/* Restore register context. */
    230 	tf = process_frame(l);
    231 	tf->tf_r0    = frame.sf_sc.sc_r0;
    232 	tf->tf_r1    = frame.sf_sc.sc_r1;
    233 	tf->tf_r2    = frame.sf_sc.sc_r2;
    234 	tf->tf_r3    = frame.sf_sc.sc_r3;
    235 	tf->tf_r4    = frame.sf_sc.sc_r4;
    236 	tf->tf_r5    = frame.sf_sc.sc_r5;
    237 	tf->tf_r6    = frame.sf_sc.sc_r6;
    238 	tf->tf_r7    = frame.sf_sc.sc_r7;
    239 	tf->tf_r8    = frame.sf_sc.sc_r8;
    240 	tf->tf_r9    = frame.sf_sc.sc_r9;
    241 	tf->tf_r10   = frame.sf_sc.sc_r10;
    242 	tf->tf_r11   = frame.sf_sc.sc_r11;
    243 	tf->tf_r12   = frame.sf_sc.sc_r12;
    244 	tf->tf_usr_sp = frame.sf_sc.sc_sp;
    245 	tf->tf_usr_lr = frame.sf_sc.sc_lr;
    246 	tf->tf_pc    = frame.sf_sc.sc_pc;
    247 	tf->tf_spsr  = frame.sf_sc.sc_cpsr;
    248 
    249 	mutex_enter(p->p_lock);
    250 
    251 	/* Restore signal stack. */
    252 	l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    253 
    254 	/* Restore signal mask. */
    255 	linux_old_extra_to_native_sigset(&mask, &frame.sf_sc.sc_mask,
    256 	    frame.sf_extramask);
    257 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
    258 
    259 	mutex_exit(p->p_lock);
    260 
    261 	return (EJUSTRETURN);
    262 }
    263 
    264 /*
    265  * major device numbers remapping
    266  */
    267 dev_t
    268 linux_fakedev(dev_t dev, int raw)
    269 {
    270 	/* XXX write me */
    271 	return dev;
    272 }
    273 
    274 /*
    275  * We come here in a last attempt to satisfy a Linux ioctl() call
    276  */
    277 int
    278 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
    279 {
    280 	/* {
    281 		syscallarg(int) fd;
    282 		syscallarg(u_long) com;
    283 		syscallarg(void *) data;
    284 	} */
    285 	struct sys_ioctl_args bia;
    286 	u_long com;
    287 
    288 	SCARG(&bia, fd) = SCARG(uap, fd);
    289 	SCARG(&bia, data) = SCARG(uap, data);
    290 	com = SCARG(uap, com);
    291 
    292 	switch (com) {
    293 	default:
    294 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
    295 		return EINVAL;
    296 	}
    297 	SCARG(&bia, com) = com;
    298 
    299 	return sys_ioctl(l, &bia, retval);
    300 }
    301 
    302 int
    303 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    304 {
    305 	return 0;
    306 }
    307