1 /*- 2 * Copyright (c) 2006 Peter Wemm 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 /* 30 * AMD64 machine dependent routines for kvm and minidumps. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/user.h> 35 #include <sys/proc.h> 36 #include <sys/stat.h> 37 #include <sys/mman.h> 38 #include <sys/fnv_hash.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <nlist.h> 43 #include <kvm.h> 44 45 #include <vm/vm.h> 46 #include <vm/vm_param.h> 47 48 #include <machine/elf.h> 49 #include <machine/cpufunc.h> 50 #include <machine/minidump.h> 51 52 #include <limits.h> 53 54 #include "kvm_private.h" 55 56 struct hpte { 57 struct hpte *next; 58 vm_paddr_t pa; 59 int64_t off; 60 }; 61 62 #define HPT_SIZE 1024 63 64 /* minidump must be the first item! */ 65 struct vmstate { 66 int minidump; /* 1 = minidump mode */ 67 struct minidumphdr hdr; 68 void *hpt_head[HPT_SIZE]; 69 uint64_t *bitmap; 70 uint64_t *page_map; 71 }; 72 73 static void 74 hpt_insert(kvm_t *kd, vm_paddr_t pa, int64_t off) 75 { 76 struct hpte *hpte; 77 uint32_t fnv = FNV1_32_INIT; 78 79 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 80 fnv &= (HPT_SIZE - 1); 81 hpte = malloc(sizeof(*hpte)); 82 hpte->pa = pa; 83 hpte->off = off; 84 hpte->next = kd->vmst->hpt_head[fnv]; 85 kd->vmst->hpt_head[fnv] = hpte; 86 } 87 88 static int64_t 89 hpt_find(kvm_t *kd, vm_paddr_t pa) 90 { 91 struct hpte *hpte; 92 uint32_t fnv = FNV1_32_INIT; 93 94 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 95 fnv &= (HPT_SIZE - 1); 96 for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) { 97 if (pa == hpte->pa) 98 return (hpte->off); 99 } 100 return (-1); 101 } 102 103 static int 104 inithash(kvm_t *kd, uint64_t *base, int len, off_t off) 105 { 106 uint64_t idx; 107 uint64_t bit, bits; 108 vm_paddr_t pa; 109 110 for (idx = 0; idx < len / sizeof(*base); idx++) { 111 bits = base[idx]; 112 while (bits) { 113 bit = bsfq(bits); 114 bits &= ~(1ul << bit); 115 pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE; 116 hpt_insert(kd, pa, off); 117 off += PAGE_SIZE; 118 } 119 } 120 return (off); 121 } 122 123 void 124 _kvm_minidump_freevtop(kvm_t *kd) 125 { 126 struct vmstate *vm = kd->vmst; 127 128 if (vm->bitmap) 129 free(vm->bitmap); 130 if (vm->page_map) 131 free(vm->page_map); 132 free(vm); 133 kd->vmst = NULL; 134 } 135 136 int 137 _kvm_minidump_initvtop(kvm_t *kd) 138 { 139 u_long pa; 140 struct vmstate *vmst; 141 off_t off; 142 143 vmst = _kvm_malloc(kd, sizeof(*vmst)); 144 if (vmst == 0) { 145 _kvm_err(kd, kd->program, "cannot allocate vm"); 146 return (-1); 147 } 148 kd->vmst = vmst; 149 vmst->minidump = 1; 150 if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != 151 sizeof(vmst->hdr)) { 152 _kvm_err(kd, kd->program, "cannot read dump header"); 153 return (-1); 154 } 155 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) { 156 _kvm_err(kd, kd->program, "not a minidump for this platform"); 157 return (-1); 158 } 159 160 /* 161 * NB: amd64 minidump header is binary compatible between version 1 162 * and version 2; this may not be the case for the future versions. 163 */ 164 if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) { 165 _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d", 166 MINIDUMP_VERSION, vmst->hdr.version); 167 return (-1); 168 } 169 170 /* Skip header and msgbuf */ 171 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize); 172 173 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); 174 if (vmst->bitmap == NULL) { 175 _kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize); 176 return (-1); 177 } 178 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) != 179 vmst->hdr.bitmapsize) { 180 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize); 181 return (-1); 182 } 183 off += round_page(vmst->hdr.bitmapsize); 184 185 vmst->page_map = _kvm_malloc(kd, vmst->hdr.pmapsize); 186 if (vmst->page_map == NULL) { 187 _kvm_err(kd, kd->program, "cannot allocate %d bytes for page_map", vmst->hdr.pmapsize); 188 return (-1); 189 } 190 if (pread(kd->pmfd, vmst->page_map, vmst->hdr.pmapsize, off) != 191 vmst->hdr.pmapsize) { 192 _kvm_err(kd, kd->program, "cannot read %d bytes for page_map", vmst->hdr.pmapsize); 193 return (-1); 194 } 195 off += vmst->hdr.pmapsize; 196 197 /* build physical address hash table for sparse pages */ 198 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off); 199 200 return (0); 201 } 202 203 static int 204 _kvm_minidump_vatop_v1(kvm_t *kd, u_long va, off_t *pa) 205 { 206 struct vmstate *vm; 207 u_long offset; 208 pt_entry_t pte; 209 u_long pteindex; 210 int i; 211 u_long a; 212 off_t ofs; 213 214 vm = kd->vmst; 215 offset = va & (PAGE_SIZE - 1); 216 217 if (va >= vm->hdr.kernbase) { 218 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT; 219 pte = vm->page_map[pteindex]; 220 if (((u_long)pte & PG_V) == 0) { 221 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); 222 goto invalid; 223 } 224 a = pte & PG_FRAME; 225 ofs = hpt_find(kd, a); 226 if (ofs == -1) { 227 _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a); 228 goto invalid; 229 } 230 *pa = ofs + offset; 231 return (PAGE_SIZE - offset); 232 } else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) { 233 a = (va - vm->hdr.dmapbase) & ~PAGE_MASK; 234 ofs = hpt_find(kd, a); 235 if (ofs == -1) { 236 _kvm_err(kd, kd->program, "_kvm_vatop: direct map address 0x%lx not in minidump", va); 237 goto invalid; 238 } 239 *pa = ofs + offset; 240 return (PAGE_SIZE - offset); 241 } else { 242 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va); 243 goto invalid; 244 } 245 246 invalid: 247 _kvm_err(kd, 0, "invalid address (0x%lx)", va); 248 return (0); 249 } 250 251 static int 252 _kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa) 253 { 254 pt_entry_t pt[NPTEPG]; 255 struct vmstate *vm; 256 u_long offset; 257 pd_entry_t pde; 258 pd_entry_t pte; 259 u_long pteindex; 260 u_long pdeindex; 261 int i; 262 u_long a; 263 off_t ofs; 264 265 vm = kd->vmst; 266 offset = va & PAGE_MASK; 267 268 if (va >= vm->hdr.kernbase) { 269 pdeindex = (va - vm->hdr.kernbase) >> PDRSHIFT; 270 pde = vm->page_map[pdeindex]; 271 if (((u_long)pde & PG_V) == 0) { 272 _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid"); 273 goto invalid; 274 } 275 if ((pde & PG_PS) == 0) { 276 a = pde & PG_FRAME; 277 ofs = hpt_find(kd, a); 278 if (ofs == -1) { 279 _kvm_err(kd, kd->program, "_kvm_vatop: pt physical address 0x%lx not in minidump", a); 280 goto invalid; 281 } 282 if (pread(kd->pmfd, &pt, PAGE_SIZE, ofs) != PAGE_SIZE) { 283 _kvm_err(kd, kd->program, "cannot read %d bytes for pt", PAGE_SIZE); 284 return (-1); 285 } 286 pteindex = (va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1); 287 pte = pt[pteindex]; 288 if (((u_long)pte & PG_V) == 0) { 289 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); 290 goto invalid; 291 } 292 a = pte & PG_FRAME; 293 } else { 294 a = pde & PG_PS_FRAME; 295 a += (va & PDRMASK) ^ offset; 296 } 297 ofs = hpt_find(kd, a); 298 if (ofs == -1) { 299 _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a); 300 goto invalid; 301 } 302 *pa = ofs + offset; 303 return (PAGE_SIZE - offset); 304 } else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) { 305 a = (va - vm->hdr.dmapbase) & ~PAGE_MASK; 306 ofs = hpt_find(kd, a); 307 if (ofs == -1) { 308 _kvm_err(kd, kd->program, "_kvm_vatop: direct map address 0x%lx not in minidump", va); 309 goto invalid; 310 } 311 *pa = ofs + offset; 312 return (PAGE_SIZE - offset); 313 } else { 314 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va); 315 goto invalid; 316 } 317 318 invalid: 319 _kvm_err(kd, 0, "invalid address (0x%lx)", va); 320 return (0); 321 } 322 323 int 324 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa) 325 { 326 327 if (ISALIVE(kd)) { 328 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); 329 return (0); 330 } 331 if (((struct vmstate *)kd->vmst)->hdr.version == 1) 332 return (_kvm_minidump_vatop_v1(kd, va, pa)); 333 else 334 return (_kvm_minidump_vatop(kd, va, pa)); 335 } 336