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 <strings.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 #define PG_FRAME_PAE (~((uint64_t)PAGE_MASK)) 57 58 struct hpte { 59 struct hpte *next; 60 uint64_t pa; 61 int64_t off; 62 }; 63 64 #define HPT_SIZE 1024 65 66 /* minidump must be the first item! */ 67 struct vmstate { 68 int minidump; /* 1 = minidump mode */ 69 struct minidumphdr hdr; 70 void *hpt_head[HPT_SIZE]; 71 uint32_t *bitmap; 72 void *ptemap; 73 }; 74 75 static void 76 hpt_insert(kvm_t *kd, uint64_t pa, int64_t off) 77 { 78 struct hpte *hpte; 79 uint32_t fnv = FNV1_32_INIT; 80 81 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 82 fnv &= (HPT_SIZE - 1); 83 hpte = malloc(sizeof(*hpte)); 84 hpte->pa = pa; 85 hpte->off = off; 86 hpte->next = kd->vmst->hpt_head[fnv]; 87 kd->vmst->hpt_head[fnv] = hpte; 88 } 89 90 static int64_t 91 hpt_find(kvm_t *kd, uint64_t pa) 92 { 93 struct hpte *hpte; 94 uint32_t fnv = FNV1_32_INIT; 95 96 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 97 fnv &= (HPT_SIZE - 1); 98 for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) { 99 if (pa == hpte->pa) 100 return (hpte->off); 101 } 102 return (-1); 103 } 104 105 static int 106 inithash(kvm_t *kd, uint32_t *base, int len, off_t off) 107 { 108 uint64_t idx; 109 uint32_t bit, bits; 110 uint64_t pa; 111 112 for (idx = 0; idx < len / sizeof(*base); idx++) { 113 bits = base[idx]; 114 while (bits) { 115 bit = bsfl(bits); 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 kd->vmst = vmst; 151 bzero(vmst, sizeof(*vmst)); 152 vmst->minidump = 1; 153 if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != 154 sizeof(vmst->hdr)) { 155 _kvm_err(kd, kd->program, "cannot read dump header"); 156 return (-1); 157 } 158 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) { 159 _kvm_err(kd, kd->program, "not a minidump for this platform"); 160 return (-1); 161 } 162 if (vmst->hdr.version != MINIDUMP_VERSION) { 163 _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d", 164 MINIDUMP_VERSION, vmst->hdr.version); 165 return (-1); 166 } 167 168 /* Skip header and msgbuf */ 169 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize); 170 171 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); 172 if (vmst->bitmap == NULL) { 173 _kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize); 174 return (-1); 175 } 176 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) != 177 vmst->hdr.bitmapsize) { 178 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize); 179 return (-1); 180 } 181 off += round_page(vmst->hdr.bitmapsize); 182 183 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize); 184 if (vmst->ptemap == NULL) { 185 _kvm_err(kd, kd->program, "cannot allocate %d bytes for ptemap", vmst->hdr.ptesize); 186 return (-1); 187 } 188 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) != 189 vmst->hdr.ptesize) { 190 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", vmst->hdr.ptesize); 191 return (-1); 192 } 193 off += vmst->hdr.ptesize; 194 195 /* build physical address hash table for sparse pages */ 196 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off); 197 198 return (0); 199 } 200 201 static int 202 _kvm_minidump_vatop_pae(kvm_t *kd, u_long va, off_t *pa) 203 { 204 struct vmstate *vm; 205 uint64_t offset; 206 uint64_t pte; 207 u_long pteindex; 208 int i; 209 uint64_t a; 210 off_t ofs; 211 uint64_t *ptemap; 212 213 vm = kd->vmst; 214 ptemap = vm->ptemap; 215 offset = va & (PAGE_SIZE - 1); 216 217 if (va >= vm->hdr.kernbase) { 218 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT; 219 pte = ptemap[pteindex]; 220 if ((pte & PG_V) == 0) { 221 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); 222 goto invalid; 223 } 224 a = pte & PG_FRAME_PAE; 225 ofs = hpt_find(kd, a); 226 if (ofs == -1) { 227 _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%llx not in minidump", a); 228 goto invalid; 229 } 230 *pa = ofs + offset; 231 return (PAGE_SIZE - offset); 232 } else { 233 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va); 234 goto invalid; 235 } 236 237 invalid: 238 _kvm_err(kd, 0, "invalid address (0x%lx)", va); 239 return (0); 240 } 241 242 static int 243 _kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa) 244 { 245 struct vmstate *vm; 246 u_long offset; 247 pt_entry_t pte; 248 u_long pteindex; 249 int i; 250 u_long a; 251 off_t ofs; 252 uint32_t *ptemap; 253 254 vm = kd->vmst; 255 ptemap = vm->ptemap; 256 offset = va & (PAGE_SIZE - 1); 257 258 if (va >= vm->hdr.kernbase) { 259 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT; 260 pte = ptemap[pteindex]; 261 if ((pte & PG_V) == 0) { 262 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); 263 goto invalid; 264 } 265 a = pte & PG_FRAME; 266 ofs = hpt_find(kd, a); 267 if (ofs == -1) { 268 _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a); 269 goto invalid; 270 } 271 *pa = ofs + offset; 272 return (PAGE_SIZE - offset); 273 } else { 274 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va); 275 goto invalid; 276 } 277 278 invalid: 279 _kvm_err(kd, 0, "invalid address (0x%lx)", va); 280 return (0); 281 } 282 283 int 284 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa) 285 { 286 287 if (ISALIVE(kd)) { 288 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); 289 return (0); 290 } 291 if (kd->vmst->hdr.paemode) 292 return (_kvm_minidump_vatop_pae(kd, va, pa)); 293 else 294 return (_kvm_minidump_vatop(kd, va, pa)); 295 } 296