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