1d3c34fc0SLeandro Lupori /*- 2d3c34fc0SLeandro Lupori * Copyright (c) 2006 Peter Wemm 3d3c34fc0SLeandro Lupori * Copyright (c) 2019 Leandro Lupori 4d3c34fc0SLeandro Lupori * 5d3c34fc0SLeandro Lupori * Redistribution and use in source and binary forms, with or without 6d3c34fc0SLeandro Lupori * modification, are permitted provided that the following conditions 7d3c34fc0SLeandro Lupori * are met: 8d3c34fc0SLeandro Lupori * 1. Redistributions of source code must retain the above copyright 9d3c34fc0SLeandro Lupori * notice, this list of conditions and the following disclaimer. 10d3c34fc0SLeandro Lupori * 2. Redistributions in binary form must reproduce the above copyright 11d3c34fc0SLeandro Lupori * notice, this list of conditions and the following disclaimer in the 12d3c34fc0SLeandro Lupori * documentation and/or other materials provided with the distribution. 13d3c34fc0SLeandro Lupori * 14d3c34fc0SLeandro Lupori * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15d3c34fc0SLeandro Lupori * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16d3c34fc0SLeandro Lupori * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17d3c34fc0SLeandro Lupori * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18d3c34fc0SLeandro Lupori * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19d3c34fc0SLeandro Lupori * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20d3c34fc0SLeandro Lupori * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21d3c34fc0SLeandro Lupori * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22d3c34fc0SLeandro Lupori * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23d3c34fc0SLeandro Lupori * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24d3c34fc0SLeandro Lupori * SUCH DAMAGE. 25d3c34fc0SLeandro Lupori * 26d3c34fc0SLeandro Lupori * From: FreeBSD: src/lib/libkvm/kvm_minidump_riscv.c 27d3c34fc0SLeandro Lupori */ 28d3c34fc0SLeandro Lupori 29d3c34fc0SLeandro Lupori #include <sys/cdefs.h> 30d3c34fc0SLeandro Lupori __FBSDID("$FreeBSD$"); 31d3c34fc0SLeandro Lupori 32d3c34fc0SLeandro Lupori #include <sys/param.h> 33d3c34fc0SLeandro Lupori 34d3c34fc0SLeandro Lupori #include <kvm.h> 35d3c34fc0SLeandro Lupori 36d3c34fc0SLeandro Lupori #include <limits.h> 37d3c34fc0SLeandro Lupori #include <stdlib.h> 38d3c34fc0SLeandro Lupori #include <string.h> 39d3c34fc0SLeandro Lupori #include <unistd.h> 40d3c34fc0SLeandro Lupori 41d3c34fc0SLeandro Lupori #include "../../sys/powerpc/include/minidump.h" 42d3c34fc0SLeandro Lupori #include "kvm_private.h" 43d3c34fc0SLeandro Lupori #include "kvm_powerpc64.h" 44d3c34fc0SLeandro Lupori 45d3c34fc0SLeandro Lupori 46d3c34fc0SLeandro Lupori static int 47d3c34fc0SLeandro Lupori _powerpc64_minidump_probe(kvm_t *kd) 48d3c34fc0SLeandro Lupori { 49d3c34fc0SLeandro Lupori return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_PPC64) && 50d3c34fc0SLeandro Lupori _kvm_is_minidump(kd)); 51d3c34fc0SLeandro Lupori } 52d3c34fc0SLeandro Lupori 53d3c34fc0SLeandro Lupori static void 54d3c34fc0SLeandro Lupori _powerpc64_minidump_freevtop(kvm_t *kd) 55d3c34fc0SLeandro Lupori { 56d3c34fc0SLeandro Lupori struct vmstate *vm = kd->vmst; 57d3c34fc0SLeandro Lupori 58d3c34fc0SLeandro Lupori if (vm == NULL) 59d3c34fc0SLeandro Lupori return; 60d3c34fc0SLeandro Lupori if (PPC64_MMU_OPS(kd)) 61d3c34fc0SLeandro Lupori PPC64_MMU_OP(kd, cleanup); 62d3c34fc0SLeandro Lupori free(vm); 63d3c34fc0SLeandro Lupori kd->vmst = NULL; 64d3c34fc0SLeandro Lupori } 65d3c34fc0SLeandro Lupori 66d3c34fc0SLeandro Lupori static int 67d3c34fc0SLeandro Lupori _powerpc64_minidump_initvtop(kvm_t *kd) 68d3c34fc0SLeandro Lupori { 69d3c34fc0SLeandro Lupori struct vmstate *vmst; 70d3c34fc0SLeandro Lupori struct minidumphdr *hdr; 71d3c34fc0SLeandro Lupori off_t bitmap_off, pmap_off, sparse_off; 72d3c34fc0SLeandro Lupori const char *mmu_name; 73d3c34fc0SLeandro Lupori 74d3c34fc0SLeandro Lupori /* Alloc VM */ 75d3c34fc0SLeandro Lupori vmst = _kvm_malloc(kd, sizeof(*vmst)); 76d3c34fc0SLeandro Lupori if (vmst == NULL) { 77d3c34fc0SLeandro Lupori _kvm_err(kd, kd->program, "cannot allocate vm"); 78d3c34fc0SLeandro Lupori return (-1); 79d3c34fc0SLeandro Lupori } 80d3c34fc0SLeandro Lupori hdr = &vmst->hdr; 81d3c34fc0SLeandro Lupori kd->vmst = vmst; 82d3c34fc0SLeandro Lupori PPC64_MMU_OPS(kd) = NULL; 83d3c34fc0SLeandro Lupori /* Read minidump header */ 84d3c34fc0SLeandro Lupori if (pread(kd->pmfd, hdr, sizeof(*hdr), 0) != sizeof(*hdr)) { 85d3c34fc0SLeandro Lupori _kvm_err(kd, kd->program, "cannot read minidump header"); 86d3c34fc0SLeandro Lupori goto failed; 87d3c34fc0SLeandro Lupori } 88d3c34fc0SLeandro Lupori /* Check magic */ 89d3c34fc0SLeandro Lupori if (strncmp(MINIDUMP_MAGIC, hdr->magic, sizeof(hdr->magic)) != 0) { 90d3c34fc0SLeandro Lupori _kvm_err(kd, kd->program, "not a minidump for this platform"); 91d3c34fc0SLeandro Lupori goto failed; 92d3c34fc0SLeandro Lupori } 93d3c34fc0SLeandro Lupori /* Check version */ 94d3c34fc0SLeandro Lupori hdr->version = be32toh(hdr->version); 95d3c34fc0SLeandro Lupori if (hdr->version != MINIDUMP_VERSION) { 96d3c34fc0SLeandro Lupori _kvm_err(kd, kd->program, "wrong minidump version. " 97d3c34fc0SLeandro Lupori "Expected %d got %d", MINIDUMP_VERSION, hdr->version); 98d3c34fc0SLeandro Lupori goto failed; 99d3c34fc0SLeandro Lupori } 100d3c34fc0SLeandro Lupori /* Convert header fields to host endian */ 101d3c34fc0SLeandro Lupori hdr->msgbufsize = be32toh(hdr->msgbufsize); 102d3c34fc0SLeandro Lupori hdr->bitmapsize = be32toh(hdr->bitmapsize); 103d3c34fc0SLeandro Lupori hdr->pmapsize = be32toh(hdr->pmapsize); 104d3c34fc0SLeandro Lupori hdr->kernbase = be64toh(hdr->kernbase); 105d3c34fc0SLeandro Lupori hdr->kernend = be64toh(hdr->kernend); 106d3c34fc0SLeandro Lupori hdr->dmapbase = be64toh(hdr->dmapbase); 107d3c34fc0SLeandro Lupori hdr->dmapend = be64toh(hdr->dmapend); 108d3c34fc0SLeandro Lupori hdr->hw_direct_map = be32toh(hdr->hw_direct_map); 109d3c34fc0SLeandro Lupori hdr->startkernel = be64toh(hdr->startkernel); 110d3c34fc0SLeandro Lupori hdr->endkernel = be64toh(hdr->endkernel); 111d3c34fc0SLeandro Lupori 112d3c34fc0SLeandro Lupori vmst->kimg_start = PPC64_KERNBASE; 113d3c34fc0SLeandro Lupori vmst->kimg_end = PPC64_KERNBASE + hdr->endkernel - hdr->startkernel; 114d3c34fc0SLeandro Lupori 115d3c34fc0SLeandro Lupori /* dump header */ 116d3c34fc0SLeandro Lupori dprintf("%s: mmu_name=%s,\n\t" 117d3c34fc0SLeandro Lupori "msgbufsize=0x%jx, bitmapsize=0x%jx, pmapsize=0x%jx, " 118d3c34fc0SLeandro Lupori "kernbase=0x%jx, kernend=0x%jx,\n\t" 119d3c34fc0SLeandro Lupori "dmapbase=0x%jx, dmapend=0x%jx, hw_direct_map=%d, " 120d3c34fc0SLeandro Lupori "startkernel=0x%jx, endkernel=0x%jx\n\t" 121d3c34fc0SLeandro Lupori "kimg_start=0x%jx, kimg_end=0x%jx\n", 122d3c34fc0SLeandro Lupori __func__, hdr->mmu_name, 123d3c34fc0SLeandro Lupori (uintmax_t)hdr->msgbufsize, 124d3c34fc0SLeandro Lupori (uintmax_t)hdr->bitmapsize, (uintmax_t)hdr->pmapsize, 125d3c34fc0SLeandro Lupori (uintmax_t)hdr->kernbase, (uintmax_t)hdr->kernend, 126d3c34fc0SLeandro Lupori (uintmax_t)hdr->dmapbase, (uintmax_t)hdr->dmapend, 127d3c34fc0SLeandro Lupori hdr->hw_direct_map, hdr->startkernel, hdr->endkernel, 128d3c34fc0SLeandro Lupori (uintmax_t)vmst->kimg_start, (uintmax_t)vmst->kimg_end); 129d3c34fc0SLeandro Lupori 130d3c34fc0SLeandro Lupori /* Detect and initialize MMU */ 131d3c34fc0SLeandro Lupori mmu_name = hdr->mmu_name; 132d3c34fc0SLeandro Lupori if (strcmp(mmu_name, PPC64_MMU_G5) == 0 || 133d3c34fc0SLeandro Lupori strcmp(mmu_name, PPC64_MMU_PHYP) == 0) 134d3c34fc0SLeandro Lupori PPC64_MMU_OPS(kd) = ppc64_mmu_ops_hpt; 135d3c34fc0SLeandro Lupori else { 136d3c34fc0SLeandro Lupori _kvm_err(kd, kd->program, "unsupported MMU: %s", mmu_name); 137d3c34fc0SLeandro Lupori goto failed; 138d3c34fc0SLeandro Lupori } 139d3c34fc0SLeandro Lupori if (PPC64_MMU_OP(kd, init) == -1) 140d3c34fc0SLeandro Lupori goto failed; 141d3c34fc0SLeandro Lupori 142d3c34fc0SLeandro Lupori /* Get dump parts' offsets */ 143d3c34fc0SLeandro Lupori bitmap_off = PPC64_PAGE_SIZE + ppc64_round_page(hdr->msgbufsize); 144d3c34fc0SLeandro Lupori pmap_off = bitmap_off + ppc64_round_page(hdr->bitmapsize); 145d3c34fc0SLeandro Lupori sparse_off = pmap_off + ppc64_round_page(hdr->pmapsize); 146d3c34fc0SLeandro Lupori 147d3c34fc0SLeandro Lupori /* dump offsets */ 148d3c34fc0SLeandro Lupori dprintf("%s: msgbuf_off=0x%jx, bitmap_off=0x%jx, pmap_off=0x%jx, " 149d3c34fc0SLeandro Lupori "sparse_off=0x%jx\n", 150d3c34fc0SLeandro Lupori __func__, (uintmax_t)PPC64_PAGE_SIZE, (uintmax_t)bitmap_off, 151d3c34fc0SLeandro Lupori (uintmax_t)pmap_off, (uintmax_t)sparse_off); 152d3c34fc0SLeandro Lupori 153d3c34fc0SLeandro Lupori /* build physical address lookup table for sparse pages */ 154d3c34fc0SLeandro Lupori if (_kvm_pt_init(kd, hdr->bitmapsize, bitmap_off, sparse_off, 155d3c34fc0SLeandro Lupori PPC64_PAGE_SIZE, sizeof(uint64_t)) == -1) 156d3c34fc0SLeandro Lupori goto failed; 157d3c34fc0SLeandro Lupori 158d3c34fc0SLeandro Lupori if (_kvm_pmap_init(kd, hdr->pmapsize, pmap_off) == -1) 159d3c34fc0SLeandro Lupori goto failed; 160d3c34fc0SLeandro Lupori return (0); 161d3c34fc0SLeandro Lupori 162d3c34fc0SLeandro Lupori failed: 163d3c34fc0SLeandro Lupori _powerpc64_minidump_freevtop(kd); 164d3c34fc0SLeandro Lupori return (-1); 165d3c34fc0SLeandro Lupori } 166d3c34fc0SLeandro Lupori 167d3c34fc0SLeandro Lupori static int 168d3c34fc0SLeandro Lupori _powerpc64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 169d3c34fc0SLeandro Lupori { 170d3c34fc0SLeandro Lupori if (ISALIVE(kd)) { 171d3c34fc0SLeandro Lupori _kvm_err(kd, 0, "%s called in live kernel!", __func__); 172d3c34fc0SLeandro Lupori return (0); 173d3c34fc0SLeandro Lupori } 174d3c34fc0SLeandro Lupori return (PPC64_MMU_OP(kd, kvatop, va, pa)); 175d3c34fc0SLeandro Lupori } 176d3c34fc0SLeandro Lupori 177d3c34fc0SLeandro Lupori static int 178d3c34fc0SLeandro Lupori _powerpc64_native(kvm_t *kd __unused) 179d3c34fc0SLeandro Lupori { 180d3c34fc0SLeandro Lupori #ifdef __powerpc64__ 181d3c34fc0SLeandro Lupori return (1); 182d3c34fc0SLeandro Lupori #else 183d3c34fc0SLeandro Lupori return (0); 184d3c34fc0SLeandro Lupori #endif 185d3c34fc0SLeandro Lupori } 186d3c34fc0SLeandro Lupori 187*38cf2a43SLeandro Lupori static kssize_t 188*38cf2a43SLeandro Lupori _powerpc64_kerndisp(kvm_t *kd) 189*38cf2a43SLeandro Lupori { 190*38cf2a43SLeandro Lupori return (kd->vmst->hdr.startkernel - PPC64_KERNBASE); 191*38cf2a43SLeandro Lupori } 192*38cf2a43SLeandro Lupori 193d3c34fc0SLeandro Lupori static int 194d3c34fc0SLeandro Lupori _powerpc64_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg) 195d3c34fc0SLeandro Lupori { 196d3c34fc0SLeandro Lupori return (PPC64_MMU_OP(kd, walk_pages, cb, arg)); 197d3c34fc0SLeandro Lupori } 198d3c34fc0SLeandro Lupori 199d3c34fc0SLeandro Lupori static struct kvm_arch kvm_powerpc64_minidump = { 200d3c34fc0SLeandro Lupori .ka_probe = _powerpc64_minidump_probe, 201d3c34fc0SLeandro Lupori .ka_initvtop = _powerpc64_minidump_initvtop, 202d3c34fc0SLeandro Lupori .ka_freevtop = _powerpc64_minidump_freevtop, 203d3c34fc0SLeandro Lupori .ka_kvatop = _powerpc64_minidump_kvatop, 204d3c34fc0SLeandro Lupori .ka_walk_pages = _powerpc64_minidump_walk_pages, 205d3c34fc0SLeandro Lupori .ka_native = _powerpc64_native, 206*38cf2a43SLeandro Lupori .ka_kerndisp = _powerpc64_kerndisp, 207d3c34fc0SLeandro Lupori }; 208d3c34fc0SLeandro Lupori 209d3c34fc0SLeandro Lupori KVM_ARCH(kvm_powerpc64_minidump); 210