FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_mmap.c
1 /*-
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
35 *
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
37 */
38
39 /*
40 * Mapped file (mmap) interface to VM
41 */
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: releng/6.3/sys/vm/vm_mmap.c 172102 2007-09-09 04:41:24Z kib $");
45
46 #include "opt_compat.h"
47 #include "opt_mac.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/filedesc.h>
56 #include <sys/proc.h>
57 #include <sys/resource.h>
58 #include <sys/resourcevar.h>
59 #include <sys/vnode.h>
60 #include <sys/fcntl.h>
61 #include <sys/file.h>
62 #include <sys/mac.h>
63 #include <sys/mman.h>
64 #include <sys/mount.h>
65 #include <sys/conf.h>
66 #include <sys/stat.h>
67 #include <sys/vmmeter.h>
68 #include <sys/sysctl.h>
69
70 #include <vm/vm.h>
71 #include <vm/vm_param.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_pager.h>
77 #include <vm/vm_pageout.h>
78 #include <vm/vm_extern.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_kern.h>
81
82 #ifndef _SYS_SYSPROTO_H_
83 struct sbrk_args {
84 int incr;
85 };
86 #endif
87
88 static int max_proc_mmap;
89 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, "");
90
91 /*
92 * Set the maximum number of vm_map_entry structures per process. Roughly
93 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
94 * of our KVM malloc space still results in generous limits. We want a
95 * default that is good enough to prevent the kernel running out of resources
96 * if attacked from compromised user account but generous enough such that
97 * multi-threaded processes are not unduly inconvenienced.
98 */
99 static void vmmapentry_rsrc_init(void *);
100 SYSINIT(vmmersrc, SI_SUB_KVM_RSRC, SI_ORDER_FIRST, vmmapentry_rsrc_init, NULL)
101
102 static void
103 vmmapentry_rsrc_init(dummy)
104 void *dummy;
105 {
106 max_proc_mmap = vm_kmem_size / sizeof(struct vm_map_entry);
107 max_proc_mmap /= 100;
108 }
109
110 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
111 int *, struct vnode *, vm_ooffset_t, vm_object_t *);
112 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
113 int *, struct cdev *, vm_ooffset_t, vm_object_t *);
114
115 /*
116 * MPSAFE
117 */
118 /* ARGSUSED */
119 int
120 sbrk(td, uap)
121 struct thread *td;
122 struct sbrk_args *uap;
123 {
124 /* Not yet implemented */
125 return (EOPNOTSUPP);
126 }
127
128 #ifndef _SYS_SYSPROTO_H_
129 struct sstk_args {
130 int incr;
131 };
132 #endif
133
134 /*
135 * MPSAFE
136 */
137 /* ARGSUSED */
138 int
139 sstk(td, uap)
140 struct thread *td;
141 struct sstk_args *uap;
142 {
143 /* Not yet implemented */
144 return (EOPNOTSUPP);
145 }
146
147 #if defined(COMPAT_43)
148 #ifndef _SYS_SYSPROTO_H_
149 struct getpagesize_args {
150 int dummy;
151 };
152 #endif
153
154 /* ARGSUSED */
155 int
156 ogetpagesize(td, uap)
157 struct thread *td;
158 struct getpagesize_args *uap;
159 {
160 /* MP SAFE */
161 td->td_retval[0] = PAGE_SIZE;
162 return (0);
163 }
164 #endif /* COMPAT_43 */
165
166
167 /*
168 * Memory Map (mmap) system call. Note that the file offset
169 * and address are allowed to be NOT page aligned, though if
170 * the MAP_FIXED flag it set, both must have the same remainder
171 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
172 * page-aligned, the actual mapping starts at trunc_page(addr)
173 * and the return value is adjusted up by the page offset.
174 *
175 * Generally speaking, only character devices which are themselves
176 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
177 * there would be no cache coherency between a descriptor and a VM mapping
178 * both to the same character device.
179 *
180 * Block devices can be mmap'd no matter what they represent. Cache coherency
181 * is maintained as long as you do not write directly to the underlying
182 * character device.
183 */
184 #ifndef _SYS_SYSPROTO_H_
185 struct mmap_args {
186 void *addr;
187 size_t len;
188 int prot;
189 int flags;
190 int fd;
191 long pad;
192 off_t pos;
193 };
194 #endif
195
196 /*
197 * MPSAFE
198 */
199 int
200 mmap(td, uap)
201 struct thread *td;
202 struct mmap_args *uap;
203 {
204 struct file *fp;
205 struct vnode *vp;
206 vm_offset_t addr;
207 vm_size_t size, pageoff;
208 vm_prot_t prot, maxprot;
209 void *handle;
210 objtype_t handle_type;
211 int flags, error;
212 off_t pos;
213 struct vmspace *vms = td->td_proc->p_vmspace;
214
215 addr = (vm_offset_t) uap->addr;
216 size = uap->len;
217 prot = uap->prot & VM_PROT_ALL;
218 flags = uap->flags;
219 pos = uap->pos;
220
221 fp = NULL;
222 /* make sure mapping fits into numeric range etc */
223 if ((ssize_t) uap->len < 0 ||
224 ((flags & MAP_ANON) && uap->fd != -1))
225 return (EINVAL);
226
227 if (flags & MAP_STACK) {
228 if ((uap->fd != -1) ||
229 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
230 return (EINVAL);
231 flags |= MAP_ANON;
232 pos = 0;
233 }
234
235 /*
236 * Align the file position to a page boundary,
237 * and save its page offset component.
238 */
239 pageoff = (pos & PAGE_MASK);
240 pos -= pageoff;
241
242 /* Adjust size for rounding (on both ends). */
243 size += pageoff; /* low end... */
244 size = (vm_size_t) round_page(size); /* hi end */
245
246 /*
247 * Check for illegal addresses. Watch out for address wrap... Note
248 * that VM_*_ADDRESS are not constants due to casts (argh).
249 */
250 if (flags & MAP_FIXED) {
251 /*
252 * The specified address must have the same remainder
253 * as the file offset taken modulo PAGE_SIZE, so it
254 * should be aligned after adjustment by pageoff.
255 */
256 addr -= pageoff;
257 if (addr & PAGE_MASK)
258 return (EINVAL);
259 /* Address range must be all in user VM space. */
260 if (addr < vm_map_min(&vms->vm_map) ||
261 addr + size > vm_map_max(&vms->vm_map))
262 return (EINVAL);
263 if (addr + size < addr)
264 return (EINVAL);
265 } else {
266 /*
267 * XXX for non-fixed mappings where no hint is provided or
268 * the hint would fall in the potential heap space,
269 * place it after the end of the largest possible heap.
270 *
271 * There should really be a pmap call to determine a reasonable
272 * location.
273 */
274 PROC_LOCK(td->td_proc);
275 if (addr == 0 ||
276 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
277 addr < round_page((vm_offset_t)vms->vm_daddr +
278 lim_max(td->td_proc, RLIMIT_DATA))))
279 addr = round_page((vm_offset_t)vms->vm_daddr +
280 lim_max(td->td_proc, RLIMIT_DATA));
281 PROC_UNLOCK(td->td_proc);
282 }
283 if (flags & MAP_ANON) {
284 /*
285 * Mapping blank space is trivial.
286 */
287 handle = NULL;
288 handle_type = OBJT_DEFAULT;
289 maxprot = VM_PROT_ALL;
290 pos = 0;
291 } else {
292 /*
293 * Mapping file, get fp for validation. Obtain vnode and make
294 * sure it is of appropriate type.
295 * don't let the descriptor disappear on us if we block
296 */
297 if ((error = fget(td, uap->fd, &fp)) != 0)
298 goto done;
299 if (fp->f_type != DTYPE_VNODE) {
300 error = EINVAL;
301 goto done;
302 }
303 /*
304 * POSIX shared-memory objects are defined to have
305 * kernel persistence, and are not defined to support
306 * read(2)/write(2) -- or even open(2). Thus, we can
307 * use MAP_ASYNC to trade on-disk coherence for speed.
308 * The shm_open(3) library routine turns on the FPOSIXSHM
309 * flag to request this behavior.
310 */
311 if (fp->f_flag & FPOSIXSHM)
312 flags |= MAP_NOSYNC;
313 vp = fp->f_vnode;
314 /*
315 * Ensure that file and memory protections are
316 * compatible. Note that we only worry about
317 * writability if mapping is shared; in this case,
318 * current and max prot are dictated by the open file.
319 * XXX use the vnode instead? Problem is: what
320 * credentials do we use for determination? What if
321 * proc does a setuid?
322 */
323 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
324 maxprot = VM_PROT_NONE;
325 else
326 maxprot = VM_PROT_EXECUTE;
327 if (fp->f_flag & FREAD) {
328 maxprot |= VM_PROT_READ;
329 } else if (prot & PROT_READ) {
330 error = EACCES;
331 goto done;
332 }
333 /*
334 * If we are sharing potential changes (either via
335 * MAP_SHARED or via the implicit sharing of character
336 * device mappings), and we are trying to get write
337 * permission although we opened it without asking
338 * for it, bail out.
339 */
340 if ((flags & MAP_SHARED) != 0) {
341 if ((fp->f_flag & FWRITE) != 0) {
342 maxprot |= VM_PROT_WRITE;
343 } else if ((prot & PROT_WRITE) != 0) {
344 error = EACCES;
345 goto done;
346 }
347 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
348 maxprot |= VM_PROT_WRITE;
349 }
350 handle = (void *)vp;
351 handle_type = OBJT_VNODE;
352 }
353
354 /*
355 * Do not allow more then a certain number of vm_map_entry structures
356 * per process. Scale with the number of rforks sharing the map
357 * to make the limit reasonable for threads.
358 */
359 if (max_proc_mmap &&
360 vms->vm_map.nentries >= max_proc_mmap * vms->vm_refcnt) {
361 error = ENOMEM;
362 goto done;
363 }
364
365 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
366 flags, handle_type, handle, pos);
367 if (error == 0)
368 td->td_retval[0] = (register_t) (addr + pageoff);
369 done:
370 if (fp)
371 fdrop(fp, td);
372
373 return (error);
374 }
375
376 #ifdef COMPAT_43
377 #ifndef _SYS_SYSPROTO_H_
378 struct ommap_args {
379 caddr_t addr;
380 int len;
381 int prot;
382 int flags;
383 int fd;
384 long pos;
385 };
386 #endif
387 int
388 ommap(td, uap)
389 struct thread *td;
390 struct ommap_args *uap;
391 {
392 struct mmap_args nargs;
393 static const char cvtbsdprot[8] = {
394 0,
395 PROT_EXEC,
396 PROT_WRITE,
397 PROT_EXEC | PROT_WRITE,
398 PROT_READ,
399 PROT_EXEC | PROT_READ,
400 PROT_WRITE | PROT_READ,
401 PROT_EXEC | PROT_WRITE | PROT_READ,
402 };
403
404 #define OMAP_ANON 0x0002
405 #define OMAP_COPY 0x0020
406 #define OMAP_SHARED 0x0010
407 #define OMAP_FIXED 0x0100
408
409 nargs.addr = uap->addr;
410 nargs.len = uap->len;
411 nargs.prot = cvtbsdprot[uap->prot & 0x7];
412 nargs.flags = 0;
413 if (uap->flags & OMAP_ANON)
414 nargs.flags |= MAP_ANON;
415 if (uap->flags & OMAP_COPY)
416 nargs.flags |= MAP_COPY;
417 if (uap->flags & OMAP_SHARED)
418 nargs.flags |= MAP_SHARED;
419 else
420 nargs.flags |= MAP_PRIVATE;
421 if (uap->flags & OMAP_FIXED)
422 nargs.flags |= MAP_FIXED;
423 nargs.fd = uap->fd;
424 nargs.pos = uap->pos;
425 return (mmap(td, &nargs));
426 }
427 #endif /* COMPAT_43 */
428
429
430 #ifndef _SYS_SYSPROTO_H_
431 struct msync_args {
432 void *addr;
433 int len;
434 int flags;
435 };
436 #endif
437 /*
438 * MPSAFE
439 */
440 int
441 msync(td, uap)
442 struct thread *td;
443 struct msync_args *uap;
444 {
445 vm_offset_t addr;
446 vm_size_t size, pageoff;
447 int flags;
448 vm_map_t map;
449 int rv;
450
451 addr = (vm_offset_t) uap->addr;
452 size = uap->len;
453 flags = uap->flags;
454
455 pageoff = (addr & PAGE_MASK);
456 addr -= pageoff;
457 size += pageoff;
458 size = (vm_size_t) round_page(size);
459 if (addr + size < addr)
460 return (EINVAL);
461
462 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
463 return (EINVAL);
464
465 map = &td->td_proc->p_vmspace->vm_map;
466
467 /*
468 * Clean the pages and interpret the return value.
469 */
470 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
471 (flags & MS_INVALIDATE) != 0);
472 switch (rv) {
473 case KERN_SUCCESS:
474 return (0);
475 case KERN_INVALID_ADDRESS:
476 return (EINVAL); /* Sun returns ENOMEM? */
477 case KERN_INVALID_ARGUMENT:
478 return (EBUSY);
479 default:
480 return (EINVAL);
481 }
482 }
483
484 #ifndef _SYS_SYSPROTO_H_
485 struct munmap_args {
486 void *addr;
487 size_t len;
488 };
489 #endif
490 /*
491 * MPSAFE
492 */
493 int
494 munmap(td, uap)
495 struct thread *td;
496 struct munmap_args *uap;
497 {
498 vm_offset_t addr;
499 vm_size_t size, pageoff;
500 vm_map_t map;
501
502 addr = (vm_offset_t) uap->addr;
503 size = uap->len;
504 if (size == 0)
505 return (EINVAL);
506
507 pageoff = (addr & PAGE_MASK);
508 addr -= pageoff;
509 size += pageoff;
510 size = (vm_size_t) round_page(size);
511 if (addr + size < addr)
512 return (EINVAL);
513
514 /*
515 * Check for illegal addresses. Watch out for address wrap...
516 */
517 map = &td->td_proc->p_vmspace->vm_map;
518 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
519 return (EINVAL);
520 vm_map_lock(map);
521 /*
522 * Make sure entire range is allocated.
523 */
524 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) {
525 vm_map_unlock(map);
526 return (EINVAL);
527 }
528 /* returns nothing but KERN_SUCCESS anyway */
529 vm_map_delete(map, addr, addr + size);
530 vm_map_unlock(map);
531 return (0);
532 }
533
534 #ifndef _SYS_SYSPROTO_H_
535 struct mprotect_args {
536 const void *addr;
537 size_t len;
538 int prot;
539 };
540 #endif
541 /*
542 * MPSAFE
543 */
544 int
545 mprotect(td, uap)
546 struct thread *td;
547 struct mprotect_args *uap;
548 {
549 vm_offset_t addr;
550 vm_size_t size, pageoff;
551 vm_prot_t prot;
552
553 addr = (vm_offset_t) uap->addr;
554 size = uap->len;
555 prot = uap->prot & VM_PROT_ALL;
556 #if defined(VM_PROT_READ_IS_EXEC)
557 if (prot & VM_PROT_READ)
558 prot |= VM_PROT_EXECUTE;
559 #endif
560
561 pageoff = (addr & PAGE_MASK);
562 addr -= pageoff;
563 size += pageoff;
564 size = (vm_size_t) round_page(size);
565 if (addr + size < addr)
566 return (EINVAL);
567
568 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
569 addr + size, prot, FALSE)) {
570 case KERN_SUCCESS:
571 return (0);
572 case KERN_PROTECTION_FAILURE:
573 return (EACCES);
574 }
575 return (EINVAL);
576 }
577
578 #ifndef _SYS_SYSPROTO_H_
579 struct minherit_args {
580 void *addr;
581 size_t len;
582 int inherit;
583 };
584 #endif
585 /*
586 * MPSAFE
587 */
588 int
589 minherit(td, uap)
590 struct thread *td;
591 struct minherit_args *uap;
592 {
593 vm_offset_t addr;
594 vm_size_t size, pageoff;
595 vm_inherit_t inherit;
596
597 addr = (vm_offset_t)uap->addr;
598 size = uap->len;
599 inherit = uap->inherit;
600
601 pageoff = (addr & PAGE_MASK);
602 addr -= pageoff;
603 size += pageoff;
604 size = (vm_size_t) round_page(size);
605 if (addr + size < addr)
606 return (EINVAL);
607
608 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
609 addr + size, inherit)) {
610 case KERN_SUCCESS:
611 return (0);
612 case KERN_PROTECTION_FAILURE:
613 return (EACCES);
614 }
615 return (EINVAL);
616 }
617
618 #ifndef _SYS_SYSPROTO_H_
619 struct madvise_args {
620 void *addr;
621 size_t len;
622 int behav;
623 };
624 #endif
625
626 /*
627 * MPSAFE
628 */
629 /* ARGSUSED */
630 int
631 madvise(td, uap)
632 struct thread *td;
633 struct madvise_args *uap;
634 {
635 vm_offset_t start, end;
636 vm_map_t map;
637 struct proc *p;
638 int error;
639
640 /*
641 * Check for our special case, advising the swap pager we are
642 * "immortal."
643 */
644 if (uap->behav == MADV_PROTECT) {
645 error = suser(td);
646 if (error == 0) {
647 p = td->td_proc;
648 PROC_LOCK(p);
649 p->p_flag |= P_PROTECTED;
650 PROC_UNLOCK(p);
651 }
652 return (error);
653 }
654 /*
655 * Check for illegal behavior
656 */
657 if (uap->behav < 0 || uap->behav > MADV_CORE)
658 return (EINVAL);
659 /*
660 * Check for illegal addresses. Watch out for address wrap... Note
661 * that VM_*_ADDRESS are not constants due to casts (argh).
662 */
663 map = &td->td_proc->p_vmspace->vm_map;
664 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
665 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
666 return (EINVAL);
667 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
668 return (EINVAL);
669
670 /*
671 * Since this routine is only advisory, we default to conservative
672 * behavior.
673 */
674 start = trunc_page((vm_offset_t) uap->addr);
675 end = round_page((vm_offset_t) uap->addr + uap->len);
676
677 if (vm_map_madvise(map, start, end, uap->behav))
678 return (EINVAL);
679 return (0);
680 }
681
682 #ifndef _SYS_SYSPROTO_H_
683 struct mincore_args {
684 const void *addr;
685 size_t len;
686 char *vec;
687 };
688 #endif
689
690 /*
691 * MPSAFE
692 */
693 /* ARGSUSED */
694 int
695 mincore(td, uap)
696 struct thread *td;
697 struct mincore_args *uap;
698 {
699 vm_offset_t addr, first_addr;
700 vm_offset_t end, cend;
701 pmap_t pmap;
702 vm_map_t map;
703 char *vec;
704 int error = 0;
705 int vecindex, lastvecindex;
706 vm_map_entry_t current;
707 vm_map_entry_t entry;
708 int mincoreinfo;
709 unsigned int timestamp;
710
711 /*
712 * Make sure that the addresses presented are valid for user
713 * mode.
714 */
715 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
716 end = addr + (vm_size_t)round_page(uap->len);
717 map = &td->td_proc->p_vmspace->vm_map;
718 if (end > vm_map_max(map) || end < addr)
719 return (ENOMEM);
720
721 /*
722 * Address of byte vector
723 */
724 vec = uap->vec;
725
726 pmap = vmspace_pmap(td->td_proc->p_vmspace);
727
728 vm_map_lock_read(map);
729 RestartScan:
730 timestamp = map->timestamp;
731
732 if (!vm_map_lookup_entry(map, addr, &entry)) {
733 vm_map_unlock_read(map);
734 return (ENOMEM);
735 }
736
737 /*
738 * Do this on a map entry basis so that if the pages are not
739 * in the current processes address space, we can easily look
740 * up the pages elsewhere.
741 */
742 lastvecindex = -1;
743 for (current = entry;
744 (current != &map->header) && (current->start < end);
745 current = current->next) {
746
747 /*
748 * check for contiguity
749 */
750 if (current->end < end &&
751 (entry->next == &map->header ||
752 current->next->start > current->end)) {
753 vm_map_unlock_read(map);
754 return (ENOMEM);
755 }
756
757 /*
758 * ignore submaps (for now) or null objects
759 */
760 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
761 current->object.vm_object == NULL)
762 continue;
763
764 /*
765 * limit this scan to the current map entry and the
766 * limits for the mincore call
767 */
768 if (addr < current->start)
769 addr = current->start;
770 cend = current->end;
771 if (cend > end)
772 cend = end;
773
774 /*
775 * scan this entry one page at a time
776 */
777 while (addr < cend) {
778 /*
779 * Check pmap first, it is likely faster, also
780 * it can provide info as to whether we are the
781 * one referencing or modifying the page.
782 */
783 mincoreinfo = pmap_mincore(pmap, addr);
784 if (!mincoreinfo) {
785 vm_pindex_t pindex;
786 vm_ooffset_t offset;
787 vm_page_t m;
788 /*
789 * calculate the page index into the object
790 */
791 offset = current->offset + (addr - current->start);
792 pindex = OFF_TO_IDX(offset);
793 VM_OBJECT_LOCK(current->object.vm_object);
794 m = vm_page_lookup(current->object.vm_object,
795 pindex);
796 /*
797 * if the page is resident, then gather information about
798 * it.
799 */
800 if (m != NULL && m->valid != 0) {
801 mincoreinfo = MINCORE_INCORE;
802 vm_page_lock_queues();
803 if (m->dirty ||
804 pmap_is_modified(m))
805 mincoreinfo |= MINCORE_MODIFIED_OTHER;
806 if ((m->flags & PG_REFERENCED) ||
807 pmap_ts_referenced(m)) {
808 vm_page_flag_set(m, PG_REFERENCED);
809 mincoreinfo |= MINCORE_REFERENCED_OTHER;
810 }
811 vm_page_unlock_queues();
812 }
813 VM_OBJECT_UNLOCK(current->object.vm_object);
814 }
815
816 /*
817 * subyte may page fault. In case it needs to modify
818 * the map, we release the lock.
819 */
820 vm_map_unlock_read(map);
821
822 /*
823 * calculate index into user supplied byte vector
824 */
825 vecindex = OFF_TO_IDX(addr - first_addr);
826
827 /*
828 * If we have skipped map entries, we need to make sure that
829 * the byte vector is zeroed for those skipped entries.
830 */
831 while ((lastvecindex + 1) < vecindex) {
832 error = subyte(vec + lastvecindex, 0);
833 if (error) {
834 error = EFAULT;
835 goto done2;
836 }
837 ++lastvecindex;
838 }
839
840 /*
841 * Pass the page information to the user
842 */
843 error = subyte(vec + vecindex, mincoreinfo);
844 if (error) {
845 error = EFAULT;
846 goto done2;
847 }
848
849 /*
850 * If the map has changed, due to the subyte, the previous
851 * output may be invalid.
852 */
853 vm_map_lock_read(map);
854 if (timestamp != map->timestamp)
855 goto RestartScan;
856
857 lastvecindex = vecindex;
858 addr += PAGE_SIZE;
859 }
860 }
861
862 /*
863 * subyte may page fault. In case it needs to modify
864 * the map, we release the lock.
865 */
866 vm_map_unlock_read(map);
867
868 /*
869 * Zero the last entries in the byte vector.
870 */
871 vecindex = OFF_TO_IDX(end - first_addr);
872 while ((lastvecindex + 1) < vecindex) {
873 error = subyte(vec + lastvecindex, 0);
874 if (error) {
875 error = EFAULT;
876 goto done2;
877 }
878 ++lastvecindex;
879 }
880
881 /*
882 * If the map has changed, due to the subyte, the previous
883 * output may be invalid.
884 */
885 vm_map_lock_read(map);
886 if (timestamp != map->timestamp)
887 goto RestartScan;
888 vm_map_unlock_read(map);
889 done2:
890 return (error);
891 }
892
893 #ifndef _SYS_SYSPROTO_H_
894 struct mlock_args {
895 const void *addr;
896 size_t len;
897 };
898 #endif
899 /*
900 * MPSAFE
901 */
902 int
903 mlock(td, uap)
904 struct thread *td;
905 struct mlock_args *uap;
906 {
907 struct proc *proc;
908 vm_offset_t addr, end, last, start;
909 vm_size_t npages, size;
910 int error;
911
912 error = suser(td);
913 if (error)
914 return (error);
915 addr = (vm_offset_t)uap->addr;
916 size = uap->len;
917 last = addr + size;
918 start = trunc_page(addr);
919 end = round_page(last);
920 if (last < addr || end < addr)
921 return (EINVAL);
922 npages = atop(end - start);
923 if (npages > vm_page_max_wired)
924 return (ENOMEM);
925 proc = td->td_proc;
926 PROC_LOCK(proc);
927 if (ptoa(npages +
928 pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
929 lim_cur(proc, RLIMIT_MEMLOCK)) {
930 PROC_UNLOCK(proc);
931 return (ENOMEM);
932 }
933 PROC_UNLOCK(proc);
934 if (npages + cnt.v_wire_count > vm_page_max_wired)
935 return (EAGAIN);
936 error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
937 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
938 return (error == KERN_SUCCESS ? 0 : ENOMEM);
939 }
940
941 #ifndef _SYS_SYSPROTO_H_
942 struct mlockall_args {
943 int how;
944 };
945 #endif
946
947 /*
948 * MPSAFE
949 */
950 int
951 mlockall(td, uap)
952 struct thread *td;
953 struct mlockall_args *uap;
954 {
955 vm_map_t map;
956 int error;
957
958 map = &td->td_proc->p_vmspace->vm_map;
959 error = 0;
960
961 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
962 return (EINVAL);
963
964 #if 0
965 /*
966 * If wiring all pages in the process would cause it to exceed
967 * a hard resource limit, return ENOMEM.
968 */
969 PROC_LOCK(td->td_proc);
970 if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
971 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
972 PROC_UNLOCK(td->td_proc);
973 return (ENOMEM);
974 }
975 PROC_UNLOCK(td->td_proc);
976 #else
977 error = suser(td);
978 if (error)
979 return (error);
980 #endif
981
982 if (uap->how & MCL_FUTURE) {
983 vm_map_lock(map);
984 vm_map_modflags(map, MAP_WIREFUTURE, 0);
985 vm_map_unlock(map);
986 error = 0;
987 }
988
989 if (uap->how & MCL_CURRENT) {
990 /*
991 * P1003.1-2001 mandates that all currently mapped pages
992 * will be memory resident and locked (wired) upon return
993 * from mlockall(). vm_map_wire() will wire pages, by
994 * calling vm_fault_wire() for each page in the region.
995 */
996 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
997 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
998 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
999 }
1000
1001 return (error);
1002 }
1003
1004 #ifndef _SYS_SYSPROTO_H_
1005 struct munlockall_args {
1006 register_t dummy;
1007 };
1008 #endif
1009
1010 /*
1011 * MPSAFE
1012 */
1013 int
1014 munlockall(td, uap)
1015 struct thread *td;
1016 struct munlockall_args *uap;
1017 {
1018 vm_map_t map;
1019 int error;
1020
1021 map = &td->td_proc->p_vmspace->vm_map;
1022 error = suser(td);
1023 if (error)
1024 return (error);
1025
1026 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1027 vm_map_lock(map);
1028 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1029 vm_map_unlock(map);
1030
1031 /* Forcibly unwire all pages. */
1032 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1033 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1034
1035 return (error);
1036 }
1037
1038 #ifndef _SYS_SYSPROTO_H_
1039 struct munlock_args {
1040 const void *addr;
1041 size_t len;
1042 };
1043 #endif
1044 /*
1045 * MPSAFE
1046 */
1047 int
1048 munlock(td, uap)
1049 struct thread *td;
1050 struct munlock_args *uap;
1051 {
1052 vm_offset_t addr, end, last, start;
1053 vm_size_t size;
1054 int error;
1055
1056 error = suser(td);
1057 if (error)
1058 return (error);
1059 addr = (vm_offset_t)uap->addr;
1060 size = uap->len;
1061 last = addr + size;
1062 start = trunc_page(addr);
1063 end = round_page(last);
1064 if (last < addr || end < addr)
1065 return (EINVAL);
1066 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1067 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1068 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1069 }
1070
1071 /*
1072 * vm_mmap_vnode()
1073 *
1074 * MPSAFE
1075 *
1076 * Helper function for vm_mmap. Perform sanity check specific for mmap
1077 * operations on vnodes.
1078 */
1079 int
1080 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1081 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1082 struct vnode *vp, vm_ooffset_t foff, vm_object_t *objp)
1083 {
1084 struct vattr va;
1085 void *handle;
1086 vm_object_t obj;
1087 struct mount *mp;
1088 int error, flags, type;
1089 int vfslocked;
1090
1091 mp = vp->v_mount;
1092 vfslocked = VFS_LOCK_GIANT(mp);
1093 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
1094 VFS_UNLOCK_GIANT(vfslocked);
1095 return (error);
1096 }
1097 flags = *flagsp;
1098 obj = vp->v_object;
1099 if (vp->v_type == VREG) {
1100 /*
1101 * Get the proper underlying object
1102 */
1103 if (obj == NULL) {
1104 error = EINVAL;
1105 goto done;
1106 }
1107 if (obj->handle != vp) {
1108 vput(vp);
1109 vp = (struct vnode*)obj->handle;
1110 vget(vp, LK_EXCLUSIVE, td);
1111 }
1112 type = OBJT_VNODE;
1113 handle = vp;
1114 } else if (vp->v_type == VCHR) {
1115 type = OBJT_DEVICE;
1116 handle = vp->v_rdev;
1117
1118 /* XXX: lack thredref on device */
1119 if(vp->v_rdev->si_devsw->d_flags & D_MMAP_ANON) {
1120 *maxprotp = VM_PROT_ALL;
1121 *flagsp |= MAP_ANON;
1122 error = 0;
1123 goto done;
1124 }
1125 /*
1126 * cdevs does not provide private mappings of any kind.
1127 */
1128 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1129 (prot & PROT_WRITE) != 0) {
1130 error = EACCES;
1131 goto done;
1132 }
1133 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1134 error = EINVAL;
1135 goto done;
1136 }
1137 /*
1138 * Force device mappings to be shared.
1139 */
1140 flags |= MAP_SHARED;
1141 } else {
1142 error = EINVAL;
1143 goto done;
1144 }
1145 if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
1146 goto done;
1147 }
1148 #ifdef MAC
1149 error = mac_check_vnode_mmap(td->td_ucred, vp, prot, flags);
1150 if (error != 0)
1151 goto done;
1152 #endif
1153 if ((flags & MAP_SHARED) != 0) {
1154 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1155 if (prot & PROT_WRITE) {
1156 error = EPERM;
1157 goto done;
1158 }
1159 *maxprotp &= ~VM_PROT_WRITE;
1160 }
1161 }
1162 /*
1163 * If it is a regular file without any references
1164 * we do not need to sync it.
1165 * Adjust object size to be the size of actual file.
1166 */
1167 if (vp->v_type == VREG) {
1168 objsize = round_page(va.va_size);
1169 if (va.va_nlink == 0)
1170 flags |= MAP_NOSYNC;
1171 }
1172 obj = vm_pager_allocate(type, handle, objsize, prot, foff);
1173 if (obj == NULL) {
1174 error = (type == OBJT_DEVICE ? EINVAL : ENOMEM);
1175 goto done;
1176 }
1177 *objp = obj;
1178 *flagsp = flags;
1179 vfs_mark_atime(vp, td);
1180
1181 done:
1182 vput(vp);
1183 VFS_UNLOCK_GIANT(vfslocked);
1184 return (error);
1185 }
1186
1187 /*
1188 * vm_mmap_cdev()
1189 *
1190 * MPSAFE
1191 *
1192 * Helper function for vm_mmap. Perform sanity check specific for mmap
1193 * operations on cdevs.
1194 */
1195 int
1196 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1197 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1198 struct cdev *cdev, vm_ooffset_t foff, vm_object_t *objp)
1199 {
1200 vm_object_t obj;
1201 int flags;
1202
1203 flags = *flagsp;
1204
1205 /* XXX: lack thredref on device */
1206 if (cdev->si_devsw->d_flags & D_MMAP_ANON) {
1207 *maxprotp = VM_PROT_ALL;
1208 *flagsp |= MAP_ANON;
1209 return (0);
1210 }
1211 /*
1212 * cdevs does not provide private mappings of any kind.
1213 */
1214 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1215 (prot & PROT_WRITE) != 0)
1216 return (EACCES);
1217 if (flags & (MAP_PRIVATE|MAP_COPY))
1218 return (EINVAL);
1219 /*
1220 * Force device mappings to be shared.
1221 */
1222 flags |= MAP_SHARED;
1223 #ifdef MAC_XXX
1224 error = mac_check_cdev_mmap(td->td_ucred, cdev, prot);
1225 if (error != 0)
1226 return (error);
1227 #endif
1228 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, foff);
1229 if (obj == NULL)
1230 return (EINVAL);
1231 *objp = obj;
1232 *flagsp = flags;
1233 return (0);
1234 }
1235
1236 /*
1237 * vm_mmap()
1238 *
1239 * MPSAFE
1240 *
1241 * Internal version of mmap. Currently used by mmap, exec, and sys5
1242 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1243 */
1244 int
1245 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1246 vm_prot_t maxprot, int flags,
1247 objtype_t handle_type, void *handle,
1248 vm_ooffset_t foff)
1249 {
1250 boolean_t fitit;
1251 vm_object_t object;
1252 int rv = KERN_SUCCESS;
1253 int docow, error;
1254 struct thread *td = curthread;
1255
1256 if (size == 0)
1257 return (0);
1258
1259 size = round_page(size);
1260
1261 PROC_LOCK(td->td_proc);
1262 if (td->td_proc->p_vmspace->vm_map.size + size >
1263 lim_cur(td->td_proc, RLIMIT_VMEM)) {
1264 PROC_UNLOCK(td->td_proc);
1265 return(ENOMEM);
1266 }
1267 PROC_UNLOCK(td->td_proc);
1268
1269 /*
1270 * We currently can only deal with page aligned file offsets.
1271 * The check is here rather than in the syscall because the
1272 * kernel calls this function internally for other mmaping
1273 * operations (such as in exec) and non-aligned offsets will
1274 * cause pmap inconsistencies...so we want to be sure to
1275 * disallow this in all cases.
1276 */
1277 if (foff & PAGE_MASK)
1278 return (EINVAL);
1279
1280 if ((flags & MAP_FIXED) == 0) {
1281 fitit = TRUE;
1282 *addr = round_page(*addr);
1283 } else {
1284 if (*addr != trunc_page(*addr))
1285 return (EINVAL);
1286 fitit = FALSE;
1287 }
1288 /*
1289 * Lookup/allocate object.
1290 */
1291 switch (handle_type) {
1292 case OBJT_DEVICE:
1293 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1294 handle, foff, &object);
1295 break;
1296 case OBJT_VNODE:
1297 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1298 handle, foff, &object);
1299 break;
1300 case OBJT_DEFAULT:
1301 if (handle == NULL) {
1302 error = 0;
1303 break;
1304 }
1305 /* FALLTHROUGH */
1306 default:
1307 error = EINVAL;
1308 }
1309 if (error)
1310 return (error);
1311 if (flags & MAP_ANON) {
1312 object = NULL;
1313 docow = 0;
1314 /*
1315 * Unnamed anonymous regions always start at 0.
1316 */
1317 if (handle == 0)
1318 foff = 0;
1319 } else {
1320 docow = MAP_PREFAULT_PARTIAL;
1321 }
1322
1323 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1324 docow |= MAP_COPY_ON_WRITE;
1325 if (flags & MAP_NOSYNC)
1326 docow |= MAP_DISABLE_SYNCER;
1327 if (flags & MAP_NOCORE)
1328 docow |= MAP_DISABLE_COREDUMP;
1329
1330 #if defined(VM_PROT_READ_IS_EXEC)
1331 if (prot & VM_PROT_READ)
1332 prot |= VM_PROT_EXECUTE;
1333
1334 if (maxprot & VM_PROT_READ)
1335 maxprot |= VM_PROT_EXECUTE;
1336 #endif
1337
1338 if (fitit)
1339 *addr = pmap_addr_hint(object, *addr, size);
1340
1341 if (flags & MAP_STACK)
1342 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1343 docow | MAP_STACK_GROWS_DOWN);
1344 else if (fitit)
1345 rv = vm_map_find(map, object, foff, addr, size, TRUE,
1346 prot, maxprot, docow);
1347 else
1348 rv = vm_map_fixed(map, object, foff, addr, size,
1349 prot, maxprot, docow);
1350
1351 if (rv != KERN_SUCCESS) {
1352 /*
1353 * Lose the object reference. Will destroy the
1354 * object if it's an unnamed anonymous mapping
1355 * or named anonymous without other references.
1356 */
1357 vm_object_deallocate(object);
1358 } else if (flags & MAP_SHARED) {
1359 /*
1360 * Shared memory is also shared with children.
1361 */
1362 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
1363 if (rv != KERN_SUCCESS)
1364 (void) vm_map_remove(map, *addr, *addr + size);
1365 }
1366
1367 /*
1368 * If the process has requested that all future mappings
1369 * be wired, then heed this.
1370 */
1371 if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
1372 vm_map_wire(map, *addr, *addr + size,
1373 VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
1374
1375 switch (rv) {
1376 case KERN_SUCCESS:
1377 return (0);
1378 case KERN_INVALID_ADDRESS:
1379 case KERN_NO_SPACE:
1380 return (ENOMEM);
1381 case KERN_PROTECTION_FAILURE:
1382 return (EACCES);
1383 default:
1384 return (EINVAL);
1385 }
1386 }
Cache object: 8c067bddc734e46d927271a2c6c25deb
|