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 return (kern_minherit(td, (uintptr_t)uap->addr, uap->len,
654 kern_minherit(struct thread *td, uintptr_t addr0, size_t len, int inherit0)
657 vm_size_t size, pageoff;
658 vm_inherit_t inherit;
660 addr = (vm_offset_t)addr0;
664 pageoff = (addr & PAGE_MASK);
667 size = (vm_size_t) round_page(size);
668 if (addr + size < addr)
671 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
672 addr + size, inherit)) {
675 case KERN_PROTECTION_FAILURE:
681 #ifndef _SYS_SYSPROTO_H_
682 struct madvise_args {
690 sys_madvise(struct thread *td, struct madvise_args *uap)
693 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
697 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
700 vm_offset_t addr, end, start;
704 * Check for our special case, advising the swap pager we are
707 if (behav == MADV_PROTECT) {
709 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
710 PROC_SPROTECT, &flags));
714 * Check for illegal addresses. Watch out for address wrap... Note
715 * that VM_*_ADDRESS are not constants due to casts (argh).
717 map = &td->td_proc->p_vmspace->vm_map;
719 if (!vm_map_range_valid(map, addr, addr + len))
723 * Since this routine is only advisory, we default to conservative
726 start = trunc_page(addr);
727 end = round_page(addr + len);
730 * vm_map_madvise() checks for illegal values of behav.
732 return (vm_map_madvise(map, start, end, behav));
735 #ifndef _SYS_SYSPROTO_H_
736 struct mincore_args {
744 sys_mincore(struct thread *td, struct mincore_args *uap)
747 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
751 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
753 vm_offset_t addr, first_addr;
754 vm_offset_t end, cend;
758 int vecindex, lastvecindex;
759 vm_map_entry_t current;
760 vm_map_entry_t entry;
762 vm_paddr_t locked_pa;
766 unsigned int timestamp;
770 * Make sure that the addresses presented are valid for user
773 first_addr = addr = trunc_page(addr0);
774 end = addr + (vm_size_t)round_page(len);
775 map = &td->td_proc->p_vmspace->vm_map;
776 if (end > vm_map_max(map) || end < addr)
779 pmap = vmspace_pmap(td->td_proc->p_vmspace);
781 vm_map_lock_read(map);
783 timestamp = map->timestamp;
785 if (!vm_map_lookup_entry(map, addr, &entry)) {
786 vm_map_unlock_read(map);
791 * Do this on a map entry basis so that if the pages are not
792 * in the current processes address space, we can easily look
793 * up the pages elsewhere.
796 for (current = entry; current->start < end; current = current->next) {
799 * check for contiguity
801 if (current->end < end && current->next->start > current->end) {
802 vm_map_unlock_read(map);
807 * ignore submaps (for now) or null objects
809 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
810 current->object.vm_object == NULL)
814 * limit this scan to the current map entry and the
815 * limits for the mincore call
817 if (addr < current->start)
818 addr = current->start;
824 * scan this entry one page at a time
826 while (addr < cend) {
828 * Check pmap first, it is likely faster, also
829 * it can provide info as to whether we are the
830 * one referencing or modifying the page.
836 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
837 if (mincore_mapped) {
839 * We only care about this pmap's
840 * mapping of the page, if any.
842 if (locked_pa != 0) {
843 vm_page_unlock(PHYS_TO_VM_PAGE(
846 } else if (locked_pa != 0) {
848 * The page is mapped by this process but not
849 * both accessed and modified. It is also
850 * managed. Acquire the object lock so that
851 * other mappings might be examined.
853 m = PHYS_TO_VM_PAGE(locked_pa);
854 if (m->object != object) {
856 VM_OBJECT_WUNLOCK(object);
858 locked = VM_OBJECT_TRYWLOCK(object);
861 VM_OBJECT_WLOCK(object);
867 KASSERT(m->valid == VM_PAGE_BITS_ALL,
868 ("mincore: page %p is mapped but invalid",
870 } else if (mincoreinfo == 0) {
872 * The page is not mapped by this process. If
873 * the object implements managed pages, then
874 * determine if the page is resident so that
875 * the mappings might be examined.
877 if (current->object.vm_object != object) {
879 VM_OBJECT_WUNLOCK(object);
880 object = current->object.vm_object;
881 VM_OBJECT_WLOCK(object);
883 if (object->type == OBJT_DEFAULT ||
884 object->type == OBJT_SWAP ||
885 object->type == OBJT_VNODE) {
886 pindex = OFF_TO_IDX(current->offset +
887 (addr - current->start));
888 m = vm_page_lookup(object, pindex);
889 if (m != NULL && m->valid == 0)
892 mincoreinfo = MINCORE_INCORE;
896 /* Examine other mappings to the page. */
897 if (m->dirty == 0 && pmap_is_modified(m))
900 mincoreinfo |= MINCORE_MODIFIED_OTHER;
902 * The first test for PGA_REFERENCED is an
903 * optimization. The second test is
904 * required because a concurrent pmap
905 * operation could clear the last reference
906 * and set PGA_REFERENCED before the call to
907 * pmap_is_referenced().
909 if ((m->aflags & PGA_REFERENCED) != 0 ||
910 pmap_is_referenced(m) ||
911 (m->aflags & PGA_REFERENCED) != 0)
912 mincoreinfo |= MINCORE_REFERENCED_OTHER;
915 VM_OBJECT_WUNLOCK(object);
918 * subyte may page fault. In case it needs to modify
919 * the map, we release the lock.
921 vm_map_unlock_read(map);
924 * calculate index into user supplied byte vector
926 vecindex = atop(addr - first_addr);
929 * If we have skipped map entries, we need to make sure that
930 * the byte vector is zeroed for those skipped entries.
932 while ((lastvecindex + 1) < vecindex) {
934 error = subyte(vec + lastvecindex, 0);
942 * Pass the page information to the user
944 error = subyte(vec + vecindex, mincoreinfo);
951 * If the map has changed, due to the subyte, the previous
952 * output may be invalid.
954 vm_map_lock_read(map);
955 if (timestamp != map->timestamp)
958 lastvecindex = vecindex;
964 * subyte may page fault. In case it needs to modify
965 * the map, we release the lock.
967 vm_map_unlock_read(map);
970 * Zero the last entries in the byte vector.
972 vecindex = atop(end - first_addr);
973 while ((lastvecindex + 1) < vecindex) {
975 error = subyte(vec + lastvecindex, 0);
983 * If the map has changed, due to the subyte, the previous
984 * output may be invalid.
986 vm_map_lock_read(map);
987 if (timestamp != map->timestamp)
989 vm_map_unlock_read(map);
994 #ifndef _SYS_SYSPROTO_H_
1001 sys_mlock(struct thread *td, struct mlock_args *uap)
1004 return (kern_mlock(td->td_proc, td->td_ucred,
1005 __DECONST(uintptr_t, uap->addr), uap->len));
1009 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1011 vm_offset_t addr, end, last, start;
1012 vm_size_t npages, size;
1014 unsigned long nsize;
1017 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
1023 start = trunc_page(addr);
1024 end = round_page(last);
1025 if (last < addr || end < addr)
1027 npages = atop(end - start);
1028 if ((u_int)npages > vm_page_max_user_wired)
1030 map = &proc->p_vmspace->vm_map;
1032 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1033 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1041 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1047 error = vm_map_wire(map, start, end,
1048 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1050 if (racct_enable && error != KERN_SUCCESS) {
1052 racct_set(proc, RACCT_MEMLOCK,
1053 ptoa(pmap_wired_count(map->pmap)));
1057 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1060 #ifndef _SYS_SYSPROTO_H_
1061 struct mlockall_args {
1067 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1072 map = &td->td_proc->p_vmspace->vm_map;
1073 error = priv_check(td, PRIV_VM_MLOCK);
1077 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1081 * If wiring all pages in the process would cause it to exceed
1082 * a hard resource limit, return ENOMEM.
1084 if (!old_mlock && uap->how & MCL_CURRENT) {
1085 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1090 PROC_LOCK(td->td_proc);
1091 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1092 PROC_UNLOCK(td->td_proc);
1098 if (uap->how & MCL_FUTURE) {
1100 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1105 if (uap->how & MCL_CURRENT) {
1107 * P1003.1-2001 mandates that all currently mapped pages
1108 * will be memory resident and locked (wired) upon return
1109 * from mlockall(). vm_map_wire() will wire pages, by
1110 * calling vm_fault_wire() for each page in the region.
1112 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1113 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1114 if (error == KERN_SUCCESS)
1116 else if (error == KERN_RESOURCE_SHORTAGE)
1122 if (racct_enable && error != KERN_SUCCESS) {
1123 PROC_LOCK(td->td_proc);
1124 racct_set(td->td_proc, RACCT_MEMLOCK,
1125 ptoa(pmap_wired_count(map->pmap)));
1126 PROC_UNLOCK(td->td_proc);
1133 #ifndef _SYS_SYSPROTO_H_
1134 struct munlockall_args {
1140 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1145 map = &td->td_proc->p_vmspace->vm_map;
1146 error = priv_check(td, PRIV_VM_MUNLOCK);
1150 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1152 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1155 /* Forcibly unwire all pages. */
1156 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1157 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1159 if (racct_enable && error == KERN_SUCCESS) {
1160 PROC_LOCK(td->td_proc);
1161 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1162 PROC_UNLOCK(td->td_proc);
1169 #ifndef _SYS_SYSPROTO_H_
1170 struct munlock_args {
1176 sys_munlock(struct thread *td, struct munlock_args *uap)
1179 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1183 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1185 vm_offset_t addr, end, last, start;
1191 error = priv_check(td, PRIV_VM_MUNLOCK);
1196 start = trunc_page(addr);
1197 end = round_page(last);
1198 if (last < addr || end < addr)
1200 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1201 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1203 if (racct_enable && error == KERN_SUCCESS) {
1204 PROC_LOCK(td->td_proc);
1205 map = &td->td_proc->p_vmspace->vm_map;
1206 racct_set(td->td_proc, RACCT_MEMLOCK,
1207 ptoa(pmap_wired_count(map->pmap)));
1208 PROC_UNLOCK(td->td_proc);
1211 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1217 * Helper function for vm_mmap. Perform sanity check specific for mmap
1218 * operations on vnodes.
1221 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1222 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1223 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1224 boolean_t *writecounted)
1233 cred = td->td_ucred;
1234 writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
1235 (*flagsp & MAP_SHARED) != 0;
1236 if ((error = vget(vp, LK_SHARED, td)) != 0)
1238 AUDIT_ARG_VNODE1(vp);
1242 if (vp->v_type == VREG) {
1244 * Get the proper underlying object
1250 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1252 vp = (struct vnode *)obj->handle;
1254 * Bypass filesystems obey the mpsafety of the
1255 * underlying fs. Tmpfs never bypasses.
1257 error = vget(vp, LK_SHARED, td);
1262 *writecounted = TRUE;
1263 vm_pager_update_writecount(obj, 0, objsize);
1269 if ((error = VOP_GETATTR(vp, &va, cred)))
1272 /* This relies on VM_PROT_* matching PROT_*. */
1273 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1277 if ((flags & MAP_SHARED) != 0) {
1278 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1279 if (prot & VM_PROT_WRITE) {
1283 *maxprotp &= ~VM_PROT_WRITE;
1287 * If it is a regular file without any references
1288 * we do not need to sync it.
1289 * Adjust object size to be the size of actual file.
1291 objsize = round_page(va.va_size);
1292 if (va.va_nlink == 0)
1293 flags |= MAP_NOSYNC;
1294 if (obj->type == OBJT_VNODE) {
1295 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1302 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1303 ("wrong object type"));
1304 VM_OBJECT_WLOCK(obj);
1305 vm_object_reference_locked(obj);
1306 #if VM_NRESERVLEVEL > 0
1307 vm_object_color(obj, 0);
1309 VM_OBJECT_WUNLOCK(obj);
1314 vfs_mark_atime(vp, cred);
1317 if (error != 0 && *writecounted) {
1318 *writecounted = FALSE;
1319 vm_pager_update_writecount(obj, objsize, 0);
1328 * Helper function for vm_mmap. Perform sanity check specific for mmap
1329 * operations on cdevs.
1332 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1333 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1334 vm_ooffset_t *foff, vm_object_t *objp)
1341 if (dsw->d_flags & D_MMAP_ANON) {
1344 *maxprotp = VM_PROT_ALL;
1345 *flagsp |= MAP_ANON;
1349 * cdevs do not provide private mappings of any kind.
1351 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1352 (prot & VM_PROT_WRITE) != 0)
1354 if (flags & (MAP_PRIVATE|MAP_COPY))
1357 * Force device mappings to be shared.
1359 flags |= MAP_SHARED;
1361 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1366 * First, try d_mmap_single(). If that is not implemented
1367 * (returns ENODEV), fall back to using the device pager.
1368 * Note that d_mmap_single() must return a reference to the
1369 * object (it needs to bump the reference count of the object
1370 * it returns somehow).
1372 * XXX assumes VM_PROT_* == PROT_*
1374 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1375 if (error != ENODEV)
1377 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1389 * Internal version of mmap used by exec, sys5 shared memory, and
1390 * various device drivers. Handle is either a vnode pointer, a
1391 * character device, or NULL for MAP_ANON.
1394 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1395 vm_prot_t maxprot, int flags,
1396 objtype_t handle_type, void *handle,
1400 struct thread *td = curthread;
1402 boolean_t writecounted;
1407 size = round_page(size);
1409 writecounted = FALSE;
1412 * Lookup/allocate object.
1414 switch (handle_type) {
1421 dsw = dev_refthread(cdev, &ref);
1424 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1425 dsw, &foff, &object);
1426 dev_relthread(cdev, ref);
1430 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1431 handle, &foff, &object, &writecounted);
1434 if (handle == NULL) {
1446 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1447 foff, writecounted, td);
1448 if (error != 0 && object != NULL) {
1450 * If this mapping was accounted for in the vnode's
1451 * writecount, then undo that now.
1454 vm_pager_release_writecount(object, 0, size);
1455 vm_object_deallocate(object);
1461 * Internal version of mmap that maps a specific VM object into an
1462 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1465 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1466 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1467 boolean_t writecounted, struct thread *td)
1469 boolean_t curmap, fitit;
1470 vm_offset_t max_addr;
1471 int docow, error, findspace, rv;
1473 curmap = map == &td->td_proc->p_vmspace->vm_map;
1475 RACCT_PROC_LOCK(td->td_proc);
1476 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1477 RACCT_PROC_UNLOCK(td->td_proc);
1480 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1481 RACCT_PROC_UNLOCK(td->td_proc);
1484 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1485 if (ptoa(pmap_wired_count(map->pmap)) + size >
1486 lim_cur(td, RLIMIT_MEMLOCK)) {
1487 racct_set_force(td->td_proc, RACCT_VMEM,
1489 RACCT_PROC_UNLOCK(td->td_proc);
1492 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1493 ptoa(pmap_wired_count(map->pmap)) + size);
1495 racct_set_force(td->td_proc, RACCT_VMEM,
1497 RACCT_PROC_UNLOCK(td->td_proc);
1501 RACCT_PROC_UNLOCK(td->td_proc);
1505 * We currently can only deal with page aligned file offsets.
1506 * The mmap() system call already enforces this by subtracting
1507 * the page offset from the file offset, but checking here
1508 * catches errors in device drivers (e.g. d_single_mmap()
1509 * callbacks) and other internal mapping requests (such as in
1512 if (foff & PAGE_MASK)
1515 if ((flags & MAP_FIXED) == 0) {
1517 *addr = round_page(*addr);
1519 if (*addr != trunc_page(*addr))
1524 if (flags & MAP_ANON) {
1525 if (object != NULL || foff != 0)
1528 } else if (flags & MAP_PREFAULT_READ)
1529 docow = MAP_PREFAULT;
1531 docow = MAP_PREFAULT_PARTIAL;
1533 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1534 docow |= MAP_COPY_ON_WRITE;
1535 if (flags & MAP_NOSYNC)
1536 docow |= MAP_DISABLE_SYNCER;
1537 if (flags & MAP_NOCORE)
1538 docow |= MAP_DISABLE_COREDUMP;
1539 /* Shared memory is also shared with children. */
1540 if (flags & MAP_SHARED)
1541 docow |= MAP_INHERIT_SHARE;
1543 docow |= MAP_WRITECOUNT;
1544 if (flags & MAP_STACK) {
1547 docow |= MAP_STACK_GROWS_DOWN;
1549 if ((flags & MAP_EXCL) != 0)
1550 docow |= MAP_CHECK_EXCL;
1551 if ((flags & MAP_GUARD) != 0)
1552 docow |= MAP_CREATE_GUARD;
1555 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1556 findspace = VMFS_SUPER_SPACE;
1557 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1558 findspace = VMFS_ALIGNED_SPACE(flags >>
1559 MAP_ALIGNMENT_SHIFT);
1561 findspace = VMFS_OPTIMAL_SPACE;
1564 if ((flags & MAP_32BIT) != 0)
1565 max_addr = MAP_32BIT_MAX_ADDR;
1568 rv = vm_map_find_min(map, object, foff, addr, size,
1569 round_page((vm_offset_t)td->td_proc->p_vmspace->
1570 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1571 findspace, prot, maxprot, docow);
1573 rv = vm_map_find(map, object, foff, addr, size,
1574 max_addr, findspace, prot, maxprot, docow);
1577 rv = vm_map_fixed(map, object, foff, *addr, size,
1578 prot, maxprot, docow);
1581 if (rv == KERN_SUCCESS) {
1583 * If the process has requested that all future mappings
1584 * be wired, then heed this.
1586 if ((map->flags & MAP_WIREFUTURE) != 0) {
1588 if ((map->flags & MAP_WIREFUTURE) != 0)
1589 (void)vm_map_wire_locked(map, *addr,
1590 *addr + size, VM_MAP_WIRE_USER |
1591 ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
1592 VM_MAP_WIRE_NOHOLES));
1596 return (vm_mmap_to_errno(rv));
1600 * Translate a Mach VM return code to zero on success or the appropriate errno
1604 vm_mmap_to_errno(int rv)
1610 case KERN_INVALID_ADDRESS:
1613 case KERN_PROTECTION_FAILURE: