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