1 /* $NetBSD: uvm_glue.c,v 1.50 2001/06/02 18:09:26 chs 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 "opt_kgdb.h" 70 #include "opt_sysv.h" 71 #include "opt_uvmhist.h" 72 73 /* 74 * uvm_glue.c: glue functions 75 */ 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/proc.h> 80 #include <sys/resourcevar.h> 81 #include <sys/buf.h> 82 #include <sys/user.h> 83 #ifdef SYSVSHM 84 #include <sys/shm.h> 85 #endif 86 87 #include <uvm/uvm.h> 88 89 #include <machine/cpu.h> 90 91 /* 92 * local prototypes 93 */ 94 95 static void uvm_swapout __P((struct proc *)); 96 97 /* 98 * XXXCDC: do these really belong here? 99 */ 100 101 int readbuffers = 0; /* allow KGDB to read kern buffer pool */ 102 /* XXX: see uvm_kernacc */ 103 104 105 /* 106 * uvm_kernacc: can the kernel access a region of memory 107 * 108 * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c) 109 */ 110 111 boolean_t 112 uvm_kernacc(addr, len, rw) 113 caddr_t addr; 114 size_t len; 115 int rw; 116 { 117 boolean_t rv; 118 vaddr_t saddr, eaddr; 119 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 120 121 saddr = trunc_page((vaddr_t)addr); 122 eaddr = round_page((vaddr_t)addr + len); 123 vm_map_lock_read(kernel_map); 124 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot); 125 vm_map_unlock_read(kernel_map); 126 127 /* 128 * XXX there are still some things (e.g. the buffer cache) that 129 * are managed behind the VM system's back so even though an 130 * address is accessible in the mind of the VM system, there may 131 * not be physical pages where the VM thinks there is. This can 132 * lead to bogus allocation of pages in the kernel address space 133 * or worse, inconsistencies at the pmap level. We only worry 134 * about the buffer cache for now. 135 */ 136 if (!readbuffers && rv && (eaddr > (vaddr_t)buffers && 137 saddr < (vaddr_t)buffers + MAXBSIZE * nbuf)) 138 rv = FALSE; 139 return(rv); 140 } 141 142 /* 143 * uvm_useracc: can the user access it? 144 * 145 * - called from physio() and sys___sysctl(). 146 */ 147 148 boolean_t 149 uvm_useracc(addr, len, rw) 150 caddr_t addr; 151 size_t len; 152 int rw; 153 { 154 struct vm_map *map; 155 boolean_t rv; 156 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 157 158 /* XXX curproc */ 159 map = &curproc->p_vmspace->vm_map; 160 161 vm_map_lock_read(map); 162 rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr), 163 round_page((vaddr_t)addr + len), prot); 164 vm_map_unlock_read(map); 165 166 return(rv); 167 } 168 169 #ifdef KGDB 170 /* 171 * Change protections on kernel pages from addr to addr+len 172 * (presumably so debugger can plant a breakpoint). 173 * 174 * We force the protection change at the pmap level. If we were 175 * to use vm_map_protect a change to allow writing would be lazily- 176 * applied meaning we would still take a protection fault, something 177 * we really don't want to do. It would also fragment the kernel 178 * map unnecessarily. We cannot use pmap_protect since it also won't 179 * enforce a write-enable request. Using pmap_enter is the only way 180 * we can ensure the change takes place properly. 181 */ 182 void 183 uvm_chgkprot(addr, len, rw) 184 caddr_t addr; 185 size_t len; 186 int rw; 187 { 188 vm_prot_t prot; 189 paddr_t pa; 190 vaddr_t sva, eva; 191 192 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE; 193 eva = round_page((vaddr_t)addr + len); 194 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) { 195 /* 196 * Extract physical address for the page. 197 */ 198 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE) 199 panic("chgkprot: invalid page"); 200 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 201 } 202 pmap_update(); 203 } 204 #endif 205 206 /* 207 * vslock: wire user memory for I/O 208 * 209 * - called from physio and sys___sysctl 210 * - XXXCDC: consider nuking this (or making it a macro?) 211 */ 212 213 int 214 uvm_vslock(p, addr, len, access_type) 215 struct proc *p; 216 caddr_t addr; 217 size_t len; 218 vm_prot_t access_type; 219 { 220 struct vm_map *map; 221 vaddr_t start, end; 222 int error; 223 224 map = &p->p_vmspace->vm_map; 225 start = trunc_page((vaddr_t)addr); 226 end = round_page((vaddr_t)addr + len); 227 error = uvm_fault_wire(map, start, end, access_type); 228 return error; 229 } 230 231 /* 232 * vslock: wire user memory for I/O 233 * 234 * - called from physio and sys___sysctl 235 * - XXXCDC: consider nuking this (or making it a macro?) 236 */ 237 238 void 239 uvm_vsunlock(p, addr, len) 240 struct proc *p; 241 caddr_t addr; 242 size_t len; 243 { 244 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr), 245 round_page((vaddr_t)addr + len)); 246 } 247 248 /* 249 * uvm_fork: fork a virtual address space 250 * 251 * - the address space is copied as per parent map's inherit values 252 * - a new "user" structure is allocated for the child process 253 * [filled in by MD layer...] 254 * - if specified, the child gets a new user stack described by 255 * stack and stacksize 256 * - NOTE: the kernel stack may be at a different location in the child 257 * process, and thus addresses of automatic variables may be invalid 258 * after cpu_fork returns in the child process. We do nothing here 259 * after cpu_fork returns. 260 * - XXXCDC: we need a way for this to return a failure value rather 261 * than just hang 262 */ 263 void 264 uvm_fork(p1, p2, shared, stack, stacksize, func, arg) 265 struct proc *p1, *p2; 266 boolean_t shared; 267 void *stack; 268 size_t stacksize; 269 void (*func) __P((void *)); 270 void *arg; 271 { 272 struct user *up = p2->p_addr; 273 int error; 274 275 if (shared == TRUE) { 276 p2->p_vmspace = NULL; 277 uvmspace_share(p1, p2); /* share vmspace */ 278 } else 279 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */ 280 281 /* 282 * Wire down the U-area for the process, which contains the PCB 283 * and the kernel stack. Wired state is stored in p->p_flag's 284 * P_INMEM bit rather than in the vm_map_entry's wired count 285 * to prevent kernel_map fragmentation. 286 * 287 * Note the kernel stack gets read/write accesses right off 288 * the bat. 289 */ 290 error = uvm_fault_wire(kernel_map, (vaddr_t)up, 291 (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE); 292 if (error) 293 panic("uvm_fork: uvm_fault_wire failed: %d", error); 294 295 /* 296 * p_stats currently points at a field in the user struct. Copy 297 * parts of p_stats, and zero out the rest. 298 */ 299 p2->p_stats = &up->u_stats; 300 memset(&up->u_stats.pstat_startzero, 0, 301 ((caddr_t)&up->u_stats.pstat_endzero - 302 (caddr_t)&up->u_stats.pstat_startzero)); 303 memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy, 304 ((caddr_t)&up->u_stats.pstat_endcopy - 305 (caddr_t)&up->u_stats.pstat_startcopy)); 306 307 /* 308 * cpu_fork() copy and update the pcb, and make the child ready 309 * to run. If this is a normal user fork, the child will exit 310 * directly to user mode via child_return() on its first time 311 * slice and will not return here. If this is a kernel thread, 312 * the specified entry point will be executed. 313 */ 314 cpu_fork(p1, p2, stack, stacksize, func, arg); 315 } 316 317 /* 318 * uvm_exit: exit a virtual address space 319 * 320 * - the process passed to us is a dead (pre-zombie) process; we 321 * are running on a different context now (the reaper). 322 * - we must run in a separate thread because freeing the vmspace 323 * of the dead process may block. 324 */ 325 void 326 uvm_exit(p) 327 struct proc *p; 328 { 329 vaddr_t va = (vaddr_t)p->p_addr; 330 331 uvmspace_free(p->p_vmspace); 332 p->p_flag &= ~P_INMEM; 333 uvm_fault_unwire(kernel_map, va, va + USPACE); 334 uvm_km_free(kernel_map, va, USPACE); 335 p->p_addr = NULL; 336 } 337 338 /* 339 * uvm_init_limit: init per-process VM limits 340 * 341 * - called for process 0 and then inherited by all others. 342 */ 343 void 344 uvm_init_limits(p) 345 struct proc *p; 346 { 347 348 /* 349 * Set up the initial limits on process VM. Set the maximum 350 * resident set size to be all of (reasonably) available memory. 351 * This causes any single, large process to start random page 352 * replacement once it fills memory. 353 */ 354 355 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 356 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 357 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 358 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 359 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free); 360 } 361 362 #ifdef DEBUG 363 int enableswap = 1; 364 int swapdebug = 0; 365 #define SDB_FOLLOW 1 366 #define SDB_SWAPIN 2 367 #define SDB_SWAPOUT 4 368 #endif 369 370 /* 371 * uvm_swapin: swap in a process's u-area. 372 */ 373 374 void 375 uvm_swapin(p) 376 struct proc *p; 377 { 378 vaddr_t addr; 379 int s; 380 381 addr = (vaddr_t)p->p_addr; 382 /* make P_INMEM true */ 383 uvm_fault_wire(kernel_map, addr, addr + USPACE, 384 VM_PROT_READ | VM_PROT_WRITE); 385 386 /* 387 * Some architectures need to be notified when the user area has 388 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c). 389 */ 390 cpu_swapin(p); 391 SCHED_LOCK(s); 392 if (p->p_stat == SRUN) 393 setrunqueue(p); 394 p->p_flag |= P_INMEM; 395 SCHED_UNLOCK(s); 396 p->p_swtime = 0; 397 ++uvmexp.swapins; 398 } 399 400 /* 401 * uvm_scheduler: process zero main loop 402 * 403 * - attempt to swapin every swaped-out, runnable process in order of 404 * priority. 405 * - if not enough memory, wake the pagedaemon and let it clear space. 406 */ 407 408 void 409 uvm_scheduler() 410 { 411 struct proc *p; 412 int pri; 413 struct proc *pp; 414 int ppri; 415 416 loop: 417 #ifdef DEBUG 418 while (!enableswap) 419 tsleep(&proc0, PVM, "noswap", 0); 420 #endif 421 pp = NULL; /* process to choose */ 422 ppri = INT_MIN; /* its priority */ 423 proclist_lock_read(); 424 LIST_FOREACH(p, &allproc, p_list) { 425 426 /* is it a runnable swapped out process? */ 427 if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) { 428 pri = p->p_swtime + p->p_slptime - 429 (p->p_nice - NZERO) * 8; 430 if (pri > ppri) { /* higher priority? remember it. */ 431 pp = p; 432 ppri = pri; 433 } 434 } 435 } 436 /* 437 * XXXSMP: possible unlock/sleep race between here and the 438 * "scheduler" tsleep below.. 439 */ 440 proclist_unlock_read(); 441 442 #ifdef DEBUG 443 if (swapdebug & SDB_FOLLOW) 444 printf("scheduler: running, procp %p pri %d\n", pp, ppri); 445 #endif 446 /* 447 * Nothing to do, back to sleep 448 */ 449 if ((p = pp) == NULL) { 450 tsleep(&proc0, PVM, "scheduler", 0); 451 goto loop; 452 } 453 454 /* 455 * we have found swapped out process which we would like to bring 456 * back in. 457 * 458 * XXX: this part is really bogus cuz we could deadlock on memory 459 * despite our feeble check 460 */ 461 if (uvmexp.free > atop(USPACE)) { 462 #ifdef DEBUG 463 if (swapdebug & SDB_SWAPIN) 464 printf("swapin: pid %d(%s)@%p, pri %d free %d\n", 465 p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free); 466 #endif 467 uvm_swapin(p); 468 goto loop; 469 } 470 /* 471 * not enough memory, jab the pageout daemon and wait til the coast 472 * is clear 473 */ 474 #ifdef DEBUG 475 if (swapdebug & SDB_FOLLOW) 476 printf("scheduler: no room for pid %d(%s), free %d\n", 477 p->p_pid, p->p_comm, uvmexp.free); 478 #endif 479 uvm_wait("schedpwait"); 480 #ifdef DEBUG 481 if (swapdebug & SDB_FOLLOW) 482 printf("scheduler: room again, free %d\n", uvmexp.free); 483 #endif 484 goto loop; 485 } 486 487 /* 488 * swappable: is process "p" swappable? 489 */ 490 491 #define swappable(p) \ 492 (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM && \ 493 (p)->p_holdcnt == 0) 494 495 /* 496 * swapout_threads: find threads that can be swapped and unwire their 497 * u-areas. 498 * 499 * - called by the pagedaemon 500 * - try and swap at least one processs 501 * - processes that are sleeping or stopped for maxslp or more seconds 502 * are swapped... otherwise the longest-sleeping or stopped process 503 * is swapped, otherwise the longest resident process... 504 */ 505 void 506 uvm_swapout_threads() 507 { 508 struct proc *p; 509 struct proc *outp, *outp2; 510 int outpri, outpri2; 511 int didswap = 0; 512 extern int maxslp; 513 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */ 514 515 #ifdef DEBUG 516 if (!enableswap) 517 return; 518 #endif 519 520 /* 521 * outp/outpri : stop/sleep process with largest sleeptime < maxslp 522 * outp2/outpri2: the longest resident process (its swap time) 523 */ 524 outp = outp2 = NULL; 525 outpri = outpri2 = 0; 526 proclist_lock_read(); 527 LIST_FOREACH(p, &allproc, p_list) { 528 if (!swappable(p)) 529 continue; 530 switch (p->p_stat) { 531 case SRUN: 532 case SONPROC: 533 if (p->p_swtime > outpri2) { 534 outp2 = p; 535 outpri2 = p->p_swtime; 536 } 537 continue; 538 539 case SSLEEP: 540 case SSTOP: 541 if (p->p_slptime >= maxslp) { 542 uvm_swapout(p); 543 didswap++; 544 } else if (p->p_slptime > outpri) { 545 outp = p; 546 outpri = p->p_slptime; 547 } 548 continue; 549 } 550 } 551 proclist_unlock_read(); 552 553 /* 554 * If we didn't get rid of any real duds, toss out the next most 555 * likely sleeping/stopped or running candidate. We only do this 556 * if we are real low on memory since we don't gain much by doing 557 * it (USPACE bytes). 558 */ 559 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) { 560 if ((p = outp) == NULL) 561 p = outp2; 562 #ifdef DEBUG 563 if (swapdebug & SDB_SWAPOUT) 564 printf("swapout_threads: no duds, try procp %p\n", p); 565 #endif 566 if (p) 567 uvm_swapout(p); 568 } 569 } 570 571 /* 572 * uvm_swapout: swap out process "p" 573 * 574 * - currently "swapout" means "unwire U-area" and "pmap_collect()" 575 * the pmap. 576 * - XXXCDC: should deactivate all process' private anonymous memory 577 */ 578 579 static void 580 uvm_swapout(p) 581 struct proc *p; 582 { 583 vaddr_t addr; 584 int s; 585 586 #ifdef DEBUG 587 if (swapdebug & SDB_SWAPOUT) 588 printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n", 589 p->p_pid, p->p_comm, p->p_addr, p->p_stat, 590 p->p_slptime, uvmexp.free); 591 #endif 592 593 /* 594 * Do any machine-specific actions necessary before swapout. 595 * This can include saving floating point state, etc. 596 */ 597 cpu_swapout(p); 598 599 /* 600 * Mark it as (potentially) swapped out. 601 */ 602 SCHED_LOCK(s); 603 p->p_flag &= ~P_INMEM; 604 if (p->p_stat == SRUN) 605 remrunqueue(p); 606 SCHED_UNLOCK(s); 607 p->p_swtime = 0; 608 ++uvmexp.swapouts; 609 610 /* 611 * Unwire the to-be-swapped process's user struct and kernel stack. 612 */ 613 addr = (vaddr_t)p->p_addr; 614 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */ 615 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map)); 616 } 617 618