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 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
104 int *, struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
105 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
106 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
107 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
108 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
110 #ifndef _SYS_SYSPROTO_H_
123 struct sbrk_args *uap;
125 /* Not yet implemented */
129 #ifndef _SYS_SYSPROTO_H_
142 struct sstk_args *uap;
144 /* Not yet implemented */
148 #if defined(COMPAT_43)
149 #ifndef _SYS_SYSPROTO_H_
150 struct getpagesize_args {
156 ogetpagesize(td, uap)
158 struct getpagesize_args *uap;
161 td->td_retval[0] = PAGE_SIZE;
164 #endif /* COMPAT_43 */
168 * Memory Map (mmap) system call. Note that the file offset
169 * and address are allowed to be NOT page aligned, though if
170 * the MAP_FIXED flag it set, both must have the same remainder
171 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
172 * page-aligned, the actual mapping starts at trunc_page(addr)
173 * and the return value is adjusted up by the page offset.
175 * Generally speaking, only character devices which are themselves
176 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
177 * there would be no cache coherency between a descriptor and a VM mapping
178 * both to the same character device.
180 #ifndef _SYS_SYSPROTO_H_
198 struct mmap_args *uap;
201 struct pmckern_map_in pkm;
206 vm_size_t size, pageoff;
207 vm_prot_t cap_maxprot, maxprot;
209 objtype_t handle_type;
210 int align, error, flags, prot;
212 struct vmspace *vms = td->td_proc->p_vmspace;
215 addr = (vm_offset_t) uap->addr;
224 * Ignore old flags that used to be defined but did not do anything.
226 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
229 * Enforce the constraints.
230 * Mapping of length 0 is only allowed for old binaries.
231 * Anonymous mapping shall specify -1 as filedescriptor and
232 * zero position for new code. Be nice to ancient a.out
233 * binaries and correct pos for anonymous mapping, since old
234 * ld.so sometimes issues anonymous map requests with non-zero
237 if (!SV_CURPROC_FLAG(SV_AOUT)) {
238 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
239 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
242 if ((flags & MAP_ANON) != 0)
246 if (flags & MAP_STACK) {
247 if ((uap->fd != -1) ||
248 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
253 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
254 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
259 MAP_ALIGNMENT_MASK)) != 0)
261 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
263 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
265 if (prot != PROT_NONE &&
266 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
270 * Align the file position to a page boundary,
271 * and save its page offset component.
273 pageoff = (pos & PAGE_MASK);
276 /* Adjust size for rounding (on both ends). */
277 size += pageoff; /* low end... */
278 size = (vm_size_t) round_page(size); /* hi end */
280 /* Ensure alignment is at least a page and fits in a pointer. */
281 align = flags & MAP_ALIGNMENT_MASK;
282 if (align != 0 && align != MAP_ALIGNED_SUPER &&
283 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
284 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
288 * Check for illegal addresses. Watch out for address wrap... Note
289 * that VM_*_ADDRESS are not constants due to casts (argh).
291 if (flags & MAP_FIXED) {
293 * The specified address must have the same remainder
294 * as the file offset taken modulo PAGE_SIZE, so it
295 * should be aligned after adjustment by pageoff.
298 if (addr & PAGE_MASK)
301 /* Address range must be all in user VM space. */
302 if (addr < vm_map_min(&vms->vm_map) ||
303 addr + size > vm_map_max(&vms->vm_map))
305 if (addr + size < addr)
308 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
310 } else if (flags & MAP_32BIT) {
312 * For MAP_32BIT, override the hint if it is too high and
313 * do not bother moving the mapping past the heap (since
314 * the heap is usually above 2GB).
316 if (addr + size > MAP_32BIT_MAX_ADDR)
321 * XXX for non-fixed mappings where no hint is provided or
322 * the hint would fall in the potential heap space,
323 * place it after the end of the largest possible heap.
325 * There should really be a pmap call to determine a reasonable
328 PROC_LOCK(td->td_proc);
330 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
331 addr < round_page((vm_offset_t)vms->vm_daddr +
332 lim_max(td->td_proc, RLIMIT_DATA))))
333 addr = round_page((vm_offset_t)vms->vm_daddr +
334 lim_max(td->td_proc, RLIMIT_DATA));
335 PROC_UNLOCK(td->td_proc);
337 if (flags & MAP_ANON) {
339 * Mapping blank space is trivial.
342 handle_type = OBJT_DEFAULT;
343 maxprot = VM_PROT_ALL;
344 cap_maxprot = VM_PROT_ALL;
347 * Mapping file, get fp for validation and don't let the
348 * descriptor disappear on us if we block. Check capability
349 * rights, but also return the maximum rights to be combined
350 * with maxprot later.
352 cap_rights_init(&rights, CAP_MMAP);
353 if (prot & PROT_READ)
354 cap_rights_set(&rights, CAP_MMAP_R);
355 if ((flags & MAP_SHARED) != 0) {
356 if (prot & PROT_WRITE)
357 cap_rights_set(&rights, CAP_MMAP_W);
359 if (prot & PROT_EXEC)
360 cap_rights_set(&rights, CAP_MMAP_X);
361 error = fget_mmap(td, uap->fd, &rights, &cap_maxprot, &fp);
364 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
365 td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
369 if (fp->f_type == DTYPE_SHM) {
371 handle_type = OBJT_SWAP;
372 maxprot = VM_PROT_NONE;
374 /* FREAD should always be set. */
375 if (fp->f_flag & FREAD)
376 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
377 if (fp->f_flag & FWRITE)
378 maxprot |= VM_PROT_WRITE;
381 if (fp->f_type != DTYPE_VNODE) {
385 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
386 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
388 * POSIX shared-memory objects are defined to have
389 * kernel persistence, and are not defined to support
390 * read(2)/write(2) -- or even open(2). Thus, we can
391 * use MAP_ASYNC to trade on-disk coherence for speed.
392 * The shm_open(3) library routine turns on the FPOSIXSHM
393 * flag to request this behavior.
395 if (fp->f_flag & FPOSIXSHM)
400 * Ensure that file and memory protections are
401 * compatible. Note that we only worry about
402 * writability if mapping is shared; in this case,
403 * current and max prot are dictated by the open file.
404 * XXX use the vnode instead? Problem is: what
405 * credentials do we use for determination? What if
406 * proc does a setuid?
408 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
409 maxprot = VM_PROT_NONE;
411 maxprot = VM_PROT_EXECUTE;
412 if (fp->f_flag & FREAD) {
413 maxprot |= VM_PROT_READ;
414 } else if (prot & PROT_READ) {
419 * If we are sharing potential changes (either via
420 * MAP_SHARED or via the implicit sharing of character
421 * device mappings), and we are trying to get write
422 * permission although we opened it without asking
425 if ((flags & MAP_SHARED) != 0) {
426 if ((fp->f_flag & FWRITE) != 0) {
427 maxprot |= VM_PROT_WRITE;
428 } else if ((prot & PROT_WRITE) != 0) {
432 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
433 maxprot |= VM_PROT_WRITE;
434 cap_maxprot |= VM_PROT_WRITE;
437 handle_type = OBJT_VNODE;
441 maxprot &= cap_maxprot;
443 /* This relies on VM_PROT_* matching PROT_*. */
444 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
445 flags, handle_type, handle, pos);
448 /* inform hwpmc(4) if an executable is being mapped */
449 if (error == 0 && handle_type == OBJT_VNODE &&
450 (prot & PROT_EXEC)) {
451 pkm.pm_file = handle;
452 pkm.pm_address = (uintptr_t) addr;
453 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
457 td->td_retval[0] = (register_t) (addr + pageoff);
466 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
468 struct mmap_args oargs;
470 oargs.addr = uap->addr;
471 oargs.len = uap->len;
472 oargs.prot = uap->prot;
473 oargs.flags = uap->flags;
475 oargs.pos = uap->pos;
476 return (sys_mmap(td, &oargs));
480 #ifndef _SYS_SYSPROTO_H_
493 struct ommap_args *uap;
495 struct mmap_args nargs;
496 static const char cvtbsdprot[8] = {
500 PROT_EXEC | PROT_WRITE,
502 PROT_EXEC | PROT_READ,
503 PROT_WRITE | PROT_READ,
504 PROT_EXEC | PROT_WRITE | PROT_READ,
507 #define OMAP_ANON 0x0002
508 #define OMAP_COPY 0x0020
509 #define OMAP_SHARED 0x0010
510 #define OMAP_FIXED 0x0100
512 nargs.addr = uap->addr;
513 nargs.len = uap->len;
514 nargs.prot = cvtbsdprot[uap->prot & 0x7];
515 #ifdef COMPAT_FREEBSD32
516 #if defined(__amd64__)
517 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
519 nargs.prot |= PROT_EXEC;
523 if (uap->flags & OMAP_ANON)
524 nargs.flags |= MAP_ANON;
525 if (uap->flags & OMAP_COPY)
526 nargs.flags |= MAP_COPY;
527 if (uap->flags & OMAP_SHARED)
528 nargs.flags |= MAP_SHARED;
530 nargs.flags |= MAP_PRIVATE;
531 if (uap->flags & OMAP_FIXED)
532 nargs.flags |= MAP_FIXED;
534 nargs.pos = uap->pos;
535 return (sys_mmap(td, &nargs));
537 #endif /* COMPAT_43 */
540 #ifndef _SYS_SYSPROTO_H_
553 struct msync_args *uap;
556 vm_size_t size, pageoff;
561 addr = (vm_offset_t) uap->addr;
565 pageoff = (addr & PAGE_MASK);
568 size = (vm_size_t) round_page(size);
569 if (addr + size < addr)
572 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
575 map = &td->td_proc->p_vmspace->vm_map;
578 * Clean the pages and interpret the return value.
580 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
581 (flags & MS_INVALIDATE) != 0);
585 case KERN_INVALID_ADDRESS:
587 case KERN_INVALID_ARGUMENT:
596 #ifndef _SYS_SYSPROTO_H_
608 struct munmap_args *uap;
611 struct pmckern_map_out pkm;
612 vm_map_entry_t entry;
615 vm_size_t size, pageoff;
618 addr = (vm_offset_t) uap->addr;
623 pageoff = (addr & PAGE_MASK);
626 size = (vm_size_t) round_page(size);
627 if (addr + size < addr)
631 * Check for illegal addresses. Watch out for address wrap...
633 map = &td->td_proc->p_vmspace->vm_map;
634 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
639 * Inform hwpmc if the address range being unmapped contains
640 * an executable region.
642 pkm.pm_address = (uintptr_t) NULL;
643 if (vm_map_lookup_entry(map, addr, &entry)) {
645 entry != &map->header && entry->start < addr + size;
646 entry = entry->next) {
647 if (vm_map_check_protection(map, entry->start,
648 entry->end, VM_PROT_EXECUTE) == TRUE) {
649 pkm.pm_address = (uintptr_t) addr;
650 pkm.pm_size = (size_t) size;
656 vm_map_delete(map, addr, addr + size);
659 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
660 vm_map_lock_downgrade(map);
661 if (pkm.pm_address != (uintptr_t) NULL)
662 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
663 vm_map_unlock_read(map);
667 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
671 #ifndef _SYS_SYSPROTO_H_
672 struct mprotect_args {
682 sys_mprotect(td, uap)
684 struct mprotect_args *uap;
687 vm_size_t size, pageoff;
690 addr = (vm_offset_t) uap->addr;
692 prot = uap->prot & VM_PROT_ALL;
694 pageoff = (addr & PAGE_MASK);
697 size = (vm_size_t) round_page(size);
698 if (addr + size < addr)
701 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
702 addr + size, prot, FALSE)) {
705 case KERN_PROTECTION_FAILURE:
707 case KERN_RESOURCE_SHORTAGE:
713 #ifndef _SYS_SYSPROTO_H_
714 struct minherit_args {
724 sys_minherit(td, uap)
726 struct minherit_args *uap;
729 vm_size_t size, pageoff;
730 vm_inherit_t inherit;
732 addr = (vm_offset_t)uap->addr;
734 inherit = uap->inherit;
736 pageoff = (addr & PAGE_MASK);
739 size = (vm_size_t) round_page(size);
740 if (addr + size < addr)
743 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
744 addr + size, inherit)) {
747 case KERN_PROTECTION_FAILURE:
753 #ifndef _SYS_SYSPROTO_H_
754 struct madvise_args {
767 struct madvise_args *uap;
769 vm_offset_t start, end;
774 * Check for our special case, advising the swap pager we are
777 if (uap->behav == MADV_PROTECT) {
779 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
780 PROC_SPROTECT, &flags));
784 * Check for illegal behavior
786 if (uap->behav < 0 || uap->behav > MADV_CORE)
789 * Check for illegal addresses. Watch out for address wrap... Note
790 * that VM_*_ADDRESS are not constants due to casts (argh).
792 map = &td->td_proc->p_vmspace->vm_map;
793 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
794 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
796 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
800 * Since this routine is only advisory, we default to conservative
803 start = trunc_page((vm_offset_t) uap->addr);
804 end = round_page((vm_offset_t) uap->addr + uap->len);
806 if (vm_map_madvise(map, start, end, uap->behav))
811 #ifndef _SYS_SYSPROTO_H_
812 struct mincore_args {
825 struct mincore_args *uap;
827 vm_offset_t addr, first_addr;
828 vm_offset_t end, cend;
833 int vecindex, lastvecindex;
834 vm_map_entry_t current;
835 vm_map_entry_t entry;
837 vm_paddr_t locked_pa;
841 unsigned int timestamp;
845 * Make sure that the addresses presented are valid for user
848 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
849 end = addr + (vm_size_t)round_page(uap->len);
850 map = &td->td_proc->p_vmspace->vm_map;
851 if (end > vm_map_max(map) || end < addr)
855 * Address of byte vector
859 pmap = vmspace_pmap(td->td_proc->p_vmspace);
861 vm_map_lock_read(map);
863 timestamp = map->timestamp;
865 if (!vm_map_lookup_entry(map, addr, &entry)) {
866 vm_map_unlock_read(map);
871 * Do this on a map entry basis so that if the pages are not
872 * in the current processes address space, we can easily look
873 * up the pages elsewhere.
876 for (current = entry;
877 (current != &map->header) && (current->start < end);
878 current = current->next) {
881 * check for contiguity
883 if (current->end < end &&
884 (entry->next == &map->header ||
885 current->next->start > current->end)) {
886 vm_map_unlock_read(map);
891 * ignore submaps (for now) or null objects
893 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
894 current->object.vm_object == NULL)
898 * limit this scan to the current map entry and the
899 * limits for the mincore call
901 if (addr < current->start)
902 addr = current->start;
908 * scan this entry one page at a time
910 while (addr < cend) {
912 * Check pmap first, it is likely faster, also
913 * it can provide info as to whether we are the
914 * one referencing or modifying the page.
920 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
921 if (locked_pa != 0) {
923 * The page is mapped by this process but not
924 * both accessed and modified. It is also
925 * managed. Acquire the object lock so that
926 * other mappings might be examined.
928 m = PHYS_TO_VM_PAGE(locked_pa);
929 if (m->object != object) {
931 VM_OBJECT_WUNLOCK(object);
933 locked = VM_OBJECT_TRYWLOCK(object);
936 VM_OBJECT_WLOCK(object);
942 KASSERT(m->valid == VM_PAGE_BITS_ALL,
943 ("mincore: page %p is mapped but invalid",
945 } else if (mincoreinfo == 0) {
947 * The page is not mapped by this process. If
948 * the object implements managed pages, then
949 * determine if the page is resident so that
950 * the mappings might be examined.
952 if (current->object.vm_object != object) {
954 VM_OBJECT_WUNLOCK(object);
955 object = current->object.vm_object;
956 VM_OBJECT_WLOCK(object);
958 if (object->type == OBJT_DEFAULT ||
959 object->type == OBJT_SWAP ||
960 object->type == OBJT_VNODE) {
961 pindex = OFF_TO_IDX(current->offset +
962 (addr - current->start));
963 m = vm_page_lookup(object, pindex);
965 vm_page_is_cached(object, pindex))
966 mincoreinfo = MINCORE_INCORE;
967 if (m != NULL && m->valid == 0)
970 mincoreinfo = MINCORE_INCORE;
974 /* Examine other mappings to the page. */
975 if (m->dirty == 0 && pmap_is_modified(m))
978 mincoreinfo |= MINCORE_MODIFIED_OTHER;
980 * The first test for PGA_REFERENCED is an
981 * optimization. The second test is
982 * required because a concurrent pmap
983 * operation could clear the last reference
984 * and set PGA_REFERENCED before the call to
985 * pmap_is_referenced().
987 if ((m->aflags & PGA_REFERENCED) != 0 ||
988 pmap_is_referenced(m) ||
989 (m->aflags & PGA_REFERENCED) != 0)
990 mincoreinfo |= MINCORE_REFERENCED_OTHER;
993 VM_OBJECT_WUNLOCK(object);
996 * subyte may page fault. In case it needs to modify
997 * the map, we release the lock.
999 vm_map_unlock_read(map);
1002 * calculate index into user supplied byte vector
1004 vecindex = OFF_TO_IDX(addr - first_addr);
1007 * If we have skipped map entries, we need to make sure that
1008 * the byte vector is zeroed for those skipped entries.
1010 while ((lastvecindex + 1) < vecindex) {
1012 error = subyte(vec + lastvecindex, 0);
1020 * Pass the page information to the user
1022 error = subyte(vec + vecindex, mincoreinfo);
1029 * If the map has changed, due to the subyte, the previous
1030 * output may be invalid.
1032 vm_map_lock_read(map);
1033 if (timestamp != map->timestamp)
1036 lastvecindex = vecindex;
1042 * subyte may page fault. In case it needs to modify
1043 * the map, we release the lock.
1045 vm_map_unlock_read(map);
1048 * Zero the last entries in the byte vector.
1050 vecindex = OFF_TO_IDX(end - first_addr);
1051 while ((lastvecindex + 1) < vecindex) {
1053 error = subyte(vec + lastvecindex, 0);
1061 * If the map has changed, due to the subyte, the previous
1062 * output may be invalid.
1064 vm_map_lock_read(map);
1065 if (timestamp != map->timestamp)
1067 vm_map_unlock_read(map);
1072 #ifndef _SYS_SYSPROTO_H_
1084 struct mlock_args *uap;
1087 return (vm_mlock(td->td_proc, td->td_ucred, uap->addr, uap->len));
1091 vm_mlock(struct proc *proc, struct ucred *cred, const void *addr0, size_t len)
1093 vm_offset_t addr, end, last, start;
1094 vm_size_t npages, size;
1096 unsigned long nsize;
1099 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
1102 addr = (vm_offset_t)addr0;
1105 start = trunc_page(addr);
1106 end = round_page(last);
1107 if (last < addr || end < addr)
1109 npages = atop(end - start);
1110 if (npages > vm_page_max_wired)
1112 map = &proc->p_vmspace->vm_map;
1114 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1115 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1120 if (npages + vm_cnt.v_wire_count > vm_page_max_wired)
1124 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1129 error = vm_map_wire(map, start, end,
1130 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1132 if (error != KERN_SUCCESS) {
1134 racct_set(proc, RACCT_MEMLOCK,
1135 ptoa(pmap_wired_count(map->pmap)));
1139 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1142 #ifndef _SYS_SYSPROTO_H_
1143 struct mlockall_args {
1152 sys_mlockall(td, uap)
1154 struct mlockall_args *uap;
1159 map = &td->td_proc->p_vmspace->vm_map;
1160 error = priv_check(td, PRIV_VM_MLOCK);
1164 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1168 * If wiring all pages in the process would cause it to exceed
1169 * a hard resource limit, return ENOMEM.
1171 if (!old_mlock && uap->how & MCL_CURRENT) {
1172 PROC_LOCK(td->td_proc);
1173 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1174 PROC_UNLOCK(td->td_proc);
1177 PROC_UNLOCK(td->td_proc);
1180 PROC_LOCK(td->td_proc);
1181 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1182 PROC_UNLOCK(td->td_proc);
1187 if (uap->how & MCL_FUTURE) {
1189 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1194 if (uap->how & MCL_CURRENT) {
1196 * P1003.1-2001 mandates that all currently mapped pages
1197 * will be memory resident and locked (wired) upon return
1198 * from mlockall(). vm_map_wire() will wire pages, by
1199 * calling vm_fault_wire() for each page in the region.
1201 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1202 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1203 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1206 if (error != KERN_SUCCESS) {
1207 PROC_LOCK(td->td_proc);
1208 racct_set(td->td_proc, RACCT_MEMLOCK,
1209 ptoa(pmap_wired_count(map->pmap)));
1210 PROC_UNLOCK(td->td_proc);
1217 #ifndef _SYS_SYSPROTO_H_
1218 struct munlockall_args {
1227 sys_munlockall(td, uap)
1229 struct munlockall_args *uap;
1234 map = &td->td_proc->p_vmspace->vm_map;
1235 error = priv_check(td, PRIV_VM_MUNLOCK);
1239 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1241 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1244 /* Forcibly unwire all pages. */
1245 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1246 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1248 if (error == KERN_SUCCESS) {
1249 PROC_LOCK(td->td_proc);
1250 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1251 PROC_UNLOCK(td->td_proc);
1258 #ifndef _SYS_SYSPROTO_H_
1259 struct munlock_args {
1268 sys_munlock(td, uap)
1270 struct munlock_args *uap;
1272 vm_offset_t addr, end, last, start;
1279 error = priv_check(td, PRIV_VM_MUNLOCK);
1282 addr = (vm_offset_t)uap->addr;
1285 start = trunc_page(addr);
1286 end = round_page(last);
1287 if (last < addr || end < addr)
1289 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1290 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1292 if (error == KERN_SUCCESS) {
1293 PROC_LOCK(td->td_proc);
1294 map = &td->td_proc->p_vmspace->vm_map;
1295 racct_set(td->td_proc, RACCT_MEMLOCK,
1296 ptoa(pmap_wired_count(map->pmap)));
1297 PROC_UNLOCK(td->td_proc);
1300 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1306 * Helper function for vm_mmap. Perform sanity check specific for mmap
1307 * operations on vnodes.
1309 * For VCHR vnodes, the vnode lock is held over the call to
1310 * vm_mmap_cdev() to keep vp->v_rdev valid.
1313 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1314 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1315 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1316 boolean_t *writecounted)
1322 int error, flags, locktype;
1324 cred = td->td_ucred;
1325 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1326 locktype = LK_EXCLUSIVE;
1328 locktype = LK_SHARED;
1329 if ((error = vget(vp, locktype, td)) != 0)
1334 if (vp->v_type == VREG) {
1336 * Get the proper underlying object
1342 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1344 vp = (struct vnode *)obj->handle;
1346 * Bypass filesystems obey the mpsafety of the
1347 * underlying fs. Tmpfs never bypasses.
1349 error = vget(vp, locktype, td);
1353 if (locktype == LK_EXCLUSIVE) {
1354 *writecounted = TRUE;
1355 vnode_pager_update_writecount(obj, 0, objsize);
1357 } else if (vp->v_type == VCHR) {
1358 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1359 vp->v_rdev, foffp, objp);
1367 if ((error = VOP_GETATTR(vp, &va, cred)))
1370 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1374 if ((flags & MAP_SHARED) != 0) {
1375 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1376 if (prot & PROT_WRITE) {
1380 *maxprotp &= ~VM_PROT_WRITE;
1384 * If it is a regular file without any references
1385 * we do not need to sync it.
1386 * Adjust object size to be the size of actual file.
1388 objsize = round_page(va.va_size);
1389 if (va.va_nlink == 0)
1390 flags |= MAP_NOSYNC;
1391 if (obj->type == OBJT_VNODE) {
1392 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1399 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1400 ("wrong object type"));
1401 VM_OBJECT_WLOCK(obj);
1402 vm_object_reference_locked(obj);
1403 #if VM_NRESERVLEVEL > 0
1404 vm_object_color(obj, 0);
1406 VM_OBJECT_WUNLOCK(obj);
1412 vfs_mark_atime(vp, cred);
1415 if (error != 0 && *writecounted) {
1416 *writecounted = FALSE;
1417 vnode_pager_update_writecount(obj, objsize, 0);
1428 * Helper function for vm_mmap. Perform sanity check specific for mmap
1429 * operations on cdevs.
1432 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1433 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1434 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1438 int error, flags, ref;
1442 dsw = dev_refthread(cdev, &ref);
1445 if (dsw->d_flags & D_MMAP_ANON) {
1446 dev_relthread(cdev, ref);
1447 *maxprotp = VM_PROT_ALL;
1448 *flagsp |= MAP_ANON;
1452 * cdevs do not provide private mappings of any kind.
1454 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1455 (prot & PROT_WRITE) != 0) {
1456 dev_relthread(cdev, ref);
1459 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1460 dev_relthread(cdev, ref);
1464 * Force device mappings to be shared.
1466 flags |= MAP_SHARED;
1468 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1470 dev_relthread(cdev, ref);
1475 * First, try d_mmap_single(). If that is not implemented
1476 * (returns ENODEV), fall back to using the device pager.
1477 * Note that d_mmap_single() must return a reference to the
1478 * object (it needs to bump the reference count of the object
1479 * it returns somehow).
1481 * XXX assumes VM_PROT_* == PROT_*
1483 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1484 dev_relthread(cdev, ref);
1485 if (error != ENODEV)
1487 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1501 * Helper function for vm_mmap. Perform sanity check specific for mmap
1502 * operations on shm file descriptors.
1505 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1506 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1507 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1511 if ((*flagsp & MAP_SHARED) != 0 &&
1512 (*maxprotp & VM_PROT_WRITE) == 0 &&
1513 (prot & PROT_WRITE) != 0)
1516 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1520 error = shm_mmap(shmfd, objsize, foff, objp);
1531 * Internal version of mmap. Currently used by mmap, exec, and sys5
1532 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1535 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1536 vm_prot_t maxprot, int flags,
1537 objtype_t handle_type, void *handle,
1541 vm_object_t object = NULL;
1542 struct thread *td = curthread;
1543 int docow, error, findspace, rv;
1544 boolean_t writecounted;
1549 size = round_page(size);
1551 if (map == &td->td_proc->p_vmspace->vm_map) {
1552 PROC_LOCK(td->td_proc);
1553 if (map->size + size > lim_cur(td->td_proc, RLIMIT_VMEM)) {
1554 PROC_UNLOCK(td->td_proc);
1557 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1558 PROC_UNLOCK(td->td_proc);
1561 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1562 if (ptoa(pmap_wired_count(map->pmap)) + size >
1563 lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1564 racct_set_force(td->td_proc, RACCT_VMEM,
1566 PROC_UNLOCK(td->td_proc);
1569 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1570 ptoa(pmap_wired_count(map->pmap)) + size);
1572 racct_set_force(td->td_proc, RACCT_VMEM,
1574 PROC_UNLOCK(td->td_proc);
1578 PROC_UNLOCK(td->td_proc);
1582 * We currently can only deal with page aligned file offsets.
1583 * The check is here rather than in the syscall because the
1584 * kernel calls this function internally for other mmaping
1585 * operations (such as in exec) and non-aligned offsets will
1586 * cause pmap inconsistencies...so we want to be sure to
1587 * disallow this in all cases.
1589 if (foff & PAGE_MASK)
1592 if ((flags & MAP_FIXED) == 0) {
1594 *addr = round_page(*addr);
1596 if (*addr != trunc_page(*addr))
1600 writecounted = FALSE;
1603 * Lookup/allocate object.
1605 switch (handle_type) {
1607 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1608 handle, &foff, &object);
1611 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1612 handle, &foff, &object, &writecounted);
1615 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1616 handle, foff, &object);
1619 if (handle == NULL) {
1630 if (flags & MAP_ANON) {
1634 * Unnamed anonymous regions always start at 0.
1638 } else if (flags & MAP_PREFAULT_READ)
1639 docow = MAP_PREFAULT;
1641 docow = MAP_PREFAULT_PARTIAL;
1643 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1644 docow |= MAP_COPY_ON_WRITE;
1645 if (flags & MAP_NOSYNC)
1646 docow |= MAP_DISABLE_SYNCER;
1647 if (flags & MAP_NOCORE)
1648 docow |= MAP_DISABLE_COREDUMP;
1649 /* Shared memory is also shared with children. */
1650 if (flags & MAP_SHARED)
1651 docow |= MAP_INHERIT_SHARE;
1653 docow |= MAP_VN_WRITECOUNT;
1654 if (flags & MAP_STACK) {
1657 docow |= MAP_STACK_GROWS_DOWN;
1659 if ((flags & MAP_EXCL) != 0)
1660 docow |= MAP_CHECK_EXCL;
1663 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1664 findspace = VMFS_SUPER_SPACE;
1665 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1666 findspace = VMFS_ALIGNED_SPACE(flags >>
1667 MAP_ALIGNMENT_SHIFT);
1669 findspace = VMFS_OPTIMAL_SPACE;
1670 rv = vm_map_find(map, object, foff, addr, size,
1672 flags & MAP_32BIT ? MAP_32BIT_MAX_ADDR :
1674 0, findspace, prot, maxprot, docow);
1676 rv = vm_map_fixed(map, object, foff, *addr, size,
1677 prot, maxprot, docow);
1680 if (rv == KERN_SUCCESS) {
1682 * If the process has requested that all future mappings
1683 * be wired, then heed this.
1685 if (map->flags & MAP_WIREFUTURE) {
1686 vm_map_wire(map, *addr, *addr + size,
1687 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1688 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1692 * If this mapping was accounted for in the vnode's
1693 * writecount, then undo that now.
1696 vnode_pager_release_writecount(object, 0, size);
1698 * Lose the object reference. Will destroy the
1699 * object if it's an unnamed anonymous mapping
1700 * or named anonymous without other references.
1702 vm_object_deallocate(object);
1704 return (vm_mmap_to_errno(rv));
1708 * Translate a Mach VM return code to zero on success or the appropriate errno
1712 vm_mmap_to_errno(int rv)
1718 case KERN_INVALID_ADDRESS:
1721 case KERN_PROTECTION_FAILURE: