linux_machdep.c revision 163372
1/*- 2 * Copyright (c) 2000 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 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. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/i386/linux/linux_machdep.c 163372 2006-10-15 13:25:23Z netchild $"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/file.h> 35#include <sys/fcntl.h> 36#include <sys/imgact.h> 37#include <sys/lock.h> 38#include <sys/malloc.h> 39#include <sys/mman.h> 40#include <sys/mutex.h> 41#include <sys/sx.h> 42#include <sys/proc.h> 43#include <sys/queue.h> 44#include <sys/resource.h> 45#include <sys/resourcevar.h> 46#include <sys/signalvar.h> 47#include <sys/syscallsubr.h> 48#include <sys/sysproto.h> 49#include <sys/unistd.h> 50#include <sys/wait.h> 51 52#include <machine/frame.h> 53#include <machine/psl.h> 54#include <machine/segments.h> 55#include <machine/sysarch.h> 56 57#include <vm/vm.h> 58#include <vm/pmap.h> 59#include <vm/vm_map.h> 60 61#include <i386/linux/linux.h> 62#include <i386/linux/linux_proto.h> 63#include <compat/linux/linux_ipc.h> 64#include <compat/linux/linux_signal.h> 65#include <compat/linux/linux_util.h> 66#include <compat/linux/linux_emul.h> 67 68#include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */ 69 70#include "opt_posix.h" 71 72extern struct sysentvec elf32_freebsd_sysvec; /* defined in i386/i386/elf_machdep.c */ 73 74struct l_descriptor { 75 l_uint entry_number; 76 l_ulong base_addr; 77 l_uint limit; 78 l_uint seg_32bit:1; 79 l_uint contents:2; 80 l_uint read_exec_only:1; 81 l_uint limit_in_pages:1; 82 l_uint seg_not_present:1; 83 l_uint useable:1; 84}; 85 86struct l_old_select_argv { 87 l_int nfds; 88 l_fd_set *readfds; 89 l_fd_set *writefds; 90 l_fd_set *exceptfds; 91 struct l_timeval *timeout; 92}; 93 94int 95linux_to_bsd_sigaltstack(int lsa) 96{ 97 int bsa = 0; 98 99 if (lsa & LINUX_SS_DISABLE) 100 bsa |= SS_DISABLE; 101 if (lsa & LINUX_SS_ONSTACK) 102 bsa |= SS_ONSTACK; 103 return (bsa); 104} 105 106int 107bsd_to_linux_sigaltstack(int bsa) 108{ 109 int lsa = 0; 110 111 if (bsa & SS_DISABLE) 112 lsa |= LINUX_SS_DISABLE; 113 if (bsa & SS_ONSTACK) 114 lsa |= LINUX_SS_ONSTACK; 115 return (lsa); 116} 117 118int 119linux_execve(struct thread *td, struct linux_execve_args *args) 120{ 121 int error; 122 char *newpath; 123 struct image_args eargs; 124 125 LCONVPATHEXIST(td, args->path, &newpath); 126 127#ifdef DEBUG 128 if (ldebug(execve)) 129 printf(ARGS(execve, "%s"), newpath); 130#endif 131 132 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE, 133 args->argp, args->envp); 134 free(newpath, M_TEMP); 135 if (error == 0) 136 error = kern_execve(td, &eargs, NULL); 137 if (error == 0) 138 /* linux process can exec fbsd one, dont attempt 139 * to create emuldata for such process using 140 * linux_proc_init, this leads to a panic on KASSERT 141 * because such process has p->p_emuldata == NULL 142 */ 143 if (td->td_proc->p_sysent == &elf_linux_sysvec) 144 error = linux_proc_init(td, 0, 0); 145 return (error); 146} 147 148struct l_ipc_kludge { 149 struct l_msgbuf *msgp; 150 l_long msgtyp; 151}; 152 153int 154linux_ipc(struct thread *td, struct linux_ipc_args *args) 155{ 156 157 switch (args->what & 0xFFFF) { 158 case LINUX_SEMOP: { 159 struct linux_semop_args a; 160 161 a.semid = args->arg1; 162 a.tsops = args->ptr; 163 a.nsops = args->arg2; 164 return (linux_semop(td, &a)); 165 } 166 case LINUX_SEMGET: { 167 struct linux_semget_args a; 168 169 a.key = args->arg1; 170 a.nsems = args->arg2; 171 a.semflg = args->arg3; 172 return (linux_semget(td, &a)); 173 } 174 case LINUX_SEMCTL: { 175 struct linux_semctl_args a; 176 int error; 177 178 a.semid = args->arg1; 179 a.semnum = args->arg2; 180 a.cmd = args->arg3; 181 error = copyin(args->ptr, &a.arg, sizeof(a.arg)); 182 if (error) 183 return (error); 184 return (linux_semctl(td, &a)); 185 } 186 case LINUX_MSGSND: { 187 struct linux_msgsnd_args a; 188 189 a.msqid = args->arg1; 190 a.msgp = args->ptr; 191 a.msgsz = args->arg2; 192 a.msgflg = args->arg3; 193 return (linux_msgsnd(td, &a)); 194 } 195 case LINUX_MSGRCV: { 196 struct linux_msgrcv_args a; 197 198 a.msqid = args->arg1; 199 a.msgsz = args->arg2; 200 a.msgflg = args->arg3; 201 if ((args->what >> 16) == 0) { 202 struct l_ipc_kludge tmp; 203 int error; 204 205 if (args->ptr == NULL) 206 return (EINVAL); 207 error = copyin(args->ptr, &tmp, sizeof(tmp)); 208 if (error) 209 return (error); 210 a.msgp = tmp.msgp; 211 a.msgtyp = tmp.msgtyp; 212 } else { 213 a.msgp = args->ptr; 214 a.msgtyp = args->arg5; 215 } 216 return (linux_msgrcv(td, &a)); 217 } 218 case LINUX_MSGGET: { 219 struct linux_msgget_args a; 220 221 a.key = args->arg1; 222 a.msgflg = args->arg2; 223 return (linux_msgget(td, &a)); 224 } 225 case LINUX_MSGCTL: { 226 struct linux_msgctl_args a; 227 228 a.msqid = args->arg1; 229 a.cmd = args->arg2; 230 a.buf = args->ptr; 231 return (linux_msgctl(td, &a)); 232 } 233 case LINUX_SHMAT: { 234 struct linux_shmat_args a; 235 236 a.shmid = args->arg1; 237 a.shmaddr = args->ptr; 238 a.shmflg = args->arg2; 239 a.raddr = (l_ulong *)args->arg3; 240 return (linux_shmat(td, &a)); 241 } 242 case LINUX_SHMDT: { 243 struct linux_shmdt_args a; 244 245 a.shmaddr = args->ptr; 246 return (linux_shmdt(td, &a)); 247 } 248 case LINUX_SHMGET: { 249 struct linux_shmget_args a; 250 251 a.key = args->arg1; 252 a.size = args->arg2; 253 a.shmflg = args->arg3; 254 return (linux_shmget(td, &a)); 255 } 256 case LINUX_SHMCTL: { 257 struct linux_shmctl_args a; 258 259 a.shmid = args->arg1; 260 a.cmd = args->arg2; 261 a.buf = args->ptr; 262 return (linux_shmctl(td, &a)); 263 } 264 default: 265 break; 266 } 267 268 return (EINVAL); 269} 270 271int 272linux_old_select(struct thread *td, struct linux_old_select_args *args) 273{ 274 struct l_old_select_argv linux_args; 275 struct linux_select_args newsel; 276 int error; 277 278#ifdef DEBUG 279 if (ldebug(old_select)) 280 printf(ARGS(old_select, "%p"), args->ptr); 281#endif 282 283 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 284 if (error) 285 return (error); 286 287 newsel.nfds = linux_args.nfds; 288 newsel.readfds = linux_args.readfds; 289 newsel.writefds = linux_args.writefds; 290 newsel.exceptfds = linux_args.exceptfds; 291 newsel.timeout = linux_args.timeout; 292 return (linux_select(td, &newsel)); 293} 294 295int 296linux_fork(struct thread *td, struct linux_fork_args *args) 297{ 298 int error; 299 300#ifdef DEBUG 301 if (ldebug(fork)) 302 printf(ARGS(fork, "")); 303#endif 304 305 if ((error = fork(td, (struct fork_args *)args)) != 0) 306 return (error); 307 308 if (td->td_retval[1] == 1) 309 td->td_retval[0] = 0; 310 error = linux_proc_init(td, td->td_retval[0], 0); 311 if (error) 312 return (error); 313 314 return (0); 315} 316 317int 318linux_vfork(struct thread *td, struct linux_vfork_args *args) 319{ 320 int error; 321 struct proc *p2; 322 323#ifdef DEBUG 324 if (ldebug(vfork)) 325 printf(ARGS(vfork, "")); 326#endif 327 328 /* exclude RFPPWAIT */ 329 if ((error = fork1(td, RFFDG | RFPROC | RFMEM, 0, &p2)) != 0) 330 return (error); 331 if (error == 0) { 332 td->td_retval[0] = p2->p_pid; 333 td->td_retval[1] = 0; 334 } 335 /* Are we the child? */ 336 if (td->td_retval[1] == 1) 337 td->td_retval[0] = 0; 338 error = linux_proc_init(td, td->td_retval[0], 0); 339 if (error) 340 return (error); 341 /* wait for the children to exit, ie. emulate vfork */ 342 PROC_LOCK(p2); 343 p2->p_flag |= P_PPWAIT; 344 while (p2->p_flag & P_PPWAIT) 345 msleep(td->td_proc, &p2->p_mtx, PWAIT, "ppwait", 0); 346 PROC_UNLOCK(p2); 347 348 return (0); 349} 350 351int 352linux_clone(struct thread *td, struct linux_clone_args *args) 353{ 354 int error, ff = RFPROC | RFSTOPPED; 355 struct proc *p2; 356 struct thread *td2; 357 int exit_signal; 358 struct linux_emuldata *em; 359 360#ifdef DEBUG 361 if (ldebug(clone)) { 362 printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"), 363 (unsigned int)args->flags, (unsigned int)args->stack, 364 (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr); 365 } 366#endif 367 368 exit_signal = args->flags & 0x000000ff; 369 if (!LINUX_SIG_VALID(exit_signal)) 370 return (EINVAL); 371 372 if (exit_signal <= LINUX_SIGTBLSZ) 373 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)]; 374 375 if (args->flags & CLONE_VM) 376 ff |= RFMEM; 377 if (args->flags & CLONE_SIGHAND) 378 ff |= RFSIGSHARE; 379 /* 380 * XXX: in linux sharing of fs info (chroot/cwd/umask) 381 * and open files is independant. in fbsd its in one 382 * structure but in reality it doesnt make any problems 383 * because both this flags are set at once usually. 384 */ 385 if (!(args->flags & (CLONE_FILES | CLONE_FS))) 386 ff |= RFFDG; 387 388 /* 389 * Attempt to detect when linux_clone(2) is used for creating 390 * kernel threads. Unfortunately despite the existence of the 391 * CLONE_THREAD flag, version of linuxthreads package used in 392 * most popular distros as of beginning of 2005 doesn't make 393 * any use of it. Therefore, this detection relay fully on 394 * empirical observation that linuxthreads sets certain 395 * combination of flags, so that we can make more or less 396 * precise detection and notify the FreeBSD kernel that several 397 * processes are in fact part of the same threading group, so 398 * that special treatment is necessary for signal delivery 399 * between those processes and fd locking. 400 */ 401 if ((args->flags & 0xffffff00) == THREADING_FLAGS) 402 ff |= RFTHREAD; 403 404 error = fork1(td, ff, 0, &p2); 405 if (error) 406 return (error); 407 408 /* create the emuldata */ 409 error = linux_proc_init(td, p2->p_pid, args->flags); 410 /* reference it - no need to check this */ 411 em = em_find(p2, EMUL_UNLOCKED); 412 KASSERT(em != NULL, ("clone: emuldata not found.\n")); 413 /* and adjust it */ 414 if (args->flags & CLONE_PARENT_SETTID) { 415 if (args->parent_tidptr == NULL) { 416 EMUL_UNLOCK(&emul_lock); 417 return (EINVAL); 418 } 419 error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid)); 420 if (error) { 421 EMUL_UNLOCK(&emul_lock); 422 return (error); 423 } 424 } 425 426 if (args->flags & (CLONE_PARENT|CLONE_THREAD)) { 427 sx_xlock(&proctree_lock); 428 PROC_LOCK(p2); 429 proc_reparent(p2, td->td_proc->p_pptr); 430 PROC_UNLOCK(p2); 431 sx_xunlock(&proctree_lock); 432 } 433 434 if (args->flags & CLONE_THREAD) { 435 /* XXX: linux mangles pgrp and pptr somehow 436 * I think it might be this but I am not sure. 437 */ 438#ifdef notyet 439 PROC_LOCK(p2); 440 p2->p_pgrp = td->td_proc->p_pgrp; 441 PROC_UNLOCK(p2); 442#endif 443 exit_signal = 0; 444 } 445 446 if (args->flags & CLONE_CHILD_SETTID) 447 em->child_set_tid = args->child_tidptr; 448 else 449 em->child_set_tid = NULL; 450 451 if (args->flags & CLONE_CHILD_CLEARTID) 452 em->child_clear_tid = args->child_tidptr; 453 else 454 em->child_clear_tid = NULL; 455 456 EMUL_UNLOCK(&emul_lock); 457 458 PROC_LOCK(p2); 459 p2->p_sigparent = exit_signal; 460 PROC_UNLOCK(p2); 461 td2 = FIRST_THREAD_IN_PROC(p2); 462 /* 463 * in a case of stack = NULL we are supposed to COW calling process stack 464 * this is what normal fork() does so we just keep the tf_esp arg intact 465 */ 466 if (args->stack) 467 td2->td_frame->tf_esp = (unsigned int)args->stack; 468 469 if (args->flags & CLONE_SETTLS) { 470 struct l_user_desc info; 471 int idx; 472 int a[2]; 473 struct segment_descriptor sd; 474 475 error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc)); 476 if (error) 477 return (error); 478 479 idx = info.entry_number; 480 481 /* 482 * looks like we're getting the idx we returned 483 * in the set_thread_area() syscall 484 */ 485 if (idx != 6 && idx != 3) 486 return (EINVAL); 487 488 /* this doesnt happen in practice */ 489 if (idx == 6) { 490 /* we might copy out the entry_number as 3 */ 491 info.entry_number = 3; 492 error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc)); 493 if (error) 494 return (error); 495 } 496 497 a[0] = LDT_entry_a(&info); 498 a[1] = LDT_entry_b(&info); 499 500 memcpy(&sd, &a, sizeof(a)); 501#ifdef DEBUG 502 if (ldebug(clone)) 503 printf("Segment created in clone with CLONE_SETTLS: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase, 504 sd.sd_hibase, 505 sd.sd_lolimit, 506 sd.sd_hilimit, 507 sd.sd_type, 508 sd.sd_dpl, 509 sd.sd_p, 510 sd.sd_xx, 511 sd.sd_def32, 512 sd.sd_gran); 513#endif 514 515 /* set %gs */ 516 td2->td_pcb->pcb_gsd = sd; 517 td2->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL); 518 } 519 520#ifdef DEBUG 521 if (ldebug(clone)) 522 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"), 523 (long)p2->p_pid, args->stack, exit_signal); 524#endif 525 526 /* 527 * Make this runnable after we are finished with it. 528 */ 529 mtx_lock_spin(&sched_lock); 530 TD_SET_CAN_RUN(td2); 531 setrunqueue(td2, SRQ_BORING); 532 mtx_unlock_spin(&sched_lock); 533 534 td->td_retval[0] = p2->p_pid; 535 td->td_retval[1] = 0; 536 return (0); 537} 538 539/* XXX move */ 540struct l_mmap_argv { 541 l_caddr_t addr; 542 l_int len; 543 l_int prot; 544 l_int flags; 545 l_int fd; 546 l_int pos; 547}; 548 549#define STACK_SIZE (2 * 1024 * 1024) 550#define GUARD_SIZE (4 * PAGE_SIZE) 551 552static int linux_mmap_common(struct thread *, struct l_mmap_argv *); 553 554int 555linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 556{ 557 struct l_mmap_argv linux_args; 558 559#ifdef DEBUG 560 if (ldebug(mmap2)) 561 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"), 562 (void *)args->addr, args->len, args->prot, 563 args->flags, args->fd, args->pgoff); 564#endif 565 566 linux_args.addr = (l_caddr_t)args->addr; 567 linux_args.len = args->len; 568 linux_args.prot = args->prot; 569 linux_args.flags = args->flags; 570 linux_args.fd = args->fd; 571 linux_args.pos = args->pgoff * PAGE_SIZE; 572 573 return (linux_mmap_common(td, &linux_args)); 574} 575 576int 577linux_mmap(struct thread *td, struct linux_mmap_args *args) 578{ 579 int error; 580 struct l_mmap_argv linux_args; 581 582 error = copyin(args->ptr, &linux_args, sizeof(linux_args)); 583 if (error) 584 return (error); 585 586#ifdef DEBUG 587 if (ldebug(mmap)) 588 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"), 589 (void *)linux_args.addr, linux_args.len, linux_args.prot, 590 linux_args.flags, linux_args.fd, linux_args.pos); 591#endif 592 593 return (linux_mmap_common(td, &linux_args)); 594} 595 596static int 597linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args) 598{ 599 struct proc *p = td->td_proc; 600 struct mmap_args /* { 601 caddr_t addr; 602 size_t len; 603 int prot; 604 int flags; 605 int fd; 606 long pad; 607 off_t pos; 608 } */ bsd_args; 609 int error; 610 struct file *fp; 611 612 error = 0; 613 bsd_args.flags = 0; 614 fp = NULL; 615 616 /* 617 * Linux mmap(2): 618 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE 619 */ 620 if (! ((linux_args->flags & LINUX_MAP_SHARED) ^ 621 (linux_args->flags & LINUX_MAP_PRIVATE))) 622 return (EINVAL); 623 624 if (linux_args->flags & LINUX_MAP_SHARED) 625 bsd_args.flags |= MAP_SHARED; 626 if (linux_args->flags & LINUX_MAP_PRIVATE) 627 bsd_args.flags |= MAP_PRIVATE; 628 if (linux_args->flags & LINUX_MAP_FIXED) 629 bsd_args.flags |= MAP_FIXED; 630 if (linux_args->flags & LINUX_MAP_ANON) 631 bsd_args.flags |= MAP_ANON; 632 else 633 bsd_args.flags |= MAP_NOSYNC; 634 if (linux_args->flags & LINUX_MAP_GROWSDOWN) { 635 bsd_args.flags |= MAP_STACK; 636 637 /* 638 * The linux MAP_GROWSDOWN option does not limit auto 639 * growth of the region. Linux mmap with this option 640 * takes as addr the inital BOS, and as len, the initial 641 * region size. It can then grow down from addr without 642 * limit. However, linux threads has an implicit internal 643 * limit to stack size of STACK_SIZE. Its just not 644 * enforced explicitly in linux. But, here we impose 645 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack 646 * region, since we can do this with our mmap. 647 * 648 * Our mmap with MAP_STACK takes addr as the maximum 649 * downsize limit on BOS, and as len the max size of 650 * the region. It them maps the top SGROWSIZ bytes, 651 * and autgrows the region down, up to the limit 652 * in addr. 653 * 654 * If we don't use the MAP_STACK option, the effect 655 * of this code is to allocate a stack region of a 656 * fixed size of (STACK_SIZE - GUARD_SIZE). 657 */ 658 659 /* This gives us TOS */ 660 bsd_args.addr = linux_args->addr + linux_args->len; 661 662 if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) { 663 /* 664 * Some linux apps will attempt to mmap 665 * thread stacks near the top of their 666 * address space. If their TOS is greater 667 * than vm_maxsaddr, vm_map_growstack() 668 * will confuse the thread stack with the 669 * process stack and deliver a SEGV if they 670 * attempt to grow the thread stack past their 671 * current stacksize rlimit. To avoid this, 672 * adjust vm_maxsaddr upwards to reflect 673 * the current stacksize rlimit rather 674 * than the maximum possible stacksize. 675 * It would be better to adjust the 676 * mmap'ed region, but some apps do not check 677 * mmap's return value. 678 */ 679 PROC_LOCK(p); 680 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK - 681 lim_cur(p, RLIMIT_STACK); 682 PROC_UNLOCK(p); 683 } 684 685 /* This gives us our maximum stack size */ 686 if (linux_args->len > STACK_SIZE - GUARD_SIZE) 687 bsd_args.len = linux_args->len; 688 else 689 bsd_args.len = STACK_SIZE - GUARD_SIZE; 690 691 /* 692 * This gives us a new BOS. If we're using VM_STACK, then 693 * mmap will just map the top SGROWSIZ bytes, and let 694 * the stack grow down to the limit at BOS. If we're 695 * not using VM_STACK we map the full stack, since we 696 * don't have a way to autogrow it. 697 */ 698 bsd_args.addr -= bsd_args.len; 699 } else { 700 bsd_args.addr = linux_args->addr; 701 bsd_args.len = linux_args->len; 702 } 703 704 bsd_args.prot = linux_args->prot; 705 if (linux_args->flags & LINUX_MAP_ANON) 706 bsd_args.fd = -1; 707 else { 708 /* 709 * Linux follows Solaris mmap(2) description: 710 * The file descriptor fildes is opened with 711 * read permission, regardless of the 712 * protection options specified. 713 * If PROT_WRITE is specified, the application 714 * must have opened the file descriptor 715 * fildes with write permission unless 716 * MAP_PRIVATE is specified in the flag 717 * argument as described below. 718 */ 719 720 if ((error = fget(td, linux_args->fd, &fp)) != 0) 721 return (error); 722 if (fp->f_type != DTYPE_VNODE) { 723 fdrop(fp, td); 724 return (EINVAL); 725 } 726 727 /* Linux mmap() just fails for O_WRONLY files */ 728 if (! (fp->f_flag & FREAD)) { 729 fdrop(fp, td); 730 return (EACCES); 731 } 732 733 bsd_args.fd = linux_args->fd; 734 fdrop(fp, td); 735 } 736 bsd_args.pos = linux_args->pos; 737 bsd_args.pad = 0; 738 739#ifdef DEBUG 740 if (ldebug(mmap)) 741 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n", 742 __func__, 743 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot, 744 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos); 745#endif 746 error = mmap(td, &bsd_args); 747#ifdef DEBUG 748 if (ldebug(mmap)) 749 printf("-> %s() return: 0x%x (0x%08x)\n", 750 __func__, error, (u_int)td->td_retval[0]); 751#endif 752 return (error); 753} 754 755int 756linux_pipe(struct thread *td, struct linux_pipe_args *args) 757{ 758 int error; 759 int reg_edx; 760 761#ifdef DEBUG 762 if (ldebug(pipe)) 763 printf(ARGS(pipe, "*")); 764#endif 765 766 reg_edx = td->td_retval[1]; 767 error = pipe(td, 0); 768 if (error) { 769 td->td_retval[1] = reg_edx; 770 return (error); 771 } 772 773 error = copyout(td->td_retval, args->pipefds, 2*sizeof(int)); 774 if (error) { 775 td->td_retval[1] = reg_edx; 776 return (error); 777 } 778 779 td->td_retval[1] = reg_edx; 780 td->td_retval[0] = 0; 781 return (0); 782} 783 784int 785linux_ioperm(struct thread *td, struct linux_ioperm_args *args) 786{ 787 int error; 788 struct i386_ioperm_args iia; 789 790 iia.start = args->start; 791 iia.length = args->length; 792 iia.enable = args->enable; 793 mtx_lock(&Giant); 794 error = i386_set_ioperm(td, &iia); 795 mtx_unlock(&Giant); 796 return (error); 797} 798 799int 800linux_iopl(struct thread *td, struct linux_iopl_args *args) 801{ 802 int error; 803 804 if (args->level < 0 || args->level > 3) 805 return (EINVAL); 806 if ((error = suser(td)) != 0) 807 return (error); 808 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 809 return (error); 810 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) | 811 (args->level * (PSL_IOPL / 3)); 812 return (0); 813} 814 815int 816linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap) 817{ 818 int error; 819 struct i386_ldt_args ldt; 820 struct l_descriptor ld; 821 union descriptor desc; 822 823 if (uap->ptr == NULL) 824 return (EINVAL); 825 826 switch (uap->func) { 827 case 0x00: /* read_ldt */ 828 ldt.start = 0; 829 ldt.descs = uap->ptr; 830 ldt.num = uap->bytecount / sizeof(union descriptor); 831 mtx_lock(&Giant); 832 error = i386_get_ldt(td, &ldt); 833 td->td_retval[0] *= sizeof(union descriptor); 834 mtx_unlock(&Giant); 835 break; 836 case 0x01: /* write_ldt */ 837 case 0x11: /* write_ldt */ 838 if (uap->bytecount != sizeof(ld)) 839 return (EINVAL); 840 841 error = copyin(uap->ptr, &ld, sizeof(ld)); 842 if (error) 843 return (error); 844 845 ldt.start = ld.entry_number; 846 ldt.descs = &desc; 847 ldt.num = 1; 848 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff); 849 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16; 850 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff); 851 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24; 852 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) | 853 (ld.contents << 2); 854 desc.sd.sd_dpl = 3; 855 desc.sd.sd_p = (ld.seg_not_present ^ 1); 856 desc.sd.sd_xx = 0; 857 desc.sd.sd_def32 = ld.seg_32bit; 858 desc.sd.sd_gran = ld.limit_in_pages; 859 mtx_lock(&Giant); 860 error = i386_set_ldt(td, &ldt, &desc); 861 mtx_unlock(&Giant); 862 break; 863 default: 864 error = EINVAL; 865 break; 866 } 867 868 if (error == EOPNOTSUPP) { 869 printf("linux: modify_ldt needs kernel option USER_LDT\n"); 870 error = ENOSYS; 871 } 872 873 return (error); 874} 875 876int 877linux_sigaction(struct thread *td, struct linux_sigaction_args *args) 878{ 879 l_osigaction_t osa; 880 l_sigaction_t act, oact; 881 int error; 882 883#ifdef DEBUG 884 if (ldebug(sigaction)) 885 printf(ARGS(sigaction, "%d, %p, %p"), 886 args->sig, (void *)args->nsa, (void *)args->osa); 887#endif 888 889 if (args->nsa != NULL) { 890 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t)); 891 if (error) 892 return (error); 893 act.lsa_handler = osa.lsa_handler; 894 act.lsa_flags = osa.lsa_flags; 895 act.lsa_restorer = osa.lsa_restorer; 896 LINUX_SIGEMPTYSET(act.lsa_mask); 897 act.lsa_mask.__bits[0] = osa.lsa_mask; 898 } 899 900 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL, 901 args->osa ? &oact : NULL); 902 903 if (args->osa != NULL && !error) { 904 osa.lsa_handler = oact.lsa_handler; 905 osa.lsa_flags = oact.lsa_flags; 906 osa.lsa_restorer = oact.lsa_restorer; 907 osa.lsa_mask = oact.lsa_mask.__bits[0]; 908 error = copyout(&osa, args->osa, sizeof(l_osigaction_t)); 909 } 910 911 return (error); 912} 913 914/* 915 * Linux has two extra args, restart and oldmask. We dont use these, 916 * but it seems that "restart" is actually a context pointer that 917 * enables the signal to happen with a different register set. 918 */ 919int 920linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args) 921{ 922 sigset_t sigmask; 923 l_sigset_t mask; 924 925#ifdef DEBUG 926 if (ldebug(sigsuspend)) 927 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask); 928#endif 929 930 LINUX_SIGEMPTYSET(mask); 931 mask.__bits[0] = args->mask; 932 linux_to_bsd_sigset(&mask, &sigmask); 933 return (kern_sigsuspend(td, sigmask)); 934} 935 936int 937linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 938{ 939 l_sigset_t lmask; 940 sigset_t sigmask; 941 int error; 942 943#ifdef DEBUG 944 if (ldebug(rt_sigsuspend)) 945 printf(ARGS(rt_sigsuspend, "%p, %d"), 946 (void *)uap->newset, uap->sigsetsize); 947#endif 948 949 if (uap->sigsetsize != sizeof(l_sigset_t)) 950 return (EINVAL); 951 952 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t)); 953 if (error) 954 return (error); 955 956 linux_to_bsd_sigset(&lmask, &sigmask); 957 return (kern_sigsuspend(td, sigmask)); 958} 959 960int 961linux_pause(struct thread *td, struct linux_pause_args *args) 962{ 963 struct proc *p = td->td_proc; 964 sigset_t sigmask; 965 966#ifdef DEBUG 967 if (ldebug(pause)) 968 printf(ARGS(pause, "")); 969#endif 970 971 PROC_LOCK(p); 972 sigmask = td->td_sigmask; 973 PROC_UNLOCK(p); 974 return (kern_sigsuspend(td, sigmask)); 975} 976 977int 978linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 979{ 980 stack_t ss, oss; 981 l_stack_t lss; 982 int error; 983 984#ifdef DEBUG 985 if (ldebug(sigaltstack)) 986 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss); 987#endif 988 989 if (uap->uss != NULL) { 990 error = copyin(uap->uss, &lss, sizeof(l_stack_t)); 991 if (error) 992 return (error); 993 994 ss.ss_sp = lss.ss_sp; 995 ss.ss_size = lss.ss_size; 996 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 997 } 998 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 999 (uap->uoss != NULL) ? &oss : NULL); 1000 if (!error && uap->uoss != NULL) { 1001 lss.ss_sp = oss.ss_sp; 1002 lss.ss_size = oss.ss_size; 1003 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 1004 error = copyout(&lss, uap->uoss, sizeof(l_stack_t)); 1005 } 1006 1007 return (error); 1008} 1009 1010int 1011linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args) 1012{ 1013 struct ftruncate_args sa; 1014 1015#ifdef DEBUG 1016 if (ldebug(ftruncate64)) 1017 printf(ARGS(ftruncate64, "%u, %jd"), args->fd, 1018 (intmax_t)args->length); 1019#endif 1020 1021 sa.fd = args->fd; 1022 sa.pad = 0; 1023 sa.length = args->length; 1024 return ftruncate(td, &sa); 1025} 1026 1027int 1028linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args) 1029{ 1030 struct l_user_desc info; 1031 int error; 1032 int idx; 1033 int a[2]; 1034 struct segment_descriptor sd; 1035 1036 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 1037 if (error) 1038 return (error); 1039 1040#ifdef DEBUG 1041 if (ldebug(set_thread_area)) 1042 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"), 1043 info.entry_number, 1044 info.base_addr, 1045 info.limit, 1046 info.seg_32bit, 1047 info.contents, 1048 info.read_exec_only, 1049 info.limit_in_pages, 1050 info.seg_not_present, 1051 info.useable); 1052#endif 1053 1054 idx = info.entry_number; 1055 /* 1056 * Semantics of linux version: every thread in the system has array 1057 * of 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This 1058 * syscall loads one of the selected tls decriptors with a value 1059 * and also loads GDT descriptors 6, 7 and 8 with the content of the per-thread 1060 * descriptors. 1061 * 1062 * Semantics of fbsd version: I think we can ignore that linux has 3 per-thread 1063 * descriptors and use just the 1st one. The tls_array[] is used only in 1064 * set/get-thread_area() syscalls and for loading the GDT descriptors. In fbsd 1065 * we use just one GDT descriptor for TLS so we will load just one. 1066 * XXX: this doesnt work when user-space process tries to use more then 1 TLS segment 1067 * comment in the linux sources says wine might do that. 1068 */ 1069 1070 /* 1071 * we support just GLIBC TLS now 1072 * we should let 3 proceed as well because we use this segment so 1073 * if code does two subsequent calls it should succeed 1074 */ 1075 if (idx != 6 && idx != -1 && idx != 3) 1076 return (EINVAL); 1077 1078 /* 1079 * we have to copy out the GDT entry we use 1080 * FreeBSD uses GDT entry #3 for storing %gs so load that 1081 * XXX: what if userspace program doesnt check this value and tries 1082 * to use 6, 7 or 8? 1083 */ 1084 idx = info.entry_number = 3; 1085 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 1086 if (error) 1087 return (error); 1088 1089 if (LDT_empty(&info)) { 1090 a[0] = 0; 1091 a[1] = 0; 1092 } else { 1093 a[0] = LDT_entry_a(&info); 1094 a[1] = LDT_entry_b(&info); 1095 } 1096 1097 memcpy(&sd, &a, sizeof(a)); 1098#ifdef DEBUG 1099 if (ldebug(set_thread_area)) 1100 printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase, 1101 sd.sd_hibase, 1102 sd.sd_lolimit, 1103 sd.sd_hilimit, 1104 sd.sd_type, 1105 sd.sd_dpl, 1106 sd.sd_p, 1107 sd.sd_xx, 1108 sd.sd_def32, 1109 sd.sd_gran); 1110#endif 1111 1112 /* this is taken from i386 version of cpu_set_user_tls() */ 1113 critical_enter(); 1114 /* set %gs */ 1115 td->td_pcb->pcb_gsd = sd; 1116 PCPU_GET(fsgs_gdt)[1] = sd; 1117 load_gs(GSEL(GUGS_SEL, SEL_UPL)); 1118 critical_exit(); 1119 1120 return (0); 1121} 1122 1123int 1124linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args) 1125{ 1126 1127 struct l_user_desc info; 1128 int error; 1129 int idx; 1130 struct l_desc_struct desc; 1131 struct segment_descriptor sd; 1132 1133#ifdef DEBUG 1134 if (ldebug(get_thread_area)) 1135 printf(ARGS(get_thread_area, "%p"), args->desc); 1136#endif 1137 1138 error = copyin(args->desc, &info, sizeof(struct l_user_desc)); 1139 if (error) 1140 return (error); 1141 1142 idx = info.entry_number; 1143 /* XXX: I am not sure if we want 3 to be allowed too. */ 1144 if (idx != 6 && idx != 3) 1145 return (EINVAL); 1146 1147 idx = 3; 1148 1149 memset(&info, 0, sizeof(info)); 1150 1151 sd = PCPU_GET(fsgs_gdt)[1]; 1152 1153 memcpy(&desc, &sd, sizeof(desc)); 1154 1155 info.entry_number = idx; 1156 info.base_addr = GET_BASE(&desc); 1157 info.limit = GET_LIMIT(&desc); 1158 info.seg_32bit = GET_32BIT(&desc); 1159 info.contents = GET_CONTENTS(&desc); 1160 info.read_exec_only = !GET_WRITABLE(&desc); 1161 info.limit_in_pages = GET_LIMIT_PAGES(&desc); 1162 info.seg_not_present = !GET_PRESENT(&desc); 1163 info.useable = GET_USEABLE(&desc); 1164 1165 error = copyout(&info, args->desc, sizeof(struct l_user_desc)); 1166 if (error) 1167 return (EFAULT); 1168 1169 return (0); 1170} 1171 1172/* copied from kern/kern_time.c */ 1173int 1174linux_timer_create(struct thread *td, struct linux_timer_create_args *args) 1175{ 1176 return ktimer_create(td, (struct ktimer_create_args *) args); 1177} 1178 1179int 1180linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args) 1181{ 1182 return ktimer_settime(td, (struct ktimer_settime_args *) args); 1183} 1184 1185int 1186linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args) 1187{ 1188 return ktimer_gettime(td, (struct ktimer_gettime_args *) args); 1189} 1190 1191int 1192linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args) 1193{ 1194 return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args); 1195} 1196 1197int 1198linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args) 1199{ 1200 return ktimer_delete(td, (struct ktimer_delete_args *) args); 1201} 1202 1203/* XXX: this wont work with module - convert it */ 1204int 1205linux_mq_open(struct thread *td, struct linux_mq_open_args *args) 1206{ 1207#ifdef P1003_1B_MQUEUE 1208 return kmq_open(td, (struct kmq_open_args *) args); 1209#else 1210 return (ENOSYS); 1211#endif 1212} 1213 1214int 1215linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args) 1216{ 1217#ifdef P1003_1B_MQUEUE 1218 return kmq_unlink(td, (struct kmq_unlink_args *) args); 1219#else 1220 return (ENOSYS); 1221#endif 1222} 1223 1224int 1225linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args) 1226{ 1227#ifdef P1003_1B_MQUEUE 1228 return kmq_timedsend(td, (struct kmq_timedsend_args *) args); 1229#else 1230 return (ENOSYS); 1231#endif 1232} 1233 1234int 1235linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args) 1236{ 1237#ifdef P1003_1B_MQUEUE 1238 return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args); 1239#else 1240 return (ENOSYS); 1241#endif 1242} 1243 1244int 1245linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args) 1246{ 1247#ifdef P1003_1B_MQUEUE 1248 return kmq_notify(td, (struct kmq_notify_args *) args); 1249#else 1250 return (ENOSYS); 1251#endif 1252} 1253 1254int 1255linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args) 1256{ 1257#ifdef P1003_1B_MQUEUE 1258 return kmq_setattr(td, (struct kmq_setattr_args *) args); 1259#else 1260 return (ENOSYS); 1261#endif 1262} 1263 1264