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