1 /*- 2 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki 3 * Copyright (c) 2006 Peter Wemm 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * From: FreeBSD: src/lib/libkvm/kvm_minidump_i386.c,v 1.2 2006/06/05 08:51:14 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * ARM machine dependent routines for kvm and minidumps. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/user.h> 38 #include <sys/proc.h> 39 #include <sys/stat.h> 40 #include <sys/mman.h> 41 #include <sys/fnv_hash.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <nlist.h> 46 #include <kvm.h> 47 48 #include <vm/vm.h> 49 #include <vm/vm_param.h> 50 51 #include <machine/elf.h> 52 #include <machine/cpufunc.h> 53 #include <machine/minidump.h> 54 55 #include <limits.h> 56 57 #include "kvm_private.h" 58 59 struct hpte { 60 struct hpte *next; 61 uint64_t pa; 62 int64_t off; 63 }; 64 65 #define HPT_SIZE 1024 66 67 /* minidump must be the first field */ 68 struct vmstate { 69 int minidump; /* 1 = minidump mode */ 70 struct minidumphdr hdr; 71 void *hpt_head[HPT_SIZE]; 72 uint32_t *bitmap; 73 void *ptemap; 74 }; 75 76 static void 77 hpt_insert(kvm_t *kd, uint64_t pa, int64_t off) 78 { 79 struct hpte *hpte; 80 uint32_t fnv = FNV1_32_INIT; 81 82 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 83 fnv &= (HPT_SIZE - 1); 84 hpte = malloc(sizeof(*hpte)); 85 hpte->pa = pa; 86 hpte->off = off; 87 hpte->next = kd->vmst->hpt_head[fnv]; 88 kd->vmst->hpt_head[fnv] = hpte; 89 } 90 91 static int64_t 92 hpt_find(kvm_t *kd, uint64_t pa) 93 { 94 struct hpte *hpte; 95 uint32_t fnv = FNV1_32_INIT; 96 97 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 98 fnv &= (HPT_SIZE - 1); 99 for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) 100 if (pa == hpte->pa) 101 return (hpte->off); 102 103 return (-1); 104 } 105 106 static int 107 inithash(kvm_t *kd, uint32_t *base, int len, off_t off) 108 { 109 uint64_t idx, pa; 110 uint32_t bit, bits; 111 112 for (idx = 0; idx < len / sizeof(*base); idx++) { 113 bits = base[idx]; 114 while (bits) { 115 bit = ffs(bits) - 1; 116 bits &= ~(1ul << bit); 117 pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE; 118 hpt_insert(kd, pa, off); 119 off += PAGE_SIZE; 120 } 121 } 122 return (off); 123 } 124 125 void 126 _kvm_minidump_freevtop(kvm_t *kd) 127 { 128 struct vmstate *vm = kd->vmst; 129 130 if (vm->bitmap) 131 free(vm->bitmap); 132 if (vm->ptemap) 133 free(vm->ptemap); 134 free(vm); 135 kd->vmst = NULL; 136 } 137 138 int 139 _kvm_minidump_initvtop(kvm_t *kd) 140 { 141 u_long pa; 142 struct vmstate *vmst; 143 off_t off; 144 145 vmst = _kvm_malloc(kd, sizeof(*vmst)); 146 if (vmst == 0) { 147 _kvm_err(kd, kd->program, "cannot allocate vm"); 148 return (-1); 149 } 150 151 kd->vmst = vmst; 152 vmst->minidump = 1; 153 154 if (pread(kd->pmfd, &vmst->hdr, 155 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) { 156 _kvm_err(kd, kd->program, "cannot read dump header"); 157 return (-1); 158 } 159 160 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, 161 sizeof(vmst->hdr.magic)) != 0) { 162 _kvm_err(kd, kd->program, "not a minidump for this platform"); 163 return (-1); 164 } 165 if (vmst->hdr.version != MINIDUMP_VERSION) { 166 _kvm_err(kd, kd->program, "wrong minidump version. " 167 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version); 168 return (-1); 169 } 170 171 /* Skip header and msgbuf */ 172 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize); 173 174 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); 175 if (vmst->bitmap == NULL) { 176 _kvm_err(kd, kd->program, "cannot allocate %d bytes for " 177 "bitmap", vmst->hdr.bitmapsize); 178 return (-1); 179 } 180 181 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) != 182 vmst->hdr.bitmapsize) { 183 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", 184 vmst->hdr.bitmapsize); 185 return (-1); 186 } 187 off += round_page(vmst->hdr.bitmapsize); 188 189 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize); 190 if (vmst->ptemap == NULL) { 191 _kvm_err(kd, kd->program, "cannot allocate %d bytes for " 192 "ptemap", vmst->hdr.ptesize); 193 return (-1); 194 } 195 196 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) != 197 vmst->hdr.ptesize) { 198 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", 199 vmst->hdr.ptesize); 200 return (-1); 201 } 202 203 off += vmst->hdr.ptesize; 204 205 /* Build physical address hash table for sparse pages */ 206 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off); 207 208 return (0); 209 } 210 211 int 212 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa) 213 { 214 struct vmstate *vm; 215 pt_entry_t pte; 216 u_long offset, pteindex, a; 217 off_t ofs; 218 uint32_t *ptemap; 219 int i; 220 221 if (ISALIVE(kd)) { 222 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); 223 return (0); 224 } 225 226 vm = kd->vmst; 227 ptemap = vm->ptemap; 228 229 if (va >= vm->hdr.kernbase) { 230 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT; 231 pte = ptemap[pteindex]; 232 if (!pte) { 233 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); 234 goto invalid; 235 } 236 if ((pte & L2_TYPE_MASK) == L2_TYPE_L) { 237 offset = va & L2_L_OFFSET; 238 a = pte & L2_L_FRAME; 239 } else if ((pte & L2_TYPE_MASK) == L2_TYPE_S) { 240 offset = va & L2_S_OFFSET; 241 a = pte & L2_S_FRAME; 242 } else 243 goto invalid; 244 245 ofs = hpt_find(kd, a); 246 if (ofs == -1) { 247 _kvm_err(kd, kd->program, "_kvm_vatop: physical " 248 "address 0x%lx not in minidump", a); 249 goto invalid; 250 } 251 252 *pa = ofs + offset; 253 return (PAGE_SIZE - offset); 254 255 } else 256 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx " 257 "not minidumped", va); 258 259 invalid: 260 _kvm_err(kd, 0, "invalid address (0x%lx)", va); 261 return (0); 262 } 263