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