]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_mmap.c
Merge ath again (addition of wisoc files).
[FreeBSD/FreeBSD.git] / sys / vm / vm_mmap.c
1 /*-
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1991, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
35  *
36  *      @(#)vm_mmap.c   8.4 (Berkeley) 1/12/94
37  */
38
39 /*
40  * Mapped file (mmap) interface to VM
41  */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include "opt_compat.h"
47 #include "opt_hwpmc_hooks.h"
48 #include "opt_mac.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/filedesc.h>
57 #include <sys/priv.h>
58 #include <sys/proc.h>
59 #include <sys/resource.h>
60 #include <sys/resourcevar.h>
61 #include <sys/vnode.h>
62 #include <sys/fcntl.h>
63 #include <sys/file.h>
64 #include <sys/mman.h>
65 #include <sys/mount.h>
66 #include <sys/conf.h>
67 #include <sys/stat.h>
68 #include <sys/vmmeter.h>
69 #include <sys/sysctl.h>
70
71 #include <security/mac/mac_framework.h>
72
73 #include <vm/vm.h>
74 #include <vm/vm_param.h>
75 #include <vm/pmap.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
80 #include <vm/vm_pageout.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_kern.h>
84
85 #ifdef HWPMC_HOOKS
86 #include <sys/pmckern.h>
87 #endif
88
89 #ifndef _SYS_SYSPROTO_H_
90 struct sbrk_args {
91         int incr;
92 };
93 #endif
94
95 static int max_proc_mmap;
96 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0,
97     "Maximum number of memory-mapped files per process");
98
99 /*
100  * Set the maximum number of vm_map_entry structures per process.  Roughly
101  * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
102  * of our KVM malloc space still results in generous limits.  We want a
103  * default that is good enough to prevent the kernel running out of resources
104  * if attacked from compromised user account but generous enough such that
105  * multi-threaded processes are not unduly inconvenienced.
106  */
107 static void vmmapentry_rsrc_init(void *);
108 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init,
109     NULL);
110
111 static void
112 vmmapentry_rsrc_init(dummy)
113         void *dummy;
114 {
115     max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
116     max_proc_mmap /= 100;
117 }
118
119 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
120     int *, struct vnode *, vm_ooffset_t, vm_object_t *);
121 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
122     int *, struct cdev *, vm_ooffset_t, vm_object_t *);
123 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
124     int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
125
126 /*
127  * MPSAFE
128  */
129 /* ARGSUSED */
130 int
131 sbrk(td, uap)
132         struct thread *td;
133         struct sbrk_args *uap;
134 {
135         /* Not yet implemented */
136         return (EOPNOTSUPP);
137 }
138
139 #ifndef _SYS_SYSPROTO_H_
140 struct sstk_args {
141         int incr;
142 };
143 #endif
144
145 /*
146  * MPSAFE
147  */
148 /* ARGSUSED */
149 int
150 sstk(td, uap)
151         struct thread *td;
152         struct sstk_args *uap;
153 {
154         /* Not yet implemented */
155         return (EOPNOTSUPP);
156 }
157
158 #if defined(COMPAT_43)
159 #ifndef _SYS_SYSPROTO_H_
160 struct getpagesize_args {
161         int dummy;
162 };
163 #endif
164
165 /* ARGSUSED */
166 int
167 ogetpagesize(td, uap)
168         struct thread *td;
169         struct getpagesize_args *uap;
170 {
171         /* MP SAFE */
172         td->td_retval[0] = PAGE_SIZE;
173         return (0);
174 }
175 #endif                          /* COMPAT_43 */
176
177
178 /*
179  * Memory Map (mmap) system call.  Note that the file offset
180  * and address are allowed to be NOT page aligned, though if
181  * the MAP_FIXED flag it set, both must have the same remainder
182  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
183  * page-aligned, the actual mapping starts at trunc_page(addr)
184  * and the return value is adjusted up by the page offset.
185  *
186  * Generally speaking, only character devices which are themselves
187  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
188  * there would be no cache coherency between a descriptor and a VM mapping
189  * both to the same character device.
190  *
191  * Block devices can be mmap'd no matter what they represent.  Cache coherency
192  * is maintained as long as you do not write directly to the underlying
193  * character device.
194  */
195 #ifndef _SYS_SYSPROTO_H_
196 struct mmap_args {
197         void *addr;
198         size_t len;
199         int prot;
200         int flags;
201         int fd;
202         long pad;
203         off_t pos;
204 };
205 #endif
206
207 /*
208  * MPSAFE
209  */
210 int
211 mmap(td, uap)
212         struct thread *td;
213         struct mmap_args *uap;
214 {
215 #ifdef HWPMC_HOOKS
216         struct pmckern_map_in pkm;
217 #endif
218         struct file *fp;
219         struct vnode *vp;
220         vm_offset_t addr;
221         vm_size_t size, pageoff;
222         vm_prot_t prot, maxprot;
223         void *handle;
224         objtype_t handle_type;
225         int flags, error;
226         off_t pos;
227         struct vmspace *vms = td->td_proc->p_vmspace;
228
229         addr = (vm_offset_t) uap->addr;
230         size = uap->len;
231         prot = uap->prot & VM_PROT_ALL;
232         flags = uap->flags;
233         pos = uap->pos;
234
235         fp = NULL;
236         /* make sure mapping fits into numeric range etc */
237         if ((ssize_t) uap->len < 0 ||
238             ((flags & MAP_ANON) && uap->fd != -1))
239                 return (EINVAL);
240
241         if (flags & MAP_STACK) {
242                 if ((uap->fd != -1) ||
243                     ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
244                         return (EINVAL);
245                 flags |= MAP_ANON;
246                 pos = 0;
247         }
248
249         /*
250          * Align the file position to a page boundary,
251          * and save its page offset component.
252          */
253         pageoff = (pos & PAGE_MASK);
254         pos -= pageoff;
255
256         /* Adjust size for rounding (on both ends). */
257         size += pageoff;                        /* low end... */
258         size = (vm_size_t) round_page(size);    /* hi end */
259
260         /*
261          * Check for illegal addresses.  Watch out for address wrap... Note
262          * that VM_*_ADDRESS are not constants due to casts (argh).
263          */
264         if (flags & MAP_FIXED) {
265                 /*
266                  * The specified address must have the same remainder
267                  * as the file offset taken modulo PAGE_SIZE, so it
268                  * should be aligned after adjustment by pageoff.
269                  */
270                 addr -= pageoff;
271                 if (addr & PAGE_MASK)
272                         return (EINVAL);
273                 /* Address range must be all in user VM space. */
274                 if (addr < vm_map_min(&vms->vm_map) ||
275                     addr + size > vm_map_max(&vms->vm_map))
276                         return (EINVAL);
277                 if (addr + size < addr)
278                         return (EINVAL);
279         } else {
280         /*
281          * XXX for non-fixed mappings where no hint is provided or
282          * the hint would fall in the potential heap space,
283          * place it after the end of the largest possible heap.
284          *
285          * There should really be a pmap call to determine a reasonable
286          * location.
287          */
288                 PROC_LOCK(td->td_proc);
289                 if (addr == 0 ||
290                     (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
291                     addr < round_page((vm_offset_t)vms->vm_daddr +
292                     lim_max(td->td_proc, RLIMIT_DATA))))
293                         addr = round_page((vm_offset_t)vms->vm_daddr +
294                             lim_max(td->td_proc, RLIMIT_DATA));
295                 PROC_UNLOCK(td->td_proc);
296         }
297         if (flags & MAP_ANON) {
298                 /*
299                  * Mapping blank space is trivial.
300                  */
301                 handle = NULL;
302                 handle_type = OBJT_DEFAULT;
303                 maxprot = VM_PROT_ALL;
304                 pos = 0;
305         } else {
306                 /*
307                  * Mapping file, get fp for validation and
308                  * don't let the descriptor disappear on us if we block.
309                  */
310                 if ((error = fget(td, uap->fd, &fp)) != 0)
311                         goto done;
312                 if (fp->f_type == DTYPE_SHM) {
313                         handle = fp->f_data;
314                         handle_type = OBJT_SWAP;
315                         maxprot = VM_PROT_NONE;
316
317                         /* FREAD should always be set. */
318                         if (fp->f_flag & FREAD)
319                                 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
320                         if (fp->f_flag & FWRITE)
321                                 maxprot |= VM_PROT_WRITE;
322                         goto map;
323                 }
324                 if (fp->f_type != DTYPE_VNODE) {
325                         error = ENODEV;
326                         goto done;
327                 }
328 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
329     defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
330                 /*
331                  * POSIX shared-memory objects are defined to have
332                  * kernel persistence, and are not defined to support
333                  * read(2)/write(2) -- or even open(2).  Thus, we can
334                  * use MAP_ASYNC to trade on-disk coherence for speed.
335                  * The shm_open(3) library routine turns on the FPOSIXSHM
336                  * flag to request this behavior.
337                  */
338                 if (fp->f_flag & FPOSIXSHM)
339                         flags |= MAP_NOSYNC;
340 #endif
341                 vp = fp->f_vnode;
342                 /*
343                  * Ensure that file and memory protections are
344                  * compatible.  Note that we only worry about
345                  * writability if mapping is shared; in this case,
346                  * current and max prot are dictated by the open file.
347                  * XXX use the vnode instead?  Problem is: what
348                  * credentials do we use for determination? What if
349                  * proc does a setuid?
350                  */
351                 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
352                         maxprot = VM_PROT_NONE;
353                 else
354                         maxprot = VM_PROT_EXECUTE;
355                 if (fp->f_flag & FREAD) {
356                         maxprot |= VM_PROT_READ;
357                 } else if (prot & PROT_READ) {
358                         error = EACCES;
359                         goto done;
360                 }
361                 /*
362                  * If we are sharing potential changes (either via
363                  * MAP_SHARED or via the implicit sharing of character
364                  * device mappings), and we are trying to get write
365                  * permission although we opened it without asking
366                  * for it, bail out.
367                  */
368                 if ((flags & MAP_SHARED) != 0) {
369                         if ((fp->f_flag & FWRITE) != 0) {
370                                 maxprot |= VM_PROT_WRITE;
371                         } else if ((prot & PROT_WRITE) != 0) {
372                                 error = EACCES;
373                                 goto done;
374                         }
375                 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
376                         maxprot |= VM_PROT_WRITE;
377                 }
378                 handle = (void *)vp;
379                 handle_type = OBJT_VNODE;
380         }
381 map:
382
383         /*
384          * Do not allow more then a certain number of vm_map_entry structures
385          * per process.  Scale with the number of rforks sharing the map
386          * to make the limit reasonable for threads.
387          */
388         if (max_proc_mmap &&
389             vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
390                 error = ENOMEM;
391                 goto done;
392         }
393
394         error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
395             flags, handle_type, handle, pos);
396 #ifdef HWPMC_HOOKS
397         /* inform hwpmc(4) if an executable is being mapped */
398         if (error == 0 && handle_type == OBJT_VNODE &&
399             (prot & PROT_EXEC)) {
400                 pkm.pm_file = handle;
401                 pkm.pm_address = (uintptr_t) addr;
402                 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
403         }
404 #endif
405         if (error == 0)
406                 td->td_retval[0] = (register_t) (addr + pageoff);
407 done:
408         if (fp)
409                 fdrop(fp, td);
410
411         return (error);
412 }
413
414 int
415 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
416 {
417         struct mmap_args oargs;
418
419         oargs.addr = uap->addr;
420         oargs.len = uap->len;
421         oargs.prot = uap->prot;
422         oargs.flags = uap->flags;
423         oargs.fd = uap->fd;
424         oargs.pos = uap->pos;
425         return (mmap(td, &oargs));
426 }
427
428 #ifdef COMPAT_43
429 #ifndef _SYS_SYSPROTO_H_
430 struct ommap_args {
431         caddr_t addr;
432         int len;
433         int prot;
434         int flags;
435         int fd;
436         long pos;
437 };
438 #endif
439 int
440 ommap(td, uap)
441         struct thread *td;
442         struct ommap_args *uap;
443 {
444         struct mmap_args nargs;
445         static const char cvtbsdprot[8] = {
446                 0,
447                 PROT_EXEC,
448                 PROT_WRITE,
449                 PROT_EXEC | PROT_WRITE,
450                 PROT_READ,
451                 PROT_EXEC | PROT_READ,
452                 PROT_WRITE | PROT_READ,
453                 PROT_EXEC | PROT_WRITE | PROT_READ,
454         };
455
456 #define OMAP_ANON       0x0002
457 #define OMAP_COPY       0x0020
458 #define OMAP_SHARED     0x0010
459 #define OMAP_FIXED      0x0100
460
461         nargs.addr = uap->addr;
462         nargs.len = uap->len;
463         nargs.prot = cvtbsdprot[uap->prot & 0x7];
464         nargs.flags = 0;
465         if (uap->flags & OMAP_ANON)
466                 nargs.flags |= MAP_ANON;
467         if (uap->flags & OMAP_COPY)
468                 nargs.flags |= MAP_COPY;
469         if (uap->flags & OMAP_SHARED)
470                 nargs.flags |= MAP_SHARED;
471         else
472                 nargs.flags |= MAP_PRIVATE;
473         if (uap->flags & OMAP_FIXED)
474                 nargs.flags |= MAP_FIXED;
475         nargs.fd = uap->fd;
476         nargs.pos = uap->pos;
477         return (mmap(td, &nargs));
478 }
479 #endif                          /* COMPAT_43 */
480
481
482 #ifndef _SYS_SYSPROTO_H_
483 struct msync_args {
484         void *addr;
485         size_t len;
486         int flags;
487 };
488 #endif
489 /*
490  * MPSAFE
491  */
492 int
493 msync(td, uap)
494         struct thread *td;
495         struct msync_args *uap;
496 {
497         vm_offset_t addr;
498         vm_size_t size, pageoff;
499         int flags;
500         vm_map_t map;
501         int rv;
502
503         addr = (vm_offset_t) uap->addr;
504         size = uap->len;
505         flags = uap->flags;
506
507         pageoff = (addr & PAGE_MASK);
508         addr -= pageoff;
509         size += pageoff;
510         size = (vm_size_t) round_page(size);
511         if (addr + size < addr)
512                 return (EINVAL);
513
514         if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
515                 return (EINVAL);
516
517         map = &td->td_proc->p_vmspace->vm_map;
518
519         /*
520          * Clean the pages and interpret the return value.
521          */
522         rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
523             (flags & MS_INVALIDATE) != 0);
524         switch (rv) {
525         case KERN_SUCCESS:
526                 return (0);
527         case KERN_INVALID_ADDRESS:
528                 return (EINVAL);        /* Sun returns ENOMEM? */
529         case KERN_INVALID_ARGUMENT:
530                 return (EBUSY);
531         default:
532                 return (EINVAL);
533         }
534 }
535
536 #ifndef _SYS_SYSPROTO_H_
537 struct munmap_args {
538         void *addr;
539         size_t len;
540 };
541 #endif
542 /*
543  * MPSAFE
544  */
545 int
546 munmap(td, uap)
547         struct thread *td;
548         struct munmap_args *uap;
549 {
550 #ifdef HWPMC_HOOKS
551         struct pmckern_map_out pkm;
552         vm_map_entry_t entry;
553 #endif
554         vm_offset_t addr;
555         vm_size_t size, pageoff;
556         vm_map_t map;
557
558         addr = (vm_offset_t) uap->addr;
559         size = uap->len;
560         if (size == 0)
561                 return (EINVAL);
562
563         pageoff = (addr & PAGE_MASK);
564         addr -= pageoff;
565         size += pageoff;
566         size = (vm_size_t) round_page(size);
567         if (addr + size < addr)
568                 return (EINVAL);
569
570         /*
571          * Check for illegal addresses.  Watch out for address wrap...
572          */
573         map = &td->td_proc->p_vmspace->vm_map;
574         if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
575                 return (EINVAL);
576         vm_map_lock(map);
577 #ifdef HWPMC_HOOKS
578         /*
579          * Inform hwpmc if the address range being unmapped contains
580          * an executable region.
581          */
582         if (vm_map_lookup_entry(map, addr, &entry)) {
583                 for (;
584                      entry != &map->header && entry->start < addr + size;
585                      entry = entry->next) {
586                         if (vm_map_check_protection(map, entry->start,
587                                 entry->end, VM_PROT_EXECUTE) == TRUE) {
588                                 pkm.pm_address = (uintptr_t) addr;
589                                 pkm.pm_size = (size_t) size;
590                                 PMC_CALL_HOOK(td, PMC_FN_MUNMAP,
591                                     (void *) &pkm);
592                                 break;
593                         }
594                 }
595         }
596 #endif
597         /* returns nothing but KERN_SUCCESS anyway */
598         vm_map_delete(map, addr, addr + size);
599         vm_map_unlock(map);
600         return (0);
601 }
602
603 #ifndef _SYS_SYSPROTO_H_
604 struct mprotect_args {
605         const void *addr;
606         size_t len;
607         int prot;
608 };
609 #endif
610 /*
611  * MPSAFE
612  */
613 int
614 mprotect(td, uap)
615         struct thread *td;
616         struct mprotect_args *uap;
617 {
618         vm_offset_t addr;
619         vm_size_t size, pageoff;
620         vm_prot_t prot;
621
622         addr = (vm_offset_t) uap->addr;
623         size = uap->len;
624         prot = uap->prot & VM_PROT_ALL;
625 #if defined(VM_PROT_READ_IS_EXEC)
626         if (prot & VM_PROT_READ)
627                 prot |= VM_PROT_EXECUTE;
628 #endif
629
630         pageoff = (addr & PAGE_MASK);
631         addr -= pageoff;
632         size += pageoff;
633         size = (vm_size_t) round_page(size);
634         if (addr + size < addr)
635                 return (EINVAL);
636
637         switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
638             addr + size, prot, FALSE)) {
639         case KERN_SUCCESS:
640                 return (0);
641         case KERN_PROTECTION_FAILURE:
642                 return (EACCES);
643         }
644         return (EINVAL);
645 }
646
647 #ifndef _SYS_SYSPROTO_H_
648 struct minherit_args {
649         void *addr;
650         size_t len;
651         int inherit;
652 };
653 #endif
654 /*
655  * MPSAFE
656  */
657 int
658 minherit(td, uap)
659         struct thread *td;
660         struct minherit_args *uap;
661 {
662         vm_offset_t addr;
663         vm_size_t size, pageoff;
664         vm_inherit_t inherit;
665
666         addr = (vm_offset_t)uap->addr;
667         size = uap->len;
668         inherit = uap->inherit;
669
670         pageoff = (addr & PAGE_MASK);
671         addr -= pageoff;
672         size += pageoff;
673         size = (vm_size_t) round_page(size);
674         if (addr + size < addr)
675                 return (EINVAL);
676
677         switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
678             addr + size, inherit)) {
679         case KERN_SUCCESS:
680                 return (0);
681         case KERN_PROTECTION_FAILURE:
682                 return (EACCES);
683         }
684         return (EINVAL);
685 }
686
687 #ifndef _SYS_SYSPROTO_H_
688 struct madvise_args {
689         void *addr;
690         size_t len;
691         int behav;
692 };
693 #endif
694
695 /*
696  * MPSAFE
697  */
698 /* ARGSUSED */
699 int
700 madvise(td, uap)
701         struct thread *td;
702         struct madvise_args *uap;
703 {
704         vm_offset_t start, end;
705         vm_map_t map;
706         struct proc *p;
707         int error;
708
709         /*
710          * Check for our special case, advising the swap pager we are
711          * "immortal."
712          */
713         if (uap->behav == MADV_PROTECT) {
714                 error = priv_check(td, PRIV_VM_MADV_PROTECT);
715                 if (error == 0) {
716                         p = td->td_proc;
717                         PROC_LOCK(p);
718                         p->p_flag |= P_PROTECTED;
719                         PROC_UNLOCK(p);
720                 }
721                 return (error);
722         }
723         /*
724          * Check for illegal behavior
725          */
726         if (uap->behav < 0 || uap->behav > MADV_CORE)
727                 return (EINVAL);
728         /*
729          * Check for illegal addresses.  Watch out for address wrap... Note
730          * that VM_*_ADDRESS are not constants due to casts (argh).
731          */
732         map = &td->td_proc->p_vmspace->vm_map;
733         if ((vm_offset_t)uap->addr < vm_map_min(map) ||
734             (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
735                 return (EINVAL);
736         if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
737                 return (EINVAL);
738
739         /*
740          * Since this routine is only advisory, we default to conservative
741          * behavior.
742          */
743         start = trunc_page((vm_offset_t) uap->addr);
744         end = round_page((vm_offset_t) uap->addr + uap->len);
745
746         if (vm_map_madvise(map, start, end, uap->behav))
747                 return (EINVAL);
748         return (0);
749 }
750
751 #ifndef _SYS_SYSPROTO_H_
752 struct mincore_args {
753         const void *addr;
754         size_t len;
755         char *vec;
756 };
757 #endif
758
759 /*
760  * MPSAFE
761  */
762 /* ARGSUSED */
763 int
764 mincore(td, uap)
765         struct thread *td;
766         struct mincore_args *uap;
767 {
768         vm_offset_t addr, first_addr;
769         vm_offset_t end, cend;
770         pmap_t pmap;
771         vm_map_t map;
772         char *vec;
773         int error = 0;
774         int vecindex, lastvecindex;
775         vm_map_entry_t current;
776         vm_map_entry_t entry;
777         int mincoreinfo;
778         unsigned int timestamp;
779
780         /*
781          * Make sure that the addresses presented are valid for user
782          * mode.
783          */
784         first_addr = addr = trunc_page((vm_offset_t) uap->addr);
785         end = addr + (vm_size_t)round_page(uap->len);
786         map = &td->td_proc->p_vmspace->vm_map;
787         if (end > vm_map_max(map) || end < addr)
788                 return (ENOMEM);
789
790         /*
791          * Address of byte vector
792          */
793         vec = uap->vec;
794
795         pmap = vmspace_pmap(td->td_proc->p_vmspace);
796
797         vm_map_lock_read(map);
798 RestartScan:
799         timestamp = map->timestamp;
800
801         if (!vm_map_lookup_entry(map, addr, &entry)) {
802                 vm_map_unlock_read(map);
803                 return (ENOMEM);
804         }
805
806         /*
807          * Do this on a map entry basis so that if the pages are not
808          * in the current processes address space, we can easily look
809          * up the pages elsewhere.
810          */
811         lastvecindex = -1;
812         for (current = entry;
813             (current != &map->header) && (current->start < end);
814             current = current->next) {
815
816                 /*
817                  * check for contiguity
818                  */
819                 if (current->end < end &&
820                     (entry->next == &map->header ||
821                      current->next->start > current->end)) {
822                         vm_map_unlock_read(map);
823                         return (ENOMEM);
824                 }
825
826                 /*
827                  * ignore submaps (for now) or null objects
828                  */
829                 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
830                         current->object.vm_object == NULL)
831                         continue;
832
833                 /*
834                  * limit this scan to the current map entry and the
835                  * limits for the mincore call
836                  */
837                 if (addr < current->start)
838                         addr = current->start;
839                 cend = current->end;
840                 if (cend > end)
841                         cend = end;
842
843                 /*
844                  * scan this entry one page at a time
845                  */
846                 while (addr < cend) {
847                         /*
848                          * Check pmap first, it is likely faster, also
849                          * it can provide info as to whether we are the
850                          * one referencing or modifying the page.
851                          */
852                         mincoreinfo = pmap_mincore(pmap, addr);
853                         if (!mincoreinfo) {
854                                 vm_pindex_t pindex;
855                                 vm_ooffset_t offset;
856                                 vm_page_t m;
857                                 /*
858                                  * calculate the page index into the object
859                                  */
860                                 offset = current->offset + (addr - current->start);
861                                 pindex = OFF_TO_IDX(offset);
862                                 VM_OBJECT_LOCK(current->object.vm_object);
863                                 m = vm_page_lookup(current->object.vm_object,
864                                         pindex);
865                                 /*
866                                  * if the page is resident, then gather information about
867                                  * it.
868                                  */
869                                 if (m != NULL && m->valid != 0) {
870                                         mincoreinfo = MINCORE_INCORE;
871                                         vm_page_lock_queues();
872                                         if (m->dirty ||
873                                                 pmap_is_modified(m))
874                                                 mincoreinfo |= MINCORE_MODIFIED_OTHER;
875                                         if ((m->flags & PG_REFERENCED) ||
876                                                 pmap_ts_referenced(m)) {
877                                                 vm_page_flag_set(m, PG_REFERENCED);
878                                                 mincoreinfo |= MINCORE_REFERENCED_OTHER;
879                                         }
880                                         vm_page_unlock_queues();
881                                 }
882                                 VM_OBJECT_UNLOCK(current->object.vm_object);
883                         }
884
885                         /*
886                          * subyte may page fault.  In case it needs to modify
887                          * the map, we release the lock.
888                          */
889                         vm_map_unlock_read(map);
890
891                         /*
892                          * calculate index into user supplied byte vector
893                          */
894                         vecindex = OFF_TO_IDX(addr - first_addr);
895
896                         /*
897                          * If we have skipped map entries, we need to make sure that
898                          * the byte vector is zeroed for those skipped entries.
899                          */
900                         while ((lastvecindex + 1) < vecindex) {
901                                 error = subyte(vec + lastvecindex, 0);
902                                 if (error) {
903                                         error = EFAULT;
904                                         goto done2;
905                                 }
906                                 ++lastvecindex;
907                         }
908
909                         /*
910                          * Pass the page information to the user
911                          */
912                         error = subyte(vec + vecindex, mincoreinfo);
913                         if (error) {
914                                 error = EFAULT;
915                                 goto done2;
916                         }
917
918                         /*
919                          * If the map has changed, due to the subyte, the previous
920                          * output may be invalid.
921                          */
922                         vm_map_lock_read(map);
923                         if (timestamp != map->timestamp)
924                                 goto RestartScan;
925
926                         lastvecindex = vecindex;
927                         addr += PAGE_SIZE;
928                 }
929         }
930
931         /*
932          * subyte may page fault.  In case it needs to modify
933          * the map, we release the lock.
934          */
935         vm_map_unlock_read(map);
936
937         /*
938          * Zero the last entries in the byte vector.
939          */
940         vecindex = OFF_TO_IDX(end - first_addr);
941         while ((lastvecindex + 1) < vecindex) {
942                 error = subyte(vec + lastvecindex, 0);
943                 if (error) {
944                         error = EFAULT;
945                         goto done2;
946                 }
947                 ++lastvecindex;
948         }
949
950         /*
951          * If the map has changed, due to the subyte, the previous
952          * output may be invalid.
953          */
954         vm_map_lock_read(map);
955         if (timestamp != map->timestamp)
956                 goto RestartScan;
957         vm_map_unlock_read(map);
958 done2:
959         return (error);
960 }
961
962 #ifndef _SYS_SYSPROTO_H_
963 struct mlock_args {
964         const void *addr;
965         size_t len;
966 };
967 #endif
968 /*
969  * MPSAFE
970  */
971 int
972 mlock(td, uap)
973         struct thread *td;
974         struct mlock_args *uap;
975 {
976         struct proc *proc;
977         vm_offset_t addr, end, last, start;
978         vm_size_t npages, size;
979         int error;
980
981         error = priv_check(td, PRIV_VM_MLOCK);
982         if (error)
983                 return (error);
984         addr = (vm_offset_t)uap->addr;
985         size = uap->len;
986         last = addr + size;
987         start = trunc_page(addr);
988         end = round_page(last);
989         if (last < addr || end < addr)
990                 return (EINVAL);
991         npages = atop(end - start);
992         if (npages > vm_page_max_wired)
993                 return (ENOMEM);
994         proc = td->td_proc;
995         PROC_LOCK(proc);
996         if (ptoa(npages +
997             pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
998             lim_cur(proc, RLIMIT_MEMLOCK)) {
999                 PROC_UNLOCK(proc);
1000                 return (ENOMEM);
1001         }
1002         PROC_UNLOCK(proc);
1003         if (npages + cnt.v_wire_count > vm_page_max_wired)
1004                 return (EAGAIN);
1005         error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
1006             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1007         return (error == KERN_SUCCESS ? 0 : ENOMEM);
1008 }
1009
1010 #ifndef _SYS_SYSPROTO_H_
1011 struct mlockall_args {
1012         int     how;
1013 };
1014 #endif
1015
1016 /*
1017  * MPSAFE
1018  */
1019 int
1020 mlockall(td, uap)
1021         struct thread *td;
1022         struct mlockall_args *uap;
1023 {
1024         vm_map_t map;
1025         int error;
1026
1027         map = &td->td_proc->p_vmspace->vm_map;
1028         error = 0;
1029
1030         if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1031                 return (EINVAL);
1032
1033 #if 0
1034         /*
1035          * If wiring all pages in the process would cause it to exceed
1036          * a hard resource limit, return ENOMEM.
1037          */
1038         PROC_LOCK(td->td_proc);
1039         if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
1040                 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
1041                 PROC_UNLOCK(td->td_proc);
1042                 return (ENOMEM);
1043         }
1044         PROC_UNLOCK(td->td_proc);
1045 #else
1046         error = priv_check(td, PRIV_VM_MLOCK);
1047         if (error)
1048                 return (error);
1049 #endif
1050
1051         if (uap->how & MCL_FUTURE) {
1052                 vm_map_lock(map);
1053                 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1054                 vm_map_unlock(map);
1055                 error = 0;
1056         }
1057
1058         if (uap->how & MCL_CURRENT) {
1059                 /*
1060                  * P1003.1-2001 mandates that all currently mapped pages
1061                  * will be memory resident and locked (wired) upon return
1062                  * from mlockall(). vm_map_wire() will wire pages, by
1063                  * calling vm_fault_wire() for each page in the region.
1064                  */
1065                 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1066                     VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1067                 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1068         }
1069
1070         return (error);
1071 }
1072
1073 #ifndef _SYS_SYSPROTO_H_
1074 struct munlockall_args {
1075         register_t dummy;
1076 };
1077 #endif
1078
1079 /*
1080  * MPSAFE
1081  */
1082 int
1083 munlockall(td, uap)
1084         struct thread *td;
1085         struct munlockall_args *uap;
1086 {
1087         vm_map_t map;
1088         int error;
1089
1090         map = &td->td_proc->p_vmspace->vm_map;
1091         error = priv_check(td, PRIV_VM_MUNLOCK);
1092         if (error)
1093                 return (error);
1094
1095         /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1096         vm_map_lock(map);
1097         vm_map_modflags(map, 0, MAP_WIREFUTURE);
1098         vm_map_unlock(map);
1099
1100         /* Forcibly unwire all pages. */
1101         error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1102             VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1103
1104         return (error);
1105 }
1106
1107 #ifndef _SYS_SYSPROTO_H_
1108 struct munlock_args {
1109         const void *addr;
1110         size_t len;
1111 };
1112 #endif
1113 /*
1114  * MPSAFE
1115  */
1116 int
1117 munlock(td, uap)
1118         struct thread *td;
1119         struct munlock_args *uap;
1120 {
1121         vm_offset_t addr, end, last, start;
1122         vm_size_t size;
1123         int error;
1124
1125         error = priv_check(td, PRIV_VM_MUNLOCK);
1126         if (error)
1127                 return (error);
1128         addr = (vm_offset_t)uap->addr;
1129         size = uap->len;
1130         last = addr + size;
1131         start = trunc_page(addr);
1132         end = round_page(last);
1133         if (last < addr || end < addr)
1134                 return (EINVAL);
1135         error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1136             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1137         return (error == KERN_SUCCESS ? 0 : ENOMEM);
1138 }
1139
1140 /*
1141  * vm_mmap_vnode()
1142  *
1143  * MPSAFE
1144  *
1145  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1146  * operations on vnodes.
1147  */
1148 int
1149 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1150     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1151     struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1152 {
1153         struct vattr va;
1154         void *handle;
1155         vm_object_t obj;
1156         struct mount *mp;
1157         struct cdevsw *dsw;
1158         struct ucred *cred;
1159         int error, flags, type;
1160         int vfslocked;
1161
1162         mp = vp->v_mount;
1163         cred = td->td_ucred;
1164         vfslocked = VFS_LOCK_GIANT(mp);
1165         if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1166                 VFS_UNLOCK_GIANT(vfslocked);
1167                 return (error);
1168         }
1169         flags = *flagsp;
1170         obj = vp->v_object;
1171         if (vp->v_type == VREG) {
1172                 /*
1173                  * Get the proper underlying object
1174                  */
1175                 if (obj == NULL) {
1176                         error = EINVAL;
1177                         goto done;
1178                 }
1179                 if (obj->handle != vp) {
1180                         vput(vp);
1181                         vp = (struct vnode*)obj->handle;
1182                         vget(vp, LK_EXCLUSIVE, td);
1183                 }
1184                 type = OBJT_VNODE;
1185                 handle = vp;
1186         } else if (vp->v_type == VCHR) {
1187                 type = OBJT_DEVICE;
1188                 handle = vp->v_rdev;
1189
1190                 dsw = dev_refthread(handle);
1191                 if (dsw == NULL) {
1192                         error = ENXIO;
1193                         goto done;
1194                 }
1195                 if (dsw->d_flags & D_MMAP_ANON) {
1196                         dev_relthread(handle);
1197                         *maxprotp = VM_PROT_ALL;
1198                         *flagsp |= MAP_ANON;
1199                         error = 0;
1200                         goto done;
1201                 }
1202                 dev_relthread(handle);
1203                 /*
1204                  * cdevs does not provide private mappings of any kind.
1205                  */
1206                 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1207                     (prot & PROT_WRITE) != 0) {
1208                         error = EACCES;
1209                         goto done;
1210                 }
1211                 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1212                         error = EINVAL;
1213                         goto done;
1214                 }
1215                 /*
1216                  * Force device mappings to be shared.
1217                  */
1218                 flags |= MAP_SHARED;
1219         } else {
1220                 error = EINVAL;
1221                 goto done;
1222         }
1223         if ((error = VOP_GETATTR(vp, &va, cred)))
1224                 goto done;
1225 #ifdef MAC
1226         error = mac_vnode_check_mmap(cred, vp, prot, flags);
1227         if (error != 0)
1228                 goto done;
1229 #endif
1230         if ((flags & MAP_SHARED) != 0) {
1231                 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1232                         if (prot & PROT_WRITE) {
1233                                 error = EPERM;
1234                                 goto done;
1235                         }
1236                         *maxprotp &= ~VM_PROT_WRITE;
1237                 }
1238         }
1239         /*
1240          * If it is a regular file without any references
1241          * we do not need to sync it.
1242          * Adjust object size to be the size of actual file.
1243          */
1244         if (vp->v_type == VREG) {
1245                 objsize = round_page(va.va_size);
1246                 if (va.va_nlink == 0)
1247                         flags |= MAP_NOSYNC;
1248         }
1249         obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1250         if (obj == NULL) {
1251                 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1252                 goto done;
1253         }
1254         *objp = obj;
1255         *flagsp = flags;
1256         vfs_mark_atime(vp, cred);
1257
1258 done:
1259         vput(vp);
1260         VFS_UNLOCK_GIANT(vfslocked);
1261         return (error);
1262 }
1263
1264 /*
1265  * vm_mmap_cdev()
1266  *
1267  * MPSAFE
1268  *
1269  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1270  * operations on cdevs.
1271  */
1272 int
1273 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1274     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1275     struct cdev *cdev, vm_ooffset_t foff, vm_object_t *objp)
1276 {
1277         vm_object_t obj;
1278         struct cdevsw *dsw;
1279         int flags;
1280
1281         flags = *flagsp;
1282
1283         dsw = dev_refthread(cdev);
1284         if (dsw == NULL)
1285                 return (ENXIO);
1286         if (dsw->d_flags & D_MMAP_ANON) {
1287                 dev_relthread(cdev);
1288                 *maxprotp = VM_PROT_ALL;
1289                 *flagsp |= MAP_ANON;
1290                 return (0);
1291         }
1292         dev_relthread(cdev);
1293         /*
1294          * cdevs does not provide private mappings of any kind.
1295          */
1296         if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1297             (prot & PROT_WRITE) != 0)
1298                 return (EACCES);
1299         if (flags & (MAP_PRIVATE|MAP_COPY))
1300                 return (EINVAL);
1301         /*
1302          * Force device mappings to be shared.
1303          */
1304         flags |= MAP_SHARED;
1305 #ifdef MAC_XXX
1306         error = mac_check_cdev_mmap(td->td_ucred, cdev, prot);
1307         if (error != 0)
1308                 return (error);
1309 #endif
1310         obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, foff);
1311         if (obj == NULL)
1312                 return (EINVAL);
1313         *objp = obj;
1314         *flagsp = flags;
1315         return (0);
1316 }
1317
1318 /*
1319  * vm_mmap_shm()
1320  *
1321  * MPSAFE
1322  *
1323  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1324  * operations on shm file descriptors.
1325  */
1326 int
1327 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1328     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1329     struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1330 {
1331         int error;
1332
1333         if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1334             (prot & PROT_WRITE) != 0)
1335                 return (EACCES);
1336 #ifdef MAC
1337         error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1338         if (error != 0)
1339                 return (error);
1340 #endif
1341         error = shm_mmap(shmfd, objsize, foff, objp);
1342         if (error)
1343                 return (error);
1344         return (0);
1345 }
1346
1347 /*
1348  * vm_mmap()
1349  *
1350  * MPSAFE
1351  *
1352  * Internal version of mmap.  Currently used by mmap, exec, and sys5
1353  * shared memory.  Handle is either a vnode pointer or NULL for MAP_ANON.
1354  */
1355 int
1356 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1357         vm_prot_t maxprot, int flags,
1358         objtype_t handle_type, void *handle,
1359         vm_ooffset_t foff)
1360 {
1361         boolean_t fitit;
1362         vm_object_t object = NULL;
1363         int rv = KERN_SUCCESS;
1364         int docow, error;
1365         struct thread *td = curthread;
1366
1367         if (size == 0)
1368                 return (0);
1369
1370         size = round_page(size);
1371
1372         PROC_LOCK(td->td_proc);
1373         if (td->td_proc->p_vmspace->vm_map.size + size >
1374             lim_cur(td->td_proc, RLIMIT_VMEM)) {
1375                 PROC_UNLOCK(td->td_proc);
1376                 return(ENOMEM);
1377         }
1378         PROC_UNLOCK(td->td_proc);
1379
1380         /*
1381          * We currently can only deal with page aligned file offsets.
1382          * The check is here rather than in the syscall because the
1383          * kernel calls this function internally for other mmaping
1384          * operations (such as in exec) and non-aligned offsets will
1385          * cause pmap inconsistencies...so we want to be sure to
1386          * disallow this in all cases.
1387          */
1388         if (foff & PAGE_MASK)
1389                 return (EINVAL);
1390
1391         if ((flags & MAP_FIXED) == 0) {
1392                 fitit = TRUE;
1393                 *addr = round_page(*addr);
1394         } else {
1395                 if (*addr != trunc_page(*addr))
1396                         return (EINVAL);
1397                 fitit = FALSE;
1398         }
1399         /*
1400          * Lookup/allocate object.
1401          */
1402         switch (handle_type) {
1403         case OBJT_DEVICE:
1404                 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1405                     handle, foff, &object);
1406                 break;
1407         case OBJT_VNODE:
1408                 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1409                     handle, foff, &object);
1410                 break;
1411         case OBJT_SWAP:
1412                 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1413                     handle, foff, &object);
1414                 break;
1415         case OBJT_DEFAULT:
1416                 if (handle == NULL) {
1417                         error = 0;
1418                         break;
1419                 }
1420                 /* FALLTHROUGH */
1421         default:
1422                 error = EINVAL;
1423                 break;
1424         }
1425         if (error)
1426                 return (error);
1427         if (flags & MAP_ANON) {
1428                 object = NULL;
1429                 docow = 0;
1430                 /*
1431                  * Unnamed anonymous regions always start at 0.
1432                  */
1433                 if (handle == 0)
1434                         foff = 0;
1435         } else {
1436                 docow = MAP_PREFAULT_PARTIAL;
1437         }
1438
1439         if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1440                 docow |= MAP_COPY_ON_WRITE;
1441         if (flags & MAP_NOSYNC)
1442                 docow |= MAP_DISABLE_SYNCER;
1443         if (flags & MAP_NOCORE)
1444                 docow |= MAP_DISABLE_COREDUMP;
1445
1446 #if defined(VM_PROT_READ_IS_EXEC)
1447         if (prot & VM_PROT_READ)
1448                 prot |= VM_PROT_EXECUTE;
1449
1450         if (maxprot & VM_PROT_READ)
1451                 maxprot |= VM_PROT_EXECUTE;
1452 #endif
1453
1454         if (flags & MAP_STACK)
1455                 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1456                     docow | MAP_STACK_GROWS_DOWN);
1457         else if (fitit)
1458                 rv = vm_map_find(map, object, foff, addr, size,
1459                     object != NULL && object->type == OBJT_DEVICE ?
1460                     VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
1461         else
1462                 rv = vm_map_fixed(map, object, foff, *addr, size,
1463                                  prot, maxprot, docow);
1464
1465         if (rv != KERN_SUCCESS) {
1466                 /*
1467                  * Lose the object reference. Will destroy the
1468                  * object if it's an unnamed anonymous mapping
1469                  * or named anonymous without other references.
1470                  */
1471                 vm_object_deallocate(object);
1472         } else if (flags & MAP_SHARED) {
1473                 /*
1474                  * Shared memory is also shared with children.
1475                  */
1476                 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1477                 if (rv != KERN_SUCCESS)
1478                         (void) vm_map_remove(map, *addr, *addr + size);
1479         }
1480
1481         /*
1482          * If the process has requested that all future mappings
1483          * be wired, then heed this.
1484          */
1485         if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1486                 vm_map_wire(map, *addr, *addr + size,
1487                     VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1488
1489         switch (rv) {
1490         case KERN_SUCCESS:
1491                 return (0);
1492         case KERN_INVALID_ADDRESS:
1493         case KERN_NO_SPACE:
1494                 return (ENOMEM);
1495         case KERN_PROTECTION_FAILURE:
1496                 return (EACCES);
1497         default:
1498                 return (EINVAL);
1499         }
1500 }