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