vm_glue.c revision 14178
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.38 1996/01/29 12:10:30 davidg 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 map; 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 map = &p2->p_vmspace->vm_map; 250 pvp = &p2->p_vmspace->vm_pmap; 251 252 /* get new pagetables and kernel stack */ 253 error = vm_map_find(map, NULL, 0, &addr, UPT_MAX_ADDRESS - addr, FALSE, 254 VM_PROT_ALL, VM_PROT_ALL, 0); 255 if (error != KERN_SUCCESS) 256 panic("vm_fork: vm_map_find failed, addr=0x%x, error=%d", addr, error); 257 258 /* get a kernel virtual address for the UPAGES for this proc */ 259 up = (struct user *) kmem_alloc_pageable(u_map, UPAGES * PAGE_SIZE); 260 if (up == NULL) 261 panic("vm_fork: u_map allocation failed"); 262 263 p2->p_vmspace->vm_upages_obj = vm_object_allocate( OBJT_DEFAULT, 264 UPAGES); 265 266 ptaddr = trunc_page((u_int) vtopte(kstack)); 267 (void) vm_fault(map, ptaddr, VM_PROT_READ|VM_PROT_WRITE, FALSE); 268 ptpa = pmap_extract(pvp, ptaddr); 269 if (ptpa == 0) { 270 panic("vm_fork: no pte for UPAGES"); 271 } 272 stkm = PHYS_TO_VM_PAGE(ptpa); 273 vm_page_hold(stkm); 274 275 for(i=0;i<UPAGES;i++) { 276 vm_page_t m; 277 278 while ((m = vm_page_alloc(p2->p_vmspace->vm_upages_obj, i, VM_ALLOC_ZERO)) == NULL) { 279 VM_WAIT; 280 } 281 282 vm_page_wire(m); 283 m->flags &= ~PG_BUSY; 284 pmap_enter( pvp, (vm_offset_t) kstack + i * PAGE_SIZE, 285 VM_PAGE_TO_PHYS(m), VM_PROT_READ|VM_PROT_WRITE, 1); 286 pmap_kenter(((vm_offset_t) up) + i * PAGE_SIZE, 287 VM_PAGE_TO_PHYS(m)); 288 if ((m->flags & PG_ZERO) == 0) 289 bzero(((caddr_t) up) + i * PAGE_SIZE, PAGE_SIZE); 290 m->flags &= ~PG_ZERO; 291 m->valid = VM_PAGE_BITS_ALL; 292 } 293 vm_page_unhold(stkm); 294 295 p2->p_addr = up; 296 297 /* 298 * p_stats and p_sigacts currently point at fields in the user struct 299 * but not at &u, instead at p_addr. Copy p_sigacts and parts of 300 * p_stats; zero the rest of p_stats (statistics). 301 */ 302 p2->p_stats = &up->u_stats; 303 p2->p_sigacts = &up->u_sigacts; 304 up->u_sigacts = *p1->p_sigacts; 305 bzero(&up->u_stats.pstat_startzero, 306 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 307 (caddr_t) &up->u_stats.pstat_startzero)); 308 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 309 ((caddr_t) &up->u_stats.pstat_endcopy - 310 (caddr_t) &up->u_stats.pstat_startcopy)); 311 312 313 /* 314 * cpu_fork will copy and update the kernel stack and pcb, and make 315 * the child ready to run. It marks the child so that it can return 316 * differently than the parent. It returns twice, once in the parent 317 * process and once in the child. 318 */ 319 return (cpu_fork(p1, p2)); 320} 321 322/* 323 * Set default limits for VM system. 324 * Called for proc 0, and then inherited by all others. 325 * 326 * XXX should probably act directly on proc0. 327 */ 328static void 329vm_init_limits(udata) 330 void *udata; 331{ 332 register struct proc *p = udata; 333 int rss_limit; 334 335 /* 336 * Set up the initial limits on process VM. Set the maximum resident 337 * set size to be half of (reasonably) available memory. Since this 338 * is a soft limit, it comes into effect only when the system is out 339 * of memory - half of main memory helps to favor smaller processes, 340 * and reduces thrashing of the object cache. 341 */ 342 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 343 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 344 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 345 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 346 /* limit the limit to no less than 2MB */ 347 rss_limit = max(cnt.v_free_count, 512); 348 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 349 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 350} 351 352void 353faultin(p) 354 struct proc *p; 355{ 356 vm_offset_t i; 357 vm_offset_t ptaddr; 358 int s; 359 360 if ((p->p_flag & P_INMEM) == 0) { 361 vm_map_t map = &p->p_vmspace->vm_map; 362 pmap_t pmap = &p->p_vmspace->vm_pmap; 363 vm_page_t stkm, m; 364 vm_offset_t ptpa; 365 int error; 366 367 ++p->p_lock; 368 369 ptaddr = trunc_page((u_int) vtopte(kstack)); 370 (void) vm_fault(map, ptaddr, VM_PROT_READ|VM_PROT_WRITE, FALSE); 371 ptpa = pmap_extract(&p->p_vmspace->vm_pmap, ptaddr); 372 if (ptpa == 0) { 373 panic("vm_fork: no pte for UPAGES"); 374 } 375 stkm = PHYS_TO_VM_PAGE(ptpa); 376 vm_page_hold(stkm); 377 378 for(i=0;i<UPAGES;i++) { 379 int s; 380 s = splhigh(); 381 382retry: 383 if ((m = vm_page_lookup(p->p_vmspace->vm_upages_obj, i)) == NULL) { 384 if ((m = vm_page_alloc(p->p_vmspace->vm_upages_obj, i, VM_ALLOC_NORMAL)) == NULL) { 385 VM_WAIT; 386 goto retry; 387 } 388 } else { 389 if ((m->flags & PG_BUSY) || m->busy) { 390 m->flags |= PG_WANTED; 391 tsleep(m, PVM, "swinuw",0); 392 goto retry; 393 } 394 } 395 vm_page_wire(m); 396 if (m->valid == VM_PAGE_BITS_ALL) 397 m->flags &= ~PG_BUSY; 398 splx(s); 399 400 pmap_enter( pmap, (vm_offset_t) kstack + i * PAGE_SIZE, 401 VM_PAGE_TO_PHYS(m), VM_PROT_READ|VM_PROT_WRITE, TRUE); 402 pmap_kenter(((vm_offset_t) p->p_addr) + i * PAGE_SIZE, 403 VM_PAGE_TO_PHYS(m)); 404 if (m->valid != VM_PAGE_BITS_ALL) { 405 int rv; 406 rv = vm_pager_get_pages(p->p_vmspace->vm_upages_obj, 407 &m, 1, 0); 408 if (rv != VM_PAGER_OK) 409 panic("faultin: cannot get upages for proc: %d\n", p->p_pid); 410 m->valid = VM_PAGE_BITS_ALL; 411 m->flags &= ~PG_BUSY; 412 } 413 } 414 vm_page_unhold(stkm); 415 416 417 s = splhigh(); 418 419 if (p->p_stat == SRUN) 420 setrunqueue(p); 421 422 p->p_flag |= P_INMEM; 423 424 /* undo the effect of setting SLOCK above */ 425 --p->p_lock; 426 splx(s); 427 428 } 429} 430 431/* 432 * This swapin algorithm attempts to swap-in processes only if there 433 * is enough space for them. Of course, if a process waits for a long 434 * time, it will be swapped in anyway. 435 */ 436/* ARGSUSED*/ 437static void 438scheduler(dummy) 439 void *dummy; 440{ 441 register struct proc *p; 442 register int pri; 443 struct proc *pp; 444 int ppri; 445 446loop: 447 while ((cnt.v_free_count + cnt.v_cache_count) < (cnt.v_free_reserved + UPAGES + 2)) { 448 VM_WAIT; 449 } 450 451 pp = NULL; 452 ppri = INT_MIN; 453 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 454 if (p->p_stat == SRUN && 455 (p->p_flag & (P_INMEM | P_SWAPPING)) == 0) { 456 int mempri; 457 458 pri = p->p_swtime + p->p_slptime - p->p_nice * 8; 459 mempri = pri > 0 ? pri : 0; 460 /* 461 * if this process is higher priority and there is 462 * enough space, then select this process instead of 463 * the previous selection. 464 */ 465 if (pri > ppri) { 466 pp = p; 467 ppri = pri; 468 } 469 } 470 } 471 472 /* 473 * Nothing to do, back to sleep 474 */ 475 if ((p = pp) == NULL) { 476 tsleep(&proc0, PVM, "sched", 0); 477 goto loop; 478 } 479 /* 480 * We would like to bring someone in. (only if there is space). 481 */ 482 faultin(p); 483 p->p_swtime = 0; 484 goto loop; 485} 486 487#ifndef NO_SWAPPING 488 489#define swappable(p) \ 490 (((p)->p_lock == 0) && \ 491 ((p)->p_flag & (P_TRACED|P_NOSWAP|P_SYSTEM|P_INMEM|P_WEXIT|P_PHYSIO|P_SWAPPING)) == P_INMEM) 492 493/* 494 * Swapout is driven by the pageout daemon. Very simple, we find eligible 495 * procs and unwire their u-areas. We try to always "swap" at least one 496 * process in case we need the room for a swapin. 497 * If any procs have been sleeping/stopped for at least maxslp seconds, 498 * they are swapped. Else, we swap the longest-sleeping or stopped process, 499 * if any, otherwise the longest-resident process. 500 */ 501void 502swapout_procs() 503{ 504 register struct proc *p; 505 struct proc *outp, *outp2; 506 int outpri, outpri2; 507 int didswap = 0; 508 509 outp = outp2 = NULL; 510 outpri = outpri2 = INT_MIN; 511retry: 512 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 513 if (!swappable(p)) 514 continue; 515 switch (p->p_stat) { 516 default: 517 continue; 518 519 case SSLEEP: 520 case SSTOP: 521 /* 522 * do not swapout a realtime process 523 */ 524 if (p->p_rtprio.type == RTP_PRIO_REALTIME) 525 continue; 526 527 /* 528 * do not swapout a process waiting on a critical 529 * event of some kind 530 */ 531 if (((p->p_priority & 0x7f) < PSOCK) || 532 (p->p_slptime <= 4)) 533 continue; 534 535 vm_map_reference(&p->p_vmspace->vm_map); 536 /* 537 * do not swapout a process that is waiting for VM 538 * datastructures there is a possible deadlock. 539 */ 540 if (!lock_try_write(&p->p_vmspace->vm_map.lock)) { 541 vm_map_deallocate(&p->p_vmspace->vm_map); 542 continue; 543 } 544 vm_map_unlock(&p->p_vmspace->vm_map); 545 /* 546 * If the process has been asleep for awhile and had 547 * most of its pages taken away already, swap it out. 548 */ 549 swapout(p); 550 vm_map_deallocate(&p->p_vmspace->vm_map); 551 didswap++; 552 goto retry; 553 } 554 } 555 /* 556 * If we swapped something out, and another process needed memory, 557 * then wakeup the sched process. 558 */ 559 if (didswap) 560 wakeup(&proc0); 561} 562 563static void 564swapout(p) 565 register struct proc *p; 566{ 567 vm_map_t map = &p->p_vmspace->vm_map; 568 pmap_t pmap = &p->p_vmspace->vm_pmap; 569 vm_offset_t ptaddr; 570 int i; 571 572 ++p->p_stats->p_ru.ru_nswap; 573 /* 574 * remember the process resident count 575 */ 576 p->p_vmspace->vm_swrss = 577 p->p_vmspace->vm_pmap.pm_stats.resident_count; 578 579 (void) splhigh(); 580 p->p_flag &= ~P_INMEM; 581 p->p_flag |= P_SWAPPING; 582 if (p->p_stat == SRUN) 583 remrq(p); 584 (void) spl0(); 585 586 /* 587 * let the upages be paged 588 */ 589 for(i=0;i<UPAGES;i++) { 590 vm_page_t m; 591 if ((m = vm_page_lookup(p->p_vmspace->vm_upages_obj, i)) == NULL) 592 panic("swapout: upage already missing???"); 593 m->dirty = VM_PAGE_BITS_ALL; 594 vm_page_unwire(m); 595 pmap_kremove( (vm_offset_t) p->p_addr + PAGE_SIZE * i); 596 } 597 pmap_remove(pmap, (vm_offset_t) kstack, 598 (vm_offset_t) kstack + PAGE_SIZE * UPAGES); 599 600 p->p_flag &= ~P_SWAPPING; 601 p->p_swtime = 0; 602} 603#endif /* !NO_SWAPPING */ 604 605#ifdef DDB 606/* 607 * DEBUG stuff 608 */ 609 610int indent; 611 612#include <machine/stdarg.h> /* see subr_prf.c */ 613 614/*ARGSUSED2*/ 615void 616#if __STDC__ 617iprintf(const char *fmt,...) 618#else 619iprintf(fmt /* , va_alist */ ) 620 char *fmt; 621 622 /* va_dcl */ 623#endif 624{ 625 register int i; 626 va_list ap; 627 628 for (i = indent; i >= 8; i -= 8) 629 printf("\t"); 630 while (--i >= 0) 631 printf(" "); 632 va_start(ap, fmt); 633 vprintf(fmt, ap); 634 va_end(ap); 635} 636#endif /* DDB */ 637