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