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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/limits.h>
49 #include <sys/mutex.h>
51 #include <sys/malloc.h>
52 #include <sys/resourcevar.h>
53 #include <sys/sched.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_map.h>
61 #ifdef ZERO_COPY_SOCKETS
62 #include <vm/vm_param.h>
63 #include <vm/vm_object.h>
66 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
67 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
69 #ifdef ZERO_COPY_SOCKETS
70 /* Declared in uipc_socket.c */
71 extern int so_zero_copy_receive;
74 * Identify the physical page mapped at the given kernel virtual
75 * address. Insert this physical page into the given address space at
76 * the given virtual address, replacing the physical page, if any,
77 * that already exists there.
80 vm_pgmoveco(vm_map_t mapa, vm_offset_t kaddr, vm_offset_t uaddr)
83 vm_page_t kern_pg, user_pg;
90 KASSERT((uaddr & PAGE_MASK) == 0,
91 ("vm_pgmoveco: uaddr is not page aligned"));
94 * Herein the physical page is validated and dirtied. It is
95 * unwired in sf_buf_mext().
97 kern_pg = PHYS_TO_VM_PAGE(vtophys(kaddr));
98 kern_pg->valid = VM_PAGE_BITS_ALL;
99 KASSERT(kern_pg->queue == PQ_NONE && kern_pg->wire_count == 1,
100 ("vm_pgmoveco: kern_pg is not correctly wired"));
102 if ((vm_map_lookup(&map, uaddr,
103 VM_PROT_WRITE, &entry, &uobject,
104 &upindex, &prot, &wired)) != KERN_SUCCESS) {
107 VM_OBJECT_LOCK(uobject);
109 if ((user_pg = vm_page_lookup(uobject, upindex)) != NULL) {
110 if (vm_page_sleep_if_busy(user_pg, TRUE, "vm_pgmoveco"))
112 vm_page_lock_queues();
113 pmap_remove_all(user_pg);
114 vm_page_free(user_pg);
117 * Even if a physical page does not exist in the
118 * object chain's first object, a physical page from a
119 * backing object may be mapped read only.
121 if (uobject->backing_object != NULL)
122 pmap_remove(map->pmap, uaddr, uaddr + PAGE_SIZE);
123 vm_page_lock_queues();
125 vm_page_insert(kern_pg, uobject, upindex);
126 vm_page_dirty(kern_pg);
127 vm_page_unlock_queues();
128 VM_OBJECT_UNLOCK(uobject);
129 vm_map_lookup_done(map, entry);
130 return(KERN_SUCCESS);
132 #endif /* ZERO_COPY_SOCKETS */
135 uiomove(void *cp, int n, struct uio *uio)
137 struct thread *td = curthread;
143 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
145 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
147 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
148 "Calling uiomove()");
150 save = td->td_pflags & TDP_DEADLKTREAT;
151 td->td_pflags |= TDP_DEADLKTREAT;
153 while (n > 0 && uio->uio_resid) {
164 switch (uio->uio_segflg) {
167 if (ticks - PCPU_GET(switchticks) >= hogticks)
169 if (uio->uio_rw == UIO_READ)
170 error = copyout(cp, iov->iov_base, cnt);
172 error = copyin(iov->iov_base, cp, cnt);
178 if (uio->uio_rw == UIO_READ)
179 bcopy(cp, iov->iov_base, cnt);
181 bcopy(iov->iov_base, cp, cnt);
186 iov->iov_base = (char *)iov->iov_base + cnt;
188 uio->uio_resid -= cnt;
189 uio->uio_offset += cnt;
190 cp = (char *)cp + cnt;
195 td->td_pflags &= ~TDP_DEADLKTREAT;
200 * Wrapper for uiomove() that validates the arguments against a known-good
201 * kernel buffer. Currently, uiomove accepts a signed (n) argument, which
202 * is almost definitely a bad thing, so we catch that here as well. We
203 * return a runtime failure, but it might be desirable to generate a runtime
204 * assertion failure instead.
207 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
209 unsigned int offset, n;
211 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
212 (offset = uio->uio_offset) != uio->uio_offset)
214 if (buflen <= 0 || offset >= buflen)
216 if ((n = buflen - offset) > INT_MAX)
218 return (uiomove((char *)buf + offset, n, uio));
221 #ifdef ZERO_COPY_SOCKETS
223 * Experimental support for zero-copy I/O
226 userspaceco(void *cp, u_int cnt, struct uio *uio, int disposable)
232 if (uio->uio_rw == UIO_READ) {
233 if ((so_zero_copy_receive != 0)
234 && ((cnt & PAGE_MASK) == 0)
235 && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0)
236 && ((uio->uio_offset & PAGE_MASK) == 0)
237 && ((((intptr_t) cp) & PAGE_MASK) == 0)
238 && (disposable != 0)) {
239 /* SOCKET: use page-trading */
241 * We only want to call vm_pgmoveco() on
242 * disposeable pages, since it gives the
243 * kernel page to the userland process.
245 error = vm_pgmoveco(&curproc->p_vmspace->vm_map,
246 (vm_offset_t)cp, (vm_offset_t)iov->iov_base);
249 * If we get an error back, attempt
250 * to use copyout() instead. The
251 * disposable page should be freed
252 * automatically if we weren't able to move
256 error = copyout(cp, iov->iov_base, cnt);
258 error = copyout(cp, iov->iov_base, cnt);
261 error = copyin(iov->iov_base, cp, cnt);
267 uiomoveco(void *cp, int n, struct uio *uio, int disposable)
273 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
274 ("uiomoveco: mode"));
275 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
278 while (n > 0 && uio->uio_resid) {
289 switch (uio->uio_segflg) {
292 if (ticks - PCPU_GET(switchticks) >= hogticks)
295 error = userspaceco(cp, cnt, uio, disposable);
302 if (uio->uio_rw == UIO_READ)
303 bcopy(cp, iov->iov_base, cnt);
305 bcopy(iov->iov_base, cp, cnt);
310 iov->iov_base = (char *)iov->iov_base + cnt;
312 uio->uio_resid -= cnt;
313 uio->uio_offset += cnt;
314 cp = (char *)cp + cnt;
319 #endif /* ZERO_COPY_SOCKETS */
322 * Give next character to user as result of read.
325 ureadc(int c, struct uio *uio)
330 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
334 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
337 if (iov->iov_len == 0) {
342 switch (uio->uio_segflg) {
345 if (subyte(iov->iov_base, c) < 0)
350 iov_base = iov->iov_base;
352 iov->iov_base = iov_base;
358 iov->iov_base = (char *)iov->iov_base + 1;
366 * General routine to allocate a hash table with control of memory flags.
369 hashinit_flags(int elements, struct malloc_type *type, u_long *hashmask,
373 LIST_HEAD(generic, generic) *hashtbl;
377 panic("hashinit: bad elements");
379 /* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */
380 KASSERT((flags & HASH_WAITOK) ^ (flags & HASH_NOWAIT),
381 ("Bad flags (0x%x) passed to hashinit_flags", flags));
383 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
387 if (flags & HASH_NOWAIT)
388 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl),
391 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl),
394 if (hashtbl != NULL) {
395 for (i = 0; i < hashsize; i++)
396 LIST_INIT(&hashtbl[i]);
397 *hashmask = hashsize - 1;
403 * Allocate and initialize a hash table with default flag: may sleep.
406 hashinit(int elements, struct malloc_type *type, u_long *hashmask)
409 return (hashinit_flags(elements, type, hashmask, HASH_WAITOK));
413 hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
415 LIST_HEAD(generic, generic) *hashtbl, *hp;
418 for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
420 panic("hashdestroy: hash not empty");
424 static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
425 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
426 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
427 #define NPRIMES (sizeof(primes) / sizeof(primes[0]))
430 * General routine to allocate a prime number sized hash table.
433 phashinit(int elements, struct malloc_type *type, u_long *nentries)
436 LIST_HEAD(generic, generic) *hashtbl;
440 panic("phashinit: bad elements");
441 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
445 hashsize = primes[i];
447 hashsize = primes[i - 1];
448 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
449 for (i = 0; i < hashsize; i++)
450 LIST_INIT(&hashtbl[i]);
451 *nentries = hashsize;
459 kern_yield(PRI_USER);
463 copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
470 error = copyin(src, dst, len);
473 bcopy(src, dst, len);
476 panic("copyinfrom: bad seg %d\n", seg);
482 copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
483 size_t * __restrict copied, int seg)
489 error = copyinstr(src, dst, len, copied);
492 error = copystr(src, dst, len, copied);
495 panic("copyinstrfrom: bad seg %d\n", seg);
501 copyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
506 if (iovcnt > UIO_MAXIOV)
508 iovlen = iovcnt * sizeof (struct iovec);
509 *iov = malloc(iovlen, M_IOV, M_WAITOK);
510 error = copyin(iovp, *iov, iovlen);
519 copyinuio(struct iovec *iovp, u_int iovcnt, struct uio **uiop)
527 if (iovcnt > UIO_MAXIOV)
529 iovlen = iovcnt * sizeof (struct iovec);
530 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
531 iov = (struct iovec *)(uio + 1);
532 error = copyin(iovp, iov, iovlen);
538 uio->uio_iovcnt = iovcnt;
539 uio->uio_segflg = UIO_USERSPACE;
540 uio->uio_offset = -1;
542 for (i = 0; i < iovcnt; i++) {
543 if (iov->iov_len > INT_MAX - uio->uio_resid) {
547 uio->uio_resid += iov->iov_len;
555 cloneuio(struct uio *uiop)
560 iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
561 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
563 uio->uio_iov = (struct iovec *)(uio + 1);
564 bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
569 * Map some anonymous memory in user space of size sz, rounded up to the page
573 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
579 vms = td->td_proc->p_vmspace;
582 * Map somewhere after heap in process memory.
584 PROC_LOCK(td->td_proc);
585 *addr = round_page((vm_offset_t)vms->vm_daddr +
586 lim_max(td->td_proc, RLIMIT_DATA));
587 PROC_UNLOCK(td->td_proc);
589 /* round size up to page boundry */
590 size = (vm_size_t)round_page(sz);
592 error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
593 VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
599 * Unmap memory in user space.
602 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
610 map = &td->td_proc->p_vmspace->vm_map;
611 size = (vm_size_t)round_page(sz);
613 if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)