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