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:
495 #ifndef _SYS_SYSPROTO_H_
507 struct munmap_args *uap;
510 struct pmckern_map_out pkm;
511 vm_map_entry_t entry;
514 vm_size_t size, pageoff;
517 addr = (vm_offset_t) uap->addr;
522 pageoff = (addr & PAGE_MASK);
525 size = (vm_size_t) round_page(size);
526 if (addr + size < addr)
530 * Check for illegal addresses. Watch out for address wrap...
532 map = &td->td_proc->p_vmspace->vm_map;
533 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
538 * Inform hwpmc if the address range being unmapped contains
539 * an executable region.
541 pkm.pm_address = (uintptr_t) NULL;
542 if (vm_map_lookup_entry(map, addr, &entry)) {
544 entry != &map->header && entry->start < addr + size;
545 entry = entry->next) {
546 if (vm_map_check_protection(map, entry->start,
547 entry->end, VM_PROT_EXECUTE) == TRUE) {
548 pkm.pm_address = (uintptr_t) addr;
549 pkm.pm_size = (size_t) size;
555 vm_map_delete(map, addr, addr + size);
558 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
559 vm_map_lock_downgrade(map);
560 if (pkm.pm_address != (uintptr_t) NULL)
561 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
562 vm_map_unlock_read(map);
566 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
570 #ifndef _SYS_SYSPROTO_H_
571 struct mprotect_args {
583 struct mprotect_args *uap;
586 vm_size_t size, pageoff;
589 addr = (vm_offset_t) uap->addr;
591 prot = uap->prot & VM_PROT_ALL;
593 pageoff = (addr & PAGE_MASK);
596 size = (vm_size_t) round_page(size);
597 if (addr + size < addr)
600 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
601 addr + size, prot, FALSE)) {
604 case KERN_PROTECTION_FAILURE:
606 case KERN_RESOURCE_SHORTAGE:
612 #ifndef _SYS_SYSPROTO_H_
613 struct minherit_args {
625 struct minherit_args *uap;
628 vm_size_t size, pageoff;
629 vm_inherit_t inherit;
631 addr = (vm_offset_t)uap->addr;
633 inherit = uap->inherit;
635 pageoff = (addr & PAGE_MASK);
638 size = (vm_size_t) round_page(size);
639 if (addr + size < addr)
642 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
643 addr + size, inherit)) {
646 case KERN_PROTECTION_FAILURE:
652 #ifndef _SYS_SYSPROTO_H_
653 struct madvise_args {
667 struct madvise_args *uap;
669 vm_offset_t start, end;
675 * Check for our special case, advising the swap pager we are
678 if (uap->behav == MADV_PROTECT) {
679 error = priv_check(td, PRIV_VM_MADV_PROTECT);
683 p->p_flag |= P_PROTECTED;
689 * Check for illegal behavior
691 if (uap->behav < 0 || uap->behav > MADV_CORE)
694 * Check for illegal addresses. Watch out for address wrap... Note
695 * that VM_*_ADDRESS are not constants due to casts (argh).
697 map = &td->td_proc->p_vmspace->vm_map;
698 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
699 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
701 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
705 * Since this routine is only advisory, we default to conservative
708 start = trunc_page((vm_offset_t) uap->addr);
709 end = round_page((vm_offset_t) uap->addr + uap->len);
711 if (vm_map_madvise(map, start, end, uap->behav))
716 #ifndef _SYS_SYSPROTO_H_
717 struct mincore_args {
731 struct mincore_args *uap;
733 vm_offset_t addr, first_addr;
734 vm_offset_t end, cend;
739 int vecindex, lastvecindex;
740 vm_map_entry_t current;
741 vm_map_entry_t entry;
743 unsigned int timestamp;
746 * Make sure that the addresses presented are valid for user
749 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
750 end = addr + (vm_size_t)round_page(uap->len);
751 map = &td->td_proc->p_vmspace->vm_map;
752 if (end > vm_map_max(map) || end < addr)
756 * Address of byte vector
760 pmap = vmspace_pmap(td->td_proc->p_vmspace);
762 vm_map_lock_read(map);
764 timestamp = map->timestamp;
766 if (!vm_map_lookup_entry(map, addr, &entry)) {
767 vm_map_unlock_read(map);
772 * Do this on a map entry basis so that if the pages are not
773 * in the current processes address space, we can easily look
774 * up the pages elsewhere.
777 for (current = entry;
778 (current != &map->header) && (current->start < end);
779 current = current->next) {
782 * check for contiguity
784 if (current->end < end &&
785 (entry->next == &map->header ||
786 current->next->start > current->end)) {
787 vm_map_unlock_read(map);
792 * ignore submaps (for now) or null objects
794 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
795 current->object.vm_object == NULL)
799 * limit this scan to the current map entry and the
800 * limits for the mincore call
802 if (addr < current->start)
803 addr = current->start;
809 * scan this entry one page at a time
811 while (addr < cend) {
813 * Check pmap first, it is likely faster, also
814 * it can provide info as to whether we are the
815 * one referencing or modifying the page.
817 mincoreinfo = pmap_mincore(pmap, addr);
823 * calculate the page index into the object
825 offset = current->offset + (addr - current->start);
826 pindex = OFF_TO_IDX(offset);
827 VM_OBJECT_LOCK(current->object.vm_object);
828 m = vm_page_lookup(current->object.vm_object,
831 * if the page is resident, then gather information about
834 if (m != NULL && m->valid != 0) {
835 mincoreinfo = MINCORE_INCORE;
836 vm_page_lock_queues();
839 mincoreinfo |= MINCORE_MODIFIED_OTHER;
840 if ((m->flags & PG_REFERENCED) ||
841 pmap_ts_referenced(m)) {
842 vm_page_flag_set(m, PG_REFERENCED);
843 mincoreinfo |= MINCORE_REFERENCED_OTHER;
845 vm_page_unlock_queues();
847 VM_OBJECT_UNLOCK(current->object.vm_object);
851 * subyte may page fault. In case it needs to modify
852 * the map, we release the lock.
854 vm_map_unlock_read(map);
857 * calculate index into user supplied byte vector
859 vecindex = OFF_TO_IDX(addr - first_addr);
862 * If we have skipped map entries, we need to make sure that
863 * the byte vector is zeroed for those skipped entries.
865 while ((lastvecindex + 1) < vecindex) {
866 error = subyte(vec + lastvecindex, 0);
875 * Pass the page information to the user
877 error = subyte(vec + vecindex, mincoreinfo);
884 * If the map has changed, due to the subyte, the previous
885 * output may be invalid.
887 vm_map_lock_read(map);
888 if (timestamp != map->timestamp)
891 lastvecindex = vecindex;
897 * subyte may page fault. In case it needs to modify
898 * the map, we release the lock.
900 vm_map_unlock_read(map);
903 * Zero the last entries in the byte vector.
905 vecindex = OFF_TO_IDX(end - first_addr);
906 while ((lastvecindex + 1) < vecindex) {
907 error = subyte(vec + lastvecindex, 0);
916 * If the map has changed, due to the subyte, the previous
917 * output may be invalid.
919 vm_map_lock_read(map);
920 if (timestamp != map->timestamp)
922 vm_map_unlock_read(map);
927 #ifndef _SYS_SYSPROTO_H_
939 struct mlock_args *uap;
942 vm_offset_t addr, end, last, start;
943 vm_size_t npages, size;
946 error = priv_check(td, PRIV_VM_MLOCK);
949 addr = (vm_offset_t)uap->addr;
952 start = trunc_page(addr);
953 end = round_page(last);
954 if (last < addr || end < addr)
956 npages = atop(end - start);
957 if (npages > vm_page_max_wired)
962 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
963 lim_cur(proc, RLIMIT_MEMLOCK)) {
968 if (npages + cnt.v_wire_count > vm_page_max_wired)
970 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
971 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
972 return (error == KERN_SUCCESS ? 0 : ENOMEM);
975 #ifndef _SYS_SYSPROTO_H_
976 struct mlockall_args {
987 struct mlockall_args *uap;
992 map = &td->td_proc->p_vmspace->vm_map;
995 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1000 * If wiring all pages in the process would cause it to exceed
1001 * a hard resource limit, return ENOMEM.
1003 PROC_LOCK(td->td_proc);
1004 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1005 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1006 PROC_UNLOCK(td->td_proc);
1009 PROC_UNLOCK(td->td_proc);
1011 error = priv_check(td, PRIV_VM_MLOCK);
1016 if (uap->how & MCL_FUTURE) {
1018 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1023 if (uap->how & MCL_CURRENT) {
1025 * P1003.1-2001 mandates that all currently mapped pages
1026 * will be memory resident and locked (wired) upon return
1027 * from mlockall(). vm_map_wire() will wire pages, by
1028 * calling vm_fault_wire() for each page in the region.
1030 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1031 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1032 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1038 #ifndef _SYS_SYSPROTO_H_
1039 struct munlockall_args {
1050 struct munlockall_args *uap;
1055 map = &td->td_proc->p_vmspace->vm_map;
1056 error = priv_check(td, PRIV_VM_MUNLOCK);
1060 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1062 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1065 /* Forcibly unwire all pages. */
1066 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1067 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1072 #ifndef _SYS_SYSPROTO_H_
1073 struct munlock_args {
1084 struct munlock_args *uap;
1086 vm_offset_t addr, end, last, start;
1090 error = priv_check(td, PRIV_VM_MUNLOCK);
1093 addr = (vm_offset_t)uap->addr;
1096 start = trunc_page(addr);
1097 end = round_page(last);
1098 if (last < addr || end < addr)
1100 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1101 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1102 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1110 * Helper function for vm_mmap. Perform sanity check specific for mmap
1111 * operations on vnodes.
1114 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1115 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1116 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp)
1127 cred = td->td_ucred;
1128 vfslocked = VFS_LOCK_GIANT(mp);
1129 if ((error = vget(vp, LK_SHARED, td)) != 0) {
1130 VFS_UNLOCK_GIANT(vfslocked);
1136 if (vp->v_type == VREG) {
1138 * Get the proper underlying object
1144 if (obj->handle != vp) {
1146 vp = (struct vnode*)obj->handle;
1147 vget(vp, LK_SHARED, td);
1149 } else if (vp->v_type == VCHR) {
1150 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1151 vp->v_rdev, foffp, objp);
1159 if ((error = VOP_GETATTR(vp, &va, cred)))
1162 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1166 if ((flags & MAP_SHARED) != 0) {
1167 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1168 if (prot & PROT_WRITE) {
1172 *maxprotp &= ~VM_PROT_WRITE;
1176 * If it is a regular file without any references
1177 * we do not need to sync it.
1178 * Adjust object size to be the size of actual file.
1180 objsize = round_page(va.va_size);
1181 if (va.va_nlink == 0)
1182 flags |= MAP_NOSYNC;
1183 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, td->td_ucred);
1192 vfs_mark_atime(vp, cred);
1196 VFS_UNLOCK_GIANT(vfslocked);
1205 * Helper function for vm_mmap. Perform sanity check specific for mmap
1206 * operations on cdevs.
1209 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1210 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1211 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1215 int error, flags, ref;
1219 dsw = dev_refthread(cdev, &ref);
1222 if (dsw->d_flags & D_MMAP_ANON) {
1223 dev_relthread(cdev, ref);
1224 *maxprotp = VM_PROT_ALL;
1225 *flagsp |= MAP_ANON;
1229 * cdevs do not provide private mappings of any kind.
1231 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1232 (prot & PROT_WRITE) != 0) {
1233 dev_relthread(cdev, ref);
1236 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1237 dev_relthread(cdev, ref);
1241 * Force device mappings to be shared.
1243 flags |= MAP_SHARED;
1245 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1247 dev_relthread(cdev, ref);
1252 * First, try d_mmap_single(). If that is not implemented
1253 * (returns ENODEV), fall back to using the device pager.
1254 * Note that d_mmap_single() must return a reference to the
1255 * object (it needs to bump the reference count of the object
1256 * it returns somehow).
1258 * XXX assumes VM_PROT_* == PROT_*
1260 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1261 dev_relthread(cdev, ref);
1262 if (error != ENODEV)
1264 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1278 * Helper function for vm_mmap. Perform sanity check specific for mmap
1279 * operations on shm file descriptors.
1282 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1283 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1284 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1288 if ((*flagsp & MAP_SHARED) != 0 &&
1289 (*maxprotp & VM_PROT_WRITE) == 0 &&
1290 (prot & PROT_WRITE) != 0)
1293 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1297 error = shm_mmap(shmfd, objsize, foff, objp);
1308 * Internal version of mmap. Currently used by mmap, exec, and sys5
1309 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1312 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1313 vm_prot_t maxprot, int flags,
1314 objtype_t handle_type, void *handle,
1318 vm_object_t object = NULL;
1319 int rv = KERN_SUCCESS;
1321 struct thread *td = curthread;
1326 size = round_page(size);
1328 PROC_LOCK(td->td_proc);
1329 if (td->td_proc->p_vmspace->vm_map.size + size >
1330 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1331 PROC_UNLOCK(td->td_proc);
1334 PROC_UNLOCK(td->td_proc);
1337 * We currently can only deal with page aligned file offsets.
1338 * The check is here rather than in the syscall because the
1339 * kernel calls this function internally for other mmaping
1340 * operations (such as in exec) and non-aligned offsets will
1341 * cause pmap inconsistencies...so we want to be sure to
1342 * disallow this in all cases.
1344 if (foff & PAGE_MASK)
1347 if ((flags & MAP_FIXED) == 0) {
1349 *addr = round_page(*addr);
1351 if (*addr != trunc_page(*addr))
1356 * Lookup/allocate object.
1358 switch (handle_type) {
1360 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1361 handle, &foff, &object);
1364 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1365 handle, &foff, &object);
1368 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1369 handle, foff, &object);
1372 if (handle == NULL) {
1383 if (flags & MAP_ANON) {
1387 * Unnamed anonymous regions always start at 0.
1391 } else if (flags & MAP_PREFAULT_READ)
1392 docow = MAP_PREFAULT;
1394 docow = MAP_PREFAULT_PARTIAL;
1396 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1397 docow |= MAP_COPY_ON_WRITE;
1398 if (flags & MAP_NOSYNC)
1399 docow |= MAP_DISABLE_SYNCER;
1400 if (flags & MAP_NOCORE)
1401 docow |= MAP_DISABLE_COREDUMP;
1403 if (flags & MAP_STACK)
1404 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1405 docow | MAP_STACK_GROWS_DOWN);
1407 rv = vm_map_find(map, object, foff, addr, size,
1408 object != NULL && object->type == OBJT_DEVICE ?
1409 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1411 rv = vm_map_fixed(map, object, foff, *addr, size,
1412 prot, maxprot, docow);
1414 if (rv != KERN_SUCCESS) {
1416 * Lose the object reference. Will destroy the
1417 * object if it's an unnamed anonymous mapping
1418 * or named anonymous without other references.
1420 vm_object_deallocate(object);
1421 } else if (flags & MAP_SHARED) {
1423 * Shared memory is also shared with children.
1425 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1426 if (rv != KERN_SUCCESS)
1427 (void) vm_map_remove(map, *addr, *addr + size);
1431 * If the process has requested that all future mappings
1432 * be wired, then heed this.
1434 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1435 vm_map_wire(map, *addr, *addr + size,
1436 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1438 return (vm_mmap_to_errno(rv));
1442 vm_mmap_to_errno(int rv)
1448 case KERN_INVALID_ADDRESS:
1451 case KERN_PROTECTION_FAILURE: