uvm_mmap.c revision 1.24 1 /* $NetBSD: uvm_mmap.c,v 1.24 1999/06/17 21:05:19 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(int) 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 mapped devices; these are always
201 * considered resident.
202 */
203 if (UVM_ET_ISOBJ(entry)) {
204 extern struct uvm_pagerops uvm_deviceops; /* XXX */
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 == &uvm_deviceops) {
210 for (/* nothing */; start < lim;
211 start += PAGE_SIZE, vec++)
212 subyte(vec, 1);
213 continue;
214 }
215 }
216
217 uobj = entry->object.uvm_obj; /* top layer */
218 amap = entry->aref.ar_amap; /* 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(&obj->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 * now let kernel internal function uvm_mmap do the work.
514 */
515
516 error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
517 flags, handle, pos);
518
519 if (error == 0)
520 /* remember to add offset */
521 *retval = (register_t)(addr + pageoff);
522
523 return (error);
524 }
525
526 /*
527 * sys___msync13: the msync system call (a front-end for flush)
528 */
529
530 int
531 sys___msync13(p, v, retval)
532 struct proc *p;
533 void *v;
534 register_t *retval;
535 {
536 struct sys___msync13_args /* {
537 syscallarg(caddr_t) addr;
538 syscallarg(size_t) len;
539 syscallarg(int) flags;
540 } */ *uap = v;
541 vaddr_t addr;
542 vsize_t size, pageoff;
543 vm_map_t map;
544 int rv, flags, uvmflags;
545
546 /*
547 * extract syscall args from the uap
548 */
549
550 addr = (vaddr_t)SCARG(uap, addr);
551 size = (vsize_t)SCARG(uap, len);
552 flags = SCARG(uap, flags);
553
554 /* sanity check flags */
555 if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
556 (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
557 (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
558 return (EINVAL);
559 if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
560 flags |= MS_SYNC;
561
562 /*
563 * align the address to a page boundary, and adjust the size accordingly
564 */
565
566 pageoff = (addr & PAGE_MASK);
567 addr -= pageoff;
568 size += pageoff;
569 size = (vsize_t) round_page(size);
570
571 /* disallow wrap-around. */
572 if (addr + size < addr)
573 return (EINVAL);
574
575 /*
576 * get map
577 */
578
579 map = &p->p_vmspace->vm_map;
580
581 /*
582 * XXXCDC: do we really need this semantic?
583 *
584 * XXX Gak! If size is zero we are supposed to sync "all modified
585 * pages with the region containing addr". Unfortunately, we
586 * don't really keep track of individual mmaps so we approximate
587 * by flushing the range of the map entry containing addr.
588 * This can be incorrect if the region splits or is coalesced
589 * with a neighbor.
590 */
591 if (size == 0) {
592 vm_map_entry_t entry;
593
594 vm_map_lock_read(map);
595 rv = uvm_map_lookup_entry(map, addr, &entry);
596 if (rv == TRUE) {
597 addr = entry->start;
598 size = entry->end - entry->start;
599 }
600 vm_map_unlock_read(map);
601 if (rv == FALSE)
602 return (EINVAL);
603 }
604
605 /*
606 * translate MS_ flags into PGO_ flags
607 */
608 uvmflags = (flags & MS_INVALIDATE) ? PGO_FREE : 0;
609 if (flags & MS_SYNC)
610 uvmflags |= PGO_SYNCIO;
611 else
612 uvmflags |= PGO_SYNCIO; /* XXXCDC: force sync for now! */
613
614 /*
615 * doit!
616 */
617 rv = uvm_map_clean(map, addr, addr+size, uvmflags);
618
619 /*
620 * and return...
621 */
622 switch (rv) {
623 case KERN_SUCCESS:
624 return(0);
625 case KERN_INVALID_ADDRESS:
626 return (ENOMEM);
627 case KERN_FAILURE:
628 return (EIO);
629 case KERN_PAGES_LOCKED: /* XXXCDC: uvm doesn't return this */
630 return (EBUSY);
631 default:
632 return (EINVAL);
633 }
634 /*NOTREACHED*/
635 }
636
637 /*
638 * sys_munmap: unmap a users memory
639 */
640
641 int
642 sys_munmap(p, v, retval)
643 register struct proc *p;
644 void *v;
645 register_t *retval;
646 {
647 register struct sys_munmap_args /* {
648 syscallarg(caddr_t) addr;
649 syscallarg(size_t) len;
650 } */ *uap = v;
651 vaddr_t addr;
652 vsize_t size, pageoff;
653 vm_map_t map;
654 vaddr_t vm_min_address = VM_MIN_ADDRESS;
655 struct vm_map_entry *dead_entries;
656
657 /*
658 * get syscall args...
659 */
660
661 addr = (vaddr_t) SCARG(uap, addr);
662 size = (vsize_t) SCARG(uap, len);
663
664 /*
665 * align the address to a page boundary, and adjust the size accordingly
666 */
667
668 pageoff = (addr & PAGE_MASK);
669 addr -= pageoff;
670 size += pageoff;
671 size = (vsize_t) round_page(size);
672
673 if ((int)size < 0)
674 return (EINVAL);
675 if (size == 0)
676 return (0);
677
678 /*
679 * Check for illegal addresses. Watch out for address wrap...
680 * Note that VM_*_ADDRESS are not constants due to casts (argh).
681 */
682 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
683 return (EINVAL);
684 if (vm_min_address > 0 && addr < vm_min_address)
685 return (EINVAL);
686 if (addr > addr + size)
687 return (EINVAL);
688 map = &p->p_vmspace->vm_map;
689
690
691 vm_map_lock(map); /* lock map so we can checkprot */
692
693 /*
694 * interesting system call semantic: make sure entire range is
695 * allocated before allowing an unmap.
696 */
697
698 if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
699 vm_map_unlock(map);
700 return (EINVAL);
701 }
702
703 /*
704 * doit!
705 */
706 (void) uvm_unmap_remove(map, addr, addr + size, &dead_entries);
707
708 vm_map_unlock(map); /* and unlock */
709
710 if (dead_entries != NULL)
711 uvm_unmap_detach(dead_entries, 0);
712
713 return (0);
714 }
715
716 /*
717 * sys_mprotect: the mprotect system call
718 */
719
720 int
721 sys_mprotect(p, v, retval)
722 struct proc *p;
723 void *v;
724 register_t *retval;
725 {
726 struct sys_mprotect_args /* {
727 syscallarg(caddr_t) addr;
728 syscallarg(int) len;
729 syscallarg(int) prot;
730 } */ *uap = v;
731 vaddr_t addr;
732 vsize_t size, pageoff;
733 vm_prot_t prot;
734 int rv;
735
736 /*
737 * extract syscall args from uap
738 */
739
740 addr = (vaddr_t)SCARG(uap, addr);
741 size = (vsize_t)SCARG(uap, len);
742 prot = SCARG(uap, prot) & VM_PROT_ALL;
743
744 /*
745 * align the address to a page boundary, and adjust the size accordingly
746 */
747 pageoff = (addr & PAGE_MASK);
748 addr -= pageoff;
749 size += pageoff;
750 size = (vsize_t) round_page(size);
751 if ((int)size < 0)
752 return (EINVAL);
753
754 /*
755 * doit
756 */
757
758 rv = uvm_map_protect(&p->p_vmspace->vm_map,
759 addr, addr+size, prot, FALSE);
760
761 if (rv == KERN_SUCCESS)
762 return (0);
763 if (rv == KERN_PROTECTION_FAILURE)
764 return (EACCES);
765 return (EINVAL);
766 }
767
768 /*
769 * sys_minherit: the minherit system call
770 */
771
772 int
773 sys_minherit(p, v, retval)
774 struct proc *p;
775 void *v;
776 register_t *retval;
777 {
778 struct sys_minherit_args /* {
779 syscallarg(caddr_t) addr;
780 syscallarg(int) len;
781 syscallarg(int) inherit;
782 } */ *uap = v;
783 vaddr_t addr;
784 vsize_t size, pageoff;
785 register vm_inherit_t inherit;
786
787 addr = (vaddr_t)SCARG(uap, addr);
788 size = (vsize_t)SCARG(uap, len);
789 inherit = SCARG(uap, inherit);
790 /*
791 * align the address to a page boundary, and adjust the size accordingly
792 */
793
794 pageoff = (addr & PAGE_MASK);
795 addr -= pageoff;
796 size += pageoff;
797 size = (vsize_t) round_page(size);
798
799 if ((int)size < 0)
800 return (EINVAL);
801
802 switch (uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size,
803 inherit)) {
804 case KERN_SUCCESS:
805 return (0);
806 case KERN_PROTECTION_FAILURE:
807 return (EACCES);
808 }
809 return (EINVAL);
810 }
811
812 /*
813 * sys_madvise: give advice about memory usage.
814 */
815
816 /* ARGSUSED */
817 int
818 sys_madvise(p, v, retval)
819 struct proc *p;
820 void *v;
821 register_t *retval;
822 {
823 struct sys_madvise_args /* {
824 syscallarg(caddr_t) addr;
825 syscallarg(size_t) len;
826 syscallarg(int) behav;
827 } */ *uap = v;
828 vaddr_t addr;
829 vsize_t size, pageoff;
830 int advice;
831
832 addr = (vaddr_t)SCARG(uap, addr);
833 size = (vsize_t)SCARG(uap, len);
834 advice = SCARG(uap, behav);
835
836 /*
837 * align the address to a page boundary, and adjust the size accordingly
838 */
839 pageoff = (addr & PAGE_MASK);
840 addr -= pageoff;
841 size += pageoff;
842 size = (vsize_t) round_page(size);
843
844 if ((int)size < 0)
845 return (EINVAL);
846
847 switch (uvm_map_advice(&p->p_vmspace->vm_map, addr, addr+size,
848 advice)) {
849 case KERN_SUCCESS:
850 return (0);
851 case KERN_PROTECTION_FAILURE:
852 return (EACCES);
853 }
854 return (EINVAL);
855 }
856
857 /*
858 * sys_mlock: memory lock
859 */
860
861 int
862 sys_mlock(p, v, retval)
863 struct proc *p;
864 void *v;
865 register_t *retval;
866 {
867 struct sys_mlock_args /* {
868 syscallarg(const void *) addr;
869 syscallarg(size_t) len;
870 } */ *uap = v;
871 vaddr_t addr;
872 vsize_t size, pageoff;
873 int error;
874
875 /*
876 * extract syscall args from uap
877 */
878 addr = (vaddr_t)SCARG(uap, addr);
879 size = (vsize_t)SCARG(uap, len);
880
881 /*
882 * align the address to a page boundary and adjust the size accordingly
883 */
884 pageoff = (addr & PAGE_MASK);
885 addr -= pageoff;
886 size += pageoff;
887 size = (vsize_t) round_page(size);
888
889 /* disallow wrap-around. */
890 if (addr + (int)size < addr)
891 return (EINVAL);
892
893 if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
894 return (EAGAIN);
895
896 #ifdef pmap_wired_count
897 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
898 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
899 return (EAGAIN);
900 #else
901 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
902 return (error);
903 #endif
904
905 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE);
906 return (error == KERN_SUCCESS ? 0 : ENOMEM);
907 }
908
909 /*
910 * sys_munlock: unlock wired pages
911 */
912
913 int
914 sys_munlock(p, v, retval)
915 struct proc *p;
916 void *v;
917 register_t *retval;
918 {
919 struct sys_munlock_args /* {
920 syscallarg(const void *) addr;
921 syscallarg(size_t) len;
922 } */ *uap = v;
923 vaddr_t addr;
924 vsize_t size, pageoff;
925 int error;
926
927 /*
928 * extract syscall args from uap
929 */
930
931 addr = (vaddr_t)SCARG(uap, addr);
932 size = (vsize_t)SCARG(uap, len);
933
934 /*
935 * align the address to a page boundary, and adjust the size accordingly
936 */
937 pageoff = (addr & PAGE_MASK);
938 addr -= pageoff;
939 size += pageoff;
940 size = (vsize_t) round_page(size);
941
942 /* disallow wrap-around. */
943 if (addr + (int)size < addr)
944 return (EINVAL);
945
946 #ifndef pmap_wired_count
947 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
948 return (error);
949 #endif
950
951 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE);
952 return (error == KERN_SUCCESS ? 0 : ENOMEM);
953 }
954
955 /*
956 * sys_mlockall: lock all pages mapped into an address space.
957 */
958
959 int
960 sys_mlockall(p, v, retval)
961 struct proc *p;
962 void *v;
963 register_t *retval;
964 {
965 struct sys_mlockall_args /* {
966 syscallarg(int) flags;
967 } */ *uap = v;
968 vsize_t limit;
969 int error, flags;
970
971 flags = SCARG(uap, flags);
972
973 if (flags == 0 ||
974 (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
975 return (EINVAL);
976
977 #ifdef pmap_wired_count
978 /* Actually checked in uvm_map_pageable_all() */
979 limit = p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur;
980 #else
981 limit = 0;
982 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
983 return (error);
984 #endif
985
986 error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags, limit);
987 switch (error) {
988 case KERN_SUCCESS:
989 error = 0;
990 break;
991
992 case KERN_NO_SPACE: /* XXX overloaded */
993 error = ENOMEM;
994 break;
995
996 default:
997 /*
998 * "Some or all of the memory could not be locked when
999 * the call was made."
1000 */
1001 error = EAGAIN;
1002 }
1003
1004 return (error);
1005 }
1006
1007 /*
1008 * sys_munlockall: unlock all pages mapped into an address space.
1009 */
1010
1011 int
1012 sys_munlockall(p, v, retval)
1013 struct proc *p;
1014 void *v;
1015 register_t *retval;
1016 {
1017
1018 (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
1019 return (0);
1020 }
1021
1022 /*
1023 * uvm_mmap: internal version of mmap
1024 *
1025 * - used by sys_mmap, exec, and sysv shm
1026 * - handle is a vnode pointer or NULL for MAP_ANON (XXX: not true,
1027 * sysv shm uses "named anonymous memory")
1028 * - caller must page-align the file offset
1029 */
1030
1031 int
1032 uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff)
1033 vm_map_t map;
1034 vaddr_t *addr;
1035 vsize_t size;
1036 vm_prot_t prot, maxprot;
1037 int flags;
1038 caddr_t handle; /* XXX: VNODE? */
1039 vaddr_t foff;
1040 {
1041 struct uvm_object *uobj;
1042 struct vnode *vp;
1043 int retval;
1044 int advice = UVM_ADV_NORMAL;
1045 uvm_flag_t uvmflag = 0;
1046
1047 /*
1048 * check params
1049 */
1050
1051 if (size == 0)
1052 return(0);
1053 if (foff & PAGE_MASK)
1054 return(EINVAL);
1055 if ((prot & maxprot) != prot)
1056 return(EINVAL);
1057
1058 /*
1059 * for non-fixed mappings, round off the suggested address.
1060 * for fixed mappings, check alignment and zap old mappings.
1061 */
1062
1063 if ((flags & MAP_FIXED) == 0) {
1064 *addr = round_page(*addr); /* round */
1065 } else {
1066
1067 if (*addr & PAGE_MASK)
1068 return(EINVAL);
1069 uvmflag |= UVM_FLAG_FIXED;
1070 (void) uvm_unmap(map, *addr, *addr + size); /* zap! */
1071 }
1072
1073 /*
1074 * handle anon vs. non-anon mappings. for non-anon mappings attach
1075 * to underlying vm object.
1076 */
1077
1078 if (flags & MAP_ANON) {
1079
1080 foff = UVM_UNKNOWN_OFFSET;
1081 uobj = NULL;
1082 if ((flags & MAP_SHARED) == 0)
1083 /* XXX: defer amap create */
1084 uvmflag |= UVM_FLAG_COPYONW;
1085 else
1086 /* shared: create amap now */
1087 uvmflag |= UVM_FLAG_OVERLAY;
1088
1089 } else {
1090
1091 vp = (struct vnode *) handle; /* get vnode */
1092 if (vp->v_type != VCHR) {
1093 uobj = uvn_attach((void *) vp, (flags & MAP_SHARED) ?
1094 maxprot : (maxprot & ~VM_PROT_WRITE));
1095
1096 /*
1097 * XXXCDC: hack from old code
1098 * don't allow vnodes which have been mapped
1099 * shared-writeable to persist [forces them to be
1100 * flushed out when last reference goes].
1101 * XXXCDC: interesting side effect: avoids a bug.
1102 * note that in WRITE [ufs_readwrite.c] that we
1103 * allocate buffer, uncache, and then do the write.
1104 * the problem with this is that if the uncache causes
1105 * VM data to be flushed to the same area of the file
1106 * we are writing to... in that case we've got the
1107 * buffer locked and our process goes to sleep forever.
1108 *
1109 * XXXCDC: checking maxprot protects us from the
1110 * "persistbug" program but this is not a long term
1111 * solution.
1112 *
1113 * XXXCDC: we don't bother calling uncache with the vp
1114 * VOP_LOCKed since we know that we are already
1115 * holding a valid reference to the uvn (from the
1116 * uvn_attach above), and thus it is impossible for
1117 * the uncache to kill the uvn and trigger I/O.
1118 */
1119 if (flags & MAP_SHARED) {
1120 if ((prot & VM_PROT_WRITE) ||
1121 (maxprot & VM_PROT_WRITE)) {
1122 uvm_vnp_uncache(vp);
1123 }
1124 }
1125
1126 } else {
1127 uobj = udv_attach((void *) &vp->v_rdev,
1128 (flags & MAP_SHARED) ?
1129 maxprot : (maxprot & ~VM_PROT_WRITE), foff, size);
1130 advice = UVM_ADV_RANDOM;
1131 }
1132
1133 if (uobj == NULL)
1134 return((vp->v_type == VREG) ? ENOMEM : EINVAL);
1135
1136 if ((flags & MAP_SHARED) == 0)
1137 uvmflag |= UVM_FLAG_COPYONW;
1138 }
1139
1140 /*
1141 * set up mapping flags
1142 */
1143
1144 uvmflag = UVM_MAPFLAG(prot, maxprot,
1145 (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
1146 advice, uvmflag);
1147
1148 /*
1149 * do it!
1150 */
1151
1152 retval = uvm_map(map, addr, size, uobj, foff, uvmflag);
1153
1154 if (retval == KERN_SUCCESS)
1155 return(0);
1156
1157 /*
1158 * errors: first detach from the uobj, if any.
1159 */
1160
1161 if (uobj)
1162 uobj->pgops->pgo_detach(uobj);
1163
1164 switch (retval) {
1165 case KERN_INVALID_ADDRESS:
1166 case KERN_NO_SPACE:
1167 return(ENOMEM);
1168 case KERN_PROTECTION_FAILURE:
1169 return(EACCES);
1170 }
1171 return(EINVAL);
1172 }
1173