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