1 /* $NetBSD: linux_machdep.c,v 1.30 2012/08/16 16:41:53 matt 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 34 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.30 2012/08/16 16:41:53 matt Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/signalvar.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/buf.h> 42 #include <sys/reboot.h> 43 #include <sys/conf.h> 44 #include <sys/exec.h> 45 #include <sys/file.h> 46 #include <sys/callout.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/msgbuf.h> 50 #include <sys/mount.h> 51 #include <sys/vnode.h> 52 #include <sys/device.h> 53 #include <sys/syscallargs.h> 54 #include <sys/filedesc.h> 55 #include <sys/exec_elf.h> 56 #include <sys/disklabel.h> 57 #include <sys/ioctl.h> 58 #include <miscfs/specfs/specdev.h> 59 60 #include <compat/linux/common/linux_types.h> 61 #include <compat/linux/common/linux_signal.h> 62 #include <compat/linux/common/linux_util.h> 63 #include <compat/linux/common/linux_ioctl.h> 64 #include <compat/linux/common/linux_hdio.h> 65 #include <compat/linux/common/linux_exec.h> 66 #include <compat/linux/common/linux_machdep.h> 67 #include <compat/linux/linux_syscallargs.h> 68 69 void 70 linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack) 71 { 72 73 setregs(l, epp, stack); 74 } 75 76 void 77 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 78 { 79 struct lwp * const l = curlwp; 80 struct proc * const p = l->l_proc; 81 struct trapframe * const tf = lwp_trapframe(l); 82 struct linux_sigframe *fp, frame; 83 int onstack, error; 84 const int sig = ksi->ksi_signo; 85 sig_t catcher = SIGACTION(p, sig).sa_handler; 86 87 88 /* 89 * The Linux version of this code is in 90 * linux/arch/arm/kernel/signal.c. 91 */ 92 93 /* Do we need to jump onto the signal stack? */ 94 onstack = 95 (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 96 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 97 98 /* Allocate space for the signal handler context. */ 99 if (onstack) 100 fp = (struct linux_sigframe *)((char *)l->l_sigstk.ss_sp + 101 l->l_sigstk.ss_size); 102 else 103 fp = (struct linux_sigframe *)tf->tf_usr_sp; 104 fp--; 105 106 /* Build stack frame for signal trampoline. */ 107 108 /* Save register context. */ 109 frame.sf_sc.sc_r0 = tf->tf_r0; 110 frame.sf_sc.sc_r1 = tf->tf_r1; 111 frame.sf_sc.sc_r2 = tf->tf_r2; 112 frame.sf_sc.sc_r3 = tf->tf_r3; 113 frame.sf_sc.sc_r4 = tf->tf_r4; 114 frame.sf_sc.sc_r5 = tf->tf_r5; 115 frame.sf_sc.sc_r6 = tf->tf_r6; 116 frame.sf_sc.sc_r7 = tf->tf_r7; 117 frame.sf_sc.sc_r8 = tf->tf_r8; 118 frame.sf_sc.sc_r9 = tf->tf_r9; 119 frame.sf_sc.sc_r10 = tf->tf_r10; 120 frame.sf_sc.sc_r11 = tf->tf_r11; 121 frame.sf_sc.sc_r12 = tf->tf_r12; 122 frame.sf_sc.sc_sp = tf->tf_usr_sp; 123 frame.sf_sc.sc_lr = tf->tf_usr_lr; 124 frame.sf_sc.sc_pc = tf->tf_pc; 125 frame.sf_sc.sc_cpsr = tf->tf_spsr; 126 127 /* Save signal stack. */ 128 /* Linux doesn't save the onstack flag in sigframe */ 129 130 /* Save signal mask. */ 131 native_to_linux_old_extra_sigset(&frame.sf_sc.sc_mask, 132 frame.sf_extramask, mask); 133 134 /* Other state (mostly faked) */ 135 /* 136 * trapno should indicate the trap that caused the signal: 137 * 6 -> undefined instruction 138 * 11 -> address exception 139 * 14 -> data/prefetch abort 140 */ 141 frame.sf_sc.sc_trapno = 0; 142 frame.sf_sc.sc_error_code = 0; 143 frame.sf_sc.sc_fault_address = (u_int32_t) ksi->ksi_addr; 144 sendsig_reset(l, sig); 145 146 mutex_exit(p->p_lock); 147 error = copyout(&frame, fp, sizeof(frame)); 148 mutex_enter(p->p_lock); 149 150 if (error != 0) { 151 /* 152 * Process has trashed its stack; give it an illegal 153 * instruction to halt it in its tracks. 154 */ 155 sigexit(l, SIGILL); 156 /* NOTREACHED */ 157 } 158 /* 159 * Build context to run handler in. 160 */ 161 tf->tf_r0 = native_to_linux_signo[sig]; 162 tf->tf_r1 = 0; /* XXX Should be a siginfo_t */ 163 tf->tf_r2 = 0; 164 tf->tf_r3 = (register_t)catcher; 165 tf->tf_usr_sp = (register_t)fp; 166 tf->tf_pc = (register_t)p->p_sigctx.ps_sigcode; 167 168 /* Remember that we're now on the signal stack. */ 169 if (onstack) 170 l->l_sigstk.ss_flags |= SS_ONSTACK; 171 172 } 173 174 #if 0 175 /* 176 * System call to cleanup state after a signal 177 * has been taken. Reset signal mask and 178 * stack state from context left by sendsig (above). 179 * Return to previous pc and psl as specified by 180 * context left by sendsig. Check carefully to 181 * make sure that the user has not modified the 182 * psl to gain improper privileges or to cause 183 * a machine fault. 184 */ 185 int 186 linux_sys_rt_sigreturn(struct proc *p, void *v, register_t *retval) 187 { 188 /* XXX XAX write me */ 189 return(ENOSYS); 190 } 191 #endif 192 193 int 194 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *v, 195 register_t *retval) 196 { 197 struct trapframe * const tf = lwp_trapframe(l); 198 struct proc * const p = l->l_proc; 199 struct linux_sigframe *sfp, frame; 200 sigset_t mask; 201 202 /* 203 * The trampoline code hands us the context. 204 * It is unsafe to keep track of it ourselves, in the event that a 205 * program jumps out of a signal handler. 206 */ 207 sfp = (struct linux_sigframe *)tf->tf_usr_sp; 208 if (copyin((void *)sfp, &frame, sizeof(*sfp)) != 0) 209 return (EFAULT); 210 211 /* 212 * Make sure the processor mode has not been tampered with and 213 * interrupts have not been disabled. 214 */ 215 if (!VALID_R15_PSR(frame.sf_sc.sc_pc, frame.sf_sc.sc_cpsr)) 216 return EINVAL; 217 218 /* Restore register context. */ 219 tf->tf_r0 = frame.sf_sc.sc_r0; 220 tf->tf_r1 = frame.sf_sc.sc_r1; 221 tf->tf_r2 = frame.sf_sc.sc_r2; 222 tf->tf_r3 = frame.sf_sc.sc_r3; 223 tf->tf_r4 = frame.sf_sc.sc_r4; 224 tf->tf_r5 = frame.sf_sc.sc_r5; 225 tf->tf_r6 = frame.sf_sc.sc_r6; 226 tf->tf_r7 = frame.sf_sc.sc_r7; 227 tf->tf_r8 = frame.sf_sc.sc_r8; 228 tf->tf_r9 = frame.sf_sc.sc_r9; 229 tf->tf_r10 = frame.sf_sc.sc_r10; 230 tf->tf_r11 = frame.sf_sc.sc_r11; 231 tf->tf_r12 = frame.sf_sc.sc_r12; 232 tf->tf_usr_sp = frame.sf_sc.sc_sp; 233 tf->tf_usr_lr = frame.sf_sc.sc_lr; 234 tf->tf_pc = frame.sf_sc.sc_pc; 235 tf->tf_spsr = frame.sf_sc.sc_cpsr; 236 237 mutex_enter(p->p_lock); 238 239 /* Restore signal stack. */ 240 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 241 242 /* Restore signal mask. */ 243 linux_old_extra_to_native_sigset(&mask, &frame.sf_sc.sc_mask, 244 frame.sf_extramask); 245 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0); 246 247 mutex_exit(p->p_lock); 248 249 return (EJUSTRETURN); 250 } 251 252 /* 253 * major device numbers remapping 254 */ 255 dev_t 256 linux_fakedev(dev_t dev, int raw) 257 { 258 /* XXX write me */ 259 return dev; 260 } 261 262 /* 263 * We come here in a last attempt to satisfy a Linux ioctl() call 264 */ 265 int 266 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval) 267 { 268 /* { 269 syscallarg(int) fd; 270 syscallarg(u_long) com; 271 syscallarg(void *) data; 272 } */ 273 struct sys_ioctl_args bia; 274 u_long com; 275 276 SCARG(&bia, fd) = SCARG(uap, fd); 277 SCARG(&bia, data) = SCARG(uap, data); 278 com = SCARG(uap, com); 279 280 switch (com) { 281 default: 282 printf("linux_machdepioctl: invalid ioctl %08lx\n", com); 283 return EINVAL; 284 } 285 SCARG(&bia, com) = com; 286 287 return sys_ioctl(l, &bia, retval); 288 } 289 290 int 291 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg) 292 { 293 return 0; 294 } 295