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.12 2003/10/08 01:30:32 daver Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/vnode.h>
53 #include <machine/limits.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_map.h>
69 int baseticks = ticks;
71 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
73 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
77 save = curproc->p_flag & P_DEADLKTREAT;
78 curproc->p_flag |= P_DEADLKTREAT;
81 while (n > 0 && uio->uio_resid) {
92 switch (uio->uio_segflg) {
96 if (ticks - baseticks >= hogticks) {
100 if (uio->uio_rw == UIO_READ)
101 error = copyout(cp, iov->iov_base, cnt);
103 error = copyin(iov->iov_base, cp, cnt);
109 if (uio->uio_rw == UIO_READ)
110 bcopy((caddr_t)cp, iov->iov_base, cnt);
112 bcopy(iov->iov_base, (caddr_t)cp, cnt);
117 iov->iov_base += cnt;
119 uio->uio_resid -= cnt;
120 uio->uio_offset += cnt;
125 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
129 * Wrapper for uiomove() that validates the arguments against a known-good
130 * kernel buffer. Currently, uiomove accepts a signed (n) argument, which
131 * is almost definitely a bad thing, so we catch that here as well. We
132 * return a runtime failure, but it might be desirable to generate a runtime
133 * assertion failure instead.
136 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
138 unsigned int offset, n;
140 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
141 (offset = uio->uio_offset) != uio->uio_offset)
143 if (buflen <= 0 || offset >= buflen)
145 if ((n = buflen - offset) > INT_MAX)
147 return (uiomove((char *)buf + offset, n, uio));
152 uiomoveco(cp, n, uio, obj)
156 struct vm_object *obj;
161 int baseticks = ticks;
163 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
164 ("uiomoveco: mode"));
165 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
168 while (n > 0 && uio->uio_resid) {
179 switch (uio->uio_segflg) {
183 if (ticks - baseticks >= hogticks) {
187 if (uio->uio_rw == UIO_READ) {
188 #ifdef ENABLE_VFS_IOOPT
189 if (vfs_ioopt && ((cnt & PAGE_MASK) == 0) &&
190 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
191 ((uio->uio_offset & PAGE_MASK) == 0) &&
192 ((((intptr_t) cp) & PAGE_MASK) == 0)) {
193 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
194 uio->uio_offset, cnt,
195 (vm_offset_t) iov->iov_base, NULL);
199 error = copyout(cp, iov->iov_base, cnt);
202 error = copyin(iov->iov_base, cp, cnt);
209 if (uio->uio_rw == UIO_READ)
210 bcopy((caddr_t)cp, iov->iov_base, cnt);
212 bcopy(iov->iov_base, (caddr_t)cp, cnt);
217 iov->iov_base += cnt;
219 uio->uio_resid -= cnt;
220 uio->uio_offset += cnt;
227 #ifdef ENABLE_VFS_IOOPT
230 uioread(n, uio, obj, nread)
233 struct vm_object *obj;
240 int baseticks = ticks;
248 while (n > 0 && uio->uio_resid) {
259 if ((uio->uio_segflg == UIO_USERSPACE) &&
260 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
261 ((uio->uio_offset & PAGE_MASK) == 0) ) {
268 if (ticks - baseticks >= hogticks) {
272 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
273 uio->uio_offset, cnt,
274 (vm_offset_t) iov->iov_base, &npagesmoved);
276 if (npagesmoved == 0)
279 tcnt = npagesmoved * PAGE_SIZE;
285 iov->iov_base += cnt;
287 uio->uio_resid -= cnt;
288 uio->uio_offset += cnt;
301 * Give next character to user as result of read.
311 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
314 if (iov->iov_len == 0) {
319 switch (uio->uio_segflg) {
322 if (subyte(iov->iov_base, c) < 0)
331 if (suibyte(iov->iov_base, c) < 0)
344 #ifdef vax /* unused except by ct.c, other oddities XXX */
346 * Get next character written in by user from uio.
355 if (uio->uio_resid <= 0)
358 if (uio->uio_iovcnt <= 0)
361 if (iov->iov_len == 0) {
363 if (--uio->uio_iovcnt == 0)
367 switch (uio->uio_segflg) {
370 c = fubyte(iov->iov_base);
374 c = *(u_char *) iov->iov_base;
378 c = fuibyte(iov->iov_base);
392 * General routine to allocate a hash table.
395 hashinit(elements, type, hashmask)
397 struct malloc_type *type;
401 LIST_HEAD(generic, generic) *hashtbl;
405 panic("hashinit: bad elements");
406 for (hashsize = 1; hashsize <= elements; hashsize <<= 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 *hashmask = hashsize - 1;
416 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
417 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
418 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
419 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
422 * General routine to allocate a prime number sized hash table.
425 phashinit(elements, type, nentries)
427 struct malloc_type *type;
431 LIST_HEAD(generic, generic) *hashtbl;
435 panic("phashinit: bad elements");
436 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
440 hashsize = primes[i];
442 hashsize = primes[i - 1];
443 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
444 for (i = 0; i < hashsize; i++)
445 LIST_INIT(&hashtbl[i]);
446 *nentries = hashsize;
451 * Copyin an iovec. If the iovec array fits, use the preallocated small
452 * iovec structure. If it is too big, dynamically allocate an iovec array
453 * of sufficient size.
456 iovec_copyin(struct iovec *uiov, struct iovec **kiov, struct iovec *siov,
457 size_t iov_cnt, size_t *iov_len)
462 if (iov_cnt >= UIO_MAXIOV)
464 if (iov_cnt >= UIO_SMALLIOV) {
465 MALLOC(*kiov, struct iovec *, sizeof(struct iovec) * iov_cnt,
470 error = copyin(uiov, *kiov, iov_cnt * sizeof(struct iovec));
474 for (i = 0, iovp = *kiov; i < iov_cnt; i++, iovp++) {
475 *iov_len += iovp->iov_len;
484 iovec_free(kiov, siov);