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