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/mutex.h> 4928993443SEd Schouten #include <sys/proc.h> 500f502d1cSKonstantin Belousov #include <sys/resourcevar.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> 560f502d1cSKonstantin Belousov #include <vm/vm_extern.h> 5728993443SEd Schouten #include <vm/vm_page.h> 5828993443SEd Schouten #include <vm/vm_map.h> 5928993443SEd Schouten #ifdef ZERO_COPY_SOCKETS 6028993443SEd Schouten #include <vm/vm_param.h> 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 6928993443SEd Schouten #ifdef ZERO_COPY_SOCKETS 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 } 10728993443SEd Schouten VM_OBJECT_LOCK(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); 12728993443SEd Schouten VM_OBJECT_UNLOCK(uobject); 12828993443SEd Schouten vm_map_lookup_done(map, entry); 12928993443SEd Schouten return(KERN_SUCCESS); 13028993443SEd Schouten } 13128993443SEd Schouten #endif /* ZERO_COPY_SOCKETS */ 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 1552801687dSKonstantin Belousov int 15628993443SEd Schouten uiomove(void *cp, int n, struct uio *uio) 15728993443SEd Schouten { 1582801687dSKonstantin Belousov 1592801687dSKonstantin Belousov return (uiomove_faultflag(cp, n, uio, 0)); 1602801687dSKonstantin Belousov } 1612801687dSKonstantin Belousov 1622801687dSKonstantin Belousov int 1632801687dSKonstantin Belousov uiomove_nofault(void *cp, int n, struct uio *uio) 1642801687dSKonstantin Belousov { 1652801687dSKonstantin Belousov 1662801687dSKonstantin Belousov return (uiomove_faultflag(cp, n, uio, 1)); 1672801687dSKonstantin Belousov } 1682801687dSKonstantin Belousov 1692801687dSKonstantin Belousov static int 1702801687dSKonstantin Belousov uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault) 1712801687dSKonstantin Belousov { 1722801687dSKonstantin Belousov struct thread *td; 17328993443SEd Schouten struct iovec *iov; 17428993443SEd Schouten u_int cnt; 1752801687dSKonstantin Belousov int error, newflags, save; 1762801687dSKonstantin Belousov 1772801687dSKonstantin Belousov td = curthread; 1782801687dSKonstantin Belousov error = 0; 17928993443SEd Schouten 18028993443SEd Schouten KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 18128993443SEd Schouten ("uiomove: mode")); 1822801687dSKonstantin Belousov KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td, 18328993443SEd Schouten ("uiomove proc")); 1842801687dSKonstantin Belousov if (!nofault) 18528993443SEd Schouten WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 18628993443SEd Schouten "Calling uiomove()"); 18728993443SEd Schouten 1882801687dSKonstantin Belousov /* XXX does it make a sense to set TDP_DEADLKTREAT for UIO_SYSSPACE ? */ 1892801687dSKonstantin Belousov newflags = TDP_DEADLKTREAT; 1902801687dSKonstantin Belousov if (uio->uio_segflg == UIO_USERSPACE && nofault) 1912801687dSKonstantin Belousov newflags |= TDP_NOFAULTING; 1922801687dSKonstantin Belousov save = curthread_pflags_set(newflags); 19328993443SEd Schouten 19428993443SEd Schouten while (n > 0 && uio->uio_resid) { 19528993443SEd Schouten iov = uio->uio_iov; 19628993443SEd Schouten cnt = iov->iov_len; 19728993443SEd Schouten if (cnt == 0) { 19828993443SEd Schouten uio->uio_iov++; 19928993443SEd Schouten uio->uio_iovcnt--; 20028993443SEd Schouten continue; 20128993443SEd Schouten } 20228993443SEd Schouten if (cnt > n) 20328993443SEd Schouten cnt = n; 20428993443SEd Schouten 20528993443SEd Schouten switch (uio->uio_segflg) { 20628993443SEd Schouten 20728993443SEd Schouten case UIO_USERSPACE: 20808b163faSMatthew D Fleming maybe_yield(); 20928993443SEd Schouten if (uio->uio_rw == UIO_READ) 21028993443SEd Schouten error = copyout(cp, iov->iov_base, cnt); 21128993443SEd Schouten else 21228993443SEd Schouten error = copyin(iov->iov_base, cp, cnt); 21328993443SEd Schouten if (error) 21428993443SEd Schouten goto out; 21528993443SEd Schouten break; 21628993443SEd Schouten 21728993443SEd Schouten case UIO_SYSSPACE: 21828993443SEd Schouten if (uio->uio_rw == UIO_READ) 21928993443SEd Schouten bcopy(cp, iov->iov_base, cnt); 22028993443SEd Schouten else 22128993443SEd Schouten bcopy(iov->iov_base, cp, cnt); 22228993443SEd Schouten break; 22328993443SEd Schouten case UIO_NOCOPY: 22428993443SEd Schouten break; 22528993443SEd Schouten } 22628993443SEd Schouten iov->iov_base = (char *)iov->iov_base + cnt; 22728993443SEd Schouten iov->iov_len -= cnt; 22828993443SEd Schouten uio->uio_resid -= cnt; 22928993443SEd Schouten uio->uio_offset += cnt; 23028993443SEd Schouten cp = (char *)cp + cnt; 23128993443SEd Schouten n -= cnt; 23228993443SEd Schouten } 23328993443SEd Schouten out: 2342801687dSKonstantin Belousov curthread_pflags_restore(save); 23528993443SEd Schouten return (error); 23628993443SEd Schouten } 23728993443SEd Schouten 23828993443SEd Schouten /* 23928993443SEd Schouten * Wrapper for uiomove() that validates the arguments against a known-good 24028993443SEd Schouten * kernel buffer. Currently, uiomove accepts a signed (n) argument, which 24128993443SEd Schouten * is almost definitely a bad thing, so we catch that here as well. We 24228993443SEd Schouten * return a runtime failure, but it might be desirable to generate a runtime 24328993443SEd Schouten * assertion failure instead. 24428993443SEd Schouten */ 24528993443SEd Schouten int 24628993443SEd Schouten uiomove_frombuf(void *buf, int buflen, struct uio *uio) 24728993443SEd Schouten { 24828993443SEd Schouten unsigned int offset, n; 24928993443SEd Schouten 25028993443SEd Schouten if (uio->uio_offset < 0 || uio->uio_resid < 0 || 25128993443SEd Schouten (offset = uio->uio_offset) != uio->uio_offset) 25228993443SEd Schouten return (EINVAL); 25328993443SEd Schouten if (buflen <= 0 || offset >= buflen) 25428993443SEd Schouten return (0); 25528993443SEd Schouten if ((n = buflen - offset) > INT_MAX) 25628993443SEd Schouten return (EINVAL); 25728993443SEd Schouten return (uiomove((char *)buf + offset, n, uio)); 25828993443SEd Schouten } 25928993443SEd Schouten 26028993443SEd Schouten #ifdef ZERO_COPY_SOCKETS 26128993443SEd Schouten /* 26228993443SEd Schouten * Experimental support for zero-copy I/O 26328993443SEd Schouten */ 26428993443SEd Schouten static int 26528993443SEd Schouten userspaceco(void *cp, u_int cnt, struct uio *uio, int disposable) 26628993443SEd Schouten { 26728993443SEd Schouten struct iovec *iov; 26828993443SEd Schouten int error; 26928993443SEd Schouten 27028993443SEd Schouten iov = uio->uio_iov; 27128993443SEd Schouten if (uio->uio_rw == UIO_READ) { 27228993443SEd Schouten if ((so_zero_copy_receive != 0) 27328993443SEd Schouten && ((cnt & PAGE_MASK) == 0) 27428993443SEd Schouten && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0) 27528993443SEd Schouten && ((uio->uio_offset & PAGE_MASK) == 0) 27628993443SEd Schouten && ((((intptr_t) cp) & PAGE_MASK) == 0) 27728993443SEd Schouten && (disposable != 0)) { 27828993443SEd Schouten /* SOCKET: use page-trading */ 27928993443SEd Schouten /* 28028993443SEd Schouten * We only want to call vm_pgmoveco() on 28128993443SEd Schouten * disposeable pages, since it gives the 28228993443SEd Schouten * kernel page to the userland process. 28328993443SEd Schouten */ 28428993443SEd Schouten error = vm_pgmoveco(&curproc->p_vmspace->vm_map, 28528993443SEd Schouten (vm_offset_t)cp, (vm_offset_t)iov->iov_base); 28628993443SEd Schouten 28728993443SEd Schouten /* 28828993443SEd Schouten * If we get an error back, attempt 28928993443SEd Schouten * to use copyout() instead. The 29028993443SEd Schouten * disposable page should be freed 29128993443SEd Schouten * automatically if we weren't able to move 29228993443SEd Schouten * it into userland. 29328993443SEd Schouten */ 29428993443SEd Schouten if (error != 0) 29528993443SEd Schouten error = copyout(cp, iov->iov_base, cnt); 29628993443SEd Schouten } else { 29728993443SEd Schouten error = copyout(cp, iov->iov_base, cnt); 29828993443SEd Schouten } 29928993443SEd Schouten } else { 30028993443SEd Schouten error = copyin(iov->iov_base, cp, cnt); 30128993443SEd Schouten } 30228993443SEd Schouten return (error); 30328993443SEd Schouten } 30428993443SEd Schouten 30528993443SEd Schouten int 30628993443SEd Schouten uiomoveco(void *cp, int n, struct uio *uio, int disposable) 30728993443SEd Schouten { 30828993443SEd Schouten struct iovec *iov; 30928993443SEd Schouten u_int cnt; 31028993443SEd Schouten int error; 31128993443SEd Schouten 31228993443SEd Schouten KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE, 31328993443SEd Schouten ("uiomoveco: mode")); 31428993443SEd Schouten KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 31528993443SEd Schouten ("uiomoveco proc")); 31628993443SEd Schouten 31728993443SEd Schouten while (n > 0 && uio->uio_resid) { 31828993443SEd Schouten iov = uio->uio_iov; 31928993443SEd Schouten cnt = iov->iov_len; 32028993443SEd Schouten if (cnt == 0) { 32128993443SEd Schouten uio->uio_iov++; 32228993443SEd Schouten uio->uio_iovcnt--; 32328993443SEd Schouten continue; 32428993443SEd Schouten } 32528993443SEd Schouten if (cnt > n) 32628993443SEd Schouten cnt = n; 32728993443SEd Schouten 32828993443SEd Schouten switch (uio->uio_segflg) { 32928993443SEd Schouten 33028993443SEd Schouten case UIO_USERSPACE: 33108b163faSMatthew D Fleming maybe_yield(); 33228993443SEd Schouten error = userspaceco(cp, cnt, uio, disposable); 33328993443SEd Schouten if (error) 33428993443SEd Schouten return (error); 33528993443SEd Schouten break; 33628993443SEd Schouten 33728993443SEd Schouten case UIO_SYSSPACE: 33828993443SEd Schouten if (uio->uio_rw == UIO_READ) 33928993443SEd Schouten bcopy(cp, iov->iov_base, cnt); 34028993443SEd Schouten else 34128993443SEd Schouten bcopy(iov->iov_base, cp, cnt); 34228993443SEd Schouten break; 34328993443SEd Schouten case UIO_NOCOPY: 34428993443SEd Schouten break; 34528993443SEd Schouten } 34628993443SEd Schouten iov->iov_base = (char *)iov->iov_base + cnt; 34728993443SEd Schouten iov->iov_len -= cnt; 34828993443SEd Schouten uio->uio_resid -= cnt; 34928993443SEd Schouten uio->uio_offset += cnt; 35028993443SEd Schouten cp = (char *)cp + cnt; 35128993443SEd Schouten n -= cnt; 35228993443SEd Schouten } 35328993443SEd Schouten return (0); 35428993443SEd Schouten } 35528993443SEd Schouten #endif /* ZERO_COPY_SOCKETS */ 35628993443SEd Schouten 35728993443SEd Schouten /* 35828993443SEd Schouten * Give next character to user as result of read. 35928993443SEd Schouten */ 36028993443SEd Schouten int 36128993443SEd Schouten ureadc(int c, struct uio *uio) 36228993443SEd Schouten { 36328993443SEd Schouten struct iovec *iov; 36428993443SEd Schouten char *iov_base; 36528993443SEd Schouten 36628993443SEd Schouten WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 36728993443SEd Schouten "Calling ureadc()"); 36828993443SEd Schouten 36928993443SEd Schouten again: 37028993443SEd Schouten if (uio->uio_iovcnt == 0 || uio->uio_resid == 0) 37128993443SEd Schouten panic("ureadc"); 37228993443SEd Schouten iov = uio->uio_iov; 37328993443SEd Schouten if (iov->iov_len == 0) { 37428993443SEd Schouten uio->uio_iovcnt--; 37528993443SEd Schouten uio->uio_iov++; 37628993443SEd Schouten goto again; 37728993443SEd Schouten } 37828993443SEd Schouten switch (uio->uio_segflg) { 37928993443SEd Schouten 38028993443SEd Schouten case UIO_USERSPACE: 38128993443SEd Schouten if (subyte(iov->iov_base, c) < 0) 38228993443SEd Schouten return (EFAULT); 38328993443SEd Schouten break; 38428993443SEd Schouten 38528993443SEd Schouten case UIO_SYSSPACE: 38628993443SEd Schouten iov_base = iov->iov_base; 38728993443SEd Schouten *iov_base = c; 38828993443SEd Schouten iov->iov_base = iov_base; 38928993443SEd Schouten break; 39028993443SEd Schouten 39128993443SEd Schouten case UIO_NOCOPY: 39228993443SEd Schouten break; 39328993443SEd Schouten } 39428993443SEd Schouten iov->iov_base = (char *)iov->iov_base + 1; 39528993443SEd Schouten iov->iov_len--; 39628993443SEd Schouten uio->uio_resid--; 39728993443SEd Schouten uio->uio_offset++; 39828993443SEd Schouten return (0); 39928993443SEd Schouten } 40028993443SEd Schouten 40128993443SEd Schouten int 40228993443SEd Schouten copyinfrom(const void * __restrict src, void * __restrict dst, size_t len, 40328993443SEd Schouten int seg) 40428993443SEd Schouten { 40528993443SEd Schouten int error = 0; 40628993443SEd Schouten 40728993443SEd Schouten switch (seg) { 40828993443SEd Schouten case UIO_USERSPACE: 40928993443SEd Schouten error = copyin(src, dst, len); 41028993443SEd Schouten break; 41128993443SEd Schouten case UIO_SYSSPACE: 41228993443SEd Schouten bcopy(src, dst, len); 41328993443SEd Schouten break; 41428993443SEd Schouten default: 41528993443SEd Schouten panic("copyinfrom: bad seg %d\n", seg); 41628993443SEd Schouten } 41728993443SEd Schouten return (error); 41828993443SEd Schouten } 41928993443SEd Schouten 42028993443SEd Schouten int 42128993443SEd Schouten copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len, 42228993443SEd Schouten size_t * __restrict copied, int seg) 42328993443SEd Schouten { 42428993443SEd Schouten int error = 0; 42528993443SEd Schouten 42628993443SEd Schouten switch (seg) { 42728993443SEd Schouten case UIO_USERSPACE: 42828993443SEd Schouten error = copyinstr(src, dst, len, copied); 42928993443SEd Schouten break; 43028993443SEd Schouten case UIO_SYSSPACE: 43128993443SEd Schouten error = copystr(src, dst, len, copied); 43228993443SEd Schouten break; 43328993443SEd Schouten default: 43428993443SEd Schouten panic("copyinstrfrom: bad seg %d\n", seg); 43528993443SEd Schouten } 43628993443SEd Schouten return (error); 43728993443SEd Schouten } 43828993443SEd Schouten 43928993443SEd Schouten int 440*cfb09e00SAlfred Perlstein copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error) 44128993443SEd Schouten { 44228993443SEd Schouten u_int iovlen; 44328993443SEd Schouten 44428993443SEd Schouten *iov = NULL; 44528993443SEd Schouten if (iovcnt > UIO_MAXIOV) 44628993443SEd Schouten return (error); 44728993443SEd Schouten iovlen = iovcnt * sizeof (struct iovec); 44828993443SEd Schouten *iov = malloc(iovlen, M_IOV, M_WAITOK); 44928993443SEd Schouten error = copyin(iovp, *iov, iovlen); 45028993443SEd Schouten if (error) { 45128993443SEd Schouten free(*iov, M_IOV); 45228993443SEd Schouten *iov = NULL; 45328993443SEd Schouten } 45428993443SEd Schouten return (error); 45528993443SEd Schouten } 45628993443SEd Schouten 45728993443SEd Schouten int 458*cfb09e00SAlfred Perlstein copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop) 45928993443SEd Schouten { 46028993443SEd Schouten struct iovec *iov; 46128993443SEd Schouten struct uio *uio; 46228993443SEd Schouten u_int iovlen; 46328993443SEd Schouten int error, i; 46428993443SEd Schouten 46528993443SEd Schouten *uiop = NULL; 46628993443SEd Schouten if (iovcnt > UIO_MAXIOV) 46728993443SEd Schouten return (EINVAL); 46828993443SEd Schouten iovlen = iovcnt * sizeof (struct iovec); 46928993443SEd Schouten uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 47028993443SEd Schouten iov = (struct iovec *)(uio + 1); 47128993443SEd Schouten error = copyin(iovp, iov, iovlen); 47228993443SEd Schouten if (error) { 47328993443SEd Schouten free(uio, M_IOV); 47428993443SEd Schouten return (error); 47528993443SEd Schouten } 47628993443SEd Schouten uio->uio_iov = iov; 47728993443SEd Schouten uio->uio_iovcnt = iovcnt; 47828993443SEd Schouten uio->uio_segflg = UIO_USERSPACE; 47928993443SEd Schouten uio->uio_offset = -1; 48028993443SEd Schouten uio->uio_resid = 0; 48128993443SEd Schouten for (i = 0; i < iovcnt; i++) { 48228993443SEd Schouten if (iov->iov_len > INT_MAX - uio->uio_resid) { 48328993443SEd Schouten free(uio, M_IOV); 48428993443SEd Schouten return (EINVAL); 48528993443SEd Schouten } 48628993443SEd Schouten uio->uio_resid += iov->iov_len; 48728993443SEd Schouten iov++; 48828993443SEd Schouten } 48928993443SEd Schouten *uiop = uio; 49028993443SEd Schouten return (0); 49128993443SEd Schouten } 49228993443SEd Schouten 49328993443SEd Schouten struct uio * 49428993443SEd Schouten cloneuio(struct uio *uiop) 49528993443SEd Schouten { 49628993443SEd Schouten struct uio *uio; 49728993443SEd Schouten int iovlen; 49828993443SEd Schouten 49928993443SEd Schouten iovlen = uiop->uio_iovcnt * sizeof (struct iovec); 50028993443SEd Schouten uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); 50128993443SEd Schouten *uio = *uiop; 50228993443SEd Schouten uio->uio_iov = (struct iovec *)(uio + 1); 50328993443SEd Schouten bcopy(uiop->uio_iov, uio->uio_iov, iovlen); 50428993443SEd Schouten return (uio); 50528993443SEd Schouten } 5060f502d1cSKonstantin Belousov 5070f502d1cSKonstantin Belousov /* 5080f502d1cSKonstantin Belousov * Map some anonymous memory in user space of size sz, rounded up to the page 5090f502d1cSKonstantin Belousov * boundary. 5100f502d1cSKonstantin Belousov */ 5110f502d1cSKonstantin Belousov int 5120f502d1cSKonstantin Belousov copyout_map(struct thread *td, vm_offset_t *addr, size_t sz) 5130f502d1cSKonstantin Belousov { 514cce6e354SKonstantin Belousov struct vmspace *vms; 5150f502d1cSKonstantin Belousov int error; 5160f502d1cSKonstantin Belousov vm_size_t size; 5170f502d1cSKonstantin Belousov 518cce6e354SKonstantin Belousov vms = td->td_proc->p_vmspace; 519cce6e354SKonstantin Belousov 5200f502d1cSKonstantin Belousov /* 5210f502d1cSKonstantin Belousov * Map somewhere after heap in process memory. 5220f502d1cSKonstantin Belousov */ 5230f502d1cSKonstantin Belousov PROC_LOCK(td->td_proc); 5240f502d1cSKonstantin Belousov *addr = round_page((vm_offset_t)vms->vm_daddr + 5250f502d1cSKonstantin Belousov lim_max(td->td_proc, RLIMIT_DATA)); 5260f502d1cSKonstantin Belousov PROC_UNLOCK(td->td_proc); 5270f502d1cSKonstantin Belousov 5280f502d1cSKonstantin Belousov /* round size up to page boundry */ 5290f502d1cSKonstantin Belousov size = (vm_size_t)round_page(sz); 5300f502d1cSKonstantin Belousov 5310f502d1cSKonstantin Belousov error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE, 5320f502d1cSKonstantin Belousov VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0); 5330f502d1cSKonstantin Belousov 5340f502d1cSKonstantin Belousov return (error); 5350f502d1cSKonstantin Belousov } 5360f502d1cSKonstantin Belousov 5370f502d1cSKonstantin Belousov /* 5380f502d1cSKonstantin Belousov * Unmap memory in user space. 5390f502d1cSKonstantin Belousov */ 5400f502d1cSKonstantin Belousov int 5410f502d1cSKonstantin Belousov copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz) 5420f502d1cSKonstantin Belousov { 5430f502d1cSKonstantin Belousov vm_map_t map; 5440f502d1cSKonstantin Belousov vm_size_t size; 5450f502d1cSKonstantin Belousov 546937060a8SKonstantin Belousov if (sz == 0) 547937060a8SKonstantin Belousov return (0); 548937060a8SKonstantin Belousov 5490f502d1cSKonstantin Belousov map = &td->td_proc->p_vmspace->vm_map; 5500f502d1cSKonstantin Belousov size = (vm_size_t)round_page(sz); 5510f502d1cSKonstantin Belousov 552cea8f30aSKonstantin Belousov if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS) 5530f502d1cSKonstantin Belousov return (EINVAL); 5540f502d1cSKonstantin Belousov 5550f502d1cSKonstantin Belousov return (0); 5560f502d1cSKonstantin Belousov } 557