1 /* $NetBSD: uvm_mmap.c,v 1.63 2002/03/22 11:06:33 darrenr 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.63 2002/03/22 11:06:33 darrenr 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 offest 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 && iszerodev(vp->v_rdev)) { 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 (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) { 653 vm_map_unlock(map); 654 return (EINVAL); 655 } 656 uvm_unmap_remove(map, addr, addr + size, &dead_entries); 657 vm_map_unlock(map); 658 if (dead_entries != NULL) 659 uvm_unmap_detach(dead_entries, 0); 660 return (0); 661 } 662 663 /* 664 * sys_mprotect: the mprotect system call 665 */ 666 667 int 668 sys_mprotect(p, v, retval) 669 struct proc *p; 670 void *v; 671 register_t *retval; 672 { 673 struct sys_mprotect_args /* { 674 syscallarg(caddr_t) addr; 675 syscallarg(int) len; 676 syscallarg(int) prot; 677 } */ *uap = v; 678 vaddr_t addr; 679 vsize_t size, pageoff; 680 vm_prot_t prot; 681 int error; 682 683 /* 684 * extract syscall args from uap 685 */ 686 687 addr = (vaddr_t)SCARG(uap, addr); 688 size = (vsize_t)SCARG(uap, len); 689 prot = SCARG(uap, prot) & VM_PROT_ALL; 690 691 /* 692 * align the address to a page boundary and adjust the size accordingly. 693 */ 694 695 pageoff = (addr & PAGE_MASK); 696 addr -= pageoff; 697 size += pageoff; 698 size = (vsize_t)round_page(size); 699 700 if ((int)size < 0) 701 return (EINVAL); 702 error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot, 703 FALSE); 704 return error; 705 } 706 707 /* 708 * sys_minherit: the minherit system call 709 */ 710 711 int 712 sys_minherit(p, v, retval) 713 struct proc *p; 714 void *v; 715 register_t *retval; 716 { 717 struct sys_minherit_args /* { 718 syscallarg(caddr_t) addr; 719 syscallarg(int) len; 720 syscallarg(int) inherit; 721 } */ *uap = v; 722 vaddr_t addr; 723 vsize_t size, pageoff; 724 vm_inherit_t inherit; 725 int error; 726 727 addr = (vaddr_t)SCARG(uap, addr); 728 size = (vsize_t)SCARG(uap, len); 729 inherit = SCARG(uap, inherit); 730 731 /* 732 * align the address to a page boundary and adjust the size accordingly. 733 */ 734 735 pageoff = (addr & PAGE_MASK); 736 addr -= pageoff; 737 size += pageoff; 738 size = (vsize_t)round_page(size); 739 740 if ((int)size < 0) 741 return (EINVAL); 742 error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size, 743 inherit); 744 return error; 745 } 746 747 /* 748 * sys_madvise: give advice about memory usage. 749 */ 750 751 /* ARGSUSED */ 752 int 753 sys_madvise(p, v, retval) 754 struct proc *p; 755 void *v; 756 register_t *retval; 757 { 758 struct sys_madvise_args /* { 759 syscallarg(caddr_t) addr; 760 syscallarg(size_t) len; 761 syscallarg(int) behav; 762 } */ *uap = v; 763 vaddr_t addr; 764 vsize_t size, pageoff; 765 int advice, error; 766 767 addr = (vaddr_t)SCARG(uap, addr); 768 size = (vsize_t)SCARG(uap, len); 769 advice = SCARG(uap, behav); 770 771 /* 772 * align the address to a page boundary, and adjust the size accordingly 773 */ 774 775 pageoff = (addr & PAGE_MASK); 776 addr -= pageoff; 777 size += pageoff; 778 size = (vsize_t)round_page(size); 779 780 if ((ssize_t)size <= 0) 781 return (EINVAL); 782 783 switch (advice) { 784 case MADV_NORMAL: 785 case MADV_RANDOM: 786 case MADV_SEQUENTIAL: 787 error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size, 788 advice); 789 break; 790 791 case MADV_WILLNEED: 792 793 /* 794 * Activate all these pages, pre-faulting them in if 795 * necessary. 796 */ 797 /* 798 * XXX IMPLEMENT ME. 799 * Should invent a "weak" mode for uvm_fault() 800 * which would only do the PGO_LOCKED pgo_get(). 801 */ 802 803 return (0); 804 805 case MADV_DONTNEED: 806 807 /* 808 * Deactivate all these pages. We don't need them 809 * any more. We don't, however, toss the data in 810 * the pages. 811 */ 812 813 error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size, 814 PGO_DEACTIVATE); 815 break; 816 817 case MADV_FREE: 818 819 /* 820 * These pages contain no valid data, and may be 821 * garbage-collected. Toss all resources, including 822 * any swap space in use. 823 */ 824 825 error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size, 826 PGO_FREE); 827 break; 828 829 case MADV_SPACEAVAIL: 830 831 /* 832 * XXXMRG What is this? I think it's: 833 * 834 * Ensure that we have allocated backing-store 835 * for these pages. 836 * 837 * This is going to require changes to the page daemon, 838 * as it will free swap space allocated to pages in core. 839 * There's also what to do for device/file/anonymous memory. 840 */ 841 842 return (EINVAL); 843 844 default: 845 return (EINVAL); 846 } 847 848 return error; 849 } 850 851 /* 852 * sys_mlock: memory lock 853 */ 854 855 int 856 sys_mlock(p, v, retval) 857 struct proc *p; 858 void *v; 859 register_t *retval; 860 { 861 struct sys_mlock_args /* { 862 syscallarg(const void *) addr; 863 syscallarg(size_t) len; 864 } */ *uap = v; 865 vaddr_t addr; 866 vsize_t size, pageoff; 867 int error; 868 869 /* 870 * extract syscall args from uap 871 */ 872 873 addr = (vaddr_t)SCARG(uap, addr); 874 size = (vsize_t)SCARG(uap, len); 875 876 /* 877 * align the address to a page boundary and adjust the size accordingly 878 */ 879 880 pageoff = (addr & PAGE_MASK); 881 addr -= pageoff; 882 size += pageoff; 883 size = (vsize_t)round_page(size); 884 885 /* disallow wrap-around. */ 886 if (addr + size < addr) 887 return (EINVAL); 888 889 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) 890 return (EAGAIN); 891 892 #ifdef pmap_wired_count 893 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 894 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 895 return (EAGAIN); 896 #else 897 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 898 return (error); 899 #endif 900 901 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE, 902 0); 903 return error; 904 } 905 906 /* 907 * sys_munlock: unlock wired pages 908 */ 909 910 int 911 sys_munlock(p, v, retval) 912 struct proc *p; 913 void *v; 914 register_t *retval; 915 { 916 struct sys_munlock_args /* { 917 syscallarg(const void *) addr; 918 syscallarg(size_t) len; 919 } */ *uap = v; 920 vaddr_t addr; 921 vsize_t size, pageoff; 922 int error; 923 924 /* 925 * extract syscall args from uap 926 */ 927 928 addr = (vaddr_t)SCARG(uap, addr); 929 size = (vsize_t)SCARG(uap, len); 930 931 /* 932 * align the address to a page boundary, and adjust the size accordingly 933 */ 934 935 pageoff = (addr & PAGE_MASK); 936 addr -= pageoff; 937 size += pageoff; 938 size = (vsize_t)round_page(size); 939 940 /* disallow wrap-around. */ 941 if (addr + size < addr) 942 return (EINVAL); 943 944 #ifndef pmap_wired_count 945 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 946 return (error); 947 #endif 948 949 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE, 950 0); 951 return error; 952 } 953 954 /* 955 * sys_mlockall: lock all pages mapped into an address space. 956 */ 957 958 int 959 sys_mlockall(p, v, retval) 960 struct proc *p; 961 void *v; 962 register_t *retval; 963 { 964 struct sys_mlockall_args /* { 965 syscallarg(int) flags; 966 } */ *uap = v; 967 int error, flags; 968 969 flags = SCARG(uap, flags); 970 971 if (flags == 0 || 972 (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0) 973 return (EINVAL); 974 975 #ifndef pmap_wired_count 976 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 977 return (error); 978 #endif 979 980 error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags, 981 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur); 982 return (error); 983 } 984 985 /* 986 * sys_munlockall: unlock all pages mapped into an address space. 987 */ 988 989 int 990 sys_munlockall(p, v, retval) 991 struct proc *p; 992 void *v; 993 register_t *retval; 994 { 995 996 (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0); 997 return (0); 998 } 999 1000 /* 1001 * uvm_mmap: internal version of mmap 1002 * 1003 * - used by sys_mmap and various framebuffers 1004 * - handle is a vnode pointer or NULL for MAP_ANON 1005 * - caller must page-align the file offset 1006 */ 1007 1008 int 1009 uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit) 1010 struct vm_map *map; 1011 vaddr_t *addr; 1012 vsize_t size; 1013 vm_prot_t prot, maxprot; 1014 int flags; 1015 void *handle; 1016 voff_t foff; 1017 vsize_t locklimit; 1018 { 1019 struct uvm_object *uobj; 1020 struct vnode *vp; 1021 int error; 1022 int advice = UVM_ADV_NORMAL; 1023 uvm_flag_t uvmflag = 0; 1024 1025 /* 1026 * check params 1027 */ 1028 1029 if (size == 0) 1030 return(0); 1031 if (foff & PAGE_MASK) 1032 return(EINVAL); 1033 if ((prot & maxprot) != prot) 1034 return(EINVAL); 1035 1036 /* 1037 * for non-fixed mappings, round off the suggested address. 1038 * for fixed mappings, check alignment and zap old mappings. 1039 */ 1040 1041 if ((flags & MAP_FIXED) == 0) { 1042 *addr = round_page(*addr); 1043 } else { 1044 if (*addr & PAGE_MASK) 1045 return(EINVAL); 1046 uvmflag |= UVM_FLAG_FIXED; 1047 (void) uvm_unmap(map, *addr, *addr + size); 1048 } 1049 1050 /* 1051 * handle anon vs. non-anon mappings. for non-anon mappings attach 1052 * to underlying vm object. 1053 */ 1054 1055 if (flags & MAP_ANON) { 1056 foff = UVM_UNKNOWN_OFFSET; 1057 uobj = NULL; 1058 if ((flags & MAP_SHARED) == 0) 1059 /* XXX: defer amap create */ 1060 uvmflag |= UVM_FLAG_COPYONW; 1061 else 1062 /* shared: create amap now */ 1063 uvmflag |= UVM_FLAG_OVERLAY; 1064 1065 } else { 1066 vp = (struct vnode *)handle; 1067 1068 /* 1069 * Don't allow mmap for EXEC if the file system 1070 * is mounted NOEXEC. 1071 */ 1072 if ((prot & PROT_EXEC) != 0 && 1073 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) 1074 return (EACCES); 1075 1076 if (vp->v_type != VCHR) { 1077 error = VOP_MMAP(vp, 0, curproc->p_ucred, curproc); 1078 if (error) { 1079 return error; 1080 } 1081 1082 uobj = uvn_attach((void *)vp, (flags & MAP_SHARED) ? 1083 maxprot : (maxprot & ~VM_PROT_WRITE)); 1084 1085 /* XXX for now, attach doesn't gain a ref */ 1086 VREF(vp); 1087 1088 /* 1089 * If the vnode is being mapped with PROT_EXEC, 1090 * then mark it as text. 1091 */ 1092 if (prot & PROT_EXEC) 1093 vn_markexec(vp); 1094 } else { 1095 uobj = udv_attach((void *) &vp->v_rdev, 1096 (flags & MAP_SHARED) ? maxprot : 1097 (maxprot & ~VM_PROT_WRITE), foff, size); 1098 /* 1099 * XXX Some devices don't like to be mapped with 1100 * XXX PROT_EXEC, but we don't really have a 1101 * XXX better way of handling this, right now 1102 */ 1103 if (uobj == NULL && (prot & PROT_EXEC) == 0) { 1104 maxprot &= ~VM_PROT_EXECUTE; 1105 uobj = udv_attach((void *)&vp->v_rdev, 1106 (flags & MAP_SHARED) ? maxprot : 1107 (maxprot & ~VM_PROT_WRITE), foff, size); 1108 } 1109 advice = UVM_ADV_RANDOM; 1110 } 1111 if (uobj == NULL) 1112 return((vp->v_type == VREG) ? ENOMEM : EINVAL); 1113 if ((flags & MAP_SHARED) == 0) 1114 uvmflag |= UVM_FLAG_COPYONW; 1115 } 1116 1117 uvmflag = UVM_MAPFLAG(prot, maxprot, 1118 (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY, 1119 advice, uvmflag); 1120 error = uvm_map(map, addr, size, uobj, foff, 0, uvmflag); 1121 if (error) { 1122 if (uobj) 1123 uobj->pgops->pgo_detach(uobj); 1124 return error; 1125 } 1126 1127 /* 1128 * POSIX 1003.1b -- if our address space was configured 1129 * to lock all future mappings, wire the one we just made. 1130 */ 1131 1132 if (prot == VM_PROT_NONE) { 1133 1134 /* 1135 * No more work to do in this case. 1136 */ 1137 1138 return (0); 1139 } 1140 vm_map_lock(map); 1141 if (map->flags & VM_MAP_WIREFUTURE) { 1142 if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax 1143 #ifdef pmap_wired_count 1144 || (locklimit != 0 && (size + 1145 ptoa(pmap_wired_count(vm_map_pmap(map)))) > 1146 locklimit) 1147 #endif 1148 ) { 1149 vm_map_unlock(map); 1150 uvm_unmap(map, *addr, *addr + size); 1151 return ENOMEM; 1152 } 1153 1154 /* 1155 * uvm_map_pageable() always returns the map unlocked. 1156 */ 1157 1158 error = uvm_map_pageable(map, *addr, *addr + size, 1159 FALSE, UVM_LK_ENTER); 1160 if (error) { 1161 uvm_unmap(map, *addr, *addr + size); 1162 return error; 1163 } 1164 return (0); 1165 } 1166 vm_map_unlock(map); 1167 return 0; 1168 } 1169