1 /* $NetBSD: linux32_machdep.c,v 1.1 2006/02/09 19:18:57 manu Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Emmanuel Dreyfus 17 * 4. The name of the author may not be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS'' 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.1 2006/02/09 19:18:57 manu 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/user.h> 42 #include <sys/buf.h> 43 #include <sys/reboot.h> 44 #include <sys/conf.h> 45 #include <sys/exec.h> 46 #include <sys/file.h> 47 #include <sys/callout.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/msgbuf.h> 51 #include <sys/mount.h> 52 #include <sys/vnode.h> 53 #include <sys/device.h> 54 #include <sys/sa.h> 55 #include <sys/syscallargs.h> 56 #include <sys/filedesc.h> 57 #include <sys/exec_elf.h> 58 #include <sys/disklabel.h> 59 #include <sys/ioctl.h> 60 #include <sys/wait.h> 61 #include <miscfs/specfs/specdev.h> 62 63 #include <machine/netbsd32_machdep.h> 64 65 #include <compat/netbsd32/netbsd32.h> 66 #include <compat/netbsd32/netbsd32_syscallargs.h> 67 68 #include <compat/linux/common/linux_signal.h> 69 #include <compat/linux/common/linux_errno.h> 70 71 #include <compat/linux32/common/linux32_types.h> 72 #include <compat/linux32/common/linux32_errno.h> 73 #include <compat/linux32/common/linux32_machdep.h> 74 #include <compat/linux32/common/linux32_signal.h> 75 #include <compat/linux32/common/linux32_exec.h> 76 #include <compat/linux32/linux32_syscallargs.h> 77 78 #include <machine/cpu.h> 79 #include <machine/cpufunc.h> 80 #include <machine/psl.h> 81 #include <machine/reg.h> 82 #include <machine/segments.h> 83 #include <machine/specialreg.h> 84 #include <machine/sysarch.h> 85 #include <machine/vmparam.h> 86 87 extern char linux32_sigcode[1]; 88 extern char linux32_rt_sigcode[1]; 89 extern char linux32_esigcode[1]; 90 91 extern void (osyscall_return) __P((void)); 92 93 static void linux32_save_ucontext __P((struct lwp *, struct trapframe *, 94 const sigset_t *, struct sigaltstack *, struct linux32_ucontext *)); 95 static void linux32_save_sigcontext __P((struct lwp *, struct trapframe *, 96 const sigset_t *, struct linux32_sigcontext *)); 97 static void linux32_rt_sendsig __P((const ksiginfo_t *, const sigset_t *)); 98 static void linux32_old_sendsig __P((const ksiginfo_t *, const sigset_t *)); 99 static int linux32_restore_sigcontext __P((struct lwp *, 100 struct linux32_sigcontext *, register_t *)); 101 102 void 103 linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 104 { 105 if (SIGACTION(curproc, ksi->ksi_signo).sa_flags & SA_SIGINFO) 106 linux32_rt_sendsig(ksi, mask); 107 else 108 linux32_old_sendsig(ksi, mask); 109 return; 110 } 111 112 void 113 linux32_old_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 114 { 115 struct lwp *l = curlwp; 116 struct proc *p = l->l_proc; 117 struct trapframe *tf; 118 struct linux32_sigframe *fp, frame; 119 int onstack; 120 int sig = ksi->ksi_signo; 121 sig_t catcher = SIGACTION(p, sig).sa_handler; 122 struct sigaltstack *sas = &p->p_sigctx.ps_sigstk; 123 124 tf = l->l_md.md_regs; 125 /* Do we need to jump onto the signal stack? */ 126 onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 127 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 128 129 130 /* Allocate space for the signal handler context. */ 131 if (onstack) 132 fp = (struct linux32_sigframe *)((caddr_t)sas->ss_sp + 133 sas->ss_size); 134 else 135 fp = (struct linux32_sigframe *)tf->tf_rsp; 136 fp--; 137 138 /* Build stack frame for signal trampoline. */ 139 frame.sf_handler = (linux32_handler_t)(long)catcher; 140 frame.sf_sig = native_to_linux32_signo[sig]; 141 142 linux32_save_sigcontext(l, tf, mask, &frame.sf_sc); 143 144 if (copyout(&frame, fp, sizeof(frame)) != 0) { 145 /* 146 * Process has trashed its stack; give it an illegal 147 * instruction to halt it in its tracks. 148 */ 149 sigexit(l, SIGILL); 150 /* NOTREACHED */ 151 } 152 153 /* 154 * Build context to run handler in. 155 */ 156 tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 157 tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 158 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 159 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 160 tf->tf_rip = ((long)p->p_sigctx.ps_sigcode) & 0xffffffff; 161 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff; 162 tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC) & 0xffffffff; 163 tf->tf_rsp = (long)fp & 0xffffffff; 164 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 165 166 /* Remember that we're now on the signal stack. */ 167 if (onstack) 168 sas->ss_flags |= SS_ONSTACK; 169 170 return; 171 } 172 173 void 174 linux32_rt_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 175 { 176 struct lwp *l = curlwp; 177 struct proc *p = l->l_proc; 178 struct trapframe *tf; 179 struct linux32_rt_sigframe *fp, frame; 180 int onstack; 181 linux32_siginfo_t *lsi; 182 int sig = ksi->ksi_signo; 183 sig_t catcher = SIGACTION(p, sig).sa_handler; 184 struct sigaltstack *sas = &p->p_sigctx.ps_sigstk; 185 186 tf = l->l_md.md_regs; 187 /* Do we need to jump onto the signal stack? */ 188 onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 189 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 190 191 192 /* Allocate space for the signal handler context. */ 193 if (onstack) 194 fp = (struct linux32_rt_sigframe *)((caddr_t)sas->ss_sp + 195 sas->ss_size); 196 else 197 fp = (struct linux32_rt_sigframe *)tf->tf_rsp; 198 fp--; 199 200 /* Build stack frame for signal trampoline. */ 201 frame.sf_handler = (linux32_handler_t)(long)catcher; 202 frame.sf_sig = native_to_linux32_signo[sig]; 203 frame.sf_sip = (linux32_siginfop_t)(long)&fp->sf_si; 204 frame.sf_ucp = (linux32_ucontextp_t)(long)&fp->sf_uc; 205 206 lsi = &frame.sf_si; 207 (void)memset(lsi, 0, sizeof(frame.sf_si)); 208 lsi->lsi_errno = native_to_linux32_errno[ksi->ksi_errno]; 209 lsi->lsi_code = ksi->ksi_code; 210 lsi->lsi_signo = native_to_linux32_signo[frame.sf_sig]; 211 switch (lsi->lsi_signo) { 212 case LINUX32_SIGILL: 213 case LINUX32_SIGFPE: 214 case LINUX32_SIGSEGV: 215 case LINUX32_SIGBUS: 216 case LINUX32_SIGTRAP: 217 lsi->lsi_addr = (netbsd32_voidp)(long)ksi->ksi_addr; 218 break; 219 case LINUX32_SIGCHLD: 220 lsi->lsi_uid = ksi->ksi_uid; 221 lsi->lsi_pid = ksi->ksi_pid; 222 lsi->lsi_utime = ksi->ksi_utime; 223 lsi->lsi_stime = ksi->ksi_stime; 224 225 /* We use the same codes */ 226 lsi->lsi_code = ksi->ksi_code; 227 /* XXX is that right? */ 228 lsi->lsi_status = WEXITSTATUS(ksi->ksi_status); 229 break; 230 case LINUX32_SIGIO: 231 lsi->lsi_band = ksi->ksi_band; 232 lsi->lsi_fd = ksi->ksi_fd; 233 break; 234 default: 235 lsi->lsi_uid = ksi->ksi_uid; 236 lsi->lsi_pid = ksi->ksi_pid; 237 if (lsi->lsi_signo == LINUX32_SIGALRM || 238 lsi->lsi_signo >= LINUX32_SIGRTMIN) 239 lsi->lsi_value.sival_ptr = 240 (netbsd32_voidp)(long)ksi->ksi_sigval.sival_ptr; 241 break; 242 } 243 244 /* Save register context. */ 245 linux32_save_ucontext(l, tf, mask, sas, &frame.sf_uc); 246 247 if (copyout(&frame, fp, sizeof(frame)) != 0) { 248 /* 249 * Process has trashed its stack; give it an illegal 250 * instruction to halt it in its tracks. 251 */ 252 sigexit(l, SIGILL); 253 /* NOTREACHED */ 254 } 255 256 /* 257 * Build context to run handler in. 258 */ 259 tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 260 tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 261 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 262 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 263 tf->tf_rip = (((long)p->p_sigctx.ps_sigcode) + 264 (linux32_rt_sigcode - linux32_sigcode)) & 0xffffffff; 265 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff; 266 tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC) & 0xffffffff; 267 tf->tf_rsp = (long)fp & 0xffffffff; 268 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 269 270 /* Remember that we're now on the signal stack. */ 271 if (onstack) 272 sas->ss_flags |= SS_ONSTACK; 273 274 return; 275 } 276 277 void 278 linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack) 279 { 280 struct pcb *pcb = &l->l_addr->u_pcb; 281 struct trapframe *tf; 282 struct proc *p = l->l_proc; 283 void **retaddr; 284 285 /* If we were using the FPU, forget about it. */ 286 if (l->l_addr->u_pcb.pcb_fpcpu != NULL) 287 fpusave_lwp(l, 0); 288 289 #if defined(USER_LDT) && 0 290 pmap_ldt_cleanup(p); 291 #endif 292 293 netbsd32_adjust_limits(p); 294 295 l->l_md.md_flags &= ~MDP_USEDFPU; 296 pcb->pcb_flags = 0; 297 pcb->pcb_savefpu.fp_fxsave.fx_fcw = __Linux_NPXCW__; 298 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__; 299 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__; 300 pcb->pcb_fs = 0; 301 pcb->pcb_gs = 0; 302 303 304 l->l_proc->p_flag |= P_32; 305 306 tf = l->l_md.md_regs; 307 tf->tf_rax = 0; 308 tf->tf_rbx = (u_int64_t)l->l_proc->p_psstr & 0xffffffff; 309 tf->tf_rcx = pack->ep_entry & 0xffffffff; 310 tf->tf_rdx = 0; 311 tf->tf_rsi = 0; 312 tf->tf_rdi = 0; 313 tf->tf_rbp = 0; 314 tf->tf_rsp = stack & 0xffffffff; 315 tf->tf_r8 = 0; 316 tf->tf_r9 = 0; 317 tf->tf_r10 = 0; 318 tf->tf_r11 = 0; 319 tf->tf_r12 = 0; 320 tf->tf_r13 = 0; 321 tf->tf_r14 = 0; 322 tf->tf_r15 = 0; 323 tf->tf_rip = pack->ep_entry & 0xffffffff; 324 tf->tf_rflags = PSL_USERSET; 325 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff; 326 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 327 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 328 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 329 tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 330 tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 331 332 /* XXX frob return address to return via old iret method, not sysret */ 333 retaddr = (void **)tf - 1; 334 *retaddr = (void *)osyscall_return; 335 return; 336 } 337 338 static void 339 linux32_save_ucontext(l, tf, mask, sas, uc) 340 struct lwp *l; 341 struct trapframe *tf; 342 const sigset_t *mask; 343 struct sigaltstack *sas; 344 struct linux32_ucontext *uc; 345 { 346 uc->uc_flags = 0; 347 uc->uc_link = (linux32_ucontextp_t)(long)NULL; 348 native_to_linux32_sigaltstack(&uc->uc_stack, sas); 349 linux32_save_sigcontext(l, tf, mask, &uc->uc_mcontext); 350 native_to_linux32_sigset(&uc->uc_sigmask, mask); 351 (void)memset(&uc->uc_fpregs_mem, 0, sizeof(uc->uc_fpregs_mem)); 352 } 353 354 static void 355 linux32_save_sigcontext(l, tf, mask, sc) 356 struct lwp *l; 357 struct trapframe *tf; 358 const sigset_t *mask; 359 struct linux32_sigcontext *sc; 360 { 361 /* Save register context. */ 362 sc->sc_gs = tf->tf_gs; 363 sc->sc_fs = tf->tf_fs; 364 sc->sc_es = tf->tf_es; 365 sc->sc_ds = tf->tf_ds; 366 sc->sc_eflags = tf->tf_rflags; 367 sc->sc_edi = tf->tf_rdi; 368 sc->sc_esi = tf->tf_rsi; 369 sc->sc_esp = tf->tf_rsp; 370 sc->sc_ebp = tf->tf_rbp; 371 sc->sc_ebx = tf->tf_rbx; 372 sc->sc_edx = tf->tf_rdx; 373 sc->sc_ecx = tf->tf_rcx; 374 sc->sc_eax = tf->tf_rax; 375 sc->sc_eip = tf->tf_rip; 376 sc->sc_cs = tf->tf_cs; 377 sc->sc_esp_at_signal = tf->tf_rsp; 378 sc->sc_ss = tf->tf_ss; 379 sc->sc_err = tf->tf_err; 380 sc->sc_trapno = tf->tf_trapno; 381 /* sc->sc_cr2 = l->l_addr->u_pcb.pcb_cr2; */ /* XXX */ 382 sc->sc_387 = (linux32_fpstatep_t)(long)NULL; 383 384 /* Save signal stack. */ 385 /* Linux doesn't save the onstack flag in sigframe */ 386 387 /* Save signal mask. */ 388 native_to_linux32_old_sigset(&sc->sc_mask, mask); 389 } 390 391 int 392 linux32_sys_sigreturn(l, v, retval) 393 struct lwp *l; 394 void *v; 395 register_t *retval; 396 { 397 struct linux32_sys_sigreturn_args /* { 398 syscallarg(linux32_sigcontextp_t) scp; 399 } */ *uap = v; 400 struct linux32_sigcontext ctx; 401 int error; 402 403 if ((error = copyin(NETBSD32PTR64(SCARG(uap, scp)), 404 &ctx, sizeof(ctx))) != 0) 405 return error; 406 407 return linux32_restore_sigcontext(l, &ctx, retval); 408 } 409 410 int 411 linux32_sys_rt_sigreturn(l, v, retval) 412 struct lwp *l; 413 void *v; 414 register_t *retval; 415 { 416 struct linux32_sys_rt_sigreturn_args /* { 417 syscallarg(linux32_ucontextp_t) ucp; 418 } */ *uap = v; 419 struct linux32_ucontext ctx; 420 int error; 421 422 if ((error = copyin(NETBSD32PTR64(SCARG(uap, ucp)), 423 &ctx, sizeof(ctx))) != 0) 424 return error; 425 426 return linux32_restore_sigcontext(l, &ctx.uc_mcontext, retval); 427 } 428 429 static int 430 linux32_restore_sigcontext(l, scp, retval) 431 struct lwp *l; 432 struct linux32_sigcontext *scp; 433 register_t *retval; 434 { 435 struct trapframe *tf; 436 struct proc *p = l->l_proc; 437 struct sigaltstack *sas = &p->p_sigctx.ps_sigstk; 438 sigset_t mask; 439 ssize_t ss_gap; 440 441 /* Restore register context. */ 442 tf = l->l_md.md_regs; 443 444 /* 445 * Check for security violations. If we're returning to 446 * protected mode, the CPU will validate the segment registers 447 * automatically and generate a trap on violations. We handle 448 * the trap, rather than doing all of the checking here. 449 */ 450 if (((scp->sc_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0 || 451 !USERMODE(scp->sc_cs, scp->sc_eflags)) 452 return EINVAL; 453 454 if (scp->sc_fs != 0 && !VALID_USER_DSEL32(scp->sc_fs)) 455 return EINVAL; 456 457 if (scp->sc_gs != 0 && !VALID_USER_DSEL32(scp->sc_gs)) 458 return EINVAL; 459 460 if (scp->sc_es != 0 && !VALID_USER_DSEL32(scp->sc_es)) 461 return EINVAL; 462 463 if (!VALID_USER_DSEL32(scp->sc_ds) || 464 !VALID_USER_DSEL32(scp->sc_ss)) 465 return EINVAL; 466 467 if (scp->sc_eip >= VM_MAXUSER_ADDRESS32) 468 return EINVAL; 469 470 tf->tf_gs = (register_t)scp->sc_gs & 0xffffffff; 471 tf->tf_fs = (register_t)scp->sc_fs & 0xffffffff; 472 tf->tf_es = (register_t)scp->sc_es & 0xffffffff; 473 tf->tf_ds = (register_t)scp->sc_ds & 0xffffffff; 474 tf->tf_rflags &= ~PSL_USER; 475 tf->tf_rflags |= ((register_t)scp->sc_eflags & PSL_USER); 476 tf->tf_rdi = (register_t)scp->sc_edi & 0xffffffff; 477 tf->tf_rsi = (register_t)scp->sc_esi & 0xffffffff; 478 tf->tf_rbp = (register_t)scp->sc_ebp & 0xffffffff; 479 tf->tf_rbx = (register_t)scp->sc_ebx & 0xffffffff; 480 tf->tf_rdx = (register_t)scp->sc_edx & 0xffffffff; 481 tf->tf_rcx = (register_t)scp->sc_ecx & 0xffffffff; 482 tf->tf_rax = (register_t)scp->sc_eax & 0xffffffff; 483 tf->tf_rip = (register_t)scp->sc_eip & 0xffffffff; 484 tf->tf_cs = (register_t)scp->sc_cs & 0xffffffff; 485 tf->tf_rsp = (register_t)scp->sc_esp_at_signal & 0xffffffff; 486 tf->tf_ss = (register_t)scp->sc_ss & 0xffffffff; 487 488 /* Restore signal stack. */ 489 ss_gap = (ssize_t) 490 ((caddr_t)NETBSD32PTR64(scp->sc_esp_at_signal) 491 - (caddr_t)sas->ss_sp); 492 if (ss_gap >= 0 && ss_gap < sas->ss_size) 493 sas->ss_flags |= SS_ONSTACK; 494 else 495 sas->ss_flags &= ~SS_ONSTACK; 496 497 /* Restore signal mask. */ 498 linux32_old_to_native_sigset(&mask, &scp->sc_mask); 499 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0); 500 #ifdef DEBUG_LINUX 501 printf("linux32_sigreturn: rip = 0x%lx, rsp = 0x%lx, flags = 0x%lx\n", 502 tf->tf_rip, tf->tf_rsp, tf->tf_rflags); 503 #endif 504 return EJUSTRETURN; 505 } 506