]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_mmap.c
IFC @r271887
[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
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capsicum.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/procctl.h>
60 #include <sys/racct.h>
61 #include <sys/resource.h>
62 #include <sys/resourcevar.h>
63 #include <sys/rwlock.h>
64 #include <sys/sysctl.h>
65 #include <sys/vnode.h>
66 #include <sys/fcntl.h>
67 #include <sys/file.h>
68 #include <sys/mman.h>
69 #include <sys/mount.h>
70 #include <sys/conf.h>
71 #include <sys/stat.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysent.h>
74 #include <sys/vmmeter.h>
75
76 #include <security/mac/mac_framework.h>
77
78 #include <vm/vm.h>
79 #include <vm/vm_param.h>
80 #include <vm/pmap.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pager.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_page.h>
88 #include <vm/vnode_pager.h>
89
90 #ifdef HWPMC_HOOKS
91 #include <sys/pmckern.h>
92 #endif
93
94 int old_mlock = 0;
95 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
96     "Do not apply RLIMIT_MEMLOCK on mlockall");
97
98 #ifdef MAP_32BIT
99 #define MAP_32BIT_MAX_ADDR      ((vm_offset_t)1 << 31)
100 #endif
101
102 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
103     int *, struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
104 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
105     int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
106 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
107     int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
108
109 #ifndef _SYS_SYSPROTO_H_
110 struct sbrk_args {
111         int incr;
112 };
113 #endif
114
115 /*
116  * MPSAFE
117  */
118 /* ARGSUSED */
119 int
120 sys_sbrk(td, uap)
121         struct thread *td;
122         struct sbrk_args *uap;
123 {
124         /* Not yet implemented */
125         return (EOPNOTSUPP);
126 }
127
128 #ifndef _SYS_SYSPROTO_H_
129 struct sstk_args {
130         int incr;
131 };
132 #endif
133
134 /*
135  * MPSAFE
136  */
137 /* ARGSUSED */
138 int
139 sys_sstk(td, uap)
140         struct thread *td;
141         struct sstk_args *uap;
142 {
143         /* Not yet implemented */
144         return (EOPNOTSUPP);
145 }
146
147 #if defined(COMPAT_43)
148 #ifndef _SYS_SYSPROTO_H_
149 struct getpagesize_args {
150         int dummy;
151 };
152 #endif
153
154 int
155 ogetpagesize(td, uap)
156         struct thread *td;
157         struct getpagesize_args *uap;
158 {
159         /* MP SAFE */
160         td->td_retval[0] = PAGE_SIZE;
161         return (0);
162 }
163 #endif                          /* COMPAT_43 */
164
165
166 /*
167  * Memory Map (mmap) system call.  Note that the file offset
168  * and address are allowed to be NOT page aligned, though if
169  * the MAP_FIXED flag it set, both must have the same remainder
170  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
171  * page-aligned, the actual mapping starts at trunc_page(addr)
172  * and the return value is adjusted up by the page offset.
173  *
174  * Generally speaking, only character devices which are themselves
175  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
176  * there would be no cache coherency between a descriptor and a VM mapping
177  * both to the same character device.
178  */
179 #ifndef _SYS_SYSPROTO_H_
180 struct mmap_args {
181         void *addr;
182         size_t len;
183         int prot;
184         int flags;
185         int fd;
186         long pad;
187         off_t pos;
188 };
189 #endif
190
191 /*
192  * MPSAFE
193  */
194 int
195 sys_mmap(td, uap)
196         struct thread *td;
197         struct mmap_args *uap;
198 {
199 #ifdef HWPMC_HOOKS
200         struct pmckern_map_in pkm;
201 #endif
202         struct file *fp;
203         struct vnode *vp;
204         vm_offset_t addr;
205         vm_size_t size, pageoff;
206         vm_prot_t cap_maxprot, maxprot;
207         void *handle;
208         objtype_t handle_type;
209         int align, error, flags, prot;
210         off_t pos;
211         struct vmspace *vms = td->td_proc->p_vmspace;
212         cap_rights_t rights;
213
214         addr = (vm_offset_t) uap->addr;
215         size = uap->len;
216         prot = uap->prot;
217         flags = uap->flags;
218         pos = uap->pos;
219
220         fp = NULL;
221
222         /*
223          * Enforce the constraints.
224          * Mapping of length 0 is only allowed for old binaries.
225          * Anonymous mapping shall specify -1 as filedescriptor and
226          * zero position for new code. Be nice to ancient a.out
227          * binaries and correct pos for anonymous mapping, since old
228          * ld.so sometimes issues anonymous map requests with non-zero
229          * pos.
230          */
231         if (!SV_CURPROC_FLAG(SV_AOUT)) {
232                 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
233                     ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
234                         return (EINVAL);
235         } else {
236                 if ((flags & MAP_ANON) != 0)
237                         pos = 0;
238         }
239
240         if (flags & MAP_STACK) {
241                 if ((uap->fd != -1) ||
242                     ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
243                         return (EINVAL);
244                 flags |= MAP_ANON;
245                 pos = 0;
246         }
247         if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_RENAME |
248             MAP_NORESERVE | MAP_HASSEMAPHORE | MAP_STACK | MAP_NOSYNC |
249             MAP_ANON | MAP_EXCL | MAP_NOCORE | MAP_PREFAULT_READ |
250 #ifdef MAP_32BIT
251             MAP_32BIT |
252 #endif
253             MAP_ALIGNMENT_MASK)) != 0)
254                 return (EINVAL);
255         if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
256                 return (EINVAL);
257         if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
258                 return (EINVAL);
259         if (prot != PROT_NONE &&
260             (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
261                 return (EINVAL);
262
263         /*
264          * Align the file position to a page boundary,
265          * and save its page offset component.
266          */
267         pageoff = (pos & PAGE_MASK);
268         pos -= pageoff;
269
270         /* Adjust size for rounding (on both ends). */
271         size += pageoff;                        /* low end... */
272         size = (vm_size_t) round_page(size);    /* hi end */
273
274         /* Ensure alignment is at least a page and fits in a pointer. */
275         align = flags & MAP_ALIGNMENT_MASK;
276         if (align != 0 && align != MAP_ALIGNED_SUPER &&
277             (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
278             align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
279                 return (EINVAL);
280
281         /*
282          * Check for illegal addresses.  Watch out for address wrap... Note
283          * that VM_*_ADDRESS are not constants due to casts (argh).
284          */
285         if (flags & MAP_FIXED) {
286                 /*
287                  * The specified address must have the same remainder
288                  * as the file offset taken modulo PAGE_SIZE, so it
289                  * should be aligned after adjustment by pageoff.
290                  */
291                 addr -= pageoff;
292                 if (addr & PAGE_MASK)
293                         return (EINVAL);
294
295                 /* Address range must be all in user VM space. */
296                 if (addr < vm_map_min(&vms->vm_map) ||
297                     addr + size > vm_map_max(&vms->vm_map))
298                         return (EINVAL);
299                 if (addr + size < addr)
300                         return (EINVAL);
301 #ifdef MAP_32BIT
302                 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
303                         return (EINVAL);
304         } else if (flags & MAP_32BIT) {
305                 /*
306                  * For MAP_32BIT, override the hint if it is too high and
307                  * do not bother moving the mapping past the heap (since
308                  * the heap is usually above 2GB).
309                  */
310                 if (addr + size > MAP_32BIT_MAX_ADDR)
311                         addr = 0;
312 #endif
313         } else {
314                 /*
315                  * XXX for non-fixed mappings where no hint is provided or
316                  * the hint would fall in the potential heap space,
317                  * place it after the end of the largest possible heap.
318                  *
319                  * There should really be a pmap call to determine a reasonable
320                  * location.
321                  */
322                 PROC_LOCK(td->td_proc);
323                 if (addr == 0 ||
324                     (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
325                     addr < round_page((vm_offset_t)vms->vm_daddr +
326                     lim_max(td->td_proc, RLIMIT_DATA))))
327                         addr = round_page((vm_offset_t)vms->vm_daddr +
328                             lim_max(td->td_proc, RLIMIT_DATA));
329                 PROC_UNLOCK(td->td_proc);
330         }
331         if (flags & MAP_ANON) {
332                 /*
333                  * Mapping blank space is trivial.
334                  */
335                 handle = NULL;
336                 handle_type = OBJT_DEFAULT;
337                 maxprot = VM_PROT_ALL;
338                 cap_maxprot = VM_PROT_ALL;
339         } else {
340                 /*
341                  * Mapping file, get fp for validation and don't let the
342                  * descriptor disappear on us if we block. Check capability
343                  * rights, but also return the maximum rights to be combined
344                  * with maxprot later.
345                  */
346                 cap_rights_init(&rights, CAP_MMAP);
347                 if (prot & PROT_READ)
348                         cap_rights_set(&rights, CAP_MMAP_R);
349                 if ((flags & MAP_SHARED) != 0) {
350                         if (prot & PROT_WRITE)
351                                 cap_rights_set(&rights, CAP_MMAP_W);
352                 }
353                 if (prot & PROT_EXEC)
354                         cap_rights_set(&rights, CAP_MMAP_X);
355                 error = fget_mmap(td, uap->fd, &rights, &cap_maxprot, &fp);
356                 if (error != 0)
357                         goto done;
358                 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
359                     td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
360                         error = EINVAL;
361                         goto done;
362                 }
363                 if (fp->f_type == DTYPE_SHM) {
364                         handle = fp->f_data;
365                         handle_type = OBJT_SWAP;
366                         maxprot = VM_PROT_NONE;
367
368                         /* FREAD should always be set. */
369                         if (fp->f_flag & FREAD)
370                                 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
371                         if (fp->f_flag & FWRITE)
372                                 maxprot |= VM_PROT_WRITE;
373                         goto map;
374                 }
375                 if (fp->f_type != DTYPE_VNODE) {
376                         error = ENODEV;
377                         goto done;
378                 }
379 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
380     defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
381                 /*
382                  * POSIX shared-memory objects are defined to have
383                  * kernel persistence, and are not defined to support
384                  * read(2)/write(2) -- or even open(2).  Thus, we can
385                  * use MAP_ASYNC to trade on-disk coherence for speed.
386                  * The shm_open(3) library routine turns on the FPOSIXSHM
387                  * flag to request this behavior.
388                  */
389                 if (fp->f_flag & FPOSIXSHM)
390                         flags |= MAP_NOSYNC;
391 #endif
392                 vp = fp->f_vnode;
393                 /*
394                  * Ensure that file and memory protections are
395                  * compatible.  Note that we only worry about
396                  * writability if mapping is shared; in this case,
397                  * current and max prot are dictated by the open file.
398                  * XXX use the vnode instead?  Problem is: what
399                  * credentials do we use for determination? What if
400                  * proc does a setuid?
401                  */
402                 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
403                         maxprot = VM_PROT_NONE;
404                 else
405                         maxprot = VM_PROT_EXECUTE;
406                 if (fp->f_flag & FREAD) {
407                         maxprot |= VM_PROT_READ;
408                 } else if (prot & PROT_READ) {
409                         error = EACCES;
410                         goto done;
411                 }
412                 /*
413                  * If we are sharing potential changes (either via
414                  * MAP_SHARED or via the implicit sharing of character
415                  * device mappings), and we are trying to get write
416                  * permission although we opened it without asking
417                  * for it, bail out.
418                  */
419                 if ((flags & MAP_SHARED) != 0) {
420                         if ((fp->f_flag & FWRITE) != 0) {
421                                 maxprot |= VM_PROT_WRITE;
422                         } else if ((prot & PROT_WRITE) != 0) {
423                                 error = EACCES;
424                                 goto done;
425                         }
426                 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
427                         maxprot |= VM_PROT_WRITE;
428                         cap_maxprot |= VM_PROT_WRITE;
429                 }
430                 handle = (void *)vp;
431                 handle_type = OBJT_VNODE;
432         }
433 map:
434         td->td_fpop = fp;
435         maxprot &= cap_maxprot;
436
437         /* This relies on VM_PROT_* matching PROT_*. */
438         error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
439             flags, handle_type, handle, pos);
440         td->td_fpop = NULL;
441 #ifdef HWPMC_HOOKS
442         /* inform hwpmc(4) if an executable is being mapped */
443         if (error == 0 && handle_type == OBJT_VNODE &&
444             (prot & PROT_EXEC)) {
445                 pkm.pm_file = handle;
446                 pkm.pm_address = (uintptr_t) addr;
447                 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
448         }
449 #endif
450         if (error == 0)
451                 td->td_retval[0] = (register_t) (addr + pageoff);
452 done:
453         if (fp)
454                 fdrop(fp, td);
455
456         return (error);
457 }
458
459 int
460 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
461 {
462         struct mmap_args oargs;
463
464         oargs.addr = uap->addr;
465         oargs.len = uap->len;
466         oargs.prot = uap->prot;
467         oargs.flags = uap->flags;
468         oargs.fd = uap->fd;
469         oargs.pos = uap->pos;
470         return (sys_mmap(td, &oargs));
471 }
472
473 #ifdef COMPAT_43
474 #ifndef _SYS_SYSPROTO_H_
475 struct ommap_args {
476         caddr_t addr;
477         int len;
478         int prot;
479         int flags;
480         int fd;
481         long pos;
482 };
483 #endif
484 int
485 ommap(td, uap)
486         struct thread *td;
487         struct ommap_args *uap;
488 {
489         struct mmap_args nargs;
490         static const char cvtbsdprot[8] = {
491                 0,
492                 PROT_EXEC,
493                 PROT_WRITE,
494                 PROT_EXEC | PROT_WRITE,
495                 PROT_READ,
496                 PROT_EXEC | PROT_READ,
497                 PROT_WRITE | PROT_READ,
498                 PROT_EXEC | PROT_WRITE | PROT_READ,
499         };
500
501 #define OMAP_ANON       0x0002
502 #define OMAP_COPY       0x0020
503 #define OMAP_SHARED     0x0010
504 #define OMAP_FIXED      0x0100
505
506         nargs.addr = uap->addr;
507         nargs.len = uap->len;
508         nargs.prot = cvtbsdprot[uap->prot & 0x7];
509 #ifdef COMPAT_FREEBSD32
510 #if defined(__amd64__)
511         if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
512             nargs.prot != 0)
513                 nargs.prot |= PROT_EXEC;
514 #endif
515 #endif
516         nargs.flags = 0;
517         if (uap->flags & OMAP_ANON)
518                 nargs.flags |= MAP_ANON;
519         if (uap->flags & OMAP_COPY)
520                 nargs.flags |= MAP_COPY;
521         if (uap->flags & OMAP_SHARED)
522                 nargs.flags |= MAP_SHARED;
523         else
524                 nargs.flags |= MAP_PRIVATE;
525         if (uap->flags & OMAP_FIXED)
526                 nargs.flags |= MAP_FIXED;
527         nargs.fd = uap->fd;
528         nargs.pos = uap->pos;
529         return (sys_mmap(td, &nargs));
530 }
531 #endif                          /* COMPAT_43 */
532
533
534 #ifndef _SYS_SYSPROTO_H_
535 struct msync_args {
536         void *addr;
537         size_t len;
538         int flags;
539 };
540 #endif
541 /*
542  * MPSAFE
543  */
544 int
545 sys_msync(td, uap)
546         struct thread *td;
547         struct msync_args *uap;
548 {
549         vm_offset_t addr;
550         vm_size_t size, pageoff;
551         int flags;
552         vm_map_t map;
553         int rv;
554
555         addr = (vm_offset_t) uap->addr;
556         size = uap->len;
557         flags = uap->flags;
558
559         pageoff = (addr & PAGE_MASK);
560         addr -= pageoff;
561         size += pageoff;
562         size = (vm_size_t) round_page(size);
563         if (addr + size < addr)
564                 return (EINVAL);
565
566         if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
567                 return (EINVAL);
568
569         map = &td->td_proc->p_vmspace->vm_map;
570
571         /*
572          * Clean the pages and interpret the return value.
573          */
574         rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
575             (flags & MS_INVALIDATE) != 0);
576         switch (rv) {
577         case KERN_SUCCESS:
578                 return (0);
579         case KERN_INVALID_ADDRESS:
580                 return (ENOMEM);
581         case KERN_INVALID_ARGUMENT:
582                 return (EBUSY);
583         case KERN_FAILURE:
584                 return (EIO);
585         default:
586                 return (EINVAL);
587         }
588 }
589
590 #ifndef _SYS_SYSPROTO_H_
591 struct munmap_args {
592         void *addr;
593         size_t len;
594 };
595 #endif
596 /*
597  * MPSAFE
598  */
599 int
600 sys_munmap(td, uap)
601         struct thread *td;
602         struct munmap_args *uap;
603 {
604 #ifdef HWPMC_HOOKS
605         struct pmckern_map_out pkm;
606         vm_map_entry_t entry;
607 #endif
608         vm_offset_t addr;
609         vm_size_t size, pageoff;
610         vm_map_t map;
611
612         addr = (vm_offset_t) uap->addr;
613         size = uap->len;
614         if (size == 0)
615                 return (EINVAL);
616
617         pageoff = (addr & PAGE_MASK);
618         addr -= pageoff;
619         size += pageoff;
620         size = (vm_size_t) round_page(size);
621         if (addr + size < addr)
622                 return (EINVAL);
623
624         /*
625          * Check for illegal addresses.  Watch out for address wrap...
626          */
627         map = &td->td_proc->p_vmspace->vm_map;
628         if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
629                 return (EINVAL);
630         vm_map_lock(map);
631 #ifdef HWPMC_HOOKS
632         /*
633          * Inform hwpmc if the address range being unmapped contains
634          * an executable region.
635          */
636         pkm.pm_address = (uintptr_t) NULL;
637         if (vm_map_lookup_entry(map, addr, &entry)) {
638                 for (;
639                      entry != &map->header && entry->start < addr + size;
640                      entry = entry->next) {
641                         if (vm_map_check_protection(map, entry->start,
642                                 entry->end, VM_PROT_EXECUTE) == TRUE) {
643                                 pkm.pm_address = (uintptr_t) addr;
644                                 pkm.pm_size = (size_t) size;
645                                 break;
646                         }
647                 }
648         }
649 #endif
650         vm_map_delete(map, addr, addr + size);
651
652 #ifdef HWPMC_HOOKS
653         /* downgrade the lock to prevent a LOR with the pmc-sx lock */
654         vm_map_lock_downgrade(map);
655         if (pkm.pm_address != (uintptr_t) NULL)
656                 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
657         vm_map_unlock_read(map);
658 #else
659         vm_map_unlock(map);
660 #endif
661         /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
662         return (0);
663 }
664
665 #ifndef _SYS_SYSPROTO_H_
666 struct mprotect_args {
667         const void *addr;
668         size_t len;
669         int prot;
670 };
671 #endif
672 /*
673  * MPSAFE
674  */
675 int
676 sys_mprotect(td, uap)
677         struct thread *td;
678         struct mprotect_args *uap;
679 {
680         vm_offset_t addr;
681         vm_size_t size, pageoff;
682         vm_prot_t prot;
683
684         addr = (vm_offset_t) uap->addr;
685         size = uap->len;
686         prot = uap->prot & VM_PROT_ALL;
687
688         pageoff = (addr & PAGE_MASK);
689         addr -= pageoff;
690         size += pageoff;
691         size = (vm_size_t) round_page(size);
692         if (addr + size < addr)
693                 return (EINVAL);
694
695         switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
696             addr + size, prot, FALSE)) {
697         case KERN_SUCCESS:
698                 return (0);
699         case KERN_PROTECTION_FAILURE:
700                 return (EACCES);
701         case KERN_RESOURCE_SHORTAGE:
702                 return (ENOMEM);
703         }
704         return (EINVAL);
705 }
706
707 #ifndef _SYS_SYSPROTO_H_
708 struct minherit_args {
709         void *addr;
710         size_t len;
711         int inherit;
712 };
713 #endif
714 /*
715  * MPSAFE
716  */
717 int
718 sys_minherit(td, uap)
719         struct thread *td;
720         struct minherit_args *uap;
721 {
722         vm_offset_t addr;
723         vm_size_t size, pageoff;
724         vm_inherit_t inherit;
725
726         addr = (vm_offset_t)uap->addr;
727         size = uap->len;
728         inherit = uap->inherit;
729
730         pageoff = (addr & PAGE_MASK);
731         addr -= pageoff;
732         size += pageoff;
733         size = (vm_size_t) round_page(size);
734         if (addr + size < addr)
735                 return (EINVAL);
736
737         switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
738             addr + size, inherit)) {
739         case KERN_SUCCESS:
740                 return (0);
741         case KERN_PROTECTION_FAILURE:
742                 return (EACCES);
743         }
744         return (EINVAL);
745 }
746
747 #ifndef _SYS_SYSPROTO_H_
748 struct madvise_args {
749         void *addr;
750         size_t len;
751         int behav;
752 };
753 #endif
754
755 /*
756  * MPSAFE
757  */
758 int
759 sys_madvise(td, uap)
760         struct thread *td;
761         struct madvise_args *uap;
762 {
763         vm_offset_t start, end;
764         vm_map_t map;
765         int flags;
766
767         /*
768          * Check for our special case, advising the swap pager we are
769          * "immortal."
770          */
771         if (uap->behav == MADV_PROTECT) {
772                 flags = PPROT_SET;
773                 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
774                     PROC_SPROTECT, &flags));
775         }
776
777         /*
778          * Check for illegal behavior
779          */
780         if (uap->behav < 0 || uap->behav > MADV_CORE)
781                 return (EINVAL);
782         /*
783          * Check for illegal addresses.  Watch out for address wrap... Note
784          * that VM_*_ADDRESS are not constants due to casts (argh).
785          */
786         map = &td->td_proc->p_vmspace->vm_map;
787         if ((vm_offset_t)uap->addr < vm_map_min(map) ||
788             (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
789                 return (EINVAL);
790         if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
791                 return (EINVAL);
792
793         /*
794          * Since this routine is only advisory, we default to conservative
795          * behavior.
796          */
797         start = trunc_page((vm_offset_t) uap->addr);
798         end = round_page((vm_offset_t) uap->addr + uap->len);
799
800         if (vm_map_madvise(map, start, end, uap->behav))
801                 return (EINVAL);
802         return (0);
803 }
804
805 #ifndef _SYS_SYSPROTO_H_
806 struct mincore_args {
807         const void *addr;
808         size_t len;
809         char *vec;
810 };
811 #endif
812
813 /*
814  * MPSAFE
815  */
816 int
817 sys_mincore(td, uap)
818         struct thread *td;
819         struct mincore_args *uap;
820 {
821         vm_offset_t addr, first_addr;
822         vm_offset_t end, cend;
823         pmap_t pmap;
824         vm_map_t map;
825         char *vec;
826         int error = 0;
827         int vecindex, lastvecindex;
828         vm_map_entry_t current;
829         vm_map_entry_t entry;
830         vm_object_t object;
831         vm_paddr_t locked_pa;
832         vm_page_t m;
833         vm_pindex_t pindex;
834         int mincoreinfo;
835         unsigned int timestamp;
836         boolean_t locked;
837
838         /*
839          * Make sure that the addresses presented are valid for user
840          * mode.
841          */
842         first_addr = addr = trunc_page((vm_offset_t) uap->addr);
843         end = addr + (vm_size_t)round_page(uap->len);
844         map = &td->td_proc->p_vmspace->vm_map;
845         if (end > vm_map_max(map) || end < addr)
846                 return (ENOMEM);
847
848         /*
849          * Address of byte vector
850          */
851         vec = uap->vec;
852
853         pmap = vmspace_pmap(td->td_proc->p_vmspace);
854
855         vm_map_lock_read(map);
856 RestartScan:
857         timestamp = map->timestamp;
858
859         if (!vm_map_lookup_entry(map, addr, &entry)) {
860                 vm_map_unlock_read(map);
861                 return (ENOMEM);
862         }
863
864         /*
865          * Do this on a map entry basis so that if the pages are not
866          * in the current processes address space, we can easily look
867          * up the pages elsewhere.
868          */
869         lastvecindex = -1;
870         for (current = entry;
871             (current != &map->header) && (current->start < end);
872             current = current->next) {
873
874                 /*
875                  * check for contiguity
876                  */
877                 if (current->end < end &&
878                     (entry->next == &map->header ||
879                      current->next->start > current->end)) {
880                         vm_map_unlock_read(map);
881                         return (ENOMEM);
882                 }
883
884                 /*
885                  * ignore submaps (for now) or null objects
886                  */
887                 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
888                         current->object.vm_object == NULL)
889                         continue;
890
891                 /*
892                  * limit this scan to the current map entry and the
893                  * limits for the mincore call
894                  */
895                 if (addr < current->start)
896                         addr = current->start;
897                 cend = current->end;
898                 if (cend > end)
899                         cend = end;
900
901                 /*
902                  * scan this entry one page at a time
903                  */
904                 while (addr < cend) {
905                         /*
906                          * Check pmap first, it is likely faster, also
907                          * it can provide info as to whether we are the
908                          * one referencing or modifying the page.
909                          */
910                         object = NULL;
911                         locked_pa = 0;
912                 retry:
913                         m = NULL;
914                         mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
915                         if (locked_pa != 0) {
916                                 /*
917                                  * The page is mapped by this process but not
918                                  * both accessed and modified.  It is also
919                                  * managed.  Acquire the object lock so that
920                                  * other mappings might be examined.
921                                  */
922                                 m = PHYS_TO_VM_PAGE(locked_pa);
923                                 if (m->object != object) {
924                                         if (object != NULL)
925                                                 VM_OBJECT_WUNLOCK(object);
926                                         object = m->object;
927                                         locked = VM_OBJECT_TRYWLOCK(object);
928                                         vm_page_unlock(m);
929                                         if (!locked) {
930                                                 VM_OBJECT_WLOCK(object);
931                                                 vm_page_lock(m);
932                                                 goto retry;
933                                         }
934                                 } else
935                                         vm_page_unlock(m);
936                                 KASSERT(m->valid == VM_PAGE_BITS_ALL,
937                                     ("mincore: page %p is mapped but invalid",
938                                     m));
939                         } else if (mincoreinfo == 0) {
940                                 /*
941                                  * The page is not mapped by this process.  If
942                                  * the object implements managed pages, then
943                                  * determine if the page is resident so that
944                                  * the mappings might be examined.
945                                  */
946                                 if (current->object.vm_object != object) {
947                                         if (object != NULL)
948                                                 VM_OBJECT_WUNLOCK(object);
949                                         object = current->object.vm_object;
950                                         VM_OBJECT_WLOCK(object);
951                                 }
952                                 if (object->type == OBJT_DEFAULT ||
953                                     object->type == OBJT_SWAP ||
954                                     object->type == OBJT_VNODE) {
955                                         pindex = OFF_TO_IDX(current->offset +
956                                             (addr - current->start));
957                                         m = vm_page_lookup(object, pindex);
958                                         if (m == NULL &&
959                                             vm_page_is_cached(object, pindex))
960                                                 mincoreinfo = MINCORE_INCORE;
961                                         if (m != NULL && m->valid == 0)
962                                                 m = NULL;
963                                         if (m != NULL)
964                                                 mincoreinfo = MINCORE_INCORE;
965                                 }
966                         }
967                         if (m != NULL) {
968                                 /* Examine other mappings to the page. */
969                                 if (m->dirty == 0 && pmap_is_modified(m))
970                                         vm_page_dirty(m);
971                                 if (m->dirty != 0)
972                                         mincoreinfo |= MINCORE_MODIFIED_OTHER;
973                                 /*
974                                  * The first test for PGA_REFERENCED is an
975                                  * optimization.  The second test is
976                                  * required because a concurrent pmap
977                                  * operation could clear the last reference
978                                  * and set PGA_REFERENCED before the call to
979                                  * pmap_is_referenced(). 
980                                  */
981                                 if ((m->aflags & PGA_REFERENCED) != 0 ||
982                                     pmap_is_referenced(m) ||
983                                     (m->aflags & PGA_REFERENCED) != 0)
984                                         mincoreinfo |= MINCORE_REFERENCED_OTHER;
985                         }
986                         if (object != NULL)
987                                 VM_OBJECT_WUNLOCK(object);
988
989                         /*
990                          * subyte may page fault.  In case it needs to modify
991                          * the map, we release the lock.
992                          */
993                         vm_map_unlock_read(map);
994
995                         /*
996                          * calculate index into user supplied byte vector
997                          */
998                         vecindex = OFF_TO_IDX(addr - first_addr);
999
1000                         /*
1001                          * If we have skipped map entries, we need to make sure that
1002                          * the byte vector is zeroed for those skipped entries.
1003                          */
1004                         while ((lastvecindex + 1) < vecindex) {
1005                                 ++lastvecindex;
1006                                 error = subyte(vec + lastvecindex, 0);
1007                                 if (error) {
1008                                         error = EFAULT;
1009                                         goto done2;
1010                                 }
1011                         }
1012
1013                         /*
1014                          * Pass the page information to the user
1015                          */
1016                         error = subyte(vec + vecindex, mincoreinfo);
1017                         if (error) {
1018                                 error = EFAULT;
1019                                 goto done2;
1020                         }
1021
1022                         /*
1023                          * If the map has changed, due to the subyte, the previous
1024                          * output may be invalid.
1025                          */
1026                         vm_map_lock_read(map);
1027                         if (timestamp != map->timestamp)
1028                                 goto RestartScan;
1029
1030                         lastvecindex = vecindex;
1031                         addr += PAGE_SIZE;
1032                 }
1033         }
1034
1035         /*
1036          * subyte may page fault.  In case it needs to modify
1037          * the map, we release the lock.
1038          */
1039         vm_map_unlock_read(map);
1040
1041         /*
1042          * Zero the last entries in the byte vector.
1043          */
1044         vecindex = OFF_TO_IDX(end - first_addr);
1045         while ((lastvecindex + 1) < vecindex) {
1046                 ++lastvecindex;
1047                 error = subyte(vec + lastvecindex, 0);
1048                 if (error) {
1049                         error = EFAULT;
1050                         goto done2;
1051                 }
1052         }
1053
1054         /*
1055          * If the map has changed, due to the subyte, the previous
1056          * output may be invalid.
1057          */
1058         vm_map_lock_read(map);
1059         if (timestamp != map->timestamp)
1060                 goto RestartScan;
1061         vm_map_unlock_read(map);
1062 done2:
1063         return (error);
1064 }
1065
1066 #ifndef _SYS_SYSPROTO_H_
1067 struct mlock_args {
1068         const void *addr;
1069         size_t len;
1070 };
1071 #endif
1072 /*
1073  * MPSAFE
1074  */
1075 int
1076 sys_mlock(td, uap)
1077         struct thread *td;
1078         struct mlock_args *uap;
1079 {
1080
1081         return (vm_mlock(td->td_proc, td->td_ucred, uap->addr, uap->len));
1082 }
1083
1084 int
1085 vm_mlock(struct proc *proc, struct ucred *cred, const void *addr0, size_t len)
1086 {
1087         vm_offset_t addr, end, last, start;
1088         vm_size_t npages, size;
1089         vm_map_t map;
1090         unsigned long nsize;
1091         int error;
1092
1093         error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
1094         if (error)
1095                 return (error);
1096         addr = (vm_offset_t)addr0;
1097         size = len;
1098         last = addr + size;
1099         start = trunc_page(addr);
1100         end = round_page(last);
1101         if (last < addr || end < addr)
1102                 return (EINVAL);
1103         npages = atop(end - start);
1104         if (npages > vm_page_max_wired)
1105                 return (ENOMEM);
1106         map = &proc->p_vmspace->vm_map;
1107         PROC_LOCK(proc);
1108         nsize = ptoa(npages + pmap_wired_count(map->pmap));
1109         if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1110                 PROC_UNLOCK(proc);
1111                 return (ENOMEM);
1112         }
1113         PROC_UNLOCK(proc);
1114         if (npages + vm_cnt.v_wire_count > vm_page_max_wired)
1115                 return (EAGAIN);
1116 #ifdef RACCT
1117         PROC_LOCK(proc);
1118         error = racct_set(proc, RACCT_MEMLOCK, nsize);
1119         PROC_UNLOCK(proc);
1120         if (error != 0)
1121                 return (ENOMEM);
1122 #endif
1123         error = vm_map_wire(map, start, end,
1124             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1125 #ifdef RACCT
1126         if (error != KERN_SUCCESS) {
1127                 PROC_LOCK(proc);
1128                 racct_set(proc, RACCT_MEMLOCK,
1129                     ptoa(pmap_wired_count(map->pmap)));
1130                 PROC_UNLOCK(proc);
1131         }
1132 #endif
1133         return (error == KERN_SUCCESS ? 0 : ENOMEM);
1134 }
1135
1136 #ifndef _SYS_SYSPROTO_H_
1137 struct mlockall_args {
1138         int     how;
1139 };
1140 #endif
1141
1142 /*
1143  * MPSAFE
1144  */
1145 int
1146 sys_mlockall(td, uap)
1147         struct thread *td;
1148         struct mlockall_args *uap;
1149 {
1150         vm_map_t map;
1151         int error;
1152
1153         map = &td->td_proc->p_vmspace->vm_map;
1154         error = priv_check(td, PRIV_VM_MLOCK);
1155         if (error)
1156                 return (error);
1157
1158         if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1159                 return (EINVAL);
1160
1161         /*
1162          * If wiring all pages in the process would cause it to exceed
1163          * a hard resource limit, return ENOMEM.
1164          */
1165         if (!old_mlock && uap->how & MCL_CURRENT) {
1166                 PROC_LOCK(td->td_proc);
1167                 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1168                         PROC_UNLOCK(td->td_proc);
1169                         return (ENOMEM);
1170                 }
1171                 PROC_UNLOCK(td->td_proc);
1172         }
1173 #ifdef RACCT
1174         PROC_LOCK(td->td_proc);
1175         error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1176         PROC_UNLOCK(td->td_proc);
1177         if (error != 0)
1178                 return (ENOMEM);
1179 #endif
1180
1181         if (uap->how & MCL_FUTURE) {
1182                 vm_map_lock(map);
1183                 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1184                 vm_map_unlock(map);
1185                 error = 0;
1186         }
1187
1188         if (uap->how & MCL_CURRENT) {
1189                 /*
1190                  * P1003.1-2001 mandates that all currently mapped pages
1191                  * will be memory resident and locked (wired) upon return
1192                  * from mlockall(). vm_map_wire() will wire pages, by
1193                  * calling vm_fault_wire() for each page in the region.
1194                  */
1195                 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1196                     VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1197                 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1198         }
1199 #ifdef RACCT
1200         if (error != KERN_SUCCESS) {
1201                 PROC_LOCK(td->td_proc);
1202                 racct_set(td->td_proc, RACCT_MEMLOCK,
1203                     ptoa(pmap_wired_count(map->pmap)));
1204                 PROC_UNLOCK(td->td_proc);
1205         }
1206 #endif
1207
1208         return (error);
1209 }
1210
1211 #ifndef _SYS_SYSPROTO_H_
1212 struct munlockall_args {
1213         register_t dummy;
1214 };
1215 #endif
1216
1217 /*
1218  * MPSAFE
1219  */
1220 int
1221 sys_munlockall(td, uap)
1222         struct thread *td;
1223         struct munlockall_args *uap;
1224 {
1225         vm_map_t map;
1226         int error;
1227
1228         map = &td->td_proc->p_vmspace->vm_map;
1229         error = priv_check(td, PRIV_VM_MUNLOCK);
1230         if (error)
1231                 return (error);
1232
1233         /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1234         vm_map_lock(map);
1235         vm_map_modflags(map, 0, MAP_WIREFUTURE);
1236         vm_map_unlock(map);
1237
1238         /* Forcibly unwire all pages. */
1239         error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1240             VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1241 #ifdef RACCT
1242         if (error == KERN_SUCCESS) {
1243                 PROC_LOCK(td->td_proc);
1244                 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1245                 PROC_UNLOCK(td->td_proc);
1246         }
1247 #endif
1248
1249         return (error);
1250 }
1251
1252 #ifndef _SYS_SYSPROTO_H_
1253 struct munlock_args {
1254         const void *addr;
1255         size_t len;
1256 };
1257 #endif
1258 /*
1259  * MPSAFE
1260  */
1261 int
1262 sys_munlock(td, uap)
1263         struct thread *td;
1264         struct munlock_args *uap;
1265 {
1266         vm_offset_t addr, end, last, start;
1267         vm_size_t size;
1268 #ifdef RACCT
1269         vm_map_t map;
1270 #endif
1271         int error;
1272
1273         error = priv_check(td, PRIV_VM_MUNLOCK);
1274         if (error)
1275                 return (error);
1276         addr = (vm_offset_t)uap->addr;
1277         size = uap->len;
1278         last = addr + size;
1279         start = trunc_page(addr);
1280         end = round_page(last);
1281         if (last < addr || end < addr)
1282                 return (EINVAL);
1283         error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1284             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1285 #ifdef RACCT
1286         if (error == KERN_SUCCESS) {
1287                 PROC_LOCK(td->td_proc);
1288                 map = &td->td_proc->p_vmspace->vm_map;
1289                 racct_set(td->td_proc, RACCT_MEMLOCK,
1290                     ptoa(pmap_wired_count(map->pmap)));
1291                 PROC_UNLOCK(td->td_proc);
1292         }
1293 #endif
1294         return (error == KERN_SUCCESS ? 0 : ENOMEM);
1295 }
1296
1297 /*
1298  * vm_mmap_vnode()
1299  *
1300  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1301  * operations on vnodes.
1302  *
1303  * For VCHR vnodes, the vnode lock is held over the call to
1304  * vm_mmap_cdev() to keep vp->v_rdev valid.
1305  */
1306 int
1307 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1308     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1309     struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1310     boolean_t *writecounted)
1311 {
1312         struct vattr va;
1313         vm_object_t obj;
1314         vm_offset_t foff;
1315         struct mount *mp;
1316         struct ucred *cred;
1317         int error, flags, locktype;
1318
1319         mp = vp->v_mount;
1320         cred = td->td_ucred;
1321         if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1322                 locktype = LK_EXCLUSIVE;
1323         else
1324                 locktype = LK_SHARED;
1325         if ((error = vget(vp, locktype, td)) != 0)
1326                 return (error);
1327         foff = *foffp;
1328         flags = *flagsp;
1329         obj = vp->v_object;
1330         if (vp->v_type == VREG) {
1331                 /*
1332                  * Get the proper underlying object
1333                  */
1334                 if (obj == NULL) {
1335                         error = EINVAL;
1336                         goto done;
1337                 }
1338                 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1339                         vput(vp);
1340                         vp = (struct vnode *)obj->handle;
1341                         /*
1342                          * Bypass filesystems obey the mpsafety of the
1343                          * underlying fs.  Tmpfs never bypasses.
1344                          */
1345                         error = vget(vp, locktype, td);
1346                         if (error != 0)
1347                                 return (error);
1348                 }
1349                 if (locktype == LK_EXCLUSIVE) {
1350                         *writecounted = TRUE;
1351                         vnode_pager_update_writecount(obj, 0, objsize);
1352                 }
1353         } else if (vp->v_type == VCHR) {
1354                 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1355                     vp->v_rdev, foffp, objp);
1356                 if (error == 0)
1357                         goto mark_atime;
1358                 goto done;
1359         } else {
1360                 error = EINVAL;
1361                 goto done;
1362         }
1363         if ((error = VOP_GETATTR(vp, &va, cred)))
1364                 goto done;
1365 #ifdef MAC
1366         error = mac_vnode_check_mmap(cred, vp, prot, flags);
1367         if (error != 0)
1368                 goto done;
1369 #endif
1370         if ((flags & MAP_SHARED) != 0) {
1371                 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1372                         if (prot & PROT_WRITE) {
1373                                 error = EPERM;
1374                                 goto done;
1375                         }
1376                         *maxprotp &= ~VM_PROT_WRITE;
1377                 }
1378         }
1379         /*
1380          * If it is a regular file without any references
1381          * we do not need to sync it.
1382          * Adjust object size to be the size of actual file.
1383          */
1384         objsize = round_page(va.va_size);
1385         if (va.va_nlink == 0)
1386                 flags |= MAP_NOSYNC;
1387         if (obj->type == OBJT_VNODE)
1388                 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1389                     cred);
1390         else {
1391                 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1392                     ("wrong object type"));
1393                 vm_object_reference(obj);
1394         }
1395         if (obj == NULL) {
1396                 error = ENOMEM;
1397                 goto done;
1398         }
1399         *objp = obj;
1400         *flagsp = flags;
1401
1402 mark_atime:
1403         vfs_mark_atime(vp, cred);
1404
1405 done:
1406         if (error != 0 && *writecounted) {
1407                 *writecounted = FALSE;
1408                 vnode_pager_update_writecount(obj, objsize, 0);
1409         }
1410         vput(vp);
1411         return (error);
1412 }
1413
1414 /*
1415  * vm_mmap_cdev()
1416  *
1417  * MPSAFE
1418  *
1419  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1420  * operations on cdevs.
1421  */
1422 int
1423 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1424     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1425     struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1426 {
1427         vm_object_t obj;
1428         struct cdevsw *dsw;
1429         int error, flags, ref;
1430
1431         flags = *flagsp;
1432
1433         dsw = dev_refthread(cdev, &ref);
1434         if (dsw == NULL)
1435                 return (ENXIO);
1436         if (dsw->d_flags & D_MMAP_ANON) {
1437                 dev_relthread(cdev, ref);
1438                 *maxprotp = VM_PROT_ALL;
1439                 *flagsp |= MAP_ANON;
1440                 return (0);
1441         }
1442         /*
1443          * cdevs do not provide private mappings of any kind.
1444          */
1445         if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1446             (prot & PROT_WRITE) != 0) {
1447                 dev_relthread(cdev, ref);
1448                 return (EACCES);
1449         }
1450         if (flags & (MAP_PRIVATE|MAP_COPY)) {
1451                 dev_relthread(cdev, ref);
1452                 return (EINVAL);
1453         }
1454         /*
1455          * Force device mappings to be shared.
1456          */
1457         flags |= MAP_SHARED;
1458 #ifdef MAC_XXX
1459         error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1460         if (error != 0) {
1461                 dev_relthread(cdev, ref);
1462                 return (error);
1463         }
1464 #endif
1465         /*
1466          * First, try d_mmap_single().  If that is not implemented
1467          * (returns ENODEV), fall back to using the device pager.
1468          * Note that d_mmap_single() must return a reference to the
1469          * object (it needs to bump the reference count of the object
1470          * it returns somehow).
1471          *
1472          * XXX assumes VM_PROT_* == PROT_*
1473          */
1474         error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1475         dev_relthread(cdev, ref);
1476         if (error != ENODEV)
1477                 return (error);
1478         obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1479             td->td_ucred);
1480         if (obj == NULL)
1481                 return (EINVAL);
1482         *objp = obj;
1483         *flagsp = flags;
1484         return (0);
1485 }
1486
1487 /*
1488  * vm_mmap_shm()
1489  *
1490  * MPSAFE
1491  *
1492  * Helper function for vm_mmap.  Perform sanity check specific for mmap
1493  * operations on shm file descriptors.
1494  */
1495 int
1496 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1497     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1498     struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1499 {
1500         int error;
1501
1502         if ((*flagsp & MAP_SHARED) != 0 &&
1503             (*maxprotp & VM_PROT_WRITE) == 0 &&
1504             (prot & PROT_WRITE) != 0)
1505                 return (EACCES);
1506 #ifdef MAC
1507         error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1508         if (error != 0)
1509                 return (error);
1510 #endif
1511         error = shm_mmap(shmfd, objsize, foff, objp);
1512         if (error)
1513                 return (error);
1514         return (0);
1515 }
1516
1517 /*
1518  * vm_mmap()
1519  *
1520  * MPSAFE
1521  *
1522  * Internal version of mmap.  Currently used by mmap, exec, and sys5
1523  * shared memory.  Handle is either a vnode pointer or NULL for MAP_ANON.
1524  */
1525 int
1526 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1527         vm_prot_t maxprot, int flags,
1528         objtype_t handle_type, void *handle,
1529         vm_ooffset_t foff)
1530 {
1531         boolean_t fitit;
1532         vm_object_t object = NULL;
1533         struct thread *td = curthread;
1534         int docow, error, findspace, rv;
1535         boolean_t writecounted;
1536
1537         if (size == 0)
1538                 return (0);
1539
1540         size = round_page(size);
1541
1542         if (map == &td->td_proc->p_vmspace->vm_map) {
1543                 PROC_LOCK(td->td_proc);
1544                 if (map->size + size > lim_cur(td->td_proc, RLIMIT_VMEM)) {
1545                         PROC_UNLOCK(td->td_proc);
1546                         return (ENOMEM);
1547                 }
1548                 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1549                         PROC_UNLOCK(td->td_proc);
1550                         return (ENOMEM);
1551                 }
1552                 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1553                         if (ptoa(pmap_wired_count(map->pmap)) + size >
1554                             lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1555                                 racct_set_force(td->td_proc, RACCT_VMEM,
1556                                     map->size);
1557                                 PROC_UNLOCK(td->td_proc);
1558                                 return (ENOMEM);
1559                         }
1560                         error = racct_set(td->td_proc, RACCT_MEMLOCK,
1561                             ptoa(pmap_wired_count(map->pmap)) + size);
1562                         if (error != 0) {
1563                                 racct_set_force(td->td_proc, RACCT_VMEM,
1564                                     map->size);
1565                                 PROC_UNLOCK(td->td_proc);
1566                                 return (error);
1567                         }
1568                 }
1569                 PROC_UNLOCK(td->td_proc);
1570         }
1571
1572         /*
1573          * We currently can only deal with page aligned file offsets.
1574          * The check is here rather than in the syscall because the
1575          * kernel calls this function internally for other mmaping
1576          * operations (such as in exec) and non-aligned offsets will
1577          * cause pmap inconsistencies...so we want to be sure to
1578          * disallow this in all cases.
1579          */
1580         if (foff & PAGE_MASK)
1581                 return (EINVAL);
1582
1583         if ((flags & MAP_FIXED) == 0) {
1584                 fitit = TRUE;
1585                 *addr = round_page(*addr);
1586         } else {
1587                 if (*addr != trunc_page(*addr))
1588                         return (EINVAL);
1589                 fitit = FALSE;
1590         }
1591         writecounted = FALSE;
1592
1593         /*
1594          * Lookup/allocate object.
1595          */
1596         switch (handle_type) {
1597         case OBJT_DEVICE:
1598                 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1599                     handle, &foff, &object);
1600                 break;
1601         case OBJT_VNODE:
1602                 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1603                     handle, &foff, &object, &writecounted);
1604                 break;
1605         case OBJT_SWAP:
1606                 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1607                     handle, foff, &object);
1608                 break;
1609         case OBJT_DEFAULT:
1610                 if (handle == NULL) {
1611                         error = 0;
1612                         break;
1613                 }
1614                 /* FALLTHROUGH */
1615         default:
1616                 error = EINVAL;
1617                 break;
1618         }
1619         if (error)
1620                 return (error);
1621         if (flags & MAP_ANON) {
1622                 object = NULL;
1623                 docow = 0;
1624                 /*
1625                  * Unnamed anonymous regions always start at 0.
1626                  */
1627                 if (handle == 0)
1628                         foff = 0;
1629         } else if (flags & MAP_PREFAULT_READ)
1630                 docow = MAP_PREFAULT;
1631         else
1632                 docow = MAP_PREFAULT_PARTIAL;
1633
1634         if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1635                 docow |= MAP_COPY_ON_WRITE;
1636         if (flags & MAP_NOSYNC)
1637                 docow |= MAP_DISABLE_SYNCER;
1638         if (flags & MAP_NOCORE)
1639                 docow |= MAP_DISABLE_COREDUMP;
1640         /* Shared memory is also shared with children. */
1641         if (flags & MAP_SHARED)
1642                 docow |= MAP_INHERIT_SHARE;
1643         if (writecounted)
1644                 docow |= MAP_VN_WRITECOUNT;
1645         if (flags & MAP_STACK) {
1646                 if (object != NULL)
1647                         return (EINVAL);
1648                 docow |= MAP_STACK_GROWS_DOWN;
1649         }
1650         if ((flags & MAP_EXCL) != 0)
1651                 docow |= MAP_CHECK_EXCL;
1652
1653         if (fitit) {
1654                 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1655                         findspace = VMFS_SUPER_SPACE;
1656                 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1657                         findspace = VMFS_ALIGNED_SPACE(flags >>
1658                             MAP_ALIGNMENT_SHIFT);
1659                 else
1660                         findspace = VMFS_OPTIMAL_SPACE;
1661                 rv = vm_map_find(map, object, foff, addr, size,
1662 #ifdef MAP_32BIT
1663                     flags & MAP_32BIT ? MAP_32BIT_MAX_ADDR :
1664 #endif
1665                     0, findspace, prot, maxprot, docow);
1666         } else {
1667                 rv = vm_map_fixed(map, object, foff, *addr, size,
1668                     prot, maxprot, docow);
1669         }
1670
1671         if (rv == KERN_SUCCESS) {
1672                 /*
1673                  * If the process has requested that all future mappings
1674                  * be wired, then heed this.
1675                  */
1676                 if (map->flags & MAP_WIREFUTURE) {
1677                         vm_map_wire(map, *addr, *addr + size,
1678                             VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1679                             VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1680                 }
1681         } else {
1682                 /*
1683                  * If this mapping was accounted for in the vnode's
1684                  * writecount, then undo that now.
1685                  */
1686                 if (writecounted)
1687                         vnode_pager_release_writecount(object, 0, size);
1688                 /*
1689                  * Lose the object reference.  Will destroy the
1690                  * object if it's an unnamed anonymous mapping
1691                  * or named anonymous without other references.
1692                  */
1693                 vm_object_deallocate(object);
1694         }
1695         return (vm_mmap_to_errno(rv));
1696 }
1697
1698 /*
1699  * Translate a Mach VM return code to zero on success or the appropriate errno
1700  * on failure.
1701  */
1702 int
1703 vm_mmap_to_errno(int rv)
1704 {
1705
1706         switch (rv) {
1707         case KERN_SUCCESS:
1708                 return (0);
1709         case KERN_INVALID_ADDRESS:
1710         case KERN_NO_SPACE:
1711                 return (ENOMEM);
1712         case KERN_PROTECTION_FAILURE:
1713                 return (EACCES);
1714         default:
1715                 return (EINVAL);
1716         }
1717 }