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