2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
40 * Mapped file (mmap) interface to VM
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
47 #include "opt_hwpmc_hooks.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capability.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/filedesc.h>
59 #include <sys/racct.h>
60 #include <sys/resource.h>
61 #include <sys/resourcevar.h>
62 #include <sys/vnode.h>
63 #include <sys/fcntl.h>
66 #include <sys/mount.h>
69 #include <sys/sysent.h>
70 #include <sys/vmmeter.h>
72 #include <security/mac/mac_framework.h>
75 #include <vm/vm_param.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vm_pageout.h>
82 #include <vm/vm_extern.h>
83 #include <vm/vm_page.h>
84 #include <vm/vnode_pager.h>
87 #include <sys/pmckern.h>
90 #ifndef _SYS_SYSPROTO_H_
96 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
97 int *, struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
98 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
99 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
100 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
101 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
110 struct sbrk_args *uap;
112 /* Not yet implemented */
116 #ifndef _SYS_SYSPROTO_H_
129 struct sstk_args *uap;
131 /* Not yet implemented */
135 #if defined(COMPAT_43)
136 #ifndef _SYS_SYSPROTO_H_
137 struct getpagesize_args {
144 ogetpagesize(td, uap)
146 struct getpagesize_args *uap;
149 td->td_retval[0] = PAGE_SIZE;
152 #endif /* COMPAT_43 */
156 * Memory Map (mmap) system call. Note that the file offset
157 * and address are allowed to be NOT page aligned, though if
158 * the MAP_FIXED flag it set, both must have the same remainder
159 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
160 * page-aligned, the actual mapping starts at trunc_page(addr)
161 * and the return value is adjusted up by the page offset.
163 * Generally speaking, only character devices which are themselves
164 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
165 * there would be no cache coherency between a descriptor and a VM mapping
166 * both to the same character device.
168 #ifndef _SYS_SYSPROTO_H_
186 struct mmap_args *uap;
189 struct pmckern_map_in pkm;
194 vm_size_t size, pageoff;
195 vm_prot_t cap_maxprot, prot, maxprot;
197 objtype_t handle_type;
200 struct vmspace *vms = td->td_proc->p_vmspace;
203 addr = (vm_offset_t) uap->addr;
205 prot = uap->prot & VM_PROT_ALL;
211 /* Make sure mapping fits into numeric range, etc. */
212 if ((uap->len == 0 && !SV_CURPROC_FLAG(SV_AOUT) &&
213 curproc->p_osrel >= P_OSREL_MAP_ANON) ||
214 ((flags & MAP_ANON) && (uap->fd != -1 || pos != 0)))
217 if (flags & MAP_STACK) {
218 if ((uap->fd != -1) ||
219 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
226 * Align the file position to a page boundary,
227 * and save its page offset component.
229 pageoff = (pos & PAGE_MASK);
232 /* Adjust size for rounding (on both ends). */
233 size += pageoff; /* low end... */
234 size = (vm_size_t) round_page(size); /* hi end */
237 * Check for illegal addresses. Watch out for address wrap... Note
238 * that VM_*_ADDRESS are not constants due to casts (argh).
240 if (flags & MAP_FIXED) {
242 * The specified address must have the same remainder
243 * as the file offset taken modulo PAGE_SIZE, so it
244 * should be aligned after adjustment by pageoff.
247 if (addr & PAGE_MASK)
250 /* Address range must be all in user VM space. */
251 if (addr < vm_map_min(&vms->vm_map) ||
252 addr + size > vm_map_max(&vms->vm_map))
254 if (addr + size < addr)
258 * XXX for non-fixed mappings where no hint is provided or
259 * the hint would fall in the potential heap space,
260 * place it after the end of the largest possible heap.
262 * There should really be a pmap call to determine a reasonable
265 PROC_LOCK(td->td_proc);
267 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
268 addr < round_page((vm_offset_t)vms->vm_daddr +
269 lim_max(td->td_proc, RLIMIT_DATA))))
270 addr = round_page((vm_offset_t)vms->vm_daddr +
271 lim_max(td->td_proc, RLIMIT_DATA));
272 PROC_UNLOCK(td->td_proc);
274 if (flags & MAP_ANON) {
276 * Mapping blank space is trivial.
279 handle_type = OBJT_DEFAULT;
280 maxprot = VM_PROT_ALL;
281 cap_maxprot = VM_PROT_ALL;
284 * Mapping file, get fp for validation and don't let the
285 * descriptor disappear on us if we block. Check capability
286 * rights, but also return the maximum rights to be combined
287 * with maxprot later.
290 if (prot & PROT_READ)
292 if ((flags & MAP_SHARED) != 0) {
293 if (prot & PROT_WRITE)
296 if (prot & PROT_EXEC)
297 rights |= CAP_MAPEXEC;
298 if ((error = fget_mmap(td, uap->fd, rights, &cap_maxprot,
301 if (fp->f_type == DTYPE_SHM) {
303 handle_type = OBJT_SWAP;
304 maxprot = VM_PROT_NONE;
306 /* FREAD should always be set. */
307 if (fp->f_flag & FREAD)
308 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
309 if (fp->f_flag & FWRITE)
310 maxprot |= VM_PROT_WRITE;
313 if (fp->f_type != DTYPE_VNODE) {
317 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
318 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
320 * POSIX shared-memory objects are defined to have
321 * kernel persistence, and are not defined to support
322 * read(2)/write(2) -- or even open(2). Thus, we can
323 * use MAP_ASYNC to trade on-disk coherence for speed.
324 * The shm_open(3) library routine turns on the FPOSIXSHM
325 * flag to request this behavior.
327 if (fp->f_flag & FPOSIXSHM)
332 * Ensure that file and memory protections are
333 * compatible. Note that we only worry about
334 * writability if mapping is shared; in this case,
335 * current and max prot are dictated by the open file.
336 * XXX use the vnode instead? Problem is: what
337 * credentials do we use for determination? What if
338 * proc does a setuid?
340 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
341 maxprot = VM_PROT_NONE;
343 maxprot = VM_PROT_EXECUTE;
344 if (fp->f_flag & FREAD) {
345 maxprot |= VM_PROT_READ;
346 } else if (prot & PROT_READ) {
351 * If we are sharing potential changes (either via
352 * MAP_SHARED or via the implicit sharing of character
353 * device mappings), and we are trying to get write
354 * permission although we opened it without asking
357 if ((flags & MAP_SHARED) != 0) {
358 if ((fp->f_flag & FWRITE) != 0) {
359 maxprot |= VM_PROT_WRITE;
360 } else if ((prot & PROT_WRITE) != 0) {
364 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
365 maxprot |= VM_PROT_WRITE;
366 cap_maxprot |= VM_PROT_WRITE;
369 handle_type = OBJT_VNODE;
373 maxprot &= cap_maxprot;
374 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
375 flags, handle_type, handle, pos);
378 /* inform hwpmc(4) if an executable is being mapped */
379 if (error == 0 && handle_type == OBJT_VNODE &&
380 (prot & PROT_EXEC)) {
381 pkm.pm_file = handle;
382 pkm.pm_address = (uintptr_t) addr;
383 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
387 td->td_retval[0] = (register_t) (addr + pageoff);
396 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
398 struct mmap_args oargs;
400 oargs.addr = uap->addr;
401 oargs.len = uap->len;
402 oargs.prot = uap->prot;
403 oargs.flags = uap->flags;
405 oargs.pos = uap->pos;
406 return (sys_mmap(td, &oargs));
410 #ifndef _SYS_SYSPROTO_H_
423 struct ommap_args *uap;
425 struct mmap_args nargs;
426 static const char cvtbsdprot[8] = {
430 PROT_EXEC | PROT_WRITE,
432 PROT_EXEC | PROT_READ,
433 PROT_WRITE | PROT_READ,
434 PROT_EXEC | PROT_WRITE | PROT_READ,
437 #define OMAP_ANON 0x0002
438 #define OMAP_COPY 0x0020
439 #define OMAP_SHARED 0x0010
440 #define OMAP_FIXED 0x0100
442 nargs.addr = uap->addr;
443 nargs.len = uap->len;
444 nargs.prot = cvtbsdprot[uap->prot & 0x7];
446 if (uap->flags & OMAP_ANON)
447 nargs.flags |= MAP_ANON;
448 if (uap->flags & OMAP_COPY)
449 nargs.flags |= MAP_COPY;
450 if (uap->flags & OMAP_SHARED)
451 nargs.flags |= MAP_SHARED;
453 nargs.flags |= MAP_PRIVATE;
454 if (uap->flags & OMAP_FIXED)
455 nargs.flags |= MAP_FIXED;
457 nargs.pos = uap->pos;
458 return (sys_mmap(td, &nargs));
460 #endif /* COMPAT_43 */
463 #ifndef _SYS_SYSPROTO_H_
476 struct msync_args *uap;
479 vm_size_t size, pageoff;
484 addr = (vm_offset_t) uap->addr;
488 pageoff = (addr & PAGE_MASK);
491 size = (vm_size_t) round_page(size);
492 if (addr + size < addr)
495 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
498 map = &td->td_proc->p_vmspace->vm_map;
501 * Clean the pages and interpret the return value.
503 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
504 (flags & MS_INVALIDATE) != 0);
508 case KERN_INVALID_ADDRESS:
509 return (EINVAL); /* Sun returns ENOMEM? */
510 case KERN_INVALID_ARGUMENT:
519 #ifndef _SYS_SYSPROTO_H_
531 struct munmap_args *uap;
534 struct pmckern_map_out pkm;
535 vm_map_entry_t entry;
538 vm_size_t size, pageoff;
541 addr = (vm_offset_t) uap->addr;
546 pageoff = (addr & PAGE_MASK);
549 size = (vm_size_t) round_page(size);
550 if (addr + size < addr)
554 * Check for illegal addresses. Watch out for address wrap...
556 map = &td->td_proc->p_vmspace->vm_map;
557 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
562 * Inform hwpmc if the address range being unmapped contains
563 * an executable region.
565 pkm.pm_address = (uintptr_t) NULL;
566 if (vm_map_lookup_entry(map, addr, &entry)) {
568 entry != &map->header && entry->start < addr + size;
569 entry = entry->next) {
570 if (vm_map_check_protection(map, entry->start,
571 entry->end, VM_PROT_EXECUTE) == TRUE) {
572 pkm.pm_address = (uintptr_t) addr;
573 pkm.pm_size = (size_t) size;
579 vm_map_delete(map, addr, addr + size);
582 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
583 vm_map_lock_downgrade(map);
584 if (pkm.pm_address != (uintptr_t) NULL)
585 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
586 vm_map_unlock_read(map);
590 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
594 #ifndef _SYS_SYSPROTO_H_
595 struct mprotect_args {
605 sys_mprotect(td, uap)
607 struct mprotect_args *uap;
610 vm_size_t size, pageoff;
613 addr = (vm_offset_t) uap->addr;
615 prot = uap->prot & VM_PROT_ALL;
617 pageoff = (addr & PAGE_MASK);
620 size = (vm_size_t) round_page(size);
621 if (addr + size < addr)
624 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
625 addr + size, prot, FALSE)) {
628 case KERN_PROTECTION_FAILURE:
630 case KERN_RESOURCE_SHORTAGE:
636 #ifndef _SYS_SYSPROTO_H_
637 struct minherit_args {
647 sys_minherit(td, uap)
649 struct minherit_args *uap;
652 vm_size_t size, pageoff;
653 vm_inherit_t inherit;
655 addr = (vm_offset_t)uap->addr;
657 inherit = uap->inherit;
659 pageoff = (addr & PAGE_MASK);
662 size = (vm_size_t) round_page(size);
663 if (addr + size < addr)
666 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
667 addr + size, inherit)) {
670 case KERN_PROTECTION_FAILURE:
676 #ifndef _SYS_SYSPROTO_H_
677 struct madvise_args {
691 struct madvise_args *uap;
693 vm_offset_t start, end;
699 * Check for our special case, advising the swap pager we are
702 if (uap->behav == MADV_PROTECT) {
703 error = priv_check(td, PRIV_VM_MADV_PROTECT);
707 p->p_flag |= P_PROTECTED;
713 * Check for illegal behavior
715 if (uap->behav < 0 || uap->behav > MADV_CORE)
718 * Check for illegal addresses. Watch out for address wrap... Note
719 * that VM_*_ADDRESS are not constants due to casts (argh).
721 map = &td->td_proc->p_vmspace->vm_map;
722 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
723 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
725 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
729 * Since this routine is only advisory, we default to conservative
732 start = trunc_page((vm_offset_t) uap->addr);
733 end = round_page((vm_offset_t) uap->addr + uap->len);
735 if (vm_map_madvise(map, start, end, uap->behav))
740 #ifndef _SYS_SYSPROTO_H_
741 struct mincore_args {
755 struct mincore_args *uap;
757 vm_offset_t addr, first_addr;
758 vm_offset_t end, cend;
763 int vecindex, lastvecindex;
764 vm_map_entry_t current;
765 vm_map_entry_t entry;
767 vm_paddr_t locked_pa;
771 unsigned int timestamp;
775 * Make sure that the addresses presented are valid for user
778 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
779 end = addr + (vm_size_t)round_page(uap->len);
780 map = &td->td_proc->p_vmspace->vm_map;
781 if (end > vm_map_max(map) || end < addr)
785 * Address of byte vector
789 pmap = vmspace_pmap(td->td_proc->p_vmspace);
791 vm_map_lock_read(map);
793 timestamp = map->timestamp;
795 if (!vm_map_lookup_entry(map, addr, &entry)) {
796 vm_map_unlock_read(map);
801 * Do this on a map entry basis so that if the pages are not
802 * in the current processes address space, we can easily look
803 * up the pages elsewhere.
806 for (current = entry;
807 (current != &map->header) && (current->start < end);
808 current = current->next) {
811 * check for contiguity
813 if (current->end < end &&
814 (entry->next == &map->header ||
815 current->next->start > current->end)) {
816 vm_map_unlock_read(map);
821 * ignore submaps (for now) or null objects
823 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
824 current->object.vm_object == NULL)
828 * limit this scan to the current map entry and the
829 * limits for the mincore call
831 if (addr < current->start)
832 addr = current->start;
838 * scan this entry one page at a time
840 while (addr < cend) {
842 * Check pmap first, it is likely faster, also
843 * it can provide info as to whether we are the
844 * one referencing or modifying the page.
850 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
851 if (locked_pa != 0) {
853 * The page is mapped by this process but not
854 * both accessed and modified. It is also
855 * managed. Acquire the object lock so that
856 * other mappings might be examined.
858 m = PHYS_TO_VM_PAGE(locked_pa);
859 if (m->object != object) {
861 VM_OBJECT_UNLOCK(object);
863 locked = VM_OBJECT_TRYLOCK(object);
866 VM_OBJECT_LOCK(object);
872 KASSERT(m->valid == VM_PAGE_BITS_ALL,
873 ("mincore: page %p is mapped but invalid",
875 } else if (mincoreinfo == 0) {
877 * The page is not mapped by this process. If
878 * the object implements managed pages, then
879 * determine if the page is resident so that
880 * the mappings might be examined.
882 if (current->object.vm_object != object) {
884 VM_OBJECT_UNLOCK(object);
885 object = current->object.vm_object;
886 VM_OBJECT_LOCK(object);
888 if (object->type == OBJT_DEFAULT ||
889 object->type == OBJT_SWAP ||
890 object->type == OBJT_VNODE) {
891 pindex = OFF_TO_IDX(current->offset +
892 (addr - current->start));
893 m = vm_page_lookup(object, pindex);
895 vm_page_is_cached(object, pindex))
896 mincoreinfo = MINCORE_INCORE;
897 if (m != NULL && m->valid == 0)
900 mincoreinfo = MINCORE_INCORE;
904 /* Examine other mappings to the page. */
905 if (m->dirty == 0 && pmap_is_modified(m))
908 mincoreinfo |= MINCORE_MODIFIED_OTHER;
910 * The first test for PGA_REFERENCED is an
911 * optimization. The second test is
912 * required because a concurrent pmap
913 * operation could clear the last reference
914 * and set PGA_REFERENCED before the call to
915 * pmap_is_referenced().
917 if ((m->aflags & PGA_REFERENCED) != 0 ||
918 pmap_is_referenced(m) ||
919 (m->aflags & PGA_REFERENCED) != 0)
920 mincoreinfo |= MINCORE_REFERENCED_OTHER;
923 VM_OBJECT_UNLOCK(object);
926 * subyte may page fault. In case it needs to modify
927 * the map, we release the lock.
929 vm_map_unlock_read(map);
932 * calculate index into user supplied byte vector
934 vecindex = OFF_TO_IDX(addr - first_addr);
937 * If we have skipped map entries, we need to make sure that
938 * the byte vector is zeroed for those skipped entries.
940 while ((lastvecindex + 1) < vecindex) {
941 error = subyte(vec + lastvecindex, 0);
950 * Pass the page information to the user
952 error = subyte(vec + vecindex, mincoreinfo);
959 * If the map has changed, due to the subyte, the previous
960 * output may be invalid.
962 vm_map_lock_read(map);
963 if (timestamp != map->timestamp)
966 lastvecindex = vecindex;
972 * subyte may page fault. In case it needs to modify
973 * the map, we release the lock.
975 vm_map_unlock_read(map);
978 * Zero the last entries in the byte vector.
980 vecindex = OFF_TO_IDX(end - first_addr);
981 while ((lastvecindex + 1) < vecindex) {
982 error = subyte(vec + lastvecindex, 0);
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)
997 vm_map_unlock_read(map);
1002 #ifndef _SYS_SYSPROTO_H_
1014 struct mlock_args *uap;
1017 vm_offset_t addr, end, last, start;
1018 vm_size_t npages, size;
1019 unsigned long nsize;
1022 error = priv_check(td, PRIV_VM_MLOCK);
1025 addr = (vm_offset_t)uap->addr;
1028 start = trunc_page(addr);
1029 end = round_page(last);
1030 if (last < addr || end < addr)
1032 npages = atop(end - start);
1033 if (npages > vm_page_max_wired)
1037 nsize = ptoa(npages +
1038 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map)));
1039 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1044 if (npages + cnt.v_wire_count > vm_page_max_wired)
1048 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1053 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
1054 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1056 if (error != KERN_SUCCESS) {
1058 racct_set(proc, RACCT_MEMLOCK,
1059 ptoa(pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))));
1063 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1066 #ifndef _SYS_SYSPROTO_H_
1067 struct mlockall_args {
1076 sys_mlockall(td, uap)
1078 struct mlockall_args *uap;
1083 map = &td->td_proc->p_vmspace->vm_map;
1086 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1091 * If wiring all pages in the process would cause it to exceed
1092 * a hard resource limit, return ENOMEM.
1094 PROC_LOCK(td->td_proc);
1095 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1096 PROC_UNLOCK(td->td_proc);
1099 PROC_UNLOCK(td->td_proc);
1101 error = priv_check(td, PRIV_VM_MLOCK);
1106 PROC_LOCK(td->td_proc);
1107 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1108 PROC_UNLOCK(td->td_proc);
1113 if (uap->how & MCL_FUTURE) {
1115 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1120 if (uap->how & MCL_CURRENT) {
1122 * P1003.1-2001 mandates that all currently mapped pages
1123 * will be memory resident and locked (wired) upon return
1124 * from mlockall(). vm_map_wire() will wire pages, by
1125 * calling vm_fault_wire() for each page in the region.
1127 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1128 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1129 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1132 if (error != KERN_SUCCESS) {
1133 PROC_LOCK(td->td_proc);
1134 racct_set(td->td_proc, RACCT_MEMLOCK,
1135 ptoa(pmap_wired_count(vm_map_pmap(&td->td_proc->p_vmspace->vm_map))));
1136 PROC_UNLOCK(td->td_proc);
1143 #ifndef _SYS_SYSPROTO_H_
1144 struct munlockall_args {
1153 sys_munlockall(td, uap)
1155 struct munlockall_args *uap;
1160 map = &td->td_proc->p_vmspace->vm_map;
1161 error = priv_check(td, PRIV_VM_MUNLOCK);
1165 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1167 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1170 /* Forcibly unwire all pages. */
1171 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1172 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1174 if (error == KERN_SUCCESS) {
1175 PROC_LOCK(td->td_proc);
1176 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1177 PROC_UNLOCK(td->td_proc);
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct munlock_args {
1194 sys_munlock(td, uap)
1196 struct munlock_args *uap;
1198 vm_offset_t addr, end, last, start;
1202 error = priv_check(td, PRIV_VM_MUNLOCK);
1205 addr = (vm_offset_t)uap->addr;
1208 start = trunc_page(addr);
1209 end = round_page(last);
1210 if (last < addr || end < addr)
1212 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1213 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1215 if (error == KERN_SUCCESS) {
1216 PROC_LOCK(td->td_proc);
1217 racct_sub(td->td_proc, RACCT_MEMLOCK, ptoa(end - start));
1218 PROC_UNLOCK(td->td_proc);
1221 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1227 * Helper function for vm_mmap. Perform sanity check specific for mmap
1228 * operations on vnodes.
1230 * For VCHR vnodes, the vnode lock is held over the call to
1231 * vm_mmap_cdev() to keep vp->v_rdev valid.
1234 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1235 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1236 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1237 boolean_t *writecounted)
1244 int error, flags, locktype, vfslocked;
1247 cred = td->td_ucred;
1248 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1249 locktype = LK_EXCLUSIVE;
1251 locktype = LK_SHARED;
1252 vfslocked = VFS_LOCK_GIANT(mp);
1253 if ((error = vget(vp, locktype, td)) != 0) {
1254 VFS_UNLOCK_GIANT(vfslocked);
1260 if (vp->v_type == VREG) {
1262 * Get the proper underlying object
1268 if (obj->handle != vp) {
1270 vp = (struct vnode *)obj->handle;
1272 * Bypass filesystems obey the mpsafety of the
1275 error = vget(vp, locktype, td);
1277 VFS_UNLOCK_GIANT(vfslocked);
1281 if (locktype == LK_EXCLUSIVE) {
1282 *writecounted = TRUE;
1283 vnode_pager_update_writecount(obj, 0, objsize);
1285 } else if (vp->v_type == VCHR) {
1286 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1287 vp->v_rdev, foffp, objp);
1295 if ((error = VOP_GETATTR(vp, &va, cred)))
1298 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1302 if ((flags & MAP_SHARED) != 0) {
1303 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1304 if (prot & PROT_WRITE) {
1308 *maxprotp &= ~VM_PROT_WRITE;
1312 * If it is a regular file without any references
1313 * we do not need to sync it.
1314 * Adjust object size to be the size of actual file.
1316 objsize = round_page(va.va_size);
1317 if (va.va_nlink == 0)
1318 flags |= MAP_NOSYNC;
1319 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, cred);
1328 vfs_mark_atime(vp, cred);
1332 VFS_UNLOCK_GIANT(vfslocked);
1341 * Helper function for vm_mmap. Perform sanity check specific for mmap
1342 * operations on cdevs.
1345 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1346 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1347 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1351 int error, flags, ref;
1355 dsw = dev_refthread(cdev, &ref);
1358 if (dsw->d_flags & D_MMAP_ANON) {
1359 dev_relthread(cdev, ref);
1360 *maxprotp = VM_PROT_ALL;
1361 *flagsp |= MAP_ANON;
1365 * cdevs do not provide private mappings of any kind.
1367 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1368 (prot & PROT_WRITE) != 0) {
1369 dev_relthread(cdev, ref);
1372 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1373 dev_relthread(cdev, ref);
1377 * Force device mappings to be shared.
1379 flags |= MAP_SHARED;
1381 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1383 dev_relthread(cdev, ref);
1388 * First, try d_mmap_single(). If that is not implemented
1389 * (returns ENODEV), fall back to using the device pager.
1390 * Note that d_mmap_single() must return a reference to the
1391 * object (it needs to bump the reference count of the object
1392 * it returns somehow).
1394 * XXX assumes VM_PROT_* == PROT_*
1396 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1397 dev_relthread(cdev, ref);
1398 if (error != ENODEV)
1400 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1414 * Helper function for vm_mmap. Perform sanity check specific for mmap
1415 * operations on shm file descriptors.
1418 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1419 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1420 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1424 if ((*flagsp & MAP_SHARED) != 0 &&
1425 (*maxprotp & VM_PROT_WRITE) == 0 &&
1426 (prot & PROT_WRITE) != 0)
1429 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1433 error = shm_mmap(shmfd, objsize, foff, objp);
1444 * Internal version of mmap. Currently used by mmap, exec, and sys5
1445 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1448 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1449 vm_prot_t maxprot, int flags,
1450 objtype_t handle_type, void *handle,
1454 vm_object_t object = NULL;
1455 struct thread *td = curthread;
1456 int docow, error, rv;
1457 boolean_t writecounted;
1462 size = round_page(size);
1464 PROC_LOCK(td->td_proc);
1465 if (td->td_proc->p_vmspace->vm_map.size + size >
1466 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1467 PROC_UNLOCK(td->td_proc);
1470 if (racct_set(td->td_proc, RACCT_VMEM,
1471 td->td_proc->p_vmspace->vm_map.size + size)) {
1472 PROC_UNLOCK(td->td_proc);
1475 PROC_UNLOCK(td->td_proc);
1478 * We currently can only deal with page aligned file offsets.
1479 * The check is here rather than in the syscall because the
1480 * kernel calls this function internally for other mmaping
1481 * operations (such as in exec) and non-aligned offsets will
1482 * cause pmap inconsistencies...so we want to be sure to
1483 * disallow this in all cases.
1485 if (foff & PAGE_MASK)
1488 if ((flags & MAP_FIXED) == 0) {
1490 *addr = round_page(*addr);
1492 if (*addr != trunc_page(*addr))
1496 writecounted = FALSE;
1499 * Lookup/allocate object.
1501 switch (handle_type) {
1503 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1504 handle, &foff, &object);
1507 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1508 handle, &foff, &object, &writecounted);
1511 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1512 handle, foff, &object);
1515 if (handle == NULL) {
1526 if (flags & MAP_ANON) {
1530 * Unnamed anonymous regions always start at 0.
1534 } else if (flags & MAP_PREFAULT_READ)
1535 docow = MAP_PREFAULT;
1537 docow = MAP_PREFAULT_PARTIAL;
1539 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1540 docow |= MAP_COPY_ON_WRITE;
1541 if (flags & MAP_NOSYNC)
1542 docow |= MAP_DISABLE_SYNCER;
1543 if (flags & MAP_NOCORE)
1544 docow |= MAP_DISABLE_COREDUMP;
1545 /* Shared memory is also shared with children. */
1546 if (flags & MAP_SHARED)
1547 docow |= MAP_INHERIT_SHARE;
1549 docow |= MAP_VN_WRITECOUNT;
1551 if (flags & MAP_STACK)
1552 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1553 docow | MAP_STACK_GROWS_DOWN);
1555 rv = vm_map_find(map, object, foff, addr, size,
1556 object != NULL && object->type == OBJT_DEVICE ?
1557 VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1559 rv = vm_map_fixed(map, object, foff, *addr, size,
1560 prot, maxprot, docow);
1562 if (rv == KERN_SUCCESS) {
1564 * If the process has requested that all future mappings
1565 * be wired, then heed this.
1567 if (map->flags & MAP_WIREFUTURE) {
1568 vm_map_wire(map, *addr, *addr + size,
1569 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1570 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1574 * If this mapping was accounted for in the vnode's
1575 * writecount, then undo that now.
1578 vnode_pager_release_writecount(object, 0, size);
1580 * Lose the object reference. Will destroy the
1581 * object if it's an unnamed anonymous mapping
1582 * or named anonymous without other references.
1584 vm_object_deallocate(object);
1586 return (vm_mmap_to_errno(rv));
1590 * Translate a Mach VM return code to zero on success or the appropriate errno
1594 vm_mmap_to_errno(int rv)
1600 case KERN_INVALID_ADDRESS:
1603 case KERN_PROTECTION_FAILURE: