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