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 */
232 if ((ssize_t) uap->len < 0 ||
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:
640 #ifndef _SYS_SYSPROTO_H_
641 struct minherit_args {
653 struct minherit_args *uap;
656 vm_size_t size, pageoff;
657 vm_inherit_t inherit;
659 addr = (vm_offset_t)uap->addr;
661 inherit = uap->inherit;
663 pageoff = (addr & PAGE_MASK);
666 size = (vm_size_t) round_page(size);
667 if (addr + size < addr)
670 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
671 addr + size, inherit)) {
674 case KERN_PROTECTION_FAILURE:
680 #ifndef _SYS_SYSPROTO_H_
681 struct madvise_args {
695 struct madvise_args *uap;
697 vm_offset_t start, end;
703 * Check for our special case, advising the swap pager we are
706 if (uap->behav == MADV_PROTECT) {
707 error = priv_check(td, PRIV_VM_MADV_PROTECT);
711 p->p_flag |= P_PROTECTED;
717 * Check for illegal behavior
719 if (uap->behav < 0 || uap->behav > MADV_CORE)
722 * Check for illegal addresses. Watch out for address wrap... Note
723 * that VM_*_ADDRESS are not constants due to casts (argh).
725 map = &td->td_proc->p_vmspace->vm_map;
726 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
727 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
729 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
733 * Since this routine is only advisory, we default to conservative
736 start = trunc_page((vm_offset_t) uap->addr);
737 end = round_page((vm_offset_t) uap->addr + uap->len);
739 if (vm_map_madvise(map, start, end, uap->behav))
744 #ifndef _SYS_SYSPROTO_H_
745 struct mincore_args {
759 struct mincore_args *uap;
761 vm_offset_t addr, first_addr;
762 vm_offset_t end, cend;
767 int vecindex, lastvecindex;
768 vm_map_entry_t current;
769 vm_map_entry_t entry;
771 unsigned int timestamp;
774 * Make sure that the addresses presented are valid for user
777 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
778 end = addr + (vm_size_t)round_page(uap->len);
779 map = &td->td_proc->p_vmspace->vm_map;
780 if (end > vm_map_max(map) || end < addr)
784 * Address of byte vector
788 pmap = vmspace_pmap(td->td_proc->p_vmspace);
790 vm_map_lock_read(map);
792 timestamp = map->timestamp;
794 if (!vm_map_lookup_entry(map, addr, &entry)) {
795 vm_map_unlock_read(map);
800 * Do this on a map entry basis so that if the pages are not
801 * in the current processes address space, we can easily look
802 * up the pages elsewhere.
805 for (current = entry;
806 (current != &map->header) && (current->start < end);
807 current = current->next) {
810 * check for contiguity
812 if (current->end < end &&
813 (entry->next == &map->header ||
814 current->next->start > current->end)) {
815 vm_map_unlock_read(map);
820 * ignore submaps (for now) or null objects
822 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
823 current->object.vm_object == NULL)
827 * limit this scan to the current map entry and the
828 * limits for the mincore call
830 if (addr < current->start)
831 addr = current->start;
837 * scan this entry one page at a time
839 while (addr < cend) {
841 * Check pmap first, it is likely faster, also
842 * it can provide info as to whether we are the
843 * one referencing or modifying the page.
845 mincoreinfo = pmap_mincore(pmap, addr);
851 * calculate the page index into the object
853 offset = current->offset + (addr - current->start);
854 pindex = OFF_TO_IDX(offset);
855 VM_OBJECT_LOCK(current->object.vm_object);
856 m = vm_page_lookup(current->object.vm_object,
859 * if the page is resident, then gather information about
862 if (m != NULL && m->valid != 0) {
863 mincoreinfo = MINCORE_INCORE;
864 vm_page_lock_queues();
867 mincoreinfo |= MINCORE_MODIFIED_OTHER;
868 if ((m->flags & PG_REFERENCED) ||
869 pmap_ts_referenced(m)) {
870 vm_page_flag_set(m, PG_REFERENCED);
871 mincoreinfo |= MINCORE_REFERENCED_OTHER;
873 vm_page_unlock_queues();
875 VM_OBJECT_UNLOCK(current->object.vm_object);
879 * subyte may page fault. In case it needs to modify
880 * the map, we release the lock.
882 vm_map_unlock_read(map);
885 * calculate index into user supplied byte vector
887 vecindex = OFF_TO_IDX(addr - first_addr);
890 * If we have skipped map entries, we need to make sure that
891 * the byte vector is zeroed for those skipped entries.
893 while ((lastvecindex + 1) < vecindex) {
894 error = subyte(vec + lastvecindex, 0);
903 * Pass the page information to the user
905 error = subyte(vec + vecindex, mincoreinfo);
912 * If the map has changed, due to the subyte, the previous
913 * output may be invalid.
915 vm_map_lock_read(map);
916 if (timestamp != map->timestamp)
919 lastvecindex = vecindex;
925 * subyte may page fault. In case it needs to modify
926 * the map, we release the lock.
928 vm_map_unlock_read(map);
931 * Zero the last entries in the byte vector.
933 vecindex = OFF_TO_IDX(end - first_addr);
934 while ((lastvecindex + 1) < vecindex) {
935 error = subyte(vec + lastvecindex, 0);
944 * If the map has changed, due to the subyte, the previous
945 * output may be invalid.
947 vm_map_lock_read(map);
948 if (timestamp != map->timestamp)
950 vm_map_unlock_read(map);
955 #ifndef _SYS_SYSPROTO_H_
967 struct mlock_args *uap;
970 vm_offset_t addr, end, last, start;
971 vm_size_t npages, size;
974 error = priv_check(td, PRIV_VM_MLOCK);
977 addr = (vm_offset_t)uap->addr;
980 start = trunc_page(addr);
981 end = round_page(last);
982 if (last < addr || end < addr)
984 npages = atop(end - start);
985 if (npages > vm_page_max_wired)
990 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
991 lim_cur(proc, RLIMIT_MEMLOCK)) {
996 if (npages + cnt.v_wire_count > vm_page_max_wired)
998 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
999 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1000 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1003 #ifndef _SYS_SYSPROTO_H_
1004 struct mlockall_args {
1015 struct mlockall_args *uap;
1020 map = &td->td_proc->p_vmspace->vm_map;
1023 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1028 * If wiring all pages in the process would cause it to exceed
1029 * a hard resource limit, return ENOMEM.
1031 PROC_LOCK(td->td_proc);
1032 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1033 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1034 PROC_UNLOCK(td->td_proc);
1037 PROC_UNLOCK(td->td_proc);
1039 error = priv_check(td, PRIV_VM_MLOCK);
1044 if (uap->how & MCL_FUTURE) {
1046 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1051 if (uap->how & MCL_CURRENT) {
1053 * P1003.1-2001 mandates that all currently mapped pages
1054 * will be memory resident and locked (wired) upon return
1055 * from mlockall(). vm_map_wire() will wire pages, by
1056 * calling vm_fault_wire() for each page in the region.
1058 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1059 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1060 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1066 #ifndef _SYS_SYSPROTO_H_
1067 struct munlockall_args {
1078 struct munlockall_args *uap;
1083 map = &td->td_proc->p_vmspace->vm_map;
1084 error = priv_check(td, PRIV_VM_MUNLOCK);
1088 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1090 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1093 /* Forcibly unwire all pages. */
1094 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1095 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1100 #ifndef _SYS_SYSPROTO_H_
1101 struct munlock_args {
1112 struct munlock_args *uap;
1114 vm_offset_t addr, end, last, start;
1118 error = priv_check(td, PRIV_VM_MUNLOCK);
1121 addr = (vm_offset_t)uap->addr;
1124 start = trunc_page(addr);
1125 end = round_page(last);
1126 if (last < addr || end < addr)
1128 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1129 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1130 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1138 * Helper function for vm_mmap. Perform sanity check specific for mmap
1139 * operations on vnodes.
1142 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1143 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1144 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp)
1155 cred = td->td_ucred;
1156 vfslocked = VFS_LOCK_GIANT(mp);
1157 if ((error = vget(vp, LK_SHARED, td)) != 0) {
1158 VFS_UNLOCK_GIANT(vfslocked);
1164 if (vp->v_type == VREG) {
1166 * Get the proper underlying object
1172 if (obj->handle != vp) {
1174 vp = (struct vnode*)obj->handle;
1175 vget(vp, LK_SHARED, td);
1177 } else if (vp->v_type == VCHR) {
1178 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1179 vp->v_rdev, foffp, objp);
1187 if ((error = VOP_GETATTR(vp, &va, cred)))
1190 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1194 if ((flags & MAP_SHARED) != 0) {
1195 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1196 if (prot & PROT_WRITE) {
1200 *maxprotp &= ~VM_PROT_WRITE;
1204 * If it is a regular file without any references
1205 * we do not need to sync it.
1206 * Adjust object size to be the size of actual file.
1208 objsize = round_page(va.va_size);
1209 if (va.va_nlink == 0)
1210 flags |= MAP_NOSYNC;
1211 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff);
1220 vfs_mark_atime(vp, cred);
1224 VFS_UNLOCK_GIANT(vfslocked);
1233 * Helper function for vm_mmap. Perform sanity check specific for mmap
1234 * operations on cdevs.
1237 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1238 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1239 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1247 dsw = dev_refthread(cdev);
1250 if (dsw->d_flags & D_MMAP_ANON) {
1251 dev_relthread(cdev);
1252 *maxprotp = VM_PROT_ALL;
1253 *flagsp |= MAP_ANON;
1257 * cdevs do not provide private mappings of any kind.
1259 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1260 (prot & PROT_WRITE) != 0) {
1261 dev_relthread(cdev);
1264 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1265 dev_relthread(cdev);
1269 * Force device mappings to be shared.
1271 flags |= MAP_SHARED;
1273 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1275 dev_relthread(cdev);
1280 * First, try d_mmap_single(). If that is not implemented
1281 * (returns ENODEV), fall back to using the device pager.
1282 * Note that d_mmap_single() must return a reference to the
1283 * object (it needs to bump the reference count of the object
1284 * it returns somehow).
1286 * XXX assumes VM_PROT_* == PROT_*
1288 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1289 dev_relthread(cdev);
1290 if (error != ENODEV)
1292 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff);
1305 * Helper function for vm_mmap. Perform sanity check specific for mmap
1306 * operations on shm file descriptors.
1309 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1310 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1311 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1315 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1316 (prot & PROT_WRITE) != 0)
1319 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1323 error = shm_mmap(shmfd, objsize, foff, objp);
1334 * Internal version of mmap. Currently used by mmap, exec, and sys5
1335 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1338 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1339 vm_prot_t maxprot, int flags,
1340 objtype_t handle_type, void *handle,
1344 vm_object_t object = NULL;
1345 int rv = KERN_SUCCESS;
1347 struct thread *td = curthread;
1352 size = round_page(size);
1354 PROC_LOCK(td->td_proc);
1355 if (td->td_proc->p_vmspace->vm_map.size + size >
1356 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1357 PROC_UNLOCK(td->td_proc);
1360 PROC_UNLOCK(td->td_proc);
1363 * We currently can only deal with page aligned file offsets.
1364 * The check is here rather than in the syscall because the
1365 * kernel calls this function internally for other mmaping
1366 * operations (such as in exec) and non-aligned offsets will
1367 * cause pmap inconsistencies...so we want to be sure to
1368 * disallow this in all cases.
1370 if (foff & PAGE_MASK)
1373 if ((flags & MAP_FIXED) == 0) {
1375 *addr = round_page(*addr);
1377 if (*addr != trunc_page(*addr))
1382 * Lookup/allocate object.
1384 switch (handle_type) {
1386 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1387 handle, &foff, &object);
1390 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1391 handle, &foff, &object);
1394 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1395 handle, foff, &object);
1398 if (handle == NULL) {
1409 if (flags & MAP_ANON) {
1413 * Unnamed anonymous regions always start at 0.
1418 docow = MAP_PREFAULT_PARTIAL;
1421 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1422 docow |= MAP_COPY_ON_WRITE;
1423 if (flags & MAP_NOSYNC)
1424 docow |= MAP_DISABLE_SYNCER;
1425 if (flags & MAP_NOCORE)
1426 docow |= MAP_DISABLE_COREDUMP;
1428 if (flags & MAP_STACK)
1429 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1430 docow | MAP_STACK_GROWS_DOWN);
1432 rv = vm_map_find(map, object, foff, addr, size,
1433 object != NULL && object->type == OBJT_DEVICE ?
1434 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1436 rv = vm_map_fixed(map, object, foff, *addr, size,
1437 prot, maxprot, docow);
1439 if (rv != KERN_SUCCESS) {
1441 * Lose the object reference. Will destroy the
1442 * object if it's an unnamed anonymous mapping
1443 * or named anonymous without other references.
1445 vm_object_deallocate(object);
1446 } else if (flags & MAP_SHARED) {
1448 * Shared memory is also shared with children.
1450 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1451 if (rv != KERN_SUCCESS)
1452 (void) vm_map_remove(map, *addr, *addr + size);
1456 * If the process has requested that all future mappings
1457 * be wired, then heed this.
1459 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1460 vm_map_wire(map, *addr, *addr + size,
1461 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1466 case KERN_INVALID_ADDRESS:
1469 case KERN_PROTECTION_FAILURE: