1 /* 2 * (MPSAFE) 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. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$ 41 * 42 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94 43 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $ 44 * $DragonFly: src/sys/vm/vm_mmap.c,v 1.39 2007/04/30 07:18:57 dillon Exp $ 45 */ 46 47 /* 48 * Mapped file (mmap) interface to VM 49 */ 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/systm.h> 54 #include <sys/sysproto.h> 55 #include <sys/filedesc.h> 56 #include <sys/kern_syscall.h> 57 #include <sys/proc.h> 58 #include <sys/priv.h> 59 #include <sys/resource.h> 60 #include <sys/resourcevar.h> 61 #include <sys/vnode.h> 62 #include <sys/fcntl.h> 63 #include <sys/file.h> 64 #include <sys/mman.h> 65 #include <sys/conf.h> 66 #include <sys/stat.h> 67 #include <sys/vmmeter.h> 68 #include <sys/sysctl.h> 69 70 #include <vm/vm.h> 71 #include <vm/vm_param.h> 72 #include <sys/lock.h> 73 #include <vm/pmap.h> 74 #include <vm/vm_map.h> 75 #include <vm/vm_object.h> 76 #include <vm/vm_page.h> 77 #include <vm/vm_pager.h> 78 #include <vm/vm_pageout.h> 79 #include <vm/vm_extern.h> 80 #include <vm/vm_page.h> 81 #include <vm/vm_kern.h> 82 83 #include <sys/file2.h> 84 #include <sys/thread.h> 85 #include <sys/thread2.h> 86 87 static int max_proc_mmap; 88 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, ""); 89 int vkernel_enable; 90 SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, ""); 91 92 /* 93 * Set the maximum number of vm_map_entry structures per process. Roughly 94 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100 95 * of our KVM malloc space still results in generous limits. We want a 96 * default that is good enough to prevent the kernel running out of resources 97 * if attacked from compromised user account but generous enough such that 98 * multi-threaded processes are not unduly inconvenienced. 99 */ 100 101 static void vmmapentry_rsrc_init (void *); 102 SYSINIT(vmmersrc, SI_BOOT1_POST, SI_ORDER_ANY, vmmapentry_rsrc_init, NULL) 103 104 static void 105 vmmapentry_rsrc_init(void *dummy) 106 { 107 max_proc_mmap = KvaSize / sizeof(struct vm_map_entry); 108 max_proc_mmap /= 100; 109 } 110 111 /* 112 * MPSAFE 113 */ 114 int 115 sys_sbrk(struct sbrk_args *uap) 116 { 117 /* Not yet implemented */ 118 return (EOPNOTSUPP); 119 } 120 121 /* 122 * sstk_args(int incr) 123 * 124 * MPSAFE 125 */ 126 int 127 sys_sstk(struct sstk_args *uap) 128 { 129 /* Not yet implemented */ 130 return (EOPNOTSUPP); 131 } 132 133 /* 134 * mmap_args(void *addr, size_t len, int prot, int flags, int fd, 135 * long pad, off_t pos) 136 * 137 * Memory Map (mmap) system call. Note that the file offset 138 * and address are allowed to be NOT page aligned, though if 139 * the MAP_FIXED flag it set, both must have the same remainder 140 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not 141 * page-aligned, the actual mapping starts at trunc_page(addr) 142 * and the return value is adjusted up by the page offset. 143 * 144 * Generally speaking, only character devices which are themselves 145 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise 146 * there would be no cache coherency between a descriptor and a VM mapping 147 * both to the same character device. 148 * 149 * Block devices can be mmap'd no matter what they represent. Cache coherency 150 * is maintained as long as you do not write directly to the underlying 151 * character device. 152 * 153 * Requires caller to hold vm_token. 154 */ 155 int 156 kern_mmap(struct vmspace *vms, caddr_t uaddr, size_t ulen, 157 int uprot, int uflags, int fd, off_t upos, void **res) 158 { 159 struct thread *td = curthread; 160 struct proc *p = td->td_proc; 161 struct file *fp = NULL; 162 struct vnode *vp; 163 vm_offset_t addr; 164 vm_offset_t tmpaddr; 165 vm_size_t size, pageoff; 166 vm_prot_t prot, maxprot; 167 void *handle; 168 int flags, error; 169 off_t pos; 170 vm_object_t obj; 171 172 KKASSERT(p); 173 ASSERT_LWKT_TOKEN_HELD(&vm_token); 174 175 addr = (vm_offset_t) uaddr; 176 size = ulen; 177 prot = uprot & VM_PROT_ALL; 178 flags = uflags; 179 pos = upos; 180 181 /* 182 * Make sure mapping fits into numeric range etc. 183 * 184 * NOTE: We support the full unsigned range for size now. 185 */ 186 if (((flags & MAP_ANON) && (fd != -1 || pos != 0))) 187 return (EINVAL); 188 189 if (flags & MAP_STACK) { 190 if ((fd != -1) || 191 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE))) 192 return (EINVAL); 193 flags |= MAP_ANON; 194 pos = 0; 195 } 196 197 /* 198 * Virtual page tables cannot be used with MAP_STACK. Apart from 199 * it not making any sense, the aux union is used by both 200 * types. 201 * 202 * Because the virtual page table is stored in the backing object 203 * and might be updated by the kernel, the mapping must be R+W. 204 */ 205 if (flags & MAP_VPAGETABLE) { 206 if (vkernel_enable == 0) 207 return (EOPNOTSUPP); 208 if (flags & MAP_STACK) 209 return (EINVAL); 210 if ((prot & (PROT_READ|PROT_WRITE)) != (PROT_READ|PROT_WRITE)) 211 return (EINVAL); 212 } 213 214 /* 215 * Align the file position to a page boundary, 216 * and save its page offset component. 217 */ 218 pageoff = (pos & PAGE_MASK); 219 pos -= pageoff; 220 221 /* Adjust size for rounding (on both ends). */ 222 size += pageoff; /* low end... */ 223 size = (vm_size_t) round_page(size); /* hi end */ 224 if (size < ulen) /* wrap */ 225 return(EINVAL); 226 227 /* 228 * Check for illegal addresses. Watch out for address wrap... Note 229 * that VM_*_ADDRESS are not constants due to casts (argh). 230 */ 231 if (flags & (MAP_FIXED | MAP_TRYFIXED)) { 232 /* 233 * The specified address must have the same remainder 234 * as the file offset taken modulo PAGE_SIZE, so it 235 * should be aligned after adjustment by pageoff. 236 */ 237 addr -= pageoff; 238 if (addr & PAGE_MASK) 239 return (EINVAL); 240 241 /* 242 * Address range must be all in user VM space and not wrap. 243 */ 244 tmpaddr = addr + size; 245 if (tmpaddr < addr) 246 return (EINVAL); 247 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) 248 return (EINVAL); 249 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS) 250 return (EINVAL); 251 } else { 252 /* 253 * Set a reasonable start point for the hint if it was 254 * not specified or if it falls within the heap space. 255 * Hinted mmap()s do not allocate out of the heap space. 256 */ 257 if (addr == 0 || 258 (addr >= round_page((vm_offset_t)vms->vm_taddr) && 259 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) 260 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz); 261 } 262 263 if (flags & MAP_ANON) { 264 /* 265 * Mapping blank space is trivial. 266 */ 267 handle = NULL; 268 maxprot = VM_PROT_ALL; 269 } else { 270 /* 271 * Mapping file, get fp for validation. Obtain vnode and make 272 * sure it is of appropriate type. 273 */ 274 fp = holdfp(p->p_fd, fd, -1); 275 if (fp == NULL) 276 return (EBADF); 277 if (fp->f_type != DTYPE_VNODE) { 278 error = EINVAL; 279 goto done; 280 } 281 /* 282 * POSIX shared-memory objects are defined to have 283 * kernel persistence, and are not defined to support 284 * read(2)/write(2) -- or even open(2). Thus, we can 285 * use MAP_ASYNC to trade on-disk coherence for speed. 286 * The shm_open(3) library routine turns on the FPOSIXSHM 287 * flag to request this behavior. 288 */ 289 if (fp->f_flag & FPOSIXSHM) 290 flags |= MAP_NOSYNC; 291 vp = (struct vnode *) fp->f_data; 292 293 /* 294 * Validate the vnode for the operation. 295 */ 296 switch(vp->v_type) { 297 case VREG: 298 /* 299 * Get the proper underlying object 300 */ 301 if ((obj = vp->v_object) == NULL) { 302 error = EINVAL; 303 goto done; 304 } 305 KKASSERT((struct vnode *)obj->handle == vp); 306 break; 307 case VCHR: 308 /* 309 * Make sure a device has not been revoked. 310 * Mappability is handled by the device layer. 311 */ 312 if (vp->v_rdev == NULL) { 313 error = EBADF; 314 goto done; 315 } 316 break; 317 default: 318 /* 319 * Nothing else is mappable. 320 */ 321 error = EINVAL; 322 goto done; 323 } 324 325 /* 326 * XXX hack to handle use of /dev/zero to map anon memory (ala 327 * SunOS). 328 */ 329 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) { 330 handle = NULL; 331 maxprot = VM_PROT_ALL; 332 flags |= MAP_ANON; 333 pos = 0; 334 } else { 335 /* 336 * cdevs does not provide private mappings of any kind. 337 */ 338 if (vp->v_type == VCHR && 339 (flags & (MAP_PRIVATE|MAP_COPY))) { 340 error = EINVAL; 341 goto done; 342 } 343 /* 344 * Ensure that file and memory protections are 345 * compatible. Note that we only worry about 346 * writability if mapping is shared; in this case, 347 * current and max prot are dictated by the open file. 348 * XXX use the vnode instead? Problem is: what 349 * credentials do we use for determination? What if 350 * proc does a setuid? 351 */ 352 maxprot = VM_PROT_EXECUTE; /* ??? */ 353 if (fp->f_flag & FREAD) { 354 maxprot |= VM_PROT_READ; 355 } else if (prot & PROT_READ) { 356 error = EACCES; 357 goto done; 358 } 359 /* 360 * If we are sharing potential changes (either via 361 * MAP_SHARED or via the implicit sharing of character 362 * device mappings), and we are trying to get write 363 * permission although we opened it without asking 364 * for it, bail out. Check for superuser, only if 365 * we're at securelevel < 1, to allow the XIG X server 366 * to continue to work. 367 */ 368 if ((flags & MAP_SHARED) != 0 || vp->v_type == VCHR) { 369 if ((fp->f_flag & FWRITE) != 0) { 370 struct vattr va; 371 if ((error = VOP_GETATTR(vp, &va))) { 372 goto done; 373 } 374 if ((va.va_flags & 375 (IMMUTABLE|APPEND)) == 0) { 376 maxprot |= VM_PROT_WRITE; 377 } else if (prot & PROT_WRITE) { 378 error = EPERM; 379 goto done; 380 } 381 } else if ((prot & PROT_WRITE) != 0) { 382 error = EACCES; 383 goto done; 384 } 385 } else { 386 maxprot |= VM_PROT_WRITE; 387 } 388 handle = (void *)vp; 389 } 390 } 391 392 /* 393 * Do not allow more then a certain number of vm_map_entry structures 394 * per process. Scale with the number of rforks sharing the map 395 * to make the limit reasonable for threads. 396 */ 397 if (max_proc_mmap && 398 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) { 399 error = ENOMEM; 400 goto done; 401 } 402 403 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot, 404 flags, handle, pos); 405 if (error == 0) 406 *res = (void *)(addr + pageoff); 407 done: 408 if (fp) 409 fdrop(fp); 410 return (error); 411 } 412 413 /* 414 * mmap system call handler 415 * 416 * No requirements. 417 */ 418 int 419 sys_mmap(struct mmap_args *uap) 420 { 421 int error; 422 423 lwkt_gettoken(&vm_token); 424 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len, 425 uap->prot, uap->flags, 426 uap->fd, uap->pos, &uap->sysmsg_resultp); 427 lwkt_reltoken(&vm_token); 428 429 return (error); 430 } 431 432 /* 433 * msync system call handler 434 * 435 * msync_args(void *addr, size_t len, int flags) 436 * 437 * No requirements 438 */ 439 int 440 sys_msync(struct msync_args *uap) 441 { 442 struct proc *p = curproc; 443 vm_offset_t addr; 444 vm_offset_t tmpaddr; 445 vm_size_t size, pageoff; 446 int flags; 447 vm_map_t map; 448 int rv; 449 450 addr = (vm_offset_t) uap->addr; 451 size = uap->len; 452 flags = uap->flags; 453 454 pageoff = (addr & PAGE_MASK); 455 addr -= pageoff; 456 size += pageoff; 457 size = (vm_size_t) round_page(size); 458 if (size < uap->len) /* wrap */ 459 return(EINVAL); 460 tmpaddr = addr + size; /* workaround gcc4 opt */ 461 if (tmpaddr < addr) /* wrap */ 462 return(EINVAL); 463 464 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 465 return (EINVAL); 466 467 lwkt_gettoken(&vm_token); 468 map = &p->p_vmspace->vm_map; 469 470 /* 471 * XXX Gak! If size is zero we are supposed to sync "all modified 472 * pages with the region containing addr". Unfortunately, we don't 473 * really keep track of individual mmaps so we approximate by flushing 474 * the range of the map entry containing addr. This can be incorrect 475 * if the region splits or is coalesced with a neighbor. 476 */ 477 if (size == 0) { 478 vm_map_entry_t entry; 479 480 vm_map_lock_read(map); 481 rv = vm_map_lookup_entry(map, addr, &entry); 482 if (rv == FALSE) { 483 vm_map_unlock_read(map); 484 rv = KERN_INVALID_ADDRESS; 485 goto done; 486 } 487 addr = entry->start; 488 size = entry->end - entry->start; 489 vm_map_unlock_read(map); 490 } 491 492 /* 493 * Clean the pages and interpret the return value. 494 */ 495 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 496 (flags & MS_INVALIDATE) != 0); 497 done: 498 lwkt_reltoken(&vm_token); 499 500 switch (rv) { 501 case KERN_SUCCESS: 502 break; 503 case KERN_INVALID_ADDRESS: 504 return (EINVAL); /* Sun returns ENOMEM? */ 505 case KERN_FAILURE: 506 return (EIO); 507 default: 508 return (EINVAL); 509 } 510 511 return (0); 512 } 513 514 /* 515 * munmap system call handler 516 * 517 * munmap_args(void *addr, size_t len) 518 * 519 * No requirements 520 */ 521 int 522 sys_munmap(struct munmap_args *uap) 523 { 524 struct proc *p = curproc; 525 vm_offset_t addr; 526 vm_offset_t tmpaddr; 527 vm_size_t size, pageoff; 528 vm_map_t map; 529 530 addr = (vm_offset_t) uap->addr; 531 size = uap->len; 532 533 pageoff = (addr & PAGE_MASK); 534 addr -= pageoff; 535 size += pageoff; 536 size = (vm_size_t) round_page(size); 537 if (size < uap->len) /* wrap */ 538 return(EINVAL); 539 tmpaddr = addr + size; /* workaround gcc4 opt */ 540 if (tmpaddr < addr) /* wrap */ 541 return(EINVAL); 542 543 if (size == 0) 544 return (0); 545 546 /* 547 * Check for illegal addresses. Watch out for address wrap... Note 548 * that VM_*_ADDRESS are not constants due to casts (argh). 549 */ 550 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) 551 return (EINVAL); 552 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS) 553 return (EINVAL); 554 555 lwkt_gettoken(&vm_token); 556 map = &p->p_vmspace->vm_map; 557 /* 558 * Make sure entire range is allocated. 559 */ 560 if (!vm_map_check_protection(map, addr, addr + size, 561 VM_PROT_NONE, FALSE)) { 562 lwkt_reltoken(&vm_token); 563 return (EINVAL); 564 } 565 /* returns nothing but KERN_SUCCESS anyway */ 566 vm_map_remove(map, addr, addr + size); 567 lwkt_reltoken(&vm_token); 568 return (0); 569 } 570 571 /* 572 * mprotect_args(const void *addr, size_t len, int prot) 573 * 574 * MPALMOSTSAFE 575 */ 576 int 577 sys_mprotect(struct mprotect_args *uap) 578 { 579 struct proc *p = curproc; 580 vm_offset_t addr; 581 vm_offset_t tmpaddr; 582 vm_size_t size, pageoff; 583 vm_prot_t prot; 584 int error; 585 586 addr = (vm_offset_t) uap->addr; 587 size = uap->len; 588 prot = uap->prot & VM_PROT_ALL; 589 #if defined(VM_PROT_READ_IS_EXEC) 590 if (prot & VM_PROT_READ) 591 prot |= VM_PROT_EXECUTE; 592 #endif 593 594 pageoff = (addr & PAGE_MASK); 595 addr -= pageoff; 596 size += pageoff; 597 size = (vm_size_t) round_page(size); 598 if (size < uap->len) /* wrap */ 599 return(EINVAL); 600 tmpaddr = addr + size; /* workaround gcc4 opt */ 601 if (tmpaddr < addr) /* wrap */ 602 return(EINVAL); 603 604 lwkt_gettoken(&vm_token); 605 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, 606 prot, FALSE)) { 607 case KERN_SUCCESS: 608 error = 0; 609 break; 610 case KERN_PROTECTION_FAILURE: 611 error = EACCES; 612 break; 613 default: 614 error = EINVAL; 615 break; 616 } 617 lwkt_reltoken(&vm_token); 618 return (error); 619 } 620 621 /* 622 * minherit system call handler 623 * 624 * minherit_args(void *addr, size_t len, int inherit) 625 * 626 * No requirements. 627 */ 628 int 629 sys_minherit(struct minherit_args *uap) 630 { 631 struct proc *p = curproc; 632 vm_offset_t addr; 633 vm_offset_t tmpaddr; 634 vm_size_t size, pageoff; 635 vm_inherit_t inherit; 636 int error; 637 638 addr = (vm_offset_t)uap->addr; 639 size = uap->len; 640 inherit = uap->inherit; 641 642 pageoff = (addr & PAGE_MASK); 643 addr -= pageoff; 644 size += pageoff; 645 size = (vm_size_t) round_page(size); 646 if (size < uap->len) /* wrap */ 647 return(EINVAL); 648 tmpaddr = addr + size; /* workaround gcc4 opt */ 649 if (tmpaddr < addr) /* wrap */ 650 return(EINVAL); 651 652 lwkt_gettoken(&vm_token); 653 654 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, 655 addr + size, inherit)) { 656 case KERN_SUCCESS: 657 error = 0; 658 break; 659 case KERN_PROTECTION_FAILURE: 660 error = EACCES; 661 break; 662 default: 663 error = EINVAL; 664 break; 665 } 666 lwkt_reltoken(&vm_token); 667 return (error); 668 } 669 670 /* 671 * madvise system call handler 672 * 673 * madvise_args(void *addr, size_t len, int behav) 674 * 675 * No requirements. 676 */ 677 int 678 sys_madvise(struct madvise_args *uap) 679 { 680 struct proc *p = curproc; 681 vm_offset_t start, end; 682 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len; 683 int error; 684 685 /* 686 * Check for illegal behavior 687 */ 688 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END) 689 return (EINVAL); 690 /* 691 * Check for illegal addresses. Watch out for address wrap... Note 692 * that VM_*_ADDRESS are not constants due to casts (argh). 693 */ 694 if (tmpaddr < (vm_offset_t)uap->addr) 695 return (EINVAL); 696 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) 697 return (EINVAL); 698 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS) 699 return (EINVAL); 700 701 /* 702 * Since this routine is only advisory, we default to conservative 703 * behavior. 704 */ 705 start = trunc_page((vm_offset_t)uap->addr); 706 end = round_page(tmpaddr); 707 708 lwkt_gettoken(&vm_token); 709 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end, 710 uap->behav, 0); 711 lwkt_reltoken(&vm_token); 712 return (error); 713 } 714 715 /* 716 * mcontrol system call handler 717 * 718 * mcontrol_args(void *addr, size_t len, int behav, off_t value) 719 * 720 * No requirements 721 */ 722 int 723 sys_mcontrol(struct mcontrol_args *uap) 724 { 725 struct proc *p = curproc; 726 vm_offset_t start, end; 727 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len; 728 int error; 729 730 /* 731 * Check for illegal behavior 732 */ 733 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END) 734 return (EINVAL); 735 /* 736 * Check for illegal addresses. Watch out for address wrap... Note 737 * that VM_*_ADDRESS are not constants due to casts (argh). 738 */ 739 if (tmpaddr < (vm_offset_t) uap->addr) 740 return (EINVAL); 741 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) 742 return (EINVAL); 743 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS) 744 return (EINVAL); 745 746 /* 747 * Since this routine is only advisory, we default to conservative 748 * behavior. 749 */ 750 start = trunc_page((vm_offset_t)uap->addr); 751 end = round_page(tmpaddr); 752 753 lwkt_gettoken(&vm_token); 754 error = vm_map_madvise(&p->p_vmspace->vm_map, start, end, 755 uap->behav, uap->value); 756 lwkt_reltoken(&vm_token); 757 return (error); 758 } 759 760 761 /* 762 * mincore system call handler 763 * 764 * mincore_args(const void *addr, size_t len, char *vec) 765 * 766 * No requirements 767 */ 768 int 769 sys_mincore(struct mincore_args *uap) 770 { 771 struct proc *p = curproc; 772 vm_offset_t addr, first_addr; 773 vm_offset_t end, cend; 774 pmap_t pmap; 775 vm_map_t map; 776 char *vec; 777 int error; 778 int vecindex, lastvecindex; 779 vm_map_entry_t current; 780 vm_map_entry_t entry; 781 int mincoreinfo; 782 unsigned int timestamp; 783 784 /* 785 * Make sure that the addresses presented are valid for user 786 * mode. 787 */ 788 first_addr = addr = trunc_page((vm_offset_t) uap->addr); 789 end = addr + (vm_size_t)round_page(uap->len); 790 if (end < addr) 791 return (EINVAL); 792 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS) 793 return (EINVAL); 794 795 /* 796 * Address of byte vector 797 */ 798 vec = uap->vec; 799 800 map = &p->p_vmspace->vm_map; 801 pmap = vmspace_pmap(p->p_vmspace); 802 803 lwkt_gettoken(&vm_token); 804 vm_map_lock_read(map); 805 RestartScan: 806 timestamp = map->timestamp; 807 808 if (!vm_map_lookup_entry(map, addr, &entry)) 809 entry = entry->next; 810 811 /* 812 * Do this on a map entry basis so that if the pages are not 813 * in the current processes address space, we can easily look 814 * up the pages elsewhere. 815 */ 816 lastvecindex = -1; 817 for(current = entry; 818 (current != &map->header) && (current->start < end); 819 current = current->next) { 820 821 /* 822 * ignore submaps (for now) or null objects 823 */ 824 if (current->maptype != VM_MAPTYPE_NORMAL && 825 current->maptype != VM_MAPTYPE_VPAGETABLE) { 826 continue; 827 } 828 if (current->object.vm_object == NULL) 829 continue; 830 831 /* 832 * limit this scan to the current map entry and the 833 * limits for the mincore call 834 */ 835 if (addr < current->start) 836 addr = current->start; 837 cend = current->end; 838 if (cend > end) 839 cend = end; 840 841 /* 842 * scan this entry one page at a time 843 */ 844 while (addr < cend) { 845 /* 846 * Check pmap first, it is likely faster, also 847 * it can provide info as to whether we are the 848 * one referencing or modifying the page. 849 * 850 * If we have to check the VM object, only mess 851 * around with normal maps. Do not mess around 852 * with virtual page tables (XXX). 853 */ 854 mincoreinfo = pmap_mincore(pmap, addr); 855 if (mincoreinfo == 0 && 856 current->maptype == VM_MAPTYPE_NORMAL) { 857 vm_pindex_t pindex; 858 vm_ooffset_t offset; 859 vm_page_t m; 860 861 /* 862 * calculate the page index into the object 863 */ 864 offset = current->offset + (addr - current->start); 865 pindex = OFF_TO_IDX(offset); 866 867 /* 868 * if the page is resident, then gather 869 * information about it. spl protection is 870 * required to maintain the object 871 * association. And XXX what if the page is 872 * busy? What's the deal with that? 873 */ 874 crit_enter(); 875 m = vm_page_lookup(current->object.vm_object, 876 pindex); 877 if (m && m->valid) { 878 mincoreinfo = MINCORE_INCORE; 879 if (m->dirty || 880 pmap_is_modified(m)) 881 mincoreinfo |= MINCORE_MODIFIED_OTHER; 882 if ((m->flags & PG_REFERENCED) || 883 pmap_ts_referenced(m)) { 884 vm_page_flag_set(m, PG_REFERENCED); 885 mincoreinfo |= MINCORE_REFERENCED_OTHER; 886 } 887 } 888 crit_exit(); 889 } 890 891 /* 892 * subyte may page fault. In case it needs to modify 893 * the map, we release the lock. 894 */ 895 vm_map_unlock_read(map); 896 897 /* 898 * calculate index into user supplied byte vector 899 */ 900 vecindex = OFF_TO_IDX(addr - first_addr); 901 902 /* 903 * If we have skipped map entries, we need to make sure that 904 * the byte vector is zeroed for those skipped entries. 905 */ 906 while((lastvecindex + 1) < vecindex) { 907 error = subyte( vec + lastvecindex, 0); 908 if (error) { 909 error = EFAULT; 910 goto done; 911 } 912 ++lastvecindex; 913 } 914 915 /* 916 * Pass the page information to the user 917 */ 918 error = subyte( vec + vecindex, mincoreinfo); 919 if (error) { 920 error = EFAULT; 921 goto done; 922 } 923 924 /* 925 * If the map has changed, due to the subyte, the previous 926 * output may be invalid. 927 */ 928 vm_map_lock_read(map); 929 if (timestamp != map->timestamp) 930 goto RestartScan; 931 932 lastvecindex = vecindex; 933 addr += PAGE_SIZE; 934 } 935 } 936 937 /* 938 * subyte may page fault. In case it needs to modify 939 * the map, we release the lock. 940 */ 941 vm_map_unlock_read(map); 942 943 /* 944 * Zero the last entries in the byte vector. 945 */ 946 vecindex = OFF_TO_IDX(end - first_addr); 947 while((lastvecindex + 1) < vecindex) { 948 error = subyte( vec + lastvecindex, 0); 949 if (error) { 950 error = EFAULT; 951 goto done; 952 } 953 ++lastvecindex; 954 } 955 956 /* 957 * If the map has changed, due to the subyte, the previous 958 * output may be invalid. 959 */ 960 vm_map_lock_read(map); 961 if (timestamp != map->timestamp) 962 goto RestartScan; 963 vm_map_unlock_read(map); 964 965 error = 0; 966 done: 967 lwkt_reltoken(&vm_token); 968 return (error); 969 } 970 971 /* 972 * mlock system call handler 973 * 974 * mlock_args(const void *addr, size_t len) 975 * 976 * No requirements 977 */ 978 int 979 sys_mlock(struct mlock_args *uap) 980 { 981 vm_offset_t addr; 982 vm_offset_t tmpaddr; 983 vm_size_t size, pageoff; 984 struct thread *td = curthread; 985 struct proc *p = td->td_proc; 986 int error; 987 988 addr = (vm_offset_t) uap->addr; 989 size = uap->len; 990 991 pageoff = (addr & PAGE_MASK); 992 addr -= pageoff; 993 size += pageoff; 994 size = (vm_size_t) round_page(size); 995 if (size < uap->len) /* wrap */ 996 return(EINVAL); 997 tmpaddr = addr + size; /* workaround gcc4 opt */ 998 if (tmpaddr < addr) /* wrap */ 999 return (EINVAL); 1000 1001 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired) 1002 return (EAGAIN); 1003 1004 lwkt_gettoken(&vm_token); 1005 #ifdef pmap_wired_count 1006 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 1007 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) { 1008 lwkt_reltoken(&vm_token); 1009 return (ENOMEM); 1010 } 1011 #else 1012 error = priv_check_cred(td->td_ucred, PRIV_ROOT, 0); 1013 if (error) { 1014 lwkt_reltoken(&vm_token); 1015 return (error); 1016 } 1017 #endif 1018 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 1019 lwkt_reltoken(&vm_token); 1020 return (error == KERN_SUCCESS ? 0 : ENOMEM); 1021 } 1022 1023 /* 1024 * mlockall_args(int how) 1025 * 1026 * Dummy routine, doesn't actually do anything. 1027 * 1028 * No requirements 1029 */ 1030 int 1031 sys_mlockall(struct mlockall_args *uap) 1032 { 1033 return (ENOSYS); 1034 } 1035 1036 /* 1037 * munlockall_args(void) 1038 * 1039 * Dummy routine, doesn't actually do anything. 1040 * 1041 * No requirements 1042 */ 1043 int 1044 sys_munlockall(struct munlockall_args *uap) 1045 { 1046 return (ENOSYS); 1047 } 1048 1049 /* 1050 * munlock system call handler 1051 * 1052 * munlock_args(const void *addr, size_t len) 1053 * 1054 * No requirements 1055 */ 1056 int 1057 sys_munlock(struct munlock_args *uap) 1058 { 1059 struct thread *td = curthread; 1060 struct proc *p = td->td_proc; 1061 vm_offset_t addr; 1062 vm_offset_t tmpaddr; 1063 vm_size_t size, pageoff; 1064 int error; 1065 1066 addr = (vm_offset_t) uap->addr; 1067 size = uap->len; 1068 1069 pageoff = (addr & PAGE_MASK); 1070 addr -= pageoff; 1071 size += pageoff; 1072 size = (vm_size_t) round_page(size); 1073 1074 tmpaddr = addr + size; 1075 if (tmpaddr < addr) /* wrap */ 1076 return (EINVAL); 1077 1078 #ifndef pmap_wired_count 1079 error = priv_check(td, PRIV_ROOT); 1080 if (error) 1081 return (error); 1082 #endif 1083 1084 lwkt_gettoken(&vm_token); 1085 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 1086 lwkt_reltoken(&vm_token); 1087 return (error == KERN_SUCCESS ? 0 : ENOMEM); 1088 } 1089 1090 /* 1091 * Internal version of mmap. 1092 * Currently used by mmap, exec, and sys5 shared memory. 1093 * Handle is either a vnode pointer or NULL for MAP_ANON. 1094 * 1095 * No requirements; kern_mmap path holds the vm_token 1096 */ 1097 int 1098 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot, 1099 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff) 1100 { 1101 boolean_t fitit; 1102 vm_object_t object; 1103 vm_offset_t eaddr; 1104 vm_size_t esize; 1105 struct vnode *vp; 1106 struct thread *td = curthread; 1107 struct proc *p; 1108 int rv = KERN_SUCCESS; 1109 off_t objsize; 1110 int docow; 1111 1112 if (size == 0) 1113 return (0); 1114 1115 objsize = round_page(size); 1116 if (objsize < size) 1117 return (EINVAL); 1118 size = objsize; 1119 1120 lwkt_gettoken(&vm_token); 1121 1122 /* 1123 * XXX messy code, fixme 1124 * 1125 * NOTE: Overflow checks require discrete statements or GCC4 1126 * will optimize it out. 1127 */ 1128 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) { 1129 esize = map->size + size; /* workaround gcc4 opt */ 1130 if (esize < map->size || 1131 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) { 1132 lwkt_reltoken(&vm_token); 1133 return(ENOMEM); 1134 } 1135 } 1136 1137 /* 1138 * We currently can only deal with page aligned file offsets. 1139 * The check is here rather than in the syscall because the 1140 * kernel calls this function internally for other mmaping 1141 * operations (such as in exec) and non-aligned offsets will 1142 * cause pmap inconsistencies...so we want to be sure to 1143 * disallow this in all cases. 1144 * 1145 * NOTE: Overflow checks require discrete statements or GCC4 1146 * will optimize it out. 1147 */ 1148 if (foff & PAGE_MASK) { 1149 lwkt_reltoken(&vm_token); 1150 return (EINVAL); 1151 } 1152 1153 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) { 1154 fitit = TRUE; 1155 *addr = round_page(*addr); 1156 } else { 1157 if (*addr != trunc_page(*addr)) { 1158 lwkt_reltoken(&vm_token); 1159 return (EINVAL); 1160 } 1161 eaddr = *addr + size; 1162 if (eaddr < *addr) { 1163 lwkt_reltoken(&vm_token); 1164 return (EINVAL); 1165 } 1166 fitit = FALSE; 1167 if ((flags & MAP_TRYFIXED) == 0) 1168 vm_map_remove(map, *addr, *addr + size); 1169 } 1170 1171 /* 1172 * Lookup/allocate object. 1173 */ 1174 if (flags & MAP_ANON) { 1175 /* 1176 * Unnamed anonymous regions always start at 0. 1177 */ 1178 if (handle) { 1179 /* 1180 * Default memory object 1181 */ 1182 object = default_pager_alloc(handle, objsize, 1183 prot, foff); 1184 if (object == NULL) { 1185 lwkt_reltoken(&vm_token); 1186 return(ENOMEM); 1187 } 1188 docow = MAP_PREFAULT_PARTIAL; 1189 } else { 1190 /* 1191 * Implicit single instance of a default memory 1192 * object, so we don't need a VM object yet. 1193 */ 1194 foff = 0; 1195 object = NULL; 1196 docow = 0; 1197 } 1198 vp = NULL; 1199 } else { 1200 vp = (struct vnode *)handle; 1201 if (vp->v_type == VCHR) { 1202 /* 1203 * Device mappings (device size unknown?). 1204 * Force them to be shared. 1205 */ 1206 handle = (void *)(intptr_t)vp->v_rdev; 1207 object = dev_pager_alloc(handle, objsize, prot, foff); 1208 if (object == NULL) { 1209 lwkt_reltoken(&vm_token); 1210 return(EINVAL); 1211 } 1212 docow = MAP_PREFAULT_PARTIAL; 1213 flags &= ~(MAP_PRIVATE|MAP_COPY); 1214 flags |= MAP_SHARED; 1215 } else { 1216 /* 1217 * Regular file mapping (typically). The attribute 1218 * check is for the link count test only. Mmapble 1219 * vnodes must already have a VM object assigned. 1220 */ 1221 struct vattr vat; 1222 int error; 1223 1224 error = VOP_GETATTR(vp, &vat); 1225 if (error) { 1226 lwkt_reltoken(&vm_token); 1227 return (error); 1228 } 1229 docow = MAP_PREFAULT_PARTIAL; 1230 object = vnode_pager_reference(vp); 1231 if (object == NULL && vp->v_type == VREG) { 1232 lwkt_reltoken(&vm_token); 1233 kprintf("Warning: cannot mmap vnode %p, no " 1234 "object\n", vp); 1235 return(EINVAL); 1236 } 1237 1238 /* 1239 * If it is a regular file without any references 1240 * we do not need to sync it. 1241 */ 1242 if (vp->v_type == VREG && vat.va_nlink == 0) { 1243 flags |= MAP_NOSYNC; 1244 } 1245 } 1246 } 1247 1248 /* 1249 * Deal with the adjusted flags 1250 */ 1251 if ((flags & (MAP_ANON|MAP_SHARED)) == 0) 1252 docow |= MAP_COPY_ON_WRITE; 1253 if (flags & MAP_NOSYNC) 1254 docow |= MAP_DISABLE_SYNCER; 1255 if (flags & MAP_NOCORE) 1256 docow |= MAP_DISABLE_COREDUMP; 1257 1258 #if defined(VM_PROT_READ_IS_EXEC) 1259 if (prot & VM_PROT_READ) 1260 prot |= VM_PROT_EXECUTE; 1261 1262 if (maxprot & VM_PROT_READ) 1263 maxprot |= VM_PROT_EXECUTE; 1264 #endif 1265 1266 /* 1267 * This may place the area in its own page directory if (size) is 1268 * large enough, otherwise it typically returns its argument. 1269 */ 1270 if (fitit) { 1271 *addr = pmap_addr_hint(object, *addr, size); 1272 } 1273 1274 /* 1275 * Stack mappings need special attention. 1276 * 1277 * Mappings that use virtual page tables will default to storing 1278 * the page table at offset 0. 1279 */ 1280 if (flags & MAP_STACK) { 1281 rv = vm_map_stack(map, *addr, size, flags, 1282 prot, maxprot, docow); 1283 } else if (flags & MAP_VPAGETABLE) { 1284 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE, 1285 fitit, VM_MAPTYPE_VPAGETABLE, 1286 prot, maxprot, docow); 1287 } else { 1288 rv = vm_map_find(map, object, foff, addr, size, PAGE_SIZE, 1289 fitit, VM_MAPTYPE_NORMAL, 1290 prot, maxprot, docow); 1291 } 1292 1293 if (rv != KERN_SUCCESS) { 1294 /* 1295 * Lose the object reference. Will destroy the 1296 * object if it's an unnamed anonymous mapping 1297 * or named anonymous without other references. 1298 */ 1299 vm_object_deallocate(object); 1300 goto out; 1301 } 1302 1303 /* 1304 * Shared memory is also shared with children. 1305 */ 1306 if (flags & (MAP_SHARED|MAP_INHERIT)) { 1307 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 1308 if (rv != KERN_SUCCESS) { 1309 vm_map_remove(map, *addr, *addr + size); 1310 goto out; 1311 } 1312 } 1313 1314 /* 1315 * Set the access time on the vnode 1316 */ 1317 if (vp != NULL) 1318 vn_mark_atime(vp, td); 1319 out: 1320 lwkt_reltoken(&vm_token); 1321 1322 switch (rv) { 1323 case KERN_SUCCESS: 1324 return (0); 1325 case KERN_INVALID_ADDRESS: 1326 case KERN_NO_SPACE: 1327 return (ENOMEM); 1328 case KERN_PROTECTION_FAILURE: 1329 return (EACCES); 1330 default: 1331 return (EINVAL); 1332 } 1333 } 1334