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: releng/6.4/sys/amd64/linux32/linux32_machdep.c 176285 2008-02-14 18:41:00Z jkim $");
   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/imgact.h>
   40 #include <sys/lock.h>
   41 #include <sys/malloc.h>
   42 #include <sys/mman.h>
   43 #include <sys/mutex.h>
   44 #include <sys/proc.h>
   45 #include <sys/resource.h>
   46 #include <sys/resourcevar.h>
   47 #include <sys/syscallsubr.h>
   48 #include <sys/sysproto.h>
   49 #include <sys/unistd.h>
   50 
   51 #include <machine/frame.h>
   52 #include <machine/psl.h>
   53 
   54 #include <vm/vm.h>
   55 #include <vm/pmap.h>
   56 #include <vm/vm_extern.h>
   57 #include <vm/vm_kern.h>
   58 #include <vm/vm_map.h>
   59 
   60 #include <amd64/linux32/linux.h>
   61 #include <amd64/linux32/linux32_proto.h>
   62 #include <compat/linux/linux_ipc.h>
   63 #include <compat/linux/linux_signal.h>
   64 #include <compat/linux/linux_util.h>
   65 
   66 struct l_old_select_argv {
   67         l_int           nfds;
   68         l_uintptr_t     readfds;
   69         l_uintptr_t     writefds;
   70         l_uintptr_t     exceptfds;
   71         l_uintptr_t     timeout;
   72 } __packed;
   73 
   74 int
   75 linux_to_bsd_sigaltstack(int lsa)
   76 {
   77         int bsa = 0;
   78 
   79         if (lsa & LINUX_SS_DISABLE)
   80                 bsa |= SS_DISABLE;
   81         if (lsa & LINUX_SS_ONSTACK)
   82                 bsa |= SS_ONSTACK;
   83         return (bsa);
   84 }
   85 
   86 int
   87 bsd_to_linux_sigaltstack(int bsa)
   88 {
   89         int lsa = 0;
   90 
   91         if (bsa & SS_DISABLE)
   92                 lsa |= LINUX_SS_DISABLE;
   93         if (bsa & SS_ONSTACK)
   94                 lsa |= LINUX_SS_ONSTACK;
   95         return (lsa);
   96 }
   97 
   98 /*
   99  * Custom version of exec_copyin_args() so that we can translate
  100  * the pointers.
  101  */
  102 static int
  103 linux_exec_copyin_args(struct image_args *args, char *fname,
  104     enum uio_seg segflg, char **argv, char **envv)
  105 {
  106         char *argp, *envp;
  107         u_int32_t *p32, arg;
  108         size_t length;
  109         int error;
  110 
  111         bzero(args, sizeof(*args));
  112         if (argv == NULL)
  113                 return (EFAULT);
  114 
  115         /*
  116          * Allocate temporary demand zeroed space for argument and
  117          *      environment strings
  118          */
  119         args->buf = (char *)kmem_alloc_wait(exec_map,
  120             PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
  121         if (args->buf == NULL)
  122                 return (ENOMEM);
  123         args->begin_argv = args->buf;
  124         args->endp = args->begin_argv;
  125         args->stringspace = ARG_MAX;
  126 
  127         args->fname = args->buf + ARG_MAX;
  128 
  129         /*
  130          * Copy the file name.
  131          */
  132         error = (segflg == UIO_SYSSPACE) ?
  133             copystr(fname, args->fname, PATH_MAX, &length) :
  134             copyinstr(fname, args->fname, PATH_MAX, &length);
  135         if (error != 0)
  136                 goto err_exit;
  137 
  138         /*
  139          * extract arguments first
  140          */
  141         p32 = (u_int32_t *)argv;
  142         for (;;) {
  143                 error = copyin(p32++, &arg, sizeof(arg));
  144                 if (error)
  145                         goto err_exit;
  146                 if (arg == 0)
  147                         break;
  148                 argp = PTRIN(arg);
  149                 error = copyinstr(argp, args->endp, args->stringspace, &length);
  150                 if (error) {
  151                         if (error == ENAMETOOLONG)
  152                                 error = E2BIG;
  153 
  154                         goto err_exit;
  155                 }
  156                 args->stringspace -= length;
  157                 args->endp += length;
  158                 args->argc++;
  159         }
  160 
  161         args->begin_envv = args->endp;
  162 
  163         /*
  164          * extract environment strings
  165          */
  166         if (envv) {
  167                 p32 = (u_int32_t *)envv;
  168                 for (;;) {
  169                         error = copyin(p32++, &arg, sizeof(arg));
  170                         if (error)
  171                                 goto err_exit;
  172                         if (arg == 0)
  173                                 break;
  174                         envp = PTRIN(arg);
  175                         error = copyinstr(envp, args->endp, args->stringspace,
  176                             &length);
  177                         if (error) {
  178                                 if (error == ENAMETOOLONG)
  179                                         error = E2BIG;
  180                                 goto err_exit;
  181                         }
  182                         args->stringspace -= length;
  183                         args->endp += length;
  184                         args->envc++;
  185                 }
  186         }
  187 
  188         return (0);
  189 
  190 err_exit:
  191         kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
  192             PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
  193         args->buf = NULL;
  194         return (error);
  195 }
  196 
  197 int
  198 linux_execve(struct thread *td, struct linux_execve_args *args)
  199 {
  200         struct image_args eargs;
  201         char *path;
  202         int error;
  203 
  204         LCONVPATHEXIST(td, args->path, &path);
  205 
  206 #ifdef DEBUG
  207         if (ldebug(execve))
  208                 printf(ARGS(execve, "%s"), path);
  209 #endif
  210 
  211         error = linux_exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
  212             args->envp);
  213         free(path, M_TEMP);
  214         if (error == 0)
  215                 error = kern_execve(td, &eargs, NULL);
  216         exec_free_args(&eargs);
  217         return (error);
  218 }
  219 
  220 struct iovec32 {
  221         u_int32_t iov_base;
  222         int     iov_len;
  223 };
  224 
  225 CTASSERT(sizeof(struct iovec32) == 8);
  226 
  227 static int
  228 linux32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
  229 {
  230         struct iovec32 iov32;
  231         struct iovec *iov;
  232         struct uio *uio;
  233         u_int iovlen;
  234         int error, i;
  235 
  236         *uiop = NULL;
  237         if (iovcnt > UIO_MAXIOV)
  238                 return (EINVAL);
  239         iovlen = iovcnt * sizeof(struct iovec);
  240         uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
  241         iov = (struct iovec *)(uio + 1);
  242         for (i = 0; i < iovcnt; i++) {
  243                 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
  244                 if (error) {
  245                         free(uio, M_IOV);
  246                         return (error);
  247                 }
  248                 iov[i].iov_base = PTRIN(iov32.iov_base);
  249                 iov[i].iov_len = iov32.iov_len;
  250         }
  251         uio->uio_iov = iov;
  252         uio->uio_iovcnt = iovcnt;
  253         uio->uio_segflg = UIO_USERSPACE;
  254         uio->uio_offset = -1;
  255         uio->uio_resid = 0;
  256         for (i = 0; i < iovcnt; i++) {
  257                 if (iov->iov_len > INT_MAX - uio->uio_resid) {
  258                         free(uio, M_IOV);
  259                         return (EINVAL);
  260                 }
  261                 uio->uio_resid += iov->iov_len;
  262                 iov++;
  263         }
  264         *uiop = uio;
  265         return (0);
  266 }
  267 
  268 int
  269 linux_readv(struct thread *td, struct linux_readv_args *uap)
  270 {
  271         struct uio *auio;
  272         int error;
  273 
  274         error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  275         if (error)
  276                 return (error);
  277         error = kern_readv(td, uap->fd, auio);
  278         free(auio, M_IOV);
  279         return (error);
  280 }
  281 
  282 int
  283 linux_writev(struct thread *td, struct linux_writev_args *uap)
  284 {
  285         struct uio *auio;
  286         int error;
  287 
  288         error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  289         if (error)
  290                 return (error);
  291         error = kern_writev(td, uap->fd, auio);
  292         free(auio, M_IOV);
  293         return (error);
  294 }
  295 
  296 struct l_ipc_kludge {
  297         l_uintptr_t msgp;
  298         l_long msgtyp;
  299 } __packed;
  300 
  301 int
  302 linux_ipc(struct thread *td, struct linux_ipc_args *args)
  303 {
  304 
  305         switch (args->what & 0xFFFF) {
  306         case LINUX_SEMOP: {
  307                 struct linux_semop_args a;
  308 
  309                 a.semid = args->arg1;
  310                 a.tsops = args->ptr;
  311                 a.nsops = args->arg2;
  312                 return (linux_semop(td, &a));
  313         }
  314         case LINUX_SEMGET: {
  315                 struct linux_semget_args a;
  316 
  317                 a.key = args->arg1;
  318                 a.nsems = args->arg2;
  319                 a.semflg = args->arg3;
  320                 return (linux_semget(td, &a));
  321         }
  322         case LINUX_SEMCTL: {
  323                 struct linux_semctl_args a;
  324                 int error;
  325 
  326                 a.semid = args->arg1;
  327                 a.semnum = args->arg2;
  328                 a.cmd = args->arg3;
  329                 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
  330                 if (error)
  331                         return (error);
  332                 return (linux_semctl(td, &a));
  333         }
  334         case LINUX_MSGSND: {
  335                 struct linux_msgsnd_args a;
  336 
  337                 a.msqid = args->arg1;
  338                 a.msgp = args->ptr;
  339                 a.msgsz = args->arg2;
  340                 a.msgflg = args->arg3;
  341                 return (linux_msgsnd(td, &a));
  342         }
  343         case LINUX_MSGRCV: {
  344                 struct linux_msgrcv_args a;
  345 
  346                 a.msqid = args->arg1;
  347                 a.msgsz = args->arg2;
  348                 a.msgflg = args->arg3;
  349                 if ((args->what >> 16) == 0) {
  350                         struct l_ipc_kludge tmp;
  351                         int error;
  352 
  353                         if (args->ptr == 0)
  354                                 return (EINVAL);
  355                         error = copyin(args->ptr, &tmp, sizeof(tmp));
  356                         if (error)
  357                                 return (error);
  358                         a.msgp = PTRIN(tmp.msgp);
  359                         a.msgtyp = tmp.msgtyp;
  360                 } else {
  361                         a.msgp = args->ptr;
  362                         a.msgtyp = args->arg5;
  363                 }
  364                 return (linux_msgrcv(td, &a));
  365         }
  366         case LINUX_MSGGET: {
  367                 struct linux_msgget_args a;
  368 
  369                 a.key = args->arg1;
  370                 a.msgflg = args->arg2;
  371                 return (linux_msgget(td, &a));
  372         }
  373         case LINUX_MSGCTL: {
  374                 struct linux_msgctl_args a;
  375 
  376                 a.msqid = args->arg1;
  377                 a.cmd = args->arg2;
  378                 a.buf = args->ptr;
  379                 return (linux_msgctl(td, &a));
  380         }
  381         case LINUX_SHMAT: {
  382                 struct linux_shmat_args a;
  383 
  384                 a.shmid = args->arg1;
  385                 a.shmaddr = args->ptr;
  386                 a.shmflg = args->arg2;
  387                 a.raddr = PTRIN((l_uint)args->arg3);
  388                 return (linux_shmat(td, &a));
  389         }
  390         case LINUX_SHMDT: {
  391                 struct linux_shmdt_args a;
  392 
  393                 a.shmaddr = args->ptr;
  394                 return (linux_shmdt(td, &a));
  395         }
  396         case LINUX_SHMGET: {
  397                 struct linux_shmget_args a;
  398 
  399                 a.key = args->arg1;
  400                 a.size = args->arg2;
  401                 a.shmflg = args->arg3;
  402                 return (linux_shmget(td, &a));
  403         }
  404         case LINUX_SHMCTL: {
  405                 struct linux_shmctl_args a;
  406 
  407                 a.shmid = args->arg1;
  408                 a.cmd = args->arg2;
  409                 a.buf = args->ptr;
  410                 return (linux_shmctl(td, &a));
  411         }
  412         default:
  413                 break;
  414         }
  415 
  416         return (EINVAL);
  417 }
  418 
  419 int
  420 linux_old_select(struct thread *td, struct linux_old_select_args *args)
  421 {
  422         struct l_old_select_argv linux_args;
  423         struct linux_select_args newsel;
  424         int error;
  425 
  426 #ifdef DEBUG
  427         if (ldebug(old_select))
  428                 printf(ARGS(old_select, "%p"), args->ptr);
  429 #endif
  430 
  431         error = copyin(args->ptr, &linux_args, sizeof(linux_args));
  432         if (error)
  433                 return (error);
  434 
  435         newsel.nfds = linux_args.nfds;
  436         newsel.readfds = PTRIN(linux_args.readfds);
  437         newsel.writefds = PTRIN(linux_args.writefds);
  438         newsel.exceptfds = PTRIN(linux_args.exceptfds);
  439         newsel.timeout = PTRIN(linux_args.timeout);
  440         return (linux_select(td, &newsel));
  441 }
  442 
  443 int
  444 linux_fork(struct thread *td, struct linux_fork_args *args)
  445 {
  446         int error;
  447 
  448 #ifdef DEBUG
  449         if (ldebug(fork))
  450                 printf(ARGS(fork, ""));
  451 #endif
  452 
  453         if ((error = fork(td, (struct fork_args *)args)) != 0)
  454                 return (error);
  455 
  456         if (td->td_retval[1] == 1)
  457                 td->td_retval[0] = 0;
  458         return (0);
  459 }
  460 
  461 int
  462 linux_vfork(struct thread *td, struct linux_vfork_args *args)
  463 {
  464         int error;
  465 
  466 #ifdef DEBUG
  467         if (ldebug(vfork))
  468                 printf(ARGS(vfork, ""));
  469 #endif
  470 
  471         if ((error = vfork(td, (struct vfork_args *)args)) != 0)
  472                 return (error);
  473         /* Are we the child? */
  474         if (td->td_retval[1] == 1)
  475                 td->td_retval[0] = 0;
  476         return (0);
  477 }
  478 
  479 #define CLONE_VM        0x100
  480 #define CLONE_FS        0x200
  481 #define CLONE_FILES     0x400
  482 #define CLONE_SIGHAND   0x800
  483 #define CLONE_PID       0x1000
  484 
  485 int
  486 linux_clone(struct thread *td, struct linux_clone_args *args)
  487 {
  488         int error, ff = RFPROC | RFSTOPPED;
  489         struct proc *p2;
  490         struct thread *td2;
  491         int exit_signal;
  492 
  493 #ifdef DEBUG
  494         if (ldebug(clone)) {
  495                 printf(ARGS(clone, "flags %x, stack %x"),
  496                     (unsigned int)(uintptr_t)args->flags,
  497                     (unsigned int)(uintptr_t)args->stack);
  498                 if (args->flags & CLONE_PID)
  499                         printf(LMSG("CLONE_PID not yet supported"));
  500         }
  501 #endif
  502 
  503         if (!args->stack)
  504                 return (EINVAL);
  505 
  506         exit_signal = args->flags & 0x000000ff;
  507         if (exit_signal >= LINUX_NSIG)
  508                 return (EINVAL);
  509 
  510         if (exit_signal <= LINUX_SIGTBLSZ)
  511                 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
  512 
  513         if (args->flags & CLONE_VM)
  514                 ff |= RFMEM;
  515         if (args->flags & CLONE_SIGHAND)
  516                 ff |= RFSIGSHARE;
  517         if (!(args->flags & CLONE_FILES))
  518                 ff |= RFFDG;
  519 
  520         error = fork1(td, ff, 0, &p2);
  521         if (error)
  522                 return (error);
  523         
  524 
  525         PROC_LOCK(p2);
  526         p2->p_sigparent = exit_signal;
  527         PROC_UNLOCK(p2);
  528         td2 = FIRST_THREAD_IN_PROC(p2);
  529         td2->td_frame->tf_rsp = PTROUT(args->stack);
  530 
  531 #ifdef DEBUG
  532         if (ldebug(clone))
  533                 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
  534                     (long)p2->p_pid, args->stack, exit_signal);
  535 #endif
  536 
  537         /*
  538          * Make this runnable after we are finished with it.
  539          */
  540         mtx_lock_spin(&sched_lock);
  541         TD_SET_CAN_RUN(td2);
  542         setrunqueue(td2, SRQ_BORING);
  543         mtx_unlock_spin(&sched_lock);
  544 
  545         td->td_retval[0] = p2->p_pid;
  546         td->td_retval[1] = 0;
  547         return (0);
  548 }
  549 
  550 #define STACK_SIZE  (2 * 1024 * 1024)
  551 #define GUARD_SIZE  (4 * PAGE_SIZE)
  552 
  553 static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
  554 
  555 int
  556 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
  557 {
  558         struct l_mmap_argv linux_args;
  559 
  560 #ifdef DEBUG
  561         if (ldebug(mmap2))
  562                 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
  563                     args->addr, args->len, args->prot,
  564                     args->flags, args->fd, args->pgoff);
  565 #endif
  566 
  567         linux_args.addr = PTROUT(args->addr);
  568         linux_args.len = args->len;
  569         linux_args.prot = args->prot;
  570         linux_args.flags = args->flags;
  571         linux_args.fd = args->fd;
  572         linux_args.pgoff = args->pgoff;
  573 
  574         return (linux_mmap_common(td, &linux_args));
  575 }
  576 
  577 int
  578 linux_mmap(struct thread *td, struct linux_mmap_args *args)
  579 {
  580         int error;
  581         struct l_mmap_argv linux_args;
  582 
  583         error = copyin(args->ptr, &linux_args, sizeof(linux_args));
  584         if (error)
  585                 return (error);
  586 
  587 #ifdef DEBUG
  588         if (ldebug(mmap))
  589                 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
  590                     linux_args.addr, linux_args.len, linux_args.prot,
  591                     linux_args.flags, linux_args.fd, linux_args.pgoff);
  592 #endif
  593         if ((linux_args.pgoff % PAGE_SIZE) != 0)
  594                 return (EINVAL);
  595         linux_args.pgoff /= PAGE_SIZE;
  596 
  597         return (linux_mmap_common(td, &linux_args));
  598 }
  599 
  600 static int
  601 linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
  602 {
  603         struct proc *p = td->td_proc;
  604         struct mmap_args /* {
  605                 caddr_t addr;
  606                 size_t len;
  607                 int prot;
  608                 int flags;
  609                 int fd;
  610                 long pad;
  611                 off_t pos;
  612         } */ bsd_args;
  613         int error;
  614         struct file *fp;
  615 
  616         error = 0;
  617         bsd_args.flags = 0;
  618         fp = NULL;
  619 
  620         /*
  621          * Linux mmap(2):
  622          * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
  623          */
  624         if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
  625             (linux_args->flags & LINUX_MAP_PRIVATE)))
  626                 return (EINVAL);
  627 
  628         if (linux_args->flags & LINUX_MAP_SHARED)
  629                 bsd_args.flags |= MAP_SHARED;
  630         if (linux_args->flags & LINUX_MAP_PRIVATE)
  631                 bsd_args.flags |= MAP_PRIVATE;
  632         if (linux_args->flags & LINUX_MAP_FIXED)
  633                 bsd_args.flags |= MAP_FIXED;
  634         if (linux_args->flags & LINUX_MAP_ANON)
  635                 bsd_args.flags |= MAP_ANON;
  636         else
  637                 bsd_args.flags |= MAP_NOSYNC;
  638         if (linux_args->flags & LINUX_MAP_GROWSDOWN)
  639                 bsd_args.flags |= MAP_STACK;
  640 
  641         /*
  642          * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
  643          * on Linux/i386. We do this to ensure maximum compatibility.
  644          * Linux/ia64 does the same in i386 emulation mode.
  645          */
  646         bsd_args.prot = linux_args->prot;
  647         if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
  648                 bsd_args.prot |= PROT_READ | PROT_EXEC;
  649 
  650         /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
  651         bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : linux_args->fd;
  652         if (bsd_args.fd != -1) {
  653                 /*
  654                  * Linux follows Solaris mmap(2) description:
  655                  * The file descriptor fildes is opened with
  656                  * read permission, regardless of the
  657                  * protection options specified.
  658                  */
  659 
  660                 if ((error = fget(td, bsd_args.fd, &fp)) != 0)
  661                         return (error);
  662                 if (fp->f_type != DTYPE_VNODE) {
  663                         fdrop(fp, td);
  664                         return (EINVAL);
  665                 }
  666 
  667                 /* Linux mmap() just fails for O_WRONLY files */
  668                 if (!(fp->f_flag & FREAD)) {
  669                         fdrop(fp, td);
  670                         return (EACCES);
  671                 }
  672 
  673                 fdrop(fp, td);
  674         }
  675 
  676         if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
  677                 /*
  678                  * The Linux MAP_GROWSDOWN option does not limit auto
  679                  * growth of the region.  Linux mmap with this option
  680                  * takes as addr the inital BOS, and as len, the initial
  681                  * region size.  It can then grow down from addr without
  682                  * limit.  However, Linux threads has an implicit internal
  683                  * limit to stack size of STACK_SIZE.  Its just not
  684                  * enforced explicitly in Linux.  But, here we impose
  685                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
  686                  * region, since we can do this with our mmap.
  687                  *
  688                  * Our mmap with MAP_STACK takes addr as the maximum
  689                  * downsize limit on BOS, and as len the max size of
  690                  * the region.  It then maps the top SGROWSIZ bytes,
  691                  * and auto grows the region down, up to the limit
  692                  * in addr.
  693                  *
  694                  * If we don't use the MAP_STACK option, the effect
  695                  * of this code is to allocate a stack region of a
  696                  * fixed size of (STACK_SIZE - GUARD_SIZE).
  697                  */
  698 
  699                 if ((caddr_t)PTRIN(linux_args->addr) + linux_args->len >
  700                     p->p_vmspace->vm_maxsaddr) {
  701                         /*
  702                          * Some Linux apps will attempt to mmap
  703                          * thread stacks near the top of their
  704                          * address space.  If their TOS is greater
  705                          * than vm_maxsaddr, vm_map_growstack()
  706                          * will confuse the thread stack with the
  707                          * process stack and deliver a SEGV if they
  708                          * attempt to grow the thread stack past their
  709                          * current stacksize rlimit.  To avoid this,
  710                          * adjust vm_maxsaddr upwards to reflect
  711                          * the current stacksize rlimit rather
  712                          * than the maximum possible stacksize.
  713                          * It would be better to adjust the
  714                          * mmap'ed region, but some apps do not check
  715                          * mmap's return value.
  716                          */
  717                         PROC_LOCK(p);
  718                         p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK -
  719                             lim_cur(p, RLIMIT_STACK);
  720                         PROC_UNLOCK(p);
  721                 }
  722 
  723                 /*
  724                  * This gives us our maximum stack size and a new BOS.
  725                  * If we're using VM_STACK, then mmap will just map
  726                  * the top SGROWSIZ bytes, and let the stack grow down
  727                  * to the limit at BOS.  If we're not using VM_STACK
  728                  * we map the full stack, since we don't have a way
  729                  * to autogrow it.
  730                  */
  731                 if (linux_args->len > STACK_SIZE - GUARD_SIZE) {
  732                         bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
  733                         bsd_args.len = linux_args->len;
  734                 } else {
  735                         bsd_args.addr = (caddr_t)PTRIN(linux_args->addr) -
  736                             (STACK_SIZE - GUARD_SIZE - linux_args->len);
  737                         bsd_args.len = STACK_SIZE - GUARD_SIZE;
  738                 }
  739         } else {
  740                 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
  741                 bsd_args.len  = linux_args->len;
  742         }
  743         bsd_args.pos = (off_t)linux_args->pgoff * PAGE_SIZE;
  744         bsd_args.pad = 0;
  745 
  746 #ifdef DEBUG
  747         if (ldebug(mmap))
  748                 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
  749                     __func__,
  750                     (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot,
  751                     bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
  752 #endif
  753         error = mmap(td, &bsd_args);
  754 #ifdef DEBUG
  755         if (ldebug(mmap))
  756                 printf("-> %s() return: 0x%x (0x%08x)\n",
  757                         __func__, error, (u_int)td->td_retval[0]);
  758 #endif
  759         return (error);
  760 }
  761 
  762 int
  763 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
  764 {
  765         struct mprotect_args bsd_args;
  766 
  767         bsd_args.addr = uap->addr;
  768         bsd_args.len = uap->len;
  769         bsd_args.prot = uap->prot;
  770         if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
  771                 bsd_args.prot |= PROT_READ | PROT_EXEC;
  772         return (mprotect(td, &bsd_args));
  773 }
  774 
  775 int
  776 linux_iopl(struct thread *td, struct linux_iopl_args *args)
  777 {
  778         int error;
  779 
  780         if (args->level < 0 || args->level > 3)
  781                 return (EINVAL);
  782         if ((error = suser(td)) != 0)
  783                 return (error);
  784         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  785                 return (error);
  786         td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
  787             (args->level * (PSL_IOPL / 3));
  788 
  789         return (0);
  790 }
  791 
  792 int
  793 linux_pipe(struct thread *td, struct linux_pipe_args *args)
  794 {
  795         int pip[2];
  796         int error;
  797         register_t reg_rdx;
  798 
  799 #ifdef DEBUG
  800         if (ldebug(pipe))
  801                 printf(ARGS(pipe, "*"));
  802 #endif
  803 
  804         reg_rdx = td->td_retval[1];
  805         error = pipe(td, 0);
  806         if (error) {
  807                 td->td_retval[1] = reg_rdx;
  808                 return (error);
  809         }
  810 
  811         pip[0] = td->td_retval[0];
  812         pip[1] = td->td_retval[1];
  813         error = copyout(pip, args->pipefds, 2 * sizeof(int));
  814         if (error) {
  815                 td->td_retval[1] = reg_rdx;
  816                 return (error);
  817         }
  818 
  819         td->td_retval[1] = reg_rdx;
  820         td->td_retval[0] = 0;
  821         return (0);
  822 }
  823 
  824 int
  825 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
  826 {
  827         l_osigaction_t osa;
  828         l_sigaction_t act, oact;
  829         int error;
  830 
  831 #ifdef DEBUG
  832         if (ldebug(sigaction))
  833                 printf(ARGS(sigaction, "%d, %p, %p"),
  834                     args->sig, (void *)args->nsa, (void *)args->osa);
  835 #endif
  836 
  837         if (args->nsa != NULL) {
  838                 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
  839                 if (error)
  840                         return (error);
  841                 act.lsa_handler = osa.lsa_handler;
  842                 act.lsa_flags = osa.lsa_flags;
  843                 act.lsa_restorer = osa.lsa_restorer;
  844                 LINUX_SIGEMPTYSET(act.lsa_mask);
  845                 act.lsa_mask.__bits[0] = osa.lsa_mask;
  846         }
  847 
  848         error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
  849             args->osa ? &oact : NULL);
  850 
  851         if (args->osa != NULL && !error) {
  852                 osa.lsa_handler = oact.lsa_handler;
  853                 osa.lsa_flags = oact.lsa_flags;
  854                 osa.lsa_restorer = oact.lsa_restorer;
  855                 osa.lsa_mask = oact.lsa_mask.__bits[0];
  856                 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
  857         }
  858 
  859         return (error);
  860 }
  861 
  862 /*
  863  * Linux has two extra args, restart and oldmask.  We don't use these,
  864  * but it seems that "restart" is actually a context pointer that
  865  * enables the signal to happen with a different register set.
  866  */
  867 int
  868 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
  869 {
  870         sigset_t sigmask;
  871         l_sigset_t mask;
  872 
  873 #ifdef DEBUG
  874         if (ldebug(sigsuspend))
  875                 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
  876 #endif
  877 
  878         LINUX_SIGEMPTYSET(mask);
  879         mask.__bits[0] = args->mask;
  880         linux_to_bsd_sigset(&mask, &sigmask);
  881         return (kern_sigsuspend(td, sigmask));
  882 }
  883 
  884 int
  885 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
  886 {
  887         l_sigset_t lmask;
  888         sigset_t sigmask;
  889         int error;
  890 
  891 #ifdef DEBUG
  892         if (ldebug(rt_sigsuspend))
  893                 printf(ARGS(rt_sigsuspend, "%p, %d"),
  894                     (void *)uap->newset, uap->sigsetsize);
  895 #endif
  896 
  897         if (uap->sigsetsize != sizeof(l_sigset_t))
  898                 return (EINVAL);
  899 
  900         error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
  901         if (error)
  902                 return (error);
  903 
  904         linux_to_bsd_sigset(&lmask, &sigmask);
  905         return (kern_sigsuspend(td, sigmask));
  906 }
  907 
  908 int
  909 linux_pause(struct thread *td, struct linux_pause_args *args)
  910 {
  911         struct proc *p = td->td_proc;
  912         sigset_t sigmask;
  913 
  914 #ifdef DEBUG
  915         if (ldebug(pause))
  916                 printf(ARGS(pause, ""));
  917 #endif
  918 
  919         PROC_LOCK(p);
  920         sigmask = td->td_sigmask;
  921         PROC_UNLOCK(p);
  922         return (kern_sigsuspend(td, sigmask));
  923 }
  924 
  925 int
  926 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
  927 {
  928         stack_t ss, oss;
  929         l_stack_t lss;
  930         int error;
  931 
  932 #ifdef DEBUG
  933         if (ldebug(sigaltstack))
  934                 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
  935 #endif
  936 
  937         if (uap->uss != NULL) {
  938                 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
  939                 if (error)
  940                         return (error);
  941 
  942                 ss.ss_sp = PTRIN(lss.ss_sp);
  943                 ss.ss_size = lss.ss_size;
  944                 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
  945         }
  946         error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
  947             (uap->uoss != NULL) ? &oss : NULL);
  948         if (!error && uap->uoss != NULL) {
  949                 lss.ss_sp = PTROUT(oss.ss_sp);
  950                 lss.ss_size = oss.ss_size;
  951                 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
  952                 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
  953         }
  954 
  955         return (error);
  956 }
  957 
  958 int
  959 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
  960 {
  961         struct ftruncate_args sa;
  962 
  963 #ifdef DEBUG
  964         if (ldebug(ftruncate64))
  965                 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
  966                     (intmax_t)args->length);
  967 #endif
  968 
  969         sa.fd = args->fd;
  970         sa.pad = 0;
  971         sa.length = args->length;
  972         return ftruncate(td, &sa);
  973 }
  974 
  975 int
  976 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
  977 {
  978         struct timeval atv;
  979         l_timeval atv32;
  980         struct timezone rtz;
  981         int error = 0;
  982 
  983         if (uap->tp) {
  984                 microtime(&atv);
  985                 atv32.tv_sec = atv.tv_sec;
  986                 atv32.tv_usec = atv.tv_usec;
  987                 error = copyout(&atv32, uap->tp, sizeof(atv32));
  988         }
  989         if (error == 0 && uap->tzp != NULL) {
  990                 rtz.tz_minuteswest = tz_minuteswest;
  991                 rtz.tz_dsttime = tz_dsttime;
  992                 error = copyout(&rtz, uap->tzp, sizeof(rtz));
  993         }
  994         return (error);
  995 }
  996 
  997 int
  998 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
  999 {
 1000         l_timeval atv32;
 1001         struct timeval atv, *tvp;
 1002         struct timezone atz, *tzp;
 1003         int error;
 1004 
 1005         if (uap->tp) {
 1006                 error = copyin(uap->tp, &atv32, sizeof(atv32));
 1007                 if (error)
 1008                         return (error);
 1009                 atv.tv_sec = atv32.tv_sec;
 1010                 atv.tv_usec = atv32.tv_usec;
 1011                 tvp = &atv;
 1012         } else
 1013                 tvp = NULL;
 1014         if (uap->tzp) {
 1015                 error = copyin(uap->tzp, &atz, sizeof(atz));
 1016                 if (error)
 1017                         return (error);
 1018                 tzp = &atz;
 1019         } else
 1020                 tzp = NULL;
 1021         return (kern_settimeofday(td, tvp, tzp));
 1022 }
 1023 
 1024 int
 1025 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
 1026 {
 1027         struct l_rusage s32;
 1028         struct rusage s;
 1029         int error;
 1030 
 1031         error = kern_getrusage(td, uap->who, &s);
 1032         if (error != 0)
 1033                 return (error);
 1034         if (uap->rusage != NULL) {
 1035                 s32.ru_utime.tv_sec = s.ru_utime.tv_sec;
 1036                 s32.ru_utime.tv_usec = s.ru_utime.tv_usec;
 1037                 s32.ru_stime.tv_sec = s.ru_stime.tv_sec;
 1038                 s32.ru_stime.tv_usec = s.ru_stime.tv_usec;
 1039                 s32.ru_maxrss = s.ru_maxrss;
 1040                 s32.ru_ixrss = s.ru_ixrss;
 1041                 s32.ru_idrss = s.ru_idrss;
 1042                 s32.ru_isrss = s.ru_isrss;
 1043                 s32.ru_minflt = s.ru_minflt;
 1044                 s32.ru_majflt = s.ru_majflt;
 1045                 s32.ru_nswap = s.ru_nswap;
 1046                 s32.ru_inblock = s.ru_inblock;
 1047                 s32.ru_oublock = s.ru_oublock;
 1048                 s32.ru_msgsnd = s.ru_msgsnd;
 1049                 s32.ru_msgrcv = s.ru_msgrcv;
 1050                 s32.ru_nsignals = s.ru_nsignals;
 1051                 s32.ru_nvcsw = s.ru_nvcsw;
 1052                 s32.ru_nivcsw = s.ru_nivcsw;
 1053                 error = copyout(&s32, uap->rusage, sizeof(s32));
 1054         }
 1055         return (error);
 1056 }
 1057 
 1058 int
 1059 linux_sched_rr_get_interval(struct thread *td,
 1060     struct linux_sched_rr_get_interval_args *uap)
 1061 {
 1062         struct timespec ts;
 1063         struct l_timespec ts32;
 1064         int error;
 1065 
 1066         error = kern_sched_rr_get_interval(td, uap->pid, &ts);
 1067         if (error != 0)
 1068                 return (error);
 1069         ts32.tv_sec = ts.tv_sec;
 1070         ts32.tv_nsec = ts.tv_nsec;
 1071         return (copyout(&ts32, uap->interval, sizeof(ts32)));
 1072 }
 1073 

Cache object: 6e4e46fe93063d31ec62bc0fbef0007b


[ 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.