uvm_glue.c revision 1.92.8.1 1 /* $NetBSD: uvm_glue.c,v 1.92.8.1 2006/04/01 12:07:58 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 (at) 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.92.8.1 2006/04/01 12:07:58 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 void *uvm_uareas;
99 int uvm_nuarea;
100 struct simplelock uvm_uareas_slock = SIMPLELOCK_INITIALIZER;
101
102 static void uvm_uarea_free(vaddr_t);
103
104 /*
105 * XXXCDC: do these really belong here?
106 */
107
108 /*
109 * uvm_kernacc: can the kernel access a region of memory
110 *
111 * - used only by /dev/kmem driver (mem.c)
112 */
113
114 boolean_t
115 uvm_kernacc(caddr_t addr, size_t len, int rw)
116 {
117 boolean_t rv;
118 vaddr_t saddr, eaddr;
119 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
120
121 saddr = trunc_page((vaddr_t)addr);
122 eaddr = round_page((vaddr_t)addr + len);
123 vm_map_lock_read(kernel_map);
124 rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
125 vm_map_unlock_read(kernel_map);
126
127 return(rv);
128 }
129
130 #ifdef KGDB
131 /*
132 * Change protections on kernel pages from addr to addr+len
133 * (presumably so debugger can plant a breakpoint).
134 *
135 * We force the protection change at the pmap level. If we were
136 * to use vm_map_protect a change to allow writing would be lazily-
137 * applied meaning we would still take a protection fault, something
138 * we really don't want to do. It would also fragment the kernel
139 * map unnecessarily. We cannot use pmap_protect since it also won't
140 * enforce a write-enable request. Using pmap_enter is the only way
141 * we can ensure the change takes place properly.
142 */
143 void
144 uvm_chgkprot(caddr_t addr, size_t len, int rw)
145 {
146 vm_prot_t prot;
147 paddr_t pa;
148 vaddr_t sva, eva;
149
150 prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
151 eva = round_page((vaddr_t)addr + len);
152 for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
153 /*
154 * Extract physical address for the page.
155 */
156 if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE)
157 panic("chgkprot: invalid page");
158 pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
159 }
160 pmap_update(pmap_kernel());
161 }
162 #endif
163
164 /*
165 * uvm_vslock: wire user memory for I/O
166 *
167 * - called from physio and sys___sysctl
168 * - XXXCDC: consider nuking this (or making it a macro?)
169 */
170
171 int
172 uvm_vslock(struct proc *p, caddr_t addr, size_t len, vm_prot_t access_type)
173 {
174 struct vm_map *map;
175 vaddr_t start, end;
176 int error;
177
178 map = &p->p_vmspace->vm_map;
179 start = trunc_page((vaddr_t)addr);
180 end = round_page((vaddr_t)addr + len);
181 error = uvm_fault_wire(map, start, end, access_type, 0);
182 return error;
183 }
184
185 /*
186 * uvm_vsunlock: unwire user memory wired by uvm_vslock()
187 *
188 * - called from physio and sys___sysctl
189 * - XXXCDC: consider nuking this (or making it a macro?)
190 */
191
192 void
193 uvm_vsunlock(struct proc *p, caddr_t addr, size_t len)
194 {
195 uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr),
196 round_page((vaddr_t)addr + len));
197 }
198
199 /*
200 * uvm_proc_fork: fork a virtual address space
201 *
202 * - the address space is copied as per parent map's inherit values
203 */
204 void
205 uvm_proc_fork(struct proc *p1, struct proc *p2, boolean_t shared)
206 {
207
208 if (shared == TRUE) {
209 p2->p_vmspace = NULL;
210 uvmspace_share(p1, p2);
211 } else {
212 p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
213 }
214
215 cpu_proc_fork(p1, p2);
216 }
217
218
219 /*
220 * uvm_lwp_fork: fork a thread
221 *
222 * - a new "user" structure is allocated for the child process
223 * [filled in by MD layer...]
224 * - if specified, the child gets a new user stack described by
225 * stack and stacksize
226 * - NOTE: the kernel stack may be at a different location in the child
227 * process, and thus addresses of automatic variables may be invalid
228 * after cpu_lwp_fork returns in the child process. We do nothing here
229 * after cpu_lwp_fork returns.
230 * - XXXCDC: we need a way for this to return a failure value rather
231 * than just hang
232 */
233 void
234 uvm_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
235 void (*func)(void *), void *arg)
236 {
237 struct user *up = l2->l_addr;
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 error = uvm_fault_wire(kernel_map, (vaddr_t)up,
252 (vaddr_t)up + USPACE, VM_PROT_READ | VM_PROT_WRITE, 0);
253 if (error)
254 panic("uvm_lwp_fork: uvm_fault_wire failed: %d", error);
255 #ifdef PMAP_UAREA
256 /* Tell the pmap this is a u-area mapping */
257 PMAP_UAREA((vaddr_t)up);
258 #endif
259 l2->l_flag |= L_INMEM;
260 }
261
262 #ifdef KSTACK_CHECK_MAGIC
263 /*
264 * fill stack with magic number
265 */
266 kstack_setup_magic(l2);
267 #endif
268
269 /*
270 * cpu_lwp_fork() copy and update the pcb, and make the child ready
271 * to run. If this is a normal user fork, the child will exit
272 * directly to user mode via child_return() on its first time
273 * slice and will not return here. If this is a kernel thread,
274 * the specified entry point will be executed.
275 */
276 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg);
277 }
278
279 /*
280 * uvm_uarea_alloc: allocate a u-area
281 */
282
283 boolean_t
284 uvm_uarea_alloc(vaddr_t *uaddrp)
285 {
286 vaddr_t uaddr;
287
288 #ifndef USPACE_ALIGN
289 #define USPACE_ALIGN 0
290 #endif
291
292 simple_lock(&uvm_uareas_slock);
293 if (uvm_nuarea > 0) {
294 uaddr = (vaddr_t)uvm_uareas;
295 uvm_uareas = *(void **)uvm_uareas;
296 uvm_nuarea--;
297 simple_unlock(&uvm_uareas_slock);
298 *uaddrp = uaddr;
299 return TRUE;
300 } else {
301 simple_unlock(&uvm_uareas_slock);
302 *uaddrp = uvm_km_alloc(kernel_map, USPACE, USPACE_ALIGN,
303 UVM_KMF_PAGEABLE);
304 return FALSE;
305 }
306 }
307
308 /*
309 * uvm_uarea_free: free a u-area; never blocks
310 */
311
312 static inline void
313 uvm_uarea_free(vaddr_t uaddr)
314 {
315 simple_lock(&uvm_uareas_slock);
316 *(void **)uaddr = uvm_uareas;
317 uvm_uareas = (void *)uaddr;
318 uvm_nuarea++;
319 simple_unlock(&uvm_uareas_slock);
320 }
321
322 /*
323 * uvm_uarea_drain: return memory of u-areas over limit
324 * back to system
325 */
326
327 void
328 uvm_uarea_drain(boolean_t empty)
329 {
330 int leave = empty ? 0 : UVM_NUAREA_MAX;
331 vaddr_t uaddr;
332
333 if (uvm_nuarea <= leave)
334 return;
335
336 simple_lock(&uvm_uareas_slock);
337 while(uvm_nuarea > leave) {
338 uaddr = (vaddr_t)uvm_uareas;
339 uvm_uareas = *(void **)uvm_uareas;
340 uvm_nuarea--;
341 simple_unlock(&uvm_uareas_slock);
342 uvm_km_free(kernel_map, uaddr, USPACE, UVM_KMF_PAGEABLE);
343 simple_lock(&uvm_uareas_slock);
344 }
345 simple_unlock(&uvm_uareas_slock);
346 }
347
348 /*
349 * uvm_exit: exit a virtual address space
350 *
351 * - the process passed to us is a dead (pre-zombie) process; we
352 * are running on a different context now (the reaper).
353 * - borrow proc0's address space because freeing the vmspace
354 * of the dead process may block.
355 */
356
357 void
358 uvm_proc_exit(struct proc *p)
359 {
360 struct lwp *l = curlwp; /* XXX */
361 struct vmspace *ovm;
362
363 KASSERT(p == l->l_proc);
364 ovm = p->p_vmspace;
365
366 /*
367 * borrow proc0's address space.
368 */
369 pmap_deactivate(l);
370 p->p_vmspace = proc0.p_vmspace;
371 pmap_activate(l);
372
373 uvmspace_free(ovm);
374 }
375
376 void
377 uvm_lwp_exit(struct lwp *l)
378 {
379 vaddr_t va = (vaddr_t)l->l_addr;
380
381 l->l_flag &= ~L_INMEM;
382 uvm_uarea_free(va);
383 l->l_addr = NULL;
384 }
385
386 /*
387 * uvm_init_limit: init per-process VM limits
388 *
389 * - called for process 0 and then inherited by all others.
390 */
391
392 void
393 uvm_init_limits(struct proc *p)
394 {
395
396 /*
397 * Set up the initial limits on process VM. Set the maximum
398 * resident set size to be all of (reasonably) available memory.
399 * This causes any single, large process to start random page
400 * replacement once it fills memory.
401 */
402
403 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
404 p->p_rlimit[RLIMIT_STACK].rlim_max = maxsmap;
405 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
406 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdmap;
407 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
408 }
409
410 #ifdef DEBUG
411 int enableswap = 1;
412 int swapdebug = 0;
413 #define SDB_FOLLOW 1
414 #define SDB_SWAPIN 2
415 #define SDB_SWAPOUT 4
416 #endif
417
418 /*
419 * uvm_swapin: swap in a process's u-area.
420 */
421
422 void
423 uvm_swapin(struct lwp *l)
424 {
425 vaddr_t addr;
426 int s, error;
427
428 addr = (vaddr_t)l->l_addr;
429 /* make L_INMEM true */
430 error = uvm_fault_wire(kernel_map, addr, addr + USPACE,
431 VM_PROT_READ | VM_PROT_WRITE, 0);
432 if (error) {
433 panic("uvm_swapin: rewiring stack failed: %d", error);
434 }
435
436 /*
437 * Some architectures need to be notified when the user area has
438 * moved to new physical page(s) (e.g. see mips/mips/vm_machdep.c).
439 */
440 cpu_swapin(l);
441 SCHED_LOCK(s);
442 if (l->l_stat == LSRUN)
443 setrunqueue(l);
444 l->l_flag |= L_INMEM;
445 SCHED_UNLOCK(s);
446 l->l_swtime = 0;
447 ++uvmexp.swapins;
448 }
449
450 /*
451 * uvm_scheduler: process zero main loop
452 *
453 * - attempt to swapin every swaped-out, runnable process in order of
454 * priority.
455 * - if not enough memory, wake the pagedaemon and let it clear space.
456 */
457
458 void
459 uvm_scheduler(void)
460 {
461 struct lwp *l, *ll;
462 int pri;
463 int ppri;
464
465 loop:
466 #ifdef DEBUG
467 while (!enableswap)
468 tsleep(&proc0, PVM, "noswap", 0);
469 #endif
470 ll = NULL; /* process to choose */
471 ppri = INT_MIN; /* its priority */
472 proclist_lock_read();
473
474 LIST_FOREACH(l, &alllwp, l_list) {
475 /* is it a runnable swapped out process? */
476 if (l->l_stat == LSRUN && (l->l_flag & L_INMEM) == 0) {
477 pri = l->l_swtime + l->l_slptime -
478 (l->l_proc->p_nice - NZERO) * 8;
479 if (pri > ppri) { /* higher priority? remember it. */
480 ll = l;
481 ppri = pri;
482 }
483 }
484 }
485 /*
486 * XXXSMP: possible unlock/sleep race between here and the
487 * "scheduler" tsleep below..
488 */
489 proclist_unlock_read();
490
491 #ifdef DEBUG
492 if (swapdebug & SDB_FOLLOW)
493 printf("scheduler: running, procp %p pri %d\n", ll, ppri);
494 #endif
495 /*
496 * Nothing to do, back to sleep
497 */
498 if ((l = ll) == NULL) {
499 tsleep(&proc0, PVM, "scheduler", 0);
500 goto loop;
501 }
502
503 /*
504 * we have found swapped out process which we would like to bring
505 * back in.
506 *
507 * XXX: this part is really bogus cuz we could deadlock on memory
508 * despite our feeble check
509 */
510 if (uvmexp.free > atop(USPACE)) {
511 #ifdef DEBUG
512 if (swapdebug & SDB_SWAPIN)
513 printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
514 l->l_proc->p_pid, l->l_proc->p_comm, l->l_addr, ppri, uvmexp.free);
515 #endif
516 uvm_swapin(l);
517 goto loop;
518 }
519 /*
520 * not enough memory, jab the pageout daemon and wait til the coast
521 * is clear
522 */
523 #ifdef DEBUG
524 if (swapdebug & SDB_FOLLOW)
525 printf("scheduler: no room for pid %d(%s), free %d\n",
526 l->l_proc->p_pid, l->l_proc->p_comm, uvmexp.free);
527 #endif
528 uvm_wait("schedpwait");
529 #ifdef DEBUG
530 if (swapdebug & SDB_FOLLOW)
531 printf("scheduler: room again, free %d\n", uvmexp.free);
532 #endif
533 goto loop;
534 }
535
536 /*
537 * swappable: is LWP "l" swappable?
538 */
539
540 #define swappable(l) \
541 (((l)->l_flag & (L_INMEM)) && \
542 ((((l)->l_proc->p_flag) & (P_SYSTEM | P_WEXIT)) == 0) && \
543 (l)->l_holdcnt == 0)
544
545 /*
546 * swapout_threads: find threads that can be swapped and unwire their
547 * u-areas.
548 *
549 * - called by the pagedaemon
550 * - try and swap at least one processs
551 * - processes that are sleeping or stopped for maxslp or more seconds
552 * are swapped... otherwise the longest-sleeping or stopped process
553 * is swapped, otherwise the longest resident process...
554 */
555
556 void
557 uvm_swapout_threads(void)
558 {
559 struct lwp *l;
560 struct lwp *outl, *outl2;
561 int outpri, outpri2;
562 int didswap = 0;
563 extern int maxslp;
564 /* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
565
566 #ifdef DEBUG
567 if (!enableswap)
568 return;
569 #endif
570
571 /*
572 * outl/outpri : stop/sleep thread with largest sleeptime < maxslp
573 * outl2/outpri2: the longest resident thread (its swap time)
574 */
575 outl = outl2 = NULL;
576 outpri = outpri2 = 0;
577 proclist_lock_read();
578 LIST_FOREACH(l, &alllwp, l_list) {
579 KASSERT(l->l_proc != NULL);
580 if (!swappable(l))
581 continue;
582 switch (l->l_stat) {
583 case LSONPROC:
584 continue;
585
586 case LSRUN:
587 if (l->l_swtime > outpri2) {
588 outl2 = l;
589 outpri2 = l->l_swtime;
590 }
591 continue;
592
593 case LSSLEEP:
594 case LSSTOP:
595 if (l->l_slptime >= maxslp) {
596 uvm_swapout(l);
597 didswap++;
598 } else if (l->l_slptime > outpri) {
599 outl = l;
600 outpri = l->l_slptime;
601 }
602 continue;
603 }
604 }
605 proclist_unlock_read();
606
607 /*
608 * If we didn't get rid of any real duds, toss out the next most
609 * likely sleeping/stopped or running candidate. We only do this
610 * if we are real low on memory since we don't gain much by doing
611 * it (USPACE bytes).
612 */
613 if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
614 if ((l = outl) == NULL)
615 l = outl2;
616 #ifdef DEBUG
617 if (swapdebug & SDB_SWAPOUT)
618 printf("swapout_threads: no duds, try procp %p\n", l);
619 #endif
620 if (l)
621 uvm_swapout(l);
622 }
623 }
624
625 /*
626 * uvm_swapout: swap out lwp "l"
627 *
628 * - currently "swapout" means "unwire U-area" and "pmap_collect()"
629 * the pmap.
630 * - XXXCDC: should deactivate all process' private anonymous memory
631 */
632
633 static void
634 uvm_swapout(struct lwp *l)
635 {
636 vaddr_t addr;
637 int s;
638 struct proc *p = l->l_proc;
639
640 #ifdef DEBUG
641 if (swapdebug & SDB_SWAPOUT)
642 printf("swapout: lid %d.%d(%s)@%p, stat %x pri %d free %d\n",
643 p->p_pid, l->l_lid, p->p_comm, l->l_addr, l->l_stat,
644 l->l_slptime, uvmexp.free);
645 #endif
646
647 /*
648 * Mark it as (potentially) swapped out.
649 */
650 SCHED_LOCK(s);
651 if (l->l_stat == LSONPROC) {
652 KDASSERT(l->l_cpu != curcpu());
653 SCHED_UNLOCK(s);
654 return;
655 }
656 l->l_flag &= ~L_INMEM;
657 if (l->l_stat == LSRUN)
658 remrunqueue(l);
659 SCHED_UNLOCK(s);
660 l->l_swtime = 0;
661 p->p_stats->p_ru.ru_nswap++;
662 ++uvmexp.swapouts;
663
664 /*
665 * Do any machine-specific actions necessary before swapout.
666 * This can include saving floating point state, etc.
667 */
668 cpu_swapout(l);
669
670 /*
671 * Unwire the to-be-swapped process's user struct and kernel stack.
672 */
673 addr = (vaddr_t)l->l_addr;
674 uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !L_INMEM */
675 pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
676 }
677
678 /*
679 * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
680 * a core file.
681 */
682
683 int
684 uvm_coredump_walkmap(struct proc *p, void *iocookie,
685 int (*func)(struct proc *, void *, struct uvm_coredump_state *),
686 void *cookie)
687 {
688 struct uvm_coredump_state state;
689 struct vmspace *vm = p->p_vmspace;
690 struct vm_map *map = &vm->vm_map;
691 struct vm_map_entry *entry;
692 int error;
693
694 entry = NULL;
695 vm_map_lock_read(map);
696 state.end = 0;
697 for (;;) {
698 if (entry == NULL)
699 entry = map->header.next;
700 else if (!uvm_map_lookup_entry(map, state.end, &entry))
701 entry = entry->next;
702 if (entry == &map->header)
703 break;
704
705 state.cookie = cookie;
706 if (state.end > entry->start) {
707 state.start = state.end;
708 } else {
709 state.start = entry->start;
710 }
711 state.realend = entry->end;
712 state.end = entry->end;
713 state.prot = entry->protection;
714 state.flags = 0;
715
716 /*
717 * Dump the region unless one of the following is true:
718 *
719 * (1) the region has neither object nor amap behind it
720 * (ie. it has never been accessed).
721 *
722 * (2) the region has no amap and is read-only
723 * (eg. an executable text section).
724 *
725 * (3) the region's object is a device.
726 *
727 * (4) the region is unreadable by the process.
728 */
729
730 KASSERT(!UVM_ET_ISSUBMAP(entry));
731 KASSERT(state.start < VM_MAXUSER_ADDRESS);
732 KASSERT(state.end <= VM_MAXUSER_ADDRESS);
733 if (entry->object.uvm_obj == NULL &&
734 entry->aref.ar_amap == NULL) {
735 state.realend = state.start;
736 } else if ((entry->protection & VM_PROT_WRITE) == 0 &&
737 entry->aref.ar_amap == NULL) {
738 state.realend = state.start;
739 } else if (entry->object.uvm_obj != NULL &&
740 UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
741 state.realend = state.start;
742 } else if ((entry->protection & VM_PROT_READ) == 0) {
743 state.realend = state.start;
744 } else {
745 if (state.start >= (vaddr_t)vm->vm_maxsaddr)
746 state.flags |= UVM_COREDUMP_STACK;
747
748 /*
749 * If this an anonymous entry, only dump instantiated
750 * pages.
751 */
752 if (entry->object.uvm_obj == NULL) {
753 vaddr_t end;
754
755 amap_lock(entry->aref.ar_amap);
756 for (end = state.start;
757 end < state.end; end += PAGE_SIZE) {
758 struct vm_anon *anon;
759 anon = amap_lookup(&entry->aref,
760 end - entry->start);
761 /*
762 * If we have already encountered an
763 * uninstantiated page, stop at the
764 * first instantied page.
765 */
766 if (anon != NULL &&
767 state.realend != state.end) {
768 state.end = end;
769 break;
770 }
771
772 /*
773 * If this page is the first
774 * uninstantiated page, mark this as
775 * the real ending point. Continue to
776 * counting uninstantiated pages.
777 */
778 if (anon == NULL &&
779 state.realend == state.end) {
780 state.realend = end;
781 }
782 }
783 amap_unlock(entry->aref.ar_amap);
784 }
785 }
786
787
788 vm_map_unlock_read(map);
789 error = (*func)(p, iocookie, &state);
790 if (error)
791 return (error);
792 vm_map_lock_read(map);
793 }
794 vm_map_unlock_read(map);
795
796 return (0);
797 }
798