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