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