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