2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
40 * Mapped file (mmap) interface to VM
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
47 #include "opt_hwpmc_hooks.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/filedesc.h>
58 #include <sys/resource.h>
59 #include <sys/resourcevar.h>
60 #include <sys/vnode.h>
61 #include <sys/fcntl.h>
64 #include <sys/mount.h>
67 #include <sys/sysent.h>
68 #include <sys/vmmeter.h>
69 #include <sys/sysctl.h>
71 #include <security/mac/mac_framework.h>
74 #include <vm/vm_param.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
80 #include <vm/vm_pageout.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_kern.h>
86 #include <sys/pmckern.h>
89 #ifndef _SYS_SYSPROTO_H_
95 static int max_proc_mmap;
96 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0,
97 "Maximum number of memory-mapped files per process");
100 * Set the maximum number of vm_map_entry structures per process. Roughly
101 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
102 * of our KVM malloc space still results in generous limits. We want a
103 * default that is good enough to prevent the kernel running out of resources
104 * if attacked from compromised user account but generous enough such that
105 * multi-threaded processes are not unduly inconvenienced.
107 static void vmmapentry_rsrc_init(void *);
108 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init,
112 vmmapentry_rsrc_init(dummy)
115 max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
116 max_proc_mmap /= 100;
119 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
120 int *, struct vnode *, vm_ooffset_t *, vm_object_t *);
121 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
122 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
123 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
124 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
133 struct sbrk_args *uap;
135 /* Not yet implemented */
139 #ifndef _SYS_SYSPROTO_H_
152 struct sstk_args *uap;
154 /* Not yet implemented */
158 #if defined(COMPAT_43)
159 #ifndef _SYS_SYSPROTO_H_
160 struct getpagesize_args {
167 ogetpagesize(td, uap)
169 struct getpagesize_args *uap;
172 td->td_retval[0] = PAGE_SIZE;
175 #endif /* COMPAT_43 */
179 * Memory Map (mmap) system call. Note that the file offset
180 * and address are allowed to be NOT page aligned, though if
181 * the MAP_FIXED flag it set, both must have the same remainder
182 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
183 * page-aligned, the actual mapping starts at trunc_page(addr)
184 * and the return value is adjusted up by the page offset.
186 * Generally speaking, only character devices which are themselves
187 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
188 * there would be no cache coherency between a descriptor and a VM mapping
189 * both to the same character device.
191 #ifndef _SYS_SYSPROTO_H_
209 struct mmap_args *uap;
212 struct pmckern_map_in pkm;
217 vm_size_t size, pageoff;
218 vm_prot_t prot, maxprot;
220 objtype_t handle_type;
223 struct vmspace *vms = td->td_proc->p_vmspace;
225 addr = (vm_offset_t) uap->addr;
227 prot = uap->prot & VM_PROT_ALL;
232 /* make sure mapping fits into numeric range etc */
233 if ((uap->len == 0 && !SV_CURPROC_FLAG(SV_AOUT) &&
234 curproc->p_osrel >= P_OSREL_MAP_ANON) ||
235 ((flags & MAP_ANON) && (uap->fd != -1 || pos != 0)))
238 if (flags & MAP_STACK) {
239 if ((uap->fd != -1) ||
240 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
247 * Align the file position to a page boundary,
248 * and save its page offset component.
250 pageoff = (pos & PAGE_MASK);
253 /* Adjust size for rounding (on both ends). */
254 size += pageoff; /* low end... */
255 size = (vm_size_t) round_page(size); /* hi end */
258 * Check for illegal addresses. Watch out for address wrap... Note
259 * that VM_*_ADDRESS are not constants due to casts (argh).
261 if (flags & MAP_FIXED) {
263 * The specified address must have the same remainder
264 * as the file offset taken modulo PAGE_SIZE, so it
265 * should be aligned after adjustment by pageoff.
268 if (addr & PAGE_MASK)
270 /* Address range must be all in user VM space. */
271 if (addr < vm_map_min(&vms->vm_map) ||
272 addr + size > vm_map_max(&vms->vm_map))
274 if (addr + size < addr)
278 * XXX for non-fixed mappings where no hint is provided or
279 * the hint would fall in the potential heap space,
280 * place it after the end of the largest possible heap.
282 * There should really be a pmap call to determine a reasonable
285 PROC_LOCK(td->td_proc);
287 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
288 addr < round_page((vm_offset_t)vms->vm_daddr +
289 lim_max(td->td_proc, RLIMIT_DATA))))
290 addr = round_page((vm_offset_t)vms->vm_daddr +
291 lim_max(td->td_proc, RLIMIT_DATA));
292 PROC_UNLOCK(td->td_proc);
294 if (flags & MAP_ANON) {
296 * Mapping blank space is trivial.
299 handle_type = OBJT_DEFAULT;
300 maxprot = VM_PROT_ALL;
303 * Mapping file, get fp for validation and
304 * don't let the descriptor disappear on us if we block.
306 if ((error = fget(td, uap->fd, &fp)) != 0)
308 if (fp->f_type == DTYPE_SHM) {
310 handle_type = OBJT_SWAP;
311 maxprot = VM_PROT_NONE;
313 /* FREAD should always be set. */
314 if (fp->f_flag & FREAD)
315 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
316 if (fp->f_flag & FWRITE)
317 maxprot |= VM_PROT_WRITE;
320 if (fp->f_type != DTYPE_VNODE) {
324 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
325 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
327 * POSIX shared-memory objects are defined to have
328 * kernel persistence, and are not defined to support
329 * read(2)/write(2) -- or even open(2). Thus, we can
330 * use MAP_ASYNC to trade on-disk coherence for speed.
331 * The shm_open(3) library routine turns on the FPOSIXSHM
332 * flag to request this behavior.
334 if (fp->f_flag & FPOSIXSHM)
339 * Ensure that file and memory protections are
340 * compatible. Note that we only worry about
341 * writability if mapping is shared; in this case,
342 * current and max prot are dictated by the open file.
343 * XXX use the vnode instead? Problem is: what
344 * credentials do we use for determination? What if
345 * proc does a setuid?
347 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
348 maxprot = VM_PROT_NONE;
350 maxprot = VM_PROT_EXECUTE;
351 if (fp->f_flag & FREAD) {
352 maxprot |= VM_PROT_READ;
353 } else if (prot & PROT_READ) {
358 * If we are sharing potential changes (either via
359 * MAP_SHARED or via the implicit sharing of character
360 * device mappings), and we are trying to get write
361 * permission although we opened it without asking
364 if ((flags & MAP_SHARED) != 0) {
365 if ((fp->f_flag & FWRITE) != 0) {
366 maxprot |= VM_PROT_WRITE;
367 } else if ((prot & PROT_WRITE) != 0) {
371 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
372 maxprot |= VM_PROT_WRITE;
375 handle_type = OBJT_VNODE;
380 * Do not allow more then a certain number of vm_map_entry structures
381 * per process. Scale with the number of rforks sharing the map
382 * to make the limit reasonable for threads.
385 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
391 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
392 flags, handle_type, handle, pos);
395 /* inform hwpmc(4) if an executable is being mapped */
396 if (error == 0 && handle_type == OBJT_VNODE &&
397 (prot & PROT_EXEC)) {
398 pkm.pm_file = handle;
399 pkm.pm_address = (uintptr_t) addr;
400 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
404 td->td_retval[0] = (register_t) (addr + pageoff);
413 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
415 struct mmap_args oargs;
417 oargs.addr = uap->addr;
418 oargs.len = uap->len;
419 oargs.prot = uap->prot;
420 oargs.flags = uap->flags;
422 oargs.pos = uap->pos;
423 return (mmap(td, &oargs));
427 #ifndef _SYS_SYSPROTO_H_
440 struct ommap_args *uap;
442 struct mmap_args nargs;
443 static const char cvtbsdprot[8] = {
447 PROT_EXEC | PROT_WRITE,
449 PROT_EXEC | PROT_READ,
450 PROT_WRITE | PROT_READ,
451 PROT_EXEC | PROT_WRITE | PROT_READ,
454 #define OMAP_ANON 0x0002
455 #define OMAP_COPY 0x0020
456 #define OMAP_SHARED 0x0010
457 #define OMAP_FIXED 0x0100
459 nargs.addr = uap->addr;
460 nargs.len = uap->len;
461 nargs.prot = cvtbsdprot[uap->prot & 0x7];
463 if (uap->flags & OMAP_ANON)
464 nargs.flags |= MAP_ANON;
465 if (uap->flags & OMAP_COPY)
466 nargs.flags |= MAP_COPY;
467 if (uap->flags & OMAP_SHARED)
468 nargs.flags |= MAP_SHARED;
470 nargs.flags |= MAP_PRIVATE;
471 if (uap->flags & OMAP_FIXED)
472 nargs.flags |= MAP_FIXED;
474 nargs.pos = uap->pos;
475 return (mmap(td, &nargs));
477 #endif /* COMPAT_43 */
480 #ifndef _SYS_SYSPROTO_H_
493 struct msync_args *uap;
496 vm_size_t size, pageoff;
501 addr = (vm_offset_t) uap->addr;
505 pageoff = (addr & PAGE_MASK);
508 size = (vm_size_t) round_page(size);
509 if (addr + size < addr)
512 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
515 map = &td->td_proc->p_vmspace->vm_map;
518 * Clean the pages and interpret the return value.
520 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
521 (flags & MS_INVALIDATE) != 0);
525 case KERN_INVALID_ADDRESS:
526 return (EINVAL); /* Sun returns ENOMEM? */
527 case KERN_INVALID_ARGUMENT:
534 #ifndef _SYS_SYSPROTO_H_
546 struct munmap_args *uap;
549 struct pmckern_map_out pkm;
550 vm_map_entry_t entry;
553 vm_size_t size, pageoff;
556 addr = (vm_offset_t) uap->addr;
561 pageoff = (addr & PAGE_MASK);
564 size = (vm_size_t) round_page(size);
565 if (addr + size < addr)
569 * Check for illegal addresses. Watch out for address wrap...
571 map = &td->td_proc->p_vmspace->vm_map;
572 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
577 * Inform hwpmc if the address range being unmapped contains
578 * an executable region.
580 pkm.pm_address = (uintptr_t) NULL;
581 if (vm_map_lookup_entry(map, addr, &entry)) {
583 entry != &map->header && entry->start < addr + size;
584 entry = entry->next) {
585 if (vm_map_check_protection(map, entry->start,
586 entry->end, VM_PROT_EXECUTE) == TRUE) {
587 pkm.pm_address = (uintptr_t) addr;
588 pkm.pm_size = (size_t) size;
594 vm_map_delete(map, addr, addr + size);
597 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
598 vm_map_lock_downgrade(map);
599 if (pkm.pm_address != (uintptr_t) NULL)
600 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
601 vm_map_unlock_read(map);
605 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
609 #ifndef _SYS_SYSPROTO_H_
610 struct mprotect_args {
622 struct mprotect_args *uap;
625 vm_size_t size, pageoff;
628 addr = (vm_offset_t) uap->addr;
630 prot = uap->prot & VM_PROT_ALL;
632 pageoff = (addr & PAGE_MASK);
635 size = (vm_size_t) round_page(size);
636 if (addr + size < addr)
639 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
640 addr + size, prot, FALSE)) {
643 case KERN_PROTECTION_FAILURE:
645 case KERN_RESOURCE_SHORTAGE:
651 #ifndef _SYS_SYSPROTO_H_
652 struct minherit_args {
664 struct minherit_args *uap;
667 vm_size_t size, pageoff;
668 vm_inherit_t inherit;
670 addr = (vm_offset_t)uap->addr;
672 inherit = uap->inherit;
674 pageoff = (addr & PAGE_MASK);
677 size = (vm_size_t) round_page(size);
678 if (addr + size < addr)
681 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
682 addr + size, inherit)) {
685 case KERN_PROTECTION_FAILURE:
691 #ifndef _SYS_SYSPROTO_H_
692 struct madvise_args {
706 struct madvise_args *uap;
708 vm_offset_t start, end;
714 * Check for our special case, advising the swap pager we are
717 if (uap->behav == MADV_PROTECT) {
718 error = priv_check(td, PRIV_VM_MADV_PROTECT);
722 p->p_flag |= P_PROTECTED;
728 * Check for illegal behavior
730 if (uap->behav < 0 || uap->behav > MADV_CORE)
733 * Check for illegal addresses. Watch out for address wrap... Note
734 * that VM_*_ADDRESS are not constants due to casts (argh).
736 map = &td->td_proc->p_vmspace->vm_map;
737 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
738 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
740 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
744 * Since this routine is only advisory, we default to conservative
747 start = trunc_page((vm_offset_t) uap->addr);
748 end = round_page((vm_offset_t) uap->addr + uap->len);
750 if (vm_map_madvise(map, start, end, uap->behav))
755 #ifndef _SYS_SYSPROTO_H_
756 struct mincore_args {
770 struct mincore_args *uap;
772 vm_offset_t addr, first_addr;
773 vm_offset_t end, cend;
778 int vecindex, lastvecindex;
779 vm_map_entry_t current;
780 vm_map_entry_t entry;
782 unsigned int timestamp;
785 * Make sure that the addresses presented are valid for user
788 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
789 end = addr + (vm_size_t)round_page(uap->len);
790 map = &td->td_proc->p_vmspace->vm_map;
791 if (end > vm_map_max(map) || end < addr)
795 * Address of byte vector
799 pmap = vmspace_pmap(td->td_proc->p_vmspace);
801 vm_map_lock_read(map);
803 timestamp = map->timestamp;
805 if (!vm_map_lookup_entry(map, addr, &entry)) {
806 vm_map_unlock_read(map);
811 * Do this on a map entry basis so that if the pages are not
812 * in the current processes address space, we can easily look
813 * up the pages elsewhere.
816 for (current = entry;
817 (current != &map->header) && (current->start < end);
818 current = current->next) {
821 * check for contiguity
823 if (current->end < end &&
824 (entry->next == &map->header ||
825 current->next->start > current->end)) {
826 vm_map_unlock_read(map);
831 * ignore submaps (for now) or null objects
833 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
834 current->object.vm_object == NULL)
838 * limit this scan to the current map entry and the
839 * limits for the mincore call
841 if (addr < current->start)
842 addr = current->start;
848 * scan this entry one page at a time
850 while (addr < cend) {
852 * Check pmap first, it is likely faster, also
853 * it can provide info as to whether we are the
854 * one referencing or modifying the page.
856 mincoreinfo = pmap_mincore(pmap, addr);
862 * calculate the page index into the object
864 offset = current->offset + (addr - current->start);
865 pindex = OFF_TO_IDX(offset);
866 VM_OBJECT_LOCK(current->object.vm_object);
867 m = vm_page_lookup(current->object.vm_object,
870 * if the page is resident, then gather information about
873 if (m != NULL && m->valid != 0) {
874 mincoreinfo = MINCORE_INCORE;
875 vm_page_lock_queues();
878 mincoreinfo |= MINCORE_MODIFIED_OTHER;
879 if ((m->flags & PG_REFERENCED) ||
880 pmap_ts_referenced(m)) {
881 vm_page_flag_set(m, PG_REFERENCED);
882 mincoreinfo |= MINCORE_REFERENCED_OTHER;
884 vm_page_unlock_queues();
886 VM_OBJECT_UNLOCK(current->object.vm_object);
890 * subyte may page fault. In case it needs to modify
891 * the map, we release the lock.
893 vm_map_unlock_read(map);
896 * calculate index into user supplied byte vector
898 vecindex = OFF_TO_IDX(addr - first_addr);
901 * If we have skipped map entries, we need to make sure that
902 * the byte vector is zeroed for those skipped entries.
904 while ((lastvecindex + 1) < vecindex) {
905 error = subyte(vec + lastvecindex, 0);
914 * Pass the page information to the user
916 error = subyte(vec + vecindex, mincoreinfo);
923 * If the map has changed, due to the subyte, the previous
924 * output may be invalid.
926 vm_map_lock_read(map);
927 if (timestamp != map->timestamp)
930 lastvecindex = vecindex;
936 * subyte may page fault. In case it needs to modify
937 * the map, we release the lock.
939 vm_map_unlock_read(map);
942 * Zero the last entries in the byte vector.
944 vecindex = OFF_TO_IDX(end - first_addr);
945 while ((lastvecindex + 1) < vecindex) {
946 error = subyte(vec + lastvecindex, 0);
955 * If the map has changed, due to the subyte, the previous
956 * output may be invalid.
958 vm_map_lock_read(map);
959 if (timestamp != map->timestamp)
961 vm_map_unlock_read(map);
966 #ifndef _SYS_SYSPROTO_H_
978 struct mlock_args *uap;
981 vm_offset_t addr, end, last, start;
982 vm_size_t npages, size;
985 error = priv_check(td, PRIV_VM_MLOCK);
988 addr = (vm_offset_t)uap->addr;
991 start = trunc_page(addr);
992 end = round_page(last);
993 if (last < addr || end < addr)
995 npages = atop(end - start);
996 if (npages > vm_page_max_wired)
1001 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
1002 lim_cur(proc, RLIMIT_MEMLOCK)) {
1007 if (npages + cnt.v_wire_count > vm_page_max_wired)
1009 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
1010 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1011 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1014 #ifndef _SYS_SYSPROTO_H_
1015 struct mlockall_args {
1026 struct mlockall_args *uap;
1031 map = &td->td_proc->p_vmspace->vm_map;
1034 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1039 * If wiring all pages in the process would cause it to exceed
1040 * a hard resource limit, return ENOMEM.
1042 PROC_LOCK(td->td_proc);
1043 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1044 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1045 PROC_UNLOCK(td->td_proc);
1048 PROC_UNLOCK(td->td_proc);
1050 error = priv_check(td, PRIV_VM_MLOCK);
1055 if (uap->how & MCL_FUTURE) {
1057 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1062 if (uap->how & MCL_CURRENT) {
1064 * P1003.1-2001 mandates that all currently mapped pages
1065 * will be memory resident and locked (wired) upon return
1066 * from mlockall(). vm_map_wire() will wire pages, by
1067 * calling vm_fault_wire() for each page in the region.
1069 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1070 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1071 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1077 #ifndef _SYS_SYSPROTO_H_
1078 struct munlockall_args {
1089 struct munlockall_args *uap;
1094 map = &td->td_proc->p_vmspace->vm_map;
1095 error = priv_check(td, PRIV_VM_MUNLOCK);
1099 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1101 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1104 /* Forcibly unwire all pages. */
1105 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1106 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1111 #ifndef _SYS_SYSPROTO_H_
1112 struct munlock_args {
1123 struct munlock_args *uap;
1125 vm_offset_t addr, end, last, start;
1129 error = priv_check(td, PRIV_VM_MUNLOCK);
1132 addr = (vm_offset_t)uap->addr;
1135 start = trunc_page(addr);
1136 end = round_page(last);
1137 if (last < addr || end < addr)
1139 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1140 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1141 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1149 * Helper function for vm_mmap. Perform sanity check specific for mmap
1150 * operations on vnodes.
1153 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1154 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1155 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp)
1166 cred = td->td_ucred;
1167 vfslocked = VFS_LOCK_GIANT(mp);
1168 if ((error = vget(vp, LK_SHARED, td)) != 0) {
1169 VFS_UNLOCK_GIANT(vfslocked);
1175 if (vp->v_type == VREG) {
1177 * Get the proper underlying object
1183 if (obj->handle != vp) {
1185 vp = (struct vnode*)obj->handle;
1186 vget(vp, LK_SHARED, td);
1188 } else if (vp->v_type == VCHR) {
1189 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1190 vp->v_rdev, foffp, objp);
1198 if ((error = VOP_GETATTR(vp, &va, cred)))
1201 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1205 if ((flags & MAP_SHARED) != 0) {
1206 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1207 if (prot & PROT_WRITE) {
1211 *maxprotp &= ~VM_PROT_WRITE;
1215 * If it is a regular file without any references
1216 * we do not need to sync it.
1217 * Adjust object size to be the size of actual file.
1219 objsize = round_page(va.va_size);
1220 if (va.va_nlink == 0)
1221 flags |= MAP_NOSYNC;
1222 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, td->td_ucred);
1231 vfs_mark_atime(vp, cred);
1235 VFS_UNLOCK_GIANT(vfslocked);
1244 * Helper function for vm_mmap. Perform sanity check specific for mmap
1245 * operations on cdevs.
1248 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1249 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1250 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1254 int error, flags, ref;
1258 dsw = dev_refthread(cdev, &ref);
1261 if (dsw->d_flags & D_MMAP_ANON) {
1262 dev_relthread(cdev, ref);
1263 *maxprotp = VM_PROT_ALL;
1264 *flagsp |= MAP_ANON;
1268 * cdevs do not provide private mappings of any kind.
1270 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1271 (prot & PROT_WRITE) != 0) {
1272 dev_relthread(cdev, ref);
1275 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1276 dev_relthread(cdev, ref);
1280 * Force device mappings to be shared.
1282 flags |= MAP_SHARED;
1284 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1286 dev_relthread(cdev, ref);
1291 * First, try d_mmap_single(). If that is not implemented
1292 * (returns ENODEV), fall back to using the device pager.
1293 * Note that d_mmap_single() must return a reference to the
1294 * object (it needs to bump the reference count of the object
1295 * it returns somehow).
1297 * XXX assumes VM_PROT_* == PROT_*
1299 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1300 dev_relthread(cdev, ref);
1301 if (error != ENODEV)
1303 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1317 * Helper function for vm_mmap. Perform sanity check specific for mmap
1318 * operations on shm file descriptors.
1321 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1322 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1323 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1327 if ((*flagsp & MAP_SHARED) != 0 &&
1328 (*maxprotp & VM_PROT_WRITE) == 0 &&
1329 (prot & PROT_WRITE) != 0)
1332 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1336 error = shm_mmap(shmfd, objsize, foff, objp);
1347 * Internal version of mmap. Currently used by mmap, exec, and sys5
1348 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1351 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1352 vm_prot_t maxprot, int flags,
1353 objtype_t handle_type, void *handle,
1357 vm_object_t object = NULL;
1358 int rv = KERN_SUCCESS;
1360 struct thread *td = curthread;
1365 size = round_page(size);
1367 PROC_LOCK(td->td_proc);
1368 if (td->td_proc->p_vmspace->vm_map.size + size >
1369 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1370 PROC_UNLOCK(td->td_proc);
1373 PROC_UNLOCK(td->td_proc);
1376 * We currently can only deal with page aligned file offsets.
1377 * The check is here rather than in the syscall because the
1378 * kernel calls this function internally for other mmaping
1379 * operations (such as in exec) and non-aligned offsets will
1380 * cause pmap inconsistencies...so we want to be sure to
1381 * disallow this in all cases.
1383 if (foff & PAGE_MASK)
1386 if ((flags & MAP_FIXED) == 0) {
1388 *addr = round_page(*addr);
1390 if (*addr != trunc_page(*addr))
1395 * Lookup/allocate object.
1397 switch (handle_type) {
1399 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1400 handle, &foff, &object);
1403 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1404 handle, &foff, &object);
1407 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1408 handle, foff, &object);
1411 if (handle == NULL) {
1422 if (flags & MAP_ANON) {
1426 * Unnamed anonymous regions always start at 0.
1431 docow = MAP_PREFAULT_PARTIAL;
1434 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1435 docow |= MAP_COPY_ON_WRITE;
1436 if (flags & MAP_NOSYNC)
1437 docow |= MAP_DISABLE_SYNCER;
1438 if (flags & MAP_NOCORE)
1439 docow |= MAP_DISABLE_COREDUMP;
1441 if (flags & MAP_STACK)
1442 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1443 docow | MAP_STACK_GROWS_DOWN);
1445 rv = vm_map_find(map, object, foff, addr, size,
1446 object != NULL && object->type == OBJT_DEVICE ?
1447 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1449 rv = vm_map_fixed(map, object, foff, *addr, size,
1450 prot, maxprot, docow);
1452 if (rv != KERN_SUCCESS) {
1454 * Lose the object reference. Will destroy the
1455 * object if it's an unnamed anonymous mapping
1456 * or named anonymous without other references.
1458 vm_object_deallocate(object);
1459 } else if (flags & MAP_SHARED) {
1461 * Shared memory is also shared with children.
1463 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1464 if (rv != KERN_SUCCESS)
1465 (void) vm_map_remove(map, *addr, *addr + size);
1469 * If the process has requested that all future mappings
1470 * be wired, then heed this.
1472 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1473 vm_map_wire(map, *addr, *addr + size,
1474 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1479 case KERN_INVALID_ADDRESS:
1482 case KERN_PROTECTION_FAILURE: