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