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.29 1995/10/23 05:35:42 dyson 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 90 static void vm_init_limits __P((void *)); 91 SYSINIT(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 */ 98 static void scheduler __P((void *)); 99 SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 100 101 102 extern char kstack[]; 103 104 /* vm_map_t upages_map; */ 105 106 int 107 kernacc(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 121 int 122 useracc(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 */ 153 chgkprot(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 163 void 164 vslock(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 172 void 173 vsunlock(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 */ 196 int 197 vm_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 * PAGE_SIZE, 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 + PAGE_SIZE, 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 * PAGE_SIZE, 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 * PAGE_SIZE); 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_kenter(((vm_offset_t) up) + PAGE_SIZE * i, 255 pmap_extract(vp->pmap, addr + PAGE_SIZE * i)); 256 257 p2->p_addr = up; 258 259 /* 260 * p_stats and p_sigacts currently point at fields in the user struct 261 * but not at &u, instead at p_addr. Copy p_sigacts and parts of 262 * p_stats; zero the rest of p_stats (statistics). 263 */ 264 p2->p_stats = &up->u_stats; 265 p2->p_sigacts = &up->u_sigacts; 266 up->u_sigacts = *p1->p_sigacts; 267 bzero(&up->u_stats.pstat_startzero, 268 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 269 (caddr_t) &up->u_stats.pstat_startzero)); 270 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 271 ((caddr_t) &up->u_stats.pstat_endcopy - 272 (caddr_t) &up->u_stats.pstat_startcopy)); 273 274 275 /* 276 * cpu_fork will copy and update the kernel stack and pcb, and make 277 * the child ready to run. It marks the child so that it can return 278 * differently than the parent. It returns twice, once in the parent 279 * process and once in the child. 280 */ 281 return (cpu_fork(p1, p2)); 282 } 283 284 /* 285 * Set default limits for VM system. 286 * Called for proc 0, and then inherited by all others. 287 * 288 * XXX should probably act directly on proc0. 289 */ 290 static void 291 vm_init_limits(udata) 292 void *udata; 293 { 294 register struct proc *p = (struct proc *)udata; 295 int rss_limit; 296 297 /* 298 * Set up the initial limits on process VM. Set the maximum resident 299 * set size to be half of (reasonably) available memory. Since this 300 * is a soft limit, it comes into effect only when the system is out 301 * of memory - half of main memory helps to favor smaller processes, 302 * and reduces thrashing of the object cache. 303 */ 304 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 305 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 306 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 307 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 308 /* limit the limit to no less than 2MB */ 309 rss_limit = max(cnt.v_free_count, 512); 310 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 311 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 312 } 313 314 void 315 faultin(p) 316 struct proc *p; 317 { 318 vm_offset_t i; 319 vm_offset_t ptaddr; 320 int s; 321 322 if ((p->p_flag & P_INMEM) == 0) { 323 vm_map_t map; 324 int error; 325 326 ++p->p_lock; 327 328 map = &p->p_vmspace->vm_map; 329 /* force the page table encompassing the kernel stack (upages) */ 330 ptaddr = trunc_page((u_int) vtopte(kstack)); 331 error = vm_map_pageable(map, ptaddr, ptaddr + PAGE_SIZE, FALSE); 332 if (error) 333 panic("faultin: wire of PT failed. error=%d", error); 334 335 /* wire in the UPAGES */ 336 error = vm_map_pageable(map, (vm_offset_t) kstack, 337 (vm_offset_t) kstack + UPAGES * PAGE_SIZE, FALSE); 338 if (error) 339 panic("faultin: wire of UPAGES failed. error=%d", error); 340 341 /* and map them nicely into the kernel pmap */ 342 for (i = 0; i < UPAGES; i++) { 343 vm_offset_t off = i * PAGE_SIZE; 344 vm_offset_t pa = (vm_offset_t) 345 pmap_extract(&p->p_vmspace->vm_pmap, 346 (vm_offset_t) kstack + off); 347 348 if (pa == 0) 349 panic("faultin: missing page for UPAGES\n"); 350 351 pmap_kenter(((vm_offset_t) p->p_addr) + off, pa); 352 } 353 354 s = splhigh(); 355 356 if (p->p_stat == SRUN) 357 setrunqueue(p); 358 359 p->p_flag |= P_INMEM; 360 361 /* undo the effect of setting SLOCK above */ 362 --p->p_lock; 363 splx(s); 364 365 } 366 } 367 368 /* 369 * This swapin algorithm attempts to swap-in processes only if there 370 * is enough space for them. Of course, if a process waits for a long 371 * time, it will be swapped in anyway. 372 */ 373 /* ARGSUSED*/ 374 static void 375 scheduler(udata) 376 void *udata; /* not used*/ 377 { 378 register struct proc *p; 379 register int pri; 380 struct proc *pp; 381 int ppri; 382 383 loop: 384 while ((cnt.v_free_count + cnt.v_cache_count) < (cnt.v_free_reserved + UPAGES + 2)) { 385 VM_WAIT; 386 } 387 388 pp = NULL; 389 ppri = INT_MIN; 390 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 391 if (p->p_stat == SRUN && (p->p_flag & (P_INMEM | P_SWAPPING)) == 0) { 392 int mempri; 393 394 pri = p->p_swtime + p->p_slptime - p->p_nice * 8; 395 mempri = pri > 0 ? pri : 0; 396 /* 397 * if this process is higher priority and there is 398 * enough space, then select this process instead of 399 * the previous selection. 400 */ 401 if (pri > ppri) { 402 pp = p; 403 ppri = pri; 404 } 405 } 406 } 407 408 /* 409 * Nothing to do, back to sleep 410 */ 411 if ((p = pp) == NULL) { 412 tsleep(&proc0, PVM, "sched", 0); 413 goto loop; 414 } 415 /* 416 * We would like to bring someone in. (only if there is space). 417 */ 418 faultin(p); 419 p->p_swtime = 0; 420 goto loop; 421 } 422 423 #define swappable(p) \ 424 (((p)->p_lock == 0) && \ 425 ((p)->p_flag & (P_TRACED|P_NOSWAP|P_SYSTEM|P_INMEM|P_WEXIT|P_PHYSIO|P_SWAPPING)) == P_INMEM) 426 427 extern int vm_pageout_free_min; 428 429 /* 430 * Swapout is driven by the pageout daemon. Very simple, we find eligible 431 * procs and unwire their u-areas. We try to always "swap" at least one 432 * process in case we need the room for a swapin. 433 * If any procs have been sleeping/stopped for at least maxslp seconds, 434 * they are swapped. Else, we swap the longest-sleeping or stopped process, 435 * if any, otherwise the longest-resident process. 436 */ 437 void 438 swapout_procs() 439 { 440 register struct proc *p; 441 struct proc *outp, *outp2; 442 int outpri, outpri2; 443 int didswap = 0; 444 445 outp = outp2 = NULL; 446 outpri = outpri2 = INT_MIN; 447 retry: 448 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 449 if (!swappable(p)) 450 continue; 451 switch (p->p_stat) { 452 default: 453 continue; 454 455 case SSLEEP: 456 case SSTOP: 457 /* 458 * do not swapout a realtime process 459 */ 460 if (p->p_rtprio.type == RTP_PRIO_REALTIME) 461 continue; 462 463 /* 464 * do not swapout a process waiting on a critical 465 * event of some kind 466 */ 467 if (((p->p_priority & 0x7f) < PSOCK) || 468 (p->p_slptime <= 4)) 469 continue; 470 471 vm_map_reference(&p->p_vmspace->vm_map); 472 /* 473 * do not swapout a process that is waiting for VM 474 * datastructures there is a possible deadlock. 475 */ 476 if (!lock_try_write(&p->p_vmspace->vm_map.lock)) { 477 vm_map_deallocate(&p->p_vmspace->vm_map); 478 continue; 479 } 480 vm_map_unlock(&p->p_vmspace->vm_map); 481 /* 482 * If the process has been asleep for awhile and had 483 * most of its pages taken away already, swap it out. 484 */ 485 swapout(p); 486 vm_map_deallocate(&p->p_vmspace->vm_map); 487 didswap++; 488 goto retry; 489 } 490 } 491 /* 492 * If we swapped something out, and another process needed memory, 493 * then wakeup the sched process. 494 */ 495 if (didswap) 496 wakeup(&proc0); 497 } 498 499 void 500 swapout(p) 501 register struct proc *p; 502 { 503 vm_map_t map = &p->p_vmspace->vm_map; 504 vm_offset_t ptaddr; 505 int i; 506 507 ++p->p_stats->p_ru.ru_nswap; 508 /* 509 * remember the process resident count 510 */ 511 p->p_vmspace->vm_swrss = 512 p->p_vmspace->vm_pmap.pm_stats.resident_count; 513 514 (void) splhigh(); 515 p->p_flag &= ~P_INMEM; 516 p->p_flag |= P_SWAPPING; 517 if (p->p_stat == SRUN) 518 remrq(p); 519 (void) spl0(); 520 521 /* 522 * let the upages be paged 523 */ 524 for(i=0;i<UPAGES;i++) 525 pmap_kremove( (vm_offset_t) p->p_addr + PAGE_SIZE * i); 526 527 vm_map_pageable(map, (vm_offset_t) kstack, 528 (vm_offset_t) kstack + UPAGES * PAGE_SIZE, TRUE); 529 530 ptaddr = trunc_page((u_int) vtopte(kstack)); 531 vm_map_pageable(map, ptaddr, ptaddr + PAGE_SIZE, TRUE); 532 533 p->p_flag &= ~P_SWAPPING; 534 p->p_swtime = 0; 535 } 536 537 #ifdef DDB 538 /* 539 * DEBUG stuff 540 */ 541 542 int indent; 543 544 #include <machine/stdarg.h> /* see subr_prf.c */ 545 546 /*ARGSUSED2*/ 547 void 548 #if __STDC__ 549 iprintf(const char *fmt,...) 550 #else 551 iprintf(fmt /* , va_alist */ ) 552 char *fmt; 553 554 /* va_dcl */ 555 #endif 556 { 557 register int i; 558 va_list ap; 559 560 for (i = indent; i >= 8; i -= 8) 561 printf("\t"); 562 while (--i >= 0) 563 printf(" "); 564 va_start(ap, fmt); 565 printf("%r", fmt, ap); 566 va_end(ap); 567 } 568 #endif /* DDB */ 569