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