2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
38 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
42 * Mapped file (mmap) interface to VM
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "opt_hwpmc_hooks.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/capsicum.h>
54 #include <sys/kernel.h>
56 #include <sys/mutex.h>
57 #include <sys/sysproto.h>
58 #include <sys/filedesc.h>
61 #include <sys/procctl.h>
62 #include <sys/racct.h>
63 #include <sys/resource.h>
64 #include <sys/resourcevar.h>
65 #include <sys/rwlock.h>
66 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
68 #include <sys/fcntl.h>
71 #include <sys/mount.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysent.h>
76 #include <sys/vmmeter.h>
78 #include <security/audit/audit.h>
79 #include <security/mac/mac_framework.h>
82 #include <vm/vm_param.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_pager.h>
88 #include <vm/vm_pageout.h>
89 #include <vm/vm_extern.h>
90 #include <vm/vm_page.h>
91 #include <vm/vnode_pager.h>
94 #include <sys/pmckern.h>
98 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
99 "Do not apply RLIMIT_MEMLOCK on mlockall");
100 static int mincore_mapped = 1;
101 SYSCTL_INT(_vm, OID_AUTO, mincore_mapped, CTLFLAG_RWTUN, &mincore_mapped, 0,
102 "mincore reports mappings, not residency");
105 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
108 #ifndef _SYS_SYSPROTO_H_
115 sys_sbrk(struct thread *td, struct sbrk_args *uap)
117 /* Not yet implemented */
121 #ifndef _SYS_SYSPROTO_H_
128 sys_sstk(struct thread *td, struct sstk_args *uap)
130 /* Not yet implemented */
134 #if defined(COMPAT_43)
135 #ifndef _SYS_SYSPROTO_H_
136 struct getpagesize_args {
142 ogetpagesize(struct thread *td, struct getpagesize_args *uap)
145 td->td_retval[0] = PAGE_SIZE;
148 #endif /* COMPAT_43 */
152 * Memory Map (mmap) system call. Note that the file offset
153 * and address are allowed to be NOT page aligned, though if
154 * the MAP_FIXED flag it set, both must have the same remainder
155 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
156 * page-aligned, the actual mapping starts at trunc_page(addr)
157 * and the return value is adjusted up by the page offset.
159 * Generally speaking, only character devices which are themselves
160 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
161 * there would be no cache coherency between a descriptor and a VM mapping
162 * both to the same character device.
164 #ifndef _SYS_SYSPROTO_H_
177 sys_mmap(struct thread *td, struct mmap_args *uap)
180 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
181 uap->flags, uap->fd, uap->pos));
185 kern_mmap(struct thread *td, uintptr_t addr0, size_t size, int prot, int flags,
189 return (kern_mmap_fpcheck(td, addr0, size, prot, flags, fd, pos, NULL));
193 * When mmap'ing a file, check_fp_fn may be used for the caller to do any
194 * last-minute validation based on the referenced file in a non-racy way.
197 kern_mmap_fpcheck(struct thread *td, uintptr_t addr0, size_t size, int prot,
198 int flags, int fd, off_t pos, mmap_check_fp_fn check_fp_fn)
205 vm_prot_t cap_maxprot;
216 * Ignore old flags that used to be defined but did not do anything.
218 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
221 * Enforce the constraints.
222 * Mapping of length 0 is only allowed for old binaries.
223 * Anonymous mapping shall specify -1 as filedescriptor and
224 * zero position for new code. Be nice to ancient a.out
225 * binaries and correct pos for anonymous mapping, since old
226 * ld.so sometimes issues anonymous map requests with non-zero
229 if (!SV_CURPROC_FLAG(SV_AOUT)) {
230 if ((size == 0 && p->p_osrel >= P_OSREL_MAP_ANON) ||
231 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
234 if ((flags & MAP_ANON) != 0)
238 if (flags & MAP_STACK) {
240 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
245 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
246 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
247 MAP_PREFAULT_READ | MAP_GUARD |
251 MAP_ALIGNMENT_MASK)) != 0)
253 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
255 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
257 if (prot != PROT_NONE &&
258 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
260 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
261 pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
265 MAP_ALIGNMENT_MASK)) != 0))
269 * Align the file position to a page boundary,
270 * and save its page offset component.
272 pageoff = (pos & PAGE_MASK);
275 /* Adjust size for rounding (on both ends). */
276 size += pageoff; /* low end... */
277 size = (vm_size_t) round_page(size); /* hi end */
279 /* Ensure alignment is at least a page and fits in a pointer. */
280 align = flags & MAP_ALIGNMENT_MASK;
281 if (align != 0 && align != MAP_ALIGNED_SUPER &&
282 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
283 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
287 * Check for illegal addresses. Watch out for address wrap... Note
288 * that VM_*_ADDRESS are not constants due to casts (argh).
290 if (flags & MAP_FIXED) {
292 * The specified address must have the same remainder
293 * as the file offset taken modulo PAGE_SIZE, so it
294 * should be aligned after adjustment by pageoff.
297 if (addr & PAGE_MASK)
300 /* Address range must be all in user VM space. */
301 if (!vm_map_range_valid(&vms->vm_map, addr, addr + size))
304 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
306 } else if (flags & MAP_32BIT) {
308 * For MAP_32BIT, override the hint if it is too high and
309 * do not bother moving the mapping past the heap (since
310 * the heap is usually above 2GB).
312 if (addr + size > MAP_32BIT_MAX_ADDR)
317 * XXX for non-fixed mappings where no hint is provided or
318 * the hint would fall in the potential heap space,
319 * place it after the end of the largest possible heap.
321 * There should really be a pmap call to determine a reasonable
325 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
326 addr < round_page((vm_offset_t)vms->vm_daddr +
327 lim_max(td, RLIMIT_DATA))))
328 addr = round_page((vm_offset_t)vms->vm_daddr +
329 lim_max(td, RLIMIT_DATA));
333 * Return success without mapping anything for old
334 * binaries that request a page-aligned mapping of
335 * length 0. For modern binaries, this function
336 * returns an error earlier.
339 } else if ((flags & MAP_GUARD) != 0) {
340 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
341 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
342 } else if ((flags & MAP_ANON) != 0) {
344 * Mapping blank space is trivial.
346 * This relies on VM_PROT_* matching PROT_*.
348 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
349 VM_PROT_ALL, flags, NULL, pos, FALSE, td);
352 * Mapping file, get fp for validation and don't let the
353 * descriptor disappear on us if we block. Check capability
354 * rights, but also return the maximum rights to be combined
355 * with maxprot later.
357 cap_rights_init(&rights, CAP_MMAP);
358 if (prot & PROT_READ)
359 cap_rights_set(&rights, CAP_MMAP_R);
360 if ((flags & MAP_SHARED) != 0) {
361 if (prot & PROT_WRITE)
362 cap_rights_set(&rights, CAP_MMAP_W);
364 if (prot & PROT_EXEC)
365 cap_rights_set(&rights, CAP_MMAP_X);
366 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
369 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
370 p->p_osrel >= P_OSREL_MAP_FSTRICT) {
374 if (check_fp_fn != NULL) {
375 error = check_fp_fn(fp, prot, cap_maxprot, flags);
379 /* This relies on VM_PROT_* matching PROT_*. */
380 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
381 cap_maxprot, flags, pos, td);
385 td->td_retval[0] = (register_t) (addr + pageoff);
393 #if defined(COMPAT_FREEBSD6)
395 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
398 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
399 uap->flags, uap->fd, uap->pos));
404 #ifndef _SYS_SYSPROTO_H_
415 ommap(struct thread *td, struct ommap_args *uap)
417 static const char cvtbsdprot[8] = {
421 PROT_EXEC | PROT_WRITE,
423 PROT_EXEC | PROT_READ,
424 PROT_WRITE | PROT_READ,
425 PROT_EXEC | PROT_WRITE | PROT_READ,
429 #define OMAP_ANON 0x0002
430 #define OMAP_COPY 0x0020
431 #define OMAP_SHARED 0x0010
432 #define OMAP_FIXED 0x0100
434 prot = cvtbsdprot[uap->prot & 0x7];
435 #ifdef COMPAT_FREEBSD32
436 #if defined(__amd64__)
437 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
443 if (uap->flags & OMAP_ANON)
445 if (uap->flags & OMAP_COPY)
447 if (uap->flags & OMAP_SHARED)
450 flags |= MAP_PRIVATE;
451 if (uap->flags & OMAP_FIXED)
453 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
456 #endif /* COMPAT_43 */
459 #ifndef _SYS_SYSPROTO_H_
467 sys_msync(struct thread *td, struct msync_args *uap)
470 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
474 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
482 pageoff = (addr & PAGE_MASK);
485 size = (vm_size_t) round_page(size);
486 if (addr + size < addr)
489 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
492 map = &td->td_proc->p_vmspace->vm_map;
495 * Clean the pages and interpret the return value.
497 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
498 (flags & MS_INVALIDATE) != 0);
502 case KERN_INVALID_ADDRESS:
504 case KERN_INVALID_ARGUMENT:
513 #ifndef _SYS_SYSPROTO_H_
520 sys_munmap(struct thread *td, struct munmap_args *uap)
523 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
527 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
530 struct pmckern_map_out pkm;
531 vm_map_entry_t entry;
534 vm_offset_t addr, end;
542 pageoff = (addr & PAGE_MASK);
545 size = (vm_size_t) round_page(size);
547 map = &td->td_proc->p_vmspace->vm_map;
548 if (!vm_map_range_valid(map, addr, end))
554 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
557 * Inform hwpmc if the address range being unmapped contains
558 * an executable region.
560 pkm.pm_address = (uintptr_t) NULL;
561 if (vm_map_lookup_entry(map, addr, &entry)) {
562 for (; entry->start < end;
563 entry = entry->next) {
564 if (vm_map_check_protection(map, entry->start,
565 entry->end, VM_PROT_EXECUTE) == TRUE) {
566 pkm.pm_address = (uintptr_t) addr;
567 pkm.pm_size = (size_t) size;
574 vm_map_delete(map, addr, end);
577 if (__predict_false(pmc_handled)) {
578 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
579 vm_map_lock_downgrade(map);
580 if (pkm.pm_address != (uintptr_t) NULL)
581 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
582 vm_map_unlock_read(map);
587 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
591 #ifndef _SYS_SYSPROTO_H_
592 struct mprotect_args {
599 sys_mprotect(struct thread *td, struct mprotect_args *uap)
602 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len, uap->prot));
606 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot)
612 prot = (prot & VM_PROT_ALL);
613 pageoff = (addr & PAGE_MASK);
616 size = (vm_size_t) round_page(size);
617 #ifdef COMPAT_FREEBSD32
618 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
619 if (((addr + size) & 0xffffffff) < addr)
623 if (addr + size < addr)
626 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
627 addr + size, prot, FALSE)) {
630 case KERN_PROTECTION_FAILURE:
632 case KERN_RESOURCE_SHORTAGE:
638 #ifndef _SYS_SYSPROTO_H_
639 struct minherit_args {
646 sys_minherit(struct thread *td, struct minherit_args *uap)
649 vm_size_t size, pageoff;
650 vm_inherit_t inherit;
652 addr = (vm_offset_t)uap->addr;
654 inherit = uap->inherit;
656 pageoff = (addr & PAGE_MASK);
659 size = (vm_size_t) round_page(size);
660 if (addr + size < addr)
663 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
664 addr + size, inherit)) {
667 case KERN_PROTECTION_FAILURE:
673 #ifndef _SYS_SYSPROTO_H_
674 struct madvise_args {
682 sys_madvise(struct thread *td, struct madvise_args *uap)
685 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
689 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
692 vm_offset_t addr, end, start;
696 * Check for our special case, advising the swap pager we are
699 if (behav == MADV_PROTECT) {
701 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
702 PROC_SPROTECT, &flags));
706 * Check for illegal addresses. Watch out for address wrap... Note
707 * that VM_*_ADDRESS are not constants due to casts (argh).
709 map = &td->td_proc->p_vmspace->vm_map;
711 if (!vm_map_range_valid(map, addr, addr + len))
715 * Since this routine is only advisory, we default to conservative
718 start = trunc_page(addr);
719 end = round_page(addr + len);
722 * vm_map_madvise() checks for illegal values of behav.
724 return (vm_map_madvise(map, start, end, behav));
727 #ifndef _SYS_SYSPROTO_H_
728 struct mincore_args {
736 sys_mincore(struct thread *td, struct mincore_args *uap)
739 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
743 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
745 vm_offset_t addr, first_addr;
746 vm_offset_t end, cend;
750 int vecindex, lastvecindex;
751 vm_map_entry_t current;
752 vm_map_entry_t entry;
754 vm_paddr_t locked_pa;
758 unsigned int timestamp;
762 * Make sure that the addresses presented are valid for user
765 first_addr = addr = trunc_page(addr0);
766 end = addr + (vm_size_t)round_page(len);
767 map = &td->td_proc->p_vmspace->vm_map;
768 if (end > vm_map_max(map) || end < addr)
771 pmap = vmspace_pmap(td->td_proc->p_vmspace);
773 vm_map_lock_read(map);
775 timestamp = map->timestamp;
777 if (!vm_map_lookup_entry(map, addr, &entry)) {
778 vm_map_unlock_read(map);
783 * Do this on a map entry basis so that if the pages are not
784 * in the current processes address space, we can easily look
785 * up the pages elsewhere.
788 for (current = entry; current->start < end; current = current->next) {
791 * check for contiguity
793 if (current->end < end && current->next->start > current->end) {
794 vm_map_unlock_read(map);
799 * ignore submaps (for now) or null objects
801 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
802 current->object.vm_object == NULL)
806 * limit this scan to the current map entry and the
807 * limits for the mincore call
809 if (addr < current->start)
810 addr = current->start;
816 * scan this entry one page at a time
818 while (addr < cend) {
820 * Check pmap first, it is likely faster, also
821 * it can provide info as to whether we are the
822 * one referencing or modifying the page.
828 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
829 if (mincore_mapped) {
831 * We only care about this pmap's
832 * mapping of the page, if any.
834 if (locked_pa != 0) {
835 vm_page_unlock(PHYS_TO_VM_PAGE(
838 } else if (locked_pa != 0) {
840 * The page is mapped by this process but not
841 * both accessed and modified. It is also
842 * managed. Acquire the object lock so that
843 * other mappings might be examined.
845 m = PHYS_TO_VM_PAGE(locked_pa);
846 if (m->object != object) {
848 VM_OBJECT_WUNLOCK(object);
850 locked = VM_OBJECT_TRYWLOCK(object);
853 VM_OBJECT_WLOCK(object);
859 KASSERT(m->valid == VM_PAGE_BITS_ALL,
860 ("mincore: page %p is mapped but invalid",
862 } else if (mincoreinfo == 0) {
864 * The page is not mapped by this process. If
865 * the object implements managed pages, then
866 * determine if the page is resident so that
867 * the mappings might be examined.
869 if (current->object.vm_object != object) {
871 VM_OBJECT_WUNLOCK(object);
872 object = current->object.vm_object;
873 VM_OBJECT_WLOCK(object);
875 if (object->type == OBJT_DEFAULT ||
876 object->type == OBJT_SWAP ||
877 object->type == OBJT_VNODE) {
878 pindex = OFF_TO_IDX(current->offset +
879 (addr - current->start));
880 m = vm_page_lookup(object, pindex);
881 if (m != NULL && m->valid == 0)
884 mincoreinfo = MINCORE_INCORE;
888 /* Examine other mappings to the page. */
889 if (m->dirty == 0 && pmap_is_modified(m))
892 mincoreinfo |= MINCORE_MODIFIED_OTHER;
894 * The first test for PGA_REFERENCED is an
895 * optimization. The second test is
896 * required because a concurrent pmap
897 * operation could clear the last reference
898 * and set PGA_REFERENCED before the call to
899 * pmap_is_referenced().
901 if ((m->aflags & PGA_REFERENCED) != 0 ||
902 pmap_is_referenced(m) ||
903 (m->aflags & PGA_REFERENCED) != 0)
904 mincoreinfo |= MINCORE_REFERENCED_OTHER;
907 VM_OBJECT_WUNLOCK(object);
910 * subyte may page fault. In case it needs to modify
911 * the map, we release the lock.
913 vm_map_unlock_read(map);
916 * calculate index into user supplied byte vector
918 vecindex = atop(addr - first_addr);
921 * If we have skipped map entries, we need to make sure that
922 * the byte vector is zeroed for those skipped entries.
924 while ((lastvecindex + 1) < vecindex) {
926 error = subyte(vec + lastvecindex, 0);
934 * Pass the page information to the user
936 error = subyte(vec + vecindex, mincoreinfo);
943 * If the map has changed, due to the subyte, the previous
944 * output may be invalid.
946 vm_map_lock_read(map);
947 if (timestamp != map->timestamp)
950 lastvecindex = vecindex;
956 * subyte may page fault. In case it needs to modify
957 * the map, we release the lock.
959 vm_map_unlock_read(map);
962 * Zero the last entries in the byte vector.
964 vecindex = atop(end - first_addr);
965 while ((lastvecindex + 1) < vecindex) {
967 error = subyte(vec + lastvecindex, 0);
975 * If the map has changed, due to the subyte, the previous
976 * output may be invalid.
978 vm_map_lock_read(map);
979 if (timestamp != map->timestamp)
981 vm_map_unlock_read(map);
986 #ifndef _SYS_SYSPROTO_H_
993 sys_mlock(struct thread *td, struct mlock_args *uap)
996 return (kern_mlock(td->td_proc, td->td_ucred,
997 __DECONST(uintptr_t, uap->addr), uap->len));
1001 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1003 vm_offset_t addr, end, last, start;
1004 vm_size_t npages, size;
1006 unsigned long nsize;
1009 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
1015 start = trunc_page(addr);
1016 end = round_page(last);
1017 if (last < addr || end < addr)
1019 npages = atop(end - start);
1020 if ((u_int)npages > vm_page_max_user_wired)
1022 map = &proc->p_vmspace->vm_map;
1024 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1025 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1033 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1039 error = vm_map_wire(map, start, end,
1040 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1042 if (racct_enable && error != KERN_SUCCESS) {
1044 racct_set(proc, RACCT_MEMLOCK,
1045 ptoa(pmap_wired_count(map->pmap)));
1049 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1052 #ifndef _SYS_SYSPROTO_H_
1053 struct mlockall_args {
1059 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1064 map = &td->td_proc->p_vmspace->vm_map;
1065 error = priv_check(td, PRIV_VM_MLOCK);
1069 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1073 * If wiring all pages in the process would cause it to exceed
1074 * a hard resource limit, return ENOMEM.
1076 if (!old_mlock && uap->how & MCL_CURRENT) {
1077 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1082 PROC_LOCK(td->td_proc);
1083 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1084 PROC_UNLOCK(td->td_proc);
1090 if (uap->how & MCL_FUTURE) {
1092 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1097 if (uap->how & MCL_CURRENT) {
1099 * P1003.1-2001 mandates that all currently mapped pages
1100 * will be memory resident and locked (wired) upon return
1101 * from mlockall(). vm_map_wire() will wire pages, by
1102 * calling vm_fault_wire() for each page in the region.
1104 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1105 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1106 if (error == KERN_SUCCESS)
1108 else if (error == KERN_RESOURCE_SHORTAGE)
1114 if (racct_enable && error != KERN_SUCCESS) {
1115 PROC_LOCK(td->td_proc);
1116 racct_set(td->td_proc, RACCT_MEMLOCK,
1117 ptoa(pmap_wired_count(map->pmap)));
1118 PROC_UNLOCK(td->td_proc);
1125 #ifndef _SYS_SYSPROTO_H_
1126 struct munlockall_args {
1132 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1137 map = &td->td_proc->p_vmspace->vm_map;
1138 error = priv_check(td, PRIV_VM_MUNLOCK);
1142 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1144 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1147 /* Forcibly unwire all pages. */
1148 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1149 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1151 if (racct_enable && error == KERN_SUCCESS) {
1152 PROC_LOCK(td->td_proc);
1153 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1154 PROC_UNLOCK(td->td_proc);
1161 #ifndef _SYS_SYSPROTO_H_
1162 struct munlock_args {
1168 sys_munlock(struct thread *td, struct munlock_args *uap)
1171 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1175 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1177 vm_offset_t addr, end, last, start;
1183 error = priv_check(td, PRIV_VM_MUNLOCK);
1188 start = trunc_page(addr);
1189 end = round_page(last);
1190 if (last < addr || end < addr)
1192 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1193 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1195 if (racct_enable && error == KERN_SUCCESS) {
1196 PROC_LOCK(td->td_proc);
1197 map = &td->td_proc->p_vmspace->vm_map;
1198 racct_set(td->td_proc, RACCT_MEMLOCK,
1199 ptoa(pmap_wired_count(map->pmap)));
1200 PROC_UNLOCK(td->td_proc);
1203 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1209 * Helper function for vm_mmap. Perform sanity check specific for mmap
1210 * operations on vnodes.
1213 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1214 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1215 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1216 boolean_t *writecounted)
1225 cred = td->td_ucred;
1226 writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
1227 (*flagsp & MAP_SHARED) != 0;
1228 if ((error = vget(vp, LK_SHARED, td)) != 0)
1230 AUDIT_ARG_VNODE1(vp);
1234 if (vp->v_type == VREG) {
1236 * Get the proper underlying object
1242 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1244 vp = (struct vnode *)obj->handle;
1246 * Bypass filesystems obey the mpsafety of the
1247 * underlying fs. Tmpfs never bypasses.
1249 error = vget(vp, LK_SHARED, td);
1254 *writecounted = TRUE;
1255 vm_pager_update_writecount(obj, 0, objsize);
1261 if ((error = VOP_GETATTR(vp, &va, cred)))
1264 /* This relies on VM_PROT_* matching PROT_*. */
1265 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1269 if ((flags & MAP_SHARED) != 0) {
1270 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1271 if (prot & VM_PROT_WRITE) {
1275 *maxprotp &= ~VM_PROT_WRITE;
1279 * If it is a regular file without any references
1280 * we do not need to sync it.
1281 * Adjust object size to be the size of actual file.
1283 objsize = round_page(va.va_size);
1284 if (va.va_nlink == 0)
1285 flags |= MAP_NOSYNC;
1286 if (obj->type == OBJT_VNODE) {
1287 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1294 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1295 ("wrong object type"));
1296 VM_OBJECT_WLOCK(obj);
1297 vm_object_reference_locked(obj);
1298 #if VM_NRESERVLEVEL > 0
1299 vm_object_color(obj, 0);
1301 VM_OBJECT_WUNLOCK(obj);
1306 vfs_mark_atime(vp, cred);
1309 if (error != 0 && *writecounted) {
1310 *writecounted = FALSE;
1311 vm_pager_update_writecount(obj, objsize, 0);
1320 * Helper function for vm_mmap. Perform sanity check specific for mmap
1321 * operations on cdevs.
1324 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1325 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1326 vm_ooffset_t *foff, vm_object_t *objp)
1333 if (dsw->d_flags & D_MMAP_ANON) {
1336 *maxprotp = VM_PROT_ALL;
1337 *flagsp |= MAP_ANON;
1341 * cdevs do not provide private mappings of any kind.
1343 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1344 (prot & VM_PROT_WRITE) != 0)
1346 if (flags & (MAP_PRIVATE|MAP_COPY))
1349 * Force device mappings to be shared.
1351 flags |= MAP_SHARED;
1353 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1358 * First, try d_mmap_single(). If that is not implemented
1359 * (returns ENODEV), fall back to using the device pager.
1360 * Note that d_mmap_single() must return a reference to the
1361 * object (it needs to bump the reference count of the object
1362 * it returns somehow).
1364 * XXX assumes VM_PROT_* == PROT_*
1366 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1367 if (error != ENODEV)
1369 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1381 * Internal version of mmap used by exec, sys5 shared memory, and
1382 * various device drivers. Handle is either a vnode pointer, a
1383 * character device, or NULL for MAP_ANON.
1386 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1387 vm_prot_t maxprot, int flags,
1388 objtype_t handle_type, void *handle,
1392 struct thread *td = curthread;
1394 boolean_t writecounted;
1399 size = round_page(size);
1401 writecounted = FALSE;
1404 * Lookup/allocate object.
1406 switch (handle_type) {
1413 dsw = dev_refthread(cdev, &ref);
1416 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1417 dsw, &foff, &object);
1418 dev_relthread(cdev, ref);
1422 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1423 handle, &foff, &object, &writecounted);
1426 if (handle == NULL) {
1438 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1439 foff, writecounted, td);
1440 if (error != 0 && object != NULL) {
1442 * If this mapping was accounted for in the vnode's
1443 * writecount, then undo that now.
1446 vm_pager_release_writecount(object, 0, size);
1447 vm_object_deallocate(object);
1453 * Internal version of mmap that maps a specific VM object into an
1454 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1457 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1458 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1459 boolean_t writecounted, struct thread *td)
1461 boolean_t curmap, fitit;
1462 vm_offset_t max_addr;
1463 int docow, error, findspace, rv;
1465 curmap = map == &td->td_proc->p_vmspace->vm_map;
1467 RACCT_PROC_LOCK(td->td_proc);
1468 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1469 RACCT_PROC_UNLOCK(td->td_proc);
1472 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1473 RACCT_PROC_UNLOCK(td->td_proc);
1476 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1477 if (ptoa(pmap_wired_count(map->pmap)) + size >
1478 lim_cur(td, RLIMIT_MEMLOCK)) {
1479 racct_set_force(td->td_proc, RACCT_VMEM,
1481 RACCT_PROC_UNLOCK(td->td_proc);
1484 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1485 ptoa(pmap_wired_count(map->pmap)) + size);
1487 racct_set_force(td->td_proc, RACCT_VMEM,
1489 RACCT_PROC_UNLOCK(td->td_proc);
1493 RACCT_PROC_UNLOCK(td->td_proc);
1497 * We currently can only deal with page aligned file offsets.
1498 * The mmap() system call already enforces this by subtracting
1499 * the page offset from the file offset, but checking here
1500 * catches errors in device drivers (e.g. d_single_mmap()
1501 * callbacks) and other internal mapping requests (such as in
1504 if (foff & PAGE_MASK)
1507 if ((flags & MAP_FIXED) == 0) {
1509 *addr = round_page(*addr);
1511 if (*addr != trunc_page(*addr))
1516 if (flags & MAP_ANON) {
1517 if (object != NULL || foff != 0)
1520 } else if (flags & MAP_PREFAULT_READ)
1521 docow = MAP_PREFAULT;
1523 docow = MAP_PREFAULT_PARTIAL;
1525 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1526 docow |= MAP_COPY_ON_WRITE;
1527 if (flags & MAP_NOSYNC)
1528 docow |= MAP_DISABLE_SYNCER;
1529 if (flags & MAP_NOCORE)
1530 docow |= MAP_DISABLE_COREDUMP;
1531 /* Shared memory is also shared with children. */
1532 if (flags & MAP_SHARED)
1533 docow |= MAP_INHERIT_SHARE;
1535 docow |= MAP_WRITECOUNT;
1536 if (flags & MAP_STACK) {
1539 docow |= MAP_STACK_GROWS_DOWN;
1541 if ((flags & MAP_EXCL) != 0)
1542 docow |= MAP_CHECK_EXCL;
1543 if ((flags & MAP_GUARD) != 0)
1544 docow |= MAP_CREATE_GUARD;
1547 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1548 findspace = VMFS_SUPER_SPACE;
1549 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1550 findspace = VMFS_ALIGNED_SPACE(flags >>
1551 MAP_ALIGNMENT_SHIFT);
1553 findspace = VMFS_OPTIMAL_SPACE;
1556 if ((flags & MAP_32BIT) != 0)
1557 max_addr = MAP_32BIT_MAX_ADDR;
1560 rv = vm_map_find_min(map, object, foff, addr, size,
1561 round_page((vm_offset_t)td->td_proc->p_vmspace->
1562 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1563 findspace, prot, maxprot, docow);
1565 rv = vm_map_find(map, object, foff, addr, size,
1566 max_addr, findspace, prot, maxprot, docow);
1569 rv = vm_map_fixed(map, object, foff, *addr, size,
1570 prot, maxprot, docow);
1573 if (rv == KERN_SUCCESS) {
1575 * If the process has requested that all future mappings
1576 * be wired, then heed this.
1578 if ((map->flags & MAP_WIREFUTURE) != 0) {
1580 if ((map->flags & MAP_WIREFUTURE) != 0)
1581 (void)vm_map_wire_locked(map, *addr,
1582 *addr + size, VM_MAP_WIRE_USER |
1583 ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
1584 VM_MAP_WIRE_NOHOLES));
1588 return (vm_mmap_to_errno(rv));
1592 * Translate a Mach VM return code to zero on success or the appropriate errno
1596 vm_mmap_to_errno(int rv)
1602 case KERN_INVALID_ADDRESS:
1605 case KERN_PROTECTION_FAILURE: