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