1 /* $NetBSD: uvm_coredump.c,v 1.1 2008/11/19 18:36:10 ad 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@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_coredump.c,v 1.1 2008/11/19 18:36:10 ad Exp $"); 71 72 /* 73 * uvm_coredump.c: glue functions for coredump 74 */ 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/proc.h> 79 80 #include <uvm/uvm.h> 81 82 /* 83 * uvm_coredump_walkmap: walk a process's map for the purpose of dumping 84 * a core file. 85 */ 86 87 int 88 uvm_coredump_walkmap(struct proc *p, void *iocookie, 89 int (*func)(struct proc *, void *, struct uvm_coredump_state *), 90 void *cookie) 91 { 92 struct uvm_coredump_state state; 93 struct vmspace *vm = p->p_vmspace; 94 struct vm_map *map = &vm->vm_map; 95 struct vm_map_entry *entry; 96 int error; 97 98 entry = NULL; 99 vm_map_lock_read(map); 100 state.end = 0; 101 for (;;) { 102 if (entry == NULL) 103 entry = map->header.next; 104 else if (!uvm_map_lookup_entry(map, state.end, &entry)) 105 entry = entry->next; 106 if (entry == &map->header) 107 break; 108 109 state.cookie = cookie; 110 if (state.end > entry->start) { 111 state.start = state.end; 112 } else { 113 state.start = entry->start; 114 } 115 state.realend = entry->end; 116 state.end = entry->end; 117 state.prot = entry->protection; 118 state.flags = 0; 119 120 /* 121 * Dump the region unless one of the following is true: 122 * 123 * (1) the region has neither object nor amap behind it 124 * (ie. it has never been accessed). 125 * 126 * (2) the region has no amap and is read-only 127 * (eg. an executable text section). 128 * 129 * (3) the region's object is a device. 130 * 131 * (4) the region is unreadable by the process. 132 */ 133 134 KASSERT(!UVM_ET_ISSUBMAP(entry)); 135 KASSERT(state.start < VM_MAXUSER_ADDRESS); 136 KASSERT(state.end <= VM_MAXUSER_ADDRESS); 137 if (entry->object.uvm_obj == NULL && 138 entry->aref.ar_amap == NULL) { 139 state.realend = state.start; 140 } else if ((entry->protection & VM_PROT_WRITE) == 0 && 141 entry->aref.ar_amap == NULL) { 142 state.realend = state.start; 143 } else if (entry->object.uvm_obj != NULL && 144 UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) { 145 state.realend = state.start; 146 } else if ((entry->protection & VM_PROT_READ) == 0) { 147 state.realend = state.start; 148 } else { 149 if (state.start >= (vaddr_t)vm->vm_maxsaddr) 150 state.flags |= UVM_COREDUMP_STACK; 151 152 /* 153 * If this an anonymous entry, only dump instantiated 154 * pages. 155 */ 156 if (entry->object.uvm_obj == NULL) { 157 vaddr_t end; 158 159 amap_lock(entry->aref.ar_amap); 160 for (end = state.start; 161 end < state.end; end += PAGE_SIZE) { 162 struct vm_anon *anon; 163 anon = amap_lookup(&entry->aref, 164 end - entry->start); 165 /* 166 * If we have already encountered an 167 * uninstantiated page, stop at the 168 * first instantied page. 169 */ 170 if (anon != NULL && 171 state.realend != state.end) { 172 state.end = end; 173 break; 174 } 175 176 /* 177 * If this page is the first 178 * uninstantiated page, mark this as 179 * the real ending point. Continue to 180 * counting uninstantiated pages. 181 */ 182 if (anon == NULL && 183 state.realend == state.end) { 184 state.realend = end; 185 } 186 } 187 amap_unlock(entry->aref.ar_amap); 188 } 189 } 190 191 192 vm_map_unlock_read(map); 193 error = (*func)(p, iocookie, &state); 194 if (error) 195 return (error); 196 vm_map_lock_read(map); 197 } 198 vm_map_unlock_read(map); 199 200 return (0); 201 } 202