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/capsicum.h>
53 #include <sys/kernel.h>
55 #include <sys/mutex.h>
56 #include <sys/sysproto.h>
57 #include <sys/filedesc.h>
60 #include <sys/procctl.h>
61 #include <sys/racct.h>
62 #include <sys/resource.h>
63 #include <sys/resourcevar.h>
64 #include <sys/rwlock.h>
65 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
67 #include <sys/fcntl.h>
70 #include <sys/mount.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysent.h>
75 #include <sys/vmmeter.h>
77 #include <security/audit/audit.h>
78 #include <security/mac/mac_framework.h>
81 #include <vm/vm_param.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pager.h>
87 #include <vm/vm_pageout.h>
88 #include <vm/vm_extern.h>
89 #include <vm/vm_page.h>
90 #include <vm/vnode_pager.h>
93 #include <sys/pmckern.h>
97 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
98 "Do not apply RLIMIT_MEMLOCK on mlockall");
101 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
104 #ifndef _SYS_SYSPROTO_H_
117 struct sbrk_args *uap;
119 /* Not yet implemented */
123 #ifndef _SYS_SYSPROTO_H_
136 struct sstk_args *uap;
138 /* Not yet implemented */
142 #if defined(COMPAT_43)
143 #ifndef _SYS_SYSPROTO_H_
144 struct getpagesize_args {
150 ogetpagesize(td, uap)
152 struct getpagesize_args *uap;
155 td->td_retval[0] = PAGE_SIZE;
158 #endif /* COMPAT_43 */
162 * Memory Map (mmap) system call. Note that the file offset
163 * and address are allowed to be NOT page aligned, though if
164 * the MAP_FIXED flag it set, both must have the same remainder
165 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
166 * page-aligned, the actual mapping starts at trunc_page(addr)
167 * and the return value is adjusted up by the page offset.
169 * Generally speaking, only character devices which are themselves
170 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
171 * there would be no cache coherency between a descriptor and a VM mapping
172 * both to the same character device.
174 #ifndef _SYS_SYSPROTO_H_
192 struct mmap_args *uap;
196 vm_size_t size, pageoff;
197 vm_prot_t cap_maxprot;
198 int align, error, flags, prot;
200 struct vmspace *vms = td->td_proc->p_vmspace;
203 addr = (vm_offset_t) uap->addr;
210 AUDIT_ARG_FD(uap->fd);
213 * Ignore old flags that used to be defined but did not do anything.
215 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
218 * Enforce the constraints.
219 * Mapping of length 0 is only allowed for old binaries.
220 * Anonymous mapping shall specify -1 as filedescriptor and
221 * zero position for new code. Be nice to ancient a.out
222 * binaries and correct pos for anonymous mapping, since old
223 * ld.so sometimes issues anonymous map requests with non-zero
226 if (!SV_CURPROC_FLAG(SV_AOUT)) {
227 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
228 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
231 if ((flags & MAP_ANON) != 0)
235 if (flags & MAP_STACK) {
236 if ((uap->fd != -1) ||
237 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
242 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
243 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
248 MAP_ALIGNMENT_MASK)) != 0)
250 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
252 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
254 if (prot != PROT_NONE &&
255 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
259 * Align the file position to a page boundary,
260 * and save its page offset component.
262 pageoff = (pos & PAGE_MASK);
265 /* Adjust size for rounding (on both ends). */
266 size += pageoff; /* low end... */
267 size = (vm_size_t) round_page(size); /* hi end */
269 /* Ensure alignment is at least a page and fits in a pointer. */
270 align = flags & MAP_ALIGNMENT_MASK;
271 if (align != 0 && align != MAP_ALIGNED_SUPER &&
272 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
273 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
277 * Check for illegal addresses. Watch out for address wrap... Note
278 * that VM_*_ADDRESS are not constants due to casts (argh).
280 if (flags & MAP_FIXED) {
282 * The specified address must have the same remainder
283 * as the file offset taken modulo PAGE_SIZE, so it
284 * should be aligned after adjustment by pageoff.
287 if (addr & PAGE_MASK)
290 /* Address range must be all in user VM space. */
291 if (addr < vm_map_min(&vms->vm_map) ||
292 addr + size > vm_map_max(&vms->vm_map))
294 if (addr + size < addr)
297 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
299 } else if (flags & MAP_32BIT) {
301 * For MAP_32BIT, override the hint if it is too high and
302 * do not bother moving the mapping past the heap (since
303 * the heap is usually above 2GB).
305 if (addr + size > MAP_32BIT_MAX_ADDR)
310 * XXX for non-fixed mappings where no hint is provided or
311 * the hint would fall in the potential heap space,
312 * place it after the end of the largest possible heap.
314 * There should really be a pmap call to determine a reasonable
318 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
319 addr < round_page((vm_offset_t)vms->vm_daddr +
320 lim_max(td, RLIMIT_DATA))))
321 addr = round_page((vm_offset_t)vms->vm_daddr +
322 lim_max(td, RLIMIT_DATA));
326 * Return success without mapping anything for old
327 * binaries that request a page-aligned mapping of
328 * length 0. For modern binaries, this function
329 * returns an error earlier.
332 } else if (flags & MAP_ANON) {
334 * Mapping blank space is trivial.
336 * This relies on VM_PROT_* matching PROT_*.
338 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
339 VM_PROT_ALL, flags, NULL, pos, FALSE, td);
342 * Mapping file, get fp for validation and don't let the
343 * descriptor disappear on us if we block. Check capability
344 * rights, but also return the maximum rights to be combined
345 * with maxprot later.
347 cap_rights_init(&rights, CAP_MMAP);
348 if (prot & PROT_READ)
349 cap_rights_set(&rights, CAP_MMAP_R);
350 if ((flags & MAP_SHARED) != 0) {
351 if (prot & PROT_WRITE)
352 cap_rights_set(&rights, CAP_MMAP_W);
354 if (prot & PROT_EXEC)
355 cap_rights_set(&rights, CAP_MMAP_X);
356 error = fget_mmap(td, uap->fd, &rights, &cap_maxprot, &fp);
359 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
360 td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
365 /* This relies on VM_PROT_* matching PROT_*. */
366 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
367 cap_maxprot, flags, pos, td);
371 td->td_retval[0] = (register_t) (addr + pageoff);
379 #if defined(COMPAT_FREEBSD6)
381 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
383 struct mmap_args oargs;
385 oargs.addr = uap->addr;
386 oargs.len = uap->len;
387 oargs.prot = uap->prot;
388 oargs.flags = uap->flags;
390 oargs.pos = uap->pos;
391 return (sys_mmap(td, &oargs));
396 #ifndef _SYS_SYSPROTO_H_
409 struct ommap_args *uap;
411 struct mmap_args nargs;
412 static const char cvtbsdprot[8] = {
416 PROT_EXEC | PROT_WRITE,
418 PROT_EXEC | PROT_READ,
419 PROT_WRITE | PROT_READ,
420 PROT_EXEC | PROT_WRITE | PROT_READ,
423 #define OMAP_ANON 0x0002
424 #define OMAP_COPY 0x0020
425 #define OMAP_SHARED 0x0010
426 #define OMAP_FIXED 0x0100
428 nargs.addr = uap->addr;
429 nargs.len = uap->len;
430 nargs.prot = cvtbsdprot[uap->prot & 0x7];
431 #ifdef COMPAT_FREEBSD32
432 #if defined(__amd64__)
433 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
435 nargs.prot |= PROT_EXEC;
439 if (uap->flags & OMAP_ANON)
440 nargs.flags |= MAP_ANON;
441 if (uap->flags & OMAP_COPY)
442 nargs.flags |= MAP_COPY;
443 if (uap->flags & OMAP_SHARED)
444 nargs.flags |= MAP_SHARED;
446 nargs.flags |= MAP_PRIVATE;
447 if (uap->flags & OMAP_FIXED)
448 nargs.flags |= MAP_FIXED;
450 nargs.pos = uap->pos;
451 return (sys_mmap(td, &nargs));
453 #endif /* COMPAT_43 */
456 #ifndef _SYS_SYSPROTO_H_
469 struct msync_args *uap;
472 vm_size_t size, pageoff;
477 addr = (vm_offset_t) uap->addr;
481 pageoff = (addr & PAGE_MASK);
484 size = (vm_size_t) round_page(size);
485 if (addr + size < addr)
488 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
491 map = &td->td_proc->p_vmspace->vm_map;
494 * Clean the pages and interpret the return value.
496 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
497 (flags & MS_INVALIDATE) != 0);
501 case KERN_INVALID_ADDRESS:
503 case KERN_INVALID_ARGUMENT:
512 #ifndef _SYS_SYSPROTO_H_
524 struct munmap_args *uap;
527 struct pmckern_map_out pkm;
528 vm_map_entry_t entry;
532 vm_size_t size, pageoff;
535 addr = (vm_offset_t) uap->addr;
540 pageoff = (addr & PAGE_MASK);
543 size = (vm_size_t) round_page(size);
544 if (addr + size < addr)
548 * Check for illegal addresses. Watch out for address wrap...
550 map = &td->td_proc->p_vmspace->vm_map;
551 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
556 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
559 * Inform hwpmc if the address range being unmapped contains
560 * an executable region.
562 pkm.pm_address = (uintptr_t) NULL;
563 if (vm_map_lookup_entry(map, addr, &entry)) {
565 entry != &map->header && entry->start < addr + size;
566 entry = entry->next) {
567 if (vm_map_check_protection(map, entry->start,
568 entry->end, VM_PROT_EXECUTE) == TRUE) {
569 pkm.pm_address = (uintptr_t) addr;
570 pkm.pm_size = (size_t) size;
577 vm_map_delete(map, addr, addr + size);
580 if (__predict_false(pmc_handled)) {
581 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
582 vm_map_lock_downgrade(map);
583 if (pkm.pm_address != (uintptr_t) NULL)
584 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
585 vm_map_unlock_read(map);
590 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
594 #ifndef _SYS_SYSPROTO_H_
595 struct mprotect_args {
605 sys_mprotect(td, uap)
607 struct mprotect_args *uap;
610 vm_size_t size, pageoff;
613 addr = (vm_offset_t) uap->addr;
615 prot = uap->prot & VM_PROT_ALL;
617 pageoff = (addr & PAGE_MASK);
620 size = (vm_size_t) round_page(size);
621 if (addr + size < addr)
624 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
625 addr + size, prot, FALSE)) {
628 case KERN_PROTECTION_FAILURE:
630 case KERN_RESOURCE_SHORTAGE:
636 #ifndef _SYS_SYSPROTO_H_
637 struct minherit_args {
647 sys_minherit(td, uap)
649 struct minherit_args *uap;
652 vm_size_t size, pageoff;
653 vm_inherit_t inherit;
655 addr = (vm_offset_t)uap->addr;
657 inherit = uap->inherit;
659 pageoff = (addr & PAGE_MASK);
662 size = (vm_size_t) round_page(size);
663 if (addr + size < addr)
666 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
667 addr + size, inherit)) {
670 case KERN_PROTECTION_FAILURE:
676 #ifndef _SYS_SYSPROTO_H_
677 struct madvise_args {
690 struct madvise_args *uap;
692 vm_offset_t start, end;
697 * Check for our special case, advising the swap pager we are
700 if (uap->behav == MADV_PROTECT) {
702 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
703 PROC_SPROTECT, &flags));
707 * Check for illegal behavior
709 if (uap->behav < 0 || uap->behav > MADV_CORE)
712 * Check for illegal addresses. Watch out for address wrap... Note
713 * that VM_*_ADDRESS are not constants due to casts (argh).
715 map = &td->td_proc->p_vmspace->vm_map;
716 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
717 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
719 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
723 * Since this routine is only advisory, we default to conservative
726 start = trunc_page((vm_offset_t) uap->addr);
727 end = round_page((vm_offset_t) uap->addr + uap->len);
729 if (vm_map_madvise(map, start, end, uap->behav))
734 #ifndef _SYS_SYSPROTO_H_
735 struct mincore_args {
748 struct mincore_args *uap;
750 vm_offset_t addr, first_addr;
751 vm_offset_t end, cend;
756 int vecindex, lastvecindex;
757 vm_map_entry_t current;
758 vm_map_entry_t entry;
760 vm_paddr_t locked_pa;
764 unsigned int timestamp;
768 * Make sure that the addresses presented are valid for user
771 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
772 end = addr + (vm_size_t)round_page(uap->len);
773 map = &td->td_proc->p_vmspace->vm_map;
774 if (end > vm_map_max(map) || end < addr)
778 * Address of byte vector
782 pmap = vmspace_pmap(td->td_proc->p_vmspace);
784 vm_map_lock_read(map);
786 timestamp = map->timestamp;
788 if (!vm_map_lookup_entry(map, addr, &entry)) {
789 vm_map_unlock_read(map);
794 * Do this on a map entry basis so that if the pages are not
795 * in the current processes address space, we can easily look
796 * up the pages elsewhere.
799 for (current = entry;
800 (current != &map->header) && (current->start < end);
801 current = current->next) {
804 * check for contiguity
806 if (current->end < end &&
807 (entry->next == &map->header ||
808 current->next->start > current->end)) {
809 vm_map_unlock_read(map);
814 * ignore submaps (for now) or null objects
816 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
817 current->object.vm_object == NULL)
821 * limit this scan to the current map entry and the
822 * limits for the mincore call
824 if (addr < current->start)
825 addr = current->start;
831 * scan this entry one page at a time
833 while (addr < cend) {
835 * Check pmap first, it is likely faster, also
836 * it can provide info as to whether we are the
837 * one referencing or modifying the page.
843 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
844 if (locked_pa != 0) {
846 * The page is mapped by this process but not
847 * both accessed and modified. It is also
848 * managed. Acquire the object lock so that
849 * other mappings might be examined.
851 m = PHYS_TO_VM_PAGE(locked_pa);
852 if (m->object != object) {
854 VM_OBJECT_WUNLOCK(object);
856 locked = VM_OBJECT_TRYWLOCK(object);
859 VM_OBJECT_WLOCK(object);
865 KASSERT(m->valid == VM_PAGE_BITS_ALL,
866 ("mincore: page %p is mapped but invalid",
868 } else if (mincoreinfo == 0) {
870 * The page is not mapped by this process. If
871 * the object implements managed pages, then
872 * determine if the page is resident so that
873 * the mappings might be examined.
875 if (current->object.vm_object != object) {
877 VM_OBJECT_WUNLOCK(object);
878 object = current->object.vm_object;
879 VM_OBJECT_WLOCK(object);
881 if (object->type == OBJT_DEFAULT ||
882 object->type == OBJT_SWAP ||
883 object->type == OBJT_VNODE) {
884 pindex = OFF_TO_IDX(current->offset +
885 (addr - current->start));
886 m = vm_page_lookup(object, pindex);
887 if (m != NULL && m->valid == 0)
890 mincoreinfo = MINCORE_INCORE;
894 /* Examine other mappings to the page. */
895 if (m->dirty == 0 && pmap_is_modified(m))
898 mincoreinfo |= MINCORE_MODIFIED_OTHER;
900 * The first test for PGA_REFERENCED is an
901 * optimization. The second test is
902 * required because a concurrent pmap
903 * operation could clear the last reference
904 * and set PGA_REFERENCED before the call to
905 * pmap_is_referenced().
907 if ((m->aflags & PGA_REFERENCED) != 0 ||
908 pmap_is_referenced(m) ||
909 (m->aflags & PGA_REFERENCED) != 0)
910 mincoreinfo |= MINCORE_REFERENCED_OTHER;
913 VM_OBJECT_WUNLOCK(object);
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 * calculate index into user supplied byte vector
924 vecindex = OFF_TO_IDX(addr - first_addr);
927 * If we have skipped map entries, we need to make sure that
928 * the byte vector is zeroed for those skipped entries.
930 while ((lastvecindex + 1) < vecindex) {
932 error = subyte(vec + lastvecindex, 0);
940 * Pass the page information to the user
942 error = subyte(vec + vecindex, mincoreinfo);
949 * If the map has changed, due to the subyte, the previous
950 * output may be invalid.
952 vm_map_lock_read(map);
953 if (timestamp != map->timestamp)
956 lastvecindex = vecindex;
962 * subyte may page fault. In case it needs to modify
963 * the map, we release the lock.
965 vm_map_unlock_read(map);
968 * Zero the last entries in the byte vector.
970 vecindex = OFF_TO_IDX(end - first_addr);
971 while ((lastvecindex + 1) < vecindex) {
973 error = subyte(vec + lastvecindex, 0);
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)
987 vm_map_unlock_read(map);
992 #ifndef _SYS_SYSPROTO_H_
1004 struct mlock_args *uap;
1007 return (vm_mlock(td->td_proc, td->td_ucred, uap->addr, uap->len));
1011 vm_mlock(struct proc *proc, struct ucred *cred, const void *addr0, size_t len)
1013 vm_offset_t addr, end, last, start;
1014 vm_size_t npages, size;
1016 unsigned long nsize;
1019 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
1022 addr = (vm_offset_t)addr0;
1025 start = trunc_page(addr);
1026 end = round_page(last);
1027 if (last < addr || end < addr)
1029 npages = atop(end - start);
1030 if (npages > vm_page_max_wired)
1032 map = &proc->p_vmspace->vm_map;
1034 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1035 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1040 if (npages + vm_cnt.v_wire_count > vm_page_max_wired)
1045 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1051 error = vm_map_wire(map, start, end,
1052 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1054 if (racct_enable && error != KERN_SUCCESS) {
1056 racct_set(proc, RACCT_MEMLOCK,
1057 ptoa(pmap_wired_count(map->pmap)));
1061 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1064 #ifndef _SYS_SYSPROTO_H_
1065 struct mlockall_args {
1074 sys_mlockall(td, uap)
1076 struct mlockall_args *uap;
1081 map = &td->td_proc->p_vmspace->vm_map;
1082 error = priv_check(td, PRIV_VM_MLOCK);
1086 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1090 * If wiring all pages in the process would cause it to exceed
1091 * a hard resource limit, return ENOMEM.
1093 if (!old_mlock && uap->how & MCL_CURRENT) {
1094 PROC_LOCK(td->td_proc);
1095 if (map->size > lim_cur(td, RLIMIT_MEMLOCK)) {
1096 PROC_UNLOCK(td->td_proc);
1099 PROC_UNLOCK(td->td_proc);
1103 PROC_LOCK(td->td_proc);
1104 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1105 PROC_UNLOCK(td->td_proc);
1111 if (uap->how & MCL_FUTURE) {
1113 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1118 if (uap->how & MCL_CURRENT) {
1120 * P1003.1-2001 mandates that all currently mapped pages
1121 * will be memory resident and locked (wired) upon return
1122 * from mlockall(). vm_map_wire() will wire pages, by
1123 * calling vm_fault_wire() for each page in the region.
1125 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1126 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1127 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1130 if (racct_enable && error != KERN_SUCCESS) {
1131 PROC_LOCK(td->td_proc);
1132 racct_set(td->td_proc, RACCT_MEMLOCK,
1133 ptoa(pmap_wired_count(map->pmap)));
1134 PROC_UNLOCK(td->td_proc);
1141 #ifndef _SYS_SYSPROTO_H_
1142 struct munlockall_args {
1151 sys_munlockall(td, uap)
1153 struct munlockall_args *uap;
1158 map = &td->td_proc->p_vmspace->vm_map;
1159 error = priv_check(td, PRIV_VM_MUNLOCK);
1163 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1165 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1168 /* Forcibly unwire all pages. */
1169 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1170 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1172 if (racct_enable && error == KERN_SUCCESS) {
1173 PROC_LOCK(td->td_proc);
1174 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1175 PROC_UNLOCK(td->td_proc);
1182 #ifndef _SYS_SYSPROTO_H_
1183 struct munlock_args {
1192 sys_munlock(td, uap)
1194 struct munlock_args *uap;
1196 vm_offset_t addr, end, last, start;
1203 error = priv_check(td, PRIV_VM_MUNLOCK);
1206 addr = (vm_offset_t)uap->addr;
1209 start = trunc_page(addr);
1210 end = round_page(last);
1211 if (last < addr || end < addr)
1213 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1214 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1216 if (racct_enable && error == KERN_SUCCESS) {
1217 PROC_LOCK(td->td_proc);
1218 map = &td->td_proc->p_vmspace->vm_map;
1219 racct_set(td->td_proc, RACCT_MEMLOCK,
1220 ptoa(pmap_wired_count(map->pmap)));
1221 PROC_UNLOCK(td->td_proc);
1224 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1230 * Helper function for vm_mmap. Perform sanity check specific for mmap
1231 * operations on vnodes.
1234 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1235 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1236 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1237 boolean_t *writecounted)
1243 int error, flags, locktype;
1245 cred = td->td_ucred;
1246 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1247 locktype = LK_EXCLUSIVE;
1249 locktype = LK_SHARED;
1250 if ((error = vget(vp, locktype, td)) != 0)
1252 AUDIT_ARG_VNODE1(vp);
1256 if (vp->v_type == VREG) {
1258 * Get the proper underlying object
1264 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1266 vp = (struct vnode *)obj->handle;
1268 * Bypass filesystems obey the mpsafety of the
1269 * underlying fs. Tmpfs never bypasses.
1271 error = vget(vp, locktype, td);
1275 if (locktype == LK_EXCLUSIVE) {
1276 *writecounted = TRUE;
1277 vnode_pager_update_writecount(obj, 0, objsize);
1283 if ((error = VOP_GETATTR(vp, &va, cred)))
1286 /* This relies on VM_PROT_* matching PROT_*. */
1287 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1291 if ((flags & MAP_SHARED) != 0) {
1292 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1293 if (prot & VM_PROT_WRITE) {
1297 *maxprotp &= ~VM_PROT_WRITE;
1301 * If it is a regular file without any references
1302 * we do not need to sync it.
1303 * Adjust object size to be the size of actual file.
1305 objsize = round_page(va.va_size);
1306 if (va.va_nlink == 0)
1307 flags |= MAP_NOSYNC;
1308 if (obj->type == OBJT_VNODE) {
1309 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1316 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1317 ("wrong object type"));
1318 VM_OBJECT_WLOCK(obj);
1319 vm_object_reference_locked(obj);
1320 #if VM_NRESERVLEVEL > 0
1321 vm_object_color(obj, 0);
1323 VM_OBJECT_WUNLOCK(obj);
1328 vfs_mark_atime(vp, cred);
1331 if (error != 0 && *writecounted) {
1332 *writecounted = FALSE;
1333 vnode_pager_update_writecount(obj, objsize, 0);
1344 * Helper function for vm_mmap. Perform sanity check specific for mmap
1345 * operations on cdevs.
1348 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1349 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1350 vm_ooffset_t *foff, vm_object_t *objp)
1357 if (dsw->d_flags & D_MMAP_ANON) {
1360 *maxprotp = VM_PROT_ALL;
1361 *flagsp |= MAP_ANON;
1365 * cdevs do not provide private mappings of any kind.
1367 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1368 (prot & VM_PROT_WRITE) != 0)
1370 if (flags & (MAP_PRIVATE|MAP_COPY))
1373 * Force device mappings to be shared.
1375 flags |= MAP_SHARED;
1377 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1382 * First, try d_mmap_single(). If that is not implemented
1383 * (returns ENODEV), fall back to using the device pager.
1384 * Note that d_mmap_single() must return a reference to the
1385 * object (it needs to bump the reference count of the object
1386 * it returns somehow).
1388 * XXX assumes VM_PROT_* == PROT_*
1390 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1391 if (error != ENODEV)
1393 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1405 * Internal version of mmap used by exec, sys5 shared memory, and
1406 * various device drivers. Handle is either a vnode pointer, a
1407 * character device, or NULL for MAP_ANON.
1410 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1411 vm_prot_t maxprot, int flags,
1412 objtype_t handle_type, void *handle,
1416 struct thread *td = curthread;
1418 boolean_t writecounted;
1423 size = round_page(size);
1425 writecounted = FALSE;
1428 * Lookup/allocate object.
1430 switch (handle_type) {
1437 dsw = dev_refthread(cdev, &ref);
1440 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1441 dsw, &foff, &object);
1442 dev_relthread(cdev, ref);
1446 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1447 handle, &foff, &object, &writecounted);
1450 if (handle == NULL) {
1462 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1463 foff, writecounted, td);
1464 if (error != 0 && object != NULL) {
1466 * If this mapping was accounted for in the vnode's
1467 * writecount, then undo that now.
1470 vnode_pager_release_writecount(object, 0, size);
1471 vm_object_deallocate(object);
1477 * Internal version of mmap that maps a specific VM object into an
1478 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1481 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1482 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1483 boolean_t writecounted, struct thread *td)
1486 int docow, error, findspace, rv;
1488 if (map == &td->td_proc->p_vmspace->vm_map) {
1489 PROC_LOCK(td->td_proc);
1490 if (map->size + size > lim_cur_proc(td->td_proc, RLIMIT_VMEM)) {
1491 PROC_UNLOCK(td->td_proc);
1494 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1495 PROC_UNLOCK(td->td_proc);
1498 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1499 if (ptoa(pmap_wired_count(map->pmap)) + size >
1500 lim_cur_proc(td->td_proc, RLIMIT_MEMLOCK)) {
1501 racct_set_force(td->td_proc, RACCT_VMEM,
1503 PROC_UNLOCK(td->td_proc);
1506 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1507 ptoa(pmap_wired_count(map->pmap)) + size);
1509 racct_set_force(td->td_proc, RACCT_VMEM,
1511 PROC_UNLOCK(td->td_proc);
1515 PROC_UNLOCK(td->td_proc);
1519 * We currently can only deal with page aligned file offsets.
1520 * The mmap() system call already enforces this by subtracting
1521 * the page offset from the file offset, but checking here
1522 * catches errors in device drivers (e.g. d_single_mmap()
1523 * callbacks) and other internal mapping requests (such as in
1526 if (foff & PAGE_MASK)
1529 if ((flags & MAP_FIXED) == 0) {
1531 *addr = round_page(*addr);
1533 if (*addr != trunc_page(*addr))
1538 if (flags & MAP_ANON) {
1539 if (object != NULL || foff != 0)
1542 } else if (flags & MAP_PREFAULT_READ)
1543 docow = MAP_PREFAULT;
1545 docow = MAP_PREFAULT_PARTIAL;
1547 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1548 docow |= MAP_COPY_ON_WRITE;
1549 if (flags & MAP_NOSYNC)
1550 docow |= MAP_DISABLE_SYNCER;
1551 if (flags & MAP_NOCORE)
1552 docow |= MAP_DISABLE_COREDUMP;
1553 /* Shared memory is also shared with children. */
1554 if (flags & MAP_SHARED)
1555 docow |= MAP_INHERIT_SHARE;
1557 docow |= MAP_VN_WRITECOUNT;
1558 if (flags & MAP_STACK) {
1561 docow |= MAP_STACK_GROWS_DOWN;
1563 if ((flags & MAP_EXCL) != 0)
1564 docow |= MAP_CHECK_EXCL;
1567 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1568 findspace = VMFS_SUPER_SPACE;
1569 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1570 findspace = VMFS_ALIGNED_SPACE(flags >>
1571 MAP_ALIGNMENT_SHIFT);
1573 findspace = VMFS_OPTIMAL_SPACE;
1574 rv = vm_map_find(map, object, foff, addr, size,
1576 flags & MAP_32BIT ? MAP_32BIT_MAX_ADDR :
1578 0, findspace, prot, maxprot, docow);
1580 rv = vm_map_fixed(map, object, foff, *addr, size,
1581 prot, maxprot, docow);
1584 if (rv == KERN_SUCCESS) {
1586 * If the process has requested that all future mappings
1587 * be wired, then heed this.
1589 if (map->flags & MAP_WIREFUTURE) {
1590 vm_map_wire(map, *addr, *addr + size,
1591 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1592 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1595 return (vm_mmap_to_errno(rv));
1599 * Translate a Mach VM return code to zero on success or the appropriate errno
1603 vm_mmap_to_errno(int rv)
1609 case KERN_INVALID_ADDRESS:
1612 case KERN_PROTECTION_FAILURE: