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>
52 #include <sys/mutex.h>
53 #include <sys/sysproto.h>
54 #include <sys/filedesc.h>
57 #include <sys/resource.h>
58 #include <sys/resourcevar.h>
59 #include <sys/vnode.h>
60 #include <sys/fcntl.h>
63 #include <sys/mount.h>
66 #include <sys/sysent.h>
67 #include <sys/vmmeter.h>
69 #include <security/mac/mac_framework.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>
83 #include <sys/pmckern.h>
86 #ifndef _SYS_SYSPROTO_H_
92 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
93 int *, struct vnode *, vm_ooffset_t *, vm_object_t *);
94 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
95 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
96 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
97 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
106 struct sbrk_args *uap;
108 /* Not yet implemented */
112 #ifndef _SYS_SYSPROTO_H_
125 struct sstk_args *uap;
127 /* Not yet implemented */
131 #if defined(COMPAT_43)
132 #ifndef _SYS_SYSPROTO_H_
133 struct getpagesize_args {
140 ogetpagesize(td, uap)
142 struct getpagesize_args *uap;
145 td->td_retval[0] = PAGE_SIZE;
148 #endif /* COMPAT_43 */
152 * Memory Map (mmap) system call. Note that the file offset
153 * and address are allowed to be NOT page aligned, though if
154 * the MAP_FIXED flag it set, both must have the same remainder
155 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
156 * page-aligned, the actual mapping starts at trunc_page(addr)
157 * and the return value is adjusted up by the page offset.
159 * Generally speaking, only character devices which are themselves
160 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
161 * there would be no cache coherency between a descriptor and a VM mapping
162 * both to the same character device.
164 #ifndef _SYS_SYSPROTO_H_
182 struct mmap_args *uap;
185 struct pmckern_map_in pkm;
190 vm_size_t size, pageoff;
191 vm_prot_t prot, maxprot;
193 objtype_t handle_type;
196 struct vmspace *vms = td->td_proc->p_vmspace;
198 addr = (vm_offset_t) uap->addr;
200 prot = uap->prot & VM_PROT_ALL;
205 /* make sure mapping fits into numeric range etc */
206 if ((uap->len == 0 && !SV_CURPROC_FLAG(SV_AOUT) &&
207 curproc->p_osrel >= P_OSREL_MAP_ANON) ||
208 ((flags & MAP_ANON) && (uap->fd != -1 || pos != 0)))
211 if (flags & MAP_STACK) {
212 if ((uap->fd != -1) ||
213 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
220 * Align the file position to a page boundary,
221 * and save its page offset component.
223 pageoff = (pos & PAGE_MASK);
226 /* Adjust size for rounding (on both ends). */
227 size += pageoff; /* low end... */
228 size = (vm_size_t) round_page(size); /* hi end */
231 * Check for illegal addresses. Watch out for address wrap... Note
232 * that VM_*_ADDRESS are not constants due to casts (argh).
234 if (flags & MAP_FIXED) {
236 * The specified address must have the same remainder
237 * as the file offset taken modulo PAGE_SIZE, so it
238 * should be aligned after adjustment by pageoff.
241 if (addr & PAGE_MASK)
243 /* Address range must be all in user VM space. */
244 if (addr < vm_map_min(&vms->vm_map) ||
245 addr + size > vm_map_max(&vms->vm_map))
247 if (addr + size < addr)
251 * XXX for non-fixed mappings where no hint is provided or
252 * the hint would fall in the potential heap space,
253 * place it after the end of the largest possible heap.
255 * There should really be a pmap call to determine a reasonable
258 PROC_LOCK(td->td_proc);
260 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
261 addr < round_page((vm_offset_t)vms->vm_daddr +
262 lim_max(td->td_proc, RLIMIT_DATA))))
263 addr = round_page((vm_offset_t)vms->vm_daddr +
264 lim_max(td->td_proc, RLIMIT_DATA));
265 PROC_UNLOCK(td->td_proc);
267 if (flags & MAP_ANON) {
269 * Mapping blank space is trivial.
272 handle_type = OBJT_DEFAULT;
273 maxprot = VM_PROT_ALL;
276 * Mapping file, get fp for validation and
277 * don't let the descriptor disappear on us if we block.
279 if ((error = fget(td, uap->fd, &fp)) != 0)
281 if (fp->f_type == DTYPE_SHM) {
283 handle_type = OBJT_SWAP;
284 maxprot = VM_PROT_NONE;
286 /* FREAD should always be set. */
287 if (fp->f_flag & FREAD)
288 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
289 if (fp->f_flag & FWRITE)
290 maxprot |= VM_PROT_WRITE;
293 if (fp->f_type != DTYPE_VNODE) {
297 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
298 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
300 * POSIX shared-memory objects are defined to have
301 * kernel persistence, and are not defined to support
302 * read(2)/write(2) -- or even open(2). Thus, we can
303 * use MAP_ASYNC to trade on-disk coherence for speed.
304 * The shm_open(3) library routine turns on the FPOSIXSHM
305 * flag to request this behavior.
307 if (fp->f_flag & FPOSIXSHM)
312 * Ensure that file and memory protections are
313 * compatible. Note that we only worry about
314 * writability if mapping is shared; in this case,
315 * current and max prot are dictated by the open file.
316 * XXX use the vnode instead? Problem is: what
317 * credentials do we use for determination? What if
318 * proc does a setuid?
320 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
321 maxprot = VM_PROT_NONE;
323 maxprot = VM_PROT_EXECUTE;
324 if (fp->f_flag & FREAD) {
325 maxprot |= VM_PROT_READ;
326 } else if (prot & PROT_READ) {
331 * If we are sharing potential changes (either via
332 * MAP_SHARED or via the implicit sharing of character
333 * device mappings), and we are trying to get write
334 * permission although we opened it without asking
337 if ((flags & MAP_SHARED) != 0) {
338 if ((fp->f_flag & FWRITE) != 0) {
339 maxprot |= VM_PROT_WRITE;
340 } else if ((prot & PROT_WRITE) != 0) {
344 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
345 maxprot |= VM_PROT_WRITE;
348 handle_type = OBJT_VNODE;
352 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
353 flags, handle_type, handle, pos);
356 /* inform hwpmc(4) if an executable is being mapped */
357 if (error == 0 && handle_type == OBJT_VNODE &&
358 (prot & PROT_EXEC)) {
359 pkm.pm_file = handle;
360 pkm.pm_address = (uintptr_t) addr;
361 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
365 td->td_retval[0] = (register_t) (addr + pageoff);
374 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
376 struct mmap_args oargs;
378 oargs.addr = uap->addr;
379 oargs.len = uap->len;
380 oargs.prot = uap->prot;
381 oargs.flags = uap->flags;
383 oargs.pos = uap->pos;
384 return (mmap(td, &oargs));
388 #ifndef _SYS_SYSPROTO_H_
401 struct ommap_args *uap;
403 struct mmap_args nargs;
404 static const char cvtbsdprot[8] = {
408 PROT_EXEC | PROT_WRITE,
410 PROT_EXEC | PROT_READ,
411 PROT_WRITE | PROT_READ,
412 PROT_EXEC | PROT_WRITE | PROT_READ,
415 #define OMAP_ANON 0x0002
416 #define OMAP_COPY 0x0020
417 #define OMAP_SHARED 0x0010
418 #define OMAP_FIXED 0x0100
420 nargs.addr = uap->addr;
421 nargs.len = uap->len;
422 nargs.prot = cvtbsdprot[uap->prot & 0x7];
424 if (uap->flags & OMAP_ANON)
425 nargs.flags |= MAP_ANON;
426 if (uap->flags & OMAP_COPY)
427 nargs.flags |= MAP_COPY;
428 if (uap->flags & OMAP_SHARED)
429 nargs.flags |= MAP_SHARED;
431 nargs.flags |= MAP_PRIVATE;
432 if (uap->flags & OMAP_FIXED)
433 nargs.flags |= MAP_FIXED;
435 nargs.pos = uap->pos;
436 return (mmap(td, &nargs));
438 #endif /* COMPAT_43 */
441 #ifndef _SYS_SYSPROTO_H_
454 struct msync_args *uap;
457 vm_size_t size, pageoff;
462 addr = (vm_offset_t) uap->addr;
466 pageoff = (addr & PAGE_MASK);
469 size = (vm_size_t) round_page(size);
470 if (addr + size < addr)
473 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
476 map = &td->td_proc->p_vmspace->vm_map;
479 * Clean the pages and interpret the return value.
481 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
482 (flags & MS_INVALIDATE) != 0);
486 case KERN_INVALID_ADDRESS:
487 return (EINVAL); /* Sun returns ENOMEM? */
488 case KERN_INVALID_ARGUMENT:
497 #ifndef _SYS_SYSPROTO_H_
509 struct munmap_args *uap;
512 struct pmckern_map_out pkm;
513 vm_map_entry_t entry;
516 vm_size_t size, pageoff;
519 addr = (vm_offset_t) uap->addr;
524 pageoff = (addr & PAGE_MASK);
527 size = (vm_size_t) round_page(size);
528 if (addr + size < addr)
532 * Check for illegal addresses. Watch out for address wrap...
534 map = &td->td_proc->p_vmspace->vm_map;
535 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
540 * Inform hwpmc if the address range being unmapped contains
541 * an executable region.
543 pkm.pm_address = (uintptr_t) NULL;
544 if (vm_map_lookup_entry(map, addr, &entry)) {
546 entry != &map->header && entry->start < addr + size;
547 entry = entry->next) {
548 if (vm_map_check_protection(map, entry->start,
549 entry->end, VM_PROT_EXECUTE) == TRUE) {
550 pkm.pm_address = (uintptr_t) addr;
551 pkm.pm_size = (size_t) size;
557 vm_map_delete(map, addr, addr + size);
560 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
561 vm_map_lock_downgrade(map);
562 if (pkm.pm_address != (uintptr_t) NULL)
563 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
564 vm_map_unlock_read(map);
568 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
572 #ifndef _SYS_SYSPROTO_H_
573 struct mprotect_args {
585 struct mprotect_args *uap;
588 vm_size_t size, pageoff;
591 addr = (vm_offset_t) uap->addr;
593 prot = uap->prot & VM_PROT_ALL;
595 pageoff = (addr & PAGE_MASK);
598 size = (vm_size_t) round_page(size);
599 if (addr + size < addr)
602 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
603 addr + size, prot, FALSE)) {
606 case KERN_PROTECTION_FAILURE:
608 case KERN_RESOURCE_SHORTAGE:
614 #ifndef _SYS_SYSPROTO_H_
615 struct minherit_args {
627 struct minherit_args *uap;
630 vm_size_t size, pageoff;
631 vm_inherit_t inherit;
633 addr = (vm_offset_t)uap->addr;
635 inherit = uap->inherit;
637 pageoff = (addr & PAGE_MASK);
640 size = (vm_size_t) round_page(size);
641 if (addr + size < addr)
644 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
645 addr + size, inherit)) {
648 case KERN_PROTECTION_FAILURE:
654 #ifndef _SYS_SYSPROTO_H_
655 struct madvise_args {
669 struct madvise_args *uap;
671 vm_offset_t start, end;
677 * Check for our special case, advising the swap pager we are
680 if (uap->behav == MADV_PROTECT) {
681 error = priv_check(td, PRIV_VM_MADV_PROTECT);
685 p->p_flag |= P_PROTECTED;
691 * Check for illegal behavior
693 if (uap->behav < 0 || uap->behav > MADV_CORE)
696 * Check for illegal addresses. Watch out for address wrap... Note
697 * that VM_*_ADDRESS are not constants due to casts (argh).
699 map = &td->td_proc->p_vmspace->vm_map;
700 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
701 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
703 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
707 * Since this routine is only advisory, we default to conservative
710 start = trunc_page((vm_offset_t) uap->addr);
711 end = round_page((vm_offset_t) uap->addr + uap->len);
713 if (vm_map_madvise(map, start, end, uap->behav))
718 #ifndef _SYS_SYSPROTO_H_
719 struct mincore_args {
733 struct mincore_args *uap;
735 vm_offset_t addr, first_addr;
736 vm_offset_t end, cend;
741 int vecindex, lastvecindex;
742 vm_map_entry_t current;
743 vm_map_entry_t entry;
745 unsigned int timestamp;
748 * Make sure that the addresses presented are valid for user
751 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
752 end = addr + (vm_size_t)round_page(uap->len);
753 map = &td->td_proc->p_vmspace->vm_map;
754 if (end > vm_map_max(map) || end < addr)
758 * Address of byte vector
762 pmap = vmspace_pmap(td->td_proc->p_vmspace);
764 vm_map_lock_read(map);
766 timestamp = map->timestamp;
768 if (!vm_map_lookup_entry(map, addr, &entry)) {
769 vm_map_unlock_read(map);
774 * Do this on a map entry basis so that if the pages are not
775 * in the current processes address space, we can easily look
776 * up the pages elsewhere.
779 for (current = entry;
780 (current != &map->header) && (current->start < end);
781 current = current->next) {
784 * check for contiguity
786 if (current->end < end &&
787 (entry->next == &map->header ||
788 current->next->start > current->end)) {
789 vm_map_unlock_read(map);
794 * ignore submaps (for now) or null objects
796 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
797 current->object.vm_object == NULL)
801 * limit this scan to the current map entry and the
802 * limits for the mincore call
804 if (addr < current->start)
805 addr = current->start;
811 * scan this entry one page at a time
813 while (addr < cend) {
815 * Check pmap first, it is likely faster, also
816 * it can provide info as to whether we are the
817 * one referencing or modifying the page.
819 mincoreinfo = pmap_mincore(pmap, addr);
825 * calculate the page index into the object
827 offset = current->offset + (addr - current->start);
828 pindex = OFF_TO_IDX(offset);
829 VM_OBJECT_LOCK(current->object.vm_object);
830 m = vm_page_lookup(current->object.vm_object,
833 * if the page is resident, then gather information about
836 if (m != NULL && m->valid != 0) {
837 mincoreinfo = MINCORE_INCORE;
838 vm_page_lock_queues();
841 mincoreinfo |= MINCORE_MODIFIED_OTHER;
842 if ((m->flags & PG_REFERENCED) ||
843 pmap_ts_referenced(m)) {
844 vm_page_flag_set(m, PG_REFERENCED);
845 mincoreinfo |= MINCORE_REFERENCED_OTHER;
847 vm_page_unlock_queues();
849 VM_OBJECT_UNLOCK(current->object.vm_object);
853 * subyte may page fault. In case it needs to modify
854 * the map, we release the lock.
856 vm_map_unlock_read(map);
859 * calculate index into user supplied byte vector
861 vecindex = OFF_TO_IDX(addr - first_addr);
864 * If we have skipped map entries, we need to make sure that
865 * the byte vector is zeroed for those skipped entries.
867 while ((lastvecindex + 1) < vecindex) {
868 error = subyte(vec + lastvecindex, 0);
877 * Pass the page information to the user
879 error = subyte(vec + vecindex, mincoreinfo);
886 * If the map has changed, due to the subyte, the previous
887 * output may be invalid.
889 vm_map_lock_read(map);
890 if (timestamp != map->timestamp)
893 lastvecindex = vecindex;
899 * subyte may page fault. In case it needs to modify
900 * the map, we release the lock.
902 vm_map_unlock_read(map);
905 * Zero the last entries in the byte vector.
907 vecindex = OFF_TO_IDX(end - first_addr);
908 while ((lastvecindex + 1) < vecindex) {
909 error = subyte(vec + lastvecindex, 0);
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)
924 vm_map_unlock_read(map);
929 #ifndef _SYS_SYSPROTO_H_
941 struct mlock_args *uap;
944 vm_offset_t addr, end, last, start;
945 vm_size_t npages, size;
948 error = priv_check(td, PRIV_VM_MLOCK);
951 addr = (vm_offset_t)uap->addr;
954 start = trunc_page(addr);
955 end = round_page(last);
956 if (last < addr || end < addr)
958 npages = atop(end - start);
959 if (npages > vm_page_max_wired)
964 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
965 lim_cur(proc, RLIMIT_MEMLOCK)) {
970 if (npages + cnt.v_wire_count > vm_page_max_wired)
972 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
973 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
974 return (error == KERN_SUCCESS ? 0 : ENOMEM);
977 #ifndef _SYS_SYSPROTO_H_
978 struct mlockall_args {
989 struct mlockall_args *uap;
994 map = &td->td_proc->p_vmspace->vm_map;
997 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1002 * If wiring all pages in the process would cause it to exceed
1003 * a hard resource limit, return ENOMEM.
1005 PROC_LOCK(td->td_proc);
1006 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1007 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1008 PROC_UNLOCK(td->td_proc);
1011 PROC_UNLOCK(td->td_proc);
1013 error = priv_check(td, PRIV_VM_MLOCK);
1018 if (uap->how & MCL_FUTURE) {
1020 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1025 if (uap->how & MCL_CURRENT) {
1027 * P1003.1-2001 mandates that all currently mapped pages
1028 * will be memory resident and locked (wired) upon return
1029 * from mlockall(). vm_map_wire() will wire pages, by
1030 * calling vm_fault_wire() for each page in the region.
1032 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1033 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1034 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1040 #ifndef _SYS_SYSPROTO_H_
1041 struct munlockall_args {
1052 struct munlockall_args *uap;
1057 map = &td->td_proc->p_vmspace->vm_map;
1058 error = priv_check(td, PRIV_VM_MUNLOCK);
1062 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1064 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1067 /* Forcibly unwire all pages. */
1068 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1069 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1074 #ifndef _SYS_SYSPROTO_H_
1075 struct munlock_args {
1086 struct munlock_args *uap;
1088 vm_offset_t addr, end, last, start;
1092 error = priv_check(td, PRIV_VM_MUNLOCK);
1095 addr = (vm_offset_t)uap->addr;
1098 start = trunc_page(addr);
1099 end = round_page(last);
1100 if (last < addr || end < addr)
1102 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1103 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1104 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1112 * Helper function for vm_mmap. Perform sanity check specific for mmap
1113 * operations on vnodes.
1116 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1117 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1118 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp)
1129 cred = td->td_ucred;
1130 vfslocked = VFS_LOCK_GIANT(mp);
1131 if ((error = vget(vp, LK_SHARED, td)) != 0) {
1132 VFS_UNLOCK_GIANT(vfslocked);
1138 if (vp->v_type == VREG) {
1140 * Get the proper underlying object
1146 if (obj->handle != vp) {
1148 vp = (struct vnode*)obj->handle;
1149 vget(vp, LK_SHARED, td);
1151 } else if (vp->v_type == VCHR) {
1152 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1153 vp->v_rdev, foffp, objp);
1161 if ((error = VOP_GETATTR(vp, &va, cred)))
1164 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1168 if ((flags & MAP_SHARED) != 0) {
1169 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1170 if (prot & PROT_WRITE) {
1174 *maxprotp &= ~VM_PROT_WRITE;
1178 * If it is a regular file without any references
1179 * we do not need to sync it.
1180 * Adjust object size to be the size of actual file.
1182 objsize = round_page(va.va_size);
1183 if (va.va_nlink == 0)
1184 flags |= MAP_NOSYNC;
1185 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, td->td_ucred);
1194 vfs_mark_atime(vp, cred);
1198 VFS_UNLOCK_GIANT(vfslocked);
1207 * Helper function for vm_mmap. Perform sanity check specific for mmap
1208 * operations on cdevs.
1211 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1212 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1213 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1217 int error, flags, ref;
1221 dsw = dev_refthread(cdev, &ref);
1224 if (dsw->d_flags & D_MMAP_ANON) {
1225 dev_relthread(cdev, ref);
1226 *maxprotp = VM_PROT_ALL;
1227 *flagsp |= MAP_ANON;
1231 * cdevs do not provide private mappings of any kind.
1233 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1234 (prot & PROT_WRITE) != 0) {
1235 dev_relthread(cdev, ref);
1238 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1239 dev_relthread(cdev, ref);
1243 * Force device mappings to be shared.
1245 flags |= MAP_SHARED;
1247 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1249 dev_relthread(cdev, ref);
1254 * First, try d_mmap_single(). If that is not implemented
1255 * (returns ENODEV), fall back to using the device pager.
1256 * Note that d_mmap_single() must return a reference to the
1257 * object (it needs to bump the reference count of the object
1258 * it returns somehow).
1260 * XXX assumes VM_PROT_* == PROT_*
1262 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1263 dev_relthread(cdev, ref);
1264 if (error != ENODEV)
1266 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1280 * Helper function for vm_mmap. Perform sanity check specific for mmap
1281 * operations on shm file descriptors.
1284 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1285 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1286 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1290 if ((*flagsp & MAP_SHARED) != 0 &&
1291 (*maxprotp & VM_PROT_WRITE) == 0 &&
1292 (prot & PROT_WRITE) != 0)
1295 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1299 error = shm_mmap(shmfd, objsize, foff, objp);
1310 * Internal version of mmap. Currently used by mmap, exec, and sys5
1311 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1314 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1315 vm_prot_t maxprot, int flags,
1316 objtype_t handle_type, void *handle,
1320 vm_object_t object = NULL;
1321 int rv = KERN_SUCCESS;
1323 struct thread *td = curthread;
1328 size = round_page(size);
1330 PROC_LOCK(td->td_proc);
1331 if (td->td_proc->p_vmspace->vm_map.size + size >
1332 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1333 PROC_UNLOCK(td->td_proc);
1336 PROC_UNLOCK(td->td_proc);
1339 * We currently can only deal with page aligned file offsets.
1340 * The check is here rather than in the syscall because the
1341 * kernel calls this function internally for other mmaping
1342 * operations (such as in exec) and non-aligned offsets will
1343 * cause pmap inconsistencies...so we want to be sure to
1344 * disallow this in all cases.
1346 if (foff & PAGE_MASK)
1349 if ((flags & MAP_FIXED) == 0) {
1351 *addr = round_page(*addr);
1353 if (*addr != trunc_page(*addr))
1358 * Lookup/allocate object.
1360 switch (handle_type) {
1362 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1363 handle, &foff, &object);
1366 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1367 handle, &foff, &object);
1370 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1371 handle, foff, &object);
1374 if (handle == NULL) {
1385 if (flags & MAP_ANON) {
1389 * Unnamed anonymous regions always start at 0.
1393 } else if (flags & MAP_PREFAULT_READ)
1394 docow = MAP_PREFAULT;
1396 docow = MAP_PREFAULT_PARTIAL;
1398 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1399 docow |= MAP_COPY_ON_WRITE;
1400 if (flags & MAP_NOSYNC)
1401 docow |= MAP_DISABLE_SYNCER;
1402 if (flags & MAP_NOCORE)
1403 docow |= MAP_DISABLE_COREDUMP;
1405 if (flags & MAP_STACK)
1406 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1407 docow | MAP_STACK_GROWS_DOWN);
1409 rv = vm_map_find(map, object, foff, addr, size,
1410 object != NULL && object->type == OBJT_DEVICE ?
1411 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1413 rv = vm_map_fixed(map, object, foff, *addr, size,
1414 prot, maxprot, docow);
1416 if (rv != KERN_SUCCESS) {
1418 * Lose the object reference. Will destroy the
1419 * object if it's an unnamed anonymous mapping
1420 * or named anonymous without other references.
1422 vm_object_deallocate(object);
1423 } else if (flags & MAP_SHARED) {
1425 * Shared memory is also shared with children.
1427 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1428 if (rv != KERN_SUCCESS)
1429 (void) vm_map_remove(map, *addr, *addr + size);
1433 * If the process has requested that all future mappings
1434 * be wired, then heed this.
1436 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1437 vm_map_wire(map, *addr, *addr + size,
1438 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1439 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1441 return (vm_mmap_to_errno(rv));
1445 vm_mmap_to_errno(int rv)
1451 case KERN_INVALID_ADDRESS:
1454 case KERN_PROTECTION_FAILURE: