xref: /freebsd/lib/libkvm/kvm_minidump_riscv.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  * Copyright (c) 2019 Mitchell Horne
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_amd64.c r261799
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * RISC-V machine dependent routines for kvm and minidumps.
34  */
35 
36 #include <sys/param.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <vm/vm.h>
42 #include <kvm.h>
43 
44 #include "../../sys/riscv/include/minidump.h"
45 
46 #include <limits.h>
47 
48 #include "kvm_private.h"
49 #include "kvm_riscv.h"
50 
51 #define	riscv_round_page(x)	roundup2((kvaddr_t)(x), RISCV_PAGE_SIZE)
52 
53 struct vmstate {
54 	struct minidumphdr hdr;
55 };
56 
57 static riscv_pt_entry_t
58 _riscv_pte_get(kvm_t *kd, u_long pteindex)
59 {
60 	riscv_pt_entry_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
61 
62 	return le64toh(*pte);
63 }
64 
65 static int
66 _riscv_minidump_probe(kvm_t *kd)
67 {
68 
69 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_RISCV) &&
70 	    _kvm_is_minidump(kd));
71 }
72 
73 static void
74 _riscv_minidump_freevtop(kvm_t *kd)
75 {
76 	struct vmstate *vm = kd->vmst;
77 
78 	free(vm);
79 	kd->vmst = NULL;
80 }
81 
82 static int
83 _riscv_minidump_initvtop(kvm_t *kd)
84 {
85 	struct vmstate *vmst;
86 	off_t off, sparse_off;
87 
88 	vmst = _kvm_malloc(kd, sizeof(*vmst));
89 	if (vmst == NULL) {
90 		_kvm_err(kd, kd->program, "cannot allocate vm");
91 		return (-1);
92 	}
93 	kd->vmst = vmst;
94 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
95 	    sizeof(vmst->hdr)) {
96 		_kvm_err(kd, kd->program, "cannot read dump header");
97 		return (-1);
98 	}
99 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
100 	    sizeof(vmst->hdr.magic)) != 0) {
101 		_kvm_err(kd, kd->program, "not a minidump for this platform");
102 		return (-1);
103 	}
104 
105 	vmst->hdr.version = le32toh(vmst->hdr.version);
106 	if (vmst->hdr.version != MINIDUMP_VERSION) {
107 		_kvm_err(kd, kd->program, "wrong minidump version. "
108 		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
109 		return (-1);
110 	}
111 	vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize);
112 	vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize);
113 	vmst->hdr.pmapsize = le32toh(vmst->hdr.pmapsize);
114 	vmst->hdr.kernbase = le64toh(vmst->hdr.kernbase);
115 	vmst->hdr.dmapphys = le64toh(vmst->hdr.dmapphys);
116 	vmst->hdr.dmapbase = le64toh(vmst->hdr.dmapbase);
117 	vmst->hdr.dmapend = le64toh(vmst->hdr.dmapend);
118 
119 	/* Skip header and msgbuf */
120 	off = RISCV_PAGE_SIZE + riscv_round_page(vmst->hdr.msgbufsize);
121 
122 	/* build physical address lookup table for sparse pages */
123 	sparse_off = off + riscv_round_page(vmst->hdr.bitmapsize) +
124 	    riscv_round_page(vmst->hdr.pmapsize);
125 	if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off,
126 	    RISCV_PAGE_SIZE, sizeof(uint64_t)) == -1) {
127 		return (-1);
128 	}
129 	off += riscv_round_page(vmst->hdr.bitmapsize);
130 
131 	if (_kvm_pmap_init(kd, vmst->hdr.pmapsize, off) == -1) {
132 		return (-1);
133 	}
134 	off += riscv_round_page(vmst->hdr.pmapsize);
135 
136 	return (0);
137 }
138 
139 static int
140 _riscv_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
141 {
142 	struct vmstate *vm;
143 	riscv_physaddr_t offset;
144 	riscv_pt_entry_t l3;
145 	kvaddr_t l3_index;
146 	riscv_physaddr_t a;
147 	off_t ofs;
148 
149 	vm = kd->vmst;
150 	offset = va & RISCV_PAGE_MASK;
151 
152 	if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
153 		a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) &
154 		    ~RISCV_PAGE_MASK;
155 		ofs = _kvm_pt_find(kd, a, RISCV_PAGE_SIZE);
156 		if (ofs == -1) {
157 			_kvm_err(kd, kd->program, "_riscv_minidump_vatop: "
158 			    "direct map address 0x%jx not in minidump",
159 			    (uintmax_t)va);
160 			goto invalid;
161 		}
162 		*pa = ofs + offset;
163 		return (RISCV_PAGE_SIZE - offset);
164 	} else if (va >= vm->hdr.kernbase) {
165 		l3_index = (va - vm->hdr.kernbase) >> RISCV_L3_SHIFT;
166 		if (l3_index >= vm->hdr.pmapsize / sizeof(l3))
167 			goto invalid;
168 		l3 = _riscv_pte_get(kd, l3_index);
169 		if ((l3 & RISCV_PTE_V) == 0 || (l3 & RISCV_PTE_RWX) == 0) {
170 			_kvm_err(kd, kd->program,
171 			    "_riscv_minidump_vatop: pte not valid");
172 			goto invalid;
173 		}
174 		a = (l3 >> RISCV_PTE_PPN0_S) << RISCV_L3_SHIFT;
175 		ofs = _kvm_pt_find(kd, a, RISCV_PAGE_SIZE);
176 		if (ofs == -1) {
177 			_kvm_err(kd, kd->program, "_riscv_minidump_vatop: "
178 			    "physical address 0x%jx not in minidump",
179 			    (uintmax_t)a);
180 			goto invalid;
181 		}
182 		*pa = ofs + offset;
183 		return (RISCV_PAGE_SIZE - offset);
184 	} else {
185 		_kvm_err(kd, kd->program,
186 	    "_riscv_minidump_vatop: virtual address 0x%jx not minidumped",
187 		    (uintmax_t)va);
188 		goto invalid;
189 	}
190 
191 invalid:
192 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
193 	return (0);
194 }
195 
196 static int
197 _riscv_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
198 {
199 
200 	if (ISALIVE(kd)) {
201 		_kvm_err(kd, 0,
202 		    "_riscv_minidump_kvatop called in live kernel!");
203 		return (0);
204 	}
205 	return (_riscv_minidump_vatop(kd, va, pa));
206 }
207 
208 static int
209 _riscv_native(kvm_t *kd __unused)
210 {
211 
212 #ifdef __riscv
213 	return (1);
214 #else
215 	return (0);
216 #endif
217 }
218 
219 static vm_prot_t
220 _riscv_entry_to_prot(riscv_pt_entry_t pte)
221 {
222 	vm_prot_t prot = VM_PROT_READ;
223 
224 	if ((pte & RISCV_PTE_W) != 0)
225 		prot |= VM_PROT_WRITE;
226 	if ((pte & RISCV_PTE_X) != 0)
227 		prot |= VM_PROT_EXECUTE;
228 	return prot;
229 }
230 
231 static int
232 _riscv_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
233 {
234 	struct vmstate *vm = kd->vmst;
235 	u_long nptes = vm->hdr.pmapsize / sizeof(riscv_pt_entry_t);
236 	u_long bmindex, dva, pa, pteindex, va;
237 	struct kvm_bitmap bm;
238 	vm_prot_t prot;
239 	int ret = 0;
240 
241 	if (!_kvm_bitmap_init(&bm, vm->hdr.bitmapsize, &bmindex))
242 		return (0);
243 
244 	for (pteindex = 0; pteindex < nptes; pteindex++) {
245 		riscv_pt_entry_t pte = _riscv_pte_get(kd, pteindex);
246 
247 		if (((pte & RISCV_PTE_V) == 0) ||
248 		    ((pte & RISCV_PTE_RWX) == 0))
249 			continue;
250 
251 		va = vm->hdr.kernbase + (pteindex << RISCV_L3_SHIFT);
252 		pa = (pte >> RISCV_PTE_PPN0_S) << RISCV_L3_SHIFT;
253 		dva = vm->hdr.dmapbase + pa;
254 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
255 		    _riscv_entry_to_prot(pte), RISCV_PAGE_SIZE, 0)) {
256 			goto out;
257 		}
258 	}
259 
260 	while (_kvm_bitmap_next(&bm, &bmindex)) {
261 		pa = bmindex * RISCV_PAGE_SIZE;
262 		dva = vm->hdr.dmapbase + pa;
263 		if (vm->hdr.dmapend < (dva + RISCV_PAGE_SIZE))
264 			break;
265 		va = 0;
266 		prot = VM_PROT_READ | VM_PROT_WRITE;
267 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
268 		    prot, RISCV_PAGE_SIZE, 0)) {
269 			goto out;
270 		}
271 	}
272 	ret = 1;
273 
274 out:
275 	_kvm_bitmap_deinit(&bm);
276 	return (ret);
277 }
278 
279 static struct kvm_arch kvm_riscv_minidump = {
280 	.ka_probe = _riscv_minidump_probe,
281 	.ka_initvtop = _riscv_minidump_initvtop,
282 	.ka_freevtop = _riscv_minidump_freevtop,
283 	.ka_kvatop = _riscv_minidump_kvatop,
284 	.ka_native = _riscv_native,
285 	.ka_walk_pages = _riscv_minidump_walk_pages,
286 };
287 
288 KVM_ARCH(kvm_riscv_minidump);
289