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/compat/linux/linux_misc.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) 2002 Doug Rabson
    3  * Copyright (c) 1994-1995 Søren Schmidt
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer
   11  *    in this position and unchanged.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. The name of the author may not be used to endorse or promote products
   16  *    derived from this software without specific prior written permission
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #include "opt_compat.h"
   34 #include "opt_mac.h"
   35 
   36 #include <sys/param.h>
   37 #include <sys/blist.h>
   38 #include <sys/fcntl.h>
   39 #if defined(__i386__)
   40 #include <sys/imgact_aout.h>
   41 #endif
   42 #include <sys/jail.h>
   43 #include <sys/kernel.h>
   44 #include <sys/limits.h>
   45 #include <sys/lock.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mman.h>
   48 #include <sys/mount.h>
   49 #include <sys/mutex.h>
   50 #include <sys/namei.h>
   51 #include <sys/priv.h>
   52 #include <sys/proc.h>
   53 #include <sys/reboot.h>
   54 #include <sys/resourcevar.h>
   55 #include <sys/sched.h>
   56 #include <sys/signalvar.h>
   57 #include <sys/stat.h>
   58 #include <sys/syscallsubr.h>
   59 #include <sys/sysctl.h>
   60 #include <sys/sysproto.h>
   61 #include <sys/systm.h>
   62 #include <sys/time.h>
   63 #include <sys/vmmeter.h>
   64 #include <sys/vnode.h>
   65 #include <sys/wait.h>
   66 
   67 #include <security/mac/mac_framework.h>
   68 
   69 #include <vm/vm.h>
   70 #include <vm/pmap.h>
   71 #include <vm/vm_kern.h>
   72 #include <vm/vm_map.h>
   73 #include <vm/vm_extern.h>
   74 #include <vm/vm_object.h>
   75 #include <vm/swap_pager.h>
   76 
   77 #include <compat/linux/linux_sysproto.h>
   78 #include <compat/linux/linux_emul.h>
   79 #include <compat/linux/linux_misc.h>
   80 
   81 #ifdef COMPAT_LINUX32
   82 #include <machine/../linux32/linux.h>
   83 #include <machine/../linux32/linux32_proto.h>
   84 #else
   85 #include <machine/../linux/linux.h>
   86 #include <machine/../linux/linux_proto.h>
   87 #endif
   88 
   89 #include <compat/linux/linux_mib.h>
   90 #include <compat/linux/linux_signal.h>
   91 #include <compat/linux/linux_util.h>
   92 
   93 #ifdef __i386__
   94 #include <machine/cputypes.h>
   95 #endif
   96 
   97 #define BSD_TO_LINUX_SIGNAL(sig)        \
   98         (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
   99 
  100 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
  101         RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
  102         RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
  103         RLIMIT_MEMLOCK, RLIMIT_AS 
  104 };
  105 
  106 struct l_sysinfo {
  107         l_long          uptime;         /* Seconds since boot */
  108         l_ulong         loads[3];       /* 1, 5, and 15 minute load averages */
  109 #define LINUX_SYSINFO_LOADS_SCALE 65536
  110         l_ulong         totalram;       /* Total usable main memory size */
  111         l_ulong         freeram;        /* Available memory size */
  112         l_ulong         sharedram;      /* Amount of shared memory */
  113         l_ulong         bufferram;      /* Memory used by buffers */
  114         l_ulong         totalswap;      /* Total swap space size */
  115         l_ulong         freeswap;       /* swap space still available */
  116         l_ushort        procs;          /* Number of current processes */
  117         l_ushort        pads;
  118         l_ulong         totalbig;
  119         l_ulong         freebig;
  120         l_uint          mem_unit;
  121         char            _f[20-2*sizeof(l_long)-sizeof(l_int)];  /* padding */
  122 };
  123 int
  124 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
  125 {
  126         struct l_sysinfo sysinfo;
  127         vm_object_t object;
  128         int i, j;
  129         struct timespec ts;
  130 
  131         getnanouptime(&ts);
  132         if (ts.tv_nsec != 0)
  133                 ts.tv_sec++;
  134         sysinfo.uptime = ts.tv_sec;
  135 
  136         /* Use the information from the mib to get our load averages */
  137         for (i = 0; i < 3; i++)
  138                 sysinfo.loads[i] = averunnable.ldavg[i] *
  139                     LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
  140 
  141         sysinfo.totalram = physmem * PAGE_SIZE;
  142         sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
  143 
  144         sysinfo.sharedram = 0;
  145         mtx_lock(&vm_object_list_mtx);
  146         TAILQ_FOREACH(object, &vm_object_list, object_list)
  147                 if (object->shadow_count > 1)
  148                         sysinfo.sharedram += object->resident_page_count;
  149         mtx_unlock(&vm_object_list_mtx);
  150 
  151         sysinfo.sharedram *= PAGE_SIZE;
  152         sysinfo.bufferram = 0;
  153 
  154         swap_pager_status(&i, &j);
  155         sysinfo.totalswap = i * PAGE_SIZE;
  156         sysinfo.freeswap = (i - j) * PAGE_SIZE;
  157 
  158         sysinfo.procs = nprocs;
  159 
  160         /* The following are only present in newer Linux kernels. */
  161         sysinfo.totalbig = 0;
  162         sysinfo.freebig = 0;
  163         sysinfo.mem_unit = 1;
  164 
  165         return copyout(&sysinfo, args->info, sizeof(sysinfo));
  166 }
  167 
  168 int
  169 linux_alarm(struct thread *td, struct linux_alarm_args *args)
  170 {
  171         struct itimerval it, old_it;
  172         int error;
  173 
  174 #ifdef DEBUG
  175         if (ldebug(alarm))
  176                 printf(ARGS(alarm, "%u"), args->secs);
  177 #endif
  178 
  179         if (args->secs > 100000000)
  180                 return (EINVAL);
  181 
  182         it.it_value.tv_sec = (long)args->secs;
  183         it.it_value.tv_usec = 0;
  184         it.it_interval.tv_sec = 0;
  185         it.it_interval.tv_usec = 0;
  186         error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
  187         if (error)
  188                 return (error);
  189         if (timevalisset(&old_it.it_value)) {
  190                 if (old_it.it_value.tv_usec != 0)
  191                         old_it.it_value.tv_sec++;
  192                 td->td_retval[0] = old_it.it_value.tv_sec;
  193         }
  194         return (0);
  195 }
  196 
  197 int
  198 linux_brk(struct thread *td, struct linux_brk_args *args)
  199 {
  200         struct vmspace *vm = td->td_proc->p_vmspace;
  201         vm_offset_t new, old;
  202         struct obreak_args /* {
  203                 char * nsize;
  204         } */ tmp;
  205 
  206 #ifdef DEBUG
  207         if (ldebug(brk))
  208                 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
  209 #endif
  210         old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
  211         new = (vm_offset_t)args->dsend;
  212         tmp.nsize = (char *)new;
  213         if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
  214                 td->td_retval[0] = (long)new;
  215         else
  216                 td->td_retval[0] = (long)old;
  217 
  218         return 0;
  219 }
  220 
  221 #if defined(__i386__)
  222 /* XXX: what about amd64/linux32? */
  223 
  224 int
  225 linux_uselib(struct thread *td, struct linux_uselib_args *args)
  226 {
  227         struct nameidata ni;
  228         struct vnode *vp;
  229         struct exec *a_out;
  230         struct vattr attr;
  231         vm_offset_t vmaddr;
  232         unsigned long file_offset;
  233         vm_offset_t buffer;
  234         unsigned long bss_size;
  235         char *library;
  236         int error;
  237         int locked, vfslocked;
  238 
  239         LCONVPATHEXIST(td, args->library, &library);
  240 
  241 #ifdef DEBUG
  242         if (ldebug(uselib))
  243                 printf(ARGS(uselib, "%s"), library);
  244 #endif
  245 
  246         a_out = NULL;
  247         vfslocked = 0;
  248         locked = 0;
  249         vp = NULL;
  250 
  251         NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
  252             UIO_SYSSPACE, library, td);
  253         error = namei(&ni);
  254         LFREEPATH(library);
  255         if (error)
  256                 goto cleanup;
  257 
  258         vp = ni.ni_vp;
  259         vfslocked = NDHASGIANT(&ni);
  260         NDFREE(&ni, NDF_ONLY_PNBUF);
  261 
  262         /*
  263          * From here on down, we have a locked vnode that must be unlocked.
  264          * XXX: The code below largely duplicates exec_check_permissions().
  265          */
  266         locked = 1;
  267 
  268         /* Writable? */
  269         if (vp->v_writecount) {
  270                 error = ETXTBSY;
  271                 goto cleanup;
  272         }
  273 
  274         /* Executable? */
  275         error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
  276         if (error)
  277                 goto cleanup;
  278 
  279         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
  280             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
  281                 /* EACCESS is what exec(2) returns. */
  282                 error = ENOEXEC;
  283                 goto cleanup;
  284         }
  285 
  286         /* Sensible size? */
  287         if (attr.va_size == 0) {
  288                 error = ENOEXEC;
  289                 goto cleanup;
  290         }
  291 
  292         /* Can we access it? */
  293         error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
  294         if (error)
  295                 goto cleanup;
  296 
  297         /*
  298          * XXX: This should use vn_open() so that it is properly authorized,
  299          * and to reduce code redundancy all over the place here.
  300          * XXX: Not really, it duplicates far more of exec_check_permissions()
  301          * than vn_open().
  302          */
  303 #ifdef MAC
  304         error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
  305         if (error)
  306                 goto cleanup;
  307 #endif
  308         error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
  309         if (error)
  310                 goto cleanup;
  311 
  312         /* Pull in executable header into kernel_map */
  313         error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
  314             VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
  315         if (error)
  316                 goto cleanup;
  317 
  318         /* Is it a Linux binary ? */
  319         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
  320                 error = ENOEXEC;
  321                 goto cleanup;
  322         }
  323 
  324         /*
  325          * While we are here, we should REALLY do some more checks
  326          */
  327 
  328         /* Set file/virtual offset based on a.out variant. */
  329         switch ((int)(a_out->a_magic & 0xffff)) {
  330         case 0413:                      /* ZMAGIC */
  331                 file_offset = 1024;
  332                 break;
  333         case 0314:                      /* QMAGIC */
  334                 file_offset = 0;
  335                 break;
  336         default:
  337                 error = ENOEXEC;
  338                 goto cleanup;
  339         }
  340 
  341         bss_size = round_page(a_out->a_bss);
  342 
  343         /* Check various fields in header for validity/bounds. */
  344         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
  345                 error = ENOEXEC;
  346                 goto cleanup;
  347         }
  348 
  349         /* text + data can't exceed file size */
  350         if (a_out->a_data + a_out->a_text > attr.va_size) {
  351                 error = EFAULT;
  352                 goto cleanup;
  353         }
  354 
  355         /*
  356          * text/data/bss must not exceed limits
  357          * XXX - this is not complete. it should check current usage PLUS
  358          * the resources needed by this library.
  359          */
  360         PROC_LOCK(td->td_proc);
  361         if (a_out->a_text > maxtsiz ||
  362             a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
  363                 PROC_UNLOCK(td->td_proc);
  364                 error = ENOMEM;
  365                 goto cleanup;
  366         }
  367         PROC_UNLOCK(td->td_proc);
  368 
  369         /*
  370          * Prevent more writers.
  371          * XXX: Note that if any of the VM operations fail below we don't
  372          * clear this flag.
  373          */
  374         vp->v_vflag |= VV_TEXT;
  375 
  376         /*
  377          * Lock no longer needed
  378          */
  379         locked = 0;
  380         VOP_UNLOCK(vp, 0, td);
  381         VFS_UNLOCK_GIANT(vfslocked);
  382 
  383         /*
  384          * Check if file_offset page aligned. Currently we cannot handle
  385          * misalinged file offsets, and so we read in the entire image
  386          * (what a waste).
  387          */
  388         if (file_offset & PAGE_MASK) {
  389 #ifdef DEBUG
  390                 printf("uselib: Non page aligned binary %lu\n", file_offset);
  391 #endif
  392                 /* Map text+data read/write/execute */
  393 
  394                 /* a_entry is the load address and is page aligned */
  395                 vmaddr = trunc_page(a_out->a_entry);
  396 
  397                 /* get anon user mapping, read+write+execute */
  398                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
  399                     &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
  400                     VM_PROT_ALL, 0);
  401                 if (error)
  402                         goto cleanup;
  403 
  404                 /* map file into kernel_map */
  405                 error = vm_mmap(kernel_map, &buffer,
  406                     round_page(a_out->a_text + a_out->a_data + file_offset),
  407                     VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
  408                     trunc_page(file_offset));
  409                 if (error)
  410                         goto cleanup;
  411 
  412                 /* copy from kernel VM space to user space */
  413                 error = copyout(PTRIN(buffer + file_offset),
  414                     (void *)vmaddr, a_out->a_text + a_out->a_data);
  415 
  416                 /* release temporary kernel space */
  417                 vm_map_remove(kernel_map, buffer, buffer +
  418                     round_page(a_out->a_text + a_out->a_data + file_offset));
  419 
  420                 if (error)
  421                         goto cleanup;
  422         } else {
  423 #ifdef DEBUG
  424                 printf("uselib: Page aligned binary %lu\n", file_offset);
  425 #endif
  426                 /*
  427                  * for QMAGIC, a_entry is 20 bytes beyond the load address
  428                  * to skip the executable header
  429                  */
  430                 vmaddr = trunc_page(a_out->a_entry);
  431 
  432                 /*
  433                  * Map it all into the process's space as a single
  434                  * copy-on-write "data" segment.
  435                  */
  436                 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
  437                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
  438                     MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
  439                 if (error)
  440                         goto cleanup;
  441         }
  442 #ifdef DEBUG
  443         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
  444             ((long *)vmaddr)[1]);
  445 #endif
  446         if (bss_size != 0) {
  447                 /* Calculate BSS start address */
  448                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
  449                     a_out->a_data;
  450 
  451                 /* allocate some 'anon' space */
  452                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
  453                     &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
  454                 if (error)
  455                         goto cleanup;
  456         }
  457 
  458 cleanup:
  459         /* Unlock vnode if needed */
  460         if (locked) {
  461                 VOP_UNLOCK(vp, 0, td);
  462                 VFS_UNLOCK_GIANT(vfslocked);
  463         }
  464 
  465         /* Release the kernel mapping. */
  466         if (a_out)
  467                 vm_map_remove(kernel_map, (vm_offset_t)a_out,
  468                     (vm_offset_t)a_out + PAGE_SIZE);
  469 
  470         return error;
  471 }
  472 
  473 #endif  /* __i386__ */
  474 
  475 int
  476 linux_select(struct thread *td, struct linux_select_args *args)
  477 {
  478         l_timeval ltv;
  479         struct timeval tv0, tv1, utv, *tvp;
  480         int error;
  481 
  482 #ifdef DEBUG
  483         if (ldebug(select))
  484                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
  485                     (void *)args->readfds, (void *)args->writefds,
  486                     (void *)args->exceptfds, (void *)args->timeout);
  487 #endif
  488 
  489         /*
  490          * Store current time for computation of the amount of
  491          * time left.
  492          */
  493         if (args->timeout) {
  494                 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
  495                         goto select_out;
  496                 utv.tv_sec = ltv.tv_sec;
  497                 utv.tv_usec = ltv.tv_usec;
  498 #ifdef DEBUG
  499                 if (ldebug(select))
  500                         printf(LMSG("incoming timeout (%jd/%ld)"),
  501                             (intmax_t)utv.tv_sec, utv.tv_usec);
  502 #endif
  503 
  504                 if (itimerfix(&utv)) {
  505                         /*
  506                          * The timeval was invalid.  Convert it to something
  507                          * valid that will act as it does under Linux.
  508                          */
  509                         utv.tv_sec += utv.tv_usec / 1000000;
  510                         utv.tv_usec %= 1000000;
  511                         if (utv.tv_usec < 0) {
  512                                 utv.tv_sec -= 1;
  513                                 utv.tv_usec += 1000000;
  514                         }
  515                         if (utv.tv_sec < 0)
  516                                 timevalclear(&utv);
  517                 }
  518                 microtime(&tv0);
  519                 tvp = &utv;
  520         } else
  521                 tvp = NULL;
  522 
  523         error = kern_select(td, args->nfds, args->readfds, args->writefds,
  524             args->exceptfds, tvp);
  525 
  526 #ifdef DEBUG
  527         if (ldebug(select))
  528                 printf(LMSG("real select returns %d"), error);
  529 #endif
  530         if (error) {
  531                 /*
  532                  * See fs/select.c in the Linux kernel.  Without this,
  533                  * Maelstrom doesn't work.
  534                  */
  535                 if (error == ERESTART)
  536                         error = EINTR;
  537                 goto select_out;
  538         }
  539 
  540         if (args->timeout) {
  541                 if (td->td_retval[0]) {
  542                         /*
  543                          * Compute how much time was left of the timeout,
  544                          * by subtracting the current time and the time
  545                          * before we started the call, and subtracting
  546                          * that result from the user-supplied value.
  547                          */
  548                         microtime(&tv1);
  549                         timevalsub(&tv1, &tv0);
  550                         timevalsub(&utv, &tv1);
  551                         if (utv.tv_sec < 0)
  552                                 timevalclear(&utv);
  553                 } else
  554                         timevalclear(&utv);
  555 #ifdef DEBUG
  556                 if (ldebug(select))
  557                         printf(LMSG("outgoing timeout (%jd/%ld)"),
  558                             (intmax_t)utv.tv_sec, utv.tv_usec);
  559 #endif
  560                 ltv.tv_sec = utv.tv_sec;
  561                 ltv.tv_usec = utv.tv_usec;
  562                 if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
  563                         goto select_out;
  564         }
  565 
  566 select_out:
  567 #ifdef DEBUG
  568         if (ldebug(select))
  569                 printf(LMSG("select_out -> %d"), error);
  570 #endif
  571         return error;
  572 }
  573 
  574 int
  575 linux_mremap(struct thread *td, struct linux_mremap_args *args)
  576 {
  577         struct munmap_args /* {
  578                 void *addr;
  579                 size_t len;
  580         } */ bsd_args;
  581         int error = 0;
  582 
  583 #ifdef DEBUG
  584         if (ldebug(mremap))
  585                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
  586                     (void *)(uintptr_t)args->addr,
  587                     (unsigned long)args->old_len,
  588                     (unsigned long)args->new_len,
  589                     (unsigned long)args->flags);
  590 #endif
  591         args->new_len = round_page(args->new_len);
  592         args->old_len = round_page(args->old_len);
  593 
  594         if (args->new_len > args->old_len) {
  595                 td->td_retval[0] = 0;
  596                 return ENOMEM;
  597         }
  598 
  599         if (args->new_len < args->old_len) {
  600                 bsd_args.addr =
  601                     (caddr_t)((uintptr_t)args->addr + args->new_len);
  602                 bsd_args.len = args->old_len - args->new_len;
  603                 error = munmap(td, &bsd_args);
  604         }
  605 
  606         td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
  607         return error;
  608 }
  609 
  610 #define LINUX_MS_ASYNC       0x0001
  611 #define LINUX_MS_INVALIDATE  0x0002
  612 #define LINUX_MS_SYNC        0x0004
  613 
  614 int
  615 linux_msync(struct thread *td, struct linux_msync_args *args)
  616 {
  617         struct msync_args bsd_args;
  618 
  619         bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
  620         bsd_args.len = (uintptr_t)args->len;
  621         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
  622 
  623         return msync(td, &bsd_args);
  624 }
  625 
  626 int
  627 linux_time(struct thread *td, struct linux_time_args *args)
  628 {
  629         struct timeval tv;
  630         l_time_t tm;
  631         int error;
  632 
  633 #ifdef DEBUG
  634         if (ldebug(time))
  635                 printf(ARGS(time, "*"));
  636 #endif
  637 
  638         microtime(&tv);
  639         tm = tv.tv_sec;
  640         if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
  641                 return error;
  642         td->td_retval[0] = tm;
  643         return 0;
  644 }
  645 
  646 struct l_times_argv {
  647         l_long  tms_utime;
  648         l_long  tms_stime;
  649         l_long  tms_cutime;
  650         l_long  tms_cstime;
  651 };
  652 
  653 #define CLK_TCK 100                     /* Linux uses 100 */
  654 
  655 #define CONVTCK(r)      (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
  656 
  657 int
  658 linux_times(struct thread *td, struct linux_times_args *args)
  659 {
  660         struct timeval tv, utime, stime, cutime, cstime;
  661         struct l_times_argv tms;
  662         struct proc *p;
  663         int error;
  664 
  665 #ifdef DEBUG
  666         if (ldebug(times))
  667                 printf(ARGS(times, "*"));
  668 #endif
  669 
  670         if (args->buf != NULL) {
  671                 p = td->td_proc;
  672                 PROC_LOCK(p);
  673                 PROC_SLOCK(p);
  674                 calcru(p, &utime, &stime);
  675                 PROC_SUNLOCK(p);
  676                 calccru(p, &cutime, &cstime);
  677                 PROC_UNLOCK(p);
  678 
  679                 tms.tms_utime = CONVTCK(utime);
  680                 tms.tms_stime = CONVTCK(stime);
  681 
  682                 tms.tms_cutime = CONVTCK(cutime);
  683                 tms.tms_cstime = CONVTCK(cstime);
  684 
  685                 if ((error = copyout(&tms, args->buf, sizeof(tms))))
  686                         return error;
  687         }
  688 
  689         microuptime(&tv);
  690         td->td_retval[0] = (int)CONVTCK(tv);
  691         return 0;
  692 }
  693 
  694 int
  695 linux_newuname(struct thread *td, struct linux_newuname_args *args)
  696 {
  697         struct l_new_utsname utsname;
  698         char osname[LINUX_MAX_UTSNAME];
  699         char osrelease[LINUX_MAX_UTSNAME];
  700         char *p;
  701 
  702 #ifdef DEBUG
  703         if (ldebug(newuname))
  704                 printf(ARGS(newuname, "*"));
  705 #endif
  706 
  707         linux_get_osname(td, osname);
  708         linux_get_osrelease(td, osrelease);
  709 
  710         bzero(&utsname, sizeof(utsname));
  711         strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
  712         getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
  713         strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
  714         strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
  715         for (p = utsname.version; *p != '\0'; ++p)
  716                 if (*p == '\n') {
  717                         *p = '\0';
  718                         break;
  719                 }
  720 #ifdef __i386__
  721         {
  722                 const char *class;
  723 
  724                 switch (cpu_class) {
  725                 case CPUCLASS_686:
  726                         class = "i686";
  727                         break;
  728                 case CPUCLASS_586:
  729                         class = "i586";
  730                         break;
  731                 case CPUCLASS_486:
  732                         class = "i486";
  733                         break;
  734                 default:
  735                         class = "i386";
  736                 }
  737                 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
  738         }
  739 #elif defined(__amd64__)        /* XXX: Linux can change 'personality'. */
  740 #ifdef COMPAT_LINUX32
  741         strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
  742 #else
  743         strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
  744 #endif /* COMPAT_LINUX32 */
  745 #else /* something other than i386 or amd64 - assume we and Linux agree */
  746         strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
  747 #endif /* __i386__ */
  748         strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
  749 
  750         return (copyout(&utsname, args->buf, sizeof(utsname)));
  751 }
  752 
  753 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
  754 struct l_utimbuf {
  755         l_time_t l_actime;
  756         l_time_t l_modtime;
  757 };
  758 
  759 int
  760 linux_utime(struct thread *td, struct linux_utime_args *args)
  761 {
  762         struct timeval tv[2], *tvp;
  763         struct l_utimbuf lut;
  764         char *fname;
  765         int error;
  766 
  767         LCONVPATHEXIST(td, args->fname, &fname);
  768 
  769 #ifdef DEBUG
  770         if (ldebug(utime))
  771                 printf(ARGS(utime, "%s, *"), fname);
  772 #endif
  773 
  774         if (args->times) {
  775                 if ((error = copyin(args->times, &lut, sizeof lut))) {
  776                         LFREEPATH(fname);
  777                         return error;
  778                 }
  779                 tv[0].tv_sec = lut.l_actime;
  780                 tv[0].tv_usec = 0;
  781                 tv[1].tv_sec = lut.l_modtime;
  782                 tv[1].tv_usec = 0;
  783                 tvp = tv;
  784         } else
  785                 tvp = NULL;
  786 
  787         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
  788         LFREEPATH(fname);
  789         return (error);
  790 }
  791 
  792 int
  793 linux_utimes(struct thread *td, struct linux_utimes_args *args)
  794 {
  795         l_timeval ltv[2];
  796         struct timeval tv[2], *tvp = NULL;
  797         char *fname;
  798         int error;
  799 
  800         LCONVPATHEXIST(td, args->fname, &fname);
  801 
  802 #ifdef DEBUG
  803         if (ldebug(utimes))
  804                 printf(ARGS(utimes, "%s, *"), fname);
  805 #endif
  806 
  807         if (args->tptr != NULL) {
  808                 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
  809                         LFREEPATH(fname);
  810                         return (error);
  811                 }
  812                 tv[0].tv_sec = ltv[0].tv_sec;
  813                 tv[0].tv_usec = ltv[0].tv_usec;
  814                 tv[1].tv_sec = ltv[1].tv_sec;
  815                 tv[1].tv_usec = ltv[1].tv_usec;
  816                 tvp = tv;
  817         }
  818 
  819         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
  820         LFREEPATH(fname);
  821         return (error);
  822 }
  823 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
  824 
  825 #define __WCLONE 0x80000000
  826 
  827 int
  828 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
  829 {
  830         int error, options, tmpstat;
  831 
  832 #ifdef DEBUG
  833         if (ldebug(waitpid))
  834                 printf(ARGS(waitpid, "%d, %p, %d"),
  835                     args->pid, (void *)args->status, args->options);
  836 #endif
  837         /*
  838          * this is necessary because the test in kern_wait doesn't work
  839          * because we mess with the options here
  840          */
  841         if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
  842                 return (EINVAL);
  843 
  844         options = (args->options & (WNOHANG | WUNTRACED));
  845         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
  846         if (args->options & __WCLONE)
  847                 options |= WLINUXCLONE;
  848 
  849         error = kern_wait(td, args->pid, &tmpstat, options, NULL);
  850         if (error)
  851                 return error;
  852 
  853         if (args->status) {
  854                 tmpstat &= 0xffff;
  855                 if (WIFSIGNALED(tmpstat))
  856                         tmpstat = (tmpstat & 0xffffff80) |
  857                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
  858                 else if (WIFSTOPPED(tmpstat))
  859                         tmpstat = (tmpstat & 0xffff00ff) |
  860                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
  861                 return copyout(&tmpstat, args->status, sizeof(int));
  862         }
  863 
  864         return 0;
  865 }
  866 
  867 int
  868 linux_wait4(struct thread *td, struct linux_wait4_args *args)
  869 {
  870         int error, options, tmpstat;
  871         struct rusage ru, *rup;
  872         struct proc *p;
  873 
  874 #ifdef DEBUG
  875         if (ldebug(wait4))
  876                 printf(ARGS(wait4, "%d, %p, %d, %p"),
  877                     args->pid, (void *)args->status, args->options,
  878                     (void *)args->rusage);
  879 #endif
  880 
  881         options = (args->options & (WNOHANG | WUNTRACED));
  882         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
  883         if (args->options & __WCLONE)
  884                 options |= WLINUXCLONE;
  885 
  886         if (args->rusage != NULL)
  887                 rup = &ru;
  888         else
  889                 rup = NULL;
  890         error = kern_wait(td, args->pid, &tmpstat, options, rup);
  891         if (error)
  892                 return error;
  893 
  894         p = td->td_proc;
  895         PROC_LOCK(p);
  896         sigqueue_delete(&p->p_sigqueue, SIGCHLD);
  897         PROC_UNLOCK(p);
  898 
  899         if (args->status) {
  900                 tmpstat &= 0xffff;
  901                 if (WIFSIGNALED(tmpstat))
  902                         tmpstat = (tmpstat & 0xffffff80) |
  903                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
  904                 else if (WIFSTOPPED(tmpstat))
  905                         tmpstat = (tmpstat & 0xffff00ff) |
  906                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
  907                 error = copyout(&tmpstat, args->status, sizeof(int));
  908         }
  909         if (args->rusage != NULL && error == 0)
  910                 error = copyout(&ru, args->rusage, sizeof(ru));
  911 
  912         return (error);
  913 }
  914 
  915 int
  916 linux_mknod(struct thread *td, struct linux_mknod_args *args)
  917 {
  918         char *path;
  919         int error;
  920 
  921         LCONVPATHCREAT(td, args->path, &path);
  922 
  923 #ifdef DEBUG
  924         if (ldebug(mknod))
  925                 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
  926 #endif
  927 
  928         switch (args->mode & S_IFMT) {
  929         case S_IFIFO:
  930         case S_IFSOCK:
  931                 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
  932                 break;
  933 
  934         case S_IFCHR:
  935         case S_IFBLK:
  936                 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
  937                     args->dev);
  938                 break;
  939 
  940         case S_IFDIR:
  941                 error = EPERM;
  942                 break;
  943 
  944         case 0:
  945                 args->mode |= S_IFREG;
  946                 /* FALLTHROUGH */
  947         case S_IFREG:
  948                 error = kern_open(td, path, UIO_SYSSPACE,
  949                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
  950                 break;
  951 
  952         default:
  953                 error = EINVAL;
  954                 break;
  955         }
  956         LFREEPATH(path);
  957         return (error);
  958 }
  959 
  960 /*
  961  * UGH! This is just about the dumbest idea I've ever heard!!
  962  */
  963 int
  964 linux_personality(struct thread *td, struct linux_personality_args *args)
  965 {
  966 #ifdef DEBUG
  967         if (ldebug(personality))
  968                 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
  969 #endif
  970         if (args->per != 0)
  971                 return EINVAL;
  972 
  973         /* Yes Jim, it's still a Linux... */
  974         td->td_retval[0] = 0;
  975         return 0;
  976 }
  977 
  978 struct l_itimerval {
  979         l_timeval it_interval;
  980         l_timeval it_value;
  981 };
  982 
  983 #define B2L_ITIMERVAL(bip, lip)                                         \
  984         (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;          \
  985         (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;        \
  986         (bip)->it_value.tv_sec = (lip)->it_value.tv_sec;                \
  987         (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
  988 
  989 int
  990 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
  991 {
  992         int error;
  993         struct l_itimerval ls;
  994         struct itimerval aitv, oitv;
  995 
  996 #ifdef DEBUG
  997         if (ldebug(setitimer))
  998                 printf(ARGS(setitimer, "%p, %p"),
  999                     (void *)uap->itv, (void *)uap->oitv);
 1000 #endif
 1001 
 1002         if (uap->itv == NULL) {
 1003                 uap->itv = uap->oitv;
 1004                 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
 1005         }
 1006 
 1007         error = copyin(uap->itv, &ls, sizeof(ls));
 1008         if (error != 0)
 1009                 return (error);
 1010         B2L_ITIMERVAL(&aitv, &ls);
 1011 #ifdef DEBUG
 1012         if (ldebug(setitimer)) {
 1013                 printf("setitimer: value: sec: %jd, usec: %ld\n",
 1014                     (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
 1015                 printf("setitimer: interval: sec: %jd, usec: %ld\n",
 1016                     (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
 1017         }
 1018 #endif
 1019         error = kern_setitimer(td, uap->which, &aitv, &oitv);
 1020         if (error != 0 || uap->oitv == NULL)
 1021                 return (error);
 1022         B2L_ITIMERVAL(&ls, &oitv);
 1023 
 1024         return (copyout(&ls, uap->oitv, sizeof(ls)));
 1025 }
 1026 
 1027 int
 1028 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
 1029 {
 1030         int error;
 1031         struct l_itimerval ls;
 1032         struct itimerval aitv;
 1033 
 1034 #ifdef DEBUG
 1035         if (ldebug(getitimer))
 1036                 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
 1037 #endif
 1038         error = kern_getitimer(td, uap->which, &aitv);
 1039         if (error != 0)
 1040                 return (error);
 1041         B2L_ITIMERVAL(&ls, &aitv);
 1042         return (copyout(&ls, uap->itv, sizeof(ls)));
 1043 }
 1044 
 1045 int
 1046 linux_nice(struct thread *td, struct linux_nice_args *args)
 1047 {
 1048         struct setpriority_args bsd_args;
 1049 
 1050         bsd_args.which = PRIO_PROCESS;
 1051         bsd_args.who = 0;               /* current process */
 1052         bsd_args.prio = args->inc;
 1053         return setpriority(td, &bsd_args);
 1054 }
 1055 
 1056 int
 1057 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
 1058 {
 1059         struct ucred *newcred, *oldcred;
 1060         l_gid_t linux_gidset[NGROUPS];
 1061         gid_t *bsd_gidset;
 1062         int ngrp, error;
 1063         struct proc *p;
 1064 
 1065         ngrp = args->gidsetsize;
 1066         if (ngrp < 0 || ngrp >= NGROUPS)
 1067                 return (EINVAL);
 1068         error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
 1069         if (error)
 1070                 return (error);
 1071         newcred = crget();
 1072         p = td->td_proc;
 1073         PROC_LOCK(p);
 1074         oldcred = p->p_ucred;
 1075 
 1076         /*
 1077          * cr_groups[0] holds egid. Setting the whole set from
 1078          * the supplied set will cause egid to be changed too.
 1079          * Keep cr_groups[0] unchanged to prevent that.
 1080          */
 1081 
 1082         if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
 1083                 PROC_UNLOCK(p);
 1084                 crfree(newcred);
 1085                 return (error);
 1086         }
 1087 
 1088         crcopy(newcred, oldcred);
 1089         if (ngrp > 0) {
 1090                 newcred->cr_ngroups = ngrp + 1;
 1091 
 1092                 bsd_gidset = newcred->cr_groups;
 1093                 ngrp--;
 1094                 while (ngrp >= 0) {
 1095                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
 1096                         ngrp--;
 1097                 }
 1098         } else
 1099                 newcred->cr_ngroups = 1;
 1100 
 1101         setsugid(p);
 1102         p->p_ucred = newcred;
 1103         PROC_UNLOCK(p);
 1104         crfree(oldcred);
 1105         return (0);
 1106 }
 1107 
 1108 int
 1109 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
 1110 {
 1111         struct ucred *cred;
 1112         l_gid_t linux_gidset[NGROUPS];
 1113         gid_t *bsd_gidset;
 1114         int bsd_gidsetsz, ngrp, error;
 1115 
 1116         cred = td->td_ucred;
 1117         bsd_gidset = cred->cr_groups;
 1118         bsd_gidsetsz = cred->cr_ngroups - 1;
 1119 
 1120         /*
 1121          * cr_groups[0] holds egid. Returning the whole set
 1122          * here will cause a duplicate. Exclude cr_groups[0]
 1123          * to prevent that.
 1124          */
 1125 
 1126         if ((ngrp = args->gidsetsize) == 0) {
 1127                 td->td_retval[0] = bsd_gidsetsz;
 1128                 return (0);
 1129         }
 1130 
 1131         if (ngrp < bsd_gidsetsz)
 1132                 return (EINVAL);
 1133 
 1134         ngrp = 0;
 1135         while (ngrp < bsd_gidsetsz) {
 1136                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
 1137                 ngrp++;
 1138         }
 1139 
 1140         if ((error = copyout(linux_gidset, args->grouplist,
 1141             ngrp * sizeof(l_gid_t))))
 1142                 return (error);
 1143 
 1144         td->td_retval[0] = ngrp;
 1145         return (0);
 1146 }
 1147 
 1148 int
 1149 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
 1150 {
 1151         struct rlimit bsd_rlim;
 1152         struct l_rlimit rlim;
 1153         u_int which;
 1154         int error;
 1155 
 1156 #ifdef DEBUG
 1157         if (ldebug(setrlimit))
 1158                 printf(ARGS(setrlimit, "%d, %p"),
 1159                     args->resource, (void *)args->rlim);
 1160 #endif
 1161 
 1162         if (args->resource >= LINUX_RLIM_NLIMITS)
 1163                 return (EINVAL);
 1164 
 1165         which = linux_to_bsd_resource[args->resource];
 1166         if (which == -1)
 1167                 return (EINVAL);
 1168 
 1169         error = copyin(args->rlim, &rlim, sizeof(rlim));
 1170         if (error)
 1171                 return (error);
 1172 
 1173         bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
 1174         bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
 1175         return (kern_setrlimit(td, which, &bsd_rlim));
 1176 }
 1177 
 1178 int
 1179 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
 1180 {
 1181         struct l_rlimit rlim;
 1182         struct proc *p = td->td_proc;
 1183         struct rlimit bsd_rlim;
 1184         u_int which;
 1185 
 1186 #ifdef DEBUG
 1187         if (ldebug(old_getrlimit))
 1188                 printf(ARGS(old_getrlimit, "%d, %p"),
 1189                     args->resource, (void *)args->rlim);
 1190 #endif
 1191 
 1192         if (args->resource >= LINUX_RLIM_NLIMITS)
 1193                 return (EINVAL);
 1194 
 1195         which = linux_to_bsd_resource[args->resource];
 1196         if (which == -1)
 1197                 return (EINVAL);
 1198 
 1199         PROC_LOCK(p);
 1200         lim_rlimit(p, which, &bsd_rlim);
 1201         PROC_UNLOCK(p);
 1202 
 1203 #ifdef COMPAT_LINUX32
 1204         rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
 1205         if (rlim.rlim_cur == UINT_MAX)
 1206                 rlim.rlim_cur = INT_MAX;
 1207         rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
 1208         if (rlim.rlim_max == UINT_MAX)
 1209                 rlim.rlim_max = INT_MAX;
 1210 #else
 1211         rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
 1212         if (rlim.rlim_cur == ULONG_MAX)
 1213                 rlim.rlim_cur = LONG_MAX;
 1214         rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
 1215         if (rlim.rlim_max == ULONG_MAX)
 1216                 rlim.rlim_max = LONG_MAX;
 1217 #endif
 1218         return (copyout(&rlim, args->rlim, sizeof(rlim)));
 1219 }
 1220 
 1221 int
 1222 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
 1223 {
 1224         struct l_rlimit rlim;
 1225         struct proc *p = td->td_proc;
 1226         struct rlimit bsd_rlim;
 1227         u_int which;
 1228 
 1229 #ifdef DEBUG
 1230         if (ldebug(getrlimit))
 1231                 printf(ARGS(getrlimit, "%d, %p"),
 1232                     args->resource, (void *)args->rlim);
 1233 #endif
 1234 
 1235         if (args->resource >= LINUX_RLIM_NLIMITS)
 1236                 return (EINVAL);
 1237 
 1238         which = linux_to_bsd_resource[args->resource];
 1239         if (which == -1)
 1240                 return (EINVAL);
 1241 
 1242         PROC_LOCK(p);
 1243         lim_rlimit(p, which, &bsd_rlim);
 1244         PROC_UNLOCK(p);
 1245 
 1246         rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
 1247         rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
 1248         return (copyout(&rlim, args->rlim, sizeof(rlim)));
 1249 }
 1250 
 1251 int
 1252 linux_sched_setscheduler(struct thread *td,
 1253     struct linux_sched_setscheduler_args *args)
 1254 {
 1255         struct sched_setscheduler_args bsd;
 1256 
 1257 #ifdef DEBUG
 1258         if (ldebug(sched_setscheduler))
 1259                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
 1260                     args->pid, args->policy, (const void *)args->param);
 1261 #endif
 1262 
 1263         switch (args->policy) {
 1264         case LINUX_SCHED_OTHER:
 1265                 bsd.policy = SCHED_OTHER;
 1266                 break;
 1267         case LINUX_SCHED_FIFO:
 1268                 bsd.policy = SCHED_FIFO;
 1269                 break;
 1270         case LINUX_SCHED_RR:
 1271                 bsd.policy = SCHED_RR;
 1272                 break;
 1273         default:
 1274                 return EINVAL;
 1275         }
 1276 
 1277         bsd.pid = args->pid;
 1278         bsd.param = (struct sched_param *)args->param;
 1279         return sched_setscheduler(td, &bsd);
 1280 }
 1281 
 1282 int
 1283 linux_sched_getscheduler(struct thread *td,
 1284     struct linux_sched_getscheduler_args *args)
 1285 {
 1286         struct sched_getscheduler_args bsd;
 1287         int error;
 1288 
 1289 #ifdef DEBUG
 1290         if (ldebug(sched_getscheduler))
 1291                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
 1292 #endif
 1293 
 1294         bsd.pid = args->pid;
 1295         error = sched_getscheduler(td, &bsd);
 1296 
 1297         switch (td->td_retval[0]) {
 1298         case SCHED_OTHER:
 1299                 td->td_retval[0] = LINUX_SCHED_OTHER;
 1300                 break;
 1301         case SCHED_FIFO:
 1302                 td->td_retval[0] = LINUX_SCHED_FIFO;
 1303                 break;
 1304         case SCHED_RR:
 1305                 td->td_retval[0] = LINUX_SCHED_RR;
 1306                 break;
 1307         }
 1308 
 1309         return error;
 1310 }
 1311 
 1312 int
 1313 linux_sched_get_priority_max(struct thread *td,
 1314     struct linux_sched_get_priority_max_args *args)
 1315 {
 1316         struct sched_get_priority_max_args bsd;
 1317 
 1318 #ifdef DEBUG
 1319         if (ldebug(sched_get_priority_max))
 1320                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
 1321 #endif
 1322 
 1323         switch (args->policy) {
 1324         case LINUX_SCHED_OTHER:
 1325                 bsd.policy = SCHED_OTHER;
 1326                 break;
 1327         case LINUX_SCHED_FIFO:
 1328                 bsd.policy = SCHED_FIFO;
 1329                 break;
 1330         case LINUX_SCHED_RR:
 1331                 bsd.policy = SCHED_RR;
 1332                 break;
 1333         default:
 1334                 return EINVAL;
 1335         }
 1336         return sched_get_priority_max(td, &bsd);
 1337 }
 1338 
 1339 int
 1340 linux_sched_get_priority_min(struct thread *td,
 1341     struct linux_sched_get_priority_min_args *args)
 1342 {
 1343         struct sched_get_priority_min_args bsd;
 1344 
 1345 #ifdef DEBUG
 1346         if (ldebug(sched_get_priority_min))
 1347                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
 1348 #endif
 1349 
 1350         switch (args->policy) {
 1351         case LINUX_SCHED_OTHER:
 1352                 bsd.policy = SCHED_OTHER;
 1353                 break;
 1354         case LINUX_SCHED_FIFO:
 1355                 bsd.policy = SCHED_FIFO;
 1356                 break;
 1357         case LINUX_SCHED_RR:
 1358                 bsd.policy = SCHED_RR;
 1359                 break;
 1360         default:
 1361                 return EINVAL;
 1362         }
 1363         return sched_get_priority_min(td, &bsd);
 1364 }
 1365 
 1366 #define REBOOT_CAD_ON   0x89abcdef
 1367 #define REBOOT_CAD_OFF  0
 1368 #define REBOOT_HALT     0xcdef0123
 1369 #define REBOOT_RESTART  0x01234567
 1370 #define REBOOT_RESTART2 0xA1B2C3D4
 1371 #define REBOOT_POWEROFF 0x4321FEDC
 1372 #define REBOOT_MAGIC1   0xfee1dead
 1373 #define REBOOT_MAGIC2   0x28121969
 1374 #define REBOOT_MAGIC2A  0x05121996
 1375 #define REBOOT_MAGIC2B  0x16041998
 1376 
 1377 int
 1378 linux_reboot(struct thread *td, struct linux_reboot_args *args)
 1379 {
 1380         struct reboot_args bsd_args;
 1381 
 1382 #ifdef DEBUG
 1383         if (ldebug(reboot))
 1384                 printf(ARGS(reboot, "0x%x"), args->cmd);
 1385 #endif
 1386 
 1387         if (args->magic1 != REBOOT_MAGIC1)
 1388                 return EINVAL;
 1389 
 1390         switch (args->magic2) {
 1391         case REBOOT_MAGIC2:
 1392         case REBOOT_MAGIC2A:
 1393         case REBOOT_MAGIC2B:
 1394                 break;
 1395         default:
 1396                 return EINVAL;
 1397         }
 1398 
 1399         switch (args->cmd) {
 1400         case REBOOT_CAD_ON:
 1401         case REBOOT_CAD_OFF:
 1402                 return (priv_check(td, PRIV_REBOOT));
 1403         case REBOOT_HALT:
 1404                 bsd_args.opt = RB_HALT;
 1405                 break;
 1406         case REBOOT_RESTART:
 1407         case REBOOT_RESTART2:
 1408                 bsd_args.opt = 0;
 1409                 break;
 1410         case REBOOT_POWEROFF:
 1411                 bsd_args.opt = RB_POWEROFF;
 1412                 break;
 1413         default:
 1414                 return EINVAL;
 1415         }
 1416         return reboot(td, &bsd_args);
 1417 }
 1418 
 1419 
 1420 /*
 1421  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
 1422  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
 1423  * are assumed to be preserved. The following lightweight syscalls fixes
 1424  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
 1425  *
 1426  * linux_getpid() - MP SAFE
 1427  * linux_getgid() - MP SAFE
 1428  * linux_getuid() - MP SAFE
 1429  */
 1430 
 1431 int
 1432 linux_getpid(struct thread *td, struct linux_getpid_args *args)
 1433 {
 1434         struct linux_emuldata *em;
 1435 
 1436 #ifdef DEBUG
 1437         if (ldebug(getpid))
 1438                 printf(ARGS(getpid, ""));
 1439 #endif
 1440 
 1441         if (linux_use26(td)) {
 1442                 em = em_find(td->td_proc, EMUL_DONTLOCK);
 1443                 KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
 1444                 td->td_retval[0] = em->shared->group_pid;
 1445         } else {
 1446                 td->td_retval[0] = td->td_proc->p_pid;
 1447         }
 1448 
 1449         return (0);
 1450 }
 1451 
 1452 int
 1453 linux_gettid(struct thread *td, struct linux_gettid_args *args)
 1454 {
 1455 
 1456 #ifdef DEBUG
 1457         if (ldebug(gettid))
 1458                 printf(ARGS(gettid, ""));
 1459 #endif
 1460 
 1461         td->td_retval[0] = td->td_proc->p_pid;
 1462         return (0);
 1463 }
 1464 
 1465 
 1466 int
 1467 linux_getppid(struct thread *td, struct linux_getppid_args *args)
 1468 {
 1469         struct linux_emuldata *em;
 1470         struct proc *p, *pp;
 1471 
 1472 #ifdef DEBUG
 1473         if (ldebug(getppid))
 1474                 printf(ARGS(getppid, ""));
 1475 #endif
 1476 
 1477         if (!linux_use26(td)) {
 1478                 PROC_LOCK(td->td_proc);
 1479                 td->td_retval[0] = td->td_proc->p_pptr->p_pid;
 1480                 PROC_UNLOCK(td->td_proc);
 1481                 return (0);
 1482         }
 1483 
 1484         em = em_find(td->td_proc, EMUL_DONTLOCK);
 1485 
 1486         KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
 1487 
 1488         /* find the group leader */
 1489         p = pfind(em->shared->group_pid);
 1490 
 1491         if (p == NULL) {
 1492 #ifdef DEBUG
 1493                 printf(LMSG("parent process not found.\n"));
 1494 #endif
 1495                 return (0);
 1496         }
 1497 
 1498         pp = p->p_pptr;         /* switch to parent */
 1499         PROC_LOCK(pp);
 1500         PROC_UNLOCK(p);
 1501 
 1502         /* if its also linux process */
 1503         if (pp->p_sysent == &elf_linux_sysvec) {
 1504                 em = em_find(pp, EMUL_DONTLOCK);
 1505                 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
 1506 
 1507                 td->td_retval[0] = em->shared->group_pid;
 1508         } else
 1509                 td->td_retval[0] = pp->p_pid;
 1510 
 1511         PROC_UNLOCK(pp);
 1512 
 1513         return (0);
 1514 }
 1515 
 1516 int
 1517 linux_getgid(struct thread *td, struct linux_getgid_args *args)
 1518 {
 1519 
 1520 #ifdef DEBUG
 1521         if (ldebug(getgid))
 1522                 printf(ARGS(getgid, ""));
 1523 #endif
 1524 
 1525         td->td_retval[0] = td->td_ucred->cr_rgid;
 1526         return (0);
 1527 }
 1528 
 1529 int
 1530 linux_getuid(struct thread *td, struct linux_getuid_args *args)
 1531 {
 1532 
 1533 #ifdef DEBUG
 1534         if (ldebug(getuid))
 1535                 printf(ARGS(getuid, ""));
 1536 #endif
 1537 
 1538         td->td_retval[0] = td->td_ucred->cr_ruid;
 1539         return (0);
 1540 }
 1541 
 1542 
 1543 int
 1544 linux_getsid(struct thread *td, struct linux_getsid_args *args)
 1545 {
 1546         struct getsid_args bsd;
 1547 
 1548 #ifdef DEBUG
 1549         if (ldebug(getsid))
 1550                 printf(ARGS(getsid, "%i"), args->pid);
 1551 #endif
 1552 
 1553         bsd.pid = args->pid;
 1554         return getsid(td, &bsd);
 1555 }
 1556 
 1557 int
 1558 linux_nosys(struct thread *td, struct nosys_args *ignore)
 1559 {
 1560 
 1561         return (ENOSYS);
 1562 }
 1563 
 1564 int
 1565 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
 1566 {
 1567         struct getpriority_args bsd_args;
 1568         int error;
 1569 
 1570 #ifdef DEBUG
 1571         if (ldebug(getpriority))
 1572                 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
 1573 #endif
 1574 
 1575         bsd_args.which = args->which;
 1576         bsd_args.who = args->who;
 1577         error = getpriority(td, &bsd_args);
 1578         td->td_retval[0] = 20 - td->td_retval[0];
 1579         return error;
 1580 }
 1581 
 1582 int
 1583 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
 1584 {
 1585         int name[2];
 1586 
 1587 #ifdef DEBUG
 1588         if (ldebug(sethostname))
 1589                 printf(ARGS(sethostname, "*, %i"), args->len);
 1590 #endif
 1591 
 1592         name[0] = CTL_KERN;
 1593         name[1] = KERN_HOSTNAME;
 1594         return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
 1595             args->len, 0, 0));
 1596 }
 1597 
 1598 int
 1599 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
 1600 {
 1601         struct linux_emuldata *em, *td_em, *tmp_em;
 1602         struct proc *sp;
 1603 
 1604 #ifdef DEBUG
 1605         if (ldebug(exit_group))
 1606                 printf(ARGS(exit_group, "%i"), args->error_code);
 1607 #endif
 1608 
 1609         if (linux_use26(td)) {
 1610                 td_em = em_find(td->td_proc, EMUL_DONTLOCK);
 1611 
 1612                 KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
 1613 
 1614                 EMUL_SHARED_RLOCK(&emul_shared_lock);
 1615                 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
 1616                         if (em->pid == td_em->pid)
 1617                                 continue;
 1618 
 1619                         sp = pfind(em->pid);
 1620                         psignal(sp, SIGKILL);
 1621                         PROC_UNLOCK(sp);
 1622 #ifdef DEBUG
 1623                         printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
 1624 #endif
 1625                 }
 1626 
 1627                 EMUL_SHARED_RUNLOCK(&emul_shared_lock);
 1628         }
 1629         /*
 1630          * XXX: we should send a signal to the parent if
 1631          * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
 1632          * as it doesnt occur often.
 1633          */
 1634         exit1(td, W_EXITCODE(args->error_code, 0));
 1635 
 1636         return (0);
 1637 }
 1638 
 1639 int
 1640 linux_prctl(struct thread *td, struct linux_prctl_args *args)
 1641 {
 1642         int error = 0, max_size;
 1643         struct proc *p = td->td_proc;
 1644         char comm[LINUX_MAX_COMM_LEN];
 1645         struct linux_emuldata *em;
 1646         int pdeath_signal;
 1647 
 1648 #ifdef DEBUG
 1649         if (ldebug(prctl))
 1650                 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
 1651                     args->arg2, args->arg3, args->arg4, args->arg5);
 1652 #endif
 1653 
 1654         switch (args->option) {
 1655         case LINUX_PR_SET_PDEATHSIG:
 1656                 if (!LINUX_SIG_VALID(args->arg2))
 1657                         return (EINVAL);
 1658                 em = em_find(p, EMUL_DOLOCK);
 1659                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
 1660                 em->pdeath_signal = args->arg2;
 1661                 EMUL_UNLOCK(&emul_lock);
 1662                 break;
 1663         case LINUX_PR_GET_PDEATHSIG:
 1664                 em = em_find(p, EMUL_DOLOCK);
 1665                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
 1666                 pdeath_signal = em->pdeath_signal;
 1667                 EMUL_UNLOCK(&emul_lock);
 1668                 error = copyout(&pdeath_signal,
 1669                     (void *)(register_t)args->arg2,
 1670                     sizeof(pdeath_signal));
 1671                 break;
 1672         case LINUX_PR_SET_NAME:
 1673                 /*
 1674                  * To be on the safe side we need to make sure to not
 1675                  * overflow the size a linux program expects. We already
 1676                  * do this here in the copyin, so that we don't need to
 1677                  * check on copyout.
 1678                  */
 1679                 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
 1680                 error = copyinstr((void *)(register_t)args->arg2, comm,
 1681                     max_size, NULL);
 1682 
 1683                 /* Linux silently truncates the name if it is too long. */
 1684                 if (error == ENAMETOOLONG) {
 1685                         /*
 1686                          * XXX: copyinstr() isn't documented to populate the
 1687                          * array completely, so do a copyin() to be on the
 1688                          * safe side. This should be changed in case
 1689                          * copyinstr() is changed to guarantee this.
 1690                          */
 1691                         error = copyin((void *)(register_t)args->arg2, comm,
 1692                             max_size - 1);
 1693                         comm[max_size - 1] = '\0';
 1694                 }
 1695                 if (error)
 1696                         return (error);
 1697 
 1698                 PROC_LOCK(p);
 1699                 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
 1700                 PROC_UNLOCK(p);
 1701                 break;
 1702         case LINUX_PR_GET_NAME:
 1703                 PROC_LOCK(p);
 1704                 strlcpy(comm, p->p_comm, sizeof(comm));
 1705                 PROC_UNLOCK(p);
 1706                 error = copyout(comm, (void *)(register_t)args->arg2,
 1707                     strlen(comm) + 1);
 1708                 break;
 1709         default:
 1710                 error = EINVAL;
 1711                 break;
 1712         }
 1713 
 1714         return (error);
 1715 }
 1716 
 1717 /*
 1718  * XXX: fake one.. waiting for real implementation of affinity mask.
 1719  */
 1720 int
 1721 linux_sched_getaffinity(struct thread *td,
 1722     struct linux_sched_getaffinity_args *args)
 1723 {
 1724         int error;
 1725         cpumask_t i = ~0;
 1726 
 1727         if (args->len < sizeof(cpumask_t))
 1728                 return (EINVAL);
 1729 
 1730         error = copyout(&i, args->user_mask_ptr, sizeof(cpumask_t));
 1731         if (error)
 1732                 return (EFAULT);
 1733 
 1734         td->td_retval[0] = sizeof(cpumask_t);
 1735         return (0);
 1736 }

Cache object: 1a7d4a3b5a1f871306bdc4e1376e10ab


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