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 $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
52 #include <sys/thread2.h>
53 #include <machine/limits.h>
55 #include <cpu/lwbuf.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_map.h>
61 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
62 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
65 * UIO_READ: copy the kernelspace cp to the user or kernelspace UIO
66 * UIO_WRITE: copy the user or kernelspace UIO to the kernelspace cp
68 * For userspace UIO's, uio_td must be the current thread.
70 * The syscall interface is responsible for limiting the length to
71 * ssize_t for things like read() or write() which return the bytes
72 * read or written as ssize_t. These functions work with unsigned
76 uiomove(caddr_t cp, size_t n, struct uio *uio)
78 thread_t td = curthread;
85 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
87 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td,
91 save = td->td_flags & TDF_DEADLKTREAT;
92 td->td_flags |= TDF_DEADLKTREAT;
97 while (n > 0 && uio->uio_resid) {
109 switch (uio->uio_segflg) {
113 if (uio->uio_rw == UIO_READ)
114 error = copyout(cp, iov->iov_base, cnt);
116 error = copyin(iov->iov_base, cp, cnt);
119 if (uio->uio_rw == UIO_READ)
120 bcopy(cp, iov->iov_base, cnt);
122 bcopy(iov->iov_base, cp, cnt);
130 iov->iov_base = (char *)iov->iov_base + cnt;
132 uio->uio_resid -= cnt;
133 uio->uio_offset += cnt;
138 td->td_flags = (td->td_flags & ~TDF_DEADLKTREAT) | save;
145 * This is the same as uiomove() except (cp, n) is within the bounds of
146 * the passed, locked buffer. Under certain circumstances a VM fault
147 * occuring with a locked buffer held can result in a deadlock or an
148 * attempt to recursively lock the buffer.
150 * This procedure deals with these cases.
152 * If the buffer represents a regular file, is B_CACHE, but the last VM page
153 * is not fully valid we fix-up the last VM page. This should handle the
154 * recursive lock issue.
156 * Deadlocks are another issue. We are holding the vp and the bp locked
157 * and could deadlock against a different vp and/or bp if another thread is
158 * trying to access us while we accessing it. The only solution here is
159 * to release the bp and vnode lock and do the uio to/from a system buffer,
160 * then regain the locks and copyback (if applicable). XXX TODO.
163 uiomovebp(struct buf *bp, caddr_t cp, size_t n, struct uio *uio)
168 if (bp->b_vp && bp->b_vp->v_type == VREG &&
169 (bp->b_flags & B_CACHE) &&
170 (count = bp->b_xio.xio_npages) != 0 &&
171 (m = bp->b_xio.xio_pages[count-1])->valid != VM_PAGE_BITS_ALL) {
172 vm_page_zero_invalid(m, TRUE);
174 return (uiomove(cp, n, uio));
178 * uiomove() but fail for non-trivial VM faults, even if the VM fault is
179 * valid. Returns EFAULT if a VM fault occurred via the copyin/copyout
182 * This allows callers to hold e.g. a busy VM page, or a busy VM object,
183 * or a locked vnode through the call and then fall-back to safer code
187 uiomove_nofault(caddr_t cp, size_t n, struct uio *uio)
189 thread_t td = curthread;
192 atomic_set_int(&td->td_flags, TDF_NOFAULT);
193 error = uiomove(cp, n, uio);
194 atomic_clear_int(&td->td_flags, TDF_NOFAULT);
199 * Like uiomove() but copies zero-fill. Only allowed for UIO_READ,
200 * for obvious reasons.
203 uiomovez(size_t n, struct uio *uio)
209 KASSERT(uio->uio_rw == UIO_READ, ("uiomovez: mode"));
210 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
213 while (n > 0 && uio->uio_resid) {
224 switch (uio->uio_segflg) {
226 error = copyout(ZeroPage, iov->iov_base, cnt);
229 bzero(iov->iov_base, cnt);
237 iov->iov_base = (char *)iov->iov_base + cnt;
239 uio->uio_resid -= cnt;
240 uio->uio_offset += cnt;
247 * Wrapper for uiomove() that validates the arguments against a known-good
248 * kernel buffer. This function automatically indexes the buffer by
249 * uio_offset and handles all range checking.
252 uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
256 offset = (size_t)uio->uio_offset;
257 if ((off_t)offset != uio->uio_offset)
259 if (buflen == 0 || offset >= buflen)
261 return (uiomove((char *)buf + offset, buflen - offset, uio));
265 * Give next character to user as result of read.
268 ureadc(int c, struct uio *uio)
274 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
277 if (iov->iov_len == 0) {
283 switch (uio->uio_segflg) {
285 if (subyte(iov->iov_base, c) < 0)
289 iov_base = iov->iov_base;
291 iov->iov_base = iov_base;
297 iov->iov_base = (char *)iov->iov_base + 1;
305 * General routine to allocate a hash table. Make the hash table size a
306 * power of 2 greater or equal to the number of elements requested, and
307 * store the masking value in *hashmask.
310 hashinit(int elements, struct malloc_type *type, u_long *hashmask)
313 LIST_HEAD(generic, generic) *hashtbl;
317 panic("hashinit: bad elements");
318 for (hashsize = 2; hashsize < elements; hashsize <<= 1)
320 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
321 for (i = 0; i < hashsize; i++)
322 LIST_INIT(&hashtbl[i]);
323 *hashmask = hashsize - 1;
328 hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
330 LIST_HEAD(generic, generic) *hashtbl, *hp;
333 for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
334 KASSERT(LIST_EMPTY(hp), ("%s: hash not empty", __func__));
335 kfree(hashtbl, type);
339 * This is a newer version which allocates a hash table of structures.
341 * The returned array will be zero'd. The caller is responsible for
342 * initializing the structures.
345 hashinit_ext(int elements, size_t size, struct malloc_type *type,
352 panic("hashinit: bad elements");
353 for (hashsize = 2; hashsize < elements; hashsize <<= 1)
355 hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO);
356 *hashmask = hashsize - 1;
360 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
361 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
362 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
363 #define NPRIMES NELEM(primes)
366 * General routine to allocate a prime number sized hash table.
369 phashinit(int elements, struct malloc_type *type, u_long *nentries)
372 LIST_HEAD(generic, generic) *hashtbl;
376 panic("phashinit: bad elements");
377 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
381 hashsize = primes[i];
383 hashsize = primes[i - 1];
384 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
385 for (i = 0; i < hashsize; i++)
386 LIST_INIT(&hashtbl[i]);
387 *nentries = hashsize;
392 * This is a newer version which allocates a hash table of structures
393 * in a prime-number size.
395 * The returned array will be zero'd. The caller is responsible for
396 * initializing the structures.
399 phashinit_ext(int elements, size_t size, struct malloc_type *type,
407 panic("phashinit: bad elements");
408 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
412 hashsize = primes[i];
414 hashsize = primes[i - 1];
415 hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO);
416 *nentries = hashsize;
421 * Copyin an iovec. If the iovec array fits, use the preallocated small
422 * iovec structure. If it is too big, dynamically allocate an iovec array
423 * of sufficient size.
428 iovec_copyin(struct iovec *uiov, struct iovec **kiov, struct iovec *siov,
429 size_t iov_cnt, size_t *iov_len)
435 if (iov_cnt > UIO_MAXIOV)
437 if (iov_cnt > UIO_SMALLIOV) {
438 *kiov = kmalloc(sizeof(struct iovec) * iov_cnt, M_IOV,
443 error = copyin(uiov, *kiov, iov_cnt * sizeof(struct iovec));
446 for (i = 0, iovp = *kiov; i < iov_cnt; i++, iovp++) {
448 * Check for both *iov_len overflows and out of
449 * range iovp->iov_len's. We limit to the
450 * capabilities of signed integers.
452 * GCC4 - overflow check opt requires assign/test.
454 len = *iov_len + iovp->iov_len;
462 * From userland disallow iovec's which exceed the sized size
463 * limit as the system calls return ssize_t.
465 * NOTE: Internal kernel interfaces can handle the unsigned
468 if (error == 0 && (ssize_t)*iov_len < 0)
472 iovec_free(kiov, siov);
478 * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
479 * Copyright (c) 1982, 1986, 1991, 1993
480 * The Regents of the University of California. All rights reserved.
481 * (c) UNIX System Laboratories, Inc.
482 * All or some portions of this file are derived from material licensed
483 * to the University of California by American Telephone and Telegraph
484 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
485 * the permission of UNIX System Laboratories, Inc.
487 * Redistribution and use in source and binary forms, with or without
488 * modification, are permitted provided that the following conditions
490 * 1. Redistributions of source code must retain the above copyright
491 * notice, this list of conditions and the following disclaimer.
492 * 2. Redistributions in binary form must reproduce the above copyright
493 * notice, this list of conditions and the following disclaimer in the
494 * documentation and/or other materials provided with the distribution.
495 * 4. Neither the name of the University nor the names of its contributors
496 * may be used to endorse or promote products derived from this software
497 * without specific prior written permission.
499 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
500 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
501 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
502 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
503 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
504 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
505 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
506 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
507 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
508 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
511 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
512 * $FreeBSD: src/sys/i386/i386/uio_machdep.c,v 1.1 2004/03/21 20:28:36 alc Exp $
516 * Implement uiomove(9) from physical memory using lwbuf's to reduce
517 * the creation and destruction of ephemeral mappings.
520 uiomove_fromphys(vm_page_t *ma, vm_offset_t offset, size_t n, struct uio *uio)
522 struct lwbuf lwb_cache;
524 struct thread *td = curthread;
527 vm_offset_t page_offset;
533 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
534 ("uiomove_fromphys: mode"));
535 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
536 ("uiomove_fromphys proc"));
539 save = td->td_flags & TDF_DEADLKTREAT;
540 td->td_flags |= TDF_DEADLKTREAT;
543 while (n > 0 && uio->uio_resid) {
553 page_offset = offset & PAGE_MASK;
554 cnt = min(cnt, PAGE_SIZE - page_offset);
555 m = ma[offset >> PAGE_SHIFT];
556 lwb = lwbuf_alloc(m, &lwb_cache);
557 cp = (char *)lwbuf_kva(lwb) + page_offset;
559 switch (uio->uio_segflg) {
562 * note: removed uioyield (it was the wrong place to
565 if (uio->uio_rw == UIO_READ)
566 error = copyout(cp, iov->iov_base, cnt);
568 error = copyin(iov->iov_base, cp, cnt);
575 if (uio->uio_rw == UIO_READ)
576 bcopy(cp, iov->iov_base, cnt);
578 bcopy(iov->iov_base, cp, cnt);
584 iov->iov_base = (char *)iov->iov_base + cnt;
586 uio->uio_resid -= cnt;
587 uio->uio_offset += cnt;
594 td->td_flags &= ~TDF_DEADLKTREAT;