2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
1994-05-24 10:09:53 +00:00
|
|
|
* Copyright (c) 1982, 1986, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1999-02-22 16:57:48 +00:00
|
|
|
#include <sys/kernel.h>
|
2003-10-02 15:00:55 +00:00
|
|
|
#include <sys/limits.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
2011-03-28 12:48:33 +00:00
|
|
|
#include <sys/mman.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/proc.h>
|
2011-03-28 12:48:33 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2013-02-20 10:38:34 +00:00
|
|
|
#include <sys/rwlock.h>
|
2002-10-12 05:32:24 +00:00
|
|
|
#include <sys/sched.h>
|
2001-06-18 20:24:54 +00:00
|
|
|
#include <sys/sysctl.h>
|
VM level code cleanups.
1) Start using TSM.
Struct procs continue to point to upages structure, after being freed.
Struct vmspace continues to point to pte object and kva space for kstack.
u_map is now superfluous.
2) vm_map's don't need to be reference counted. They always exist either
in the kernel or in a vmspace. The vmspaces are managed by reference
counts.
3) Remove the "wired" vm_map nonsense.
4) No need to keep a cache of kernel stack kva's.
5) Get rid of strange looking ++var, and change to var++.
6) Change more data structures to use our "zone" allocator. Added
struct proc, struct vmspace and struct vnode. This saves a significant
amount of kva space and physical memory. Additionally, this enables
TSM for the zone managed memory.
7) Keep ioopt disabled for now.
8) Remove the now bogus "single use" map concept.
9) Use generation counts or id's for data structures residing in TSM, where
it allows us to avoid unneeded restart overhead during traversals, where
blocking might occur.
10) Account better for memory deficits, so the pageout daemon will be able
to make enough memory available (experimental.)
11) Fix some vnode locking problems. (From Tor, I think.)
12) Add a check in ufs_lookup, to avoid lots of unneeded calls to bcmp.
(experimental.)
13) Significantly shrink, cleanup, and make slightly faster the vm_fault.c
code. Use generation counts, get rid of unneded collpase operations,
and clean up the cluster code.
14) Make vm_zone more suitable for TSM.
This commit is partially as a result of discussions and contributions from
other people, including DG, Tor Egge, PHK, and probably others that I
have forgotten to attribute (so let me know, if I forgot.)
This is not the infamous, final cleanup of the vnode stuff, but a necessary
step. Vnode mgmt should be correct, but things might still change, and
there is still some missing stuff (like ioopt, and physical backing of
non-merged cache files, debugging of layering concepts.)
1998-01-22 17:30:44 +00:00
|
|
|
#include <sys/vnode.h>
|
1997-12-19 09:03:37 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
2012-08-05 14:11:42 +00:00
|
|
|
#include <vm/vm_param.h>
|
2011-03-28 12:48:33 +00:00
|
|
|
#include <vm/vm_extern.h>
|
1997-12-19 09:03:37 +00:00
|
|
|
#include <vm/vm_page.h>
|
2013-08-09 11:28:55 +00:00
|
|
|
#include <vm/vm_pageout.h>
|
1997-12-19 09:03:37 +00:00
|
|
|
#include <vm/vm_map.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2003-03-02 15:08:33 +00:00
|
|
|
SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
|
2001-06-18 20:24:54 +00:00
|
|
|
"Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
|
|
|
|
|
2011-07-09 15:21:10 +00:00
|
|
|
static int uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault);
|
|
|
|
|
|
|
|
int
|
|
|
|
copyin_nofault(const void *udaddr, void *kaddr, size_t len)
|
|
|
|
{
|
|
|
|
int error, save;
|
|
|
|
|
|
|
|
save = vm_fault_disable_pagefaults();
|
|
|
|
error = copyin(udaddr, kaddr, len);
|
|
|
|
vm_fault_enable_pagefaults(save);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
copyout_nofault(const void *kaddr, void *udaddr, size_t len)
|
|
|
|
{
|
|
|
|
int error, save;
|
|
|
|
|
|
|
|
save = vm_fault_disable_pagefaults();
|
|
|
|
error = copyout(kaddr, udaddr, len);
|
|
|
|
vm_fault_enable_pagefaults(save);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2013-02-12 16:57:20 +00:00
|
|
|
#define PHYS_PAGE_COUNT(len) (howmany(len, PAGE_SIZE) + 1)
|
|
|
|
|
|
|
|
int
|
|
|
|
physcopyin(void *src, vm_paddr_t dst, size_t len)
|
|
|
|
{
|
|
|
|
vm_page_t m[PHYS_PAGE_COUNT(len)];
|
|
|
|
struct iovec iov[1];
|
|
|
|
struct uio uio;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
iov[0].iov_base = src;
|
|
|
|
iov[0].iov_len = len;
|
|
|
|
uio.uio_iov = iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_offset = 0;
|
|
|
|
uio.uio_resid = len;
|
|
|
|
uio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
uio.uio_rw = UIO_WRITE;
|
|
|
|
for (i = 0; i < PHYS_PAGE_COUNT(len); i++, dst += PAGE_SIZE)
|
|
|
|
m[i] = PHYS_TO_VM_PAGE(dst);
|
|
|
|
return (uiomove_fromphys(m, dst & PAGE_MASK, len, &uio));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
physcopyout(vm_paddr_t src, void *dst, size_t len)
|
|
|
|
{
|
|
|
|
vm_page_t m[PHYS_PAGE_COUNT(len)];
|
|
|
|
struct iovec iov[1];
|
|
|
|
struct uio uio;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
iov[0].iov_base = dst;
|
|
|
|
iov[0].iov_len = len;
|
|
|
|
uio.uio_iov = iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_offset = 0;
|
|
|
|
uio.uio_resid = len;
|
|
|
|
uio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
uio.uio_rw = UIO_READ;
|
|
|
|
for (i = 0; i < PHYS_PAGE_COUNT(len); i++, src += PAGE_SIZE)
|
|
|
|
m[i] = PHYS_TO_VM_PAGE(src);
|
|
|
|
return (uiomove_fromphys(m, src & PAGE_MASK, len, &uio));
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef PHYS_PAGE_COUNT
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
2003-03-02 15:29:13 +00:00
|
|
|
uiomove(void *cp, int n, struct uio *uio)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2011-07-09 15:21:10 +00:00
|
|
|
|
|
|
|
return (uiomove_faultflag(cp, n, uio, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
uiomove_nofault(void *cp, int n, struct uio *uio)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (uiomove_faultflag(cp, n, uio, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
uiomove_faultflag(void *cp, int n, struct uio *uio, int nofault)
|
|
|
|
{
|
|
|
|
struct thread *td;
|
2003-03-02 15:08:33 +00:00
|
|
|
struct iovec *iov;
|
2012-02-21 01:05:12 +00:00
|
|
|
size_t cnt;
|
2011-07-09 15:21:10 +00:00
|
|
|
int error, newflags, save;
|
|
|
|
|
|
|
|
td = curthread;
|
|
|
|
error = 0;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1999-01-08 17:31:30 +00:00
|
|
|
KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
|
1999-01-10 01:58:29 +00:00
|
|
|
("uiomove: mode"));
|
2011-07-09 15:21:10 +00:00
|
|
|
KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == td,
|
1999-01-10 01:58:29 +00:00
|
|
|
("uiomove proc"));
|
2011-07-09 15:21:10 +00:00
|
|
|
if (!nofault)
|
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
|
|
|
|
"Calling uiomove()");
|
1999-01-08 17:31:30 +00:00
|
|
|
|
2011-07-09 15:21:10 +00:00
|
|
|
/* XXX does it make a sense to set TDP_DEADLKTREAT for UIO_SYSSPACE ? */
|
|
|
|
newflags = TDP_DEADLKTREAT;
|
Handle spurious page faults that may occur in no-fault sections of the
kernel.
When access restrictions are added to a page table entry, we flush the
corresponding virtual address mapping from the TLB. In contrast, when
access restrictions are removed from a page table entry, we do not
flush the virtual address mapping from the TLB. This is exactly as
recommended in AMD's documentation. In effect, when access
restrictions are removed from a page table entry, AMD's MMUs will
transparently refresh a stale TLB entry. In short, this saves us from
having to perform potentially costly TLB flushes. In contrast,
Intel's MMUs are allowed to generate a spurious page fault based upon
the stale TLB entry. Usually, such spurious page faults are handled
by vm_fault() without incident. However, when we are executing
no-fault sections of the kernel, we are not allowed to execute
vm_fault(). This change introduces special-case handling for spurious
page faults that occur in no-fault sections of the kernel.
In collaboration with: kib
Tested by: gibbs (an earlier version)
I would also like to acknowledge Hiroki Sato's assistance in
diagnosing this problem.
MFC after: 1 week
2012-03-22 04:52:51 +00:00
|
|
|
if (uio->uio_segflg == UIO_USERSPACE && nofault) {
|
|
|
|
/*
|
|
|
|
* Fail if a non-spurious page fault occurs.
|
|
|
|
*/
|
|
|
|
newflags |= TDP_NOFAULTING | TDP_RESETSPUR;
|
|
|
|
}
|
2011-07-09 15:21:10 +00:00
|
|
|
save = curthread_pflags_set(newflags);
|
1999-03-12 03:09:29 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
while (n > 0 && uio->uio_resid) {
|
|
|
|
iov = uio->uio_iov;
|
|
|
|
cnt = iov->iov_len;
|
|
|
|
if (cnt == 0) {
|
|
|
|
uio->uio_iov++;
|
|
|
|
uio->uio_iovcnt--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cnt > n)
|
|
|
|
cnt = n;
|
1995-02-12 09:11:47 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
switch (uio->uio_segflg) {
|
|
|
|
|
|
|
|
case UIO_USERSPACE:
|
2011-02-02 16:35:10 +00:00
|
|
|
maybe_yield();
|
1994-05-24 10:09:53 +00:00
|
|
|
if (uio->uio_rw == UIO_READ)
|
|
|
|
error = copyout(cp, iov->iov_base, cnt);
|
|
|
|
else
|
|
|
|
error = copyin(iov->iov_base, cp, cnt);
|
|
|
|
if (error)
|
2002-02-08 20:19:44 +00:00
|
|
|
goto out;
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UIO_SYSSPACE:
|
|
|
|
if (uio->uio_rw == UIO_READ)
|
2002-06-29 00:29:12 +00:00
|
|
|
bcopy(cp, iov->iov_base, cnt);
|
1994-05-24 10:09:53 +00:00
|
|
|
else
|
2002-06-29 00:29:12 +00:00
|
|
|
bcopy(iov->iov_base, cp, cnt);
|
1997-12-19 09:03:37 +00:00
|
|
|
break;
|
|
|
|
case UIO_NOCOPY:
|
|
|
|
break;
|
|
|
|
}
|
2002-10-11 14:58:34 +00:00
|
|
|
iov->iov_base = (char *)iov->iov_base + cnt;
|
1997-12-19 09:03:37 +00:00
|
|
|
iov->iov_len -= cnt;
|
|
|
|
uio->uio_resid -= cnt;
|
|
|
|
uio->uio_offset += cnt;
|
2003-03-02 15:29:13 +00:00
|
|
|
cp = (char *)cp + cnt;
|
1997-12-19 09:03:37 +00:00
|
|
|
n -= cnt;
|
|
|
|
}
|
2002-02-08 20:19:44 +00:00
|
|
|
out:
|
2011-07-09 15:21:10 +00:00
|
|
|
curthread_pflags_restore(save);
|
1999-03-12 03:09:29 +00:00
|
|
|
return (error);
|
1997-12-19 09:03:37 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 15:00:55 +00:00
|
|
|
/*
|
|
|
|
* Wrapper for uiomove() that validates the arguments against a known-good
|
|
|
|
* kernel buffer. Currently, uiomove accepts a signed (n) argument, which
|
|
|
|
* is almost definitely a bad thing, so we catch that here as well. We
|
|
|
|
* return a runtime failure, but it might be desirable to generate a runtime
|
|
|
|
* assertion failure instead.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
uiomove_frombuf(void *buf, int buflen, struct uio *uio)
|
|
|
|
{
|
2012-02-21 01:05:12 +00:00
|
|
|
size_t offset, n;
|
2003-10-02 15:00:55 +00:00
|
|
|
|
|
|
|
if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
|
|
|
|
(offset = uio->uio_offset) != uio->uio_offset)
|
|
|
|
return (EINVAL);
|
|
|
|
if (buflen <= 0 || offset >= buflen)
|
|
|
|
return (0);
|
2012-02-21 01:05:12 +00:00
|
|
|
if ((n = buflen - offset) > IOSIZE_MAX)
|
2003-10-02 15:00:55 +00:00
|
|
|
return (EINVAL);
|
|
|
|
return (uiomove((char *)buf + offset, n, uio));
|
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Give next character to user as result of read.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
2003-03-02 15:08:33 +00:00
|
|
|
ureadc(int c, struct uio *uio)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2003-03-02 15:08:33 +00:00
|
|
|
struct iovec *iov;
|
|
|
|
char *iov_base;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2008-08-28 19:34:58 +00:00
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
|
|
|
|
"Calling ureadc()");
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
again:
|
|
|
|
if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
|
|
|
|
panic("ureadc");
|
|
|
|
iov = uio->uio_iov;
|
|
|
|
if (iov->iov_len == 0) {
|
|
|
|
uio->uio_iovcnt--;
|
|
|
|
uio->uio_iov++;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
switch (uio->uio_segflg) {
|
|
|
|
|
|
|
|
case UIO_USERSPACE:
|
|
|
|
if (subyte(iov->iov_base, c) < 0)
|
|
|
|
return (EFAULT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UIO_SYSSPACE:
|
2002-10-11 14:58:34 +00:00
|
|
|
iov_base = iov->iov_base;
|
|
|
|
*iov_base = c;
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
|
1995-04-30 05:11:46 +00:00
|
|
|
case UIO_NOCOPY:
|
|
|
|
break;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2002-10-11 14:58:34 +00:00
|
|
|
iov->iov_base = (char *)iov->iov_base + 1;
|
1994-05-24 10:09:53 +00:00
|
|
|
iov->iov_len--;
|
|
|
|
uio->uio_resid--;
|
|
|
|
uio->uio_offset++;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-02-16 14:31:49 +00:00
|
|
|
int
|
2003-12-26 05:54:35 +00:00
|
|
|
copyinfrom(const void * __restrict src, void * __restrict dst, size_t len,
|
|
|
|
int seg)
|
2001-02-16 14:31:49 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (seg) {
|
|
|
|
case UIO_USERSPACE:
|
|
|
|
error = copyin(src, dst, len);
|
|
|
|
break;
|
|
|
|
case UIO_SYSSPACE:
|
|
|
|
bcopy(src, dst, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panic("copyinfrom: bad seg %d\n", seg);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-12-26 05:54:35 +00:00
|
|
|
copyinstrfrom(const void * __restrict src, void * __restrict dst, size_t len,
|
|
|
|
size_t * __restrict copied, int seg)
|
2001-02-16 14:31:49 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (seg) {
|
|
|
|
case UIO_USERSPACE:
|
|
|
|
error = copyinstr(src, dst, len, copied);
|
|
|
|
break;
|
|
|
|
case UIO_SYSSPACE:
|
|
|
|
error = copystr(src, dst, len, copied);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panic("copyinstrfrom: bad seg %d\n", seg);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
2004-02-01 07:56:44 +00:00
|
|
|
|
|
|
|
int
|
2011-11-14 07:12:10 +00:00
|
|
|
copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
|
2004-07-10 15:42:16 +00:00
|
|
|
{
|
|
|
|
u_int iovlen;
|
|
|
|
|
|
|
|
*iov = NULL;
|
|
|
|
if (iovcnt > UIO_MAXIOV)
|
|
|
|
return (error);
|
|
|
|
iovlen = iovcnt * sizeof (struct iovec);
|
|
|
|
*iov = malloc(iovlen, M_IOV, M_WAITOK);
|
|
|
|
error = copyin(iovp, *iov, iovlen);
|
|
|
|
if (error) {
|
|
|
|
free(*iov, M_IOV);
|
|
|
|
*iov = NULL;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-11-14 07:12:10 +00:00
|
|
|
copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
|
2004-02-01 07:56:44 +00:00
|
|
|
{
|
2004-02-04 08:14:47 +00:00
|
|
|
struct iovec *iov;
|
2004-07-10 15:42:16 +00:00
|
|
|
struct uio *uio;
|
2004-02-01 07:56:44 +00:00
|
|
|
u_int iovlen;
|
2004-02-04 08:14:47 +00:00
|
|
|
int error, i;
|
|
|
|
|
2004-07-10 15:42:16 +00:00
|
|
|
*uiop = NULL;
|
|
|
|
if (iovcnt > UIO_MAXIOV)
|
|
|
|
return (EINVAL);
|
2004-02-04 08:14:47 +00:00
|
|
|
iovlen = iovcnt * sizeof (struct iovec);
|
2004-07-10 15:42:16 +00:00
|
|
|
uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
|
|
|
|
iov = (struct iovec *)(uio + 1);
|
|
|
|
error = copyin(iovp, iov, iovlen);
|
|
|
|
if (error) {
|
|
|
|
free(uio, M_IOV);
|
|
|
|
return (error);
|
2004-02-04 08:14:47 +00:00
|
|
|
}
|
|
|
|
uio->uio_iov = iov;
|
|
|
|
uio->uio_iovcnt = iovcnt;
|
|
|
|
uio->uio_segflg = UIO_USERSPACE;
|
|
|
|
uio->uio_offset = -1;
|
|
|
|
uio->uio_resid = 0;
|
|
|
|
for (i = 0; i < iovcnt; i++) {
|
2012-02-21 01:05:12 +00:00
|
|
|
if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) {
|
2004-07-10 15:42:16 +00:00
|
|
|
free(uio, M_IOV);
|
|
|
|
return (EINVAL);
|
2004-02-04 08:14:47 +00:00
|
|
|
}
|
|
|
|
uio->uio_resid += iov->iov_len;
|
|
|
|
iov++;
|
|
|
|
}
|
2004-07-10 15:42:16 +00:00
|
|
|
*uiop = uio;
|
|
|
|
return (0);
|
|
|
|
}
|
2004-02-01 07:56:44 +00:00
|
|
|
|
2004-07-10 15:42:16 +00:00
|
|
|
struct uio *
|
|
|
|
cloneuio(struct uio *uiop)
|
|
|
|
{
|
|
|
|
struct uio *uio;
|
|
|
|
int iovlen;
|
|
|
|
|
|
|
|
iovlen = uiop->uio_iovcnt * sizeof (struct iovec);
|
|
|
|
uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
|
|
|
|
*uio = *uiop;
|
|
|
|
uio->uio_iov = (struct iovec *)(uio + 1);
|
|
|
|
bcopy(uiop->uio_iov, uio->uio_iov, iovlen);
|
|
|
|
return (uio);
|
2004-02-01 07:56:44 +00:00
|
|
|
}
|
2011-03-28 12:48:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Map some anonymous memory in user space of size sz, rounded up to the page
|
|
|
|
* boundary.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
copyout_map(struct thread *td, vm_offset_t *addr, size_t sz)
|
|
|
|
{
|
2011-03-28 13:28:23 +00:00
|
|
|
struct vmspace *vms;
|
2011-03-28 12:48:33 +00:00
|
|
|
int error;
|
|
|
|
vm_size_t size;
|
2011-03-28 13:28:23 +00:00
|
|
|
|
|
|
|
vms = td->td_proc->p_vmspace;
|
|
|
|
|
|
|
|
/*
|
2011-03-28 12:48:33 +00:00
|
|
|
* Map somewhere after heap in process memory.
|
|
|
|
*/
|
|
|
|
PROC_LOCK(td->td_proc);
|
2011-03-28 13:28:23 +00:00
|
|
|
*addr = round_page((vm_offset_t)vms->vm_daddr +
|
2011-03-28 12:48:33 +00:00
|
|
|
lim_max(td->td_proc, RLIMIT_DATA));
|
|
|
|
PROC_UNLOCK(td->td_proc);
|
|
|
|
|
|
|
|
/* round size up to page boundry */
|
2011-03-28 13:28:23 +00:00
|
|
|
size = (vm_size_t)round_page(sz);
|
|
|
|
|
|
|
|
error = vm_mmap(&vms->vm_map, addr, size, PROT_READ | PROT_WRITE,
|
2011-03-28 12:48:33 +00:00
|
|
|
VM_PROT_ALL, MAP_PRIVATE | MAP_ANON, OBJT_DEFAULT, NULL, 0);
|
2011-03-28 13:28:23 +00:00
|
|
|
|
2011-03-28 12:48:33 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmap memory in user space.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
copyout_unmap(struct thread *td, vm_offset_t addr, size_t sz)
|
|
|
|
{
|
|
|
|
vm_map_t map;
|
|
|
|
vm_size_t size;
|
2011-03-28 13:28:23 +00:00
|
|
|
|
2011-03-28 13:21:26 +00:00
|
|
|
if (sz == 0)
|
|
|
|
return (0);
|
|
|
|
|
2011-03-28 12:48:33 +00:00
|
|
|
map = &td->td_proc->p_vmspace->vm_map;
|
2011-03-28 13:28:23 +00:00
|
|
|
size = (vm_size_t)round_page(sz);
|
2011-03-28 12:48:33 +00:00
|
|
|
|
2011-03-28 19:44:54 +00:00
|
|
|
if (vm_map_remove(map, addr, addr + size) != KERN_SUCCESS)
|
2011-03-28 12:48:33 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|