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, (uintptr_t)uap->addr, uap->len, uap->prot,
183 uap->flags, uap->fd, uap->pos));
187 kern_mmap_maxprot(struct proc *p, int prot)
190 if ((p->p_flag2 & P2_PROTMAX_DISABLE) != 0 ||
191 (p->p_fctl0 & NT_FREEBSD_FCTL_PROTMAX_DISABLE) != 0)
193 if (((p->p_flag2 & P2_PROTMAX_ENABLE) != 0 || imply_prot_max) &&
200 kern_mmap(struct thread *td, uintptr_t addr0, size_t len, int prot, int flags,
203 struct mmap_req mr = {
212 return (kern_mmap_req(td, &mr));
216 kern_mmap_req(struct thread *td, const struct mmap_req *mrp)
223 vm_size_t len, pageoff, size;
224 vm_prot_t cap_maxprot;
225 int align, error, fd, flags, max_prot, prot;
227 mmap_check_fp_fn check_fp_fn;
232 flags = mrp->mr_flags;
235 check_fp_fn = mrp->mr_check_fp_fn;
237 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
239 max_prot = PROT_MAX_EXTRACT(prot);
240 prot = PROT_EXTRACT(prot);
241 if (max_prot != 0 && (max_prot & prot) != prot)
247 * Always honor PROT_MAX if set. If not, default to all
248 * permissions unless we're implying maximum permissions.
251 max_prot = kern_mmap_maxprot(p, prot);
258 * Ignore old flags that used to be defined but did not do anything.
260 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
263 * Enforce the constraints.
264 * Mapping of length 0 is only allowed for old binaries.
265 * Anonymous mapping shall specify -1 as filedescriptor and
266 * zero position for new code. Be nice to ancient a.out
267 * binaries and correct pos for anonymous mapping, since old
268 * ld.so sometimes issues anonymous map requests with non-zero
271 if (!SV_CURPROC_FLAG(SV_AOUT)) {
272 if ((len == 0 && p->p_osrel >= P_OSREL_MAP_ANON) ||
273 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
276 if ((flags & MAP_ANON) != 0)
280 if (flags & MAP_STACK) {
282 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
287 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
288 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
289 MAP_PREFAULT_READ | MAP_GUARD |
293 MAP_ALIGNMENT_MASK)) != 0)
295 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
297 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
299 if (prot != PROT_NONE &&
300 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
302 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
303 pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
307 MAP_ALIGNMENT_MASK)) != 0))
311 * Align the file position to a page boundary,
312 * and save its page offset component.
314 pageoff = (pos & PAGE_MASK);
317 /* Compute size from len by rounding (on both ends). */
318 size = len + pageoff; /* low end... */
319 size = round_page(size); /* hi end */
320 /* Check for rounding up to zero. */
324 /* Ensure alignment is at least a page and fits in a pointer. */
325 align = flags & MAP_ALIGNMENT_MASK;
326 if (align != 0 && align != MAP_ALIGNED_SUPER &&
327 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
328 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
332 * Check for illegal addresses. Watch out for address wrap... Note
333 * that VM_*_ADDRESS are not constants due to casts (argh).
335 if (flags & MAP_FIXED) {
337 * The specified address must have the same remainder
338 * as the file offset taken modulo PAGE_SIZE, so it
339 * should be aligned after adjustment by pageoff.
342 if (addr & PAGE_MASK)
345 /* Address range must be all in user VM space. */
346 if (!vm_map_range_valid(&vms->vm_map, addr, addr + size))
349 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
351 } else if (flags & MAP_32BIT) {
353 * For MAP_32BIT, override the hint if it is too high and
354 * do not bother moving the mapping past the heap (since
355 * the heap is usually above 2GB).
357 if (addr + size > MAP_32BIT_MAX_ADDR)
362 * XXX for non-fixed mappings where no hint is provided or
363 * the hint would fall in the potential heap space,
364 * place it after the end of the largest possible heap.
366 * There should really be a pmap call to determine a reasonable
370 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
371 addr < round_page((vm_offset_t)vms->vm_daddr +
372 lim_max(td, RLIMIT_DATA))))
373 addr = round_page((vm_offset_t)vms->vm_daddr +
374 lim_max(td, RLIMIT_DATA));
378 * Return success without mapping anything for old
379 * binaries that request a page-aligned mapping of
380 * length 0. For modern binaries, this function
381 * returns an error earlier.
384 } else if ((flags & MAP_GUARD) != 0) {
385 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
386 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
387 } else if ((flags & MAP_ANON) != 0) {
389 * Mapping blank space is trivial.
391 * This relies on VM_PROT_* matching PROT_*.
393 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
394 max_prot, flags, NULL, pos, FALSE, td);
397 * Mapping file, get fp for validation and don't let the
398 * descriptor disappear on us if we block. Check capability
399 * rights, but also return the maximum rights to be combined
400 * with maxprot later.
402 cap_rights_init_one(&rights, CAP_MMAP);
403 if (prot & PROT_READ)
404 cap_rights_set_one(&rights, CAP_MMAP_R);
405 if ((flags & MAP_SHARED) != 0) {
406 if (prot & PROT_WRITE)
407 cap_rights_set_one(&rights, CAP_MMAP_W);
409 if (prot & PROT_EXEC)
410 cap_rights_set_one(&rights, CAP_MMAP_X);
411 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
414 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
415 p->p_osrel >= P_OSREL_MAP_FSTRICT) {
419 if (check_fp_fn != NULL) {
420 error = check_fp_fn(fp, prot, max_prot & cap_maxprot,
425 /* This relies on VM_PROT_* matching PROT_*. */
426 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
427 max_prot & cap_maxprot, flags, pos, td);
431 td->td_retval[0] = (register_t) (addr + pageoff);
439 #if defined(COMPAT_FREEBSD6)
441 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
444 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
445 uap->flags, uap->fd, uap->pos));
450 #ifndef _SYS_SYSPROTO_H_
461 ommap(struct thread *td, struct ommap_args *uap)
463 static const char cvtbsdprot[8] = {
467 PROT_EXEC | PROT_WRITE,
469 PROT_EXEC | PROT_READ,
470 PROT_WRITE | PROT_READ,
471 PROT_EXEC | PROT_WRITE | PROT_READ,
475 #define OMAP_ANON 0x0002
476 #define OMAP_COPY 0x0020
477 #define OMAP_SHARED 0x0010
478 #define OMAP_FIXED 0x0100
480 prot = cvtbsdprot[uap->prot & 0x7];
481 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
482 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
487 if (uap->flags & OMAP_ANON)
489 if (uap->flags & OMAP_COPY)
491 if (uap->flags & OMAP_SHARED)
494 flags |= MAP_PRIVATE;
495 if (uap->flags & OMAP_FIXED)
497 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
500 #endif /* COMPAT_43 */
502 #ifndef _SYS_SYSPROTO_H_
510 sys_msync(struct thread *td, struct msync_args *uap)
513 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
517 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
525 pageoff = (addr & PAGE_MASK);
528 size = (vm_size_t) round_page(size);
529 if (addr + size < addr)
532 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
535 map = &td->td_proc->p_vmspace->vm_map;
538 * Clean the pages and interpret the return value.
540 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
541 (flags & MS_INVALIDATE) != 0);
545 case KERN_INVALID_ADDRESS:
547 case KERN_INVALID_ARGUMENT:
556 #ifndef _SYS_SYSPROTO_H_
563 sys_munmap(struct thread *td, struct munmap_args *uap)
566 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
570 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
573 struct pmckern_map_out pkm;
574 vm_map_entry_t entry;
577 vm_offset_t addr, end;
585 pageoff = (addr & PAGE_MASK);
588 size = (vm_size_t) round_page(size);
590 map = &td->td_proc->p_vmspace->vm_map;
591 if (!vm_map_range_valid(map, addr, end))
597 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
600 * Inform hwpmc if the address range being unmapped contains
601 * an executable region.
603 pkm.pm_address = (uintptr_t) NULL;
604 if (vm_map_lookup_entry(map, addr, &entry)) {
605 for (; entry->start < end;
606 entry = vm_map_entry_succ(entry)) {
607 if (vm_map_check_protection(map, entry->start,
608 entry->end, VM_PROT_EXECUTE) == TRUE) {
609 pkm.pm_address = (uintptr_t) addr;
610 pkm.pm_size = (size_t) size;
617 vm_map_delete(map, addr, end);
620 if (__predict_false(pmc_handled)) {
621 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
622 vm_map_lock_downgrade(map);
623 if (pkm.pm_address != (uintptr_t) NULL)
624 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
625 vm_map_unlock_read(map);
630 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
634 #ifndef _SYS_SYSPROTO_H_
635 struct mprotect_args {
642 sys_mprotect(struct thread *td, struct mprotect_args *uap)
645 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len, uap->prot));
649 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot)
653 int vm_error, max_prot;
656 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
658 max_prot = PROT_MAX_EXTRACT(prot);
659 prot = PROT_EXTRACT(prot);
660 pageoff = (addr & PAGE_MASK);
663 size = (vm_size_t) round_page(size);
664 #ifdef COMPAT_FREEBSD32
665 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
666 if (((addr + size) & 0xffffffff) < addr)
670 if (addr + size < addr)
673 vm_error = KERN_SUCCESS;
675 if ((max_prot & prot) != prot)
677 vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
678 addr, addr + size, max_prot, TRUE);
680 if (vm_error == KERN_SUCCESS)
681 vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
682 addr, addr + size, prot, FALSE);
687 case KERN_PROTECTION_FAILURE:
689 case KERN_RESOURCE_SHORTAGE:
695 #ifndef _SYS_SYSPROTO_H_
696 struct minherit_args {
703 sys_minherit(struct thread *td, struct minherit_args *uap)
706 return (kern_minherit(td, (uintptr_t)uap->addr, uap->len,
711 kern_minherit(struct thread *td, uintptr_t addr0, size_t len, int inherit0)
714 vm_size_t size, pageoff;
715 vm_inherit_t inherit;
717 addr = (vm_offset_t)addr0;
721 pageoff = (addr & PAGE_MASK);
724 size = (vm_size_t) round_page(size);
725 if (addr + size < addr)
728 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
729 addr + size, inherit)) {
732 case KERN_PROTECTION_FAILURE:
738 #ifndef _SYS_SYSPROTO_H_
739 struct madvise_args {
747 sys_madvise(struct thread *td, struct madvise_args *uap)
750 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
754 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
757 vm_offset_t addr, end, start;
761 * Check for our special case, advising the swap pager we are
764 if (behav == MADV_PROTECT) {
766 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
767 PROC_SPROTECT, &flags));
771 * Check for illegal addresses. Watch out for address wrap... Note
772 * that VM_*_ADDRESS are not constants due to casts (argh).
774 map = &td->td_proc->p_vmspace->vm_map;
776 if (!vm_map_range_valid(map, addr, addr + len))
780 * Since this routine is only advisory, we default to conservative
783 start = trunc_page(addr);
784 end = round_page(addr + len);
787 * vm_map_madvise() checks for illegal values of behav.
789 return (vm_map_madvise(map, start, end, behav));
792 #ifndef _SYS_SYSPROTO_H_
793 struct mincore_args {
801 sys_mincore(struct thread *td, struct mincore_args *uap)
804 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
808 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
812 vm_map_entry_t current, entry;
814 vm_offset_t addr, cend, end, first_addr;
818 int error, lastvecindex, mincoreinfo, vecindex;
819 unsigned int timestamp;
822 * Make sure that the addresses presented are valid for user
825 first_addr = addr = trunc_page(addr0);
826 end = round_page(addr0 + len);
827 map = &td->td_proc->p_vmspace->vm_map;
828 if (end > vm_map_max(map) || end < addr)
831 pmap = vmspace_pmap(td->td_proc->p_vmspace);
833 vm_map_lock_read(map);
835 timestamp = map->timestamp;
837 if (!vm_map_lookup_entry(map, addr, &entry)) {
838 vm_map_unlock_read(map);
843 * Do this on a map entry basis so that if the pages are not
844 * in the current processes address space, we can easily look
845 * up the pages elsewhere.
848 while (entry->start < end) {
850 * check for contiguity
853 entry = vm_map_entry_succ(current);
854 if (current->end < end &&
855 entry->start > current->end) {
856 vm_map_unlock_read(map);
861 * ignore submaps (for now) or null objects
863 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
864 current->object.vm_object == NULL)
868 * limit this scan to the current map entry and the
869 * limits for the mincore call
871 if (addr < current->start)
872 addr = current->start;
877 for (; addr < cend; addr += PAGE_SIZE) {
879 * Check pmap first, it is likely faster, also
880 * it can provide info as to whether we are the
881 * one referencing or modifying the page.
887 mincoreinfo = pmap_mincore(pmap, addr, &pa);
888 if (mincore_mapped) {
890 * We only care about this pmap's
891 * mapping of the page, if any.
894 } else if (pa != 0) {
896 * The page is mapped by this process but not
897 * both accessed and modified. It is also
898 * managed. Acquire the object lock so that
899 * other mappings might be examined. The page's
900 * identity may change at any point before its
901 * object lock is acquired, so re-validate if
904 m = PHYS_TO_VM_PAGE(pa);
905 while (object == NULL || m->object != object) {
907 VM_OBJECT_WUNLOCK(object);
908 object = atomic_load_ptr(&m->object);
911 VM_OBJECT_WLOCK(object);
913 if (pa != pmap_extract(pmap, addr))
915 KASSERT(vm_page_all_valid(m),
916 ("mincore: page %p is mapped but invalid",
918 } else if (mincoreinfo == 0) {
920 * The page is not mapped by this process. If
921 * the object implements managed pages, then
922 * determine if the page is resident so that
923 * the mappings might be examined.
925 if (current->object.vm_object != object) {
927 VM_OBJECT_WUNLOCK(object);
928 object = current->object.vm_object;
929 VM_OBJECT_WLOCK(object);
931 if (object->type == OBJT_DEFAULT ||
932 object->type == OBJT_SWAP ||
933 object->type == OBJT_VNODE) {
934 pindex = OFF_TO_IDX(current->offset +
935 (addr - current->start));
936 m = vm_page_lookup(object, pindex);
937 if (m != NULL && vm_page_none_valid(m))
940 mincoreinfo = MINCORE_INCORE;
944 VM_OBJECT_ASSERT_WLOCKED(m->object);
946 /* Examine other mappings of the page. */
947 if (m->dirty == 0 && pmap_is_modified(m))
950 mincoreinfo |= MINCORE_MODIFIED_OTHER;
953 * The first test for PGA_REFERENCED is an
954 * optimization. The second test is
955 * required because a concurrent pmap
956 * operation could clear the last reference
957 * and set PGA_REFERENCED before the call to
958 * pmap_is_referenced().
960 if ((m->a.flags & PGA_REFERENCED) != 0 ||
961 pmap_is_referenced(m) ||
962 (m->a.flags & PGA_REFERENCED) != 0)
963 mincoreinfo |= MINCORE_REFERENCED_OTHER;
966 VM_OBJECT_WUNLOCK(object);
969 * subyte may page fault. In case it needs to modify
970 * the map, we release the lock.
972 vm_map_unlock_read(map);
975 * calculate index into user supplied byte vector
977 vecindex = atop(addr - first_addr);
980 * If we have skipped map entries, we need to make sure that
981 * the byte vector is zeroed for those skipped entries.
983 while ((lastvecindex + 1) < vecindex) {
985 error = subyte(vec + lastvecindex, 0);
993 * Pass the page information to the user
995 error = subyte(vec + vecindex, mincoreinfo);
1002 * If the map has changed, due to the subyte, the previous
1003 * output may be invalid.
1005 vm_map_lock_read(map);
1006 if (timestamp != map->timestamp)
1009 lastvecindex = vecindex;
1014 * subyte may page fault. In case it needs to modify
1015 * the map, we release the lock.
1017 vm_map_unlock_read(map);
1020 * Zero the last entries in the byte vector.
1022 vecindex = atop(end - first_addr);
1023 while ((lastvecindex + 1) < vecindex) {
1025 error = subyte(vec + lastvecindex, 0);
1033 * If the map has changed, due to the subyte, the previous
1034 * output may be invalid.
1036 vm_map_lock_read(map);
1037 if (timestamp != map->timestamp)
1039 vm_map_unlock_read(map);
1044 #ifndef _SYS_SYSPROTO_H_
1051 sys_mlock(struct thread *td, struct mlock_args *uap)
1054 return (kern_mlock(td->td_proc, td->td_ucred,
1055 __DECONST(uintptr_t, uap->addr), uap->len));
1059 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1061 vm_offset_t addr, end, last, start;
1062 vm_size_t npages, size;
1064 unsigned long nsize;
1067 error = priv_check_cred(cred, PRIV_VM_MLOCK);
1073 start = trunc_page(addr);
1074 end = round_page(last);
1075 if (last < addr || end < addr)
1077 npages = atop(end - start);
1078 if (npages > vm_page_max_user_wired)
1080 map = &proc->p_vmspace->vm_map;
1082 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1083 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1091 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1097 error = vm_map_wire(map, start, end,
1098 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1100 if (racct_enable && error != KERN_SUCCESS) {
1102 racct_set(proc, RACCT_MEMLOCK,
1103 ptoa(pmap_wired_count(map->pmap)));
1107 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1110 #ifndef _SYS_SYSPROTO_H_
1111 struct mlockall_args {
1117 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1122 map = &td->td_proc->p_vmspace->vm_map;
1123 error = priv_check(td, PRIV_VM_MLOCK);
1127 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1131 * If wiring all pages in the process would cause it to exceed
1132 * a hard resource limit, return ENOMEM.
1134 if (!old_mlock && uap->how & MCL_CURRENT) {
1135 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1140 PROC_LOCK(td->td_proc);
1141 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1142 PROC_UNLOCK(td->td_proc);
1148 if (uap->how & MCL_FUTURE) {
1150 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1155 if (uap->how & MCL_CURRENT) {
1157 * P1003.1-2001 mandates that all currently mapped pages
1158 * will be memory resident and locked (wired) upon return
1159 * from mlockall(). vm_map_wire() will wire pages, by
1160 * calling vm_fault_wire() for each page in the region.
1162 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1163 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1164 if (error == KERN_SUCCESS)
1166 else if (error == KERN_RESOURCE_SHORTAGE)
1172 if (racct_enable && error != KERN_SUCCESS) {
1173 PROC_LOCK(td->td_proc);
1174 racct_set(td->td_proc, RACCT_MEMLOCK,
1175 ptoa(pmap_wired_count(map->pmap)));
1176 PROC_UNLOCK(td->td_proc);
1183 #ifndef _SYS_SYSPROTO_H_
1184 struct munlockall_args {
1190 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1195 map = &td->td_proc->p_vmspace->vm_map;
1196 error = priv_check(td, PRIV_VM_MUNLOCK);
1200 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1202 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1205 /* Forcibly unwire all pages. */
1206 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1207 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1209 if (racct_enable && error == KERN_SUCCESS) {
1210 PROC_LOCK(td->td_proc);
1211 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1212 PROC_UNLOCK(td->td_proc);
1219 #ifndef _SYS_SYSPROTO_H_
1220 struct munlock_args {
1226 sys_munlock(struct thread *td, struct munlock_args *uap)
1229 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1233 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1235 vm_offset_t addr, end, last, start;
1241 error = priv_check(td, PRIV_VM_MUNLOCK);
1246 start = trunc_page(addr);
1247 end = round_page(last);
1248 if (last < addr || end < addr)
1250 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1251 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1253 if (racct_enable && error == KERN_SUCCESS) {
1254 PROC_LOCK(td->td_proc);
1255 map = &td->td_proc->p_vmspace->vm_map;
1256 racct_set(td->td_proc, RACCT_MEMLOCK,
1257 ptoa(pmap_wired_count(map->pmap)));
1258 PROC_UNLOCK(td->td_proc);
1261 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1267 * Helper function for vm_mmap. Perform sanity check specific for mmap
1268 * operations on vnodes.
1271 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1272 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1273 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1274 boolean_t *writecounted)
1283 cred = td->td_ucred;
1284 writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
1285 (*flagsp & MAP_SHARED) != 0;
1286 if ((error = vget(vp, LK_SHARED)) != 0)
1288 AUDIT_ARG_VNODE1(vp);
1292 if (vp->v_type == VREG) {
1294 * Get the proper underlying object
1300 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1302 vp = (struct vnode *)obj->handle;
1304 * Bypass filesystems obey the mpsafety of the
1305 * underlying fs. Tmpfs never bypasses.
1307 error = vget(vp, LK_SHARED);
1312 *writecounted = TRUE;
1313 vm_pager_update_writecount(obj, 0, objsize);
1319 if ((error = VOP_GETATTR(vp, &va, cred)))
1322 /* This relies on VM_PROT_* matching PROT_*. */
1323 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1327 if ((flags & MAP_SHARED) != 0) {
1328 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1329 if (prot & VM_PROT_WRITE) {
1333 *maxprotp &= ~VM_PROT_WRITE;
1337 * If it is a regular file without any references
1338 * we do not need to sync it.
1339 * Adjust object size to be the size of actual file.
1341 objsize = round_page(va.va_size);
1342 if (va.va_nlink == 0)
1343 flags |= MAP_NOSYNC;
1344 if (obj->type == OBJT_VNODE) {
1345 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1352 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1353 ("wrong object type"));
1354 vm_object_reference(obj);
1355 #if VM_NRESERVLEVEL > 0
1356 if ((obj->flags & OBJ_COLORED) == 0) {
1357 VM_OBJECT_WLOCK(obj);
1358 vm_object_color(obj, 0);
1359 VM_OBJECT_WUNLOCK(obj);
1369 if (error != 0 && *writecounted) {
1370 *writecounted = FALSE;
1371 vm_pager_update_writecount(obj, objsize, 0);
1380 * Helper function for vm_mmap. Perform sanity check specific for mmap
1381 * operations on cdevs.
1384 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1385 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1386 vm_ooffset_t *foff, vm_object_t *objp)
1393 if (dsw->d_flags & D_MMAP_ANON) {
1396 *maxprotp = VM_PROT_ALL;
1397 *flagsp |= MAP_ANON;
1401 * cdevs do not provide private mappings of any kind.
1403 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1404 (prot & VM_PROT_WRITE) != 0)
1406 if (flags & (MAP_PRIVATE|MAP_COPY))
1409 * Force device mappings to be shared.
1411 flags |= MAP_SHARED;
1413 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1418 * First, try d_mmap_single(). If that is not implemented
1419 * (returns ENODEV), fall back to using the device pager.
1420 * Note that d_mmap_single() must return a reference to the
1421 * object (it needs to bump the reference count of the object
1422 * it returns somehow).
1424 * XXX assumes VM_PROT_* == PROT_*
1426 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1427 if (error != ENODEV)
1429 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1441 * Internal version of mmap used by exec, sys5 shared memory, and
1442 * various device drivers. Handle is either a vnode pointer, a
1443 * character device, or NULL for MAP_ANON.
1446 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1447 vm_prot_t maxprot, int flags,
1448 objtype_t handle_type, void *handle,
1452 struct thread *td = curthread;
1454 boolean_t writecounted;
1459 size = round_page(size);
1461 writecounted = FALSE;
1464 * Lookup/allocate object.
1466 switch (handle_type) {
1473 dsw = dev_refthread(cdev, &ref);
1476 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1477 dsw, &foff, &object);
1478 dev_relthread(cdev, ref);
1482 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1483 handle, &foff, &object, &writecounted);
1486 if (handle == NULL) {
1498 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1499 foff, writecounted, td);
1500 if (error != 0 && object != NULL) {
1502 * If this mapping was accounted for in the vnode's
1503 * writecount, then undo that now.
1506 vm_pager_release_writecount(object, 0, size);
1507 vm_object_deallocate(object);
1513 * Internal version of mmap that maps a specific VM object into an
1514 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1517 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1518 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1519 boolean_t writecounted, struct thread *td)
1521 boolean_t curmap, fitit;
1522 vm_offset_t max_addr;
1523 int docow, error, findspace, rv;
1525 curmap = map == &td->td_proc->p_vmspace->vm_map;
1527 RACCT_PROC_LOCK(td->td_proc);
1528 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1529 RACCT_PROC_UNLOCK(td->td_proc);
1532 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1533 RACCT_PROC_UNLOCK(td->td_proc);
1536 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1537 if (ptoa(pmap_wired_count(map->pmap)) + size >
1538 lim_cur(td, RLIMIT_MEMLOCK)) {
1539 racct_set_force(td->td_proc, RACCT_VMEM,
1541 RACCT_PROC_UNLOCK(td->td_proc);
1544 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1545 ptoa(pmap_wired_count(map->pmap)) + size);
1547 racct_set_force(td->td_proc, RACCT_VMEM,
1549 RACCT_PROC_UNLOCK(td->td_proc);
1553 RACCT_PROC_UNLOCK(td->td_proc);
1557 * We currently can only deal with page aligned file offsets.
1558 * The mmap() system call already enforces this by subtracting
1559 * the page offset from the file offset, but checking here
1560 * catches errors in device drivers (e.g. d_single_mmap()
1561 * callbacks) and other internal mapping requests (such as in
1564 if (foff & PAGE_MASK)
1567 if ((flags & MAP_FIXED) == 0) {
1569 *addr = round_page(*addr);
1571 if (*addr != trunc_page(*addr))
1576 if (flags & MAP_ANON) {
1577 if (object != NULL || foff != 0)
1580 } else if (flags & MAP_PREFAULT_READ)
1581 docow = MAP_PREFAULT;
1583 docow = MAP_PREFAULT_PARTIAL;
1585 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1586 docow |= MAP_COPY_ON_WRITE;
1587 if (flags & MAP_NOSYNC)
1588 docow |= MAP_DISABLE_SYNCER;
1589 if (flags & MAP_NOCORE)
1590 docow |= MAP_DISABLE_COREDUMP;
1591 /* Shared memory is also shared with children. */
1592 if (flags & MAP_SHARED)
1593 docow |= MAP_INHERIT_SHARE;
1595 docow |= MAP_WRITECOUNT;
1596 if (flags & MAP_STACK) {
1599 docow |= MAP_STACK_GROWS_DOWN;
1601 if ((flags & MAP_EXCL) != 0)
1602 docow |= MAP_CHECK_EXCL;
1603 if ((flags & MAP_GUARD) != 0)
1604 docow |= MAP_CREATE_GUARD;
1607 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1608 findspace = VMFS_SUPER_SPACE;
1609 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1610 findspace = VMFS_ALIGNED_SPACE(flags >>
1611 MAP_ALIGNMENT_SHIFT);
1613 findspace = VMFS_OPTIMAL_SPACE;
1616 if ((flags & MAP_32BIT) != 0)
1617 max_addr = MAP_32BIT_MAX_ADDR;
1620 rv = vm_map_find_min(map, object, foff, addr, size,
1621 round_page((vm_offset_t)td->td_proc->p_vmspace->
1622 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1623 findspace, prot, maxprot, docow);
1625 rv = vm_map_find(map, object, foff, addr, size,
1626 max_addr, findspace, prot, maxprot, docow);
1629 rv = vm_map_fixed(map, object, foff, *addr, size,
1630 prot, maxprot, docow);
1633 if (rv == KERN_SUCCESS) {
1635 * If the process has requested that all future mappings
1636 * be wired, then heed this.
1638 if ((map->flags & MAP_WIREFUTURE) != 0) {
1640 if ((map->flags & MAP_WIREFUTURE) != 0)
1641 (void)vm_map_wire_locked(map, *addr,
1642 *addr + size, VM_MAP_WIRE_USER |
1643 ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
1644 VM_MAP_WIRE_NOHOLES));
1648 return (vm_mmap_to_errno(rv));
1652 * Translate a Mach VM return code to zero on success or the appropriate errno
1656 vm_mmap_to_errno(int rv)
1662 case KERN_INVALID_ADDRESS:
1665 case KERN_PROTECTION_FAILURE: