vm_glue.c revision 10835
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: vm_glue.c,v 1.25 1995/09/09 18:10:35 davidg Exp $ 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/shm.h> 71#include <sys/user.h> 72 73#include <sys/kernel.h> 74#include <sys/dkstat.h> 75 76#include <vm/vm.h> 77#include <vm/vm_page.h> 78#include <vm/vm_pageout.h> 79#include <vm/vm_kern.h> 80 81#include <machine/stdarg.h> 82#include <machine/cpu.h> 83 84/* 85 * System initialization 86 * 87 * Note: proc0 from proc.h 88 */ 89 90static void vm_init_limits __P((void *)); 91SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0) 92 93/* 94 * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 95 * 96 * Note: run scheduling should be divorced from the vm system. 97 */ 98static void scheduler __P((void *)); 99SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 100 101 102extern char kstack[]; 103 104/* vm_map_t upages_map; */ 105 106int 107kernacc(addr, len, rw) 108 caddr_t addr; 109 int len, rw; 110{ 111 boolean_t rv; 112 vm_offset_t saddr, eaddr; 113 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 114 115 saddr = trunc_page(addr); 116 eaddr = round_page(addr + len); 117 rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 118 return (rv == TRUE); 119} 120 121int 122useracc(addr, len, rw) 123 caddr_t addr; 124 int len, rw; 125{ 126 boolean_t rv; 127 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 128 129 /* 130 * XXX - check separately to disallow access to user area and user 131 * page tables - they are in the map. 132 * 133 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. It was once 134 * only used (as an end address) in trap.c. Use it as an end address 135 * here too. This bogusness has spread. I just fixed where it was 136 * used as a max in vm_mmap.c. 137 */ 138 if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS 139 || (vm_offset_t) addr + len < (vm_offset_t) addr) { 140 return (FALSE); 141 } 142 rv = vm_map_check_protection(&curproc->p_vmspace->vm_map, 143 trunc_page(addr), round_page(addr + len), prot); 144 return (rv == TRUE); 145} 146 147#ifdef KGDB 148/* 149 * Change protections on kernel pages from addr to addr+len 150 * (presumably so debugger can plant a breakpoint). 151 * All addresses are assumed to reside in the Sysmap, 152 */ 153chgkprot(addr, len, rw) 154 register caddr_t addr; 155 int len, rw; 156{ 157 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 158 159 vm_map_protect(kernel_map, trunc_page(addr), 160 round_page(addr + len), prot, FALSE); 161} 162#endif 163void 164vslock(addr, len) 165 caddr_t addr; 166 u_int len; 167{ 168 vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr), 169 round_page(addr + len), FALSE); 170} 171 172void 173vsunlock(addr, len, dirtied) 174 caddr_t addr; 175 u_int len; 176 int dirtied; 177{ 178#ifdef lint 179 dirtied++; 180#endif /* lint */ 181 vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr), 182 round_page(addr + len), TRUE); 183} 184 185/* 186 * Implement fork's actions on an address space. 187 * Here we arrange for the address space to be copied or referenced, 188 * allocate a user struct (pcb and kernel stack), then call the 189 * machine-dependent layer to fill those in and make the new process 190 * ready to run. 191 * NOTE: the kernel stack may be at a different location in the child 192 * process, and thus addresses of automatic variables may be invalid 193 * after cpu_fork returns in the child process. We do nothing here 194 * after cpu_fork returns. 195 */ 196int 197vm_fork(p1, p2, isvfork) 198 register struct proc *p1, *p2; 199 int isvfork; 200{ 201 register struct user *up; 202 vm_offset_t addr, ptaddr; 203 int error, i; 204 struct vm_map *vp; 205 206 while ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) { 207 VM_WAIT; 208 } 209 210 /* 211 * avoid copying any of the parent's pagetables or other per-process 212 * objects that reside in the map by marking all of them 213 * non-inheritable 214 */ 215 (void) vm_map_inherit(&p1->p_vmspace->vm_map, 216 UPT_MIN_ADDRESS - UPAGES * NBPG, VM_MAX_ADDRESS, VM_INHERIT_NONE); 217 p2->p_vmspace = vmspace_fork(p1->p_vmspace); 218 219#ifdef SYSVSHM 220 if (p1->p_vmspace->vm_shm) 221 shmfork(p1, p2, isvfork); 222#endif 223 224 /* 225 * Allocate a wired-down (for now) pcb and kernel stack for the 226 * process 227 */ 228 229 addr = (vm_offset_t) kstack; 230 231 vp = &p2->p_vmspace->vm_map; 232 233 /* get new pagetables and kernel stack */ 234 (void) vm_map_find(vp, NULL, 0, &addr, UPT_MAX_ADDRESS - addr, FALSE); 235 236 /* force in the page table encompassing the UPAGES */ 237 ptaddr = trunc_page((u_int) vtopte(addr)); 238 error = vm_map_pageable(vp, ptaddr, ptaddr + NBPG, FALSE); 239 if (error) 240 panic("vm_fork: wire of PT failed. error=%d", error); 241 242 /* and force in (demand-zero) the UPAGES */ 243 error = vm_map_pageable(vp, addr, addr + UPAGES * NBPG, FALSE); 244 if (error) 245 panic("vm_fork: wire of UPAGES failed. error=%d", error); 246 247 /* get a kernel virtual address for the UPAGES for this proc */ 248 up = (struct user *) kmem_alloc_pageable(u_map, UPAGES * NBPG); 249 if (up == NULL) 250 panic("vm_fork: u_map allocation failed"); 251 252 /* and force-map the upages into the kernel pmap */ 253 for (i = 0; i < UPAGES; i++) 254 pmap_enter(vm_map_pmap(u_map), 255 ((vm_offset_t) up) + NBPG * i, 256 pmap_extract(vp->pmap, addr + NBPG * i), 257 VM_PROT_READ | VM_PROT_WRITE, 1); 258 259 p2->p_addr = up; 260 261 /* 262 * p_stats and p_sigacts currently point at fields in the user struct 263 * but not at &u, instead at p_addr. Copy p_sigacts and parts of 264 * p_stats; zero the rest of p_stats (statistics). 265 */ 266 p2->p_stats = &up->u_stats; 267 p2->p_sigacts = &up->u_sigacts; 268 up->u_sigacts = *p1->p_sigacts; 269 bzero(&up->u_stats.pstat_startzero, 270 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 271 (caddr_t) &up->u_stats.pstat_startzero)); 272 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 273 ((caddr_t) &up->u_stats.pstat_endcopy - 274 (caddr_t) &up->u_stats.pstat_startcopy)); 275 276 277 /* 278 * cpu_fork will copy and update the kernel stack and pcb, and make 279 * the child ready to run. It marks the child so that it can return 280 * differently than the parent. It returns twice, once in the parent 281 * process and once in the child. 282 */ 283 return (cpu_fork(p1, p2)); 284} 285 286/* 287 * Set default limits for VM system. 288 * Called for proc 0, and then inherited by all others. 289 * 290 * XXX should probably act directly on proc0. 291 */ 292static void 293vm_init_limits(udata) 294 void *udata; 295{ 296 register struct proc *p = (struct proc *)udata; 297 int rss_limit; 298 299 /* 300 * Set up the initial limits on process VM. Set the maximum resident 301 * set size to be half of (reasonably) available memory. Since this 302 * is a soft limit, it comes into effect only when the system is out 303 * of memory - half of main memory helps to favor smaller processes, 304 * and reduces thrashing of the object cache. 305 */ 306 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 307 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 308 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 309 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 310 /* limit the limit to no less than 2MB */ 311 rss_limit = max(cnt.v_free_count, 512); 312 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 313 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 314} 315 316void 317faultin(p) 318 struct proc *p; 319{ 320 vm_offset_t i; 321 vm_offset_t ptaddr; 322 int s; 323 324 if ((p->p_flag & P_INMEM) == 0) { 325 vm_map_t map; 326 327 ++p->p_lock; 328 329 map = &p->p_vmspace->vm_map; 330 /* force the page table encompassing the kernel stack (upages) */ 331 ptaddr = trunc_page((u_int) vtopte(kstack)); 332 vm_map_pageable(map, ptaddr, ptaddr + NBPG, FALSE); 333 334 /* wire in the UPAGES */ 335 vm_map_pageable(map, (vm_offset_t) kstack, 336 (vm_offset_t) kstack + UPAGES * NBPG, FALSE); 337 338 /* and map them nicely into the kernel pmap */ 339 for (i = 0; i < UPAGES; i++) { 340 vm_offset_t off = i * NBPG; 341 vm_offset_t pa = (vm_offset_t) 342 pmap_extract(&p->p_vmspace->vm_pmap, 343 (vm_offset_t) kstack + off); 344 345 pmap_enter(vm_map_pmap(u_map), 346 ((vm_offset_t) p->p_addr) + off, 347 pa, VM_PROT_READ | VM_PROT_WRITE, 1); 348 } 349 350 s = splhigh(); 351 352 if (p->p_stat == SRUN) 353 setrunqueue(p); 354 355 p->p_flag |= P_INMEM; 356 357 /* undo the effect of setting SLOCK above */ 358 --p->p_lock; 359 splx(s); 360 361 } 362} 363 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 */ 369/* ARGSUSED*/ 370static void 371scheduler(udata) 372 void *udata; /* not used*/ 373{ 374 register struct proc *p; 375 register int pri; 376 struct proc *pp; 377 int ppri; 378 379loop: 380 while ((cnt.v_free_count + cnt.v_cache_count) < (cnt.v_free_reserved + UPAGES + 2)) { 381 VM_WAIT; 382 tsleep(&proc0, PVM, "schedm", 0); 383 } 384 385 pp = NULL; 386 ppri = INT_MIN; 387 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 388 if (p->p_stat == SRUN && (p->p_flag & (P_INMEM | P_SWAPPING)) == 0) { 389 int mempri; 390 391 pri = p->p_swtime + p->p_slptime - p->p_nice * 8; 392 mempri = pri > 0 ? pri : 0; 393 /* 394 * if this process is higher priority and there is 395 * enough space, then select this process instead of 396 * the previous selection. 397 */ 398 if (pri > ppri) { 399 pp = p; 400 ppri = pri; 401 } 402 } 403 } 404 405 /* 406 * Nothing to do, back to sleep 407 */ 408 if ((p = pp) == NULL) { 409 tsleep(&proc0, PVM, "sched", 0); 410 goto loop; 411 } 412 /* 413 * We would like to bring someone in. (only if there is space). 414 */ 415 faultin(p); 416 p->p_swtime = 0; 417 goto loop; 418} 419 420#define swappable(p) \ 421 (((p)->p_lock == 0) && \ 422 ((p)->p_flag & (P_TRACED|P_NOSWAP|P_SYSTEM|P_INMEM|P_WEXIT|P_PHYSIO|P_SWAPPING)) == P_INMEM) 423 424extern int vm_pageout_free_min; 425 426/* 427 * Swapout is driven by the pageout daemon. Very simple, we find eligible 428 * procs and unwire their u-areas. We try to always "swap" at least one 429 * process in case we need the room for a swapin. 430 * If any procs have been sleeping/stopped for at least maxslp seconds, 431 * they are swapped. Else, we swap the longest-sleeping or stopped process, 432 * if any, otherwise the longest-resident process. 433 */ 434void 435swapout_procs() 436{ 437 register struct proc *p; 438 struct proc *outp, *outp2; 439 int outpri, outpri2; 440 int didswap = 0; 441 442 outp = outp2 = NULL; 443 outpri = outpri2 = INT_MIN; 444retry: 445 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 446 if (!swappable(p)) 447 continue; 448 switch (p->p_stat) { 449 default: 450 continue; 451 452 case SSLEEP: 453 case SSTOP: 454 /* 455 * do not swapout a realtime process 456 */ 457 if (p->p_rtprio.type == RTP_PRIO_REALTIME) 458 continue; 459 460 /* 461 * do not swapout a process waiting on a critical 462 * event of some kind 463 */ 464 if ((p->p_priority & 0x7f) < PSOCK) 465 continue; 466 467 vm_map_reference(&p->p_vmspace->vm_map); 468 /* 469 * do not swapout a process that is waiting for VM 470 * datastructures there is a possible deadlock. 471 */ 472 if (!lock_try_write(&p->p_vmspace->vm_map.lock)) { 473 vm_map_deallocate(&p->p_vmspace->vm_map); 474 continue; 475 } 476 vm_map_unlock(&p->p_vmspace->vm_map); 477 /* 478 * If the process has been asleep for awhile and had 479 * most of its pages taken away already, swap it out. 480 */ 481 if (p->p_slptime > 4) { 482 swapout(p); 483 vm_map_deallocate(&p->p_vmspace->vm_map); 484 didswap++; 485 goto retry; 486 } 487 vm_map_deallocate(&p->p_vmspace->vm_map); 488 } 489 } 490 /* 491 * If we swapped something out, and another process needed memory, 492 * then wakeup the sched process. 493 */ 494 if (didswap) 495 wakeup(&proc0); 496} 497 498void 499swapout(p) 500 register struct proc *p; 501{ 502 vm_map_t map = &p->p_vmspace->vm_map; 503 vm_offset_t ptaddr; 504 505 ++p->p_stats->p_ru.ru_nswap; 506 /* 507 * remember the process resident count 508 */ 509 p->p_vmspace->vm_swrss = 510 p->p_vmspace->vm_pmap.pm_stats.resident_count; 511 512 (void) splhigh(); 513 p->p_flag &= ~P_INMEM; 514 if (p->p_stat == SRUN) 515 remrq(p); 516 (void) spl0(); 517 518 p->p_flag |= P_SWAPPING; 519 /* 520 * let the upages be paged 521 */ 522 pmap_remove(vm_map_pmap(u_map), 523 (vm_offset_t) p->p_addr, ((vm_offset_t) p->p_addr) + UPAGES * NBPG); 524 525 vm_map_pageable(map, (vm_offset_t) kstack, 526 (vm_offset_t) kstack + UPAGES * NBPG, TRUE); 527 528 ptaddr = trunc_page((u_int) vtopte(kstack)); 529 vm_map_pageable(map, ptaddr, ptaddr + NBPG, TRUE); 530 531 p->p_flag &= ~P_SWAPPING; 532 p->p_swtime = 0; 533} 534 535#ifdef DDB 536/* 537 * DEBUG stuff 538 */ 539 540int indent; 541 542#include <machine/stdarg.h> /* see subr_prf.c */ 543 544/*ARGSUSED2*/ 545void 546#if __STDC__ 547iprintf(const char *fmt,...) 548#else 549iprintf(fmt /* , va_alist */ ) 550 char *fmt; 551 552 /* va_dcl */ 553#endif 554{ 555 register int i; 556 va_list ap; 557 558 for (i = indent; i >= 8; i -= 8) 559 printf("\t"); 560 while (--i >= 0) 561 printf(" "); 562 va_start(ap, fmt); 563 printf("%r", fmt, ap); 564 va_end(ap); 565} 566#endif /* DDB */ 567