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