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