1 /* $OpenBSD: uvm_glue.c,v 1.19 2001/08/11 10:57:22 art Exp $ */ 2 /* $NetBSD: uvm_glue.c,v 1.36 2000/06/18 05:20:27 simonb 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 <vm/vm.h> 85 #include <vm/vm_page.h> 86 #include <vm/vm_kern.h> 87 88 #include <uvm/uvm.h> 89 90 #include <machine/cpu.h> 91 92 /* 93 * local prototypes 94 */ 95 96 static void uvm_swapout __P((struct proc *)); 97 98 /* 99 * XXXCDC: do these really belong here? 100 */ 101 102 unsigned maxdmap = MAXDSIZ; /* kern_resource.c: RLIMIT_DATA max */ 103 unsigned maxsmap = MAXSSIZ; /* kern_resource.c: RLIMIT_STACK max */ 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 vm_map_t 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 * We use a cheezy hack to differentiate physical 202 * page 0 from an invalid mapping, not that it 203 * really matters... 204 */ 205 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE) 206 panic("chgkprot: invalid page"); 207 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 208 } 209 } 210 #endif 211 212 /* 213 * vslock: wire user memory for I/O 214 * 215 * - called from physio and sys___sysctl 216 * - XXXCDC: consider nuking this (or making it a macro?) 217 */ 218 219 int 220 uvm_vslock(p, addr, len, access_type) 221 struct proc *p; 222 caddr_t addr; 223 size_t len; 224 vm_prot_t access_type; 225 { 226 vm_map_t map; 227 vaddr_t start, end; 228 int rv; 229 230 map = &p->p_vmspace->vm_map; 231 start = trunc_page((vaddr_t)addr); 232 end = round_page((vaddr_t)addr + len); 233 234 rv = uvm_fault_wire(map, start, end, access_type); 235 236 return (rv); 237 } 238 239 /* 240 * vslock: wire user memory for I/O 241 * 242 * - called from physio and sys___sysctl 243 * - XXXCDC: consider nuking this (or making it a macro?) 244 */ 245 246 void 247 uvm_vsunlock(p, addr, len) 248 struct proc *p; 249 caddr_t addr; 250 size_t len; 251 { 252 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr), 253 round_page((vaddr_t)addr+len)); 254 } 255 256 /* 257 * uvm_fork: fork a virtual address space 258 * 259 * - the address space is copied as per parent map's inherit values 260 * - a new "user" structure is allocated for the child process 261 * [filled in by MD layer...] 262 * - if specified, the child gets a new user stack described by 263 * stack and stacksize 264 * - NOTE: the kernel stack may be at a different location in the child 265 * process, and thus addresses of automatic variables may be invalid 266 * after cpu_fork returns in the child process. We do nothing here 267 * after cpu_fork returns. 268 * - XXXCDC: we need a way for this to return a failure value rather 269 * than just hang 270 */ 271 void 272 uvm_fork(p1, p2, shared, stack, stacksize) 273 struct proc *p1, *p2; 274 boolean_t shared; 275 void *stack; 276 size_t stacksize; 277 { 278 struct user *up = p2->p_addr; 279 int rv; 280 281 if (shared == TRUE) 282 uvmspace_share(p1, p2); /* share vmspace */ 283 else 284 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */ 285 286 /* 287 * Wire down the U-area for the process, which contains the PCB 288 * and the kernel stack. Wired state is stored in p->p_flag's 289 * P_INMEM bit rather than in the vm_map_entry's wired count 290 * to prevent kernel_map fragmentation. 291 * 292 * Note the kernel stack gets read/write accesses right off 293 * the bat. 294 */ 295 rv = uvm_fault_wire(kernel_map, (vaddr_t)up, 296 (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE); 297 if (rv != KERN_SUCCESS) 298 panic("uvm_fork: uvm_fault_wire failed: %d", rv); 299 300 /* 301 * p_stats currently points at a field in the user struct. Copy 302 * parts of p_stats, and zero out the rest. 303 */ 304 p2->p_stats = &up->u_stats; 305 memset(&up->u_stats.pstat_startzero, 0, 306 (unsigned) ((caddr_t)&up->u_stats.pstat_endzero - 307 (caddr_t)&up->u_stats.pstat_startzero)); 308 memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy, 309 ((caddr_t)&up->u_stats.pstat_endcopy - 310 (caddr_t)&up->u_stats.pstat_startcopy)); 311 312 /* 313 * cpu_fork will copy and update the kernel stack and pcb, and make 314 * the child ready to run. The child will exit directly to user 315 * mode on its first time slice, and will not return here. 316 */ 317 cpu_fork(p1, p2, stack, stacksize); 318 } 319 320 /* 321 * uvm_exit: exit a virtual address space 322 * 323 * - the process passed to us is a dead (pre-zombie) process; we 324 * are running on a different context now (the reaper). 325 * - we must run in a separate thread because freeing the vmspace 326 * of the dead process may block. 327 */ 328 void 329 uvm_exit(p) 330 struct proc *p; 331 { 332 333 uvmspace_free(p->p_vmspace); 334 uvm_km_free(kernel_map, (vaddr_t)p->p_addr, 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 s = splstatclock(); 392 if (p->p_stat == SRUN) 393 setrunqueue(p); 394 p->p_flag |= P_INMEM; 395 splx(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 UVMHIST_FUNC("uvm_scheduler"); UVMHIST_CALLED(maphist); 416 417 loop: 418 #ifdef DEBUG 419 while (!enableswap) 420 tsleep((caddr_t)&proc0, PVM, "noswap", 0); 421 #endif 422 pp = NULL; /* process to choose */ 423 ppri = INT_MIN; /* its priority */ 424 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 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 #ifdef DEBUG 438 if (swapdebug & SDB_FOLLOW) 439 printf("scheduler: running, procp %p pri %d\n", pp, ppri); 440 #endif 441 /* 442 * Nothing to do, back to sleep 443 */ 444 if ((p = pp) == NULL) { 445 tsleep((caddr_t)&proc0, PVM, "scheduler", 0); 446 goto loop; 447 } 448 449 /* 450 * we have found swapped out process which we would like to bring 451 * back in. 452 * 453 * XXX: this part is really bogus cuz we could deadlock on memory 454 * despite our feeble check 455 */ 456 if (uvmexp.free > atop(USPACE)) { 457 #ifdef DEBUG 458 if (swapdebug & SDB_SWAPIN) 459 printf("swapin: pid %d(%s)@%p, pri %d free %d\n", 460 p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free); 461 #endif 462 uvm_swapin(p); 463 goto loop; 464 } 465 /* 466 * not enough memory, jab the pageout daemon and wait til the coast 467 * is clear 468 */ 469 #ifdef DEBUG 470 if (swapdebug & SDB_FOLLOW) 471 printf("scheduler: no room for pid %d(%s), free %d\n", 472 p->p_pid, p->p_comm, uvmexp.free); 473 #endif 474 uvm_wait("schedpwait"); 475 #ifdef DEBUG 476 if (swapdebug & SDB_FOLLOW) 477 printf("scheduler: room again, free %d\n", uvmexp.free); 478 #endif 479 goto loop; 480 } 481 482 /* 483 * swappable: is process "p" swappable? 484 */ 485 486 #define swappable(p) \ 487 (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM && \ 488 (p)->p_holdcnt == 0) 489 490 /* 491 * swapout_threads: find threads that can be swapped and unwire their 492 * u-areas. 493 * 494 * - called by the pagedaemon 495 * - try and swap at least one processs 496 * - processes that are sleeping or stopped for maxslp or more seconds 497 * are swapped... otherwise the longest-sleeping or stopped process 498 * is swapped, otherwise the longest resident process... 499 */ 500 void 501 uvm_swapout_threads() 502 { 503 struct proc *p; 504 struct proc *outp, *outp2; 505 int outpri, outpri2; 506 int didswap = 0; 507 extern int maxslp; 508 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */ 509 510 #ifdef DEBUG 511 if (!enableswap) 512 return; 513 #endif 514 515 /* 516 * outp/outpri : stop/sleep process with largest sleeptime < maxslp 517 * outp2/outpri2: the longest resident process (its swap time) 518 */ 519 outp = outp2 = NULL; 520 outpri = outpri2 = 0; 521 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 522 if (!swappable(p)) 523 continue; 524 switch (p->p_stat) { 525 case SRUN: 526 if (p->p_swtime > outpri2) { 527 outp2 = p; 528 outpri2 = p->p_swtime; 529 } 530 continue; 531 532 case SSLEEP: 533 case SSTOP: 534 if (p->p_slptime >= maxslp) { 535 uvm_swapout(p); /* zap! */ 536 didswap++; 537 } else if (p->p_slptime > outpri) { 538 outp = p; 539 outpri = p->p_slptime; 540 } 541 continue; 542 } 543 } 544 545 /* 546 * If we didn't get rid of any real duds, toss out the next most 547 * likely sleeping/stopped or running candidate. We only do this 548 * if we are real low on memory since we don't gain much by doing 549 * it (USPACE bytes). 550 */ 551 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) { 552 if ((p = outp) == NULL) 553 p = outp2; 554 #ifdef DEBUG 555 if (swapdebug & SDB_SWAPOUT) 556 printf("swapout_threads: no duds, try procp %p\n", p); 557 #endif 558 if (p) 559 uvm_swapout(p); 560 } 561 } 562 563 /* 564 * uvm_swapout: swap out process "p" 565 * 566 * - currently "swapout" means "unwire U-area" and "pmap_collect()" 567 * the pmap. 568 * - XXXCDC: should deactivate all process' private anonymous memory 569 */ 570 571 static void 572 uvm_swapout(p) 573 struct proc *p; 574 { 575 vaddr_t addr; 576 int s; 577 578 #ifdef DEBUG 579 if (swapdebug & SDB_SWAPOUT) 580 printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n", 581 p->p_pid, p->p_comm, p->p_addr, p->p_stat, 582 p->p_slptime, uvmexp.free); 583 #endif 584 585 /* 586 * Do any machine-specific actions necessary before swapout. 587 * This can include saving floating point state, etc. 588 */ 589 cpu_swapout(p); 590 591 /* 592 * Unwire the to-be-swapped process's user struct and kernel stack. 593 */ 594 addr = (vaddr_t)p->p_addr; 595 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */ 596 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map)); 597 598 /* 599 * Mark it as (potentially) swapped out. 600 */ 601 s = splstatclock(); 602 p->p_flag &= ~P_INMEM; 603 if (p->p_stat == SRUN) 604 remrunqueue(p); 605 splx(s); 606 p->p_swtime = 0; 607 ++uvmexp.swapouts; 608 } 609 610