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/endian.h> 37 #include <sys/param.h> 38 #include <kvm.h> 39 #include <limits.h> 40 #include <stdint.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "../../sys/arm/include/minidump.h" 46 47 #include "kvm_private.h" 48 #include "kvm_arm.h" 49 50 #define arm_round_page(x) roundup2((kvaddr_t)(x), ARM_PAGE_SIZE) 51 52 struct vmstate { 53 struct minidumphdr hdr; 54 struct hpt hpt; 55 void *ptemap; 56 unsigned char ei_data; 57 }; 58 59 static int 60 _arm_minidump_probe(kvm_t *kd) 61 { 62 63 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) && 64 _kvm_is_minidump(kd)); 65 } 66 67 static void 68 _arm_minidump_freevtop(kvm_t *kd) 69 { 70 struct vmstate *vm = kd->vmst; 71 72 _kvm_hpt_free(&vm->hpt); 73 if (vm->ptemap) 74 free(vm->ptemap); 75 free(vm); 76 kd->vmst = NULL; 77 } 78 79 static int 80 _arm_minidump_initvtop(kvm_t *kd) 81 { 82 struct vmstate *vmst; 83 uint32_t *bitmap; 84 off_t off; 85 86 vmst = _kvm_malloc(kd, sizeof(*vmst)); 87 if (vmst == 0) { 88 _kvm_err(kd, kd->program, "cannot allocate vm"); 89 return (-1); 90 } 91 92 kd->vmst = vmst; 93 94 if (pread(kd->pmfd, &vmst->hdr, 95 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) { 96 _kvm_err(kd, kd->program, "cannot read dump header"); 97 return (-1); 98 } 99 100 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, 101 sizeof(vmst->hdr.magic)) != 0) { 102 _kvm_err(kd, kd->program, "not a minidump for this platform"); 103 return (-1); 104 } 105 vmst->hdr.version = _kvm32toh(kd, vmst->hdr.version); 106 if (vmst->hdr.version != MINIDUMP_VERSION) { 107 _kvm_err(kd, kd->program, "wrong minidump version. " 108 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version); 109 return (-1); 110 } 111 vmst->hdr.msgbufsize = _kvm32toh(kd, vmst->hdr.msgbufsize); 112 vmst->hdr.bitmapsize = _kvm32toh(kd, vmst->hdr.bitmapsize); 113 vmst->hdr.ptesize = _kvm32toh(kd, vmst->hdr.ptesize); 114 vmst->hdr.kernbase = _kvm32toh(kd, vmst->hdr.kernbase); 115 116 /* Skip header and msgbuf */ 117 off = ARM_PAGE_SIZE + arm_round_page(vmst->hdr.msgbufsize); 118 119 bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); 120 if (bitmap == NULL) { 121 _kvm_err(kd, kd->program, "cannot allocate %d bytes for " 122 "bitmap", vmst->hdr.bitmapsize); 123 return (-1); 124 } 125 126 if (pread(kd->pmfd, bitmap, vmst->hdr.bitmapsize, off) != 127 (ssize_t)vmst->hdr.bitmapsize) { 128 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", 129 vmst->hdr.bitmapsize); 130 free(bitmap); 131 return (-1); 132 } 133 off += arm_round_page(vmst->hdr.bitmapsize); 134 135 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize); 136 if (vmst->ptemap == NULL) { 137 _kvm_err(kd, kd->program, "cannot allocate %d bytes for " 138 "ptemap", vmst->hdr.ptesize); 139 free(bitmap); 140 return (-1); 141 } 142 143 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) != 144 (ssize_t)vmst->hdr.ptesize) { 145 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", 146 vmst->hdr.ptesize); 147 free(bitmap); 148 return (-1); 149 } 150 151 off += vmst->hdr.ptesize; 152 153 /* Build physical address hash table for sparse pages */ 154 _kvm_hpt_init(kd, &vmst->hpt, bitmap, vmst->hdr.bitmapsize, off, 155 ARM_PAGE_SIZE, sizeof(*bitmap)); 156 free(bitmap); 157 158 return (0); 159 } 160 161 static int 162 _arm_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 163 { 164 struct vmstate *vm; 165 arm_pt_entry_t pte; 166 arm_physaddr_t offset, a; 167 kvaddr_t pteindex; 168 off_t ofs; 169 arm_pt_entry_t *ptemap; 170 171 if (ISALIVE(kd)) { 172 _kvm_err(kd, 0, "_arm_minidump_kvatop called in live kernel!"); 173 return (0); 174 } 175 176 vm = kd->vmst; 177 ptemap = vm->ptemap; 178 179 if (va >= vm->hdr.kernbase) { 180 pteindex = (va - vm->hdr.kernbase) >> ARM_PAGE_SHIFT; 181 pte = _kvm32toh(kd, ptemap[pteindex]); 182 if (!pte) { 183 _kvm_err(kd, kd->program, 184 "_arm_minidump_kvatop: pte not valid"); 185 goto invalid; 186 } 187 if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) { 188 offset = va & ARM_L2_L_OFFSET; 189 a = pte & ARM_L2_L_FRAME; 190 } else if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_S) { 191 offset = va & ARM_L2_S_OFFSET; 192 a = pte & ARM_L2_S_FRAME; 193 } else 194 goto invalid; 195 196 ofs = _kvm_hpt_find(&vm->hpt, a); 197 if (ofs == -1) { 198 _kvm_err(kd, kd->program, "_arm_minidump_kvatop: " 199 "physical address 0x%jx not in minidump", 200 (uintmax_t)a); 201 goto invalid; 202 } 203 204 *pa = ofs + offset; 205 return (ARM_PAGE_SIZE - offset); 206 207 } else 208 _kvm_err(kd, kd->program, "_arm_minidump_kvatop: virtual " 209 "address 0x%jx not minidumped", (uintmax_t)va); 210 211 invalid: 212 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 213 return (0); 214 } 215 216 struct kvm_arch kvm_arm_minidump = { 217 .ka_probe = _arm_minidump_probe, 218 .ka_initvtop = _arm_minidump_initvtop, 219 .ka_freevtop = _arm_minidump_freevtop, 220 .ka_kvatop = _arm_minidump_kvatop, 221 .ka_native = _arm_native, 222 }; 223 224 KVM_ARCH(kvm_arm_minidump); 225