2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
40 * Mapped file (mmap) interface to VM
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
47 #include "opt_hwpmc_hooks.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/capsicum.h>
53 #include <sys/kernel.h>
55 #include <sys/mutex.h>
56 #include <sys/sysproto.h>
57 #include <sys/filedesc.h>
60 #include <sys/procctl.h>
61 #include <sys/racct.h>
62 #include <sys/resource.h>
63 #include <sys/resourcevar.h>
64 #include <sys/rwlock.h>
65 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
67 #include <sys/fcntl.h>
70 #include <sys/mount.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysent.h>
75 #include <sys/vmmeter.h>
77 #include <security/mac/mac_framework.h>
80 #include <vm/vm_param.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pager.h>
86 #include <vm/vm_pageout.h>
87 #include <vm/vm_extern.h>
88 #include <vm/vm_page.h>
89 #include <vm/vnode_pager.h>
92 #include <sys/pmckern.h>
96 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
97 "Do not apply RLIMIT_MEMLOCK on mlockall");
100 #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31)
103 #ifndef _SYS_SYSPROTO_H_
116 struct sbrk_args *uap;
118 /* Not yet implemented */
122 #ifndef _SYS_SYSPROTO_H_
135 struct sstk_args *uap;
137 /* Not yet implemented */
141 #if defined(COMPAT_43)
142 #ifndef _SYS_SYSPROTO_H_
143 struct getpagesize_args {
149 ogetpagesize(td, uap)
151 struct getpagesize_args *uap;
154 td->td_retval[0] = PAGE_SIZE;
157 #endif /* COMPAT_43 */
161 * Memory Map (mmap) system call. Note that the file offset
162 * and address are allowed to be NOT page aligned, though if
163 * the MAP_FIXED flag it set, both must have the same remainder
164 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
165 * page-aligned, the actual mapping starts at trunc_page(addr)
166 * and the return value is adjusted up by the page offset.
168 * Generally speaking, only character devices which are themselves
169 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
170 * there would be no cache coherency between a descriptor and a VM mapping
171 * both to the same character device.
173 #ifndef _SYS_SYSPROTO_H_
191 struct mmap_args *uap;
195 vm_size_t size, pageoff;
196 vm_prot_t cap_maxprot;
197 int align, error, flags, prot;
199 struct vmspace *vms = td->td_proc->p_vmspace;
202 addr = (vm_offset_t) uap->addr;
211 * Ignore old flags that used to be defined but did not do anything.
213 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
216 * Enforce the constraints.
217 * Mapping of length 0 is only allowed for old binaries.
218 * Anonymous mapping shall specify -1 as filedescriptor and
219 * zero position for new code. Be nice to ancient a.out
220 * binaries and correct pos for anonymous mapping, since old
221 * ld.so sometimes issues anonymous map requests with non-zero
224 if (!SV_CURPROC_FLAG(SV_AOUT)) {
225 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
226 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
229 if ((flags & MAP_ANON) != 0)
233 if (flags & MAP_STACK) {
234 if ((uap->fd != -1) ||
235 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
240 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
241 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
246 MAP_ALIGNMENT_MASK)) != 0)
248 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
250 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
252 if (prot != PROT_NONE &&
253 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
257 * Align the file position to a page boundary,
258 * and save its page offset component.
260 pageoff = (pos & PAGE_MASK);
263 /* Adjust size for rounding (on both ends). */
264 size += pageoff; /* low end... */
265 size = (vm_size_t) round_page(size); /* hi end */
267 /* Ensure alignment is at least a page and fits in a pointer. */
268 align = flags & MAP_ALIGNMENT_MASK;
269 if (align != 0 && align != MAP_ALIGNED_SUPER &&
270 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
271 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
275 * Check for illegal addresses. Watch out for address wrap... Note
276 * that VM_*_ADDRESS are not constants due to casts (argh).
278 if (flags & MAP_FIXED) {
280 * The specified address must have the same remainder
281 * as the file offset taken modulo PAGE_SIZE, so it
282 * should be aligned after adjustment by pageoff.
285 if (addr & PAGE_MASK)
288 /* Address range must be all in user VM space. */
289 if (addr < vm_map_min(&vms->vm_map) ||
290 addr + size > vm_map_max(&vms->vm_map))
292 if (addr + size < addr)
295 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
297 } else if (flags & MAP_32BIT) {
299 * For MAP_32BIT, override the hint if it is too high and
300 * do not bother moving the mapping past the heap (since
301 * the heap is usually above 2GB).
303 if (addr + size > MAP_32BIT_MAX_ADDR)
308 * XXX for non-fixed mappings where no hint is provided or
309 * the hint would fall in the potential heap space,
310 * place it after the end of the largest possible heap.
312 * There should really be a pmap call to determine a reasonable
316 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
317 addr < round_page((vm_offset_t)vms->vm_daddr +
318 lim_max(td, RLIMIT_DATA))))
319 addr = round_page((vm_offset_t)vms->vm_daddr +
320 lim_max(td, RLIMIT_DATA));
324 * Return success without mapping anything for old
325 * binaries that request a page-aligned mapping of
326 * length 0. For modern binaries, this function
327 * returns an error earlier.
330 } else if (flags & MAP_ANON) {
332 * Mapping blank space is trivial.
334 * This relies on VM_PROT_* matching PROT_*.
336 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
337 VM_PROT_ALL, flags, NULL, pos, FALSE, td);
340 * Mapping file, get fp for validation and don't let the
341 * descriptor disappear on us if we block. Check capability
342 * rights, but also return the maximum rights to be combined
343 * with maxprot later.
345 cap_rights_init(&rights, CAP_MMAP);
346 if (prot & PROT_READ)
347 cap_rights_set(&rights, CAP_MMAP_R);
348 if ((flags & MAP_SHARED) != 0) {
349 if (prot & PROT_WRITE)
350 cap_rights_set(&rights, CAP_MMAP_W);
352 if (prot & PROT_EXEC)
353 cap_rights_set(&rights, CAP_MMAP_X);
354 error = fget_mmap(td, uap->fd, &rights, &cap_maxprot, &fp);
357 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
358 td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
363 /* This relies on VM_PROT_* matching PROT_*. */
364 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
365 cap_maxprot, flags, pos, td);
369 td->td_retval[0] = (register_t) (addr + pageoff);
377 #if defined(COMPAT_FREEBSD6)
379 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
381 struct mmap_args oargs;
383 oargs.addr = uap->addr;
384 oargs.len = uap->len;
385 oargs.prot = uap->prot;
386 oargs.flags = uap->flags;
388 oargs.pos = uap->pos;
389 return (sys_mmap(td, &oargs));
394 #ifndef _SYS_SYSPROTO_H_
407 struct ommap_args *uap;
409 struct mmap_args nargs;
410 static const char cvtbsdprot[8] = {
414 PROT_EXEC | PROT_WRITE,
416 PROT_EXEC | PROT_READ,
417 PROT_WRITE | PROT_READ,
418 PROT_EXEC | PROT_WRITE | PROT_READ,
421 #define OMAP_ANON 0x0002
422 #define OMAP_COPY 0x0020
423 #define OMAP_SHARED 0x0010
424 #define OMAP_FIXED 0x0100
426 nargs.addr = uap->addr;
427 nargs.len = uap->len;
428 nargs.prot = cvtbsdprot[uap->prot & 0x7];
429 #ifdef COMPAT_FREEBSD32
430 #if defined(__amd64__)
431 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
433 nargs.prot |= PROT_EXEC;
437 if (uap->flags & OMAP_ANON)
438 nargs.flags |= MAP_ANON;
439 if (uap->flags & OMAP_COPY)
440 nargs.flags |= MAP_COPY;
441 if (uap->flags & OMAP_SHARED)
442 nargs.flags |= MAP_SHARED;
444 nargs.flags |= MAP_PRIVATE;
445 if (uap->flags & OMAP_FIXED)
446 nargs.flags |= MAP_FIXED;
448 nargs.pos = uap->pos;
449 return (sys_mmap(td, &nargs));
451 #endif /* COMPAT_43 */
454 #ifndef _SYS_SYSPROTO_H_
467 struct msync_args *uap;
470 vm_size_t size, pageoff;
475 addr = (vm_offset_t) uap->addr;
479 pageoff = (addr & PAGE_MASK);
482 size = (vm_size_t) round_page(size);
483 if (addr + size < addr)
486 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
489 map = &td->td_proc->p_vmspace->vm_map;
492 * Clean the pages and interpret the return value.
494 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
495 (flags & MS_INVALIDATE) != 0);
499 case KERN_INVALID_ADDRESS:
501 case KERN_INVALID_ARGUMENT:
510 #ifndef _SYS_SYSPROTO_H_
522 struct munmap_args *uap;
525 struct pmckern_map_out pkm;
526 vm_map_entry_t entry;
529 vm_size_t size, pageoff;
532 addr = (vm_offset_t) uap->addr;
537 pageoff = (addr & PAGE_MASK);
540 size = (vm_size_t) round_page(size);
541 if (addr + size < addr)
545 * Check for illegal addresses. Watch out for address wrap...
547 map = &td->td_proc->p_vmspace->vm_map;
548 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
553 * Inform hwpmc if the address range being unmapped contains
554 * an executable region.
556 pkm.pm_address = (uintptr_t) NULL;
557 if (vm_map_lookup_entry(map, addr, &entry)) {
559 entry != &map->header && entry->start < addr + size;
560 entry = entry->next) {
561 if (vm_map_check_protection(map, entry->start,
562 entry->end, VM_PROT_EXECUTE) == TRUE) {
563 pkm.pm_address = (uintptr_t) addr;
564 pkm.pm_size = (size_t) size;
570 vm_map_delete(map, addr, addr + size);
573 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
574 vm_map_lock_downgrade(map);
575 if (pkm.pm_address != (uintptr_t) NULL)
576 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
577 vm_map_unlock_read(map);
581 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
585 #ifndef _SYS_SYSPROTO_H_
586 struct mprotect_args {
596 sys_mprotect(td, uap)
598 struct mprotect_args *uap;
601 vm_size_t size, pageoff;
604 addr = (vm_offset_t) uap->addr;
606 prot = uap->prot & VM_PROT_ALL;
608 pageoff = (addr & PAGE_MASK);
611 size = (vm_size_t) round_page(size);
612 if (addr + size < addr)
615 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
616 addr + size, prot, FALSE)) {
619 case KERN_PROTECTION_FAILURE:
621 case KERN_RESOURCE_SHORTAGE:
627 #ifndef _SYS_SYSPROTO_H_
628 struct minherit_args {
638 sys_minherit(td, uap)
640 struct minherit_args *uap;
643 vm_size_t size, pageoff;
644 vm_inherit_t inherit;
646 addr = (vm_offset_t)uap->addr;
648 inherit = uap->inherit;
650 pageoff = (addr & PAGE_MASK);
653 size = (vm_size_t) round_page(size);
654 if (addr + size < addr)
657 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
658 addr + size, inherit)) {
661 case KERN_PROTECTION_FAILURE:
667 #ifndef _SYS_SYSPROTO_H_
668 struct madvise_args {
681 struct madvise_args *uap;
683 vm_offset_t start, end;
688 * Check for our special case, advising the swap pager we are
691 if (uap->behav == MADV_PROTECT) {
693 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
694 PROC_SPROTECT, &flags));
698 * Check for illegal behavior
700 if (uap->behav < 0 || uap->behav > MADV_CORE)
703 * Check for illegal addresses. Watch out for address wrap... Note
704 * that VM_*_ADDRESS are not constants due to casts (argh).
706 map = &td->td_proc->p_vmspace->vm_map;
707 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
708 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
710 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
714 * Since this routine is only advisory, we default to conservative
717 start = trunc_page((vm_offset_t) uap->addr);
718 end = round_page((vm_offset_t) uap->addr + uap->len);
720 if (vm_map_madvise(map, start, end, uap->behav))
725 #ifndef _SYS_SYSPROTO_H_
726 struct mincore_args {
739 struct mincore_args *uap;
741 vm_offset_t addr, first_addr;
742 vm_offset_t end, cend;
747 int vecindex, lastvecindex;
748 vm_map_entry_t current;
749 vm_map_entry_t entry;
751 vm_paddr_t locked_pa;
755 unsigned int timestamp;
759 * Make sure that the addresses presented are valid for user
762 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
763 end = addr + (vm_size_t)round_page(uap->len);
764 map = &td->td_proc->p_vmspace->vm_map;
765 if (end > vm_map_max(map) || end < addr)
769 * Address of byte vector
773 pmap = vmspace_pmap(td->td_proc->p_vmspace);
775 vm_map_lock_read(map);
777 timestamp = map->timestamp;
779 if (!vm_map_lookup_entry(map, addr, &entry)) {
780 vm_map_unlock_read(map);
785 * Do this on a map entry basis so that if the pages are not
786 * in the current processes address space, we can easily look
787 * up the pages elsewhere.
790 for (current = entry;
791 (current != &map->header) && (current->start < end);
792 current = current->next) {
795 * check for contiguity
797 if (current->end < end &&
798 (entry->next == &map->header ||
799 current->next->start > current->end)) {
800 vm_map_unlock_read(map);
805 * ignore submaps (for now) or null objects
807 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
808 current->object.vm_object == NULL)
812 * limit this scan to the current map entry and the
813 * limits for the mincore call
815 if (addr < current->start)
816 addr = current->start;
822 * scan this entry one page at a time
824 while (addr < cend) {
826 * Check pmap first, it is likely faster, also
827 * it can provide info as to whether we are the
828 * one referencing or modifying the page.
834 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
835 if (locked_pa != 0) {
837 * The page is mapped by this process but not
838 * both accessed and modified. It is also
839 * managed. Acquire the object lock so that
840 * other mappings might be examined.
842 m = PHYS_TO_VM_PAGE(locked_pa);
843 if (m->object != object) {
845 VM_OBJECT_WUNLOCK(object);
847 locked = VM_OBJECT_TRYWLOCK(object);
850 VM_OBJECT_WLOCK(object);
856 KASSERT(m->valid == VM_PAGE_BITS_ALL,
857 ("mincore: page %p is mapped but invalid",
859 } else if (mincoreinfo == 0) {
861 * The page is not mapped by this process. If
862 * the object implements managed pages, then
863 * determine if the page is resident so that
864 * the mappings might be examined.
866 if (current->object.vm_object != object) {
868 VM_OBJECT_WUNLOCK(object);
869 object = current->object.vm_object;
870 VM_OBJECT_WLOCK(object);
872 if (object->type == OBJT_DEFAULT ||
873 object->type == OBJT_SWAP ||
874 object->type == OBJT_VNODE) {
875 pindex = OFF_TO_IDX(current->offset +
876 (addr - current->start));
877 m = vm_page_lookup(object, pindex);
879 vm_page_is_cached(object, pindex))
880 mincoreinfo = MINCORE_INCORE;
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 = OFF_TO_IDX(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 = OFF_TO_IDX(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_
998 struct mlock_args *uap;
1001 return (vm_mlock(td->td_proc, td->td_ucred, uap->addr, uap->len));
1005 vm_mlock(struct proc *proc, struct ucred *cred, const void *addr0, size_t len)
1007 vm_offset_t addr, end, last, start;
1008 vm_size_t npages, size;
1010 unsigned long nsize;
1013 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
1016 addr = (vm_offset_t)addr0;
1019 start = trunc_page(addr);
1020 end = round_page(last);
1021 if (last < addr || end < addr)
1023 npages = atop(end - start);
1024 if (npages > vm_page_max_wired)
1026 map = &proc->p_vmspace->vm_map;
1028 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1029 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1034 if (npages + vm_cnt.v_wire_count > vm_page_max_wired)
1039 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1045 error = vm_map_wire(map, start, end,
1046 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1048 if (racct_enable && error != KERN_SUCCESS) {
1050 racct_set(proc, RACCT_MEMLOCK,
1051 ptoa(pmap_wired_count(map->pmap)));
1055 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1058 #ifndef _SYS_SYSPROTO_H_
1059 struct mlockall_args {
1068 sys_mlockall(td, uap)
1070 struct mlockall_args *uap;
1075 map = &td->td_proc->p_vmspace->vm_map;
1076 error = priv_check(td, PRIV_VM_MLOCK);
1080 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1084 * If wiring all pages in the process would cause it to exceed
1085 * a hard resource limit, return ENOMEM.
1087 if (!old_mlock && uap->how & MCL_CURRENT) {
1088 PROC_LOCK(td->td_proc);
1089 if (map->size > lim_cur(td, RLIMIT_MEMLOCK)) {
1090 PROC_UNLOCK(td->td_proc);
1093 PROC_UNLOCK(td->td_proc);
1097 PROC_LOCK(td->td_proc);
1098 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1099 PROC_UNLOCK(td->td_proc);
1105 if (uap->how & MCL_FUTURE) {
1107 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1112 if (uap->how & MCL_CURRENT) {
1114 * P1003.1-2001 mandates that all currently mapped pages
1115 * will be memory resident and locked (wired) upon return
1116 * from mlockall(). vm_map_wire() will wire pages, by
1117 * calling vm_fault_wire() for each page in the region.
1119 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1120 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1121 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1124 if (racct_enable && error != KERN_SUCCESS) {
1125 PROC_LOCK(td->td_proc);
1126 racct_set(td->td_proc, RACCT_MEMLOCK,
1127 ptoa(pmap_wired_count(map->pmap)));
1128 PROC_UNLOCK(td->td_proc);
1135 #ifndef _SYS_SYSPROTO_H_
1136 struct munlockall_args {
1145 sys_munlockall(td, uap)
1147 struct munlockall_args *uap;
1152 map = &td->td_proc->p_vmspace->vm_map;
1153 error = priv_check(td, PRIV_VM_MUNLOCK);
1157 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1159 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1162 /* Forcibly unwire all pages. */
1163 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1164 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1166 if (racct_enable && error == KERN_SUCCESS) {
1167 PROC_LOCK(td->td_proc);
1168 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1169 PROC_UNLOCK(td->td_proc);
1176 #ifndef _SYS_SYSPROTO_H_
1177 struct munlock_args {
1186 sys_munlock(td, uap)
1188 struct munlock_args *uap;
1190 vm_offset_t addr, end, last, start;
1197 error = priv_check(td, PRIV_VM_MUNLOCK);
1200 addr = (vm_offset_t)uap->addr;
1203 start = trunc_page(addr);
1204 end = round_page(last);
1205 if (last < addr || end < addr)
1207 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1208 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1210 if (racct_enable && error == KERN_SUCCESS) {
1211 PROC_LOCK(td->td_proc);
1212 map = &td->td_proc->p_vmspace->vm_map;
1213 racct_set(td->td_proc, RACCT_MEMLOCK,
1214 ptoa(pmap_wired_count(map->pmap)));
1215 PROC_UNLOCK(td->td_proc);
1218 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1224 * Helper function for vm_mmap. Perform sanity check specific for mmap
1225 * operations on vnodes.
1228 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1229 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1230 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1231 boolean_t *writecounted)
1237 int error, flags, locktype;
1239 cred = td->td_ucred;
1240 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1241 locktype = LK_EXCLUSIVE;
1243 locktype = LK_SHARED;
1244 if ((error = vget(vp, locktype, td)) != 0)
1249 if (vp->v_type == VREG) {
1251 * Get the proper underlying object
1257 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1259 vp = (struct vnode *)obj->handle;
1261 * Bypass filesystems obey the mpsafety of the
1262 * underlying fs. Tmpfs never bypasses.
1264 error = vget(vp, locktype, td);
1268 if (locktype == LK_EXCLUSIVE) {
1269 *writecounted = TRUE;
1270 vnode_pager_update_writecount(obj, 0, objsize);
1276 if ((error = VOP_GETATTR(vp, &va, cred)))
1279 /* This relies on VM_PROT_* matching PROT_*. */
1280 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1284 if ((flags & MAP_SHARED) != 0) {
1285 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1286 if (prot & VM_PROT_WRITE) {
1290 *maxprotp &= ~VM_PROT_WRITE;
1294 * If it is a regular file without any references
1295 * we do not need to sync it.
1296 * Adjust object size to be the size of actual file.
1298 objsize = round_page(va.va_size);
1299 if (va.va_nlink == 0)
1300 flags |= MAP_NOSYNC;
1301 if (obj->type == OBJT_VNODE) {
1302 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1309 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1310 ("wrong object type"));
1311 VM_OBJECT_WLOCK(obj);
1312 vm_object_reference_locked(obj);
1313 #if VM_NRESERVLEVEL > 0
1314 vm_object_color(obj, 0);
1316 VM_OBJECT_WUNLOCK(obj);
1321 vfs_mark_atime(vp, cred);
1324 if (error != 0 && *writecounted) {
1325 *writecounted = FALSE;
1326 vnode_pager_update_writecount(obj, objsize, 0);
1337 * Helper function for vm_mmap. Perform sanity check specific for mmap
1338 * operations on cdevs.
1341 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1342 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1343 vm_ooffset_t *foff, vm_object_t *objp)
1350 if (dsw->d_flags & D_MMAP_ANON) {
1353 *maxprotp = VM_PROT_ALL;
1354 *flagsp |= MAP_ANON;
1358 * cdevs do not provide private mappings of any kind.
1360 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1361 (prot & VM_PROT_WRITE) != 0)
1363 if (flags & (MAP_PRIVATE|MAP_COPY))
1366 * Force device mappings to be shared.
1368 flags |= MAP_SHARED;
1370 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1375 * First, try d_mmap_single(). If that is not implemented
1376 * (returns ENODEV), fall back to using the device pager.
1377 * Note that d_mmap_single() must return a reference to the
1378 * object (it needs to bump the reference count of the object
1379 * it returns somehow).
1381 * XXX assumes VM_PROT_* == PROT_*
1383 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1384 if (error != ENODEV)
1386 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1398 * Internal version of mmap used by exec, sys5 shared memory, and
1399 * various device drivers. Handle is either a vnode pointer, a
1400 * character device, or NULL for MAP_ANON.
1403 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1404 vm_prot_t maxprot, int flags,
1405 objtype_t handle_type, void *handle,
1409 struct thread *td = curthread;
1411 boolean_t writecounted;
1416 size = round_page(size);
1418 writecounted = FALSE;
1421 * Lookup/allocate object.
1423 switch (handle_type) {
1430 dsw = dev_refthread(cdev, &ref);
1433 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1434 dsw, &foff, &object);
1435 dev_relthread(cdev, ref);
1439 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1440 handle, &foff, &object, &writecounted);
1443 if (handle == NULL) {
1455 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1456 foff, writecounted, td);
1457 if (error != 0 && object != NULL) {
1459 * If this mapping was accounted for in the vnode's
1460 * writecount, then undo that now.
1463 vnode_pager_release_writecount(object, 0, size);
1464 vm_object_deallocate(object);
1470 * Internal version of mmap that maps a specific VM object into an
1471 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1474 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1475 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1476 boolean_t writecounted, struct thread *td)
1479 int docow, error, findspace, rv;
1481 if (map == &td->td_proc->p_vmspace->vm_map) {
1482 PROC_LOCK(td->td_proc);
1483 if (map->size + size > lim_cur_proc(td->td_proc, RLIMIT_VMEM)) {
1484 PROC_UNLOCK(td->td_proc);
1487 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1488 PROC_UNLOCK(td->td_proc);
1491 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1492 if (ptoa(pmap_wired_count(map->pmap)) + size >
1493 lim_cur_proc(td->td_proc, RLIMIT_MEMLOCK)) {
1494 racct_set_force(td->td_proc, RACCT_VMEM,
1496 PROC_UNLOCK(td->td_proc);
1499 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1500 ptoa(pmap_wired_count(map->pmap)) + size);
1502 racct_set_force(td->td_proc, RACCT_VMEM,
1504 PROC_UNLOCK(td->td_proc);
1508 PROC_UNLOCK(td->td_proc);
1512 * We currently can only deal with page aligned file offsets.
1513 * The mmap() system call already enforces this by subtracting
1514 * the page offset from the file offset, but checking here
1515 * catches errors in device drivers (e.g. d_single_mmap()
1516 * callbacks) and other internal mapping requests (such as in
1519 if (foff & PAGE_MASK)
1522 if ((flags & MAP_FIXED) == 0) {
1524 *addr = round_page(*addr);
1526 if (*addr != trunc_page(*addr))
1531 if (flags & MAP_ANON) {
1532 if (object != NULL || foff != 0)
1535 } else if (flags & MAP_PREFAULT_READ)
1536 docow = MAP_PREFAULT;
1538 docow = MAP_PREFAULT_PARTIAL;
1540 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1541 docow |= MAP_COPY_ON_WRITE;
1542 if (flags & MAP_NOSYNC)
1543 docow |= MAP_DISABLE_SYNCER;
1544 if (flags & MAP_NOCORE)
1545 docow |= MAP_DISABLE_COREDUMP;
1546 /* Shared memory is also shared with children. */
1547 if (flags & MAP_SHARED)
1548 docow |= MAP_INHERIT_SHARE;
1550 docow |= MAP_VN_WRITECOUNT;
1551 if (flags & MAP_STACK) {
1554 docow |= MAP_STACK_GROWS_DOWN;
1556 if ((flags & MAP_EXCL) != 0)
1557 docow |= MAP_CHECK_EXCL;
1560 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1561 findspace = VMFS_SUPER_SPACE;
1562 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1563 findspace = VMFS_ALIGNED_SPACE(flags >>
1564 MAP_ALIGNMENT_SHIFT);
1566 findspace = VMFS_OPTIMAL_SPACE;
1567 rv = vm_map_find(map, object, foff, addr, size,
1569 flags & MAP_32BIT ? MAP_32BIT_MAX_ADDR :
1571 0, findspace, prot, maxprot, docow);
1573 rv = vm_map_fixed(map, object, foff, *addr, size,
1574 prot, maxprot, docow);
1577 if (rv == KERN_SUCCESS) {
1579 * If the process has requested that all future mappings
1580 * be wired, then heed this.
1582 if (map->flags & MAP_WIREFUTURE) {
1583 vm_map_wire(map, *addr, *addr + size,
1584 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1585 VM_MAP_WIRE_HOLESOK : 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: