1 /*- 2 * Copyright (c) 2006 Peter Wemm 3 * Copyright (c) 2019 Mitchell Horne 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_amd64.c r261799 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * RISC-V machine dependent routines for kvm and minidumps. 34 */ 35 36 #include <sys/param.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/riscv/include/minidump.h" 45 46 #include <limits.h> 47 48 #include "kvm_private.h" 49 #include "kvm_riscv.h" 50 51 #define riscv_round_page(x) roundup2((kvaddr_t)(x), RISCV_PAGE_SIZE) 52 53 struct vmstate { 54 struct minidumphdr hdr; 55 }; 56 57 static riscv_pt_entry_t 58 _riscv_pte_get(kvm_t *kd, u_long pteindex) 59 { 60 riscv_pt_entry_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte)); 61 62 return le64toh(*pte); 63 } 64 65 static int 66 _riscv_minidump_probe(kvm_t *kd) 67 { 68 69 return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_RISCV) && 70 _kvm_is_minidump(kd)); 71 } 72 73 static void 74 _riscv_minidump_freevtop(kvm_t *kd) 75 { 76 struct vmstate *vm = kd->vmst; 77 78 free(vm); 79 kd->vmst = NULL; 80 } 81 82 static int 83 _riscv_minidump_initvtop(kvm_t *kd) 84 { 85 struct vmstate *vmst; 86 off_t off, dump_avail_off, sparse_off; 87 88 vmst = _kvm_malloc(kd, sizeof(*vmst)); 89 if (vmst == NULL) { 90 _kvm_err(kd, kd->program, "cannot allocate vm"); 91 return (-1); 92 } 93 kd->vmst = vmst; 94 if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != 95 sizeof(vmst->hdr)) { 96 _kvm_err(kd, kd->program, "cannot read dump header"); 97 return (-1); 98 } 99 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, 100 sizeof(vmst->hdr.magic)) != 0) { 101 _kvm_err(kd, kd->program, "not a minidump for this platform"); 102 return (-1); 103 } 104 105 vmst->hdr.version = le32toh(vmst->hdr.version); 106 if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) { 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 = le32toh(vmst->hdr.msgbufsize); 112 vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize); 113 vmst->hdr.pmapsize = le32toh(vmst->hdr.pmapsize); 114 vmst->hdr.kernbase = le64toh(vmst->hdr.kernbase); 115 vmst->hdr.dmapphys = le64toh(vmst->hdr.dmapphys); 116 vmst->hdr.dmapbase = le64toh(vmst->hdr.dmapbase); 117 vmst->hdr.dmapend = le64toh(vmst->hdr.dmapend); 118 vmst->hdr.dumpavailsize = vmst->hdr.version == MINIDUMP_VERSION ? 119 le32toh(vmst->hdr.dumpavailsize) : 0; 120 121 /* Skip header and msgbuf */ 122 dump_avail_off = RISCV_PAGE_SIZE + riscv_round_page(vmst->hdr.msgbufsize); 123 124 /* Skip dump_avail */ 125 off = dump_avail_off + riscv_round_page(vmst->hdr.dumpavailsize); 126 127 /* build physical address lookup table for sparse pages */ 128 sparse_off = off + riscv_round_page(vmst->hdr.bitmapsize) + 129 riscv_round_page(vmst->hdr.pmapsize); 130 if (_kvm_pt_init(kd, vmst->hdr.dumpavailsize, dump_avail_off, 131 vmst->hdr.bitmapsize, off, sparse_off, RISCV_PAGE_SIZE) == -1) { 132 return (-1); 133 } 134 off += riscv_round_page(vmst->hdr.bitmapsize); 135 136 if (_kvm_pmap_init(kd, vmst->hdr.pmapsize, off) == -1) { 137 return (-1); 138 } 139 off += riscv_round_page(vmst->hdr.pmapsize); 140 141 return (0); 142 } 143 144 static int 145 _riscv_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa) 146 { 147 struct vmstate *vm; 148 riscv_physaddr_t offset; 149 riscv_pt_entry_t l3; 150 kvaddr_t l3_index; 151 riscv_physaddr_t a; 152 off_t ofs; 153 154 vm = kd->vmst; 155 offset = va & RISCV_PAGE_MASK; 156 157 if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) { 158 a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) & 159 ~RISCV_PAGE_MASK; 160 ofs = _kvm_pt_find(kd, a, RISCV_PAGE_SIZE); 161 if (ofs == -1) { 162 _kvm_err(kd, kd->program, "_riscv_minidump_vatop: " 163 "direct map address 0x%jx not in minidump", 164 (uintmax_t)va); 165 goto invalid; 166 } 167 *pa = ofs + offset; 168 return (RISCV_PAGE_SIZE - offset); 169 } else if (va >= vm->hdr.kernbase) { 170 l3_index = (va - vm->hdr.kernbase) >> RISCV_L3_SHIFT; 171 if (l3_index >= vm->hdr.pmapsize / sizeof(l3)) 172 goto invalid; 173 l3 = _riscv_pte_get(kd, l3_index); 174 if ((l3 & RISCV_PTE_V) == 0 || (l3 & RISCV_PTE_RWX) == 0) { 175 _kvm_err(kd, kd->program, 176 "_riscv_minidump_vatop: pte not valid"); 177 goto invalid; 178 } 179 a = (l3 >> RISCV_PTE_PPN0_S) << RISCV_L3_SHIFT; 180 ofs = _kvm_pt_find(kd, a, RISCV_PAGE_SIZE); 181 if (ofs == -1) { 182 _kvm_err(kd, kd->program, "_riscv_minidump_vatop: " 183 "physical address 0x%jx not in minidump", 184 (uintmax_t)a); 185 goto invalid; 186 } 187 *pa = ofs + offset; 188 return (RISCV_PAGE_SIZE - offset); 189 } else { 190 _kvm_err(kd, kd->program, 191 "_riscv_minidump_vatop: virtual address 0x%jx not minidumped", 192 (uintmax_t)va); 193 goto invalid; 194 } 195 196 invalid: 197 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 198 return (0); 199 } 200 201 static int 202 _riscv_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 203 { 204 205 if (ISALIVE(kd)) { 206 _kvm_err(kd, 0, 207 "_riscv_minidump_kvatop called in live kernel!"); 208 return (0); 209 } 210 return (_riscv_minidump_vatop(kd, va, pa)); 211 } 212 213 static int 214 _riscv_native(kvm_t *kd __unused) 215 { 216 217 #ifdef __riscv 218 return (1); 219 #else 220 return (0); 221 #endif 222 } 223 224 static vm_prot_t 225 _riscv_entry_to_prot(riscv_pt_entry_t pte) 226 { 227 vm_prot_t prot = VM_PROT_READ; 228 229 if ((pte & RISCV_PTE_W) != 0) 230 prot |= VM_PROT_WRITE; 231 if ((pte & RISCV_PTE_X) != 0) 232 prot |= VM_PROT_EXECUTE; 233 return prot; 234 } 235 236 static int 237 _riscv_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg) 238 { 239 struct vmstate *vm = kd->vmst; 240 u_long nptes = vm->hdr.pmapsize / sizeof(riscv_pt_entry_t); 241 u_long bmindex, dva, pa, pteindex, va; 242 struct kvm_bitmap bm; 243 vm_prot_t prot; 244 int ret = 0; 245 246 if (!_kvm_bitmap_init(&bm, vm->hdr.bitmapsize, &bmindex)) 247 return (0); 248 249 for (pteindex = 0; pteindex < nptes; pteindex++) { 250 riscv_pt_entry_t pte = _riscv_pte_get(kd, pteindex); 251 252 if (((pte & RISCV_PTE_V) == 0) || 253 ((pte & RISCV_PTE_RWX) == 0)) 254 continue; 255 256 va = vm->hdr.kernbase + (pteindex << RISCV_L3_SHIFT); 257 pa = (pte >> RISCV_PTE_PPN0_S) << RISCV_L3_SHIFT; 258 dva = vm->hdr.dmapbase + pa; 259 if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva, 260 _riscv_entry_to_prot(pte), RISCV_PAGE_SIZE, 0)) { 261 goto out; 262 } 263 } 264 265 while (_kvm_bitmap_next(&bm, &bmindex)) { 266 pa = _kvm_bit_id_pa(kd, bmindex, RISCV_PAGE_SIZE); 267 if (pa == _KVM_PA_INVALID) 268 break; 269 dva = vm->hdr.dmapbase + pa; 270 if (vm->hdr.dmapend < (dva + RISCV_PAGE_SIZE)) 271 break; 272 va = 0; 273 prot = VM_PROT_READ | VM_PROT_WRITE; 274 if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva, 275 prot, RISCV_PAGE_SIZE, 0)) { 276 goto out; 277 } 278 } 279 ret = 1; 280 281 out: 282 _kvm_bitmap_deinit(&bm); 283 return (ret); 284 } 285 286 static struct kvm_arch kvm_riscv_minidump = { 287 .ka_probe = _riscv_minidump_probe, 288 .ka_initvtop = _riscv_minidump_initvtop, 289 .ka_freevtop = _riscv_minidump_freevtop, 290 .ka_kvatop = _riscv_minidump_kvatop, 291 .ka_native = _riscv_native, 292 .ka_walk_pages = _riscv_minidump_walk_pages, 293 }; 294 295 KVM_ARCH(kvm_riscv_minidump); 296