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