vm_glue.c revision 10653
1170808Sdelphij/* 2170808Sdelphij * Copyright (c) 1991, 1993 3182739Sdelphij * The Regents of the University of California. All rights reserved. 4170808Sdelphij * 5170808Sdelphij * This code is derived from software contributed to Berkeley by 6170808Sdelphij * The Mach Operating System project at Carnegie-Mellon University. 7170808Sdelphij * 8170808Sdelphij * Redistribution and use in source and binary forms, with or without 9170808Sdelphij * modification, are permitted provided that the following conditions 10170808Sdelphij * are met: 11170808Sdelphij * 1. Redistributions of source code must retain the above copyright 12170808Sdelphij * notice, this list of conditions and the following disclaimer. 13170808Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 14170808Sdelphij * notice, this list of conditions and the following disclaimer in the 15170808Sdelphij * documentation and/or other materials provided with the distribution. 16170808Sdelphij * 3. All advertising materials mentioning features or use of this software 17170808Sdelphij * must display the following acknowledgement: 18170808Sdelphij * This product includes software developed by the University of 19170808Sdelphij * California, Berkeley and its contributors. 20170808Sdelphij * 4. Neither the name of the University nor the names of its contributors 21170808Sdelphij * may be used to endorse or promote products derived from this software 22170808Sdelphij * without specific prior written permission. 23170808Sdelphij * 24170808Sdelphij * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25170808Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26170808Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27170808Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28170808Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29170808Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30170808Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31170808Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32170808Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33170808Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34170808Sdelphij * SUCH DAMAGE. 35170808Sdelphij * 36170808Sdelphij * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 37170808Sdelphij * 38170808Sdelphij * 39170808Sdelphij * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40170808Sdelphij * All rights reserved. 41170808Sdelphij * 42170808Sdelphij * Permission to use, copy, modify and distribute this software and 43170808Sdelphij * its documentation is hereby granted, provided that both the copyright 44170808Sdelphij * notice and this permission notice appear in all copies of the 45170808Sdelphij * software, derivative works or modified versions, and any portions 46170808Sdelphij * thereof, and that both notices appear in supporting documentation. 47171308Sdelphij * 48170808Sdelphij * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49170808Sdelphij * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50170808Sdelphij * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51170808Sdelphij * 52170808Sdelphij * Carnegie Mellon requests users of this software to return to 53170808Sdelphij * 54170808Sdelphij * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55170808Sdelphij * School of Computer Science 56170808Sdelphij * Carnegie Mellon University 57170808Sdelphij * Pittsburgh PA 15213-3890 58170808Sdelphij * 59170808Sdelphij * any improvements or extensions that they make and grant Carnegie the 60170808Sdelphij * rights to redistribute these changes. 61170808Sdelphij * 62171070Sdelphij * $Id: vm_glue.c,v 1.24 1995/08/28 09:19:22 julian Exp $ 63170808Sdelphij */ 64170808Sdelphij 65170808Sdelphij#include <sys/param.h> 66170808Sdelphij#include <sys/systm.h> 67171087Sdelphij#include <sys/proc.h> 68170808Sdelphij#include <sys/resourcevar.h> 69170808Sdelphij#include <sys/buf.h> 70170808Sdelphij#include <sys/shm.h> 71170808Sdelphij#include <sys/user.h> 72170808Sdelphij 73170808Sdelphij#include <sys/kernel.h> 74170808Sdelphij#include <sys/dkstat.h> 75170808Sdelphij 76170808Sdelphij#include <vm/vm.h> 77170808Sdelphij#include <vm/vm_page.h> 78170808Sdelphij#include <vm/vm_pageout.h> 79170808Sdelphij#include <vm/vm_kern.h> 80170808Sdelphij 81172442Sdelphij#include <machine/stdarg.h> 82170808Sdelphij#include <machine/cpu.h> 83170808Sdelphij 84170808Sdelphij/* 85170808Sdelphij * System initialization 86170808Sdelphij * 87170808Sdelphij * Note: proc0 from proc.h 88170808Sdelphij */ 89171029Sdelphij 90170808Sdelphijstatic void vm_init_limits __P((void *)); 91170808SdelphijSYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0) 92170808Sdelphij 93170808Sdelphij/* 94170808Sdelphij * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 95171029Sdelphij * 96170808Sdelphij * Note: run scheduling should be divorced from the vm system. 97170808Sdelphij */ 98170808Sdelphijstatic void scheduler __P((void *)); 99170808SdelphijSYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 100170808Sdelphij 101170808Sdelphij 102170808Sdelphijextern char kstack[]; 103170808Sdelphij 104170808Sdelphij/* vm_map_t upages_map; */ 105170808Sdelphij 106171070Sdelphijint 107170808Sdelphijkernacc(addr, len, rw) 108170808Sdelphij caddr_t addr; 109170808Sdelphij int len, rw; 110170808Sdelphij{ 111170808Sdelphij boolean_t rv; 112170808Sdelphij vm_offset_t saddr, eaddr; 113170808Sdelphij vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 114170808Sdelphij 115171070Sdelphij saddr = trunc_page(addr); 116170808Sdelphij eaddr = round_page(addr + len); 117170808Sdelphij rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 118170808Sdelphij return (rv == TRUE); 119170808Sdelphij} 120170808Sdelphij 121170808Sdelphijint 122170808Sdelphijuseracc(addr, len, rw) 123170808Sdelphij caddr_t addr; 124171070Sdelphij int len, rw; 125171070Sdelphij{ 126170808Sdelphij boolean_t rv; 127170808Sdelphij vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 128170808Sdelphij 129170808Sdelphij /* 130170808Sdelphij * XXX - check separately to disallow access to user area and user 131170808Sdelphij * page tables - they are in the map. 132170808Sdelphij * 133170808Sdelphij * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. It was once 134170808Sdelphij * only used (as an end address) in trap.c. Use it as an end address 135170808Sdelphij * here too. This bogusness has spread. I just fixed where it was 136170808Sdelphij * used as a max in vm_mmap.c. 137171029Sdelphij */ 138171029Sdelphij if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS 139171029Sdelphij || (vm_offset_t) addr + len < (vm_offset_t) addr) { 140171029Sdelphij return (FALSE); 141171070Sdelphij } 142171362Sdelphij rv = vm_map_check_protection(&curproc->p_vmspace->vm_map, 143171029Sdelphij trunc_page(addr), round_page(addr + len), prot); 144171029Sdelphij return (rv == TRUE); 145171029Sdelphij} 146171029Sdelphij 147171029Sdelphij#ifdef KGDB 148171029Sdelphij/* 149170808Sdelphij * Change protections on kernel pages from addr to addr+len 150171029Sdelphij * (presumably so debugger can plant a breakpoint). 151171029Sdelphij * All addresses are assumed to reside in the Sysmap, 152171029Sdelphij */ 153171029Sdelphijchgkprot(addr, len, rw) 154171029Sdelphij register caddr_t addr; 155171029Sdelphij int len, rw; 156171029Sdelphij{ 157171029Sdelphij vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 158171029Sdelphij 159171029Sdelphij vm_map_protect(kernel_map, trunc_page(addr), 160171070Sdelphij round_page(addr + len), prot, FALSE); 161171029Sdelphij} 162171029Sdelphij#endif 163171029Sdelphijvoid 164171029Sdelphijvslock(addr, len) 165171029Sdelphij caddr_t addr; 166171029Sdelphij u_int len; 167171362Sdelphij{ 168171070Sdelphij vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr), 169171029Sdelphij round_page(addr + len), FALSE); 170171029Sdelphij} 171171029Sdelphij 172171029Sdelphijvoid 173171029Sdelphijvsunlock(addr, len, dirtied) 174171029Sdelphij caddr_t addr; 175171029Sdelphij u_int len; 176171070Sdelphij int dirtied; 177171029Sdelphij{ 178171029Sdelphij#ifdef lint 179171029Sdelphij dirtied++; 180170808Sdelphij#endif /* lint */ 181171308Sdelphij vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr), 182170808Sdelphij round_page(addr + len), TRUE); 183170808Sdelphij} 184170808Sdelphij 185170808Sdelphij/* 186170808Sdelphij * Implement fork's actions on an address space. 187170808Sdelphij * Here we arrange for the address space to be copied or referenced, 188171308Sdelphij * allocate a user struct (pcb and kernel stack), then call the 189171308Sdelphij * machine-dependent layer to fill those in and make the new process 190174379Sdelphij * ready to run. 191170808Sdelphij * NOTE: the kernel stack may be at a different location in the child 192171308Sdelphij * process, and thus addresses of automatic variables may be invalid 193171308Sdelphij * after cpu_fork returns in the child process. We do nothing here 194171308Sdelphij * after cpu_fork returns. 195171308Sdelphij */ 196171308Sdelphijint 197171308Sdelphijvm_fork(p1, p2, isvfork) 198171308Sdelphij register struct proc *p1, *p2; 199170808Sdelphij int isvfork; 200170808Sdelphij{ 201170808Sdelphij register struct user *up; 202170808Sdelphij vm_offset_t addr, ptaddr; 203170808Sdelphij int i; 204170808Sdelphij struct vm_map *vp; 205170808Sdelphij 206170808Sdelphij while ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) { 207170808Sdelphij VM_WAIT; 208170808Sdelphij } 209172441Sdelphij 210173724Sdelphij /* 211172441Sdelphij * avoid copying any of the parent's pagetables or other per-process 212175202Sattilio * objects that reside in the map by marking all of them 213182371Sattilio * non-inheritable 214175294Sattilio */ 215171308Sdelphij (void) vm_map_inherit(&p1->p_vmspace->vm_map, 216171308Sdelphij UPT_MIN_ADDRESS - UPAGES * NBPG, VM_MAX_ADDRESS, VM_INHERIT_NONE); 217170808Sdelphij p2->p_vmspace = vmspace_fork(p1->p_vmspace); 218171308Sdelphij 219171308Sdelphij#ifdef SYSVSHM 220171308Sdelphij if (p1->p_vmspace->vm_shm) 221171308Sdelphij shmfork(p1, p2, isvfork); 222171308Sdelphij#endif 223171308Sdelphij 224171308Sdelphij /* 225173570Sdelphij * Allocate a wired-down (for now) pcb and kernel stack for the 226171308Sdelphij * process 227173724Sdelphij */ 228171308Sdelphij 229173724Sdelphij addr = (vm_offset_t) kstack; 230171308Sdelphij 231170808Sdelphij vp = &p2->p_vmspace->vm_map; 232170808Sdelphij 233170808Sdelphij /* get new pagetables and kernel stack */ 234170808Sdelphij (void) vm_map_find(vp, NULL, 0, &addr, UPT_MAX_ADDRESS - addr, FALSE); 235170808Sdelphij 236170808Sdelphij /* force in the page table encompassing the UPAGES */ 237170808Sdelphij ptaddr = trunc_page((u_int) vtopte(addr)); 238170808Sdelphij vm_map_pageable(vp, ptaddr, ptaddr + NBPG, FALSE); 239170808Sdelphij 240170808Sdelphij /* and force in (demand-zero) the UPAGES */ 241170808Sdelphij vm_map_pageable(vp, addr, addr + UPAGES * NBPG, FALSE); 242170808Sdelphij 243171308Sdelphij /* get a kernel virtual address for the UPAGES for this proc */ 244170808Sdelphij up = (struct user *) kmem_alloc_pageable(u_map, UPAGES * NBPG); 245170808Sdelphij if (up == NULL) 246171308Sdelphij panic("vm_fork: u_map allocation failed"); 247170808Sdelphij 248170808Sdelphij /* and force-map the upages into the kernel pmap */ 249171308Sdelphij for (i = 0; i < UPAGES; i++) 250170808Sdelphij pmap_enter(vm_map_pmap(u_map), 251170808Sdelphij ((vm_offset_t) up) + NBPG * i, 252171308Sdelphij pmap_extract(vp->pmap, addr + NBPG * i), 253170808Sdelphij VM_PROT_READ | VM_PROT_WRITE, 1); 254170808Sdelphij 255170808Sdelphij p2->p_addr = up; 256170808Sdelphij 257170808Sdelphij /* 258171070Sdelphij * p_stats and p_sigacts currently point at fields in the user struct 259170808Sdelphij * but not at &u, instead at p_addr. Copy p_sigacts and parts of 260170808Sdelphij * p_stats; zero the rest of p_stats (statistics). 261170808Sdelphij */ 262171570Sdelphij p2->p_stats = &up->u_stats; 263170808Sdelphij p2->p_sigacts = &up->u_sigacts; 264171070Sdelphij up->u_sigacts = *p1->p_sigacts; 265170808Sdelphij bzero(&up->u_stats.pstat_startzero, 266170808Sdelphij (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 267171362Sdelphij (caddr_t) &up->u_stats.pstat_startzero)); 268173724Sdelphij bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 269173724Sdelphij ((caddr_t) &up->u_stats.pstat_endcopy - 270173724Sdelphij (caddr_t) &up->u_stats.pstat_startcopy)); 271173724Sdelphij 272173724Sdelphij 273173724Sdelphij /* 274173724Sdelphij * cpu_fork will copy and update the kernel stack and pcb, and make 275173724Sdelphij * the child ready to run. It marks the child so that it can return 276173724Sdelphij * differently than the parent. It returns twice, once in the parent 277170808Sdelphij * process and once in the child. 278170808Sdelphij */ 279171308Sdelphij return (cpu_fork(p1, p2)); 280171308Sdelphij} 281171308Sdelphij 282170808Sdelphij/* 283170808Sdelphij * Set default limits for VM system. 284171029Sdelphij * Called for proc 0, and then inherited by all others. 285171029Sdelphij * 286171362Sdelphij * XXX should probably act directly on proc0. 287170808Sdelphij */ 288170808Sdelphijstatic void 289170808Sdelphijvm_init_limits(udata) 290171362Sdelphij void *udata; 291170808Sdelphij{ 292170808Sdelphij register struct proc *p = (struct proc *)udata; 293170808Sdelphij int rss_limit; 294170808Sdelphij 295170808Sdelphij /* 296170808Sdelphij * Set up the initial limits on process VM. Set the maximum resident 297171070Sdelphij * set size to be half of (reasonably) available memory. Since this 298170808Sdelphij * is a soft limit, it comes into effect only when the system is out 299170808Sdelphij * of memory - half of main memory helps to favor smaller processes, 300170808Sdelphij * and reduces thrashing of the object cache. 301170808Sdelphij */ 302170808Sdelphij p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 303170808Sdelphij p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 304170808Sdelphij p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 305170808Sdelphij p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 306170808Sdelphij /* limit the limit to no less than 2MB */ 307170808Sdelphij rss_limit = max(cnt.v_free_count, 512); 308170808Sdelphij p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 309170808Sdelphij p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 310170808Sdelphij} 311170808Sdelphij 312170808Sdelphijvoid 313170808Sdelphijfaultin(p) 314170808Sdelphij struct proc *p; 315170808Sdelphij{ 316170808Sdelphij vm_offset_t i; 317170808Sdelphij vm_offset_t ptaddr; 318170808Sdelphij int s; 319170808Sdelphij 320170808Sdelphij if ((p->p_flag & P_INMEM) == 0) { 321170808Sdelphij vm_map_t map; 322170808Sdelphij 323170808Sdelphij ++p->p_lock; 324170808Sdelphij 325170808Sdelphij map = &p->p_vmspace->vm_map; 326170808Sdelphij /* force the page table encompassing the kernel stack (upages) */ 327171070Sdelphij ptaddr = trunc_page((u_int) vtopte(kstack)); 328170808Sdelphij vm_map_pageable(map, ptaddr, ptaddr + NBPG, FALSE); 329170808Sdelphij 330170808Sdelphij /* wire in the UPAGES */ 331170808Sdelphij vm_map_pageable(map, (vm_offset_t) kstack, 332170808Sdelphij (vm_offset_t) kstack + UPAGES * NBPG, FALSE); 333170808Sdelphij 334170808Sdelphij /* and map them nicely into the kernel pmap */ 335170808Sdelphij for (i = 0; i < UPAGES; i++) { 336170808Sdelphij vm_offset_t off = i * NBPG; 337170808Sdelphij vm_offset_t pa = (vm_offset_t) 338170808Sdelphij pmap_extract(&p->p_vmspace->vm_pmap, 339170808Sdelphij (vm_offset_t) kstack + off); 340170808Sdelphij 341170808Sdelphij pmap_enter(vm_map_pmap(u_map), 342170808Sdelphij ((vm_offset_t) p->p_addr) + off, 343170808Sdelphij pa, VM_PROT_READ | VM_PROT_WRITE, 1); 344170808Sdelphij } 345170808Sdelphij 346170808Sdelphij s = splhigh(); 347170808Sdelphij 348170808Sdelphij if (p->p_stat == SRUN) 349170808Sdelphij setrunqueue(p); 350170808Sdelphij 351170808Sdelphij p->p_flag |= P_INMEM; 352170808Sdelphij 353170808Sdelphij /* undo the effect of setting SLOCK above */ 354170808Sdelphij --p->p_lock; 355170808Sdelphij splx(s); 356171029Sdelphij 357171029Sdelphij } 358171362Sdelphij} 359170808Sdelphij 360170808Sdelphij/* 361170808Sdelphij * This swapin algorithm attempts to swap-in processes only if there 362171308Sdelphij * is enough space for them. Of course, if a process waits for a long 363170808Sdelphij * time, it will be swapped in anyway. 364170808Sdelphij */ 365170808Sdelphij/* ARGSUSED*/ 366170808Sdelphijstatic void 367171070Sdelphijscheduler(udata) 368170808Sdelphij void *udata; /* not used*/ 369170808Sdelphij{ 370170808Sdelphij register struct proc *p; 371170808Sdelphij register int pri; 372170808Sdelphij struct proc *pp; 373170808Sdelphij int ppri; 374170808Sdelphij 375170808Sdelphijloop: 376170808Sdelphij while ((cnt.v_free_count + cnt.v_cache_count) < (cnt.v_free_reserved + UPAGES + 2)) { 377170808Sdelphij VM_WAIT; 378170808Sdelphij tsleep(&proc0, PVM, "schedm", 0); 379170808Sdelphij } 380171799Sdelphij 381170808Sdelphij pp = NULL; 382170808Sdelphij ppri = INT_MIN; 383171029Sdelphij for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 384170808Sdelphij if (p->p_stat == SRUN && (p->p_flag & (P_INMEM | P_SWAPPING)) == 0) { 385170808Sdelphij int mempri; 386170808Sdelphij 387170808Sdelphij pri = p->p_swtime + p->p_slptime - p->p_nice * 8; 388170808Sdelphij mempri = pri > 0 ? pri : 0; 389170808Sdelphij /* 390170808Sdelphij * if this process is higher priority and there is 391170808Sdelphij * enough space, then select this process instead of 392170808Sdelphij * the previous selection. 393170808Sdelphij */ 394170808Sdelphij if (pri > ppri) { 395170808Sdelphij pp = p; 396170808Sdelphij ppri = pri; 397170808Sdelphij } 398170808Sdelphij } 399170808Sdelphij } 400170808Sdelphij 401170808Sdelphij /* 402170808Sdelphij * Nothing to do, back to sleep 403170808Sdelphij */ 404170808Sdelphij if ((p = pp) == NULL) { 405170808Sdelphij tsleep(&proc0, PVM, "sched", 0); 406170808Sdelphij goto loop; 407170808Sdelphij } 408170808Sdelphij /* 409170808Sdelphij * We would like to bring someone in. (only if there is space). 410170808Sdelphij */ 411170808Sdelphij faultin(p); 412170808Sdelphij p->p_swtime = 0; 413170808Sdelphij goto loop; 414170808Sdelphij} 415170808Sdelphij 416170808Sdelphij#define swappable(p) \ 417170808Sdelphij (((p)->p_lock == 0) && \ 418170808Sdelphij ((p)->p_flag & (P_TRACED|P_NOSWAP|P_SYSTEM|P_INMEM|P_WEXIT|P_PHYSIO|P_SWAPPING)) == P_INMEM) 419171799Sdelphij 420171799Sdelphijextern int vm_pageout_free_min; 421171799Sdelphij 422171799Sdelphij/* 423170808Sdelphij * Swapout is driven by the pageout daemon. Very simple, we find eligible 424170808Sdelphij * procs and unwire their u-areas. We try to always "swap" at least one 425170808Sdelphij * process in case we need the room for a swapin. 426170808Sdelphij * If any procs have been sleeping/stopped for at least maxslp seconds, 427170808Sdelphij * they are swapped. Else, we swap the longest-sleeping or stopped process, 428170808Sdelphij * if any, otherwise the longest-resident process. 429170808Sdelphij */ 430170808Sdelphijvoid 431170808Sdelphijswapout_procs() 432170808Sdelphij{ 433170808Sdelphij register struct proc *p; 434170808Sdelphij struct proc *outp, *outp2; 435170808Sdelphij int outpri, outpri2; 436171070Sdelphij int didswap = 0; 437170808Sdelphij 438170808Sdelphij outp = outp2 = NULL; 439170808Sdelphij outpri = outpri2 = INT_MIN; 440170808Sdelphijretry: 441170808Sdelphij for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 442170808Sdelphij if (!swappable(p)) 443170808Sdelphij continue; 444170808Sdelphij switch (p->p_stat) { 445170808Sdelphij default: 446170808Sdelphij continue; 447170808Sdelphij 448170808Sdelphij case SSLEEP: 449170808Sdelphij case SSTOP: 450170808Sdelphij /* 451170808Sdelphij * do not swapout a realtime process 452170808Sdelphij */ 453170808Sdelphij if (p->p_rtprio.type == RTP_PRIO_REALTIME) 454170808Sdelphij continue; 455170808Sdelphij 456170808Sdelphij /* 457170808Sdelphij * do not swapout a process waiting on a critical 458170808Sdelphij * event of some kind 459170808Sdelphij */ 460170808Sdelphij if ((p->p_priority & 0x7f) < PSOCK) 461170808Sdelphij continue; 462170808Sdelphij 463170808Sdelphij vm_map_reference(&p->p_vmspace->vm_map); 464170808Sdelphij /* 465170808Sdelphij * do not swapout a process that is waiting for VM 466 * datastructures there is a possible deadlock. 467 */ 468 if (!lock_try_write(&p->p_vmspace->vm_map.lock)) { 469 vm_map_deallocate(&p->p_vmspace->vm_map); 470 continue; 471 } 472 vm_map_unlock(&p->p_vmspace->vm_map); 473 /* 474 * If the process has been asleep for awhile and had 475 * most of its pages taken away already, swap it out. 476 */ 477 if (p->p_slptime > 4) { 478 swapout(p); 479 vm_map_deallocate(&p->p_vmspace->vm_map); 480 didswap++; 481 goto retry; 482 } 483 vm_map_deallocate(&p->p_vmspace->vm_map); 484 } 485 } 486 /* 487 * If we swapped something out, and another process needed memory, 488 * then wakeup the sched process. 489 */ 490 if (didswap) 491 wakeup(&proc0); 492} 493 494void 495swapout(p) 496 register struct proc *p; 497{ 498 vm_map_t map = &p->p_vmspace->vm_map; 499 vm_offset_t ptaddr; 500 501 ++p->p_stats->p_ru.ru_nswap; 502 /* 503 * remember the process resident count 504 */ 505 p->p_vmspace->vm_swrss = 506 p->p_vmspace->vm_pmap.pm_stats.resident_count; 507 508 (void) splhigh(); 509 p->p_flag &= ~P_INMEM; 510 if (p->p_stat == SRUN) 511 remrq(p); 512 (void) spl0(); 513 514 p->p_flag |= P_SWAPPING; 515 /* 516 * let the upages be paged 517 */ 518 pmap_remove(vm_map_pmap(u_map), 519 (vm_offset_t) p->p_addr, ((vm_offset_t) p->p_addr) + UPAGES * NBPG); 520 521 vm_map_pageable(map, (vm_offset_t) kstack, 522 (vm_offset_t) kstack + UPAGES * NBPG, TRUE); 523 524 ptaddr = trunc_page((u_int) vtopte(kstack)); 525 vm_map_pageable(map, ptaddr, ptaddr + NBPG, TRUE); 526 527 p->p_flag &= ~P_SWAPPING; 528 p->p_swtime = 0; 529} 530 531#ifdef DDB 532/* 533 * DEBUG stuff 534 */ 535 536int indent; 537 538#include <machine/stdarg.h> /* see subr_prf.c */ 539 540/*ARGSUSED2*/ 541void 542#if __STDC__ 543iprintf(const char *fmt,...) 544#else 545iprintf(fmt /* , va_alist */ ) 546 char *fmt; 547 548 /* va_dcl */ 549#endif 550{ 551 register int i; 552 va_list ap; 553 554 for (i = indent; i >= 8; i -= 8) 555 printf("\t"); 556 while (--i >= 0) 557 printf(" "); 558 va_start(ap, fmt); 559 printf("%r", fmt, ap); 560 va_end(ap); 561} 562#endif /* DDB */ 563