vm_glue.c revision 1817
1/* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * The Mach Operating System project at Carnegie-Mellon University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 37 * 38 * 39 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40 * All rights reserved. 41 * 42 * Permission to use, copy, modify and distribute this software and 43 * its documentation is hereby granted, provided that both the copyright 44 * notice and this permission notice appear in all copies of the 45 * software, derivative works or modified versions, and any portions 46 * thereof, and that both notices appear in supporting documentation. 47 * 48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51 * 52 * Carnegie Mellon requests users of this software to return to 53 * 54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55 * School of Computer Science 56 * Carnegie Mellon University 57 * Pittsburgh PA 15213-3890 58 * 59 * any improvements or extensions that they make and grant Carnegie the 60 * rights to redistribute these changes. 61 * 62 * $Id$ 63 */ 64 65#include <sys/param.h> 66#include <sys/systm.h> 67#include <sys/proc.h> 68#include <sys/resourcevar.h> 69#include <sys/buf.h> 70#include <sys/user.h> 71 72#include <sys/kernel.h> 73#include <sys/dkstat.h> 74 75#include <vm/vm.h> 76#include <vm/vm_page.h> 77#include <vm/vm_pageout.h> 78#include <vm/vm_kern.h> 79 80#include <machine/stdarg.h> 81 82extern char kstack[]; 83int avefree = 0; /* XXX */ 84int readbuffers = 0; /* XXX allow kgdb to read kernel buffer pool */ 85/* vm_map_t upages_map; */ 86 87void swapout(struct proc *p); 88int 89kernacc(addr, len, rw) 90 caddr_t addr; 91 int len, rw; 92{ 93 boolean_t rv; 94 vm_offset_t saddr, eaddr; 95 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 96 97 saddr = trunc_page(addr); 98 eaddr = round_page(addr+len); 99 rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 100 return(rv == TRUE); 101} 102 103int 104useracc(addr, len, rw) 105 caddr_t addr; 106 int len, rw; 107{ 108 boolean_t rv; 109 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 110 111 /* 112 * XXX - specially disallow access to user page tables - they are 113 * in the map. 114 * 115 * XXX - don't specially disallow access to the user area - treat 116 * it as incorrectly as elsewhere. 117 * 118 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. It was 119 * only used (as an end address) in trap.c. Use it as an end 120 * address here too. 121 */ 122 if ((vm_offset_t) addr >= VM_MAXUSER_ADDRESS 123 || (vm_offset_t) addr + len > VM_MAXUSER_ADDRESS 124 || (vm_offset_t) addr + len <= (vm_offset_t) addr) { 125 return (FALSE); 126 } 127 128 rv = vm_map_check_protection(&curproc->p_vmspace->vm_map, 129 trunc_page(addr), round_page(addr+len), prot); 130 return(rv == TRUE); 131} 132 133#ifdef KGDB 134/* 135 * Change protections on kernel pages from addr to addr+len 136 * (presumably so debugger can plant a breakpoint). 137 * All addresses are assumed to reside in the Sysmap, 138 */ 139chgkprot(addr, len, rw) 140 register caddr_t addr; 141 int len, rw; 142{ 143 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 144 145 vm_map_protect(kernel_map, trunc_page(addr), 146 round_page(addr+len), prot, FALSE); 147} 148#endif 149void 150vslock(addr, len) 151 caddr_t addr; 152 u_int len; 153{ 154 vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr), 155 round_page(addr+len), FALSE); 156} 157 158void 159vsunlock(addr, len, dirtied) 160 caddr_t addr; 161 u_int len; 162 int dirtied; 163{ 164#ifdef lint 165 dirtied++; 166#endif lint 167 vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr), 168 round_page(addr+len), TRUE); 169} 170 171/* 172 * Implement fork's actions on an address space. 173 * Here we arrange for the address space to be copied or referenced, 174 * allocate a user struct (pcb and kernel stack), then call the 175 * machine-dependent layer to fill those in and make the new process 176 * ready to run. 177 * NOTE: the kernel stack may be at a different location in the child 178 * process, and thus addresses of automatic variables may be invalid 179 * after cpu_fork returns in the child process. We do nothing here 180 * after cpu_fork returns. 181 */ 182int 183vm_fork(p1, p2, isvfork) 184 register struct proc *p1, *p2; 185 int isvfork; 186{ 187 register struct user *up; 188 vm_offset_t addr, ptaddr; 189 int i; 190 struct vm_map *vp; 191 192 while( cnt.v_free_count < cnt.v_free_min) 193 VM_WAIT; 194 195 /* 196 * avoid copying any of the parent's pagetables or other per-process 197 * objects that reside in the map by marking all of them non-inheritable 198 */ 199 (void)vm_map_inherit(&p1->p_vmspace->vm_map, 200 UPT_MIN_ADDRESS - UPAGES * NBPG, VM_MAX_ADDRESS, VM_INHERIT_NONE); 201 p2->p_vmspace = vmspace_fork(p1->p_vmspace); 202 203#ifdef SYSVSHM 204 if (p1->p_vmspace->vm_shm) 205 shmfork(p1, p2, isvfork); 206#endif 207 208 /* 209 * Allocate a wired-down (for now) pcb and kernel stack for the process 210 */ 211 212 addr = (vm_offset_t) kstack; 213 214 vp = &p2->p_vmspace->vm_map; 215 216 /* ream out old pagetables and kernel stack */ 217 (void)vm_deallocate(vp, addr, UPT_MAX_ADDRESS - addr); 218 219 /* get new pagetables and kernel stack */ 220 (void)vm_allocate(vp, &addr, UPT_MAX_ADDRESS - addr, FALSE); 221 222 /* force in the page table encompassing the UPAGES */ 223 ptaddr = trunc_page((u_int)vtopte(addr)); 224 vm_map_pageable(vp, ptaddr, ptaddr + NBPG, FALSE); 225 226 /* and force in (demand-zero) the UPAGES */ 227 vm_map_pageable(vp, addr, addr + UPAGES * NBPG, FALSE); 228 229 /* get a kernel virtual address for the UPAGES for this proc */ 230 up = (struct user *)kmem_alloc_pageable(kernel_map, UPAGES * NBPG); 231 232 /* and force-map the upages into the kernel pmap */ 233 for (i = 0; i < UPAGES; i++) 234 pmap_enter(vm_map_pmap(kernel_map), 235 ((vm_offset_t) up) + NBPG * i, 236 pmap_extract(vp->pmap, addr + NBPG * i), 237 VM_PROT_READ|VM_PROT_WRITE, 1); 238 239 /* and allow the UPAGES page table entry to be paged (at the vm system level) */ 240 vm_map_pageable(vp, ptaddr, ptaddr + NBPG, TRUE); 241 242 p2->p_addr = up; 243 244 /* 245 * p_stats and p_sigacts currently point at fields 246 * in the user struct but not at &u, instead at p_addr. 247 * Copy p_sigacts and parts of p_stats; zero the rest 248 * of p_stats (statistics). 249 */ 250 p2->p_stats = &up->u_stats; 251 p2->p_sigacts = &up->u_sigacts; 252 up->u_sigacts = *p1->p_sigacts; 253 bzero(&up->u_stats.pstat_startzero, 254 (unsigned) ((caddr_t)&up->u_stats.pstat_endzero - 255 (caddr_t)&up->u_stats.pstat_startzero)); 256 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 257 ((caddr_t)&up->u_stats.pstat_endcopy - 258 (caddr_t)&up->u_stats.pstat_startcopy)); 259 260 261 /* 262 * cpu_fork will copy and update the kernel stack and pcb, 263 * and make the child ready to run. It marks the child 264 * so that it can return differently than the parent. 265 * It returns twice, once in the parent process and 266 * once in the child. 267 */ 268 return (cpu_fork(p1, p2)); 269} 270 271/* 272 * Set default limits for VM system. 273 * Called for proc 0, and then inherited by all others. 274 */ 275void 276vm_init_limits(p) 277 register struct proc *p; 278{ 279 int tmp; 280 281 /* 282 * Set up the initial limits on process VM. 283 * Set the maximum resident set size to be all 284 * of (reasonably) available memory. This causes 285 * any single, large process to start random page 286 * replacement once it fills memory. 287 */ 288 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 289 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 290 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 291 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 292 tmp = ((2 * cnt.v_free_count) / 3) - 32; 293 if (cnt.v_free_count < 512) 294 tmp = cnt.v_free_count; 295 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(tmp); 296 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 297} 298 299#ifdef DEBUG 300int enableswap = 1; 301int swapdebug = 0; 302#define SDB_FOLLOW 1 303#define SDB_SWAPIN 2 304#define SDB_SWAPOUT 4 305#endif 306 307void 308faultin(p) 309struct proc *p; 310{ 311 vm_offset_t i; 312 vm_offset_t vaddr, ptaddr; 313 vm_offset_t v, v1; 314 struct user *up; 315 int s; 316 int opflag; 317 318 if ((p->p_flag & P_INMEM) == 0) { 319 int rv0, rv1; 320 vm_map_t map; 321 322 ++p->p_lock; 323 324 map = &p->p_vmspace->vm_map; 325 /* force the page table encompassing the kernel stack (upages) */ 326 ptaddr = trunc_page((u_int)vtopte(kstack)); 327 vm_map_pageable(map, ptaddr, ptaddr + NBPG, FALSE); 328 329 /* wire in the UPAGES */ 330 vm_map_pageable(map, (vm_offset_t) kstack, 331 (vm_offset_t) kstack + UPAGES * NBPG, FALSE); 332 333 /* and map them nicely into the kernel pmap */ 334 for (i = 0; i < UPAGES; i++) { 335 vm_offset_t off = i * NBPG; 336 vm_offset_t pa = (vm_offset_t) 337 pmap_extract(&p->p_vmspace->vm_pmap, 338 (vm_offset_t) kstack + off); 339 pmap_enter(vm_map_pmap(kernel_map), 340 ((vm_offset_t)p->p_addr) + off, 341 pa, VM_PROT_READ|VM_PROT_WRITE, 1); 342 } 343 344 /* and let the page table pages go (at least above pmap level) */ 345 vm_map_pageable(map, ptaddr, ptaddr + NBPG, TRUE); 346 347 s = splhigh(); 348 349 if (p->p_stat == SRUN) 350 setrunqueue(p); 351 352 p->p_flag |= P_INMEM; 353 354 /* undo the effect of setting SLOCK above */ 355 --p->p_lock; 356 splx(s); 357 358 } 359 360} 361 362int swapinreq; 363int percentactive; 364/* 365 * This swapin algorithm attempts to swap-in processes only if there 366 * is enough space for them. Of course, if a process waits for a long 367 * time, it will be swapped in anyway. 368 */ 369void 370scheduler() 371{ 372 register struct proc *p; 373 register int pri; 374 struct proc *pp; 375 int ppri; 376 vm_offset_t addr; 377 int lastidle, lastrun; 378 int curidle, currun; 379 int forceload; 380 int percent; 381 int ntries; 382 383 lastidle = 0; 384 lastrun = 0; 385 386loop: 387 ntries = 0; 388 vmmeter(); 389 390 curidle = cp_time[CP_IDLE]; 391 currun = cp_time[CP_USER] + cp_time[CP_SYS] + cp_time[CP_NICE]; 392 percent = (100*(currun-lastrun)) / ( 1 + (currun-lastrun) + (curidle-lastidle)); 393 lastrun = currun; 394 lastidle = curidle; 395 if( percent > 100) 396 percent = 100; 397 percentactive = percent; 398 399 if( percentactive < 25) 400 forceload = 1; 401 else 402 forceload = 0; 403 404loop1: 405 pp = NULL; 406 ppri = INT_MIN; 407 for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { 408 if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) { 409 int mempri; 410 pri = p->p_swtime + p->p_slptime - p->p_nice * 8; 411 mempri = pri > 0 ? pri : 0; 412 /* 413 * if this process is higher priority and there is 414 * enough space, then select this process instead 415 * of the previous selection. 416 */ 417 if (pri > ppri && 418 (((cnt.v_free_count + (mempri * (4*PAGE_SIZE) / PAGE_SIZE) >= (p->p_vmspace->vm_swrss)) || (ntries > 0 && forceload)))) { 419 pp = p; 420 ppri = pri; 421 } 422 } 423 } 424 425 if ((pp == NULL) && (ntries == 0) && forceload) { 426 ++ntries; 427 goto loop1; 428 } 429 430 /* 431 * Nothing to do, back to sleep 432 */ 433 if ((p = pp) == NULL) { 434 tsleep((caddr_t)&proc0, PVM, "sched", 0); 435 goto loop; 436 } 437 438 /* 439 * We would like to bring someone in. (only if there is space). 440 */ 441/* 442 printf("swapin: %d, free: %d, res: %d, min: %d\n", 443 p->p_pid, cnt.v_free_count, cnt.v_free_reserved, cnt.v_free_min); 444*/ 445 (void) splhigh(); 446 if ((forceload && (cnt.v_free_count > (cnt.v_free_reserved + UPAGES + 1))) || 447 (cnt.v_free_count >= cnt.v_free_min)) { 448 spl0(); 449 faultin(p); 450 p->p_swtime = 0; 451 goto loop; 452 } 453 /* 454 * log the memory shortage 455 */ 456 swapinreq += p->p_vmspace->vm_swrss; 457 /* 458 * Not enough memory, jab the pageout daemon and wait til the 459 * coast is clear. 460 */ 461 if( cnt.v_free_count < cnt.v_free_min) { 462 VM_WAIT; 463 } else { 464 tsleep((caddr_t)&proc0, PVM, "sched", 0); 465 } 466 (void) spl0(); 467 goto loop; 468} 469 470#define swappable(p) \ 471 (((p)->p_lock == 0) && \ 472 ((p)->p_flag & (P_TRACED|P_NOSWAP|P_SYSTEM|P_INMEM|P_WEXIT|P_PHYSIO)) == P_INMEM) 473 474extern int vm_pageout_free_min; 475/* 476 * Swapout is driven by the pageout daemon. Very simple, we find eligible 477 * procs and unwire their u-areas. We try to always "swap" at least one 478 * process in case we need the room for a swapin. 479 * If any procs have been sleeping/stopped for at least maxslp seconds, 480 * they are swapped. Else, we swap the longest-sleeping or stopped process, 481 * if any, otherwise the longest-resident process. 482 */ 483void 484swapout_threads() 485{ 486 register struct proc *p; 487 struct proc *outp, *outp2; 488 int outpri, outpri2; 489 int tpri; 490 int didswap = 0; 491 int swapneeded = swapinreq; 492 extern int maxslp; 493 int runnablenow; 494 int s; 495 496swapmore: 497 runnablenow = 0; 498 outp = outp2 = NULL; 499 outpri = outpri2 = INT_MIN; 500 for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { 501 if (!swappable(p)) 502 continue; 503 switch (p->p_stat) { 504 case SRUN: 505 ++runnablenow; 506 /* 507 * count the process as being in a runnable state 508 */ 509 if ((tpri = p->p_swtime + p->p_nice * 8) > outpri2) { 510 outp2 = p; 511 outpri2 = tpri; 512 } 513 continue; 514 515 case SSLEEP: 516 case SSTOP: 517 /* 518 * do not swapout a process that is waiting for VM datastructures 519 * there is a possible deadlock. 520 */ 521 if (!lock_try_write( &p->p_vmspace->vm_map.lock)) { 522 continue; 523 } 524 vm_map_unlock( &p->p_vmspace->vm_map); 525 if (p->p_slptime > maxslp) { 526 swapout(p); 527 didswap++; 528 } else if ((tpri = p->p_slptime + p->p_nice * 8) > outpri) { 529 outp = p; 530 outpri = tpri ; 531 } 532 continue; 533 } 534 } 535 /* 536 * We swapout only if there are more than two runnable processes or if 537 * another process needs some space to swapin. 538 */ 539 if ((swapinreq || ((percentactive > 90) && (runnablenow > 2))) && 540 (((cnt.v_free_count + cnt.v_inactive_count) <= (cnt.v_free_target + cnt.v_inactive_target)) || 541 (cnt.v_free_count < cnt.v_free_min))) { 542 if ((p = outp) == 0) { 543 p = outp2; 544 } 545 546 if (p) { 547 swapout(p); 548 didswap = 1; 549 } 550 } 551 552 /* 553 * if we previously had found a process to swapout, and we need to swapout 554 * more then try again. 555 */ 556#if 0 557 if( p && swapinreq) 558 goto swapmore; 559#endif 560 561 /* 562 * If we swapped something out, and another process needed memory, 563 * then wakeup the sched process. 564 */ 565 if (didswap) { 566 if (swapneeded) 567 wakeup((caddr_t)&proc0); 568 swapinreq = 0; 569 } 570} 571 572void 573swapout(p) 574 register struct proc *p; 575{ 576 vm_offset_t addr; 577 struct pmap *pmap = &p->p_vmspace->vm_pmap; 578 vm_map_t map = &p->p_vmspace->vm_map; 579 vm_offset_t ptaddr; 580 int i; 581 582 ++p->p_stats->p_ru.ru_nswap; 583 /* 584 * remember the process resident count 585 */ 586 p->p_vmspace->vm_swrss = 587 p->p_vmspace->vm_pmap.pm_stats.resident_count; 588 /* 589 * and decrement the amount of needed space 590 */ 591 swapinreq -= min(swapinreq, p->p_vmspace->vm_pmap.pm_stats.resident_count); 592 593 (void) splhigh(); 594 p->p_flag &= ~P_INMEM; 595 if (p->p_stat == SRUN) 596 remrq(p); 597 (void) spl0(); 598 599 ++p->p_lock; 600/* let the upages be paged */ 601 pmap_remove(vm_map_pmap(kernel_map), 602 (vm_offset_t) p->p_addr, ((vm_offset_t) p->p_addr) + UPAGES * NBPG); 603 604 vm_map_pageable(map, (vm_offset_t) kstack, 605 (vm_offset_t) kstack + UPAGES * NBPG, TRUE); 606 607 --p->p_lock; 608 p->p_swtime = 0; 609} 610 611/* 612 * The rest of these routines fake thread handling 613 */ 614 615#ifndef assert_wait 616void 617assert_wait(event, ruptible) 618 int event; 619 boolean_t ruptible; 620{ 621#ifdef lint 622 ruptible++; 623#endif 624 curproc->p_thread = event; 625} 626#endif 627 628void 629thread_block(char *msg) 630{ 631 if (curproc->p_thread) 632 tsleep((caddr_t)curproc->p_thread, PVM, msg, 0); 633} 634 635 636void 637thread_sleep_(event, lock, wmesg) 638 int event; 639 simple_lock_t lock; 640 char *wmesg; 641{ 642 643 curproc->p_thread = event; 644 simple_unlock(lock); 645 if (curproc->p_thread) { 646 tsleep((caddr_t)event, PVM, wmesg, 0); 647 } 648} 649 650#ifndef thread_wakeup 651void 652thread_wakeup(event) 653 int event; 654{ 655 wakeup((caddr_t)event); 656} 657#endif 658 659/* 660 * DEBUG stuff 661 */ 662 663int indent = 0; 664 665#include <machine/stdarg.h> /* see subr_prf.c */ 666 667/*ARGSUSED2*/ 668void 669#if __STDC__ 670iprintf(const char *fmt, ...) 671#else 672iprintf(fmt /* , va_alist */) 673 char *fmt; 674 /* va_dcl */ 675#endif 676{ 677 register int i; 678 va_list ap; 679 680 for (i = indent; i >= 8; i -= 8) 681 printf("\t"); 682 while (--i >= 0) 683 printf(" "); 684 va_start(ap, fmt); 685 printf("%r", fmt, ap); 686 va_end(ap); 687} 688