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"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capability.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/filedesc.h>
59 #include <sys/racct.h>
60 #include <sys/resource.h>
61 #include <sys/resourcevar.h>
62 #include <sys/vnode.h>
63 #include <sys/fcntl.h>
66 #include <sys/mount.h>
69 #include <sys/sysent.h>
70 #include <sys/vmmeter.h>
72 #include <security/mac/mac_framework.h>
75 #include <vm/vm_param.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vm_pageout.h>
82 #include <vm/vm_extern.h>
83 #include <vm/vm_page.h>
84 #include <vm/vnode_pager.h>
87 #include <sys/pmckern.h>
90 #ifndef _SYS_SYSPROTO_H_
96 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
97 int *, struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
98 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
99 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
100 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
101 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
110 struct sbrk_args *uap;
112 /* Not yet implemented */
116 #ifndef _SYS_SYSPROTO_H_
129 struct sstk_args *uap;
131 /* Not yet implemented */
135 #if defined(COMPAT_43)
136 #ifndef _SYS_SYSPROTO_H_
137 struct getpagesize_args {
143 ogetpagesize(td, uap)
145 struct getpagesize_args *uap;
148 td->td_retval[0] = PAGE_SIZE;
151 #endif /* COMPAT_43 */
155 * Memory Map (mmap) system call. Note that the file offset
156 * and address are allowed to be NOT page aligned, though if
157 * the MAP_FIXED flag it set, both must have the same remainder
158 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
159 * page-aligned, the actual mapping starts at trunc_page(addr)
160 * and the return value is adjusted up by the page offset.
162 * Generally speaking, only character devices which are themselves
163 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
164 * there would be no cache coherency between a descriptor and a VM mapping
165 * both to the same character device.
167 #ifndef _SYS_SYSPROTO_H_
185 struct mmap_args *uap;
188 struct pmckern_map_in pkm;
193 vm_size_t size, pageoff;
194 vm_prot_t cap_maxprot, prot, maxprot;
196 objtype_t handle_type;
199 struct vmspace *vms = td->td_proc->p_vmspace;
202 addr = (vm_offset_t) uap->addr;
204 prot = uap->prot & VM_PROT_ALL;
211 * Enforce the constraints.
212 * Mapping of length 0 is only allowed for old binaries.
213 * Anonymous mapping shall specify -1 as filedescriptor and
214 * zero position for new code. Be nice to ancient a.out
215 * binaries and correct pos for anonymous mapping, since old
216 * ld.so sometimes issues anonymous map requests with non-zero
219 if (!SV_CURPROC_FLAG(SV_AOUT)) {
220 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
221 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
224 if ((flags & MAP_ANON) != 0)
228 if (flags & MAP_STACK) {
229 if ((uap->fd != -1) ||
230 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
237 * Align the file position to a page boundary,
238 * and save its page offset component.
240 pageoff = (pos & PAGE_MASK);
243 /* Adjust size for rounding (on both ends). */
244 size += pageoff; /* low end... */
245 size = (vm_size_t) round_page(size); /* hi end */
248 * Check for illegal addresses. Watch out for address wrap... Note
249 * that VM_*_ADDRESS are not constants due to casts (argh).
251 if (flags & MAP_FIXED) {
253 * The specified address must have the same remainder
254 * as the file offset taken modulo PAGE_SIZE, so it
255 * should be aligned after adjustment by pageoff.
258 if (addr & PAGE_MASK)
261 /* Address range must be all in user VM space. */
262 if (addr < vm_map_min(&vms->vm_map) ||
263 addr + size > vm_map_max(&vms->vm_map))
265 if (addr + size < addr)
269 * XXX for non-fixed mappings where no hint is provided or
270 * the hint would fall in the potential heap space,
271 * place it after the end of the largest possible heap.
273 * There should really be a pmap call to determine a reasonable
276 PROC_LOCK(td->td_proc);
278 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
279 addr < round_page((vm_offset_t)vms->vm_daddr +
280 lim_max(td->td_proc, RLIMIT_DATA))))
281 addr = round_page((vm_offset_t)vms->vm_daddr +
282 lim_max(td->td_proc, RLIMIT_DATA));
283 PROC_UNLOCK(td->td_proc);
285 if (flags & MAP_ANON) {
287 * Mapping blank space is trivial.
290 handle_type = OBJT_DEFAULT;
291 maxprot = VM_PROT_ALL;
292 cap_maxprot = VM_PROT_ALL;
295 * Mapping file, get fp for validation and don't let the
296 * descriptor disappear on us if we block. Check capability
297 * rights, but also return the maximum rights to be combined
298 * with maxprot later.
301 if (prot & PROT_READ)
303 if ((flags & MAP_SHARED) != 0) {
304 if (prot & PROT_WRITE)
307 if (prot & PROT_EXEC)
308 rights |= CAP_MAPEXEC;
309 if ((error = fget_mmap(td, uap->fd, rights, &cap_maxprot,
312 if (fp->f_type == DTYPE_SHM) {
314 handle_type = OBJT_SWAP;
315 maxprot = VM_PROT_NONE;
317 /* FREAD should always be set. */
318 if (fp->f_flag & FREAD)
319 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
320 if (fp->f_flag & FWRITE)
321 maxprot |= VM_PROT_WRITE;
324 if (fp->f_type != DTYPE_VNODE) {
328 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
329 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
331 * POSIX shared-memory objects are defined to have
332 * kernel persistence, and are not defined to support
333 * read(2)/write(2) -- or even open(2). Thus, we can
334 * use MAP_ASYNC to trade on-disk coherence for speed.
335 * The shm_open(3) library routine turns on the FPOSIXSHM
336 * flag to request this behavior.
338 if (fp->f_flag & FPOSIXSHM)
343 * Ensure that file and memory protections are
344 * compatible. Note that we only worry about
345 * writability if mapping is shared; in this case,
346 * current and max prot are dictated by the open file.
347 * XXX use the vnode instead? Problem is: what
348 * credentials do we use for determination? What if
349 * proc does a setuid?
351 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
352 maxprot = VM_PROT_NONE;
354 maxprot = VM_PROT_EXECUTE;
355 if (fp->f_flag & FREAD) {
356 maxprot |= VM_PROT_READ;
357 } else if (prot & PROT_READ) {
362 * If we are sharing potential changes (either via
363 * MAP_SHARED or via the implicit sharing of character
364 * device mappings), and we are trying to get write
365 * permission although we opened it without asking
368 if ((flags & MAP_SHARED) != 0) {
369 if ((fp->f_flag & FWRITE) != 0) {
370 maxprot |= VM_PROT_WRITE;
371 } else if ((prot & PROT_WRITE) != 0) {
375 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
376 maxprot |= VM_PROT_WRITE;
377 cap_maxprot |= VM_PROT_WRITE;
380 handle_type = OBJT_VNODE;
384 maxprot &= cap_maxprot;
385 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
386 flags, handle_type, handle, pos);
389 /* inform hwpmc(4) if an executable is being mapped */
390 if (error == 0 && handle_type == OBJT_VNODE &&
391 (prot & PROT_EXEC)) {
392 pkm.pm_file = handle;
393 pkm.pm_address = (uintptr_t) addr;
394 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
398 td->td_retval[0] = (register_t) (addr + pageoff);
407 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
409 struct mmap_args oargs;
411 oargs.addr = uap->addr;
412 oargs.len = uap->len;
413 oargs.prot = uap->prot;
414 oargs.flags = uap->flags;
416 oargs.pos = uap->pos;
417 return (sys_mmap(td, &oargs));
421 #ifndef _SYS_SYSPROTO_H_
434 struct ommap_args *uap;
436 struct mmap_args nargs;
437 static const char cvtbsdprot[8] = {
441 PROT_EXEC | PROT_WRITE,
443 PROT_EXEC | PROT_READ,
444 PROT_WRITE | PROT_READ,
445 PROT_EXEC | PROT_WRITE | PROT_READ,
448 #define OMAP_ANON 0x0002
449 #define OMAP_COPY 0x0020
450 #define OMAP_SHARED 0x0010
451 #define OMAP_FIXED 0x0100
453 nargs.addr = uap->addr;
454 nargs.len = uap->len;
455 nargs.prot = cvtbsdprot[uap->prot & 0x7];
456 #ifdef COMPAT_FREEBSD32
457 #if defined(__amd64__) || defined(__ia64__)
458 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
460 nargs.prot |= PROT_EXEC;
464 if (uap->flags & OMAP_ANON)
465 nargs.flags |= MAP_ANON;
466 if (uap->flags & OMAP_COPY)
467 nargs.flags |= MAP_COPY;
468 if (uap->flags & OMAP_SHARED)
469 nargs.flags |= MAP_SHARED;
471 nargs.flags |= MAP_PRIVATE;
472 if (uap->flags & OMAP_FIXED)
473 nargs.flags |= MAP_FIXED;
475 nargs.pos = uap->pos;
476 return (sys_mmap(td, &nargs));
478 #endif /* COMPAT_43 */
481 #ifndef _SYS_SYSPROTO_H_
494 struct msync_args *uap;
497 vm_size_t size, pageoff;
502 addr = (vm_offset_t) uap->addr;
506 pageoff = (addr & PAGE_MASK);
509 size = (vm_size_t) round_page(size);
510 if (addr + size < addr)
513 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
516 map = &td->td_proc->p_vmspace->vm_map;
519 * Clean the pages and interpret the return value.
521 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
522 (flags & MS_INVALIDATE) != 0);
526 case KERN_INVALID_ADDRESS:
527 return (EINVAL); /* Sun returns ENOMEM? */
528 case KERN_INVALID_ARGUMENT:
537 #ifndef _SYS_SYSPROTO_H_
549 struct munmap_args *uap;
552 struct pmckern_map_out pkm;
553 vm_map_entry_t entry;
556 vm_size_t size, pageoff;
559 addr = (vm_offset_t) uap->addr;
564 pageoff = (addr & PAGE_MASK);
567 size = (vm_size_t) round_page(size);
568 if (addr + size < addr)
572 * Check for illegal addresses. Watch out for address wrap...
574 map = &td->td_proc->p_vmspace->vm_map;
575 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
580 * Inform hwpmc if the address range being unmapped contains
581 * an executable region.
583 pkm.pm_address = (uintptr_t) NULL;
584 if (vm_map_lookup_entry(map, addr, &entry)) {
586 entry != &map->header && entry->start < addr + size;
587 entry = entry->next) {
588 if (vm_map_check_protection(map, entry->start,
589 entry->end, VM_PROT_EXECUTE) == TRUE) {
590 pkm.pm_address = (uintptr_t) addr;
591 pkm.pm_size = (size_t) size;
597 vm_map_delete(map, addr, addr + size);
600 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
601 vm_map_lock_downgrade(map);
602 if (pkm.pm_address != (uintptr_t) NULL)
603 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
604 vm_map_unlock_read(map);
608 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
612 #ifndef _SYS_SYSPROTO_H_
613 struct mprotect_args {
623 sys_mprotect(td, uap)
625 struct mprotect_args *uap;
628 vm_size_t size, pageoff;
631 addr = (vm_offset_t) uap->addr;
633 prot = uap->prot & VM_PROT_ALL;
635 pageoff = (addr & PAGE_MASK);
638 size = (vm_size_t) round_page(size);
639 if (addr + size < addr)
642 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
643 addr + size, prot, FALSE)) {
646 case KERN_PROTECTION_FAILURE:
648 case KERN_RESOURCE_SHORTAGE:
654 #ifndef _SYS_SYSPROTO_H_
655 struct minherit_args {
665 sys_minherit(td, uap)
667 struct minherit_args *uap;
670 vm_size_t size, pageoff;
671 vm_inherit_t inherit;
673 addr = (vm_offset_t)uap->addr;
675 inherit = uap->inherit;
677 pageoff = (addr & PAGE_MASK);
680 size = (vm_size_t) round_page(size);
681 if (addr + size < addr)
684 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
685 addr + size, inherit)) {
688 case KERN_PROTECTION_FAILURE:
694 #ifndef _SYS_SYSPROTO_H_
695 struct madvise_args {
708 struct madvise_args *uap;
710 vm_offset_t start, end;
716 * Check for our special case, advising the swap pager we are
719 if (uap->behav == MADV_PROTECT) {
720 error = priv_check(td, PRIV_VM_MADV_PROTECT);
724 p->p_flag |= P_PROTECTED;
730 * Check for illegal behavior
732 if (uap->behav < 0 || uap->behav > MADV_CORE)
735 * Check for illegal addresses. Watch out for address wrap... Note
736 * that VM_*_ADDRESS are not constants due to casts (argh).
738 map = &td->td_proc->p_vmspace->vm_map;
739 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
740 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
742 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
746 * Since this routine is only advisory, we default to conservative
749 start = trunc_page((vm_offset_t) uap->addr);
750 end = round_page((vm_offset_t) uap->addr + uap->len);
752 if (vm_map_madvise(map, start, end, uap->behav))
757 #ifndef _SYS_SYSPROTO_H_
758 struct mincore_args {
771 struct mincore_args *uap;
773 vm_offset_t addr, first_addr;
774 vm_offset_t end, cend;
779 int vecindex, lastvecindex;
780 vm_map_entry_t current;
781 vm_map_entry_t entry;
783 vm_paddr_t locked_pa;
787 unsigned int timestamp;
791 * Make sure that the addresses presented are valid for user
794 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
795 end = addr + (vm_size_t)round_page(uap->len);
796 map = &td->td_proc->p_vmspace->vm_map;
797 if (end > vm_map_max(map) || end < addr)
801 * Address of byte vector
805 pmap = vmspace_pmap(td->td_proc->p_vmspace);
807 vm_map_lock_read(map);
809 timestamp = map->timestamp;
811 if (!vm_map_lookup_entry(map, addr, &entry)) {
812 vm_map_unlock_read(map);
817 * Do this on a map entry basis so that if the pages are not
818 * in the current processes address space, we can easily look
819 * up the pages elsewhere.
822 for (current = entry;
823 (current != &map->header) && (current->start < end);
824 current = current->next) {
827 * check for contiguity
829 if (current->end < end &&
830 (entry->next == &map->header ||
831 current->next->start > current->end)) {
832 vm_map_unlock_read(map);
837 * ignore submaps (for now) or null objects
839 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
840 current->object.vm_object == NULL)
844 * limit this scan to the current map entry and the
845 * limits for the mincore call
847 if (addr < current->start)
848 addr = current->start;
854 * scan this entry one page at a time
856 while (addr < cend) {
858 * Check pmap first, it is likely faster, also
859 * it can provide info as to whether we are the
860 * one referencing or modifying the page.
866 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
867 if (locked_pa != 0) {
869 * The page is mapped by this process but not
870 * both accessed and modified. It is also
871 * managed. Acquire the object lock so that
872 * other mappings might be examined.
874 m = PHYS_TO_VM_PAGE(locked_pa);
875 if (m->object != object) {
877 VM_OBJECT_UNLOCK(object);
879 locked = VM_OBJECT_TRYLOCK(object);
882 VM_OBJECT_LOCK(object);
888 KASSERT(m->valid == VM_PAGE_BITS_ALL,
889 ("mincore: page %p is mapped but invalid",
891 } else if (mincoreinfo == 0) {
893 * The page is not mapped by this process. If
894 * the object implements managed pages, then
895 * determine if the page is resident so that
896 * the mappings might be examined.
898 if (current->object.vm_object != object) {
900 VM_OBJECT_UNLOCK(object);
901 object = current->object.vm_object;
902 VM_OBJECT_LOCK(object);
904 if (object->type == OBJT_DEFAULT ||
905 object->type == OBJT_SWAP ||
906 object->type == OBJT_VNODE) {
907 pindex = OFF_TO_IDX(current->offset +
908 (addr - current->start));
909 m = vm_page_lookup(object, pindex);
911 vm_page_is_cached(object, pindex))
912 mincoreinfo = MINCORE_INCORE;
913 if (m != NULL && m->valid == 0)
916 mincoreinfo = MINCORE_INCORE;
920 /* Examine other mappings to the page. */
921 if (m->dirty == 0 && pmap_is_modified(m))
924 mincoreinfo |= MINCORE_MODIFIED_OTHER;
926 * The first test for PGA_REFERENCED is an
927 * optimization. The second test is
928 * required because a concurrent pmap
929 * operation could clear the last reference
930 * and set PGA_REFERENCED before the call to
931 * pmap_is_referenced().
933 if ((m->aflags & PGA_REFERENCED) != 0 ||
934 pmap_is_referenced(m) ||
935 (m->aflags & PGA_REFERENCED) != 0)
936 mincoreinfo |= MINCORE_REFERENCED_OTHER;
939 VM_OBJECT_UNLOCK(object);
942 * subyte may page fault. In case it needs to modify
943 * the map, we release the lock.
945 vm_map_unlock_read(map);
948 * calculate index into user supplied byte vector
950 vecindex = OFF_TO_IDX(addr - first_addr);
953 * If we have skipped map entries, we need to make sure that
954 * the byte vector is zeroed for those skipped entries.
956 while ((lastvecindex + 1) < vecindex) {
957 error = subyte(vec + lastvecindex, 0);
966 * Pass the page information to the user
968 error = subyte(vec + vecindex, mincoreinfo);
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)
982 lastvecindex = vecindex;
988 * subyte may page fault. In case it needs to modify
989 * the map, we release the lock.
991 vm_map_unlock_read(map);
994 * Zero the last entries in the byte vector.
996 vecindex = OFF_TO_IDX(end - first_addr);
997 while ((lastvecindex + 1) < vecindex) {
998 error = subyte(vec + lastvecindex, 0);
1007 * If the map has changed, due to the subyte, the previous
1008 * output may be invalid.
1010 vm_map_lock_read(map);
1011 if (timestamp != map->timestamp)
1013 vm_map_unlock_read(map);
1018 #ifndef _SYS_SYSPROTO_H_
1030 struct mlock_args *uap;
1033 vm_offset_t addr, end, last, start;
1034 vm_size_t npages, size;
1035 unsigned long nsize;
1038 error = priv_check(td, PRIV_VM_MLOCK);
1041 addr = (vm_offset_t)uap->addr;
1044 start = trunc_page(addr);
1045 end = round_page(last);
1046 if (last < addr || end < addr)
1048 npages = atop(end - start);
1049 if (npages > vm_page_max_wired)
1053 nsize = ptoa(npages + vmspace_wired_count(proc->p_vmspace));
1054 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1059 if (npages + cnt.v_wire_count > vm_page_max_wired)
1063 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1068 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
1069 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1071 if (error != KERN_SUCCESS) {
1073 racct_set(proc, RACCT_MEMLOCK,
1074 ptoa(vmspace_wired_count(proc->p_vmspace)));
1078 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1081 #ifndef _SYS_SYSPROTO_H_
1082 struct mlockall_args {
1091 sys_mlockall(td, uap)
1093 struct mlockall_args *uap;
1098 map = &td->td_proc->p_vmspace->vm_map;
1101 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1106 * If wiring all pages in the process would cause it to exceed
1107 * a hard resource limit, return ENOMEM.
1109 PROC_LOCK(td->td_proc);
1110 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1111 PROC_UNLOCK(td->td_proc);
1114 PROC_UNLOCK(td->td_proc);
1116 error = priv_check(td, PRIV_VM_MLOCK);
1121 PROC_LOCK(td->td_proc);
1122 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1123 PROC_UNLOCK(td->td_proc);
1128 if (uap->how & MCL_FUTURE) {
1130 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1135 if (uap->how & MCL_CURRENT) {
1137 * P1003.1-2001 mandates that all currently mapped pages
1138 * will be memory resident and locked (wired) upon return
1139 * from mlockall(). vm_map_wire() will wire pages, by
1140 * calling vm_fault_wire() for each page in the region.
1142 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1143 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1144 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1147 if (error != KERN_SUCCESS) {
1148 PROC_LOCK(td->td_proc);
1149 racct_set(td->td_proc, RACCT_MEMLOCK,
1150 ptoa(vmspace_wired_count(td->td_proc->p_vmspace)));
1151 PROC_UNLOCK(td->td_proc);
1158 #ifndef _SYS_SYSPROTO_H_
1159 struct munlockall_args {
1168 sys_munlockall(td, uap)
1170 struct munlockall_args *uap;
1175 map = &td->td_proc->p_vmspace->vm_map;
1176 error = priv_check(td, PRIV_VM_MUNLOCK);
1180 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1182 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1185 /* Forcibly unwire all pages. */
1186 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1187 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1189 if (error == KERN_SUCCESS) {
1190 PROC_LOCK(td->td_proc);
1191 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1192 PROC_UNLOCK(td->td_proc);
1199 #ifndef _SYS_SYSPROTO_H_
1200 struct munlock_args {
1209 sys_munlock(td, uap)
1211 struct munlock_args *uap;
1213 vm_offset_t addr, end, last, start;
1217 error = priv_check(td, PRIV_VM_MUNLOCK);
1220 addr = (vm_offset_t)uap->addr;
1223 start = trunc_page(addr);
1224 end = round_page(last);
1225 if (last < addr || end < addr)
1227 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1228 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1230 if (error == KERN_SUCCESS) {
1231 PROC_LOCK(td->td_proc);
1232 racct_sub(td->td_proc, RACCT_MEMLOCK, ptoa(end - start));
1233 PROC_UNLOCK(td->td_proc);
1236 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1242 * Helper function for vm_mmap. Perform sanity check specific for mmap
1243 * operations on vnodes.
1245 * For VCHR vnodes, the vnode lock is held over the call to
1246 * vm_mmap_cdev() to keep vp->v_rdev valid.
1249 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1250 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1251 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1252 boolean_t *writecounted)
1259 int error, flags, locktype;
1262 cred = td->td_ucred;
1263 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1264 locktype = LK_EXCLUSIVE;
1266 locktype = LK_SHARED;
1267 if ((error = vget(vp, locktype, td)) != 0)
1272 if (vp->v_type == VREG) {
1274 * Get the proper underlying object
1280 if (obj->handle != vp) {
1282 vp = (struct vnode *)obj->handle;
1284 * Bypass filesystems obey the mpsafety of the
1287 error = vget(vp, locktype, td);
1291 if (locktype == LK_EXCLUSIVE) {
1292 *writecounted = TRUE;
1293 vnode_pager_update_writecount(obj, 0, objsize);
1295 } else if (vp->v_type == VCHR) {
1296 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1297 vp->v_rdev, foffp, objp);
1305 if ((error = VOP_GETATTR(vp, &va, cred)))
1308 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1312 if ((flags & MAP_SHARED) != 0) {
1313 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1314 if (prot & PROT_WRITE) {
1318 *maxprotp &= ~VM_PROT_WRITE;
1322 * If it is a regular file without any references
1323 * we do not need to sync it.
1324 * Adjust object size to be the size of actual file.
1326 objsize = round_page(va.va_size);
1327 if (va.va_nlink == 0)
1328 flags |= MAP_NOSYNC;
1329 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, cred);
1338 vfs_mark_atime(vp, cred);
1350 * Helper function for vm_mmap. Perform sanity check specific for mmap
1351 * operations on cdevs.
1354 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1355 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1356 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1360 int error, flags, ref;
1364 dsw = dev_refthread(cdev, &ref);
1367 if (dsw->d_flags & D_MMAP_ANON) {
1368 dev_relthread(cdev, ref);
1369 *maxprotp = VM_PROT_ALL;
1370 *flagsp |= MAP_ANON;
1374 * cdevs do not provide private mappings of any kind.
1376 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1377 (prot & PROT_WRITE) != 0) {
1378 dev_relthread(cdev, ref);
1381 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1382 dev_relthread(cdev, ref);
1386 * Force device mappings to be shared.
1388 flags |= MAP_SHARED;
1390 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1392 dev_relthread(cdev, ref);
1397 * First, try d_mmap_single(). If that is not implemented
1398 * (returns ENODEV), fall back to using the device pager.
1399 * Note that d_mmap_single() must return a reference to the
1400 * object (it needs to bump the reference count of the object
1401 * it returns somehow).
1403 * XXX assumes VM_PROT_* == PROT_*
1405 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1406 dev_relthread(cdev, ref);
1407 if (error != ENODEV)
1409 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1423 * Helper function for vm_mmap. Perform sanity check specific for mmap
1424 * operations on shm file descriptors.
1427 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1428 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1429 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1433 if ((*flagsp & MAP_SHARED) != 0 &&
1434 (*maxprotp & VM_PROT_WRITE) == 0 &&
1435 (prot & PROT_WRITE) != 0)
1438 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1442 error = shm_mmap(shmfd, objsize, foff, objp);
1453 * Internal version of mmap. Currently used by mmap, exec, and sys5
1454 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1457 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1458 vm_prot_t maxprot, int flags,
1459 objtype_t handle_type, void *handle,
1463 vm_object_t object = NULL;
1464 struct thread *td = curthread;
1465 int docow, error, rv;
1466 boolean_t writecounted;
1471 size = round_page(size);
1473 if (map == &td->td_proc->p_vmspace->vm_map) {
1474 PROC_LOCK(td->td_proc);
1475 if (map->size + size > lim_cur(td->td_proc, RLIMIT_VMEM)) {
1476 PROC_UNLOCK(td->td_proc);
1479 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1480 PROC_UNLOCK(td->td_proc);
1483 PROC_UNLOCK(td->td_proc);
1487 * We currently can only deal with page aligned file offsets.
1488 * The check is here rather than in the syscall because the
1489 * kernel calls this function internally for other mmaping
1490 * operations (such as in exec) and non-aligned offsets will
1491 * cause pmap inconsistencies...so we want to be sure to
1492 * disallow this in all cases.
1494 if (foff & PAGE_MASK)
1497 if ((flags & MAP_FIXED) == 0) {
1499 *addr = round_page(*addr);
1501 if (*addr != trunc_page(*addr))
1505 writecounted = FALSE;
1508 * Lookup/allocate object.
1510 switch (handle_type) {
1512 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1513 handle, &foff, &object);
1516 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1517 handle, &foff, &object, &writecounted);
1520 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1521 handle, foff, &object);
1524 if (handle == NULL) {
1535 if (flags & MAP_ANON) {
1539 * Unnamed anonymous regions always start at 0.
1543 } else if (flags & MAP_PREFAULT_READ)
1544 docow = MAP_PREFAULT;
1546 docow = MAP_PREFAULT_PARTIAL;
1548 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1549 docow |= MAP_COPY_ON_WRITE;
1550 if (flags & MAP_NOSYNC)
1551 docow |= MAP_DISABLE_SYNCER;
1552 if (flags & MAP_NOCORE)
1553 docow |= MAP_DISABLE_COREDUMP;
1554 /* Shared memory is also shared with children. */
1555 if (flags & MAP_SHARED)
1556 docow |= MAP_INHERIT_SHARE;
1558 docow |= MAP_VN_WRITECOUNT;
1560 if (flags & MAP_STACK)
1561 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1562 docow | MAP_STACK_GROWS_DOWN);
1564 rv = vm_map_find(map, object, foff, addr, size,
1565 object != NULL && object->type == OBJT_DEVICE ?
1566 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1568 rv = vm_map_fixed(map, object, foff, *addr, size,
1569 prot, maxprot, docow);
1571 if (rv == KERN_SUCCESS) {
1573 * If the process has requested that all future mappings
1574 * be wired, then heed this.
1576 if (map->flags & MAP_WIREFUTURE) {
1577 vm_map_wire(map, *addr, *addr + size,
1578 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1579 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1583 * If this mapping was accounted for in the vnode's
1584 * writecount, then undo that now.
1587 vnode_pager_release_writecount(object, 0, size);
1589 * Lose the object reference. Will destroy the
1590 * object if it's an unnamed anonymous mapping
1591 * or named anonymous without other references.
1593 vm_object_deallocate(object);
1595 return (vm_mmap_to_errno(rv));
1599 * Translate a Mach VM return code to zero on success or the appropriate errno
1603 vm_mmap_to_errno(int rv)
1609 case KERN_INVALID_ADDRESS:
1612 case KERN_PROTECTION_FAILURE: