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.39 2007/04/30 07:18:57 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/priv.h> 57 #include <sys/resource.h> 58 #include <sys/resourcevar.h> 59 #include <sys/vnode.h> 60 #include <sys/fcntl.h> 61 #include <sys/file.h> 62 #include <sys/mman.h> 63 #include <sys/conf.h> 64 #include <sys/stat.h> 65 #include <sys/vmmeter.h> 66 #include <sys/sysctl.h> 67 68 #include <vm/vm.h> 69 #include <vm/vm_param.h> 70 #include <sys/lock.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 #include <sys/file2.h> 82 #include <sys/thread2.h> 83 84 static int max_proc_mmap; 85 SYSCTL_INT(_vm, OID_AUTO, max_proc_mmap, CTLFLAG_RW, &max_proc_mmap, 0, ""); 86 int vkernel_enable; 87 SYSCTL_INT(_vm, OID_AUTO, vkernel_enable, CTLFLAG_RW, &vkernel_enable, 0, ""); 88 89 /* 90 * Set the maximum number of vm_map_entry structures per process. Roughly 91 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100 92 * of our KVM malloc space still results in generous limits. We want a 93 * default that is good enough to prevent the kernel running out of resources 94 * if attacked from compromised user account but generous enough such that 95 * multi-threaded processes are not unduly inconvenienced. 96 */ 97 98 static void vmmapentry_rsrc_init (void *); 99 SYSINIT(vmmersrc, SI_BOOT1_POST, SI_ORDER_ANY, vmmapentry_rsrc_init, NULL) 100 101 static void 102 vmmapentry_rsrc_init(void *dummy) 103 { 104 max_proc_mmap = KvaSize / 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 | MAP_TRYFIXED)) { 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_MAX_USER_ADDRESS > 0 && addr + size > VM_MAX_USER_ADDRESS) 228 return (EINVAL); 229 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS) 230 return (EINVAL); 231 if (addr + size < addr) 232 return (EINVAL); 233 } else { 234 /* 235 * Set a reasonable start point for the hint if it was 236 * not specified or if it falls within the heap space. 237 * Hinted mmap()s do not allocate out of the heap space. 238 */ 239 if (addr == 0 || 240 (addr >= round_page((vm_offset_t)vms->vm_taddr) && 241 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) 242 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz); 243 } 244 245 if (flags & MAP_ANON) { 246 /* 247 * Mapping blank space is trivial. 248 */ 249 handle = NULL; 250 maxprot = VM_PROT_ALL; 251 pos = 0; 252 } else { 253 /* 254 * Mapping file, get fp for validation. Obtain vnode and make 255 * sure it is of appropriate type. 256 */ 257 fp = holdfp(p->p_fd, fd, -1); 258 if (fp == NULL) 259 return (EBADF); 260 if (fp->f_type != DTYPE_VNODE) { 261 error = EINVAL; 262 goto done; 263 } 264 /* 265 * POSIX shared-memory objects are defined to have 266 * kernel persistence, and are not defined to support 267 * read(2)/write(2) -- or even open(2). Thus, we can 268 * use MAP_ASYNC to trade on-disk coherence for speed. 269 * The shm_open(3) library routine turns on the FPOSIXSHM 270 * flag to request this behavior. 271 */ 272 if (fp->f_flag & FPOSIXSHM) 273 flags |= MAP_NOSYNC; 274 vp = (struct vnode *) fp->f_data; 275 276 /* 277 * Validate the vnode for the operation. 278 */ 279 switch(vp->v_type) { 280 case VREG: 281 /* 282 * Get the proper underlying object 283 */ 284 if ((obj = vp->v_object) == NULL) { 285 error = EINVAL; 286 goto done; 287 } 288 KKASSERT((struct vnode *)obj->handle == vp); 289 break; 290 case VCHR: 291 /* 292 * Make sure a device has not been revoked. 293 * Mappability is handled by the device layer. 294 */ 295 if (vp->v_rdev == NULL) { 296 error = EBADF; 297 goto done; 298 } 299 break; 300 default: 301 /* 302 * Nothing else is mappable. 303 */ 304 error = EINVAL; 305 goto done; 306 } 307 308 /* 309 * XXX hack to handle use of /dev/zero to map anon memory (ala 310 * SunOS). 311 */ 312 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) { 313 handle = NULL; 314 maxprot = VM_PROT_ALL; 315 flags |= MAP_ANON; 316 pos = 0; 317 } else { 318 /* 319 * cdevs does not provide private mappings of any kind. 320 */ 321 /* 322 * However, for XIG X server to continue to work, 323 * we should allow the superuser to do it anyway. 324 * We only allow it at securelevel < 1. 325 * (Because the XIG X server writes directly to video 326 * memory via /dev/mem, it should never work at any 327 * other securelevel. 328 * XXX this will have to go 329 */ 330 if (securelevel >= 1) 331 disablexworkaround = 1; 332 else 333 disablexworkaround = priv_check(td, PRIV_ROOT); 334 if (vp->v_type == VCHR && disablexworkaround && 335 (flags & (MAP_PRIVATE|MAP_COPY))) { 336 error = EINVAL; 337 goto done; 338 } 339 /* 340 * Ensure that file and memory protections are 341 * compatible. Note that we only worry about 342 * writability if mapping is shared; in this case, 343 * current and max prot are dictated by the open file. 344 * XXX use the vnode instead? Problem is: what 345 * credentials do we use for determination? What if 346 * proc does a setuid? 347 */ 348 maxprot = VM_PROT_EXECUTE; /* ??? */ 349 if (fp->f_flag & FREAD) { 350 maxprot |= VM_PROT_READ; 351 } else if (prot & PROT_READ) { 352 error = EACCES; 353 goto done; 354 } 355 /* 356 * If we are sharing potential changes (either via 357 * MAP_SHARED or via the implicit sharing of character 358 * device mappings), and we are trying to get write 359 * permission although we opened it without asking 360 * for it, bail out. Check for superuser, only if 361 * we're at securelevel < 1, to allow the XIG X server 362 * to continue to work. 363 */ 364 365 if ((flags & MAP_SHARED) != 0 || 366 (vp->v_type == VCHR && disablexworkaround)) { 367 if ((fp->f_flag & FWRITE) != 0) { 368 struct vattr va; 369 if ((error = VOP_GETATTR(vp, &va))) { 370 goto done; 371 } 372 if ((va.va_flags & 373 (IMMUTABLE|APPEND)) == 0) { 374 maxprot |= VM_PROT_WRITE; 375 } else if (prot & PROT_WRITE) { 376 error = EPERM; 377 goto done; 378 } 379 } else if ((prot & PROT_WRITE) != 0) { 380 error = EACCES; 381 goto done; 382 } 383 } else { 384 maxprot |= VM_PROT_WRITE; 385 } 386 handle = (void *)vp; 387 } 388 } 389 390 /* 391 * Do not allow more then a certain number of vm_map_entry structures 392 * per process. Scale with the number of rforks sharing the map 393 * to make the limit reasonable for threads. 394 */ 395 if (max_proc_mmap && 396 vms->vm_map.nentries >= max_proc_mmap * vms->vm_sysref.refcnt) { 397 error = ENOMEM; 398 goto done; 399 } 400 401 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot, 402 flags, handle, pos); 403 if (error == 0) 404 *res = (void *)(addr + pageoff); 405 done: 406 if (fp) 407 fdrop(fp); 408 return (error); 409 } 410 411 int 412 sys_mmap(struct mmap_args *uap) 413 { 414 int error; 415 416 error = kern_mmap(curproc->p_vmspace, uap->addr, uap->len, 417 uap->prot, uap->flags, 418 uap->fd, uap->pos, &uap->sysmsg_resultp); 419 420 return (error); 421 } 422 423 /* 424 * msync_args(void *addr, int len, int flags) 425 */ 426 int 427 sys_msync(struct msync_args *uap) 428 { 429 struct proc *p = curproc; 430 vm_offset_t addr; 431 vm_size_t size, pageoff; 432 int flags; 433 vm_map_t map; 434 int rv; 435 436 addr = (vm_offset_t) uap->addr; 437 size = uap->len; 438 flags = uap->flags; 439 440 pageoff = (addr & PAGE_MASK); 441 addr -= pageoff; 442 size += pageoff; 443 size = (vm_size_t) round_page(size); 444 if (addr + size < addr) 445 return(EINVAL); 446 447 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE)) 448 return (EINVAL); 449 450 map = &p->p_vmspace->vm_map; 451 452 /* 453 * XXX Gak! If size is zero we are supposed to sync "all modified 454 * pages with the region containing addr". Unfortunately, we don't 455 * really keep track of individual mmaps so we approximate by flushing 456 * the range of the map entry containing addr. This can be incorrect 457 * if the region splits or is coalesced with a neighbor. 458 */ 459 if (size == 0) { 460 vm_map_entry_t entry; 461 462 vm_map_lock_read(map); 463 rv = vm_map_lookup_entry(map, addr, &entry); 464 vm_map_unlock_read(map); 465 if (rv == FALSE) 466 return (EINVAL); 467 addr = entry->start; 468 size = entry->end - entry->start; 469 } 470 471 /* 472 * Clean the pages and interpret the return value. 473 */ 474 rv = vm_map_clean(map, addr, addr + size, (flags & MS_ASYNC) == 0, 475 (flags & MS_INVALIDATE) != 0); 476 477 switch (rv) { 478 case KERN_SUCCESS: 479 break; 480 case KERN_INVALID_ADDRESS: 481 return (EINVAL); /* Sun returns ENOMEM? */ 482 case KERN_FAILURE: 483 return (EIO); 484 default: 485 return (EINVAL); 486 } 487 488 return (0); 489 } 490 491 /* 492 * munmap_args(void *addr, size_t len) 493 */ 494 int 495 sys_munmap(struct munmap_args *uap) 496 { 497 struct proc *p = curproc; 498 vm_offset_t addr; 499 vm_size_t size, pageoff; 500 vm_map_t map; 501 502 addr = (vm_offset_t) uap->addr; 503 size = uap->len; 504 505 pageoff = (addr & PAGE_MASK); 506 addr -= pageoff; 507 size += pageoff; 508 size = (vm_size_t) round_page(size); 509 if (addr + size < addr) 510 return(EINVAL); 511 512 if (size == 0) 513 return (0); 514 515 /* 516 * Check for illegal addresses. Watch out for address wrap... Note 517 * that VM_*_ADDRESS are not constants due to casts (argh). 518 */ 519 if (VM_MAX_USER_ADDRESS > 0 && addr + size > VM_MAX_USER_ADDRESS) 520 return (EINVAL); 521 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS) 522 return (EINVAL); 523 map = &p->p_vmspace->vm_map; 524 /* 525 * Make sure entire range is allocated. 526 */ 527 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE)) 528 return (EINVAL); 529 /* returns nothing but KERN_SUCCESS anyway */ 530 vm_map_remove(map, addr, addr + size); 531 return (0); 532 } 533 534 /* 535 * mprotect_args(const void *addr, size_t len, int prot) 536 */ 537 int 538 sys_mprotect(struct mprotect_args *uap) 539 { 540 struct proc *p = curproc; 541 vm_offset_t addr; 542 vm_size_t size, pageoff; 543 vm_prot_t prot; 544 545 addr = (vm_offset_t) uap->addr; 546 size = uap->len; 547 prot = uap->prot & VM_PROT_ALL; 548 #if defined(VM_PROT_READ_IS_EXEC) 549 if (prot & VM_PROT_READ) 550 prot |= VM_PROT_EXECUTE; 551 #endif 552 553 pageoff = (addr & PAGE_MASK); 554 addr -= pageoff; 555 size += pageoff; 556 size = (vm_size_t) round_page(size); 557 if (addr + size < addr) 558 return(EINVAL); 559 560 switch (vm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 561 FALSE)) { 562 case KERN_SUCCESS: 563 return (0); 564 case KERN_PROTECTION_FAILURE: 565 return (EACCES); 566 } 567 return (EINVAL); 568 } 569 570 /* 571 * minherit_args(void *addr, size_t len, int inherit) 572 */ 573 int 574 sys_minherit(struct minherit_args *uap) 575 { 576 struct proc *p = curproc; 577 vm_offset_t addr; 578 vm_size_t size, pageoff; 579 vm_inherit_t inherit; 580 581 addr = (vm_offset_t)uap->addr; 582 size = uap->len; 583 inherit = uap->inherit; 584 585 pageoff = (addr & PAGE_MASK); 586 addr -= pageoff; 587 size += pageoff; 588 size = (vm_size_t) round_page(size); 589 if (addr + size < addr) 590 return(EINVAL); 591 592 switch (vm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size, 593 inherit)) { 594 case KERN_SUCCESS: 595 return (0); 596 case KERN_PROTECTION_FAILURE: 597 return (EACCES); 598 } 599 return (EINVAL); 600 } 601 602 /* 603 * madvise_args(void *addr, size_t len, int behav) 604 */ 605 /* ARGSUSED */ 606 int 607 sys_madvise(struct madvise_args *uap) 608 { 609 struct proc *p = curproc; 610 vm_offset_t start, end; 611 612 /* 613 * Check for illegal behavior 614 */ 615 if (uap->behav < 0 || uap->behav >= MADV_CONTROL_END) 616 return (EINVAL); 617 /* 618 * Check for illegal addresses. Watch out for address wrap... Note 619 * that VM_*_ADDRESS are not constants due to casts (argh). 620 */ 621 if (VM_MAX_USER_ADDRESS > 0 && 622 ((vm_offset_t) uap->addr + uap->len) > VM_MAX_USER_ADDRESS) 623 return (EINVAL); 624 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS) 625 return (EINVAL); 626 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 627 return (EINVAL); 628 629 /* 630 * Since this routine is only advisory, we default to conservative 631 * behavior. 632 */ 633 start = trunc_page((vm_offset_t) uap->addr); 634 end = round_page((vm_offset_t) uap->addr + uap->len); 635 636 return (vm_map_madvise(&p->p_vmspace->vm_map, start, end, 637 uap->behav, 0)); 638 } 639 640 /* 641 * mcontrol_args(void *addr, size_t len, int behav, off_t value) 642 */ 643 /* ARGSUSED */ 644 int 645 sys_mcontrol(struct mcontrol_args *uap) 646 { 647 struct proc *p = curproc; 648 vm_offset_t start, end; 649 650 /* 651 * Check for illegal behavior 652 */ 653 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END) 654 return (EINVAL); 655 /* 656 * Check for illegal addresses. Watch out for address wrap... Note 657 * that VM_*_ADDRESS are not constants due to casts (argh). 658 */ 659 if (VM_MAX_USER_ADDRESS > 0 && 660 ((vm_offset_t) uap->addr + uap->len) > VM_MAX_USER_ADDRESS) 661 return (EINVAL); 662 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS) 663 return (EINVAL); 664 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr) 665 return (EINVAL); 666 667 /* 668 * Since this routine is only advisory, we default to conservative 669 * behavior. 670 */ 671 start = trunc_page((vm_offset_t) uap->addr); 672 end = round_page((vm_offset_t) uap->addr + uap->len); 673 674 return (vm_map_madvise(&p->p_vmspace->vm_map, start, end, 675 uap->behav, uap->value)); 676 } 677 678 679 /* 680 * mincore_args(const void *addr, size_t len, char *vec) 681 */ 682 /* ARGSUSED */ 683 int 684 sys_mincore(struct mincore_args *uap) 685 { 686 struct proc *p = curproc; 687 vm_offset_t addr, first_addr; 688 vm_offset_t end, cend; 689 pmap_t pmap; 690 vm_map_t map; 691 char *vec; 692 int error; 693 int vecindex, lastvecindex; 694 vm_map_entry_t current; 695 vm_map_entry_t entry; 696 int mincoreinfo; 697 unsigned int timestamp; 698 699 /* 700 * Make sure that the addresses presented are valid for user 701 * mode. 702 */ 703 first_addr = addr = trunc_page((vm_offset_t) uap->addr); 704 end = addr + (vm_size_t)round_page(uap->len); 705 if (VM_MAX_USER_ADDRESS > 0 && end > VM_MAX_USER_ADDRESS) 706 return (EINVAL); 707 if (end < addr) 708 return (EINVAL); 709 710 /* 711 * Address of byte vector 712 */ 713 vec = uap->vec; 714 715 map = &p->p_vmspace->vm_map; 716 pmap = vmspace_pmap(p->p_vmspace); 717 718 vm_map_lock_read(map); 719 RestartScan: 720 timestamp = map->timestamp; 721 722 if (!vm_map_lookup_entry(map, addr, &entry)) 723 entry = entry->next; 724 725 /* 726 * Do this on a map entry basis so that if the pages are not 727 * in the current processes address space, we can easily look 728 * up the pages elsewhere. 729 */ 730 lastvecindex = -1; 731 for(current = entry; 732 (current != &map->header) && (current->start < end); 733 current = current->next) { 734 735 /* 736 * ignore submaps (for now) or null objects 737 */ 738 if (current->maptype != VM_MAPTYPE_NORMAL && 739 current->maptype != VM_MAPTYPE_VPAGETABLE) { 740 continue; 741 } 742 if (current->object.vm_object == NULL) 743 continue; 744 745 /* 746 * limit this scan to the current map entry and the 747 * limits for the mincore call 748 */ 749 if (addr < current->start) 750 addr = current->start; 751 cend = current->end; 752 if (cend > end) 753 cend = end; 754 755 /* 756 * scan this entry one page at a time 757 */ 758 while (addr < cend) { 759 /* 760 * Check pmap first, it is likely faster, also 761 * it can provide info as to whether we are the 762 * one referencing or modifying the page. 763 * 764 * If we have to check the VM object, only mess 765 * around with normal maps. Do not mess around 766 * with virtual page tables (XXX). 767 */ 768 mincoreinfo = pmap_mincore(pmap, addr); 769 if (mincoreinfo == 0 && 770 current->maptype == VM_MAPTYPE_NORMAL) { 771 vm_pindex_t pindex; 772 vm_ooffset_t offset; 773 vm_page_t m; 774 775 /* 776 * calculate the page index into the object 777 */ 778 offset = current->offset + (addr - current->start); 779 pindex = OFF_TO_IDX(offset); 780 781 /* 782 * if the page is resident, then gather 783 * information about it. spl protection is 784 * required to maintain the object 785 * association. And XXX what if the page is 786 * busy? What's the deal with that? 787 */ 788 crit_enter(); 789 m = vm_page_lookup(current->object.vm_object, 790 pindex); 791 if (m && m->valid) { 792 mincoreinfo = MINCORE_INCORE; 793 if (m->dirty || 794 pmap_is_modified(m)) 795 mincoreinfo |= MINCORE_MODIFIED_OTHER; 796 if ((m->flags & PG_REFERENCED) || 797 pmap_ts_referenced(m)) { 798 vm_page_flag_set(m, PG_REFERENCED); 799 mincoreinfo |= MINCORE_REFERENCED_OTHER; 800 } 801 } 802 crit_exit(); 803 } 804 805 /* 806 * subyte may page fault. In case it needs to modify 807 * the map, we release the lock. 808 */ 809 vm_map_unlock_read(map); 810 811 /* 812 * calculate index into user supplied byte vector 813 */ 814 vecindex = OFF_TO_IDX(addr - first_addr); 815 816 /* 817 * If we have skipped map entries, we need to make sure that 818 * the byte vector is zeroed for those skipped entries. 819 */ 820 while((lastvecindex + 1) < vecindex) { 821 error = subyte( vec + lastvecindex, 0); 822 if (error) { 823 return (EFAULT); 824 } 825 ++lastvecindex; 826 } 827 828 /* 829 * Pass the page information to the user 830 */ 831 error = subyte( vec + vecindex, mincoreinfo); 832 if (error) { 833 return (EFAULT); 834 } 835 836 /* 837 * If the map has changed, due to the subyte, the previous 838 * output may be invalid. 839 */ 840 vm_map_lock_read(map); 841 if (timestamp != map->timestamp) 842 goto RestartScan; 843 844 lastvecindex = vecindex; 845 addr += PAGE_SIZE; 846 } 847 } 848 849 /* 850 * subyte may page fault. In case it needs to modify 851 * the map, we release the lock. 852 */ 853 vm_map_unlock_read(map); 854 855 /* 856 * Zero the last entries in the byte vector. 857 */ 858 vecindex = OFF_TO_IDX(end - first_addr); 859 while((lastvecindex + 1) < vecindex) { 860 error = subyte( vec + lastvecindex, 0); 861 if (error) { 862 return (EFAULT); 863 } 864 ++lastvecindex; 865 } 866 867 /* 868 * If the map has changed, due to the subyte, the previous 869 * output may be invalid. 870 */ 871 vm_map_lock_read(map); 872 if (timestamp != map->timestamp) 873 goto RestartScan; 874 vm_map_unlock_read(map); 875 876 return (0); 877 } 878 879 /* 880 * mlock_args(const void *addr, size_t len) 881 */ 882 int 883 sys_mlock(struct mlock_args *uap) 884 { 885 vm_offset_t addr; 886 vm_size_t size, pageoff; 887 int error; 888 struct proc *p = curproc; 889 890 addr = (vm_offset_t) uap->addr; 891 size = uap->len; 892 893 pageoff = (addr & PAGE_MASK); 894 addr -= pageoff; 895 size += pageoff; 896 size = (vm_size_t) round_page(size); 897 898 /* disable wrap around */ 899 if (addr + size < addr) 900 return (EINVAL); 901 902 if (atop(size) + vmstats.v_wire_count > vm_page_max_wired) 903 return (EAGAIN); 904 905 #ifdef pmap_wired_count 906 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 907 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 908 return (ENOMEM); 909 #else 910 error = priv_check_cred(p->p_ucred, PRIV_ROOT, 0); 911 if (error) 912 return (error); 913 #endif 914 915 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, FALSE); 916 return (error == KERN_SUCCESS ? 0 : ENOMEM); 917 } 918 919 /* 920 * mlockall_args(int how) 921 */ 922 int 923 sys_mlockall(struct mlockall_args *uap) 924 { 925 return 0; 926 } 927 928 /* 929 * munlockall_args(void) 930 */ 931 int 932 sys_munlockall(struct munlockall_args *uap) 933 { 934 return 0; 935 } 936 937 /* 938 * munlock_args(const void *addr, size_t len) 939 */ 940 int 941 sys_munlock(struct munlock_args *uap) 942 { 943 struct thread *td = curthread; 944 struct proc *p = td->td_proc; 945 vm_offset_t addr; 946 vm_size_t size, pageoff; 947 int error; 948 949 addr = (vm_offset_t) uap->addr; 950 size = uap->len; 951 952 pageoff = (addr & PAGE_MASK); 953 addr -= pageoff; 954 size += pageoff; 955 size = (vm_size_t) round_page(size); 956 957 /* disable wrap around */ 958 if (addr + size < addr) 959 return (EINVAL); 960 961 #ifndef pmap_wired_count 962 error = priv_check(td, PRIV_ROOT); 963 if (error) 964 return (error); 965 #endif 966 967 error = vm_map_unwire(&p->p_vmspace->vm_map, addr, addr + size, TRUE); 968 return (error == KERN_SUCCESS ? 0 : ENOMEM); 969 } 970 971 /* 972 * Internal version of mmap. 973 * Currently used by mmap, exec, and sys5 shared memory. 974 * Handle is either a vnode pointer or NULL for MAP_ANON. 975 */ 976 int 977 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot, 978 vm_prot_t maxprot, int flags, void *handle, vm_ooffset_t foff) 979 { 980 boolean_t fitit; 981 vm_object_t object; 982 vm_offset_t eaddr; 983 vm_size_t esize; 984 struct vnode *vp; 985 struct thread *td = curthread; 986 struct proc *p; 987 objtype_t type; 988 int rv = KERN_SUCCESS; 989 off_t objsize; 990 int docow; 991 992 if (size == 0) 993 return (0); 994 995 objsize = size = round_page(size); 996 997 /* 998 * XXX messy code, fixme 999 * 1000 * NOTE: Overflow checks require discrete statements or GCC4 1001 * will optimize it out. 1002 */ 1003 if ((p = curproc) != NULL && map == &p->p_vmspace->vm_map) { 1004 esize = map->size + size; 1005 if (esize < map->size || 1006 esize > p->p_rlimit[RLIMIT_VMEM].rlim_cur) { 1007 return(ENOMEM); 1008 } 1009 } 1010 1011 /* 1012 * We currently can only deal with page aligned file offsets. 1013 * The check is here rather than in the syscall because the 1014 * kernel calls this function internally for other mmaping 1015 * operations (such as in exec) and non-aligned offsets will 1016 * cause pmap inconsistencies...so we want to be sure to 1017 * disallow this in all cases. 1018 * 1019 * NOTE: Overflow checks require discrete statements or GCC4 1020 * will optimize it out. 1021 */ 1022 if (foff & PAGE_MASK) 1023 return (EINVAL); 1024 1025 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) { 1026 fitit = TRUE; 1027 *addr = round_page(*addr); 1028 } else { 1029 if (*addr != trunc_page(*addr)) 1030 return (EINVAL); 1031 eaddr = *addr + size; 1032 if (eaddr < *addr) 1033 return (EINVAL); 1034 fitit = FALSE; 1035 if ((flags & MAP_TRYFIXED) == 0) 1036 vm_map_remove(map, *addr, *addr + size); 1037 } 1038 1039 /* 1040 * Lookup/allocate object. 1041 */ 1042 if (flags & MAP_ANON) { 1043 type = OBJT_DEFAULT; 1044 /* 1045 * Unnamed anonymous regions always start at 0. 1046 */ 1047 if (handle == NULL) 1048 foff = 0; 1049 vp = NULL; 1050 } else { 1051 vp = (struct vnode *)handle; 1052 if (vp->v_type == VCHR) { 1053 type = OBJT_DEVICE; 1054 handle = (void *)(intptr_t)vp->v_rdev; 1055 } else { 1056 struct vattr vat; 1057 int error; 1058 1059 error = VOP_GETATTR(vp, &vat); 1060 if (error) 1061 return (error); 1062 objsize = vat.va_size; 1063 type = OBJT_VNODE; 1064 /* 1065 * if it is a regular file without any references 1066 * we do not need to sync it. 1067 */ 1068 if (vp->v_type == VREG && vat.va_nlink == 0) { 1069 flags |= MAP_NOSYNC; 1070 } 1071 } 1072 } 1073 1074 if (handle == NULL) { 1075 object = NULL; 1076 docow = 0; 1077 } else { 1078 object = vm_pager_allocate(type, handle, objsize, prot, foff); 1079 if (object == NULL) 1080 return (type == OBJT_DEVICE ? EINVAL : ENOMEM); 1081 docow = MAP_PREFAULT_PARTIAL; 1082 } 1083 1084 /* 1085 * Force device mappings to be shared. 1086 */ 1087 if (type == OBJT_DEVICE || type == OBJT_PHYS) { 1088 flags &= ~(MAP_PRIVATE|MAP_COPY); 1089 flags |= MAP_SHARED; 1090 } 1091 1092 if ((flags & (MAP_ANON|MAP_SHARED)) == 0) 1093 docow |= MAP_COPY_ON_WRITE; 1094 if (flags & MAP_NOSYNC) 1095 docow |= MAP_DISABLE_SYNCER; 1096 if (flags & MAP_NOCORE) 1097 docow |= MAP_DISABLE_COREDUMP; 1098 1099 #if defined(VM_PROT_READ_IS_EXEC) 1100 if (prot & VM_PROT_READ) 1101 prot |= VM_PROT_EXECUTE; 1102 1103 if (maxprot & VM_PROT_READ) 1104 maxprot |= VM_PROT_EXECUTE; 1105 #endif 1106 1107 /* 1108 * This may place the area in its own page directory if (size) is 1109 * large enough, otherwise it typically returns its argument. 1110 */ 1111 if (fitit) { 1112 *addr = pmap_addr_hint(object, *addr, size); 1113 } 1114 1115 /* 1116 * Stack mappings need special attention. 1117 * 1118 * Mappings that use virtual page tables will default to storing 1119 * the page table at offset 0. 1120 */ 1121 if (flags & MAP_STACK) { 1122 rv = vm_map_stack(map, *addr, size, flags, 1123 prot, maxprot, docow); 1124 } else if (flags & MAP_VPAGETABLE) { 1125 rv = vm_map_find(map, object, foff, addr, size, fitit, 1126 VM_MAPTYPE_VPAGETABLE, prot, maxprot, docow); 1127 } else { 1128 rv = vm_map_find(map, object, foff, addr, size, fitit, 1129 VM_MAPTYPE_NORMAL, prot, maxprot, docow); 1130 } 1131 1132 if (rv != KERN_SUCCESS) { 1133 /* 1134 * Lose the object reference. Will destroy the 1135 * object if it's an unnamed anonymous mapping 1136 * or named anonymous without other references. 1137 */ 1138 vm_object_deallocate(object); 1139 goto out; 1140 } 1141 1142 /* 1143 * Shared memory is also shared with children. 1144 */ 1145 if (flags & (MAP_SHARED|MAP_INHERIT)) { 1146 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE); 1147 if (rv != KERN_SUCCESS) { 1148 vm_map_remove(map, *addr, *addr + size); 1149 goto out; 1150 } 1151 } 1152 1153 /* 1154 * Set the access time on the vnode 1155 */ 1156 if (vp != NULL) 1157 vn_mark_atime(vp, td); 1158 out: 1159 switch (rv) { 1160 case KERN_SUCCESS: 1161 return (0); 1162 case KERN_INVALID_ADDRESS: 1163 case KERN_NO_SPACE: 1164 return (ENOMEM); 1165 case KERN_PROTECTION_FAILURE: 1166 return (EACCES); 1167 default: 1168 return (EINVAL); 1169 } 1170 } 1171