2007-02-10 00:55:00 +03:00
|
|
|
/* $NetBSD: uvm_mmap.c,v 1.105 2007/02/09 21:55:43 ad Exp $ */
|
1998-02-05 09:25:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1997 Charles D. Cranor and Washington University.
|
2001-05-25 08:06:11 +04:00
|
|
|
* Copyright (c) 1991, 1993 The Regents of the University of California.
|
1998-02-05 09:25:08 +03:00
|
|
|
* Copyright (c) 1988 University of Utah.
|
2001-05-25 08:06:11 +04:00
|
|
|
*
|
1998-02-05 09:25:08 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* the Systems Programming Group of the University of Utah Computer
|
|
|
|
* Science Department.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the Charles D. Cranor,
|
2001-05-25 08:06:11 +04:00
|
|
|
* Washington University, University of California, Berkeley and
|
1998-02-05 09:25:08 +03:00
|
|
|
* its contributors.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
|
|
|
|
* @(#)vm_mmap.c 8.5 (Berkeley) 5/19/94
|
1998-02-07 14:07:38 +03:00
|
|
|
* from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
|
1998-02-05 09:25:08 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
|
|
|
|
* function.
|
|
|
|
*/
|
2001-11-10 10:36:59 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.105 2007/02/09 21:55:43 ad Exp $");
|
2004-02-14 15:20:14 +03:00
|
|
|
|
|
|
|
#include "opt_compat_netbsd.h"
|
2006-05-20 19:45:37 +04:00
|
|
|
#include "opt_pax.h"
|
2006-09-30 14:56:31 +04:00
|
|
|
#include "veriexec.h"
|
2001-11-10 10:36:59 +03:00
|
|
|
|
1998-02-05 09:25:08 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/conf.h>
|
1998-05-10 16:35:58 +04:00
|
|
|
#include <sys/stat.h>
|
2006-09-30 14:56:31 +04:00
|
|
|
|
|
|
|
#if NVERIEXEC > 0
|
|
|
|
#include <sys/verified_exec.h>
|
|
|
|
#endif /* NVERIEXEC > 0 */
|
2006-05-20 19:45:37 +04:00
|
|
|
|
|
|
|
#ifdef PAX_MPROTECT
|
|
|
|
#include <sys/pax.h>
|
|
|
|
#endif /* PAX_MPROTECT */
|
1998-02-05 09:25:08 +03:00
|
|
|
|
|
|
|
#include <miscfs/specfs/specdev.h>
|
|
|
|
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
#include <uvm/uvm_device.h>
|
|
|
|
|
2004-02-14 15:20:14 +03:00
|
|
|
#ifndef COMPAT_ZERODEV
|
2004-02-14 19:40:22 +03:00
|
|
|
#define COMPAT_ZERODEV(dev) (0)
|
2004-02-14 15:20:14 +03:00
|
|
|
#endif
|
1998-02-05 09:25:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* unimplemented VM system calls:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_sbrk: sbrk system call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_sbrk(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
|
|
|
#if 0
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys_sbrk_args /* {
|
1999-07-13 01:55:19 +04:00
|
|
|
syscallarg(intptr_t) incr;
|
1999-05-03 13:08:28 +04:00
|
|
|
} */ *uap = v;
|
1998-02-05 09:25:08 +03:00
|
|
|
#endif
|
1998-03-09 03:58:55 +03:00
|
|
|
|
1999-03-09 15:18:22 +03:00
|
|
|
return (ENOSYS);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_sstk: sstk system call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_sstk(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
|
|
|
#if 0
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys_sstk_args /* {
|
1999-05-03 13:08:28 +04:00
|
|
|
syscallarg(int) incr;
|
|
|
|
} */ *uap = v;
|
1998-02-05 09:25:08 +03:00
|
|
|
#endif
|
1998-03-09 03:58:55 +03:00
|
|
|
|
1999-03-09 15:18:22 +03:00
|
|
|
return (ENOSYS);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_mincore: determine if pages are in core or not.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_mincore(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys_mincore_args /* {
|
1999-06-16 03:27:47 +04:00
|
|
|
syscallarg(void *) addr;
|
1999-05-03 13:08:28 +04:00
|
|
|
syscallarg(size_t) len;
|
|
|
|
syscallarg(char *) vec;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
struct vm_page *pg;
|
1999-06-16 03:27:47 +04:00
|
|
|
char *vec, pgi;
|
|
|
|
struct uvm_object *uobj;
|
|
|
|
struct vm_amap *amap;
|
|
|
|
struct vm_anon *anon;
|
2001-06-02 22:09:08 +04:00
|
|
|
struct vm_map_entry *entry;
|
1999-06-16 03:27:47 +04:00
|
|
|
vaddr_t start, end, lim;
|
2001-06-02 22:09:08 +04:00
|
|
|
struct vm_map *map;
|
1999-06-16 03:27:47 +04:00
|
|
|
vsize_t len;
|
|
|
|
int error = 0, npgs;
|
|
|
|
|
|
|
|
map = &p->p_vmspace->vm_map;
|
|
|
|
|
|
|
|
start = (vaddr_t)SCARG(uap, addr);
|
|
|
|
len = SCARG(uap, len);
|
|
|
|
vec = SCARG(uap, vec);
|
|
|
|
|
|
|
|
if (start & PAGE_MASK)
|
|
|
|
return (EINVAL);
|
|
|
|
len = round_page(len);
|
|
|
|
end = start + len;
|
|
|
|
if (end <= start)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock down vec, so our returned status isn't outdated by
|
|
|
|
* storing the status byte for a page.
|
|
|
|
*/
|
|
|
|
|
2001-12-14 07:21:22 +03:00
|
|
|
npgs = len >> PAGE_SHIFT;
|
2006-10-05 18:48:32 +04:00
|
|
|
error = uvm_vslock(p->p_vmspace, vec, npgs, VM_PROT_WRITE);
|
2001-12-14 07:21:22 +03:00
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
1999-06-16 03:27:47 +04:00
|
|
|
vm_map_lock_read(map);
|
|
|
|
|
|
|
|
if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (/* nothing */;
|
|
|
|
entry != &map->header && entry->start < end;
|
|
|
|
entry = entry->next) {
|
2001-02-19 00:19:08 +03:00
|
|
|
KASSERT(!UVM_ET_ISSUBMAP(entry));
|
|
|
|
KASSERT(start >= entry->start);
|
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
/* Make sure there are no holes. */
|
|
|
|
if (entry->end < end &&
|
|
|
|
(entry->next == &map->header ||
|
|
|
|
entry->next->start > entry->end)) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
1998-03-09 03:58:55 +03:00
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
lim = end < entry->end ? end : entry->end;
|
|
|
|
|
|
|
|
/*
|
1999-07-11 00:29:24 +04:00
|
|
|
* Special case for objects with no "real" pages. Those
|
|
|
|
* are always considered resident (mapped devices).
|
1999-06-16 03:27:47 +04:00
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
if (UVM_ET_ISOBJ(entry)) {
|
2001-02-19 00:19:08 +03:00
|
|
|
KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
|
2003-11-29 22:06:48 +03:00
|
|
|
if (UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
|
1999-06-16 03:27:47 +04:00
|
|
|
for (/* nothing */; start < lim;
|
|
|
|
start += PAGE_SIZE, vec++)
|
|
|
|
subyte(vec, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-11 00:40:23 +04:00
|
|
|
amap = entry->aref.ar_amap; /* top layer */
|
|
|
|
uobj = entry->object.uvm_obj; /* bottom layer */
|
1999-06-16 03:27:47 +04:00
|
|
|
|
|
|
|
if (amap != NULL)
|
|
|
|
amap_lock(amap);
|
|
|
|
if (uobj != NULL)
|
|
|
|
simple_lock(&uobj->vmobjlock);
|
|
|
|
|
|
|
|
for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
|
|
|
|
pgi = 0;
|
|
|
|
if (amap != NULL) {
|
|
|
|
/* Check the top layer first. */
|
|
|
|
anon = amap_lookup(&entry->aref,
|
|
|
|
start - entry->start);
|
|
|
|
/* Don't need to lock anon here. */
|
2005-05-11 17:02:25 +04:00
|
|
|
if (anon != NULL && anon->an_page != NULL) {
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
/*
|
|
|
|
* Anon has the page for this entry
|
|
|
|
* offset.
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
pgi = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uobj != NULL && pgi == 0) {
|
|
|
|
/* Check the bottom layer. */
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
pg = uvm_pagelookup(uobj,
|
1999-06-16 03:27:47 +04:00
|
|
|
entry->offset + (start - entry->start));
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
if (pg != NULL) {
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
/*
|
|
|
|
* Object has the page for this entry
|
|
|
|
* offset.
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
pgi = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void) subyte(vec, pgi);
|
|
|
|
}
|
|
|
|
if (uobj != NULL)
|
1999-07-01 22:40:39 +04:00
|
|
|
simple_unlock(&uobj->vmobjlock);
|
1999-06-16 03:27:47 +04:00
|
|
|
if (amap != NULL)
|
|
|
|
amap_unlock(amap);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
vm_map_unlock_read(map);
|
2006-10-05 18:48:32 +04:00
|
|
|
uvm_vsunlock(p->p_vmspace, SCARG(uap, vec), npgs);
|
1999-06-16 03:27:47 +04:00
|
|
|
return (error);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_mmap: mmap system call.
|
|
|
|
*
|
2002-05-31 20:49:50 +04:00
|
|
|
* => file offset and address may not be page aligned
|
1998-02-05 09:25:08 +03:00
|
|
|
* - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
|
|
|
|
* - if address isn't page aligned the mapping starts at trunc_page(addr)
|
|
|
|
* and the return value is adjusted up by the page offset.
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2003-01-18 11:51:40 +03:00
|
|
|
sys_mmap(l, v, retval)
|
|
|
|
struct lwp *l;
|
1998-03-09 03:58:55 +03:00
|
|
|
void *v;
|
|
|
|
register_t *retval;
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
2000-03-30 16:31:50 +04:00
|
|
|
struct sys_mmap_args /* {
|
1998-03-09 03:58:55 +03:00
|
|
|
syscallarg(caddr_t) addr;
|
|
|
|
syscallarg(size_t) len;
|
|
|
|
syscallarg(int) prot;
|
|
|
|
syscallarg(int) flags;
|
|
|
|
syscallarg(int) fd;
|
|
|
|
syscallarg(long) pad;
|
|
|
|
syscallarg(off_t) pos;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr;
|
1998-05-10 16:35:58 +04:00
|
|
|
struct vattr va;
|
1998-03-09 03:58:55 +03:00
|
|
|
off_t pos;
|
1998-08-13 06:10:37 +04:00
|
|
|
vsize_t size, pageoff;
|
1998-03-09 03:58:55 +03:00
|
|
|
vm_prot_t prot, maxprot;
|
|
|
|
int flags, fd;
|
2005-03-26 08:12:34 +03:00
|
|
|
vaddr_t vm_min_address = VM_MIN_ADDRESS, defaddr;
|
2000-03-30 16:31:50 +04:00
|
|
|
struct filedesc *fdp = p->p_fd;
|
|
|
|
struct file *fp;
|
1998-03-09 03:58:55 +03:00
|
|
|
struct vnode *vp;
|
2001-03-15 09:10:32 +03:00
|
|
|
void *handle;
|
1998-03-09 03:58:55 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first, extract syscall args from the uap.
|
|
|
|
*/
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
1998-03-09 03:58:55 +03:00
|
|
|
prot = SCARG(uap, prot) & VM_PROT_ALL;
|
|
|
|
flags = SCARG(uap, flags);
|
|
|
|
fd = SCARG(uap, fd);
|
|
|
|
pos = SCARG(uap, pos);
|
|
|
|
|
1999-06-18 01:05:19 +04:00
|
|
|
/*
|
|
|
|
* Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and
|
|
|
|
* validate the flags.
|
|
|
|
*/
|
|
|
|
if (flags & MAP_COPY)
|
|
|
|
flags = (flags & ~MAP_COPY) | MAP_PRIVATE;
|
|
|
|
if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
|
|
|
|
return (EINVAL);
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* align file position and save offset. adjust size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pageoff = (pos & PAGE_MASK);
|
|
|
|
pos -= pageoff;
|
|
|
|
size += pageoff; /* add offset */
|
2001-03-15 09:10:32 +03:00
|
|
|
size = (vsize_t)round_page(size); /* round up */
|
1998-03-09 03:58:55 +03:00
|
|
|
if ((ssize_t) size < 0)
|
|
|
|
return (EINVAL); /* don't allow wrap */
|
|
|
|
|
|
|
|
/*
|
2001-05-25 08:06:11 +04:00
|
|
|
* now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (flags & MAP_FIXED) {
|
|
|
|
|
|
|
|
/* ensure address and file offset are aligned properly */
|
|
|
|
addr -= pageoff;
|
|
|
|
if (addr & PAGE_MASK)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (VM_MAXUSER_ADDRESS > 0 &&
|
|
|
|
(addr + size) > VM_MAXUSER_ADDRESS)
|
2002-03-22 14:06:33 +03:00
|
|
|
return (EFBIG);
|
1998-03-09 03:58:55 +03:00
|
|
|
if (vm_min_address > 0 && addr < vm_min_address)
|
|
|
|
return (EINVAL);
|
|
|
|
if (addr > addr + size)
|
2002-03-22 14:06:33 +03:00
|
|
|
return (EOVERFLOW); /* no wrapping! */
|
1998-03-09 03:58:55 +03:00
|
|
|
|
2003-07-06 20:19:18 +04:00
|
|
|
} else if (addr == 0 || !(flags & MAP_TRYFIXED)) {
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
2003-02-21 01:16:05 +03:00
|
|
|
* not fixed: make sure we skip over the largest
|
|
|
|
* possible heap for non-topdown mapping arrangements.
|
|
|
|
* we will refine our guess later (e.g. to account for
|
|
|
|
* VAC, etc)
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
2000-11-27 11:39:39 +03:00
|
|
|
|
2005-03-26 08:12:34 +03:00
|
|
|
defaddr = p->p_emul->e_vm_default_addr(p,
|
|
|
|
(vaddr_t)p->p_vmspace->vm_daddr, size);
|
|
|
|
|
2003-02-21 01:16:05 +03:00
|
|
|
if (addr == 0 ||
|
|
|
|
!(p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
|
2005-03-26 08:12:34 +03:00
|
|
|
addr = MAX(addr, defaddr);
|
2003-02-21 01:16:05 +03:00
|
|
|
else
|
2005-03-26 08:12:34 +03:00
|
|
|
addr = MIN(addr, defaddr);
|
1998-03-09 03:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for file mappings (i.e. not anonymous) and verify file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & MAP_ANON) == 0) {
|
|
|
|
|
2001-06-15 00:32:41 +04:00
|
|
|
if ((fp = fd_getfile(fdp, fd)) == NULL)
|
|
|
|
return (EBADF);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
2003-02-23 17:37:32 +03:00
|
|
|
simple_unlock(&fp->f_slock);
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
if (fp->f_type != DTYPE_VNODE)
|
1998-03-28 19:58:04 +03:00
|
|
|
return (ENODEV); /* only mmap vnodes! */
|
1998-03-09 03:58:55 +03:00
|
|
|
vp = (struct vnode *)fp->f_data; /* convert to vnode */
|
|
|
|
|
1998-07-08 03:22:13 +04:00
|
|
|
if (vp->v_type != VREG && vp->v_type != VCHR &&
|
|
|
|
vp->v_type != VBLK)
|
|
|
|
return (ENODEV); /* only REG/CHR/BLK support mmap */
|
1998-03-09 03:58:55 +03:00
|
|
|
|
2001-11-25 09:42:47 +03:00
|
|
|
if (vp->v_type != VCHR && pos < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (vp->v_type != VCHR && (pos + size) < pos)
|
2000-03-28 22:45:19 +04:00
|
|
|
return (EOVERFLOW); /* no offset wrapping */
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/* special case: catch SunOS style /dev/zero */
|
2004-02-14 15:20:14 +03:00
|
|
|
if (vp->v_type == VCHR
|
|
|
|
&& (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
|
1998-03-09 03:58:55 +03:00
|
|
|
flags |= MAP_ANON;
|
|
|
|
goto is_anon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Old programs may not select a specific sharing type, so
|
|
|
|
* default to an appropriate one.
|
|
|
|
*
|
|
|
|
* XXX: how does MAP_ANON fit in the picture?
|
|
|
|
*/
|
1999-06-18 01:05:19 +04:00
|
|
|
if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
|
1998-04-02 01:43:52 +04:00
|
|
|
#if defined(DEBUG)
|
1998-03-09 03:58:55 +03:00
|
|
|
printf("WARNING: defaulted mmap() share type to "
|
2003-05-04 05:54:26 +04:00
|
|
|
"%s (pid %d command %s)\n", vp->v_type == VCHR ?
|
1998-03-09 03:58:55 +03:00
|
|
|
"MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
|
|
|
|
p->p_comm);
|
1998-02-05 09:25:08 +03:00
|
|
|
#endif
|
1998-03-09 03:58:55 +03:00
|
|
|
if (vp->v_type == VCHR)
|
|
|
|
flags |= MAP_SHARED; /* for a device */
|
|
|
|
else
|
|
|
|
flags |= MAP_PRIVATE; /* for a file */
|
|
|
|
}
|
|
|
|
|
2001-05-25 08:06:11 +04:00
|
|
|
/*
|
1998-03-09 03:58:55 +03:00
|
|
|
* MAP_PRIVATE device mappings don't make sense (and aren't
|
|
|
|
* supported anyway). However, some programs rely on this,
|
|
|
|
* so just change it to MAP_SHARED.
|
|
|
|
*/
|
|
|
|
if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
|
|
|
|
flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
|
|
|
|
}
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* now check protection
|
|
|
|
*/
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2001-01-08 04:35:03 +03:00
|
|
|
maxprot = VM_PROT_EXECUTE;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
2007-02-03 04:11:50 +03:00
|
|
|
#if NVERIEXEC > 0
|
|
|
|
/*
|
|
|
|
* Check if the file can be executed indirectly.
|
|
|
|
*/
|
|
|
|
if (veriexec_verify(l, vp, "(mmap)", VERIEXEC_INDIRECT, NULL)) {
|
|
|
|
/*
|
|
|
|
* Don't allow executable mappings if we can't
|
|
|
|
* indirectly execute the file.
|
|
|
|
*/
|
|
|
|
if (prot & VM_PROT_EXECUTE)
|
|
|
|
return (EPERM);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip the executable bit from 'maxprot' to make sure
|
|
|
|
* it can't be made executable later.
|
|
|
|
*/
|
|
|
|
maxprot &= ~VM_PROT_EXECUTE;
|
|
|
|
}
|
|
|
|
#endif /* NVERIEXEC > 0 */
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/* check read access */
|
|
|
|
if (fp->f_flag & FREAD)
|
|
|
|
maxprot |= VM_PROT_READ;
|
|
|
|
else if (prot & PROT_READ)
|
|
|
|
return (EACCES);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-05-10 16:35:58 +04:00
|
|
|
/* check write access, shared case first */
|
1998-03-09 03:58:55 +03:00
|
|
|
if (flags & MAP_SHARED) {
|
1998-05-10 16:35:58 +04:00
|
|
|
/*
|
|
|
|
* if the file is writable, only add PROT_WRITE to
|
|
|
|
* maxprot if the file is not immutable, append-only.
|
|
|
|
* otherwise, if we have asked for PROT_WRITE, return
|
|
|
|
* EPERM.
|
|
|
|
*/
|
|
|
|
if (fp->f_flag & FWRITE) {
|
|
|
|
if ((error =
|
2006-07-21 20:48:45 +04:00
|
|
|
VOP_GETATTR(vp, &va, l->l_cred, l)))
|
1998-05-10 16:35:58 +04:00
|
|
|
return (error);
|
2004-05-25 18:54:55 +04:00
|
|
|
if ((va.va_flags &
|
|
|
|
(SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
|
1998-05-10 16:35:58 +04:00
|
|
|
maxprot |= VM_PROT_WRITE;
|
|
|
|
else if (prot & PROT_WRITE)
|
|
|
|
return (EPERM);
|
|
|
|
}
|
1998-03-09 03:58:55 +03:00
|
|
|
else if (prot & PROT_WRITE)
|
|
|
|
return (EACCES);
|
|
|
|
} else {
|
|
|
|
/* MAP_PRIVATE mappings can always write to */
|
|
|
|
maxprot |= VM_PROT_WRITE;
|
|
|
|
}
|
2001-03-15 09:10:32 +03:00
|
|
|
handle = vp;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
} else { /* MAP_ANON case */
|
1999-06-18 01:05:19 +04:00
|
|
|
/*
|
|
|
|
* XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
|
|
|
|
*/
|
1998-03-09 03:58:55 +03:00
|
|
|
if (fd != -1)
|
|
|
|
return (EINVAL);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1999-06-18 01:05:19 +04:00
|
|
|
is_anon: /* label for SunOS style /dev/zero */
|
1998-03-09 03:58:55 +03:00
|
|
|
handle = NULL;
|
|
|
|
maxprot = VM_PROT_ALL;
|
|
|
|
pos = 0;
|
|
|
|
}
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1999-07-06 06:31:05 +04:00
|
|
|
/*
|
|
|
|
* XXX (in)sanity check. We don't do proper datasize checking
|
|
|
|
* XXX for anonymous (or private writable) mmap(). However,
|
|
|
|
* XXX know that if we're trying to allocate more than the amount
|
|
|
|
* XXX remaining under our current data size limit, _that_ should
|
|
|
|
* XXX be disallowed.
|
|
|
|
*/
|
|
|
|
if ((flags & MAP_ANON) != 0 ||
|
|
|
|
((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) {
|
|
|
|
if (size >
|
2001-03-15 09:10:32 +03:00
|
|
|
(p->p_rlimit[RLIMIT_DATA].rlim_cur -
|
|
|
|
ctob(p->p_vmspace->vm_dsize))) {
|
1999-07-06 06:31:05 +04:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-20 19:45:37 +04:00
|
|
|
#ifdef PAX_MPROTECT
|
|
|
|
pax_mprotect(l, &prot, &maxprot);
|
|
|
|
#endif /* PAX_MPROTECT */
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* now let kernel internal function uvm_mmap do the work.
|
|
|
|
*/
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
|
1999-06-18 09:13:45 +04:00
|
|
|
flags, handle, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
if (error == 0)
|
|
|
|
/* remember to add offset */
|
|
|
|
*retval = (register_t)(addr + pageoff);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
return (error);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys___msync13: the msync system call (a front-end for flush)
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys___msync13(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys___msync13_args /* {
|
|
|
|
syscallarg(caddr_t) addr;
|
|
|
|
syscallarg(size_t) len;
|
|
|
|
syscallarg(int) flags;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr;
|
|
|
|
vsize_t size, pageoff;
|
2001-06-02 22:09:08 +04:00
|
|
|
struct vm_map *map;
|
2001-03-15 09:10:32 +03:00
|
|
|
int error, rv, flags, uvmflags;
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* extract syscall args from the uap
|
|
|
|
*/
|
|
|
|
|
1998-08-13 06:10:37 +04:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
1998-03-09 03:58:55 +03:00
|
|
|
flags = SCARG(uap, flags);
|
|
|
|
|
|
|
|
/* sanity check flags */
|
|
|
|
if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
|
2003-08-24 22:12:25 +04:00
|
|
|
(flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
|
|
|
|
(flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
|
|
|
|
return (EINVAL);
|
1998-03-09 03:58:55 +03:00
|
|
|
if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
|
2003-08-24 22:12:25 +04:00
|
|
|
flags |= MS_SYNC;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
2001-03-15 09:10:32 +03:00
|
|
|
* align the address to a page boundary and adjust the size accordingly.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
pageoff = (addr & PAGE_MASK);
|
|
|
|
addr -= pageoff;
|
|
|
|
size += pageoff;
|
2001-03-15 09:10:32 +03:00
|
|
|
size = (vsize_t)round_page(size);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/* disallow wrap-around. */
|
|
|
|
if (addr + size < addr)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get map
|
|
|
|
*/
|
|
|
|
|
|
|
|
map = &p->p_vmspace->vm_map;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXXCDC: do we really need this semantic?
|
|
|
|
*
|
|
|
|
* XXX Gak! If size is zero we are supposed to sync "all modified
|
|
|
|
* pages with the region containing addr". Unfortunately, we
|
|
|
|
* don't really keep track of individual mmaps so we approximate
|
|
|
|
* by flushing the range of the map entry containing addr.
|
|
|
|
* This can be incorrect if the region splits or is coalesced
|
|
|
|
* with a neighbor.
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
if (size == 0) {
|
2001-06-02 22:09:08 +04:00
|
|
|
struct vm_map_entry *entry;
|
2001-05-25 08:06:11 +04:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
vm_map_lock_read(map);
|
|
|
|
rv = uvm_map_lookup_entry(map, addr, &entry);
|
|
|
|
if (rv == TRUE) {
|
|
|
|
addr = entry->start;
|
|
|
|
size = entry->end - entry->start;
|
|
|
|
}
|
|
|
|
vm_map_unlock_read(map);
|
|
|
|
if (rv == FALSE)
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* translate MS_ flags into PGO_ flags
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1999-07-15 01:06:30 +04:00
|
|
|
uvmflags = PGO_CLEANIT;
|
|
|
|
if (flags & MS_INVALIDATE)
|
|
|
|
uvmflags |= PGO_FREE;
|
1998-03-09 03:58:55 +03:00
|
|
|
if (flags & MS_SYNC)
|
|
|
|
uvmflags |= PGO_SYNCIO;
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
error = uvm_map_clean(map, addr, addr+size, uvmflags);
|
|
|
|
return error;
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_munmap: unmap a users memory
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_munmap(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
2000-03-30 16:31:50 +04:00
|
|
|
struct sys_munmap_args /* {
|
1998-03-09 03:58:55 +03:00
|
|
|
syscallarg(caddr_t) addr;
|
|
|
|
syscallarg(size_t) len;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr;
|
|
|
|
vsize_t size, pageoff;
|
2001-06-02 22:09:08 +04:00
|
|
|
struct vm_map *map;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t vm_min_address = VM_MIN_ADDRESS;
|
1998-03-09 03:58:55 +03:00
|
|
|
struct vm_map_entry *dead_entries;
|
|
|
|
|
|
|
|
/*
|
2001-03-15 09:10:32 +03:00
|
|
|
* get syscall args.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
2001-05-25 08:06:11 +04:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
2001-03-15 09:10:32 +03:00
|
|
|
* align the address to a page boundary and adjust the size accordingly.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
pageoff = (addr & PAGE_MASK);
|
|
|
|
addr -= pageoff;
|
|
|
|
size += pageoff;
|
2001-03-15 09:10:32 +03:00
|
|
|
size = (vsize_t)round_page(size);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
if ((int)size < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
if (size == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for illegal addresses. Watch out for address wrap...
|
|
|
|
* Note that VM_*_ADDRESS are not constants due to casts (argh).
|
|
|
|
*/
|
|
|
|
if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
|
|
|
|
return (EINVAL);
|
|
|
|
if (vm_min_address > 0 && addr < vm_min_address)
|
|
|
|
return (EINVAL);
|
|
|
|
if (addr > addr + size)
|
|
|
|
return (EINVAL);
|
|
|
|
map = &p->p_vmspace->vm_map;
|
|
|
|
|
|
|
|
/*
|
2001-05-25 08:06:11 +04:00
|
|
|
* interesting system call semantic: make sure entire range is
|
1998-03-09 03:58:55 +03:00
|
|
|
* allocated before allowing an unmap.
|
|
|
|
*/
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
vm_map_lock(map);
|
2002-09-27 23:13:29 +04:00
|
|
|
#if 0
|
1998-03-09 03:58:55 +03:00
|
|
|
if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
|
|
|
|
vm_map_unlock(map);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-09-27 23:13:29 +04:00
|
|
|
#endif
|
2005-04-01 15:59:21 +04:00
|
|
|
uvm_unmap_remove(map, addr, addr + size, &dead_entries, NULL, 0);
|
2001-03-15 09:10:32 +03:00
|
|
|
vm_map_unlock(map);
|
1998-03-09 03:58:55 +03:00
|
|
|
if (dead_entries != NULL)
|
|
|
|
uvm_unmap_detach(dead_entries, 0);
|
|
|
|
return (0);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_mprotect: the mprotect system call
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_mprotect(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys_mprotect_args /* {
|
|
|
|
syscallarg(caddr_t) addr;
|
2003-08-24 20:32:50 +04:00
|
|
|
syscallarg(size_t) len;
|
1998-03-09 03:58:55 +03:00
|
|
|
syscallarg(int) prot;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr;
|
|
|
|
vsize_t size, pageoff;
|
1998-03-09 03:58:55 +03:00
|
|
|
vm_prot_t prot;
|
2001-03-15 09:10:32 +03:00
|
|
|
int error;
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* extract syscall args from uap
|
|
|
|
*/
|
|
|
|
|
1998-08-13 06:10:37 +04:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
1998-03-09 03:58:55 +03:00
|
|
|
prot = SCARG(uap, prot) & VM_PROT_ALL;
|
|
|
|
|
|
|
|
/*
|
2001-03-15 09:10:32 +03:00
|
|
|
* align the address to a page boundary and adjust the size accordingly.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
pageoff = (addr & PAGE_MASK);
|
|
|
|
addr -= pageoff;
|
|
|
|
size += pageoff;
|
2003-08-24 20:32:50 +04:00
|
|
|
size = round_page(size);
|
2001-03-15 09:10:32 +03:00
|
|
|
|
|
|
|
error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
|
|
|
|
FALSE);
|
|
|
|
return error;
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_minherit: the minherit system call
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_minherit(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys_minherit_args /* {
|
|
|
|
syscallarg(caddr_t) addr;
|
|
|
|
syscallarg(int) len;
|
|
|
|
syscallarg(int) inherit;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr;
|
|
|
|
vsize_t size, pageoff;
|
2000-03-30 16:31:50 +04:00
|
|
|
vm_inherit_t inherit;
|
2001-03-15 09:10:32 +03:00
|
|
|
int error;
|
2001-05-25 08:06:11 +04:00
|
|
|
|
1998-08-13 06:10:37 +04:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
1998-03-09 03:58:55 +03:00
|
|
|
inherit = SCARG(uap, inherit);
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
2001-03-15 09:10:32 +03:00
|
|
|
* align the address to a page boundary and adjust the size accordingly.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
pageoff = (addr & PAGE_MASK);
|
|
|
|
addr -= pageoff;
|
|
|
|
size += pageoff;
|
2001-03-15 09:10:32 +03:00
|
|
|
size = (vsize_t)round_page(size);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
if ((int)size < 0)
|
|
|
|
return (EINVAL);
|
2001-03-15 09:10:32 +03:00
|
|
|
error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size,
|
|
|
|
inherit);
|
|
|
|
return error;
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
1999-05-23 10:27:13 +04:00
|
|
|
/*
|
|
|
|
* sys_madvise: give advice about memory usage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_madvise(struct lwp *l, void *v, register_t *retval)
|
1999-05-23 10:27:13 +04:00
|
|
|
{
|
|
|
|
struct sys_madvise_args /* {
|
|
|
|
syscallarg(caddr_t) addr;
|
|
|
|
syscallarg(size_t) len;
|
|
|
|
syscallarg(int) behav;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1999-05-23 10:27:13 +04:00
|
|
|
vaddr_t addr;
|
|
|
|
vsize_t size, pageoff;
|
2001-03-15 09:10:32 +03:00
|
|
|
int advice, error;
|
2001-05-25 08:06:11 +04:00
|
|
|
|
1999-05-23 10:27:13 +04:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
|
|
|
advice = SCARG(uap, behav);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* align the address to a page boundary, and adjust the size accordingly
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1999-05-23 10:27:13 +04:00
|
|
|
pageoff = (addr & PAGE_MASK);
|
|
|
|
addr -= pageoff;
|
|
|
|
size += pageoff;
|
2001-03-15 09:10:32 +03:00
|
|
|
size = (vsize_t)round_page(size);
|
1999-05-23 10:27:13 +04:00
|
|
|
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
if ((ssize_t)size <= 0)
|
1999-05-23 10:27:13 +04:00
|
|
|
return (EINVAL);
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
|
|
|
|
switch (advice) {
|
|
|
|
case MADV_NORMAL:
|
|
|
|
case MADV_RANDOM:
|
|
|
|
case MADV_SEQUENTIAL:
|
2001-03-15 09:10:32 +03:00
|
|
|
error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
advice);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MADV_WILLNEED:
|
2001-03-15 09:10:32 +03:00
|
|
|
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
/*
|
|
|
|
* Activate all these pages, pre-faulting them in if
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* XXX IMPLEMENT ME.
|
|
|
|
* Should invent a "weak" mode for uvm_fault()
|
|
|
|
* which would only do the PGO_LOCKED pgo_get().
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
case MADV_DONTNEED:
|
2001-03-15 09:10:32 +03:00
|
|
|
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
/*
|
|
|
|
* Deactivate all these pages. We don't need them
|
|
|
|
* any more. We don't, however, toss the data in
|
|
|
|
* the pages.
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
|
|
|
error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
PGO_DEACTIVATE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MADV_FREE:
|
2001-03-15 09:10:32 +03:00
|
|
|
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
/*
|
|
|
|
* These pages contain no valid data, and may be
|
2000-11-25 02:30:01 +03:00
|
|
|
* garbage-collected. Toss all resources, including
|
1999-07-08 04:52:45 +04:00
|
|
|
* any swap space in use.
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
|
|
|
error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
PGO_FREE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MADV_SPACEAVAIL:
|
2001-03-15 09:10:32 +03:00
|
|
|
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
/*
|
|
|
|
* XXXMRG What is this? I think it's:
|
|
|
|
*
|
|
|
|
* Ensure that we have allocated backing-store
|
|
|
|
* for these pages.
|
|
|
|
*
|
|
|
|
* This is going to require changes to the page daemon,
|
|
|
|
* as it will free swap space allocated to pages in core.
|
|
|
|
* There's also what to do for device/file/anonymous memory.
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
Add some more meat to madvise(2):
* Implement MADV_DONTNEED: deactivate pages in the specified range,
semantics similar to Solaris's MADV_DONTNEED.
* Add MADV_FREE: free pages and swap resources associated with the
specified range, causing the range to be reloaded from backing
store (vnodes) or zero-fill (anonymous), semantics like FreeBSD's
MADV_FREE and like Digital UNIX's MADV_DONTNEED (isn't it SO GREAT
that madvise(2) isn't standardized!?)
As part of this, move the non-map-modifying advice handling out of
uvm_map_advise(), and into sys_madvise().
As another part, implement general amap cleaning in uvm_map_clean(), and
change uvm_map_clean() to only push dirty pages to disk if PGO_CLEANIT
is set in its flags (and update sys___msync13() accordingly). XXX Add
a patchable global "amap_clean_works", defaulting to 1, which can disable
the amap cleaning code, just in case problems are unearthed; this gives
a developer/user a quick way to recover and send a bug report (e.g. boot
into DDB and change the value).
XXX Still need to implement a real uao_flush().
XXX Need to update the manual page.
With these changes, rebuilding libc will automatically cause the new
malloc(3) to use MADV_FREE to actually release pages and swap resources
when it decides that can be done.
1999-07-07 10:02:21 +04:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
return error;
|
1999-05-23 10:27:13 +04:00
|
|
|
}
|
|
|
|
|
1998-02-05 09:25:08 +03:00
|
|
|
/*
|
|
|
|
* sys_mlock: memory lock
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_mlock(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys_mlock_args /* {
|
1998-05-31 02:21:03 +04:00
|
|
|
syscallarg(const void *) addr;
|
1998-03-09 03:58:55 +03:00
|
|
|
syscallarg(size_t) len;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr;
|
|
|
|
vsize_t size, pageoff;
|
1998-03-09 03:58:55 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract syscall args from uap
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1998-08-13 06:10:37 +04:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* align the address to a page boundary and adjust the size accordingly
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
pageoff = (addr & PAGE_MASK);
|
|
|
|
addr -= pageoff;
|
|
|
|
size += pageoff;
|
2001-03-15 09:10:32 +03:00
|
|
|
size = (vsize_t)round_page(size);
|
2001-05-25 08:06:11 +04:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/* disallow wrap-around. */
|
2001-03-15 09:10:32 +03:00
|
|
|
if (addr + size < addr)
|
1998-03-09 03:58:55 +03:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
|
|
|
|
return (EAGAIN);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
|
|
|
|
p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
|
|
|
|
return (EAGAIN);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1999-06-18 09:13:45 +04:00
|
|
|
error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE,
|
1999-07-18 01:35:49 +04:00
|
|
|
0);
|
2004-12-02 18:23:47 +03:00
|
|
|
if (error == EFAULT)
|
|
|
|
error = ENOMEM;
|
2001-03-15 09:10:32 +03:00
|
|
|
return error;
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_munlock: unlock wired pages
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_munlock(struct lwp *l, void *v, register_t *retval)
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
struct sys_munlock_args /* {
|
1998-05-31 02:21:03 +04:00
|
|
|
syscallarg(const void *) addr;
|
1998-03-09 03:58:55 +03:00
|
|
|
syscallarg(size_t) len;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t addr;
|
|
|
|
vsize_t size, pageoff;
|
1998-03-09 03:58:55 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract syscall args from uap
|
|
|
|
*/
|
|
|
|
|
1998-08-13 06:10:37 +04:00
|
|
|
addr = (vaddr_t)SCARG(uap, addr);
|
|
|
|
size = (vsize_t)SCARG(uap, len);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* align the address to a page boundary, and adjust the size accordingly
|
|
|
|
*/
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
pageoff = (addr & PAGE_MASK);
|
|
|
|
addr -= pageoff;
|
|
|
|
size += pageoff;
|
2001-03-15 09:10:32 +03:00
|
|
|
size = (vsize_t)round_page(size);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/* disallow wrap-around. */
|
2001-03-15 09:10:32 +03:00
|
|
|
if (addr + size < addr)
|
1998-03-09 03:58:55 +03:00
|
|
|
return (EINVAL);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1999-06-18 09:13:45 +04:00
|
|
|
error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE,
|
1999-07-18 01:35:49 +04:00
|
|
|
0);
|
2004-12-02 18:23:47 +03:00
|
|
|
if (error == EFAULT)
|
|
|
|
error = ENOMEM;
|
2001-03-15 09:10:32 +03:00
|
|
|
return error;
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
1999-06-16 03:27:47 +04:00
|
|
|
/*
|
|
|
|
* sys_mlockall: lock all pages mapped into an address space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_mlockall(struct lwp *l, void *v, register_t *retval)
|
1999-06-16 03:27:47 +04:00
|
|
|
{
|
|
|
|
struct sys_mlockall_args /* {
|
|
|
|
syscallarg(int) flags;
|
|
|
|
} */ *uap = v;
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1999-06-16 03:27:47 +04:00
|
|
|
int error, flags;
|
|
|
|
|
|
|
|
flags = SCARG(uap, flags);
|
|
|
|
|
|
|
|
if (flags == 0 ||
|
|
|
|
(flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
1999-06-18 09:13:45 +04:00
|
|
|
error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
|
|
|
|
p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
|
1999-06-16 03:27:47 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sys_munlockall: unlock all pages mapped into an address space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
sys_munlockall(struct lwp *l, void *v, register_t *retval)
|
1999-06-16 03:27:47 +04:00
|
|
|
{
|
2003-01-18 11:51:40 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1999-06-16 03:27:47 +04:00
|
|
|
|
|
|
|
(void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1998-02-05 09:25:08 +03:00
|
|
|
/*
|
|
|
|
* uvm_mmap: internal version of mmap
|
|
|
|
*
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
* - used by sys_mmap and various framebuffers
|
|
|
|
* - handle is a vnode pointer or NULL for MAP_ANON
|
1998-02-05 09:25:08 +03:00
|
|
|
* - caller must page-align the file offset
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
1999-06-18 09:13:45 +04:00
|
|
|
uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit)
|
2001-06-02 22:09:08 +04:00
|
|
|
struct vm_map *map;
|
1998-08-13 06:10:37 +04:00
|
|
|
vaddr_t *addr;
|
|
|
|
vsize_t size;
|
1998-03-09 03:58:55 +03:00
|
|
|
vm_prot_t prot, maxprot;
|
|
|
|
int flags;
|
2001-03-15 09:10:32 +03:00
|
|
|
void *handle;
|
2000-03-27 00:54:45 +04:00
|
|
|
voff_t foff;
|
1999-06-18 09:13:45 +04:00
|
|
|
vsize_t locklimit;
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
struct uvm_object *uobj;
|
|
|
|
struct vnode *vp;
|
2003-03-06 03:41:51 +03:00
|
|
|
vaddr_t align = 0;
|
2001-03-15 09:10:32 +03:00
|
|
|
int error;
|
1998-03-09 03:58:55 +03:00
|
|
|
int advice = UVM_ADV_NORMAL;
|
|
|
|
uvm_flag_t uvmflag = 0;
|
2006-10-05 18:48:32 +04:00
|
|
|
boolean_t needwritemap;
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check params
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return(0);
|
|
|
|
if (foff & PAGE_MASK)
|
|
|
|
return(EINVAL);
|
|
|
|
if ((prot & maxprot) != prot)
|
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* for non-fixed mappings, round off the suggested address.
|
|
|
|
* for fixed mappings, check alignment and zap old mappings.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & MAP_FIXED) == 0) {
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
*addr = round_page(*addr);
|
1998-03-09 03:58:55 +03:00
|
|
|
} else {
|
|
|
|
if (*addr & PAGE_MASK)
|
|
|
|
return(EINVAL);
|
|
|
|
uvmflag |= UVM_FLAG_FIXED;
|
a whole bunch of changes to improve performance and robustness under load:
- remove special treatment of pager_map mappings in pmaps. this is
required now, since I've removed the globals that expose the address range.
pager_map now uses pmap_kenter_pa() instead of pmap_enter(), so there's
no longer any need to special-case it.
- eliminate struct uvm_vnode by moving its fields into struct vnode.
- rewrite the pageout path. the pager is now responsible for handling the
high-level requests instead of only getting control after a bunch of work
has already been done on its behalf. this will allow us to UBCify LFS,
which needs tighter control over its pages than other filesystems do.
writing a page to disk no longer requires making it read-only, which
allows us to write wired pages without causing all kinds of havoc.
- use a new PG_PAGEOUT flag to indicate that a page should be freed
on behalf of the pagedaemon when it's unlocked. this flag is very similar
to PG_RELEASED, but unlike PG_RELEASED, PG_PAGEOUT can be cleared if the
pageout fails due to eg. an indirect-block buffer being locked.
this allows us to remove the "version" field from struct vm_page,
and together with shrinking "loan_count" from 32 bits to 16,
struct vm_page is now 4 bytes smaller.
- no longer use PG_RELEASED for swap-backed pages. if the page is busy
because it's being paged out, we can't release the swap slot to be
reallocated until that write is complete, but unlike with vnodes we
don't keep a count of in-progress writes so there's no good way to
know when the write is done. instead, when we need to free a busy
swap-backed page, just sleep until we can get it busy ourselves.
- implement a fast-path for extending writes which allows us to avoid
zeroing new pages. this substantially reduces cpu usage.
- encapsulate the data used by the genfs code in a struct genfs_node,
which must be the first element of the filesystem-specific vnode data
for filesystems which use genfs_{get,put}pages().
- eliminate many of the UVM pagerops, since they aren't needed anymore
now that the pager "put" operation is a higher-level operation.
- enhance the genfs code to allow NFS to use the genfs_{get,put}pages
instead of a modified copy.
- clean up struct vnode by removing all the fields that used to be used by
the vfs_cluster.c code (which we don't use anymore with UBC).
- remove kmem_object and mb_object since they were useless.
instead of allocating pages to these objects, we now just allocate
pages with no object. such pages are mapped in the kernel until they
are freed, so we can use the mapping to find the page to free it.
this allows us to remove splvm() protection in several places.
The sum of all these changes improves write throughput on my
decstation 5000/200 to within 1% of the rate of NetBSD 1.5
and reduces the elapsed time for "make release" of a NetBSD 1.5
source tree on my 128MB pc to 10% less than a 1.5 kernel took.
2001-09-16 00:36:31 +04:00
|
|
|
(void) uvm_unmap(map, *addr, *addr + size);
|
1998-03-09 03:58:55 +03:00
|
|
|
}
|
|
|
|
|
2003-03-06 03:41:51 +03:00
|
|
|
/*
|
|
|
|
* Try to see if any requested alignment can even be attemped.
|
|
|
|
* Make sure we can express the alignment (asking for a >= 4GB
|
|
|
|
* alignment on an ILP32 architecure make no sense) and the
|
|
|
|
* alignment is at least for a page sized quanitiy. If the
|
|
|
|
* request was for a fixed mapping, make sure supplied address
|
|
|
|
* adheres to the request alignment.
|
|
|
|
*/
|
|
|
|
align = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
|
|
|
|
if (align) {
|
|
|
|
if (align >= sizeof(vaddr_t) * NBBY)
|
|
|
|
return(EINVAL);
|
|
|
|
align = 1L << align;
|
|
|
|
if (align < PAGE_SIZE)
|
|
|
|
return(EINVAL);
|
2005-02-11 05:12:03 +03:00
|
|
|
if (align >= vm_map_max(map))
|
2003-03-06 03:41:51 +03:00
|
|
|
return(ENOMEM);
|
|
|
|
if (flags & MAP_FIXED) {
|
|
|
|
if ((*addr & (align-1)) != 0)
|
|
|
|
return(EINVAL);
|
|
|
|
align = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* handle anon vs. non-anon mappings. for non-anon mappings attach
|
|
|
|
* to underlying vm object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (flags & MAP_ANON) {
|
2006-04-05 23:49:28 +04:00
|
|
|
KASSERT(handle == NULL);
|
1999-11-13 03:24:38 +03:00
|
|
|
foff = UVM_UNKNOWN_OFFSET;
|
1998-03-09 03:58:55 +03:00
|
|
|
uobj = NULL;
|
|
|
|
if ((flags & MAP_SHARED) == 0)
|
|
|
|
/* XXX: defer amap create */
|
|
|
|
uvmflag |= UVM_FLAG_COPYONW;
|
|
|
|
else
|
|
|
|
/* shared: create amap now */
|
|
|
|
uvmflag |= UVM_FLAG_OVERLAY;
|
|
|
|
|
|
|
|
} else {
|
2006-04-05 23:49:28 +04:00
|
|
|
KASSERT(handle != NULL);
|
2001-03-15 09:10:32 +03:00
|
|
|
vp = (struct vnode *)handle;
|
2001-10-30 22:05:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't allow mmap for EXEC if the file system
|
|
|
|
* is mounted NOEXEC.
|
|
|
|
*/
|
|
|
|
if ((prot & PROT_EXEC) != 0 &&
|
|
|
|
(vp->v_mount->mnt_flag & MNT_NOEXEC) != 0)
|
|
|
|
return (EACCES);
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
if (vp->v_type != VCHR) {
|
2006-07-21 20:48:45 +04:00
|
|
|
error = VOP_MMAP(vp, 0, curlwp->l_cred, curlwp);
|
2001-08-17 09:52:46 +04:00
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
uobj = uvn_attach((void *)vp, (flags & MAP_SHARED) ?
|
1998-03-09 03:58:55 +03:00
|
|
|
maxprot : (maxprot & ~VM_PROT_WRITE));
|
|
|
|
|
2000-11-27 11:39:39 +03:00
|
|
|
/* XXX for now, attach doesn't gain a ref */
|
|
|
|
VREF(vp);
|
2001-10-30 02:06:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the vnode is being mapped with PROT_EXEC,
|
|
|
|
* then mark it as text.
|
|
|
|
*/
|
|
|
|
if (prot & PROT_EXEC)
|
2001-10-30 18:32:01 +03:00
|
|
|
vn_markexec(vp);
|
1998-03-09 03:58:55 +03:00
|
|
|
} else {
|
2004-05-19 17:20:27 +04:00
|
|
|
int i = maxprot;
|
|
|
|
|
2001-01-08 04:35:03 +03:00
|
|
|
/*
|
|
|
|
* XXX Some devices don't like to be mapped with
|
2004-05-19 17:20:27 +04:00
|
|
|
* XXX PROT_EXEC or PROT_WRITE, but we don't really
|
|
|
|
* XXX have a better way of handling this, right now
|
2001-01-08 04:35:03 +03:00
|
|
|
*/
|
2004-05-19 17:20:27 +04:00
|
|
|
do {
|
|
|
|
uobj = udv_attach((void *) &vp->v_rdev,
|
|
|
|
(flags & MAP_SHARED) ? i :
|
|
|
|
(i & ~VM_PROT_WRITE), foff, size);
|
|
|
|
i--;
|
|
|
|
} while ((uobj == NULL) && (i > 0));
|
1998-03-09 03:58:55 +03:00
|
|
|
advice = UVM_ADV_RANDOM;
|
|
|
|
}
|
|
|
|
if (uobj == NULL)
|
1998-07-08 03:22:13 +04:00
|
|
|
return((vp->v_type == VREG) ? ENOMEM : EINVAL);
|
2005-07-23 16:18:41 +04:00
|
|
|
if ((flags & MAP_SHARED) == 0) {
|
1998-03-09 03:58:55 +03:00
|
|
|
uvmflag |= UVM_FLAG_COPYONW;
|
2006-10-05 18:48:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set vnode flags to indicate the new kinds of mapping.
|
|
|
|
* We take the vnode lock in exclusive mode here to serialize
|
|
|
|
* with direct I/O.
|
|
|
|
*/
|
|
|
|
|
|
|
|
needwritemap = (vp->v_flag & VWRITEMAP) == 0 &&
|
|
|
|
(flags & MAP_SHARED) != 0 &&
|
|
|
|
(maxprot & VM_PROT_WRITE) != 0;
|
|
|
|
if ((vp->v_flag & VMAPPED) == 0 || needwritemap) {
|
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
2005-07-23 16:18:41 +04:00
|
|
|
simple_lock(&vp->v_interlock);
|
2006-10-05 18:48:32 +04:00
|
|
|
vp->v_flag |= VMAPPED;
|
|
|
|
if (needwritemap) {
|
|
|
|
vp->v_flag |= VWRITEMAP;
|
|
|
|
}
|
2005-07-23 16:18:41 +04:00
|
|
|
simple_unlock(&vp->v_interlock);
|
2006-10-05 18:48:32 +04:00
|
|
|
VOP_UNLOCK(vp, 0);
|
2005-07-23 16:18:41 +04:00
|
|
|
}
|
1998-03-09 03:58:55 +03:00
|
|
|
}
|
|
|
|
|
2001-05-25 08:06:11 +04:00
|
|
|
uvmflag = UVM_MAPFLAG(prot, maxprot,
|
1998-02-05 09:25:08 +03:00
|
|
|
(flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
|
|
|
|
advice, uvmflag);
|
2003-03-06 03:41:51 +03:00
|
|
|
error = uvm_map(map, addr, size, uobj, foff, align, uvmflag);
|
2001-03-15 09:10:32 +03:00
|
|
|
if (error) {
|
|
|
|
if (uobj)
|
|
|
|
uobj->pgops->pgo_detach(uobj);
|
|
|
|
return error;
|
|
|
|
}
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
2001-03-15 09:10:32 +03:00
|
|
|
* POSIX 1003.1b -- if our address space was configured
|
|
|
|
* to lock all future mappings, wire the one we just made.
|
2003-10-07 04:17:09 +04:00
|
|
|
*
|
|
|
|
* Also handle the MAP_WIRED flag here.
|
1998-03-09 03:58:55 +03:00
|
|
|
*/
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
if (prot == VM_PROT_NONE) {
|
1998-03-09 03:58:55 +03:00
|
|
|
|
1999-06-18 09:13:45 +04:00
|
|
|
/*
|
2001-03-15 09:10:32 +03:00
|
|
|
* No more work to do in this case.
|
1999-06-18 09:13:45 +04:00
|
|
|
*/
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
vm_map_lock(map);
|
2003-10-07 04:17:09 +04:00
|
|
|
if ((flags & MAP_WIRED) != 0 || (map->flags & VM_MAP_WIREFUTURE) != 0) {
|
2005-01-23 18:58:13 +03:00
|
|
|
if (atop(size) + uvmexp.wired > uvmexp.wiredmax ||
|
|
|
|
(locklimit != 0 &&
|
|
|
|
size + ptoa(pmap_wired_count(vm_map_pmap(map))) >
|
|
|
|
locklimit)) {
|
2001-03-15 09:10:32 +03:00
|
|
|
vm_map_unlock(map);
|
|
|
|
uvm_unmap(map, *addr, *addr + size);
|
|
|
|
return ENOMEM;
|
1999-06-18 09:13:45 +04:00
|
|
|
}
|
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
/*
|
|
|
|
* uvm_map_pageable() always returns the map unlocked.
|
|
|
|
*/
|
1999-06-18 09:13:45 +04:00
|
|
|
|
2001-03-15 09:10:32 +03:00
|
|
|
error = uvm_map_pageable(map, *addr, *addr + size,
|
|
|
|
FALSE, UVM_LK_ENTER);
|
|
|
|
if (error) {
|
|
|
|
uvm_unmap(map, *addr, *addr + size);
|
|
|
|
return error;
|
|
|
|
}
|
1999-06-18 09:13:45 +04:00
|
|
|
return (0);
|
|
|
|
}
|
2001-03-15 09:10:32 +03:00
|
|
|
vm_map_unlock(map);
|
|
|
|
return 0;
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
2005-03-26 08:12:34 +03:00
|
|
|
|
|
|
|
vaddr_t
|
2006-11-01 13:17:58 +03:00
|
|
|
uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
|
2005-03-26 08:12:34 +03:00
|
|
|
{
|
2006-11-01 13:17:58 +03:00
|
|
|
|
2005-03-26 08:12:34 +03:00
|
|
|
return VM_DEFAULT_ADDRESS(base, sz);
|
|
|
|
}
|