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