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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
40 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
41 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
42 * $DragonFly: src/sys/vm/vm_mmap.c,v 1.2 2003/06/17 04:29:00 dillon Exp $
46 * Mapped file (mmap) interface to VM
49 #include "opt_compat.h"
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/sysproto.h>
55 #include <sys/filedesc.h>
57 #include <sys/resource.h>
58 #include <sys/resourcevar.h>
59 #include <sys/vnode.h>
60 #include <sys/fcntl.h>
65 #include <sys/vmmeter.h>
66 #include <sys/sysctl.h>
69 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_kern.h>
81 #ifndef _SYS_SYSPROTO_H_
87 static int max_proc_mmap;
88 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
91 * Set the maximum number of vm_map_entry structures per process. Roughly
92 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
93 * of our KVM malloc space still results in generous limits. We want a
94 * default that is good enough to prevent the kernel running out of resources
95 * if attacked from compromised user account but generous enough such that
96 * multi-threaded processes are not unduly inconvenienced.
99 static void vmmapentry_rsrc_init __P((void *));
100 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
103 vmmapentry_rsrc_init(dummy)
106 max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
107 max_proc_mmap /= 100;
114 struct sbrk_args *uap;
117 /* Not yet implemented */
121 #ifndef _SYS_SYSPROTO_H_
131 struct sstk_args *uap;
134 /* Not yet implemented */
138 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
139 #ifndef _SYS_SYSPROTO_H_
140 struct getpagesize_args {
149 struct getpagesize_args *uap;
152 p->p_retval[0] = PAGE_SIZE;
155 #endif /* COMPAT_43 || COMPAT_SUNOS */
159 * Memory Map (mmap) system call. Note that the file offset
160 * and address are allowed to be NOT page aligned, though if
161 * the MAP_FIXED flag it set, both must have the same remainder
162 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
163 * page-aligned, the actual mapping starts at trunc_page(addr)
164 * and the return value is adjusted up by the page offset.
166 * Generally speaking, only character devices which are themselves
167 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
168 * there would be no cache coherency between a descriptor and a VM mapping
169 * both to the same character device.
171 * Block devices can be mmap'd no matter what they represent. Cache coherency
172 * is maintained as long as you do not write directly to the underlying
175 #ifndef _SYS_SYSPROTO_H_
190 register struct mmap_args *uap;
192 register struct filedesc *fdp = p->p_fd;
193 register struct file *fp = NULL;
196 vm_size_t size, pageoff;
197 vm_prot_t prot, maxprot;
200 int disablexworkaround;
202 struct vmspace *vms = p->p_vmspace;
205 addr = (vm_offset_t) uap->addr;
207 prot = uap->prot & VM_PROT_ALL;
211 /* make sure mapping fits into numeric range etc */
212 if ((ssize_t) uap->len < 0 ||
213 ((flags & MAP_ANON) && uap->fd != -1))
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)
248 /* Address range must be all in user VM space. */
249 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
252 if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
255 if (addr + size < addr)
259 * XXX for non-fixed mappings where no hint is provided or
260 * the hint would fall in the potential heap space,
261 * place it after the end of the largest possible heap.
263 * There should really be a pmap call to determine a reasonable
266 else if (addr == 0 ||
267 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
268 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
269 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
271 if (flags & MAP_ANON) {
273 * Mapping blank space is trivial.
276 maxprot = VM_PROT_ALL;
280 * Mapping file, get fp for validation. Obtain vnode and make
281 * sure it is of appropriate type.
283 if (((unsigned) uap->fd) >= fdp->fd_nfiles ||
284 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
286 if (fp->f_type != DTYPE_VNODE)
289 * POSIX shared-memory objects are defined to have
290 * kernel persistence, and are not defined to support
291 * read(2)/write(2) -- or even open(2). Thus, we can
292 * use MAP_ASYNC to trade on-disk coherence for speed.
293 * The shm_open(3) library routine turns on the FPOSIXSHM
294 * flag to request this behavior.
296 if (fp->f_flag & FPOSIXSHM)
298 vp = (struct vnode *) fp->f_data;
299 if (vp->v_type != VREG && vp->v_type != VCHR)
301 if (vp->v_type == VREG) {
303 * Get the proper underlying object
305 if (VOP_GETVOBJECT(vp, &obj) != 0)
307 vp = (struct vnode*)obj->handle;
311 * don't let the descriptor disappear on us if we block
316 * XXX hack to handle use of /dev/zero to map anon memory (ala
319 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
321 maxprot = VM_PROT_ALL;
326 * cdevs does not provide private mappings of any kind.
329 * However, for XIG X server to continue to work,
330 * we should allow the superuser to do it anyway.
331 * We only allow it at securelevel < 1.
332 * (Because the XIG X server writes directly to video
333 * memory via /dev/mem, it should never work at any
335 * XXX this will have to go
337 if (securelevel >= 1)
338 disablexworkaround = 1;
340 disablexworkaround = suser(p);
341 if (vp->v_type == VCHR && disablexworkaround &&
342 (flags & (MAP_PRIVATE|MAP_COPY))) {
347 * Ensure that file and memory protections are
348 * compatible. Note that we only worry about
349 * writability if mapping is shared; in this case,
350 * current and max prot are dictated by the open file.
351 * XXX use the vnode instead? Problem is: what
352 * credentials do we use for determination? What if
353 * proc does a setuid?
355 maxprot = VM_PROT_EXECUTE; /* ??? */
356 if (fp->f_flag & FREAD) {
357 maxprot |= VM_PROT_READ;
358 } else if (prot & PROT_READ) {
363 * If we are sharing potential changes (either via
364 * MAP_SHARED or via the implicit sharing of character
365 * device mappings), and we are trying to get write
366 * permission although we opened it without asking
367 * for it, bail out. Check for superuser, only if
368 * we're at securelevel < 1, to allow the XIG X server
369 * to continue to work.
372 if ((flags & MAP_SHARED) != 0 ||
373 (vp->v_type == VCHR && disablexworkaround)) {
374 if ((fp->f_flag & FWRITE) != 0) {
382 (IMMUTABLE|APPEND)) == 0) {
383 maxprot |= VM_PROT_WRITE;
384 } else if (prot & PROT_WRITE) {
388 } else if ((prot & PROT_WRITE) != 0) {
393 maxprot |= VM_PROT_WRITE;
400 * Do not allow more then a certain number of vm_map_entry structures
401 * per process. Scale with the number of rforks sharing the map
402 * to make the limit reasonable for threads.
405 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
410 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
413 p->p_retval[0] = (register_t) (addr + pageoff);
421 #ifndef _SYS_SYSPROTO_H_
434 register struct ommap_args *uap;
436 struct mmap_args nargs;
437 static const char cvtbsdprot[8] = {
441 PROT_EXEC | PROT_WRITE,
443 PROT_EXEC | PROT_READ,
444 PROT_WRITE | PROT_READ,
445 PROT_EXEC | PROT_WRITE | PROT_READ,
448 #define OMAP_ANON 0x0002
449 #define OMAP_COPY 0x0020
450 #define OMAP_SHARED 0x0010
451 #define OMAP_FIXED 0x0100
452 #define OMAP_INHERIT 0x0800
454 nargs.addr = uap->addr;
455 nargs.len = uap->len;
456 nargs.prot = cvtbsdprot[uap->prot & 0x7];
458 if (uap->flags & OMAP_ANON)
459 nargs.flags |= MAP_ANON;
460 if (uap->flags & OMAP_COPY)
461 nargs.flags |= MAP_COPY;
462 if (uap->flags & OMAP_SHARED)
463 nargs.flags |= MAP_SHARED;
465 nargs.flags |= MAP_PRIVATE;
466 if (uap->flags & OMAP_FIXED)
467 nargs.flags |= MAP_FIXED;
468 if (uap->flags & OMAP_INHERIT)
469 nargs.flags |= MAP_INHERIT;
471 nargs.pos = uap->pos;
472 return (mmap(p, &nargs));
474 #endif /* COMPAT_43 */
477 #ifndef _SYS_SYSPROTO_H_
487 struct msync_args *uap;
490 vm_size_t size, pageoff;
495 addr = (vm_offset_t) uap->addr;
499 pageoff = (addr & PAGE_MASK);
502 size = (vm_size_t) round_page(size);
503 if (addr + size < addr)
506 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
509 map = &p->p_vmspace->vm_map;
512 * XXX Gak! If size is zero we are supposed to sync "all modified
513 * pages with the region containing addr". Unfortunately, we don't
514 * really keep track of individual mmaps so we approximate by flushing
515 * the range of the map entry containing addr. This can be incorrect
516 * if the region splits or is coalesced with a neighbor.
519 vm_map_entry_t entry;
521 vm_map_lock_read(map);
522 rv = vm_map_lookup_entry(map, addr, &entry);
523 vm_map_unlock_read(map);
527 size = entry->end - entry->start;
531 * Clean the pages and interpret the return value.
533 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
534 (flags & MS_INVALIDATE) != 0);
539 case KERN_INVALID_ADDRESS:
540 return (EINVAL); /* Sun returns ENOMEM? */
550 #ifndef _SYS_SYSPROTO_H_
558 register struct proc *p;
559 register struct munmap_args *uap;
562 vm_size_t size, pageoff;
565 addr = (vm_offset_t) uap->addr;
568 pageoff = (addr & PAGE_MASK);
571 size = (vm_size_t) round_page(size);
572 if (addr + size < addr)
579 * Check for illegal addresses. Watch out for address wrap... Note
580 * that VM_*_ADDRESS are not constants due to casts (argh).
582 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
585 if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
588 map = &p->p_vmspace->vm_map;
590 * Make sure entire range is allocated.
592 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
594 /* returns nothing but KERN_SUCCESS anyway */
595 (void) vm_map_remove(map, addr, addr + size);
606 * XXX should unmap any regions mapped to this file
608 p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
612 #ifndef _SYS_SYSPROTO_H_
613 struct mprotect_args {
622 struct mprotect_args *uap;
625 vm_size_t size, pageoff;
626 register vm_prot_t prot;
628 addr = (vm_offset_t) uap->addr;
630 prot = uap->prot & VM_PROT_ALL;
631 #if defined(VM_PROT_READ_IS_EXEC)
632 if (prot & VM_PROT_READ)
633 prot |= VM_PROT_EXECUTE;
636 pageoff = (addr & PAGE_MASK);
639 size = (vm_size_t) round_page(size);
640 if (addr + size < addr)
643 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
647 case KERN_PROTECTION_FAILURE:
653 #ifndef _SYS_SYSPROTO_H_
654 struct minherit_args {
663 struct minherit_args *uap;
666 vm_size_t size, pageoff;
667 register vm_inherit_t inherit;
669 addr = (vm_offset_t)uap->addr;
671 inherit = uap->inherit;
673 pageoff = (addr & PAGE_MASK);
676 size = (vm_size_t) round_page(size);
677 if (addr + size < addr)
680 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
684 case KERN_PROTECTION_FAILURE:
690 #ifndef _SYS_SYSPROTO_H_
691 struct madvise_args {
702 struct madvise_args *uap;
704 vm_offset_t start, end;
707 * Check for illegal behavior
709 if (uap->behav < 0 || uap->behav > MADV_CORE)
712 * Check for illegal addresses. Watch out for address wrap... Note
713 * that VM_*_ADDRESS are not constants due to casts (argh).
715 if (VM_MAXUSER_ADDRESS > 0 &&
716 ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS)
719 if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS)
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(&p->p_vmspace->vm_map, start, end, uap->behav))
737 #ifndef _SYS_SYSPROTO_H_
738 struct mincore_args {
749 struct mincore_args *uap;
751 vm_offset_t addr, first_addr;
752 vm_offset_t end, cend;
757 int vecindex, lastvecindex;
758 register vm_map_entry_t current;
759 vm_map_entry_t entry;
761 unsigned int timestamp;
764 * Make sure that the addresses presented are valid for user
767 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
768 end = addr + (vm_size_t)round_page(uap->len);
769 if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS)
775 * Address of byte vector
779 map = &p->p_vmspace->vm_map;
780 pmap = vmspace_pmap(p->p_vmspace);
782 vm_map_lock_read(map);
784 timestamp = map->timestamp;
786 if (!vm_map_lookup_entry(map, addr, &entry))
790 * Do this on a map entry basis so that if the pages are not
791 * in the current processes address space, we can easily look
792 * up the pages elsewhere.
796 (current != &map->header) && (current->start < end);
797 current = current->next) {
800 * ignore submaps (for now) or null objects
802 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
803 current->object.vm_object == NULL)
807 * limit this scan to the current map entry and the
808 * limits for the mincore call
810 if (addr < current->start)
811 addr = current->start;
817 * scan this entry one page at a time
821 * Check pmap first, it is likely faster, also
822 * it can provide info as to whether we are the
823 * one referencing or modifying the page.
825 mincoreinfo = pmap_mincore(pmap, addr);
831 * calculate the page index into the object
833 offset = current->offset + (addr - current->start);
834 pindex = OFF_TO_IDX(offset);
835 m = vm_page_lookup(current->object.vm_object,
838 * if the page is resident, then gather information about
842 mincoreinfo = MINCORE_INCORE;
845 mincoreinfo |= MINCORE_MODIFIED_OTHER;
846 if ((m->flags & PG_REFERENCED) ||
847 pmap_ts_referenced(m)) {
848 vm_page_flag_set(m, PG_REFERENCED);
849 mincoreinfo |= MINCORE_REFERENCED_OTHER;
855 * subyte may page fault. In case it needs to modify
856 * the map, we release the lock.
858 vm_map_unlock_read(map);
861 * calculate index into user supplied byte vector
863 vecindex = OFF_TO_IDX(addr - first_addr);
866 * If we have skipped map entries, we need to make sure that
867 * the byte vector is zeroed for those skipped entries.
869 while((lastvecindex + 1) < vecindex) {
870 error = subyte( vec + lastvecindex, 0);
878 * Pass the page information to the user
880 error = subyte( vec + vecindex, mincoreinfo);
886 * If the map has changed, due to the subyte, the previous
887 * output may be invalid.
889 vm_map_lock_read(map);
890 if (timestamp != map->timestamp)
893 lastvecindex = vecindex;
899 * subyte may page fault. In case it needs to modify
900 * the map, we release the lock.
902 vm_map_unlock_read(map);
905 * Zero the last entries in the byte vector.
907 vecindex = OFF_TO_IDX(end - first_addr);
908 while((lastvecindex + 1) < vecindex) {
909 error = subyte( vec + lastvecindex, 0);
917 * If the map has changed, due to the subyte, the previous
918 * output may be invalid.
920 vm_map_lock_read(map);
921 if (timestamp != map->timestamp)
923 vm_map_unlock_read(map);
928 #ifndef _SYS_SYSPROTO_H_
937 struct mlock_args *uap;
940 vm_size_t size, pageoff;
943 addr = (vm_offset_t) uap->addr;
946 pageoff = (addr & PAGE_MASK);
949 size = (vm_size_t) round_page(size);
951 /* disable wrap around */
952 if (addr + size < addr)
955 if (atop(size) + cnt.v_wire_count > vm_page_max_wired)
958 #ifdef pmap_wired_count
959 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
960 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
968 error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
969 return (error == KERN_SUCCESS ? 0 : ENOMEM);
972 #ifndef _SYS_SYSPROTO_H_
973 struct mlockall_args {
981 struct mlockall_args *uap;
986 #ifndef _SYS_SYSPROTO_H_
987 struct mlockall_args {
995 struct munlockall_args *uap;
1000 #ifndef _SYS_SYSPROTO_H_
1001 struct munlock_args {
1009 struct munlock_args *uap;
1012 vm_size_t size, pageoff;
1015 addr = (vm_offset_t) uap->addr;
1018 pageoff = (addr & PAGE_MASK);
1021 size = (vm_size_t) round_page(size);
1023 /* disable wrap around */
1024 if (addr + size < addr)
1027 #ifndef pmap_wired_count
1033 error = vm_map_user_pageable(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
1034 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1038 * Internal version of mmap.
1039 * Currently used by mmap, exec, and sys5 shared memory.
1040 * Handle is either a vnode pointer or NULL for MAP_ANON.
1043 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1044 vm_prot_t maxprot, int flags,
1050 struct vnode *vp = NULL;
1052 int rv = KERN_SUCCESS;
1053 vm_ooffset_t objsize;
1055 struct proc *p = curproc;
1060 objsize = size = round_page(size);
1062 if (p->p_vmspace->vm_map.size + size >
1063 p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
1068 * We currently can only deal with page aligned file offsets.
1069 * The check is here rather than in the syscall because the
1070 * kernel calls this function internally for other mmaping
1071 * operations (such as in exec) and non-aligned offsets will
1072 * cause pmap inconsistencies...so we want to be sure to
1073 * disallow this in all cases.
1075 if (foff & PAGE_MASK)
1078 if ((flags & MAP_FIXED) == 0) {
1080 *addr = round_page(*addr);
1082 if (*addr != trunc_page(*addr))
1085 (void) vm_map_remove(map, *addr, *addr + size);
1089 * Lookup/allocate object.
1091 if (flags & MAP_ANON) {
1092 type = OBJT_DEFAULT;
1094 * Unnamed anonymous regions always start at 0.
1099 vp = (struct vnode *) handle;
1100 if (vp->v_type == VCHR) {
1102 handle = (void *)(intptr_t)vp->v_rdev;
1107 error = VOP_GETATTR(vp, &vat, p->p_ucred, p);
1110 objsize = round_page(vat.va_size);
1113 * if it is a regular file without any references
1114 * we do not need to sync it.
1116 if (vp->v_type == VREG && vat.va_nlink == 0) {
1117 flags |= MAP_NOSYNC;
1122 if (handle == NULL) {
1126 object = vm_pager_allocate(type,
1127 handle, objsize, prot, foff);
1129 return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1130 docow = MAP_PREFAULT_PARTIAL;
1134 * Force device mappings to be shared.
1136 if (type == OBJT_DEVICE || type == OBJT_PHYS) {
1137 flags &= ~(MAP_PRIVATE|MAP_COPY);
1138 flags |= MAP_SHARED;
1141 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1142 docow |= MAP_COPY_ON_WRITE;
1143 if (flags & MAP_NOSYNC)
1144 docow |= MAP_DISABLE_SYNCER;
1145 if (flags & MAP_NOCORE)
1146 docow |= MAP_DISABLE_COREDUMP;
1148 #if defined(VM_PROT_READ_IS_EXEC)
1149 if (prot & VM_PROT_READ)
1150 prot |= VM_PROT_EXECUTE;
1152 if (maxprot & VM_PROT_READ)
1153 maxprot |= VM_PROT_EXECUTE;
1157 *addr = pmap_addr_hint(object, *addr, size);
1160 if (flags & MAP_STACK)
1161 rv = vm_map_stack (map, *addr, size, prot,
1164 rv = vm_map_find(map, object, foff, addr, size, fitit,
1165 prot, maxprot, docow);
1167 if (rv != KERN_SUCCESS) {
1169 * Lose the object reference. Will destroy the
1170 * object if it's an unnamed anonymous mapping
1171 * or named anonymous without other references.
1173 vm_object_deallocate(object);
1178 * Shared memory is also shared with children.
1180 if (flags & (MAP_SHARED|MAP_INHERIT)) {
1181 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1182 if (rv != KERN_SUCCESS) {
1183 (void) vm_map_remove(map, *addr, *addr + size);
1191 case KERN_INVALID_ADDRESS:
1194 case KERN_PROTECTION_FAILURE: