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/kern/kern_subr.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) 1982, 1986, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. All advertising materials mentioning features or use of this software
   19  *    must display the following acknowledgement:
   20  *      This product includes software developed by the University of
   21  *      California, Berkeley and its contributors.
   22  * 4. Neither the name of the University nor the names of its contributors
   23  *    may be used to endorse or promote products derived from this software
   24  *    without specific prior written permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   36  * SUCH DAMAGE.
   37  *
   38  *      @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
   39  * $FreeBSD$
   40  */
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/proc.h>
   46 #include <sys/malloc.h>
   47 #include <sys/lock.h>
   48 #include <sys/resourcevar.h>
   49 #include <sys/vnode.h>
   50 #include <machine/limits.h>
   51 
   52 #include <vm/vm.h>
   53 #include <vm/vm_page.h>
   54 #include <vm/vm_map.h>
   55 
   56 int
   57 uiomove(cp, n, uio)
   58         register caddr_t cp;
   59         register int n;
   60         register struct uio *uio;
   61 {
   62         register struct iovec *iov;
   63         u_int cnt;
   64         int error = 0;
   65         int save = 0;
   66 
   67         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
   68             ("uiomove: mode"));
   69         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
   70             ("uiomove proc"));
   71 
   72         if (curproc) {
   73                 save = curproc->p_flag & P_DEADLKTREAT;
   74                 curproc->p_flag |= P_DEADLKTREAT;
   75         }
   76 
   77         while (n > 0 && uio->uio_resid) {
   78                 iov = uio->uio_iov;
   79                 cnt = iov->iov_len;
   80                 if (cnt == 0) {
   81                         uio->uio_iov++;
   82                         uio->uio_iovcnt--;
   83                         continue;
   84                 }
   85                 if (cnt > n)
   86                         cnt = n;
   87 
   88                 switch (uio->uio_segflg) {
   89 
   90                 case UIO_USERSPACE:
   91                 case UIO_USERISPACE:
   92                         if (ticks - switchticks >= hogticks)
   93                                 uio_yield();
   94                         if (uio->uio_rw == UIO_READ)
   95                                 error = copyout(cp, iov->iov_base, cnt);
   96                         else
   97                                 error = copyin(iov->iov_base, cp, cnt);
   98                         if (error)
   99                                 break;
  100                         break;
  101 
  102                 case UIO_SYSSPACE:
  103                         if (uio->uio_rw == UIO_READ)
  104                                 bcopy((caddr_t)cp, iov->iov_base, cnt);
  105                         else
  106                                 bcopy(iov->iov_base, (caddr_t)cp, cnt);
  107                         break;
  108                 case UIO_NOCOPY:
  109                         break;
  110                 }
  111                 iov->iov_base += cnt;
  112                 iov->iov_len -= cnt;
  113                 uio->uio_resid -= cnt;
  114                 uio->uio_offset += cnt;
  115                 cp += cnt;
  116                 n -= cnt;
  117         }
  118         if (curproc)
  119                 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
  120         return (error);
  121 }
  122 
  123 /*
  124  * Wrapper for uiomove() that validates the arguments against a known-good
  125  * kernel buffer.  Currently, uiomove accepts a signed (n) argument, which
  126  * is almost definitely a bad thing, so we catch that here as well.  We
  127  * return a runtime failure, but it might be desirable to generate a runtime
  128  * assertion failure instead.
  129  */
  130 int
  131 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
  132 {
  133         unsigned int offset, n;
  134 
  135         if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
  136             (offset = uio->uio_offset) != uio->uio_offset)
  137                 return (EINVAL);
  138         if (buflen <= 0 || offset >= buflen)
  139                 return (0);
  140         if ((n = buflen - offset) > INT_MAX)
  141                 return (EINVAL);
  142         return (uiomove((char *)buf + offset, n, uio));
  143 }
  144 
  145 int
  146 uiomoveco(cp, n, uio, obj)
  147         caddr_t cp;
  148         int n;
  149         struct uio *uio;
  150         struct vm_object *obj;
  151 {
  152         struct iovec *iov;
  153         u_int cnt;
  154         int error;
  155 
  156         KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
  157             ("uiomoveco: mode"));
  158         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
  159             ("uiomoveco proc"));
  160 
  161         while (n > 0 && uio->uio_resid) {
  162                 iov = uio->uio_iov;
  163                 cnt = iov->iov_len;
  164                 if (cnt == 0) {
  165                         uio->uio_iov++;
  166                         uio->uio_iovcnt--;
  167                         continue;
  168                 }
  169                 if (cnt > n)
  170                         cnt = n;
  171 
  172                 switch (uio->uio_segflg) {
  173 
  174                 case UIO_USERSPACE:
  175                 case UIO_USERISPACE:
  176                         if (ticks - switchticks >= hogticks)
  177                                 uio_yield();
  178                         if (uio->uio_rw == UIO_READ) {
  179 #ifdef ENABLE_VFS_IOOPT
  180                                 if (vfs_ioopt && ((cnt & PAGE_MASK) == 0) &&
  181                                         ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
  182                                         ((uio->uio_offset & PAGE_MASK) == 0) &&
  183                                         ((((intptr_t) cp) & PAGE_MASK) == 0)) {
  184                                                 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
  185                                                                 uio->uio_offset, cnt,
  186                                                                 (vm_offset_t) iov->iov_base, NULL);
  187                                 } else
  188 #endif
  189                                 {
  190                                         error = copyout(cp, iov->iov_base, cnt);
  191                                 }
  192                         } else {
  193                                 error = copyin(iov->iov_base, cp, cnt);
  194                         }
  195                         if (error)
  196                                 return (error);
  197                         break;
  198 
  199                 case UIO_SYSSPACE:
  200                         if (uio->uio_rw == UIO_READ)
  201                                 bcopy((caddr_t)cp, iov->iov_base, cnt);
  202                         else
  203                                 bcopy(iov->iov_base, (caddr_t)cp, cnt);
  204                         break;
  205                 case UIO_NOCOPY:
  206                         break;
  207                 }
  208                 iov->iov_base += cnt;
  209                 iov->iov_len -= cnt;
  210                 uio->uio_resid -= cnt;
  211                 uio->uio_offset += cnt;
  212                 cp += cnt;
  213                 n -= cnt;
  214         }
  215         return (0);
  216 }
  217 
  218 #ifdef ENABLE_VFS_IOOPT
  219 
  220 int
  221 uioread(n, uio, obj, nread)
  222         int n;
  223         struct uio *uio;
  224         struct vm_object *obj;
  225         int *nread;
  226 {
  227         int npagesmoved;
  228         struct iovec *iov;
  229         u_int cnt, tcnt;
  230         int error;
  231 
  232         *nread = 0;
  233         if (vfs_ioopt < 2)
  234                 return 0;
  235 
  236         error = 0;
  237 
  238         while (n > 0 && uio->uio_resid) {
  239                 iov = uio->uio_iov;
  240                 cnt = iov->iov_len;
  241                 if (cnt == 0) {
  242                         uio->uio_iov++;
  243                         uio->uio_iovcnt--;
  244                         continue;
  245                 }
  246                 if (cnt > n)
  247                         cnt = n;
  248 
  249                 if ((uio->uio_segflg == UIO_USERSPACE) &&
  250                         ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
  251                                  ((uio->uio_offset & PAGE_MASK) == 0) ) {
  252 
  253                         if (cnt < PAGE_SIZE)
  254                                 break;
  255 
  256                         cnt &= ~PAGE_MASK;
  257 
  258                         if (ticks - switchticks >= hogticks)
  259                                 uio_yield();
  260                         error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
  261                                                 uio->uio_offset, cnt,
  262                                                 (vm_offset_t) iov->iov_base, &npagesmoved);
  263 
  264                         if (npagesmoved == 0)
  265                                 break;
  266 
  267                         tcnt = npagesmoved * PAGE_SIZE;
  268                         cnt = tcnt;
  269 
  270                         if (error)
  271                                 break;
  272 
  273                         iov->iov_base += cnt;
  274                         iov->iov_len -= cnt;
  275                         uio->uio_resid -= cnt;
  276                         uio->uio_offset += cnt;
  277                         *nread += cnt;
  278                         n -= cnt;
  279                 } else {
  280                         break;
  281                 }
  282         }
  283         return error;
  284 }
  285 
  286 #endif
  287 
  288 /*
  289  * Give next character to user as result of read.
  290  */
  291 int
  292 ureadc(c, uio)
  293         register int c;
  294         register struct uio *uio;
  295 {
  296         register struct iovec *iov;
  297 
  298 again:
  299         if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
  300                 panic("ureadc");
  301         iov = uio->uio_iov;
  302         if (iov->iov_len == 0) {
  303                 uio->uio_iovcnt--;
  304                 uio->uio_iov++;
  305                 goto again;
  306         }
  307         switch (uio->uio_segflg) {
  308 
  309         case UIO_USERSPACE:
  310                 if (subyte(iov->iov_base, c) < 0)
  311                         return (EFAULT);
  312                 break;
  313 
  314         case UIO_SYSSPACE:
  315                 *iov->iov_base = c;
  316                 break;
  317 
  318         case UIO_USERISPACE:
  319                 if (suibyte(iov->iov_base, c) < 0)
  320                         return (EFAULT);
  321                 break;
  322         case UIO_NOCOPY:
  323                 break;
  324         }
  325         iov->iov_base++;
  326         iov->iov_len--;
  327         uio->uio_resid--;
  328         uio->uio_offset++;
  329         return (0);
  330 }
  331 
  332 #ifdef vax      /* unused except by ct.c, other oddities XXX */
  333 /*
  334  * Get next character written in by user from uio.
  335  */
  336 int
  337 uwritec(uio)
  338         struct uio *uio;
  339 {
  340         register struct iovec *iov;
  341         register int c;
  342 
  343         if (uio->uio_resid <= 0)
  344                 return (-1);
  345 again:
  346         if (uio->uio_iovcnt <= 0)
  347                 panic("uwritec");
  348         iov = uio->uio_iov;
  349         if (iov->iov_len == 0) {
  350                 uio->uio_iov++;
  351                 if (--uio->uio_iovcnt == 0)
  352                         return (-1);
  353                 goto again;
  354         }
  355         switch (uio->uio_segflg) {
  356 
  357         case UIO_USERSPACE:
  358                 c = fubyte(iov->iov_base);
  359                 break;
  360 
  361         case UIO_SYSSPACE:
  362                 c = *(u_char *) iov->iov_base;
  363                 break;
  364 
  365         case UIO_USERISPACE:
  366                 c = fuibyte(iov->iov_base);
  367                 break;
  368         }
  369         if (c < 0)
  370                 return (-1);
  371         iov->iov_base++;
  372         iov->iov_len--;
  373         uio->uio_resid--;
  374         uio->uio_offset++;
  375         return (c);
  376 }
  377 #endif /* vax */
  378 
  379 /*
  380  * General routine to allocate a hash table.
  381  */
  382 void *
  383 hashinit(elements, type, hashmask)
  384         int elements;
  385         struct malloc_type *type;
  386         u_long *hashmask;
  387 {
  388         long hashsize;
  389         LIST_HEAD(generic, generic) *hashtbl;
  390         int i;
  391 
  392         if (elements <= 0)
  393                 panic("hashinit: bad elements");
  394         for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
  395                 continue;
  396         hashsize >>= 1;
  397         hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
  398         for (i = 0; i < hashsize; i++)
  399                 LIST_INIT(&hashtbl[i]);
  400         *hashmask = hashsize - 1;
  401         return (hashtbl);
  402 }
  403 
  404 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
  405                         2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
  406                         7159, 7673, 8191, 12281, 16381, 24571, 32749 };
  407 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
  408 
  409 /*
  410  * General routine to allocate a prime number sized hash table.
  411  */
  412 void *
  413 phashinit(elements, type, nentries)
  414         int elements;
  415         struct malloc_type *type;
  416         u_long *nentries;
  417 {
  418         long hashsize;
  419         LIST_HEAD(generic, generic) *hashtbl;
  420         int i;
  421 
  422         if (elements <= 0)
  423                 panic("phashinit: bad elements");
  424         for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
  425                 i++;
  426                 if (i == NPRIMES)
  427                         break;
  428                 hashsize = primes[i];
  429         }
  430         hashsize = primes[i - 1];
  431         hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
  432         for (i = 0; i < hashsize; i++)
  433                 LIST_INIT(&hashtbl[i]);
  434         *nentries = hashsize;
  435         return (hashtbl);
  436 }
  437 
  438 void
  439 uio_yield()
  440 {
  441         struct proc *p;
  442         int s;
  443 
  444         p = curproc;
  445         s = splhigh();
  446         p->p_priority = p->p_usrpri;
  447         setrunqueue(p);
  448         p->p_stats->p_ru.ru_nivcsw++;
  449         mi_switch();
  450         splx(s);
  451 }

Cache object: abd9a197ed9d985235c155441f911fdb


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