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