1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2006 Peter Wemm 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * i386 machine dependent routines for kvm and minidumps. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/endian.h> 37 #include <stdint.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <vm/vm.h> 42 #include <kvm.h> 43 44 #include "../../sys/i386/include/minidump.h" 45 46 #include <limits.h> 47 48 #include "kvm_private.h" 49 #include "kvm_i386.h" 50 51 #define i386_round_page(x) roundup2((kvaddr_t)(x), I386_PAGE_SIZE) 52 53 struct vmstate { 54 struct minidumphdr hdr; 55 }; 56 57 static i386_pte_pae_t 58 _i386_pte_pae_get(kvm_t *kd, u_long pteindex) 59 { 60 i386_pte_pae_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte)); 61 62 return le64toh(*pte); 63 } 64 65 static i386_pte_t 66 _i386_pte_get(kvm_t *kd, u_long pteindex) 67 { 68 i386_pte_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte)); 69 70 return le32toh(*pte); 71 } 72 73 static int 74 _i386_minidump_probe(kvm_t *kd) 75 { 76 77 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_386) && 78 _kvm_is_minidump(kd)); 79 } 80 81 static void 82 _i386_minidump_freevtop(kvm_t *kd) 83 { 84 struct vmstate *vm = kd->vmst; 85 86 free(vm); 87 kd->vmst = NULL; 88 } 89 90 static int 91 _i386_minidump_initvtop(kvm_t *kd) 92 { 93 struct vmstate *vmst; 94 off_t off, sparse_off; 95 96 vmst = _kvm_malloc(kd, sizeof(*vmst)); 97 if (vmst == NULL) { 98 _kvm_err(kd, kd->program, "cannot allocate vm"); 99 return (-1); 100 } 101 kd->vmst = vmst; 102 if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != 103 sizeof(vmst->hdr)) { 104 _kvm_err(kd, kd->program, "cannot read dump header"); 105 return (-1); 106 } 107 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) { 108 _kvm_err(kd, kd->program, "not a minidump for this platform"); 109 return (-1); 110 } 111 vmst->hdr.version = le32toh(vmst->hdr.version); 112 if (vmst->hdr.version != MINIDUMP_VERSION) { 113 _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d", 114 MINIDUMP_VERSION, vmst->hdr.version); 115 return (-1); 116 } 117 vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize); 118 vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize); 119 vmst->hdr.ptesize = le32toh(vmst->hdr.ptesize); 120 vmst->hdr.kernbase = le32toh(vmst->hdr.kernbase); 121 vmst->hdr.paemode = le32toh(vmst->hdr.paemode); 122 123 /* Skip header and msgbuf */ 124 off = I386_PAGE_SIZE + i386_round_page(vmst->hdr.msgbufsize); 125 126 sparse_off = off + i386_round_page(vmst->hdr.bitmapsize) + 127 i386_round_page(vmst->hdr.ptesize); 128 if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off, 129 I386_PAGE_SIZE, sizeof(uint32_t)) == -1) { 130 return (-1); 131 } 132 off += i386_round_page(vmst->hdr.bitmapsize); 133 134 if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) { 135 return (-1); 136 } 137 off += i386_round_page(vmst->hdr.ptesize); 138 139 return (0); 140 } 141 142 static int 143 _i386_minidump_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa) 144 { 145 struct vmstate *vm; 146 i386_physaddr_pae_t offset; 147 i386_pte_pae_t pte; 148 kvaddr_t pteindex; 149 i386_physaddr_pae_t a; 150 off_t ofs; 151 152 vm = kd->vmst; 153 offset = va & I386_PAGE_MASK; 154 155 if (va >= vm->hdr.kernbase) { 156 pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT; 157 if (pteindex >= vm->hdr.ptesize / sizeof(pte)) 158 goto invalid; 159 pte = _i386_pte_pae_get(kd, pteindex); 160 if ((pte & I386_PG_V) == 0) { 161 _kvm_err(kd, kd->program, 162 "_i386_minidump_vatop_pae: pte not valid"); 163 goto invalid; 164 } 165 a = pte & I386_PG_FRAME_PAE; 166 ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE); 167 if (ofs == -1) { 168 _kvm_err(kd, kd->program, 169 "_i386_minidump_vatop_pae: physical address 0x%jx not in minidump", 170 (uintmax_t)a); 171 goto invalid; 172 } 173 *pa = ofs + offset; 174 return (I386_PAGE_SIZE - offset); 175 } else { 176 _kvm_err(kd, kd->program, 177 "_i386_minidump_vatop_pae: virtual address 0x%jx not minidumped", 178 (uintmax_t)va); 179 goto invalid; 180 } 181 182 invalid: 183 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 184 return (0); 185 } 186 187 static int 188 _i386_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa) 189 { 190 struct vmstate *vm; 191 i386_physaddr_t offset; 192 i386_pte_t pte; 193 kvaddr_t pteindex; 194 i386_physaddr_t a; 195 off_t ofs; 196 197 vm = kd->vmst; 198 offset = va & I386_PAGE_MASK; 199 200 if (va >= vm->hdr.kernbase) { 201 pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT; 202 if (pteindex >= vm->hdr.ptesize / sizeof(pte)) 203 goto invalid; 204 pte = _i386_pte_get(kd, pteindex); 205 if ((pte & I386_PG_V) == 0) { 206 _kvm_err(kd, kd->program, 207 "_i386_minidump_vatop: pte not valid"); 208 goto invalid; 209 } 210 a = pte & I386_PG_FRAME; 211 ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE); 212 if (ofs == -1) { 213 _kvm_err(kd, kd->program, 214 "_i386_minidump_vatop: physical address 0x%jx not in minidump", 215 (uintmax_t)a); 216 goto invalid; 217 } 218 *pa = ofs + offset; 219 return (I386_PAGE_SIZE - offset); 220 } else { 221 _kvm_err(kd, kd->program, 222 "_i386_minidump_vatop: virtual address 0x%jx not minidumped", 223 (uintmax_t)va); 224 goto invalid; 225 } 226 227 invalid: 228 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 229 return (0); 230 } 231 232 static int 233 _i386_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 234 { 235 236 if (ISALIVE(kd)) { 237 _kvm_err(kd, 0, "_i386_minidump_kvatop called in live kernel!"); 238 return (0); 239 } 240 if (kd->vmst->hdr.paemode) 241 return (_i386_minidump_vatop_pae(kd, va, pa)); 242 else 243 return (_i386_minidump_vatop(kd, va, pa)); 244 } 245 246 static vm_prot_t 247 _i386_entry_to_prot(uint64_t pte) 248 { 249 vm_prot_t prot = VM_PROT_READ; 250 251 /* Source: i386/pmap.c:pmap_protect() */ 252 if (pte & I386_PG_RW) 253 prot |= VM_PROT_WRITE; 254 if ((pte & I386_PG_NX) == 0) 255 prot |= VM_PROT_EXECUTE; 256 257 return prot; 258 } 259 260 struct i386_iter { 261 kvm_t *kd; 262 u_long nptes; 263 u_long pteindex; 264 }; 265 266 static void 267 _i386_iterator_init(struct i386_iter *it, kvm_t *kd) 268 { 269 struct vmstate *vm = kd->vmst; 270 271 it->kd = kd; 272 it->pteindex = 0; 273 if (vm->hdr.paemode) { 274 it->nptes = vm->hdr.ptesize / sizeof(i386_pte_pae_t); 275 } else { 276 it->nptes = vm->hdr.ptesize / sizeof(i386_pte_t); 277 } 278 return; 279 } 280 281 static int 282 _i386_iterator_next(struct i386_iter *it, u_long *pa, u_long *va, u_long *dva, 283 vm_prot_t *prot) 284 { 285 struct vmstate *vm = it->kd->vmst; 286 i386_pte_t pte32; 287 i386_pte_pae_t pte64; 288 int found = 0; 289 290 *dva = 0; 291 *pa = 0; 292 *va = 0; 293 *dva = 0; 294 *prot = 0; 295 for (; it->pteindex < it->nptes && found == 0; it->pteindex++) { 296 if (vm->hdr.paemode) { 297 pte64 = _i386_pte_pae_get(it->kd, it->pteindex); 298 if ((pte64 & I386_PG_V) == 0) 299 continue; 300 *prot = _i386_entry_to_prot(pte64); 301 *pa = pte64 & I386_PG_FRAME_PAE; 302 } else { 303 pte32 = _i386_pte_get(it->kd, it->pteindex); 304 if ((pte32 & I386_PG_V) == 0) 305 continue; 306 *prot = _i386_entry_to_prot(pte32); 307 *pa = pte32 & I386_PG_FRAME; 308 } 309 *va = vm->hdr.kernbase + (it->pteindex << I386_PAGE_SHIFT); 310 found = 1; 311 } 312 return found; 313 } 314 315 static int 316 _i386_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg) 317 { 318 struct i386_iter it; 319 u_long dva, pa, va; 320 vm_prot_t prot; 321 322 _i386_iterator_init(&it, kd); 323 while (_i386_iterator_next(&it, &pa, &va, &dva, &prot)) { 324 if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva, 325 prot, I386_PAGE_SIZE, 0)) { 326 return (0); 327 } 328 } 329 return (1); 330 } 331 332 static struct kvm_arch kvm_i386_minidump = { 333 .ka_probe = _i386_minidump_probe, 334 .ka_initvtop = _i386_minidump_initvtop, 335 .ka_freevtop = _i386_minidump_freevtop, 336 .ka_kvatop = _i386_minidump_kvatop, 337 .ka_native = _i386_native, 338 .ka_walk_pages = _i386_minidump_walk_pages, 339 }; 340 341 KVM_ARCH(kvm_i386_minidump); 342