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