1 /* $NetBSD: uvm_glue.c,v 1.59 2002/07/02 20:27:48 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 * Copyright (c) 1991, 1993, The Regents of the University of California. 6 * 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by Charles D. Cranor, 23 * Washington University, the University of California, Berkeley and 24 * its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 42 * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp 43 * 44 * 45 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46 * All rights reserved. 47 * 48 * Permission to use, copy, modify and distribute this software and 49 * its documentation is hereby granted, provided that both the copyright 50 * notice and this permission notice appear in all copies of the 51 * software, derivative works or modified versions, and any portions 52 * thereof, and that both notices appear in supporting documentation. 53 * 54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57 * 58 * Carnegie Mellon requests users of this software to return to 59 * 60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61 * School of Computer Science 62 * Carnegie Mellon University 63 * Pittsburgh PA 15213-3890 64 * 65 * any improvements or extensions that they make and grant Carnegie the 66 * rights to redistribute these changes. 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.59 2002/07/02 20:27:48 yamt Exp $"); 71 72 #include "opt_kgdb.h" 73 #include "opt_kstack.h" 74 #include "opt_sysv.h" 75 #include "opt_uvmhist.h" 76 77 /* 78 * uvm_glue.c: glue functions 79 */ 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/proc.h> 84 #include <sys/resourcevar.h> 85 #include <sys/buf.h> 86 #include <sys/user.h> 87 #ifdef SYSVSHM 88 #include <sys/shm.h> 89 #endif 90 91 #include <uvm/uvm.h> 92 93 #include <machine/cpu.h> 94 95 /* 96 * local prototypes 97 */ 98 99 static void uvm_swapout __P((struct proc *)); 100 101 /* 102 * XXXCDC: do these really belong here? 103 */ 104 105 int readbuffers = 0; /* allow KGDB to read kern buffer pool */ 106 /* XXX: see uvm_kernacc */ 107 108 109 /* 110 * uvm_kernacc: can the kernel access a region of memory 111 * 112 * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c) 113 */ 114 115 boolean_t 116 uvm_kernacc(addr, len, rw) 117 caddr_t addr; 118 size_t len; 119 int rw; 120 { 121 boolean_t rv; 122 vaddr_t saddr, eaddr; 123 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 124 125 saddr = trunc_page((vaddr_t)addr); 126 eaddr = round_page((vaddr_t)addr + len); 127 vm_map_lock_read(kernel_map); 128 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot); 129 vm_map_unlock_read(kernel_map); 130 131 /* 132 * XXX there are still some things (e.g. the buffer cache) that 133 * are managed behind the VM system's back so even though an 134 * address is accessible in the mind of the VM system, there may 135 * not be physical pages where the VM thinks there is. This can 136 * lead to bogus allocation of pages in the kernel address space 137 * or worse, inconsistencies at the pmap level. We only worry 138 * about the buffer cache for now. 139 */ 140 if (!readbuffers && rv && (eaddr > (vaddr_t)buffers && 141 saddr < (vaddr_t)buffers + MAXBSIZE * nbuf)) 142 rv = FALSE; 143 return(rv); 144 } 145 146 /* 147 * uvm_useracc: can the user access it? 148 * 149 * - called from physio() and sys___sysctl(). 150 */ 151 152 boolean_t 153 uvm_useracc(addr, len, rw) 154 caddr_t addr; 155 size_t len; 156 int rw; 157 { 158 struct vm_map *map; 159 boolean_t rv; 160 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 161 162 /* XXX curproc */ 163 map = &curproc->p_vmspace->vm_map; 164 165 vm_map_lock_read(map); 166 rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr), 167 round_page((vaddr_t)addr + len), prot); 168 vm_map_unlock_read(map); 169 170 return(rv); 171 } 172 173 #ifdef KGDB 174 /* 175 * Change protections on kernel pages from addr to addr+len 176 * (presumably so debugger can plant a breakpoint). 177 * 178 * We force the protection change at the pmap level. If we were 179 * to use vm_map_protect a change to allow writing would be lazily- 180 * applied meaning we would still take a protection fault, something 181 * we really don't want to do. It would also fragment the kernel 182 * map unnecessarily. We cannot use pmap_protect since it also won't 183 * enforce a write-enable request. Using pmap_enter is the only way 184 * we can ensure the change takes place properly. 185 */ 186 void 187 uvm_chgkprot(addr, len, rw) 188 caddr_t addr; 189 size_t len; 190 int rw; 191 { 192 vm_prot_t prot; 193 paddr_t pa; 194 vaddr_t sva, eva; 195 196 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE; 197 eva = round_page((vaddr_t)addr + len); 198 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) { 199 /* 200 * Extract physical address for the page. 201 */ 202 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE) 203 panic("chgkprot: invalid page"); 204 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 205 } 206 pmap_update(pmap_kernel()); 207 } 208 #endif 209 210 /* 211 * uvm_vslock: wire user memory for I/O 212 * 213 * - called from physio and sys___sysctl 214 * - XXXCDC: consider nuking this (or making it a macro?) 215 */ 216 217 int 218 uvm_vslock(p, addr, len, access_type) 219 struct proc *p; 220 caddr_t addr; 221 size_t len; 222 vm_prot_t access_type; 223 { 224 struct vm_map *map; 225 vaddr_t start, end; 226 int error; 227 228 map = &p->p_vmspace->vm_map; 229 start = trunc_page((vaddr_t)addr); 230 end = round_page((vaddr_t)addr + len); 231 error = uvm_fault_wire(map, start, end, VM_FAULT_WIRE, access_type); 232 return error; 233 } 234 235 /* 236 * uvm_vsunlock: unwire user memory wired by uvm_vslock() 237 * 238 * - called from physio and sys___sysctl 239 * - XXXCDC: consider nuking this (or making it a macro?) 240 */ 241 242 void 243 uvm_vsunlock(p, addr, len) 244 struct proc *p; 245 caddr_t addr; 246 size_t len; 247 { 248 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr), 249 round_page((vaddr_t)addr + len)); 250 } 251 252 /* 253 * uvm_fork: fork a virtual address space 254 * 255 * - the address space is copied as per parent map's inherit values 256 * - a new "user" structure is allocated for the child process 257 * [filled in by MD layer...] 258 * - if specified, the child gets a new user stack described by 259 * stack and stacksize 260 * - NOTE: the kernel stack may be at a different location in the child 261 * process, and thus addresses of automatic variables may be invalid 262 * after cpu_fork returns in the child process. We do nothing here 263 * after cpu_fork returns. 264 * - XXXCDC: we need a way for this to return a failure value rather 265 * than just hang 266 */ 267 void 268 uvm_fork(p1, p2, shared, stack, stacksize, func, arg) 269 struct proc *p1, *p2; 270 boolean_t shared; 271 void *stack; 272 size_t stacksize; 273 void (*func) __P((void *)); 274 void *arg; 275 { 276 struct user *up = p2->p_addr; 277 int error; 278 279 if (shared == TRUE) { 280 p2->p_vmspace = NULL; 281 uvmspace_share(p1, p2); 282 } else 283 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); 284 285 /* 286 * Wire down the U-area for the process, which contains the PCB 287 * and the kernel stack. Wired state is stored in p->p_flag's 288 * P_INMEM bit rather than in the vm_map_entry's wired count 289 * to prevent kernel_map fragmentation. 290 * 291 * Note the kernel stack gets read/write accesses right off 292 * the bat. 293 */ 294 error = uvm_fault_wire(kernel_map, (vaddr_t)up, (vaddr_t)up + USPACE, 295 VM_FAULT_WIRE, VM_PROT_READ | VM_PROT_WRITE); 296 if (error) 297 panic("uvm_fork: uvm_fault_wire failed: %d", error); 298 299 #ifdef KSTACK_CHECK_MAGIC 300 /* 301 * fill stack with magic number 302 */ 303 kstack_setup_magic(p2); 304 #endif 305 306 /* 307 * p_stats currently points at a field in the user struct. Copy 308 * parts of p_stats, and zero out the rest. 309 */ 310 p2->p_stats = &up->u_stats; 311 memset(&up->u_stats.pstat_startzero, 0, 312 ((caddr_t)&up->u_stats.pstat_endzero - 313 (caddr_t)&up->u_stats.pstat_startzero)); 314 memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy, 315 ((caddr_t)&up->u_stats.pstat_endcopy - 316 (caddr_t)&up->u_stats.pstat_startcopy)); 317 318 /* 319 * cpu_fork() copy and update the pcb, and make the child ready 320 * to run. If this is a normal user fork, the child will exit 321 * directly to user mode via child_return() on its first time 322 * slice and will not return here. If this is a kernel thread, 323 * the specified entry point will be executed. 324 */ 325 cpu_fork(p1, p2, stack, stacksize, func, arg); 326 } 327 328 /* 329 * uvm_exit: exit a virtual address space 330 * 331 * - the process passed to us is a dead (pre-zombie) process; we 332 * are running on a different context now (the reaper). 333 * - we must run in a separate thread because freeing the vmspace 334 * of the dead process may block. 335 */ 336 void 337 uvm_exit(p) 338 struct proc *p; 339 { 340 vaddr_t va = (vaddr_t)p->p_addr; 341 342 uvmspace_free(p->p_vmspace); 343 p->p_flag &= ~P_INMEM; 344 uvm_km_free(kernel_map, va, USPACE); 345 p->p_addr = NULL; 346 } 347 348 /* 349 * uvm_init_limit: init per-process VM limits 350 * 351 * - called for process 0 and then inherited by all others. 352 */ 353 void 354 uvm_init_limits(p) 355 struct proc *p; 356 { 357 358 /* 359 * Set up the initial limits on process VM. Set the maximum 360 * resident set size to be all of (reasonably) available memory. 361 * This causes any single, large process to start random page 362 * replacement once it fills memory. 363 */ 364 365 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 366 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 367 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 368 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 369 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free); 370 } 371 372 #ifdef DEBUG 373 int enableswap = 1; 374 int swapdebug = 0; 375 #define SDB_FOLLOW 1 376 #define SDB_SWAPIN 2 377 #define SDB_SWAPOUT 4 378 #endif 379 380 /* 381 * uvm_swapin: swap in a process's u-area. 382 */ 383 384 void 385 uvm_swapin(p) 386 struct proc *p; 387 { 388 vaddr_t addr; 389 int s, error; 390 391 addr = (vaddr_t)p->p_addr; 392 /* make P_INMEM true */ 393 error = uvm_fault_wire(kernel_map, addr, addr + USPACE, VM_FAULT_WIRE, 394 VM_PROT_READ | VM_PROT_WRITE); 395 if (error) { 396 panic("uvm_swapin: rewiring stack failed: %d", error); 397 } 398 399 /* 400 * Some architectures need to be notified when the user area has 401 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c). 402 */ 403 cpu_swapin(p); 404 SCHED_LOCK(s); 405 if (p->p_stat == SRUN) 406 setrunqueue(p); 407 p->p_flag |= P_INMEM; 408 SCHED_UNLOCK(s); 409 p->p_swtime = 0; 410 ++uvmexp.swapins; 411 } 412 413 /* 414 * uvm_scheduler: process zero main loop 415 * 416 * - attempt to swapin every swaped-out, runnable process in order of 417 * priority. 418 * - if not enough memory, wake the pagedaemon and let it clear space. 419 */ 420 421 void 422 uvm_scheduler() 423 { 424 struct proc *p; 425 int pri; 426 struct proc *pp; 427 int ppri; 428 429 loop: 430 #ifdef DEBUG 431 while (!enableswap) 432 tsleep(&proc0, PVM, "noswap", 0); 433 #endif 434 pp = NULL; /* process to choose */ 435 ppri = INT_MIN; /* its priority */ 436 proclist_lock_read(); 437 LIST_FOREACH(p, &allproc, p_list) { 438 439 /* is it a runnable swapped out process? */ 440 if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) { 441 pri = p->p_swtime + p->p_slptime - 442 (p->p_nice - NZERO) * 8; 443 if (pri > ppri) { /* higher priority? remember it. */ 444 pp = p; 445 ppri = pri; 446 } 447 } 448 } 449 /* 450 * XXXSMP: possible unlock/sleep race between here and the 451 * "scheduler" tsleep below.. 452 */ 453 proclist_unlock_read(); 454 455 #ifdef DEBUG 456 if (swapdebug & SDB_FOLLOW) 457 printf("scheduler: running, procp %p pri %d\n", pp, ppri); 458 #endif 459 /* 460 * Nothing to do, back to sleep 461 */ 462 if ((p = pp) == NULL) { 463 tsleep(&proc0, PVM, "scheduler", 0); 464 goto loop; 465 } 466 467 /* 468 * we have found swapped out process which we would like to bring 469 * back in. 470 * 471 * XXX: this part is really bogus cuz we could deadlock on memory 472 * despite our feeble check 473 */ 474 if (uvmexp.free > atop(USPACE)) { 475 #ifdef DEBUG 476 if (swapdebug & SDB_SWAPIN) 477 printf("swapin: pid %d(%s)@%p, pri %d free %d\n", 478 p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free); 479 #endif 480 uvm_swapin(p); 481 goto loop; 482 } 483 /* 484 * not enough memory, jab the pageout daemon and wait til the coast 485 * is clear 486 */ 487 #ifdef DEBUG 488 if (swapdebug & SDB_FOLLOW) 489 printf("scheduler: no room for pid %d(%s), free %d\n", 490 p->p_pid, p->p_comm, uvmexp.free); 491 #endif 492 uvm_wait("schedpwait"); 493 #ifdef DEBUG 494 if (swapdebug & SDB_FOLLOW) 495 printf("scheduler: room again, free %d\n", uvmexp.free); 496 #endif 497 goto loop; 498 } 499 500 /* 501 * swappable: is process "p" swappable? 502 */ 503 504 #define swappable(p) \ 505 (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM && \ 506 (p)->p_holdcnt == 0) 507 508 /* 509 * swapout_threads: find threads that can be swapped and unwire their 510 * u-areas. 511 * 512 * - called by the pagedaemon 513 * - try and swap at least one processs 514 * - processes that are sleeping or stopped for maxslp or more seconds 515 * are swapped... otherwise the longest-sleeping or stopped process 516 * is swapped, otherwise the longest resident process... 517 */ 518 void 519 uvm_swapout_threads() 520 { 521 struct proc *p; 522 struct proc *outp, *outp2; 523 int outpri, outpri2; 524 int didswap = 0; 525 extern int maxslp; 526 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */ 527 528 #ifdef DEBUG 529 if (!enableswap) 530 return; 531 #endif 532 533 /* 534 * outp/outpri : stop/sleep process with largest sleeptime < maxslp 535 * outp2/outpri2: the longest resident process (its swap time) 536 */ 537 outp = outp2 = NULL; 538 outpri = outpri2 = 0; 539 proclist_lock_read(); 540 LIST_FOREACH(p, &allproc, p_list) { 541 if (!swappable(p)) 542 continue; 543 switch (p->p_stat) { 544 case SRUN: 545 case SONPROC: 546 if (p->p_swtime > outpri2) { 547 outp2 = p; 548 outpri2 = p->p_swtime; 549 } 550 continue; 551 552 case SSLEEP: 553 case SSTOP: 554 if (p->p_slptime >= maxslp) { 555 uvm_swapout(p); 556 didswap++; 557 } else if (p->p_slptime > outpri) { 558 outp = p; 559 outpri = p->p_slptime; 560 } 561 continue; 562 } 563 } 564 proclist_unlock_read(); 565 566 /* 567 * If we didn't get rid of any real duds, toss out the next most 568 * likely sleeping/stopped or running candidate. We only do this 569 * if we are real low on memory since we don't gain much by doing 570 * it (USPACE bytes). 571 */ 572 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) { 573 if ((p = outp) == NULL) 574 p = outp2; 575 #ifdef DEBUG 576 if (swapdebug & SDB_SWAPOUT) 577 printf("swapout_threads: no duds, try procp %p\n", p); 578 #endif 579 if (p) 580 uvm_swapout(p); 581 } 582 } 583 584 /* 585 * uvm_swapout: swap out process "p" 586 * 587 * - currently "swapout" means "unwire U-area" and "pmap_collect()" 588 * the pmap. 589 * - XXXCDC: should deactivate all process' private anonymous memory 590 */ 591 592 static void 593 uvm_swapout(p) 594 struct proc *p; 595 { 596 vaddr_t addr; 597 int s; 598 599 #ifdef DEBUG 600 if (swapdebug & SDB_SWAPOUT) 601 printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n", 602 p->p_pid, p->p_comm, p->p_addr, p->p_stat, 603 p->p_slptime, uvmexp.free); 604 #endif 605 606 /* 607 * Do any machine-specific actions necessary before swapout. 608 * This can include saving floating point state, etc. 609 */ 610 cpu_swapout(p); 611 612 /* 613 * Mark it as (potentially) swapped out. 614 */ 615 SCHED_LOCK(s); 616 p->p_flag &= ~P_INMEM; 617 if (p->p_stat == SRUN) 618 remrunqueue(p); 619 SCHED_UNLOCK(s); 620 p->p_swtime = 0; 621 p->p_stats->p_ru.ru_nswap++; 622 ++uvmexp.swapouts; 623 624 /* 625 * Unwire the to-be-swapped process's user struct and kernel stack. 626 */ 627 addr = (vaddr_t)p->p_addr; 628 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */ 629 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map)); 630 } 631 632 /* 633 * uvm_coredump_walkmap: walk a process's map for the purpose of dumping 634 * a core file. 635 */ 636 637 int 638 uvm_coredump_walkmap(p, vp, cred, func, cookie) 639 struct proc *p; 640 struct vnode *vp; 641 struct ucred *cred; 642 int (*func)(struct proc *, struct vnode *, struct ucred *, 643 struct uvm_coredump_state *); 644 void *cookie; 645 { 646 struct uvm_coredump_state state; 647 struct vmspace *vm = p->p_vmspace; 648 struct vm_map *map = &vm->vm_map; 649 struct vm_map_entry *entry; 650 vaddr_t maxstack; 651 int error; 652 653 maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize)); 654 655 for (entry = map->header.next; entry != &map->header; 656 entry = entry->next) { 657 /* Should never happen for a user process. */ 658 if (UVM_ET_ISSUBMAP(entry)) 659 panic("uvm_coredump_walkmap: user process with " 660 "submap?"); 661 662 state.cookie = cookie; 663 state.start = entry->start; 664 state.end = entry->end; 665 state.prot = entry->protection; 666 state.flags = 0; 667 668 if (state.start >= VM_MAXUSER_ADDRESS) 669 continue; 670 671 if (state.end > VM_MAXUSER_ADDRESS) 672 state.end = VM_MAXUSER_ADDRESS; 673 674 if (state.start >= (vaddr_t)vm->vm_maxsaddr) { 675 if (state.end <= maxstack) 676 continue; 677 if (state.start < maxstack) 678 state.start = maxstack; 679 state.flags |= UVM_COREDUMP_STACK; 680 } 681 682 if ((entry->protection & VM_PROT_WRITE) == 0) 683 state.flags |= UVM_COREDUMP_NODUMP; 684 685 if (entry->object.uvm_obj != NULL && 686 entry->object.uvm_obj->pgops == &uvm_deviceops) 687 state.flags |= UVM_COREDUMP_NODUMP; 688 689 error = (*func)(p, vp, cred, &state); 690 if (error) 691 return (error); 692 } 693 694 return (0); 695 } 696