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