2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
40 * Mapped file (mmap) interface to VM
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
47 #include "opt_hwpmc_hooks.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capability.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/filedesc.h>
59 #include <sys/racct.h>
60 #include <sys/resource.h>
61 #include <sys/resourcevar.h>
62 #include <sys/vnode.h>
63 #include <sys/fcntl.h>
66 #include <sys/mount.h>
69 #include <sys/sysent.h>
70 #include <sys/vmmeter.h>
72 #include <security/mac/mac_framework.h>
75 #include <vm/vm_param.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vm_pageout.h>
82 #include <vm/vm_extern.h>
83 #include <vm/vm_page.h>
86 #include <sys/pmckern.h>
89 #ifndef _SYS_SYSPROTO_H_
95 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
96 int *, struct vnode *, vm_ooffset_t *, vm_object_t *);
97 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
98 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
99 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
100 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
109 struct sbrk_args *uap;
111 /* Not yet implemented */
115 #ifndef _SYS_SYSPROTO_H_
128 struct sstk_args *uap;
130 /* Not yet implemented */
134 #if defined(COMPAT_43)
135 #ifndef _SYS_SYSPROTO_H_
136 struct getpagesize_args {
143 ogetpagesize(td, uap)
145 struct getpagesize_args *uap;
148 td->td_retval[0] = PAGE_SIZE;
151 #endif /* COMPAT_43 */
155 * Memory Map (mmap) system call. Note that the file offset
156 * and address are allowed to be NOT page aligned, though if
157 * the MAP_FIXED flag it set, both must have the same remainder
158 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
159 * page-aligned, the actual mapping starts at trunc_page(addr)
160 * and the return value is adjusted up by the page offset.
162 * Generally speaking, only character devices which are themselves
163 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
164 * there would be no cache coherency between a descriptor and a VM mapping
165 * both to the same character device.
167 #ifndef _SYS_SYSPROTO_H_
185 struct mmap_args *uap;
188 struct pmckern_map_in pkm;
193 vm_size_t size, pageoff;
194 vm_prot_t cap_maxprot, prot, maxprot;
196 objtype_t handle_type;
199 struct vmspace *vms = td->td_proc->p_vmspace;
202 addr = (vm_offset_t) uap->addr;
204 prot = uap->prot & VM_PROT_ALL;
210 /* Make sure mapping fits into numeric range, etc. */
211 if ((uap->len == 0 && !SV_CURPROC_FLAG(SV_AOUT) &&
212 curproc->p_osrel >= P_OSREL_MAP_ANON) ||
213 ((flags & MAP_ANON) && (uap->fd != -1 || pos != 0)))
216 if (flags & MAP_STACK) {
217 if ((uap->fd != -1) ||
218 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
225 * Align the file position to a page boundary,
226 * and save its page offset component.
228 pageoff = (pos & PAGE_MASK);
231 /* Adjust size for rounding (on both ends). */
232 size += pageoff; /* low end... */
233 size = (vm_size_t) round_page(size); /* hi end */
236 * Check for illegal addresses. Watch out for address wrap... Note
237 * that VM_*_ADDRESS are not constants due to casts (argh).
239 if (flags & MAP_FIXED) {
241 * The specified address must have the same remainder
242 * as the file offset taken modulo PAGE_SIZE, so it
243 * should be aligned after adjustment by pageoff.
246 if (addr & PAGE_MASK)
249 /* Address range must be all in user VM space. */
250 if (addr < vm_map_min(&vms->vm_map) ||
251 addr + size > vm_map_max(&vms->vm_map))
253 if (addr + size < addr)
257 * XXX for non-fixed mappings where no hint is provided or
258 * the hint would fall in the potential heap space,
259 * place it after the end of the largest possible heap.
261 * There should really be a pmap call to determine a reasonable
264 PROC_LOCK(td->td_proc);
266 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
267 addr < round_page((vm_offset_t)vms->vm_daddr +
268 lim_max(td->td_proc, RLIMIT_DATA))))
269 addr = round_page((vm_offset_t)vms->vm_daddr +
270 lim_max(td->td_proc, RLIMIT_DATA));
271 PROC_UNLOCK(td->td_proc);
273 if (flags & MAP_ANON) {
275 * Mapping blank space is trivial.
278 handle_type = OBJT_DEFAULT;
279 maxprot = VM_PROT_ALL;
280 cap_maxprot = VM_PROT_ALL;
283 * Mapping file, get fp for validation and don't let the
284 * descriptor disappear on us if we block. Check capability
285 * rights, but also return the maximum rights to be combined
286 * with maxprot later.
289 if (prot & PROT_READ)
291 if ((flags & MAP_SHARED) != 0) {
292 if (prot & PROT_WRITE)
295 if (prot & PROT_EXEC)
296 rights |= CAP_MAPEXEC;
297 if ((error = fget_mmap(td, uap->fd, rights, &cap_maxprot,
300 if (fp->f_type == DTYPE_SHM) {
302 handle_type = OBJT_SWAP;
303 maxprot = VM_PROT_NONE;
305 /* FREAD should always be set. */
306 if (fp->f_flag & FREAD)
307 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
308 if (fp->f_flag & FWRITE)
309 maxprot |= VM_PROT_WRITE;
312 if (fp->f_type != DTYPE_VNODE) {
316 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
317 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
319 * POSIX shared-memory objects are defined to have
320 * kernel persistence, and are not defined to support
321 * read(2)/write(2) -- or even open(2). Thus, we can
322 * use MAP_ASYNC to trade on-disk coherence for speed.
323 * The shm_open(3) library routine turns on the FPOSIXSHM
324 * flag to request this behavior.
326 if (fp->f_flag & FPOSIXSHM)
331 * Ensure that file and memory protections are
332 * compatible. Note that we only worry about
333 * writability if mapping is shared; in this case,
334 * current and max prot are dictated by the open file.
335 * XXX use the vnode instead? Problem is: what
336 * credentials do we use for determination? What if
337 * proc does a setuid?
339 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
340 maxprot = VM_PROT_NONE;
342 maxprot = VM_PROT_EXECUTE;
343 if (fp->f_flag & FREAD) {
344 maxprot |= VM_PROT_READ;
345 } else if (prot & PROT_READ) {
350 * If we are sharing potential changes (either via
351 * MAP_SHARED or via the implicit sharing of character
352 * device mappings), and we are trying to get write
353 * permission although we opened it without asking
356 if ((flags & MAP_SHARED) != 0) {
357 if ((fp->f_flag & FWRITE) != 0) {
358 maxprot |= VM_PROT_WRITE;
359 } else if ((prot & PROT_WRITE) != 0) {
363 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
364 maxprot |= VM_PROT_WRITE;
365 cap_maxprot |= VM_PROT_WRITE;
368 handle_type = OBJT_VNODE;
372 maxprot &= cap_maxprot;
373 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
374 flags, handle_type, handle, pos);
377 /* inform hwpmc(4) if an executable is being mapped */
378 if (error == 0 && handle_type == OBJT_VNODE &&
379 (prot & PROT_EXEC)) {
380 pkm.pm_file = handle;
381 pkm.pm_address = (uintptr_t) addr;
382 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
386 td->td_retval[0] = (register_t) (addr + pageoff);
395 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
397 struct mmap_args oargs;
399 oargs.addr = uap->addr;
400 oargs.len = uap->len;
401 oargs.prot = uap->prot;
402 oargs.flags = uap->flags;
404 oargs.pos = uap->pos;
405 return (sys_mmap(td, &oargs));
409 #ifndef _SYS_SYSPROTO_H_
422 struct ommap_args *uap;
424 struct mmap_args nargs;
425 static const char cvtbsdprot[8] = {
429 PROT_EXEC | PROT_WRITE,
431 PROT_EXEC | PROT_READ,
432 PROT_WRITE | PROT_READ,
433 PROT_EXEC | PROT_WRITE | PROT_READ,
436 #define OMAP_ANON 0x0002
437 #define OMAP_COPY 0x0020
438 #define OMAP_SHARED 0x0010
439 #define OMAP_FIXED 0x0100
441 nargs.addr = uap->addr;
442 nargs.len = uap->len;
443 nargs.prot = cvtbsdprot[uap->prot & 0x7];
445 if (uap->flags & OMAP_ANON)
446 nargs.flags |= MAP_ANON;
447 if (uap->flags & OMAP_COPY)
448 nargs.flags |= MAP_COPY;
449 if (uap->flags & OMAP_SHARED)
450 nargs.flags |= MAP_SHARED;
452 nargs.flags |= MAP_PRIVATE;
453 if (uap->flags & OMAP_FIXED)
454 nargs.flags |= MAP_FIXED;
456 nargs.pos = uap->pos;
457 return (sys_mmap(td, &nargs));
459 #endif /* COMPAT_43 */
462 #ifndef _SYS_SYSPROTO_H_
475 struct msync_args *uap;
478 vm_size_t size, pageoff;
483 addr = (vm_offset_t) uap->addr;
487 pageoff = (addr & PAGE_MASK);
490 size = (vm_size_t) round_page(size);
491 if (addr + size < addr)
494 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
497 map = &td->td_proc->p_vmspace->vm_map;
500 * Clean the pages and interpret the return value.
502 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
503 (flags & MS_INVALIDATE) != 0);
507 case KERN_INVALID_ADDRESS:
508 return (EINVAL); /* Sun returns ENOMEM? */
509 case KERN_INVALID_ARGUMENT:
516 #ifndef _SYS_SYSPROTO_H_
528 struct munmap_args *uap;
531 struct pmckern_map_out pkm;
532 vm_map_entry_t entry;
535 vm_size_t size, pageoff;
538 addr = (vm_offset_t) uap->addr;
543 pageoff = (addr & PAGE_MASK);
546 size = (vm_size_t) round_page(size);
547 if (addr + size < addr)
551 * Check for illegal addresses. Watch out for address wrap...
553 map = &td->td_proc->p_vmspace->vm_map;
554 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
559 * Inform hwpmc if the address range being unmapped contains
560 * an executable region.
562 pkm.pm_address = (uintptr_t) NULL;
563 if (vm_map_lookup_entry(map, addr, &entry)) {
565 entry != &map->header && entry->start < addr + size;
566 entry = entry->next) {
567 if (vm_map_check_protection(map, entry->start,
568 entry->end, VM_PROT_EXECUTE) == TRUE) {
569 pkm.pm_address = (uintptr_t) addr;
570 pkm.pm_size = (size_t) size;
576 vm_map_delete(map, addr, addr + size);
579 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
580 vm_map_lock_downgrade(map);
581 if (pkm.pm_address != (uintptr_t) NULL)
582 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
583 vm_map_unlock_read(map);
587 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
591 #ifndef _SYS_SYSPROTO_H_
592 struct mprotect_args {
602 sys_mprotect(td, uap)
604 struct mprotect_args *uap;
607 vm_size_t size, pageoff;
610 addr = (vm_offset_t) uap->addr;
612 prot = uap->prot & VM_PROT_ALL;
614 pageoff = (addr & PAGE_MASK);
617 size = (vm_size_t) round_page(size);
618 if (addr + size < addr)
621 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
622 addr + size, prot, FALSE)) {
625 case KERN_PROTECTION_FAILURE:
627 case KERN_RESOURCE_SHORTAGE:
633 #ifndef _SYS_SYSPROTO_H_
634 struct minherit_args {
644 sys_minherit(td, uap)
646 struct minherit_args *uap;
649 vm_size_t size, pageoff;
650 vm_inherit_t inherit;
652 addr = (vm_offset_t)uap->addr;
654 inherit = uap->inherit;
656 pageoff = (addr & PAGE_MASK);
659 size = (vm_size_t) round_page(size);
660 if (addr + size < addr)
663 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
664 addr + size, inherit)) {
667 case KERN_PROTECTION_FAILURE:
673 #ifndef _SYS_SYSPROTO_H_
674 struct madvise_args {
688 struct madvise_args *uap;
690 vm_offset_t start, end;
696 * Check for our special case, advising the swap pager we are
699 if (uap->behav == MADV_PROTECT) {
700 error = priv_check(td, PRIV_VM_MADV_PROTECT);
704 p->p_flag |= P_PROTECTED;
710 * Check for illegal behavior
712 if (uap->behav < 0 || uap->behav > MADV_CORE)
715 * Check for illegal addresses. Watch out for address wrap... Note
716 * that VM_*_ADDRESS are not constants due to casts (argh).
718 map = &td->td_proc->p_vmspace->vm_map;
719 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
720 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
722 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
726 * Since this routine is only advisory, we default to conservative
729 start = trunc_page((vm_offset_t) uap->addr);
730 end = round_page((vm_offset_t) uap->addr + uap->len);
732 if (vm_map_madvise(map, start, end, uap->behav))
737 #ifndef _SYS_SYSPROTO_H_
738 struct mincore_args {
752 struct mincore_args *uap;
754 vm_offset_t addr, first_addr;
755 vm_offset_t end, cend;
760 int vecindex, lastvecindex;
761 vm_map_entry_t current;
762 vm_map_entry_t entry;
764 vm_paddr_t locked_pa;
768 unsigned int timestamp;
772 * Make sure that the addresses presented are valid for user
775 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
776 end = addr + (vm_size_t)round_page(uap->len);
777 map = &td->td_proc->p_vmspace->vm_map;
778 if (end > vm_map_max(map) || end < addr)
782 * Address of byte vector
786 pmap = vmspace_pmap(td->td_proc->p_vmspace);
788 vm_map_lock_read(map);
790 timestamp = map->timestamp;
792 if (!vm_map_lookup_entry(map, addr, &entry)) {
793 vm_map_unlock_read(map);
798 * Do this on a map entry basis so that if the pages are not
799 * in the current processes address space, we can easily look
800 * up the pages elsewhere.
803 for (current = entry;
804 (current != &map->header) && (current->start < end);
805 current = current->next) {
808 * check for contiguity
810 if (current->end < end &&
811 (entry->next == &map->header ||
812 current->next->start > current->end)) {
813 vm_map_unlock_read(map);
818 * ignore submaps (for now) or null objects
820 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
821 current->object.vm_object == NULL)
825 * limit this scan to the current map entry and the
826 * limits for the mincore call
828 if (addr < current->start)
829 addr = current->start;
835 * scan this entry one page at a time
837 while (addr < cend) {
839 * Check pmap first, it is likely faster, also
840 * it can provide info as to whether we are the
841 * one referencing or modifying the page.
847 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
848 if (locked_pa != 0) {
850 * The page is mapped by this process but not
851 * both accessed and modified. It is also
852 * managed. Acquire the object lock so that
853 * other mappings might be examined.
855 m = PHYS_TO_VM_PAGE(locked_pa);
856 if (m->object != object) {
858 VM_OBJECT_UNLOCK(object);
860 locked = VM_OBJECT_TRYLOCK(object);
863 VM_OBJECT_LOCK(object);
869 KASSERT(m->valid == VM_PAGE_BITS_ALL,
870 ("mincore: page %p is mapped but invalid",
872 } else if (mincoreinfo == 0) {
874 * The page is not mapped by this process. If
875 * the object implements managed pages, then
876 * determine if the page is resident so that
877 * the mappings might be examined.
879 if (current->object.vm_object != object) {
881 VM_OBJECT_UNLOCK(object);
882 object = current->object.vm_object;
883 VM_OBJECT_LOCK(object);
885 if (object->type == OBJT_DEFAULT ||
886 object->type == OBJT_SWAP ||
887 object->type == OBJT_VNODE) {
888 pindex = OFF_TO_IDX(current->offset +
889 (addr - current->start));
890 m = vm_page_lookup(object, pindex);
891 if (m != NULL && m->valid == 0)
894 mincoreinfo = MINCORE_INCORE;
898 /* Examine other mappings to the page. */
899 if (m->dirty == 0 && pmap_is_modified(m))
902 mincoreinfo |= MINCORE_MODIFIED_OTHER;
904 * The first test for PGA_REFERENCED is an
905 * optimization. The second test is
906 * required because a concurrent pmap
907 * operation could clear the last reference
908 * and set PGA_REFERENCED before the call to
909 * pmap_is_referenced().
911 if ((m->aflags & PGA_REFERENCED) != 0 ||
912 pmap_is_referenced(m) ||
913 (m->aflags & PGA_REFERENCED) != 0)
914 mincoreinfo |= MINCORE_REFERENCED_OTHER;
917 VM_OBJECT_UNLOCK(object);
920 * subyte may page fault. In case it needs to modify
921 * the map, we release the lock.
923 vm_map_unlock_read(map);
926 * calculate index into user supplied byte vector
928 vecindex = OFF_TO_IDX(addr - first_addr);
931 * If we have skipped map entries, we need to make sure that
932 * the byte vector is zeroed for those skipped entries.
934 while ((lastvecindex + 1) < vecindex) {
935 error = subyte(vec + lastvecindex, 0);
944 * Pass the page information to the user
946 error = subyte(vec + vecindex, mincoreinfo);
953 * If the map has changed, due to the subyte, the previous
954 * output may be invalid.
956 vm_map_lock_read(map);
957 if (timestamp != map->timestamp)
960 lastvecindex = vecindex;
966 * subyte may page fault. In case it needs to modify
967 * the map, we release the lock.
969 vm_map_unlock_read(map);
972 * Zero the last entries in the byte vector.
974 vecindex = OFF_TO_IDX(end - first_addr);
975 while ((lastvecindex + 1) < vecindex) {
976 error = subyte(vec + lastvecindex, 0);
985 * If the map has changed, due to the subyte, the previous
986 * output may be invalid.
988 vm_map_lock_read(map);
989 if (timestamp != map->timestamp)
991 vm_map_unlock_read(map);
996 #ifndef _SYS_SYSPROTO_H_
1008 struct mlock_args *uap;
1011 vm_offset_t addr, end, last, start;
1012 vm_size_t npages, size;
1013 unsigned long nsize;
1016 error = priv_check(td, PRIV_VM_MLOCK);
1019 addr = (vm_offset_t)uap->addr;
1022 start = trunc_page(addr);
1023 end = round_page(last);
1024 if (last < addr || end < addr)
1026 npages = atop(end - start);
1027 if (npages > vm_page_max_wired)
1031 nsize = ptoa(npages +
1032 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map)));
1033 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1038 if (npages + cnt.v_wire_count > vm_page_max_wired)
1042 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1047 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
1048 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1050 if (error != KERN_SUCCESS) {
1052 racct_set(proc, RACCT_MEMLOCK,
1053 ptoa(pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))));
1057 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1060 #ifndef _SYS_SYSPROTO_H_
1061 struct mlockall_args {
1070 sys_mlockall(td, uap)
1072 struct mlockall_args *uap;
1077 map = &td->td_proc->p_vmspace->vm_map;
1080 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1085 * If wiring all pages in the process would cause it to exceed
1086 * a hard resource limit, return ENOMEM.
1088 PROC_LOCK(td->td_proc);
1089 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1090 PROC_UNLOCK(td->td_proc);
1093 PROC_UNLOCK(td->td_proc);
1095 error = priv_check(td, PRIV_VM_MLOCK);
1100 PROC_LOCK(td->td_proc);
1101 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1102 PROC_UNLOCK(td->td_proc);
1107 if (uap->how & MCL_FUTURE) {
1109 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1114 if (uap->how & MCL_CURRENT) {
1116 * P1003.1-2001 mandates that all currently mapped pages
1117 * will be memory resident and locked (wired) upon return
1118 * from mlockall(). vm_map_wire() will wire pages, by
1119 * calling vm_fault_wire() for each page in the region.
1121 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1122 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1123 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1126 if (error != KERN_SUCCESS) {
1127 PROC_LOCK(td->td_proc);
1128 racct_set(td->td_proc, RACCT_MEMLOCK,
1129 ptoa(pmap_wired_count(vm_map_pmap(&td->td_proc->p_vmspace->vm_map))));
1130 PROC_UNLOCK(td->td_proc);
1137 #ifndef _SYS_SYSPROTO_H_
1138 struct munlockall_args {
1147 sys_munlockall(td, uap)
1149 struct munlockall_args *uap;
1154 map = &td->td_proc->p_vmspace->vm_map;
1155 error = priv_check(td, PRIV_VM_MUNLOCK);
1159 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1161 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1164 /* Forcibly unwire all pages. */
1165 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1166 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1168 if (error == KERN_SUCCESS) {
1169 PROC_LOCK(td->td_proc);
1170 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1171 PROC_UNLOCK(td->td_proc);
1178 #ifndef _SYS_SYSPROTO_H_
1179 struct munlock_args {
1188 sys_munlock(td, uap)
1190 struct munlock_args *uap;
1192 vm_offset_t addr, end, last, start;
1196 error = priv_check(td, PRIV_VM_MUNLOCK);
1199 addr = (vm_offset_t)uap->addr;
1202 start = trunc_page(addr);
1203 end = round_page(last);
1204 if (last < addr || end < addr)
1206 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1207 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1209 if (error == KERN_SUCCESS) {
1210 PROC_LOCK(td->td_proc);
1211 racct_sub(td->td_proc, RACCT_MEMLOCK, ptoa(end - start));
1212 PROC_UNLOCK(td->td_proc);
1215 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1223 * Helper function for vm_mmap. Perform sanity check specific for mmap
1224 * operations on vnodes.
1227 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1228 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1229 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp)
1240 cred = td->td_ucred;
1241 vfslocked = VFS_LOCK_GIANT(mp);
1242 if ((error = vget(vp, LK_SHARED, td)) != 0) {
1243 VFS_UNLOCK_GIANT(vfslocked);
1249 if (vp->v_type == VREG) {
1251 * Get the proper underlying object
1257 if (obj->handle != vp) {
1259 vp = (struct vnode*)obj->handle;
1260 vget(vp, LK_SHARED, td);
1262 } else if (vp->v_type == VCHR) {
1263 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1264 vp->v_rdev, foffp, objp);
1272 if ((error = VOP_GETATTR(vp, &va, cred)))
1275 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1279 if ((flags & MAP_SHARED) != 0) {
1280 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1281 if (prot & PROT_WRITE) {
1285 *maxprotp &= ~VM_PROT_WRITE;
1289 * If it is a regular file without any references
1290 * we do not need to sync it.
1291 * Adjust object size to be the size of actual file.
1293 objsize = round_page(va.va_size);
1294 if (va.va_nlink == 0)
1295 flags |= MAP_NOSYNC;
1296 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, td->td_ucred);
1305 vfs_mark_atime(vp, cred);
1309 VFS_UNLOCK_GIANT(vfslocked);
1318 * Helper function for vm_mmap. Perform sanity check specific for mmap
1319 * operations on cdevs.
1322 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1323 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1324 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1328 int error, flags, ref;
1332 dsw = dev_refthread(cdev, &ref);
1335 if (dsw->d_flags & D_MMAP_ANON) {
1336 dev_relthread(cdev, ref);
1337 *maxprotp = VM_PROT_ALL;
1338 *flagsp |= MAP_ANON;
1342 * cdevs do not provide private mappings of any kind.
1344 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1345 (prot & PROT_WRITE) != 0) {
1346 dev_relthread(cdev, ref);
1349 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1350 dev_relthread(cdev, ref);
1354 * Force device mappings to be shared.
1356 flags |= MAP_SHARED;
1358 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1360 dev_relthread(cdev, ref);
1365 * First, try d_mmap_single(). If that is not implemented
1366 * (returns ENODEV), fall back to using the device pager.
1367 * Note that d_mmap_single() must return a reference to the
1368 * object (it needs to bump the reference count of the object
1369 * it returns somehow).
1371 * XXX assumes VM_PROT_* == PROT_*
1373 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1374 dev_relthread(cdev, ref);
1375 if (error != ENODEV)
1377 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1391 * Helper function for vm_mmap. Perform sanity check specific for mmap
1392 * operations on shm file descriptors.
1395 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1396 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1397 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1401 if ((*flagsp & MAP_SHARED) != 0 &&
1402 (*maxprotp & VM_PROT_WRITE) == 0 &&
1403 (prot & PROT_WRITE) != 0)
1406 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1410 error = shm_mmap(shmfd, objsize, foff, objp);
1421 * Internal version of mmap. Currently used by mmap, exec, and sys5
1422 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1425 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1426 vm_prot_t maxprot, int flags,
1427 objtype_t handle_type, void *handle,
1431 vm_object_t object = NULL;
1432 int rv = KERN_SUCCESS;
1434 struct thread *td = curthread;
1439 size = round_page(size);
1441 PROC_LOCK(td->td_proc);
1442 if (td->td_proc->p_vmspace->vm_map.size + size >
1443 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1444 PROC_UNLOCK(td->td_proc);
1447 if (racct_set(td->td_proc, RACCT_VMEM,
1448 td->td_proc->p_vmspace->vm_map.size + size)) {
1449 PROC_UNLOCK(td->td_proc);
1452 PROC_UNLOCK(td->td_proc);
1455 * We currently can only deal with page aligned file offsets.
1456 * The check is here rather than in the syscall because the
1457 * kernel calls this function internally for other mmaping
1458 * operations (such as in exec) and non-aligned offsets will
1459 * cause pmap inconsistencies...so we want to be sure to
1460 * disallow this in all cases.
1462 if (foff & PAGE_MASK)
1465 if ((flags & MAP_FIXED) == 0) {
1467 *addr = round_page(*addr);
1469 if (*addr != trunc_page(*addr))
1474 * Lookup/allocate object.
1476 switch (handle_type) {
1478 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1479 handle, &foff, &object);
1482 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1483 handle, &foff, &object);
1486 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1487 handle, foff, &object);
1490 if (handle == NULL) {
1501 if (flags & MAP_ANON) {
1505 * Unnamed anonymous regions always start at 0.
1509 } else if (flags & MAP_PREFAULT_READ)
1510 docow = MAP_PREFAULT;
1512 docow = MAP_PREFAULT_PARTIAL;
1514 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1515 docow |= MAP_COPY_ON_WRITE;
1516 if (flags & MAP_NOSYNC)
1517 docow |= MAP_DISABLE_SYNCER;
1518 if (flags & MAP_NOCORE)
1519 docow |= MAP_DISABLE_COREDUMP;
1521 if (flags & MAP_STACK)
1522 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1523 docow | MAP_STACK_GROWS_DOWN);
1525 rv = vm_map_find(map, object, foff, addr, size,
1526 object != NULL && object->type == OBJT_DEVICE ?
1527 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1529 rv = vm_map_fixed(map, object, foff, *addr, size,
1530 prot, maxprot, docow);
1532 if (rv != KERN_SUCCESS) {
1534 * Lose the object reference. Will destroy the
1535 * object if it's an unnamed anonymous mapping
1536 * or named anonymous without other references.
1538 vm_object_deallocate(object);
1539 } else if (flags & MAP_SHARED) {
1541 * Shared memory is also shared with children.
1543 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1544 if (rv != KERN_SUCCESS)
1545 (void) vm_map_remove(map, *addr, *addr + size);
1549 * If the process has requested that all future mappings
1550 * be wired, then heed this.
1552 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1553 vm_map_wire(map, *addr, *addr + size,
1554 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1556 return (vm_mmap_to_errno(rv));
1560 vm_mmap_to_errno(int rv)
1566 case KERN_INVALID_ADDRESS:
1569 case KERN_PROTECTION_FAILURE: