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>
45 #include <sys/limits.h>
48 #include <sys/mutex.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sched.h>
52 #include <sys/sysctl.h>
53 #include <sys/vnode.h>
56 #include <vm/vm_param.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_map.h>
60 #ifdef SOCKET_SEND_COW
61 #include <vm/vm_object.h>
64 SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
65 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
67 static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault);
69 #ifdef SOCKET_SEND_COW
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;
91 KASSERT((uaddr & PAGE_MASK) == 0,
92 ("vm_pgmoveco: uaddr is not page aligned"));
95 * Herein the physical page is validated and dirtied. It is
96 * unwired in sf_buf_mext().
98 kern_pg = PHYS_TO_VM_PAGE(vtophys(kaddr));
99 vbits = kern_pg->valid;
100 kern_pg->valid = VM_PAGE_BITS_ALL;
101 KASSERT(kern_pg->queue == PQ_NONE && kern_pg->wire_count == 1,
102 ("vm_pgmoveco: kern_pg is not correctly wired"));
104 if ((vm_map_lookup(&map, uaddr,
105 VM_PROT_WRITE, &entry, &uobject,
106 &upindex, &prot, &wired)) != KERN_SUCCESS) {
109 VM_OBJECT_LOCK(uobject);
110 if (vm_page_insert(kern_pg, uobject, upindex) != 0) {
111 kern_pg->valid = vbits;
112 VM_OBJECT_UNLOCK(uobject);
113 vm_map_lookup_done(map, entry);
116 vm_page_dirty(kern_pg);
118 if ((user_pg = vm_page_lookup(uobject, upindex)) != NULL) {
119 if (vm_page_sleep_if_busy(user_pg, TRUE, "vm_pgmoveco"))
121 vm_page_lock(user_pg);
122 pmap_remove_all(user_pg);
123 vm_page_free(user_pg);
124 vm_page_unlock(user_pg);
127 * Even if a physical page does not exist in the
128 * object chain's first object, a physical page from a
129 * backing object may be mapped read only.
131 if (uobject->backing_object != NULL)
132 pmap_remove(map->pmap, uaddr, uaddr + PAGE_SIZE);
134 VM_OBJECT_UNLOCK(uobject);
135 vm_map_lookup_done(map, entry);
136 return(KERN_SUCCESS);
138 #endif /* SOCKET_SEND_COW */
141 copyin_nofault(const void *udaddr, void *kaddr, size_t len)
145 save = vm_fault_disable_pagefaults();
146 error = copyin(udaddr, kaddr, len);
147 vm_fault_enable_pagefaults(save);
152 copyout_nofault(const void *kaddr, void *udaddr, size_t len)
156 save = vm_fault_disable_pagefaults();
157 error = copyout(kaddr, udaddr, len);
158 vm_fault_enable_pagefaults(save);
163 uiomove(void *cp, int n, struct uio *uio)
166 return (uiomove_faultflag(cp, n, uio, 0));
170 uiomove_nofault(void *cp, int n, struct uio *uio)
173 return (uiomove_faultflag(cp, n, uio, 1));
177 uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
182 int error, newflags, save;
187 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
189 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td,
192 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
193 "Calling uiomove()");
195 /* XXX does it make a sense to set TDP_DEADLKTREAT for UIO_SYSSPACE ? */
196 newflags = TDP_DEADLKTREAT;
197 if (uio->uio_segflg == UIO_USERSPACE && nofault) {
199 * Fail if a non-spurious page fault occurs.
201 newflags |= TDP_NOFAULTING | TDP_RESETSPUR;
203 save = curthread_pflags_set(newflags);
205 while (n > 0 && uio->uio_resid) {
216 switch (uio->uio_segflg) {
220 if (uio->uio_rw == UIO_READ)
221 error = copyout(cp, iov->iov_base, cnt);
223 error = copyin(iov->iov_base, cp, cnt);
229 if (uio->uio_rw == UIO_READ)
230 bcopy(cp, iov->iov_base, cnt);
232 bcopy(iov->iov_base, cp, cnt);
237 iov->iov_base = (char *)iov->iov_base + cnt;
239 uio->uio_resid -= cnt;
240 uio->uio_offset += cnt;
241 cp = (char *)cp + cnt;
245 curthread_pflags_restore(save);
250 * Wrapper for uiomove() that validates the arguments against a known-good
251 * kernel buffer. Currently, uiomove accepts a signed (n) argument, which
252 * is almost definitely a bad thing, so we catch that here as well. We
253 * return a runtime failure, but it might be desirable to generate a runtime
254 * assertion failure instead.
257 uiomove_frombuf(void *buf, int buflen, struct uio *uio)
261 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
262 (offset = uio->uio_offset) != uio->uio_offset)
264 if (buflen <= 0 || offset >= buflen)
266 if ((n = buflen - offset) > IOSIZE_MAX)
268 return (uiomove((char *)buf + offset, n, uio));
271 #ifdef SOCKET_RECV_PFLIP
273 * Experimental support for zero-copy I/O
276 userspaceco(void *cp, u_int cnt, struct uio *uio, int disposable)
282 if (uio->uio_rw == UIO_READ) {
283 if ((so_zero_copy_receive != 0)
284 && ((cnt & PAGE_MASK) == 0)
285 && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0)
286 && ((uio->uio_offset & PAGE_MASK) == 0)
287 && ((((intptr_t) cp) & PAGE_MASK) == 0)
288 && (disposable != 0)) {
289 /* SOCKET: use page-trading */
291 * We only want to call vm_pgmoveco() on
292 * disposeable pages, since it gives the
293 * kernel page to the userland process.
295 error = vm_pgmoveco(&curproc->p_vmspace->vm_map,
296 (vm_offset_t)cp, (vm_offset_t)iov->iov_base);
299 * If we get an error back, attempt
300 * to use copyout() instead. The
301 * disposable page should be freed
302 * automatically if we weren't able to move
306 error = copyout(cp, iov->iov_base, cnt);
308 error = copyout(cp, iov->iov_base, cnt);
311 error = copyin(iov->iov_base, cp, cnt);
317 uiomoveco(void *cp, int n, struct uio *uio, int disposable)
323 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
324 ("uiomoveco: mode"));
325 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
328 while (n > 0 && uio->uio_resid) {
339 switch (uio->uio_segflg) {
343 error = userspaceco(cp, cnt, uio, disposable);
349 if (uio->uio_rw == UIO_READ)
350 bcopy(cp, iov->iov_base, cnt);
352 bcopy(iov->iov_base, cp, cnt);
357 iov->iov_base = (char *)iov->iov_base + cnt;
359 uio->uio_resid -= cnt;
360 uio->uio_offset += cnt;
361 cp = (char *)cp + cnt;
366 #endif /* SOCKET_RECV_PFLIP */
369 * Give next character to user as result of read.
372 ureadc(int c, struct uio *uio)
377 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
381 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
384 if (iov->iov_len == 0) {
389 switch (uio->uio_segflg) {
392 if (subyte(iov->iov_base, c) < 0)
397 iov_base = iov->iov_base;
404 iov->iov_base = (char *)iov->iov_base + 1;
412 copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
419 error = copyin(src, dst, len);
422 bcopy(src, dst, len);
425 panic("copyinfrom: bad seg %d\n", seg);
431 copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
432 size_t * __restrict copied, int seg)
438 error = copyinstr(src, dst, len, copied);
441 error = copystr(src, dst, len, copied);
444 panic("copyinstrfrom: bad seg %d\n", seg);
450 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
455 if (iovcnt > UIO_MAXIOV)
457 iovlen = iovcnt * sizeof (struct iovec);
458 *iov = malloc(iovlen, M_IOV, M_WAITOK);
459 error = copyin(iovp, *iov, iovlen);
468 copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
476 if (iovcnt > UIO_MAXIOV)
478 iovlen = iovcnt * sizeof (struct iovec);
479 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
480 iov = (struct iovec *)(uio + 1);
481 error = copyin(iovp, iov, iovlen);
487 uio->uio_iovcnt = iovcnt;
488 uio->uio_segflg = UIO_USERSPACE;
489 uio->uio_offset = -1;
491 for (i = 0; i < iovcnt; i++) {
492 if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
496 uio->uio_resid += iov->iov_len;
504 cloneuio(struct uio *uiop)
509 iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
510 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
512 uio->uio_iov = (struct iovec *)(uio + 1);
513 bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
518 * Map some anonymous memory in user space of size sz, rounded up to the page
522 copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
528 vms = td->td_proc->p_vmspace;
531 * Map somewhere after heap in process memory.
533 PROC_LOCK(td->td_proc);
534 *addr = round_page((vm_offset_t)vms->vm_daddr +
535 lim_max(td->td_proc, RLIMIT_DATA));
536 PROC_UNLOCK(td->td_proc);
538 /* round size up to page boundry */
539 size = (vm_size_t)round_page(sz);
541 error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
542 VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
548 * Unmap memory in user space.
551 copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
559 map = &td->td_proc->p_vmspace->vm_map;
560 size = (vm_size_t)round_page(sz);
562 if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)