1 /* $NetBSD: uvm_glue.c,v 1.95 2006/06/13 13:22:06 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 * Copyright (c) 1991, 1993, The Regents of the University of California. 6 * 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by Charles D. Cranor, 23 * Washington University, the University of California, Berkeley and 24 * its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 42 * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp 43 * 44 * 45 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46 * All rights reserved. 47 * 48 * Permission to use, copy, modify and distribute this software and 49 * its documentation is hereby granted, provided that both the copyright 50 * notice and this permission notice appear in all copies of the 51 * software, derivative works or modified versions, and any portions 52 * thereof, and that both notices appear in supporting documentation. 53 * 54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57 * 58 * Carnegie Mellon requests users of this software to return to 59 * 60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61 * School of Computer Science 62 * Carnegie Mellon University 63 * Pittsburgh PA 15213-3890 64 * 65 * any improvements or extensions that they make and grant Carnegie the 66 * rights to redistribute these changes. 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.95 2006/06/13 13:22:06 yamt Exp $"); 71 72 #include "opt_kgdb.h" 73 #include "opt_kstack.h" 74 #include "opt_uvmhist.h" 75 76 /* 77 * uvm_glue.c: glue functions 78 */ 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/proc.h> 83 #include <sys/resourcevar.h> 84 #include <sys/buf.h> 85 #include <sys/user.h> 86 87 #include <uvm/uvm.h> 88 89 #include <machine/cpu.h> 90 91 /* 92 * local prototypes 93 */ 94 95 static void uvm_swapout(struct lwp *); 96 97 #define UVM_NUAREA_MAX 16 98 static vaddr_t uvm_uareas; 99 static int uvm_nuarea; 100 static struct simplelock uvm_uareas_slock = SIMPLELOCK_INITIALIZER; 101 #define UAREA_NEXTFREE(uarea) (*(vaddr_t *)(UAREA_TO_USER(uarea))) 102 103 static void uvm_uarea_free(vaddr_t); 104 105 /* 106 * XXXCDC: do these really belong here? 107 */ 108 109 /* 110 * uvm_kernacc: can the kernel access a region of memory 111 * 112 * - used only by /dev/kmem driver (mem.c) 113 */ 114 115 boolean_t 116 uvm_kernacc(caddr_t addr, size_t len, int rw) 117 { 118 boolean_t rv; 119 vaddr_t saddr, eaddr; 120 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 121 122 saddr = trunc_page((vaddr_t)addr); 123 eaddr = round_page((vaddr_t)addr + len); 124 vm_map_lock_read(kernel_map); 125 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot); 126 vm_map_unlock_read(kernel_map); 127 128 return(rv); 129 } 130 131 #ifdef KGDB 132 /* 133 * Change protections on kernel pages from addr to addr+len 134 * (presumably so debugger can plant a breakpoint). 135 * 136 * We force the protection change at the pmap level. If we were 137 * to use vm_map_protect a change to allow writing would be lazily- 138 * applied meaning we would still take a protection fault, something 139 * we really don't want to do. It would also fragment the kernel 140 * map unnecessarily. We cannot use pmap_protect since it also won't 141 * enforce a write-enable request. Using pmap_enter is the only way 142 * we can ensure the change takes place properly. 143 */ 144 void 145 uvm_chgkprot(caddr_t addr, size_t len, int rw) 146 { 147 vm_prot_t prot; 148 paddr_t pa; 149 vaddr_t sva, eva; 150 151 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE; 152 eva = round_page((vaddr_t)addr + len); 153 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) { 154 /* 155 * Extract physical address for the page. 156 */ 157 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE) 158 panic("chgkprot: invalid page"); 159 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED); 160 } 161 pmap_update(pmap_kernel()); 162 } 163 #endif 164 165 /* 166 * uvm_vslock: wire user memory for I/O 167 * 168 * - called from physio and sys___sysctl 169 * - XXXCDC: consider nuking this (or making it a macro?) 170 */ 171 172 int 173 uvm_vslock(struct proc *p, caddr_t addr, size_t len, vm_prot_t access_type) 174 { 175 struct vm_map *map; 176 vaddr_t start, end; 177 int error; 178 179 map = &p->p_vmspace->vm_map; 180 start = trunc_page((vaddr_t)addr); 181 end = round_page((vaddr_t)addr + len); 182 error = uvm_fault_wire(map, start, end, access_type, 0); 183 return error; 184 } 185 186 /* 187 * uvm_vsunlock: unwire user memory wired by uvm_vslock() 188 * 189 * - called from physio and sys___sysctl 190 * - XXXCDC: consider nuking this (or making it a macro?) 191 */ 192 193 void 194 uvm_vsunlock(struct proc *p, caddr_t addr, size_t len) 195 { 196 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr), 197 round_page((vaddr_t)addr + len)); 198 } 199 200 /* 201 * uvm_proc_fork: fork a virtual address space 202 * 203 * - the address space is copied as per parent map's inherit values 204 */ 205 void 206 uvm_proc_fork(struct proc *p1, struct proc *p2, boolean_t shared) 207 { 208 209 if (shared == TRUE) { 210 p2->p_vmspace = NULL; 211 uvmspace_share(p1, p2); 212 } else { 213 p2->p_vmspace = uvmspace_fork(p1->p_vmspace); 214 } 215 216 cpu_proc_fork(p1, p2); 217 } 218 219 220 /* 221 * uvm_lwp_fork: fork a thread 222 * 223 * - a new "user" structure is allocated for the child process 224 * [filled in by MD layer...] 225 * - if specified, the child gets a new user stack described by 226 * stack and stacksize 227 * - NOTE: the kernel stack may be at a different location in the child 228 * process, and thus addresses of automatic variables may be invalid 229 * after cpu_lwp_fork returns in the child process. We do nothing here 230 * after cpu_lwp_fork returns. 231 * - XXXCDC: we need a way for this to return a failure value rather 232 * than just hang 233 */ 234 void 235 uvm_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize, 236 void (*func)(void *), void *arg) 237 { 238 int error; 239 240 /* 241 * Wire down the U-area for the process, which contains the PCB 242 * and the kernel stack. Wired state is stored in l->l_flag's 243 * L_INMEM bit rather than in the vm_map_entry's wired count 244 * to prevent kernel_map fragmentation. If we reused a cached U-area, 245 * L_INMEM will already be set and we don't need to do anything. 246 * 247 * Note the kernel stack gets read/write accesses right off the bat. 248 */ 249 250 if ((l2->l_flag & L_INMEM) == 0) { 251 vaddr_t uarea = USER_TO_UAREA(l2->l_addr); 252 253 error = uvm_fault_wire(kernel_map, uarea, 254 uarea + USPACE, VM_PROT_READ | VM_PROT_WRITE, 0); 255 if (error) 256 panic("uvm_lwp_fork: uvm_fault_wire failed: %d", error); 257 #ifdef PMAP_UAREA 258 /* Tell the pmap this is a u-area mapping */ 259 PMAP_UAREA(uarea); 260 #endif 261 l2->l_flag |= L_INMEM; 262 } 263 264 #ifdef KSTACK_CHECK_MAGIC 265 /* 266 * fill stack with magic number 267 */ 268 kstack_setup_magic(l2); 269 #endif 270 271 /* 272 * cpu_lwp_fork() copy and update the pcb, and make the child ready 273 * to run. If this is a normal user fork, the child will exit 274 * directly to user mode via child_return() on its first time 275 * slice and will not return here. If this is a kernel thread, 276 * the specified entry point will be executed. 277 */ 278 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg); 279 } 280 281 /* 282 * uvm_uarea_alloc: allocate a u-area 283 */ 284 285 boolean_t 286 uvm_uarea_alloc(vaddr_t *uaddrp) 287 { 288 vaddr_t uaddr; 289 290 #ifndef USPACE_ALIGN 291 #define USPACE_ALIGN 0 292 #endif 293 294 simple_lock(&uvm_uareas_slock); 295 if (uvm_nuarea > 0) { 296 uaddr = uvm_uareas; 297 uvm_uareas = UAREA_NEXTFREE(uaddr); 298 uvm_nuarea--; 299 simple_unlock(&uvm_uareas_slock); 300 *uaddrp = uaddr; 301 return TRUE; 302 } else { 303 simple_unlock(&uvm_uareas_slock); 304 *uaddrp = uvm_km_alloc(kernel_map, USPACE, USPACE_ALIGN, 305 UVM_KMF_PAGEABLE); 306 return FALSE; 307 } 308 } 309 310 /* 311 * uvm_uarea_free: free a u-area; never blocks 312 */ 313 314 static inline void 315 uvm_uarea_free(vaddr_t uaddr) 316 { 317 simple_lock(&uvm_uareas_slock); 318 UAREA_NEXTFREE(uaddr) = uvm_uareas; 319 uvm_uareas = uaddr; 320 uvm_nuarea++; 321 simple_unlock(&uvm_uareas_slock); 322 } 323 324 /* 325 * uvm_uarea_drain: return memory of u-areas over limit 326 * back to system 327 */ 328 329 void 330 uvm_uarea_drain(boolean_t empty) 331 { 332 int leave = empty ? 0 : UVM_NUAREA_MAX; 333 vaddr_t uaddr; 334 335 if (uvm_nuarea <= leave) 336 return; 337 338 simple_lock(&uvm_uareas_slock); 339 while(uvm_nuarea > leave) { 340 uaddr = uvm_uareas; 341 uvm_uareas = UAREA_NEXTFREE(uaddr); 342 uvm_nuarea--; 343 simple_unlock(&uvm_uareas_slock); 344 uvm_km_free(kernel_map, uaddr, USPACE, UVM_KMF_PAGEABLE); 345 simple_lock(&uvm_uareas_slock); 346 } 347 simple_unlock(&uvm_uareas_slock); 348 } 349 350 /* 351 * uvm_exit: exit a virtual address space 352 * 353 * - the process passed to us is a dead (pre-zombie) process; we 354 * are running on a different context now (the reaper). 355 * - borrow proc0's address space because freeing the vmspace 356 * of the dead process may block. 357 */ 358 359 void 360 uvm_proc_exit(struct proc *p) 361 { 362 struct lwp *l = curlwp; /* XXX */ 363 struct vmspace *ovm; 364 365 KASSERT(p == l->l_proc); 366 ovm = p->p_vmspace; 367 368 /* 369 * borrow proc0's address space. 370 */ 371 pmap_deactivate(l); 372 p->p_vmspace = proc0.p_vmspace; 373 pmap_activate(l); 374 375 uvmspace_free(ovm); 376 } 377 378 void 379 uvm_lwp_exit(struct lwp *l) 380 { 381 vaddr_t va = USER_TO_UAREA(l->l_addr); 382 383 l->l_flag &= ~L_INMEM; 384 uvm_uarea_free(va); 385 l->l_addr = NULL; 386 } 387 388 /* 389 * uvm_init_limit: init per-process VM limits 390 * 391 * - called for process 0 and then inherited by all others. 392 */ 393 394 void 395 uvm_init_limits(struct proc *p) 396 { 397 398 /* 399 * Set up the initial limits on process VM. Set the maximum 400 * resident set size to be all of (reasonably) available memory. 401 * This causes any single, large process to start random page 402 * replacement once it fills memory. 403 */ 404 405 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 406 p->p_rlimit[RLIMIT_STACK].rlim_max = maxsmap; 407 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 408 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdmap; 409 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free); 410 } 411 412 #ifdef DEBUG 413 int enableswap = 1; 414 int swapdebug = 0; 415 #define SDB_FOLLOW 1 416 #define SDB_SWAPIN 2 417 #define SDB_SWAPOUT 4 418 #endif 419 420 /* 421 * uvm_swapin: swap in an lwp's u-area. 422 */ 423 424 void 425 uvm_swapin(struct lwp *l) 426 { 427 vaddr_t addr; 428 int s, error; 429 430 addr = USER_TO_UAREA(l->l_addr); 431 /* make L_INMEM true */ 432 error = uvm_fault_wire(kernel_map, addr, addr + USPACE, 433 VM_PROT_READ | VM_PROT_WRITE, 0); 434 if (error) { 435 panic("uvm_swapin: rewiring stack failed: %d", error); 436 } 437 438 /* 439 * Some architectures need to be notified when the user area has 440 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c). 441 */ 442 cpu_swapin(l); 443 SCHED_LOCK(s); 444 if (l->l_stat == LSRUN) 445 setrunqueue(l); 446 l->l_flag |= L_INMEM; 447 SCHED_UNLOCK(s); 448 l->l_swtime = 0; 449 ++uvmexp.swapins; 450 } 451 452 /* 453 * uvm_scheduler: process zero main loop 454 * 455 * - attempt to swapin every swaped-out, runnable process in order of 456 * priority. 457 * - if not enough memory, wake the pagedaemon and let it clear space. 458 */ 459 460 void 461 uvm_scheduler(void) 462 { 463 struct lwp *l, *ll; 464 int pri; 465 int ppri; 466 467 loop: 468 #ifdef DEBUG 469 while (!enableswap) 470 tsleep(&proc0, PVM, "noswap", 0); 471 #endif 472 ll = NULL; /* process to choose */ 473 ppri = INT_MIN; /* its priority */ 474 proclist_lock_read(); 475 476 LIST_FOREACH(l, &alllwp, l_list) { 477 /* is it a runnable swapped out process? */ 478 if (l->l_stat == LSRUN && (l->l_flag & L_INMEM) == 0) { 479 pri = l->l_swtime + l->l_slptime - 480 (l->l_proc->p_nice - NZERO) * 8; 481 if (pri > ppri) { /* higher priority? remember it. */ 482 ll = l; 483 ppri = pri; 484 } 485 } 486 } 487 /* 488 * XXXSMP: possible unlock/sleep race between here and the 489 * "scheduler" tsleep below.. 490 */ 491 proclist_unlock_read(); 492 493 #ifdef DEBUG 494 if (swapdebug & SDB_FOLLOW) 495 printf("scheduler: running, procp %p pri %d\n", ll, ppri); 496 #endif 497 /* 498 * Nothing to do, back to sleep 499 */ 500 if ((l = ll) == NULL) { 501 tsleep(&proc0, PVM, "scheduler", 0); 502 goto loop; 503 } 504 505 /* 506 * we have found swapped out process which we would like to bring 507 * back in. 508 * 509 * XXX: this part is really bogus cuz we could deadlock on memory 510 * despite our feeble check 511 */ 512 if (uvmexp.free > atop(USPACE)) { 513 #ifdef DEBUG 514 if (swapdebug & SDB_SWAPIN) 515 printf("swapin: pid %d(%s)@%p, pri %d free %d\n", 516 l->l_proc->p_pid, l->l_proc->p_comm, l->l_addr, ppri, uvmexp.free); 517 #endif 518 uvm_swapin(l); 519 goto loop; 520 } 521 /* 522 * not enough memory, jab the pageout daemon and wait til the coast 523 * is clear 524 */ 525 #ifdef DEBUG 526 if (swapdebug & SDB_FOLLOW) 527 printf("scheduler: no room for pid %d(%s), free %d\n", 528 l->l_proc->p_pid, l->l_proc->p_comm, uvmexp.free); 529 #endif 530 uvm_wait("schedpwait"); 531 #ifdef DEBUG 532 if (swapdebug & SDB_FOLLOW) 533 printf("scheduler: room again, free %d\n", uvmexp.free); 534 #endif 535 goto loop; 536 } 537 538 /* 539 * swappable: is LWP "l" swappable? 540 */ 541 542 #define swappable(l) \ 543 (((l)->l_flag & (L_INMEM)) && \ 544 ((((l)->l_proc->p_flag) & (P_SYSTEM | P_WEXIT)) == 0) && \ 545 (l)->l_holdcnt == 0) 546 547 /* 548 * swapout_threads: find threads that can be swapped and unwire their 549 * u-areas. 550 * 551 * - called by the pagedaemon 552 * - try and swap at least one processs 553 * - processes that are sleeping or stopped for maxslp or more seconds 554 * are swapped... otherwise the longest-sleeping or stopped process 555 * is swapped, otherwise the longest resident process... 556 */ 557 558 void 559 uvm_swapout_threads(void) 560 { 561 struct lwp *l; 562 struct lwp *outl, *outl2; 563 int outpri, outpri2; 564 int didswap = 0; 565 extern int maxslp; 566 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */ 567 568 #ifdef DEBUG 569 if (!enableswap) 570 return; 571 #endif 572 573 /* 574 * outl/outpri : stop/sleep thread with largest sleeptime < maxslp 575 * outl2/outpri2: the longest resident thread (its swap time) 576 */ 577 outl = outl2 = NULL; 578 outpri = outpri2 = 0; 579 proclist_lock_read(); 580 LIST_FOREACH(l, &alllwp, l_list) { 581 KASSERT(l->l_proc != NULL); 582 if (!swappable(l)) 583 continue; 584 switch (l->l_stat) { 585 case LSONPROC: 586 continue; 587 588 case LSRUN: 589 if (l->l_swtime > outpri2) { 590 outl2 = l; 591 outpri2 = l->l_swtime; 592 } 593 continue; 594 595 case LSSLEEP: 596 case LSSTOP: 597 if (l->l_slptime >= maxslp) { 598 uvm_swapout(l); 599 didswap++; 600 } else if (l->l_slptime > outpri) { 601 outl = l; 602 outpri = l->l_slptime; 603 } 604 continue; 605 } 606 } 607 proclist_unlock_read(); 608 609 /* 610 * If we didn't get rid of any real duds, toss out the next most 611 * likely sleeping/stopped or running candidate. We only do this 612 * if we are real low on memory since we don't gain much by doing 613 * it (USPACE bytes). 614 */ 615 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) { 616 if ((l = outl) == NULL) 617 l = outl2; 618 #ifdef DEBUG 619 if (swapdebug & SDB_SWAPOUT) 620 printf("swapout_threads: no duds, try procp %p\n", l); 621 #endif 622 if (l) 623 uvm_swapout(l); 624 } 625 } 626 627 /* 628 * uvm_swapout: swap out lwp "l" 629 * 630 * - currently "swapout" means "unwire U-area" and "pmap_collect()" 631 * the pmap. 632 * - XXXCDC: should deactivate all process' private anonymous memory 633 */ 634 635 static void 636 uvm_swapout(struct lwp *l) 637 { 638 vaddr_t addr; 639 int s; 640 struct proc *p = l->l_proc; 641 642 #ifdef DEBUG 643 if (swapdebug & SDB_SWAPOUT) 644 printf("swapout: lid %d.%d(%s)@%p, stat %x pri %d free %d\n", 645 p->p_pid, l->l_lid, p->p_comm, l->l_addr, l->l_stat, 646 l->l_slptime, uvmexp.free); 647 #endif 648 649 /* 650 * Mark it as (potentially) swapped out. 651 */ 652 SCHED_LOCK(s); 653 if (l->l_stat == LSONPROC) { 654 KDASSERT(l->l_cpu != curcpu()); 655 SCHED_UNLOCK(s); 656 return; 657 } 658 l->l_flag &= ~L_INMEM; 659 if (l->l_stat == LSRUN) 660 remrunqueue(l); 661 SCHED_UNLOCK(s); 662 l->l_swtime = 0; 663 p->p_stats->p_ru.ru_nswap++; 664 ++uvmexp.swapouts; 665 666 /* 667 * Do any machine-specific actions necessary before swapout. 668 * This can include saving floating point state, etc. 669 */ 670 cpu_swapout(l); 671 672 /* 673 * Unwire the to-be-swapped process's user struct and kernel stack. 674 */ 675 addr = USER_TO_UAREA(l->l_addr); 676 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !L_INMEM */ 677 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map)); 678 } 679 680 /* 681 * uvm_coredump_walkmap: walk a process's map for the purpose of dumping 682 * a core file. 683 */ 684 685 int 686 uvm_coredump_walkmap(struct proc *p, void *iocookie, 687 int (*func)(struct proc *, void *, struct uvm_coredump_state *), 688 void *cookie) 689 { 690 struct uvm_coredump_state state; 691 struct vmspace *vm = p->p_vmspace; 692 struct vm_map *map = &vm->vm_map; 693 struct vm_map_entry *entry; 694 int error; 695 696 entry = NULL; 697 vm_map_lock_read(map); 698 state.end = 0; 699 for (;;) { 700 if (entry == NULL) 701 entry = map->header.next; 702 else if (!uvm_map_lookup_entry(map, state.end, &entry)) 703 entry = entry->next; 704 if (entry == &map->header) 705 break; 706 707 state.cookie = cookie; 708 if (state.end > entry->start) { 709 state.start = state.end; 710 } else { 711 state.start = entry->start; 712 } 713 state.realend = entry->end; 714 state.end = entry->end; 715 state.prot = entry->protection; 716 state.flags = 0; 717 718 /* 719 * Dump the region unless one of the following is true: 720 * 721 * (1) the region has neither object nor amap behind it 722 * (ie. it has never been accessed). 723 * 724 * (2) the region has no amap and is read-only 725 * (eg. an executable text section). 726 * 727 * (3) the region's object is a device. 728 * 729 * (4) the region is unreadable by the process. 730 */ 731 732 KASSERT(!UVM_ET_ISSUBMAP(entry)); 733 KASSERT(state.start < VM_MAXUSER_ADDRESS); 734 KASSERT(state.end <= VM_MAXUSER_ADDRESS); 735 if (entry->object.uvm_obj == NULL && 736 entry->aref.ar_amap == NULL) { 737 state.realend = state.start; 738 } else if ((entry->protection & VM_PROT_WRITE) == 0 && 739 entry->aref.ar_amap == NULL) { 740 state.realend = state.start; 741 } else if (entry->object.uvm_obj != NULL && 742 UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) { 743 state.realend = state.start; 744 } else if ((entry->protection & VM_PROT_READ) == 0) { 745 state.realend = state.start; 746 } else { 747 if (state.start >= (vaddr_t)vm->vm_maxsaddr) 748 state.flags |= UVM_COREDUMP_STACK; 749 750 /* 751 * If this an anonymous entry, only dump instantiated 752 * pages. 753 */ 754 if (entry->object.uvm_obj == NULL) { 755 vaddr_t end; 756 757 amap_lock(entry->aref.ar_amap); 758 for (end = state.start; 759 end < state.end; end += PAGE_SIZE) { 760 struct vm_anon *anon; 761 anon = amap_lookup(&entry->aref, 762 end - entry->start); 763 /* 764 * If we have already encountered an 765 * uninstantiated page, stop at the 766 * first instantied page. 767 */ 768 if (anon != NULL && 769 state.realend != state.end) { 770 state.end = end; 771 break; 772 } 773 774 /* 775 * If this page is the first 776 * uninstantiated page, mark this as 777 * the real ending point. Continue to 778 * counting uninstantiated pages. 779 */ 780 if (anon == NULL && 781 state.realend == state.end) { 782 state.realend = end; 783 } 784 } 785 amap_unlock(entry->aref.ar_amap); 786 } 787 } 788 789 790 vm_map_unlock_read(map); 791 error = (*func)(p, iocookie, &state); 792 if (error) 793 return (error); 794 vm_map_lock_read(map); 795 } 796 vm_map_unlock_read(map); 797 798 return (0); 799 } 800