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