1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki 5 * Copyright (c) 2006 Peter Wemm 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * From: FreeBSD: src/lib/libkvm/kvm_minidump_i386.c,v 1.2 2006/06/05 08:51:14 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * ARM machine dependent routines for kvm and minidumps. 36 */ 37 38 #include <sys/endian.h> 39 #include <sys/param.h> 40 #include <vm/vm.h> 41 #include <kvm.h> 42 #include <limits.h> 43 #include <stdint.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #include "../../sys/arm/include/minidump.h" 49 50 #include "kvm_private.h" 51 #include "kvm_arm.h" 52 53 #define arm_round_page(x) roundup2((kvaddr_t)(x), ARM_PAGE_SIZE) 54 55 struct vmstate { 56 struct minidumphdr hdr; 57 unsigned char ei_data; 58 }; 59 60 static arm_pt_entry_t 61 _arm_pte_get(kvm_t *kd, u_long pteindex) 62 { 63 arm_pt_entry_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte)); 64 65 return _kvm32toh(kd, *pte); 66 } 67 68 static int 69 _arm_minidump_probe(kvm_t *kd) 70 { 71 72 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) && 73 _kvm_is_minidump(kd)); 74 } 75 76 static void 77 _arm_minidump_freevtop(kvm_t *kd) 78 { 79 struct vmstate *vm = kd->vmst; 80 81 free(vm); 82 kd->vmst = NULL; 83 } 84 85 static int 86 _arm_minidump_initvtop(kvm_t *kd) 87 { 88 struct vmstate *vmst; 89 off_t off, sparse_off; 90 91 vmst = _kvm_malloc(kd, sizeof(*vmst)); 92 if (vmst == NULL) { 93 _kvm_err(kd, kd->program, "cannot allocate vm"); 94 return (-1); 95 } 96 97 kd->vmst = vmst; 98 99 if (pread(kd->pmfd, &vmst->hdr, 100 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) { 101 _kvm_err(kd, kd->program, "cannot read dump header"); 102 return (-1); 103 } 104 105 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, 106 sizeof(vmst->hdr.magic)) != 0) { 107 _kvm_err(kd, kd->program, "not a minidump for this platform"); 108 return (-1); 109 } 110 vmst->hdr.version = _kvm32toh(kd, vmst->hdr.version); 111 if (vmst->hdr.version != MINIDUMP_VERSION) { 112 _kvm_err(kd, kd->program, "wrong minidump version. " 113 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version); 114 return (-1); 115 } 116 vmst->hdr.msgbufsize = _kvm32toh(kd, vmst->hdr.msgbufsize); 117 vmst->hdr.bitmapsize = _kvm32toh(kd, vmst->hdr.bitmapsize); 118 vmst->hdr.ptesize = _kvm32toh(kd, vmst->hdr.ptesize); 119 vmst->hdr.kernbase = _kvm32toh(kd, vmst->hdr.kernbase); 120 vmst->hdr.arch = _kvm32toh(kd, vmst->hdr.arch); 121 vmst->hdr.mmuformat = _kvm32toh(kd, vmst->hdr.mmuformat); 122 if (vmst->hdr.mmuformat == MINIDUMP_MMU_FORMAT_UNKNOWN) { 123 /* This is a safe default as 1K pages are not used. */ 124 vmst->hdr.mmuformat = MINIDUMP_MMU_FORMAT_V6; 125 } 126 127 /* Skip header and msgbuf */ 128 off = ARM_PAGE_SIZE + arm_round_page(vmst->hdr.msgbufsize); 129 130 sparse_off = off + arm_round_page(vmst->hdr.bitmapsize) + 131 arm_round_page(vmst->hdr.ptesize); 132 if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off, 133 ARM_PAGE_SIZE, sizeof(uint32_t)) == -1) { 134 return (-1); 135 } 136 off += arm_round_page(vmst->hdr.bitmapsize); 137 138 if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) { 139 return (-1); 140 } 141 off += arm_round_page(vmst->hdr.ptesize); 142 143 return (0); 144 } 145 146 static int 147 _arm_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 148 { 149 struct vmstate *vm; 150 arm_pt_entry_t pte; 151 arm_physaddr_t offset, a; 152 kvaddr_t pteindex; 153 off_t ofs; 154 155 if (ISALIVE(kd)) { 156 _kvm_err(kd, 0, "_arm_minidump_kvatop called in live kernel!"); 157 return (0); 158 } 159 160 vm = kd->vmst; 161 162 if (va >= vm->hdr.kernbase) { 163 pteindex = (va - vm->hdr.kernbase) >> ARM_PAGE_SHIFT; 164 if (pteindex >= vm->hdr.ptesize / sizeof(pte)) 165 goto invalid; 166 pte = _arm_pte_get(kd, pteindex); 167 if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_INV) { 168 _kvm_err(kd, kd->program, 169 "_arm_minidump_kvatop: pte not valid"); 170 goto invalid; 171 } 172 if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) { 173 /* 64K page -> convert to be like 4K page */ 174 offset = va & ARM_L2_S_OFFSET; 175 a = (pte & ARM_L2_L_FRAME) + 176 (va & ARM_L2_L_OFFSET & ARM_L2_S_FRAME); 177 } else { 178 if (kd->vmst->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4 && 179 (pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_T) { 180 _kvm_err(kd, kd->program, 181 "_arm_minidump_kvatop: pte not supported"); 182 goto invalid; 183 } 184 /* 4K page */ 185 offset = va & ARM_L2_S_OFFSET; 186 a = pte & ARM_L2_S_FRAME; 187 } 188 189 ofs = _kvm_pt_find(kd, a, ARM_PAGE_SIZE); 190 if (ofs == -1) { 191 _kvm_err(kd, kd->program, "_arm_minidump_kvatop: " 192 "physical address 0x%jx not in minidump", 193 (uintmax_t)a); 194 goto invalid; 195 } 196 197 *pa = ofs + offset; 198 return (ARM_PAGE_SIZE - offset); 199 } else 200 _kvm_err(kd, kd->program, "_arm_minidump_kvatop: virtual " 201 "address 0x%jx not minidumped", (uintmax_t)va); 202 203 invalid: 204 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 205 return (0); 206 } 207 208 static vm_prot_t 209 _arm_entry_to_prot(kvm_t *kd, arm_pt_entry_t pte) 210 { 211 struct vmstate *vm = kd->vmst; 212 vm_prot_t prot = VM_PROT_READ; 213 214 /* Source: arm/arm/pmap-v4.c:pmap_fault_fixup() */ 215 if (vm->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4) { 216 if (pte & ARM_L2_S_PROT_W) 217 prot |= VM_PROT_WRITE; 218 return prot; 219 } 220 221 /* Source: arm/arm/pmap-v6.c:pmap_protect() */ 222 if ((pte & ARM_PTE2_RO) == 0) 223 prot |= VM_PROT_WRITE; 224 if ((pte & ARM_PTE2_NX) == 0) 225 prot |= VM_PROT_EXECUTE; 226 return prot; 227 } 228 229 static int 230 _arm_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg) 231 { 232 struct vmstate *vm = kd->vmst; 233 u_long nptes = vm->hdr.ptesize / sizeof(arm_pt_entry_t); 234 u_long dva, pa, pteindex, va; 235 236 for (pteindex = 0; pteindex < nptes; pteindex++) { 237 arm_pt_entry_t pte = _arm_pte_get(kd, pteindex); 238 239 if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_INV) 240 continue; 241 242 va = vm->hdr.kernbase + (pteindex << ARM_PAGE_SHIFT); 243 if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) { 244 /* 64K page */ 245 pa = (pte & ARM_L2_L_FRAME) + 246 (va & ARM_L2_L_OFFSET & ARM_L2_S_FRAME); 247 } else { 248 if (vm->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4 && 249 (pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_T) { 250 continue; 251 } 252 /* 4K page */ 253 pa = pte & ARM_L2_S_FRAME; 254 } 255 256 dva = 0; /* no direct map on this platform */ 257 if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva, 258 _arm_entry_to_prot(kd, pte), ARM_PAGE_SIZE, 0)) 259 return (0); 260 } 261 return (1); 262 } 263 264 static struct kvm_arch kvm_arm_minidump = { 265 .ka_probe = _arm_minidump_probe, 266 .ka_initvtop = _arm_minidump_initvtop, 267 .ka_freevtop = _arm_minidump_freevtop, 268 .ka_kvatop = _arm_minidump_kvatop, 269 .ka_native = _arm_native, 270 .ka_walk_pages = _arm_minidump_walk_pages, 271 }; 272 273 KVM_ARCH(kvm_arm_minidump); 274