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"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/filedesc.h>
59 #include <sys/resource.h>
60 #include <sys/resourcevar.h>
61 #include <sys/vnode.h>
62 #include <sys/fcntl.h>
65 #include <sys/mount.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, "");
99 * Set the maximum number of vm_map_entry structures per process. Roughly
100 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
101 * of our KVM malloc space still results in generous limits. We want a
102 * default that is good enough to prevent the kernel running out of resources
103 * if attacked from compromised user account but generous enough such that
104 * multi-threaded processes are not unduly inconvenienced.
106 static void vmmapentry_rsrc_init(void *);
107 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init,
111 vmmapentry_rsrc_init(dummy)
114 max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
115 max_proc_mmap /= 100;
118 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
119 int *, struct vnode *, vm_ooffset_t, vm_object_t *);
120 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
121 int *, struct cdev *, vm_ooffset_t, vm_object_t *);
122 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
123 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
132 struct sbrk_args *uap;
134 /* Not yet implemented */
138 #ifndef _SYS_SYSPROTO_H_
151 struct sstk_args *uap;
153 /* Not yet implemented */
157 #if defined(COMPAT_43)
158 #ifndef _SYS_SYSPROTO_H_
159 struct getpagesize_args {
166 ogetpagesize(td, uap)
168 struct getpagesize_args *uap;
171 td->td_retval[0] = PAGE_SIZE;
174 #endif /* COMPAT_43 */
178 * Memory Map (mmap) system call. Note that the file offset
179 * and address are allowed to be NOT page aligned, though if
180 * the MAP_FIXED flag it set, both must have the same remainder
181 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
182 * page-aligned, the actual mapping starts at trunc_page(addr)
183 * and the return value is adjusted up by the page offset.
185 * Generally speaking, only character devices which are themselves
186 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
187 * there would be no cache coherency between a descriptor and a VM mapping
188 * both to the same character device.
190 * Block devices can be mmap'd no matter what they represent. Cache coherency
191 * is maintained as long as you do not write directly to the underlying
194 #ifndef _SYS_SYSPROTO_H_
212 struct mmap_args *uap;
215 struct pmckern_map_in pkm;
220 vm_size_t size, pageoff;
221 vm_prot_t prot, maxprot;
223 objtype_t handle_type;
226 struct vmspace *vms = td->td_proc->p_vmspace;
228 addr = (vm_offset_t) uap->addr;
230 prot = uap->prot & VM_PROT_ALL;
235 /* make sure mapping fits into numeric range etc */
236 if ((ssize_t) uap->len < 0 ||
237 ((flags & MAP_ANON) && uap->fd != -1))
240 if (flags & MAP_STACK) {
241 if ((uap->fd != -1) ||
242 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
249 * Align the file position to a page boundary,
250 * and save its page offset component.
252 pageoff = (pos & PAGE_MASK);
255 /* Adjust size for rounding (on both ends). */
256 size += pageoff; /* low end... */
257 size = (vm_size_t) round_page(size); /* hi end */
260 * Check for illegal addresses. Watch out for address wrap... Note
261 * that VM_*_ADDRESS are not constants due to casts (argh).
263 if (flags & MAP_FIXED) {
265 * The specified address must have the same remainder
266 * as the file offset taken modulo PAGE_SIZE, so it
267 * should be aligned after adjustment by pageoff.
270 if (addr & PAGE_MASK)
272 /* Address range must be all in user VM space. */
273 if (addr < vm_map_min(&vms->vm_map) ||
274 addr + size > vm_map_max(&vms->vm_map))
276 if (addr + size < addr)
280 * XXX for non-fixed mappings where no hint is provided or
281 * the hint would fall in the potential heap space,
282 * place it after the end of the largest possible heap.
284 * There should really be a pmap call to determine a reasonable
287 PROC_LOCK(td->td_proc);
289 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
290 addr < round_page((vm_offset_t)vms->vm_daddr +
291 lim_max(td->td_proc, RLIMIT_DATA))))
292 addr = round_page((vm_offset_t)vms->vm_daddr +
293 lim_max(td->td_proc, RLIMIT_DATA));
294 PROC_UNLOCK(td->td_proc);
296 if (flags & MAP_ANON) {
298 * Mapping blank space is trivial.
301 handle_type = OBJT_DEFAULT;
302 maxprot = VM_PROT_ALL;
306 * Mapping file, get fp for validation and
307 * don't let the descriptor disappear on us if we block.
309 if ((error = fget(td, uap->fd, &fp)) != 0)
311 if (fp->f_type == DTYPE_SHM) {
313 handle_type = OBJT_SWAP;
314 maxprot = VM_PROT_NONE;
316 /* FREAD should always be set. */
317 if (fp->f_flag & FREAD)
318 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
319 if (fp->f_flag & FWRITE)
320 maxprot |= VM_PROT_WRITE;
323 if (fp->f_type != DTYPE_VNODE) {
327 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
328 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
330 * POSIX shared-memory objects are defined to have
331 * kernel persistence, and are not defined to support
332 * read(2)/write(2) -- or even open(2). Thus, we can
333 * use MAP_ASYNC to trade on-disk coherence for speed.
334 * The shm_open(3) library routine turns on the FPOSIXSHM
335 * flag to request this behavior.
337 if (fp->f_flag & FPOSIXSHM)
342 * Ensure that file and memory protections are
343 * compatible. Note that we only worry about
344 * writability if mapping is shared; in this case,
345 * current and max prot are dictated by the open file.
346 * XXX use the vnode instead? Problem is: what
347 * credentials do we use for determination? What if
348 * proc does a setuid?
350 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
351 maxprot = VM_PROT_NONE;
353 maxprot = VM_PROT_EXECUTE;
354 if (fp->f_flag & FREAD) {
355 maxprot |= VM_PROT_READ;
356 } else if (prot & PROT_READ) {
361 * If we are sharing potential changes (either via
362 * MAP_SHARED or via the implicit sharing of character
363 * device mappings), and we are trying to get write
364 * permission although we opened it without asking
367 if ((flags & MAP_SHARED) != 0) {
368 if ((fp->f_flag & FWRITE) != 0) {
369 maxprot |= VM_PROT_WRITE;
370 } else if ((prot & PROT_WRITE) != 0) {
374 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
375 maxprot |= VM_PROT_WRITE;
378 handle_type = OBJT_VNODE;
383 * Do not allow more then a certain number of vm_map_entry structures
384 * per process. Scale with the number of rforks sharing the map
385 * to make the limit reasonable for threads.
388 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
393 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
394 flags, handle_type, handle, pos);
396 /* inform hwpmc(4) if an executable is being mapped */
397 if (error == 0 && handle_type == OBJT_VNODE &&
398 (prot & PROT_EXEC)) {
399 pkm.pm_file = handle;
400 pkm.pm_address = (uintptr_t) addr;
401 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
405 td->td_retval[0] = (register_t) (addr + pageoff);
414 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
416 struct mmap_args oargs;
418 oargs.addr = uap->addr;
419 oargs.len = uap->len;
420 oargs.prot = uap->prot;
421 oargs.flags = uap->flags;
423 oargs.pos = uap->pos;
424 return (mmap(td, &oargs));
428 #ifndef _SYS_SYSPROTO_H_
441 struct ommap_args *uap;
443 struct mmap_args nargs;
444 static const char cvtbsdprot[8] = {
448 PROT_EXEC | PROT_WRITE,
450 PROT_EXEC | PROT_READ,
451 PROT_WRITE | PROT_READ,
452 PROT_EXEC | PROT_WRITE | PROT_READ,
455 #define OMAP_ANON 0x0002
456 #define OMAP_COPY 0x0020
457 #define OMAP_SHARED 0x0010
458 #define OMAP_FIXED 0x0100
460 nargs.addr = uap->addr;
461 nargs.len = uap->len;
462 nargs.prot = cvtbsdprot[uap->prot & 0x7];
464 if (uap->flags & OMAP_ANON)
465 nargs.flags |= MAP_ANON;
466 if (uap->flags & OMAP_COPY)
467 nargs.flags |= MAP_COPY;
468 if (uap->flags & OMAP_SHARED)
469 nargs.flags |= MAP_SHARED;
471 nargs.flags |= MAP_PRIVATE;
472 if (uap->flags & OMAP_FIXED)
473 nargs.flags |= MAP_FIXED;
475 nargs.pos = uap->pos;
476 return (mmap(td, &nargs));
478 #endif /* COMPAT_43 */
481 #ifndef _SYS_SYSPROTO_H_
494 struct msync_args *uap;
497 vm_size_t size, pageoff;
502 addr = (vm_offset_t) uap->addr;
506 pageoff = (addr & PAGE_MASK);
509 size = (vm_size_t) round_page(size);
510 if (addr + size < addr)
513 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
516 map = &td->td_proc->p_vmspace->vm_map;
519 * Clean the pages and interpret the return value.
521 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
522 (flags & MS_INVALIDATE) != 0);
526 case KERN_INVALID_ADDRESS:
527 return (EINVAL); /* Sun returns ENOMEM? */
528 case KERN_INVALID_ARGUMENT:
535 #ifndef _SYS_SYSPROTO_H_
547 struct munmap_args *uap;
550 struct pmckern_map_out pkm;
551 vm_map_entry_t entry;
554 vm_size_t size, pageoff;
557 addr = (vm_offset_t) uap->addr;
562 pageoff = (addr & PAGE_MASK);
565 size = (vm_size_t) round_page(size);
566 if (addr + size < addr)
570 * Check for illegal addresses. Watch out for address wrap...
572 map = &td->td_proc->p_vmspace->vm_map;
573 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
578 * Inform hwpmc if the address range being unmapped contains
579 * an executable region.
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;
589 PMC_CALL_HOOK(td, PMC_FN_MUNMAP,
596 /* returns nothing but KERN_SUCCESS anyway */
597 vm_map_delete(map, addr, addr + size);
602 #ifndef _SYS_SYSPROTO_H_
603 struct mprotect_args {
615 struct mprotect_args *uap;
618 vm_size_t size, pageoff;
621 addr = (vm_offset_t) uap->addr;
623 prot = uap->prot & VM_PROT_ALL;
624 #if defined(VM_PROT_READ_IS_EXEC)
625 if (prot & VM_PROT_READ)
626 prot |= VM_PROT_EXECUTE;
629 pageoff = (addr & PAGE_MASK);
632 size = (vm_size_t) round_page(size);
633 if (addr + size < addr)
636 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
637 addr + size, prot, FALSE)) {
640 case KERN_PROTECTION_FAILURE:
646 #ifndef _SYS_SYSPROTO_H_
647 struct minherit_args {
659 struct minherit_args *uap;
662 vm_size_t size, pageoff;
663 vm_inherit_t inherit;
665 addr = (vm_offset_t)uap->addr;
667 inherit = uap->inherit;
669 pageoff = (addr & PAGE_MASK);
672 size = (vm_size_t) round_page(size);
673 if (addr + size < addr)
676 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
677 addr + size, inherit)) {
680 case KERN_PROTECTION_FAILURE:
686 #ifndef _SYS_SYSPROTO_H_
687 struct madvise_args {
701 struct madvise_args *uap;
703 vm_offset_t start, end;
709 * Check for our special case, advising the swap pager we are
712 if (uap->behav == MADV_PROTECT) {
713 error = priv_check(td, PRIV_VM_MADV_PROTECT);
717 p->p_flag |= P_PROTECTED;
723 * Check for illegal behavior
725 if (uap->behav < 0 || uap->behav > MADV_CORE)
728 * Check for illegal addresses. Watch out for address wrap... Note
729 * that VM_*_ADDRESS are not constants due to casts (argh).
731 map = &td->td_proc->p_vmspace->vm_map;
732 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
733 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
735 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
739 * Since this routine is only advisory, we default to conservative
742 start = trunc_page((vm_offset_t) uap->addr);
743 end = round_page((vm_offset_t) uap->addr + uap->len);
745 if (vm_map_madvise(map, start, end, uap->behav))
750 #ifndef _SYS_SYSPROTO_H_
751 struct mincore_args {
765 struct mincore_args *uap;
767 vm_offset_t addr, first_addr;
768 vm_offset_t end, cend;
773 int vecindex, lastvecindex;
774 vm_map_entry_t current;
775 vm_map_entry_t entry;
777 unsigned int timestamp;
780 * Make sure that the addresses presented are valid for user
783 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
784 end = addr + (vm_size_t)round_page(uap->len);
785 map = &td->td_proc->p_vmspace->vm_map;
786 if (end > vm_map_max(map) || end < addr)
790 * Address of byte vector
794 pmap = vmspace_pmap(td->td_proc->p_vmspace);
796 vm_map_lock_read(map);
798 timestamp = map->timestamp;
800 if (!vm_map_lookup_entry(map, addr, &entry)) {
801 vm_map_unlock_read(map);
806 * Do this on a map entry basis so that if the pages are not
807 * in the current processes address space, we can easily look
808 * up the pages elsewhere.
811 for (current = entry;
812 (current != &map->header) && (current->start < end);
813 current = current->next) {
816 * check for contiguity
818 if (current->end < end &&
819 (entry->next == &map->header ||
820 current->next->start > current->end)) {
821 vm_map_unlock_read(map);
826 * ignore submaps (for now) or null objects
828 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
829 current->object.vm_object == NULL)
833 * limit this scan to the current map entry and the
834 * limits for the mincore call
836 if (addr < current->start)
837 addr = current->start;
843 * scan this entry one page at a time
845 while (addr < cend) {
847 * Check pmap first, it is likely faster, also
848 * it can provide info as to whether we are the
849 * one referencing or modifying the page.
851 mincoreinfo = pmap_mincore(pmap, addr);
857 * calculate the page index into the object
859 offset = current->offset + (addr - current->start);
860 pindex = OFF_TO_IDX(offset);
861 VM_OBJECT_LOCK(current->object.vm_object);
862 m = vm_page_lookup(current->object.vm_object,
865 * if the page is resident, then gather information about
868 if (m != NULL && m->valid != 0) {
869 mincoreinfo = MINCORE_INCORE;
870 vm_page_lock_queues();
873 mincoreinfo |= MINCORE_MODIFIED_OTHER;
874 if ((m->flags & PG_REFERENCED) ||
875 pmap_ts_referenced(m)) {
876 vm_page_flag_set(m, PG_REFERENCED);
877 mincoreinfo |= MINCORE_REFERENCED_OTHER;
879 vm_page_unlock_queues();
881 VM_OBJECT_UNLOCK(current->object.vm_object);
885 * subyte may page fault. In case it needs to modify
886 * the map, we release the lock.
888 vm_map_unlock_read(map);
891 * calculate index into user supplied byte vector
893 vecindex = OFF_TO_IDX(addr - first_addr);
896 * If we have skipped map entries, we need to make sure that
897 * the byte vector is zeroed for those skipped entries.
899 while ((lastvecindex + 1) < vecindex) {
900 error = subyte(vec + lastvecindex, 0);
909 * Pass the page information to the user
911 error = subyte(vec + vecindex, mincoreinfo);
918 * If the map has changed, due to the subyte, the previous
919 * output may be invalid.
921 vm_map_lock_read(map);
922 if (timestamp != map->timestamp)
925 lastvecindex = vecindex;
931 * subyte may page fault. In case it needs to modify
932 * the map, we release the lock.
934 vm_map_unlock_read(map);
937 * Zero the last entries in the byte vector.
939 vecindex = OFF_TO_IDX(end - first_addr);
940 while ((lastvecindex + 1) < vecindex) {
941 error = subyte(vec + lastvecindex, 0);
950 * If the map has changed, due to the subyte, the previous
951 * output may be invalid.
953 vm_map_lock_read(map);
954 if (timestamp != map->timestamp)
956 vm_map_unlock_read(map);
961 #ifndef _SYS_SYSPROTO_H_
973 struct mlock_args *uap;
976 vm_offset_t addr, end, last, start;
977 vm_size_t npages, size;
980 error = priv_check(td, PRIV_VM_MLOCK);
983 addr = (vm_offset_t)uap->addr;
986 start = trunc_page(addr);
987 end = round_page(last);
988 if (last < addr || end < addr)
990 npages = atop(end - start);
991 if (npages > vm_page_max_wired)
996 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
997 lim_cur(proc, RLIMIT_MEMLOCK)) {
1002 if (npages + cnt.v_wire_count > vm_page_max_wired)
1004 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
1005 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1006 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1009 #ifndef _SYS_SYSPROTO_H_
1010 struct mlockall_args {
1021 struct mlockall_args *uap;
1026 map = &td->td_proc->p_vmspace->vm_map;
1029 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1034 * If wiring all pages in the process would cause it to exceed
1035 * a hard resource limit, return ENOMEM.
1037 PROC_LOCK(td->td_proc);
1038 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1039 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1040 PROC_UNLOCK(td->td_proc);
1043 PROC_UNLOCK(td->td_proc);
1045 error = priv_check(td, PRIV_VM_MLOCK);
1050 if (uap->how & MCL_FUTURE) {
1052 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1057 if (uap->how & MCL_CURRENT) {
1059 * P1003.1-2001 mandates that all currently mapped pages
1060 * will be memory resident and locked (wired) upon return
1061 * from mlockall(). vm_map_wire() will wire pages, by
1062 * calling vm_fault_wire() for each page in the region.
1064 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1065 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1066 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1072 #ifndef _SYS_SYSPROTO_H_
1073 struct munlockall_args {
1084 struct munlockall_args *uap;
1089 map = &td->td_proc->p_vmspace->vm_map;
1090 error = priv_check(td, PRIV_VM_MUNLOCK);
1094 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1096 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1099 /* Forcibly unwire all pages. */
1100 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1101 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1106 #ifndef _SYS_SYSPROTO_H_
1107 struct munlock_args {
1118 struct munlock_args *uap;
1120 vm_offset_t addr, end, last, start;
1124 error = priv_check(td, PRIV_VM_MUNLOCK);
1127 addr = (vm_offset_t)uap->addr;
1130 start = trunc_page(addr);
1131 end = round_page(last);
1132 if (last < addr || end < addr)
1134 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1135 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1136 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1144 * Helper function for vm_mmap. Perform sanity check specific for mmap
1145 * operations on vnodes.
1148 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1149 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1150 struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1157 int error, flags, type;
1161 vfslocked = VFS_LOCK_GIANT(mp);
1162 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1163 VFS_UNLOCK_GIANT(vfslocked);
1168 if (vp->v_type == VREG) {
1170 * Get the proper underlying object
1176 if (obj->handle != vp) {
1178 vp = (struct vnode*)obj->handle;
1179 vget(vp, LK_EXCLUSIVE, td);
1183 } else if (vp->v_type == VCHR) {
1185 handle = vp->v_rdev;
1187 dsw = dev_refthread(handle);
1192 if (dsw->d_flags & D_MMAP_ANON) {
1193 dev_relthread(handle);
1194 *maxprotp = VM_PROT_ALL;
1195 *flagsp |= MAP_ANON;
1199 dev_relthread(handle);
1201 * cdevs does not provide private mappings of any kind.
1203 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1204 (prot & PROT_WRITE) != 0) {
1208 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1213 * Force device mappings to be shared.
1215 flags |= MAP_SHARED;
1220 if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1224 error = mac_vnode_check_mmap(td->td_ucred, vp, prot, flags);
1228 if ((flags & MAP_SHARED) != 0) {
1229 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1230 if (prot & PROT_WRITE) {
1234 *maxprotp &= ~VM_PROT_WRITE;
1238 * If it is a regular file without any references
1239 * we do not need to sync it.
1240 * Adjust object size to be the size of actual file.
1242 if (vp->v_type == VREG) {
1243 objsize = round_page(va.va_size);
1244 if (va.va_nlink == 0)
1245 flags |= MAP_NOSYNC;
1247 obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1249 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1254 vfs_mark_atime(vp, td);
1258 VFS_UNLOCK_GIANT(vfslocked);
1267 * Helper function for vm_mmap. Perform sanity check specific for mmap
1268 * operations on cdevs.
1271 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1272 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1273 struct cdev *cdev, vm_ooffset_t foff, vm_object_t *objp)
1281 dsw = dev_refthread(cdev);
1284 if (dsw->d_flags & D_MMAP_ANON) {
1285 dev_relthread(cdev);
1286 *maxprotp = VM_PROT_ALL;
1287 *flagsp |= MAP_ANON;
1290 dev_relthread(cdev);
1292 * cdevs does not provide private mappings of any kind.
1294 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1295 (prot & PROT_WRITE) != 0)
1297 if (flags & (MAP_PRIVATE|MAP_COPY))
1300 * Force device mappings to be shared.
1302 flags |= MAP_SHARED;
1304 error = mac_check_cdev_mmap(td->td_ucred, cdev, prot);
1308 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, foff);
1321 * Helper function for vm_mmap. Perform sanity check specific for mmap
1322 * operations on shm file descriptors.
1325 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1326 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1327 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1331 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1332 (prot & PROT_WRITE) != 0)
1335 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1339 error = shm_mmap(shmfd, objsize, foff, objp);
1350 * Internal version of mmap. Currently used by mmap, exec, and sys5
1351 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1354 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1355 vm_prot_t maxprot, int flags,
1356 objtype_t handle_type, void *handle,
1360 vm_object_t object = NULL;
1361 int rv = KERN_SUCCESS;
1363 struct thread *td = curthread;
1368 size = round_page(size);
1370 PROC_LOCK(td->td_proc);
1371 if (td->td_proc->p_vmspace->vm_map.size + size >
1372 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1373 PROC_UNLOCK(td->td_proc);
1376 PROC_UNLOCK(td->td_proc);
1379 * We currently can only deal with page aligned file offsets.
1380 * The check is here rather than in the syscall because the
1381 * kernel calls this function internally for other mmaping
1382 * operations (such as in exec) and non-aligned offsets will
1383 * cause pmap inconsistencies...so we want to be sure to
1384 * disallow this in all cases.
1386 if (foff & PAGE_MASK)
1389 if ((flags & MAP_FIXED) == 0) {
1391 *addr = round_page(*addr);
1393 if (*addr != trunc_page(*addr))
1398 * Lookup/allocate object.
1400 switch (handle_type) {
1402 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1403 handle, foff, &object);
1406 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1407 handle, foff, &object);
1410 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1411 handle, foff, &object);
1414 if (handle == NULL) {
1425 if (flags & MAP_ANON) {
1429 * Unnamed anonymous regions always start at 0.
1434 docow = MAP_PREFAULT_PARTIAL;
1437 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1438 docow |= MAP_COPY_ON_WRITE;
1439 if (flags & MAP_NOSYNC)
1440 docow |= MAP_DISABLE_SYNCER;
1441 if (flags & MAP_NOCORE)
1442 docow |= MAP_DISABLE_COREDUMP;
1444 #if defined(VM_PROT_READ_IS_EXEC)
1445 if (prot & VM_PROT_READ)
1446 prot |= VM_PROT_EXECUTE;
1448 if (maxprot & VM_PROT_READ)
1449 maxprot |= VM_PROT_EXECUTE;
1452 if (flags & MAP_STACK)
1453 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1454 docow | MAP_STACK_GROWS_DOWN);
1456 rv = vm_map_find(map, object, foff, addr, size,
1457 object != NULL && object->type == OBJT_DEVICE ?
1458 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1460 rv = vm_map_fixed(map, object, foff, *addr, size,
1461 prot, maxprot, docow);
1463 if (rv != KERN_SUCCESS) {
1465 * Lose the object reference. Will destroy the
1466 * object if it's an unnamed anonymous mapping
1467 * or named anonymous without other references.
1469 vm_object_deallocate(object);
1470 } else if (flags & MAP_SHARED) {
1472 * Shared memory is also shared with children.
1474 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1475 if (rv != KERN_SUCCESS)
1476 (void) vm_map_remove(map, *addr, *addr + size);
1480 * If the process has requested that all future mappings
1481 * be wired, then heed this.
1483 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1484 vm_map_wire(map, *addr, *addr + size,
1485 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1490 case KERN_INVALID_ADDRESS:
1493 case KERN_PROTECTION_FAILURE: