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