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;
204 int align, error, flags;
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 */
254 /* Ensure alignment is at least a page and fits in a pointer. */
255 align = flags & MAP_ALIGNMENT_MASK;
256 if (align != 0 && align != MAP_ALIGNED_SUPER &&
257 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
258 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
262 * Check for illegal addresses. Watch out for address wrap... Note
263 * that VM_*_ADDRESS are not constants due to casts (argh).
265 if (flags & MAP_FIXED) {
267 * The specified address must have the same remainder
268 * as the file offset taken modulo PAGE_SIZE, so it
269 * should be aligned after adjustment by pageoff.
272 if (addr & PAGE_MASK)
275 /* Address range must be all in user VM space. */
276 if (addr < vm_map_min(&vms->vm_map) ||
277 addr + size > vm_map_max(&vms->vm_map))
279 if (addr + size < addr)
283 * XXX for non-fixed mappings where no hint is provided or
284 * the hint would fall in the potential heap space,
285 * place it after the end of the largest possible heap.
287 * There should really be a pmap call to determine a reasonable
290 PROC_LOCK(td->td_proc);
292 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
293 addr < round_page((vm_offset_t)vms->vm_daddr +
294 lim_max(td->td_proc, RLIMIT_DATA))))
295 addr = round_page((vm_offset_t)vms->vm_daddr +
296 lim_max(td->td_proc, RLIMIT_DATA));
297 PROC_UNLOCK(td->td_proc);
299 if (flags & MAP_ANON) {
301 * Mapping blank space is trivial.
304 handle_type = OBJT_DEFAULT;
305 maxprot = VM_PROT_ALL;
306 cap_maxprot = VM_PROT_ALL;
309 * Mapping file, get fp for validation and don't let the
310 * descriptor disappear on us if we block. Check capability
311 * rights, but also return the maximum rights to be combined
312 * with maxprot later.
315 if (prot & PROT_READ)
317 if ((flags & MAP_SHARED) != 0) {
318 if (prot & PROT_WRITE)
321 if (prot & PROT_EXEC)
322 rights |= CAP_MAPEXEC;
323 if ((error = fget_mmap(td, uap->fd, rights, &cap_maxprot,
326 if (fp->f_type == DTYPE_SHM) {
328 handle_type = OBJT_SWAP;
329 maxprot = VM_PROT_NONE;
331 /* FREAD should always be set. */
332 if (fp->f_flag & FREAD)
333 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
334 if (fp->f_flag & FWRITE)
335 maxprot |= VM_PROT_WRITE;
338 if (fp->f_type != DTYPE_VNODE) {
342 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
343 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
345 * POSIX shared-memory objects are defined to have
346 * kernel persistence, and are not defined to support
347 * read(2)/write(2) -- or even open(2). Thus, we can
348 * use MAP_ASYNC to trade on-disk coherence for speed.
349 * The shm_open(3) library routine turns on the FPOSIXSHM
350 * flag to request this behavior.
352 if (fp->f_flag & FPOSIXSHM)
357 * Ensure that file and memory protections are
358 * compatible. Note that we only worry about
359 * writability if mapping is shared; in this case,
360 * current and max prot are dictated by the open file.
361 * XXX use the vnode instead? Problem is: what
362 * credentials do we use for determination? What if
363 * proc does a setuid?
365 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
366 maxprot = VM_PROT_NONE;
368 maxprot = VM_PROT_EXECUTE;
369 if (fp->f_flag & FREAD) {
370 maxprot |= VM_PROT_READ;
371 } else if (prot & PROT_READ) {
376 * If we are sharing potential changes (either via
377 * MAP_SHARED or via the implicit sharing of character
378 * device mappings), and we are trying to get write
379 * permission although we opened it without asking
382 if ((flags & MAP_SHARED) != 0) {
383 if ((fp->f_flag & FWRITE) != 0) {
384 maxprot |= VM_PROT_WRITE;
385 } else if ((prot & PROT_WRITE) != 0) {
389 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
390 maxprot |= VM_PROT_WRITE;
391 cap_maxprot |= VM_PROT_WRITE;
394 handle_type = OBJT_VNODE;
398 maxprot &= cap_maxprot;
399 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
400 flags, handle_type, handle, pos);
403 /* inform hwpmc(4) if an executable is being mapped */
404 if (error == 0 && handle_type == OBJT_VNODE &&
405 (prot & PROT_EXEC)) {
406 pkm.pm_file = handle;
407 pkm.pm_address = (uintptr_t) addr;
408 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
412 td->td_retval[0] = (register_t) (addr + pageoff);
421 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
423 struct mmap_args oargs;
425 oargs.addr = uap->addr;
426 oargs.len = uap->len;
427 oargs.prot = uap->prot;
428 oargs.flags = uap->flags;
430 oargs.pos = uap->pos;
431 return (sys_mmap(td, &oargs));
435 #ifndef _SYS_SYSPROTO_H_
448 struct ommap_args *uap;
450 struct mmap_args nargs;
451 static const char cvtbsdprot[8] = {
455 PROT_EXEC | PROT_WRITE,
457 PROT_EXEC | PROT_READ,
458 PROT_WRITE | PROT_READ,
459 PROT_EXEC | PROT_WRITE | PROT_READ,
462 #define OMAP_ANON 0x0002
463 #define OMAP_COPY 0x0020
464 #define OMAP_SHARED 0x0010
465 #define OMAP_FIXED 0x0100
467 nargs.addr = uap->addr;
468 nargs.len = uap->len;
469 nargs.prot = cvtbsdprot[uap->prot & 0x7];
470 #ifdef COMPAT_FREEBSD32
471 #if defined(__amd64__) || defined(__ia64__)
472 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
474 nargs.prot |= PROT_EXEC;
478 if (uap->flags & OMAP_ANON)
479 nargs.flags |= MAP_ANON;
480 if (uap->flags & OMAP_COPY)
481 nargs.flags |= MAP_COPY;
482 if (uap->flags & OMAP_SHARED)
483 nargs.flags |= MAP_SHARED;
485 nargs.flags |= MAP_PRIVATE;
486 if (uap->flags & OMAP_FIXED)
487 nargs.flags |= MAP_FIXED;
489 nargs.pos = uap->pos;
490 return (sys_mmap(td, &nargs));
492 #endif /* COMPAT_43 */
495 #ifndef _SYS_SYSPROTO_H_
508 struct msync_args *uap;
511 vm_size_t size, pageoff;
516 addr = (vm_offset_t) uap->addr;
520 pageoff = (addr & PAGE_MASK);
523 size = (vm_size_t) round_page(size);
524 if (addr + size < addr)
527 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
530 map = &td->td_proc->p_vmspace->vm_map;
533 * Clean the pages and interpret the return value.
535 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
536 (flags & MS_INVALIDATE) != 0);
540 case KERN_INVALID_ADDRESS:
541 return (EINVAL); /* Sun returns ENOMEM? */
542 case KERN_INVALID_ARGUMENT:
551 #ifndef _SYS_SYSPROTO_H_
563 struct munmap_args *uap;
566 struct pmckern_map_out pkm;
567 vm_map_entry_t entry;
570 vm_size_t size, pageoff;
573 addr = (vm_offset_t) uap->addr;
578 pageoff = (addr & PAGE_MASK);
581 size = (vm_size_t) round_page(size);
582 if (addr + size < addr)
586 * Check for illegal addresses. Watch out for address wrap...
588 map = &td->td_proc->p_vmspace->vm_map;
589 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
594 * Inform hwpmc if the address range being unmapped contains
595 * an executable region.
597 pkm.pm_address = (uintptr_t) NULL;
598 if (vm_map_lookup_entry(map, addr, &entry)) {
600 entry != &map->header && entry->start < addr + size;
601 entry = entry->next) {
602 if (vm_map_check_protection(map, entry->start,
603 entry->end, VM_PROT_EXECUTE) == TRUE) {
604 pkm.pm_address = (uintptr_t) addr;
605 pkm.pm_size = (size_t) size;
611 vm_map_delete(map, addr, addr + size);
614 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
615 vm_map_lock_downgrade(map);
616 if (pkm.pm_address != (uintptr_t) NULL)
617 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
618 vm_map_unlock_read(map);
622 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
626 #ifndef _SYS_SYSPROTO_H_
627 struct mprotect_args {
637 sys_mprotect(td, uap)
639 struct mprotect_args *uap;
642 vm_size_t size, pageoff;
645 addr = (vm_offset_t) uap->addr;
647 prot = uap->prot & VM_PROT_ALL;
649 pageoff = (addr & PAGE_MASK);
652 size = (vm_size_t) round_page(size);
653 if (addr + size < addr)
656 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
657 addr + size, prot, FALSE)) {
660 case KERN_PROTECTION_FAILURE:
662 case KERN_RESOURCE_SHORTAGE:
668 #ifndef _SYS_SYSPROTO_H_
669 struct minherit_args {
679 sys_minherit(td, uap)
681 struct minherit_args *uap;
684 vm_size_t size, pageoff;
685 vm_inherit_t inherit;
687 addr = (vm_offset_t)uap->addr;
689 inherit = uap->inherit;
691 pageoff = (addr & PAGE_MASK);
694 size = (vm_size_t) round_page(size);
695 if (addr + size < addr)
698 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
699 addr + size, inherit)) {
702 case KERN_PROTECTION_FAILURE:
708 #ifndef _SYS_SYSPROTO_H_
709 struct madvise_args {
723 struct madvise_args *uap;
725 vm_offset_t start, end;
731 * Check for our special case, advising the swap pager we are
734 if (uap->behav == MADV_PROTECT) {
735 error = priv_check(td, PRIV_VM_MADV_PROTECT);
739 p->p_flag |= P_PROTECTED;
745 * Check for illegal behavior
747 if (uap->behav < 0 || uap->behav > MADV_CORE)
750 * Check for illegal addresses. Watch out for address wrap... Note
751 * that VM_*_ADDRESS are not constants due to casts (argh).
753 map = &td->td_proc->p_vmspace->vm_map;
754 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
755 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
757 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
761 * Since this routine is only advisory, we default to conservative
764 start = trunc_page((vm_offset_t) uap->addr);
765 end = round_page((vm_offset_t) uap->addr + uap->len);
767 if (vm_map_madvise(map, start, end, uap->behav))
772 #ifndef _SYS_SYSPROTO_H_
773 struct mincore_args {
787 struct mincore_args *uap;
789 vm_offset_t addr, first_addr;
790 vm_offset_t end, cend;
795 int vecindex, lastvecindex;
796 vm_map_entry_t current;
797 vm_map_entry_t entry;
799 vm_paddr_t locked_pa;
803 unsigned int timestamp;
807 * Make sure that the addresses presented are valid for user
810 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
811 end = addr + (vm_size_t)round_page(uap->len);
812 map = &td->td_proc->p_vmspace->vm_map;
813 if (end > vm_map_max(map) || end < addr)
817 * Address of byte vector
821 pmap = vmspace_pmap(td->td_proc->p_vmspace);
823 vm_map_lock_read(map);
825 timestamp = map->timestamp;
827 if (!vm_map_lookup_entry(map, addr, &entry)) {
828 vm_map_unlock_read(map);
833 * Do this on a map entry basis so that if the pages are not
834 * in the current processes address space, we can easily look
835 * up the pages elsewhere.
838 for (current = entry;
839 (current != &map->header) && (current->start < end);
840 current = current->next) {
843 * check for contiguity
845 if (current->end < end &&
846 (entry->next == &map->header ||
847 current->next->start > current->end)) {
848 vm_map_unlock_read(map);
853 * ignore submaps (for now) or null objects
855 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
856 current->object.vm_object == NULL)
860 * limit this scan to the current map entry and the
861 * limits for the mincore call
863 if (addr < current->start)
864 addr = current->start;
870 * scan this entry one page at a time
872 while (addr < cend) {
874 * Check pmap first, it is likely faster, also
875 * it can provide info as to whether we are the
876 * one referencing or modifying the page.
882 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
883 if (locked_pa != 0) {
885 * The page is mapped by this process but not
886 * both accessed and modified. It is also
887 * managed. Acquire the object lock so that
888 * other mappings might be examined.
890 m = PHYS_TO_VM_PAGE(locked_pa);
891 if (m->object != object) {
893 VM_OBJECT_UNLOCK(object);
895 locked = VM_OBJECT_TRYLOCK(object);
898 VM_OBJECT_LOCK(object);
904 KASSERT(m->valid == VM_PAGE_BITS_ALL,
905 ("mincore: page %p is mapped but invalid",
907 } else if (mincoreinfo == 0) {
909 * The page is not mapped by this process. If
910 * the object implements managed pages, then
911 * determine if the page is resident so that
912 * the mappings might be examined.
914 if (current->object.vm_object != object) {
916 VM_OBJECT_UNLOCK(object);
917 object = current->object.vm_object;
918 VM_OBJECT_LOCK(object);
920 if (object->type == OBJT_DEFAULT ||
921 object->type == OBJT_SWAP ||
922 object->type == OBJT_VNODE) {
923 pindex = OFF_TO_IDX(current->offset +
924 (addr - current->start));
925 m = vm_page_lookup(object, pindex);
927 vm_page_is_cached(object, pindex))
928 mincoreinfo = MINCORE_INCORE;
929 if (m != NULL && m->valid == 0)
932 mincoreinfo = MINCORE_INCORE;
936 /* Examine other mappings to the page. */
937 if (m->dirty == 0 && pmap_is_modified(m))
940 mincoreinfo |= MINCORE_MODIFIED_OTHER;
942 * The first test for PGA_REFERENCED is an
943 * optimization. The second test is
944 * required because a concurrent pmap
945 * operation could clear the last reference
946 * and set PGA_REFERENCED before the call to
947 * pmap_is_referenced().
949 if ((m->aflags & PGA_REFERENCED) != 0 ||
950 pmap_is_referenced(m) ||
951 (m->aflags & PGA_REFERENCED) != 0)
952 mincoreinfo |= MINCORE_REFERENCED_OTHER;
955 VM_OBJECT_UNLOCK(object);
958 * subyte may page fault. In case it needs to modify
959 * the map, we release the lock.
961 vm_map_unlock_read(map);
964 * calculate index into user supplied byte vector
966 vecindex = OFF_TO_IDX(addr - first_addr);
969 * If we have skipped map entries, we need to make sure that
970 * the byte vector is zeroed for those skipped entries.
972 while ((lastvecindex + 1) < vecindex) {
974 error = subyte(vec + lastvecindex, 0);
982 * Pass the page information to the user
984 error = subyte(vec + vecindex, mincoreinfo);
991 * If the map has changed, due to the subyte, the previous
992 * output may be invalid.
994 vm_map_lock_read(map);
995 if (timestamp != map->timestamp)
998 lastvecindex = vecindex;
1004 * subyte may page fault. In case it needs to modify
1005 * the map, we release the lock.
1007 vm_map_unlock_read(map);
1010 * Zero the last entries in the byte vector.
1012 vecindex = OFF_TO_IDX(end - first_addr);
1013 while ((lastvecindex + 1) < vecindex) {
1015 error = subyte(vec + lastvecindex, 0);
1023 * If the map has changed, due to the subyte, the previous
1024 * output may be invalid.
1026 vm_map_lock_read(map);
1027 if (timestamp != map->timestamp)
1029 vm_map_unlock_read(map);
1034 #ifndef _SYS_SYSPROTO_H_
1046 struct mlock_args *uap;
1049 vm_offset_t addr, end, last, start;
1050 vm_size_t npages, size;
1052 unsigned long nsize;
1055 error = priv_check(td, PRIV_VM_MLOCK);
1058 addr = (vm_offset_t)uap->addr;
1061 start = trunc_page(addr);
1062 end = round_page(last);
1063 if (last < addr || end < addr)
1065 npages = atop(end - start);
1066 if (npages > vm_page_max_wired)
1069 map = &proc->p_vmspace->vm_map;
1071 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1072 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1077 if (npages + cnt.v_wire_count > vm_page_max_wired)
1081 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1086 error = vm_map_wire(map, start, end,
1087 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1089 if (error != KERN_SUCCESS) {
1091 racct_set(proc, RACCT_MEMLOCK,
1092 ptoa(pmap_wired_count(map->pmap)));
1096 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1099 #ifndef _SYS_SYSPROTO_H_
1100 struct mlockall_args {
1109 sys_mlockall(td, uap)
1111 struct mlockall_args *uap;
1116 map = &td->td_proc->p_vmspace->vm_map;
1117 error = priv_check(td, PRIV_VM_MLOCK);
1121 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1125 * If wiring all pages in the process would cause it to exceed
1126 * a hard resource limit, return ENOMEM.
1128 if (!old_mlock && uap->how & MCL_CURRENT) {
1129 PROC_LOCK(td->td_proc);
1130 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1131 PROC_UNLOCK(td->td_proc);
1134 PROC_UNLOCK(td->td_proc);
1137 PROC_LOCK(td->td_proc);
1138 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1139 PROC_UNLOCK(td->td_proc);
1144 if (uap->how & MCL_FUTURE) {
1146 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1151 if (uap->how & MCL_CURRENT) {
1153 * P1003.1-2001 mandates that all currently mapped pages
1154 * will be memory resident and locked (wired) upon return
1155 * from mlockall(). vm_map_wire() will wire pages, by
1156 * calling vm_fault_wire() for each page in the region.
1158 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1159 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1160 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1163 if (error != KERN_SUCCESS) {
1164 PROC_LOCK(td->td_proc);
1165 racct_set(td->td_proc, RACCT_MEMLOCK,
1166 ptoa(pmap_wired_count(map->pmap)));
1167 PROC_UNLOCK(td->td_proc);
1174 #ifndef _SYS_SYSPROTO_H_
1175 struct munlockall_args {
1184 sys_munlockall(td, uap)
1186 struct munlockall_args *uap;
1191 map = &td->td_proc->p_vmspace->vm_map;
1192 error = priv_check(td, PRIV_VM_MUNLOCK);
1196 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1198 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1201 /* Forcibly unwire all pages. */
1202 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1203 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1205 if (error == KERN_SUCCESS) {
1206 PROC_LOCK(td->td_proc);
1207 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1208 PROC_UNLOCK(td->td_proc);
1215 #ifndef _SYS_SYSPROTO_H_
1216 struct munlock_args {
1225 sys_munlock(td, uap)
1227 struct munlock_args *uap;
1229 vm_offset_t addr, end, last, start;
1236 error = priv_check(td, PRIV_VM_MUNLOCK);
1239 addr = (vm_offset_t)uap->addr;
1242 start = trunc_page(addr);
1243 end = round_page(last);
1244 if (last < addr || end < addr)
1246 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1247 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1249 if (error == KERN_SUCCESS) {
1250 PROC_LOCK(td->td_proc);
1251 map = &td->td_proc->p_vmspace->vm_map;
1252 racct_set(td->td_proc, RACCT_MEMLOCK,
1253 ptoa(pmap_wired_count(map->pmap)));
1254 PROC_UNLOCK(td->td_proc);
1257 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1263 * Helper function for vm_mmap. Perform sanity check specific for mmap
1264 * operations on vnodes.
1266 * For VCHR vnodes, the vnode lock is held over the call to
1267 * vm_mmap_cdev() to keep vp->v_rdev valid.
1270 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1271 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1272 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1273 boolean_t *writecounted)
1280 int error, flags, locktype, vfslocked;
1283 cred = td->td_ucred;
1284 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1285 locktype = LK_EXCLUSIVE;
1287 locktype = LK_SHARED;
1288 vfslocked = VFS_LOCK_GIANT(mp);
1289 if ((error = vget(vp, locktype, td)) != 0) {
1290 VFS_UNLOCK_GIANT(vfslocked);
1296 if (vp->v_type == VREG) {
1298 * Get the proper underlying object
1304 if (obj->handle != vp) {
1306 vp = (struct vnode *)obj->handle;
1308 * Bypass filesystems obey the mpsafety of the
1311 error = vget(vp, locktype, td);
1313 VFS_UNLOCK_GIANT(vfslocked);
1317 if (locktype == LK_EXCLUSIVE) {
1318 *writecounted = TRUE;
1319 vnode_pager_update_writecount(obj, 0, objsize);
1321 } else if (vp->v_type == VCHR) {
1322 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1323 vp->v_rdev, foffp, objp);
1331 if ((error = VOP_GETATTR(vp, &va, cred)))
1334 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1338 if ((flags & MAP_SHARED) != 0) {
1339 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1340 if (prot & PROT_WRITE) {
1344 *maxprotp &= ~VM_PROT_WRITE;
1348 * If it is a regular file without any references
1349 * we do not need to sync it.
1350 * Adjust object size to be the size of actual file.
1352 objsize = round_page(va.va_size);
1353 if (va.va_nlink == 0)
1354 flags |= MAP_NOSYNC;
1355 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, cred);
1364 vfs_mark_atime(vp, cred);
1367 if (error != 0 && *writecounted) {
1368 *writecounted = FALSE;
1369 vnode_pager_update_writecount(obj, objsize, 0);
1372 VFS_UNLOCK_GIANT(vfslocked);
1381 * Helper function for vm_mmap. Perform sanity check specific for mmap
1382 * operations on cdevs.
1385 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1386 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1387 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1391 int error, flags, ref;
1395 dsw = dev_refthread(cdev, &ref);
1398 if (dsw->d_flags & D_MMAP_ANON) {
1399 dev_relthread(cdev, ref);
1400 *maxprotp = VM_PROT_ALL;
1401 *flagsp |= MAP_ANON;
1405 * cdevs do not provide private mappings of any kind.
1407 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1408 (prot & PROT_WRITE) != 0) {
1409 dev_relthread(cdev, ref);
1412 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1413 dev_relthread(cdev, ref);
1417 * Force device mappings to be shared.
1419 flags |= MAP_SHARED;
1421 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1423 dev_relthread(cdev, ref);
1428 * First, try d_mmap_single(). If that is not implemented
1429 * (returns ENODEV), fall back to using the device pager.
1430 * Note that d_mmap_single() must return a reference to the
1431 * object (it needs to bump the reference count of the object
1432 * it returns somehow).
1434 * XXX assumes VM_PROT_* == PROT_*
1436 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1437 dev_relthread(cdev, ref);
1438 if (error != ENODEV)
1440 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1454 * Helper function for vm_mmap. Perform sanity check specific for mmap
1455 * operations on shm file descriptors.
1458 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1459 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1460 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1464 if ((*flagsp & MAP_SHARED) != 0 &&
1465 (*maxprotp & VM_PROT_WRITE) == 0 &&
1466 (prot & PROT_WRITE) != 0)
1469 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1473 error = shm_mmap(shmfd, objsize, foff, objp);
1484 * Internal version of mmap. Currently used by mmap, exec, and sys5
1485 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1488 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1489 vm_prot_t maxprot, int flags,
1490 objtype_t handle_type, void *handle,
1494 vm_object_t object = NULL;
1495 struct thread *td = curthread;
1496 int docow, error, findspace, rv;
1497 boolean_t writecounted;
1502 size = round_page(size);
1504 PROC_LOCK(td->td_proc);
1505 if (td->td_proc->p_vmspace->vm_map.size + size >
1506 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1507 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1508 if (ptoa(pmap_wired_count(map->pmap)) + size >
1509 lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1510 racct_set_force(td->td_proc, RACCT_VMEM,
1512 PROC_UNLOCK(td->td_proc);
1515 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1516 ptoa(pmap_wired_count(map->pmap)) + size);
1518 racct_set_force(td->td_proc, RACCT_VMEM,
1520 PROC_UNLOCK(td->td_proc);
1524 PROC_UNLOCK(td->td_proc);
1527 if (racct_set(td->td_proc, RACCT_VMEM,
1528 td->td_proc->p_vmspace->vm_map.size + size)) {
1529 PROC_UNLOCK(td->td_proc);
1532 PROC_UNLOCK(td->td_proc);
1535 * We currently can only deal with page aligned file offsets.
1536 * The check is here rather than in the syscall because the
1537 * kernel calls this function internally for other mmaping
1538 * operations (such as in exec) and non-aligned offsets will
1539 * cause pmap inconsistencies...so we want to be sure to
1540 * disallow this in all cases.
1542 if (foff & PAGE_MASK)
1545 if ((flags & MAP_FIXED) == 0) {
1547 *addr = round_page(*addr);
1549 if (*addr != trunc_page(*addr))
1553 writecounted = FALSE;
1556 * Lookup/allocate object.
1558 switch (handle_type) {
1560 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1561 handle, &foff, &object);
1564 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1565 handle, &foff, &object, &writecounted);
1568 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1569 handle, foff, &object);
1572 if (handle == NULL) {
1583 if (flags & MAP_ANON) {
1587 * Unnamed anonymous regions always start at 0.
1591 } else if (flags & MAP_PREFAULT_READ)
1592 docow = MAP_PREFAULT;
1594 docow = MAP_PREFAULT_PARTIAL;
1596 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1597 docow |= MAP_COPY_ON_WRITE;
1598 if (flags & MAP_NOSYNC)
1599 docow |= MAP_DISABLE_SYNCER;
1600 if (flags & MAP_NOCORE)
1601 docow |= MAP_DISABLE_COREDUMP;
1602 /* Shared memory is also shared with children. */
1603 if (flags & MAP_SHARED)
1604 docow |= MAP_INHERIT_SHARE;
1606 docow |= MAP_VN_WRITECOUNT;
1608 if (flags & MAP_STACK)
1609 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1610 docow | MAP_STACK_GROWS_DOWN);
1612 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1613 findspace = VMFS_SUPER_SPACE;
1614 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1615 findspace = VMFS_ALIGNED_SPACE(flags >>
1616 MAP_ALIGNMENT_SHIFT);
1618 findspace = VMFS_OPTIMAL_SPACE;
1619 rv = vm_map_find(map, object, foff, addr, size, findspace,
1620 prot, maxprot, docow);
1622 rv = vm_map_fixed(map, object, foff, *addr, size,
1623 prot, maxprot, docow);
1625 if (rv == KERN_SUCCESS) {
1627 * If the process has requested that all future mappings
1628 * be wired, then heed this.
1630 if (map->flags & MAP_WIREFUTURE) {
1631 vm_map_wire(map, *addr, *addr + size,
1632 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1633 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1637 * If this mapping was accounted for in the vnode's
1638 * writecount, then undo that now.
1641 vnode_pager_release_writecount(object, 0, size);
1643 * Lose the object reference. Will destroy the
1644 * object if it's an unnamed anonymous mapping
1645 * or named anonymous without other references.
1647 vm_object_deallocate(object);
1649 return (vm_mmap_to_errno(rv));
1653 * Translate a Mach VM return code to zero on success or the appropriate errno
1657 vm_mmap_to_errno(int rv)
1663 case KERN_INVALID_ADDRESS:
1666 case KERN_PROTECTION_FAILURE: