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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_subr.c,v 1.31.2.2 2002/04/21 08:09:37 bde Exp $
40 * $DragonFly: src/sys/kern/kern_subr.c,v 1.3 2003/06/19 01:55:06 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/resourcevar.h>
50 #include <sys/vnode.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_map.h>
60 register struct uio *uio;
62 register struct iovec *iov;
67 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
69 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
73 save = curproc->p_flag & P_DEADLKTREAT;
74 curproc->p_flag |= P_DEADLKTREAT;
77 while (n > 0 && uio->uio_resid) {
88 switch (uio->uio_segflg) {
92 if (ticks - mycpu->gd_switchticks >= hogticks)
94 if (uio->uio_rw == UIO_READ)
95 error = copyout(cp, iov->iov_base, cnt);
97 error = copyin(iov->iov_base, cp, cnt);
103 if (uio->uio_rw == UIO_READ)
104 bcopy((caddr_t)cp, iov->iov_base, cnt);
106 bcopy(iov->iov_base, (caddr_t)cp, cnt);
111 iov->iov_base += cnt;
113 uio->uio_resid -= cnt;
114 uio->uio_offset += cnt;
119 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
124 uiomoveco(cp, n, uio, obj)
128 struct vm_object *obj;
134 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
135 ("uiomoveco: mode"));
136 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_procp == curproc,
139 while (n > 0 && uio->uio_resid) {
150 switch (uio->uio_segflg) {
154 if (ticks - mycpu->gd_switchticks >= hogticks)
156 if (uio->uio_rw == UIO_READ) {
157 #ifdef ENABLE_VFS_IOOPT
158 if (vfs_ioopt && ((cnt & PAGE_MASK) == 0) &&
159 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
160 ((uio->uio_offset & PAGE_MASK) == 0) &&
161 ((((intptr_t) cp) & PAGE_MASK) == 0)) {
162 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
163 uio->uio_offset, cnt,
164 (vm_offset_t) iov->iov_base, NULL);
168 error = copyout(cp, iov->iov_base, cnt);
171 error = copyin(iov->iov_base, cp, cnt);
178 if (uio->uio_rw == UIO_READ)
179 bcopy((caddr_t)cp, iov->iov_base, cnt);
181 bcopy(iov->iov_base, (caddr_t)cp, cnt);
186 iov->iov_base += cnt;
188 uio->uio_resid -= cnt;
189 uio->uio_offset += cnt;
196 #ifdef ENABLE_VFS_IOOPT
199 uioread(n, uio, obj, nread)
202 struct vm_object *obj;
216 while (n > 0 && uio->uio_resid) {
227 if ((uio->uio_segflg == UIO_USERSPACE) &&
228 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
229 ((uio->uio_offset & PAGE_MASK) == 0) ) {
236 if (ticks - switchticks >= hogticks)
238 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
239 uio->uio_offset, cnt,
240 (vm_offset_t) iov->iov_base, &npagesmoved);
242 if (npagesmoved == 0)
245 tcnt = npagesmoved * PAGE_SIZE;
251 iov->iov_base += cnt;
253 uio->uio_resid -= cnt;
254 uio->uio_offset += cnt;
267 * Give next character to user as result of read.
272 register struct uio *uio;
274 register struct iovec *iov;
277 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
280 if (iov->iov_len == 0) {
285 switch (uio->uio_segflg) {
288 if (subyte(iov->iov_base, c) < 0)
297 if (suibyte(iov->iov_base, c) < 0)
310 #ifdef vax /* unused except by ct.c, other oddities XXX */
312 * Get next character written in by user from uio.
318 register struct iovec *iov;
321 if (uio->uio_resid <= 0)
324 if (uio->uio_iovcnt <= 0)
327 if (iov->iov_len == 0) {
329 if (--uio->uio_iovcnt == 0)
333 switch (uio->uio_segflg) {
336 c = fubyte(iov->iov_base);
340 c = *(u_char *) iov->iov_base;
344 c = fuibyte(iov->iov_base);
358 * General routine to allocate a hash table.
361 hashinit(elements, type, hashmask)
363 struct malloc_type *type;
367 LIST_HEAD(generic, generic) *hashtbl;
371 panic("hashinit: bad elements");
372 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
375 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
376 for (i = 0; i < hashsize; i++)
377 LIST_INIT(&hashtbl[i]);
378 *hashmask = hashsize - 1;
382 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
383 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
384 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
385 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
388 * General routine to allocate a prime number sized hash table.
391 phashinit(elements, type, nentries)
393 struct malloc_type *type;
397 LIST_HEAD(generic, generic) *hashtbl;
401 panic("phashinit: bad elements");
402 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
406 hashsize = primes[i];
408 hashsize = primes[i - 1];
409 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
410 for (i = 0; i < hashsize; i++)
411 LIST_INIT(&hashtbl[i]);
412 *nentries = hashsize;
424 p->p_priority = p->p_usrpri;
426 p->p_stats->p_ru.ru_nivcsw++;