2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
38 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
42 * Mapped file (mmap) interface to VM
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "opt_hwpmc_hooks.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/capsicum.h>
54 #include <sys/kernel.h>
56 #include <sys/mutex.h>
57 #include <sys/sysproto.h>
58 #include <sys/filedesc.h>
61 #include <sys/procctl.h>
62 #include <sys/racct.h>
63 #include <sys/resource.h>
64 #include <sys/resourcevar.h>
65 #include <sys/rwlock.h>
66 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
68 #include <sys/fcntl.h>
71 #include <sys/mount.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysent.h>
76 #include <sys/vmmeter.h>
77 #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
78 #include <machine/md_var.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_param.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_page.h>
94 #include <vm/vnode_pager.h>
97 #include <sys/pmckern.h>
101 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
102 "Do not apply RLIMIT_MEMLOCK on mlockall");
103 static int mincore_mapped = 1;
104 SYSCTL_INT(_vm, OID_AUTO, mincore_mapped, CTLFLAG_RWTUN, &mincore_mapped, 0,
105 "mincore reports mappings, not residency");
108 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
111 #ifndef _SYS_SYSPROTO_H_
118 sys_sbrk(struct thread *td, struct sbrk_args *uap)
120 /* Not yet implemented */
124 #ifndef _SYS_SYSPROTO_H_
131 sys_sstk(struct thread *td, struct sstk_args *uap)
133 /* Not yet implemented */
137 #if defined(COMPAT_43)
139 ogetpagesize(struct thread *td, struct ogetpagesize_args *uap)
142 td->td_retval[0] = PAGE_SIZE;
145 #endif /* COMPAT_43 */
149 * Memory Map (mmap) system call. Note that the file offset
150 * and address are allowed to be NOT page aligned, though if
151 * the MAP_FIXED flag it set, both must have the same remainder
152 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
153 * page-aligned, the actual mapping starts at trunc_page(addr)
154 * and the return value is adjusted up by the page offset.
156 * Generally speaking, only character devices which are themselves
157 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
158 * there would be no cache coherency between a descriptor and a VM mapping
159 * both to the same character device.
161 #ifndef _SYS_SYSPROTO_H_
174 sys_mmap(struct thread *td, struct mmap_args *uap)
177 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
178 uap->flags, uap->fd, uap->pos));
182 kern_mmap(struct thread *td, uintptr_t addr0, size_t size, int prot, int flags,
189 vm_prot_t cap_maxprot;
193 vms = td->td_proc->p_vmspace;
199 * Ignore old flags that used to be defined but did not do anything.
201 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
204 * Enforce the constraints.
205 * Mapping of length 0 is only allowed for old binaries.
206 * Anonymous mapping shall specify -1 as filedescriptor and
207 * zero position for new code. Be nice to ancient a.out
208 * binaries and correct pos for anonymous mapping, since old
209 * ld.so sometimes issues anonymous map requests with non-zero
212 if (!SV_CURPROC_FLAG(SV_AOUT)) {
213 if ((size == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
214 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
217 if ((flags & MAP_ANON) != 0)
221 if (flags & MAP_STACK) {
223 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
228 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
229 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
230 MAP_PREFAULT_READ | MAP_GUARD |
234 MAP_ALIGNMENT_MASK)) != 0)
236 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
238 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
240 if (prot != PROT_NONE &&
241 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
243 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
244 pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
248 MAP_ALIGNMENT_MASK)) != 0))
252 * Align the file position to a page boundary,
253 * and save its page offset component.
255 pageoff = (pos & PAGE_MASK);
258 /* Adjust size for rounding (on both ends). */
259 size += pageoff; /* low end... */
260 size = (vm_size_t) round_page(size); /* hi end */
262 /* Ensure alignment is at least a page and fits in a pointer. */
263 align = flags & MAP_ALIGNMENT_MASK;
264 if (align != 0 && align != MAP_ALIGNED_SUPER &&
265 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
266 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
270 * Check for illegal addresses. Watch out for address wrap... Note
271 * that VM_*_ADDRESS are not constants due to casts (argh).
273 if (flags & MAP_FIXED) {
275 * The specified address must have the same remainder
276 * as the file offset taken modulo PAGE_SIZE, so it
277 * should be aligned after adjustment by pageoff.
280 if (addr & PAGE_MASK)
283 /* Address range must be all in user VM space. */
284 if (addr < vm_map_min(&vms->vm_map) ||
285 addr + size > vm_map_max(&vms->vm_map))
287 if (addr + size < addr)
290 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
292 } else if (flags & MAP_32BIT) {
294 * For MAP_32BIT, override the hint if it is too high and
295 * do not bother moving the mapping past the heap (since
296 * the heap is usually above 2GB).
298 if (addr + size > MAP_32BIT_MAX_ADDR)
303 * XXX for non-fixed mappings where no hint is provided or
304 * the hint would fall in the potential heap space,
305 * place it after the end of the largest possible heap.
307 * There should really be a pmap call to determine a reasonable
311 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
312 addr < round_page((vm_offset_t)vms->vm_daddr +
313 lim_max(td, RLIMIT_DATA))))
314 addr = round_page((vm_offset_t)vms->vm_daddr +
315 lim_max(td, RLIMIT_DATA));
319 * Return success without mapping anything for old
320 * binaries that request a page-aligned mapping of
321 * length 0. For modern binaries, this function
322 * returns an error earlier.
325 } else if ((flags & MAP_GUARD) != 0) {
326 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
327 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
328 } else if ((flags & MAP_ANON) != 0) {
330 * Mapping blank space is trivial.
332 * This relies on VM_PROT_* matching PROT_*.
334 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
335 VM_PROT_ALL, flags, NULL, pos, FALSE, td);
338 * Mapping file, get fp for validation and don't let the
339 * descriptor disappear on us if we block. Check capability
340 * rights, but also return the maximum rights to be combined
341 * with maxprot later.
343 cap_rights_init(&rights, CAP_MMAP);
344 if (prot & PROT_READ)
345 cap_rights_set(&rights, CAP_MMAP_R);
346 if ((flags & MAP_SHARED) != 0) {
347 if (prot & PROT_WRITE)
348 cap_rights_set(&rights, CAP_MMAP_W);
350 if (prot & PROT_EXEC)
351 cap_rights_set(&rights, CAP_MMAP_X);
352 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
355 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
356 td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
361 /* This relies on VM_PROT_* matching PROT_*. */
362 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
363 cap_maxprot, flags, pos, td);
367 td->td_retval[0] = (register_t) (addr + pageoff);
375 #if defined(COMPAT_FREEBSD6)
377 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
380 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
381 uap->flags, uap->fd, uap->pos));
386 #ifndef _SYS_SYSPROTO_H_
397 ommap(struct thread *td, struct ommap_args *uap)
399 static const char cvtbsdprot[8] = {
403 PROT_EXEC | PROT_WRITE,
405 PROT_EXEC | PROT_READ,
406 PROT_WRITE | PROT_READ,
407 PROT_EXEC | PROT_WRITE | PROT_READ,
411 #define OMAP_ANON 0x0002
412 #define OMAP_COPY 0x0020
413 #define OMAP_SHARED 0x0010
414 #define OMAP_FIXED 0x0100
416 prot = cvtbsdprot[uap->prot & 0x7];
417 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
418 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
423 if (uap->flags & OMAP_ANON)
425 if (uap->flags & OMAP_COPY)
427 if (uap->flags & OMAP_SHARED)
430 flags |= MAP_PRIVATE;
431 if (uap->flags & OMAP_FIXED)
433 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
436 #endif /* COMPAT_43 */
439 #ifndef _SYS_SYSPROTO_H_
447 sys_msync(struct thread *td, struct msync_args *uap)
450 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
454 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
462 pageoff = (addr & PAGE_MASK);
465 size = (vm_size_t) round_page(size);
466 if (addr + size < addr)
469 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
472 map = &td->td_proc->p_vmspace->vm_map;
475 * Clean the pages and interpret the return value.
477 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
478 (flags & MS_INVALIDATE) != 0);
482 case KERN_INVALID_ADDRESS:
484 case KERN_INVALID_ARGUMENT:
493 #ifndef _SYS_SYSPROTO_H_
500 sys_munmap(struct thread *td, struct munmap_args *uap)
503 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
507 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
510 struct pmckern_map_out pkm;
511 vm_map_entry_t entry;
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 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
541 * Inform hwpmc if the address range being unmapped contains
542 * an executable region.
544 pkm.pm_address = (uintptr_t) NULL;
545 if (vm_map_lookup_entry(map, addr, &entry)) {
546 for (; entry->start < addr + size;
547 entry = entry->next) {
548 if (vm_map_check_protection(map, entry->start,
549 entry->end, VM_PROT_EXECUTE) == TRUE) {
550 pkm.pm_address = (uintptr_t) addr;
551 pkm.pm_size = (size_t) size;
558 vm_map_delete(map, addr, addr + size);
561 if (__predict_false(pmc_handled)) {
562 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
563 vm_map_lock_downgrade(map);
564 if (pkm.pm_address != (uintptr_t) NULL)
565 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
566 vm_map_unlock_read(map);
571 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
575 #ifndef _SYS_SYSPROTO_H_
576 struct mprotect_args {
583 sys_mprotect(struct thread *td, struct mprotect_args *uap)
586 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len, uap->prot));
590 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot)
596 prot = (prot & VM_PROT_ALL);
597 pageoff = (addr & PAGE_MASK);
600 size = (vm_size_t) round_page(size);
601 #ifdef COMPAT_FREEBSD32
602 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
603 if (((addr + size) & 0xffffffff) < addr)
607 if (addr + size < addr)
610 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
611 addr + size, prot, FALSE)) {
614 case KERN_PROTECTION_FAILURE:
616 case KERN_RESOURCE_SHORTAGE:
622 #ifndef _SYS_SYSPROTO_H_
623 struct minherit_args {
630 sys_minherit(struct thread *td, struct minherit_args *uap)
633 vm_size_t size, pageoff;
634 vm_inherit_t inherit;
636 addr = (vm_offset_t)uap->addr;
638 inherit = uap->inherit;
640 pageoff = (addr & PAGE_MASK);
643 size = (vm_size_t) round_page(size);
644 if (addr + size < addr)
647 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
648 addr + size, inherit)) {
651 case KERN_PROTECTION_FAILURE:
657 #ifndef _SYS_SYSPROTO_H_
658 struct madvise_args {
666 sys_madvise(struct thread *td, struct madvise_args *uap)
669 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
673 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
676 vm_offset_t addr, end, start;
680 * Check for our special case, advising the swap pager we are
683 if (behav == MADV_PROTECT) {
685 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
686 PROC_SPROTECT, &flags));
690 * Check for illegal addresses. Watch out for address wrap... Note
691 * that VM_*_ADDRESS are not constants due to casts (argh).
693 map = &td->td_proc->p_vmspace->vm_map;
695 if (addr < vm_map_min(map) || addr + len > vm_map_max(map))
697 if ((addr + len) < addr)
701 * Since this routine is only advisory, we default to conservative
704 start = trunc_page(addr);
705 end = round_page(addr + len);
708 * vm_map_madvise() checks for illegal values of behav.
710 return (vm_map_madvise(map, start, end, behav));
713 #ifndef _SYS_SYSPROTO_H_
714 struct mincore_args {
722 sys_mincore(struct thread *td, struct mincore_args *uap)
725 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
729 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
731 vm_offset_t addr, first_addr;
732 vm_offset_t end, cend;
736 int vecindex, lastvecindex;
737 vm_map_entry_t current;
738 vm_map_entry_t entry;
740 vm_paddr_t locked_pa;
744 unsigned int timestamp;
748 * Make sure that the addresses presented are valid for user
751 first_addr = addr = trunc_page(addr0);
752 end = addr + (vm_size_t)round_page(len);
753 map = &td->td_proc->p_vmspace->vm_map;
754 if (end > vm_map_max(map) || end < addr)
757 pmap = vmspace_pmap(td->td_proc->p_vmspace);
759 vm_map_lock_read(map);
761 timestamp = map->timestamp;
763 if (!vm_map_lookup_entry(map, addr, &entry)) {
764 vm_map_unlock_read(map);
769 * Do this on a map entry basis so that if the pages are not
770 * in the current processes address space, we can easily look
771 * up the pages elsewhere.
774 for (current = entry; current->start < end; current = current->next) {
777 * check for contiguity
779 if (current->end < end && current->next->start > current->end) {
780 vm_map_unlock_read(map);
785 * ignore submaps (for now) or null objects
787 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
788 current->object.vm_object == NULL)
792 * limit this scan to the current map entry and the
793 * limits for the mincore call
795 if (addr < current->start)
796 addr = current->start;
802 * scan this entry one page at a time
804 while (addr < cend) {
806 * Check pmap first, it is likely faster, also
807 * it can provide info as to whether we are the
808 * one referencing or modifying the page.
814 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
815 if (mincore_mapped) {
817 * We only care about this pmap's
818 * mapping of the page, if any.
820 if (locked_pa != 0) {
821 vm_page_unlock(PHYS_TO_VM_PAGE(
824 } else if (locked_pa != 0) {
826 * The page is mapped by this process but not
827 * both accessed and modified. It is also
828 * managed. Acquire the object lock so that
829 * other mappings might be examined.
831 m = PHYS_TO_VM_PAGE(locked_pa);
832 if (m->object != object) {
834 VM_OBJECT_WUNLOCK(object);
836 locked = VM_OBJECT_TRYWLOCK(object);
839 VM_OBJECT_WLOCK(object);
845 KASSERT(m->valid == VM_PAGE_BITS_ALL,
846 ("mincore: page %p is mapped but invalid",
848 } else if (mincoreinfo == 0) {
850 * The page is not mapped by this process. If
851 * the object implements managed pages, then
852 * determine if the page is resident so that
853 * the mappings might be examined.
855 if (current->object.vm_object != object) {
857 VM_OBJECT_WUNLOCK(object);
858 object = current->object.vm_object;
859 VM_OBJECT_WLOCK(object);
861 if (object->type == OBJT_DEFAULT ||
862 object->type == OBJT_SWAP ||
863 object->type == OBJT_VNODE) {
864 pindex = OFF_TO_IDX(current->offset +
865 (addr - current->start));
866 m = vm_page_lookup(object, pindex);
867 if (m != NULL && m->valid == 0)
870 mincoreinfo = MINCORE_INCORE;
874 /* Examine other mappings to the page. */
875 if (m->dirty == 0 && pmap_is_modified(m))
878 mincoreinfo |= MINCORE_MODIFIED_OTHER;
880 * The first test for PGA_REFERENCED is an
881 * optimization. The second test is
882 * required because a concurrent pmap
883 * operation could clear the last reference
884 * and set PGA_REFERENCED before the call to
885 * pmap_is_referenced().
887 if ((m->aflags & PGA_REFERENCED) != 0 ||
888 pmap_is_referenced(m) ||
889 (m->aflags & PGA_REFERENCED) != 0)
890 mincoreinfo |= MINCORE_REFERENCED_OTHER;
893 VM_OBJECT_WUNLOCK(object);
896 * subyte may page fault. In case it needs to modify
897 * the map, we release the lock.
899 vm_map_unlock_read(map);
902 * calculate index into user supplied byte vector
904 vecindex = atop(addr - first_addr);
907 * If we have skipped map entries, we need to make sure that
908 * the byte vector is zeroed for those skipped entries.
910 while ((lastvecindex + 1) < vecindex) {
912 error = subyte(vec + lastvecindex, 0);
920 * Pass the page information to the user
922 error = subyte(vec + vecindex, mincoreinfo);
929 * If the map has changed, due to the subyte, the previous
930 * output may be invalid.
932 vm_map_lock_read(map);
933 if (timestamp != map->timestamp)
936 lastvecindex = vecindex;
942 * subyte may page fault. In case it needs to modify
943 * the map, we release the lock.
945 vm_map_unlock_read(map);
948 * Zero the last entries in the byte vector.
950 vecindex = atop(end - first_addr);
951 while ((lastvecindex + 1) < vecindex) {
953 error = subyte(vec + lastvecindex, 0);
961 * If the map has changed, due to the subyte, the previous
962 * output may be invalid.
964 vm_map_lock_read(map);
965 if (timestamp != map->timestamp)
967 vm_map_unlock_read(map);
972 #ifndef _SYS_SYSPROTO_H_
979 sys_mlock(struct thread *td, struct mlock_args *uap)
982 return (kern_mlock(td->td_proc, td->td_ucred,
983 __DECONST(uintptr_t, uap->addr), uap->len));
987 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
989 vm_offset_t addr, end, last, start;
990 vm_size_t npages, size;
995 error = priv_check_cred(cred, PRIV_VM_MLOCK);
1001 start = trunc_page(addr);
1002 end = round_page(last);
1003 if (last < addr || end < addr)
1005 npages = atop(end - start);
1006 if (npages > vm_page_max_wired)
1008 map = &proc->p_vmspace->vm_map;
1010 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1011 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1016 if (npages + vm_wire_count() > vm_page_max_wired)
1021 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1027 error = vm_map_wire(map, start, end,
1028 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1030 if (racct_enable && error != KERN_SUCCESS) {
1032 racct_set(proc, RACCT_MEMLOCK,
1033 ptoa(pmap_wired_count(map->pmap)));
1037 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1040 #ifndef _SYS_SYSPROTO_H_
1041 struct mlockall_args {
1047 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1052 map = &td->td_proc->p_vmspace->vm_map;
1053 error = priv_check(td, PRIV_VM_MLOCK);
1057 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1061 * If wiring all pages in the process would cause it to exceed
1062 * a hard resource limit, return ENOMEM.
1064 if (!old_mlock && uap->how & MCL_CURRENT) {
1065 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1070 PROC_LOCK(td->td_proc);
1071 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1072 PROC_UNLOCK(td->td_proc);
1078 if (uap->how & MCL_FUTURE) {
1080 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1085 if (uap->how & MCL_CURRENT) {
1087 * P1003.1-2001 mandates that all currently mapped pages
1088 * will be memory resident and locked (wired) upon return
1089 * from mlockall(). vm_map_wire() will wire pages, by
1090 * calling vm_fault_wire() for each page in the region.
1092 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1093 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1094 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1097 if (racct_enable && error != KERN_SUCCESS) {
1098 PROC_LOCK(td->td_proc);
1099 racct_set(td->td_proc, RACCT_MEMLOCK,
1100 ptoa(pmap_wired_count(map->pmap)));
1101 PROC_UNLOCK(td->td_proc);
1108 #ifndef _SYS_SYSPROTO_H_
1109 struct munlockall_args {
1115 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1120 map = &td->td_proc->p_vmspace->vm_map;
1121 error = priv_check(td, PRIV_VM_MUNLOCK);
1125 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1127 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1130 /* Forcibly unwire all pages. */
1131 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1132 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1134 if (racct_enable && error == KERN_SUCCESS) {
1135 PROC_LOCK(td->td_proc);
1136 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1137 PROC_UNLOCK(td->td_proc);
1144 #ifndef _SYS_SYSPROTO_H_
1145 struct munlock_args {
1151 sys_munlock(struct thread *td, struct munlock_args *uap)
1154 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1158 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1160 vm_offset_t addr, end, last, start;
1166 error = priv_check(td, PRIV_VM_MUNLOCK);
1171 start = trunc_page(addr);
1172 end = round_page(last);
1173 if (last < addr || end < addr)
1175 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1176 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1178 if (racct_enable && error == KERN_SUCCESS) {
1179 PROC_LOCK(td->td_proc);
1180 map = &td->td_proc->p_vmspace->vm_map;
1181 racct_set(td->td_proc, RACCT_MEMLOCK,
1182 ptoa(pmap_wired_count(map->pmap)));
1183 PROC_UNLOCK(td->td_proc);
1186 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1192 * Helper function for vm_mmap. Perform sanity check specific for mmap
1193 * operations on vnodes.
1196 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1197 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1198 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1199 boolean_t *writecounted)
1205 int error, flags, locktype;
1207 cred = td->td_ucred;
1208 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1209 locktype = LK_EXCLUSIVE;
1211 locktype = LK_SHARED;
1212 if ((error = vget(vp, locktype, td)) != 0)
1214 AUDIT_ARG_VNODE1(vp);
1218 if (vp->v_type == VREG) {
1220 * Get the proper underlying object
1226 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1228 vp = (struct vnode *)obj->handle;
1230 * Bypass filesystems obey the mpsafety of the
1231 * underlying fs. Tmpfs never bypasses.
1233 error = vget(vp, locktype, td);
1237 if (locktype == LK_EXCLUSIVE) {
1238 *writecounted = TRUE;
1239 vnode_pager_update_writecount(obj, 0, objsize);
1245 if ((error = VOP_GETATTR(vp, &va, cred)))
1248 /* This relies on VM_PROT_* matching PROT_*. */
1249 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1253 if ((flags & MAP_SHARED) != 0) {
1254 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1255 if (prot & VM_PROT_WRITE) {
1259 *maxprotp &= ~VM_PROT_WRITE;
1263 * If it is a regular file without any references
1264 * we do not need to sync it.
1265 * Adjust object size to be the size of actual file.
1267 objsize = round_page(va.va_size);
1268 if (va.va_nlink == 0)
1269 flags |= MAP_NOSYNC;
1270 if (obj->type == OBJT_VNODE) {
1271 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1278 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1279 ("wrong object type"));
1280 VM_OBJECT_WLOCK(obj);
1281 vm_object_reference_locked(obj);
1282 #if VM_NRESERVLEVEL > 0
1283 vm_object_color(obj, 0);
1285 VM_OBJECT_WUNLOCK(obj);
1290 vfs_mark_atime(vp, cred);
1293 if (error != 0 && *writecounted) {
1294 *writecounted = FALSE;
1295 vnode_pager_update_writecount(obj, objsize, 0);
1304 * Helper function for vm_mmap. Perform sanity check specific for mmap
1305 * operations on cdevs.
1308 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1309 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1310 vm_ooffset_t *foff, vm_object_t *objp)
1317 if (dsw->d_flags & D_MMAP_ANON) {
1320 *maxprotp = VM_PROT_ALL;
1321 *flagsp |= MAP_ANON;
1325 * cdevs do not provide private mappings of any kind.
1327 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1328 (prot & VM_PROT_WRITE) != 0)
1330 if (flags & (MAP_PRIVATE|MAP_COPY))
1333 * Force device mappings to be shared.
1335 flags |= MAP_SHARED;
1337 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1342 * First, try d_mmap_single(). If that is not implemented
1343 * (returns ENODEV), fall back to using the device pager.
1344 * Note that d_mmap_single() must return a reference to the
1345 * object (it needs to bump the reference count of the object
1346 * it returns somehow).
1348 * XXX assumes VM_PROT_* == PROT_*
1350 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1351 if (error != ENODEV)
1353 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1365 * Internal version of mmap used by exec, sys5 shared memory, and
1366 * various device drivers. Handle is either a vnode pointer, a
1367 * character device, or NULL for MAP_ANON.
1370 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1371 vm_prot_t maxprot, int flags,
1372 objtype_t handle_type, void *handle,
1376 struct thread *td = curthread;
1378 boolean_t writecounted;
1383 size = round_page(size);
1385 writecounted = FALSE;
1388 * Lookup/allocate object.
1390 switch (handle_type) {
1397 dsw = dev_refthread(cdev, &ref);
1400 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1401 dsw, &foff, &object);
1402 dev_relthread(cdev, ref);
1406 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1407 handle, &foff, &object, &writecounted);
1410 if (handle == NULL) {
1422 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1423 foff, writecounted, td);
1424 if (error != 0 && object != NULL) {
1426 * If this mapping was accounted for in the vnode's
1427 * writecount, then undo that now.
1430 vnode_pager_release_writecount(object, 0, size);
1431 vm_object_deallocate(object);
1437 * Internal version of mmap that maps a specific VM object into an
1438 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1441 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1442 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1443 boolean_t writecounted, struct thread *td)
1445 boolean_t curmap, fitit;
1446 vm_offset_t max_addr;
1447 int docow, error, findspace, rv;
1449 curmap = map == &td->td_proc->p_vmspace->vm_map;
1451 RACCT_PROC_LOCK(td->td_proc);
1452 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1453 RACCT_PROC_UNLOCK(td->td_proc);
1456 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1457 RACCT_PROC_UNLOCK(td->td_proc);
1460 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1461 if (ptoa(pmap_wired_count(map->pmap)) + size >
1462 lim_cur(td, RLIMIT_MEMLOCK)) {
1463 racct_set_force(td->td_proc, RACCT_VMEM,
1465 RACCT_PROC_UNLOCK(td->td_proc);
1468 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1469 ptoa(pmap_wired_count(map->pmap)) + size);
1471 racct_set_force(td->td_proc, RACCT_VMEM,
1473 RACCT_PROC_UNLOCK(td->td_proc);
1477 RACCT_PROC_UNLOCK(td->td_proc);
1481 * We currently can only deal with page aligned file offsets.
1482 * The mmap() system call already enforces this by subtracting
1483 * the page offset from the file offset, but checking here
1484 * catches errors in device drivers (e.g. d_single_mmap()
1485 * callbacks) and other internal mapping requests (such as in
1488 if (foff & PAGE_MASK)
1491 if ((flags & MAP_FIXED) == 0) {
1493 *addr = round_page(*addr);
1495 if (*addr != trunc_page(*addr))
1500 if (flags & MAP_ANON) {
1501 if (object != NULL || foff != 0)
1504 } else if (flags & MAP_PREFAULT_READ)
1505 docow = MAP_PREFAULT;
1507 docow = MAP_PREFAULT_PARTIAL;
1509 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1510 docow |= MAP_COPY_ON_WRITE;
1511 if (flags & MAP_NOSYNC)
1512 docow |= MAP_DISABLE_SYNCER;
1513 if (flags & MAP_NOCORE)
1514 docow |= MAP_DISABLE_COREDUMP;
1515 /* Shared memory is also shared with children. */
1516 if (flags & MAP_SHARED)
1517 docow |= MAP_INHERIT_SHARE;
1519 docow |= MAP_VN_WRITECOUNT;
1520 if (flags & MAP_STACK) {
1523 docow |= MAP_STACK_GROWS_DOWN;
1525 if ((flags & MAP_EXCL) != 0)
1526 docow |= MAP_CHECK_EXCL;
1527 if ((flags & MAP_GUARD) != 0)
1528 docow |= MAP_CREATE_GUARD;
1531 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1532 findspace = VMFS_SUPER_SPACE;
1533 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1534 findspace = VMFS_ALIGNED_SPACE(flags >>
1535 MAP_ALIGNMENT_SHIFT);
1537 findspace = VMFS_OPTIMAL_SPACE;
1540 if ((flags & MAP_32BIT) != 0)
1541 max_addr = MAP_32BIT_MAX_ADDR;
1544 rv = vm_map_find_min(map, object, foff, addr, size,
1545 round_page((vm_offset_t)td->td_proc->p_vmspace->
1546 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1547 findspace, prot, maxprot, docow);
1549 rv = vm_map_find(map, object, foff, addr, size,
1550 max_addr, findspace, prot, maxprot, docow);
1553 rv = vm_map_fixed(map, object, foff, *addr, size,
1554 prot, maxprot, docow);
1557 if (rv == KERN_SUCCESS) {
1559 * If the process has requested that all future mappings
1560 * be wired, then heed this.
1562 if (map->flags & MAP_WIREFUTURE) {
1563 vm_map_wire(map, *addr, *addr + size,
1564 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1565 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1568 return (vm_mmap_to_errno(rv));
1572 * Translate a Mach VM return code to zero on success or the appropriate errno
1576 vm_mmap_to_errno(int rv)
1582 case KERN_INVALID_ADDRESS:
1585 case KERN_PROTECTION_FAILURE: