1 /*- 2 * Copyright (c) 2004 Tim J. Robbins 3 * Copyright (c) 2002 Doug Rabson 4 * Copyright (c) 2000 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/systm.h> 37 #include <sys/file.h> 38 #include <sys/fcntl.h> 39 #include <sys/clock.h> 40 #include <sys/imgact.h> 41 #include <sys/limits.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mman.h> 45 #include <sys/mutex.h> 46 #include <sys/priv.h> 47 #include <sys/proc.h> 48 #include <sys/resource.h> 49 #include <sys/resourcevar.h> 50 #include <sys/sched.h> 51 #include <sys/syscallsubr.h> 52 #include <sys/sysproto.h> 53 #include <sys/unistd.h> 54 55 #include <machine/frame.h> 56 #include <machine/pcb.h> 57 #include <machine/psl.h> 58 #include <machine/segments.h> 59 #include <machine/specialreg.h> 60 61 #include <vm/vm.h> 62 #include <vm/pmap.h> 63 #include <vm/vm_extern.h> 64 #include <vm/vm_kern.h> 65 #include <vm/vm_map.h> 66 67 #include <amd64/linux32/linux.h> 68 #include <amd64/linux32/linux32_proto.h> 69 #include <compat/linux/linux_ipc.h> 70 #include <compat/linux/linux_signal.h> 71 #include <compat/linux/linux_util.h> 72 #include <compat/linux/linux_emul.h> 73 74 struct l_old_select_argv { 75 l_int nfds; 76 l_uintptr_t readfds; 77 l_uintptr_t writefds; 78 l_uintptr_t exceptfds; 79 l_uintptr_t timeout; 80 } __packed; 81 82 int 83 linux_to_bsd_sigaltstack(int lsa) 84 { 85 int bsa = 0; 86 87 if (lsa & LINUX_SS_DISABLE) 88 bsa |= SS_DISABLE; 89 if (lsa & LINUX_SS_ONSTACK) 90 bsa |= SS_ONSTACK; 91 return (bsa); 92 } 93 94 static int linux_mmap_common(struct thread *td, l_uintptr_t addr, 95 l_size_t len, l_int prot, l_int flags, l_int fd, 96 l_loff_t pos); 97 98 int 99 bsd_to_linux_sigaltstack(int bsa) 100 { 101 int lsa = 0; 102 103 if (bsa & SS_DISABLE) 104 lsa |= LINUX_SS_DISABLE; 105 if (bsa & SS_ONSTACK) 106 lsa |= LINUX_SS_ONSTACK; 107 return (lsa); 108 } 109 110 /* 111 * Custom version of exec_copyin_args() so that we can translate 112 * the pointers. 113 */ 114 static int 115 linux_exec_copyin_args(struct image_args *args, char *fname, 116 enum uio_seg segflg, char **argv, char **envv) 117 { 118 char *argp, *envp; 119 u_int32_t *p32, arg; 120 size_t length; 121 int error; 122 123 bzero(args, sizeof(*args)); 124 if (argv == NULL) 125 return (EFAULT); 126 127 /* 128 * Allocate temporary demand zeroed space for argument and 129 * environment strings 130 */ 131 args->buf = (char *)kmem_alloc_wait(exec_map, 132 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 133 if (args->buf == NULL) 134 return (ENOMEM); 135 args->begin_argv = args->buf; 136 args->endp = args->begin_argv; 137 args->stringspace = ARG_MAX; 138 139 args->fname = args->buf + ARG_MAX; 140 141 /* 142 * Copy the file name. 143 */ 144 error = (segflg == UIO_SYSSPACE) ? 145 copystr(fname, args->fname, PATH_MAX, &length) : 146 copyinstr(fname, args->fname, PATH_MAX, &length); 147 if (error != 0) 148 goto err_exit; 149 150 /* 151 * extract arguments first 152 */ 153 p32 = (u_int32_t *)argv; 154 for (;;) { 155 error = copyin(p32++, &arg, sizeof(arg)); 156 if (error) 157 goto err_exit; 158 if (arg == 0) 159 break; 160 argp = PTRIN(arg); 161 error = copyinstr(argp, args->endp, args->stringspace, &length); 162 if (error) { 163 if (error == ENAMETOOLONG) 164 error = E2BIG; 165 166 goto err_exit; 167 } 168 args->stringspace -= length; 169 args->endp += length; 170 args->argc++; 171 } 172 173 args->begin_envv = args->endp; 174 175 /* 176 * extract environment strings 177 */ 178 if (envv) { 179 p32 = (u_int32_t *)envv; 180 for (;;) { 181 error = copyin(p32++, &arg, sizeof(arg)); 182 if (error) 183 goto err_exit; 184 if (arg == 0) 185 break; 186 envp = PTRIN(arg); 187 error = copyinstr(envp, args->endp, args->stringspace, 188 &length); 189 if (error) { 190 if (error == ENAMETOOLONG) 191 error = E2BIG; 192 goto err_exit; 193 } 194 args->stringspace -= length; 195 args->endp += length; 196 args->envc++; 197 } 198 } 199 200 return (0); 201 202 err_exit: 203 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf, 204 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 205 args->buf = NULL; 206 return (error); 207 } 208 209 int 210 linux_execve(struct thread *td, struct linux_execve_args *args) 211 { 212 struct image_args eargs; 213 char *path; 214 int error; 215 216 LCONVPATHEXIST(td, args->path, &path); 217 218 #ifdef DEBUG 219 if (ldebug(execve)) 220 printf(ARGS(execve, "%s"), path); 221 #endif 222 223 error = linux_exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp, 224 args->envp); 225 free(path, M_TEMP); 226 if (error == 0) 227 error = kern_execve(td, &eargs, NULL); 228 if (error == 0) 229 /* Linux process can execute FreeBSD one, do not attempt 230 * to create emuldata for such process using 231 * linux_proc_init, this leads to a panic on KASSERT 232 * because such process has p->p_emuldata == NULL. 233 */ 234 if (td->td_proc->p_sysent == &elf_linux_sysvec) 235 error = linux_proc_init(td, 0, 0); 236 return (error); 237 } 238 239 CTASSERT(sizeof(struct l_iovec32) == 8); 240 241 static int 242 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop) 243 { 244 struct l_iovec32 iov32; 245 struct iovec *iov; 246 struct uio *uio; 247 uint32_t iovlen; 248 int error, i; 249 250 *uiop = NULL; 251 if (iovcnt > UIO_MAXIOV) 252 return (EINVAL); 253 iovlen = iovcnt * sizeof(struct iovec); 254 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK); 255 iov = (struct iovec *)(uio + 1); 256 for (i = 0; i < iovcnt; i++) { 257 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32)); 258 if (error) { 259 free(uio, M_IOV); 260 return (error); 261 } 262 iov[i].iov_base = PTRIN(iov32.iov_base); 263 iov[i].iov_len = iov32.iov_len; 264 } 265 uio->uio_iov = iov; 266 uio->uio_iovcnt = iovcnt; 267 uio->uio_segflg = UIO_USERSPACE; 268 uio->uio_offset = -1; 269 uio->uio_resid = 0; 270 for (i = 0; i < iovcnt; i++) { 271 if (iov->iov_len > INT_MAX - uio->uio_resid) { 272 free(uio, M_IOV); 273 return (EINVAL); 274 } 275 uio->uio_resid += iov->iov_len; 276 iov++; 277 } 278 *uiop = uio; 279 return (0); 280 } 281 282 int 283 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp, 284 int error) 285 { 286 struct l_iovec32 iov32; 287 struct iovec *iov; 288 uint32_t iovlen; 289 int i; 290 291 *iovp = NULL; 292 if (iovcnt > UIO_MAXIOV) 293 return (error); 294 iovlen = iovcnt * sizeof(struct iovec); 295 iov = malloc(iovlen, M_IOV, M_WAITOK); 296 for (i = 0; i < iovcnt; i++) { 297 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32)); 298 if (error) { 299 free(iov, M_IOV); 300 return (error); 301 } 302 iov[i].iov_base = PTRIN(iov32.iov_base); 303 iov[i].iov_len = iov32.iov_len; 304 } 305 *iovp = iov; 306 return(0); 307 308 } 309 310 int 311 linux_readv(struct thread *td, struct linux_readv_args *uap) 312 { 313 struct uio *auio; 314 int error; 315 316 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio); 317 if (error) 318 return (error); 319 error = kern_readv(td, uap->fd, auio); 320 free(auio, M_IOV); 321 return (error); 322 } 323 324 int 325 linux_writev(struct thread *td, struct linux_writev_args *uap) 326 { 327 struct uio *auio; 328 int error; 329 330 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio); 331 if (error) 332 return (error); 333 error = kern_writev(td, uap->fd, auio); 334 free(auio, M_IOV); 335 return (error); 336 } 337 338 struct l_ipc_kludge { 339 l_uintptr_t msgp; 340 l_long msgtyp; 341 } __packed; 342 343 int 344 linux_ipc(struct thread *td, struct linux_ipc_args *args) 345 { 346 347 switch (args->what & 0xFFFF) { 348 case LINUX_SEMOP: { 349 struct linux_semop_args a; 350 351 a.semid = args->arg1; 352 a.tsops = args->ptr; 353 a.nsops = args->arg2; 354 return (linux_semop(td, &a)); 355 } 356 case LINUX_SEMGET: { 357 struct linux_semget_args a; 358 359 a.key = args->arg1; 360 a.nsems = args->arg2; 361 a.semflg = args->arg3; 362 return (linux_semget(td, &a)); 363 } 364 case LINUX_SEMCTL: { 365 struct linux_semctl_args a; 366 int error; 367 368 a.semid = args->arg1; 369 a.semnum = args->arg2; 370 a.cmd = args->arg3; 371 error = copyin(args->ptr, &a.arg, sizeof(a.arg)); 372 if (error) 373 return (error); 374 return (linux_semctl(td, &a)); 375 } 376 case LINUX_MSGSND: { 377 struct linux_msgsnd_args a; 378 379 a.msqid = args->arg1; 380 a.msgp = args->ptr; 381 a.msgsz = args->arg2; 382 a.msgflg = args->arg3; 383 return (linux_msgsnd(td, &a)); 384 } 385 case LINUX_MSGRCV: { 386 struct linux_msgrcv_args a; 387 388 a.msqid = args->arg1; 389 a.msgsz = args->arg2; 390 a.msgflg = args->arg3; 391 if ((args->what >> 16) == 0) { 392 struct l_ipc_kludge tmp; 393 int error; 394 395 if (args->ptr == 0) 396 return (EINVAL); 397 error = copyin(args->ptr, &tmp, sizeof(tmp)); 398 if (error) 399 return (error); 400 a.msgp = PTRIN(tmp.msgp); 401 a.msgtyp = tmp.msgtyp; 402 } else { 403 a.msgp = args->ptr; 404 a.msgtyp = args->arg5; 405 } 406 return (linux_msgrcv(td, &a)); 407 } 408 case LINUX_MSGGET: { 409 struct linux_msgget_args a; 410 411 a.key = args->arg1; 412 a.msgflg = args->arg2; 413 return (linux_msgget(td, &a)); 414 } 415 case LINUX_MSGCTL: { 416 struct linux_msgctl_args a; 417 418 a.msqid = args->arg1; 419 a.cmd = args->arg2; 420 a.buf = args->ptr; 421 return (linux_msgctl(td, &a)); 422 } 423 case LINUX_SHMAT: { 424 struct linux_shmat_args a; 425 426 a.shmid = args->arg1; 427 a.shmaddr = args->ptr; 428 a.shmflg = args->arg2; 429 a.raddr = PTRIN((l_uint)args->arg3); 430 return (linux_shmat(td, &a)); 431 } 432 case LINUX_SHMDT: { 433 struct linux_shmdt_args a; 434 435 a.shmaddr = args->ptr; 436 return (linux_shmdt(td, &a)); 437 } 438 case LINUX_SHMGET: { 439 struct linux_shmget_args a; 440 441 a.key = args->arg1; 442 a.size = args->arg2; 443 a.shmflg = args->arg3; 444 return (linux_shmget(td, &a)); 445 } 446 case LINUX_SHMCTL: { 447 struct linux_shmctl_args a; 448 449 a.shmid = args->arg1; 450 a.cmd = args->arg2; 451 a.buf = args->ptr; 452 return (linux_shmctl(td, &a)); 453 } 454 default: 455 break; 456 } 457 458 return (EINVAL); 459 } 460 461 int 462 linux_old_select(struct thread *td, struct linux_old_select_args *args) 463 { 464 struct l_old_select_argv linux_args; 465 struct linux_select_args newsel; 466 int error; 467 468 #ifdef DEBUG 469 if (ldebug(old_select)) 470 printf(ARGS(old_select, "%p"), args->ptr); 471 #endif 472 473 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 474 if (error) 475 return (error); 476 477 newsel.nfds = linux_args.nfds; 478 newsel.readfds = PTRIN(linux_args.readfds); 479 newsel.writefds = PTRIN(linux_args.writefds); 480 newsel.exceptfds = PTRIN(linux_args.exceptfds); 481 newsel.timeout = PTRIN(linux_args.timeout); 482 return (linux_select(td, &newsel)); 483 } 484 485 int 486 linux_fork(struct thread *td, struct linux_fork_args *args) 487 { 488 int error; 489 struct proc *p2; 490 struct thread *td2; 491 492 #ifdef DEBUG 493 if (ldebug(fork)) 494 printf(ARGS(fork, "")); 495 #endif 496 497 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0) 498 return (error); 499 500 if (error == 0) { 501 td->td_retval[0] = p2->p_pid; 502 td->td_retval[1] = 0; 503 } 504 505 if (td->td_retval[1] == 1) 506 td->td_retval[0] = 0; 507 error = linux_proc_init(td, td->td_retval[0], 0); 508 if (error) 509 return (error); 510 511 td2 = FIRST_THREAD_IN_PROC(p2); 512 513 /* 514 * Make this runnable after we are finished with it. 515 */ 516 thread_lock(td2); 517 TD_SET_CAN_RUN(td2); 518 sched_add(td2, SRQ_BORING); 519 thread_unlock(td2); 520 521 return (0); 522 } 523 524 int 525 linux_vfork(struct thread *td, struct linux_vfork_args *args) 526 { 527 int error; 528 struct proc *p2; 529 struct thread *td2; 530 531 #ifdef DEBUG 532 if (ldebug(vfork)) 533 printf(ARGS(vfork, "")); 534 #endif 535 536 /* Exclude RFPPWAIT */ 537 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0) 538 return (error); 539 if (error == 0) { 540 td->td_retval[0] = p2->p_pid; 541 td->td_retval[1] = 0; 542 } 543 /* Are we the child? */ 544 if (td->td_retval[1] == 1) 545 td->td_retval[0] = 0; 546 error = linux_proc_init(td, td->td_retval[0], 0); 547 if (error) 548 return (error); 549 550 PROC_LOCK(p2); 551 p2->p_flag |= P_PPWAIT; 552 PROC_UNLOCK(p2); 553 554 td2 = FIRST_THREAD_IN_PROC(p2); 555 556 /* 557 * Make this runnable after we are finished with it. 558 */ 559 thread_lock(td2); 560 TD_SET_CAN_RUN(td2); 561 sched_add(td2, SRQ_BORING); 562 thread_unlock(td2); 563 564 /* wait for the children to exit, ie. emulate vfork */ 565 PROC_LOCK(p2); 566 while (p2->p_flag & P_PPWAIT) 567 cv_wait(&p2->p_pwait, &p2->p_mtx); 568 PROC_UNLOCK(p2); 569 570 return (0); 571 } 572 573 int 574 linux_clone(struct thread *td, struct linux_clone_args *args) 575 { 576 int error, ff = RFPROC | RFSTOPPED; 577 struct proc *p2; 578 struct thread *td2; 579 int exit_signal; 580 struct linux_emuldata *em; 581 582 #ifdef DEBUG 583 if (ldebug(clone)) { 584 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, " 585 "child tid: %p"), (unsigned)args->flags, 586 args->stack, args->parent_tidptr, args->child_tidptr); 587 } 588 #endif 589 590 exit_signal = args->flags & 0x000000ff; 591 if (LINUX_SIG_VALID(exit_signal)) { 592 if (exit_signal <= LINUX_SIGTBLSZ) 593 exit_signal = 594 linux_to_bsd_signal[_SIG_IDX(exit_signal)]; 595 } else if (exit_signal != 0) 596 return (EINVAL); 597 598 if (args->flags & LINUX_CLONE_VM) 599 ff |= RFMEM; 600 if (args->flags & LINUX_CLONE_SIGHAND) 601 ff |= RFSIGSHARE; 602 /* 603 * XXX: In Linux, sharing of fs info (chroot/cwd/umask) 604 * and open files is independant. In FreeBSD, its in one 605 * structure but in reality it does not cause any problems 606 * because both of these flags are usually set together. 607 */ 608 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS))) 609 ff |= RFFDG; 610 611 /* 612 * Attempt to detect when linux_clone(2) is used for creating 613 * kernel threads. Unfortunately despite the existence of the 614 * CLONE_THREAD flag, version of linuxthreads package used in 615 * most popular distros as of beginning of 2005 doesn't make 616 * any use of it. Therefore, this detection relies on 617 * empirical observation that linuxthreads sets certain 618 * combination of flags, so that we can make more or less 619 * precise detection and notify the FreeBSD kernel that several 620 * processes are in fact part of the same threading group, so 621 * that special treatment is necessary for signal delivery 622 * between those processes and fd locking. 623 */ 624 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS) 625 ff |= RFTHREAD; 626 627 if (args->flags & LINUX_CLONE_PARENT_SETTID) 628 if (args->parent_tidptr == NULL) 629 return (EINVAL); 630 631 error = fork1(td, ff, 0, &p2); 632 if (error) 633 return (error); 634 635 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) { 636 sx_xlock(&proctree_lock); 637 PROC_LOCK(p2); 638 proc_reparent(p2, td->td_proc->p_pptr); 639 PROC_UNLOCK(p2); 640 sx_xunlock(&proctree_lock); 641 } 642 643 /* create the emuldata */ 644 error = linux_proc_init(td, p2->p_pid, args->flags); 645 /* reference it - no need to check this */ 646 em = em_find(p2, EMUL_DOLOCK); 647 KASSERT(em != NULL, ("clone: emuldata not found.\n")); 648 /* and adjust it */ 649 650 if (args->flags & LINUX_CLONE_THREAD) { 651 #ifdef notyet 652 PROC_LOCK(p2); 653 p2->p_pgrp = td->td_proc->p_pgrp; 654 PROC_UNLOCK(p2); 655 #endif 656 exit_signal = 0; 657 } 658 659 if (args->flags & LINUX_CLONE_CHILD_SETTID) 660 em->child_set_tid = args->child_tidptr; 661 else 662 em->child_set_tid = NULL; 663 664 if (args->flags & LINUX_CLONE_CHILD_CLEARTID) 665 em->child_clear_tid = args->child_tidptr; 666 else 667 em->child_clear_tid = NULL; 668 669 EMUL_UNLOCK(&emul_lock); 670 671 if (args->flags & LINUX_CLONE_PARENT_SETTID) { 672 error = copyout(&p2->p_pid, args->parent_tidptr, 673 sizeof(p2->p_pid)); 674 if (error) 675 printf(LMSG("copyout failed!")); 676 } 677 678 PROC_LOCK(p2); 679 p2->p_sigparent = exit_signal; 680 PROC_UNLOCK(p2); 681 td2 = FIRST_THREAD_IN_PROC(p2); 682 /* 683 * In a case of stack = NULL, we are supposed to COW calling process 684 * stack. This is what normal fork() does, so we just keep tf_rsp arg 685 * intact. 686 */ 687 if (args->stack) 688 td2->td_frame->tf_rsp = PTROUT(args->stack); 689 690 if (args->flags & LINUX_CLONE_SETTLS) { 691 struct user_segment_descriptor sd; 692 struct l_user_desc info; 693 int a[2]; 694 695 error = copyin((void *)td->td_frame->tf_rsi, &info, 696 sizeof(struct l_user_desc)); 697 if (error) { 698 printf(LMSG("copyin failed!")); 699 } else { 700 /* We might copy out the entry_number as GUGS32_SEL. */ 701 info.entry_number = GUGS32_SEL; 702 error = copyout(&info, (void *)td->td_frame->tf_rsi, 703 sizeof(struct l_user_desc)); 704 if (error) 705 printf(LMSG("copyout failed!")); 706 707 a[0] = LINUX_LDT_entry_a(&info); 708 a[1] = LINUX_LDT_entry_b(&info); 709 710 memcpy(&sd, &a, sizeof(a)); 711 #ifdef DEBUG 712 if (ldebug(clone)) 713 printf("Segment created in clone with " 714 "CLONE_SETTLS: lobase: %x, hibase: %x, " 715 "lolimit: %x, hilimit: %x, type: %i, " 716 "dpl: %i, p: %i, xx: %i, long: %i, " 717 "def32: %i, gran: %i\n", sd.sd_lobase, 718 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit, 719 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx, 720 sd.sd_long, sd.sd_def32, sd.sd_gran); 721 #endif 722 td2->td_pcb->pcb_gsbase = (register_t)info.base_addr; 723 /* XXXKIB td2->td_pcb->pcb_gs32sd = sd; */ 724 td2->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL); 725 td2->td_pcb->pcb_flags |= PCB_GS32BIT | PCB_32BIT; 726 } 727 } 728 729 #ifdef DEBUG 730 if (ldebug(clone)) 731 printf(LMSG("clone: successful rfork to %d, " 732 "stack %p sig = %d"), (int)p2->p_pid, args->stack, 733 exit_signal); 734 #endif 735 if (args->flags & LINUX_CLONE_VFORK) { 736 PROC_LOCK(p2); 737 p2->p_flag |= P_PPWAIT; 738 PROC_UNLOCK(p2); 739 } 740 741 /* 742 * Make this runnable after we are finished with it. 743 */ 744 thread_lock(td2); 745 TD_SET_CAN_RUN(td2); 746 sched_add(td2, SRQ_BORING); 747 thread_unlock(td2); 748 749 td->td_retval[0] = p2->p_pid; 750 td->td_retval[1] = 0; 751 752 if (args->flags & LINUX_CLONE_VFORK) { 753 /* wait for the children to exit, ie. emulate vfork */ 754 PROC_LOCK(p2); 755 while (p2->p_flag & P_PPWAIT) 756 cv_wait(&p2->p_pwait, &p2->p_mtx); 757 PROC_UNLOCK(p2); 758 } 759 760 return (0); 761 } 762 763 #define STACK_SIZE (2 * 1024 * 1024) 764 #define GUARD_SIZE (4 * PAGE_SIZE) 765 766 int 767 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 768 { 769 770 #ifdef DEBUG 771 if (ldebug(mmap2)) 772 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"), 773 args->addr, args->len, args->prot, 774 args->flags, args->fd, args->pgoff); 775 #endif 776 777 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot, 778 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff * 779 PAGE_SIZE)); 780 } 781 782 int 783 linux_mmap(struct thread *td, struct linux_mmap_args *args) 784 { 785 int error; 786 struct l_mmap_argv linux_args; 787 788 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 789 if (error) 790 return (error); 791 792 #ifdef DEBUG 793 if (ldebug(mmap)) 794 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"), 795 linux_args.addr, linux_args.len, linux_args.prot, 796 linux_args.flags, linux_args.fd, linux_args.pgoff); 797 #endif 798 799 return (linux_mmap_common(td, linux_args.addr, linux_args.len, 800 linux_args.prot, linux_args.flags, linux_args.fd, 801 (uint32_t)linux_args.pgoff)); 802 } 803 804 static int 805 linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot, 806 l_int flags, l_int fd, l_loff_t pos) 807 { 808 struct proc *p = td->td_proc; 809 struct mmap_args /* { 810 caddr_t addr; 811 size_t len; 812 int prot; 813 int flags; 814 int fd; 815 long pad; 816 off_t pos; 817 } */ bsd_args; 818 int error; 819 struct file *fp; 820 821 error = 0; 822 bsd_args.flags = 0; 823 fp = NULL; 824 825 /* 826 * Linux mmap(2): 827 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE 828 */ 829 if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE))) 830 return (EINVAL); 831 832 if (flags & LINUX_MAP_SHARED) 833 bsd_args.flags |= MAP_SHARED; 834 if (flags & LINUX_MAP_PRIVATE) 835 bsd_args.flags |= MAP_PRIVATE; 836 if (flags & LINUX_MAP_FIXED) 837 bsd_args.flags |= MAP_FIXED; 838 if (flags & LINUX_MAP_ANON) { 839 /* Enforce pos to be on page boundary, then ignore. */ 840 if ((pos & PAGE_MASK) != 0) 841 return (EINVAL); 842 pos = 0; 843 bsd_args.flags |= MAP_ANON; 844 } else 845 bsd_args.flags |= MAP_NOSYNC; 846 if (flags & LINUX_MAP_GROWSDOWN) 847 bsd_args.flags |= MAP_STACK; 848 849 /* 850 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC 851 * on Linux/i386. We do this to ensure maximum compatibility. 852 * Linux/ia64 does the same in i386 emulation mode. 853 */ 854 bsd_args.prot = prot; 855 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC)) 856 bsd_args.prot |= PROT_READ | PROT_EXEC; 857 858 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */ 859 bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd; 860 if (bsd_args.fd != -1) { 861 /* 862 * Linux follows Solaris mmap(2) description: 863 * The file descriptor fildes is opened with 864 * read permission, regardless of the 865 * protection options specified. 866 */ 867 868 if ((error = fget(td, bsd_args.fd, &fp)) != 0) 869 return (error); 870 if (fp->f_type != DTYPE_VNODE) { 871 fdrop(fp, td); 872 return (EINVAL); 873 } 874 875 /* Linux mmap() just fails for O_WRONLY files */ 876 if (!(fp->f_flag & FREAD)) { 877 fdrop(fp, td); 878 return (EACCES); 879 } 880 881 fdrop(fp, td); 882 } 883 884 if (flags & LINUX_MAP_GROWSDOWN) { 885 /* 886 * The Linux MAP_GROWSDOWN option does not limit auto 887 * growth of the region. Linux mmap with this option 888 * takes as addr the inital BOS, and as len, the initial 889 * region size. It can then grow down from addr without 890 * limit. However, Linux threads has an implicit internal 891 * limit to stack size of STACK_SIZE. Its just not 892 * enforced explicitly in Linux. But, here we impose 893 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack 894 * region, since we can do this with our mmap. 895 * 896 * Our mmap with MAP_STACK takes addr as the maximum 897 * downsize limit on BOS, and as len the max size of 898 * the region. It then maps the top SGROWSIZ bytes, 899 * and auto grows the region down, up to the limit 900 * in addr. 901 * 902 * If we don't use the MAP_STACK option, the effect 903 * of this code is to allocate a stack region of a 904 * fixed size of (STACK_SIZE - GUARD_SIZE). 905 */ 906 907 if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) { 908 /* 909 * Some Linux apps will attempt to mmap 910 * thread stacks near the top of their 911 * address space. If their TOS is greater 912 * than vm_maxsaddr, vm_map_growstack() 913 * will confuse the thread stack with the 914 * process stack and deliver a SEGV if they 915 * attempt to grow the thread stack past their 916 * current stacksize rlimit. To avoid this, 917 * adjust vm_maxsaddr upwards to reflect 918 * the current stacksize rlimit rather 919 * than the maximum possible stacksize. 920 * It would be better to adjust the 921 * mmap'ed region, but some apps do not check 922 * mmap's return value. 923 */ 924 PROC_LOCK(p); 925 p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK - 926 lim_cur(p, RLIMIT_STACK); 927 PROC_UNLOCK(p); 928 } 929 930 /* 931 * This gives us our maximum stack size and a new BOS. 932 * If we're using VM_STACK, then mmap will just map 933 * the top SGROWSIZ bytes, and let the stack grow down 934 * to the limit at BOS. If we're not using VM_STACK 935 * we map the full stack, since we don't have a way 936 * to autogrow it. 937 */ 938 if (len > STACK_SIZE - GUARD_SIZE) { 939 bsd_args.addr = (caddr_t)PTRIN(addr); 940 bsd_args.len = len; 941 } else { 942 bsd_args.addr = (caddr_t)PTRIN(addr) - 943 (STACK_SIZE - GUARD_SIZE - len); 944 bsd_args.len = STACK_SIZE - GUARD_SIZE; 945 } 946 } else { 947 bsd_args.addr = (caddr_t)PTRIN(addr); 948 bsd_args.len = len; 949 } 950 bsd_args.pos = pos; 951 952 #ifdef DEBUG 953 if (ldebug(mmap)) 954 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n", 955 __func__, 956 (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot, 957 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos); 958 #endif 959 error = mmap(td, &bsd_args); 960 #ifdef DEBUG 961 if (ldebug(mmap)) 962 printf("-> %s() return: 0x%x (0x%08x)\n", 963 __func__, error, (u_int)td->td_retval[0]); 964 #endif 965 return (error); 966 } 967 968 int 969 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 970 { 971 struct mprotect_args bsd_args; 972 973 bsd_args.addr = uap->addr; 974 bsd_args.len = uap->len; 975 bsd_args.prot = uap->prot; 976 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC)) 977 bsd_args.prot |= PROT_READ | PROT_EXEC; 978 return (mprotect(td, &bsd_args)); 979 } 980 981 int 982 linux_iopl(struct thread *td, struct linux_iopl_args *args) 983 { 984 int error; 985 986 if (args->level < 0 || args->level > 3) 987 return (EINVAL); 988 if ((error = priv_check(td, PRIV_IO)) != 0) 989 return (error); 990 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 991 return (error); 992 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 993 (args->level * (PSL_IOPL / 3)); 994 995 return (0); 996 } 997 998 int 999 linux_pipe(struct thread *td, struct linux_pipe_args *args) 1000 { 1001 int error; 1002 int fildes[2]; 1003 1004 #ifdef DEBUG 1005 if (ldebug(pipe)) 1006 printf(ARGS(pipe, "*")); 1007 #endif 1008 1009 error = kern_pipe(td, fildes); 1010 if (error) 1011 return (error); 1012 1013 /* XXX: Close descriptors on error. */ 1014 return (copyout(fildes, args->pipefds, sizeof fildes)); 1015 } 1016 1017 int 1018 linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 1019 { 1020 l_osigaction_t osa; 1021 l_sigaction_t act, oact; 1022 int error; 1023 1024 #ifdef DEBUG 1025 if (ldebug(sigaction)) 1026 printf(ARGS(sigaction, "%d, %p, %p"), 1027 args->sig, (void *)args->nsa, (void *)args->osa); 1028 #endif 1029 1030 if (args->nsa != NULL) { 1031 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 1032 if (error) 1033 return (error); 1034 act.lsa_handler = osa.lsa_handler; 1035 act.lsa_flags = osa.lsa_flags; 1036 act.lsa_restorer = osa.lsa_restorer; 1037 LINUX_SIGEMPTYSET(act.lsa_mask); 1038 act.lsa_mask.__bits[0] = osa.lsa_mask; 1039 } 1040 1041 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 1042 args->osa ? &oact : NULL); 1043 1044 if (args->osa != NULL && !error) { 1045 osa.lsa_handler = oact.lsa_handler; 1046 osa.lsa_flags = oact.lsa_flags; 1047 osa.lsa_restorer = oact.lsa_restorer; 1048 osa.lsa_mask = oact.lsa_mask.__bits[0]; 1049 error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 1050 } 1051 1052 return (error); 1053 } 1054 1055 /* 1056 * Linux has two extra args, restart and oldmask. We don't use these, 1057 * but it seems that "restart" is actually a context pointer that 1058 * enables the signal to happen with a different register set. 1059 */ 1060 int 1061 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 1062 { 1063 sigset_t sigmask; 1064 l_sigset_t mask; 1065 1066 #ifdef DEBUG 1067 if (ldebug(sigsuspend)) 1068 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask); 1069 #endif 1070 1071 LINUX_SIGEMPTYSET(mask); 1072 mask.__bits[0] = args->mask; 1073 linux_to_bsd_sigset(&mask, &sigmask); 1074 return (kern_sigsuspend(td, sigmask)); 1075 } 1076 1077 int 1078 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 1079 { 1080 l_sigset_t lmask; 1081 sigset_t sigmask; 1082 int error; 1083 1084 #ifdef DEBUG 1085 if (ldebug(rt_sigsuspend)) 1086 printf(ARGS(rt_sigsuspend, "%p, %d"), 1087 (void *)uap->newset, uap->sigsetsize); 1088 #endif 1089 1090 if (uap->sigsetsize != sizeof(l_sigset_t)) 1091 return (EINVAL); 1092 1093 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t)); 1094 if (error) 1095 return (error); 1096 1097 linux_to_bsd_sigset(&lmask, &sigmask); 1098 return (kern_sigsuspend(td, sigmask)); 1099 } 1100 1101 int 1102 linux_pause(struct thread *td, struct linux_pause_args *args) 1103 { 1104 struct proc *p = td->td_proc; 1105 sigset_t sigmask; 1106 1107 #ifdef DEBUG 1108 if (ldebug(pause)) 1109 printf(ARGS(pause, "")); 1110 #endif 1111 1112 PROC_LOCK(p); 1113 sigmask = td->td_sigmask; 1114 PROC_UNLOCK(p); 1115 return (kern_sigsuspend(td, sigmask)); 1116 } 1117 1118 int 1119 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 1120 { 1121 stack_t ss, oss; 1122 l_stack_t lss; 1123 int error; 1124 1125 #ifdef DEBUG 1126 if (ldebug(sigaltstack)) 1127 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss); 1128 #endif 1129 1130 if (uap->uss != NULL) { 1131 error = copyin(uap->uss, &lss, sizeof(l_stack_t)); 1132 if (error) 1133 return (error); 1134 1135 ss.ss_sp = PTRIN(lss.ss_sp); 1136 ss.ss_size = lss.ss_size; 1137 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 1138 } 1139 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 1140 (uap->uoss != NULL) ? &oss : NULL); 1141 if (!error && uap->uoss != NULL) { 1142 lss.ss_sp = PTROUT(oss.ss_sp); 1143 lss.ss_size = oss.ss_size; 1144 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 1145 error = copyout(&lss, uap->uoss, sizeof(l_stack_t)); 1146 } 1147 1148 return (error); 1149 } 1150 1151 int 1152 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args) 1153 { 1154 struct ftruncate_args sa; 1155 1156 #ifdef DEBUG 1157 if (ldebug(ftruncate64)) 1158 printf(ARGS(ftruncate64, "%u, %jd"), args->fd, 1159 (intmax_t)args->length); 1160 #endif 1161 1162 sa.fd = args->fd; 1163 sa.length = args->length; 1164 return ftruncate(td, &sa); 1165 } 1166 1167 int 1168 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap) 1169 { 1170 struct timeval atv; 1171 l_timeval atv32; 1172 struct timezone rtz; 1173 int error = 0; 1174 1175 if (uap->tp) { 1176 microtime(&atv); 1177 atv32.tv_sec = atv.tv_sec; 1178 atv32.tv_usec = atv.tv_usec; 1179 error = copyout(&atv32, uap->tp, sizeof(atv32)); 1180 } 1181 if (error == 0 && uap->tzp != NULL) { 1182 rtz.tz_minuteswest = tz_minuteswest; 1183 rtz.tz_dsttime = tz_dsttime; 1184 error = copyout(&rtz, uap->tzp, sizeof(rtz)); 1185 } 1186 return (error); 1187 } 1188 1189 int 1190 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap) 1191 { 1192 l_timeval atv32; 1193 struct timeval atv, *tvp; 1194 struct timezone atz, *tzp; 1195 int error; 1196 1197 if (uap->tp) { 1198 error = copyin(uap->tp, &atv32, sizeof(atv32)); 1199 if (error) 1200 return (error); 1201 atv.tv_sec = atv32.tv_sec; 1202 atv.tv_usec = atv32.tv_usec; 1203 tvp = &atv; 1204 } else 1205 tvp = NULL; 1206 if (uap->tzp) { 1207 error = copyin(uap->tzp, &atz, sizeof(atz)); 1208 if (error) 1209 return (error); 1210 tzp = &atz; 1211 } else 1212 tzp = NULL; 1213 return (kern_settimeofday(td, tvp, tzp)); 1214 } 1215 1216 int 1217 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap) 1218 { 1219 struct l_rusage s32; 1220 struct rusage s; 1221 int error; 1222 1223 error = kern_getrusage(td, uap->who, &s); 1224 if (error != 0) 1225 return (error); 1226 if (uap->rusage != NULL) { 1227 s32.ru_utime.tv_sec = s.ru_utime.tv_sec; 1228 s32.ru_utime.tv_usec = s.ru_utime.tv_usec; 1229 s32.ru_stime.tv_sec = s.ru_stime.tv_sec; 1230 s32.ru_stime.tv_usec = s.ru_stime.tv_usec; 1231 s32.ru_maxrss = s.ru_maxrss; 1232 s32.ru_ixrss = s.ru_ixrss; 1233 s32.ru_idrss = s.ru_idrss; 1234 s32.ru_isrss = s.ru_isrss; 1235 s32.ru_minflt = s.ru_minflt; 1236 s32.ru_majflt = s.ru_majflt; 1237 s32.ru_nswap = s.ru_nswap; 1238 s32.ru_inblock = s.ru_inblock; 1239 s32.ru_oublock = s.ru_oublock; 1240 s32.ru_msgsnd = s.ru_msgsnd; 1241 s32.ru_msgrcv = s.ru_msgrcv; 1242 s32.ru_nsignals = s.ru_nsignals; 1243 s32.ru_nvcsw = s.ru_nvcsw; 1244 s32.ru_nivcsw = s.ru_nivcsw; 1245 error = copyout(&s32, uap->rusage, sizeof(s32)); 1246 } 1247 return (error); 1248 } 1249 1250 int 1251 linux_sched_rr_get_interval(struct thread *td, 1252 struct linux_sched_rr_get_interval_args *uap) 1253 { 1254 struct timespec ts; 1255 struct l_timespec ts32; 1256 int error; 1257 1258 error = kern_sched_rr_get_interval(td, uap->pid, &ts); 1259 if (error != 0) 1260 return (error); 1261 ts32.tv_sec = ts.tv_sec; 1262 ts32.tv_nsec = ts.tv_nsec; 1263 return (copyout(&ts32, uap->interval, sizeof(ts32))); 1264 } 1265 1266 int 1267 linux_set_thread_area(struct thread *td, 1268 struct linux_set_thread_area_args *args) 1269 { 1270 struct l_user_desc info; 1271 struct user_segment_descriptor sd; 1272 int a[2]; 1273 int error; 1274 1275 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 1276 if (error) 1277 return (error); 1278 1279 #ifdef DEBUG 1280 if (ldebug(set_thread_area)) 1281 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, " 1282 "%i, %i, %i"), info.entry_number, info.base_addr, 1283 info.limit, info.seg_32bit, info.contents, 1284 info.read_exec_only, info.limit_in_pages, 1285 info.seg_not_present, info.useable); 1286 #endif 1287 1288 /* 1289 * Semantics of Linux version: every thread in the system has array 1290 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. 1291 * This syscall loads one of the selected TLS decriptors with a value 1292 * and also loads GDT descriptors 6, 7 and 8 with the content of 1293 * the per-thread descriptors. 1294 * 1295 * Semantics of FreeBSD version: I think we can ignore that Linux has 1296 * three per-thread descriptors and use just the first one. 1297 * The tls_array[] is used only in [gs]et_thread_area() syscalls and 1298 * for loading the GDT descriptors. We use just one GDT descriptor 1299 * for TLS, so we will load just one. 1300 * 1301 * XXX: This doesn't work when a user space process tries to use more 1302 * than one TLS segment. Comment in the Linux source says wine might 1303 * do this. 1304 */ 1305 1306 /* 1307 * GLIBC reads current %gs and call set_thread_area() with it. 1308 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because 1309 * we use these segments. 1310 */ 1311 switch (info.entry_number) { 1312 case GUGS32_SEL: 1313 case GUDATA_SEL: 1314 case 6: 1315 case -1: 1316 info.entry_number = GUGS32_SEL; 1317 break; 1318 default: 1319 return (EINVAL); 1320 } 1321 1322 /* 1323 * We have to copy out the GDT entry we use. 1324 * 1325 * XXX: What if a user space program does not check the return value 1326 * and tries to use 6, 7 or 8? 1327 */ 1328 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 1329 if (error) 1330 return (error); 1331 1332 if (LINUX_LDT_empty(&info)) { 1333 a[0] = 0; 1334 a[1] = 0; 1335 } else { 1336 a[0] = LINUX_LDT_entry_a(&info); 1337 a[1] = LINUX_LDT_entry_b(&info); 1338 } 1339 1340 memcpy(&sd, &a, sizeof(a)); 1341 #ifdef DEBUG 1342 if (ldebug(set_thread_area)) 1343 printf("Segment created in set_thread_area: " 1344 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, " 1345 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, " 1346 "def32: %i, gran: %i\n", 1347 sd.sd_lobase, 1348 sd.sd_hibase, 1349 sd.sd_lolimit, 1350 sd.sd_hilimit, 1351 sd.sd_type, 1352 sd.sd_dpl, 1353 sd.sd_p, 1354 sd.sd_xx, 1355 sd.sd_long, 1356 sd.sd_def32, 1357 sd.sd_gran); 1358 #endif 1359 1360 td->td_pcb->pcb_gsbase = (register_t)info.base_addr; 1361 td->td_pcb->pcb_flags |= PCB_32BIT | PCB_GS32BIT; 1362 update_gdt_gsbase(td, info.base_addr); 1363 1364 return (0); 1365 } 1366