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.14 2003/10/15 21:52:38 dillon 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>
60 * UIO_READ: copy the kernelspace cp to the user or kernelspace UIO
61 * UIO_WRITE: copy the user or kernelspace UIO to cp
63 * For userspace UIO's, uio_td must be the current thread.
66 uiomove(caddr_t cp, int n, struct uio *uio)
72 int baseticks = ticks;
74 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
76 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
80 save = curproc->p_flag & P_DEADLKTREAT;
81 curproc->p_flag |= P_DEADLKTREAT;
84 while (n > 0 && uio->uio_resid) {
95 switch (uio->uio_segflg) {
99 if (ticks - baseticks >= hogticks) {
103 if (uio->uio_rw == UIO_READ)
104 error = copyout(cp, iov->iov_base, cnt);
106 error = copyin(iov->iov_base, cp, cnt);
112 if (uio->uio_rw == UIO_READ)
113 bcopy((caddr_t)cp, iov->iov_base, cnt);
115 bcopy(iov->iov_base, (caddr_t)cp, cnt);
120 iov->iov_base += cnt;
122 uio->uio_resid -= cnt;
123 uio->uio_offset += cnt;
128 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
132 * Wrapper for uiomove() that validates the arguments against a known-good
133 * kernel buffer. Currently, uiomove accepts a signed (n) argument, which
134 * is almost definitely a bad thing, so we catch that here as well. We
135 * return a runtime failure, but it might be desirable to generate a runtime
136 * assertion failure instead.
139 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
141 unsigned int offset, n;
143 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
144 (offset = uio->uio_offset) != uio->uio_offset)
146 if (buflen <= 0 || offset >= buflen)
148 if ((n = buflen - offset) > INT_MAX)
150 return (uiomove((char *)buf + offset, n, uio));
155 uiomoveco(cp, n, uio, obj)
159 struct vm_object *obj;
164 int baseticks = ticks;
166 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
167 ("uiomoveco: mode"));
168 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
171 while (n > 0 && uio->uio_resid) {
182 switch (uio->uio_segflg) {
186 if (ticks - baseticks >= hogticks) {
190 if (uio->uio_rw == UIO_READ) {
191 #ifdef ENABLE_VFS_IOOPT
192 if (vfs_ioopt && ((cnt & PAGE_MASK) == 0) &&
193 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
194 ((uio->uio_offset & PAGE_MASK) == 0) &&
195 ((((intptr_t) cp) & PAGE_MASK) == 0)) {
196 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
197 uio->uio_offset, cnt,
198 (vm_offset_t) iov->iov_base, NULL);
202 error = copyout(cp, iov->iov_base, cnt);
205 error = copyin(iov->iov_base, cp, cnt);
212 if (uio->uio_rw == UIO_READ)
213 bcopy((caddr_t)cp, iov->iov_base, cnt);
215 bcopy(iov->iov_base, (caddr_t)cp, cnt);
220 iov->iov_base += cnt;
222 uio->uio_resid -= cnt;
223 uio->uio_offset += cnt;
230 #ifdef ENABLE_VFS_IOOPT
233 uioread(n, uio, obj, nread)
236 struct vm_object *obj;
243 int baseticks = ticks;
251 while (n > 0 && uio->uio_resid) {
262 if ((uio->uio_segflg == UIO_USERSPACE) &&
263 ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) &&
264 ((uio->uio_offset & PAGE_MASK) == 0) ) {
271 if (ticks - baseticks >= hogticks) {
275 error = vm_uiomove(&curproc->p_vmspace->vm_map, obj,
276 uio->uio_offset, cnt,
277 (vm_offset_t) iov->iov_base, &npagesmoved);
279 if (npagesmoved == 0)
282 tcnt = npagesmoved * PAGE_SIZE;
288 iov->iov_base += cnt;
290 uio->uio_resid -= cnt;
291 uio->uio_offset += cnt;
304 * Give next character to user as result of read.
314 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
317 if (iov->iov_len == 0) {
322 switch (uio->uio_segflg) {
325 if (subyte(iov->iov_base, c) < 0)
334 if (suibyte(iov->iov_base, c) < 0)
347 #ifdef vax /* unused except by ct.c, other oddities XXX */
349 * Get next character written in by user from uio.
358 if (uio->uio_resid <= 0)
361 if (uio->uio_iovcnt <= 0)
364 if (iov->iov_len == 0) {
366 if (--uio->uio_iovcnt == 0)
370 switch (uio->uio_segflg) {
373 c = fubyte(iov->iov_base);
377 c = *(u_char *) iov->iov_base;
381 c = fuibyte(iov->iov_base);
395 * General routine to allocate a hash table.
398 hashinit(elements, type, hashmask)
400 struct malloc_type *type;
404 LIST_HEAD(generic, generic) *hashtbl;
408 panic("hashinit: bad elements");
409 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
412 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
413 for (i = 0; i < hashsize; i++)
414 LIST_INIT(&hashtbl[i]);
415 *hashmask = hashsize - 1;
419 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
420 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
421 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
422 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
425 * General routine to allocate a prime number sized hash table.
428 phashinit(elements, type, nentries)
430 struct malloc_type *type;
434 LIST_HEAD(generic, generic) *hashtbl;
438 panic("phashinit: bad elements");
439 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
443 hashsize = primes[i];
445 hashsize = primes[i - 1];
446 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
447 for (i = 0; i < hashsize; i++)
448 LIST_INIT(&hashtbl[i]);
449 *nentries = hashsize;
454 * Copyin an iovec. If the iovec array fits, use the preallocated small
455 * iovec structure. If it is too big, dynamically allocate an iovec array
456 * of sufficient size.
459 iovec_copyin(struct iovec *uiov, struct iovec **kiov, struct iovec *siov,
460 size_t iov_cnt, size_t *iov_len)
465 if (iov_cnt >= UIO_MAXIOV)
467 if (iov_cnt >= UIO_SMALLIOV) {
468 MALLOC(*kiov, struct iovec *, sizeof(struct iovec) * iov_cnt,
473 error = copyin(uiov, *kiov, iov_cnt * sizeof(struct iovec));
477 for (i = 0, iovp = *kiov; i < iov_cnt; i++, iovp++)
478 *iov_len += iovp->iov_len;
482 iovec_free(kiov, siov);