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