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>
58 #include <sys/resource.h>
59 #include <sys/resourcevar.h>
60 #include <sys/vnode.h>
61 #include <sys/fcntl.h>
65 #include <sys/mount.h>
68 #include <sys/vmmeter.h>
69 #include <sys/sysctl.h>
72 #include <vm/vm_param.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_pageout.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_kern.h>
84 #include <sys/pmckern.h>
87 #ifndef _SYS_SYSPROTO_H_
93 static int max_proc_mmap;
94 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
97 * Set the maximum number of vm_map_entry structures per process. Roughly
98 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
99 * of our KVM malloc space still results in generous limits. We want a
100 * default that is good enough to prevent the kernel running out of resources
101 * if attacked from compromised user account but generous enough such that
102 * multi-threaded processes are not unduly inconvenienced.
104 static void vmmapentry_rsrc_init(void *);
105 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
108 vmmapentry_rsrc_init(dummy)
111 max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
112 max_proc_mmap /= 100;
115 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
116 int *, struct vnode *, vm_ooffset_t, vm_object_t *);
117 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
118 int *, struct cdev *, vm_ooffset_t, vm_object_t *);
127 struct sbrk_args *uap;
129 /* Not yet implemented */
133 #ifndef _SYS_SYSPROTO_H_
146 struct sstk_args *uap;
148 /* Not yet implemented */
152 #if defined(COMPAT_43)
153 #ifndef _SYS_SYSPROTO_H_
154 struct getpagesize_args {
161 ogetpagesize(td, uap)
163 struct getpagesize_args *uap;
166 td->td_retval[0] = PAGE_SIZE;
169 #endif /* COMPAT_43 */
173 * Memory Map (mmap) system call. Note that the file offset
174 * and address are allowed to be NOT page aligned, though if
175 * the MAP_FIXED flag it set, both must have the same remainder
176 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
177 * page-aligned, the actual mapping starts at trunc_page(addr)
178 * and the return value is adjusted up by the page offset.
180 * Generally speaking, only character devices which are themselves
181 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
182 * there would be no cache coherency between a descriptor and a VM mapping
183 * both to the same character device.
185 * Block devices can be mmap'd no matter what they represent. Cache coherency
186 * is maintained as long as you do not write directly to the underlying
189 #ifndef _SYS_SYSPROTO_H_
207 struct mmap_args *uap;
210 struct pmckern_map_in pkm;
215 vm_size_t size, pageoff;
216 vm_prot_t prot, maxprot;
218 objtype_t handle_type;
221 struct vmspace *vms = td->td_proc->p_vmspace;
223 addr = (vm_offset_t) uap->addr;
225 prot = uap->prot & VM_PROT_ALL;
230 /* make sure mapping fits into numeric range etc */
231 if ((ssize_t) uap->len < 0 ||
232 ((flags & MAP_ANON) && uap->fd != -1))
235 if (flags & MAP_STACK) {
236 if ((uap->fd != -1) ||
237 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
244 * Align the file position to a page boundary,
245 * and save its page offset component.
247 pageoff = (pos & PAGE_MASK);
250 /* Adjust size for rounding (on both ends). */
251 size += pageoff; /* low end... */
252 size = (vm_size_t) round_page(size); /* hi end */
255 * Check for illegal addresses. Watch out for address wrap... Note
256 * that VM_*_ADDRESS are not constants due to casts (argh).
258 if (flags & MAP_FIXED) {
260 * The specified address must have the same remainder
261 * as the file offset taken modulo PAGE_SIZE, so it
262 * should be aligned after adjustment by pageoff.
265 if (addr & PAGE_MASK)
267 /* Address range must be all in user VM space. */
268 if (addr < vm_map_min(&vms->vm_map) ||
269 addr + size > vm_map_max(&vms->vm_map))
271 if (addr + size < addr)
275 * XXX for non-fixed mappings where no hint is provided or
276 * the hint would fall in the potential heap space,
277 * place it after the end of the largest possible heap.
279 * There should really be a pmap call to determine a reasonable
282 PROC_LOCK(td->td_proc);
284 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
285 addr < round_page((vm_offset_t)vms->vm_daddr +
286 lim_max(td->td_proc, RLIMIT_DATA))))
287 addr = round_page((vm_offset_t)vms->vm_daddr +
288 lim_max(td->td_proc, RLIMIT_DATA));
289 PROC_UNLOCK(td->td_proc);
291 if (flags & MAP_ANON) {
293 * Mapping blank space is trivial.
296 handle_type = OBJT_DEFAULT;
297 maxprot = VM_PROT_ALL;
301 * Mapping file, get fp for validation. Obtain vnode and make
302 * sure it is of appropriate type.
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_VNODE) {
312 * POSIX shared-memory objects are defined to have
313 * kernel persistence, and are not defined to support
314 * read(2)/write(2) -- or even open(2). Thus, we can
315 * use MAP_ASYNC to trade on-disk coherence for speed.
316 * The shm_open(3) library routine turns on the FPOSIXSHM
317 * flag to request this behavior.
319 if (fp->f_flag & FPOSIXSHM)
323 * Ensure that file and memory protections are
324 * compatible. Note that we only worry about
325 * writability if mapping is shared; in this case,
326 * current and max prot are dictated by the open file.
327 * XXX use the vnode instead? Problem is: what
328 * credentials do we use for determination? What if
329 * proc does a setuid?
331 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
332 maxprot = VM_PROT_NONE;
334 maxprot = VM_PROT_EXECUTE;
335 if (fp->f_flag & FREAD) {
336 maxprot |= VM_PROT_READ;
337 } else if (prot & PROT_READ) {
342 * If we are sharing potential changes (either via
343 * MAP_SHARED or via the implicit sharing of character
344 * device mappings), and we are trying to get write
345 * permission although we opened it without asking
348 if ((flags & MAP_SHARED) != 0) {
349 if ((fp->f_flag & FWRITE) != 0) {
350 maxprot |= VM_PROT_WRITE;
351 } else if ((prot & PROT_WRITE) != 0) {
355 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
356 maxprot |= VM_PROT_WRITE;
359 handle_type = OBJT_VNODE;
363 * Do not allow more then a certain number of vm_map_entry structures
364 * per process. Scale with the number of rforks sharing the map
365 * to make the limit reasonable for threads.
368 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
373 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
374 flags, handle_type, handle, pos);
376 /* inform hwpmc(4) if an executable is being mapped */
377 if (error == 0 && handle_type == OBJT_VNODE &&
378 (prot & PROT_EXEC)) {
379 pkm.pm_file = handle;
380 pkm.pm_address = (uintptr_t) addr;
381 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
385 td->td_retval[0] = (register_t) (addr + pageoff);
394 #ifndef _SYS_SYSPROTO_H_
407 struct ommap_args *uap;
409 struct mmap_args nargs;
410 static const char cvtbsdprot[8] = {
414 PROT_EXEC | PROT_WRITE,
416 PROT_EXEC | PROT_READ,
417 PROT_WRITE | PROT_READ,
418 PROT_EXEC | PROT_WRITE | PROT_READ,
421 #define OMAP_ANON 0x0002
422 #define OMAP_COPY 0x0020
423 #define OMAP_SHARED 0x0010
424 #define OMAP_FIXED 0x0100
426 nargs.addr = uap->addr;
427 nargs.len = uap->len;
428 nargs.prot = cvtbsdprot[uap->prot & 0x7];
430 if (uap->flags & OMAP_ANON)
431 nargs.flags |= MAP_ANON;
432 if (uap->flags & OMAP_COPY)
433 nargs.flags |= MAP_COPY;
434 if (uap->flags & OMAP_SHARED)
435 nargs.flags |= MAP_SHARED;
437 nargs.flags |= MAP_PRIVATE;
438 if (uap->flags & OMAP_FIXED)
439 nargs.flags |= MAP_FIXED;
441 nargs.pos = uap->pos;
442 return (mmap(td, &nargs));
444 #endif /* COMPAT_43 */
447 #ifndef _SYS_SYSPROTO_H_
460 struct msync_args *uap;
463 vm_size_t size, pageoff;
468 addr = (vm_offset_t) uap->addr;
472 pageoff = (addr & PAGE_MASK);
475 size = (vm_size_t) round_page(size);
476 if (addr + size < addr)
479 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
482 map = &td->td_proc->p_vmspace->vm_map;
485 * Clean the pages and interpret the return value.
487 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
488 (flags & MS_INVALIDATE) != 0);
492 case KERN_INVALID_ADDRESS:
493 return (EINVAL); /* Sun returns ENOMEM? */
494 case KERN_INVALID_ARGUMENT:
501 #ifndef _SYS_SYSPROTO_H_
513 struct munmap_args *uap;
516 struct pmckern_map_out pkm;
517 vm_map_entry_t entry;
520 vm_size_t size, pageoff;
523 addr = (vm_offset_t) uap->addr;
528 pageoff = (addr & PAGE_MASK);
531 size = (vm_size_t) round_page(size);
532 if (addr + size < addr)
536 * Check for illegal addresses. Watch out for address wrap...
538 map = &td->td_proc->p_vmspace->vm_map;
539 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
543 * Make sure entire range is allocated.
545 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
551 * Inform hwpmc if the address range being unmapped contains
552 * an executable region.
554 if (vm_map_lookup_entry(map, addr, &entry)) {
556 entry != &map->header && entry->start < addr + size;
557 entry = entry->next) {
558 if (vm_map_check_protection(map, entry->start,
559 entry->end, VM_PROT_EXECUTE) == TRUE) {
560 pkm.pm_address = (uintptr_t) addr;
561 pkm.pm_size = (size_t) size;
562 PMC_CALL_HOOK(td, PMC_FN_MUNMAP,
569 /* returns nothing but KERN_SUCCESS anyway */
570 vm_map_delete(map, addr, addr + size);
575 #ifndef _SYS_SYSPROTO_H_
576 struct mprotect_args {
588 struct mprotect_args *uap;
591 vm_size_t size, pageoff;
594 addr = (vm_offset_t) uap->addr;
596 prot = uap->prot & VM_PROT_ALL;
597 #if defined(VM_PROT_READ_IS_EXEC)
598 if (prot & VM_PROT_READ)
599 prot |= VM_PROT_EXECUTE;
602 pageoff = (addr & PAGE_MASK);
605 size = (vm_size_t) round_page(size);
606 if (addr + size < addr)
609 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
610 addr + size, prot, FALSE)) {
613 case KERN_PROTECTION_FAILURE:
619 #ifndef _SYS_SYSPROTO_H_
620 struct minherit_args {
632 struct minherit_args *uap;
635 vm_size_t size, pageoff;
636 vm_inherit_t inherit;
638 addr = (vm_offset_t)uap->addr;
640 inherit = uap->inherit;
642 pageoff = (addr & PAGE_MASK);
645 size = (vm_size_t) round_page(size);
646 if (addr + size < addr)
649 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
650 addr + size, inherit)) {
653 case KERN_PROTECTION_FAILURE:
659 #ifndef _SYS_SYSPROTO_H_
660 struct madvise_args {
674 struct madvise_args *uap;
676 vm_offset_t start, end;
682 * Check for our special case, advising the swap pager we are
685 if (uap->behav == MADV_PROTECT) {
690 p->p_flag |= P_PROTECTED;
696 * Check for illegal behavior
698 if (uap->behav < 0 || uap->behav > MADV_CORE)
701 * Check for illegal addresses. Watch out for address wrap... Note
702 * that VM_*_ADDRESS are not constants due to casts (argh).
704 map = &td->td_proc->p_vmspace->vm_map;
705 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
706 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
708 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
712 * Since this routine is only advisory, we default to conservative
715 start = trunc_page((vm_offset_t) uap->addr);
716 end = round_page((vm_offset_t) uap->addr + uap->len);
718 if (vm_map_madvise(map, start, end, uap->behav))
723 #ifndef _SYS_SYSPROTO_H_
724 struct mincore_args {
738 struct mincore_args *uap;
740 vm_offset_t addr, first_addr;
741 vm_offset_t end, cend;
746 int vecindex, lastvecindex;
747 vm_map_entry_t current;
748 vm_map_entry_t entry;
750 unsigned int timestamp;
753 * Make sure that the addresses presented are valid for user
756 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
757 end = addr + (vm_size_t)round_page(uap->len);
758 map = &td->td_proc->p_vmspace->vm_map;
759 if (end > vm_map_max(map) || end < addr)
763 * Address of byte vector
767 pmap = vmspace_pmap(td->td_proc->p_vmspace);
769 vm_map_lock_read(map);
771 timestamp = map->timestamp;
773 if (!vm_map_lookup_entry(map, addr, &entry)) {
774 vm_map_unlock_read(map);
779 * Do this on a map entry basis so that if the pages are not
780 * in the current processes address space, we can easily look
781 * up the pages elsewhere.
784 for (current = entry;
785 (current != &map->header) && (current->start < end);
786 current = current->next) {
789 * check for contiguity
791 if (current->end < end &&
792 (entry->next == &map->header ||
793 current->next->start > current->end)) {
794 vm_map_unlock_read(map);
799 * ignore submaps (for now) or null objects
801 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
802 current->object.vm_object == NULL)
806 * limit this scan to the current map entry and the
807 * limits for the mincore call
809 if (addr < current->start)
810 addr = current->start;
816 * scan this entry one page at a time
818 while (addr < cend) {
820 * Check pmap first, it is likely faster, also
821 * it can provide info as to whether we are the
822 * one referencing or modifying the page.
824 mincoreinfo = pmap_mincore(pmap, addr);
830 * calculate the page index into the object
832 offset = current->offset + (addr - current->start);
833 pindex = OFF_TO_IDX(offset);
834 VM_OBJECT_LOCK(current->object.vm_object);
835 m = vm_page_lookup(current->object.vm_object,
838 * if the page is resident, then gather information about
841 if (m != NULL && m->valid != 0) {
842 mincoreinfo = MINCORE_INCORE;
843 vm_page_lock_queues();
846 mincoreinfo |= MINCORE_MODIFIED_OTHER;
847 if ((m->flags & PG_REFERENCED) ||
848 pmap_ts_referenced(m)) {
849 vm_page_flag_set(m, PG_REFERENCED);
850 mincoreinfo |= MINCORE_REFERENCED_OTHER;
852 vm_page_unlock_queues();
854 VM_OBJECT_UNLOCK(current->object.vm_object);
858 * subyte may page fault. In case it needs to modify
859 * the map, we release the lock.
861 vm_map_unlock_read(map);
864 * calculate index into user supplied byte vector
866 vecindex = OFF_TO_IDX(addr - first_addr);
869 * If we have skipped map entries, we need to make sure that
870 * the byte vector is zeroed for those skipped entries.
872 while ((lastvecindex + 1) < vecindex) {
873 error = subyte(vec + lastvecindex, 0);
882 * Pass the page information to the user
884 error = subyte(vec + vecindex, mincoreinfo);
891 * If the map has changed, due to the subyte, the previous
892 * output may be invalid.
894 vm_map_lock_read(map);
895 if (timestamp != map->timestamp)
898 lastvecindex = vecindex;
904 * subyte may page fault. In case it needs to modify
905 * the map, we release the lock.
907 vm_map_unlock_read(map);
910 * Zero the last entries in the byte vector.
912 vecindex = OFF_TO_IDX(end - first_addr);
913 while ((lastvecindex + 1) < vecindex) {
914 error = subyte(vec + lastvecindex, 0);
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)
929 vm_map_unlock_read(map);
934 #ifndef _SYS_SYSPROTO_H_
946 struct mlock_args *uap;
949 vm_offset_t addr, end, last, start;
950 vm_size_t npages, size;
956 addr = (vm_offset_t)uap->addr;
959 start = trunc_page(addr);
960 end = round_page(last);
961 if (last < addr || end < addr)
963 npages = atop(end - start);
964 if (npages > vm_page_max_wired)
969 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
970 lim_cur(proc, RLIMIT_MEMLOCK)) {
975 if (npages + cnt.v_wire_count > vm_page_max_wired)
977 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
978 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
979 return (error == KERN_SUCCESS ? 0 : ENOMEM);
982 #ifndef _SYS_SYSPROTO_H_
983 struct mlockall_args {
994 struct mlockall_args *uap;
999 map = &td->td_proc->p_vmspace->vm_map;
1002 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1007 * If wiring all pages in the process would cause it to exceed
1008 * a hard resource limit, return ENOMEM.
1010 PROC_LOCK(td->td_proc);
1011 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1012 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1013 PROC_UNLOCK(td->td_proc);
1016 PROC_UNLOCK(td->td_proc);
1023 if (uap->how & MCL_FUTURE) {
1025 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1030 if (uap->how & MCL_CURRENT) {
1032 * P1003.1-2001 mandates that all currently mapped pages
1033 * will be memory resident and locked (wired) upon return
1034 * from mlockall(). vm_map_wire() will wire pages, by
1035 * calling vm_fault_wire() for each page in the region.
1037 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1038 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1039 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1045 #ifndef _SYS_SYSPROTO_H_
1046 struct munlockall_args {
1057 struct munlockall_args *uap;
1062 map = &td->td_proc->p_vmspace->vm_map;
1067 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1069 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1072 /* Forcibly unwire all pages. */
1073 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1074 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1079 #ifndef _SYS_SYSPROTO_H_
1080 struct munlock_args {
1091 struct munlock_args *uap;
1093 vm_offset_t addr, end, last, start;
1100 addr = (vm_offset_t)uap->addr;
1103 start = trunc_page(addr);
1104 end = round_page(last);
1105 if (last < addr || end < addr)
1107 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1108 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1109 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1117 * Helper function for vm_mmap. Perform sanity check specific for mmap
1118 * operations on vnodes.
1121 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1122 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1123 struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1129 int error, flags, type;
1133 vfslocked = VFS_LOCK_GIANT(mp);
1134 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1135 VFS_UNLOCK_GIANT(vfslocked);
1140 if (vp->v_type == VREG) {
1142 * Get the proper underlying object
1148 if (obj->handle != vp) {
1150 vp = (struct vnode*)obj->handle;
1151 vget(vp, LK_EXCLUSIVE, td);
1155 } else if (vp->v_type == VCHR) {
1157 handle = vp->v_rdev;
1159 /* XXX: lack thredref on device */
1160 if(vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON) {
1161 *maxprotp = VM_PROT_ALL;
1162 *flagsp |= MAP_ANON;
1167 * cdevs does not provide private mappings of any kind.
1169 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1170 (prot & PROT_WRITE) != 0) {
1174 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1179 * Force device mappings to be shared.
1181 flags |= MAP_SHARED;
1186 if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1190 error = mac_check_vnode_mmap(td->td_ucred, 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 if (vp->v_type == VREG) {
1209 objsize = round_page(va.va_size);
1210 if (va.va_nlink == 0)
1211 flags |= MAP_NOSYNC;
1213 obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1215 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1220 vfs_mark_atime(vp, td);
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)
1246 /* XXX: lack thredref on device */
1247 if (cdev->si_devsw->d_flags & D_MMAP_ANON) {
1248 *maxprotp = VM_PROT_ALL;
1249 *flagsp |= MAP_ANON;
1253 * cdevs does not provide private mappings of any kind.
1255 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1256 (prot & PROT_WRITE) != 0)
1258 if (flags & (MAP_PRIVATE|MAP_COPY))
1261 * Force device mappings to be shared.
1263 flags |= MAP_SHARED;
1265 error = mac_check_cdev_mmap(td->td_ucred, cdev, prot);
1269 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, foff);
1282 * Internal version of mmap. Currently used by mmap, exec, and sys5
1283 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1286 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1287 vm_prot_t maxprot, int flags,
1288 objtype_t handle_type, void *handle,
1293 int rv = KERN_SUCCESS;
1295 struct thread *td = curthread;
1300 size = round_page(size);
1302 PROC_LOCK(td->td_proc);
1303 if (td->td_proc->p_vmspace->vm_map.size + size >
1304 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1305 PROC_UNLOCK(td->td_proc);
1308 PROC_UNLOCK(td->td_proc);
1311 * We currently can only deal with page aligned file offsets.
1312 * The check is here rather than in the syscall because the
1313 * kernel calls this function internally for other mmaping
1314 * operations (such as in exec) and non-aligned offsets will
1315 * cause pmap inconsistencies...so we want to be sure to
1316 * disallow this in all cases.
1318 if (foff & PAGE_MASK)
1321 if ((flags & MAP_FIXED) == 0) {
1323 *addr = round_page(*addr);
1325 if (*addr != trunc_page(*addr))
1328 (void) vm_map_remove(map, *addr, *addr + size);
1331 * Lookup/allocate object.
1333 switch (handle_type) {
1335 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1336 handle, foff, &object);
1339 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1340 handle, foff, &object);
1343 if (handle == NULL) {
1353 if (flags & MAP_ANON) {
1357 * Unnamed anonymous regions always start at 0.
1362 docow = MAP_PREFAULT_PARTIAL;
1365 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1366 docow |= MAP_COPY_ON_WRITE;
1367 if (flags & MAP_NOSYNC)
1368 docow |= MAP_DISABLE_SYNCER;
1369 if (flags & MAP_NOCORE)
1370 docow |= MAP_DISABLE_COREDUMP;
1372 #if defined(VM_PROT_READ_IS_EXEC)
1373 if (prot & VM_PROT_READ)
1374 prot |= VM_PROT_EXECUTE;
1376 if (maxprot & VM_PROT_READ)
1377 maxprot |= VM_PROT_EXECUTE;
1381 *addr = pmap_addr_hint(object, *addr, size);
1383 if (flags & MAP_STACK)
1384 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1385 docow | MAP_STACK_GROWS_DOWN);
1387 rv = vm_map_find(map, object, foff, addr, size, fitit,
1388 prot, maxprot, docow);
1390 if (rv != KERN_SUCCESS) {
1392 * Lose the object reference. Will destroy the
1393 * object if it's an unnamed anonymous mapping
1394 * or named anonymous without other references.
1396 vm_object_deallocate(object);
1397 } else if (flags & MAP_SHARED) {
1399 * Shared memory is also shared with children.
1401 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1402 if (rv != KERN_SUCCESS)
1403 (void) vm_map_remove(map, *addr, *addr + size);
1407 * If the process has requested that all future mappings
1408 * be wired, then heed this.
1410 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1411 vm_map_wire(map, *addr, *addr + size,
1412 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1417 case KERN_INVALID_ADDRESS:
1420 case KERN_PROTECTION_FAILURE: