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/param.h>
30d3c34fc0SLeandro Lupori 
31d3c34fc0SLeandro Lupori #include <kvm.h>
32d3c34fc0SLeandro Lupori 
33d3c34fc0SLeandro Lupori #include <limits.h>
34d3c34fc0SLeandro Lupori #include <stdlib.h>
35d3c34fc0SLeandro Lupori #include <string.h>
36d3c34fc0SLeandro Lupori #include <unistd.h>
37d3c34fc0SLeandro Lupori 
38d3c34fc0SLeandro Lupori #include "../../sys/powerpc/include/minidump.h"
39d3c34fc0SLeandro Lupori #include "kvm_private.h"
40d3c34fc0SLeandro Lupori #include "kvm_powerpc64.h"
41d3c34fc0SLeandro Lupori 
42d3c34fc0SLeandro Lupori 
43d3c34fc0SLeandro Lupori static int
_powerpc64_minidump_probe(kvm_t * kd)44d3c34fc0SLeandro Lupori _powerpc64_minidump_probe(kvm_t *kd)
45d3c34fc0SLeandro Lupori {
46d3c34fc0SLeandro Lupori 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_PPC64) &&
47d3c34fc0SLeandro Lupori 	    _kvm_is_minidump(kd));
48d3c34fc0SLeandro Lupori }
49d3c34fc0SLeandro Lupori 
50d3c34fc0SLeandro Lupori static void
_powerpc64_minidump_freevtop(kvm_t * kd)51d3c34fc0SLeandro Lupori _powerpc64_minidump_freevtop(kvm_t *kd)
52d3c34fc0SLeandro Lupori {
53d3c34fc0SLeandro Lupori 	struct vmstate *vm = kd->vmst;
54d3c34fc0SLeandro Lupori 
55d3c34fc0SLeandro Lupori 	if (vm == NULL)
56d3c34fc0SLeandro Lupori 		return;
57d3c34fc0SLeandro Lupori 	if (PPC64_MMU_OPS(kd))
58d3c34fc0SLeandro Lupori 		PPC64_MMU_OP(kd, cleanup);
59d3c34fc0SLeandro Lupori 	free(vm);
60d3c34fc0SLeandro Lupori 	kd->vmst = NULL;
61d3c34fc0SLeandro Lupori }
62d3c34fc0SLeandro Lupori 
63d3c34fc0SLeandro Lupori static int
_powerpc64_minidump_initvtop(kvm_t * kd)64d3c34fc0SLeandro Lupori _powerpc64_minidump_initvtop(kvm_t *kd)
65d3c34fc0SLeandro Lupori {
66d3c34fc0SLeandro Lupori 	struct vmstate *vmst;
67d3c34fc0SLeandro Lupori 	struct minidumphdr *hdr;
6800e66147SD Scott Phillips 	off_t dump_avail_off, bitmap_off, pmap_off, sparse_off;
69d3c34fc0SLeandro Lupori 	const char *mmu_name;
70d3c34fc0SLeandro Lupori 
71d3c34fc0SLeandro Lupori 	/* Alloc VM */
72d3c34fc0SLeandro Lupori 	vmst = _kvm_malloc(kd, sizeof(*vmst));
73d3c34fc0SLeandro Lupori 	if (vmst == NULL) {
74d3c34fc0SLeandro Lupori 		_kvm_err(kd, kd->program, "cannot allocate vm");
75d3c34fc0SLeandro Lupori 		return (-1);
76d3c34fc0SLeandro Lupori 	}
77d3c34fc0SLeandro Lupori 	hdr = &vmst->hdr;
78d3c34fc0SLeandro Lupori 	kd->vmst = vmst;
79d3c34fc0SLeandro Lupori 	PPC64_MMU_OPS(kd) = NULL;
80d3c34fc0SLeandro Lupori 	/* Read minidump header */
81d3c34fc0SLeandro Lupori 	if (pread(kd->pmfd, hdr, sizeof(*hdr), 0) != sizeof(*hdr)) {
82d3c34fc0SLeandro Lupori 		_kvm_err(kd, kd->program, "cannot read minidump header");
83d3c34fc0SLeandro Lupori 		goto failed;
84d3c34fc0SLeandro Lupori 	}
85d3c34fc0SLeandro Lupori 	/* Check magic */
86d3c34fc0SLeandro Lupori 	if (strncmp(MINIDUMP_MAGIC, hdr->magic, sizeof(hdr->magic)) != 0) {
87d3c34fc0SLeandro Lupori 		_kvm_err(kd, kd->program, "not a minidump for this platform");
88d3c34fc0SLeandro Lupori 		goto failed;
89d3c34fc0SLeandro Lupori 	}
90d3c34fc0SLeandro Lupori 	/* Check version */
91d3c34fc0SLeandro Lupori 	hdr->version = be32toh(hdr->version);
9200e66147SD Scott Phillips 	if (hdr->version != MINIDUMP_VERSION && hdr->version != 1) {
93d3c34fc0SLeandro Lupori 		_kvm_err(kd, kd->program, "wrong minidump version. "
94d3c34fc0SLeandro Lupori 		    "Expected %d got %d", MINIDUMP_VERSION, hdr->version);
95d3c34fc0SLeandro Lupori 		goto failed;
96d3c34fc0SLeandro Lupori 	}
97d3c34fc0SLeandro Lupori 	/* Convert header fields to host endian */
98d3c34fc0SLeandro Lupori 	hdr->msgbufsize		= be32toh(hdr->msgbufsize);
99d3c34fc0SLeandro Lupori 	hdr->bitmapsize		= be32toh(hdr->bitmapsize);
100d3c34fc0SLeandro Lupori 	hdr->pmapsize		= be32toh(hdr->pmapsize);
101d3c34fc0SLeandro Lupori 	hdr->kernbase		= be64toh(hdr->kernbase);
102d3c34fc0SLeandro Lupori 	hdr->kernend		= be64toh(hdr->kernend);
103d3c34fc0SLeandro Lupori 	hdr->dmapbase		= be64toh(hdr->dmapbase);
104d3c34fc0SLeandro Lupori 	hdr->dmapend		= be64toh(hdr->dmapend);
105d3c34fc0SLeandro Lupori 	hdr->hw_direct_map	= be32toh(hdr->hw_direct_map);
106d3c34fc0SLeandro Lupori 	hdr->startkernel	= be64toh(hdr->startkernel);
107d3c34fc0SLeandro Lupori 	hdr->endkernel		= be64toh(hdr->endkernel);
10800e66147SD Scott Phillips 	hdr->dumpavailsize	= hdr->version == MINIDUMP_VERSION ?
10900e66147SD Scott Phillips 	    be32toh(hdr->dumpavailsize) : 0;
110d3c34fc0SLeandro Lupori 
111d3c34fc0SLeandro Lupori 	vmst->kimg_start = PPC64_KERNBASE;
112d3c34fc0SLeandro Lupori 	vmst->kimg_end = PPC64_KERNBASE + hdr->endkernel - hdr->startkernel;
113d3c34fc0SLeandro Lupori 
114d3c34fc0SLeandro Lupori 	/* dump header */
115d3c34fc0SLeandro Lupori 	dprintf("%s: mmu_name=%s,\n\t"
116d3c34fc0SLeandro Lupori 	    "msgbufsize=0x%jx, bitmapsize=0x%jx, pmapsize=0x%jx, "
117d3c34fc0SLeandro Lupori 	    "kernbase=0x%jx, kernend=0x%jx,\n\t"
118d3c34fc0SLeandro Lupori 	    "dmapbase=0x%jx, dmapend=0x%jx, hw_direct_map=%d, "
119d3c34fc0SLeandro Lupori 	    "startkernel=0x%jx, endkernel=0x%jx\n\t"
120d3c34fc0SLeandro Lupori 	    "kimg_start=0x%jx, kimg_end=0x%jx\n",
121d3c34fc0SLeandro Lupori 	    __func__, hdr->mmu_name,
122d3c34fc0SLeandro Lupori 	    (uintmax_t)hdr->msgbufsize,
123d3c34fc0SLeandro Lupori 	    (uintmax_t)hdr->bitmapsize, (uintmax_t)hdr->pmapsize,
124d3c34fc0SLeandro Lupori 	    (uintmax_t)hdr->kernbase, (uintmax_t)hdr->kernend,
125d3c34fc0SLeandro Lupori 	    (uintmax_t)hdr->dmapbase, (uintmax_t)hdr->dmapend,
126d3c34fc0SLeandro Lupori 	    hdr->hw_direct_map, hdr->startkernel, hdr->endkernel,
127d3c34fc0SLeandro Lupori 	    (uintmax_t)vmst->kimg_start, (uintmax_t)vmst->kimg_end);
128d3c34fc0SLeandro Lupori 
129d3c34fc0SLeandro Lupori 	/* Detect and initialize MMU */
130d3c34fc0SLeandro Lupori 	mmu_name = hdr->mmu_name;
131d3c34fc0SLeandro Lupori 	if (strcmp(mmu_name, PPC64_MMU_G5) == 0 ||
132d3c34fc0SLeandro Lupori 	    strcmp(mmu_name, PPC64_MMU_PHYP) == 0)
133d3c34fc0SLeandro Lupori 		PPC64_MMU_OPS(kd) = ppc64_mmu_ops_hpt;
134d3c34fc0SLeandro Lupori 	else {
135d3c34fc0SLeandro Lupori 		_kvm_err(kd, kd->program, "unsupported MMU: %s", mmu_name);
136d3c34fc0SLeandro Lupori 		goto failed;
137d3c34fc0SLeandro Lupori 	}
138d3c34fc0SLeandro Lupori 	if (PPC64_MMU_OP(kd, init) == -1)
139d3c34fc0SLeandro Lupori 		goto failed;
140d3c34fc0SLeandro Lupori 
141d3c34fc0SLeandro Lupori 	/* Get dump parts' offsets */
14200e66147SD Scott Phillips 	dump_avail_off	= PPC64_PAGE_SIZE + ppc64_round_page(hdr->msgbufsize);
14300e66147SD Scott Phillips 	bitmap_off	= dump_avail_off + ppc64_round_page(hdr->dumpavailsize);
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 */
15400e66147SD Scott Phillips 	if (_kvm_pt_init(kd, hdr->dumpavailsize, dump_avail_off,
155b957b185SMark Johnston 	    hdr->bitmapsize, bitmap_off, sparse_off, PPC64_PAGE_SIZE) == -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
_powerpc64_minidump_kvatop(kvm_t * kd,kvaddr_t va,off_t * pa)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
_powerpc64_native(kvm_t * kd __unused)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 
18738cf2a43SLeandro Lupori static kssize_t
_powerpc64_kerndisp(kvm_t * kd)18838cf2a43SLeandro Lupori _powerpc64_kerndisp(kvm_t *kd)
18938cf2a43SLeandro Lupori {
19038cf2a43SLeandro Lupori 	return (kd->vmst->hdr.startkernel - PPC64_KERNBASE);
19138cf2a43SLeandro Lupori }
19238cf2a43SLeandro Lupori 
193d3c34fc0SLeandro Lupori static int
_powerpc64_minidump_walk_pages(kvm_t * kd,kvm_walk_pages_cb_t * cb,void * arg)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,
20638cf2a43SLeandro Lupori 	.ka_kerndisp	= _powerpc64_kerndisp,
207d3c34fc0SLeandro Lupori };
208d3c34fc0SLeandro Lupori 
209d3c34fc0SLeandro Lupori KVM_ARCH(kvm_powerpc64_minidump);
210