1 /* $NetBSD: uvm_mmap.c,v 1.66 2002/09/27 19:13:29 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 * Copyright (c) 1991, 1993 The Regents of the University of California. 6 * Copyright (c) 1988 University of Utah. 7 * 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * the Systems Programming Group of the University of Utah Computer 12 * Science Department. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the Charles D. Cranor, 25 * Washington University, University of California, Berkeley and 26 * its contributors. 27 * 4. Neither the name of the University nor the names of its contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 * 43 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$ 44 * @(#)vm_mmap.c 8.5 (Berkeley) 5/19/94 45 * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp 46 */ 47 48 /* 49 * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap 50 * function. 51 */ 52 53 #include <sys/cdefs.h> 54 __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.66 2002/09/27 19:13:29 mycroft Exp $"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/file.h> 59 #include <sys/filedesc.h> 60 #include <sys/resourcevar.h> 61 #include <sys/mman.h> 62 #include <sys/mount.h> 63 #include <sys/proc.h> 64 #include <sys/malloc.h> 65 #include <sys/vnode.h> 66 #include <sys/conf.h> 67 #include <sys/stat.h> 68 69 #include <miscfs/specfs/specdev.h> 70 71 #include <sys/syscallargs.h> 72 73 #include <uvm/uvm.h> 74 #include <uvm/uvm_device.h> 75 76 77 /* 78 * unimplemented VM system calls: 79 */ 80 81 /* 82 * sys_sbrk: sbrk system call. 83 */ 84 85 /* ARGSUSED */ 86 int 87 sys_sbrk(p, v, retval) 88 struct proc *p; 89 void *v; 90 register_t *retval; 91 { 92 #if 0 93 struct sys_sbrk_args /* { 94 syscallarg(intptr_t) incr; 95 } */ *uap = v; 96 #endif 97 98 return (ENOSYS); 99 } 100 101 /* 102 * sys_sstk: sstk system call. 103 */ 104 105 /* ARGSUSED */ 106 int 107 sys_sstk(p, v, retval) 108 struct proc *p; 109 void *v; 110 register_t *retval; 111 { 112 #if 0 113 struct sys_sstk_args /* { 114 syscallarg(int) incr; 115 } */ *uap = v; 116 #endif 117 118 return (ENOSYS); 119 } 120 121 /* 122 * sys_mincore: determine if pages are in core or not. 123 */ 124 125 /* ARGSUSED */ 126 int 127 sys_mincore(p, v, retval) 128 struct proc *p; 129 void *v; 130 register_t *retval; 131 { 132 struct sys_mincore_args /* { 133 syscallarg(void *) addr; 134 syscallarg(size_t) len; 135 syscallarg(char *) vec; 136 } */ *uap = v; 137 struct vm_page *pg; 138 char *vec, pgi; 139 struct uvm_object *uobj; 140 struct vm_amap *amap; 141 struct vm_anon *anon; 142 struct vm_map_entry *entry; 143 vaddr_t start, end, lim; 144 struct vm_map *map; 145 vsize_t len; 146 int error = 0, npgs; 147 148 map = &p->p_vmspace->vm_map; 149 150 start = (vaddr_t)SCARG(uap, addr); 151 len = SCARG(uap, len); 152 vec = SCARG(uap, vec); 153 154 if (start & PAGE_MASK) 155 return (EINVAL); 156 len = round_page(len); 157 end = start + len; 158 if (end <= start) 159 return (EINVAL); 160 161 /* 162 * Lock down vec, so our returned status isn't outdated by 163 * storing the status byte for a page. 164 */ 165 166 npgs = len >> PAGE_SHIFT; 167 error = uvm_vslock(p, vec, npgs, VM_PROT_WRITE); 168 if (error) { 169 return error; 170 } 171 vm_map_lock_read(map); 172 173 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) { 174 error = ENOMEM; 175 goto out; 176 } 177 178 for (/* nothing */; 179 entry != &map->header && entry->start < end; 180 entry = entry->next) { 181 KASSERT(!UVM_ET_ISSUBMAP(entry)); 182 KASSERT(start >= entry->start); 183 184 /* Make sure there are no holes. */ 185 if (entry->end < end && 186 (entry->next == &map->header || 187 entry->next->start > entry->end)) { 188 error = ENOMEM; 189 goto out; 190 } 191 192 lim = end < entry->end ? end : entry->end; 193 194 /* 195 * Special case for objects with no "real" pages. Those 196 * are always considered resident (mapped devices). 197 */ 198 199 if (UVM_ET_ISOBJ(entry)) { 200 KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)); 201 if (!UVM_OBJ_IS_VNODE(entry->object.uvm_obj)) { 202 for (/* nothing */; start < lim; 203 start += PAGE_SIZE, vec++) 204 subyte(vec, 1); 205 continue; 206 } 207 } 208 209 amap = entry->aref.ar_amap; /* top layer */ 210 uobj = entry->object.uvm_obj; /* bottom layer */ 211 212 if (amap != NULL) 213 amap_lock(amap); 214 if (uobj != NULL) 215 simple_lock(&uobj->vmobjlock); 216 217 for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) { 218 pgi = 0; 219 if (amap != NULL) { 220 /* Check the top layer first. */ 221 anon = amap_lookup(&entry->aref, 222 start - entry->start); 223 /* Don't need to lock anon here. */ 224 if (anon != NULL && anon->u.an_page != NULL) { 225 226 /* 227 * Anon has the page for this entry 228 * offset. 229 */ 230 231 pgi = 1; 232 } 233 } 234 if (uobj != NULL && pgi == 0) { 235 /* Check the bottom layer. */ 236 pg = uvm_pagelookup(uobj, 237 entry->offset + (start - entry->start)); 238 if (pg != NULL) { 239 240 /* 241 * Object has the page for this entry 242 * offset. 243 */ 244 245 pgi = 1; 246 } 247 } 248 (void) subyte(vec, pgi); 249 } 250 if (uobj != NULL) 251 simple_unlock(&uobj->vmobjlock); 252 if (amap != NULL) 253 amap_unlock(amap); 254 } 255 256 out: 257 vm_map_unlock_read(map); 258 uvm_vsunlock(p, SCARG(uap, vec), npgs); 259 return (error); 260 } 261 262 /* 263 * sys_mmap: mmap system call. 264 * 265 * => file offset and address may not be page aligned 266 * - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE 267 * - if address isn't page aligned the mapping starts at trunc_page(addr) 268 * and the return value is adjusted up by the page offset. 269 */ 270 271 int 272 sys_mmap(p, v, retval) 273 struct proc *p; 274 void *v; 275 register_t *retval; 276 { 277 struct sys_mmap_args /* { 278 syscallarg(caddr_t) addr; 279 syscallarg(size_t) len; 280 syscallarg(int) prot; 281 syscallarg(int) flags; 282 syscallarg(int) fd; 283 syscallarg(long) pad; 284 syscallarg(off_t) pos; 285 } */ *uap = v; 286 vaddr_t addr; 287 struct vattr va; 288 off_t pos; 289 vsize_t size, pageoff; 290 vm_prot_t prot, maxprot; 291 int flags, fd; 292 vaddr_t vm_min_address = VM_MIN_ADDRESS; 293 struct filedesc *fdp = p->p_fd; 294 struct file *fp; 295 struct vnode *vp; 296 void *handle; 297 int error; 298 299 /* 300 * first, extract syscall args from the uap. 301 */ 302 303 addr = (vaddr_t)SCARG(uap, addr); 304 size = (vsize_t)SCARG(uap, len); 305 prot = SCARG(uap, prot) & VM_PROT_ALL; 306 flags = SCARG(uap, flags); 307 fd = SCARG(uap, fd); 308 pos = SCARG(uap, pos); 309 310 /* 311 * Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and 312 * validate the flags. 313 */ 314 if (flags & MAP_COPY) 315 flags = (flags & ~MAP_COPY) | MAP_PRIVATE; 316 if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE)) 317 return (EINVAL); 318 319 /* 320 * align file position and save offset. adjust size. 321 */ 322 323 pageoff = (pos & PAGE_MASK); 324 pos -= pageoff; 325 size += pageoff; /* add offset */ 326 size = (vsize_t)round_page(size); /* round up */ 327 if ((ssize_t) size < 0) 328 return (EINVAL); /* don't allow wrap */ 329 330 /* 331 * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr" 332 */ 333 334 if (flags & MAP_FIXED) { 335 336 /* ensure address and file offset are aligned properly */ 337 addr -= pageoff; 338 if (addr & PAGE_MASK) 339 return (EINVAL); 340 341 if (VM_MAXUSER_ADDRESS > 0 && 342 (addr + size) > VM_MAXUSER_ADDRESS) 343 return (EFBIG); 344 if (vm_min_address > 0 && addr < vm_min_address) 345 return (EINVAL); 346 if (addr > addr + size) 347 return (EOVERFLOW); /* no wrapping! */ 348 349 } else { 350 351 /* 352 * not fixed: make sure we skip over the largest possible heap. 353 * we will refine our guess later (e.g. to account for VAC, etc) 354 */ 355 356 addr = MAX(addr, round_page((vaddr_t)p->p_vmspace->vm_daddr + 357 MAXDSIZ)); 358 } 359 360 /* 361 * check for file mappings (i.e. not anonymous) and verify file. 362 */ 363 364 if ((flags & MAP_ANON) == 0) { 365 366 if ((fp = fd_getfile(fdp, fd)) == NULL) 367 return (EBADF); 368 369 if (fp->f_type != DTYPE_VNODE) 370 return (ENODEV); /* only mmap vnodes! */ 371 vp = (struct vnode *)fp->f_data; /* convert to vnode */ 372 373 if (vp->v_type != VREG && vp->v_type != VCHR && 374 vp->v_type != VBLK) 375 return (ENODEV); /* only REG/CHR/BLK support mmap */ 376 377 if (vp->v_type != VCHR && pos < 0) 378 return (EINVAL); 379 380 if (vp->v_type != VCHR && (pos + size) < pos) 381 return (EOVERFLOW); /* no offset wrapping */ 382 383 /* special case: catch SunOS style /dev/zero */ 384 if (vp->v_type == VCHR && vp->v_rdev == zerodev) { 385 flags |= MAP_ANON; 386 goto is_anon; 387 } 388 389 /* 390 * Old programs may not select a specific sharing type, so 391 * default to an appropriate one. 392 * 393 * XXX: how does MAP_ANON fit in the picture? 394 */ 395 if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) { 396 #if defined(DEBUG) 397 printf("WARNING: defaulted mmap() share type to " 398 "%s (pid %d comm %s)\n", vp->v_type == VCHR ? 399 "MAP_SHARED" : "MAP_PRIVATE", p->p_pid, 400 p->p_comm); 401 #endif 402 if (vp->v_type == VCHR) 403 flags |= MAP_SHARED; /* for a device */ 404 else 405 flags |= MAP_PRIVATE; /* for a file */ 406 } 407 408 /* 409 * MAP_PRIVATE device mappings don't make sense (and aren't 410 * supported anyway). However, some programs rely on this, 411 * so just change it to MAP_SHARED. 412 */ 413 if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) { 414 flags = (flags & ~MAP_PRIVATE) | MAP_SHARED; 415 } 416 417 /* 418 * now check protection 419 */ 420 421 maxprot = VM_PROT_EXECUTE; 422 423 /* check read access */ 424 if (fp->f_flag & FREAD) 425 maxprot |= VM_PROT_READ; 426 else if (prot & PROT_READ) 427 return (EACCES); 428 429 /* check write access, shared case first */ 430 if (flags & MAP_SHARED) { 431 /* 432 * if the file is writable, only add PROT_WRITE to 433 * maxprot if the file is not immutable, append-only. 434 * otherwise, if we have asked for PROT_WRITE, return 435 * EPERM. 436 */ 437 if (fp->f_flag & FWRITE) { 438 if ((error = 439 VOP_GETATTR(vp, &va, p->p_ucred, p))) 440 return (error); 441 if ((va.va_flags & (IMMUTABLE|APPEND)) == 0) 442 maxprot |= VM_PROT_WRITE; 443 else if (prot & PROT_WRITE) 444 return (EPERM); 445 } 446 else if (prot & PROT_WRITE) 447 return (EACCES); 448 } else { 449 /* MAP_PRIVATE mappings can always write to */ 450 maxprot |= VM_PROT_WRITE; 451 } 452 handle = vp; 453 454 } else { /* MAP_ANON case */ 455 /* 456 * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0? 457 */ 458 if (fd != -1) 459 return (EINVAL); 460 461 is_anon: /* label for SunOS style /dev/zero */ 462 handle = NULL; 463 maxprot = VM_PROT_ALL; 464 pos = 0; 465 } 466 467 /* 468 * XXX (in)sanity check. We don't do proper datasize checking 469 * XXX for anonymous (or private writable) mmap(). However, 470 * XXX know that if we're trying to allocate more than the amount 471 * XXX remaining under our current data size limit, _that_ should 472 * XXX be disallowed. 473 */ 474 if ((flags & MAP_ANON) != 0 || 475 ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) { 476 if (size > 477 (p->p_rlimit[RLIMIT_DATA].rlim_cur - 478 ctob(p->p_vmspace->vm_dsize))) { 479 return (ENOMEM); 480 } 481 } 482 483 /* 484 * now let kernel internal function uvm_mmap do the work. 485 */ 486 487 error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot, 488 flags, handle, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur); 489 490 if (error == 0) 491 /* remember to add offset */ 492 *retval = (register_t)(addr + pageoff); 493 494 return (error); 495 } 496 497 /* 498 * sys___msync13: the msync system call (a front-end for flush) 499 */ 500 501 int 502 sys___msync13(p, v, retval) 503 struct proc *p; 504 void *v; 505 register_t *retval; 506 { 507 struct sys___msync13_args /* { 508 syscallarg(caddr_t) addr; 509 syscallarg(size_t) len; 510 syscallarg(int) flags; 511 } */ *uap = v; 512 vaddr_t addr; 513 vsize_t size, pageoff; 514 struct vm_map *map; 515 int error, rv, flags, uvmflags; 516 517 /* 518 * extract syscall args from the uap 519 */ 520 521 addr = (vaddr_t)SCARG(uap, addr); 522 size = (vsize_t)SCARG(uap, len); 523 flags = SCARG(uap, flags); 524 525 /* sanity check flags */ 526 if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 || 527 (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 || 528 (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC)) 529 return (EINVAL); 530 if ((flags & (MS_ASYNC | MS_SYNC)) == 0) 531 flags |= MS_SYNC; 532 533 /* 534 * align the address to a page boundary and adjust the size accordingly. 535 */ 536 537 pageoff = (addr & PAGE_MASK); 538 addr -= pageoff; 539 size += pageoff; 540 size = (vsize_t)round_page(size); 541 542 /* disallow wrap-around. */ 543 if (addr + size < addr) 544 return (EINVAL); 545 546 /* 547 * get map 548 */ 549 550 map = &p->p_vmspace->vm_map; 551 552 /* 553 * XXXCDC: do we really need this semantic? 554 * 555 * XXX Gak! If size is zero we are supposed to sync "all modified 556 * pages with the region containing addr". Unfortunately, we 557 * don't really keep track of individual mmaps so we approximate 558 * by flushing the range of the map entry containing addr. 559 * This can be incorrect if the region splits or is coalesced 560 * with a neighbor. 561 */ 562 563 if (size == 0) { 564 struct vm_map_entry *entry; 565 566 vm_map_lock_read(map); 567 rv = uvm_map_lookup_entry(map, addr, &entry); 568 if (rv == TRUE) { 569 addr = entry->start; 570 size = entry->end - entry->start; 571 } 572 vm_map_unlock_read(map); 573 if (rv == FALSE) 574 return (EINVAL); 575 } 576 577 /* 578 * translate MS_ flags into PGO_ flags 579 */ 580 581 uvmflags = PGO_CLEANIT; 582 if (flags & MS_INVALIDATE) 583 uvmflags |= PGO_FREE; 584 if (flags & MS_SYNC) 585 uvmflags |= PGO_SYNCIO; 586 else 587 uvmflags |= PGO_SYNCIO; /* XXXCDC: force sync for now! */ 588 589 error = uvm_map_clean(map, addr, addr+size, uvmflags); 590 return error; 591 } 592 593 /* 594 * sys_munmap: unmap a users memory 595 */ 596 597 int 598 sys_munmap(p, v, retval) 599 struct proc *p; 600 void *v; 601 register_t *retval; 602 { 603 struct sys_munmap_args /* { 604 syscallarg(caddr_t) addr; 605 syscallarg(size_t) len; 606 } */ *uap = v; 607 vaddr_t addr; 608 vsize_t size, pageoff; 609 struct vm_map *map; 610 vaddr_t vm_min_address = VM_MIN_ADDRESS; 611 struct vm_map_entry *dead_entries; 612 613 /* 614 * get syscall args. 615 */ 616 617 addr = (vaddr_t)SCARG(uap, addr); 618 size = (vsize_t)SCARG(uap, len); 619 620 /* 621 * align the address to a page boundary and adjust the size accordingly. 622 */ 623 624 pageoff = (addr & PAGE_MASK); 625 addr -= pageoff; 626 size += pageoff; 627 size = (vsize_t)round_page(size); 628 629 if ((int)size < 0) 630 return (EINVAL); 631 if (size == 0) 632 return (0); 633 634 /* 635 * Check for illegal addresses. Watch out for address wrap... 636 * Note that VM_*_ADDRESS are not constants due to casts (argh). 637 */ 638 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 639 return (EINVAL); 640 if (vm_min_address > 0 && addr < vm_min_address) 641 return (EINVAL); 642 if (addr > addr + size) 643 return (EINVAL); 644 map = &p->p_vmspace->vm_map; 645 646 /* 647 * interesting system call semantic: make sure entire range is 648 * allocated before allowing an unmap. 649 */ 650 651 vm_map_lock(map); 652 #if 0 653 if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) { 654 vm_map_unlock(map); 655 return (EINVAL); 656 } 657 #endif 658 uvm_unmap_remove(map, addr, addr + size, &dead_entries); 659 vm_map_unlock(map); 660 if (dead_entries != NULL) 661 uvm_unmap_detach(dead_entries, 0); 662 return (0); 663 } 664 665 /* 666 * sys_mprotect: the mprotect system call 667 */ 668 669 int 670 sys_mprotect(p, v, retval) 671 struct proc *p; 672 void *v; 673 register_t *retval; 674 { 675 struct sys_mprotect_args /* { 676 syscallarg(caddr_t) addr; 677 syscallarg(int) len; 678 syscallarg(int) prot; 679 } */ *uap = v; 680 vaddr_t addr; 681 vsize_t size, pageoff; 682 vm_prot_t prot; 683 int error; 684 685 /* 686 * extract syscall args from uap 687 */ 688 689 addr = (vaddr_t)SCARG(uap, addr); 690 size = (vsize_t)SCARG(uap, len); 691 prot = SCARG(uap, prot) & VM_PROT_ALL; 692 693 /* 694 * align the address to a page boundary and adjust the size accordingly. 695 */ 696 697 pageoff = (addr & PAGE_MASK); 698 addr -= pageoff; 699 size += pageoff; 700 size = (vsize_t)round_page(size); 701 702 if ((int)size < 0) 703 return (EINVAL); 704 error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 705 FALSE); 706 return error; 707 } 708 709 /* 710 * sys_minherit: the minherit system call 711 */ 712 713 int 714 sys_minherit(p, v, retval) 715 struct proc *p; 716 void *v; 717 register_t *retval; 718 { 719 struct sys_minherit_args /* { 720 syscallarg(caddr_t) addr; 721 syscallarg(int) len; 722 syscallarg(int) inherit; 723 } */ *uap = v; 724 vaddr_t addr; 725 vsize_t size, pageoff; 726 vm_inherit_t inherit; 727 int error; 728 729 addr = (vaddr_t)SCARG(uap, addr); 730 size = (vsize_t)SCARG(uap, len); 731 inherit = SCARG(uap, inherit); 732 733 /* 734 * align the address to a page boundary and adjust the size accordingly. 735 */ 736 737 pageoff = (addr & PAGE_MASK); 738 addr -= pageoff; 739 size += pageoff; 740 size = (vsize_t)round_page(size); 741 742 if ((int)size < 0) 743 return (EINVAL); 744 error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size, 745 inherit); 746 return error; 747 } 748 749 /* 750 * sys_madvise: give advice about memory usage. 751 */ 752 753 /* ARGSUSED */ 754 int 755 sys_madvise(p, v, retval) 756 struct proc *p; 757 void *v; 758 register_t *retval; 759 { 760 struct sys_madvise_args /* { 761 syscallarg(caddr_t) addr; 762 syscallarg(size_t) len; 763 syscallarg(int) behav; 764 } */ *uap = v; 765 vaddr_t addr; 766 vsize_t size, pageoff; 767 int advice, error; 768 769 addr = (vaddr_t)SCARG(uap, addr); 770 size = (vsize_t)SCARG(uap, len); 771 advice = SCARG(uap, behav); 772 773 /* 774 * align the address to a page boundary, and adjust the size accordingly 775 */ 776 777 pageoff = (addr & PAGE_MASK); 778 addr -= pageoff; 779 size += pageoff; 780 size = (vsize_t)round_page(size); 781 782 if ((ssize_t)size <= 0) 783 return (EINVAL); 784 785 switch (advice) { 786 case MADV_NORMAL: 787 case MADV_RANDOM: 788 case MADV_SEQUENTIAL: 789 error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size, 790 advice); 791 break; 792 793 case MADV_WILLNEED: 794 795 /* 796 * Activate all these pages, pre-faulting them in if 797 * necessary. 798 */ 799 /* 800 * XXX IMPLEMENT ME. 801 * Should invent a "weak" mode for uvm_fault() 802 * which would only do the PGO_LOCKED pgo_get(). 803 */ 804 805 return (0); 806 807 case MADV_DONTNEED: 808 809 /* 810 * Deactivate all these pages. We don't need them 811 * any more. We don't, however, toss the data in 812 * the pages. 813 */ 814 815 error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size, 816 PGO_DEACTIVATE); 817 break; 818 819 case MADV_FREE: 820 821 /* 822 * These pages contain no valid data, and may be 823 * garbage-collected. Toss all resources, including 824 * any swap space in use. 825 */ 826 827 error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size, 828 PGO_FREE); 829 break; 830 831 case MADV_SPACEAVAIL: 832 833 /* 834 * XXXMRG What is this? I think it's: 835 * 836 * Ensure that we have allocated backing-store 837 * for these pages. 838 * 839 * This is going to require changes to the page daemon, 840 * as it will free swap space allocated to pages in core. 841 * There's also what to do for device/file/anonymous memory. 842 */ 843 844 return (EINVAL); 845 846 default: 847 return (EINVAL); 848 } 849 850 return error; 851 } 852 853 /* 854 * sys_mlock: memory lock 855 */ 856 857 int 858 sys_mlock(p, v, retval) 859 struct proc *p; 860 void *v; 861 register_t *retval; 862 { 863 struct sys_mlock_args /* { 864 syscallarg(const void *) addr; 865 syscallarg(size_t) len; 866 } */ *uap = v; 867 vaddr_t addr; 868 vsize_t size, pageoff; 869 int error; 870 871 /* 872 * extract syscall args from uap 873 */ 874 875 addr = (vaddr_t)SCARG(uap, addr); 876 size = (vsize_t)SCARG(uap, len); 877 878 /* 879 * align the address to a page boundary and adjust the size accordingly 880 */ 881 882 pageoff = (addr & PAGE_MASK); 883 addr -= pageoff; 884 size += pageoff; 885 size = (vsize_t)round_page(size); 886 887 /* disallow wrap-around. */ 888 if (addr + size < addr) 889 return (EINVAL); 890 891 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) 892 return (EAGAIN); 893 894 #ifdef pmap_wired_count 895 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 896 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 897 return (EAGAIN); 898 #else 899 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 900 return (error); 901 #endif 902 903 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE, 904 0); 905 return error; 906 } 907 908 /* 909 * sys_munlock: unlock wired pages 910 */ 911 912 int 913 sys_munlock(p, v, retval) 914 struct proc *p; 915 void *v; 916 register_t *retval; 917 { 918 struct sys_munlock_args /* { 919 syscallarg(const void *) addr; 920 syscallarg(size_t) len; 921 } */ *uap = v; 922 vaddr_t addr; 923 vsize_t size, pageoff; 924 int error; 925 926 /* 927 * extract syscall args from uap 928 */ 929 930 addr = (vaddr_t)SCARG(uap, addr); 931 size = (vsize_t)SCARG(uap, len); 932 933 /* 934 * align the address to a page boundary, and adjust the size accordingly 935 */ 936 937 pageoff = (addr & PAGE_MASK); 938 addr -= pageoff; 939 size += pageoff; 940 size = (vsize_t)round_page(size); 941 942 /* disallow wrap-around. */ 943 if (addr + size < addr) 944 return (EINVAL); 945 946 #ifndef pmap_wired_count 947 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 948 return (error); 949 #endif 950 951 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE, 952 0); 953 return error; 954 } 955 956 /* 957 * sys_mlockall: lock all pages mapped into an address space. 958 */ 959 960 int 961 sys_mlockall(p, v, retval) 962 struct proc *p; 963 void *v; 964 register_t *retval; 965 { 966 struct sys_mlockall_args /* { 967 syscallarg(int) flags; 968 } */ *uap = v; 969 int error, flags; 970 971 flags = SCARG(uap, flags); 972 973 if (flags == 0 || 974 (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0) 975 return (EINVAL); 976 977 #ifndef pmap_wired_count 978 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 979 return (error); 980 #endif 981 982 error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags, 983 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur); 984 return (error); 985 } 986 987 /* 988 * sys_munlockall: unlock all pages mapped into an address space. 989 */ 990 991 int 992 sys_munlockall(p, v, retval) 993 struct proc *p; 994 void *v; 995 register_t *retval; 996 { 997 998 (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0); 999 return (0); 1000 } 1001 1002 /* 1003 * uvm_mmap: internal version of mmap 1004 * 1005 * - used by sys_mmap and various framebuffers 1006 * - handle is a vnode pointer or NULL for MAP_ANON 1007 * - caller must page-align the file offset 1008 */ 1009 1010 int 1011 uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit) 1012 struct vm_map *map; 1013 vaddr_t *addr; 1014 vsize_t size; 1015 vm_prot_t prot, maxprot; 1016 int flags; 1017 void *handle; 1018 voff_t foff; 1019 vsize_t locklimit; 1020 { 1021 struct uvm_object *uobj; 1022 struct vnode *vp; 1023 int error; 1024 int advice = UVM_ADV_NORMAL; 1025 uvm_flag_t uvmflag = 0; 1026 1027 /* 1028 * check params 1029 */ 1030 1031 if (size == 0) 1032 return(0); 1033 if (foff & PAGE_MASK) 1034 return(EINVAL); 1035 if ((prot & maxprot) != prot) 1036 return(EINVAL); 1037 1038 /* 1039 * for non-fixed mappings, round off the suggested address. 1040 * for fixed mappings, check alignment and zap old mappings. 1041 */ 1042 1043 if ((flags & MAP_FIXED) == 0) { 1044 *addr = round_page(*addr); 1045 } else { 1046 if (*addr & PAGE_MASK) 1047 return(EINVAL); 1048 uvmflag |= UVM_FLAG_FIXED; 1049 (void) uvm_unmap(map, *addr, *addr + size); 1050 } 1051 1052 /* 1053 * handle anon vs. non-anon mappings. for non-anon mappings attach 1054 * to underlying vm object. 1055 */ 1056 1057 if (flags & MAP_ANON) { 1058 foff = UVM_UNKNOWN_OFFSET; 1059 uobj = NULL; 1060 if ((flags & MAP_SHARED) == 0) 1061 /* XXX: defer amap create */ 1062 uvmflag |= UVM_FLAG_COPYONW; 1063 else 1064 /* shared: create amap now */ 1065 uvmflag |= UVM_FLAG_OVERLAY; 1066 1067 } else { 1068 vp = (struct vnode *)handle; 1069 1070 /* 1071 * Don't allow mmap for EXEC if the file system 1072 * is mounted NOEXEC. 1073 */ 1074 if ((prot & PROT_EXEC) != 0 && 1075 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) 1076 return (EACCES); 1077 1078 if (vp->v_type != VCHR) { 1079 error = VOP_MMAP(vp, 0, curproc->p_ucred, curproc); 1080 if (error) { 1081 return error; 1082 } 1083 1084 uobj = uvn_attach((void *)vp, (flags & MAP_SHARED) ? 1085 maxprot : (maxprot & ~VM_PROT_WRITE)); 1086 1087 /* XXX for now, attach doesn't gain a ref */ 1088 VREF(vp); 1089 1090 /* 1091 * If the vnode is being mapped with PROT_EXEC, 1092 * then mark it as text. 1093 */ 1094 if (prot & PROT_EXEC) 1095 vn_markexec(vp); 1096 } else { 1097 uobj = udv_attach((void *) &vp->v_rdev, 1098 (flags & MAP_SHARED) ? maxprot : 1099 (maxprot & ~VM_PROT_WRITE), foff, size); 1100 /* 1101 * XXX Some devices don't like to be mapped with 1102 * XXX PROT_EXEC, but we don't really have a 1103 * XXX better way of handling this, right now 1104 */ 1105 if (uobj == NULL && (prot & PROT_EXEC) == 0) { 1106 maxprot &= ~VM_PROT_EXECUTE; 1107 uobj = udv_attach((void *)&vp->v_rdev, 1108 (flags & MAP_SHARED) ? maxprot : 1109 (maxprot & ~VM_PROT_WRITE), foff, size); 1110 } 1111 advice = UVM_ADV_RANDOM; 1112 } 1113 if (uobj == NULL) 1114 return((vp->v_type == VREG) ? ENOMEM : EINVAL); 1115 if ((flags & MAP_SHARED) == 0) 1116 uvmflag |= UVM_FLAG_COPYONW; 1117 } 1118 1119 uvmflag = UVM_MAPFLAG(prot, maxprot, 1120 (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY, 1121 advice, uvmflag); 1122 error = uvm_map(map, addr, size, uobj, foff, 0, uvmflag); 1123 if (error) { 1124 if (uobj) 1125 uobj->pgops->pgo_detach(uobj); 1126 return error; 1127 } 1128 1129 /* 1130 * POSIX 1003.1b -- if our address space was configured 1131 * to lock all future mappings, wire the one we just made. 1132 */ 1133 1134 if (prot == VM_PROT_NONE) { 1135 1136 /* 1137 * No more work to do in this case. 1138 */ 1139 1140 return (0); 1141 } 1142 vm_map_lock(map); 1143 if (map->flags & VM_MAP_WIREFUTURE) { 1144 if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax 1145 #ifdef pmap_wired_count 1146 || (locklimit != 0 && (size + 1147 ptoa(pmap_wired_count(vm_map_pmap(map)))) > 1148 locklimit) 1149 #endif 1150 ) { 1151 vm_map_unlock(map); 1152 uvm_unmap(map, *addr, *addr + size); 1153 return ENOMEM; 1154 } 1155 1156 /* 1157 * uvm_map_pageable() always returns the map unlocked. 1158 */ 1159 1160 error = uvm_map_pageable(map, *addr, *addr + size, 1161 FALSE, UVM_LK_ENTER); 1162 if (error) { 1163 uvm_unmap(map, *addr, *addr + size); 1164 return error; 1165 } 1166 return (0); 1167 } 1168 vm_map_unlock(map); 1169 return 0; 1170 } 1171