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