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 permissions in mmap() when none are specified");
112 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
115 #ifndef _SYS_SYSPROTO_H_
122 sys_sbrk(struct thread *td, struct sbrk_args *uap)
124 /* Not yet implemented */
128 #ifndef _SYS_SYSPROTO_H_
135 sys_sstk(struct thread *td, struct sstk_args *uap)
137 /* Not yet implemented */
141 #if defined(COMPAT_43)
143 ogetpagesize(struct thread *td, struct ogetpagesize_args *uap)
146 td->td_retval[0] = PAGE_SIZE;
149 #endif /* COMPAT_43 */
153 * Memory Map (mmap) system call. Note that the file offset
154 * and address are allowed to be NOT page aligned, though if
155 * the MAP_FIXED flag it set, both must have the same remainder
156 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
157 * page-aligned, the actual mapping starts at trunc_page(addr)
158 * and the return value is adjusted up by the page offset.
160 * Generally speaking, only character devices which are themselves
161 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
162 * there would be no cache coherency between a descriptor and a VM mapping
163 * both to the same character device.
165 #ifndef _SYS_SYSPROTO_H_
178 sys_mmap(struct thread *td, struct mmap_args *uap)
181 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
182 uap->flags, uap->fd, uap->pos));
186 kern_mmap_maxprot(struct proc *p, int prot)
189 if ((p->p_flag2 & P2_PROTMAX_DISABLE) != 0 ||
190 (p->p_fctl0 & NT_FREEBSD_FCTL_PROTMAX_DISABLE) != 0)
192 if (((p->p_flag2 & P2_PROTMAX_ENABLE) != 0 || imply_prot_max) &&
199 kern_mmap(struct thread *td, uintptr_t addr0, size_t len, int prot, int flags,
206 vm_size_t pageoff, size;
207 vm_prot_t cap_maxprot;
208 int align, error, max_prot;
211 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
213 max_prot = PROT_MAX_EXTRACT(prot);
214 prot = PROT_EXTRACT(prot);
215 if (max_prot != 0 && (max_prot & prot) != prot)
221 * Always honor PROT_MAX if set. If not, default to all
222 * permissions unless we're implying maximum permissions.
225 max_prot = kern_mmap_maxprot(p, prot);
233 * Ignore old flags that used to be defined but did not do anything.
235 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
238 * Enforce the constraints.
239 * Mapping of length 0 is only allowed for old binaries.
240 * Anonymous mapping shall specify -1 as filedescriptor and
241 * zero position for new code. Be nice to ancient a.out
242 * binaries and correct pos for anonymous mapping, since old
243 * ld.so sometimes issues anonymous map requests with non-zero
246 if (!SV_CURPROC_FLAG(SV_AOUT)) {
247 if ((len == 0 && p->p_osrel >= P_OSREL_MAP_ANON) ||
248 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
251 if ((flags & MAP_ANON) != 0)
255 if (flags & MAP_STACK) {
257 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
262 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
263 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
264 MAP_PREFAULT_READ | MAP_GUARD |
268 MAP_ALIGNMENT_MASK)) != 0)
270 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
272 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
274 if (prot != PROT_NONE &&
275 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
277 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
278 pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
282 MAP_ALIGNMENT_MASK)) != 0))
286 * Align the file position to a page boundary,
287 * and save its page offset component.
289 pageoff = (pos & PAGE_MASK);
292 /* Compute size from len by rounding (on both ends). */
293 size = len + pageoff; /* low end... */
294 size = round_page(size); /* hi end */
295 /* Check for rounding up to zero. */
299 /* Ensure alignment is at least a page and fits in a pointer. */
300 align = flags & MAP_ALIGNMENT_MASK;
301 if (align != 0 && align != MAP_ALIGNED_SUPER &&
302 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
303 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
307 * Check for illegal addresses. Watch out for address wrap... Note
308 * that VM_*_ADDRESS are not constants due to casts (argh).
310 if (flags & MAP_FIXED) {
312 * The specified address must have the same remainder
313 * as the file offset taken modulo PAGE_SIZE, so it
314 * should be aligned after adjustment by pageoff.
317 if (addr & PAGE_MASK)
320 /* Address range must be all in user VM space. */
321 if (addr < vm_map_min(&vms->vm_map) ||
322 addr + size > vm_map_max(&vms->vm_map))
324 if (addr + size < addr)
327 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
329 } else if (flags & MAP_32BIT) {
331 * For MAP_32BIT, override the hint if it is too high and
332 * do not bother moving the mapping past the heap (since
333 * the heap is usually above 2GB).
335 if (addr + size > MAP_32BIT_MAX_ADDR)
340 * XXX for non-fixed mappings where no hint is provided or
341 * the hint would fall in the potential heap space,
342 * place it after the end of the largest possible heap.
344 * There should really be a pmap call to determine a reasonable
348 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
349 addr < round_page((vm_offset_t)vms->vm_daddr +
350 lim_max(td, RLIMIT_DATA))))
351 addr = round_page((vm_offset_t)vms->vm_daddr +
352 lim_max(td, RLIMIT_DATA));
356 * Return success without mapping anything for old
357 * binaries that request a page-aligned mapping of
358 * length 0. For modern binaries, this function
359 * returns an error earlier.
362 } else if ((flags & MAP_GUARD) != 0) {
363 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
364 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
365 } else if ((flags & MAP_ANON) != 0) {
367 * Mapping blank space is trivial.
369 * This relies on VM_PROT_* matching PROT_*.
371 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
372 max_prot, flags, NULL, pos, FALSE, td);
375 * Mapping file, get fp for validation and don't let the
376 * descriptor disappear on us if we block. Check capability
377 * rights, but also return the maximum rights to be combined
378 * with maxprot later.
380 cap_rights_init(&rights, CAP_MMAP);
381 if (prot & PROT_READ)
382 cap_rights_set(&rights, CAP_MMAP_R);
383 if ((flags & MAP_SHARED) != 0) {
384 if (prot & PROT_WRITE)
385 cap_rights_set(&rights, CAP_MMAP_W);
387 if (prot & PROT_EXEC)
388 cap_rights_set(&rights, CAP_MMAP_X);
389 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
392 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
393 p->p_osrel >= P_OSREL_MAP_FSTRICT) {
398 /* This relies on VM_PROT_* matching PROT_*. */
399 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
400 max_prot & cap_maxprot, flags, pos, td);
404 td->td_retval[0] = (register_t) (addr + pageoff);
412 #if defined(COMPAT_FREEBSD6)
414 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
417 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
418 uap->flags, uap->fd, uap->pos));
423 #ifndef _SYS_SYSPROTO_H_
434 ommap(struct thread *td, struct ommap_args *uap)
436 static const char cvtbsdprot[8] = {
440 PROT_EXEC | PROT_WRITE,
442 PROT_EXEC | PROT_READ,
443 PROT_WRITE | PROT_READ,
444 PROT_EXEC | PROT_WRITE | PROT_READ,
448 #define OMAP_ANON 0x0002
449 #define OMAP_COPY 0x0020
450 #define OMAP_SHARED 0x0010
451 #define OMAP_FIXED 0x0100
453 prot = cvtbsdprot[uap->prot & 0x7];
454 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
455 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
460 if (uap->flags & OMAP_ANON)
462 if (uap->flags & OMAP_COPY)
464 if (uap->flags & OMAP_SHARED)
467 flags |= MAP_PRIVATE;
468 if (uap->flags & OMAP_FIXED)
470 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
473 #endif /* COMPAT_43 */
476 #ifndef _SYS_SYSPROTO_H_
484 sys_msync(struct thread *td, struct msync_args *uap)
487 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
491 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
499 pageoff = (addr & PAGE_MASK);
502 size = (vm_size_t) round_page(size);
503 if (addr + size < addr)
506 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
509 map = &td->td_proc->p_vmspace->vm_map;
512 * Clean the pages and interpret the return value.
514 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
515 (flags & MS_INVALIDATE) != 0);
519 case KERN_INVALID_ADDRESS:
521 case KERN_INVALID_ARGUMENT:
530 #ifndef _SYS_SYSPROTO_H_
537 sys_munmap(struct thread *td, struct munmap_args *uap)
540 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
544 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
547 struct pmckern_map_out pkm;
548 vm_map_entry_t entry;
559 pageoff = (addr & PAGE_MASK);
562 size = (vm_size_t) round_page(size);
563 if (addr + size < addr)
567 * Check for illegal addresses. Watch out for address wrap...
569 map = &td->td_proc->p_vmspace->vm_map;
570 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
575 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
578 * Inform hwpmc if the address range being unmapped contains
579 * an executable region.
581 pkm.pm_address = (uintptr_t) NULL;
582 if (vm_map_lookup_entry(map, addr, &entry)) {
583 for (; entry->start < addr + size;
584 entry = vm_map_entry_succ(entry)) {
585 if (vm_map_check_protection(map, entry->start,
586 entry->end, VM_PROT_EXECUTE) == TRUE) {
587 pkm.pm_address = (uintptr_t) addr;
588 pkm.pm_size = (size_t) size;
595 vm_map_delete(map, addr, addr + size);
598 if (__predict_false(pmc_handled)) {
599 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
600 vm_map_lock_downgrade(map);
601 if (pkm.pm_address != (uintptr_t) NULL)
602 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
603 vm_map_unlock_read(map);
608 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
612 #ifndef _SYS_SYSPROTO_H_
613 struct mprotect_args {
620 sys_mprotect(struct thread *td, struct mprotect_args *uap)
623 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len, uap->prot));
627 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot)
631 int vm_error, max_prot;
634 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
636 max_prot = PROT_MAX_EXTRACT(prot);
637 prot = PROT_EXTRACT(prot);
638 pageoff = (addr & PAGE_MASK);
641 size = (vm_size_t) round_page(size);
642 #ifdef COMPAT_FREEBSD32
643 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
644 if (((addr + size) & 0xffffffff) < addr)
648 if (addr + size < addr)
651 vm_error = KERN_SUCCESS;
653 if ((max_prot & prot) != prot)
655 vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
656 addr, addr + size, max_prot, TRUE);
658 if (vm_error == KERN_SUCCESS)
659 vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
660 addr, addr + size, prot, FALSE);
665 case KERN_PROTECTION_FAILURE:
667 case KERN_RESOURCE_SHORTAGE:
673 #ifndef _SYS_SYSPROTO_H_
674 struct minherit_args {
681 sys_minherit(struct thread *td, struct minherit_args *uap)
684 vm_size_t size, pageoff;
685 vm_inherit_t inherit;
687 addr = (vm_offset_t)uap->addr;
689 inherit = uap->inherit;
691 pageoff = (addr & PAGE_MASK);
694 size = (vm_size_t) round_page(size);
695 if (addr + size < addr)
698 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
699 addr + size, inherit)) {
702 case KERN_PROTECTION_FAILURE:
708 #ifndef _SYS_SYSPROTO_H_
709 struct madvise_args {
717 sys_madvise(struct thread *td, struct madvise_args *uap)
720 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
724 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
727 vm_offset_t addr, end, start;
731 * Check for our special case, advising the swap pager we are
734 if (behav == MADV_PROTECT) {
736 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
737 PROC_SPROTECT, &flags));
741 * Check for illegal addresses. Watch out for address wrap... Note
742 * that VM_*_ADDRESS are not constants due to casts (argh).
744 map = &td->td_proc->p_vmspace->vm_map;
746 if (addr < vm_map_min(map) || addr + len > vm_map_max(map))
748 if ((addr + len) < addr)
752 * Since this routine is only advisory, we default to conservative
755 start = trunc_page(addr);
756 end = round_page(addr + len);
759 * vm_map_madvise() checks for illegal values of behav.
761 return (vm_map_madvise(map, start, end, behav));
764 #ifndef _SYS_SYSPROTO_H_
765 struct mincore_args {
773 sys_mincore(struct thread *td, struct mincore_args *uap)
776 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
780 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
784 vm_map_entry_t current, entry;
786 vm_offset_t addr, cend, end, first_addr;
790 int error, lastvecindex, mincoreinfo, vecindex;
791 unsigned int timestamp;
794 * Make sure that the addresses presented are valid for user
797 first_addr = addr = trunc_page(addr0);
798 end = round_page(addr0 + len);
799 map = &td->td_proc->p_vmspace->vm_map;
800 if (end > vm_map_max(map) || end < addr)
803 pmap = vmspace_pmap(td->td_proc->p_vmspace);
805 vm_map_lock_read(map);
807 timestamp = map->timestamp;
809 if (!vm_map_lookup_entry(map, addr, &entry)) {
810 vm_map_unlock_read(map);
815 * Do this on a map entry basis so that if the pages are not
816 * in the current processes address space, we can easily look
817 * up the pages elsewhere.
820 while (entry->start < end) {
823 * check for contiguity
826 entry = vm_map_entry_succ(current);
827 if (current->end < end &&
828 entry->start > current->end) {
829 vm_map_unlock_read(map);
834 * ignore submaps (for now) or null objects
836 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
837 current->object.vm_object == NULL)
841 * limit this scan to the current map entry and the
842 * limits for the mincore call
844 if (addr < current->start)
845 addr = current->start;
850 for (; addr < cend; addr += PAGE_SIZE) {
852 * Check pmap first, it is likely faster, also
853 * it can provide info as to whether we are the
854 * one referencing or modifying the page.
860 mincoreinfo = pmap_mincore(pmap, addr, &pa);
861 if (mincore_mapped) {
863 * We only care about this pmap's
864 * mapping of the page, if any.
867 } else if (pa != 0) {
869 * The page is mapped by this process but not
870 * both accessed and modified. It is also
871 * managed. Acquire the object lock so that
872 * other mappings might be examined. The page's
873 * identity may change at any point before its
874 * object lock is acquired, so re-validate if
877 m = PHYS_TO_VM_PAGE(pa);
878 while (object == NULL || m->object != object) {
880 VM_OBJECT_WUNLOCK(object);
881 object = (vm_object_t)atomic_load_ptr(
885 VM_OBJECT_WLOCK(object);
887 if (pa != pmap_extract(pmap, addr))
889 KASSERT(vm_page_all_valid(m),
890 ("mincore: page %p is mapped but invalid",
892 } else if (mincoreinfo == 0) {
894 * The page is not mapped by this process. If
895 * the object implements managed pages, then
896 * determine if the page is resident so that
897 * the mappings might be examined.
899 if (current->object.vm_object != object) {
901 VM_OBJECT_WUNLOCK(object);
902 object = current->object.vm_object;
903 VM_OBJECT_WLOCK(object);
905 if (object->type == OBJT_DEFAULT ||
906 object->type == OBJT_SWAP ||
907 object->type == OBJT_VNODE) {
908 pindex = OFF_TO_IDX(current->offset +
909 (addr - current->start));
910 m = vm_page_lookup(object, pindex);
911 if (m != NULL && vm_page_none_valid(m))
914 mincoreinfo = MINCORE_INCORE;
918 VM_OBJECT_ASSERT_WLOCKED(m->object);
920 /* Examine other mappings of the page. */
921 if (m->dirty == 0 && pmap_is_modified(m))
924 mincoreinfo |= MINCORE_MODIFIED_OTHER;
927 * The first test for PGA_REFERENCED is an
928 * optimization. The second test is
929 * required because a concurrent pmap
930 * operation could clear the last reference
931 * and set PGA_REFERENCED before the call to
932 * pmap_is_referenced().
934 if ((m->aflags & PGA_REFERENCED) != 0 ||
935 pmap_is_referenced(m) ||
936 (m->aflags & PGA_REFERENCED) != 0)
937 mincoreinfo |= MINCORE_REFERENCED_OTHER;
940 VM_OBJECT_WUNLOCK(object);
943 * subyte may page fault. In case it needs to modify
944 * the map, we release the lock.
946 vm_map_unlock_read(map);
949 * calculate index into user supplied byte vector
951 vecindex = atop(addr - first_addr);
954 * If we have skipped map entries, we need to make sure that
955 * the byte vector is zeroed for those skipped entries.
957 while ((lastvecindex + 1) < vecindex) {
959 error = subyte(vec + lastvecindex, 0);
967 * Pass the page information to the user
969 error = subyte(vec + vecindex, mincoreinfo);
976 * If the map has changed, due to the subyte, the previous
977 * output may be invalid.
979 vm_map_lock_read(map);
980 if (timestamp != map->timestamp)
983 lastvecindex = vecindex;
988 * subyte may page fault. In case it needs to modify
989 * the map, we release the lock.
991 vm_map_unlock_read(map);
994 * Zero the last entries in the byte vector.
996 vecindex = atop(end - first_addr);
997 while ((lastvecindex + 1) < vecindex) {
999 error = subyte(vec + lastvecindex, 0);
1007 * If the map has changed, due to the subyte, the previous
1008 * output may be invalid.
1010 vm_map_lock_read(map);
1011 if (timestamp != map->timestamp)
1013 vm_map_unlock_read(map);
1018 #ifndef _SYS_SYSPROTO_H_
1025 sys_mlock(struct thread *td, struct mlock_args *uap)
1028 return (kern_mlock(td->td_proc, td->td_ucred,
1029 __DECONST(uintptr_t, uap->addr), uap->len));
1033 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1035 vm_offset_t addr, end, last, start;
1036 vm_size_t npages, size;
1038 unsigned long nsize;
1041 error = priv_check_cred(cred, PRIV_VM_MLOCK);
1047 start = trunc_page(addr);
1048 end = round_page(last);
1049 if (last < addr || end < addr)
1051 npages = atop(end - start);
1052 if (npages > vm_page_max_user_wired)
1054 map = &proc->p_vmspace->vm_map;
1056 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1057 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1065 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1071 error = vm_map_wire(map, start, end,
1072 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1074 if (racct_enable && error != KERN_SUCCESS) {
1076 racct_set(proc, RACCT_MEMLOCK,
1077 ptoa(pmap_wired_count(map->pmap)));
1081 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1084 #ifndef _SYS_SYSPROTO_H_
1085 struct mlockall_args {
1091 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1096 map = &td->td_proc->p_vmspace->vm_map;
1097 error = priv_check(td, PRIV_VM_MLOCK);
1101 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1105 * If wiring all pages in the process would cause it to exceed
1106 * a hard resource limit, return ENOMEM.
1108 if (!old_mlock && uap->how & MCL_CURRENT) {
1109 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1114 PROC_LOCK(td->td_proc);
1115 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1116 PROC_UNLOCK(td->td_proc);
1122 if (uap->how & MCL_FUTURE) {
1124 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1129 if (uap->how & MCL_CURRENT) {
1131 * P1003.1-2001 mandates that all currently mapped pages
1132 * will be memory resident and locked (wired) upon return
1133 * from mlockall(). vm_map_wire() will wire pages, by
1134 * calling vm_fault_wire() for each page in the region.
1136 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1137 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1138 if (error == KERN_SUCCESS)
1140 else if (error == KERN_RESOURCE_SHORTAGE)
1146 if (racct_enable && error != KERN_SUCCESS) {
1147 PROC_LOCK(td->td_proc);
1148 racct_set(td->td_proc, RACCT_MEMLOCK,
1149 ptoa(pmap_wired_count(map->pmap)));
1150 PROC_UNLOCK(td->td_proc);
1157 #ifndef _SYS_SYSPROTO_H_
1158 struct munlockall_args {
1164 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1169 map = &td->td_proc->p_vmspace->vm_map;
1170 error = priv_check(td, PRIV_VM_MUNLOCK);
1174 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1176 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1179 /* Forcibly unwire all pages. */
1180 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1181 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1183 if (racct_enable && error == KERN_SUCCESS) {
1184 PROC_LOCK(td->td_proc);
1185 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1186 PROC_UNLOCK(td->td_proc);
1193 #ifndef _SYS_SYSPROTO_H_
1194 struct munlock_args {
1200 sys_munlock(struct thread *td, struct munlock_args *uap)
1203 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1207 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1209 vm_offset_t addr, end, last, start;
1215 error = priv_check(td, PRIV_VM_MUNLOCK);
1220 start = trunc_page(addr);
1221 end = round_page(last);
1222 if (last < addr || end < addr)
1224 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1225 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1227 if (racct_enable && error == KERN_SUCCESS) {
1228 PROC_LOCK(td->td_proc);
1229 map = &td->td_proc->p_vmspace->vm_map;
1230 racct_set(td->td_proc, RACCT_MEMLOCK,
1231 ptoa(pmap_wired_count(map->pmap)));
1232 PROC_UNLOCK(td->td_proc);
1235 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1241 * Helper function for vm_mmap. Perform sanity check specific for mmap
1242 * operations on vnodes.
1245 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1246 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1247 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1248 boolean_t *writecounted)
1257 cred = td->td_ucred;
1258 writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
1259 (*flagsp & MAP_SHARED) != 0;
1260 if ((error = vget(vp, LK_SHARED, td)) != 0)
1262 AUDIT_ARG_VNODE1(vp);
1266 if (vp->v_type == VREG) {
1268 * Get the proper underlying object
1274 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1276 vp = (struct vnode *)obj->handle;
1278 * Bypass filesystems obey the mpsafety of the
1279 * underlying fs. Tmpfs never bypasses.
1281 error = vget(vp, LK_SHARED, td);
1286 *writecounted = TRUE;
1287 vm_pager_update_writecount(obj, 0, objsize);
1293 if ((error = VOP_GETATTR(vp, &va, cred)))
1296 /* This relies on VM_PROT_* matching PROT_*. */
1297 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1301 if ((flags & MAP_SHARED) != 0) {
1302 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1303 if (prot & VM_PROT_WRITE) {
1307 *maxprotp &= ~VM_PROT_WRITE;
1311 * If it is a regular file without any references
1312 * we do not need to sync it.
1313 * Adjust object size to be the size of actual file.
1315 objsize = round_page(va.va_size);
1316 if (va.va_nlink == 0)
1317 flags |= MAP_NOSYNC;
1318 if (obj->type == OBJT_VNODE) {
1319 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1326 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1327 ("wrong object type"));
1328 VM_OBJECT_WLOCK(obj);
1329 vm_object_reference_locked(obj);
1330 #if VM_NRESERVLEVEL > 0
1331 vm_object_color(obj, 0);
1333 VM_OBJECT_WUNLOCK(obj);
1338 vfs_mark_atime(vp, cred);
1341 if (error != 0 && *writecounted) {
1342 *writecounted = FALSE;
1343 vm_pager_update_writecount(obj, objsize, 0);
1352 * Helper function for vm_mmap. Perform sanity check specific for mmap
1353 * operations on cdevs.
1356 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1357 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1358 vm_ooffset_t *foff, vm_object_t *objp)
1365 if (dsw->d_flags & D_MMAP_ANON) {
1368 *maxprotp = VM_PROT_ALL;
1369 *flagsp |= MAP_ANON;
1373 * cdevs do not provide private mappings of any kind.
1375 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1376 (prot & VM_PROT_WRITE) != 0)
1378 if (flags & (MAP_PRIVATE|MAP_COPY))
1381 * Force device mappings to be shared.
1383 flags |= MAP_SHARED;
1385 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1390 * First, try d_mmap_single(). If that is not implemented
1391 * (returns ENODEV), fall back to using the device pager.
1392 * Note that d_mmap_single() must return a reference to the
1393 * object (it needs to bump the reference count of the object
1394 * it returns somehow).
1396 * XXX assumes VM_PROT_* == PROT_*
1398 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1399 if (error != ENODEV)
1401 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1413 * Internal version of mmap used by exec, sys5 shared memory, and
1414 * various device drivers. Handle is either a vnode pointer, a
1415 * character device, or NULL for MAP_ANON.
1418 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1419 vm_prot_t maxprot, int flags,
1420 objtype_t handle_type, void *handle,
1424 struct thread *td = curthread;
1426 boolean_t writecounted;
1431 size = round_page(size);
1433 writecounted = FALSE;
1436 * Lookup/allocate object.
1438 switch (handle_type) {
1445 dsw = dev_refthread(cdev, &ref);
1448 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1449 dsw, &foff, &object);
1450 dev_relthread(cdev, ref);
1454 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1455 handle, &foff, &object, &writecounted);
1458 if (handle == NULL) {
1470 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1471 foff, writecounted, td);
1472 if (error != 0 && object != NULL) {
1474 * If this mapping was accounted for in the vnode's
1475 * writecount, then undo that now.
1478 vm_pager_release_writecount(object, 0, size);
1479 vm_object_deallocate(object);
1485 * Internal version of mmap that maps a specific VM object into an
1486 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1489 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1490 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1491 boolean_t writecounted, struct thread *td)
1493 boolean_t curmap, fitit;
1494 vm_offset_t max_addr;
1495 int docow, error, findspace, rv;
1497 curmap = map == &td->td_proc->p_vmspace->vm_map;
1499 RACCT_PROC_LOCK(td->td_proc);
1500 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1501 RACCT_PROC_UNLOCK(td->td_proc);
1504 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1505 RACCT_PROC_UNLOCK(td->td_proc);
1508 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1509 if (ptoa(pmap_wired_count(map->pmap)) + size >
1510 lim_cur(td, RLIMIT_MEMLOCK)) {
1511 racct_set_force(td->td_proc, RACCT_VMEM,
1513 RACCT_PROC_UNLOCK(td->td_proc);
1516 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1517 ptoa(pmap_wired_count(map->pmap)) + size);
1519 racct_set_force(td->td_proc, RACCT_VMEM,
1521 RACCT_PROC_UNLOCK(td->td_proc);
1525 RACCT_PROC_UNLOCK(td->td_proc);
1529 * We currently can only deal with page aligned file offsets.
1530 * The mmap() system call already enforces this by subtracting
1531 * the page offset from the file offset, but checking here
1532 * catches errors in device drivers (e.g. d_single_mmap()
1533 * callbacks) and other internal mapping requests (such as in
1536 if (foff & PAGE_MASK)
1539 if ((flags & MAP_FIXED) == 0) {
1541 *addr = round_page(*addr);
1543 if (*addr != trunc_page(*addr))
1548 if (flags & MAP_ANON) {
1549 if (object != NULL || foff != 0)
1552 } else if (flags & MAP_PREFAULT_READ)
1553 docow = MAP_PREFAULT;
1555 docow = MAP_PREFAULT_PARTIAL;
1557 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1558 docow |= MAP_COPY_ON_WRITE;
1559 if (flags & MAP_NOSYNC)
1560 docow |= MAP_DISABLE_SYNCER;
1561 if (flags & MAP_NOCORE)
1562 docow |= MAP_DISABLE_COREDUMP;
1563 /* Shared memory is also shared with children. */
1564 if (flags & MAP_SHARED)
1565 docow |= MAP_INHERIT_SHARE;
1567 docow |= MAP_WRITECOUNT;
1568 if (flags & MAP_STACK) {
1571 docow |= MAP_STACK_GROWS_DOWN;
1573 if ((flags & MAP_EXCL) != 0)
1574 docow |= MAP_CHECK_EXCL;
1575 if ((flags & MAP_GUARD) != 0)
1576 docow |= MAP_CREATE_GUARD;
1579 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1580 findspace = VMFS_SUPER_SPACE;
1581 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1582 findspace = VMFS_ALIGNED_SPACE(flags >>
1583 MAP_ALIGNMENT_SHIFT);
1585 findspace = VMFS_OPTIMAL_SPACE;
1588 if ((flags & MAP_32BIT) != 0)
1589 max_addr = MAP_32BIT_MAX_ADDR;
1592 rv = vm_map_find_min(map, object, foff, addr, size,
1593 round_page((vm_offset_t)td->td_proc->p_vmspace->
1594 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1595 findspace, prot, maxprot, docow);
1597 rv = vm_map_find(map, object, foff, addr, size,
1598 max_addr, findspace, prot, maxprot, docow);
1601 rv = vm_map_fixed(map, object, foff, *addr, size,
1602 prot, maxprot, docow);
1605 if (rv == KERN_SUCCESS) {
1607 * If the process has requested that all future mappings
1608 * be wired, then heed this.
1610 if ((map->flags & MAP_WIREFUTURE) != 0) {
1612 if ((map->flags & MAP_WIREFUTURE) != 0)
1613 (void)vm_map_wire_locked(map, *addr,
1614 *addr + size, VM_MAP_WIRE_USER |
1615 ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
1616 VM_MAP_WIRE_NOHOLES));
1620 return (vm_mmap_to_errno(rv));
1624 * Translate a Mach VM return code to zero on success or the appropriate errno
1628 vm_mmap_to_errno(int rv)
1634 case KERN_INVALID_ADDRESS:
1637 case KERN_PROTECTION_FAILURE: