xref: /freebsd/lib/libkvm/kvm_minidump_riscv.c (revision 1d386b48)
15e58ed82SMark Johnston /*-
25e58ed82SMark Johnston  * Copyright (c) 2006 Peter Wemm
35e58ed82SMark Johnston  * Copyright (c) 2019 Mitchell Horne
45e58ed82SMark Johnston  *
55e58ed82SMark Johnston  * Redistribution and use in source and binary forms, with or without
65e58ed82SMark Johnston  * modification, are permitted provided that the following conditions
75e58ed82SMark Johnston  * are met:
85e58ed82SMark Johnston  * 1. Redistributions of source code must retain the above copyright
95e58ed82SMark Johnston  *    notice, this list of conditions and the following disclaimer.
105e58ed82SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
115e58ed82SMark Johnston  *    notice, this list of conditions and the following disclaimer in the
125e58ed82SMark Johnston  *    documentation and/or other materials provided with the distribution.
135e58ed82SMark Johnston  *
145e58ed82SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
155e58ed82SMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165e58ed82SMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175e58ed82SMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
185e58ed82SMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195e58ed82SMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
205e58ed82SMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215e58ed82SMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
225e58ed82SMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
235e58ed82SMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
245e58ed82SMark Johnston  * SUCH DAMAGE.
255e58ed82SMark Johnston  *
265e58ed82SMark Johnston  * From: FreeBSD: src/lib/libkvm/kvm_minidump_amd64.c r261799
275e58ed82SMark Johnston  */
285e58ed82SMark Johnston 
295e58ed82SMark Johnston #include <sys/cdefs.h>
305e58ed82SMark Johnston /*
315e58ed82SMark Johnston  * RISC-V machine dependent routines for kvm and minidumps.
325e58ed82SMark Johnston  */
335e58ed82SMark Johnston 
345e58ed82SMark Johnston #include <sys/param.h>
355e58ed82SMark Johnston #include <stdint.h>
365e58ed82SMark Johnston #include <stdlib.h>
375e58ed82SMark Johnston #include <string.h>
385e58ed82SMark Johnston #include <unistd.h>
395e58ed82SMark Johnston #include <vm/vm.h>
405e58ed82SMark Johnston #include <kvm.h>
415e58ed82SMark Johnston 
425e58ed82SMark Johnston #include "../../sys/riscv/include/minidump.h"
435e58ed82SMark Johnston 
445e58ed82SMark Johnston #include <limits.h>
455e58ed82SMark Johnston 
465e58ed82SMark Johnston #include "kvm_private.h"
475e58ed82SMark Johnston #include "kvm_riscv.h"
485e58ed82SMark Johnston 
495e58ed82SMark Johnston #define	riscv_round_page(x)	roundup2((kvaddr_t)(x), RISCV_PAGE_SIZE)
505e58ed82SMark Johnston 
515e58ed82SMark Johnston struct vmstate {
525e58ed82SMark Johnston 	struct minidumphdr hdr;
535e58ed82SMark Johnston };
545e58ed82SMark Johnston 
555e58ed82SMark Johnston static riscv_pt_entry_t
_riscv_pte_get(kvm_t * kd,u_long pteindex)565e58ed82SMark Johnston _riscv_pte_get(kvm_t *kd, u_long pteindex)
575e58ed82SMark Johnston {
585e58ed82SMark Johnston 	riscv_pt_entry_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
595e58ed82SMark Johnston 
605e58ed82SMark Johnston 	return le64toh(*pte);
615e58ed82SMark Johnston }
625e58ed82SMark Johnston 
635e58ed82SMark Johnston static int
_riscv_minidump_probe(kvm_t * kd)645e58ed82SMark Johnston _riscv_minidump_probe(kvm_t *kd)
655e58ed82SMark Johnston {
665e58ed82SMark Johnston 
675e58ed82SMark Johnston 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_RISCV) &&
685e58ed82SMark Johnston 	    _kvm_is_minidump(kd));
695e58ed82SMark Johnston }
705e58ed82SMark Johnston 
715e58ed82SMark Johnston static void
_riscv_minidump_freevtop(kvm_t * kd)725e58ed82SMark Johnston _riscv_minidump_freevtop(kvm_t *kd)
735e58ed82SMark Johnston {
745e58ed82SMark Johnston 	struct vmstate *vm = kd->vmst;
755e58ed82SMark Johnston 
765e58ed82SMark Johnston 	free(vm);
775e58ed82SMark Johnston 	kd->vmst = NULL;
785e58ed82SMark Johnston }
795e58ed82SMark Johnston 
805e58ed82SMark Johnston static int
_riscv_minidump_initvtop(kvm_t * kd)815e58ed82SMark Johnston _riscv_minidump_initvtop(kvm_t *kd)
825e58ed82SMark Johnston {
835e58ed82SMark Johnston 	struct vmstate *vmst;
8400e66147SD Scott Phillips 	off_t off, dump_avail_off, sparse_off;
855e58ed82SMark Johnston 
865e58ed82SMark Johnston 	vmst = _kvm_malloc(kd, sizeof(*vmst));
875e58ed82SMark Johnston 	if (vmst == NULL) {
885e58ed82SMark Johnston 		_kvm_err(kd, kd->program, "cannot allocate vm");
895e58ed82SMark Johnston 		return (-1);
905e58ed82SMark Johnston 	}
915e58ed82SMark Johnston 	kd->vmst = vmst;
925e58ed82SMark Johnston 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
935e58ed82SMark Johnston 	    sizeof(vmst->hdr)) {
945e58ed82SMark Johnston 		_kvm_err(kd, kd->program, "cannot read dump header");
955e58ed82SMark Johnston 		return (-1);
965e58ed82SMark Johnston 	}
975e58ed82SMark Johnston 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
985e58ed82SMark Johnston 	    sizeof(vmst->hdr.magic)) != 0) {
995e58ed82SMark Johnston 		_kvm_err(kd, kd->program, "not a minidump for this platform");
1005e58ed82SMark Johnston 		return (-1);
1015e58ed82SMark Johnston 	}
1025e58ed82SMark Johnston 
1035e58ed82SMark Johnston 	vmst->hdr.version = le32toh(vmst->hdr.version);
10400e66147SD Scott Phillips 	if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) {
1055e58ed82SMark Johnston 		_kvm_err(kd, kd->program, "wrong minidump version. "
1065e58ed82SMark Johnston 		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
1075e58ed82SMark Johnston 		return (-1);
1085e58ed82SMark Johnston 	}
1095e58ed82SMark Johnston 	vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize);
1105e58ed82SMark Johnston 	vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize);
1115e58ed82SMark Johnston 	vmst->hdr.pmapsize = le32toh(vmst->hdr.pmapsize);
1125e58ed82SMark Johnston 	vmst->hdr.kernbase = le64toh(vmst->hdr.kernbase);
1135e58ed82SMark Johnston 	vmst->hdr.dmapphys = le64toh(vmst->hdr.dmapphys);
1145e58ed82SMark Johnston 	vmst->hdr.dmapbase = le64toh(vmst->hdr.dmapbase);
1155e58ed82SMark Johnston 	vmst->hdr.dmapend = le64toh(vmst->hdr.dmapend);
11600e66147SD Scott Phillips 	vmst->hdr.dumpavailsize = vmst->hdr.version == MINIDUMP_VERSION ?
11700e66147SD Scott Phillips 	    le32toh(vmst->hdr.dumpavailsize) : 0;
1185e58ed82SMark Johnston 
1195e58ed82SMark Johnston 	/* Skip header and msgbuf */
12000e66147SD Scott Phillips 	dump_avail_off = RISCV_PAGE_SIZE + riscv_round_page(vmst->hdr.msgbufsize);
12100e66147SD Scott Phillips 
12200e66147SD Scott Phillips 	/* Skip dump_avail */
12300e66147SD Scott Phillips 	off = dump_avail_off + riscv_round_page(vmst->hdr.dumpavailsize);
1245e58ed82SMark Johnston 
1255e58ed82SMark Johnston 	/* build physical address lookup table for sparse pages */
1265e58ed82SMark Johnston 	sparse_off = off + riscv_round_page(vmst->hdr.bitmapsize) +
1275e58ed82SMark Johnston 	    riscv_round_page(vmst->hdr.pmapsize);
12800e66147SD Scott Phillips 	if (_kvm_pt_init(kd, vmst->hdr.dumpavailsize, dump_avail_off,
129b957b185SMark Johnston 	    vmst->hdr.bitmapsize, off, sparse_off, RISCV_PAGE_SIZE) == -1) {
1305e58ed82SMark Johnston 		return (-1);
1315e58ed82SMark Johnston 	}
1325e58ed82SMark Johnston 	off += riscv_round_page(vmst->hdr.bitmapsize);
1335e58ed82SMark Johnston 
1345e58ed82SMark Johnston 	if (_kvm_pmap_init(kd, vmst->hdr.pmapsize, off) == -1) {
1355e58ed82SMark Johnston 		return (-1);
1365e58ed82SMark Johnston 	}
1375e58ed82SMark Johnston 	off += riscv_round_page(vmst->hdr.pmapsize);
1385e58ed82SMark Johnston 
1395e58ed82SMark Johnston 	return (0);
1405e58ed82SMark Johnston }
1415e58ed82SMark Johnston 
1425e58ed82SMark Johnston static int
_riscv_minidump_vatop(kvm_t * kd,kvaddr_t va,off_t * pa)1435e58ed82SMark Johnston _riscv_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
1445e58ed82SMark Johnston {
1455e58ed82SMark Johnston 	struct vmstate *vm;
1465e58ed82SMark Johnston 	riscv_physaddr_t offset;
1475e58ed82SMark Johnston 	riscv_pt_entry_t l3;
1485e58ed82SMark Johnston 	kvaddr_t l3_index;
1495e58ed82SMark Johnston 	riscv_physaddr_t a;
1505e58ed82SMark Johnston 	off_t ofs;
1515e58ed82SMark Johnston 
1525e58ed82SMark Johnston 	vm = kd->vmst;
1535e58ed82SMark Johnston 	offset = va & RISCV_PAGE_MASK;
1545e58ed82SMark Johnston 
1555e58ed82SMark Johnston 	if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
1565e58ed82SMark Johnston 		a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) &
1575e58ed82SMark Johnston 		    ~RISCV_PAGE_MASK;
1585e58ed82SMark Johnston 		ofs = _kvm_pt_find(kd, a, RISCV_PAGE_SIZE);
1595e58ed82SMark Johnston 		if (ofs == -1) {
1605e58ed82SMark Johnston 			_kvm_err(kd, kd->program, "_riscv_minidump_vatop: "
1615e58ed82SMark Johnston 			    "direct map address 0x%jx not in minidump",
1625e58ed82SMark Johnston 			    (uintmax_t)va);
1635e58ed82SMark Johnston 			goto invalid;
1645e58ed82SMark Johnston 		}
1655e58ed82SMark Johnston 		*pa = ofs + offset;
1665e58ed82SMark Johnston 		return (RISCV_PAGE_SIZE - offset);
1675e58ed82SMark Johnston 	} else if (va >= vm->hdr.kernbase) {
1685e58ed82SMark Johnston 		l3_index = (va - vm->hdr.kernbase) >> RISCV_L3_SHIFT;
1695e58ed82SMark Johnston 		if (l3_index >= vm->hdr.pmapsize / sizeof(l3))
1705e58ed82SMark Johnston 			goto invalid;
1715e58ed82SMark Johnston 		l3 = _riscv_pte_get(kd, l3_index);
1725e58ed82SMark Johnston 		if ((l3 & RISCV_PTE_V) == 0 || (l3 & RISCV_PTE_RWX) == 0) {
1735e58ed82SMark Johnston 			_kvm_err(kd, kd->program,
1745e58ed82SMark Johnston 			    "_riscv_minidump_vatop: pte not valid");
1755e58ed82SMark Johnston 			goto invalid;
1765e58ed82SMark Johnston 		}
1775e58ed82SMark Johnston 		a = (l3 >> RISCV_PTE_PPN0_S) << RISCV_L3_SHIFT;
1785e58ed82SMark Johnston 		ofs = _kvm_pt_find(kd, a, RISCV_PAGE_SIZE);
1795e58ed82SMark Johnston 		if (ofs == -1) {
1805e58ed82SMark Johnston 			_kvm_err(kd, kd->program, "_riscv_minidump_vatop: "
1815e58ed82SMark Johnston 			    "physical address 0x%jx not in minidump",
1825e58ed82SMark Johnston 			    (uintmax_t)a);
1835e58ed82SMark Johnston 			goto invalid;
1845e58ed82SMark Johnston 		}
1855e58ed82SMark Johnston 		*pa = ofs + offset;
1865e58ed82SMark Johnston 		return (RISCV_PAGE_SIZE - offset);
1875e58ed82SMark Johnston 	} else {
1885e58ed82SMark Johnston 		_kvm_err(kd, kd->program,
1895e58ed82SMark Johnston 	    "_riscv_minidump_vatop: virtual address 0x%jx not minidumped",
1905e58ed82SMark Johnston 		    (uintmax_t)va);
1915e58ed82SMark Johnston 		goto invalid;
1925e58ed82SMark Johnston 	}
1935e58ed82SMark Johnston 
1945e58ed82SMark Johnston invalid:
1955e58ed82SMark Johnston 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
1965e58ed82SMark Johnston 	return (0);
1975e58ed82SMark Johnston }
1985e58ed82SMark Johnston 
1995e58ed82SMark Johnston static int
_riscv_minidump_kvatop(kvm_t * kd,kvaddr_t va,off_t * pa)2005e58ed82SMark Johnston _riscv_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
2015e58ed82SMark Johnston {
2025e58ed82SMark Johnston 
2035e58ed82SMark Johnston 	if (ISALIVE(kd)) {
2045e58ed82SMark Johnston 		_kvm_err(kd, 0,
2055e58ed82SMark Johnston 		    "_riscv_minidump_kvatop called in live kernel!");
2065e58ed82SMark Johnston 		return (0);
2075e58ed82SMark Johnston 	}
2085e58ed82SMark Johnston 	return (_riscv_minidump_vatop(kd, va, pa));
2095e58ed82SMark Johnston }
2105e58ed82SMark Johnston 
2115e58ed82SMark Johnston static int
_riscv_native(kvm_t * kd __unused)2125e58ed82SMark Johnston _riscv_native(kvm_t *kd __unused)
2135e58ed82SMark Johnston {
2145e58ed82SMark Johnston 
2155e58ed82SMark Johnston #ifdef __riscv
2165e58ed82SMark Johnston 	return (1);
2175e58ed82SMark Johnston #else
2185e58ed82SMark Johnston 	return (0);
2195e58ed82SMark Johnston #endif
2205e58ed82SMark Johnston }
2215e58ed82SMark Johnston 
2225e58ed82SMark Johnston static vm_prot_t
_riscv_entry_to_prot(riscv_pt_entry_t pte)2235e58ed82SMark Johnston _riscv_entry_to_prot(riscv_pt_entry_t pte)
2245e58ed82SMark Johnston {
2255e58ed82SMark Johnston 	vm_prot_t prot = VM_PROT_READ;
2265e58ed82SMark Johnston 
2275e58ed82SMark Johnston 	if ((pte & RISCV_PTE_W) != 0)
2285e58ed82SMark Johnston 		prot |= VM_PROT_WRITE;
2295e58ed82SMark Johnston 	if ((pte & RISCV_PTE_X) != 0)
2305e58ed82SMark Johnston 		prot |= VM_PROT_EXECUTE;
2315e58ed82SMark Johnston 	return prot;
2325e58ed82SMark Johnston }
2335e58ed82SMark Johnston 
2345e58ed82SMark Johnston static int
_riscv_minidump_walk_pages(kvm_t * kd,kvm_walk_pages_cb_t * cb,void * arg)2355e58ed82SMark Johnston _riscv_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
2365e58ed82SMark Johnston {
2375e58ed82SMark Johnston 	struct vmstate *vm = kd->vmst;
2385e58ed82SMark Johnston 	u_long nptes = vm->hdr.pmapsize / sizeof(riscv_pt_entry_t);
2395e58ed82SMark Johnston 	u_long bmindex, dva, pa, pteindex, va;
2405e58ed82SMark Johnston 	struct kvm_bitmap bm;
2415e58ed82SMark Johnston 	vm_prot_t prot;
2425e58ed82SMark Johnston 	int ret = 0;
2435e58ed82SMark Johnston 
2445e58ed82SMark Johnston 	if (!_kvm_bitmap_init(&bm, vm->hdr.bitmapsize, &bmindex))
2455e58ed82SMark Johnston 		return (0);
2465e58ed82SMark Johnston 
2475e58ed82SMark Johnston 	for (pteindex = 0; pteindex < nptes; pteindex++) {
2485e58ed82SMark Johnston 		riscv_pt_entry_t pte = _riscv_pte_get(kd, pteindex);
2495e58ed82SMark Johnston 
2505e58ed82SMark Johnston 		if (((pte & RISCV_PTE_V) == 0) ||
2515e58ed82SMark Johnston 		    ((pte & RISCV_PTE_RWX) == 0))
2525e58ed82SMark Johnston 			continue;
2535e58ed82SMark Johnston 
2545e58ed82SMark Johnston 		va = vm->hdr.kernbase + (pteindex << RISCV_L3_SHIFT);
2555e58ed82SMark Johnston 		pa = (pte >> RISCV_PTE_PPN0_S) << RISCV_L3_SHIFT;
2565e58ed82SMark Johnston 		dva = vm->hdr.dmapbase + pa;
2575e58ed82SMark Johnston 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
2585e58ed82SMark Johnston 		    _riscv_entry_to_prot(pte), RISCV_PAGE_SIZE, 0)) {
2595e58ed82SMark Johnston 			goto out;
2605e58ed82SMark Johnston 		}
2615e58ed82SMark Johnston 	}
2625e58ed82SMark Johnston 
2635e58ed82SMark Johnston 	while (_kvm_bitmap_next(&bm, &bmindex)) {
26400e66147SD Scott Phillips 		pa = _kvm_bit_id_pa(kd, bmindex, RISCV_PAGE_SIZE);
26500e66147SD Scott Phillips 		if (pa == _KVM_PA_INVALID)
26600e66147SD Scott Phillips 			break;
2675e58ed82SMark Johnston 		dva = vm->hdr.dmapbase + pa;
2685e58ed82SMark Johnston 		if (vm->hdr.dmapend < (dva + RISCV_PAGE_SIZE))
2695e58ed82SMark Johnston 			break;
2705e58ed82SMark Johnston 		va = 0;
2715e58ed82SMark Johnston 		prot = VM_PROT_READ | VM_PROT_WRITE;
2725e58ed82SMark Johnston 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
2735e58ed82SMark Johnston 		    prot, RISCV_PAGE_SIZE, 0)) {
2745e58ed82SMark Johnston 			goto out;
2755e58ed82SMark Johnston 		}
2765e58ed82SMark Johnston 	}
2775e58ed82SMark Johnston 	ret = 1;
2785e58ed82SMark Johnston 
2795e58ed82SMark Johnston out:
2805e58ed82SMark Johnston 	_kvm_bitmap_deinit(&bm);
2815e58ed82SMark Johnston 	return (ret);
2825e58ed82SMark Johnston }
2835e58ed82SMark Johnston 
2845e58ed82SMark Johnston static struct kvm_arch kvm_riscv_minidump = {
2855e58ed82SMark Johnston 	.ka_probe = _riscv_minidump_probe,
2865e58ed82SMark Johnston 	.ka_initvtop = _riscv_minidump_initvtop,
2875e58ed82SMark Johnston 	.ka_freevtop = _riscv_minidump_freevtop,
2885e58ed82SMark Johnston 	.ka_kvatop = _riscv_minidump_kvatop,
2895e58ed82SMark Johnston 	.ka_native = _riscv_native,
2905e58ed82SMark Johnston 	.ka_walk_pages = _riscv_minidump_walk_pages,
2915e58ed82SMark Johnston };
2925e58ed82SMark Johnston 
2935e58ed82SMark Johnston KVM_ARCH(kvm_riscv_minidump);
294