xref: /freebsd/lib/libkvm/kvm_minidump_powerpc64.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  * Copyright (c) 2019 Leandro Lupori
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_riscv.c
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 
34 #include <kvm.h>
35 
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "../../sys/powerpc/include/minidump.h"
42 #include "kvm_private.h"
43 #include "kvm_powerpc64.h"
44 
45 
46 static int
47 _powerpc64_minidump_probe(kvm_t *kd)
48 {
49 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_PPC64) &&
50 	    _kvm_is_minidump(kd));
51 }
52 
53 static void
54 _powerpc64_minidump_freevtop(kvm_t *kd)
55 {
56 	struct vmstate *vm = kd->vmst;
57 
58 	if (vm == NULL)
59 		return;
60 	if (PPC64_MMU_OPS(kd))
61 		PPC64_MMU_OP(kd, cleanup);
62 	free(vm);
63 	kd->vmst = NULL;
64 }
65 
66 static int
67 _powerpc64_minidump_initvtop(kvm_t *kd)
68 {
69 	struct vmstate *vmst;
70 	struct minidumphdr *hdr;
71 	off_t dump_avail_off, bitmap_off, pmap_off, sparse_off;
72 	const char *mmu_name;
73 
74 	/* Alloc VM */
75 	vmst = _kvm_malloc(kd, sizeof(*vmst));
76 	if (vmst == NULL) {
77 		_kvm_err(kd, kd->program, "cannot allocate vm");
78 		return (-1);
79 	}
80 	hdr = &vmst->hdr;
81 	kd->vmst = vmst;
82 	PPC64_MMU_OPS(kd) = NULL;
83 	/* Read minidump header */
84 	if (pread(kd->pmfd, hdr, sizeof(*hdr), 0) != sizeof(*hdr)) {
85 		_kvm_err(kd, kd->program, "cannot read minidump header");
86 		goto failed;
87 	}
88 	/* Check magic */
89 	if (strncmp(MINIDUMP_MAGIC, hdr->magic, sizeof(hdr->magic)) != 0) {
90 		_kvm_err(kd, kd->program, "not a minidump for this platform");
91 		goto failed;
92 	}
93 	/* Check version */
94 	hdr->version = be32toh(hdr->version);
95 	if (hdr->version != MINIDUMP_VERSION && hdr->version != 1) {
96 		_kvm_err(kd, kd->program, "wrong minidump version. "
97 		    "Expected %d got %d", MINIDUMP_VERSION, hdr->version);
98 		goto failed;
99 	}
100 	/* Convert header fields to host endian */
101 	hdr->msgbufsize		= be32toh(hdr->msgbufsize);
102 	hdr->bitmapsize		= be32toh(hdr->bitmapsize);
103 	hdr->pmapsize		= be32toh(hdr->pmapsize);
104 	hdr->kernbase		= be64toh(hdr->kernbase);
105 	hdr->kernend		= be64toh(hdr->kernend);
106 	hdr->dmapbase		= be64toh(hdr->dmapbase);
107 	hdr->dmapend		= be64toh(hdr->dmapend);
108 	hdr->hw_direct_map	= be32toh(hdr->hw_direct_map);
109 	hdr->startkernel	= be64toh(hdr->startkernel);
110 	hdr->endkernel		= be64toh(hdr->endkernel);
111 	hdr->dumpavailsize	= hdr->version == MINIDUMP_VERSION ?
112 	    be32toh(hdr->dumpavailsize) : 0;
113 
114 	vmst->kimg_start = PPC64_KERNBASE;
115 	vmst->kimg_end = PPC64_KERNBASE + hdr->endkernel - hdr->startkernel;
116 
117 	/* dump header */
118 	dprintf("%s: mmu_name=%s,\n\t"
119 	    "msgbufsize=0x%jx, bitmapsize=0x%jx, pmapsize=0x%jx, "
120 	    "kernbase=0x%jx, kernend=0x%jx,\n\t"
121 	    "dmapbase=0x%jx, dmapend=0x%jx, hw_direct_map=%d, "
122 	    "startkernel=0x%jx, endkernel=0x%jx\n\t"
123 	    "kimg_start=0x%jx, kimg_end=0x%jx\n",
124 	    __func__, hdr->mmu_name,
125 	    (uintmax_t)hdr->msgbufsize,
126 	    (uintmax_t)hdr->bitmapsize, (uintmax_t)hdr->pmapsize,
127 	    (uintmax_t)hdr->kernbase, (uintmax_t)hdr->kernend,
128 	    (uintmax_t)hdr->dmapbase, (uintmax_t)hdr->dmapend,
129 	    hdr->hw_direct_map, hdr->startkernel, hdr->endkernel,
130 	    (uintmax_t)vmst->kimg_start, (uintmax_t)vmst->kimg_end);
131 
132 	/* Detect and initialize MMU */
133 	mmu_name = hdr->mmu_name;
134 	if (strcmp(mmu_name, PPC64_MMU_G5) == 0 ||
135 	    strcmp(mmu_name, PPC64_MMU_PHYP) == 0)
136 		PPC64_MMU_OPS(kd) = ppc64_mmu_ops_hpt;
137 	else {
138 		_kvm_err(kd, kd->program, "unsupported MMU: %s", mmu_name);
139 		goto failed;
140 	}
141 	if (PPC64_MMU_OP(kd, init) == -1)
142 		goto failed;
143 
144 	/* Get dump parts' offsets */
145 	dump_avail_off	= PPC64_PAGE_SIZE + ppc64_round_page(hdr->msgbufsize);
146 	bitmap_off	= dump_avail_off + ppc64_round_page(hdr->dumpavailsize);
147 	pmap_off	= bitmap_off + ppc64_round_page(hdr->bitmapsize);
148 	sparse_off	= pmap_off + ppc64_round_page(hdr->pmapsize);
149 
150 	/* dump offsets */
151 	dprintf("%s: msgbuf_off=0x%jx, bitmap_off=0x%jx, pmap_off=0x%jx, "
152 	    "sparse_off=0x%jx\n",
153 	    __func__, (uintmax_t)PPC64_PAGE_SIZE, (uintmax_t)bitmap_off,
154 	    (uintmax_t)pmap_off, (uintmax_t)sparse_off);
155 
156 	/* build physical address lookup table for sparse pages */
157 	if (_kvm_pt_init(kd, hdr->dumpavailsize, dump_avail_off,
158 	    hdr->bitmapsize, bitmap_off, sparse_off, PPC64_PAGE_SIZE) == -1)
159 		goto failed;
160 
161 	if (_kvm_pmap_init(kd, hdr->pmapsize, pmap_off) == -1)
162 		goto failed;
163 	return (0);
164 
165 failed:
166 	_powerpc64_minidump_freevtop(kd);
167 	return (-1);
168 }
169 
170 static int
171 _powerpc64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
172 {
173 	if (ISALIVE(kd)) {
174 		_kvm_err(kd, 0, "%s called in live kernel!", __func__);
175 		return (0);
176 	}
177 	return (PPC64_MMU_OP(kd, kvatop, va, pa));
178 }
179 
180 static int
181 _powerpc64_native(kvm_t *kd __unused)
182 {
183 #ifdef __powerpc64__
184 	return (1);
185 #else
186 	return (0);
187 #endif
188 }
189 
190 static kssize_t
191 _powerpc64_kerndisp(kvm_t *kd)
192 {
193 	return (kd->vmst->hdr.startkernel - PPC64_KERNBASE);
194 }
195 
196 static int
197 _powerpc64_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
198 {
199 	return (PPC64_MMU_OP(kd, walk_pages, cb, arg));
200 }
201 
202 static struct kvm_arch kvm_powerpc64_minidump = {
203 	.ka_probe	= _powerpc64_minidump_probe,
204 	.ka_initvtop	= _powerpc64_minidump_initvtop,
205 	.ka_freevtop	= _powerpc64_minidump_freevtop,
206 	.ka_kvatop	= _powerpc64_minidump_kvatop,
207 	.ka_walk_pages	= _powerpc64_minidump_walk_pages,
208 	.ka_native	= _powerpc64_native,
209 	.ka_kerndisp	= _powerpc64_kerndisp,
210 };
211 
212 KVM_ARCH(kvm_powerpc64_minidump);
213