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