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>
59 #include <sys/resource.h>
60 #include <sys/resourcevar.h>
61 #include <sys/vnode.h>
62 #include <sys/fcntl.h>
65 #include <sys/mount.h>
68 #include <sys/vmmeter.h>
69 #include <sys/sysctl.h>
71 #include <security/mac/mac_framework.h>
74 #include <vm/vm_param.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
80 #include <vm/vm_pageout.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_kern.h>
86 #include <sys/pmckern.h>
89 #ifndef _SYS_SYSPROTO_H_
95 static int max_proc_mmap;
96 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
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 *);
130 struct sbrk_args *uap;
132 /* Not yet implemented */
136 #ifndef _SYS_SYSPROTO_H_
149 struct sstk_args *uap;
151 /* Not yet implemented */
155 #if defined(COMPAT_43)
156 #ifndef _SYS_SYSPROTO_H_
157 struct getpagesize_args {
164 ogetpagesize(td, uap)
166 struct getpagesize_args *uap;
169 td->td_retval[0] = PAGE_SIZE;
172 #endif /* COMPAT_43 */
176 * Memory Map (mmap) system call. Note that the file offset
177 * and address are allowed to be NOT page aligned, though if
178 * the MAP_FIXED flag it set, both must have the same remainder
179 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
180 * page-aligned, the actual mapping starts at trunc_page(addr)
181 * and the return value is adjusted up by the page offset.
183 * Generally speaking, only character devices which are themselves
184 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
185 * there would be no cache coherency between a descriptor and a VM mapping
186 * both to the same character device.
188 * Block devices can be mmap'd no matter what they represent. Cache coherency
189 * is maintained as long as you do not write directly to the underlying
192 #ifndef _SYS_SYSPROTO_H_
210 struct mmap_args *uap;
213 struct pmckern_map_in pkm;
218 vm_size_t size, pageoff;
219 vm_prot_t prot, maxprot;
221 objtype_t handle_type;
224 struct vmspace *vms = td->td_proc->p_vmspace;
226 addr = (vm_offset_t) uap->addr;
228 prot = uap->prot & VM_PROT_ALL;
233 /* make sure mapping fits into numeric range etc */
234 if ((ssize_t) uap->len < 0 ||
235 ((flags & MAP_ANON) && uap->fd != -1))
238 if (flags & MAP_STACK) {
239 if ((uap->fd != -1) ||
240 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
247 * Align the file position to a page boundary,
248 * and save its page offset component.
250 pageoff = (pos & PAGE_MASK);
253 /* Adjust size for rounding (on both ends). */
254 size += pageoff; /* low end... */
255 size = (vm_size_t) round_page(size); /* hi end */
258 * Check for illegal addresses. Watch out for address wrap... Note
259 * that VM_*_ADDRESS are not constants due to casts (argh).
261 if (flags & MAP_FIXED) {
263 * The specified address must have the same remainder
264 * as the file offset taken modulo PAGE_SIZE, so it
265 * should be aligned after adjustment by pageoff.
268 if (addr & PAGE_MASK)
270 /* Address range must be all in user VM space. */
271 if (addr < vm_map_min(&vms->vm_map) ||
272 addr + size > vm_map_max(&vms->vm_map))
274 if (addr + size < addr)
278 * XXX for non-fixed mappings where no hint is provided or
279 * the hint would fall in the potential heap space,
280 * place it after the end of the largest possible heap.
282 * There should really be a pmap call to determine a reasonable
285 PROC_LOCK(td->td_proc);
287 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
288 addr < round_page((vm_offset_t)vms->vm_daddr +
289 lim_max(td->td_proc, RLIMIT_DATA))))
290 addr = round_page((vm_offset_t)vms->vm_daddr +
291 lim_max(td->td_proc, RLIMIT_DATA));
292 PROC_UNLOCK(td->td_proc);
294 if (flags & MAP_ANON) {
296 * Mapping blank space is trivial.
299 handle_type = OBJT_DEFAULT;
300 maxprot = VM_PROT_ALL;
304 * Mapping file, get fp for validation. Obtain vnode and make
305 * sure it is of appropriate type.
306 * don't let the descriptor disappear on us if we block
308 if ((error = fget(td, uap->fd, &fp)) != 0)
310 if (fp->f_type != DTYPE_VNODE) {
315 * POSIX shared-memory objects are defined to have
316 * kernel persistence, and are not defined to support
317 * read(2)/write(2) -- or even open(2). Thus, we can
318 * use MAP_ASYNC to trade on-disk coherence for speed.
319 * The shm_open(3) library routine turns on the FPOSIXSHM
320 * flag to request this behavior.
322 if (fp->f_flag & FPOSIXSHM)
326 * Ensure that file and memory protections are
327 * compatible. Note that we only worry about
328 * writability if mapping is shared; in this case,
329 * current and max prot are dictated by the open file.
330 * XXX use the vnode instead? Problem is: what
331 * credentials do we use for determination? What if
332 * proc does a setuid?
334 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
335 maxprot = VM_PROT_NONE;
337 maxprot = VM_PROT_EXECUTE;
338 if (fp->f_flag & FREAD) {
339 maxprot |= VM_PROT_READ;
340 } else if (prot & PROT_READ) {
345 * If we are sharing potential changes (either via
346 * MAP_SHARED or via the implicit sharing of character
347 * device mappings), and we are trying to get write
348 * permission although we opened it without asking
351 if ((flags & MAP_SHARED) != 0) {
352 if ((fp->f_flag & FWRITE) != 0) {
353 maxprot |= VM_PROT_WRITE;
354 } else if ((prot & PROT_WRITE) != 0) {
358 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
359 maxprot |= VM_PROT_WRITE;
362 handle_type = OBJT_VNODE;
366 * Do not allow more then a certain number of vm_map_entry structures
367 * per process. Scale with the number of rforks sharing the map
368 * to make the limit reasonable for threads.
371 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
377 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
378 flags, handle_type, handle, pos);
381 /* inform hwpmc(4) if an executable is being mapped */
382 if (error == 0 && handle_type == OBJT_VNODE &&
383 (prot & PROT_EXEC)) {
384 pkm.pm_file = handle;
385 pkm.pm_address = (uintptr_t) addr;
386 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
390 td->td_retval[0] = (register_t) (addr + pageoff);
399 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
401 struct mmap_args oargs;
403 oargs.addr = uap->addr;
404 oargs.len = uap->len;
405 oargs.prot = uap->prot;
406 oargs.flags = uap->flags;
408 oargs.pos = uap->pos;
409 return (mmap(td, &oargs));
413 #ifndef _SYS_SYSPROTO_H_
426 struct ommap_args *uap;
428 struct mmap_args nargs;
429 static const char cvtbsdprot[8] = {
433 PROT_EXEC | PROT_WRITE,
435 PROT_EXEC | PROT_READ,
436 PROT_WRITE | PROT_READ,
437 PROT_EXEC | PROT_WRITE | PROT_READ,
440 #define OMAP_ANON 0x0002
441 #define OMAP_COPY 0x0020
442 #define OMAP_SHARED 0x0010
443 #define OMAP_FIXED 0x0100
445 nargs.addr = uap->addr;
446 nargs.len = uap->len;
447 nargs.prot = cvtbsdprot[uap->prot & 0x7];
449 if (uap->flags & OMAP_ANON)
450 nargs.flags |= MAP_ANON;
451 if (uap->flags & OMAP_COPY)
452 nargs.flags |= MAP_COPY;
453 if (uap->flags & OMAP_SHARED)
454 nargs.flags |= MAP_SHARED;
456 nargs.flags |= MAP_PRIVATE;
457 if (uap->flags & OMAP_FIXED)
458 nargs.flags |= MAP_FIXED;
460 nargs.pos = uap->pos;
461 return (mmap(td, &nargs));
463 #endif /* COMPAT_43 */
466 #ifndef _SYS_SYSPROTO_H_
479 struct msync_args *uap;
482 vm_size_t size, pageoff;
487 addr = (vm_offset_t) uap->addr;
491 pageoff = (addr & PAGE_MASK);
494 size = (vm_size_t) round_page(size);
495 if (addr + size < addr)
498 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
501 map = &td->td_proc->p_vmspace->vm_map;
504 * Clean the pages and interpret the return value.
506 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
507 (flags & MS_INVALIDATE) != 0);
511 case KERN_INVALID_ADDRESS:
512 return (EINVAL); /* Sun returns ENOMEM? */
513 case KERN_INVALID_ARGUMENT:
520 #ifndef _SYS_SYSPROTO_H_
532 struct munmap_args *uap;
535 struct pmckern_map_out pkm;
536 vm_map_entry_t entry;
539 vm_size_t size, pageoff;
542 addr = (vm_offset_t) uap->addr;
547 pageoff = (addr & PAGE_MASK);
550 size = (vm_size_t) round_page(size);
551 if (addr + size < addr)
555 * Check for illegal addresses. Watch out for address wrap...
557 map = &td->td_proc->p_vmspace->vm_map;
558 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
563 * Inform hwpmc if the address range being unmapped contains
564 * an executable region.
566 if (vm_map_lookup_entry(map, addr, &entry)) {
568 entry != &map->header && entry->start < addr + size;
569 entry = entry->next) {
570 if (vm_map_check_protection(map, entry->start,
571 entry->end, VM_PROT_EXECUTE) == TRUE) {
572 pkm.pm_address = (uintptr_t) addr;
573 pkm.pm_size = (size_t) size;
574 PMC_CALL_HOOK(td, PMC_FN_MUNMAP,
581 /* returns nothing but KERN_SUCCESS anyway */
582 vm_map_delete(map, addr, addr + size);
587 #ifndef _SYS_SYSPROTO_H_
588 struct mprotect_args {
600 struct mprotect_args *uap;
603 vm_size_t size, pageoff;
606 addr = (vm_offset_t) uap->addr;
608 prot = uap->prot & VM_PROT_ALL;
609 #if defined(VM_PROT_READ_IS_EXEC)
610 if (prot & VM_PROT_READ)
611 prot |= VM_PROT_EXECUTE;
614 pageoff = (addr & PAGE_MASK);
617 size = (vm_size_t) round_page(size);
618 if (addr + size < addr)
621 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
622 addr + size, prot, FALSE)) {
625 case KERN_PROTECTION_FAILURE:
631 #ifndef _SYS_SYSPROTO_H_
632 struct minherit_args {
644 struct minherit_args *uap;
647 vm_size_t size, pageoff;
648 vm_inherit_t inherit;
650 addr = (vm_offset_t)uap->addr;
652 inherit = uap->inherit;
654 pageoff = (addr & PAGE_MASK);
657 size = (vm_size_t) round_page(size);
658 if (addr + size < addr)
661 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
662 addr + size, inherit)) {
665 case KERN_PROTECTION_FAILURE:
671 #ifndef _SYS_SYSPROTO_H_
672 struct madvise_args {
686 struct madvise_args *uap;
688 vm_offset_t start, end;
694 * Check for our special case, advising the swap pager we are
697 if (uap->behav == MADV_PROTECT) {
698 error = priv_check(td, PRIV_VM_MADV_PROTECT);
702 p->p_flag |= P_PROTECTED;
708 * Check for illegal behavior
710 if (uap->behav < 0 || uap->behav > MADV_CORE)
713 * Check for illegal addresses. Watch out for address wrap... Note
714 * that VM_*_ADDRESS are not constants due to casts (argh).
716 map = &td->td_proc->p_vmspace->vm_map;
717 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
718 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
720 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
724 * Since this routine is only advisory, we default to conservative
727 start = trunc_page((vm_offset_t) uap->addr);
728 end = round_page((vm_offset_t) uap->addr + uap->len);
730 if (vm_map_madvise(map, start, end, uap->behav))
735 #ifndef _SYS_SYSPROTO_H_
736 struct mincore_args {
750 struct mincore_args *uap;
752 vm_offset_t addr, first_addr;
753 vm_offset_t end, cend;
758 int vecindex, lastvecindex;
759 vm_map_entry_t current;
760 vm_map_entry_t entry;
762 unsigned int timestamp;
765 * Make sure that the addresses presented are valid for user
768 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
769 end = addr + (vm_size_t)round_page(uap->len);
770 map = &td->td_proc->p_vmspace->vm_map;
771 if (end > vm_map_max(map) || end < addr)
775 * Address of byte vector
779 pmap = vmspace_pmap(td->td_proc->p_vmspace);
781 vm_map_lock_read(map);
783 timestamp = map->timestamp;
785 if (!vm_map_lookup_entry(map, addr, &entry)) {
786 vm_map_unlock_read(map);
791 * Do this on a map entry basis so that if the pages are not
792 * in the current processes address space, we can easily look
793 * up the pages elsewhere.
796 for (current = entry;
797 (current != &map->header) && (current->start < end);
798 current = current->next) {
801 * check for contiguity
803 if (current->end < end &&
804 (entry->next == &map->header ||
805 current->next->start > current->end)) {
806 vm_map_unlock_read(map);
811 * ignore submaps (for now) or null objects
813 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
814 current->object.vm_object == NULL)
818 * limit this scan to the current map entry and the
819 * limits for the mincore call
821 if (addr < current->start)
822 addr = current->start;
828 * scan this entry one page at a time
830 while (addr < cend) {
832 * Check pmap first, it is likely faster, also
833 * it can provide info as to whether we are the
834 * one referencing or modifying the page.
836 mincoreinfo = pmap_mincore(pmap, addr);
842 * calculate the page index into the object
844 offset = current->offset + (addr - current->start);
845 pindex = OFF_TO_IDX(offset);
846 VM_OBJECT_LOCK(current->object.vm_object);
847 m = vm_page_lookup(current->object.vm_object,
850 * if the page is resident, then gather information about
853 if (m != NULL && m->valid != 0) {
854 mincoreinfo = MINCORE_INCORE;
855 vm_page_lock_queues();
858 mincoreinfo |= MINCORE_MODIFIED_OTHER;
859 if ((m->flags & PG_REFERENCED) ||
860 pmap_ts_referenced(m)) {
861 vm_page_flag_set(m, PG_REFERENCED);
862 mincoreinfo |= MINCORE_REFERENCED_OTHER;
864 vm_page_unlock_queues();
866 VM_OBJECT_UNLOCK(current->object.vm_object);
870 * subyte may page fault. In case it needs to modify
871 * the map, we release the lock.
873 vm_map_unlock_read(map);
876 * calculate index into user supplied byte vector
878 vecindex = OFF_TO_IDX(addr - first_addr);
881 * If we have skipped map entries, we need to make sure that
882 * the byte vector is zeroed for those skipped entries.
884 while ((lastvecindex + 1) < vecindex) {
885 error = subyte(vec + lastvecindex, 0);
894 * Pass the page information to the user
896 error = subyte(vec + vecindex, mincoreinfo);
903 * If the map has changed, due to the subyte, the previous
904 * output may be invalid.
906 vm_map_lock_read(map);
907 if (timestamp != map->timestamp)
910 lastvecindex = vecindex;
916 * subyte may page fault. In case it needs to modify
917 * the map, we release the lock.
919 vm_map_unlock_read(map);
922 * Zero the last entries in the byte vector.
924 vecindex = OFF_TO_IDX(end - first_addr);
925 while ((lastvecindex + 1) < vecindex) {
926 error = subyte(vec + lastvecindex, 0);
935 * If the map has changed, due to the subyte, the previous
936 * output may be invalid.
938 vm_map_lock_read(map);
939 if (timestamp != map->timestamp)
941 vm_map_unlock_read(map);
946 #ifndef _SYS_SYSPROTO_H_
958 struct mlock_args *uap;
961 vm_offset_t addr, end, last, start;
962 vm_size_t npages, size;
965 error = priv_check(td, PRIV_VM_MLOCK);
968 addr = (vm_offset_t)uap->addr;
971 start = trunc_page(addr);
972 end = round_page(last);
973 if (last < addr || end < addr)
975 npages = atop(end - start);
976 if (npages > vm_page_max_wired)
981 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
982 lim_cur(proc, RLIMIT_MEMLOCK)) {
987 if (npages + cnt.v_wire_count > vm_page_max_wired)
989 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
990 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
991 return (error == KERN_SUCCESS ? 0 : ENOMEM);
994 #ifndef _SYS_SYSPROTO_H_
995 struct mlockall_args {
1006 struct mlockall_args *uap;
1011 map = &td->td_proc->p_vmspace->vm_map;
1014 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1019 * If wiring all pages in the process would cause it to exceed
1020 * a hard resource limit, return ENOMEM.
1022 PROC_LOCK(td->td_proc);
1023 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1024 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1025 PROC_UNLOCK(td->td_proc);
1028 PROC_UNLOCK(td->td_proc);
1030 error = priv_check(td, PRIV_VM_MLOCK);
1035 if (uap->how & MCL_FUTURE) {
1037 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1042 if (uap->how & MCL_CURRENT) {
1044 * P1003.1-2001 mandates that all currently mapped pages
1045 * will be memory resident and locked (wired) upon return
1046 * from mlockall(). vm_map_wire() will wire pages, by
1047 * calling vm_fault_wire() for each page in the region.
1049 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1050 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1051 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1057 #ifndef _SYS_SYSPROTO_H_
1058 struct munlockall_args {
1069 struct munlockall_args *uap;
1074 map = &td->td_proc->p_vmspace->vm_map;
1075 error = priv_check(td, PRIV_VM_MUNLOCK);
1079 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1081 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1084 /* Forcibly unwire all pages. */
1085 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1086 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1091 #ifndef _SYS_SYSPROTO_H_
1092 struct munlock_args {
1103 struct munlock_args *uap;
1105 vm_offset_t addr, end, last, start;
1109 error = priv_check(td, PRIV_VM_MUNLOCK);
1112 addr = (vm_offset_t)uap->addr;
1115 start = trunc_page(addr);
1116 end = round_page(last);
1117 if (last < addr || end < addr)
1119 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1120 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1121 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1129 * Helper function for vm_mmap. Perform sanity check specific for mmap
1130 * operations on vnodes.
1133 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1134 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1135 struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1142 int error, flags, type;
1146 vfslocked = VFS_LOCK_GIANT(mp);
1147 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1148 VFS_UNLOCK_GIANT(vfslocked);
1153 if (vp->v_type == VREG) {
1155 * Get the proper underlying object
1161 if (obj->handle != vp) {
1163 vp = (struct vnode*)obj->handle;
1164 vget(vp, LK_EXCLUSIVE, td);
1168 } else if (vp->v_type == VCHR) {
1170 handle = vp->v_rdev;
1172 dsw = dev_refthread(handle);
1177 if (dsw->d_flags & D_MMAP_ANON) {
1178 dev_relthread(handle);
1179 *maxprotp = VM_PROT_ALL;
1180 *flagsp |= MAP_ANON;
1184 dev_relthread(handle);
1186 * cdevs does not provide private mappings of any kind.
1188 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1189 (prot & PROT_WRITE) != 0) {
1193 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1198 * Force device mappings to be shared.
1200 flags |= MAP_SHARED;
1205 if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1209 error = mac_check_vnode_mmap(td->td_ucred, vp, prot, flags);
1213 if ((flags & MAP_SHARED) != 0) {
1214 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1215 if (prot & PROT_WRITE) {
1219 *maxprotp &= ~VM_PROT_WRITE;
1223 * If it is a regular file without any references
1224 * we do not need to sync it.
1225 * Adjust object size to be the size of actual file.
1227 if (vp->v_type == VREG) {
1228 objsize = round_page(va.va_size);
1229 if (va.va_nlink == 0)
1230 flags |= MAP_NOSYNC;
1232 obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1234 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1239 vfs_mark_atime(vp, td);
1243 VFS_UNLOCK_GIANT(vfslocked);
1252 * Helper function for vm_mmap. Perform sanity check specific for mmap
1253 * operations on cdevs.
1256 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1257 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1258 struct cdev *cdev, vm_ooffset_t foff, vm_object_t *objp)
1266 dsw = dev_refthread(cdev);
1269 if (dsw->d_flags & D_MMAP_ANON) {
1270 dev_relthread(cdev);
1271 *maxprotp = VM_PROT_ALL;
1272 *flagsp |= MAP_ANON;
1275 dev_relthread(cdev);
1277 * cdevs does not provide private mappings of any kind.
1279 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1280 (prot & PROT_WRITE) != 0)
1282 if (flags & (MAP_PRIVATE|MAP_COPY))
1285 * Force device mappings to be shared.
1287 flags |= MAP_SHARED;
1289 error = mac_check_cdev_mmap(td->td_ucred, cdev, prot);
1293 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, foff);
1306 * Internal version of mmap. Currently used by mmap, exec, and sys5
1307 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1310 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1311 vm_prot_t maxprot, int flags,
1312 objtype_t handle_type, void *handle,
1316 vm_object_t object = NULL;
1317 int rv = KERN_SUCCESS;
1319 struct thread *td = curthread;
1324 size = round_page(size);
1326 PROC_LOCK(td->td_proc);
1327 if (td->td_proc->p_vmspace->vm_map.size + size >
1328 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1329 PROC_UNLOCK(td->td_proc);
1332 PROC_UNLOCK(td->td_proc);
1335 * We currently can only deal with page aligned file offsets.
1336 * The check is here rather than in the syscall because the
1337 * kernel calls this function internally for other mmaping
1338 * operations (such as in exec) and non-aligned offsets will
1339 * cause pmap inconsistencies...so we want to be sure to
1340 * disallow this in all cases.
1342 if (foff & PAGE_MASK)
1345 if ((flags & MAP_FIXED) == 0) {
1347 *addr = round_page(*addr);
1349 if (*addr != trunc_page(*addr))
1354 * Lookup/allocate object.
1356 switch (handle_type) {
1358 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1359 handle, foff, &object);
1362 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1363 handle, foff, &object);
1366 if (handle == NULL) {
1377 if (flags & MAP_ANON) {
1381 * Unnamed anonymous regions always start at 0.
1386 docow = MAP_PREFAULT_PARTIAL;
1389 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1390 docow |= MAP_COPY_ON_WRITE;
1391 if (flags & MAP_NOSYNC)
1392 docow |= MAP_DISABLE_SYNCER;
1393 if (flags & MAP_NOCORE)
1394 docow |= MAP_DISABLE_COREDUMP;
1396 #if defined(VM_PROT_READ_IS_EXEC)
1397 if (prot & VM_PROT_READ)
1398 prot |= VM_PROT_EXECUTE;
1400 if (maxprot & VM_PROT_READ)
1401 maxprot |= VM_PROT_EXECUTE;
1404 if (flags & MAP_STACK)
1405 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1406 docow | MAP_STACK_GROWS_DOWN);
1408 rv = vm_map_find(map, object, foff, addr, size,
1409 object != NULL && object->type == OBJT_DEVICE ?
1410 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1412 rv = vm_map_fixed(map, object, foff, *addr, size,
1413 prot, maxprot, docow);
1415 if (rv != KERN_SUCCESS) {
1417 * Lose the object reference. Will destroy the
1418 * object if it's an unnamed anonymous mapping
1419 * or named anonymous without other references.
1421 vm_object_deallocate(object);
1422 } else if (flags & MAP_SHARED) {
1424 * Shared memory is also shared with children.
1426 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1427 if (rv != KERN_SUCCESS)
1428 (void) vm_map_remove(map, *addr, *addr + size);
1432 * If the process has requested that all future mappings
1433 * be wired, then heed this.
1435 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1436 vm_map_wire(map, *addr, *addr + size,
1437 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1442 case KERN_INVALID_ADDRESS:
1445 case KERN_PROTECTION_FAILURE: