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