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