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