1 /* $OpenBSD: uvm_mmap.c,v 1.137 2016/07/13 17:52:37 kettenis 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/signalvar.h> 65 #include <sys/syslog.h> 66 #include <sys/stat.h> 67 #include <sys/specdev.h> 68 #include <sys/stdint.h> 69 #include <sys/pledge.h> 70 #include <sys/unistd.h> /* for KBIND* */ 71 #include <sys/user.h> 72 73 #include <machine/exec.h> /* for __LDPGSZ */ 74 75 #include <sys/syscallargs.h> 76 77 #include <uvm/uvm.h> 78 #include <uvm/uvm_device.h> 79 #include <uvm/uvm_vnode.h> 80 81 int uvm_mmapanon(vm_map_t, vaddr_t *, vsize_t, vm_prot_t, vm_prot_t, int, 82 vsize_t, struct proc *); 83 int uvm_mmapfile(vm_map_t, vaddr_t *, vsize_t, vm_prot_t, vm_prot_t, int, 84 struct vnode *, voff_t, vsize_t, struct proc *); 85 86 87 /* 88 * Page align addr and size, returning EINVAL on wraparound. 89 */ 90 #define ALIGN_ADDR(addr, size, pageoff) do { \ 91 pageoff = (addr & PAGE_MASK); \ 92 if (pageoff != 0) { \ 93 if (size > SIZE_MAX - pageoff) \ 94 return (EINVAL); /* wraparound */ \ 95 addr -= pageoff; \ 96 size += pageoff; \ 97 } \ 98 if (size != 0) { \ 99 size = (vsize_t)round_page(size); \ 100 if (size == 0) \ 101 return (EINVAL); /* wraparound */ \ 102 } \ 103 } while (0) 104 105 /* 106 * sys_mquery: provide mapping hints to applications that do fixed mappings 107 * 108 * flags: 0 or MAP_FIXED (MAP_FIXED - means that we insist on this addr and 109 * don't care about PMAP_PREFER or such) 110 * addr: hint where we'd like to place the mapping. 111 * size: size of the mapping 112 * fd: fd of the file we want to map 113 * off: offset within the file 114 */ 115 int 116 sys_mquery(struct proc *p, void *v, register_t *retval) 117 { 118 struct sys_mquery_args /* { 119 syscallarg(void *) addr; 120 syscallarg(size_t) len; 121 syscallarg(int) prot; 122 syscallarg(int) flags; 123 syscallarg(int) fd; 124 syscallarg(long) pad; 125 syscallarg(off_t) pos; 126 } */ *uap = v; 127 struct file *fp; 128 voff_t uoff; 129 int error; 130 vaddr_t vaddr; 131 int flags = 0; 132 vsize_t size; 133 vm_prot_t prot; 134 int fd; 135 136 vaddr = (vaddr_t) SCARG(uap, addr); 137 prot = SCARG(uap, prot); 138 size = (vsize_t) SCARG(uap, len); 139 fd = SCARG(uap, fd); 140 141 if ((prot & PROT_MASK) != prot) 142 return (EINVAL); 143 144 if (SCARG(uap, flags) & MAP_FIXED) 145 flags |= UVM_FLAG_FIXED; 146 147 if (fd >= 0) { 148 if ((error = getvnode(p, fd, &fp)) != 0) 149 return (error); 150 uoff = SCARG(uap, pos); 151 } else { 152 fp = NULL; 153 uoff = UVM_UNKNOWN_OFFSET; 154 } 155 156 if (vaddr == 0) 157 vaddr = uvm_map_hint(p->p_vmspace, prot, VM_MIN_ADDRESS, 158 VM_MAXUSER_ADDRESS); 159 160 error = uvm_map_mquery(&p->p_vmspace->vm_map, &vaddr, size, uoff, 161 flags); 162 if (error == 0) 163 *retval = (register_t)(vaddr); 164 165 if (fp != NULL) 166 FRELE(fp, p); 167 return (error); 168 } 169 170 /* 171 * sys_mincore: determine if pages are in core or not. 172 */ 173 /* ARGSUSED */ 174 int 175 sys_mincore(struct proc *p, void *v, register_t *retval) 176 { 177 struct sys_mincore_args /* { 178 syscallarg(void *) addr; 179 syscallarg(size_t) len; 180 syscallarg(char *) vec; 181 } */ *uap = v; 182 vm_page_t m; 183 char *vec, *pgi, *pgs; 184 struct uvm_object *uobj; 185 struct vm_amap *amap; 186 struct vm_anon *anon; 187 vm_map_entry_t entry, next; 188 vaddr_t start, end, lim; 189 vm_map_t map; 190 vsize_t len, npgs; 191 int error = 0; 192 193 map = &p->p_vmspace->vm_map; 194 195 start = (vaddr_t)SCARG(uap, addr); 196 len = SCARG(uap, len); 197 vec = SCARG(uap, vec); 198 199 if (start & PAGE_MASK) 200 return (EINVAL); 201 len = round_page(len); 202 end = start + len; 203 if (end <= start) 204 return (EINVAL); 205 206 npgs = len >> PAGE_SHIFT; 207 208 /* 209 * < art> Anyone trying to mincore more than 4GB of address space is 210 * clearly insane. 211 */ 212 if (npgs >= (0xffffffff >> PAGE_SHIFT)) 213 return (E2BIG); 214 pgs = mallocarray(npgs, sizeof(*pgs), M_TEMP, M_WAITOK | M_CANFAIL); 215 if (pgs == NULL) 216 return (ENOMEM); 217 pgi = pgs; 218 219 /* 220 * Lock down vec, so our returned status isn't outdated by 221 * storing the status byte for a page. 222 */ 223 if ((error = uvm_vslock(p, vec, npgs, PROT_WRITE)) != 0) { 224 free(pgs, M_TEMP, npgs * sizeof(*pgs)); 225 return (error); 226 } 227 228 vm_map_lock_read(map); 229 230 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) { 231 error = ENOMEM; 232 goto out; 233 } 234 235 for (/* nothing */; 236 entry != NULL && entry->start < end; 237 entry = RB_NEXT(uvm_map_addr, &map->addr, entry)) { 238 KASSERT(!UVM_ET_ISSUBMAP(entry)); 239 KASSERT(start >= entry->start); 240 241 /* Make sure there are no holes. */ 242 next = RB_NEXT(uvm_map_addr, &map->addr, entry); 243 if (entry->end < end && 244 (next == NULL || 245 next->start > entry->end)) { 246 error = ENOMEM; 247 goto out; 248 } 249 250 lim = end < entry->end ? end : entry->end; 251 252 /* 253 * Special case for objects with no "real" pages. Those 254 * are always considered resident (mapped devices). 255 */ 256 if (UVM_ET_ISOBJ(entry)) { 257 KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)); 258 if (entry->object.uvm_obj->pgops->pgo_fault != NULL) { 259 for (/* nothing */; start < lim; 260 start += PAGE_SIZE, pgi++) 261 *pgi = 1; 262 continue; 263 } 264 } 265 266 amap = entry->aref.ar_amap; /* top layer */ 267 uobj = entry->object.uvm_obj; /* bottom layer */ 268 269 for (/* nothing */; start < lim; start += PAGE_SIZE, pgi++) { 270 *pgi = 0; 271 if (amap != NULL) { 272 /* Check the top layer first. */ 273 anon = amap_lookup(&entry->aref, 274 start - entry->start); 275 if (anon != NULL && anon->an_page != NULL) { 276 /* 277 * Anon has the page for this entry 278 * offset. 279 */ 280 *pgi = 1; 281 } 282 } 283 284 if (uobj != NULL && *pgi == 0) { 285 /* Check the bottom layer. */ 286 m = uvm_pagelookup(uobj, 287 entry->offset + (start - entry->start)); 288 if (m != NULL) { 289 /* 290 * Object has the page for this entry 291 * offset. 292 */ 293 *pgi = 1; 294 } 295 } 296 } 297 } 298 299 out: 300 vm_map_unlock_read(map); 301 uvm_vsunlock(p, SCARG(uap, vec), npgs); 302 /* now the map is unlocked we can copyout without fear. */ 303 if (error == 0) 304 copyout(pgs, vec, npgs * sizeof(char)); 305 free(pgs, M_TEMP, npgs * sizeof(*pgs)); 306 return (error); 307 } 308 309 int uvm_wxabort; 310 311 /* 312 * W^X violations are only allowed on permitted filesystems. 313 */ 314 static inline int 315 uvm_wxcheck(struct proc *p, char *call) 316 { 317 struct process *pr = p->p_p; 318 int wxallowed = (pr->ps_textvp->v_mount && 319 (pr->ps_textvp->v_mount->mnt_flag & MNT_WXALLOWED)); 320 321 if (wxallowed && (pr->ps_flags & PS_WXNEEDED)) 322 return (0); 323 324 /* Report W^X failures, and potentially SIGABRT */ 325 if (pr->ps_wxcounter++ == 0) 326 log(LOG_NOTICE, "%s(%d): %s W^X violation\n", 327 p->p_comm, p->p_pid, call); 328 if (!wxallowed || uvm_wxabort) { 329 struct sigaction sa; 330 331 /* Send uncatchable SIGABRT for coredump */ 332 memset(&sa, 0, sizeof sa); 333 sa.sa_handler = SIG_DFL; 334 setsigvec(p, SIGABRT, &sa); 335 psignal(p, SIGABRT); 336 } 337 return (0); /* ENOTSUP later */ 338 } 339 340 /* 341 * sys_mmap: mmap system call. 342 * 343 * => file offset and address may not be page aligned 344 * - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE 345 * - if address isn't page aligned the mapping starts at trunc_page(addr) 346 * and the return value is adjusted up by the page offset. 347 */ 348 int 349 sys_mmap(struct proc *p, void *v, register_t *retval) 350 { 351 struct sys_mmap_args /* { 352 syscallarg(void *) addr; 353 syscallarg(size_t) len; 354 syscallarg(int) prot; 355 syscallarg(int) flags; 356 syscallarg(int) fd; 357 syscallarg(long) pad; 358 syscallarg(off_t) pos; 359 } */ *uap = v; 360 vaddr_t addr; 361 struct vattr va; 362 off_t pos; 363 vsize_t size, pageoff; 364 vm_prot_t prot, maxprot; 365 int flags, fd; 366 vaddr_t vm_min_address = VM_MIN_ADDRESS; 367 struct filedesc *fdp = p->p_fd; 368 struct file *fp = NULL; 369 struct vnode *vp; 370 int error; 371 372 /* first, extract syscall args from the uap. */ 373 addr = (vaddr_t) SCARG(uap, addr); 374 size = (vsize_t) SCARG(uap, len); 375 prot = SCARG(uap, prot); 376 flags = SCARG(uap, flags); 377 fd = SCARG(uap, fd); 378 pos = SCARG(uap, pos); 379 380 /* 381 * Validate the flags. 382 */ 383 if ((prot & PROT_MASK) != prot) 384 return (EINVAL); 385 if ((prot & (PROT_WRITE | PROT_EXEC)) == (PROT_WRITE | PROT_EXEC) && 386 (error = uvm_wxcheck(p, "mmap"))) 387 return (error); 388 389 if ((flags & MAP_FLAGMASK) != flags) 390 return (EINVAL); 391 if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE)) 392 return (EINVAL); 393 if ((flags & (MAP_FIXED|__MAP_NOREPLACE)) == __MAP_NOREPLACE) 394 return (EINVAL); 395 if (size == 0) 396 return (EINVAL); 397 398 error = pledge_protexec(p, prot); 399 if (error) 400 return (error); 401 402 /* align file position and save offset. adjust size. */ 403 ALIGN_ADDR(pos, size, pageoff); 404 405 /* now check (MAP_FIXED) or get (!MAP_FIXED) the "addr" */ 406 if (flags & MAP_FIXED) { 407 /* adjust address by the same amount as we did the offset */ 408 addr -= pageoff; 409 if (addr & PAGE_MASK) 410 return (EINVAL); /* not page aligned */ 411 412 if (addr > SIZE_MAX - size) 413 return (EINVAL); /* no wrapping! */ 414 if (VM_MAXUSER_ADDRESS > 0 && 415 (addr + size) > VM_MAXUSER_ADDRESS) 416 return (EINVAL); 417 if (vm_min_address > 0 && addr < vm_min_address) 418 return (EINVAL); 419 420 } 421 422 /* check for file mappings (i.e. not anonymous) and verify file. */ 423 if ((flags & MAP_ANON) == 0) { 424 KERNEL_LOCK(); 425 if ((fp = fd_getfile(fdp, fd)) == NULL) { 426 KERNEL_UNLOCK(); 427 return (EBADF); 428 } 429 430 FREF(fp); 431 432 if (fp->f_type != DTYPE_VNODE) { 433 error = ENODEV; /* only mmap vnodes! */ 434 goto out; 435 } 436 vp = (struct vnode *)fp->f_data; /* convert to vnode */ 437 438 if (vp->v_type != VREG && vp->v_type != VCHR && 439 vp->v_type != VBLK) { 440 error = ENODEV; /* only REG/CHR/BLK support mmap */ 441 goto out; 442 } 443 444 if (vp->v_type == VREG && (pos + size) < pos) { 445 error = EINVAL; /* no offset wrapping */ 446 goto out; 447 } 448 449 /* special case: catch SunOS style /dev/zero */ 450 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) { 451 flags |= MAP_ANON; 452 FRELE(fp, p); 453 fp = NULL; 454 /* XXX */ 455 KERNEL_UNLOCK(); 456 goto is_anon; 457 } 458 459 /* 460 * Old programs may not select a specific sharing type, so 461 * default to an appropriate one. 462 * 463 * XXX: how does MAP_ANON fit in the picture? 464 */ 465 if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) { 466 #if defined(DEBUG) 467 printf("WARNING: defaulted mmap() share type to " 468 "%s (pid %d comm %s)\n", vp->v_type == VCHR ? 469 "MAP_SHARED" : "MAP_PRIVATE", p->p_pid, 470 p->p_comm); 471 #endif 472 if (vp->v_type == VCHR) 473 flags |= MAP_SHARED; /* for a device */ 474 else 475 flags |= MAP_PRIVATE; /* for a file */ 476 } 477 478 /* 479 * MAP_PRIVATE device mappings don't make sense (and aren't 480 * supported anyway). However, some programs rely on this, 481 * so just change it to MAP_SHARED. 482 */ 483 if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) { 484 flags = (flags & ~MAP_PRIVATE) | MAP_SHARED; 485 } 486 487 /* now check protection */ 488 maxprot = PROT_EXEC; 489 490 /* check read access */ 491 if (fp->f_flag & FREAD) 492 maxprot |= PROT_READ; 493 else if (prot & PROT_READ) { 494 error = EACCES; 495 goto out; 496 } 497 498 /* check write access, shared case first */ 499 if (flags & MAP_SHARED) { 500 /* 501 * if the file is writable, only add PROT_WRITE to 502 * maxprot if the file is not immutable, append-only. 503 * otherwise, if we have asked for PROT_WRITE, return 504 * EPERM. 505 */ 506 if (fp->f_flag & FWRITE) { 507 if ((error = 508 VOP_GETATTR(vp, &va, p->p_ucred, p))) 509 goto out; 510 if ((va.va_flags & (IMMUTABLE|APPEND)) == 0) 511 maxprot |= PROT_WRITE; 512 else if (prot & PROT_WRITE) { 513 error = EPERM; 514 goto out; 515 } 516 } else if (prot & PROT_WRITE) { 517 error = EACCES; 518 goto out; 519 } 520 } else { 521 /* MAP_PRIVATE mappings can always write to */ 522 maxprot |= PROT_WRITE; 523 } 524 if ((flags & MAP_ANON) != 0 || (flags & __MAP_NOFAULT) != 0 || 525 ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) { 526 if (p->p_rlimit[RLIMIT_DATA].rlim_cur < size || 527 p->p_rlimit[RLIMIT_DATA].rlim_cur - size < 528 ptoa(p->p_vmspace->vm_dused)) { 529 error = ENOMEM; 530 goto out; 531 } 532 } 533 error = uvm_mmapfile(&p->p_vmspace->vm_map, &addr, size, prot, maxprot, 534 flags, vp, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur, p); 535 } else { /* MAP_ANON case */ 536 /* 537 * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0? 538 */ 539 if (fd != -1) 540 return EINVAL; 541 542 is_anon: /* label for SunOS style /dev/zero */ 543 544 if ((flags & MAP_ANON) != 0 || (flags & __MAP_NOFAULT) != 0 || 545 ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) { 546 if (p->p_rlimit[RLIMIT_DATA].rlim_cur < size || 547 p->p_rlimit[RLIMIT_DATA].rlim_cur - size < 548 ptoa(p->p_vmspace->vm_dused)) { 549 return ENOMEM; 550 } 551 } 552 maxprot = PROT_MASK; 553 error = uvm_mmapanon(&p->p_vmspace->vm_map, &addr, size, prot, maxprot, 554 flags, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur, p); 555 } 556 557 if (error == 0) 558 /* remember to add offset */ 559 *retval = (register_t)(addr + pageoff); 560 561 out: 562 if (fp) { 563 FRELE(fp, p); 564 KERNEL_UNLOCK(); 565 } 566 return (error); 567 } 568 569 /* 570 * sys_msync: the msync system call (a front-end for flush) 571 */ 572 573 int 574 sys_msync(struct proc *p, void *v, register_t *retval) 575 { 576 struct sys_msync_args /* { 577 syscallarg(void *) addr; 578 syscallarg(size_t) len; 579 syscallarg(int) flags; 580 } */ *uap = v; 581 vaddr_t addr; 582 vsize_t size, pageoff; 583 vm_map_t map; 584 int flags, uvmflags; 585 586 /* extract syscall args from the uap */ 587 addr = (vaddr_t)SCARG(uap, addr); 588 size = (vsize_t)SCARG(uap, len); 589 flags = SCARG(uap, flags); 590 591 /* sanity check flags */ 592 if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 || 593 (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 || 594 (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC)) 595 return (EINVAL); 596 if ((flags & (MS_ASYNC | MS_SYNC)) == 0) 597 flags |= MS_SYNC; 598 599 /* align the address to a page boundary, and adjust the size accordingly */ 600 ALIGN_ADDR(addr, size, pageoff); 601 if (addr > SIZE_MAX - size) 602 return (EINVAL); /* disallow wrap-around. */ 603 604 /* get map */ 605 map = &p->p_vmspace->vm_map; 606 607 /* translate MS_ flags into PGO_ flags */ 608 uvmflags = PGO_CLEANIT; 609 if (flags & MS_INVALIDATE) 610 uvmflags |= PGO_FREE; 611 if (flags & MS_SYNC) 612 uvmflags |= PGO_SYNCIO; 613 else 614 uvmflags |= PGO_SYNCIO; /* XXXCDC: force sync for now! */ 615 616 return (uvm_map_clean(map, addr, addr+size, uvmflags)); 617 } 618 619 /* 620 * sys_munmap: unmap a users memory 621 */ 622 int 623 sys_munmap(struct proc *p, void *v, register_t *retval) 624 { 625 struct sys_munmap_args /* { 626 syscallarg(void *) addr; 627 syscallarg(size_t) len; 628 } */ *uap = v; 629 vaddr_t addr; 630 vsize_t size, pageoff; 631 vm_map_t map; 632 vaddr_t vm_min_address = VM_MIN_ADDRESS; 633 struct uvm_map_deadq dead_entries; 634 635 /* get syscall args... */ 636 addr = (vaddr_t) SCARG(uap, addr); 637 size = (vsize_t) SCARG(uap, len); 638 639 /* align address to a page boundary, and adjust size accordingly */ 640 ALIGN_ADDR(addr, size, pageoff); 641 642 /* 643 * Check for illegal addresses. Watch out for address wrap... 644 * Note that VM_*_ADDRESS are not constants due to casts (argh). 645 */ 646 if (addr > SIZE_MAX - size) 647 return (EINVAL); 648 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS) 649 return (EINVAL); 650 if (vm_min_address > 0 && addr < vm_min_address) 651 return (EINVAL); 652 map = &p->p_vmspace->vm_map; 653 654 655 vm_map_lock(map); /* lock map so we can checkprot */ 656 657 /* 658 * interesting system call semantic: make sure entire range is 659 * allocated before allowing an unmap. 660 */ 661 if (!uvm_map_checkprot(map, addr, addr + size, PROT_NONE)) { 662 vm_map_unlock(map); 663 return (EINVAL); 664 } 665 666 TAILQ_INIT(&dead_entries); 667 uvm_unmap_remove(map, addr, addr + size, &dead_entries, FALSE, TRUE); 668 669 vm_map_unlock(map); /* and unlock */ 670 671 uvm_unmap_detach(&dead_entries, 0); 672 673 return (0); 674 } 675 676 /* 677 * sys_mprotect: the mprotect system call 678 */ 679 int 680 sys_mprotect(struct proc *p, void *v, register_t *retval) 681 { 682 struct sys_mprotect_args /* { 683 syscallarg(void *) addr; 684 syscallarg(size_t) len; 685 syscallarg(int) prot; 686 } */ *uap = v; 687 vaddr_t addr; 688 vsize_t size, pageoff; 689 vm_prot_t prot; 690 int error; 691 692 /* 693 * extract syscall args from uap 694 */ 695 696 addr = (vaddr_t)SCARG(uap, addr); 697 size = (vsize_t)SCARG(uap, len); 698 prot = SCARG(uap, prot); 699 700 if ((prot & PROT_MASK) != prot) 701 return (EINVAL); 702 if ((prot & (PROT_WRITE | PROT_EXEC)) == (PROT_WRITE | PROT_EXEC) && 703 (error = uvm_wxcheck(p, "mprotect"))) 704 return (error); 705 706 error = pledge_protexec(p, prot); 707 if (error) 708 return (error); 709 710 /* 711 * align the address to a page boundary, and adjust the size accordingly 712 */ 713 ALIGN_ADDR(addr, size, pageoff); 714 if (addr > SIZE_MAX - size) 715 return (EINVAL); /* disallow wrap-around. */ 716 717 return (uvm_map_protect(&p->p_vmspace->vm_map, addr, addr+size, 718 prot, FALSE)); 719 } 720 721 /* 722 * sys_minherit: the minherit system call 723 */ 724 int 725 sys_minherit(struct proc *p, void *v, register_t *retval) 726 { 727 struct sys_minherit_args /* { 728 syscallarg(void *) addr; 729 syscallarg(size_t) len; 730 syscallarg(int) inherit; 731 } */ *uap = v; 732 vaddr_t addr; 733 vsize_t size, pageoff; 734 vm_inherit_t inherit; 735 736 addr = (vaddr_t)SCARG(uap, addr); 737 size = (vsize_t)SCARG(uap, len); 738 inherit = SCARG(uap, inherit); 739 740 /* 741 * align the address to a page boundary, and adjust the size accordingly 742 */ 743 ALIGN_ADDR(addr, size, pageoff); 744 if (addr > SIZE_MAX - size) 745 return (EINVAL); /* disallow wrap-around. */ 746 747 return (uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr+size, 748 inherit)); 749 } 750 751 /* 752 * sys_madvise: give advice about memory usage. 753 */ 754 /* ARGSUSED */ 755 int 756 sys_madvise(struct proc *p, void *v, register_t *retval) 757 { 758 struct sys_madvise_args /* { 759 syscallarg(void *) 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 ALIGN_ADDR(addr, size, pageoff); 775 if (addr > SIZE_MAX - size) 776 return (EINVAL); /* disallow wrap-around. */ 777 778 switch (advice) { 779 case MADV_NORMAL: 780 case MADV_RANDOM: 781 case MADV_SEQUENTIAL: 782 error = uvm_map_advice(&p->p_vmspace->vm_map, addr, 783 addr + size, advice); 784 break; 785 786 case MADV_WILLNEED: 787 /* 788 * Activate all these pages, pre-faulting them in if 789 * necessary. 790 */ 791 /* 792 * XXX IMPLEMENT ME. 793 * Should invent a "weak" mode for uvm_fault() 794 * which would only do the PGO_LOCKED pgo_get(). 795 */ 796 return (0); 797 798 case MADV_DONTNEED: 799 /* 800 * Deactivate all these pages. We don't need them 801 * any more. We don't, however, toss the data in 802 * the pages. 803 */ 804 error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size, 805 PGO_DEACTIVATE); 806 break; 807 808 case MADV_FREE: 809 /* 810 * These pages contain no valid data, and may be 811 * garbage-collected. Toss all resources, including 812 * any swap space in use. 813 */ 814 error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size, 815 PGO_FREE); 816 break; 817 818 case MADV_SPACEAVAIL: 819 /* 820 * XXXMRG What is this? I think it's: 821 * 822 * Ensure that we have allocated backing-store 823 * for these pages. 824 * 825 * This is going to require changes to the page daemon, 826 * as it will free swap space allocated to pages in core. 827 * There's also what to do for device/file/anonymous memory. 828 */ 829 return (EINVAL); 830 831 default: 832 return (EINVAL); 833 } 834 835 return (error); 836 } 837 838 /* 839 * sys_mlock: memory lock 840 */ 841 842 int 843 sys_mlock(struct proc *p, void *v, register_t *retval) 844 { 845 struct sys_mlock_args /* { 846 syscallarg(const void *) addr; 847 syscallarg(size_t) len; 848 } */ *uap = v; 849 vaddr_t addr; 850 vsize_t size, pageoff; 851 int error; 852 853 /* extract syscall args from uap */ 854 addr = (vaddr_t)SCARG(uap, addr); 855 size = (vsize_t)SCARG(uap, len); 856 857 /* align address to a page boundary and adjust size accordingly */ 858 ALIGN_ADDR(addr, size, pageoff); 859 if (addr > SIZE_MAX - size) 860 return (EINVAL); /* disallow wrap-around. */ 861 862 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) 863 return (EAGAIN); 864 865 #ifdef pmap_wired_count 866 if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) > 867 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur) 868 return (EAGAIN); 869 #else 870 if ((error = suser(p, 0)) != 0) 871 return (error); 872 #endif 873 874 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE, 875 0); 876 return (error == 0 ? 0 : ENOMEM); 877 } 878 879 /* 880 * sys_munlock: unlock wired pages 881 */ 882 883 int 884 sys_munlock(struct proc *p, void *v, register_t *retval) 885 { 886 struct sys_munlock_args /* { 887 syscallarg(const void *) addr; 888 syscallarg(size_t) len; 889 } */ *uap = v; 890 vaddr_t addr; 891 vsize_t size, pageoff; 892 int error; 893 894 /* extract syscall args from uap */ 895 addr = (vaddr_t)SCARG(uap, addr); 896 size = (vsize_t)SCARG(uap, len); 897 898 /* align address to a page boundary, and adjust size accordingly */ 899 ALIGN_ADDR(addr, size, pageoff); 900 if (addr > SIZE_MAX - size) 901 return (EINVAL); /* disallow wrap-around. */ 902 903 #ifndef pmap_wired_count 904 if ((error = suser(p, 0)) != 0) 905 return (error); 906 #endif 907 908 error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE, 909 0); 910 return (error == 0 ? 0 : ENOMEM); 911 } 912 913 /* 914 * sys_mlockall: lock all pages mapped into an address space. 915 */ 916 int 917 sys_mlockall(struct proc *p, void *v, register_t *retval) 918 { 919 struct sys_mlockall_args /* { 920 syscallarg(int) flags; 921 } */ *uap = v; 922 int error, flags; 923 924 flags = SCARG(uap, flags); 925 926 if (flags == 0 || 927 (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0) 928 return (EINVAL); 929 930 #ifndef pmap_wired_count 931 if ((error = suser(p, 0)) != 0) 932 return (error); 933 #endif 934 935 error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags, 936 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur); 937 if (error != 0 && error != ENOMEM) 938 return (EAGAIN); 939 return (error); 940 } 941 942 /* 943 * sys_munlockall: unlock all pages mapped into an address space. 944 */ 945 int 946 sys_munlockall(struct proc *p, void *v, register_t *retval) 947 { 948 949 (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0); 950 return (0); 951 } 952 953 /* 954 * common code for mmapanon and mmapfile to lock a mmaping 955 */ 956 int 957 uvm_mmaplock(vm_map_t map, vaddr_t *addr, vsize_t size, vm_prot_t prot, 958 vsize_t locklimit) 959 { 960 int error; 961 962 /* 963 * POSIX 1003.1b -- if our address space was configured 964 * to lock all future mappings, wire the one we just made. 965 */ 966 if (prot == PROT_NONE) { 967 /* 968 * No more work to do in this case. 969 */ 970 return (0); 971 } 972 973 vm_map_lock(map); 974 if (map->flags & VM_MAP_WIREFUTURE) { 975 KERNEL_LOCK(); 976 if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax 977 #ifdef pmap_wired_count 978 || (locklimit != 0 && (size + 979 ptoa(pmap_wired_count(vm_map_pmap(map)))) > 980 locklimit) 981 #endif 982 ) { 983 error = ENOMEM; 984 vm_map_unlock(map); 985 /* unmap the region! */ 986 uvm_unmap(map, *addr, *addr + size); 987 KERNEL_UNLOCK(); 988 return (error); 989 } 990 /* 991 * uvm_map_pageable() always returns the map 992 * unlocked. 993 */ 994 error = uvm_map_pageable(map, *addr, *addr + size, 995 FALSE, UVM_LK_ENTER); 996 if (error != 0) { 997 /* unmap the region! */ 998 uvm_unmap(map, *addr, *addr + size); 999 KERNEL_UNLOCK(); 1000 return (error); 1001 } 1002 KERNEL_UNLOCK(); 1003 return (0); 1004 } 1005 vm_map_unlock(map); 1006 return (0); 1007 } 1008 1009 /* 1010 * uvm_mmapanon: internal version of mmap for anons 1011 * 1012 * - used by sys_mmap 1013 */ 1014 int 1015 uvm_mmapanon(vm_map_t map, vaddr_t *addr, vsize_t size, vm_prot_t prot, 1016 vm_prot_t maxprot, int flags, vsize_t locklimit, struct proc *p) 1017 { 1018 int error; 1019 int advice = MADV_NORMAL; 1020 unsigned int uvmflag = 0; 1021 vsize_t align = 0; /* userland page size */ 1022 1023 /* 1024 * for non-fixed mappings, round off the suggested address. 1025 * for fixed mappings, check alignment and zap old mappings. 1026 */ 1027 if ((flags & MAP_FIXED) == 0) { 1028 *addr = round_page(*addr); /* round */ 1029 } else { 1030 if (*addr & PAGE_MASK) 1031 return(EINVAL); 1032 1033 uvmflag |= UVM_FLAG_FIXED; 1034 if ((flags & __MAP_NOREPLACE) == 0) 1035 uvmflag |= UVM_FLAG_UNMAP; 1036 } 1037 1038 if ((flags & MAP_FIXED) == 0 && size >= __LDPGSZ) 1039 align = __LDPGSZ; 1040 if ((flags & MAP_SHARED) == 0) 1041 /* XXX: defer amap create */ 1042 uvmflag |= UVM_FLAG_COPYONW; 1043 else 1044 /* shared: create amap now */ 1045 uvmflag |= UVM_FLAG_OVERLAY; 1046 1047 /* set up mapping flags */ 1048 uvmflag = UVM_MAPFLAG(prot, maxprot, 1049 (flags & MAP_SHARED) ? MAP_INHERIT_SHARE : MAP_INHERIT_COPY, 1050 advice, uvmflag); 1051 1052 error = uvm_mapanon(map, addr, size, align, uvmflag); 1053 1054 if (error == 0) 1055 error = uvm_mmaplock(map, addr, size, prot, locklimit); 1056 return error; 1057 } 1058 1059 /* 1060 * uvm_mmapfile: internal version of mmap for non-anons 1061 * 1062 * - used by sys_mmap 1063 * - caller must page-align the file offset 1064 */ 1065 int 1066 uvm_mmapfile(vm_map_t map, vaddr_t *addr, vsize_t size, vm_prot_t prot, 1067 vm_prot_t maxprot, int flags, struct vnode *vp, voff_t foff, 1068 vsize_t locklimit, struct proc *p) 1069 { 1070 struct uvm_object *uobj; 1071 int error; 1072 int advice = MADV_NORMAL; 1073 unsigned int uvmflag = 0; 1074 vsize_t align = 0; /* userland page size */ 1075 1076 /* 1077 * for non-fixed mappings, round off the suggested address. 1078 * for fixed mappings, check alignment and zap old mappings. 1079 */ 1080 if ((flags & MAP_FIXED) == 0) { 1081 *addr = round_page(*addr); /* round */ 1082 } else { 1083 if (*addr & PAGE_MASK) 1084 return(EINVAL); 1085 1086 uvmflag |= UVM_FLAG_FIXED; 1087 if ((flags & __MAP_NOREPLACE) == 0) 1088 uvmflag |= UVM_FLAG_UNMAP; 1089 } 1090 1091 /* 1092 * attach to underlying vm object. 1093 */ 1094 if (vp->v_type != VCHR) { 1095 uobj = uvn_attach(vp, (flags & MAP_SHARED) ? 1096 maxprot : (maxprot & ~PROT_WRITE)); 1097 1098 /* 1099 * XXXCDC: hack from old code 1100 * don't allow vnodes which have been mapped 1101 * shared-writeable to persist [forces them to be 1102 * flushed out when last reference goes]. 1103 * XXXCDC: interesting side effect: avoids a bug. 1104 * note that in WRITE [ufs_readwrite.c] that we 1105 * allocate buffer, uncache, and then do the write. 1106 * the problem with this is that if the uncache causes 1107 * VM data to be flushed to the same area of the file 1108 * we are writing to... in that case we've got the 1109 * buffer locked and our process goes to sleep forever. 1110 * 1111 * XXXCDC: checking maxprot protects us from the 1112 * "persistbug" program but this is not a long term 1113 * solution. 1114 * 1115 * XXXCDC: we don't bother calling uncache with the vp 1116 * VOP_LOCKed since we know that we are already 1117 * holding a valid reference to the uvn (from the 1118 * uvn_attach above), and thus it is impossible for 1119 * the uncache to kill the uvn and trigger I/O. 1120 */ 1121 if (flags & MAP_SHARED) { 1122 if ((prot & PROT_WRITE) || 1123 (maxprot & PROT_WRITE)) { 1124 uvm_vnp_uncache(vp); 1125 } 1126 } 1127 } else { 1128 uobj = udv_attach(vp->v_rdev, 1129 (flags & MAP_SHARED) ? maxprot : 1130 (maxprot & ~PROT_WRITE), foff, size); 1131 /* 1132 * XXX Some devices don't like to be mapped with 1133 * XXX PROT_EXEC, but we don't really have a 1134 * XXX better way of handling this, right now 1135 */ 1136 if (uobj == NULL && (prot & PROT_EXEC) == 0) { 1137 maxprot &= ~PROT_EXEC; 1138 uobj = udv_attach(vp->v_rdev, 1139 (flags & MAP_SHARED) ? maxprot : 1140 (maxprot & ~PROT_WRITE), foff, size); 1141 } 1142 advice = MADV_RANDOM; 1143 } 1144 1145 if (uobj == NULL) 1146 return((vp->v_type == VREG) ? ENOMEM : EINVAL); 1147 1148 if ((flags & MAP_SHARED) == 0) 1149 uvmflag |= UVM_FLAG_COPYONW; 1150 if (flags & __MAP_NOFAULT) 1151 uvmflag |= (UVM_FLAG_NOFAULT | UVM_FLAG_OVERLAY); 1152 1153 /* set up mapping flags */ 1154 uvmflag = UVM_MAPFLAG(prot, maxprot, 1155 (flags & MAP_SHARED) ? MAP_INHERIT_SHARE : MAP_INHERIT_COPY, 1156 advice, uvmflag); 1157 1158 error = uvm_map(map, addr, size, uobj, foff, align, uvmflag); 1159 1160 if (error == 0) 1161 return uvm_mmaplock(map, addr, size, prot, locklimit); 1162 1163 /* errors: first detach from the uobj, if any. */ 1164 if (uobj) 1165 uobj->pgops->pgo_detach(uobj); 1166 1167 return (error); 1168 } 1169 1170 /* an address that can't be in userspace */ 1171 #define BOGO_PC (KERNBASE + 1) 1172 int 1173 sys_kbind(struct proc *p, void *v, register_t *retval) 1174 { 1175 struct sys_kbind_args /* { 1176 syscallarg(const struct __kbind *) param; 1177 syscallarg(size_t) psize; 1178 syscallarg(uint64_t) proc_cookie; 1179 } */ *uap = v; 1180 const struct __kbind *paramp; 1181 union { 1182 struct __kbind uk[KBIND_BLOCK_MAX]; 1183 char upad[KBIND_BLOCK_MAX * sizeof(*paramp) + KBIND_DATA_MAX]; 1184 } param; 1185 struct uvm_map_deadq dead_entries; 1186 struct process *pr = p->p_p; 1187 const char *data; 1188 vaddr_t baseva, last_baseva, endva, pageoffset, kva; 1189 size_t psize, s; 1190 u_long pc; 1191 int count, i; 1192 int error; 1193 1194 /* 1195 * extract syscall args from uap 1196 */ 1197 paramp = SCARG(uap, param); 1198 psize = SCARG(uap, psize); 1199 1200 /* a NULL paramp disables the syscall for the process */ 1201 if (paramp == NULL) { 1202 pr->ps_kbind_addr = BOGO_PC; 1203 return (0); 1204 } 1205 1206 /* security checks */ 1207 pc = PROC_PC(p); 1208 if (pr->ps_kbind_addr == 0) { 1209 pr->ps_kbind_addr = pc; 1210 pr->ps_kbind_cookie = SCARG(uap, proc_cookie); 1211 } else if (pc != pr->ps_kbind_addr || pc == BOGO_PC) 1212 sigexit(p, SIGILL); 1213 else if (pr->ps_kbind_cookie != SCARG(uap, proc_cookie)) 1214 sigexit(p, SIGILL); 1215 if (psize < sizeof(struct __kbind) || psize > sizeof(param)) 1216 return (EINVAL); 1217 if ((error = copyin(paramp, ¶m, psize))) 1218 return (error); 1219 1220 /* 1221 * The param argument points to an array of __kbind structures 1222 * followed by the corresponding new data areas for them. Verify 1223 * that the sizes in the __kbind structures add up to the total 1224 * size and find the start of the new area. 1225 */ 1226 paramp = ¶m.uk[0]; 1227 s = psize; 1228 for (count = 0; s > 0 && count < KBIND_BLOCK_MAX; count++) { 1229 if (s < sizeof(*paramp)) 1230 return (EINVAL); 1231 s -= sizeof(*paramp); 1232 1233 baseva = (vaddr_t)paramp[count].kb_addr; 1234 endva = baseva + paramp[count].kb_size - 1; 1235 if (paramp[count].kb_addr == NULL || 1236 paramp[count].kb_size == 0 || 1237 paramp[count].kb_size > KBIND_DATA_MAX || 1238 baseva >= VM_MAXUSER_ADDRESS || 1239 endva >= VM_MAXUSER_ADDRESS || 1240 trunc_page(baseva) != trunc_page(endva) || 1241 s < paramp[count].kb_size) 1242 return (EINVAL); 1243 1244 s -= paramp[count].kb_size; 1245 } 1246 if (s > 0) 1247 return (EINVAL); 1248 data = (const char *)¶mp[count]; 1249 1250 /* all looks good, so do the bindings */ 1251 last_baseva = VM_MAXUSER_ADDRESS; 1252 kva = 0; 1253 TAILQ_INIT(&dead_entries); 1254 for (i = 0; i < count; i++) { 1255 baseva = (vaddr_t)paramp[i].kb_addr; 1256 pageoffset = baseva & PAGE_MASK; 1257 baseva = trunc_page(baseva); 1258 1259 /* make sure sure the desired page is mapped into kernel_map */ 1260 if (baseva != last_baseva) { 1261 if (kva != 0) { 1262 vm_map_lock(kernel_map); 1263 uvm_unmap_remove(kernel_map, kva, 1264 kva+PAGE_SIZE, &dead_entries, FALSE, TRUE); 1265 vm_map_unlock(kernel_map); 1266 kva = 0; 1267 } 1268 if ((error = uvm_map_extract(&p->p_vmspace->vm_map, 1269 baseva, PAGE_SIZE, &kva, UVM_EXTRACT_FIXPROT))) 1270 break; 1271 last_baseva = baseva; 1272 } 1273 1274 /* do the update */ 1275 if ((error = kcopy(data, (char *)kva + pageoffset, 1276 paramp[i].kb_size))) 1277 break; 1278 data += paramp[i].kb_size; 1279 } 1280 1281 if (kva != 0) { 1282 vm_map_lock(kernel_map); 1283 uvm_unmap_remove(kernel_map, kva, kva+PAGE_SIZE, 1284 &dead_entries, FALSE, TRUE); 1285 vm_map_unlock(kernel_map); 1286 } 1287 uvm_unmap_detach(&dead_entries, AMAP_REFALL); 1288 1289 return (error); 1290 } 1291