128993443SEd Schouten /*- 228993443SEd Schouten * Copyright (c) 1982, 1986, 1991, 1993 328993443SEd Schouten * The Regents of the University of California. All rights reserved. 428993443SEd Schouten * (c) UNIX System Laboratories, Inc. 528993443SEd Schouten * All or some portions of this file are derived from material licensed 628993443SEd Schouten * to the University of California by American Telephone and Telegraph 728993443SEd Schouten * Co. or Unix System Laboratories, Inc. and are reproduced herein with 828993443SEd Schouten * the permission of UNIX System Laboratories, Inc. 928993443SEd Schouten * 1028993443SEd Schouten * Redistribution and use in source and binary forms, with or without 1128993443SEd Schouten * modification, are permitted provided that the following conditions 1228993443SEd Schouten * are met: 1328993443SEd Schouten * 1. Redistributions of source code must retain the above copyright 1428993443SEd Schouten * notice, this list of conditions and the following disclaimer. 1528993443SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright 1628993443SEd Schouten * notice, this list of conditions and the following disclaimer in the 1728993443SEd Schouten * documentation and/or other materials provided with the distribution. 1828993443SEd Schouten * 4. Neither the name of the University nor the names of its contributors 1928993443SEd Schouten * may be used to endorse or promote products derived from this software 2028993443SEd Schouten * without specific prior written permission. 2128993443SEd Schouten * 2228993443SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2328993443SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2428993443SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2528993443SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2628993443SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2728993443SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2828993443SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2928993443SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3028993443SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3128993443SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3228993443SEd Schouten * SUCH DAMAGE. 3328993443SEd Schouten * 3428993443SEd Schouten * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 3528993443SEd Schouten */ 3628993443SEd Schouten 3728993443SEd Schouten #include <sys/cdefs.h> 3828993443SEd Schouten __FBSDID("$FreeBSD$"); 3928993443SEd Schouten 4028993443SEd Schouten #include "opt_zero.h" 4128993443SEd Schouten 4228993443SEd Schouten #include <sys/param.h> 4328993443SEd Schouten #include <sys/systm.h> 4428993443SEd Schouten #include <sys/kernel.h> 4528993443SEd Schouten #include <sys/limits.h> 4628993443SEd Schouten #include <sys/lock.h> 470f502d1cSKonstantin Belousov #include <sys/mman.h> 4828993443SEd Schouten #include <sys/proc.h> 490f502d1cSKonstantin Belousov #include <sys/resourcevar.h> 50*89f6b863SAttilio Rao #include <sys/rwlock.h> 5128993443SEd Schouten #include <sys/sched.h> 5228993443SEd Schouten #include <sys/sysctl.h> 5328993443SEd Schouten #include <sys/vnode.h> 5428993443SEd Schouten 5528993443SEd Schouten #include <vm/vm.h> 561c771f92SKonstantin Belousov #include <vm/vm_param.h> 570f502d1cSKonstantin Belousov #include <vm/vm_extern.h> 5828993443SEd Schouten #include <vm/vm_page.h> 5928993443SEd Schouten #include <vm/vm_map.h> 60e37e60c3SAndre Oppermann #ifdef SOCKET_SEND_COW 6128993443SEd Schouten #include <vm/vm_object.h> 6228993443SEd Schouten #endif 6328993443SEd Schouten 6428993443SEd Schouten SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV, 6528993443SEd Schouten "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)"); 6628993443SEd Schouten 672801687dSKonstantin Belousov static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault); 682801687dSKonstantin Belousov 69e37e60c3SAndre Oppermann #ifdef SOCKET_SEND_COW 7028993443SEd Schouten /* Declared in uipc_socket.c */ 7128993443SEd Schouten extern int so_zero_copy_receive; 7228993443SEd Schouten 7328993443SEd Schouten /* 7428993443SEd Schouten * Identify the physical page mapped at the given kernel virtual 7528993443SEd Schouten * address. Insert this physical page into the given address space at 7628993443SEd Schouten * the given virtual address, replacing the physical page, if any, 7728993443SEd Schouten * that already exists there. 7828993443SEd Schouten */ 7928993443SEd Schouten static int 8028993443SEd Schouten vm_pgmoveco(vm_map_t mapa, vm_offset_t kaddr, vm_offset_t uaddr) 8128993443SEd Schouten { 8228993443SEd Schouten vm_map_t map = mapa; 8328993443SEd Schouten vm_page_t kern_pg, user_pg; 8428993443SEd Schouten vm_object_t uobject; 8528993443SEd Schouten vm_map_entry_t entry; 8628993443SEd Schouten vm_pindex_t upindex; 8728993443SEd Schouten vm_prot_t prot; 8828993443SEd Schouten boolean_t wired; 8928993443SEd Schouten 9028993443SEd Schouten KASSERT((uaddr & PAGE_MASK) == 0, 9128993443SEd Schouten ("vm_pgmoveco: uaddr is not page aligned")); 9228993443SEd Schouten 9328993443SEd Schouten /* 9428993443SEd Schouten * Herein the physical page is validated and dirtied. It is 9528993443SEd Schouten * unwired in sf_buf_mext(). 9628993443SEd Schouten */ 9728993443SEd Schouten kern_pg = PHYS_TO_VM_PAGE(vtophys(kaddr)); 9828993443SEd Schouten kern_pg->valid = VM_PAGE_BITS_ALL; 9928993443SEd Schouten KASSERT(kern_pg->queue == PQ_NONE && kern_pg->wire_count == 1, 10028993443SEd Schouten ("vm_pgmoveco: kern_pg is not correctly wired")); 10128993443SEd Schouten 10228993443SEd Schouten if ((vm_map_lookup(&map, uaddr, 10328993443SEd Schouten VM_PROT_WRITE, &entry, &uobject, 10428993443SEd Schouten &upindex, &prot, &wired)) != KERN_SUCCESS) { 10528993443SEd Schouten return(EFAULT); 10628993443SEd Schouten } 107*89f6b863SAttilio Rao VM_OBJECT_WLOCK(uobject); 10828993443SEd Schouten retry: 10928993443SEd Schouten if ((user_pg = vm_page_lookup(uobject, upindex)) != NULL) { 11028993443SEd Schouten if (vm_page_sleep_if_busy(user_pg, TRUE, "vm_pgmoveco")) 11128993443SEd Schouten goto retry; 1125ac59343SAlan Cox vm_page_lock(user_pg); 11328993443SEd Schouten pmap_remove_all(user_pg); 11428993443SEd Schouten vm_page_free(user_pg); 1155ac59343SAlan Cox vm_page_unlock(user_pg); 11628993443SEd Schouten } else { 11728993443SEd Schouten /* 11828993443SEd Schouten * Even if a physical page does not exist in the 11928993443SEd Schouten * object chain's first object, a physical page from a 12028993443SEd Schouten * backing object may be mapped read only. 12128993443SEd Schouten */ 12228993443SEd Schouten if (uobject->backing_object != NULL) 12328993443SEd Schouten pmap_remove(map->pmap, uaddr, uaddr + PAGE_SIZE); 12428993443SEd Schouten } 12528993443SEd Schouten vm_page_insert(kern_pg, uobject, upindex); 12628993443SEd Schouten vm_page_dirty(kern_pg); 127*89f6b863SAttilio Rao VM_OBJECT_WUNLOCK(uobject); 12828993443SEd Schouten vm_map_lookup_done(map, entry); 12928993443SEd Schouten return(KERN_SUCCESS); 13028993443SEd Schouten } 131e37e60c3SAndre Oppermann #endif /* SOCKET_SEND_COW */ 13228993443SEd Schouten 13328993443SEd Schouten int 1342801687dSKonstantin Belousov copyin_nofault(const void *udaddr, void *kaddr, size_t len) 1352801687dSKonstantin Belousov { 1362801687dSKonstantin Belousov int error, save; 1372801687dSKonstantin Belousov 1382801687dSKonstantin Belousov save = vm_fault_disable_pagefaults(); 1392801687dSKonstantin Belousov error = copyin(udaddr, kaddr, len); 1402801687dSKonstantin Belousov vm_fault_enable_pagefaults(save); 1412801687dSKonstantin Belousov return (error); 1422801687dSKonstantin Belousov } 1432801687dSKonstantin Belousov 1442801687dSKonstantin Belousov int 1452801687dSKonstantin Belousov copyout_nofault(const void *kaddr, void *udaddr, size_t len) 1462801687dSKonstantin Belousov { 1472801687dSKonstantin Belousov int error, save; 1482801687dSKonstantin Belousov 1492801687dSKonstantin Belousov save = vm_fault_disable_pagefaults(); 1502801687dSKonstantin Belousov error = copyout(kaddr, udaddr, len); 1512801687dSKonstantin Belousov vm_fault_enable_pagefaults(save); 1522801687dSKonstantin Belousov return (error); 1532801687dSKonstantin Belousov } 1542801687dSKonstantin Belousov 155dd0b4fb6SKonstantin Belousov #define PHYS_PAGE_COUNT(len) (howmany(len, PAGE_SIZE) + 1) 156dd0b4fb6SKonstantin Belousov 157dd0b4fb6SKonstantin Belousov int 158dd0b4fb6SKonstantin Belousov physcopyin(void *src, vm_paddr_t dst, size_t len) 159dd0b4fb6SKonstantin Belousov { 160dd0b4fb6SKonstantin Belousov vm_page_t m[PHYS_PAGE_COUNT(len)]; 161dd0b4fb6SKonstantin Belousov struct iovec iov[1]; 162dd0b4fb6SKonstantin Belousov struct uio uio; 163dd0b4fb6SKonstantin Belousov int i; 164dd0b4fb6SKonstantin Belousov 165dd0b4fb6SKonstantin Belousov iov[0].iov_base = src; 166dd0b4fb6SKonstantin Belousov iov[0].iov_len = len; 167dd0b4fb6SKonstantin Belousov uio.uio_iov = iov; 168dd0b4fb6SKonstantin Belousov uio.uio_iovcnt = 1; 169dd0b4fb6SKonstantin Belousov uio.uio_offset = 0; 170dd0b4fb6SKonstantin Belousov uio.uio_resid = len; 171dd0b4fb6SKonstantin Belousov uio.uio_segflg = UIO_SYSSPACE; 172dd0b4fb6SKonstantin Belousov uio.uio_rw = UIO_WRITE; 173dd0b4fb6SKonstantin Belousov for (i = 0; i < PHYS_PAGE_COUNT(len); i++, dst += PAGE_SIZE) 174dd0b4fb6SKonstantin Belousov m[i] = PHYS_TO_VM_PAGE(dst); 175dd0b4fb6SKonstantin Belousov return (uiomove_fromphys(m, dst & PAGE_MASK, len, &uio)); 176dd0b4fb6SKonstantin Belousov } 177dd0b4fb6SKonstantin Belousov 178dd0b4fb6SKonstantin Belousov int 179dd0b4fb6SKonstantin Belousov physcopyout(vm_paddr_t src, void *dst, size_t len) 180dd0b4fb6SKonstantin Belousov { 181dd0b4fb6SKonstantin Belousov vm_page_t m[PHYS_PAGE_COUNT(len)]; 182dd0b4fb6SKonstantin Belousov struct iovec iov[1]; 183dd0b4fb6SKonstantin Belousov struct uio uio; 184dd0b4fb6SKonstantin Belousov int i; 185dd0b4fb6SKonstantin Belousov 186dd0b4fb6SKonstantin Belousov iov[0].iov_base = dst; 187dd0b4fb6SKonstantin Belousov iov[0].iov_len = len; 188dd0b4fb6SKonstantin Belousov uio.uio_iov = iov; 189dd0b4fb6SKonstantin Belousov uio.uio_iovcnt = 1; 190dd0b4fb6SKonstantin Belousov uio.uio_offset = 0; 191dd0b4fb6SKonstantin Belousov uio.uio_resid = len; 192dd0b4fb6SKonstantin Belousov uio.uio_segflg = UIO_SYSSPACE; 193dd0b4fb6SKonstantin Belousov uio.uio_rw = UIO_READ; 194dd0b4fb6SKonstantin Belousov for (i = 0; i < PHYS_PAGE_COUNT(len); i++, src += PAGE_SIZE) 195dd0b4fb6SKonstantin Belousov m[i] = PHYS_TO_VM_PAGE(src); 196dd0b4fb6SKonstantin Belousov return (uiomove_fromphys(m, src & PAGE_MASK, len, &uio)); 197dd0b4fb6SKonstantin Belousov } 198dd0b4fb6SKonstantin Belousov 199dd0b4fb6SKonstantin Belousov #undef PHYS_PAGE_COUNT 200dd0b4fb6SKonstantin Belousov 2012801687dSKonstantin Belousov int 20228993443SEd Schouten uiomove(void *cp, int n, struct uio *uio) 20328993443SEd Schouten { 2042801687dSKonstantin Belousov 2052801687dSKonstantin Belousov return (uiomove_faultflag(cp, n, uio, 0)); 2062801687dSKonstantin Belousov } 2072801687dSKonstantin Belousov 2082801687dSKonstantin Belousov int 2092801687dSKonstantin Belousov uiomove_nofault(void *cp, int n, struct uio *uio) 2102801687dSKonstantin Belousov { 2112801687dSKonstantin Belousov 2122801687dSKonstantin Belousov return (uiomove_faultflag(cp, n, uio, 1)); 2132801687dSKonstantin Belousov } 2142801687dSKonstantin Belousov 2152801687dSKonstantin Belousov static int 2162801687dSKonstantin Belousov uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault) 2172801687dSKonstantin Belousov { 2182801687dSKonstantin Belousov struct thread *td; 21928993443SEd Schouten struct iovec *iov; 220526d0bd5SKonstantin Belousov size_t cnt; 2212801687dSKonstantin Belousov int error, newflags, save; 2222801687dSKonstantin Belousov 2232801687dSKonstantin Belousov td = curthread; 2242801687dSKonstantin Belousov error = 0; 22528993443SEd Schouten 22628993443SEd Schouten KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 22728993443SEd Schouten ("uiomove: mode")); 2282801687dSKonstantin Belousov KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td, 22928993443SEd Schouten ("uiomove proc")); 2302801687dSKonstantin Belousov if (!nofault) 23128993443SEd Schouten WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 23228993443SEd Schouten "Calling uiomove()"); 23328993443SEd Schouten 2342801687dSKonstantin Belousov /* XXX does it make a sense to set TDP_DEADLKTREAT for UIO_SYSSPACE ? */ 2352801687dSKonstantin Belousov newflags = TDP_DEADLKTREAT; 2365730afc9SAlan Cox if (uio->uio_segflg == UIO_USERSPACE && nofault) { 2375730afc9SAlan Cox /* 2385730afc9SAlan Cox * Fail if a non-spurious page fault occurs. 2395730afc9SAlan Cox */ 2405730afc9SAlan Cox newflags |= TDP_NOFAULTING | TDP_RESETSPUR; 2415730afc9SAlan Cox } 2422801687dSKonstantin Belousov save = curthread_pflags_set(newflags); 24328993443SEd Schouten 24428993443SEd Schouten while (n > 0 && uio->uio_resid) { 24528993443SEd Schouten iov = uio->uio_iov; 24628993443SEd Schouten cnt = iov->iov_len; 24728993443SEd Schouten if (cnt == 0) { 24828993443SEd Schouten uio->uio_iov++; 24928993443SEd Schouten uio->uio_iovcnt--; 25028993443SEd Schouten continue; 25128993443SEd Schouten } 25228993443SEd Schouten if (cnt > n) 25328993443SEd Schouten cnt = n; 25428993443SEd Schouten 25528993443SEd Schouten switch (uio->uio_segflg) { 25628993443SEd Schouten 25728993443SEd Schouten case UIO_USERSPACE: 25808b163faSMatthew D Fleming maybe_yield(); 25928993443SEd Schouten if (uio->uio_rw == UIO_READ) 26028993443SEd Schouten error = copyout(cp, iov->iov_base, cnt); 26128993443SEd Schouten else 26228993443SEd Schouten error = copyin(iov->iov_base, cp, cnt); 26328993443SEd Schouten if (error) 26428993443SEd Schouten goto out; 26528993443SEd Schouten break; 26628993443SEd Schouten 26728993443SEd Schouten case UIO_SYSSPACE: 26828993443SEd Schouten if (uio->uio_rw == UIO_READ) 26928993443SEd Schouten bcopy(cp, iov->iov_base, cnt); 27028993443SEd Schouten else 27128993443SEd Schouten bcopy(iov->iov_base, cp, cnt); 27228993443SEd Schouten break; 27328993443SEd Schouten case UIO_NOCOPY: 27428993443SEd Schouten break; 27528993443SEd Schouten } 27628993443SEd Schouten iov->iov_base = (char *)iov->iov_base + cnt; 27728993443SEd Schouten iov->iov_len -= cnt; 27828993443SEd Schouten uio->uio_resid -= cnt; 27928993443SEd Schouten uio->uio_offset += cnt; 28028993443SEd Schouten cp = (char *)cp + cnt; 28128993443SEd Schouten n -= cnt; 28228993443SEd Schouten } 28328993443SEd Schouten out: 2842801687dSKonstantin Belousov curthread_pflags_restore(save); 28528993443SEd Schouten return (error); 28628993443SEd Schouten } 28728993443SEd Schouten 28828993443SEd Schouten /* 28928993443SEd Schouten * Wrapper for uiomove() that validates the arguments against a known-good 29028993443SEd Schouten * kernel buffer. Currently, uiomove accepts a signed (n) argument, which 29128993443SEd Schouten * is almost definitely a bad thing, so we catch that here as well. We 29228993443SEd Schouten * return a runtime failure, but it might be desirable to generate a runtime 29328993443SEd Schouten * assertion failure instead. 29428993443SEd Schouten */ 29528993443SEd Schouten int 29628993443SEd Schouten uiomove_frombuf(void *buf, int buflen, struct uio *uio) 29728993443SEd Schouten { 298526d0bd5SKonstantin Belousov size_t offset, n; 29928993443SEd Schouten 30028993443SEd Schouten if (uio->uio_offset < 0 || uio->uio_resid < 0 || 30128993443SEd Schouten (offset = uio->uio_offset) != uio->uio_offset) 30228993443SEd Schouten return (EINVAL); 30328993443SEd Schouten if (buflen <= 0 || offset >= buflen) 30428993443SEd Schouten return (0); 305526d0bd5SKonstantin Belousov if ((n = buflen - offset) > IOSIZE_MAX) 30628993443SEd Schouten return (EINVAL); 30728993443SEd Schouten return (uiomove((char *)buf + offset, n, uio)); 30828993443SEd Schouten } 30928993443SEd Schouten 310e37e60c3SAndre Oppermann #ifdef SOCKET_RECV_PFLIP 31128993443SEd Schouten /* 31228993443SEd Schouten * Experimental support for zero-copy I/O 31328993443SEd Schouten */ 31428993443SEd Schouten static int 31528993443SEd Schouten userspaceco(void *cp, u_int cnt, struct uio *uio, int disposable) 31628993443SEd Schouten { 31728993443SEd Schouten struct iovec *iov; 31828993443SEd Schouten int error; 31928993443SEd Schouten 32028993443SEd Schouten iov = uio->uio_iov; 32128993443SEd Schouten if (uio->uio_rw == UIO_READ) { 32228993443SEd Schouten if ((so_zero_copy_receive != 0) 32328993443SEd Schouten && ((cnt & PAGE_MASK) == 0) 32428993443SEd Schouten && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) 32528993443SEd Schouten && ((uio->uio_offset & PAGE_MASK) == 0) 32628993443SEd Schouten && ((((intptr_t) cp) & PAGE_MASK) == 0) 32728993443SEd Schouten && (disposable != 0)) { 32828993443SEd Schouten /* SOCKET: use page-trading */ 32928993443SEd Schouten /* 33028993443SEd Schouten * We only want to call vm_pgmoveco() on 33128993443SEd Schouten * disposeable pages, since it gives the 33228993443SEd Schouten * kernel page to the userland process. 33328993443SEd Schouten */ 33428993443SEd Schouten error = vm_pgmoveco(&curproc->p_vmspace->vm_map, 33528993443SEd Schouten (vm_offset_t)cp, (vm_offset_t)iov->iov_base); 33628993443SEd Schouten 33728993443SEd Schouten /* 33828993443SEd Schouten * If we get an error back, attempt 33928993443SEd Schouten * to use copyout() instead. The 34028993443SEd Schouten * disposable page should be freed 34128993443SEd Schouten * automatically if we weren't able to move 34228993443SEd Schouten * it into userland. 34328993443SEd Schouten */ 34428993443SEd Schouten if (error != 0) 34528993443SEd Schouten error = copyout(cp, iov->iov_base, cnt); 34628993443SEd Schouten } else { 34728993443SEd Schouten error = copyout(cp, iov->iov_base, cnt); 34828993443SEd Schouten } 34928993443SEd Schouten } else { 35028993443SEd Schouten error = copyin(iov->iov_base, cp, cnt); 35128993443SEd Schouten } 35228993443SEd Schouten return (error); 35328993443SEd Schouten } 35428993443SEd Schouten 35528993443SEd Schouten int 35628993443SEd Schouten uiomoveco(void *cp, int n, struct uio *uio, int disposable) 35728993443SEd Schouten { 35828993443SEd Schouten struct iovec *iov; 35928993443SEd Schouten u_int cnt; 36028993443SEd Schouten int error; 36128993443SEd Schouten 36228993443SEd Schouten KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 36328993443SEd Schouten ("uiomoveco: mode")); 36428993443SEd Schouten KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 36528993443SEd Schouten ("uiomoveco proc")); 36628993443SEd Schouten 36728993443SEd Schouten while (n > 0 && uio->uio_resid) { 36828993443SEd Schouten iov = uio->uio_iov; 36928993443SEd Schouten cnt = iov->iov_len; 37028993443SEd Schouten if (cnt == 0) { 37128993443SEd Schouten uio->uio_iov++; 37228993443SEd Schouten uio->uio_iovcnt--; 37328993443SEd Schouten continue; 37428993443SEd Schouten } 37528993443SEd Schouten if (cnt > n) 37628993443SEd Schouten cnt = n; 37728993443SEd Schouten 37828993443SEd Schouten switch (uio->uio_segflg) { 37928993443SEd Schouten 38028993443SEd Schouten case UIO_USERSPACE: 38108b163faSMatthew D Fleming maybe_yield(); 38228993443SEd Schouten error = userspaceco(cp, cnt, uio, disposable); 38328993443SEd Schouten if (error) 38428993443SEd Schouten return (error); 38528993443SEd Schouten break; 38628993443SEd Schouten 38728993443SEd Schouten case UIO_SYSSPACE: 38828993443SEd Schouten if (uio->uio_rw == UIO_READ) 38928993443SEd Schouten bcopy(cp, iov->iov_base, cnt); 39028993443SEd Schouten else 39128993443SEd Schouten bcopy(iov->iov_base, cp, cnt); 39228993443SEd Schouten break; 39328993443SEd Schouten case UIO_NOCOPY: 39428993443SEd Schouten break; 39528993443SEd Schouten } 39628993443SEd Schouten iov->iov_base = (char *)iov->iov_base + cnt; 39728993443SEd Schouten iov->iov_len -= cnt; 39828993443SEd Schouten uio->uio_resid -= cnt; 39928993443SEd Schouten uio->uio_offset += cnt; 40028993443SEd Schouten cp = (char *)cp + cnt; 40128993443SEd Schouten n -= cnt; 40228993443SEd Schouten } 40328993443SEd Schouten return (0); 40428993443SEd Schouten } 405e37e60c3SAndre Oppermann #endif /* SOCKET_RECV_PFLIP */ 40628993443SEd Schouten 40728993443SEd Schouten /* 40828993443SEd Schouten * Give next character to user as result of read. 40928993443SEd Schouten */ 41028993443SEd Schouten int 41128993443SEd Schouten ureadc(int c, struct uio *uio) 41228993443SEd Schouten { 41328993443SEd Schouten struct iovec *iov; 41428993443SEd Schouten char *iov_base; 41528993443SEd Schouten 41628993443SEd Schouten WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 41728993443SEd Schouten "Calling ureadc()"); 41828993443SEd Schouten 41928993443SEd Schouten again: 42028993443SEd Schouten if (uio->uio_iovcnt == 0 || uio->uio_resid == 0) 42128993443SEd Schouten panic("ureadc"); 42228993443SEd Schouten iov = uio->uio_iov; 42328993443SEd Schouten if (iov->iov_len == 0) { 42428993443SEd Schouten uio->uio_iovcnt--; 42528993443SEd Schouten uio->uio_iov++; 42628993443SEd Schouten goto again; 42728993443SEd Schouten } 42828993443SEd Schouten switch (uio->uio_segflg) { 42928993443SEd Schouten 43028993443SEd Schouten case UIO_USERSPACE: 43128993443SEd Schouten if (subyte(iov->iov_base, c) < 0) 43228993443SEd Schouten return (EFAULT); 43328993443SEd Schouten break; 43428993443SEd Schouten 43528993443SEd Schouten case UIO_SYSSPACE: 43628993443SEd Schouten iov_base = iov->iov_base; 43728993443SEd Schouten *iov_base = c; 43828993443SEd Schouten break; 43928993443SEd Schouten 44028993443SEd Schouten case UIO_NOCOPY: 44128993443SEd Schouten break; 44228993443SEd Schouten } 44328993443SEd Schouten iov->iov_base = (char *)iov->iov_base + 1; 44428993443SEd Schouten iov->iov_len--; 44528993443SEd Schouten uio->uio_resid--; 44628993443SEd Schouten uio->uio_offset++; 44728993443SEd Schouten return (0); 44828993443SEd Schouten } 44928993443SEd Schouten 45028993443SEd Schouten int 45128993443SEd Schouten copyinfrom(const void * __restrict src, void * __restrict dst, size_t len, 45228993443SEd Schouten int seg) 45328993443SEd Schouten { 45428993443SEd Schouten int error = 0; 45528993443SEd Schouten 45628993443SEd Schouten switch (seg) { 45728993443SEd Schouten case UIO_USERSPACE: 45828993443SEd Schouten error = copyin(src, dst, len); 45928993443SEd Schouten break; 46028993443SEd Schouten case UIO_SYSSPACE: 46128993443SEd Schouten bcopy(src, dst, len); 46228993443SEd Schouten break; 46328993443SEd Schouten default: 46428993443SEd Schouten panic("copyinfrom: bad seg %d\n", seg); 46528993443SEd Schouten } 46628993443SEd Schouten return (error); 46728993443SEd Schouten } 46828993443SEd Schouten 46928993443SEd Schouten int 47028993443SEd Schouten copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len, 47128993443SEd Schouten size_t * __restrict copied, int seg) 47228993443SEd Schouten { 47328993443SEd Schouten int error = 0; 47428993443SEd Schouten 47528993443SEd Schouten switch (seg) { 47628993443SEd Schouten case UIO_USERSPACE: 47728993443SEd Schouten error = copyinstr(src, dst, len, copied); 47828993443SEd Schouten break; 47928993443SEd Schouten case UIO_SYSSPACE: 48028993443SEd Schouten error = copystr(src, dst, len, copied); 48128993443SEd Schouten break; 48228993443SEd Schouten default: 48328993443SEd Schouten panic("copyinstrfrom: bad seg %d\n", seg); 48428993443SEd Schouten } 48528993443SEd Schouten return (error); 48628993443SEd Schouten } 48728993443SEd Schouten 48828993443SEd Schouten int 489cfb09e00SAlfred Perlstein copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error) 49028993443SEd Schouten { 49128993443SEd Schouten u_int iovlen; 49228993443SEd Schouten 49328993443SEd Schouten *iov = NULL; 49428993443SEd Schouten if (iovcnt > UIO_MAXIOV) 49528993443SEd Schouten return (error); 49628993443SEd Schouten iovlen = iovcnt * sizeof (struct iovec); 49728993443SEd Schouten *iov = malloc(iovlen, M_IOV, M_WAITOK); 49828993443SEd Schouten error = copyin(iovp, *iov, iovlen); 49928993443SEd Schouten if (error) { 50028993443SEd Schouten free(*iov, M_IOV); 50128993443SEd Schouten *iov = NULL; 50228993443SEd Schouten } 50328993443SEd Schouten return (error); 50428993443SEd Schouten } 50528993443SEd Schouten 50628993443SEd Schouten int 507cfb09e00SAlfred Perlstein copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop) 50828993443SEd Schouten { 50928993443SEd Schouten struct iovec *iov; 51028993443SEd Schouten struct uio *uio; 51128993443SEd Schouten u_int iovlen; 51228993443SEd Schouten int error, i; 51328993443SEd Schouten 51428993443SEd Schouten *uiop = NULL; 51528993443SEd Schouten if (iovcnt > UIO_MAXIOV) 51628993443SEd Schouten return (EINVAL); 51728993443SEd Schouten iovlen = iovcnt * sizeof (struct iovec); 51828993443SEd Schouten uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 51928993443SEd Schouten iov = (struct iovec *)(uio + 1); 52028993443SEd Schouten error = copyin(iovp, iov, iovlen); 52128993443SEd Schouten if (error) { 52228993443SEd Schouten free(uio, M_IOV); 52328993443SEd Schouten return (error); 52428993443SEd Schouten } 52528993443SEd Schouten uio->uio_iov = iov; 52628993443SEd Schouten uio->uio_iovcnt = iovcnt; 52728993443SEd Schouten uio->uio_segflg = UIO_USERSPACE; 52828993443SEd Schouten uio->uio_offset = -1; 52928993443SEd Schouten uio->uio_resid = 0; 53028993443SEd Schouten for (i = 0; i < iovcnt; i++) { 531526d0bd5SKonstantin Belousov if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) { 53228993443SEd Schouten free(uio, M_IOV); 53328993443SEd Schouten return (EINVAL); 53428993443SEd Schouten } 53528993443SEd Schouten uio->uio_resid += iov->iov_len; 53628993443SEd Schouten iov++; 53728993443SEd Schouten } 53828993443SEd Schouten *uiop = uio; 53928993443SEd Schouten return (0); 54028993443SEd Schouten } 54128993443SEd Schouten 54228993443SEd Schouten struct uio * 54328993443SEd Schouten cloneuio(struct uio *uiop) 54428993443SEd Schouten { 54528993443SEd Schouten struct uio *uio; 54628993443SEd Schouten int iovlen; 54728993443SEd Schouten 54828993443SEd Schouten iovlen = uiop->uio_iovcnt * sizeof (struct iovec); 54928993443SEd Schouten uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 55028993443SEd Schouten *uio = *uiop; 55128993443SEd Schouten uio->uio_iov = (struct iovec *)(uio + 1); 55228993443SEd Schouten bcopy(uiop->uio_iov, uio->uio_iov, iovlen); 55328993443SEd Schouten return (uio); 55428993443SEd Schouten } 5550f502d1cSKonstantin Belousov 5560f502d1cSKonstantin Belousov /* 5570f502d1cSKonstantin Belousov * Map some anonymous memory in user space of size sz, rounded up to the page 5580f502d1cSKonstantin Belousov * boundary. 5590f502d1cSKonstantin Belousov */ 5600f502d1cSKonstantin Belousov int 5610f502d1cSKonstantin Belousov copyout_map(struct thread *td, vm_offset_t *addr, size_t sz) 5620f502d1cSKonstantin Belousov { 563cce6e354SKonstantin Belousov struct vmspace *vms; 5640f502d1cSKonstantin Belousov int error; 5650f502d1cSKonstantin Belousov vm_size_t size; 5660f502d1cSKonstantin Belousov 567cce6e354SKonstantin Belousov vms = td->td_proc->p_vmspace; 568cce6e354SKonstantin Belousov 5690f502d1cSKonstantin Belousov /* 5700f502d1cSKonstantin Belousov * Map somewhere after heap in process memory. 5710f502d1cSKonstantin Belousov */ 5720f502d1cSKonstantin Belousov PROC_LOCK(td->td_proc); 5730f502d1cSKonstantin Belousov *addr = round_page((vm_offset_t)vms->vm_daddr + 5740f502d1cSKonstantin Belousov lim_max(td->td_proc, RLIMIT_DATA)); 5750f502d1cSKonstantin Belousov PROC_UNLOCK(td->td_proc); 5760f502d1cSKonstantin Belousov 5770f502d1cSKonstantin Belousov /* round size up to page boundry */ 5780f502d1cSKonstantin Belousov size = (vm_size_t)round_page(sz); 5790f502d1cSKonstantin Belousov 5800f502d1cSKonstantin Belousov error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE, 5810f502d1cSKonstantin Belousov VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0); 5820f502d1cSKonstantin Belousov 5830f502d1cSKonstantin Belousov return (error); 5840f502d1cSKonstantin Belousov } 5850f502d1cSKonstantin Belousov 5860f502d1cSKonstantin Belousov /* 5870f502d1cSKonstantin Belousov * Unmap memory in user space. 5880f502d1cSKonstantin Belousov */ 5890f502d1cSKonstantin Belousov int 5900f502d1cSKonstantin Belousov copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz) 5910f502d1cSKonstantin Belousov { 5920f502d1cSKonstantin Belousov vm_map_t map; 5930f502d1cSKonstantin Belousov vm_size_t size; 5940f502d1cSKonstantin Belousov 595937060a8SKonstantin Belousov if (sz == 0) 596937060a8SKonstantin Belousov return (0); 597937060a8SKonstantin Belousov 5980f502d1cSKonstantin Belousov map = &td->td_proc->p_vmspace->vm_map; 5990f502d1cSKonstantin Belousov size = (vm_size_t)round_page(sz); 6000f502d1cSKonstantin Belousov 601cea8f30aSKonstantin Belousov if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS) 6020f502d1cSKonstantin Belousov return (EINVAL); 6030f502d1cSKonstantin Belousov 6040f502d1cSKonstantin Belousov return (0); 6050f502d1cSKonstantin Belousov } 606