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/i386/linux/linux_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) 2000 Marcel Moolenaar
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer
   10  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD: releng/10.2/sys/i386/linux/linux_machdep.c 283359 2015-05-24 07:32:02Z kib $");
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/capsicum.h>
   35 #include <sys/file.h>
   36 #include <sys/fcntl.h>
   37 #include <sys/imgact.h>
   38 #include <sys/lock.h>
   39 #include <sys/malloc.h>
   40 #include <sys/mman.h>
   41 #include <sys/mutex.h>
   42 #include <sys/sx.h>
   43 #include <sys/priv.h>
   44 #include <sys/proc.h>
   45 #include <sys/queue.h>
   46 #include <sys/resource.h>
   47 #include <sys/resourcevar.h>
   48 #include <sys/signalvar.h>
   49 #include <sys/syscallsubr.h>
   50 #include <sys/sysproto.h>
   51 #include <sys/unistd.h>
   52 #include <sys/wait.h>
   53 #include <sys/sched.h>
   54 
   55 #include <machine/frame.h>
   56 #include <machine/psl.h>
   57 #include <machine/segments.h>
   58 #include <machine/sysarch.h>
   59 
   60 #include <vm/vm.h>
   61 #include <vm/pmap.h>
   62 #include <vm/vm_map.h>
   63 
   64 #include <i386/linux/linux.h>
   65 #include <i386/linux/linux_proto.h>
   66 #include <compat/linux/linux_ipc.h>
   67 #include <compat/linux/linux_misc.h>
   68 #include <compat/linux/linux_signal.h>
   69 #include <compat/linux/linux_util.h>
   70 #include <compat/linux/linux_emul.h>
   71 
   72 #include <i386/include/pcb.h>                   /* needed for pcb definition in linux_set_thread_area */
   73 
   74 #include "opt_posix.h"
   75 
   76 extern struct sysentvec elf32_freebsd_sysvec;   /* defined in i386/i386/elf_machdep.c */
   77 
   78 struct l_descriptor {
   79         l_uint          entry_number;
   80         l_ulong         base_addr;
   81         l_uint          limit;
   82         l_uint          seg_32bit:1;
   83         l_uint          contents:2;
   84         l_uint          read_exec_only:1;
   85         l_uint          limit_in_pages:1;
   86         l_uint          seg_not_present:1;
   87         l_uint          useable:1;
   88 };
   89 
   90 struct l_old_select_argv {
   91         l_int           nfds;
   92         l_fd_set        *readfds;
   93         l_fd_set        *writefds;
   94         l_fd_set        *exceptfds;
   95         struct l_timeval        *timeout;
   96 };
   97 
   98 static int      linux_mmap_common(struct thread *td, l_uintptr_t addr,
   99                     l_size_t len, l_int prot, l_int flags, l_int fd,
  100                     l_loff_t pos);
  101 
  102 int
  103 linux_to_bsd_sigaltstack(int lsa)
  104 {
  105         int bsa = 0;
  106 
  107         if (lsa & LINUX_SS_DISABLE)
  108                 bsa |= SS_DISABLE;
  109         if (lsa & LINUX_SS_ONSTACK)
  110                 bsa |= SS_ONSTACK;
  111         return (bsa);
  112 }
  113 
  114 int
  115 bsd_to_linux_sigaltstack(int bsa)
  116 {
  117         int lsa = 0;
  118 
  119         if (bsa & SS_DISABLE)
  120                 lsa |= LINUX_SS_DISABLE;
  121         if (bsa & SS_ONSTACK)
  122                 lsa |= LINUX_SS_ONSTACK;
  123         return (lsa);
  124 }
  125 
  126 int
  127 linux_execve(struct thread *td, struct linux_execve_args *args)
  128 {
  129         struct image_args eargs;
  130         struct vmspace *oldvmspace;
  131         char *newpath;
  132         int error;
  133 
  134         LCONVPATHEXIST(td, args->path, &newpath);
  135 
  136 #ifdef DEBUG
  137         if (ldebug(execve))
  138                 printf(ARGS(execve, "%s"), newpath);
  139 #endif
  140 
  141         error = pre_execve(td, &oldvmspace);
  142         if (error != 0) {
  143                 free(newpath, M_TEMP);
  144                 return (error);
  145         }
  146         error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
  147             args->argp, args->envp);
  148         free(newpath, M_TEMP);
  149         if (error == 0)
  150                 error = kern_execve(td, &eargs, NULL);
  151         if (error == 0) {
  152                 /* linux process can exec fbsd one, dont attempt
  153                  * to create emuldata for such process using
  154                  * linux_proc_init, this leads to a panic on KASSERT
  155                  * because such process has p->p_emuldata == NULL
  156                  */
  157                 if (SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX)
  158                         error = linux_proc_init(td, 0, 0);
  159         }
  160         post_execve(td, error, oldvmspace);
  161         return (error);
  162 }
  163 
  164 struct l_ipc_kludge {
  165         struct l_msgbuf *msgp;
  166         l_long msgtyp;
  167 };
  168 
  169 int
  170 linux_ipc(struct thread *td, struct linux_ipc_args *args)
  171 {
  172 
  173         switch (args->what & 0xFFFF) {
  174         case LINUX_SEMOP: {
  175                 struct linux_semop_args a;
  176 
  177                 a.semid = args->arg1;
  178                 a.tsops = args->ptr;
  179                 a.nsops = args->arg2;
  180                 return (linux_semop(td, &a));
  181         }
  182         case LINUX_SEMGET: {
  183                 struct linux_semget_args a;
  184 
  185                 a.key = args->arg1;
  186                 a.nsems = args->arg2;
  187                 a.semflg = args->arg3;
  188                 return (linux_semget(td, &a));
  189         }
  190         case LINUX_SEMCTL: {
  191                 struct linux_semctl_args a;
  192                 int error;
  193 
  194                 a.semid = args->arg1;
  195                 a.semnum = args->arg2;
  196                 a.cmd = args->arg3;
  197                 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
  198                 if (error)
  199                         return (error);
  200                 return (linux_semctl(td, &a));
  201         }
  202         case LINUX_MSGSND: {
  203                 struct linux_msgsnd_args a;
  204 
  205                 a.msqid = args->arg1;
  206                 a.msgp = args->ptr;
  207                 a.msgsz = args->arg2;
  208                 a.msgflg = args->arg3;
  209                 return (linux_msgsnd(td, &a));
  210         }
  211         case LINUX_MSGRCV: {
  212                 struct linux_msgrcv_args a;
  213 
  214                 a.msqid = args->arg1;
  215                 a.msgsz = args->arg2;
  216                 a.msgflg = args->arg3;
  217                 if ((args->what >> 16) == 0) {
  218                         struct l_ipc_kludge tmp;
  219                         int error;
  220 
  221                         if (args->ptr == NULL)
  222                                 return (EINVAL);
  223                         error = copyin(args->ptr, &tmp, sizeof(tmp));
  224                         if (error)
  225                                 return (error);
  226                         a.msgp = tmp.msgp;
  227                         a.msgtyp = tmp.msgtyp;
  228                 } else {
  229                         a.msgp = args->ptr;
  230                         a.msgtyp = args->arg5;
  231                 }
  232                 return (linux_msgrcv(td, &a));
  233         }
  234         case LINUX_MSGGET: {
  235                 struct linux_msgget_args a;
  236 
  237                 a.key = args->arg1;
  238                 a.msgflg = args->arg2;
  239                 return (linux_msgget(td, &a));
  240         }
  241         case LINUX_MSGCTL: {
  242                 struct linux_msgctl_args a;
  243 
  244                 a.msqid = args->arg1;
  245                 a.cmd = args->arg2;
  246                 a.buf = args->ptr;
  247                 return (linux_msgctl(td, &a));
  248         }
  249         case LINUX_SHMAT: {
  250                 struct linux_shmat_args a;
  251 
  252                 a.shmid = args->arg1;
  253                 a.shmaddr = args->ptr;
  254                 a.shmflg = args->arg2;
  255                 a.raddr = (l_ulong *)args->arg3;
  256                 return (linux_shmat(td, &a));
  257         }
  258         case LINUX_SHMDT: {
  259                 struct linux_shmdt_args a;
  260 
  261                 a.shmaddr = args->ptr;
  262                 return (linux_shmdt(td, &a));
  263         }
  264         case LINUX_SHMGET: {
  265                 struct linux_shmget_args a;
  266 
  267                 a.key = args->arg1;
  268                 a.size = args->arg2;
  269                 a.shmflg = args->arg3;
  270                 return (linux_shmget(td, &a));
  271         }
  272         case LINUX_SHMCTL: {
  273                 struct linux_shmctl_args a;
  274 
  275                 a.shmid = args->arg1;
  276                 a.cmd = args->arg2;
  277                 a.buf = args->ptr;
  278                 return (linux_shmctl(td, &a));
  279         }
  280         default:
  281                 break;
  282         }
  283 
  284         return (EINVAL);
  285 }
  286 
  287 int
  288 linux_old_select(struct thread *td, struct linux_old_select_args *args)
  289 {
  290         struct l_old_select_argv linux_args;
  291         struct linux_select_args newsel;
  292         int error;
  293 
  294 #ifdef DEBUG
  295         if (ldebug(old_select))
  296                 printf(ARGS(old_select, "%p"), args->ptr);
  297 #endif
  298 
  299         error = copyin(args->ptr, &linux_args, sizeof(linux_args));
  300         if (error)
  301                 return (error);
  302 
  303         newsel.nfds = linux_args.nfds;
  304         newsel.readfds = linux_args.readfds;
  305         newsel.writefds = linux_args.writefds;
  306         newsel.exceptfds = linux_args.exceptfds;
  307         newsel.timeout = linux_args.timeout;
  308         return (linux_select(td, &newsel));
  309 }
  310 
  311 int
  312 linux_set_cloned_tls(struct thread *td, void *desc)
  313 {
  314         struct segment_descriptor sd;
  315         struct l_user_desc info;
  316         int idx, error;
  317         int a[2];
  318 
  319         error = copyin(desc, &info, sizeof(struct l_user_desc));
  320         if (error) {
  321                 printf(LMSG("copyin failed!"));
  322         } else {
  323                 idx = info.entry_number;
  324 
  325                 /* 
  326                  * looks like we're getting the idx we returned
  327                  * in the set_thread_area() syscall
  328                  */
  329                 if (idx != 6 && idx != 3) {
  330                         printf(LMSG("resetting idx!"));
  331                         idx = 3;
  332                 }
  333 
  334                 /* this doesnt happen in practice */
  335                 if (idx == 6) {
  336                         /* we might copy out the entry_number as 3 */
  337                         info.entry_number = 3;
  338                         error = copyout(&info, desc, sizeof(struct l_user_desc));
  339                         if (error)
  340                                 printf(LMSG("copyout failed!"));
  341                 }
  342 
  343                 a[0] = LINUX_LDT_entry_a(&info);
  344                 a[1] = LINUX_LDT_entry_b(&info);
  345 
  346                 memcpy(&sd, &a, sizeof(a));
  347 #ifdef DEBUG
  348                 if (ldebug(clone))
  349                         printf("Segment created in clone with "
  350                         "CLONE_SETTLS: lobase: %x, hibase: %x, "
  351                         "lolimit: %x, hilimit: %x, type: %i, "
  352                         "dpl: %i, p: %i, xx: %i, def32: %i, "
  353                         "gran: %i\n", sd.sd_lobase, sd.sd_hibase,
  354                         sd.sd_lolimit, sd.sd_hilimit, sd.sd_type,
  355                         sd.sd_dpl, sd.sd_p, sd.sd_xx,
  356                         sd.sd_def32, sd.sd_gran);
  357 #endif
  358 
  359                 /* set %gs */
  360                 td->td_pcb->pcb_gsd = sd;
  361                 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
  362         }
  363 
  364         return (error);
  365 }
  366 
  367 int
  368 linux_set_upcall_kse(struct thread *td, register_t stack)
  369 {
  370 
  371         td->td_frame->tf_esp = stack;
  372 
  373         return (0);
  374 }
  375 
  376 #define STACK_SIZE  (2 * 1024 * 1024)
  377 #define GUARD_SIZE  (4 * PAGE_SIZE)
  378 
  379 int
  380 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
  381 {
  382 
  383 #ifdef DEBUG
  384         if (ldebug(mmap2))
  385                 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
  386                     (void *)args->addr, args->len, args->prot,
  387                     args->flags, args->fd, args->pgoff);
  388 #endif
  389 
  390         return (linux_mmap_common(td, args->addr, args->len, args->prot,
  391                 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
  392                 PAGE_SIZE));
  393 }
  394 
  395 int
  396 linux_mmap(struct thread *td, struct linux_mmap_args *args)
  397 {
  398         int error;
  399         struct l_mmap_argv linux_args;
  400 
  401         error = copyin(args->ptr, &linux_args, sizeof(linux_args));
  402         if (error)
  403                 return (error);
  404 
  405 #ifdef DEBUG
  406         if (ldebug(mmap))
  407                 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
  408                     (void *)linux_args.addr, linux_args.len, linux_args.prot,
  409                     linux_args.flags, linux_args.fd, linux_args.pgoff);
  410 #endif
  411 
  412         return (linux_mmap_common(td, linux_args.addr, linux_args.len,
  413             linux_args.prot, linux_args.flags, linux_args.fd,
  414             (uint32_t)linux_args.pgoff));
  415 }
  416 
  417 static int
  418 linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
  419     l_int flags, l_int fd, l_loff_t pos)
  420 {
  421         struct proc *p = td->td_proc;
  422         struct mmap_args /* {
  423                 caddr_t addr;
  424                 size_t len;
  425                 int prot;
  426                 int flags;
  427                 int fd;
  428                 long pad;
  429                 off_t pos;
  430         } */ bsd_args;
  431         int error;
  432         struct file *fp;
  433         cap_rights_t rights;
  434 
  435         error = 0;
  436         bsd_args.flags = 0;
  437         fp = NULL;
  438 
  439         /*
  440          * Linux mmap(2):
  441          * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
  442          */
  443         if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
  444                 return (EINVAL);
  445 
  446         if (flags & LINUX_MAP_SHARED)
  447                 bsd_args.flags |= MAP_SHARED;
  448         if (flags & LINUX_MAP_PRIVATE)
  449                 bsd_args.flags |= MAP_PRIVATE;
  450         if (flags & LINUX_MAP_FIXED)
  451                 bsd_args.flags |= MAP_FIXED;
  452         if (flags & LINUX_MAP_ANON) {
  453                 /* Enforce pos to be on page boundary, then ignore. */
  454                 if ((pos & PAGE_MASK) != 0)
  455                         return (EINVAL);
  456                 pos = 0;
  457                 bsd_args.flags |= MAP_ANON;
  458         } else
  459                 bsd_args.flags |= MAP_NOSYNC;
  460         if (flags & LINUX_MAP_GROWSDOWN)
  461                 bsd_args.flags |= MAP_STACK;
  462 
  463         /*
  464          * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
  465          * on Linux/i386. We do this to ensure maximum compatibility.
  466          * Linux/ia64 does the same in i386 emulation mode.
  467          */
  468         bsd_args.prot = prot;
  469         if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
  470                 bsd_args.prot |= PROT_READ | PROT_EXEC;
  471 
  472         /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
  473         bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
  474         if (bsd_args.fd != -1) {
  475                 /*
  476                  * Linux follows Solaris mmap(2) description:
  477                  * The file descriptor fildes is opened with
  478                  * read permission, regardless of the
  479                  * protection options specified.
  480                  *
  481                  * Checking just CAP_MMAP is fine here, since the real work
  482                  * is done in the FreeBSD mmap().
  483                  */
  484 
  485                 error = fget(td, bsd_args.fd,
  486                     cap_rights_init(&rights, CAP_MMAP), &fp);
  487                 if (error != 0)
  488                         return (error);
  489                 if (fp->f_type != DTYPE_VNODE) {
  490                         fdrop(fp, td);
  491                         return (EINVAL);
  492                 }
  493 
  494                 /* Linux mmap() just fails for O_WRONLY files */
  495                 if (!(fp->f_flag & FREAD)) {
  496                         fdrop(fp, td);
  497                         return (EACCES);
  498                 }
  499 
  500                 fdrop(fp, td);
  501         }
  502 
  503         if (flags & LINUX_MAP_GROWSDOWN) {
  504                 /* 
  505                  * The Linux MAP_GROWSDOWN option does not limit auto
  506                  * growth of the region.  Linux mmap with this option
  507                  * takes as addr the inital BOS, and as len, the initial
  508                  * region size.  It can then grow down from addr without
  509                  * limit.  However, linux threads has an implicit internal
  510                  * limit to stack size of STACK_SIZE.  Its just not
  511                  * enforced explicitly in linux.  But, here we impose
  512                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
  513                  * region, since we can do this with our mmap.
  514                  *
  515                  * Our mmap with MAP_STACK takes addr as the maximum
  516                  * downsize limit on BOS, and as len the max size of
  517                  * the region.  It them maps the top SGROWSIZ bytes,
  518                  * and auto grows the region down, up to the limit
  519                  * in addr.
  520                  *
  521                  * If we don't use the MAP_STACK option, the effect
  522                  * of this code is to allocate a stack region of a
  523                  * fixed size of (STACK_SIZE - GUARD_SIZE).
  524                  */
  525 
  526                 if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
  527                         /* 
  528                          * Some linux apps will attempt to mmap
  529                          * thread stacks near the top of their
  530                          * address space.  If their TOS is greater
  531                          * than vm_maxsaddr, vm_map_growstack()
  532                          * will confuse the thread stack with the
  533                          * process stack and deliver a SEGV if they
  534                          * attempt to grow the thread stack past their
  535                          * current stacksize rlimit.  To avoid this,
  536                          * adjust vm_maxsaddr upwards to reflect
  537                          * the current stacksize rlimit rather
  538                          * than the maximum possible stacksize.
  539                          * It would be better to adjust the
  540                          * mmap'ed region, but some apps do not check
  541                          * mmap's return value.
  542                          */
  543                         PROC_LOCK(p);
  544                         p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
  545                             lim_cur(p, RLIMIT_STACK);
  546                         PROC_UNLOCK(p);
  547                 }
  548 
  549                 /*
  550                  * This gives us our maximum stack size and a new BOS.
  551                  * If we're using VM_STACK, then mmap will just map
  552                  * the top SGROWSIZ bytes, and let the stack grow down
  553                  * to the limit at BOS.  If we're not using VM_STACK
  554                  * we map the full stack, since we don't have a way
  555                  * to autogrow it.
  556                  */
  557                 if (len > STACK_SIZE - GUARD_SIZE) {
  558                         bsd_args.addr = (caddr_t)PTRIN(addr);
  559                         bsd_args.len = len;
  560                 } else {
  561                         bsd_args.addr = (caddr_t)PTRIN(addr) -
  562                             (STACK_SIZE - GUARD_SIZE - len);
  563                         bsd_args.len = STACK_SIZE - GUARD_SIZE;
  564                 }
  565         } else {
  566                 bsd_args.addr = (caddr_t)PTRIN(addr);
  567                 bsd_args.len  = len;
  568         }
  569         bsd_args.pos = pos;
  570 
  571 #ifdef DEBUG
  572         if (ldebug(mmap))
  573                 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
  574                     __func__,
  575                     (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
  576                     bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
  577 #endif
  578         error = sys_mmap(td, &bsd_args);
  579 #ifdef DEBUG
  580         if (ldebug(mmap))
  581                 printf("-> %s() return: 0x%x (0x%08x)\n",
  582                         __func__, error, (u_int)td->td_retval[0]);
  583 #endif
  584         return (error);
  585 }
  586 
  587 int
  588 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
  589 {
  590         struct mprotect_args bsd_args;
  591 
  592         bsd_args.addr = uap->addr;
  593         bsd_args.len = uap->len;
  594         bsd_args.prot = uap->prot;
  595         if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
  596                 bsd_args.prot |= PROT_READ | PROT_EXEC;
  597         return (sys_mprotect(td, &bsd_args));
  598 }
  599 
  600 int
  601 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
  602 {
  603         int error;
  604         struct i386_ioperm_args iia;
  605 
  606         iia.start = args->start;
  607         iia.length = args->length;
  608         iia.enable = args->enable;
  609         error = i386_set_ioperm(td, &iia);
  610         return (error);
  611 }
  612 
  613 int
  614 linux_iopl(struct thread *td, struct linux_iopl_args *args)
  615 {
  616         int error;
  617 
  618         if (args->level < 0 || args->level > 3)
  619                 return (EINVAL);
  620         if ((error = priv_check(td, PRIV_IO)) != 0)
  621                 return (error);
  622         if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
  623                 return (error);
  624         td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
  625             (args->level * (PSL_IOPL / 3));
  626         return (0);
  627 }
  628 
  629 int
  630 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
  631 {
  632         int error;
  633         struct i386_ldt_args ldt;
  634         struct l_descriptor ld;
  635         union descriptor desc;
  636         int size, written;
  637 
  638         switch (uap->func) {
  639         case 0x00: /* read_ldt */
  640                 ldt.start = 0;
  641                 ldt.descs = uap->ptr;
  642                 ldt.num = uap->bytecount / sizeof(union descriptor);
  643                 error = i386_get_ldt(td, &ldt);
  644                 td->td_retval[0] *= sizeof(union descriptor);
  645                 break;
  646         case 0x02: /* read_default_ldt = 0 */
  647                 size = 5*sizeof(struct l_desc_struct);
  648                 if (size > uap->bytecount)
  649                         size = uap->bytecount;
  650                 for (written = error = 0; written < size && error == 0; written++)
  651                         error = subyte((char *)uap->ptr + written, 0);
  652                 td->td_retval[0] = written;
  653                 break;
  654         case 0x01: /* write_ldt */
  655         case 0x11: /* write_ldt */
  656                 if (uap->bytecount != sizeof(ld))
  657                         return (EINVAL);
  658 
  659                 error = copyin(uap->ptr, &ld, sizeof(ld));
  660                 if (error)
  661                         return (error);
  662 
  663                 ldt.start = ld.entry_number;
  664                 ldt.descs = &desc;
  665                 ldt.num = 1;
  666                 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
  667                 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
  668                 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
  669                 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
  670                 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
  671                         (ld.contents << 2);
  672                 desc.sd.sd_dpl = 3;
  673                 desc.sd.sd_p = (ld.seg_not_present ^ 1);
  674                 desc.sd.sd_xx = 0;
  675                 desc.sd.sd_def32 = ld.seg_32bit;
  676                 desc.sd.sd_gran = ld.limit_in_pages;
  677                 error = i386_set_ldt(td, &ldt, &desc);
  678                 break;
  679         default:
  680                 error = ENOSYS;
  681                 break;
  682         }
  683 
  684         if (error == EOPNOTSUPP) {
  685                 printf("linux: modify_ldt needs kernel option USER_LDT\n");
  686                 error = ENOSYS;
  687         }
  688 
  689         return (error);
  690 }
  691 
  692 int
  693 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
  694 {
  695         l_osigaction_t osa;
  696         l_sigaction_t act, oact;
  697         int error;
  698 
  699 #ifdef DEBUG
  700         if (ldebug(sigaction))
  701                 printf(ARGS(sigaction, "%d, %p, %p"),
  702                     args->sig, (void *)args->nsa, (void *)args->osa);
  703 #endif
  704 
  705         if (args->nsa != NULL) {
  706                 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
  707                 if (error)
  708                         return (error);
  709                 act.lsa_handler = osa.lsa_handler;
  710                 act.lsa_flags = osa.lsa_flags;
  711                 act.lsa_restorer = osa.lsa_restorer;
  712                 LINUX_SIGEMPTYSET(act.lsa_mask);
  713                 act.lsa_mask.__bits[0] = osa.lsa_mask;
  714         }
  715 
  716         error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
  717             args->osa ? &oact : NULL);
  718 
  719         if (args->osa != NULL && !error) {
  720                 osa.lsa_handler = oact.lsa_handler;
  721                 osa.lsa_flags = oact.lsa_flags;
  722                 osa.lsa_restorer = oact.lsa_restorer;
  723                 osa.lsa_mask = oact.lsa_mask.__bits[0];
  724                 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
  725         }
  726 
  727         return (error);
  728 }
  729 
  730 /*
  731  * Linux has two extra args, restart and oldmask.  We dont use these,
  732  * but it seems that "restart" is actually a context pointer that
  733  * enables the signal to happen with a different register set.
  734  */
  735 int
  736 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
  737 {
  738         sigset_t sigmask;
  739         l_sigset_t mask;
  740 
  741 #ifdef DEBUG
  742         if (ldebug(sigsuspend))
  743                 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
  744 #endif
  745 
  746         LINUX_SIGEMPTYSET(mask);
  747         mask.__bits[0] = args->mask;
  748         linux_to_bsd_sigset(&mask, &sigmask);
  749         return (kern_sigsuspend(td, sigmask));
  750 }
  751 
  752 int
  753 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
  754 {
  755         l_sigset_t lmask;
  756         sigset_t sigmask;
  757         int error;
  758 
  759 #ifdef DEBUG
  760         if (ldebug(rt_sigsuspend))
  761                 printf(ARGS(rt_sigsuspend, "%p, %d"),
  762                     (void *)uap->newset, uap->sigsetsize);
  763 #endif
  764 
  765         if (uap->sigsetsize != sizeof(l_sigset_t))
  766                 return (EINVAL);
  767 
  768         error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
  769         if (error)
  770                 return (error);
  771 
  772         linux_to_bsd_sigset(&lmask, &sigmask);
  773         return (kern_sigsuspend(td, sigmask));
  774 }
  775 
  776 int
  777 linux_pause(struct thread *td, struct linux_pause_args *args)
  778 {
  779         struct proc *p = td->td_proc;
  780         sigset_t sigmask;
  781 
  782 #ifdef DEBUG
  783         if (ldebug(pause))
  784                 printf(ARGS(pause, ""));
  785 #endif
  786 
  787         PROC_LOCK(p);
  788         sigmask = td->td_sigmask;
  789         PROC_UNLOCK(p);
  790         return (kern_sigsuspend(td, sigmask));
  791 }
  792 
  793 int
  794 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
  795 {
  796         stack_t ss, oss;
  797         l_stack_t lss;
  798         int error;
  799 
  800 #ifdef DEBUG
  801         if (ldebug(sigaltstack))
  802                 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
  803 #endif
  804 
  805         if (uap->uss != NULL) {
  806                 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
  807                 if (error)
  808                         return (error);
  809 
  810                 ss.ss_sp = lss.ss_sp;
  811                 ss.ss_size = lss.ss_size;
  812                 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
  813         }
  814         error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
  815             (uap->uoss != NULL) ? &oss : NULL);
  816         if (!error && uap->uoss != NULL) {
  817                 lss.ss_sp = oss.ss_sp;
  818                 lss.ss_size = oss.ss_size;
  819                 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
  820                 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
  821         }
  822 
  823         return (error);
  824 }
  825 
  826 int
  827 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
  828 {
  829         struct ftruncate_args sa;
  830 
  831 #ifdef DEBUG
  832         if (ldebug(ftruncate64))
  833                 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
  834                     (intmax_t)args->length);
  835 #endif
  836 
  837         sa.fd = args->fd;
  838         sa.length = args->length;
  839         return sys_ftruncate(td, &sa);
  840 }
  841 
  842 int
  843 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
  844 {
  845         struct l_user_desc info;
  846         int error;
  847         int idx;
  848         int a[2];
  849         struct segment_descriptor sd;
  850 
  851         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
  852         if (error)
  853                 return (error);
  854 
  855 #ifdef DEBUG
  856         if (ldebug(set_thread_area))
  857                 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
  858                       info.entry_number,
  859                       info.base_addr,
  860                       info.limit,
  861                       info.seg_32bit,
  862                       info.contents,
  863                       info.read_exec_only,
  864                       info.limit_in_pages,
  865                       info.seg_not_present,
  866                       info.useable);
  867 #endif
  868 
  869         idx = info.entry_number;
  870         /* 
  871          * Semantics of linux version: every thread in the system has array of
  872          * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This 
  873          * syscall loads one of the selected tls decriptors with a value and
  874          * also loads GDT descriptors 6, 7 and 8 with the content of the
  875          * per-thread descriptors.
  876          *
  877          * Semantics of fbsd version: I think we can ignore that linux has 3 
  878          * per-thread descriptors and use just the 1st one. The tls_array[]
  879          * is used only in set/get-thread_area() syscalls and for loading the
  880          * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
  881          * we will load just one. 
  882          *
  883          * XXX: this doesn't work when a user space process tries to use more
  884          * than 1 TLS segment. Comment in the linux sources says wine might do
  885          * this.
  886          */
  887 
  888         /* 
  889          * we support just GLIBC TLS now 
  890          * we should let 3 proceed as well because we use this segment so
  891          * if code does two subsequent calls it should succeed
  892          */
  893         if (idx != 6 && idx != -1 && idx != 3)
  894                 return (EINVAL);
  895 
  896         /* 
  897          * we have to copy out the GDT entry we use
  898          * FreeBSD uses GDT entry #3 for storing %gs so load that
  899          *
  900          * XXX: what if a user space program doesn't check this value and tries
  901          * to use 6, 7 or 8? 
  902          */
  903         idx = info.entry_number = 3;
  904         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
  905         if (error)
  906                 return (error);
  907 
  908         if (LINUX_LDT_empty(&info)) {
  909                 a[0] = 0;
  910                 a[1] = 0;
  911         } else {
  912                 a[0] = LINUX_LDT_entry_a(&info);
  913                 a[1] = LINUX_LDT_entry_b(&info);
  914         }
  915 
  916         memcpy(&sd, &a, sizeof(a));
  917 #ifdef DEBUG
  918         if (ldebug(set_thread_area))
  919                 printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
  920                         sd.sd_hibase,
  921                         sd.sd_lolimit,
  922                         sd.sd_hilimit,
  923                         sd.sd_type,
  924                         sd.sd_dpl,
  925                         sd.sd_p,
  926                         sd.sd_xx,
  927                         sd.sd_def32,
  928                         sd.sd_gran);
  929 #endif
  930 
  931         /* this is taken from i386 version of cpu_set_user_tls() */
  932         critical_enter();
  933         /* set %gs */
  934         td->td_pcb->pcb_gsd = sd;
  935         PCPU_GET(fsgs_gdt)[1] = sd;
  936         load_gs(GSEL(GUGS_SEL, SEL_UPL));
  937         critical_exit();
  938    
  939         return (0);
  940 }
  941 
  942 int
  943 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
  944 {
  945         
  946         struct l_user_desc info;
  947         int error;
  948         int idx;
  949         struct l_desc_struct desc;
  950         struct segment_descriptor sd;
  951 
  952 #ifdef DEBUG
  953         if (ldebug(get_thread_area))
  954                 printf(ARGS(get_thread_area, "%p"), args->desc);
  955 #endif
  956 
  957         error = copyin(args->desc, &info, sizeof(struct l_user_desc));
  958         if (error)
  959                 return (error);
  960 
  961         idx = info.entry_number;
  962         /* XXX: I am not sure if we want 3 to be allowed too. */
  963         if (idx != 6 && idx != 3)
  964                 return (EINVAL);
  965 
  966         idx = 3;
  967 
  968         memset(&info, 0, sizeof(info));
  969 
  970         sd = PCPU_GET(fsgs_gdt)[1];
  971 
  972         memcpy(&desc, &sd, sizeof(desc));
  973 
  974         info.entry_number = idx;
  975         info.base_addr = LINUX_GET_BASE(&desc);
  976         info.limit = LINUX_GET_LIMIT(&desc);
  977         info.seg_32bit = LINUX_GET_32BIT(&desc);
  978         info.contents = LINUX_GET_CONTENTS(&desc);
  979         info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
  980         info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
  981         info.seg_not_present = !LINUX_GET_PRESENT(&desc);
  982         info.useable = LINUX_GET_USEABLE(&desc);
  983 
  984         error = copyout(&info, args->desc, sizeof(struct l_user_desc));
  985         if (error)
  986                 return (EFAULT);
  987 
  988         return (0);
  989 }
  990 
  991 /* XXX: this wont work with module - convert it */
  992 int
  993 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
  994 {
  995 #ifdef P1003_1B_MQUEUE
  996         return sys_kmq_open(td, (struct kmq_open_args *) args);
  997 #else
  998         return (ENOSYS);
  999 #endif
 1000 }
 1001 
 1002 int
 1003 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
 1004 {
 1005 #ifdef P1003_1B_MQUEUE
 1006         return sys_kmq_unlink(td, (struct kmq_unlink_args *) args);
 1007 #else
 1008         return (ENOSYS);
 1009 #endif
 1010 }
 1011 
 1012 int
 1013 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
 1014 {
 1015 #ifdef P1003_1B_MQUEUE
 1016         return sys_kmq_timedsend(td, (struct kmq_timedsend_args *) args);
 1017 #else
 1018         return (ENOSYS);
 1019 #endif
 1020 }
 1021 
 1022 int
 1023 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
 1024 {
 1025 #ifdef P1003_1B_MQUEUE
 1026         return sys_kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
 1027 #else
 1028         return (ENOSYS);
 1029 #endif
 1030 }
 1031 
 1032 int
 1033 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
 1034 {
 1035 #ifdef P1003_1B_MQUEUE
 1036         return sys_kmq_notify(td, (struct kmq_notify_args *) args);
 1037 #else
 1038         return (ENOSYS);
 1039 #endif
 1040 }
 1041 
 1042 int
 1043 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
 1044 {
 1045 #ifdef P1003_1B_MQUEUE
 1046         return sys_kmq_setattr(td, (struct kmq_setattr_args *) args);
 1047 #else
 1048         return (ENOSYS);
 1049 #endif
 1050 }
 1051 
 1052 int
 1053 linux_wait4(struct thread *td, struct linux_wait4_args *args)
 1054 {
 1055         int error, options;
 1056         struct rusage ru, *rup;
 1057 
 1058 #ifdef DEBUG
 1059         if (ldebug(wait4))
 1060                 printf(ARGS(wait4, "%d, %p, %d, %p"),
 1061                     args->pid, (void *)args->status, args->options,
 1062                     (void *)args->rusage);
 1063 #endif
 1064 
 1065         options = (args->options & (WNOHANG | WUNTRACED));
 1066         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
 1067         if (args->options & __WCLONE)
 1068                 options |= WLINUXCLONE;
 1069 
 1070         if (args->rusage != NULL)
 1071                 rup = &ru;
 1072         else
 1073                 rup = NULL;
 1074         error = linux_common_wait(td, args->pid, args->status, options, rup);
 1075         if (error)
 1076                 return (error);
 1077         if (args->rusage != NULL)
 1078                 error = copyout(&ru, args->rusage, sizeof(ru));
 1079 
 1080         return (error);
 1081 }

Cache object: 54d6754ad6b729c09bb1fd9fd4932dfa


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