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_
110 sys_sbrk(struct thread *td, struct sbrk_args *uap)
112 /* Not yet implemented */
116 #ifndef _SYS_SYSPROTO_H_
123 sys_sstk(struct thread *td, struct sstk_args *uap)
125 /* Not yet implemented */
129 #if defined(COMPAT_43)
130 #ifndef _SYS_SYSPROTO_H_
131 struct getpagesize_args {
137 ogetpagesize(struct thread *td, struct getpagesize_args *uap)
140 td->td_retval[0] = PAGE_SIZE;
143 #endif /* COMPAT_43 */
147 * Memory Map (mmap) system call. Note that the file offset
148 * and address are allowed to be NOT page aligned, though if
149 * the MAP_FIXED flag it set, both must have the same remainder
150 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
151 * page-aligned, the actual mapping starts at trunc_page(addr)
152 * and the return value is adjusted up by the page offset.
154 * Generally speaking, only character devices which are themselves
155 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
156 * there would be no cache coherency between a descriptor and a VM mapping
157 * both to the same character device.
159 #ifndef _SYS_SYSPROTO_H_
174 struct mmap_args *uap;
178 vm_size_t size, pageoff;
179 vm_prot_t cap_maxprot;
180 int align, error, flags, prot;
182 struct vmspace *vms = td->td_proc->p_vmspace;
185 addr = (vm_offset_t) uap->addr;
194 * Ignore old flags that used to be defined but did not do anything.
196 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
199 * Enforce the constraints.
200 * Mapping of length 0 is only allowed for old binaries.
201 * Anonymous mapping shall specify -1 as filedescriptor and
202 * zero position for new code. Be nice to ancient a.out
203 * binaries and correct pos for anonymous mapping, since old
204 * ld.so sometimes issues anonymous map requests with non-zero
207 if (!SV_CURPROC_FLAG(SV_AOUT)) {
208 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
209 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
212 if ((flags & MAP_ANON) != 0)
216 if (flags & MAP_STACK) {
217 if ((uap->fd != -1) ||
218 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
223 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
224 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
229 MAP_ALIGNMENT_MASK)) != 0)
231 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
233 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
235 if (prot != PROT_NONE &&
236 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
240 * Align the file position to a page boundary,
241 * and save its page offset component.
243 pageoff = (pos & PAGE_MASK);
246 /* Adjust size for rounding (on both ends). */
247 size += pageoff; /* low end... */
248 size = (vm_size_t) round_page(size); /* hi end */
250 /* Ensure alignment is at least a page and fits in a pointer. */
251 align = flags & MAP_ALIGNMENT_MASK;
252 if (align != 0 && align != MAP_ALIGNED_SUPER &&
253 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
254 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
258 * Check for illegal addresses. Watch out for address wrap... Note
259 * that VM_*_ADDRESS are not constants due to casts (argh).
261 if (flags & MAP_FIXED) {
263 * The specified address must have the same remainder
264 * as the file offset taken modulo PAGE_SIZE, so it
265 * should be aligned after adjustment by pageoff.
268 if (addr & PAGE_MASK)
271 /* Address range must be all in user VM space. */
272 if (addr < vm_map_min(&vms->vm_map) ||
273 addr + size > vm_map_max(&vms->vm_map))
275 if (addr + size < addr)
278 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
280 } else if (flags & MAP_32BIT) {
282 * For MAP_32BIT, override the hint if it is too high and
283 * do not bother moving the mapping past the heap (since
284 * the heap is usually above 2GB).
286 if (addr + size > MAP_32BIT_MAX_ADDR)
291 * XXX for non-fixed mappings where no hint is provided or
292 * the hint would fall in the potential heap space,
293 * place it after the end of the largest possible heap.
295 * There should really be a pmap call to determine a reasonable
299 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
300 addr < round_page((vm_offset_t)vms->vm_daddr +
301 lim_max(td, RLIMIT_DATA))))
302 addr = round_page((vm_offset_t)vms->vm_daddr +
303 lim_max(td, RLIMIT_DATA));
307 * Return success without mapping anything for old
308 * binaries that request a page-aligned mapping of
309 * length 0. For modern binaries, this function
310 * returns an error earlier.
313 } else if (flags & MAP_ANON) {
315 * Mapping blank space is trivial.
317 * This relies on VM_PROT_* matching PROT_*.
319 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
320 VM_PROT_ALL, flags, NULL, pos, FALSE, td);
323 * Mapping file, get fp for validation and don't let the
324 * descriptor disappear on us if we block. Check capability
325 * rights, but also return the maximum rights to be combined
326 * with maxprot later.
328 cap_rights_init(&rights, CAP_MMAP);
329 if (prot & PROT_READ)
330 cap_rights_set(&rights, CAP_MMAP_R);
331 if ((flags & MAP_SHARED) != 0) {
332 if (prot & PROT_WRITE)
333 cap_rights_set(&rights, CAP_MMAP_W);
335 if (prot & PROT_EXEC)
336 cap_rights_set(&rights, CAP_MMAP_X);
337 error = fget_mmap(td, uap->fd, &rights, &cap_maxprot, &fp);
340 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
341 td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
346 /* This relies on VM_PROT_* matching PROT_*. */
347 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
348 cap_maxprot, flags, pos, td);
352 td->td_retval[0] = (register_t) (addr + pageoff);
360 #if defined(COMPAT_FREEBSD6)
362 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
364 struct mmap_args oargs;
366 oargs.addr = uap->addr;
367 oargs.len = uap->len;
368 oargs.prot = uap->prot;
369 oargs.flags = uap->flags;
371 oargs.pos = uap->pos;
372 return (sys_mmap(td, &oargs));
377 #ifndef _SYS_SYSPROTO_H_
390 struct ommap_args *uap;
392 struct mmap_args nargs;
393 static const char cvtbsdprot[8] = {
397 PROT_EXEC | PROT_WRITE,
399 PROT_EXEC | PROT_READ,
400 PROT_WRITE | PROT_READ,
401 PROT_EXEC | PROT_WRITE | PROT_READ,
404 #define OMAP_ANON 0x0002
405 #define OMAP_COPY 0x0020
406 #define OMAP_SHARED 0x0010
407 #define OMAP_FIXED 0x0100
409 nargs.addr = uap->addr;
410 nargs.len = uap->len;
411 nargs.prot = cvtbsdprot[uap->prot & 0x7];
412 #ifdef COMPAT_FREEBSD32
413 #if defined(__amd64__)
414 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
416 nargs.prot |= PROT_EXEC;
420 if (uap->flags & OMAP_ANON)
421 nargs.flags |= MAP_ANON;
422 if (uap->flags & OMAP_COPY)
423 nargs.flags |= MAP_COPY;
424 if (uap->flags & OMAP_SHARED)
425 nargs.flags |= MAP_SHARED;
427 nargs.flags |= MAP_PRIVATE;
428 if (uap->flags & OMAP_FIXED)
429 nargs.flags |= MAP_FIXED;
431 nargs.pos = uap->pos;
432 return (sys_mmap(td, &nargs));
434 #endif /* COMPAT_43 */
437 #ifndef _SYS_SYSPROTO_H_
447 struct msync_args *uap;
450 vm_size_t size, pageoff;
455 addr = (vm_offset_t) uap->addr;
459 pageoff = (addr & PAGE_MASK);
462 size = (vm_size_t) round_page(size);
463 if (addr + size < addr)
466 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
469 map = &td->td_proc->p_vmspace->vm_map;
472 * Clean the pages and interpret the return value.
474 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
475 (flags & MS_INVALIDATE) != 0);
479 case KERN_INVALID_ADDRESS:
481 case KERN_INVALID_ARGUMENT:
490 #ifndef _SYS_SYSPROTO_H_
499 struct munmap_args *uap;
502 struct pmckern_map_out pkm;
503 vm_map_entry_t entry;
506 vm_size_t size, pageoff;
509 addr = (vm_offset_t) uap->addr;
514 pageoff = (addr & PAGE_MASK);
517 size = (vm_size_t) round_page(size);
518 if (addr + size < addr)
522 * Check for illegal addresses. Watch out for address wrap...
524 map = &td->td_proc->p_vmspace->vm_map;
525 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
530 * Inform hwpmc if the address range being unmapped contains
531 * an executable region.
533 pkm.pm_address = (uintptr_t) NULL;
534 if (vm_map_lookup_entry(map, addr, &entry)) {
536 entry != &map->header && entry->start < addr + size;
537 entry = entry->next) {
538 if (vm_map_check_protection(map, entry->start,
539 entry->end, VM_PROT_EXECUTE) == TRUE) {
540 pkm.pm_address = (uintptr_t) addr;
541 pkm.pm_size = (size_t) size;
547 vm_map_delete(map, addr, addr + size);
550 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
551 vm_map_lock_downgrade(map);
552 if (pkm.pm_address != (uintptr_t) NULL)
553 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
554 vm_map_unlock_read(map);
558 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
562 #ifndef _SYS_SYSPROTO_H_
563 struct mprotect_args {
570 sys_mprotect(td, uap)
572 struct mprotect_args *uap;
575 vm_size_t size, pageoff;
578 addr = (vm_offset_t) uap->addr;
580 prot = uap->prot & VM_PROT_ALL;
582 pageoff = (addr & PAGE_MASK);
585 size = (vm_size_t) round_page(size);
586 if (addr + size < addr)
589 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
590 addr + size, prot, FALSE)) {
593 case KERN_PROTECTION_FAILURE:
595 case KERN_RESOURCE_SHORTAGE:
601 #ifndef _SYS_SYSPROTO_H_
602 struct minherit_args {
609 sys_minherit(struct thread *td, struct minherit_args *uap)
612 vm_size_t size, pageoff;
613 vm_inherit_t inherit;
615 addr = (vm_offset_t)uap->addr;
617 inherit = uap->inherit;
619 pageoff = (addr & PAGE_MASK);
622 size = (vm_size_t) round_page(size);
623 if (addr + size < addr)
626 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
627 addr + size, inherit)) {
630 case KERN_PROTECTION_FAILURE:
636 #ifndef _SYS_SYSPROTO_H_
637 struct madvise_args {
645 sys_madvise(struct thread *td, struct madvise_args *uap)
647 vm_offset_t start, end;
652 * Check for our special case, advising the swap pager we are
655 if (uap->behav == MADV_PROTECT) {
657 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
658 PROC_SPROTECT, &flags));
662 * Check for illegal behavior
664 if (uap->behav < 0 || uap->behav > MADV_CORE)
667 * Check for illegal addresses. Watch out for address wrap... Note
668 * that VM_*_ADDRESS are not constants due to casts (argh).
670 map = &td->td_proc->p_vmspace->vm_map;
671 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
672 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
674 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
678 * Since this routine is only advisory, we default to conservative
681 start = trunc_page((vm_offset_t) uap->addr);
682 end = round_page((vm_offset_t) uap->addr + uap->len);
684 if (vm_map_madvise(map, start, end, uap->behav))
689 #ifndef _SYS_SYSPROTO_H_
690 struct mincore_args {
698 sys_mincore(struct thread *td, struct mincore_args *uap)
700 vm_offset_t addr, first_addr;
701 vm_offset_t end, cend;
706 int vecindex, lastvecindex;
707 vm_map_entry_t current;
708 vm_map_entry_t entry;
710 vm_paddr_t locked_pa;
714 unsigned int timestamp;
718 * Make sure that the addresses presented are valid for user
721 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
722 end = addr + (vm_size_t)round_page(uap->len);
723 map = &td->td_proc->p_vmspace->vm_map;
724 if (end > vm_map_max(map) || end < addr)
728 * Address of byte vector
732 pmap = vmspace_pmap(td->td_proc->p_vmspace);
734 vm_map_lock_read(map);
736 timestamp = map->timestamp;
738 if (!vm_map_lookup_entry(map, addr, &entry)) {
739 vm_map_unlock_read(map);
744 * Do this on a map entry basis so that if the pages are not
745 * in the current processes address space, we can easily look
746 * up the pages elsewhere.
749 for (current = entry;
750 (current != &map->header) && (current->start < end);
751 current = current->next) {
754 * check for contiguity
756 if (current->end < end &&
757 (entry->next == &map->header ||
758 current->next->start > current->end)) {
759 vm_map_unlock_read(map);
764 * ignore submaps (for now) or null objects
766 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
767 current->object.vm_object == NULL)
771 * limit this scan to the current map entry and the
772 * limits for the mincore call
774 if (addr < current->start)
775 addr = current->start;
781 * scan this entry one page at a time
783 while (addr < cend) {
785 * Check pmap first, it is likely faster, also
786 * it can provide info as to whether we are the
787 * one referencing or modifying the page.
793 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
794 if (locked_pa != 0) {
796 * The page is mapped by this process but not
797 * both accessed and modified. It is also
798 * managed. Acquire the object lock so that
799 * other mappings might be examined.
801 m = PHYS_TO_VM_PAGE(locked_pa);
802 if (m->object != object) {
804 VM_OBJECT_WUNLOCK(object);
806 locked = VM_OBJECT_TRYWLOCK(object);
809 VM_OBJECT_WLOCK(object);
815 KASSERT(m->valid == VM_PAGE_BITS_ALL,
816 ("mincore: page %p is mapped but invalid",
818 } else if (mincoreinfo == 0) {
820 * The page is not mapped by this process. If
821 * the object implements managed pages, then
822 * determine if the page is resident so that
823 * the mappings might be examined.
825 if (current->object.vm_object != object) {
827 VM_OBJECT_WUNLOCK(object);
828 object = current->object.vm_object;
829 VM_OBJECT_WLOCK(object);
831 if (object->type == OBJT_DEFAULT ||
832 object->type == OBJT_SWAP ||
833 object->type == OBJT_VNODE) {
834 pindex = OFF_TO_IDX(current->offset +
835 (addr - current->start));
836 m = vm_page_lookup(object, pindex);
838 vm_page_is_cached(object, pindex))
839 mincoreinfo = MINCORE_INCORE;
840 if (m != NULL && m->valid == 0)
843 mincoreinfo = MINCORE_INCORE;
847 /* Examine other mappings to the page. */
848 if (m->dirty == 0 && pmap_is_modified(m))
851 mincoreinfo |= MINCORE_MODIFIED_OTHER;
853 * The first test for PGA_REFERENCED is an
854 * optimization. The second test is
855 * required because a concurrent pmap
856 * operation could clear the last reference
857 * and set PGA_REFERENCED before the call to
858 * pmap_is_referenced().
860 if ((m->aflags & PGA_REFERENCED) != 0 ||
861 pmap_is_referenced(m) ||
862 (m->aflags & PGA_REFERENCED) != 0)
863 mincoreinfo |= MINCORE_REFERENCED_OTHER;
866 VM_OBJECT_WUNLOCK(object);
869 * subyte may page fault. In case it needs to modify
870 * the map, we release the lock.
872 vm_map_unlock_read(map);
875 * calculate index into user supplied byte vector
877 vecindex = OFF_TO_IDX(addr - first_addr);
880 * If we have skipped map entries, we need to make sure that
881 * the byte vector is zeroed for those skipped entries.
883 while ((lastvecindex + 1) < vecindex) {
885 error = subyte(vec + lastvecindex, 0);
893 * Pass the page information to the user
895 error = subyte(vec + vecindex, mincoreinfo);
902 * If the map has changed, due to the subyte, the previous
903 * output may be invalid.
905 vm_map_lock_read(map);
906 if (timestamp != map->timestamp)
909 lastvecindex = vecindex;
915 * subyte may page fault. In case it needs to modify
916 * the map, we release the lock.
918 vm_map_unlock_read(map);
921 * Zero the last entries in the byte vector.
923 vecindex = OFF_TO_IDX(end - first_addr);
924 while ((lastvecindex + 1) < vecindex) {
926 error = subyte(vec + lastvecindex, 0);
934 * If the map has changed, due to the subyte, the previous
935 * output may be invalid.
937 vm_map_lock_read(map);
938 if (timestamp != map->timestamp)
940 vm_map_unlock_read(map);
945 #ifndef _SYS_SYSPROTO_H_
952 sys_mlock(struct thread *td, struct mlock_args *uap)
955 return (vm_mlock(td->td_proc, td->td_ucred, uap->addr, uap->len));
959 vm_mlock(struct proc *proc, struct ucred *cred, const void *addr0, size_t len)
961 vm_offset_t addr, end, last, start;
962 vm_size_t npages, size;
967 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
970 addr = (vm_offset_t)addr0;
973 start = trunc_page(addr);
974 end = round_page(last);
975 if (last < addr || end < addr)
977 npages = atop(end - start);
978 if (npages > vm_page_max_wired)
980 map = &proc->p_vmspace->vm_map;
982 nsize = ptoa(npages + pmap_wired_count(map->pmap));
983 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
988 if (npages + vm_cnt.v_wire_count > vm_page_max_wired)
993 error = racct_set(proc, RACCT_MEMLOCK, nsize);
999 error = vm_map_wire(map, start, end,
1000 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1002 if (racct_enable && error != KERN_SUCCESS) {
1004 racct_set(proc, RACCT_MEMLOCK,
1005 ptoa(pmap_wired_count(map->pmap)));
1009 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1012 #ifndef _SYS_SYSPROTO_H_
1013 struct mlockall_args {
1019 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1024 map = &td->td_proc->p_vmspace->vm_map;
1025 error = priv_check(td, PRIV_VM_MLOCK);
1029 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1033 * If wiring all pages in the process would cause it to exceed
1034 * a hard resource limit, return ENOMEM.
1036 if (!old_mlock && uap->how & MCL_CURRENT) {
1037 PROC_LOCK(td->td_proc);
1038 if (map->size > lim_cur(td, RLIMIT_MEMLOCK)) {
1039 PROC_UNLOCK(td->td_proc);
1042 PROC_UNLOCK(td->td_proc);
1046 PROC_LOCK(td->td_proc);
1047 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1048 PROC_UNLOCK(td->td_proc);
1054 if (uap->how & MCL_FUTURE) {
1056 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1061 if (uap->how & MCL_CURRENT) {
1063 * P1003.1-2001 mandates that all currently mapped pages
1064 * will be memory resident and locked (wired) upon return
1065 * from mlockall(). vm_map_wire() will wire pages, by
1066 * calling vm_fault_wire() for each page in the region.
1068 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1069 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1070 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1073 if (racct_enable && error != KERN_SUCCESS) {
1074 PROC_LOCK(td->td_proc);
1075 racct_set(td->td_proc, RACCT_MEMLOCK,
1076 ptoa(pmap_wired_count(map->pmap)));
1077 PROC_UNLOCK(td->td_proc);
1084 #ifndef _SYS_SYSPROTO_H_
1085 struct munlockall_args {
1091 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1096 map = &td->td_proc->p_vmspace->vm_map;
1097 error = priv_check(td, PRIV_VM_MUNLOCK);
1101 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1103 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1106 /* Forcibly unwire all pages. */
1107 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1108 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1110 if (racct_enable && error == KERN_SUCCESS) {
1111 PROC_LOCK(td->td_proc);
1112 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1113 PROC_UNLOCK(td->td_proc);
1120 #ifndef _SYS_SYSPROTO_H_
1121 struct munlock_args {
1127 sys_munlock(td, uap)
1129 struct munlock_args *uap;
1131 vm_offset_t addr, end, last, start;
1138 error = priv_check(td, PRIV_VM_MUNLOCK);
1141 addr = (vm_offset_t)uap->addr;
1144 start = trunc_page(addr);
1145 end = round_page(last);
1146 if (last < addr || end < addr)
1148 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1149 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1151 if (racct_enable && error == KERN_SUCCESS) {
1152 PROC_LOCK(td->td_proc);
1153 map = &td->td_proc->p_vmspace->vm_map;
1154 racct_set(td->td_proc, RACCT_MEMLOCK,
1155 ptoa(pmap_wired_count(map->pmap)));
1156 PROC_UNLOCK(td->td_proc);
1159 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1165 * Helper function for vm_mmap. Perform sanity check specific for mmap
1166 * operations on vnodes.
1169 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1170 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1171 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1172 boolean_t *writecounted)
1178 int error, flags, locktype;
1180 cred = td->td_ucred;
1181 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1182 locktype = LK_EXCLUSIVE;
1184 locktype = LK_SHARED;
1185 if ((error = vget(vp, locktype, td)) != 0)
1190 if (vp->v_type == VREG) {
1192 * Get the proper underlying object
1198 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1200 vp = (struct vnode *)obj->handle;
1202 * Bypass filesystems obey the mpsafety of the
1203 * underlying fs. Tmpfs never bypasses.
1205 error = vget(vp, locktype, td);
1209 if (locktype == LK_EXCLUSIVE) {
1210 *writecounted = TRUE;
1211 vnode_pager_update_writecount(obj, 0, objsize);
1217 if ((error = VOP_GETATTR(vp, &va, cred)))
1220 /* This relies on VM_PROT_* matching PROT_*. */
1221 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1225 if ((flags & MAP_SHARED) != 0) {
1226 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1227 if (prot & VM_PROT_WRITE) {
1231 *maxprotp &= ~VM_PROT_WRITE;
1235 * If it is a regular file without any references
1236 * we do not need to sync it.
1237 * Adjust object size to be the size of actual file.
1239 objsize = round_page(va.va_size);
1240 if (va.va_nlink == 0)
1241 flags |= MAP_NOSYNC;
1242 if (obj->type == OBJT_VNODE) {
1243 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1250 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1251 ("wrong object type"));
1252 VM_OBJECT_WLOCK(obj);
1253 vm_object_reference_locked(obj);
1254 #if VM_NRESERVLEVEL > 0
1255 vm_object_color(obj, 0);
1257 VM_OBJECT_WUNLOCK(obj);
1262 vfs_mark_atime(vp, cred);
1265 if (error != 0 && *writecounted) {
1266 *writecounted = FALSE;
1267 vnode_pager_update_writecount(obj, objsize, 0);
1276 * Helper function for vm_mmap. Perform sanity check specific for mmap
1277 * operations on cdevs.
1280 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
1281 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1282 vm_ooffset_t *foff, vm_object_t *objp)
1289 if (dsw->d_flags & D_MMAP_ANON) {
1292 *maxprotp = VM_PROT_ALL;
1293 *flagsp |= MAP_ANON;
1297 * cdevs do not provide private mappings of any kind.
1299 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1300 (prot & VM_PROT_WRITE) != 0)
1302 if (flags & (MAP_PRIVATE|MAP_COPY))
1305 * Force device mappings to be shared.
1307 flags |= MAP_SHARED;
1309 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1314 * First, try d_mmap_single(). If that is not implemented
1315 * (returns ENODEV), fall back to using the device pager.
1316 * Note that d_mmap_single() must return a reference to the
1317 * object (it needs to bump the reference count of the object
1318 * it returns somehow).
1320 * XXX assumes VM_PROT_* == PROT_*
1322 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1323 if (error != ENODEV)
1325 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1337 * Internal version of mmap used by exec, sys5 shared memory, and
1338 * various device drivers. Handle is either a vnode pointer, a
1339 * character device, or NULL for MAP_ANON.
1342 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1343 vm_prot_t maxprot, int flags,
1344 objtype_t handle_type, void *handle,
1348 struct thread *td = curthread;
1350 boolean_t writecounted;
1355 size = round_page(size);
1357 writecounted = FALSE;
1360 * Lookup/allocate object.
1362 switch (handle_type) {
1369 dsw = dev_refthread(cdev, &ref);
1372 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1373 dsw, &foff, &object);
1374 dev_relthread(cdev, ref);
1378 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1379 handle, &foff, &object, &writecounted);
1382 if (handle == NULL) {
1394 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1395 foff, writecounted, td);
1396 if (error != 0 && object != NULL) {
1398 * If this mapping was accounted for in the vnode's
1399 * writecount, then undo that now.
1402 vnode_pager_release_writecount(object, 0, size);
1403 vm_object_deallocate(object);
1409 * Internal version of mmap that maps a specific VM object into an
1410 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1413 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1414 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1415 boolean_t writecounted, struct thread *td)
1418 int docow, error, findspace, rv;
1420 if (map == &td->td_proc->p_vmspace->vm_map) {
1421 PROC_LOCK(td->td_proc);
1422 if (map->size + size > lim_cur_proc(td->td_proc, RLIMIT_VMEM)) {
1423 PROC_UNLOCK(td->td_proc);
1426 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1427 PROC_UNLOCK(td->td_proc);
1430 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1431 if (ptoa(pmap_wired_count(map->pmap)) + size >
1432 lim_cur_proc(td->td_proc, RLIMIT_MEMLOCK)) {
1433 racct_set_force(td->td_proc, RACCT_VMEM,
1435 PROC_UNLOCK(td->td_proc);
1438 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1439 ptoa(pmap_wired_count(map->pmap)) + size);
1441 racct_set_force(td->td_proc, RACCT_VMEM,
1443 PROC_UNLOCK(td->td_proc);
1447 PROC_UNLOCK(td->td_proc);
1451 * We currently can only deal with page aligned file offsets.
1452 * The mmap() system call already enforces this by subtracting
1453 * the page offset from the file offset, but checking here
1454 * catches errors in device drivers (e.g. d_single_mmap()
1455 * callbacks) and other internal mapping requests (such as in
1458 if (foff & PAGE_MASK)
1461 if ((flags & MAP_FIXED) == 0) {
1463 *addr = round_page(*addr);
1465 if (*addr != trunc_page(*addr))
1470 if (flags & MAP_ANON) {
1471 if (object != NULL || foff != 0)
1474 } else if (flags & MAP_PREFAULT_READ)
1475 docow = MAP_PREFAULT;
1477 docow = MAP_PREFAULT_PARTIAL;
1479 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1480 docow |= MAP_COPY_ON_WRITE;
1481 if (flags & MAP_NOSYNC)
1482 docow |= MAP_DISABLE_SYNCER;
1483 if (flags & MAP_NOCORE)
1484 docow |= MAP_DISABLE_COREDUMP;
1485 /* Shared memory is also shared with children. */
1486 if (flags & MAP_SHARED)
1487 docow |= MAP_INHERIT_SHARE;
1489 docow |= MAP_VN_WRITECOUNT;
1490 if (flags & MAP_STACK) {
1493 docow |= MAP_STACK_GROWS_DOWN;
1495 if ((flags & MAP_EXCL) != 0)
1496 docow |= MAP_CHECK_EXCL;
1499 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1500 findspace = VMFS_SUPER_SPACE;
1501 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1502 findspace = VMFS_ALIGNED_SPACE(flags >>
1503 MAP_ALIGNMENT_SHIFT);
1505 findspace = VMFS_OPTIMAL_SPACE;
1506 rv = vm_map_find(map, object, foff, addr, size,
1508 flags & MAP_32BIT ? MAP_32BIT_MAX_ADDR :
1510 0, findspace, prot, maxprot, docow);
1512 rv = vm_map_fixed(map, object, foff, *addr, size,
1513 prot, maxprot, docow);
1516 if (rv == KERN_SUCCESS) {
1518 * If the process has requested that all future mappings
1519 * be wired, then heed this.
1521 if (map->flags & MAP_WIREFUTURE) {
1522 vm_map_wire(map, *addr, *addr + size,
1523 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1524 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1527 return (vm_mmap_to_errno(rv));
1531 * Translate a Mach VM return code to zero on success or the appropriate errno
1535 vm_mmap_to_errno(int rv)
1541 case KERN_INVALID_ADDRESS:
1544 case KERN_PROTECTION_FAILURE: