The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/amd64/linux32/linux32_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 2382316a509f03d745684ebd6fec9d00


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.