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