1 /* $NetBSD: uvm_glue.c,v 1.30 1999/11/13 00:24:38 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(addr); 153 eaddr = round_page(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(addr), round_page(addr+len), 194 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 register 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(addr + len); 225 for (sva = trunc_page(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(addr); 259 end = round_page(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(addr), 280 round_page(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) 300 struct proc *p1, *p2; 301 boolean_t shared; 302 void *stack; 303 size_t stacksize; 304 { 305 struct user *up = p2->p_addr; 306 int rv; 307 308 if (shared == TRUE) 309 uvmspace_share(p1, p2); /* share vmspace */ 310 else 311 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */ 312 313 /* 314 * Wire down the U-area for the process, which contains the PCB 315 * and the kernel stack. Wired state is stored in p->p_flag's 316 * P_INMEM bit rather than in the vm_map_entry's wired count 317 * to prevent kernel_map fragmentation. 318 * 319 * Note the kernel stack gets read/write accesses right off 320 * the bat. 321 */ 322 rv = uvm_fault_wire(kernel_map, (vaddr_t)up, 323 (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE); 324 if (rv != KERN_SUCCESS) 325 panic("uvm_fork: uvm_fault_wire failed: %d", rv); 326 327 /* 328 * p_stats currently points at a field in the user struct. Copy 329 * parts of p_stats, and zero out the rest. 330 */ 331 p2->p_stats = &up->u_stats; 332 memset(&up->u_stats.pstat_startzero, 0, 333 (unsigned) ((caddr_t)&up->u_stats.pstat_endzero - 334 (caddr_t)&up->u_stats.pstat_startzero)); 335 memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy, 336 ((caddr_t)&up->u_stats.pstat_endcopy - 337 (caddr_t)&up->u_stats.pstat_startcopy)); 338 339 /* 340 * cpu_fork will copy and update the kernel stack and pcb, and make 341 * the child ready to run. The child will exit directly to user 342 * mode on its first time slice, and will not return here. 343 */ 344 cpu_fork(p1, p2, stack, stacksize); 345 } 346 347 /* 348 * uvm_exit: exit a virtual address space 349 * 350 * - the process passed to us is a dead (pre-zombie) process; we 351 * are running on a different context now (the reaper). 352 * - we must run in a separate thread because freeing the vmspace 353 * of the dead process may block. 354 */ 355 void 356 uvm_exit(p) 357 struct proc *p; 358 { 359 360 uvmspace_free(p->p_vmspace); 361 uvm_km_free(kernel_map, (vaddr_t)p->p_addr, USPACE); 362 } 363 364 /* 365 * uvm_init_limit: init per-process VM limits 366 * 367 * - called for process 0 and then inherited by all others. 368 */ 369 void 370 uvm_init_limits(p) 371 struct proc *p; 372 { 373 374 /* 375 * Set up the initial limits on process VM. Set the maximum 376 * resident set size to be all of (reasonably) available memory. 377 * This causes any single, large process to start random page 378 * replacement once it fills memory. 379 */ 380 381 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 382 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 383 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 384 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 385 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free); 386 } 387 388 #ifdef DEBUG 389 int enableswap = 1; 390 int swapdebug = 0; 391 #define SDB_FOLLOW 1 392 #define SDB_SWAPIN 2 393 #define SDB_SWAPOUT 4 394 #endif 395 396 /* 397 * uvm_swapin: swap in a process's u-area. 398 */ 399 400 void 401 uvm_swapin(p) 402 struct proc *p; 403 { 404 vaddr_t addr; 405 int s; 406 407 addr = (vaddr_t)p->p_addr; 408 /* make P_INMEM true */ 409 uvm_fault_wire(kernel_map, addr, addr + USPACE, 410 VM_PROT_READ | VM_PROT_WRITE); 411 412 /* 413 * Some architectures need to be notified when the user area has 414 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c). 415 */ 416 cpu_swapin(p); 417 s = splstatclock(); 418 if (p->p_stat == SRUN) 419 setrunqueue(p); 420 p->p_flag |= P_INMEM; 421 splx(s); 422 p->p_swtime = 0; 423 ++uvmexp.swapins; 424 } 425 426 /* 427 * uvm_scheduler: process zero main loop 428 * 429 * - attempt to swapin every swaped-out, runnable process in order of 430 * priority. 431 * - if not enough memory, wake the pagedaemon and let it clear space. 432 */ 433 434 void 435 uvm_scheduler() 436 { 437 register struct proc *p; 438 register int pri; 439 struct proc *pp; 440 int ppri; 441 UVMHIST_FUNC("uvm_scheduler"); UVMHIST_CALLED(maphist); 442 443 loop: 444 #ifdef DEBUG 445 while (!enableswap) 446 tsleep((caddr_t)&proc0, PVM, "noswap", 0); 447 #endif 448 pp = NULL; /* process to choose */ 449 ppri = INT_MIN; /* its priority */ 450 proclist_lock_read(); 451 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 452 453 /* is it a runnable swapped out process? */ 454 if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) { 455 pri = p->p_swtime + p->p_slptime - 456 (p->p_nice - NZERO) * 8; 457 if (pri > ppri) { /* higher priority? remember it. */ 458 pp = p; 459 ppri = pri; 460 } 461 } 462 } 463 proclist_unlock_read(); 464 465 #ifdef DEBUG 466 if (swapdebug & SDB_FOLLOW) 467 printf("scheduler: running, procp %p pri %d\n", pp, ppri); 468 #endif 469 /* 470 * Nothing to do, back to sleep 471 */ 472 if ((p = pp) == NULL) { 473 tsleep((caddr_t)&proc0, PVM, "scheduler", 0); 474 goto loop; 475 } 476 477 /* 478 * we have found swapped out process which we would like to bring 479 * back in. 480 * 481 * XXX: this part is really bogus cuz we could deadlock on memory 482 * despite our feeble check 483 */ 484 if (uvmexp.free > atop(USPACE)) { 485 #ifdef DEBUG 486 if (swapdebug & SDB_SWAPIN) 487 printf("swapin: pid %d(%s)@%p, pri %d free %d\n", 488 p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free); 489 #endif 490 uvm_swapin(p); 491 goto loop; 492 } 493 /* 494 * not enough memory, jab the pageout daemon and wait til the coast 495 * is clear 496 */ 497 #ifdef DEBUG 498 if (swapdebug & SDB_FOLLOW) 499 printf("scheduler: no room for pid %d(%s), free %d\n", 500 p->p_pid, p->p_comm, uvmexp.free); 501 #endif 502 (void) splhigh(); 503 uvm_wait("schedpwait"); 504 (void) spl0(); 505 #ifdef DEBUG 506 if (swapdebug & SDB_FOLLOW) 507 printf("scheduler: room again, free %d\n", uvmexp.free); 508 #endif 509 goto loop; 510 } 511 512 /* 513 * swappable: is process "p" swappable? 514 */ 515 516 #define swappable(p) \ 517 (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM && \ 518 (p)->p_holdcnt == 0) 519 520 /* 521 * swapout_threads: find threads that can be swapped and unwire their 522 * u-areas. 523 * 524 * - called by the pagedaemon 525 * - try and swap at least one processs 526 * - processes that are sleeping or stopped for maxslp or more seconds 527 * are swapped... otherwise the longest-sleeping or stopped process 528 * is swapped, otherwise the longest resident process... 529 */ 530 void 531 uvm_swapout_threads() 532 { 533 register struct proc *p; 534 struct proc *outp, *outp2; 535 int outpri, outpri2; 536 int didswap = 0; 537 extern int maxslp; 538 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */ 539 540 #ifdef DEBUG 541 if (!enableswap) 542 return; 543 #endif 544 545 /* 546 * outp/outpri : stop/sleep process with largest sleeptime < maxslp 547 * outp2/outpri2: the longest resident process (its swap time) 548 */ 549 outp = outp2 = NULL; 550 outpri = outpri2 = 0; 551 proclist_lock_read(); 552 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 553 if (!swappable(p)) 554 continue; 555 switch (p->p_stat) { 556 case SRUN: 557 if (p->p_swtime > outpri2) { 558 outp2 = p; 559 outpri2 = p->p_swtime; 560 } 561 continue; 562 563 case SSLEEP: 564 case SSTOP: 565 if (p->p_slptime >= maxslp) { 566 uvm_swapout(p); /* zap! */ 567 didswap++; 568 } else if (p->p_slptime > outpri) { 569 outp = p; 570 outpri = p->p_slptime; 571 } 572 continue; 573 } 574 } 575 proclist_unlock_read(); 576 577 /* 578 * If we didn't get rid of any real duds, toss out the next most 579 * likely sleeping/stopped or running candidate. We only do this 580 * if we are real low on memory since we don't gain much by doing 581 * it (USPACE bytes). 582 */ 583 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) { 584 if ((p = outp) == NULL) 585 p = outp2; 586 #ifdef DEBUG 587 if (swapdebug & SDB_SWAPOUT) 588 printf("swapout_threads: no duds, try procp %p\n", p); 589 #endif 590 if (p) 591 uvm_swapout(p); 592 } 593 } 594 595 /* 596 * uvm_swapout: swap out process "p" 597 * 598 * - currently "swapout" means "unwire U-area" and "pmap_collect()" 599 * the pmap. 600 * - XXXCDC: should deactivate all process' private anonymous memory 601 */ 602 603 static void 604 uvm_swapout(p) 605 register struct proc *p; 606 { 607 vaddr_t addr; 608 int s; 609 610 #ifdef DEBUG 611 if (swapdebug & SDB_SWAPOUT) 612 printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n", 613 p->p_pid, p->p_comm, p->p_addr, p->p_stat, 614 p->p_slptime, uvmexp.free); 615 #endif 616 617 /* 618 * Do any machine-specific actions necessary before swapout. 619 * This can include saving floating point state, etc. 620 */ 621 cpu_swapout(p); 622 623 /* 624 * Unwire the to-be-swapped process's user struct and kernel stack. 625 */ 626 addr = (vaddr_t)p->p_addr; 627 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */ 628 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map)); 629 630 /* 631 * Mark it as (potentially) swapped out. 632 */ 633 s = splstatclock(); 634 p->p_flag &= ~P_INMEM; 635 if (p->p_stat == SRUN) 636 remrunqueue(p); 637 splx(s); 638 p->p_swtime = 0; 639 ++uvmexp.swapouts; 640 } 641 642