1/*-
2 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
3 * Copyright (c) 2006 Peter Wemm
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_i386.c,v 1.2 2006/06/05 08:51:14
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32/*
33 * ARM machine dependent routines for kvm and minidumps.
34 */
35
36#include <sys/param.h>
37#include <sys/user.h>
38#include <sys/proc.h>
39#include <sys/stat.h>
40#include <sys/mman.h>
41#include <sys/fnv_hash.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45#include <nlist.h>
46#include <kvm.h>
47
48#include <vm/vm.h>
49#include <vm/vm_param.h>
50
51#include <machine/elf.h>
52#include <machine/cpufunc.h>
53#include <machine/minidump.h>
54
55#include <limits.h>
56
57#include "kvm_private.h"
58
59struct hpte {
60	struct hpte	*next;
61	uint64_t	pa;
62	int64_t		off;
63};
64
65#define HPT_SIZE 1024
66
67/* minidump must be the first field */
68struct vmstate {
69	int		minidump;		/* 1 = minidump mode */
70	struct		minidumphdr hdr;
71	void		*hpt_head[HPT_SIZE];
72	uint32_t	*bitmap;
73	void		*ptemap;
74};
75
76static void
77hpt_insert(kvm_t *kd, uint64_t pa, int64_t off)
78{
79	struct hpte *hpte;
80	uint32_t fnv = FNV1_32_INIT;
81
82	fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
83	fnv &= (HPT_SIZE - 1);
84	hpte = malloc(sizeof(*hpte));
85	hpte->pa = pa;
86	hpte->off = off;
87	hpte->next = kd->vmst->hpt_head[fnv];
88	kd->vmst->hpt_head[fnv] = hpte;
89}
90
91static int64_t
92hpt_find(kvm_t *kd, uint64_t pa)
93{
94	struct hpte *hpte;
95	uint32_t fnv = FNV1_32_INIT;
96
97	fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
98	fnv &= (HPT_SIZE - 1);
99	for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next)
100		if (pa == hpte->pa)
101			return (hpte->off);
102
103	return (-1);
104}
105
106static int
107inithash(kvm_t *kd, uint32_t *base, int len, off_t off)
108{
109	uint64_t idx, pa;
110	uint32_t bit, bits;
111
112	for (idx = 0; idx < len / sizeof(*base); idx++) {
113		bits = base[idx];
114		while (bits) {
115			bit = ffs(bits) - 1;
116			bits &= ~(1ul << bit);
117			pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
118			hpt_insert(kd, pa, off);
119			off += PAGE_SIZE;
120		}
121	}
122	return (off);
123}
124
125void
126_kvm_minidump_freevtop(kvm_t *kd)
127{
128	struct vmstate *vm = kd->vmst;
129
130	if (vm->bitmap)
131		free(vm->bitmap);
132	if (vm->ptemap)
133		free(vm->ptemap);
134	free(vm);
135	kd->vmst = NULL;
136}
137
138int
139_kvm_minidump_initvtop(kvm_t *kd)
140{
141	struct vmstate *vmst;
142	off_t off;
143
144	vmst = _kvm_malloc(kd, sizeof(*vmst));
145	if (vmst == 0) {
146		_kvm_err(kd, kd->program, "cannot allocate vm");
147		return (-1);
148	}
149
150	kd->vmst = vmst;
151	vmst->minidump = 1;
152
153	if (pread(kd->pmfd, &vmst->hdr,
154	    sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
155		_kvm_err(kd, kd->program, "cannot read dump header");
156		return (-1);
157	}
158
159	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
160	    sizeof(vmst->hdr.magic)) != 0) {
161		_kvm_err(kd, kd->program, "not a minidump for this platform");
162		return (-1);
163	}
164	if (vmst->hdr.version != MINIDUMP_VERSION) {
165		_kvm_err(kd, kd->program, "wrong minidump version. "
166		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
167		return (-1);
168	}
169
170	/* Skip header and msgbuf */
171	off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
172
173	vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
174	if (vmst->bitmap == NULL) {
175		_kvm_err(kd, kd->program, "cannot allocate %d bytes for "
176		    "bitmap", vmst->hdr.bitmapsize);
177		return (-1);
178	}
179
180	if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
181	    (ssize_t)vmst->hdr.bitmapsize) {
182		_kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap",
183		    vmst->hdr.bitmapsize);
184		return (-1);
185	}
186	off += round_page(vmst->hdr.bitmapsize);
187
188	vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
189	if (vmst->ptemap == NULL) {
190		_kvm_err(kd, kd->program, "cannot allocate %d bytes for "
191		    "ptemap", vmst->hdr.ptesize);
192		return (-1);
193	}
194
195	if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
196	    (ssize_t)vmst->hdr.ptesize) {
197		_kvm_err(kd, kd->program, "cannot read %d bytes for ptemap",
198		    vmst->hdr.ptesize);
199		return (-1);
200	}
201
202	off += vmst->hdr.ptesize;
203
204	/* Build physical address hash table for sparse pages */
205	inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
206
207	return (0);
208}
209
210int
211_kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
212{
213	struct vmstate *vm;
214	pt_entry_t pte;
215	u_long offset, pteindex, a;
216	off_t ofs;
217	uint32_t *ptemap;
218
219	if (ISALIVE(kd)) {
220		_kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
221		return (0);
222	}
223
224	vm = kd->vmst;
225	ptemap = vm->ptemap;
226
227	if (va >= vm->hdr.kernbase) {
228		pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
229		pte = ptemap[pteindex];
230		if (!pte) {
231			_kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
232			goto invalid;
233		}
234		if ((pte & L2_TYPE_MASK) == L2_TYPE_L) {
235			offset = va & L2_L_OFFSET;
236			a = pte & L2_L_FRAME;
237		} else if ((pte & L2_TYPE_MASK) == L2_TYPE_S) {
238			offset = va & L2_S_OFFSET;
239			a = pte & L2_S_FRAME;
240		} else
241			goto invalid;
242
243		ofs = hpt_find(kd, a);
244		if (ofs == -1) {
245			_kvm_err(kd, kd->program, "_kvm_vatop: physical "
246			    "address 0x%lx not in minidump", a);
247			goto invalid;
248		}
249
250		*pa = ofs + offset;
251		return (PAGE_SIZE - offset);
252
253	} else
254		_kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx "
255		    "not minidumped", va);
256
257invalid:
258	_kvm_err(kd, 0, "invalid address (0x%lx)", va);
259	return (0);
260}
261