1 /* $OpenBSD: uvm_glue.c,v 1.31 2001/12/19 08:58:07 art Exp $ */ 2 /* $NetBSD: uvm_glue.c,v 1.44 2001/02/06 19:54:44 eeh Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993, The Regents of the University of California. 7 * 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Charles D. Cranor, 24 * Washington University, the University of California, Berkeley and 25 * its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 43 * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp 44 * 45 * 46 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 47 * All rights reserved. 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70 /* 71 * uvm_glue.c: glue functions 72 */ 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/proc.h> 77 #include <sys/resourcevar.h> 78 #include <sys/buf.h> 79 #include <sys/user.h> 80 #ifdef SYSVSHM 81 #include <sys/shm.h> 82 #endif 83 84 #include <uvm/uvm.h> 85 86 #include <machine/cpu.h> 87 88 /* 89 * local prototypes 90 */ 91 92 static void uvm_swapout __P((struct proc *)); 93 94 /* 95 * XXXCDC: do these really belong here? 96 */ 97 98 int readbuffers = 0; /* allow KGDB to read kern buffer pool */ 99 /* XXX: see uvm_kernacc */ 100 101 102 /* 103 * uvm_kernacc: can the kernel access a region of memory 104 * 105 * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c) 106 */ 107 108 boolean_t 109 uvm_kernacc(addr, len, rw) 110 caddr_t addr; 111 size_t len; 112 int rw; 113 { 114 boolean_t rv; 115 vaddr_t saddr, eaddr; 116 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 117 118 saddr = trunc_page((vaddr_t)addr); 119 eaddr = round_page((vaddr_t)addr + len); 120 vm_map_lock_read(kernel_map); 121 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot); 122 vm_map_unlock_read(kernel_map); 123 124 /* 125 * XXX there are still some things (e.g. the buffer cache) that 126 * are managed behind the VM system's back so even though an 127 * address is accessible in the mind of the VM system, there may 128 * not be physical pages where the VM thinks there is. This can 129 * lead to bogus allocation of pages in the kernel address space 130 * or worse, inconsistencies at the pmap level. We only worry 131 * about the buffer cache for now. 132 */ 133 if (!readbuffers && rv && (eaddr > (vaddr_t)buffers && 134 saddr < (vaddr_t)buffers + MAXBSIZE * nbuf)) 135 rv = FALSE; 136 return(rv); 137 } 138 139 /* 140 * uvm_useracc: can the user access it? 141 * 142 * - called from physio() and sys___sysctl(). 143 */ 144 145 boolean_t 146 uvm_useracc(addr, len, rw) 147 caddr_t addr; 148 size_t len; 149 int rw; 150 { 151 vm_map_t map; 152 boolean_t rv; 153 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 154 155 /* XXX curproc */ 156 map = &curproc->p_vmspace->vm_map; 157 158 vm_map_lock_read(map); 159 rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr), 160 round_page((vaddr_t)addr + len), prot); 161 vm_map_unlock_read(map); 162 163 return(rv); 164 } 165 166 #ifdef KGDB 167 /* 168 * Change protections on kernel pages from addr to addr+len 169 * (presumably so debugger can plant a breakpoint). 170 * 171 * We force the protection change at the pmap level. If we were 172 * to use vm_map_protect a change to allow writing would be lazily- 173 * applied meaning we would still take a protection fault, something 174 * we really don't want to do. It would also fragment the kernel 175 * map unnecessarily. We cannot use pmap_protect since it also won't 176 * enforce a write-enable request. Using pmap_enter is the only way 177 * we can ensure the change takes place properly. 178 */ 179 void 180 uvm_chgkprot(addr, len, rw) 181 caddr_t addr; 182 size_t len; 183 int rw; 184 { 185 vm_prot_t prot; 186 paddr_t pa; 187 vaddr_t sva, eva; 188 189 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE; 190 eva = round_page((vaddr_t)addr + len); 191 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) { 192 /* 193 * Extract physical address for the page. 194 * We use a cheezy hack to differentiate physical 195 * page 0 from an invalid mapping, not that it 196 * really matters... 197 */ 198 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE) 199 panic("chgkprot: invalid page"); 200 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 201 } 202 } 203 #endif 204 205 /* 206 * vslock: wire user memory for I/O 207 * 208 * - called from physio and sys___sysctl 209 * - XXXCDC: consider nuking this (or making it a macro?) 210 */ 211 212 int 213 uvm_vslock(p, addr, len, access_type) 214 struct proc *p; 215 caddr_t addr; 216 size_t len; 217 vm_prot_t access_type; 218 { 219 vm_map_t map; 220 vaddr_t start, end; 221 int rv; 222 223 map = &p->p_vmspace->vm_map; 224 start = trunc_page((vaddr_t)addr); 225 end = round_page((vaddr_t)addr + len); 226 227 rv = uvm_fault_wire(map, start, end, access_type); 228 229 return (rv); 230 } 231 232 /* 233 * vslock: wire user memory for I/O 234 * 235 * - called from physio and sys___sysctl 236 * - XXXCDC: consider nuking this (or making it a macro?) 237 */ 238 239 void 240 uvm_vsunlock(p, addr, len) 241 struct proc *p; 242 caddr_t addr; 243 size_t len; 244 { 245 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr), 246 round_page((vaddr_t)addr + len)); 247 } 248 249 /* 250 * uvm_fork: fork a virtual address space 251 * 252 * - the address space is copied as per parent map's inherit values 253 * - a new "user" structure is allocated for the child process 254 * [filled in by MD layer...] 255 * - if specified, the child gets a new user stack described by 256 * stack and stacksize 257 * - NOTE: the kernel stack may be at a different location in the child 258 * process, and thus addresses of automatic variables may be invalid 259 * after cpu_fork returns in the child process. We do nothing here 260 * after cpu_fork returns. 261 * - XXXCDC: we need a way for this to return a failure value rather 262 * than just hang 263 */ 264 void 265 uvm_fork(p1, p2, shared, stack, stacksize, func, arg) 266 struct proc *p1, *p2; 267 boolean_t shared; 268 void *stack; 269 size_t stacksize; 270 void (*func) __P((void *)); 271 void *arg; 272 { 273 struct user *up = p2->p_addr; 274 int rv; 275 276 if (shared == TRUE) { 277 p2->p_vmspace = NULL; 278 uvmspace_share(p1, p2); /* share vmspace */ 279 } else 280 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); /* fork vmspace */ 281 282 /* 283 * Wire down the U-area for the process, which contains the PCB 284 * and the kernel stack. Wired state is stored in p->p_flag's 285 * P_INMEM bit rather than in the vm_map_entry's wired count 286 * to prevent kernel_map fragmentation. 287 * 288 * Note the kernel stack gets read/write accesses right off 289 * the bat. 290 */ 291 rv = uvm_fault_wire(kernel_map, (vaddr_t)up, 292 (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE); 293 if (rv != KERN_SUCCESS) 294 panic("uvm_fork: uvm_fault_wire failed: %d", rv); 295 296 /* 297 * p_stats currently points at a field in the user struct. Copy 298 * parts of p_stats, and zero out the rest. 299 */ 300 p2->p_stats = &up->u_stats; 301 memset(&up->u_stats.pstat_startzero, 0, 302 ((caddr_t)&up->u_stats.pstat_endzero - 303 (caddr_t)&up->u_stats.pstat_startzero)); 304 memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy, 305 ((caddr_t)&up->u_stats.pstat_endcopy - 306 (caddr_t)&up->u_stats.pstat_startcopy)); 307 308 /* 309 * cpu_fork() copy and update the pcb, and make the child ready 310 * to run. If this is a normal user fork, the child will exit 311 * directly to user mode via child_return() on its first time 312 * slice and will not return here. If this is a kernel thread, 313 * the specified entry point will be executed. 314 */ 315 cpu_fork(p1, p2, stack, stacksize, func, arg); 316 } 317 318 /* 319 * uvm_exit: exit a virtual address space 320 * 321 * - the process passed to us is a dead (pre-zombie) process; we 322 * are running on a different context now (the reaper). 323 * - we must run in a separate thread because freeing the vmspace 324 * of the dead process may block. 325 */ 326 void 327 uvm_exit(p) 328 struct proc *p; 329 { 330 vaddr_t va = (vaddr_t)p->p_addr; 331 332 uvmspace_free(p->p_vmspace); 333 p->p_flag &= ~P_INMEM; 334 uvm_fault_unwire(kernel_map, va, va + USPACE); 335 uvm_km_free(kernel_map, va, USPACE); 336 p->p_addr = NULL; 337 } 338 339 /* 340 * uvm_init_limit: init per-process VM limits 341 * 342 * - called for process 0 and then inherited by all others. 343 */ 344 void 345 uvm_init_limits(p) 346 struct proc *p; 347 { 348 349 /* 350 * Set up the initial limits on process VM. Set the maximum 351 * resident set size to be all of (reasonably) available memory. 352 * This causes any single, large process to start random page 353 * replacement once it fills memory. 354 */ 355 356 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 357 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 358 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 359 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 360 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free); 361 } 362 363 #ifdef DEBUG 364 int enableswap = 1; 365 int swapdebug = 0; 366 #define SDB_FOLLOW 1 367 #define SDB_SWAPIN 2 368 #define SDB_SWAPOUT 4 369 #endif 370 371 /* 372 * uvm_swapin: swap in a process's u-area. 373 */ 374 375 void 376 uvm_swapin(p) 377 struct proc *p; 378 { 379 vaddr_t addr; 380 int s; 381 382 addr = (vaddr_t)p->p_addr; 383 /* make P_INMEM true */ 384 uvm_fault_wire(kernel_map, addr, addr + USPACE, 385 VM_PROT_READ | VM_PROT_WRITE); 386 387 /* 388 * Some architectures need to be notified when the user area has 389 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c). 390 */ 391 cpu_swapin(p); 392 s = splstatclock(); 393 if (p->p_stat == SRUN) 394 setrunqueue(p); 395 p->p_flag |= P_INMEM; 396 splx(s); 397 p->p_swtime = 0; 398 ++uvmexp.swapins; 399 } 400 401 /* 402 * uvm_scheduler: process zero main loop 403 * 404 * - attempt to swapin every swaped-out, runnable process in order of 405 * priority. 406 * - if not enough memory, wake the pagedaemon and let it clear space. 407 */ 408 409 void 410 uvm_scheduler() 411 { 412 struct proc *p; 413 int pri; 414 struct proc *pp; 415 int ppri; 416 417 loop: 418 #ifdef DEBUG 419 while (!enableswap) 420 tsleep(&proc0, PVM, "noswap", 0); 421 #endif 422 pp = NULL; /* process to choose */ 423 ppri = INT_MIN; /* its priority */ 424 LIST_FOREACH(p, &allproc, p_list) { 425 426 /* is it a runnable swapped out process? */ 427 if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) { 428 pri = p->p_swtime + p->p_slptime - 429 (p->p_nice - NZERO) * 8; 430 if (pri > ppri) { /* higher priority? remember it. */ 431 pp = p; 432 ppri = pri; 433 } 434 } 435 } 436 437 #ifdef DEBUG 438 if (swapdebug & SDB_FOLLOW) 439 printf("scheduler: running, procp %p pri %d\n", pp, ppri); 440 #endif 441 /* 442 * Nothing to do, back to sleep 443 */ 444 if ((p = pp) == NULL) { 445 tsleep(&proc0, PVM, "scheduler", 0); 446 goto loop; 447 } 448 449 /* 450 * we have found swapped out process which we would like to bring 451 * back in. 452 * 453 * XXX: this part is really bogus cuz we could deadlock on memory 454 * despite our feeble check 455 */ 456 if (uvmexp.free > atop(USPACE)) { 457 #ifdef DEBUG 458 if (swapdebug & SDB_SWAPIN) 459 printf("swapin: pid %d(%s)@%p, pri %d free %d\n", 460 p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free); 461 #endif 462 uvm_swapin(p); 463 goto loop; 464 } 465 /* 466 * not enough memory, jab the pageout daemon and wait til the coast 467 * is clear 468 */ 469 #ifdef DEBUG 470 if (swapdebug & SDB_FOLLOW) 471 printf("scheduler: no room for pid %d(%s), free %d\n", 472 p->p_pid, p->p_comm, uvmexp.free); 473 #endif 474 uvm_wait("schedpwait"); 475 #ifdef DEBUG 476 if (swapdebug & SDB_FOLLOW) 477 printf("scheduler: room again, free %d\n", uvmexp.free); 478 #endif 479 goto loop; 480 } 481 482 /* 483 * swappable: is process "p" swappable? 484 */ 485 486 #define swappable(p) \ 487 (((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM && \ 488 (p)->p_holdcnt == 0) 489 490 /* 491 * swapout_threads: find threads that can be swapped and unwire their 492 * u-areas. 493 * 494 * - called by the pagedaemon 495 * - try and swap at least one processs 496 * - processes that are sleeping or stopped for maxslp or more seconds 497 * are swapped... otherwise the longest-sleeping or stopped process 498 * is swapped, otherwise the longest resident process... 499 */ 500 void 501 uvm_swapout_threads() 502 { 503 struct proc *p; 504 struct proc *outp, *outp2; 505 int outpri, outpri2; 506 int didswap = 0; 507 extern int maxslp; 508 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */ 509 510 #ifdef DEBUG 511 if (!enableswap) 512 return; 513 #endif 514 515 /* 516 * outp/outpri : stop/sleep process with largest sleeptime < maxslp 517 * outp2/outpri2: the longest resident process (its swap time) 518 */ 519 outp = outp2 = NULL; 520 outpri = outpri2 = 0; 521 LIST_FOREACH(p, &allproc, p_list) { 522 if (!swappable(p)) 523 continue; 524 switch (p->p_stat) { 525 case SRUN: 526 if (p->p_swtime > outpri2) { 527 outp2 = p; 528 outpri2 = p->p_swtime; 529 } 530 continue; 531 532 case SSLEEP: 533 case SSTOP: 534 if (p->p_slptime >= maxslp) { 535 uvm_swapout(p); 536 didswap++; 537 } else if (p->p_slptime > outpri) { 538 outp = p; 539 outpri = p->p_slptime; 540 } 541 continue; 542 } 543 } 544 545 /* 546 * If we didn't get rid of any real duds, toss out the next most 547 * likely sleeping/stopped or running candidate. We only do this 548 * if we are real low on memory since we don't gain much by doing 549 * it (USPACE bytes). 550 */ 551 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) { 552 if ((p = outp) == NULL) 553 p = outp2; 554 #ifdef DEBUG 555 if (swapdebug & SDB_SWAPOUT) 556 printf("swapout_threads: no duds, try procp %p\n", p); 557 #endif 558 if (p) 559 uvm_swapout(p); 560 } 561 } 562 563 /* 564 * uvm_swapout: swap out process "p" 565 * 566 * - currently "swapout" means "unwire U-area" and "pmap_collect()" 567 * the pmap. 568 * - XXXCDC: should deactivate all process' private anonymous memory 569 */ 570 571 static void 572 uvm_swapout(p) 573 struct proc *p; 574 { 575 vaddr_t addr; 576 int s; 577 578 #ifdef DEBUG 579 if (swapdebug & SDB_SWAPOUT) 580 printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n", 581 p->p_pid, p->p_comm, p->p_addr, p->p_stat, 582 p->p_slptime, uvmexp.free); 583 #endif 584 585 /* 586 * Do any machine-specific actions necessary before swapout. 587 * This can include saving floating point state, etc. 588 */ 589 cpu_swapout(p); 590 591 /* 592 * Mark it as (potentially) swapped out. 593 */ 594 s = splstatclock(); 595 p->p_flag &= ~P_INMEM; 596 if (p->p_stat == SRUN) 597 remrunqueue(p); 598 splx(s); 599 p->p_swtime = 0; 600 ++uvmexp.swapouts; 601 602 /* 603 * Unwire the to-be-swapped process's user struct and kernel stack. 604 */ 605 addr = (vaddr_t)p->p_addr; 606 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */ 607 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map)); 608 } 609 610