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