uvm_coredump.c revision 1.1.12.2 1 1.1.12.2 yamt /* $NetBSD: uvm_coredump.c,v 1.1.12.2 2009/05/04 08:14:39 yamt Exp $ */
2 1.1.12.2 yamt
3 1.1.12.2 yamt /*
4 1.1.12.2 yamt * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.1.12.2 yamt * Copyright (c) 1991, 1993, The Regents of the University of California.
6 1.1.12.2 yamt *
7 1.1.12.2 yamt * All rights reserved.
8 1.1.12.2 yamt *
9 1.1.12.2 yamt * This code is derived from software contributed to Berkeley by
10 1.1.12.2 yamt * The Mach Operating System project at Carnegie-Mellon University.
11 1.1.12.2 yamt *
12 1.1.12.2 yamt * Redistribution and use in source and binary forms, with or without
13 1.1.12.2 yamt * modification, are permitted provided that the following conditions
14 1.1.12.2 yamt * are met:
15 1.1.12.2 yamt * 1. Redistributions of source code must retain the above copyright
16 1.1.12.2 yamt * notice, this list of conditions and the following disclaimer.
17 1.1.12.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
18 1.1.12.2 yamt * notice, this list of conditions and the following disclaimer in the
19 1.1.12.2 yamt * documentation and/or other materials provided with the distribution.
20 1.1.12.2 yamt * 3. All advertising materials mentioning features or use of this software
21 1.1.12.2 yamt * must display the following acknowledgement:
22 1.1.12.2 yamt * This product includes software developed by Charles D. Cranor,
23 1.1.12.2 yamt * Washington University, the University of California, Berkeley and
24 1.1.12.2 yamt * its contributors.
25 1.1.12.2 yamt * 4. Neither the name of the University nor the names of its contributors
26 1.1.12.2 yamt * may be used to endorse or promote products derived from this software
27 1.1.12.2 yamt * without specific prior written permission.
28 1.1.12.2 yamt *
29 1.1.12.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.1.12.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1.12.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1.12.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.1.12.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.1.12.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.1.12.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.1.12.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.1.12.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.1.12.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.1.12.2 yamt * SUCH DAMAGE.
40 1.1.12.2 yamt *
41 1.1.12.2 yamt * @(#)vm_glue.c 8.6 (Berkeley) 1/5/94
42 1.1.12.2 yamt * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
43 1.1.12.2 yamt *
44 1.1.12.2 yamt *
45 1.1.12.2 yamt * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 1.1.12.2 yamt * All rights reserved.
47 1.1.12.2 yamt *
48 1.1.12.2 yamt * Permission to use, copy, modify and distribute this software and
49 1.1.12.2 yamt * its documentation is hereby granted, provided that both the copyright
50 1.1.12.2 yamt * notice and this permission notice appear in all copies of the
51 1.1.12.2 yamt * software, derivative works or modified versions, and any portions
52 1.1.12.2 yamt * thereof, and that both notices appear in supporting documentation.
53 1.1.12.2 yamt *
54 1.1.12.2 yamt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 1.1.12.2 yamt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 1.1.12.2 yamt * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 1.1.12.2 yamt *
58 1.1.12.2 yamt * Carnegie Mellon requests users of this software to return to
59 1.1.12.2 yamt *
60 1.1.12.2 yamt * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 1.1.12.2 yamt * School of Computer Science
62 1.1.12.2 yamt * Carnegie Mellon University
63 1.1.12.2 yamt * Pittsburgh PA 15213-3890
64 1.1.12.2 yamt *
65 1.1.12.2 yamt * any improvements or extensions that they make and grant Carnegie the
66 1.1.12.2 yamt * rights to redistribute these changes.
67 1.1.12.2 yamt */
68 1.1.12.2 yamt
69 1.1.12.2 yamt #include <sys/cdefs.h>
70 1.1.12.2 yamt __KERNEL_RCSID(0, "$NetBSD: uvm_coredump.c,v 1.1.12.2 2009/05/04 08:14:39 yamt Exp $");
71 1.1.12.2 yamt
72 1.1.12.2 yamt /*
73 1.1.12.2 yamt * uvm_coredump.c: glue functions for coredump
74 1.1.12.2 yamt */
75 1.1.12.2 yamt
76 1.1.12.2 yamt #include <sys/param.h>
77 1.1.12.2 yamt #include <sys/systm.h>
78 1.1.12.2 yamt #include <sys/proc.h>
79 1.1.12.2 yamt
80 1.1.12.2 yamt #include <uvm/uvm.h>
81 1.1.12.2 yamt
82 1.1.12.2 yamt /*
83 1.1.12.2 yamt * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
84 1.1.12.2 yamt * a core file.
85 1.1.12.2 yamt */
86 1.1.12.2 yamt
87 1.1.12.2 yamt int
88 1.1.12.2 yamt uvm_coredump_walkmap(struct proc *p, void *iocookie,
89 1.1.12.2 yamt int (*func)(struct proc *, void *, struct uvm_coredump_state *),
90 1.1.12.2 yamt void *cookie)
91 1.1.12.2 yamt {
92 1.1.12.2 yamt struct uvm_coredump_state state;
93 1.1.12.2 yamt struct vmspace *vm = p->p_vmspace;
94 1.1.12.2 yamt struct vm_map *map = &vm->vm_map;
95 1.1.12.2 yamt struct vm_map_entry *entry;
96 1.1.12.2 yamt int error;
97 1.1.12.2 yamt
98 1.1.12.2 yamt entry = NULL;
99 1.1.12.2 yamt vm_map_lock_read(map);
100 1.1.12.2 yamt state.end = 0;
101 1.1.12.2 yamt for (;;) {
102 1.1.12.2 yamt if (entry == NULL)
103 1.1.12.2 yamt entry = map->header.next;
104 1.1.12.2 yamt else if (!uvm_map_lookup_entry(map, state.end, &entry))
105 1.1.12.2 yamt entry = entry->next;
106 1.1.12.2 yamt if (entry == &map->header)
107 1.1.12.2 yamt break;
108 1.1.12.2 yamt
109 1.1.12.2 yamt state.cookie = cookie;
110 1.1.12.2 yamt if (state.end > entry->start) {
111 1.1.12.2 yamt state.start = state.end;
112 1.1.12.2 yamt } else {
113 1.1.12.2 yamt state.start = entry->start;
114 1.1.12.2 yamt }
115 1.1.12.2 yamt state.realend = entry->end;
116 1.1.12.2 yamt state.end = entry->end;
117 1.1.12.2 yamt state.prot = entry->protection;
118 1.1.12.2 yamt state.flags = 0;
119 1.1.12.2 yamt
120 1.1.12.2 yamt /*
121 1.1.12.2 yamt * Dump the region unless one of the following is true:
122 1.1.12.2 yamt *
123 1.1.12.2 yamt * (1) the region has neither object nor amap behind it
124 1.1.12.2 yamt * (ie. it has never been accessed).
125 1.1.12.2 yamt *
126 1.1.12.2 yamt * (2) the region has no amap and is read-only
127 1.1.12.2 yamt * (eg. an executable text section).
128 1.1.12.2 yamt *
129 1.1.12.2 yamt * (3) the region's object is a device.
130 1.1.12.2 yamt *
131 1.1.12.2 yamt * (4) the region is unreadable by the process.
132 1.1.12.2 yamt */
133 1.1.12.2 yamt
134 1.1.12.2 yamt KASSERT(!UVM_ET_ISSUBMAP(entry));
135 1.1.12.2 yamt KASSERT(state.start < VM_MAXUSER_ADDRESS);
136 1.1.12.2 yamt KASSERT(state.end <= VM_MAXUSER_ADDRESS);
137 1.1.12.2 yamt if (entry->object.uvm_obj == NULL &&
138 1.1.12.2 yamt entry->aref.ar_amap == NULL) {
139 1.1.12.2 yamt state.realend = state.start;
140 1.1.12.2 yamt } else if ((entry->protection & VM_PROT_WRITE) == 0 &&
141 1.1.12.2 yamt entry->aref.ar_amap == NULL) {
142 1.1.12.2 yamt state.realend = state.start;
143 1.1.12.2 yamt } else if (entry->object.uvm_obj != NULL &&
144 1.1.12.2 yamt UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
145 1.1.12.2 yamt state.realend = state.start;
146 1.1.12.2 yamt } else if ((entry->protection & VM_PROT_READ) == 0) {
147 1.1.12.2 yamt state.realend = state.start;
148 1.1.12.2 yamt } else {
149 1.1.12.2 yamt if (state.start >= (vaddr_t)vm->vm_maxsaddr)
150 1.1.12.2 yamt state.flags |= UVM_COREDUMP_STACK;
151 1.1.12.2 yamt
152 1.1.12.2 yamt /*
153 1.1.12.2 yamt * If this an anonymous entry, only dump instantiated
154 1.1.12.2 yamt * pages.
155 1.1.12.2 yamt */
156 1.1.12.2 yamt if (entry->object.uvm_obj == NULL) {
157 1.1.12.2 yamt vaddr_t end;
158 1.1.12.2 yamt
159 1.1.12.2 yamt amap_lock(entry->aref.ar_amap);
160 1.1.12.2 yamt for (end = state.start;
161 1.1.12.2 yamt end < state.end; end += PAGE_SIZE) {
162 1.1.12.2 yamt struct vm_anon *anon;
163 1.1.12.2 yamt anon = amap_lookup(&entry->aref,
164 1.1.12.2 yamt end - entry->start);
165 1.1.12.2 yamt /*
166 1.1.12.2 yamt * If we have already encountered an
167 1.1.12.2 yamt * uninstantiated page, stop at the
168 1.1.12.2 yamt * first instantied page.
169 1.1.12.2 yamt */
170 1.1.12.2 yamt if (anon != NULL &&
171 1.1.12.2 yamt state.realend != state.end) {
172 1.1.12.2 yamt state.end = end;
173 1.1.12.2 yamt break;
174 1.1.12.2 yamt }
175 1.1.12.2 yamt
176 1.1.12.2 yamt /*
177 1.1.12.2 yamt * If this page is the first
178 1.1.12.2 yamt * uninstantiated page, mark this as
179 1.1.12.2 yamt * the real ending point. Continue to
180 1.1.12.2 yamt * counting uninstantiated pages.
181 1.1.12.2 yamt */
182 1.1.12.2 yamt if (anon == NULL &&
183 1.1.12.2 yamt state.realend == state.end) {
184 1.1.12.2 yamt state.realend = end;
185 1.1.12.2 yamt }
186 1.1.12.2 yamt }
187 1.1.12.2 yamt amap_unlock(entry->aref.ar_amap);
188 1.1.12.2 yamt }
189 1.1.12.2 yamt }
190 1.1.12.2 yamt
191 1.1.12.2 yamt
192 1.1.12.2 yamt vm_map_unlock_read(map);
193 1.1.12.2 yamt error = (*func)(p, iocookie, &state);
194 1.1.12.2 yamt if (error)
195 1.1.12.2 yamt return (error);
196 1.1.12.2 yamt vm_map_lock_read(map);
197 1.1.12.2 yamt }
198 1.1.12.2 yamt vm_map_unlock_read(map);
199 1.1.12.2 yamt
200 1.1.12.2 yamt return (0);
201 1.1.12.2 yamt }
202