2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
40 * Mapped file (mmap) interface to VM
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
47 #include "opt_hwpmc_hooks.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capability.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/filedesc.h>
59 #include <sys/racct.h>
60 #include <sys/resource.h>
61 #include <sys/resourcevar.h>
62 #include <sys/sysctl.h>
63 #include <sys/vnode.h>
64 #include <sys/fcntl.h>
67 #include <sys/mount.h>
70 #include <sys/sysent.h>
71 #include <sys/vmmeter.h>
73 #include <security/mac/mac_framework.h>
76 #include <vm/vm_param.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vm_pageout.h>
83 #include <vm/vm_extern.h>
84 #include <vm/vm_page.h>
85 #include <vm/vnode_pager.h>
88 #include <sys/pmckern.h>
92 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RW | CTLFLAG_TUN, &old_mlock, 0,
93 "Do not apply RLIMIT_MEMLOCK on mlockall");
94 TUNABLE_INT("vm.old_mlock", &old_mlock);
96 #ifndef _SYS_SYSPROTO_H_
102 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
103 int *, struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
104 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
105 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
106 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
107 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
116 struct sbrk_args *uap;
118 /* Not yet implemented */
122 #ifndef _SYS_SYSPROTO_H_
135 struct sstk_args *uap;
137 /* Not yet implemented */
141 #if defined(COMPAT_43)
142 #ifndef _SYS_SYSPROTO_H_
143 struct getpagesize_args {
149 ogetpagesize(td, uap)
151 struct getpagesize_args *uap;
154 td->td_retval[0] = PAGE_SIZE;
157 #endif /* COMPAT_43 */
161 * Memory Map (mmap) system call. Note that the file offset
162 * and address are allowed to be NOT page aligned, though if
163 * the MAP_FIXED flag it set, both must have the same remainder
164 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
165 * page-aligned, the actual mapping starts at trunc_page(addr)
166 * and the return value is adjusted up by the page offset.
168 * Generally speaking, only character devices which are themselves
169 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
170 * there would be no cache coherency between a descriptor and a VM mapping
171 * both to the same character device.
173 #ifndef _SYS_SYSPROTO_H_
191 struct mmap_args *uap;
194 struct pmckern_map_in pkm;
199 vm_size_t size, pageoff;
200 vm_prot_t cap_maxprot, prot, maxprot;
202 objtype_t handle_type;
205 struct vmspace *vms = td->td_proc->p_vmspace;
208 addr = (vm_offset_t) uap->addr;
210 prot = uap->prot & VM_PROT_ALL;
217 * Enforce the constraints.
218 * Mapping of length 0 is only allowed for old binaries.
219 * Anonymous mapping shall specify -1 as filedescriptor and
220 * zero position for new code. Be nice to ancient a.out
221 * binaries and correct pos for anonymous mapping, since old
222 * ld.so sometimes issues anonymous map requests with non-zero
225 if (!SV_CURPROC_FLAG(SV_AOUT)) {
226 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
227 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
230 if ((flags & MAP_ANON) != 0)
234 if (flags & MAP_STACK) {
235 if ((uap->fd != -1) ||
236 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
243 * Align the file position to a page boundary,
244 * and save its page offset component.
246 pageoff = (pos & PAGE_MASK);
249 /* Adjust size for rounding (on both ends). */
250 size += pageoff; /* low end... */
251 size = (vm_size_t) round_page(size); /* hi end */
254 * Check for illegal addresses. Watch out for address wrap... Note
255 * that VM_*_ADDRESS are not constants due to casts (argh).
257 if (flags & MAP_FIXED) {
259 * The specified address must have the same remainder
260 * as the file offset taken modulo PAGE_SIZE, so it
261 * should be aligned after adjustment by pageoff.
264 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;
298 cap_maxprot = VM_PROT_ALL;
301 * Mapping file, get fp for validation and don't let the
302 * descriptor disappear on us if we block. Check capability
303 * rights, but also return the maximum rights to be combined
304 * with maxprot later.
307 if (prot & PROT_READ)
308 rights |= CAP_MMAP_R;
309 if ((flags & MAP_SHARED) != 0) {
310 if (prot & PROT_WRITE)
311 rights |= CAP_MMAP_W;
313 if (prot & PROT_EXEC)
314 rights |= CAP_MMAP_X;
315 if ((error = fget_mmap(td, uap->fd, rights, &cap_maxprot,
318 if (fp->f_type == DTYPE_SHM) {
320 handle_type = OBJT_SWAP;
321 maxprot = VM_PROT_NONE;
323 /* FREAD should always be set. */
324 if (fp->f_flag & FREAD)
325 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
326 if (fp->f_flag & FWRITE)
327 maxprot |= VM_PROT_WRITE;
330 if (fp->f_type != DTYPE_VNODE) {
334 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
335 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
337 * POSIX shared-memory objects are defined to have
338 * kernel persistence, and are not defined to support
339 * read(2)/write(2) -- or even open(2). Thus, we can
340 * use MAP_ASYNC to trade on-disk coherence for speed.
341 * The shm_open(3) library routine turns on the FPOSIXSHM
342 * flag to request this behavior.
344 if (fp->f_flag & FPOSIXSHM)
349 * Ensure that file and memory protections are
350 * compatible. Note that we only worry about
351 * writability if mapping is shared; in this case,
352 * current and max prot are dictated by the open file.
353 * XXX use the vnode instead? Problem is: what
354 * credentials do we use for determination? What if
355 * proc does a setuid?
357 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
358 maxprot = VM_PROT_NONE;
360 maxprot = VM_PROT_EXECUTE;
361 if (fp->f_flag & FREAD) {
362 maxprot |= VM_PROT_READ;
363 } else if (prot & PROT_READ) {
368 * If we are sharing potential changes (either via
369 * MAP_SHARED or via the implicit sharing of character
370 * device mappings), and we are trying to get write
371 * permission although we opened it without asking
374 if ((flags & MAP_SHARED) != 0) {
375 if ((fp->f_flag & FWRITE) != 0) {
376 maxprot |= VM_PROT_WRITE;
377 } else if ((prot & PROT_WRITE) != 0) {
381 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
382 maxprot |= VM_PROT_WRITE;
383 cap_maxprot |= VM_PROT_WRITE;
386 handle_type = OBJT_VNODE;
390 maxprot &= cap_maxprot;
391 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
392 flags, handle_type, handle, pos);
395 /* inform hwpmc(4) if an executable is being mapped */
396 if (error == 0 && handle_type == OBJT_VNODE &&
397 (prot & PROT_EXEC)) {
398 pkm.pm_file = handle;
399 pkm.pm_address = (uintptr_t) addr;
400 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
404 td->td_retval[0] = (register_t) (addr + pageoff);
413 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
415 struct mmap_args oargs;
417 oargs.addr = uap->addr;
418 oargs.len = uap->len;
419 oargs.prot = uap->prot;
420 oargs.flags = uap->flags;
422 oargs.pos = uap->pos;
423 return (sys_mmap(td, &oargs));
427 #ifndef _SYS_SYSPROTO_H_
440 struct ommap_args *uap;
442 struct mmap_args nargs;
443 static const char cvtbsdprot[8] = {
447 PROT_EXEC | PROT_WRITE,
449 PROT_EXEC | PROT_READ,
450 PROT_WRITE | PROT_READ,
451 PROT_EXEC | PROT_WRITE | PROT_READ,
454 #define OMAP_ANON 0x0002
455 #define OMAP_COPY 0x0020
456 #define OMAP_SHARED 0x0010
457 #define OMAP_FIXED 0x0100
459 nargs.addr = uap->addr;
460 nargs.len = uap->len;
461 nargs.prot = cvtbsdprot[uap->prot & 0x7];
462 #ifdef COMPAT_FREEBSD32
463 #if defined(__amd64__) || defined(__ia64__)
464 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
466 nargs.prot |= PROT_EXEC;
470 if (uap->flags & OMAP_ANON)
471 nargs.flags |= MAP_ANON;
472 if (uap->flags & OMAP_COPY)
473 nargs.flags |= MAP_COPY;
474 if (uap->flags & OMAP_SHARED)
475 nargs.flags |= MAP_SHARED;
477 nargs.flags |= MAP_PRIVATE;
478 if (uap->flags & OMAP_FIXED)
479 nargs.flags |= MAP_FIXED;
481 nargs.pos = uap->pos;
482 return (sys_mmap(td, &nargs));
484 #endif /* COMPAT_43 */
487 #ifndef _SYS_SYSPROTO_H_
500 struct msync_args *uap;
503 vm_size_t size, pageoff;
508 addr = (vm_offset_t) uap->addr;
512 pageoff = (addr & PAGE_MASK);
515 size = (vm_size_t) round_page(size);
516 if (addr + size < addr)
519 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
522 map = &td->td_proc->p_vmspace->vm_map;
525 * Clean the pages and interpret the return value.
527 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
528 (flags & MS_INVALIDATE) != 0);
532 case KERN_INVALID_ADDRESS:
533 return (EINVAL); /* Sun returns ENOMEM? */
534 case KERN_INVALID_ARGUMENT:
543 #ifndef _SYS_SYSPROTO_H_
555 struct munmap_args *uap;
558 struct pmckern_map_out pkm;
559 vm_map_entry_t entry;
562 vm_size_t size, pageoff;
565 addr = (vm_offset_t) uap->addr;
570 pageoff = (addr & PAGE_MASK);
573 size = (vm_size_t) round_page(size);
574 if (addr + size < addr)
578 * Check for illegal addresses. Watch out for address wrap...
580 map = &td->td_proc->p_vmspace->vm_map;
581 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
586 * Inform hwpmc if the address range being unmapped contains
587 * an executable region.
589 pkm.pm_address = (uintptr_t) NULL;
590 if (vm_map_lookup_entry(map, addr, &entry)) {
592 entry != &map->header && entry->start < addr + size;
593 entry = entry->next) {
594 if (vm_map_check_protection(map, entry->start,
595 entry->end, VM_PROT_EXECUTE) == TRUE) {
596 pkm.pm_address = (uintptr_t) addr;
597 pkm.pm_size = (size_t) size;
603 vm_map_delete(map, addr, addr + size);
606 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
607 vm_map_lock_downgrade(map);
608 if (pkm.pm_address != (uintptr_t) NULL)
609 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
610 vm_map_unlock_read(map);
614 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
618 #ifndef _SYS_SYSPROTO_H_
619 struct mprotect_args {
629 sys_mprotect(td, uap)
631 struct mprotect_args *uap;
634 vm_size_t size, pageoff;
637 addr = (vm_offset_t) uap->addr;
639 prot = uap->prot & VM_PROT_ALL;
641 pageoff = (addr & PAGE_MASK);
644 size = (vm_size_t) round_page(size);
645 if (addr + size < addr)
648 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
649 addr + size, prot, FALSE)) {
652 case KERN_PROTECTION_FAILURE:
654 case KERN_RESOURCE_SHORTAGE:
660 #ifndef _SYS_SYSPROTO_H_
661 struct minherit_args {
671 sys_minherit(td, uap)
673 struct minherit_args *uap;
676 vm_size_t size, pageoff;
677 vm_inherit_t inherit;
679 addr = (vm_offset_t)uap->addr;
681 inherit = uap->inherit;
683 pageoff = (addr & PAGE_MASK);
686 size = (vm_size_t) round_page(size);
687 if (addr + size < addr)
690 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
691 addr + size, inherit)) {
694 case KERN_PROTECTION_FAILURE:
700 #ifndef _SYS_SYSPROTO_H_
701 struct madvise_args {
714 struct madvise_args *uap;
716 vm_offset_t start, end;
722 * Check for our special case, advising the swap pager we are
725 if (uap->behav == MADV_PROTECT) {
726 error = priv_check(td, PRIV_VM_MADV_PROTECT);
730 p->p_flag |= P_PROTECTED;
736 * Check for illegal behavior
738 if (uap->behav < 0 || uap->behav > MADV_CORE)
741 * Check for illegal addresses. Watch out for address wrap... Note
742 * that VM_*_ADDRESS are not constants due to casts (argh).
744 map = &td->td_proc->p_vmspace->vm_map;
745 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
746 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
748 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
752 * Since this routine is only advisory, we default to conservative
755 start = trunc_page((vm_offset_t) uap->addr);
756 end = round_page((vm_offset_t) uap->addr + uap->len);
758 if (vm_map_madvise(map, start, end, uap->behav))
763 #ifndef _SYS_SYSPROTO_H_
764 struct mincore_args {
777 struct mincore_args *uap;
779 vm_offset_t addr, first_addr;
780 vm_offset_t end, cend;
785 int vecindex, lastvecindex;
786 vm_map_entry_t current;
787 vm_map_entry_t entry;
789 vm_paddr_t locked_pa;
793 unsigned int timestamp;
797 * Make sure that the addresses presented are valid for user
800 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
801 end = addr + (vm_size_t)round_page(uap->len);
802 map = &td->td_proc->p_vmspace->vm_map;
803 if (end > vm_map_max(map) || end < addr)
807 * Address of byte vector
811 pmap = vmspace_pmap(td->td_proc->p_vmspace);
813 vm_map_lock_read(map);
815 timestamp = map->timestamp;
817 if (!vm_map_lookup_entry(map, addr, &entry)) {
818 vm_map_unlock_read(map);
823 * Do this on a map entry basis so that if the pages are not
824 * in the current processes address space, we can easily look
825 * up the pages elsewhere.
828 for (current = entry;
829 (current != &map->header) && (current->start < end);
830 current = current->next) {
833 * check for contiguity
835 if (current->end < end &&
836 (entry->next == &map->header ||
837 current->next->start > current->end)) {
838 vm_map_unlock_read(map);
843 * ignore submaps (for now) or null objects
845 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
846 current->object.vm_object == NULL)
850 * limit this scan to the current map entry and the
851 * limits for the mincore call
853 if (addr < current->start)
854 addr = current->start;
860 * scan this entry one page at a time
862 while (addr < cend) {
864 * Check pmap first, it is likely faster, also
865 * it can provide info as to whether we are the
866 * one referencing or modifying the page.
872 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
873 if (locked_pa != 0) {
875 * The page is mapped by this process but not
876 * both accessed and modified. It is also
877 * managed. Acquire the object lock so that
878 * other mappings might be examined.
880 m = PHYS_TO_VM_PAGE(locked_pa);
881 if (m->object != object) {
883 VM_OBJECT_UNLOCK(object);
885 locked = VM_OBJECT_TRYLOCK(object);
888 VM_OBJECT_LOCK(object);
894 KASSERT(m->valid == VM_PAGE_BITS_ALL,
895 ("mincore: page %p is mapped but invalid",
897 } else if (mincoreinfo == 0) {
899 * The page is not mapped by this process. If
900 * the object implements managed pages, then
901 * determine if the page is resident so that
902 * the mappings might be examined.
904 if (current->object.vm_object != object) {
906 VM_OBJECT_UNLOCK(object);
907 object = current->object.vm_object;
908 VM_OBJECT_LOCK(object);
910 if (object->type == OBJT_DEFAULT ||
911 object->type == OBJT_SWAP ||
912 object->type == OBJT_VNODE) {
913 pindex = OFF_TO_IDX(current->offset +
914 (addr - current->start));
915 m = vm_page_lookup(object, pindex);
917 vm_page_is_cached(object, pindex))
918 mincoreinfo = MINCORE_INCORE;
919 if (m != NULL && m->valid == 0)
922 mincoreinfo = MINCORE_INCORE;
926 /* Examine other mappings to the page. */
927 if (m->dirty == 0 && pmap_is_modified(m))
930 mincoreinfo |= MINCORE_MODIFIED_OTHER;
932 * The first test for PGA_REFERENCED is an
933 * optimization. The second test is
934 * required because a concurrent pmap
935 * operation could clear the last reference
936 * and set PGA_REFERENCED before the call to
937 * pmap_is_referenced().
939 if ((m->aflags & PGA_REFERENCED) != 0 ||
940 pmap_is_referenced(m) ||
941 (m->aflags & PGA_REFERENCED) != 0)
942 mincoreinfo |= MINCORE_REFERENCED_OTHER;
945 VM_OBJECT_UNLOCK(object);
948 * subyte may page fault. In case it needs to modify
949 * the map, we release the lock.
951 vm_map_unlock_read(map);
954 * calculate index into user supplied byte vector
956 vecindex = OFF_TO_IDX(addr - first_addr);
959 * If we have skipped map entries, we need to make sure that
960 * the byte vector is zeroed for those skipped entries.
962 while ((lastvecindex + 1) < vecindex) {
963 error = subyte(vec + lastvecindex, 0);
972 * Pass the page information to the user
974 error = subyte(vec + vecindex, mincoreinfo);
981 * If the map has changed, due to the subyte, the previous
982 * output may be invalid.
984 vm_map_lock_read(map);
985 if (timestamp != map->timestamp)
988 lastvecindex = vecindex;
994 * subyte may page fault. In case it needs to modify
995 * the map, we release the lock.
997 vm_map_unlock_read(map);
1000 * Zero the last entries in the byte vector.
1002 vecindex = OFF_TO_IDX(end - first_addr);
1003 while ((lastvecindex + 1) < vecindex) {
1004 error = subyte(vec + lastvecindex, 0);
1013 * If the map has changed, due to the subyte, the previous
1014 * output may be invalid.
1016 vm_map_lock_read(map);
1017 if (timestamp != map->timestamp)
1019 vm_map_unlock_read(map);
1024 #ifndef _SYS_SYSPROTO_H_
1036 struct mlock_args *uap;
1039 vm_offset_t addr, end, last, start;
1040 vm_size_t npages, size;
1042 unsigned long nsize;
1045 error = priv_check(td, PRIV_VM_MLOCK);
1048 addr = (vm_offset_t)uap->addr;
1051 start = trunc_page(addr);
1052 end = round_page(last);
1053 if (last < addr || end < addr)
1055 npages = atop(end - start);
1056 if (npages > vm_page_max_wired)
1059 map = &proc->p_vmspace->vm_map;
1061 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1062 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1067 if (npages + cnt.v_wire_count > vm_page_max_wired)
1071 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1076 error = vm_map_wire(map, start, end,
1077 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1079 if (error != KERN_SUCCESS) {
1081 racct_set(proc, RACCT_MEMLOCK,
1082 ptoa(pmap_wired_count(map->pmap)));
1086 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1089 #ifndef _SYS_SYSPROTO_H_
1090 struct mlockall_args {
1099 sys_mlockall(td, uap)
1101 struct mlockall_args *uap;
1106 map = &td->td_proc->p_vmspace->vm_map;
1107 error = priv_check(td, PRIV_VM_MLOCK);
1111 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1115 * If wiring all pages in the process would cause it to exceed
1116 * a hard resource limit, return ENOMEM.
1118 if (!old_mlock && uap->how & MCL_CURRENT) {
1119 PROC_LOCK(td->td_proc);
1120 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1121 PROC_UNLOCK(td->td_proc);
1124 PROC_UNLOCK(td->td_proc);
1127 PROC_LOCK(td->td_proc);
1128 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1129 PROC_UNLOCK(td->td_proc);
1134 if (uap->how & MCL_FUTURE) {
1136 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1141 if (uap->how & MCL_CURRENT) {
1143 * P1003.1-2001 mandates that all currently mapped pages
1144 * will be memory resident and locked (wired) upon return
1145 * from mlockall(). vm_map_wire() will wire pages, by
1146 * calling vm_fault_wire() for each page in the region.
1148 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1149 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1150 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1153 if (error != KERN_SUCCESS) {
1154 PROC_LOCK(td->td_proc);
1155 racct_set(td->td_proc, RACCT_MEMLOCK,
1156 ptoa(pmap_wired_count(map->pmap)));
1157 PROC_UNLOCK(td->td_proc);
1164 #ifndef _SYS_SYSPROTO_H_
1165 struct munlockall_args {
1174 sys_munlockall(td, uap)
1176 struct munlockall_args *uap;
1181 map = &td->td_proc->p_vmspace->vm_map;
1182 error = priv_check(td, PRIV_VM_MUNLOCK);
1186 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1188 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1191 /* Forcibly unwire all pages. */
1192 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1193 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1195 if (error == KERN_SUCCESS) {
1196 PROC_LOCK(td->td_proc);
1197 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1198 PROC_UNLOCK(td->td_proc);
1205 #ifndef _SYS_SYSPROTO_H_
1206 struct munlock_args {
1215 sys_munlock(td, uap)
1217 struct munlock_args *uap;
1219 vm_offset_t addr, end, last, start;
1223 error = priv_check(td, PRIV_VM_MUNLOCK);
1226 addr = (vm_offset_t)uap->addr;
1229 start = trunc_page(addr);
1230 end = round_page(last);
1231 if (last < addr || end < addr)
1233 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1234 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1236 if (error == KERN_SUCCESS) {
1237 PROC_LOCK(td->td_proc);
1238 racct_sub(td->td_proc, RACCT_MEMLOCK, ptoa(end - start));
1239 PROC_UNLOCK(td->td_proc);
1242 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1248 * Helper function for vm_mmap. Perform sanity check specific for mmap
1249 * operations on vnodes.
1251 * For VCHR vnodes, the vnode lock is held over the call to
1252 * vm_mmap_cdev() to keep vp->v_rdev valid.
1255 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1256 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1257 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1258 boolean_t *writecounted)
1265 int error, flags, locktype;
1268 cred = td->td_ucred;
1269 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1270 locktype = LK_EXCLUSIVE;
1272 locktype = LK_SHARED;
1273 if ((error = vget(vp, locktype, td)) != 0)
1278 if (vp->v_type == VREG) {
1280 * Get the proper underlying object
1286 if (obj->handle != vp) {
1288 vp = (struct vnode *)obj->handle;
1290 * Bypass filesystems obey the mpsafety of the
1293 error = vget(vp, locktype, td);
1297 if (locktype == LK_EXCLUSIVE) {
1298 *writecounted = TRUE;
1299 vnode_pager_update_writecount(obj, 0, objsize);
1301 } else if (vp->v_type == VCHR) {
1302 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1303 vp->v_rdev, foffp, objp);
1311 if ((error = VOP_GETATTR(vp, &va, cred)))
1314 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1318 if ((flags & MAP_SHARED) != 0) {
1319 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1320 if (prot & PROT_WRITE) {
1324 *maxprotp &= ~VM_PROT_WRITE;
1328 * If it is a regular file without any references
1329 * we do not need to sync it.
1330 * Adjust object size to be the size of actual file.
1332 objsize = round_page(va.va_size);
1333 if (va.va_nlink == 0)
1334 flags |= MAP_NOSYNC;
1335 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, cred);
1344 vfs_mark_atime(vp, cred);
1356 * Helper function for vm_mmap. Perform sanity check specific for mmap
1357 * operations on cdevs.
1360 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1361 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1362 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1366 int error, flags, ref;
1370 dsw = dev_refthread(cdev, &ref);
1373 if (dsw->d_flags & D_MMAP_ANON) {
1374 dev_relthread(cdev, ref);
1375 *maxprotp = VM_PROT_ALL;
1376 *flagsp |= MAP_ANON;
1380 * cdevs do not provide private mappings of any kind.
1382 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1383 (prot & PROT_WRITE) != 0) {
1384 dev_relthread(cdev, ref);
1387 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1388 dev_relthread(cdev, ref);
1392 * Force device mappings to be shared.
1394 flags |= MAP_SHARED;
1396 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1398 dev_relthread(cdev, ref);
1403 * First, try d_mmap_single(). If that is not implemented
1404 * (returns ENODEV), fall back to using the device pager.
1405 * Note that d_mmap_single() must return a reference to the
1406 * object (it needs to bump the reference count of the object
1407 * it returns somehow).
1409 * XXX assumes VM_PROT_* == PROT_*
1411 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1412 dev_relthread(cdev, ref);
1413 if (error != ENODEV)
1415 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1429 * Helper function for vm_mmap. Perform sanity check specific for mmap
1430 * operations on shm file descriptors.
1433 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1434 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1435 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1439 if ((*flagsp & MAP_SHARED) != 0 &&
1440 (*maxprotp & VM_PROT_WRITE) == 0 &&
1441 (prot & PROT_WRITE) != 0)
1444 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1448 error = shm_mmap(shmfd, objsize, foff, objp);
1459 * Internal version of mmap. Currently used by mmap, exec, and sys5
1460 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1463 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1464 vm_prot_t maxprot, int flags,
1465 objtype_t handle_type, void *handle,
1469 vm_object_t object = NULL;
1470 struct thread *td = curthread;
1471 int docow, error, rv;
1472 boolean_t writecounted;
1477 size = round_page(size);
1479 if (map == &td->td_proc->p_vmspace->vm_map) {
1480 PROC_LOCK(td->td_proc);
1481 if (map->size + size > lim_cur(td->td_proc, RLIMIT_VMEM)) {
1482 PROC_UNLOCK(td->td_proc);
1485 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1486 PROC_UNLOCK(td->td_proc);
1489 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1490 if (ptoa(pmap_wired_count(map->pmap)) + size >
1491 lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1492 racct_set_force(td->td_proc, RACCT_VMEM,
1494 PROC_UNLOCK(td->td_proc);
1497 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1498 ptoa(pmap_wired_count(map->pmap)) + size);
1500 racct_set_force(td->td_proc, RACCT_VMEM,
1502 PROC_UNLOCK(td->td_proc);
1506 PROC_UNLOCK(td->td_proc);
1510 * We currently can only deal with page aligned file offsets.
1511 * The check is here rather than in the syscall because the
1512 * kernel calls this function internally for other mmaping
1513 * operations (such as in exec) and non-aligned offsets will
1514 * cause pmap inconsistencies...so we want to be sure to
1515 * disallow this in all cases.
1517 if (foff & PAGE_MASK)
1520 if ((flags & MAP_FIXED) == 0) {
1522 *addr = round_page(*addr);
1524 if (*addr != trunc_page(*addr))
1528 writecounted = FALSE;
1531 * Lookup/allocate object.
1533 switch (handle_type) {
1535 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1536 handle, &foff, &object);
1539 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1540 handle, &foff, &object, &writecounted);
1543 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1544 handle, foff, &object);
1547 if (handle == NULL) {
1558 if (flags & MAP_ANON) {
1562 * Unnamed anonymous regions always start at 0.
1566 } else if (flags & MAP_PREFAULT_READ)
1567 docow = MAP_PREFAULT;
1569 docow = MAP_PREFAULT_PARTIAL;
1571 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1572 docow |= MAP_COPY_ON_WRITE;
1573 if (flags & MAP_NOSYNC)
1574 docow |= MAP_DISABLE_SYNCER;
1575 if (flags & MAP_NOCORE)
1576 docow |= MAP_DISABLE_COREDUMP;
1577 /* Shared memory is also shared with children. */
1578 if (flags & MAP_SHARED)
1579 docow |= MAP_INHERIT_SHARE;
1581 docow |= MAP_VN_WRITECOUNT;
1583 if (flags & MAP_STACK)
1584 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1585 docow | MAP_STACK_GROWS_DOWN);
1587 rv = vm_map_find(map, object, foff, addr, size,
1588 object != NULL && object->type == OBJT_DEVICE ?
1589 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1591 rv = vm_map_fixed(map, object, foff, *addr, size,
1592 prot, maxprot, docow);
1594 if (rv == KERN_SUCCESS) {
1596 * If the process has requested that all future mappings
1597 * be wired, then heed this.
1599 if (map->flags & MAP_WIREFUTURE) {
1600 vm_map_wire(map, *addr, *addr + size,
1601 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1602 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1606 * If this mapping was accounted for in the vnode's
1607 * writecount, then undo that now.
1610 vnode_pager_release_writecount(object, 0, size);
1612 * Lose the object reference. Will destroy the
1613 * object if it's an unnamed anonymous mapping
1614 * or named anonymous without other references.
1616 vm_object_deallocate(object);
1618 return (vm_mmap_to_errno(rv));
1622 * Translate a Mach VM return code to zero on success or the appropriate errno
1626 vm_mmap_to_errno(int rv)
1632 case KERN_INVALID_ADDRESS:
1635 case KERN_PROTECTION_FAILURE: