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