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_compat.h"
49 #include "opt_hwpmc_hooks.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/capsicum.h>
55 #include <sys/kernel.h>
57 #include <sys/mutex.h>
58 #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>
79 #include <security/audit/audit.h>
80 #include <security/mac/mac_framework.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_pageout.h>
90 #include <vm/vm_extern.h>
91 #include <vm/vm_page.h>
92 #include <vm/vnode_pager.h>
95 #include <sys/pmckern.h>
99 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
100 "Do not apply RLIMIT_MEMLOCK on mlockall");
103 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
106 #ifndef _SYS_SYSPROTO_H_
113 sys_sbrk(struct thread *td, struct sbrk_args *uap)
115 /* Not yet implemented */
119 #ifndef _SYS_SYSPROTO_H_
126 sys_sstk(struct thread *td, struct sstk_args *uap)
128 /* Not yet implemented */
132 #if defined(COMPAT_43)
133 #ifndef _SYS_SYSPROTO_H_
134 struct getpagesize_args {
140 ogetpagesize(struct thread *td, struct getpagesize_args *uap)
143 td->td_retval[0] = PAGE_SIZE;
146 #endif /* COMPAT_43 */
150 * Memory Map (mmap) system call. Note that the file offset
151 * and address are allowed to be NOT page aligned, though if
152 * the MAP_FIXED flag it set, both must have the same remainder
153 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
154 * page-aligned, the actual mapping starts at trunc_page(addr)
155 * and the return value is adjusted up by the page offset.
157 * Generally speaking, only character devices which are themselves
158 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
159 * there would be no cache coherency between a descriptor and a VM mapping
160 * both to the same character device.
162 #ifndef _SYS_SYSPROTO_H_
175 sys_mmap(struct thread *td, struct mmap_args *uap)
178 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
179 uap->flags, uap->fd, uap->pos));
183 kern_mmap(struct thread *td, uintptr_t addr0, size_t size, int prot, int flags,
190 vm_prot_t cap_maxprot;
194 vms = td->td_proc->p_vmspace;
200 * Ignore old flags that used to be defined but did not do anything.
202 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
205 * Enforce the constraints.
206 * Mapping of length 0 is only allowed for old binaries.
207 * Anonymous mapping shall specify -1 as filedescriptor and
208 * zero position for new code. Be nice to ancient a.out
209 * binaries and correct pos for anonymous mapping, since old
210 * ld.so sometimes issues anonymous map requests with non-zero
213 if (!SV_CURPROC_FLAG(SV_AOUT)) {
214 if ((size == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
215 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
218 if ((flags & MAP_ANON) != 0)
222 if (flags & MAP_STACK) {
224 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
229 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
230 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
231 MAP_PREFAULT_READ | MAP_GUARD |
235 MAP_ALIGNMENT_MASK)) != 0)
237 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
239 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
241 if (prot != PROT_NONE &&
242 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
244 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
245 pos != 0 || (flags & (MAP_SHARED | MAP_PRIVATE | MAP_PREFAULT |
246 MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0))
250 * Align the file position to a page boundary,
251 * and save its page offset component.
253 pageoff = (pos & PAGE_MASK);
256 /* Adjust size for rounding (on both ends). */
257 size += pageoff; /* low end... */
258 size = (vm_size_t) round_page(size); /* hi end */
260 /* Ensure alignment is at least a page and fits in a pointer. */
261 align = flags & MAP_ALIGNMENT_MASK;
262 if (align != 0 && align != MAP_ALIGNED_SUPER &&
263 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
264 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
268 * Check for illegal addresses. Watch out for address wrap... Note
269 * that VM_*_ADDRESS are not constants due to casts (argh).
271 if (flags & MAP_FIXED) {
273 * The specified address must have the same remainder
274 * as the file offset taken modulo PAGE_SIZE, so it
275 * should be aligned after adjustment by pageoff.
278 if (addr & PAGE_MASK)
281 /* Address range must be all in user VM space. */
282 if (addr < vm_map_min(&vms->vm_map) ||
283 addr + size > vm_map_max(&vms->vm_map))
285 if (addr + size < addr)
288 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
290 } else if (flags & MAP_32BIT) {
292 * For MAP_32BIT, override the hint if it is too high and
293 * do not bother moving the mapping past the heap (since
294 * the heap is usually above 2GB).
296 if (addr + size > MAP_32BIT_MAX_ADDR)
301 * XXX for non-fixed mappings where no hint is provided or
302 * the hint would fall in the potential heap space,
303 * place it after the end of the largest possible heap.
305 * There should really be a pmap call to determine a reasonable
309 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
310 addr < round_page((vm_offset_t)vms->vm_daddr +
311 lim_max(td, RLIMIT_DATA))))
312 addr = round_page((vm_offset_t)vms->vm_daddr +
313 lim_max(td, RLIMIT_DATA));
317 * Return success without mapping anything for old
318 * binaries that request a page-aligned mapping of
319 * length 0. For modern binaries, this function
320 * returns an error earlier.
323 } else if ((flags & MAP_GUARD) != 0) {
324 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
325 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
326 } else if ((flags & MAP_ANON) != 0) {
328 * Mapping blank space is trivial.
330 * This relies on VM_PROT_* matching PROT_*.
332 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
333 VM_PROT_ALL, flags, NULL, pos, FALSE, td);
336 * Mapping file, get fp for validation and don't let the
337 * descriptor disappear on us if we block. Check capability
338 * rights, but also return the maximum rights to be combined
339 * with maxprot later.
341 cap_rights_init(&rights, CAP_MMAP);
342 if (prot & PROT_READ)
343 cap_rights_set(&rights, CAP_MMAP_R);
344 if ((flags & MAP_SHARED) != 0) {
345 if (prot & PROT_WRITE)
346 cap_rights_set(&rights, CAP_MMAP_W);
348 if (prot & PROT_EXEC)
349 cap_rights_set(&rights, CAP_MMAP_X);
350 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
353 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
354 td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
359 /* This relies on VM_PROT_* matching PROT_*. */
360 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
361 cap_maxprot, flags, pos, td);
365 td->td_retval[0] = (register_t) (addr + pageoff);
373 #if defined(COMPAT_FREEBSD6)
375 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
378 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
379 uap->flags, uap->fd, uap->pos));
384 #ifndef _SYS_SYSPROTO_H_
395 ommap(struct thread *td, struct ommap_args *uap)
397 static const char cvtbsdprot[8] = {
401 PROT_EXEC | PROT_WRITE,
403 PROT_EXEC | PROT_READ,
404 PROT_WRITE | PROT_READ,
405 PROT_EXEC | PROT_WRITE | PROT_READ,
409 #define OMAP_ANON 0x0002
410 #define OMAP_COPY 0x0020
411 #define OMAP_SHARED 0x0010
412 #define OMAP_FIXED 0x0100
414 prot = cvtbsdprot[uap->prot & 0x7];
415 #ifdef COMPAT_FREEBSD32
416 #if defined(__amd64__)
417 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
423 if (uap->flags & OMAP_ANON)
425 if (uap->flags & OMAP_COPY)
427 if (uap->flags & OMAP_SHARED)
430 flags |= MAP_PRIVATE;
431 if (uap->flags & OMAP_FIXED)
433 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
436 #endif /* COMPAT_43 */
439 #ifndef _SYS_SYSPROTO_H_
447 sys_msync(struct thread *td, struct msync_args *uap)
450 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
454 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
462 pageoff = (addr & PAGE_MASK);
465 size = (vm_size_t) round_page(size);
466 if (addr + size < addr)
469 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
472 map = &td->td_proc->p_vmspace->vm_map;
475 * Clean the pages and interpret the return value.
477 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
478 (flags & MS_INVALIDATE) != 0);
482 case KERN_INVALID_ADDRESS:
484 case KERN_INVALID_ARGUMENT:
493 #ifndef _SYS_SYSPROTO_H_
500 sys_munmap(struct thread *td, struct munmap_args *uap)
503 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
507 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
510 struct pmckern_map_out pkm;
511 vm_map_entry_t entry;
522 pageoff = (addr & PAGE_MASK);
525 size = (vm_size_t) round_page(size);
526 if (addr + size < addr)
530 * Check for illegal addresses. Watch out for address wrap...
532 map = &td->td_proc->p_vmspace->vm_map;
533 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
538 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
541 * Inform hwpmc if the address range being unmapped contains
542 * an executable region.
544 pkm.pm_address = (uintptr_t) NULL;
545 if (vm_map_lookup_entry(map, addr, &entry)) {
546 for (; entry->start < addr + size;
547 entry = entry->next) {
548 if (vm_map_check_protection(map, entry->start,
549 entry->end, VM_PROT_EXECUTE) == TRUE) {
550 pkm.pm_address = (uintptr_t) addr;
551 pkm.pm_size = (size_t) size;
558 vm_map_delete(map, addr, addr + size);
561 if (__predict_false(pmc_handled)) {
562 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
563 vm_map_lock_downgrade(map);
564 if (pkm.pm_address != (uintptr_t) NULL)
565 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
566 vm_map_unlock_read(map);
571 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
575 #ifndef _SYS_SYSPROTO_H_
576 struct mprotect_args {
583 sys_mprotect(struct thread *td, struct mprotect_args *uap)
586 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len, uap->prot));
590 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot)
596 prot = (prot & VM_PROT_ALL);
597 pageoff = (addr & PAGE_MASK);
600 size = (vm_size_t) round_page(size);
601 if (addr + size < addr)
604 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
605 addr + size, prot, FALSE)) {
608 case KERN_PROTECTION_FAILURE:
610 case KERN_RESOURCE_SHORTAGE:
616 #ifndef _SYS_SYSPROTO_H_
617 struct minherit_args {
624 sys_minherit(struct thread *td, struct minherit_args *uap)
627 vm_size_t size, pageoff;
628 vm_inherit_t inherit;
630 addr = (vm_offset_t)uap->addr;
632 inherit = uap->inherit;
634 pageoff = (addr & PAGE_MASK);
637 size = (vm_size_t) round_page(size);
638 if (addr + size < addr)
641 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
642 addr + size, inherit)) {
645 case KERN_PROTECTION_FAILURE:
651 #ifndef _SYS_SYSPROTO_H_
652 struct madvise_args {
660 sys_madvise(struct thread *td, struct madvise_args *uap)
663 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
667 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
670 vm_offset_t addr, end, start;
674 * Check for our special case, advising the swap pager we are
677 if (behav == MADV_PROTECT) {
679 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
680 PROC_SPROTECT, &flags));
684 * Check for illegal behavior
686 if (behav < 0 || behav > MADV_CORE)
689 * Check for illegal addresses. Watch out for address wrap... Note
690 * that VM_*_ADDRESS are not constants due to casts (argh).
692 map = &td->td_proc->p_vmspace->vm_map;
694 if (addr < vm_map_min(map) || addr + len > vm_map_max(map))
696 if ((addr + len) < addr)
700 * Since this routine is only advisory, we default to conservative
703 start = trunc_page(addr);
704 end = round_page(addr + len);
706 if (vm_map_madvise(map, start, end, behav))
711 #ifndef _SYS_SYSPROTO_H_
712 struct mincore_args {
720 sys_mincore(struct thread *td, struct mincore_args *uap)
723 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
727 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
729 vm_offset_t addr, first_addr;
730 vm_offset_t end, cend;
734 int vecindex, lastvecindex;
735 vm_map_entry_t current;
736 vm_map_entry_t entry;
738 vm_paddr_t locked_pa;
742 unsigned int timestamp;
746 * Make sure that the addresses presented are valid for user
749 first_addr = addr = trunc_page(addr0);
750 end = addr + (vm_size_t)round_page(len);
751 map = &td->td_proc->p_vmspace->vm_map;
752 if (end > vm_map_max(map) || end < addr)
755 pmap = vmspace_pmap(td->td_proc->p_vmspace);
757 vm_map_lock_read(map);
759 timestamp = map->timestamp;
761 if (!vm_map_lookup_entry(map, addr, &entry)) {
762 vm_map_unlock_read(map);
767 * Do this on a map entry basis so that if the pages are not
768 * in the current processes address space, we can easily look
769 * up the pages elsewhere.
772 for (current = entry; current->start < end; current = current->next) {
775 * check for contiguity
777 if (current->end < end && current->next->start > current->end) {
778 vm_map_unlock_read(map);
783 * ignore submaps (for now) or null objects
785 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
786 current->object.vm_object == NULL)
790 * limit this scan to the current map entry and the
791 * limits for the mincore call
793 if (addr < current->start)
794 addr = current->start;
800 * scan this entry one page at a time
802 while (addr < cend) {
804 * Check pmap first, it is likely faster, also
805 * it can provide info as to whether we are the
806 * one referencing or modifying the page.
812 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
813 if (locked_pa != 0) {
815 * The page is mapped by this process but not
816 * both accessed and modified. It is also
817 * managed. Acquire the object lock so that
818 * other mappings might be examined.
820 m = PHYS_TO_VM_PAGE(locked_pa);
821 if (m->object != object) {
823 VM_OBJECT_WUNLOCK(object);
825 locked = VM_OBJECT_TRYWLOCK(object);
828 VM_OBJECT_WLOCK(object);
834 KASSERT(m->valid == VM_PAGE_BITS_ALL,
835 ("mincore: page %p is mapped but invalid",
837 } else if (mincoreinfo == 0) {
839 * The page is not mapped by this process. If
840 * the object implements managed pages, then
841 * determine if the page is resident so that
842 * the mappings might be examined.
844 if (current->object.vm_object != object) {
846 VM_OBJECT_WUNLOCK(object);
847 object = current->object.vm_object;
848 VM_OBJECT_WLOCK(object);
850 if (object->type == OBJT_DEFAULT ||
851 object->type == OBJT_SWAP ||
852 object->type == OBJT_VNODE) {
853 pindex = OFF_TO_IDX(current->offset +
854 (addr - current->start));
855 m = vm_page_lookup(object, pindex);
856 if (m != NULL && m->valid == 0)
859 mincoreinfo = MINCORE_INCORE;
863 /* Examine other mappings to the page. */
864 if (m->dirty == 0 && pmap_is_modified(m))
867 mincoreinfo |= MINCORE_MODIFIED_OTHER;
869 * The first test for PGA_REFERENCED is an
870 * optimization. The second test is
871 * required because a concurrent pmap
872 * operation could clear the last reference
873 * and set PGA_REFERENCED before the call to
874 * pmap_is_referenced().
876 if ((m->aflags & PGA_REFERENCED) != 0 ||
877 pmap_is_referenced(m) ||
878 (m->aflags & PGA_REFERENCED) != 0)
879 mincoreinfo |= MINCORE_REFERENCED_OTHER;
882 VM_OBJECT_WUNLOCK(object);
885 * subyte may page fault. In case it needs to modify
886 * the map, we release the lock.
888 vm_map_unlock_read(map);
891 * calculate index into user supplied byte vector
893 vecindex = atop(addr - first_addr);
896 * If we have skipped map entries, we need to make sure that
897 * the byte vector is zeroed for those skipped entries.
899 while ((lastvecindex + 1) < vecindex) {
901 error = subyte(vec + lastvecindex, 0);
909 * Pass the page information to the user
911 error = subyte(vec + vecindex, mincoreinfo);
918 * If the map has changed, due to the subyte, the previous
919 * output may be invalid.
921 vm_map_lock_read(map);
922 if (timestamp != map->timestamp)
925 lastvecindex = vecindex;
931 * subyte may page fault. In case it needs to modify
932 * the map, we release the lock.
934 vm_map_unlock_read(map);
937 * Zero the last entries in the byte vector.
939 vecindex = atop(end - first_addr);
940 while ((lastvecindex + 1) < vecindex) {
942 error = subyte(vec + lastvecindex, 0);
950 * If the map has changed, due to the subyte, the previous
951 * output may be invalid.
953 vm_map_lock_read(map);
954 if (timestamp != map->timestamp)
956 vm_map_unlock_read(map);
961 #ifndef _SYS_SYSPROTO_H_
968 sys_mlock(struct thread *td, struct mlock_args *uap)
971 return (kern_mlock(td->td_proc, td->td_ucred,
972 __DECONST(uintptr_t, uap->addr), uap->len));
976 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
978 vm_offset_t addr, end, last, start;
979 vm_size_t npages, size;
984 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
990 start = trunc_page(addr);
991 end = round_page(last);
992 if (last < addr || end < addr)
994 npages = atop(end - start);
995 if (npages > vm_page_max_wired)
997 map = &proc->p_vmspace->vm_map;
999 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1000 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1005 if (npages + vm_wire_count() > vm_page_max_wired)
1010 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1016 error = vm_map_wire(map, start, end,
1017 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1019 if (racct_enable && error != KERN_SUCCESS) {
1021 racct_set(proc, RACCT_MEMLOCK,
1022 ptoa(pmap_wired_count(map->pmap)));
1026 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1029 #ifndef _SYS_SYSPROTO_H_
1030 struct mlockall_args {
1036 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1041 map = &td->td_proc->p_vmspace->vm_map;
1042 error = priv_check(td, PRIV_VM_MLOCK);
1046 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1050 * If wiring all pages in the process would cause it to exceed
1051 * a hard resource limit, return ENOMEM.
1053 if (!old_mlock && uap->how & MCL_CURRENT) {
1054 PROC_LOCK(td->td_proc);
1055 if (map->size > lim_cur(td, RLIMIT_MEMLOCK)) {
1056 PROC_UNLOCK(td->td_proc);
1059 PROC_UNLOCK(td->td_proc);
1063 PROC_LOCK(td->td_proc);
1064 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1065 PROC_UNLOCK(td->td_proc);
1071 if (uap->how & MCL_FUTURE) {
1073 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1078 if (uap->how & MCL_CURRENT) {
1080 * P1003.1-2001 mandates that all currently mapped pages
1081 * will be memory resident and locked (wired) upon return
1082 * from mlockall(). vm_map_wire() will wire pages, by
1083 * calling vm_fault_wire() for each page in the region.
1085 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1086 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1087 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1090 if (racct_enable && error != KERN_SUCCESS) {
1091 PROC_LOCK(td->td_proc);
1092 racct_set(td->td_proc, RACCT_MEMLOCK,
1093 ptoa(pmap_wired_count(map->pmap)));
1094 PROC_UNLOCK(td->td_proc);
1101 #ifndef _SYS_SYSPROTO_H_
1102 struct munlockall_args {
1108 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1113 map = &td->td_proc->p_vmspace->vm_map;
1114 error = priv_check(td, PRIV_VM_MUNLOCK);
1118 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1120 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1123 /* Forcibly unwire all pages. */
1124 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1125 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1127 if (racct_enable && error == KERN_SUCCESS) {
1128 PROC_LOCK(td->td_proc);
1129 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1130 PROC_UNLOCK(td->td_proc);
1137 #ifndef _SYS_SYSPROTO_H_
1138 struct munlock_args {
1144 sys_munlock(struct thread *td, struct munlock_args *uap)
1147 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1151 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1153 vm_offset_t addr, end, last, start;
1159 error = priv_check(td, PRIV_VM_MUNLOCK);
1164 start = trunc_page(addr);
1165 end = round_page(last);
1166 if (last < addr || end < addr)
1168 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1169 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1171 if (racct_enable && error == KERN_SUCCESS) {
1172 PROC_LOCK(td->td_proc);
1173 map = &td->td_proc->p_vmspace->vm_map;
1174 racct_set(td->td_proc, RACCT_MEMLOCK,
1175 ptoa(pmap_wired_count(map->pmap)));
1176 PROC_UNLOCK(td->td_proc);
1179 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1185 * Helper function for vm_mmap. Perform sanity check specific for mmap
1186 * operations on vnodes.
1189 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1190 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1191 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1192 boolean_t *writecounted)
1198 int error, flags, locktype;
1200 cred = td->td_ucred;
1201 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1202 locktype = LK_EXCLUSIVE;
1204 locktype = LK_SHARED;
1205 if ((error = vget(vp, locktype, td)) != 0)
1207 AUDIT_ARG_VNODE1(vp);
1211 if (vp->v_type == VREG) {
1213 * Get the proper underlying object
1219 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1221 vp = (struct vnode *)obj->handle;
1223 * Bypass filesystems obey the mpsafety of the
1224 * underlying fs. Tmpfs never bypasses.
1226 error = vget(vp, locktype, td);
1230 if (locktype == LK_EXCLUSIVE) {
1231 *writecounted = TRUE;
1232 vnode_pager_update_writecount(obj, 0, objsize);
1238 if ((error = VOP_GETATTR(vp, &va, cred)))
1241 /* This relies on VM_PROT_* matching PROT_*. */
1242 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1246 if ((flags & MAP_SHARED) != 0) {
1247 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1248 if (prot & VM_PROT_WRITE) {
1252 *maxprotp &= ~VM_PROT_WRITE;
1256 * If it is a regular file without any references
1257 * we do not need to sync it.
1258 * Adjust object size to be the size of actual file.
1260 objsize = round_page(va.va_size);
1261 if (va.va_nlink == 0)
1262 flags |= MAP_NOSYNC;
1263 if (obj->type == OBJT_VNODE) {
1264 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1271 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1272 ("wrong object type"));
1273 VM_OBJECT_WLOCK(obj);
1274 vm_object_reference_locked(obj);
1275 #if VM_NRESERVLEVEL > 0
1276 vm_object_color(obj, 0);
1278 VM_OBJECT_WUNLOCK(obj);
1283 vfs_mark_atime(vp, cred);
1286 if (error != 0 && *writecounted) {
1287 *writecounted = FALSE;
1288 vnode_pager_update_writecount(obj, objsize, 0);
1297 * Helper function for vm_mmap. Perform sanity check specific for mmap
1298 * operations on cdevs.
1301 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1302 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1303 vm_ooffset_t *foff, vm_object_t *objp)
1310 if (dsw->d_flags & D_MMAP_ANON) {
1313 *maxprotp = VM_PROT_ALL;
1314 *flagsp |= MAP_ANON;
1318 * cdevs do not provide private mappings of any kind.
1320 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1321 (prot & VM_PROT_WRITE) != 0)
1323 if (flags & (MAP_PRIVATE|MAP_COPY))
1326 * Force device mappings to be shared.
1328 flags |= MAP_SHARED;
1330 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1335 * First, try d_mmap_single(). If that is not implemented
1336 * (returns ENODEV), fall back to using the device pager.
1337 * Note that d_mmap_single() must return a reference to the
1338 * object (it needs to bump the reference count of the object
1339 * it returns somehow).
1341 * XXX assumes VM_PROT_* == PROT_*
1343 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1344 if (error != ENODEV)
1346 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1358 * Internal version of mmap used by exec, sys5 shared memory, and
1359 * various device drivers. Handle is either a vnode pointer, a
1360 * character device, or NULL for MAP_ANON.
1363 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1364 vm_prot_t maxprot, int flags,
1365 objtype_t handle_type, void *handle,
1369 struct thread *td = curthread;
1371 boolean_t writecounted;
1376 size = round_page(size);
1378 writecounted = FALSE;
1381 * Lookup/allocate object.
1383 switch (handle_type) {
1390 dsw = dev_refthread(cdev, &ref);
1393 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1394 dsw, &foff, &object);
1395 dev_relthread(cdev, ref);
1399 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1400 handle, &foff, &object, &writecounted);
1403 if (handle == NULL) {
1415 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1416 foff, writecounted, td);
1417 if (error != 0 && object != NULL) {
1419 * If this mapping was accounted for in the vnode's
1420 * writecount, then undo that now.
1423 vnode_pager_release_writecount(object, 0, size);
1424 vm_object_deallocate(object);
1430 * Internal version of mmap that maps a specific VM object into an
1431 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1434 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1435 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1436 boolean_t writecounted, struct thread *td)
1438 boolean_t curmap, fitit;
1439 vm_offset_t max_addr;
1440 int docow, error, findspace, rv;
1442 curmap = map == &td->td_proc->p_vmspace->vm_map;
1444 PROC_LOCK(td->td_proc);
1445 if (map->size + size > lim_cur_proc(td->td_proc, RLIMIT_VMEM)) {
1446 PROC_UNLOCK(td->td_proc);
1449 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1450 PROC_UNLOCK(td->td_proc);
1453 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1454 if (ptoa(pmap_wired_count(map->pmap)) + size >
1455 lim_cur_proc(td->td_proc, RLIMIT_MEMLOCK)) {
1456 racct_set_force(td->td_proc, RACCT_VMEM,
1458 PROC_UNLOCK(td->td_proc);
1461 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1462 ptoa(pmap_wired_count(map->pmap)) + size);
1464 racct_set_force(td->td_proc, RACCT_VMEM,
1466 PROC_UNLOCK(td->td_proc);
1470 PROC_UNLOCK(td->td_proc);
1474 * We currently can only deal with page aligned file offsets.
1475 * The mmap() system call already enforces this by subtracting
1476 * the page offset from the file offset, but checking here
1477 * catches errors in device drivers (e.g. d_single_mmap()
1478 * callbacks) and other internal mapping requests (such as in
1481 if (foff & PAGE_MASK)
1484 if ((flags & MAP_FIXED) == 0) {
1486 *addr = round_page(*addr);
1488 if (*addr != trunc_page(*addr))
1493 if (flags & MAP_ANON) {
1494 if (object != NULL || foff != 0)
1497 } else if (flags & MAP_PREFAULT_READ)
1498 docow = MAP_PREFAULT;
1500 docow = MAP_PREFAULT_PARTIAL;
1502 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1503 docow |= MAP_COPY_ON_WRITE;
1504 if (flags & MAP_NOSYNC)
1505 docow |= MAP_DISABLE_SYNCER;
1506 if (flags & MAP_NOCORE)
1507 docow |= MAP_DISABLE_COREDUMP;
1508 /* Shared memory is also shared with children. */
1509 if (flags & MAP_SHARED)
1510 docow |= MAP_INHERIT_SHARE;
1512 docow |= MAP_VN_WRITECOUNT;
1513 if (flags & MAP_STACK) {
1516 docow |= MAP_STACK_GROWS_DOWN;
1518 if ((flags & MAP_EXCL) != 0)
1519 docow |= MAP_CHECK_EXCL;
1520 if ((flags & MAP_GUARD) != 0)
1521 docow |= MAP_CREATE_GUARD;
1524 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1525 findspace = VMFS_SUPER_SPACE;
1526 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1527 findspace = VMFS_ALIGNED_SPACE(flags >>
1528 MAP_ALIGNMENT_SHIFT);
1530 findspace = VMFS_OPTIMAL_SPACE;
1533 if ((flags & MAP_32BIT) != 0)
1534 max_addr = MAP_32BIT_MAX_ADDR;
1537 rv = vm_map_find_min(map, object, foff, addr, size,
1538 round_page((vm_offset_t)td->td_proc->p_vmspace->
1539 vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
1540 findspace, prot, maxprot, docow);
1542 rv = vm_map_find(map, object, foff, addr, size,
1543 max_addr, findspace, prot, maxprot, docow);
1546 rv = vm_map_fixed(map, object, foff, *addr, size,
1547 prot, maxprot, docow);
1550 if (rv == KERN_SUCCESS) {
1552 * If the process has requested that all future mappings
1553 * be wired, then heed this.
1555 if (map->flags & MAP_WIREFUTURE) {
1556 vm_map_wire(map, *addr, *addr + size,
1557 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1558 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1561 return (vm_mmap_to_errno(rv));
1565 * Translate a Mach VM return code to zero on success or the appropriate errno
1569 vm_mmap_to_errno(int rv)
1575 case KERN_INVALID_ADDRESS:
1578 case KERN_PROTECTION_FAILURE: