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/vmmeter.h>
68 #include <sys/sysctl.h>
70 #include <security/mac/mac_framework.h>
73 #include <vm/vm_param.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_pager.h>
79 #include <vm/vm_pageout.h>
80 #include <vm/vm_extern.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_kern.h>
85 #include <sys/pmckern.h>
88 #ifndef _SYS_SYSPROTO_H_
94 static int max_proc_mmap;
95 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0,
96 "Maximum number of memory-mapped files per process");
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 #ifndef _SYS_SYSPROTO_H_
208 struct mmap_args *uap;
211 struct pmckern_map_in pkm;
216 vm_size_t size, pageoff;
217 vm_prot_t prot, maxprot;
219 objtype_t handle_type;
222 struct vmspace *vms = td->td_proc->p_vmspace;
224 addr = (vm_offset_t) uap->addr;
226 prot = uap->prot & VM_PROT_ALL;
231 /* make sure mapping fits into numeric range etc */
233 ((flags & MAP_ANON) && uap->fd != -1))
236 if (flags & MAP_STACK) {
237 if ((uap->fd != -1) ||
238 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
245 * Align the file position to a page boundary,
246 * and save its page offset component.
248 pageoff = (pos & PAGE_MASK);
251 /* Adjust size for rounding (on both ends). */
252 size += pageoff; /* low end... */
253 size = (vm_size_t) round_page(size); /* hi end */
256 * Check for illegal addresses. Watch out for address wrap... Note
257 * that VM_*_ADDRESS are not constants due to casts (argh).
259 if (flags & MAP_FIXED) {
261 * The specified address must have the same remainder
262 * as the file offset taken modulo PAGE_SIZE, so it
263 * should be aligned after adjustment by pageoff.
266 if (addr & PAGE_MASK)
268 /* Address range must be all in user VM space. */
269 if (addr < vm_map_min(&vms->vm_map) ||
270 addr + size > vm_map_max(&vms->vm_map))
272 if (addr + size < addr)
276 * XXX for non-fixed mappings where no hint is provided or
277 * the hint would fall in the potential heap space,
278 * place it after the end of the largest possible heap.
280 * There should really be a pmap call to determine a reasonable
283 PROC_LOCK(td->td_proc);
285 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
286 addr < round_page((vm_offset_t)vms->vm_daddr +
287 lim_max(td->td_proc, RLIMIT_DATA))))
288 addr = round_page((vm_offset_t)vms->vm_daddr +
289 lim_max(td->td_proc, RLIMIT_DATA));
290 PROC_UNLOCK(td->td_proc);
292 if (flags & MAP_ANON) {
294 * Mapping blank space is trivial.
297 handle_type = OBJT_DEFAULT;
298 maxprot = VM_PROT_ALL;
302 * Mapping file, get fp for validation and
303 * don't let the descriptor disappear on us if we block.
305 if ((error = fget(td, uap->fd, &fp)) != 0)
307 if (fp->f_type == DTYPE_SHM) {
309 handle_type = OBJT_SWAP;
310 maxprot = VM_PROT_NONE;
312 /* FREAD should always be set. */
313 if (fp->f_flag & FREAD)
314 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
315 if (fp->f_flag & FWRITE)
316 maxprot |= VM_PROT_WRITE;
319 if (fp->f_type != DTYPE_VNODE) {
323 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
324 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
326 * POSIX shared-memory objects are defined to have
327 * kernel persistence, and are not defined to support
328 * read(2)/write(2) -- or even open(2). Thus, we can
329 * use MAP_ASYNC to trade on-disk coherence for speed.
330 * The shm_open(3) library routine turns on the FPOSIXSHM
331 * flag to request this behavior.
333 if (fp->f_flag & FPOSIXSHM)
338 * Ensure that file and memory protections are
339 * compatible. Note that we only worry about
340 * writability if mapping is shared; in this case,
341 * current and max prot are dictated by the open file.
342 * XXX use the vnode instead? Problem is: what
343 * credentials do we use for determination? What if
344 * proc does a setuid?
346 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
347 maxprot = VM_PROT_NONE;
349 maxprot = VM_PROT_EXECUTE;
350 if (fp->f_flag & FREAD) {
351 maxprot |= VM_PROT_READ;
352 } else if (prot & PROT_READ) {
357 * If we are sharing potential changes (either via
358 * MAP_SHARED or via the implicit sharing of character
359 * device mappings), and we are trying to get write
360 * permission although we opened it without asking
363 if ((flags & MAP_SHARED) != 0) {
364 if ((fp->f_flag & FWRITE) != 0) {
365 maxprot |= VM_PROT_WRITE;
366 } else if ((prot & PROT_WRITE) != 0) {
370 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
371 maxprot |= VM_PROT_WRITE;
374 handle_type = OBJT_VNODE;
379 * Do not allow more then a certain number of vm_map_entry structures
380 * per process. Scale with the number of rforks sharing the map
381 * to make the limit reasonable for threads.
384 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
390 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
391 flags, handle_type, handle, pos);
394 /* inform hwpmc(4) if an executable is being mapped */
395 if (error == 0 && handle_type == OBJT_VNODE &&
396 (prot & PROT_EXEC)) {
397 pkm.pm_file = handle;
398 pkm.pm_address = (uintptr_t) addr;
399 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
403 td->td_retval[0] = (register_t) (addr + pageoff);
412 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
414 struct mmap_args oargs;
416 oargs.addr = uap->addr;
417 oargs.len = uap->len;
418 oargs.prot = uap->prot;
419 oargs.flags = uap->flags;
421 oargs.pos = uap->pos;
422 return (mmap(td, &oargs));
426 #ifndef _SYS_SYSPROTO_H_
439 struct ommap_args *uap;
441 struct mmap_args nargs;
442 static const char cvtbsdprot[8] = {
446 PROT_EXEC | PROT_WRITE,
448 PROT_EXEC | PROT_READ,
449 PROT_WRITE | PROT_READ,
450 PROT_EXEC | PROT_WRITE | PROT_READ,
453 #define OMAP_ANON 0x0002
454 #define OMAP_COPY 0x0020
455 #define OMAP_SHARED 0x0010
456 #define OMAP_FIXED 0x0100
458 nargs.addr = uap->addr;
459 nargs.len = uap->len;
460 nargs.prot = cvtbsdprot[uap->prot & 0x7];
462 if (uap->flags & OMAP_ANON)
463 nargs.flags |= MAP_ANON;
464 if (uap->flags & OMAP_COPY)
465 nargs.flags |= MAP_COPY;
466 if (uap->flags & OMAP_SHARED)
467 nargs.flags |= MAP_SHARED;
469 nargs.flags |= MAP_PRIVATE;
470 if (uap->flags & OMAP_FIXED)
471 nargs.flags |= MAP_FIXED;
473 nargs.pos = uap->pos;
474 return (mmap(td, &nargs));
476 #endif /* COMPAT_43 */
479 #ifndef _SYS_SYSPROTO_H_
492 struct msync_args *uap;
495 vm_size_t size, pageoff;
500 addr = (vm_offset_t) uap->addr;
504 pageoff = (addr & PAGE_MASK);
507 size = (vm_size_t) round_page(size);
508 if (addr + size < addr)
511 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
514 map = &td->td_proc->p_vmspace->vm_map;
517 * Clean the pages and interpret the return value.
519 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
520 (flags & MS_INVALIDATE) != 0);
524 case KERN_INVALID_ADDRESS:
525 return (EINVAL); /* Sun returns ENOMEM? */
526 case KERN_INVALID_ARGUMENT:
533 #ifndef _SYS_SYSPROTO_H_
545 struct munmap_args *uap;
548 struct pmckern_map_out pkm;
549 vm_map_entry_t entry;
552 vm_size_t size, pageoff;
555 addr = (vm_offset_t) uap->addr;
560 pageoff = (addr & PAGE_MASK);
563 size = (vm_size_t) round_page(size);
564 if (addr + size < addr)
568 * Check for illegal addresses. Watch out for address wrap...
570 map = &td->td_proc->p_vmspace->vm_map;
571 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
576 * Inform hwpmc if the address range being unmapped contains
577 * an executable region.
579 if (vm_map_lookup_entry(map, addr, &entry)) {
581 entry != &map->header && entry->start < addr + size;
582 entry = entry->next) {
583 if (vm_map_check_protection(map, entry->start,
584 entry->end, VM_PROT_EXECUTE) == TRUE) {
585 pkm.pm_address = (uintptr_t) addr;
586 pkm.pm_size = (size_t) size;
587 PMC_CALL_HOOK(td, PMC_FN_MUNMAP,
594 /* returns nothing but KERN_SUCCESS anyway */
595 vm_map_delete(map, addr, addr + size);
600 #ifndef _SYS_SYSPROTO_H_
601 struct mprotect_args {
613 struct mprotect_args *uap;
616 vm_size_t size, pageoff;
619 addr = (vm_offset_t) uap->addr;
621 prot = uap->prot & VM_PROT_ALL;
623 pageoff = (addr & PAGE_MASK);
626 size = (vm_size_t) round_page(size);
627 if (addr + size < addr)
630 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
631 addr + size, prot, FALSE)) {
634 case KERN_PROTECTION_FAILURE:
636 case KERN_RESOURCE_SHORTAGE:
642 #ifndef _SYS_SYSPROTO_H_
643 struct minherit_args {
655 struct minherit_args *uap;
658 vm_size_t size, pageoff;
659 vm_inherit_t inherit;
661 addr = (vm_offset_t)uap->addr;
663 inherit = uap->inherit;
665 pageoff = (addr & PAGE_MASK);
668 size = (vm_size_t) round_page(size);
669 if (addr + size < addr)
672 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
673 addr + size, inherit)) {
676 case KERN_PROTECTION_FAILURE:
682 #ifndef _SYS_SYSPROTO_H_
683 struct madvise_args {
697 struct madvise_args *uap;
699 vm_offset_t start, end;
705 * Check for our special case, advising the swap pager we are
708 if (uap->behav == MADV_PROTECT) {
709 error = priv_check(td, PRIV_VM_MADV_PROTECT);
713 p->p_flag |= P_PROTECTED;
719 * Check for illegal behavior
721 if (uap->behav < 0 || uap->behav > MADV_CORE)
724 * Check for illegal addresses. Watch out for address wrap... Note
725 * that VM_*_ADDRESS are not constants due to casts (argh).
727 map = &td->td_proc->p_vmspace->vm_map;
728 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
729 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
731 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
735 * Since this routine is only advisory, we default to conservative
738 start = trunc_page((vm_offset_t) uap->addr);
739 end = round_page((vm_offset_t) uap->addr + uap->len);
741 if (vm_map_madvise(map, start, end, uap->behav))
746 #ifndef _SYS_SYSPROTO_H_
747 struct mincore_args {
761 struct mincore_args *uap;
763 vm_offset_t addr, first_addr;
764 vm_offset_t end, cend;
769 int vecindex, lastvecindex;
770 vm_map_entry_t current;
771 vm_map_entry_t entry;
773 unsigned int timestamp;
776 * Make sure that the addresses presented are valid for user
779 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
780 end = addr + (vm_size_t)round_page(uap->len);
781 map = &td->td_proc->p_vmspace->vm_map;
782 if (end > vm_map_max(map) || end < addr)
786 * Address of byte vector
790 pmap = vmspace_pmap(td->td_proc->p_vmspace);
792 vm_map_lock_read(map);
794 timestamp = map->timestamp;
796 if (!vm_map_lookup_entry(map, addr, &entry)) {
797 vm_map_unlock_read(map);
802 * Do this on a map entry basis so that if the pages are not
803 * in the current processes address space, we can easily look
804 * up the pages elsewhere.
807 for (current = entry;
808 (current != &map->header) && (current->start < end);
809 current = current->next) {
812 * check for contiguity
814 if (current->end < end &&
815 (entry->next == &map->header ||
816 current->next->start > current->end)) {
817 vm_map_unlock_read(map);
822 * ignore submaps (for now) or null objects
824 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
825 current->object.vm_object == NULL)
829 * limit this scan to the current map entry and the
830 * limits for the mincore call
832 if (addr < current->start)
833 addr = current->start;
839 * scan this entry one page at a time
841 while (addr < cend) {
843 * Check pmap first, it is likely faster, also
844 * it can provide info as to whether we are the
845 * one referencing or modifying the page.
847 mincoreinfo = pmap_mincore(pmap, addr);
853 * calculate the page index into the object
855 offset = current->offset + (addr - current->start);
856 pindex = OFF_TO_IDX(offset);
857 VM_OBJECT_LOCK(current->object.vm_object);
858 m = vm_page_lookup(current->object.vm_object,
861 * if the page is resident, then gather information about
864 if (m != NULL && m->valid != 0) {
865 mincoreinfo = MINCORE_INCORE;
866 vm_page_lock_queues();
869 mincoreinfo |= MINCORE_MODIFIED_OTHER;
870 if ((m->flags & PG_REFERENCED) ||
871 pmap_ts_referenced(m)) {
872 vm_page_flag_set(m, PG_REFERENCED);
873 mincoreinfo |= MINCORE_REFERENCED_OTHER;
875 vm_page_unlock_queues();
877 VM_OBJECT_UNLOCK(current->object.vm_object);
881 * subyte may page fault. In case it needs to modify
882 * the map, we release the lock.
884 vm_map_unlock_read(map);
887 * calculate index into user supplied byte vector
889 vecindex = OFF_TO_IDX(addr - first_addr);
892 * If we have skipped map entries, we need to make sure that
893 * the byte vector is zeroed for those skipped entries.
895 while ((lastvecindex + 1) < vecindex) {
896 error = subyte(vec + lastvecindex, 0);
905 * Pass the page information to the user
907 error = subyte(vec + vecindex, mincoreinfo);
914 * If the map has changed, due to the subyte, the previous
915 * output may be invalid.
917 vm_map_lock_read(map);
918 if (timestamp != map->timestamp)
921 lastvecindex = vecindex;
927 * subyte may page fault. In case it needs to modify
928 * the map, we release the lock.
930 vm_map_unlock_read(map);
933 * Zero the last entries in the byte vector.
935 vecindex = OFF_TO_IDX(end - first_addr);
936 while ((lastvecindex + 1) < vecindex) {
937 error = subyte(vec + lastvecindex, 0);
946 * If the map has changed, due to the subyte, the previous
947 * output may be invalid.
949 vm_map_lock_read(map);
950 if (timestamp != map->timestamp)
952 vm_map_unlock_read(map);
957 #ifndef _SYS_SYSPROTO_H_
969 struct mlock_args *uap;
972 vm_offset_t addr, end, last, start;
973 vm_size_t npages, size;
976 error = priv_check(td, PRIV_VM_MLOCK);
979 addr = (vm_offset_t)uap->addr;
982 start = trunc_page(addr);
983 end = round_page(last);
984 if (last < addr || end < addr)
986 npages = atop(end - start);
987 if (npages > vm_page_max_wired)
992 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
993 lim_cur(proc, RLIMIT_MEMLOCK)) {
998 if (npages + cnt.v_wire_count > vm_page_max_wired)
1000 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
1001 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1002 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1005 #ifndef _SYS_SYSPROTO_H_
1006 struct mlockall_args {
1017 struct mlockall_args *uap;
1022 map = &td->td_proc->p_vmspace->vm_map;
1025 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1030 * If wiring all pages in the process would cause it to exceed
1031 * a hard resource limit, return ENOMEM.
1033 PROC_LOCK(td->td_proc);
1034 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1035 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1036 PROC_UNLOCK(td->td_proc);
1039 PROC_UNLOCK(td->td_proc);
1041 error = priv_check(td, PRIV_VM_MLOCK);
1046 if (uap->how & MCL_FUTURE) {
1048 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1053 if (uap->how & MCL_CURRENT) {
1055 * P1003.1-2001 mandates that all currently mapped pages
1056 * will be memory resident and locked (wired) upon return
1057 * from mlockall(). vm_map_wire() will wire pages, by
1058 * calling vm_fault_wire() for each page in the region.
1060 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1061 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1062 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1068 #ifndef _SYS_SYSPROTO_H_
1069 struct munlockall_args {
1080 struct munlockall_args *uap;
1085 map = &td->td_proc->p_vmspace->vm_map;
1086 error = priv_check(td, PRIV_VM_MUNLOCK);
1090 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1092 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1095 /* Forcibly unwire all pages. */
1096 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1097 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1102 #ifndef _SYS_SYSPROTO_H_
1103 struct munlock_args {
1114 struct munlock_args *uap;
1116 vm_offset_t addr, end, last, start;
1120 error = priv_check(td, PRIV_VM_MUNLOCK);
1123 addr = (vm_offset_t)uap->addr;
1126 start = trunc_page(addr);
1127 end = round_page(last);
1128 if (last < addr || end < addr)
1130 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1131 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1132 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1140 * Helper function for vm_mmap. Perform sanity check specific for mmap
1141 * operations on vnodes.
1144 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1145 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1146 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp)
1157 cred = td->td_ucred;
1158 vfslocked = VFS_LOCK_GIANT(mp);
1159 if ((error = vget(vp, LK_SHARED, td)) != 0) {
1160 VFS_UNLOCK_GIANT(vfslocked);
1166 if (vp->v_type == VREG) {
1168 * Get the proper underlying object
1174 if (obj->handle != vp) {
1176 vp = (struct vnode*)obj->handle;
1177 vget(vp, LK_SHARED, td);
1179 } else if (vp->v_type == VCHR) {
1180 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1181 vp->v_rdev, foffp, objp);
1189 if ((error = VOP_GETATTR(vp, &va, cred)))
1192 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1196 if ((flags & MAP_SHARED) != 0) {
1197 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1198 if (prot & PROT_WRITE) {
1202 *maxprotp &= ~VM_PROT_WRITE;
1206 * If it is a regular file without any references
1207 * we do not need to sync it.
1208 * Adjust object size to be the size of actual file.
1210 objsize = round_page(va.va_size);
1211 if (va.va_nlink == 0)
1212 flags |= MAP_NOSYNC;
1213 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, td->td_ucred);
1222 vfs_mark_atime(vp, cred);
1226 VFS_UNLOCK_GIANT(vfslocked);
1235 * Helper function for vm_mmap. Perform sanity check specific for mmap
1236 * operations on cdevs.
1239 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1240 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1241 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1249 dsw = dev_refthread(cdev);
1252 if (dsw->d_flags & D_MMAP_ANON) {
1253 dev_relthread(cdev);
1254 *maxprotp = VM_PROT_ALL;
1255 *flagsp |= MAP_ANON;
1259 * cdevs do not provide private mappings of any kind.
1261 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1262 (prot & PROT_WRITE) != 0) {
1263 dev_relthread(cdev);
1266 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1267 dev_relthread(cdev);
1271 * Force device mappings to be shared.
1273 flags |= MAP_SHARED;
1275 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1277 dev_relthread(cdev);
1282 * First, try d_mmap_single(). If that is not implemented
1283 * (returns ENODEV), fall back to using the device pager.
1284 * Note that d_mmap_single() must return a reference to the
1285 * object (it needs to bump the reference count of the object
1286 * it returns somehow).
1288 * XXX assumes VM_PROT_* == PROT_*
1290 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1291 dev_relthread(cdev);
1292 if (error != ENODEV)
1294 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1308 * Helper function for vm_mmap. Perform sanity check specific for mmap
1309 * operations on shm file descriptors.
1312 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1313 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1314 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1318 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1319 (prot & PROT_WRITE) != 0)
1322 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1326 error = shm_mmap(shmfd, objsize, foff, objp);
1337 * Internal version of mmap. Currently used by mmap, exec, and sys5
1338 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1341 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1342 vm_prot_t maxprot, int flags,
1343 objtype_t handle_type, void *handle,
1347 vm_object_t object = NULL;
1348 int rv = KERN_SUCCESS;
1350 struct thread *td = curthread;
1355 size = round_page(size);
1357 PROC_LOCK(td->td_proc);
1358 if (td->td_proc->p_vmspace->vm_map.size + size >
1359 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1360 PROC_UNLOCK(td->td_proc);
1363 PROC_UNLOCK(td->td_proc);
1366 * We currently can only deal with page aligned file offsets.
1367 * The check is here rather than in the syscall because the
1368 * kernel calls this function internally for other mmaping
1369 * operations (such as in exec) and non-aligned offsets will
1370 * cause pmap inconsistencies...so we want to be sure to
1371 * disallow this in all cases.
1373 if (foff & PAGE_MASK)
1376 if ((flags & MAP_FIXED) == 0) {
1378 *addr = round_page(*addr);
1380 if (*addr != trunc_page(*addr))
1385 * Lookup/allocate object.
1387 switch (handle_type) {
1389 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1390 handle, &foff, &object);
1393 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1394 handle, &foff, &object);
1397 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1398 handle, foff, &object);
1401 if (handle == NULL) {
1412 if (flags & MAP_ANON) {
1416 * Unnamed anonymous regions always start at 0.
1421 docow = MAP_PREFAULT_PARTIAL;
1424 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1425 docow |= MAP_COPY_ON_WRITE;
1426 if (flags & MAP_NOSYNC)
1427 docow |= MAP_DISABLE_SYNCER;
1428 if (flags & MAP_NOCORE)
1429 docow |= MAP_DISABLE_COREDUMP;
1431 if (flags & MAP_STACK)
1432 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1433 docow | MAP_STACK_GROWS_DOWN);
1435 rv = vm_map_find(map, object, foff, addr, size,
1436 object != NULL && object->type == OBJT_DEVICE ?
1437 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1439 rv = vm_map_fixed(map, object, foff, *addr, size,
1440 prot, maxprot, docow);
1442 if (rv != KERN_SUCCESS) {
1444 * Lose the object reference. Will destroy the
1445 * object if it's an unnamed anonymous mapping
1446 * or named anonymous without other references.
1448 vm_object_deallocate(object);
1449 } else if (flags & MAP_SHARED) {
1451 * Shared memory is also shared with children.
1453 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1454 if (rv != KERN_SUCCESS)
1455 (void) vm_map_remove(map, *addr, *addr + size);
1459 * If the process has requested that all future mappings
1460 * be wired, then heed this.
1462 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1463 vm_map_wire(map, *addr, *addr + size,
1464 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1469 case KERN_INVALID_ADDRESS:
1472 case KERN_PROTECTION_FAILURE: