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