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