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