1 /* $NetBSD: linux32_machdep.c,v 1.28 2011/02/07 03:54:45 chs 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.28 2011/02/07 03:54:45 chs Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/proc.h> 38 #include <sys/exec.h> 39 40 #include <machine/vmparam.h> 41 #include <machine/cpufunc.h> 42 #include <machine/netbsd32_machdep.h> 43 44 #include <compat/netbsd32/netbsd32.h> 45 #include <compat/netbsd32/netbsd32_syscallargs.h> 46 47 #include <compat/linux/common/linux_types.h> 48 #include <compat/linux/common/linux_emuldata.h> 49 #include <compat/linux/common/linux_signal.h> 50 #include <compat/linux/common/linux_errno.h> 51 #include <compat/linux/common/linux_exec.h> 52 #include <compat/linux/common/linux_ipc.h> 53 #include <compat/linux/common/linux_sem.h> 54 #include <compat/linux/linux_syscallargs.h> 55 56 #include <compat/linux32/common/linux32_types.h> 57 #include <compat/linux32/common/linux32_errno.h> 58 #include <compat/linux32/common/linux32_machdep.h> 59 #include <compat/linux32/common/linux32_signal.h> 60 #include <compat/linux32/common/linux32_exec.h> 61 #include <compat/linux32/common/linux32_ipc.h> 62 #include <compat/linux32/common/linux32_sem.h> 63 #include <compat/linux32/linux32_syscallargs.h> 64 65 #ifdef DEBUG_LINUX 66 #define DPRINTF(a) uprintf a 67 #else 68 #define DPRINTF(a) 69 #endif 70 71 extern char linux32_sigcode[]; 72 extern char linux32_rt_sigcode[]; 73 extern char linux32_esigcode[]; 74 75 extern void (osyscall_return)(void); 76 77 static void linux32_save_ucontext(struct lwp *, struct trapframe *, 78 const sigset_t *, struct sigaltstack *, struct linux32_ucontext *); 79 static void linux32_save_sigcontext(struct lwp *, struct trapframe *, 80 const sigset_t *, struct linux32_sigcontext *); 81 static void linux32_rt_sendsig(const ksiginfo_t *, const sigset_t *); 82 static void linux32_old_sendsig(const ksiginfo_t *, const sigset_t *); 83 static int linux32_restore_sigcontext(struct lwp *, 84 struct linux32_sigcontext *, register_t *); 85 86 void 87 linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 88 { 89 if (SIGACTION(curproc, ksi->ksi_signo).sa_flags & SA_SIGINFO) 90 linux32_rt_sendsig(ksi, mask); 91 else 92 linux32_old_sendsig(ksi, mask); 93 return; 94 } 95 96 void 97 linux32_old_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 98 { 99 struct lwp *l = curlwp; 100 struct proc *p = l->l_proc; 101 struct trapframe *tf; 102 struct linux32_sigframe *fp, frame; 103 int onstack, error; 104 int sig = ksi->ksi_signo; 105 sig_t catcher = SIGACTION(p, sig).sa_handler; 106 struct sigaltstack *sas = &l->l_sigstk; 107 108 tf = l->l_md.md_regs; 109 /* Do we need to jump onto the signal stack? */ 110 onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 111 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 112 113 114 /* Allocate space for the signal handler context. */ 115 if (onstack) 116 fp = (struct linux32_sigframe *)((char *)sas->ss_sp + 117 sas->ss_size); 118 else 119 fp = (struct linux32_sigframe *)tf->tf_rsp; 120 fp--; 121 122 DPRINTF(("old: onstack = %d, fp = %p sig = %d rip = 0x%lx\n", 123 onstack, fp, sig, tf->tf_rip)); 124 125 /* Build stack frame for signal trampoline. */ 126 NETBSD32PTR32(frame.sf_handler, catcher); 127 frame.sf_sig = native_to_linux32_signo[sig]; 128 129 linux32_save_sigcontext(l, tf, mask, &frame.sf_sc); 130 131 sendsig_reset(l, sig); 132 mutex_exit(p->p_lock); 133 error = copyout(&frame, fp, sizeof(frame)); 134 mutex_enter(p->p_lock); 135 136 if (error != 0) { 137 /* 138 * Process has trashed its stack; give it an illegal 139 * instruction to halt it in its tracks. 140 */ 141 sigexit(l, SIGILL); 142 /* NOTREACHED */ 143 } 144 145 /* 146 * Build context to run handler in. 147 */ 148 tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 149 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 150 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 151 tf->tf_rip = ((long)p->p_sigctx.ps_sigcode) & 0xffffffff; 152 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff; 153 tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff; 154 tf->tf_rsp = (long)fp & 0xffffffff; 155 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 156 157 /* Remember that we're now on the signal stack. */ 158 if (onstack) 159 sas->ss_flags |= SS_ONSTACK; 160 161 return; 162 } 163 164 void 165 linux32_rt_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 166 { 167 struct lwp *l = curlwp; 168 struct proc *p = l->l_proc; 169 struct trapframe *tf; 170 struct linux32_rt_sigframe *fp, frame; 171 int onstack, error; 172 linux32_siginfo_t *lsi; 173 int sig = ksi->ksi_signo; 174 sig_t catcher = SIGACTION(p, sig).sa_handler; 175 struct sigaltstack *sas = &l->l_sigstk; 176 177 tf = l->l_md.md_regs; 178 /* Do we need to jump onto the signal stack? */ 179 onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 180 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 181 182 183 /* Allocate space for the signal handler context. */ 184 if (onstack) 185 fp = (struct linux32_rt_sigframe *)((char *)sas->ss_sp + 186 sas->ss_size); 187 else 188 fp = (struct linux32_rt_sigframe *)tf->tf_rsp; 189 fp--; 190 191 /* Build stack frame for signal trampoline. */ 192 NETBSD32PTR32(frame.sf_handler, catcher); 193 frame.sf_sig = native_to_linux32_signo[sig]; 194 NETBSD32PTR32(frame.sf_sip, &fp->sf_si); 195 NETBSD32PTR32(frame.sf_ucp, &fp->sf_uc); 196 197 DPRINTF(("rt: onstack = %d, fp = %p sig = %d rip = 0x%lx\n", 198 onstack, fp, sig, tf->tf_rip)); 199 200 lsi = &frame.sf_si; 201 (void)memset(lsi, 0, sizeof(frame.sf_si)); 202 lsi->lsi_errno = native_to_linux32_errno[ksi->ksi_errno]; 203 lsi->lsi_code = native_to_linux_si_code(ksi->ksi_code); 204 lsi->lsi_signo = frame.sf_sig; 205 switch (lsi->lsi_signo) { 206 case LINUX32_SIGILL: 207 case LINUX32_SIGFPE: 208 case LINUX32_SIGSEGV: 209 case LINUX32_SIGBUS: 210 case LINUX32_SIGTRAP: 211 NETBSD32PTR32(lsi->lsi_addr, ksi->ksi_addr); 212 break; 213 case LINUX32_SIGCHLD: 214 lsi->lsi_uid = ksi->ksi_uid; 215 lsi->lsi_pid = ksi->ksi_pid; 216 lsi->lsi_utime = ksi->ksi_utime; 217 lsi->lsi_stime = ksi->ksi_stime; 218 lsi->lsi_status = native_to_linux_si_status(ksi->ksi_code, 219 ksi->ksi_status); 220 break; 221 case LINUX32_SIGIO: 222 lsi->lsi_band = ksi->ksi_band; 223 lsi->lsi_fd = ksi->ksi_fd; 224 break; 225 default: 226 lsi->lsi_uid = ksi->ksi_uid; 227 lsi->lsi_pid = ksi->ksi_pid; 228 if (lsi->lsi_signo == LINUX32_SIGALRM || 229 lsi->lsi_signo >= LINUX32_SIGRTMIN) 230 NETBSD32PTR32(lsi->lsi_value.sival_ptr, 231 ksi->ksi_value.sival_ptr); 232 break; 233 } 234 235 /* Save register context. */ 236 linux32_save_ucontext(l, tf, mask, sas, &frame.sf_uc); 237 sendsig_reset(l, sig); 238 mutex_exit(p->p_lock); 239 error = copyout(&frame, fp, sizeof(frame)); 240 mutex_enter(p->p_lock); 241 242 if (error != 0) { 243 /* 244 * Process has trashed its stack; give it an illegal 245 * instruction to halt it in its tracks. 246 */ 247 sigexit(l, SIGILL); 248 /* NOTREACHED */ 249 } 250 251 /* 252 * Build context to run handler in. 253 */ 254 tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 255 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 256 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 257 tf->tf_rip = (((long)p->p_sigctx.ps_sigcode) + 258 (linux32_rt_sigcode - linux32_sigcode)) & 0xffffffff; 259 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff; 260 tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff; 261 tf->tf_rsp = (long)fp & 0xffffffff; 262 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff; 263 264 /* Remember that we're now on the signal stack. */ 265 if (onstack) 266 sas->ss_flags |= SS_ONSTACK; 267 268 return; 269 } 270 271 void 272 linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack) 273 { 274 struct pcb *pcb = lwp_getpcb(l); 275 struct trapframe *tf; 276 struct proc *p = l->l_proc; 277 void **retaddr; 278 279 /* If we were using the FPU, forget about it. */ 280 if (pcb->pcb_fpcpu != NULL) 281 fpusave_lwp(l, 0); 282 283 #if defined(USER_LDT) && 0 284 pmap_ldt_cleanup(p); 285 #endif 286 287 netbsd32_adjust_limits(p); 288 289 l->l_md.md_flags &= ~MDP_USEDFPU; 290 pcb->pcb_flags = PCB_COMPAT32; 291 pcb->pcb_savefpu.fp_fxsave.fx_fcw = __Linux_NPXCW__; 292 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__; 293 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__; 294 295 p->p_flag |= PK_32; 296 297 tf = l->l_md.md_regs; 298 tf->tf_rax = 0; 299 tf->tf_rbx = (u_int64_t)p->p_psstr & 0xffffffff; 300 tf->tf_rcx = pack->ep_entry & 0xffffffff; 301 tf->tf_rdx = 0; 302 tf->tf_rsi = 0; 303 tf->tf_rdi = 0; 304 tf->tf_rbp = 0; 305 tf->tf_rsp = stack & 0xffffffff; 306 tf->tf_r8 = 0; 307 tf->tf_r9 = 0; 308 tf->tf_r10 = 0; 309 tf->tf_r11 = 0; 310 tf->tf_r12 = 0; 311 tf->tf_r13 = 0; 312 tf->tf_r14 = 0; 313 tf->tf_r15 = 0; 314 tf->tf_rip = pack->ep_entry & 0xffffffff; 315 tf->tf_rflags = PSL_USERSET; 316 tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL); 317 tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL); 318 tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL); 319 tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL); 320 cpu_fsgs_zero(l); 321 cpu_fsgs_reload(l, GSEL(GUDATA32_SEL, SEL_UPL), GSEL(GUDATA32_SEL, SEL_UPL)); 322 323 /* XXX frob return address to return via old iret method, not sysret */ 324 retaddr = (void **)tf - 1; 325 *retaddr = (void *)osyscall_return; 326 return; 327 } 328 329 static void 330 linux32_save_ucontext(struct lwp *l, struct trapframe *tf, 331 const sigset_t *mask, struct sigaltstack *sas, struct linux32_ucontext *uc) 332 { 333 334 uc->uc_flags = 0; 335 NETBSD32PTR32(uc->uc_link, NULL); 336 native_to_linux32_sigaltstack(&uc->uc_stack, sas); 337 linux32_save_sigcontext(l, tf, mask, &uc->uc_mcontext); 338 native_to_linux32_sigset(&uc->uc_sigmask, mask); 339 (void)memset(&uc->uc_fpregs_mem, 0, sizeof(uc->uc_fpregs_mem)); 340 } 341 342 static void 343 linux32_save_sigcontext(struct lwp *l, struct trapframe *tf, 344 const sigset_t *mask, struct linux32_sigcontext *sc) 345 { 346 struct pcb *pcb = lwp_getpcb(l); 347 348 /* Save register context. */ 349 sc->sc_gs = tf->tf_gs; 350 sc->sc_fs = tf->tf_fs; 351 sc->sc_es = tf->tf_es; 352 sc->sc_ds = tf->tf_ds; 353 sc->sc_eflags = tf->tf_rflags; 354 sc->sc_edi = tf->tf_rdi; 355 sc->sc_esi = tf->tf_rsi; 356 sc->sc_esp = tf->tf_rsp; 357 sc->sc_ebp = tf->tf_rbp; 358 sc->sc_ebx = tf->tf_rbx; 359 sc->sc_edx = tf->tf_rdx; 360 sc->sc_ecx = tf->tf_rcx; 361 sc->sc_eax = tf->tf_rax; 362 sc->sc_eip = tf->tf_rip; 363 sc->sc_cs = tf->tf_cs; 364 sc->sc_esp_at_signal = tf->tf_rsp; 365 sc->sc_ss = tf->tf_ss; 366 sc->sc_err = tf->tf_err; 367 sc->sc_trapno = tf->tf_trapno; 368 sc->sc_cr2 = pcb->pcb_cr2; 369 NETBSD32PTR32(sc->sc_387, NULL); 370 371 /* Save signal stack. */ 372 /* Linux doesn't save the onstack flag in sigframe */ 373 374 /* Save signal mask. */ 375 native_to_linux32_old_sigset(&sc->sc_mask, mask); 376 } 377 378 int 379 linux32_sys_sigreturn(struct lwp *l, 380 const struct linux32_sys_sigreturn_args *uap, register_t *retval) 381 { 382 /* { 383 syscallarg(linux32_sigcontextp_t) scp; 384 } */ 385 struct linux32_sigcontext ctx; 386 int error; 387 388 if ((error = copyin(SCARG_P32(uap, scp), &ctx, sizeof(ctx))) != 0) 389 return error; 390 391 return linux32_restore_sigcontext(l, &ctx, retval); 392 } 393 394 int 395 linux32_sys_rt_sigreturn(struct lwp *l, 396 const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval) 397 { 398 /* { 399 syscallarg(linux32_ucontextp_t) ucp; 400 } */ 401 struct linux32_ucontext ctx; 402 int error; 403 404 if ((error = copyin(SCARG_P32(uap, ucp), &ctx, sizeof(ctx))) != 0) 405 return error; 406 407 return linux32_restore_sigcontext(l, &ctx.uc_mcontext, retval); 408 } 409 410 static int 411 linux32_restore_sigcontext(struct lwp *l, struct linux32_sigcontext *scp, 412 register_t *retval) 413 { 414 struct trapframe *tf; 415 struct proc *p = l->l_proc; 416 struct sigaltstack *sas = &l->l_sigstk; 417 struct pcb *pcb; 418 sigset_t mask; 419 ssize_t ss_gap; 420 register_t fssel, gssel; 421 422 /* Restore register context. */ 423 tf = l->l_md.md_regs; 424 pcb = lwp_getpcb(l); 425 DPRINTF(("sigreturn enter rsp=0x%lx rip=0x%lx\n", tf->tf_rsp, 426 tf->tf_rip)); 427 428 /* 429 * Check for security violations. 430 */ 431 if (((scp->sc_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0 || 432 !USERMODE(scp->sc_cs, scp->sc_eflags)) 433 return EINVAL; 434 435 if (scp->sc_fs != 0 && !VALID_USER_DSEL32(scp->sc_fs) && 436 !(VALID_USER_FSEL32(scp->sc_fs) && pcb->pcb_fs != 0)) 437 return EINVAL; 438 439 if (scp->sc_gs != 0 && !VALID_USER_DSEL32(scp->sc_gs) && 440 !(VALID_USER_GSEL32(scp->sc_gs) && pcb->pcb_gs != 0)) 441 return EINVAL; 442 443 if (scp->sc_es != 0 && !VALID_USER_DSEL32(scp->sc_es)) 444 return EINVAL; 445 446 if (!VALID_USER_DSEL32(scp->sc_ds) || 447 !VALID_USER_DSEL32(scp->sc_ss)) 448 return EINVAL; 449 450 if (scp->sc_eip >= VM_MAXUSER_ADDRESS32) 451 return EINVAL; 452 453 gssel = (register_t)scp->sc_gs & 0xffff; 454 fssel = (register_t)scp->sc_fs & 0xffff; 455 cpu_fsgs_reload(l, fssel, gssel); 456 tf->tf_es = (register_t)scp->sc_es & 0xffff; 457 tf->tf_ds = (register_t)scp->sc_ds & 0xffff; 458 tf->tf_rflags &= ~PSL_USER; 459 tf->tf_rflags |= ((register_t)scp->sc_eflags & PSL_USER); 460 tf->tf_rdi = (register_t)scp->sc_edi & 0xffffffff; 461 tf->tf_rsi = (register_t)scp->sc_esi & 0xffffffff; 462 tf->tf_rbp = (register_t)scp->sc_ebp & 0xffffffff; 463 tf->tf_rbx = (register_t)scp->sc_ebx & 0xffffffff; 464 tf->tf_rdx = (register_t)scp->sc_edx & 0xffffffff; 465 tf->tf_rcx = (register_t)scp->sc_ecx & 0xffffffff; 466 tf->tf_rax = (register_t)scp->sc_eax & 0xffffffff; 467 tf->tf_rip = (register_t)scp->sc_eip & 0xffffffff; 468 tf->tf_cs = (register_t)scp->sc_cs & 0xffff; 469 tf->tf_rsp = (register_t)scp->sc_esp_at_signal & 0xffffffff; 470 tf->tf_ss = (register_t)scp->sc_ss & 0xffff; 471 472 mutex_enter(p->p_lock); 473 474 /* Restore signal stack. */ 475 ss_gap = (ssize_t) 476 ((char *)NETBSD32IPTR64(scp->sc_esp_at_signal) 477 - (char *)sas->ss_sp); 478 if (ss_gap >= 0 && ss_gap < sas->ss_size) 479 sas->ss_flags |= SS_ONSTACK; 480 else 481 sas->ss_flags &= ~SS_ONSTACK; 482 483 /* Restore signal mask. */ 484 linux32_old_to_native_sigset(&mask, &scp->sc_mask); 485 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0); 486 487 mutex_exit(p->p_lock); 488 489 DPRINTF(("linux32_sigreturn: rip = 0x%lx, rsp = 0x%lx, flags = 0x%lx\n", 490 tf->tf_rip, tf->tf_rsp, tf->tf_rflags)); 491 return EJUSTRETURN; 492 } 493 494 int 495 linux32_sys_set_thread_area(struct lwp *l, 496 const struct linux32_sys_set_thread_area_args *uap, register_t *retval) 497 { 498 /* { 499 syscallarg(linux32_user_descp_t) desc; 500 } */ 501 502 return linux_lwp_setprivate(l, SCARG_P32(uap, desc)); 503 } 504 505 int 506 linux32_sys_get_thread_area(struct lwp *l, 507 const struct linux32_sys_get_thread_area_args *uap, register_t *retval) 508 { 509 /* { 510 syscallarg(linux32_user_descp_t) desc; 511 } */ 512 513 /* glibc doesn't actually call this. */ 514 return ENOSYS; 515 } 516 517 int 518 linux32_sys_modify_ldt(struct lwp *l, const struct linux32_sys_modify_ldt_args *uap, register_t *retval) 519 { 520 /* { 521 syscallarg(int) func; 522 syscallarg(netbsd32_charp) ptr; 523 syscallarg(netbsd32_size_t) bytecount; 524 } */ 525 struct linux_sys_modify_ldt_args ua; 526 527 NETBSD32TO64_UAP(func); 528 NETBSD32TOP_UAP(ptr, void *); 529 NETBSD32TOX_UAP(bytecount, size_t); 530 return linux_sys_modify_ldt(l, &ua, retval); 531 } 532