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