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