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