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>
59 #include <sys/filedesc.h>
62 #include <sys/procctl.h>
63 #include <sys/racct.h>
64 #include <sys/resource.h>
65 #include <sys/resourcevar.h>
66 #include <sys/rwlock.h>
67 #include <sys/sysctl.h>
68 #include <sys/vnode.h>
69 #include <sys/fcntl.h>
72 #include <sys/mount.h>
75 #include <sys/syscallsubr.h>
76 #include <sys/sysent.h>
77 #include <sys/vmmeter.h>
78 #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
79 #include <machine/md_var.h>
82 #include <security/audit/audit.h>
83 #include <security/mac/mac_framework.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pager.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_extern.h>
94 #include <vm/vm_page.h>
95 #include <vm/vnode_pager.h>
98 #include <sys/pmckern.h>
102 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
103 "Do not apply RLIMIT_MEMLOCK on mlockall");
104 static int mincore_mapped = 1;
105 SYSCTL_INT(_vm, OID_AUTO, mincore_mapped, CTLFLAG_RWTUN, &mincore_mapped, 0,
106 "mincore reports mappings, not residency");
107 static int imply_prot_max = 0;
108 SYSCTL_INT(_vm, OID_AUTO, imply_prot_max, CTLFLAG_RWTUN, &imply_prot_max, 0,
109 "Imply maximum page protections in mmap() when none are specified");
112 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
115 _Static_assert(MAXPAGESIZES <= 4, "MINCORE_SUPER too narrow");
117 #ifndef _SYS_SYSPROTO_H_
124 sys_sbrk(struct thread *td, struct sbrk_args *uap)
126 /* Not yet implemented */
130 #ifndef _SYS_SYSPROTO_H_
137 sys_sstk(struct thread *td, struct sstk_args *uap)
139 /* Not yet implemented */
143 #if defined(COMPAT_43)
145 ogetpagesize(struct thread *td, struct ogetpagesize_args *uap)
148 td->td_retval[0] = PAGE_SIZE;
151 #endif /* COMPAT_43 */
154 * Memory Map (mmap) system call. Note that the file offset
155 * and address are allowed to be NOT page aligned, though if
156 * the MAP_FIXED flag it set, both must have the same remainder
157 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
158 * page-aligned, the actual mapping starts at trunc_page(addr)
159 * and the return value is adjusted up by the page offset.
161 * Generally speaking, only character devices which are themselves
162 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
163 * there would be no cache coherency between a descriptor and a VM mapping
164 * both to the same character device.
166 #ifndef _SYS_SYSPROTO_H_
179 sys_mmap(struct thread *td, struct mmap_args *uap)
182 return (kern_mmap(td, &(struct mmap_req){
183 .mr_hint = (uintptr_t)uap->addr,
185 .mr_prot = uap->prot,
186 .mr_flags = uap->flags,
193 kern_mmap_maxprot(struct proc *p, int prot)
196 if ((p->p_flag2 & P2_PROTMAX_DISABLE) != 0 ||
197 (p->p_fctl0 & NT_FREEBSD_FCTL_PROTMAX_DISABLE) != 0)
199 if (((p->p_flag2 & P2_PROTMAX_ENABLE) != 0 || imply_prot_max) &&
206 kern_mmap(struct thread *td, const struct mmap_req *mrp)
212 vm_offset_t addr, orig_addr;
213 vm_size_t len, pageoff, size;
214 vm_prot_t cap_maxprot;
215 int align, error, fd, flags, max_prot, prot;
217 mmap_check_fp_fn check_fp_fn;
219 orig_addr = addr = mrp->mr_hint;
222 flags = mrp->mr_flags;
225 check_fp_fn = mrp->mr_check_fp_fn;
227 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
229 max_prot = PROT_MAX_EXTRACT(prot);
230 prot = PROT_EXTRACT(prot);
231 if (max_prot != 0 && (max_prot & prot) != prot)
237 * Always honor PROT_MAX if set. If not, default to all
238 * permissions unless we're implying maximum permissions.
241 max_prot = kern_mmap_maxprot(p, prot);
248 * Ignore old flags that used to be defined but did not do anything.
250 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
253 * Enforce the constraints.
254 * Mapping of length 0 is only allowed for old binaries.
255 * Anonymous mapping shall specify -1 as filedescriptor and
256 * zero position for new code. Be nice to ancient a.out
257 * binaries and correct pos for anonymous mapping, since old
258 * ld.so sometimes issues anonymous map requests with non-zero
261 if (!SV_CURPROC_FLAG(SV_AOUT)) {
262 if ((len == 0 && p->p_osrel >= P_OSREL_MAP_ANON) ||
263 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
266 if ((flags & MAP_ANON) != 0)
270 if (flags & MAP_STACK) {
272 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
277 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
278 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
279 MAP_PREFAULT_READ | MAP_GUARD |
283 MAP_ALIGNMENT_MASK)) != 0)
285 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
287 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
289 if (prot != PROT_NONE &&
290 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
292 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
293 pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
297 MAP_ALIGNMENT_MASK)) != 0))
301 * Align the file position to a page boundary,
302 * and save its page offset component.
304 pageoff = (pos & PAGE_MASK);
307 /* Compute size from len by rounding (on both ends). */
308 size = len + pageoff; /* low end... */
309 size = round_page(size); /* hi end */
310 /* Check for rounding up to zero. */
314 /* Ensure alignment is at least a page and fits in a pointer. */
315 align = flags & MAP_ALIGNMENT_MASK;
316 if (align != 0 && align != MAP_ALIGNED_SUPER &&
317 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
318 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
322 * Check for illegal addresses. Watch out for address wrap... Note
323 * that VM_*_ADDRESS are not constants due to casts (argh).
325 if (flags & MAP_FIXED) {
327 * The specified address must have the same remainder
328 * as the file offset taken modulo PAGE_SIZE, so it
329 * should be aligned after adjustment by pageoff.
332 if (addr & PAGE_MASK)
335 /* Address range must be all in user VM space. */
336 if (!vm_map_range_valid(&vms->vm_map, addr, addr + size))
339 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
341 } else if (flags & MAP_32BIT) {
343 * For MAP_32BIT, override the hint if it is too high and
344 * do not bother moving the mapping past the heap (since
345 * the heap is usually above 2GB).
347 if (addr + size > MAP_32BIT_MAX_ADDR)
352 * XXX for non-fixed mappings where no hint is provided or
353 * the hint would fall in the potential heap space,
354 * place it after the end of the largest possible heap.
356 * There should really be a pmap call to determine a reasonable
360 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
361 addr < round_page((vm_offset_t)vms->vm_daddr +
362 lim_max(td, RLIMIT_DATA))))
363 addr = round_page((vm_offset_t)vms->vm_daddr +
364 lim_max(td, RLIMIT_DATA));
368 * Return success without mapping anything for old
369 * binaries that request a page-aligned mapping of
370 * length 0. For modern binaries, this function
371 * returns an error earlier.
374 } else if ((flags & MAP_GUARD) != 0) {
375 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
376 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
377 } else if ((flags & MAP_ANON) != 0) {
379 * Mapping blank space is trivial.
381 * This relies on VM_PROT_* matching PROT_*.
383 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
384 max_prot, flags, NULL, pos, FALSE, td);
387 * Mapping file, get fp for validation and don't let the
388 * descriptor disappear on us if we block. Check capability
389 * rights, but also return the maximum rights to be combined
390 * with maxprot later.
392 cap_rights_init_one(&rights, CAP_MMAP);
393 if (prot & PROT_READ)
394 cap_rights_set_one(&rights, CAP_MMAP_R);
395 if ((flags & MAP_SHARED) != 0) {
396 if (prot & PROT_WRITE)
397 cap_rights_set_one(&rights, CAP_MMAP_W);
399 if (prot & PROT_EXEC)
400 cap_rights_set_one(&rights, CAP_MMAP_X);
401 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
404 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
405 p->p_osrel >= P_OSREL_MAP_FSTRICT) {
409 if (check_fp_fn != NULL) {
410 error = check_fp_fn(fp, prot, max_prot & cap_maxprot,
415 if (fp->f_ops == &shm_ops && shm_largepage(fp->f_data))
417 /* This relies on VM_PROT_* matching PROT_*. */
418 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
419 max_prot & cap_maxprot, flags, pos, td);
423 td->td_retval[0] = (register_t) (addr + pageoff);
431 #if defined(COMPAT_FREEBSD6)
433 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
435 return (kern_mmap(td, &(struct mmap_req){
436 .mr_hint = (uintptr_t)uap->addr,
438 .mr_prot = uap->prot,
439 .mr_flags = uap->flags,
447 #ifndef _SYS_SYSPROTO_H_
458 ommap(struct thread *td, struct ommap_args *uap)
460 static const char cvtbsdprot[8] = {
464 PROT_EXEC | PROT_WRITE,
466 PROT_EXEC | PROT_READ,
467 PROT_WRITE | PROT_READ,
468 PROT_EXEC | PROT_WRITE | PROT_READ,
472 #define OMAP_ANON 0x0002
473 #define OMAP_COPY 0x0020
474 #define OMAP_SHARED 0x0010
475 #define OMAP_FIXED 0x0100
477 prot = cvtbsdprot[uap->prot & 0x7];
478 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
479 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
484 if (uap->flags & OMAP_ANON)
486 if (uap->flags & OMAP_COPY)
488 if (uap->flags & OMAP_SHARED)
491 flags |= MAP_PRIVATE;
492 if (uap->flags & OMAP_FIXED)
494 return (kern_mmap(td, &(struct mmap_req){
495 .mr_hint = (uintptr_t)uap->addr,
503 #endif /* COMPAT_43 */
505 #ifndef _SYS_SYSPROTO_H_
513 sys_msync(struct thread *td, struct msync_args *uap)
516 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
520 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
528 pageoff = (addr & PAGE_MASK);
531 size = (vm_size_t) round_page(size);
532 if (addr + size < addr)
535 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
538 map = &td->td_proc->p_vmspace->vm_map;
541 * Clean the pages and interpret the return value.
543 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
544 (flags & MS_INVALIDATE) != 0);
548 case KERN_INVALID_ADDRESS:
550 case KERN_INVALID_ARGUMENT:
559 #ifndef _SYS_SYSPROTO_H_
566 sys_munmap(struct thread *td, struct munmap_args *uap)
569 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
573 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
576 struct pmckern_map_out pkm;
577 vm_map_entry_t entry;
580 vm_offset_t addr, end;
589 pageoff = (addr & PAGE_MASK);
592 size = (vm_size_t) round_page(size);
594 map = &td->td_proc->p_vmspace->vm_map;
595 if (!vm_map_range_valid(map, addr, end))
601 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
604 * Inform hwpmc if the address range being unmapped contains
605 * an executable region.
607 pkm.pm_address = (uintptr_t) NULL;
608 if (vm_map_lookup_entry(map, addr, &entry)) {
609 for (; entry->start < end;
610 entry = vm_map_entry_succ(entry)) {
611 if (vm_map_check_protection(map, entry->start,
612 entry->end, VM_PROT_EXECUTE) == TRUE) {
613 pkm.pm_address = (uintptr_t) addr;
614 pkm.pm_size = (size_t) size;
621 rv = vm_map_delete(map, addr, end);
624 if (rv == KERN_SUCCESS && __predict_false(pmc_handled)) {
625 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
626 vm_map_lock_downgrade(map);
627 if (pkm.pm_address != (uintptr_t) NULL)
628 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
629 vm_map_unlock_read(map);
634 return (vm_mmap_to_errno(rv));
637 #ifndef _SYS_SYSPROTO_H_
638 struct mprotect_args {
645 sys_mprotect(struct thread *td, struct mprotect_args *uap)
648 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len, uap->prot));
652 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot)
656 int vm_error, max_prot;
660 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
662 max_prot = PROT_MAX_EXTRACT(prot);
663 prot = PROT_EXTRACT(prot);
664 pageoff = (addr & PAGE_MASK);
667 size = (vm_size_t) round_page(size);
668 #ifdef COMPAT_FREEBSD32
669 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
670 if (((addr + size) & 0xffffffff) < addr)
674 if (addr + size < addr)
677 flags = VM_MAP_PROTECT_SET_PROT;
679 flags |= VM_MAP_PROTECT_SET_MAXPROT;
680 vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
681 addr, addr + size, prot, max_prot, flags);
686 case KERN_PROTECTION_FAILURE:
688 case KERN_RESOURCE_SHORTAGE:
690 case KERN_OUT_OF_BOUNDS:
696 #ifndef _SYS_SYSPROTO_H_
697 struct minherit_args {
704 sys_minherit(struct thread *td, struct minherit_args *uap)
707 return (kern_minherit(td, (uintptr_t)uap->addr, uap->len,
712 kern_minherit(struct thread *td, uintptr_t addr0, size_t len, int inherit0)
715 vm_size_t size, pageoff;
716 vm_inherit_t inherit;
718 addr = (vm_offset_t)addr0;
722 pageoff = (addr & PAGE_MASK);
725 size = (vm_size_t) round_page(size);
726 if (addr + size < addr)
729 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
730 addr + size, inherit)) {
733 case KERN_PROTECTION_FAILURE:
739 #ifndef _SYS_SYSPROTO_H_
740 struct madvise_args {
748 sys_madvise(struct thread *td, struct madvise_args *uap)
751 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
755 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
758 vm_offset_t addr, end, start;
762 * Check for our special case, advising the swap pager we are
765 if (behav == MADV_PROTECT) {
767 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
768 PROC_SPROTECT, &flags));
772 * Check for illegal addresses. Watch out for address wrap... Note
773 * that VM_*_ADDRESS are not constants due to casts (argh).
775 map = &td->td_proc->p_vmspace->vm_map;
777 if (!vm_map_range_valid(map, addr, addr + len))
781 * Since this routine is only advisory, we default to conservative
784 start = trunc_page(addr);
785 end = round_page(addr + len);
788 * vm_map_madvise() checks for illegal values of behav.
790 return (vm_map_madvise(map, start, end, behav));
793 #ifndef _SYS_SYSPROTO_H_
794 struct mincore_args {
802 sys_mincore(struct thread *td, struct mincore_args *uap)
805 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
809 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
813 vm_map_entry_t current, entry;
815 vm_offset_t addr, cend, end, first_addr;
819 int error, lastvecindex, mincoreinfo, vecindex;
820 unsigned int timestamp;
823 * Make sure that the addresses presented are valid for user
826 first_addr = addr = trunc_page(addr0);
827 end = round_page(addr0 + len);
828 map = &td->td_proc->p_vmspace->vm_map;
829 if (end > vm_map_max(map) || end < addr)
832 pmap = vmspace_pmap(td->td_proc->p_vmspace);
834 vm_map_lock_read(map);
836 timestamp = map->timestamp;
838 if (!vm_map_lookup_entry(map, addr, &entry)) {
839 vm_map_unlock_read(map);
844 * Do this on a map entry basis so that if the pages are not
845 * in the current processes address space, we can easily look
846 * up the pages elsewhere.
849 while (entry->start < end) {
851 * check for contiguity
854 entry = vm_map_entry_succ(current);
855 if (current->end < end &&
856 entry->start > current->end) {
857 vm_map_unlock_read(map);
862 * ignore submaps (for now) or null objects
864 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
865 current->object.vm_object == NULL)
869 * limit this scan to the current map entry and the
870 * limits for the mincore call
872 if (addr < current->start)
873 addr = current->start;
878 for (; addr < cend; addr += PAGE_SIZE) {
880 * Check pmap first, it is likely faster, also
881 * it can provide info as to whether we are the
882 * one referencing or modifying the page.
888 mincoreinfo = pmap_mincore(pmap, addr, &pa);
889 if (mincore_mapped) {
891 * We only care about this pmap's
892 * mapping of the page, if any.
895 } else if (pa != 0) {
897 * The page is mapped by this process but not
898 * both accessed and modified. It is also
899 * managed. Acquire the object lock so that
900 * other mappings might be examined. The page's
901 * identity may change at any point before its
902 * object lock is acquired, so re-validate if
905 m = PHYS_TO_VM_PAGE(pa);
906 while (object == NULL || m->object != object) {
908 VM_OBJECT_WUNLOCK(object);
909 object = atomic_load_ptr(&m->object);
912 VM_OBJECT_WLOCK(object);
914 if (pa != pmap_extract(pmap, addr))
916 KASSERT(vm_page_all_valid(m),
917 ("mincore: page %p is mapped but invalid",
919 } else if (mincoreinfo == 0) {
921 * The page is not mapped by this process. If
922 * the object implements managed pages, then
923 * determine if the page is resident so that
924 * the mappings might be examined.
926 if (current->object.vm_object != object) {
928 VM_OBJECT_WUNLOCK(object);
929 object = current->object.vm_object;
930 VM_OBJECT_WLOCK(object);
932 if (object->type == OBJT_DEFAULT ||
933 (object->flags & OBJ_SWAP) != 0 ||
934 object->type == OBJT_VNODE) {
935 pindex = OFF_TO_IDX(current->offset +
936 (addr - current->start));
937 m = vm_page_lookup(object, pindex);
938 if (m != NULL && vm_page_none_valid(m))
941 mincoreinfo = MINCORE_INCORE;
945 VM_OBJECT_ASSERT_WLOCKED(m->object);
947 /* Examine other mappings of the page. */
948 if (m->dirty == 0 && pmap_is_modified(m))
951 mincoreinfo |= MINCORE_MODIFIED_OTHER;
954 * The first test for PGA_REFERENCED is an
955 * optimization. The second test is
956 * required because a concurrent pmap
957 * operation could clear the last reference
958 * and set PGA_REFERENCED before the call to
959 * pmap_is_referenced().
961 if ((m->a.flags & PGA_REFERENCED) != 0 ||
962 pmap_is_referenced(m) ||
963 (m->a.flags & PGA_REFERENCED) != 0)
964 mincoreinfo |= MINCORE_REFERENCED_OTHER;
967 VM_OBJECT_WUNLOCK(object);
970 * subyte may page fault. In case it needs to modify
971 * the map, we release the lock.
973 vm_map_unlock_read(map);
976 * calculate index into user supplied byte vector
978 vecindex = atop(addr - first_addr);
981 * If we have skipped map entries, we need to make sure that
982 * the byte vector is zeroed for those skipped entries.
984 while ((lastvecindex + 1) < vecindex) {
986 error = subyte(vec + lastvecindex, 0);
994 * Pass the page information to the user
996 error = subyte(vec + vecindex, mincoreinfo);
1003 * If the map has changed, due to the subyte, the previous
1004 * output may be invalid.
1006 vm_map_lock_read(map);
1007 if (timestamp != map->timestamp)
1010 lastvecindex = vecindex;
1015 * subyte may page fault. In case it needs to modify
1016 * the map, we release the lock.
1018 vm_map_unlock_read(map);
1021 * Zero the last entries in the byte vector.
1023 vecindex = atop(end - first_addr);
1024 while ((lastvecindex + 1) < vecindex) {
1026 error = subyte(vec + lastvecindex, 0);
1034 * If the map has changed, due to the subyte, the previous
1035 * output may be invalid.
1037 vm_map_lock_read(map);
1038 if (timestamp != map->timestamp)
1040 vm_map_unlock_read(map);
1045 #ifndef _SYS_SYSPROTO_H_
1052 sys_mlock(struct thread *td, struct mlock_args *uap)
1055 return (kern_mlock(td->td_proc, td->td_ucred,
1056 __DECONST(uintptr_t, uap->addr), uap->len));
1060 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1062 vm_offset_t addr, end, last, start;
1063 vm_size_t npages, size;
1065 unsigned long nsize;
1068 error = priv_check_cred(cred, PRIV_VM_MLOCK);
1074 start = trunc_page(addr);
1075 end = round_page(last);
1076 if (last < addr || end < addr)
1078 npages = atop(end - start);
1079 if (npages > vm_page_max_user_wired)
1081 map = &proc->p_vmspace->vm_map;
1083 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1084 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1092 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1098 error = vm_map_wire(map, start, end,
1099 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1101 if (racct_enable && error != KERN_SUCCESS) {
1103 racct_set(proc, RACCT_MEMLOCK,
1104 ptoa(pmap_wired_count(map->pmap)));
1111 case KERN_INVALID_ARGUMENT:
1118 #ifndef _SYS_SYSPROTO_H_
1119 struct mlockall_args {
1125 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1130 map = &td->td_proc->p_vmspace->vm_map;
1131 error = priv_check(td, PRIV_VM_MLOCK);
1135 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1139 * If wiring all pages in the process would cause it to exceed
1140 * a hard resource limit, return ENOMEM.
1142 if (!old_mlock && uap->how & MCL_CURRENT) {
1143 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1148 PROC_LOCK(td->td_proc);
1149 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1150 PROC_UNLOCK(td->td_proc);
1156 if (uap->how & MCL_FUTURE) {
1158 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1163 if (uap->how & MCL_CURRENT) {
1165 * P1003.1-2001 mandates that all currently mapped pages
1166 * will be memory resident and locked (wired) upon return
1167 * from mlockall(). vm_map_wire() will wire pages, by
1168 * calling vm_fault_wire() for each page in the region.
1170 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1171 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1172 if (error == KERN_SUCCESS)
1174 else if (error == KERN_RESOURCE_SHORTAGE)
1180 if (racct_enable && error != KERN_SUCCESS) {
1181 PROC_LOCK(td->td_proc);
1182 racct_set(td->td_proc, RACCT_MEMLOCK,
1183 ptoa(pmap_wired_count(map->pmap)));
1184 PROC_UNLOCK(td->td_proc);
1191 #ifndef _SYS_SYSPROTO_H_
1192 struct munlockall_args {
1198 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1203 map = &td->td_proc->p_vmspace->vm_map;
1204 error = priv_check(td, PRIV_VM_MUNLOCK);
1208 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1210 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1213 /* Forcibly unwire all pages. */
1214 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1215 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1217 if (racct_enable && error == KERN_SUCCESS) {
1218 PROC_LOCK(td->td_proc);
1219 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1220 PROC_UNLOCK(td->td_proc);
1227 #ifndef _SYS_SYSPROTO_H_
1228 struct munlock_args {
1234 sys_munlock(struct thread *td, struct munlock_args *uap)
1237 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1241 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1243 vm_offset_t addr, end, last, start;
1249 error = priv_check(td, PRIV_VM_MUNLOCK);
1254 start = trunc_page(addr);
1255 end = round_page(last);
1256 if (last < addr || end < addr)
1258 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1259 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1261 if (racct_enable && error == KERN_SUCCESS) {
1262 PROC_LOCK(td->td_proc);
1263 map = &td->td_proc->p_vmspace->vm_map;
1264 racct_set(td->td_proc, RACCT_MEMLOCK,
1265 ptoa(pmap_wired_count(map->pmap)));
1266 PROC_UNLOCK(td->td_proc);
1269 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1275 * Helper function for vm_mmap. Perform sanity check specific for mmap
1276 * operations on vnodes.
1279 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1280 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1281 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1282 boolean_t *writecounted)
1291 cred = td->td_ucred;
1292 writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
1293 (*flagsp & MAP_SHARED) != 0;
1294 if ((error = vget(vp, LK_SHARED)) != 0)
1296 AUDIT_ARG_VNODE1(vp);
1300 if (vp->v_type == VREG) {
1302 * Get the proper underlying object
1308 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1310 vp = (struct vnode *)obj->handle;
1312 * Bypass filesystems obey the mpsafety of the
1313 * underlying fs. Tmpfs never bypasses.
1315 error = vget(vp, LK_SHARED);
1320 *writecounted = TRUE;
1321 vm_pager_update_writecount(obj, 0, objsize);
1327 if ((error = VOP_GETATTR(vp, &va, cred)))
1330 /* This relies on VM_PROT_* matching PROT_*. */
1331 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1335 if ((flags & MAP_SHARED) != 0) {
1336 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1337 if (prot & VM_PROT_WRITE) {
1341 *maxprotp &= ~VM_PROT_WRITE;
1345 * If it is a regular file without any references
1346 * we do not need to sync it.
1347 * Adjust object size to be the size of actual file.
1349 objsize = round_page(va.va_size);
1350 if (va.va_nlink == 0)
1351 flags |= MAP_NOSYNC;
1352 if (obj->type == OBJT_VNODE) {
1353 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1360 KASSERT(obj->type == OBJT_DEFAULT ||
1361 (obj->flags & OBJ_SWAP) != 0,
1362 ("wrong object type"));
1363 vm_object_reference(obj);
1364 #if VM_NRESERVLEVEL > 0
1365 if ((obj->flags & OBJ_COLORED) == 0) {
1366 VM_OBJECT_WLOCK(obj);
1367 vm_object_color(obj, 0);
1368 VM_OBJECT_WUNLOCK(obj);
1378 if (error != 0 && *writecounted) {
1379 *writecounted = FALSE;
1380 vm_pager_update_writecount(obj, objsize, 0);
1389 * Helper function for vm_mmap. Perform sanity check specific for mmap
1390 * operations on cdevs.
1393 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1394 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1395 vm_ooffset_t *foff, vm_object_t *objp)
1402 if (dsw->d_flags & D_MMAP_ANON) {
1405 *maxprotp = VM_PROT_ALL;
1406 *flagsp |= MAP_ANON;
1410 * cdevs do not provide private mappings of any kind.
1412 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1413 (prot & VM_PROT_WRITE) != 0)
1415 if (flags & (MAP_PRIVATE|MAP_COPY))
1418 * Force device mappings to be shared.
1420 flags |= MAP_SHARED;
1422 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1427 * First, try d_mmap_single(). If that is not implemented
1428 * (returns ENODEV), fall back to using the device pager.
1429 * Note that d_mmap_single() must return a reference to the
1430 * object (it needs to bump the reference count of the object
1431 * it returns somehow).
1433 * XXX assumes VM_PROT_* == PROT_*
1435 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1436 if (error != ENODEV)
1438 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1450 * Internal version of mmap used by exec, sys5 shared memory, and
1451 * various device drivers. Handle is either a vnode pointer, a
1452 * character device, or NULL for MAP_ANON.
1455 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1456 vm_prot_t maxprot, int flags,
1457 objtype_t handle_type, void *handle,
1461 struct thread *td = curthread;
1463 boolean_t writecounted;
1468 size = round_page(size);
1470 writecounted = FALSE;
1473 * Lookup/allocate object.
1475 switch (handle_type) {
1482 dsw = dev_refthread(cdev, &ref);
1485 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1486 dsw, &foff, &object);
1487 dev_relthread(cdev, ref);
1491 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1492 handle, &foff, &object, &writecounted);
1495 if (handle == NULL) {
1507 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1508 foff, writecounted, td);
1509 if (error != 0 && object != NULL) {
1511 * If this mapping was accounted for in the vnode's
1512 * writecount, then undo that now.
1515 vm_pager_release_writecount(object, 0, size);
1516 vm_object_deallocate(object);
1522 kern_mmap_racct_check(struct thread *td, vm_map_t map, vm_size_t size)
1526 RACCT_PROC_LOCK(td->td_proc);
1527 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1528 RACCT_PROC_UNLOCK(td->td_proc);
1531 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1532 RACCT_PROC_UNLOCK(td->td_proc);
1535 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1536 if (ptoa(pmap_wired_count(map->pmap)) + size >
1537 lim_cur(td, RLIMIT_MEMLOCK)) {
1538 racct_set_force(td->td_proc, RACCT_VMEM, map->size);
1539 RACCT_PROC_UNLOCK(td->td_proc);
1542 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1543 ptoa(pmap_wired_count(map->pmap)) + size);
1545 racct_set_force(td->td_proc, RACCT_VMEM, map->size);
1546 RACCT_PROC_UNLOCK(td->td_proc);
1550 RACCT_PROC_UNLOCK(td->td_proc);
1555 * Internal version of mmap that maps a specific VM object into an
1556 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1559 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1560 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1561 boolean_t writecounted, struct thread *td)
1563 vm_offset_t max_addr;
1564 int docow, error, findspace, rv;
1567 curmap = map == &td->td_proc->p_vmspace->vm_map;
1569 error = kern_mmap_racct_check(td, map, size);
1575 * We currently can only deal with page aligned file offsets.
1576 * The mmap() system call already enforces this by subtracting
1577 * the page offset from the file offset, but checking here
1578 * catches errors in device drivers (e.g. d_single_mmap()
1579 * callbacks) and other internal mapping requests (such as in
1582 if (foff & PAGE_MASK)
1585 if ((flags & MAP_FIXED) == 0) {
1587 *addr = round_page(*addr);
1589 if (*addr != trunc_page(*addr))
1594 if (flags & MAP_ANON) {
1595 if (object != NULL || foff != 0)
1598 } else if (flags & MAP_PREFAULT_READ)
1599 docow = MAP_PREFAULT;
1601 docow = MAP_PREFAULT_PARTIAL;
1603 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1604 docow |= MAP_COPY_ON_WRITE;
1605 if (flags & MAP_NOSYNC)
1606 docow |= MAP_DISABLE_SYNCER;
1607 if (flags & MAP_NOCORE)
1608 docow |= MAP_DISABLE_COREDUMP;
1609 /* Shared memory is also shared with children. */
1610 if (flags & MAP_SHARED)
1611 docow |= MAP_INHERIT_SHARE;
1613 docow |= MAP_WRITECOUNT;
1614 if (flags & MAP_STACK) {
1617 docow |= MAP_STACK_GROWS_DOWN;
1619 if ((flags & MAP_EXCL) != 0)
1620 docow |= MAP_CHECK_EXCL;
1621 if ((flags & MAP_GUARD) != 0)
1622 docow |= MAP_CREATE_GUARD;
1625 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1626 findspace = VMFS_SUPER_SPACE;
1627 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1628 findspace = VMFS_ALIGNED_SPACE(flags >>
1629 MAP_ALIGNMENT_SHIFT);
1631 findspace = VMFS_OPTIMAL_SPACE;
1634 if ((flags & MAP_32BIT) != 0)
1635 max_addr = MAP_32BIT_MAX_ADDR;
1638 rv = vm_map_find_min(map, object, foff, addr, size,
1639 round_page((vm_offset_t)td->td_proc->p_vmspace->
1640 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1641 findspace, prot, maxprot, docow);
1643 rv = vm_map_find(map, object, foff, addr, size,
1644 max_addr, findspace, prot, maxprot, docow);
1647 rv = vm_map_fixed(map, object, foff, *addr, size,
1648 prot, maxprot, docow);
1651 if (rv == KERN_SUCCESS) {
1653 * If the process has requested that all future mappings
1654 * be wired, then heed this.
1656 if ((map->flags & MAP_WIREFUTURE) != 0) {
1658 if ((map->flags & MAP_WIREFUTURE) != 0)
1659 (void)vm_map_wire_locked(map, *addr,
1660 *addr + size, VM_MAP_WIRE_USER |
1661 ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
1662 VM_MAP_WIRE_NOHOLES));
1666 return (vm_mmap_to_errno(rv));
1670 * Translate a Mach VM return code to zero on success or the appropriate errno
1674 vm_mmap_to_errno(int rv)
1680 case KERN_INVALID_ADDRESS:
1683 case KERN_PROTECTION_FAILURE: