ANSIfication (procedure args) cleanup.
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
39  *
40  *      @(#)vm_mmap.c   8.4 (Berkeley) 1/12/94
41  * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
42  * $DragonFly: src/sys/vm/vm_mmap.c,v 1.19 2004/03/23 22:54:32 dillon Exp $
43  */
44
45 /*
46  * Mapped file (mmap) interface to VM
47  */
48
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <sys/filedesc.h>
54 #include <sys/kern_syscall.h>
55 #include <sys/proc.h>
56 #include <sys/resource.h>
57 #include <sys/resourcevar.h>
58 #include <sys/vnode.h>
59 #include <sys/fcntl.h>
60 #include <sys/file.h>
61 #include <sys/mman.h>
62 #include <sys/conf.h>
63 #include <sys/stat.h>
64 #include <sys/vmmeter.h>
65 #include <sys/sysctl.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_param.h>
69 #include <sys/lock.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pager.h>
75 #include <vm/vm_pageout.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_kern.h>
79
80 #include <sys/file2.h>
81
82 static int max_proc_mmap;
83 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
84
85 /*
86  * Set the maximum number of vm_map_entry structures per process.  Roughly
87  * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
88  * of our KVM malloc space still results in generous limits.  We want a 
89  * default that is good enough to prevent the kernel running out of resources
90  * if attacked from compromised user account but generous enough such that
91  * multi-threaded processes are not unduly inconvenienced.
92  */
93
94 static void vmmapentry_rsrc_init (void *);
95 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
96
97 static void
98 vmmapentry_rsrc_init(void *dummy)
99 {
100     max_proc_mmap = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
101                         sizeof(struct vm_map_entry);
102     max_proc_mmap /= 100;
103 }
104
105 /* ARGSUSED */
106 int
107 sbrk(struct sbrk_args *uap)
108 {
109         /* Not yet implemented */
110         return (EOPNOTSUPP);
111 }
112
113 /*
114  * sstk_args(int incr)
115  */
116 /* ARGSUSED */
117 int
118 sstk(struct sstk_args *uap)
119 {
120         /* Not yet implemented */
121         return (EOPNOTSUPP);
122 }
123
124 /* 
125  * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
126  *              long pad, off_t pos)
127  *
128  * Memory Map (mmap) system call.  Note that the file offset
129  * and address are allowed to be NOT page aligned, though if
130  * the MAP_FIXED flag it set, both must have the same remainder
131  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
132  * page-aligned, the actual mapping starts at trunc_page(addr)
133  * and the return value is adjusted up by the page offset.
134  *
135  * Generally speaking, only character devices which are themselves
136  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
137  * there would be no cache coherency between a descriptor and a VM mapping
138  * both to the same character device.
139  *
140  * Block devices can be mmap'd no matter what they represent.  Cache coherency
141  * is maintained as long as you do not write directly to the underlying
142  * character device.
143  */
144
145 int
146 kern_mmap(caddr_t uaddr, size_t ulen, int uprot, int uflags, int fd, 
147     off_t upos, void **res)
148 {
149         struct thread *td = curthread;
150         struct proc *p = td->td_proc;
151         struct filedesc *fdp = p->p_fd;
152         struct file *fp = NULL;
153         struct vnode *vp;
154         vm_offset_t addr;
155         vm_size_t size, pageoff;
156         vm_prot_t prot, maxprot;
157         void *handle;
158         int flags, error;
159         int disablexworkaround;
160         off_t pos;
161         struct vmspace *vms = p->p_vmspace;
162         vm_object_t obj;
163
164         KKASSERT(p);
165
166         addr = (vm_offset_t) uaddr;
167         size = ulen;
168         prot = uprot & VM_PROT_ALL;
169         flags = uflags;
170         pos = upos;
171
172         /* make sure mapping fits into numeric range etc */
173         if ((ssize_t) ulen < 0 ||
174             ((flags & MAP_ANON) && fd != -1))
175                 return (EINVAL);
176
177         if (flags & MAP_STACK) {
178                 if ((fd != -1) ||
179                     ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
180                         return (EINVAL);
181                 flags |= MAP_ANON;
182                 pos = 0;
183         }
184
185         /*
186          * Align the file position to a page boundary,
187          * and save its page offset component.
188          */
189         pageoff = (pos & PAGE_MASK);
190         pos -= pageoff;
191
192         /* Adjust size for rounding (on both ends). */
193         size += pageoff;                        /* low end... */
194         size = (vm_size_t) round_page(size);    /* hi end */
195
196         /*
197          * Check for illegal addresses.  Watch out for address wrap... Note
198          * that VM_*_ADDRESS are not constants due to casts (argh).
199          */
200         if (flags & MAP_FIXED) {
201                 /*
202                  * The specified address must have the same remainder
203                  * as the file offset taken modulo PAGE_SIZE, so it
204                  * should be aligned after adjustment by pageoff.
205                  */
206                 addr -= pageoff;
207                 if (addr & PAGE_MASK)
208                         return (EINVAL);
209                 /* Address range must be all in user VM space. */
210                 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
211                         return (EINVAL);
212 #ifndef i386
213                 if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
214                         return (EINVAL);
215 #endif
216                 if (addr + size < addr)
217                         return (EINVAL);
218         }
219         /*
220          * XXX for non-fixed mappings where no hint is provided or
221          * the hint would fall in the potential heap space,
222          * place it after the end of the largest possible heap.
223          *
224          * There should really be a pmap call to determine a reasonable
225          * location.
226          */
227         else if (addr == 0 ||
228             (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
229              addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz)))
230                 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
231
232         if (flags & MAP_ANON) {
233                 /*
234                  * Mapping blank space is trivial.
235                  */
236                 handle = NULL;
237                 maxprot = VM_PROT_ALL;
238                 pos = 0;
239         } else {
240                 /*
241                  * Mapping file, get fp for validation. Obtain vnode and make
242                  * sure it is of appropriate type.
243                  */
244                 if (((unsigned) fd) >= fdp->fd_nfiles ||
245                     (fp = fdp->fd_ofiles[fd]) == NULL)
246                         return (EBADF);
247                 if (fp->f_type != DTYPE_VNODE)
248                         return (EINVAL);
249                 /*
250                  * POSIX shared-memory objects are defined to have
251                  * kernel persistence, and are not defined to support
252                  * read(2)/write(2) -- or even open(2).  Thus, we can
253                  * use MAP_ASYNC to trade on-disk coherence for speed.
254                  * The shm_open(3) library routine turns on the FPOSIXSHM
255                  * flag to request this behavior.
256                  */
257                 if (fp->f_flag & FPOSIXSHM)
258                         flags |= MAP_NOSYNC;
259                 vp = (struct vnode *) fp->f_data;
260                 if (vp->v_type != VREG && vp->v_type != VCHR)
261                         return (EINVAL);
262                 if (vp->v_type == VREG) {
263                         /*
264                          * Get the proper underlying object
265                          */
266                         if (VOP_GETVOBJECT(vp, &obj) != 0)
267                                 return (EINVAL);
268                         vp = (struct vnode*)obj->handle;
269                 }
270
271                 /*
272                  * don't let the descriptor disappear on us if we block
273                  */
274                 fhold(fp);
275
276                 /*
277                  * XXX hack to handle use of /dev/zero to map anon memory (ala
278                  * SunOS).
279                  */
280                 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
281                         handle = NULL;
282                         maxprot = VM_PROT_ALL;
283                         flags |= MAP_ANON;
284                         pos = 0;
285                 } else {
286                         /*
287                          * cdevs does not provide private mappings of any kind.
288                          */
289                         /*
290                          * However, for XIG X server to continue to work,
291                          * we should allow the superuser to do it anyway.
292                          * We only allow it at securelevel < 1.
293                          * (Because the XIG X server writes directly to video
294                          * memory via /dev/mem, it should never work at any
295                          * other securelevel.
296                          * XXX this will have to go
297                          */
298                         if (securelevel >= 1)
299                                 disablexworkaround = 1;
300                         else
301                                 disablexworkaround = suser(td);
302                         if (vp->v_type == VCHR && disablexworkaround &&
303                             (flags & (MAP_PRIVATE|MAP_COPY))) {
304                                 error = EINVAL;
305                                 goto done;
306                         }
307                         /*
308                          * Ensure that file and memory protections are
309                          * compatible.  Note that we only worry about
310                          * writability if mapping is shared; in this case,
311                          * current and max prot are dictated by the open file.
312                          * XXX use the vnode instead?  Problem is: what
313                          * credentials do we use for determination? What if
314                          * proc does a setuid?
315                          */
316                         maxprot = VM_PROT_EXECUTE;      /* ??? */
317                         if (fp->f_flag & FREAD) {
318                                 maxprot |= VM_PROT_READ;
319                         } else if (prot & PROT_READ) {
320                                 error = EACCES;
321                                 goto done;
322                         }
323                         /*
324                          * If we are sharing potential changes (either via
325                          * MAP_SHARED or via the implicit sharing of character
326                          * device mappings), and we are trying to get write
327                          * permission although we opened it without asking
328                          * for it, bail out.  Check for superuser, only if
329                          * we're at securelevel < 1, to allow the XIG X server
330                          * to continue to work.
331                          */
332
333                         if ((flags & MAP_SHARED) != 0 ||
334                             (vp->v_type == VCHR && disablexworkaround)) {
335                                 if ((fp->f_flag & FWRITE) != 0) {
336                                         struct vattr va;
337                                         if ((error = VOP_GETATTR(vp, &va, td))) {
338                                                 goto done;
339                                         }
340                                         if ((va.va_flags &
341                                             (IMMUTABLE|APPEND)) == 0) {
342                                                 maxprot |= VM_PROT_WRITE;
343                                         } else if (prot & PROT_WRITE) {
344                                                 error = EPERM;
345                                                 goto done;
346                                         }
347                                 } else if ((prot & PROT_WRITE) != 0) {
348                                         error = EACCES;
349                                         goto done;
350                                 }
351                         } else {
352                                 maxprot |= VM_PROT_WRITE;
353                         }
354                         handle = (void *)vp;
355                 }
356         }
357
358         /*
359          * Do not allow more then a certain number of vm_map_entry structures
360          * per process.  Scale with the number of rforks sharing the map
361          * to make the limit reasonable for threads.
362          */
363         if (max_proc_mmap && 
364             vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
365                 error = ENOMEM;
366                 goto done;
367         }
368
369         error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
370             flags, handle, pos);
371         if (error == 0)
372                 *res = (void *)(addr + pageoff);
373 done:
374         if (fp)
375                 fdrop(fp, td);
376         return (error);
377 }
378
379 int
380 mmap(struct mmap_args *uap)
381 {
382         int error;
383
384         error = kern_mmap(uap->addr, uap->len, uap->prot, uap->flags,
385             uap->fd, uap->pos, &uap->sysmsg_resultp);
386
387         return (error);
388 }
389
390 /*
391  * msync_args(void *addr, int len, int flags)
392  */
393 int
394 msync(struct msync_args *uap)
395 {
396         struct proc *p = curproc;
397         vm_offset_t addr;
398         vm_size_t size, pageoff;
399         int flags;
400         vm_map_t map;
401         int rv;
402
403         addr = (vm_offset_t) uap->addr;
404         size = uap->len;
405         flags = uap->flags;
406
407         pageoff = (addr & PAGE_MASK);
408         addr -= pageoff;
409         size += pageoff;
410         size = (vm_size_t) round_page(size);
411         if (addr + size < addr)
412                 return(EINVAL);
413
414         if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
415                 return (EINVAL);
416
417         map = &p->p_vmspace->vm_map;
418
419         /*
420          * XXX Gak!  If size is zero we are supposed to sync "all modified
421          * pages with the region containing addr".  Unfortunately, we don't
422          * really keep track of individual mmaps so we approximate by flushing
423          * the range of the map entry containing addr. This can be incorrect
424          * if the region splits or is coalesced with a neighbor.
425          */
426         if (size == 0) {
427                 vm_map_entry_t entry;
428
429                 vm_map_lock_read(map);
430                 rv = vm_map_lookup_entry(map, addr, &entry);
431                 vm_map_unlock_read(map);
432                 if (rv == FALSE)
433                         return (EINVAL);
434                 addr = entry->start;
435                 size = entry->end - entry->start;
436         }
437
438         /*
439          * Clean the pages and interpret the return value.
440          */
441         rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0,
442             (flags & MS_INVALIDATE) != 0);
443
444         switch (rv) {
445         case KERN_SUCCESS:
446                 break;
447         case KERN_INVALID_ADDRESS:
448                 return (EINVAL);        /* Sun returns ENOMEM? */
449         case KERN_FAILURE:
450                 return (EIO);
451         default:
452                 return (EINVAL);
453         }
454
455         return (0);
456 }
457
458 /*
459  * munmap_args(void *addr, size_t len)
460  */
461 int
462 munmap(struct munmap_args *uap)
463 {
464         struct proc *p = curproc;
465         vm_offset_t addr;
466         vm_size_t size, pageoff;
467         vm_map_t map;
468
469         addr = (vm_offset_t) uap->addr;
470         size = uap->len;
471
472         pageoff = (addr & PAGE_MASK);
473         addr -= pageoff;
474         size += pageoff;
475         size = (vm_size_t) round_page(size);
476         if (addr + size < addr)
477                 return(EINVAL);
478
479         if (size == 0)
480                 return (0);
481
482         /*
483          * Check for illegal addresses.  Watch out for address wrap... Note
484          * that VM_*_ADDRESS are not constants due to casts (argh).
485          */
486         if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
487                 return (EINVAL);
488 #ifndef i386
489         if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
490                 return (EINVAL);
491 #endif
492         map = &p->p_vmspace->vm_map;
493         /*
494          * Make sure entire range is allocated.
495          */
496         if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
497                 return (EINVAL);
498         /* returns nothing but KERN_SUCCESS anyway */
499         (void) vm_map_remove(map, addr, addr + size);
500         return (0);
501 }
502
503 #if 0
504 void
505 munmapfd(p, fd)
506         struct proc *p;
507         int fd;
508 {
509         /*
510          * XXX should unmap any regions mapped to this file
511          */
512         p->p_fd->fd_ofileflags[fd] &= ~UF_MAPPED;
513 }
514 #endif
515
516 /*
517  * mprotect_args(const void *addr, size_t len, int prot)
518  */
519 int
520 mprotect(struct mprotect_args *uap)
521 {
522         struct proc *p = curproc;
523         vm_offset_t addr;
524         vm_size_t size, pageoff;
525         vm_prot_t prot;
526
527         addr = (vm_offset_t) uap->addr;
528         size = uap->len;
529         prot = uap->prot & VM_PROT_ALL;
530 #if defined(VM_PROT_READ_IS_EXEC)
531         if (prot & VM_PROT_READ)
532                 prot |= VM_PROT_EXECUTE;
533 #endif
534
535         pageoff = (addr & PAGE_MASK);
536         addr -= pageoff;
537         size += pageoff;
538         size = (vm_size_t) round_page(size);
539         if (addr + size < addr)
540                 return(EINVAL);
541
542         switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
543                 FALSE)) {
544         case KERN_SUCCESS:
545                 return (0);
546         case KERN_PROTECTION_FAILURE:
547                 return (EACCES);
548         }
549         return (EINVAL);
550 }
551
552 /*
553  * minherit_args(void *addr, size_t len, int inherit)
554  */
555 int
556 minherit(struct minherit_args *uap)
557 {
558         struct proc *p = curproc;
559         vm_offset_t addr;
560         vm_size_t size, pageoff;
561         vm_inherit_t inherit;
562
563         addr = (vm_offset_t)uap->addr;
564         size = uap->len;
565         inherit = uap->inherit;
566
567         pageoff = (addr & PAGE_MASK);
568         addr -= pageoff;
569         size += pageoff;
570         size = (vm_size_t) round_page(size);
571         if (addr + size < addr)
572                 return(EINVAL);
573
574         switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
575             inherit)) {
576         case KERN_SUCCESS:
577                 return (0);
578         case KERN_PROTECTION_FAILURE:
579                 return (EACCES);
580         }
581         return (EINVAL);
582 }
583
584 /*
585  * madvise_args(void *addr, size_t len, int behav)
586  */
587 /* ARGSUSED */
588 int
589 madvise(struct madvise_args *uap)
590 {
591         struct proc *p = curproc;
592         vm_offset_t start, end;
593
594         /*
595          * Check for illegal behavior
596          */
597         if (uap->behav < 0 || uap->behav > MADV_CORE)
598                 return (EINVAL);
599         /*
600          * Check for illegal addresses.  Watch out for address wrap... Note
601          * that VM_*_ADDRESS are not constants due to casts (argh).
602          */
603         if (VM_MAXUSER_ADDRESS > 0 &&
604                 ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS)
605                 return (EINVAL);
606 #ifndef i386
607         if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS)
608                 return (EINVAL);
609 #endif
610         if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
611                 return (EINVAL);
612
613         /*
614          * Since this routine is only advisory, we default to conservative
615          * behavior.
616          */
617         start = trunc_page((vm_offset_t) uap->addr);
618         end = round_page((vm_offset_t) uap->addr + uap->len);
619         
620         if (vm_map_madvise(&p->p_vmspace->vm_map, start, end, uap->behav))
621                 return (EINVAL);
622         return (0);
623 }
624
625 /*
626  * mincore_args(const void *addr, size_t len, char *vec)
627  */
628 /* ARGSUSED */
629 int
630 mincore(struct mincore_args *uap)
631 {
632         struct proc *p = curproc;
633         vm_offset_t addr, first_addr;
634         vm_offset_t end, cend;
635         pmap_t pmap;
636         vm_map_t map;
637         char *vec;
638         int error;
639         int vecindex, lastvecindex;
640         vm_map_entry_t current;
641         vm_map_entry_t entry;
642         int mincoreinfo;
643         unsigned int timestamp;
644
645         /*
646          * Make sure that the addresses presented are valid for user
647          * mode.
648          */
649         first_addr = addr = trunc_page((vm_offset_t) uap->addr);
650         end = addr + (vm_size_t)round_page(uap->len);
651         if (VM_MAXUSER_ADDRESS > 0 && end > VM_MAXUSER_ADDRESS)
652                 return (EINVAL);
653         if (end < addr)
654                 return (EINVAL);
655
656         /*
657          * Address of byte vector
658          */
659         vec = uap->vec;
660
661         map = &p->p_vmspace->vm_map;
662         pmap = vmspace_pmap(p->p_vmspace);
663
664         vm_map_lock_read(map);
665 RestartScan:
666         timestamp = map->timestamp;
667
668         if (!vm_map_lookup_entry(map, addr, &entry))
669                 entry = entry->next;
670
671         /*
672          * Do this on a map entry basis so that if the pages are not
673          * in the current processes address space, we can easily look
674          * up the pages elsewhere.
675          */
676         lastvecindex = -1;
677         for(current = entry;
678                 (current != &map->header) && (current->start < end);
679                 current = current->next) {
680
681                 /*
682                  * ignore submaps (for now) or null objects
683                  */
684                 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
685                         current->object.vm_object == NULL)
686                         continue;
687                 
688                 /*
689                  * limit this scan to the current map entry and the
690                  * limits for the mincore call
691                  */
692                 if (addr < current->start)
693                         addr = current->start;
694                 cend = current->end;
695                 if (cend > end)
696                         cend = end;
697
698                 /*
699                  * scan this entry one page at a time
700                  */
701                 while(addr < cend) {
702                         /*
703                          * Check pmap first, it is likely faster, also
704                          * it can provide info as to whether we are the
705                          * one referencing or modifying the page.
706                          */
707                         mincoreinfo = pmap_mincore(pmap, addr);
708                         if (!mincoreinfo) {
709                                 vm_pindex_t pindex;
710                                 vm_ooffset_t offset;
711                                 vm_page_t m;
712                                 /*
713                                  * calculate the page index into the object
714                                  */
715                                 offset = current->offset + (addr - current->start);
716                                 pindex = OFF_TO_IDX(offset);
717                                 m = vm_page_lookup(current->object.vm_object,
718                                         pindex);
719                                 /*
720                                  * if the page is resident, then gather information about
721                                  * it.
722                                  */
723                                 if (m && m->valid) {
724                                         mincoreinfo = MINCORE_INCORE;
725                                         if (m->dirty ||
726                                                 pmap_is_modified(m))
727                                                 mincoreinfo |= MINCORE_MODIFIED_OTHER;
728                                         if ((m->flags & PG_REFERENCED) ||
729                                                 pmap_ts_referenced(m)) {
730                                                 vm_page_flag_set(m, PG_REFERENCED);
731                                                 mincoreinfo |= MINCORE_REFERENCED_OTHER;
732                                         }
733                                 }
734                         }
735
736                         /*
737                          * subyte may page fault.  In case it needs to modify
738                          * the map, we release the lock.
739                          */
740                         vm_map_unlock_read(map);
741
742                         /*
743                          * calculate index into user supplied byte vector
744                          */
745                         vecindex = OFF_TO_IDX(addr - first_addr);
746
747                         /*
748                          * If we have skipped map entries, we need to make sure that
749                          * the byte vector is zeroed for those skipped entries.
750                          */
751                         while((lastvecindex + 1) < vecindex) {
752                                 error = subyte( vec + lastvecindex, 0);
753                                 if (error) {
754                                         return (EFAULT);
755                                 }
756                                 ++lastvecindex;
757                         }
758
759                         /*
760                          * Pass the page information to the user
761                          */
762                         error = subyte( vec + vecindex, mincoreinfo);
763                         if (error) {
764                                 return (EFAULT);
765                         }
766
767                         /*
768                          * If the map has changed, due to the subyte, the previous
769                          * output may be invalid.
770                          */
771                         vm_map_lock_read(map);
772                         if (timestamp != map->timestamp)
773                                 goto RestartScan;
774
775                         lastvecindex = vecindex;
776                         addr += PAGE_SIZE;
777                 }
778         }
779
780         /*
781          * subyte may page fault.  In case it needs to modify
782          * the map, we release the lock.
783          */
784         vm_map_unlock_read(map);
785
786         /*
787          * Zero the last entries in the byte vector.
788          */
789         vecindex = OFF_TO_IDX(end - first_addr);
790         while((lastvecindex + 1) < vecindex) {
791                 error = subyte( vec + lastvecindex, 0);
792                 if (error) {
793                         return (EFAULT);
794                 }
795                 ++lastvecindex;
796         }
797         
798         /*
799          * If the map has changed, due to the subyte, the previous
800          * output may be invalid.
801          */
802         vm_map_lock_read(map);
803         if (timestamp != map->timestamp)
804                 goto RestartScan;
805         vm_map_unlock_read(map);
806
807         return (0);
808 }
809
810 /*
811  * mlock_args(const void *addr, size_t len)
812  */
813 int
814 mlock(struct mlock_args *uap)
815 {
816         vm_offset_t addr;
817         vm_size_t size, pageoff;
818         int error;
819         struct proc *p = curproc;
820
821         addr = (vm_offset_t) uap->addr;
822         size = uap->len;
823
824         pageoff = (addr & PAGE_MASK);
825         addr -= pageoff;
826         size += pageoff;
827         size = (vm_size_t) round_page(size);
828
829         /* disable wrap around */
830         if (addr + size < addr)
831                 return (EINVAL);
832
833         if (atop(size) + vmstats.v_wire_count > vm_page_max_wired)
834                 return (EAGAIN);
835
836 #ifdef pmap_wired_count
837         if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
838             p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
839                 return (ENOMEM);
840 #else
841         error = suser_cred(p->p_ucred, 0);
842         if (error)
843                 return (error);
844 #endif
845
846         error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE);
847         return (error == KERN_SUCCESS ? 0 : ENOMEM);
848 }
849
850 /*
851  * mlockall_args(int how)
852  */
853 int
854 mlockall(struct mlockall_args *uap)
855 {
856         return 0;
857 }
858
859 /*
860  * munlockall_args(void)
861  */
862 int
863 munlockall(struct munlockall_args *uap)
864 {
865         return 0;
866 }
867
868 /*
869  * munlock_args(const void *addr, size_t len)
870  */
871 int
872 munlock(struct munlock_args *uap)
873 {
874         struct thread *td = curthread;
875         struct proc *p = td->td_proc;
876         vm_offset_t addr;
877         vm_size_t size, pageoff;
878         int error;
879
880         addr = (vm_offset_t) uap->addr;
881         size = uap->len;
882
883         pageoff = (addr & PAGE_MASK);
884         addr -= pageoff;
885         size += pageoff;
886         size = (vm_size_t) round_page(size);
887
888         /* disable wrap around */
889         if (addr + size < addr)
890                 return (EINVAL);
891
892 #ifndef pmap_wired_count
893         error = suser(td);
894         if (error)
895                 return (error);
896 #endif
897
898         error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE);
899         return (error == KERN_SUCCESS ? 0 : ENOMEM);
900 }
901
902 /*
903  * Internal version of mmap.
904  * Currently used by mmap, exec, and sys5 shared memory.
905  * Handle is either a vnode pointer or NULL for MAP_ANON.
906  */
907 int
908 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
909         vm_prot_t maxprot, int flags,
910         void *handle,
911         vm_ooffset_t foff)
912 {
913         boolean_t fitit;
914         vm_object_t object;
915         struct vnode *vp = NULL;
916         objtype_t type;
917         int rv = KERN_SUCCESS;
918         vm_ooffset_t objsize;
919         int docow;
920         struct thread *td = curthread;  /* XXX */
921         struct proc *p = td->td_proc;
922
923         KKASSERT(p);
924
925         if (size == 0)
926                 return (0);
927
928         objsize = size = round_page(size);
929
930         if (p->p_vmspace->vm_map.size + size >
931             p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
932                 return(ENOMEM);
933         }
934
935         /*
936          * We currently can only deal with page aligned file offsets.
937          * The check is here rather than in the syscall because the
938          * kernel calls this function internally for other mmaping
939          * operations (such as in exec) and non-aligned offsets will
940          * cause pmap inconsistencies...so we want to be sure to
941          * disallow this in all cases.
942          */
943         if (foff & PAGE_MASK)
944                 return (EINVAL);
945
946         if ((flags & MAP_FIXED) == 0) {
947                 fitit = TRUE;
948                 *addr = round_page(*addr);
949         } else {
950                 if (*addr != trunc_page(*addr))
951                         return (EINVAL);
952                 fitit = FALSE;
953                 (void) vm_map_remove(map, *addr, *addr + size);
954         }
955
956         /*
957          * Lookup/allocate object.
958          */
959         if (flags & MAP_ANON) {
960                 type = OBJT_DEFAULT;
961                 /*
962                  * Unnamed anonymous regions always start at 0.
963                  */
964                 if (handle == 0)
965                         foff = 0;
966         } else {
967                 vp = (struct vnode *) handle;
968                 if (vp->v_type == VCHR) {
969                         type = OBJT_DEVICE;
970                         handle = (void *)(intptr_t)vp->v_rdev;
971                 } else {
972                         struct vattr vat;
973                         int error;
974
975                         error = VOP_GETATTR(vp, &vat, td);
976                         if (error)
977                                 return (error);
978                         objsize = round_page(vat.va_size);
979                         type = OBJT_VNODE;
980                         /*
981                          * if it is a regular file without any references
982                          * we do not need to sync it.
983                          */
984                         if (vp->v_type == VREG && vat.va_nlink == 0) {
985                                 flags |= MAP_NOSYNC;
986                         }
987                 }
988         }
989
990         if (handle == NULL) {
991                 object = NULL;
992                 docow = 0;
993         } else {
994                 object = vm_pager_allocate(type,
995                         handle, objsize, prot, foff);
996                 if (object == NULL)
997                         return (type == OBJT_DEVICE ? EINVAL : ENOMEM);
998                 docow = MAP_PREFAULT_PARTIAL;
999         }
1000
1001         /*
1002          * Force device mappings to be shared.
1003          */
1004         if (type == OBJT_DEVICE || type == OBJT_PHYS) {
1005                 flags &= ~(MAP_PRIVATE|MAP_COPY);
1006                 flags |= MAP_SHARED;
1007         }
1008
1009         if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1010                 docow |= MAP_COPY_ON_WRITE;
1011         if (flags & MAP_NOSYNC)
1012                 docow |= MAP_DISABLE_SYNCER;
1013         if (flags & MAP_NOCORE)
1014                 docow |= MAP_DISABLE_COREDUMP;
1015
1016 #if defined(VM_PROT_READ_IS_EXEC)
1017         if (prot & VM_PROT_READ)
1018                 prot |= VM_PROT_EXECUTE;
1019
1020         if (maxprot & VM_PROT_READ)
1021                 maxprot |= VM_PROT_EXECUTE;
1022 #endif
1023
1024         if (fitit) {
1025                 *addr = pmap_addr_hint(object, *addr, size);
1026         }
1027
1028         if (flags & MAP_STACK)
1029                 rv = vm_map_stack (map, *addr, size, prot,
1030                                    maxprot, docow);
1031         else
1032                 rv = vm_map_find(map, object, foff, addr, size, fitit,
1033                                  prot, maxprot, docow);
1034
1035         if (rv != KERN_SUCCESS) {
1036                 /*
1037                  * Lose the object reference. Will destroy the
1038                  * object if it's an unnamed anonymous mapping
1039                  * or named anonymous without other references.
1040                  */
1041                 vm_object_deallocate(object);
1042                 goto out;
1043         }
1044
1045         /*
1046          * Shared memory is also shared with children.
1047          */
1048         if (flags & (MAP_SHARED|MAP_INHERIT)) {
1049                 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1050                 if (rv != KERN_SUCCESS) {
1051                         (void) vm_map_remove(map, *addr, *addr + size);
1052                         goto out;
1053                 }
1054         }
1055 out:
1056         switch (rv) {
1057         case KERN_SUCCESS:
1058                 return (0);
1059         case KERN_INVALID_ADDRESS:
1060         case KERN_NO_SPACE:
1061                 return (ENOMEM);
1062         case KERN_PROTECTION_FAILURE:
1063                 return (EACCES);
1064         default:
1065                 return (EINVAL);
1066         }
1067 }