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/sysctl.h>
63 #include <sys/vnode.h>
64 #include <sys/fcntl.h>
67 #include <sys/mount.h>
70 #include <sys/sysent.h>
71 #include <sys/vmmeter.h>
73 #include <security/mac/mac_framework.h>
76 #include <vm/vm_param.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vm_pageout.h>
83 #include <vm/vm_extern.h>
84 #include <vm/vm_page.h>
85 #include <vm/vnode_pager.h>
88 #include <sys/pmckern.h>
92 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RW | CTLFLAG_TUN, &old_mlock, 0,
93 "Do not apply RLIMIT_MEMLOCK on mlockall");
94 TUNABLE_INT("vm.old_mlock", &old_mlock);
96 #ifndef _SYS_SYSPROTO_H_
102 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
103 int *, struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
104 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
105 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
106 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
107 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
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 {
150 ogetpagesize(td, uap)
152 struct getpagesize_args *uap;
155 td->td_retval[0] = PAGE_SIZE;
158 #endif /* COMPAT_43 */
162 * Memory Map (mmap) system call. Note that the file offset
163 * and address are allowed to be NOT page aligned, though if
164 * the MAP_FIXED flag it set, both must have the same remainder
165 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
166 * page-aligned, the actual mapping starts at trunc_page(addr)
167 * and the return value is adjusted up by the page offset.
169 * Generally speaking, only character devices which are themselves
170 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
171 * there would be no cache coherency between a descriptor and a VM mapping
172 * both to the same character device.
174 #ifndef _SYS_SYSPROTO_H_
192 struct mmap_args *uap;
195 struct pmckern_map_in pkm;
200 vm_size_t size, pageoff;
201 vm_prot_t cap_maxprot, prot, maxprot;
203 objtype_t handle_type;
206 struct vmspace *vms = td->td_proc->p_vmspace;
209 addr = (vm_offset_t) uap->addr;
211 prot = uap->prot & VM_PROT_ALL;
218 * Enforce the constraints.
219 * Mapping of length 0 is only allowed for old binaries.
220 * Anonymous mapping shall specify -1 as filedescriptor and
221 * zero position for new code. Be nice to ancient a.out
222 * binaries and correct pos for anonymous mapping, since old
223 * ld.so sometimes issues anonymous map requests with non-zero
226 if (!SV_CURPROC_FLAG(SV_AOUT)) {
227 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
228 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
231 if ((flags & MAP_ANON) != 0)
235 if (flags & MAP_STACK) {
236 if ((uap->fd != -1) ||
237 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
244 * Align the file position to a page boundary,
245 * and save its page offset component.
247 pageoff = (pos & PAGE_MASK);
250 /* Adjust size for rounding (on both ends). */
251 size += pageoff; /* low end... */
252 size = (vm_size_t) round_page(size); /* hi end */
255 * Check for illegal addresses. Watch out for address wrap... Note
256 * that VM_*_ADDRESS are not constants due to casts (argh).
258 if (flags & MAP_FIXED) {
260 * The specified address must have the same remainder
261 * as the file offset taken modulo PAGE_SIZE, so it
262 * should be aligned after adjustment by pageoff.
265 if (addr & PAGE_MASK)
268 /* Address range must be all in user VM space. */
269 if (addr < vm_map_min(&vms->vm_map) ||
270 addr + size > vm_map_max(&vms->vm_map))
272 if (addr + size < addr)
276 * XXX for non-fixed mappings where no hint is provided or
277 * the hint would fall in the potential heap space,
278 * place it after the end of the largest possible heap.
280 * There should really be a pmap call to determine a reasonable
283 PROC_LOCK(td->td_proc);
285 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
286 addr < round_page((vm_offset_t)vms->vm_daddr +
287 lim_max(td->td_proc, RLIMIT_DATA))))
288 addr = round_page((vm_offset_t)vms->vm_daddr +
289 lim_max(td->td_proc, RLIMIT_DATA));
290 PROC_UNLOCK(td->td_proc);
292 if (flags & MAP_ANON) {
294 * Mapping blank space is trivial.
297 handle_type = OBJT_DEFAULT;
298 maxprot = VM_PROT_ALL;
299 cap_maxprot = VM_PROT_ALL;
302 * Mapping file, get fp for validation and don't let the
303 * descriptor disappear on us if we block. Check capability
304 * rights, but also return the maximum rights to be combined
305 * with maxprot later.
308 if (prot & PROT_READ)
310 if ((flags & MAP_SHARED) != 0) {
311 if (prot & PROT_WRITE)
314 if (prot & PROT_EXEC)
315 rights |= CAP_MAPEXEC;
316 if ((error = fget_mmap(td, uap->fd, rights, &cap_maxprot,
319 if (fp->f_type == DTYPE_SHM) {
321 handle_type = OBJT_SWAP;
322 maxprot = VM_PROT_NONE;
324 /* FREAD should always be set. */
325 if (fp->f_flag & FREAD)
326 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
327 if (fp->f_flag & FWRITE)
328 maxprot |= VM_PROT_WRITE;
331 if (fp->f_type != DTYPE_VNODE) {
335 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
336 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
338 * POSIX shared-memory objects are defined to have
339 * kernel persistence, and are not defined to support
340 * read(2)/write(2) -- or even open(2). Thus, we can
341 * use MAP_ASYNC to trade on-disk coherence for speed.
342 * The shm_open(3) library routine turns on the FPOSIXSHM
343 * flag to request this behavior.
345 if (fp->f_flag & FPOSIXSHM)
350 * Ensure that file and memory protections are
351 * compatible. Note that we only worry about
352 * writability if mapping is shared; in this case,
353 * current and max prot are dictated by the open file.
354 * XXX use the vnode instead? Problem is: what
355 * credentials do we use for determination? What if
356 * proc does a setuid?
358 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
359 maxprot = VM_PROT_NONE;
361 maxprot = VM_PROT_EXECUTE;
362 if (fp->f_flag & FREAD) {
363 maxprot |= VM_PROT_READ;
364 } else if (prot & PROT_READ) {
369 * If we are sharing potential changes (either via
370 * MAP_SHARED or via the implicit sharing of character
371 * device mappings), and we are trying to get write
372 * permission although we opened it without asking
375 if ((flags & MAP_SHARED) != 0) {
376 if ((fp->f_flag & FWRITE) != 0) {
377 maxprot |= VM_PROT_WRITE;
378 } else if ((prot & PROT_WRITE) != 0) {
382 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
383 maxprot |= VM_PROT_WRITE;
384 cap_maxprot |= VM_PROT_WRITE;
387 handle_type = OBJT_VNODE;
391 maxprot &= cap_maxprot;
392 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
393 flags, handle_type, handle, pos);
396 /* inform hwpmc(4) if an executable is being mapped */
397 if (error == 0 && handle_type == OBJT_VNODE &&
398 (prot & PROT_EXEC)) {
399 pkm.pm_file = handle;
400 pkm.pm_address = (uintptr_t) addr;
401 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
405 td->td_retval[0] = (register_t) (addr + pageoff);
414 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
416 struct mmap_args oargs;
418 oargs.addr = uap->addr;
419 oargs.len = uap->len;
420 oargs.prot = uap->prot;
421 oargs.flags = uap->flags;
423 oargs.pos = uap->pos;
424 return (sys_mmap(td, &oargs));
428 #ifndef _SYS_SYSPROTO_H_
441 struct ommap_args *uap;
443 struct mmap_args nargs;
444 static const char cvtbsdprot[8] = {
448 PROT_EXEC | PROT_WRITE,
450 PROT_EXEC | PROT_READ,
451 PROT_WRITE | PROT_READ,
452 PROT_EXEC | PROT_WRITE | PROT_READ,
455 #define OMAP_ANON 0x0002
456 #define OMAP_COPY 0x0020
457 #define OMAP_SHARED 0x0010
458 #define OMAP_FIXED 0x0100
460 nargs.addr = uap->addr;
461 nargs.len = uap->len;
462 nargs.prot = cvtbsdprot[uap->prot & 0x7];
463 #ifdef COMPAT_FREEBSD32
464 #if defined(__amd64__) || defined(__ia64__)
465 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
467 nargs.prot |= PROT_EXEC;
471 if (uap->flags & OMAP_ANON)
472 nargs.flags |= MAP_ANON;
473 if (uap->flags & OMAP_COPY)
474 nargs.flags |= MAP_COPY;
475 if (uap->flags & OMAP_SHARED)
476 nargs.flags |= MAP_SHARED;
478 nargs.flags |= MAP_PRIVATE;
479 if (uap->flags & OMAP_FIXED)
480 nargs.flags |= MAP_FIXED;
482 nargs.pos = uap->pos;
483 return (sys_mmap(td, &nargs));
485 #endif /* COMPAT_43 */
488 #ifndef _SYS_SYSPROTO_H_
501 struct msync_args *uap;
504 vm_size_t size, pageoff;
509 addr = (vm_offset_t) uap->addr;
513 pageoff = (addr & PAGE_MASK);
516 size = (vm_size_t) round_page(size);
517 if (addr + size < addr)
520 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
523 map = &td->td_proc->p_vmspace->vm_map;
526 * Clean the pages and interpret the return value.
528 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
529 (flags & MS_INVALIDATE) != 0);
533 case KERN_INVALID_ADDRESS:
534 return (EINVAL); /* Sun returns ENOMEM? */
535 case KERN_INVALID_ARGUMENT:
544 #ifndef _SYS_SYSPROTO_H_
556 struct munmap_args *uap;
559 struct pmckern_map_out pkm;
560 vm_map_entry_t entry;
563 vm_size_t size, pageoff;
566 addr = (vm_offset_t) uap->addr;
571 pageoff = (addr & PAGE_MASK);
574 size = (vm_size_t) round_page(size);
575 if (addr + size < addr)
579 * Check for illegal addresses. Watch out for address wrap...
581 map = &td->td_proc->p_vmspace->vm_map;
582 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
587 * Inform hwpmc if the address range being unmapped contains
588 * an executable region.
590 pkm.pm_address = (uintptr_t) NULL;
591 if (vm_map_lookup_entry(map, addr, &entry)) {
593 entry != &map->header && entry->start < addr + size;
594 entry = entry->next) {
595 if (vm_map_check_protection(map, entry->start,
596 entry->end, VM_PROT_EXECUTE) == TRUE) {
597 pkm.pm_address = (uintptr_t) addr;
598 pkm.pm_size = (size_t) size;
604 vm_map_delete(map, addr, addr + size);
607 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
608 vm_map_lock_downgrade(map);
609 if (pkm.pm_address != (uintptr_t) NULL)
610 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
611 vm_map_unlock_read(map);
615 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
619 #ifndef _SYS_SYSPROTO_H_
620 struct mprotect_args {
630 sys_mprotect(td, uap)
632 struct mprotect_args *uap;
635 vm_size_t size, pageoff;
638 addr = (vm_offset_t) uap->addr;
640 prot = uap->prot & VM_PROT_ALL;
642 pageoff = (addr & PAGE_MASK);
645 size = (vm_size_t) round_page(size);
646 if (addr + size < addr)
649 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
650 addr + size, prot, FALSE)) {
653 case KERN_PROTECTION_FAILURE:
655 case KERN_RESOURCE_SHORTAGE:
661 #ifndef _SYS_SYSPROTO_H_
662 struct minherit_args {
672 sys_minherit(td, uap)
674 struct minherit_args *uap;
677 vm_size_t size, pageoff;
678 vm_inherit_t inherit;
680 addr = (vm_offset_t)uap->addr;
682 inherit = uap->inherit;
684 pageoff = (addr & PAGE_MASK);
687 size = (vm_size_t) round_page(size);
688 if (addr + size < addr)
691 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
692 addr + size, inherit)) {
695 case KERN_PROTECTION_FAILURE:
701 #ifndef _SYS_SYSPROTO_H_
702 struct madvise_args {
716 struct madvise_args *uap;
718 vm_offset_t start, end;
724 * Check for our special case, advising the swap pager we are
727 if (uap->behav == MADV_PROTECT) {
728 error = priv_check(td, PRIV_VM_MADV_PROTECT);
732 p->p_flag |= P_PROTECTED;
738 * Check for illegal behavior
740 if (uap->behav < 0 || uap->behav > MADV_CORE)
743 * Check for illegal addresses. Watch out for address wrap... Note
744 * that VM_*_ADDRESS are not constants due to casts (argh).
746 map = &td->td_proc->p_vmspace->vm_map;
747 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
748 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
750 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
754 * Since this routine is only advisory, we default to conservative
757 start = trunc_page((vm_offset_t) uap->addr);
758 end = round_page((vm_offset_t) uap->addr + uap->len);
760 if (vm_map_madvise(map, start, end, uap->behav))
765 #ifndef _SYS_SYSPROTO_H_
766 struct mincore_args {
780 struct mincore_args *uap;
782 vm_offset_t addr, first_addr;
783 vm_offset_t end, cend;
788 int vecindex, lastvecindex;
789 vm_map_entry_t current;
790 vm_map_entry_t entry;
792 vm_paddr_t locked_pa;
796 unsigned int timestamp;
800 * Make sure that the addresses presented are valid for user
803 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
804 end = addr + (vm_size_t)round_page(uap->len);
805 map = &td->td_proc->p_vmspace->vm_map;
806 if (end > vm_map_max(map) || end < addr)
810 * Address of byte vector
814 pmap = vmspace_pmap(td->td_proc->p_vmspace);
816 vm_map_lock_read(map);
818 timestamp = map->timestamp;
820 if (!vm_map_lookup_entry(map, addr, &entry)) {
821 vm_map_unlock_read(map);
826 * Do this on a map entry basis so that if the pages are not
827 * in the current processes address space, we can easily look
828 * up the pages elsewhere.
831 for (current = entry;
832 (current != &map->header) && (current->start < end);
833 current = current->next) {
836 * check for contiguity
838 if (current->end < end &&
839 (entry->next == &map->header ||
840 current->next->start > current->end)) {
841 vm_map_unlock_read(map);
846 * ignore submaps (for now) or null objects
848 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
849 current->object.vm_object == NULL)
853 * limit this scan to the current map entry and the
854 * limits for the mincore call
856 if (addr < current->start)
857 addr = current->start;
863 * scan this entry one page at a time
865 while (addr < cend) {
867 * Check pmap first, it is likely faster, also
868 * it can provide info as to whether we are the
869 * one referencing or modifying the page.
875 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
876 if (locked_pa != 0) {
878 * The page is mapped by this process but not
879 * both accessed and modified. It is also
880 * managed. Acquire the object lock so that
881 * other mappings might be examined.
883 m = PHYS_TO_VM_PAGE(locked_pa);
884 if (m->object != object) {
886 VM_OBJECT_UNLOCK(object);
888 locked = VM_OBJECT_TRYLOCK(object);
891 VM_OBJECT_LOCK(object);
897 KASSERT(m->valid == VM_PAGE_BITS_ALL,
898 ("mincore: page %p is mapped but invalid",
900 } else if (mincoreinfo == 0) {
902 * The page is not mapped by this process. If
903 * the object implements managed pages, then
904 * determine if the page is resident so that
905 * the mappings might be examined.
907 if (current->object.vm_object != object) {
909 VM_OBJECT_UNLOCK(object);
910 object = current->object.vm_object;
911 VM_OBJECT_LOCK(object);
913 if (object->type == OBJT_DEFAULT ||
914 object->type == OBJT_SWAP ||
915 object->type == OBJT_VNODE) {
916 pindex = OFF_TO_IDX(current->offset +
917 (addr - current->start));
918 m = vm_page_lookup(object, pindex);
920 vm_page_is_cached(object, pindex))
921 mincoreinfo = MINCORE_INCORE;
922 if (m != NULL && m->valid == 0)
925 mincoreinfo = MINCORE_INCORE;
929 /* Examine other mappings to the page. */
930 if (m->dirty == 0 && pmap_is_modified(m))
933 mincoreinfo |= MINCORE_MODIFIED_OTHER;
935 * The first test for PGA_REFERENCED is an
936 * optimization. The second test is
937 * required because a concurrent pmap
938 * operation could clear the last reference
939 * and set PGA_REFERENCED before the call to
940 * pmap_is_referenced().
942 if ((m->aflags & PGA_REFERENCED) != 0 ||
943 pmap_is_referenced(m) ||
944 (m->aflags & PGA_REFERENCED) != 0)
945 mincoreinfo |= MINCORE_REFERENCED_OTHER;
948 VM_OBJECT_UNLOCK(object);
951 * subyte may page fault. In case it needs to modify
952 * the map, we release the lock.
954 vm_map_unlock_read(map);
957 * calculate index into user supplied byte vector
959 vecindex = OFF_TO_IDX(addr - first_addr);
962 * If we have skipped map entries, we need to make sure that
963 * the byte vector is zeroed for those skipped entries.
965 while ((lastvecindex + 1) < vecindex) {
966 error = subyte(vec + lastvecindex, 0);
975 * Pass the page information to the user
977 error = subyte(vec + vecindex, mincoreinfo);
984 * If the map has changed, due to the subyte, the previous
985 * output may be invalid.
987 vm_map_lock_read(map);
988 if (timestamp != map->timestamp)
991 lastvecindex = vecindex;
997 * subyte may page fault. In case it needs to modify
998 * the map, we release the lock.
1000 vm_map_unlock_read(map);
1003 * Zero the last entries in the byte vector.
1005 vecindex = OFF_TO_IDX(end - first_addr);
1006 while ((lastvecindex + 1) < vecindex) {
1007 error = subyte(vec + lastvecindex, 0);
1016 * If the map has changed, due to the subyte, the previous
1017 * output may be invalid.
1019 vm_map_lock_read(map);
1020 if (timestamp != map->timestamp)
1022 vm_map_unlock_read(map);
1027 #ifndef _SYS_SYSPROTO_H_
1039 struct mlock_args *uap;
1042 vm_offset_t addr, end, last, start;
1043 vm_size_t npages, size;
1045 unsigned long nsize;
1048 error = priv_check(td, PRIV_VM_MLOCK);
1051 addr = (vm_offset_t)uap->addr;
1054 start = trunc_page(addr);
1055 end = round_page(last);
1056 if (last < addr || end < addr)
1058 npages = atop(end - start);
1059 if (npages > vm_page_max_wired)
1062 map = &proc->p_vmspace->vm_map;
1064 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1065 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1070 if (npages + cnt.v_wire_count > vm_page_max_wired)
1074 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1079 error = vm_map_wire(map, start, end,
1080 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1082 if (error != KERN_SUCCESS) {
1084 racct_set(proc, RACCT_MEMLOCK,
1085 ptoa(pmap_wired_count(map->pmap)));
1089 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1092 #ifndef _SYS_SYSPROTO_H_
1093 struct mlockall_args {
1102 sys_mlockall(td, uap)
1104 struct mlockall_args *uap;
1109 map = &td->td_proc->p_vmspace->vm_map;
1110 error = priv_check(td, PRIV_VM_MLOCK);
1114 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1118 * If wiring all pages in the process would cause it to exceed
1119 * a hard resource limit, return ENOMEM.
1121 if (!old_mlock && uap->how & MCL_CURRENT) {
1122 PROC_LOCK(td->td_proc);
1123 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1124 PROC_UNLOCK(td->td_proc);
1127 PROC_UNLOCK(td->td_proc);
1130 PROC_LOCK(td->td_proc);
1131 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1132 PROC_UNLOCK(td->td_proc);
1137 if (uap->how & MCL_FUTURE) {
1139 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1144 if (uap->how & MCL_CURRENT) {
1146 * P1003.1-2001 mandates that all currently mapped pages
1147 * will be memory resident and locked (wired) upon return
1148 * from mlockall(). vm_map_wire() will wire pages, by
1149 * calling vm_fault_wire() for each page in the region.
1151 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1152 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1153 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1156 if (error != KERN_SUCCESS) {
1157 PROC_LOCK(td->td_proc);
1158 racct_set(td->td_proc, RACCT_MEMLOCK,
1159 ptoa(pmap_wired_count(map->pmap)));
1160 PROC_UNLOCK(td->td_proc);
1167 #ifndef _SYS_SYSPROTO_H_
1168 struct munlockall_args {
1177 sys_munlockall(td, uap)
1179 struct munlockall_args *uap;
1184 map = &td->td_proc->p_vmspace->vm_map;
1185 error = priv_check(td, PRIV_VM_MUNLOCK);
1189 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1191 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1194 /* Forcibly unwire all pages. */
1195 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1196 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1198 if (error == KERN_SUCCESS) {
1199 PROC_LOCK(td->td_proc);
1200 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1201 PROC_UNLOCK(td->td_proc);
1208 #ifndef _SYS_SYSPROTO_H_
1209 struct munlock_args {
1218 sys_munlock(td, uap)
1220 struct munlock_args *uap;
1222 vm_offset_t addr, end, last, start;
1229 error = priv_check(td, PRIV_VM_MUNLOCK);
1232 addr = (vm_offset_t)uap->addr;
1235 start = trunc_page(addr);
1236 end = round_page(last);
1237 if (last < addr || end < addr)
1239 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1240 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1242 if (error == KERN_SUCCESS) {
1243 PROC_LOCK(td->td_proc);
1244 map = &td->td_proc->p_vmspace->vm_map;
1245 racct_set(td->td_proc, RACCT_MEMLOCK,
1246 ptoa(pmap_wired_count(map->pmap)));
1247 PROC_UNLOCK(td->td_proc);
1250 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1256 * Helper function for vm_mmap. Perform sanity check specific for mmap
1257 * operations on vnodes.
1259 * For VCHR vnodes, the vnode lock is held over the call to
1260 * vm_mmap_cdev() to keep vp->v_rdev valid.
1263 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1264 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1265 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1266 boolean_t *writecounted)
1273 int error, flags, locktype, vfslocked;
1276 cred = td->td_ucred;
1277 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1278 locktype = LK_EXCLUSIVE;
1280 locktype = LK_SHARED;
1281 vfslocked = VFS_LOCK_GIANT(mp);
1282 if ((error = vget(vp, locktype, td)) != 0) {
1283 VFS_UNLOCK_GIANT(vfslocked);
1289 if (vp->v_type == VREG) {
1291 * Get the proper underlying object
1297 if (obj->handle != vp) {
1299 vp = (struct vnode *)obj->handle;
1301 * Bypass filesystems obey the mpsafety of the
1304 error = vget(vp, locktype, td);
1306 VFS_UNLOCK_GIANT(vfslocked);
1310 if (locktype == LK_EXCLUSIVE) {
1311 *writecounted = TRUE;
1312 vnode_pager_update_writecount(obj, 0, objsize);
1314 } else if (vp->v_type == VCHR) {
1315 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1316 vp->v_rdev, foffp, objp);
1324 if ((error = VOP_GETATTR(vp, &va, cred)))
1327 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1331 if ((flags & MAP_SHARED) != 0) {
1332 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1333 if (prot & PROT_WRITE) {
1337 *maxprotp &= ~VM_PROT_WRITE;
1341 * If it is a regular file without any references
1342 * we do not need to sync it.
1343 * Adjust object size to be the size of actual file.
1345 objsize = round_page(va.va_size);
1346 if (va.va_nlink == 0)
1347 flags |= MAP_NOSYNC;
1348 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, cred);
1357 vfs_mark_atime(vp, cred);
1360 if (error != 0 && *writecounted) {
1361 *writecounted = FALSE;
1362 vnode_pager_update_writecount(obj, objsize, 0);
1365 VFS_UNLOCK_GIANT(vfslocked);
1374 * Helper function for vm_mmap. Perform sanity check specific for mmap
1375 * operations on cdevs.
1378 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1379 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1380 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1384 int error, flags, ref;
1388 dsw = dev_refthread(cdev, &ref);
1391 if (dsw->d_flags & D_MMAP_ANON) {
1392 dev_relthread(cdev, ref);
1393 *maxprotp = VM_PROT_ALL;
1394 *flagsp |= MAP_ANON;
1398 * cdevs do not provide private mappings of any kind.
1400 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1401 (prot & PROT_WRITE) != 0) {
1402 dev_relthread(cdev, ref);
1405 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1406 dev_relthread(cdev, ref);
1410 * Force device mappings to be shared.
1412 flags |= MAP_SHARED;
1414 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1416 dev_relthread(cdev, ref);
1421 * First, try d_mmap_single(). If that is not implemented
1422 * (returns ENODEV), fall back to using the device pager.
1423 * Note that d_mmap_single() must return a reference to the
1424 * object (it needs to bump the reference count of the object
1425 * it returns somehow).
1427 * XXX assumes VM_PROT_* == PROT_*
1429 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1430 dev_relthread(cdev, ref);
1431 if (error != ENODEV)
1433 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1447 * Helper function for vm_mmap. Perform sanity check specific for mmap
1448 * operations on shm file descriptors.
1451 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1452 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1453 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1457 if ((*flagsp & MAP_SHARED) != 0 &&
1458 (*maxprotp & VM_PROT_WRITE) == 0 &&
1459 (prot & PROT_WRITE) != 0)
1462 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1466 error = shm_mmap(shmfd, objsize, foff, objp);
1477 * Internal version of mmap. Currently used by mmap, exec, and sys5
1478 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1481 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1482 vm_prot_t maxprot, int flags,
1483 objtype_t handle_type, void *handle,
1487 vm_object_t object = NULL;
1488 struct thread *td = curthread;
1489 int docow, error, rv;
1490 boolean_t writecounted;
1495 size = round_page(size);
1497 PROC_LOCK(td->td_proc);
1498 if (td->td_proc->p_vmspace->vm_map.size + size >
1499 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1500 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1501 if (ptoa(pmap_wired_count(map->pmap)) + size >
1502 lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1503 racct_set_force(td->td_proc, RACCT_VMEM,
1505 PROC_UNLOCK(td->td_proc);
1508 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1509 ptoa(pmap_wired_count(map->pmap)) + size);
1511 racct_set_force(td->td_proc, RACCT_VMEM,
1513 PROC_UNLOCK(td->td_proc);
1517 PROC_UNLOCK(td->td_proc);
1520 if (racct_set(td->td_proc, RACCT_VMEM,
1521 td->td_proc->p_vmspace->vm_map.size + size)) {
1522 PROC_UNLOCK(td->td_proc);
1525 PROC_UNLOCK(td->td_proc);
1528 * We currently can only deal with page aligned file offsets.
1529 * The check is here rather than in the syscall because the
1530 * kernel calls this function internally for other mmaping
1531 * operations (such as in exec) and non-aligned offsets will
1532 * cause pmap inconsistencies...so we want to be sure to
1533 * disallow this in all cases.
1535 if (foff & PAGE_MASK)
1538 if ((flags & MAP_FIXED) == 0) {
1540 *addr = round_page(*addr);
1542 if (*addr != trunc_page(*addr))
1546 writecounted = FALSE;
1549 * Lookup/allocate object.
1551 switch (handle_type) {
1553 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1554 handle, &foff, &object);
1557 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1558 handle, &foff, &object, &writecounted);
1561 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1562 handle, foff, &object);
1565 if (handle == NULL) {
1576 if (flags & MAP_ANON) {
1580 * Unnamed anonymous regions always start at 0.
1584 } else if (flags & MAP_PREFAULT_READ)
1585 docow = MAP_PREFAULT;
1587 docow = MAP_PREFAULT_PARTIAL;
1589 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1590 docow |= MAP_COPY_ON_WRITE;
1591 if (flags & MAP_NOSYNC)
1592 docow |= MAP_DISABLE_SYNCER;
1593 if (flags & MAP_NOCORE)
1594 docow |= MAP_DISABLE_COREDUMP;
1595 /* Shared memory is also shared with children. */
1596 if (flags & MAP_SHARED)
1597 docow |= MAP_INHERIT_SHARE;
1599 docow |= MAP_VN_WRITECOUNT;
1601 if (flags & MAP_STACK)
1602 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1603 docow | MAP_STACK_GROWS_DOWN);
1605 rv = vm_map_find(map, object, foff, addr, size,
1606 object != NULL && object->type == OBJT_DEVICE ?
1607 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1609 rv = vm_map_fixed(map, object, foff, *addr, size,
1610 prot, maxprot, docow);
1612 if (rv == KERN_SUCCESS) {
1614 * If the process has requested that all future mappings
1615 * be wired, then heed this.
1617 if (map->flags & MAP_WIREFUTURE) {
1618 vm_map_wire(map, *addr, *addr + size,
1619 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1620 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1624 * If this mapping was accounted for in the vnode's
1625 * writecount, then undo that now.
1628 vnode_pager_release_writecount(object, 0, size);
1630 * Lose the object reference. Will destroy the
1631 * object if it's an unnamed anonymous mapping
1632 * or named anonymous without other references.
1634 vm_object_deallocate(object);
1636 return (vm_mmap_to_errno(rv));
1640 * Translate a Mach VM return code to zero on success or the appropriate errno
1644 vm_mmap_to_errno(int rv)
1650 case KERN_INVALID_ADDRESS:
1653 case KERN_PROTECTION_FAILURE: