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