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