xref: /netbsd-src/sys/uvm/uvm_mmap.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: uvm_mmap.c,v 1.72 2003/06/23 21:32:34 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993 The Regents of the University of California.
6  * Copyright (c) 1988 University of Utah.
7  *
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * the Systems Programming Group of the University of Utah Computer
12  * Science Department.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *      This product includes software developed by the Charles D. Cranor,
25  *	Washington University, University of California, Berkeley and
26  *	its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
44  *      @(#)vm_mmap.c   8.5 (Berkeley) 5/19/94
45  * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
46  */
47 
48 /*
49  * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
50  * function.
51  */
52 
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.72 2003/06/23 21:32:34 christos Exp $");
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/file.h>
59 #include <sys/filedesc.h>
60 #include <sys/resourcevar.h>
61 #include <sys/mman.h>
62 #include <sys/mount.h>
63 #include <sys/proc.h>
64 #include <sys/malloc.h>
65 #include <sys/vnode.h>
66 #include <sys/conf.h>
67 #include <sys/stat.h>
68 
69 #include <miscfs/specfs/specdev.h>
70 
71 #include <sys/sa.h>
72 #include <sys/syscallargs.h>
73 
74 #include <uvm/uvm.h>
75 #include <uvm/uvm_device.h>
76 
77 
78 /*
79  * unimplemented VM system calls:
80  */
81 
82 /*
83  * sys_sbrk: sbrk system call.
84  */
85 
86 /* ARGSUSED */
87 int
88 sys_sbrk(l, v, retval)
89 	struct lwp *l;
90 	void *v;
91 	register_t *retval;
92 {
93 #if 0
94 	struct sys_sbrk_args /* {
95 		syscallarg(intptr_t) incr;
96 	} */ *uap = v;
97 #endif
98 
99 	return (ENOSYS);
100 }
101 
102 /*
103  * sys_sstk: sstk system call.
104  */
105 
106 /* ARGSUSED */
107 int
108 sys_sstk(l, v, retval)
109 	struct lwp *l;
110 	void *v;
111 	register_t *retval;
112 {
113 #if 0
114 	struct sys_sstk_args /* {
115 		syscallarg(int) incr;
116 	} */ *uap = v;
117 #endif
118 
119 	return (ENOSYS);
120 }
121 
122 /*
123  * sys_mincore: determine if pages are in core or not.
124  */
125 
126 /* ARGSUSED */
127 int
128 sys_mincore(l, v, retval)
129 	struct lwp *l;
130 	void *v;
131 	register_t *retval;
132 {
133 	struct sys_mincore_args /* {
134 		syscallarg(void *) addr;
135 		syscallarg(size_t) len;
136 		syscallarg(char *) vec;
137 	} */ *uap = v;
138 	struct proc *p = l->l_proc;
139 	struct vm_page *pg;
140 	char *vec, pgi;
141 	struct uvm_object *uobj;
142 	struct vm_amap *amap;
143 	struct vm_anon *anon;
144 	struct vm_map_entry *entry;
145 	vaddr_t start, end, lim;
146 	struct vm_map *map;
147 	vsize_t len;
148 	int error = 0, npgs;
149 
150 	map = &p->p_vmspace->vm_map;
151 
152 	start = (vaddr_t)SCARG(uap, addr);
153 	len = SCARG(uap, len);
154 	vec = SCARG(uap, vec);
155 
156 	if (start & PAGE_MASK)
157 		return (EINVAL);
158 	len = round_page(len);
159 	end = start + len;
160 	if (end <= start)
161 		return (EINVAL);
162 
163 	/*
164 	 * Lock down vec, so our returned status isn't outdated by
165 	 * storing the status byte for a page.
166 	 */
167 
168 	npgs = len >> PAGE_SHIFT;
169 	error = uvm_vslock(p, vec, npgs, VM_PROT_WRITE);
170 	if (error) {
171 		return error;
172 	}
173 	vm_map_lock_read(map);
174 
175 	if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
176 		error = ENOMEM;
177 		goto out;
178 	}
179 
180 	for (/* nothing */;
181 	     entry != &map->header && entry->start < end;
182 	     entry = entry->next) {
183 		KASSERT(!UVM_ET_ISSUBMAP(entry));
184 		KASSERT(start >= entry->start);
185 
186 		/* Make sure there are no holes. */
187 		if (entry->end < end &&
188 		     (entry->next == &map->header ||
189 		      entry->next->start > entry->end)) {
190 			error = ENOMEM;
191 			goto out;
192 		}
193 
194 		lim = end < entry->end ? end : entry->end;
195 
196 		/*
197 		 * Special case for objects with no "real" pages.  Those
198 		 * are always considered resident (mapped devices).
199 		 */
200 
201 		if (UVM_ET_ISOBJ(entry)) {
202 			KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
203 			if (!UVM_OBJ_IS_VNODE(entry->object.uvm_obj)) {
204 				for (/* nothing */; start < lim;
205 				     start += PAGE_SIZE, vec++)
206 					subyte(vec, 1);
207 				continue;
208 			}
209 		}
210 
211 		amap = entry->aref.ar_amap;	/* top layer */
212 		uobj = entry->object.uvm_obj;	/* bottom layer */
213 
214 		if (amap != NULL)
215 			amap_lock(amap);
216 		if (uobj != NULL)
217 			simple_lock(&uobj->vmobjlock);
218 
219 		for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
220 			pgi = 0;
221 			if (amap != NULL) {
222 				/* Check the top layer first. */
223 				anon = amap_lookup(&entry->aref,
224 				    start - entry->start);
225 				/* Don't need to lock anon here. */
226 				if (anon != NULL && anon->u.an_page != NULL) {
227 
228 					/*
229 					 * Anon has the page for this entry
230 					 * offset.
231 					 */
232 
233 					pgi = 1;
234 				}
235 			}
236 			if (uobj != NULL && pgi == 0) {
237 				/* Check the bottom layer. */
238 				pg = uvm_pagelookup(uobj,
239 				    entry->offset + (start - entry->start));
240 				if (pg != NULL) {
241 
242 					/*
243 					 * Object has the page for this entry
244 					 * offset.
245 					 */
246 
247 					pgi = 1;
248 				}
249 			}
250 			(void) subyte(vec, pgi);
251 		}
252 		if (uobj != NULL)
253 			simple_unlock(&uobj->vmobjlock);
254 		if (amap != NULL)
255 			amap_unlock(amap);
256 	}
257 
258  out:
259 	vm_map_unlock_read(map);
260 	uvm_vsunlock(p, SCARG(uap, vec), npgs);
261 	return (error);
262 }
263 
264 /*
265  * sys_mmap: mmap system call.
266  *
267  * => file offset and address may not be page aligned
268  *    - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
269  *    - if address isn't page aligned the mapping starts at trunc_page(addr)
270  *      and the return value is adjusted up by the page offset.
271  */
272 
273 int
274 sys_mmap(l, v, retval)
275 	struct lwp *l;
276 	void *v;
277 	register_t *retval;
278 {
279 	struct sys_mmap_args /* {
280 		syscallarg(caddr_t) addr;
281 		syscallarg(size_t) len;
282 		syscallarg(int) prot;
283 		syscallarg(int) flags;
284 		syscallarg(int) fd;
285 		syscallarg(long) pad;
286 		syscallarg(off_t) pos;
287 	} */ *uap = v;
288 	struct proc *p = l->l_proc;
289 	vaddr_t addr;
290 	struct vattr va;
291 	off_t pos;
292 	vsize_t size, pageoff;
293 	vm_prot_t prot, maxprot;
294 	int flags, fd;
295 	vaddr_t vm_min_address = VM_MIN_ADDRESS;
296 	struct filedesc *fdp = p->p_fd;
297 	struct file *fp;
298 	struct vnode *vp;
299 	void *handle;
300 	int error;
301 
302 	/*
303 	 * first, extract syscall args from the uap.
304 	 */
305 
306 	addr = (vaddr_t)SCARG(uap, addr);
307 	size = (vsize_t)SCARG(uap, len);
308 	prot = SCARG(uap, prot) & VM_PROT_ALL;
309 	flags = SCARG(uap, flags);
310 	fd = SCARG(uap, fd);
311 	pos = SCARG(uap, pos);
312 
313 	/*
314 	 * Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and
315 	 * validate the flags.
316 	 */
317 	if (flags & MAP_COPY)
318 		flags = (flags & ~MAP_COPY) | MAP_PRIVATE;
319 	if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
320 		return (EINVAL);
321 
322 	/*
323 	 * align file position and save offset.  adjust size.
324 	 */
325 
326 	pageoff = (pos & PAGE_MASK);
327 	pos  -= pageoff;
328 	size += pageoff;			/* add offset */
329 	size = (vsize_t)round_page(size);	/* round up */
330 	if ((ssize_t) size < 0)
331 		return (EINVAL);			/* don't allow wrap */
332 
333 	/*
334 	 * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
335 	 */
336 
337 	if (flags & MAP_FIXED) {
338 
339 		/* ensure address and file offset are aligned properly */
340 		addr -= pageoff;
341 		if (addr & PAGE_MASK)
342 			return (EINVAL);
343 
344 		if (VM_MAXUSER_ADDRESS > 0 &&
345 		    (addr + size) > VM_MAXUSER_ADDRESS)
346 			return (EFBIG);
347 		if (vm_min_address > 0 && addr < vm_min_address)
348 			return (EINVAL);
349 		if (addr > addr + size)
350 			return (EOVERFLOW);		/* no wrapping! */
351 
352 	} else if (addr == NULL || !(flags & MAP_TRYFIXED)) {
353 
354 		/*
355 		 * not fixed: make sure we skip over the largest
356 		 * possible heap for non-topdown mapping arrangements.
357 		 * we will refine our guess later (e.g. to account for
358 		 * VAC, etc)
359 		 */
360 
361 		if (addr == 0 ||
362 		    !(p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
363 			addr = MAX(addr,
364 			    VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size));
365 		else
366 			addr = MIN(addr,
367 			    VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size));
368 	}
369 
370 	/*
371 	 * check for file mappings (i.e. not anonymous) and verify file.
372 	 */
373 
374 	if ((flags & MAP_ANON) == 0) {
375 
376 		if ((fp = fd_getfile(fdp, fd)) == NULL)
377 			return (EBADF);
378 
379 		simple_unlock(&fp->f_slock);
380 
381 		if (fp->f_type != DTYPE_VNODE)
382 			return (ENODEV);		/* only mmap vnodes! */
383 		vp = (struct vnode *)fp->f_data;	/* convert to vnode */
384 
385 		if (vp->v_type != VREG && vp->v_type != VCHR &&
386 		    vp->v_type != VBLK)
387 			return (ENODEV);  /* only REG/CHR/BLK support mmap */
388 
389 		if (vp->v_type != VCHR && pos < 0)
390 			return (EINVAL);
391 
392 		if (vp->v_type != VCHR && (pos + size) < pos)
393 			return (EOVERFLOW);		/* no offset wrapping */
394 
395 		/* special case: catch SunOS style /dev/zero */
396 		if (vp->v_type == VCHR && vp->v_rdev == zerodev) {
397 			flags |= MAP_ANON;
398 			goto is_anon;
399 		}
400 
401 		/*
402 		 * Old programs may not select a specific sharing type, so
403 		 * default to an appropriate one.
404 		 *
405 		 * XXX: how does MAP_ANON fit in the picture?
406 		 */
407 		if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
408 #if defined(DEBUG)
409 			printf("WARNING: defaulted mmap() share type to "
410 			   "%s (pid %d command %s)\n", vp->v_type == VCHR ?
411 			   "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
412 			    p->p_comm);
413 #endif
414 			if (vp->v_type == VCHR)
415 				flags |= MAP_SHARED;	/* for a device */
416 			else
417 				flags |= MAP_PRIVATE;	/* for a file */
418 		}
419 
420 		/*
421 		 * MAP_PRIVATE device mappings don't make sense (and aren't
422 		 * supported anyway).  However, some programs rely on this,
423 		 * so just change it to MAP_SHARED.
424 		 */
425 		if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
426 			flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
427 		}
428 
429 		/*
430 		 * now check protection
431 		 */
432 
433 		maxprot = VM_PROT_EXECUTE;
434 
435 		/* check read access */
436 		if (fp->f_flag & FREAD)
437 			maxprot |= VM_PROT_READ;
438 		else if (prot & PROT_READ)
439 			return (EACCES);
440 
441 		/* check write access, shared case first */
442 		if (flags & MAP_SHARED) {
443 			/*
444 			 * if the file is writable, only add PROT_WRITE to
445 			 * maxprot if the file is not immutable, append-only.
446 			 * otherwise, if we have asked for PROT_WRITE, return
447 			 * EPERM.
448 			 */
449 			if (fp->f_flag & FWRITE) {
450 				if ((error =
451 				    VOP_GETATTR(vp, &va, p->p_ucred, p)))
452 					return (error);
453 				if ((va.va_flags & (IMMUTABLE|APPEND)) == 0)
454 					maxprot |= VM_PROT_WRITE;
455 				else if (prot & PROT_WRITE)
456 					return (EPERM);
457 			}
458 			else if (prot & PROT_WRITE)
459 				return (EACCES);
460 		} else {
461 			/* MAP_PRIVATE mappings can always write to */
462 			maxprot |= VM_PROT_WRITE;
463 		}
464 		handle = vp;
465 
466 	} else {		/* MAP_ANON case */
467 		/*
468 		 * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
469 		 */
470 		if (fd != -1)
471 			return (EINVAL);
472 
473  is_anon:		/* label for SunOS style /dev/zero */
474 		handle = NULL;
475 		maxprot = VM_PROT_ALL;
476 		pos = 0;
477 	}
478 
479 	/*
480 	 * XXX (in)sanity check.  We don't do proper datasize checking
481 	 * XXX for anonymous (or private writable) mmap().  However,
482 	 * XXX know that if we're trying to allocate more than the amount
483 	 * XXX remaining under our current data size limit, _that_ should
484 	 * XXX be disallowed.
485 	 */
486 	if ((flags & MAP_ANON) != 0 ||
487 	    ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) {
488 		if (size >
489 		    (p->p_rlimit[RLIMIT_DATA].rlim_cur -
490 		     ctob(p->p_vmspace->vm_dsize))) {
491 			return (ENOMEM);
492 		}
493 	}
494 
495 	/*
496 	 * now let kernel internal function uvm_mmap do the work.
497 	 */
498 
499 	error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
500 	    flags, handle, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
501 
502 	if (error == 0)
503 		/* remember to add offset */
504 		*retval = (register_t)(addr + pageoff);
505 
506 	return (error);
507 }
508 
509 /*
510  * sys___msync13: the msync system call (a front-end for flush)
511  */
512 
513 int
514 sys___msync13(l, v, retval)
515 	struct lwp *l;
516 	void *v;
517 	register_t *retval;
518 {
519 	struct sys___msync13_args /* {
520 		syscallarg(caddr_t) addr;
521 		syscallarg(size_t) len;
522 		syscallarg(int) flags;
523 	} */ *uap = v;
524 	struct proc *p = l->l_proc;
525 	vaddr_t addr;
526 	vsize_t size, pageoff;
527 	struct vm_map *map;
528 	int error, rv, flags, uvmflags;
529 
530 	/*
531 	 * extract syscall args from the uap
532 	 */
533 
534 	addr = (vaddr_t)SCARG(uap, addr);
535 	size = (vsize_t)SCARG(uap, len);
536 	flags = SCARG(uap, flags);
537 
538 	/* sanity check flags */
539 	if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
540 			(flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
541 			(flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
542 	  return (EINVAL);
543 	if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
544 	  flags |= MS_SYNC;
545 
546 	/*
547 	 * align the address to a page boundary and adjust the size accordingly.
548 	 */
549 
550 	pageoff = (addr & PAGE_MASK);
551 	addr -= pageoff;
552 	size += pageoff;
553 	size = (vsize_t)round_page(size);
554 
555 	/* disallow wrap-around. */
556 	if (addr + size < addr)
557 		return (EINVAL);
558 
559 	/*
560 	 * get map
561 	 */
562 
563 	map = &p->p_vmspace->vm_map;
564 
565 	/*
566 	 * XXXCDC: do we really need this semantic?
567 	 *
568 	 * XXX Gak!  If size is zero we are supposed to sync "all modified
569 	 * pages with the region containing addr".  Unfortunately, we
570 	 * don't really keep track of individual mmaps so we approximate
571 	 * by flushing the range of the map entry containing addr.
572 	 * This can be incorrect if the region splits or is coalesced
573 	 * with a neighbor.
574 	 */
575 
576 	if (size == 0) {
577 		struct vm_map_entry *entry;
578 
579 		vm_map_lock_read(map);
580 		rv = uvm_map_lookup_entry(map, addr, &entry);
581 		if (rv == TRUE) {
582 			addr = entry->start;
583 			size = entry->end - entry->start;
584 		}
585 		vm_map_unlock_read(map);
586 		if (rv == FALSE)
587 			return (EINVAL);
588 	}
589 
590 	/*
591 	 * translate MS_ flags into PGO_ flags
592 	 */
593 
594 	uvmflags = PGO_CLEANIT;
595 	if (flags & MS_INVALIDATE)
596 		uvmflags |= PGO_FREE;
597 	if (flags & MS_SYNC)
598 		uvmflags |= PGO_SYNCIO;
599 	else
600 		uvmflags |= PGO_SYNCIO;	 /* XXXCDC: force sync for now! */
601 
602 	error = uvm_map_clean(map, addr, addr+size, uvmflags);
603 	return error;
604 }
605 
606 /*
607  * sys_munmap: unmap a users memory
608  */
609 
610 int
611 sys_munmap(l, v, retval)
612 	struct lwp *l;
613 	void *v;
614 	register_t *retval;
615 {
616 	struct sys_munmap_args /* {
617 		syscallarg(caddr_t) addr;
618 		syscallarg(size_t) len;
619 	} */ *uap = v;
620 	struct proc *p = l->l_proc;
621 	vaddr_t addr;
622 	vsize_t size, pageoff;
623 	struct vm_map *map;
624 	vaddr_t vm_min_address = VM_MIN_ADDRESS;
625 	struct vm_map_entry *dead_entries;
626 
627 	/*
628 	 * get syscall args.
629 	 */
630 
631 	addr = (vaddr_t)SCARG(uap, addr);
632 	size = (vsize_t)SCARG(uap, len);
633 
634 	/*
635 	 * align the address to a page boundary and adjust the size accordingly.
636 	 */
637 
638 	pageoff = (addr & PAGE_MASK);
639 	addr -= pageoff;
640 	size += pageoff;
641 	size = (vsize_t)round_page(size);
642 
643 	if ((int)size < 0)
644 		return (EINVAL);
645 	if (size == 0)
646 		return (0);
647 
648 	/*
649 	 * Check for illegal addresses.  Watch out for address wrap...
650 	 * Note that VM_*_ADDRESS are not constants due to casts (argh).
651 	 */
652 	if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
653 		return (EINVAL);
654 	if (vm_min_address > 0 && addr < vm_min_address)
655 		return (EINVAL);
656 	if (addr > addr + size)
657 		return (EINVAL);
658 	map = &p->p_vmspace->vm_map;
659 
660 	/*
661 	 * interesting system call semantic: make sure entire range is
662 	 * allocated before allowing an unmap.
663 	 */
664 
665 	vm_map_lock(map);
666 #if 0
667 	if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
668 		vm_map_unlock(map);
669 		return (EINVAL);
670 	}
671 #endif
672 	uvm_unmap_remove(map, addr, addr + size, &dead_entries);
673 	vm_map_unlock(map);
674 	if (dead_entries != NULL)
675 		uvm_unmap_detach(dead_entries, 0);
676 	return (0);
677 }
678 
679 /*
680  * sys_mprotect: the mprotect system call
681  */
682 
683 int
684 sys_mprotect(l, v, retval)
685 	struct lwp *l;
686 	void *v;
687 	register_t *retval;
688 {
689 	struct sys_mprotect_args /* {
690 		syscallarg(caddr_t) addr;
691 		syscallarg(int) len;
692 		syscallarg(int) prot;
693 	} */ *uap = v;
694 	struct proc *p = l->l_proc;
695 	vaddr_t addr;
696 	vsize_t size, pageoff;
697 	vm_prot_t prot;
698 	int error;
699 
700 	/*
701 	 * extract syscall args from uap
702 	 */
703 
704 	addr = (vaddr_t)SCARG(uap, addr);
705 	size = (vsize_t)SCARG(uap, len);
706 	prot = SCARG(uap, prot) & VM_PROT_ALL;
707 
708 	/*
709 	 * align the address to a page boundary and adjust the size accordingly.
710 	 */
711 
712 	pageoff = (addr & PAGE_MASK);
713 	addr -= pageoff;
714 	size += pageoff;
715 	size = (vsize_t)round_page(size);
716 
717 	if ((int)size < 0)
718 		return (EINVAL);
719 	error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
720 				FALSE);
721 	return error;
722 }
723 
724 /*
725  * sys_minherit: the minherit system call
726  */
727 
728 int
729 sys_minherit(l, v, retval)
730 	struct lwp *l;
731 	void *v;
732 	register_t *retval;
733 {
734 	struct sys_minherit_args /* {
735 		syscallarg(caddr_t) addr;
736 		syscallarg(int) len;
737 		syscallarg(int) inherit;
738 	} */ *uap = v;
739 	struct proc *p = l->l_proc;
740 	vaddr_t addr;
741 	vsize_t size, pageoff;
742 	vm_inherit_t inherit;
743 	int error;
744 
745 	addr = (vaddr_t)SCARG(uap, addr);
746 	size = (vsize_t)SCARG(uap, len);
747 	inherit = SCARG(uap, inherit);
748 
749 	/*
750 	 * align the address to a page boundary and adjust the size accordingly.
751 	 */
752 
753 	pageoff = (addr & PAGE_MASK);
754 	addr -= pageoff;
755 	size += pageoff;
756 	size = (vsize_t)round_page(size);
757 
758 	if ((int)size < 0)
759 		return (EINVAL);
760 	error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size,
761 				inherit);
762 	return error;
763 }
764 
765 /*
766  * sys_madvise: give advice about memory usage.
767  */
768 
769 /* ARGSUSED */
770 int
771 sys_madvise(l, v, retval)
772 	struct lwp *l;
773 	void *v;
774 	register_t *retval;
775 {
776 	struct sys_madvise_args /* {
777 		syscallarg(caddr_t) addr;
778 		syscallarg(size_t) len;
779 		syscallarg(int) behav;
780 	} */ *uap = v;
781 	struct proc *p = l->l_proc;
782 	vaddr_t addr;
783 	vsize_t size, pageoff;
784 	int advice, error;
785 
786 	addr = (vaddr_t)SCARG(uap, addr);
787 	size = (vsize_t)SCARG(uap, len);
788 	advice = SCARG(uap, behav);
789 
790 	/*
791 	 * align the address to a page boundary, and adjust the size accordingly
792 	 */
793 
794 	pageoff = (addr & PAGE_MASK);
795 	addr -= pageoff;
796 	size += pageoff;
797 	size = (vsize_t)round_page(size);
798 
799 	if ((ssize_t)size <= 0)
800 		return (EINVAL);
801 
802 	switch (advice) {
803 	case MADV_NORMAL:
804 	case MADV_RANDOM:
805 	case MADV_SEQUENTIAL:
806 		error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
807 		    advice);
808 		break;
809 
810 	case MADV_WILLNEED:
811 
812 		/*
813 		 * Activate all these pages, pre-faulting them in if
814 		 * necessary.
815 		 */
816 		/*
817 		 * XXX IMPLEMENT ME.
818 		 * Should invent a "weak" mode for uvm_fault()
819 		 * which would only do the PGO_LOCKED pgo_get().
820 		 */
821 
822 		return (0);
823 
824 	case MADV_DONTNEED:
825 
826 		/*
827 		 * Deactivate all these pages.  We don't need them
828 		 * any more.  We don't, however, toss the data in
829 		 * the pages.
830 		 */
831 
832 		error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
833 		    PGO_DEACTIVATE);
834 		break;
835 
836 	case MADV_FREE:
837 
838 		/*
839 		 * These pages contain no valid data, and may be
840 		 * garbage-collected.  Toss all resources, including
841 		 * any swap space in use.
842 		 */
843 
844 		error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
845 		    PGO_FREE);
846 		break;
847 
848 	case MADV_SPACEAVAIL:
849 
850 		/*
851 		 * XXXMRG What is this?  I think it's:
852 		 *
853 		 *	Ensure that we have allocated backing-store
854 		 *	for these pages.
855 		 *
856 		 * This is going to require changes to the page daemon,
857 		 * as it will free swap space allocated to pages in core.
858 		 * There's also what to do for device/file/anonymous memory.
859 		 */
860 
861 		return (EINVAL);
862 
863 	default:
864 		return (EINVAL);
865 	}
866 
867 	return error;
868 }
869 
870 /*
871  * sys_mlock: memory lock
872  */
873 
874 int
875 sys_mlock(l, v, retval)
876 	struct lwp *l;
877 	void *v;
878 	register_t *retval;
879 {
880 	struct sys_mlock_args /* {
881 		syscallarg(const void *) addr;
882 		syscallarg(size_t) len;
883 	} */ *uap = v;
884 	struct proc *p = l->l_proc;
885 	vaddr_t addr;
886 	vsize_t size, pageoff;
887 	int error;
888 
889 	/*
890 	 * extract syscall args from uap
891 	 */
892 
893 	addr = (vaddr_t)SCARG(uap, addr);
894 	size = (vsize_t)SCARG(uap, len);
895 
896 	/*
897 	 * align the address to a page boundary and adjust the size accordingly
898 	 */
899 
900 	pageoff = (addr & PAGE_MASK);
901 	addr -= pageoff;
902 	size += pageoff;
903 	size = (vsize_t)round_page(size);
904 
905 	/* disallow wrap-around. */
906 	if (addr + size < addr)
907 		return (EINVAL);
908 
909 	if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
910 		return (EAGAIN);
911 
912 #ifdef pmap_wired_count
913 	if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
914 			p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
915 		return (EAGAIN);
916 #else
917 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
918 		return (error);
919 #endif
920 
921 	error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE,
922 	    0);
923 	return error;
924 }
925 
926 /*
927  * sys_munlock: unlock wired pages
928  */
929 
930 int
931 sys_munlock(l, v, retval)
932 	struct lwp *l;
933 	void *v;
934 	register_t *retval;
935 {
936 	struct sys_munlock_args /* {
937 		syscallarg(const void *) addr;
938 		syscallarg(size_t) len;
939 	} */ *uap = v;
940 	struct proc *p = l->l_proc;
941 	vaddr_t addr;
942 	vsize_t size, pageoff;
943 	int error;
944 
945 	/*
946 	 * extract syscall args from uap
947 	 */
948 
949 	addr = (vaddr_t)SCARG(uap, addr);
950 	size = (vsize_t)SCARG(uap, len);
951 
952 	/*
953 	 * align the address to a page boundary, and adjust the size accordingly
954 	 */
955 
956 	pageoff = (addr & PAGE_MASK);
957 	addr -= pageoff;
958 	size += pageoff;
959 	size = (vsize_t)round_page(size);
960 
961 	/* disallow wrap-around. */
962 	if (addr + size < addr)
963 		return (EINVAL);
964 
965 #ifndef pmap_wired_count
966 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
967 		return (error);
968 #endif
969 
970 	error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE,
971 	    0);
972 	return error;
973 }
974 
975 /*
976  * sys_mlockall: lock all pages mapped into an address space.
977  */
978 
979 int
980 sys_mlockall(l, v, retval)
981 	struct lwp *l;
982 	void *v;
983 	register_t *retval;
984 {
985 	struct sys_mlockall_args /* {
986 		syscallarg(int) flags;
987 	} */ *uap = v;
988 	struct proc *p = l->l_proc;
989 	int error, flags;
990 
991 	flags = SCARG(uap, flags);
992 
993 	if (flags == 0 ||
994 	    (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
995 		return (EINVAL);
996 
997 #ifndef pmap_wired_count
998 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
999 		return (error);
1000 #endif
1001 
1002 	error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
1003 	    p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1004 	return (error);
1005 }
1006 
1007 /*
1008  * sys_munlockall: unlock all pages mapped into an address space.
1009  */
1010 
1011 int
1012 sys_munlockall(l, v, retval)
1013 	struct lwp *l;
1014 	void *v;
1015 	register_t *retval;
1016 {
1017 	struct proc *p = l->l_proc;
1018 
1019 	(void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
1020 	return (0);
1021 }
1022 
1023 /*
1024  * uvm_mmap: internal version of mmap
1025  *
1026  * - used by sys_mmap and various framebuffers
1027  * - handle is a vnode pointer or NULL for MAP_ANON
1028  * - caller must page-align the file offset
1029  */
1030 
1031 int
1032 uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit)
1033 	struct vm_map *map;
1034 	vaddr_t *addr;
1035 	vsize_t size;
1036 	vm_prot_t prot, maxprot;
1037 	int flags;
1038 	void *handle;
1039 	voff_t foff;
1040 	vsize_t locklimit;
1041 {
1042 	struct uvm_object *uobj;
1043 	struct vnode *vp;
1044 	vaddr_t align = 0;
1045 	int error;
1046 	int advice = UVM_ADV_NORMAL;
1047 	uvm_flag_t uvmflag = 0;
1048 
1049 	/*
1050 	 * check params
1051 	 */
1052 
1053 	if (size == 0)
1054 		return(0);
1055 	if (foff & PAGE_MASK)
1056 		return(EINVAL);
1057 	if ((prot & maxprot) != prot)
1058 		return(EINVAL);
1059 
1060 	/*
1061 	 * for non-fixed mappings, round off the suggested address.
1062 	 * for fixed mappings, check alignment and zap old mappings.
1063 	 */
1064 
1065 	if ((flags & MAP_FIXED) == 0) {
1066 		*addr = round_page(*addr);
1067 	} else {
1068 		if (*addr & PAGE_MASK)
1069 			return(EINVAL);
1070 		uvmflag |= UVM_FLAG_FIXED;
1071 		(void) uvm_unmap(map, *addr, *addr + size);
1072 	}
1073 
1074 	/*
1075 	 * Try to see if any requested alignment can even be attemped.
1076 	 * Make sure we can express the alignment (asking for a >= 4GB
1077 	 * alignment on an ILP32 architecure make no sense) and the
1078 	 * alignment is at least for a page sized quanitiy.  If the
1079 	 * request was for a fixed mapping, make sure supplied address
1080 	 * adheres to the request alignment.
1081 	 */
1082 	align = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
1083 	if (align) {
1084 		if (align >= sizeof(vaddr_t) * NBBY)
1085 			return(EINVAL);
1086 		align = 1L << align;
1087 		if (align < PAGE_SIZE)
1088 			return(EINVAL);
1089 		if (align >= map->max_offset)
1090 			return(ENOMEM);
1091 		if (flags & MAP_FIXED) {
1092 			if ((*addr & (align-1)) != 0)
1093 				return(EINVAL);
1094 			align = 0;
1095 		}
1096 	}
1097 
1098 	/*
1099 	 * handle anon vs. non-anon mappings.   for non-anon mappings attach
1100 	 * to underlying vm object.
1101 	 */
1102 
1103 	if (flags & MAP_ANON) {
1104 		foff = UVM_UNKNOWN_OFFSET;
1105 		uobj = NULL;
1106 		if ((flags & MAP_SHARED) == 0)
1107 			/* XXX: defer amap create */
1108 			uvmflag |= UVM_FLAG_COPYONW;
1109 		else
1110 			/* shared: create amap now */
1111 			uvmflag |= UVM_FLAG_OVERLAY;
1112 
1113 	} else {
1114 		vp = (struct vnode *)handle;
1115 
1116 		/*
1117 		 * Don't allow mmap for EXEC if the file system
1118 		 * is mounted NOEXEC.
1119 		 */
1120 		if ((prot & PROT_EXEC) != 0 &&
1121 		    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0)
1122 			return (EACCES);
1123 
1124 		if (vp->v_type != VCHR) {
1125 			error = VOP_MMAP(vp, 0, curproc->p_ucred, curproc);
1126 			if (error) {
1127 				return error;
1128 			}
1129 
1130 			uobj = uvn_attach((void *)vp, (flags & MAP_SHARED) ?
1131 			   maxprot : (maxprot & ~VM_PROT_WRITE));
1132 
1133 			/* XXX for now, attach doesn't gain a ref */
1134 			VREF(vp);
1135 
1136 			/*
1137 			 * If the vnode is being mapped with PROT_EXEC,
1138 			 * then mark it as text.
1139 			 */
1140 			if (prot & PROT_EXEC)
1141 				vn_markexec(vp);
1142 		} else {
1143 			uobj = udv_attach((void *) &vp->v_rdev,
1144 			    (flags & MAP_SHARED) ? maxprot :
1145 			    (maxprot & ~VM_PROT_WRITE), foff, size);
1146 			/*
1147 			 * XXX Some devices don't like to be mapped with
1148 			 * XXX PROT_EXEC, but we don't really have a
1149 			 * XXX better way of handling this, right now
1150 			 */
1151 			if (uobj == NULL && (prot & PROT_EXEC) == 0) {
1152 				maxprot &= ~VM_PROT_EXECUTE;
1153 				uobj = udv_attach((void *)&vp->v_rdev,
1154 				    (flags & MAP_SHARED) ? maxprot :
1155 				    (maxprot & ~VM_PROT_WRITE), foff, size);
1156 			}
1157 			advice = UVM_ADV_RANDOM;
1158 		}
1159 		if (uobj == NULL)
1160 			return((vp->v_type == VREG) ? ENOMEM : EINVAL);
1161 		if ((flags & MAP_SHARED) == 0)
1162 			uvmflag |= UVM_FLAG_COPYONW;
1163 	}
1164 
1165 	uvmflag = UVM_MAPFLAG(prot, maxprot,
1166 			(flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
1167 			advice, uvmflag);
1168 	error = uvm_map(map, addr, size, uobj, foff, align, uvmflag);
1169 	if (error) {
1170 		if (uobj)
1171 			uobj->pgops->pgo_detach(uobj);
1172 		return error;
1173 	}
1174 
1175 	/*
1176 	 * POSIX 1003.1b -- if our address space was configured
1177 	 * to lock all future mappings, wire the one we just made.
1178 	 */
1179 
1180 	if (prot == VM_PROT_NONE) {
1181 
1182 		/*
1183 		 * No more work to do in this case.
1184 		 */
1185 
1186 		return (0);
1187 	}
1188 	vm_map_lock(map);
1189 	if (map->flags & VM_MAP_WIREFUTURE) {
1190 		if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax
1191 #ifdef pmap_wired_count
1192 		    || (locklimit != 0 && (size +
1193 		    ptoa(pmap_wired_count(vm_map_pmap(map)))) >
1194 			locklimit)
1195 #endif
1196 		) {
1197 			vm_map_unlock(map);
1198 			uvm_unmap(map, *addr, *addr + size);
1199 			return ENOMEM;
1200 		}
1201 
1202 		/*
1203 		 * uvm_map_pageable() always returns the map unlocked.
1204 		 */
1205 
1206 		error = uvm_map_pageable(map, *addr, *addr + size,
1207 					 FALSE, UVM_LK_ENTER);
1208 		if (error) {
1209 			uvm_unmap(map, *addr, *addr + size);
1210 			return error;
1211 		}
1212 		return (0);
1213 	}
1214 	vm_map_unlock(map);
1215 	return 0;
1216 }
1217