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