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