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 $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sysctl.h>
53 #include <sys/vnode.h>
54 #include <sys/thread2.h>
55 #include <machine/limits.h>
57 #include <cpu/lwbuf.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_map.h>
63 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
64 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
67 * UIO_READ: copy the kernelspace cp to the user or kernelspace UIO
68 * UIO_WRITE: copy the user or kernelspace UIO to the kernelspace cp
70 * For userspace UIO's, uio_td must be the current thread.
72 * The syscall interface is responsible for limiting the length to
73 * ssize_t for things like read() or write() which return the bytes
74 * read or written as ssize_t. These functions work with unsigned
78 uiomove(caddr_t cp, size_t n, struct uio *uio)
85 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
87 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
91 save = curproc->p_flag & P_DEADLKTREAT;
92 curproc->p_flag |= P_DEADLKTREAT;
95 while (n > 0 && uio->uio_resid) {
106 switch (uio->uio_segflg) {
110 if (uio->uio_rw == UIO_READ)
111 error = copyout(cp, iov->iov_base, cnt);
113 error = copyin(iov->iov_base, cp, cnt);
119 if (uio->uio_rw == UIO_READ)
120 bcopy((caddr_t)cp, iov->iov_base, cnt);
122 bcopy(iov->iov_base, (caddr_t)cp, cnt);
127 iov->iov_base = (char *)iov->iov_base + cnt;
129 uio->uio_resid -= cnt;
130 uio->uio_offset += cnt;
135 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
140 * Like uiomove() but copies zero-fill. Only allowed for UIO_READ,
141 * for obvious reasons.
144 uiomovez(size_t n, struct uio *uio)
150 KASSERT(uio->uio_rw == UIO_READ, ("uiomovez: mode"));
151 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
154 while (n > 0 && uio->uio_resid) {
165 switch (uio->uio_segflg) {
167 error = copyout(ZeroPage, iov->iov_base, cnt);
172 bzero(iov->iov_base, cnt);
177 iov->iov_base = (char *)iov->iov_base + cnt;
179 uio->uio_resid -= cnt;
180 uio->uio_offset += cnt;
187 * Wrapper for uiomove() that validates the arguments against a known-good
191 uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
195 offset = (size_t)uio->uio_offset;
196 if ((off_t)offset != uio->uio_offset)
198 if (buflen == 0 || offset >= buflen)
200 return (uiomove((char *)buf + offset, buflen - offset, uio));
204 * Give next character to user as result of read.
207 ureadc(int c, struct uio *uio)
213 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
216 if (iov->iov_len == 0) {
221 switch (uio->uio_segflg) {
224 if (subyte(iov->iov_base, c) < 0)
229 iov_base = iov->iov_base;
231 iov->iov_base = iov_base;
237 iov->iov_base = (char *)iov->iov_base + 1;
245 * General routine to allocate a hash table. Make the hash table size a
246 * power of 2 greater or equal to the number of elements requested, and
247 * store the masking value in *hashmask.
250 hashinit(int elements, struct malloc_type *type, u_long *hashmask)
253 LIST_HEAD(generic, generic) *hashtbl;
257 panic("hashinit: bad elements");
258 for (hashsize = 2; hashsize < elements; hashsize <<= 1)
260 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
261 for (i = 0; i < hashsize; i++)
262 LIST_INIT(&hashtbl[i]);
263 *hashmask = hashsize - 1;
268 * This is a newer version which allocates a hash table of structures.
270 * The returned array will be zero'd. The caller is responsible for
271 * initializing the structures.
274 hashinit_ext(int elements, size_t size, struct malloc_type *type,
281 panic("hashinit: bad elements");
282 for (hashsize = 2; hashsize < elements; hashsize <<= 1)
284 hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO);
285 *hashmask = hashsize - 1;
289 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
290 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
291 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
292 #define NPRIMES NELEM(primes)
295 * General routine to allocate a prime number sized hash table.
298 phashinit(int elements, struct malloc_type *type, u_long *nentries)
301 LIST_HEAD(generic, generic) *hashtbl;
305 panic("phashinit: bad elements");
306 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
310 hashsize = primes[i];
312 hashsize = primes[i - 1];
313 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
314 for (i = 0; i < hashsize; i++)
315 LIST_INIT(&hashtbl[i]);
316 *nentries = hashsize;
321 * This is a newer version which allocates a hash table of structures
322 * in a prime-number size.
324 * The returned array will be zero'd. The caller is responsible for
325 * initializing the structures.
328 phashinit_ext(int elements, size_t size, struct malloc_type *type,
336 panic("phashinit: bad elements");
337 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
341 hashsize = primes[i];
343 hashsize = primes[i - 1];
344 hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO);
345 *nentries = hashsize;
350 * Copyin an iovec. If the iovec array fits, use the preallocated small
351 * iovec structure. If it is too big, dynamically allocate an iovec array
352 * of sufficient size.
357 iovec_copyin(struct iovec *uiov, struct iovec **kiov, struct iovec *siov,
358 size_t iov_cnt, size_t *iov_len)
364 if (iov_cnt > UIO_MAXIOV)
366 if (iov_cnt > UIO_SMALLIOV) {
367 MALLOC(*kiov, struct iovec *, sizeof(struct iovec) * iov_cnt,
372 error = copyin(uiov, *kiov, iov_cnt * sizeof(struct iovec));
375 for (i = 0, iovp = *kiov; i < iov_cnt; i++, iovp++) {
377 * Check for both *iov_len overflows and out of
378 * range iovp->iov_len's. We limit to the
379 * capabilities of signed integers.
381 * GCC4 - overflow check opt requires assign/test.
383 len = *iov_len + iovp->iov_len;
391 * From userland disallow iovec's which exceed the sized size
392 * limit as the system calls return ssize_t.
394 * NOTE: Internal kernel interfaces can handle the unsigned
397 if (error == 0 && (ssize_t)*iov_len < 0)
401 iovec_free(kiov, siov);
407 * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
408 * Copyright (c) 1982, 1986, 1991, 1993
409 * The Regents of the University of California. All rights reserved.
410 * (c) UNIX System Laboratories, Inc.
411 * All or some portions of this file are derived from material licensed
412 * to the University of California by American Telephone and Telegraph
413 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
414 * the permission of UNIX System Laboratories, Inc.
416 * Redistribution and use in source and binary forms, with or without
417 * modification, are permitted provided that the following conditions
419 * 1. Redistributions of source code must retain the above copyright
420 * notice, this list of conditions and the following disclaimer.
421 * 2. Redistributions in binary form must reproduce the above copyright
422 * notice, this list of conditions and the following disclaimer in the
423 * documentation and/or other materials provided with the distribution.
424 * 4. Neither the name of the University nor the names of its contributors
425 * may be used to endorse or promote products derived from this software
426 * without specific prior written permission.
428 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
429 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
431 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
432 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
433 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
434 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
435 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
436 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
437 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
440 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
441 * $FreeBSD: src/sys/i386/i386/uio_machdep.c,v 1.1 2004/03/21 20:28:36 alc Exp $
445 * Implement uiomove(9) from physical memory using lwbuf's to reduce
446 * the creation and destruction of ephemeral mappings.
449 uiomove_fromphys(vm_page_t *ma, vm_offset_t offset, size_t n, struct uio *uio)
451 struct lwbuf lwb_cache;
453 struct thread *td = curthread;
456 vm_offset_t page_offset;
462 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
463 ("uiomove_fromphys: mode"));
464 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
465 ("uiomove_fromphys proc"));
468 save = td->td_flags & TDF_DEADLKTREAT;
469 td->td_flags |= TDF_DEADLKTREAT;
472 while (n > 0 && uio->uio_resid) {
482 page_offset = offset & PAGE_MASK;
483 cnt = min(cnt, PAGE_SIZE - page_offset);
484 m = ma[offset >> PAGE_SHIFT];
485 lwb = lwbuf_alloc(m, &lwb_cache);
486 cp = (char *)lwbuf_kva(lwb) + page_offset;
487 switch (uio->uio_segflg) {
490 * note: removed uioyield (it was the wrong place to
493 if (uio->uio_rw == UIO_READ)
494 error = copyout(cp, iov->iov_base, cnt);
496 error = copyin(iov->iov_base, cp, cnt);
503 if (uio->uio_rw == UIO_READ)
504 bcopy(cp, iov->iov_base, cnt);
506 bcopy(iov->iov_base, cp, cnt);
512 iov->iov_base = (char *)iov->iov_base + cnt;
514 uio->uio_resid -= cnt;
515 uio->uio_offset += cnt;
522 td->td_flags &= ~TDF_DEADLKTREAT;