xref: /freebsd/lib/libkvm/kvm_private.c (revision 08055452cbf24a3cea48cb4f665bab78d89b7a39)
1197eca22SWill Andrews /*-
2197eca22SWill Andrews  * Copyright (c) 1989, 1992, 1993
3197eca22SWill Andrews  *	The Regents of the University of California.  All rights reserved.
4197eca22SWill Andrews  *
5197eca22SWill Andrews  * This code is derived from software developed by the Computer Systems
6197eca22SWill Andrews  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7197eca22SWill Andrews  * BG 91-66 and contributed to Berkeley.
8197eca22SWill Andrews  *
9197eca22SWill Andrews  * Redistribution and use in source and binary forms, with or without
10197eca22SWill Andrews  * modification, are permitted provided that the following conditions
11197eca22SWill Andrews  * are met:
12197eca22SWill Andrews  * 1. Redistributions of source code must retain the above copyright
13197eca22SWill Andrews  *    notice, this list of conditions and the following disclaimer.
14197eca22SWill Andrews  * 2. Redistributions in binary form must reproduce the above copyright
15197eca22SWill Andrews  *    notice, this list of conditions and the following disclaimer in the
16197eca22SWill Andrews  *    documentation and/or other materials provided with the distribution.
17fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
18197eca22SWill Andrews  *    may be used to endorse or promote products derived from this software
19197eca22SWill Andrews  *    without specific prior written permission.
20197eca22SWill Andrews  *
21197eca22SWill Andrews  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22197eca22SWill Andrews  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23197eca22SWill Andrews  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24197eca22SWill Andrews  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25197eca22SWill Andrews  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26197eca22SWill Andrews  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27197eca22SWill Andrews  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28197eca22SWill Andrews  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29197eca22SWill Andrews  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30197eca22SWill Andrews  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31197eca22SWill Andrews  * SUCH DAMAGE.
32197eca22SWill Andrews  */
33197eca22SWill Andrews 
34197eca22SWill Andrews #include <sys/cdefs.h>
35197eca22SWill Andrews __FBSDID("$FreeBSD$");
36197eca22SWill Andrews 
37197eca22SWill Andrews #include <sys/param.h>
38197eca22SWill Andrews #include <sys/fnv_hash.h>
39197eca22SWill Andrews 
40197eca22SWill Andrews #define	_WANT_VNET
41197eca22SWill Andrews 
42197eca22SWill Andrews #include <sys/user.h>
43197eca22SWill Andrews #include <sys/linker.h>
44197eca22SWill Andrews #include <sys/pcpu.h>
45197eca22SWill Andrews #include <sys/stat.h>
46c9057838SWill Andrews #include <sys/mman.h>
47197eca22SWill Andrews 
4810108cb6SBjoern A. Zeeb #include <stdbool.h>
49197eca22SWill Andrews #include <net/vnet.h>
50197eca22SWill Andrews 
51ffdeef32SWill Andrews #include <assert.h>
52197eca22SWill Andrews #include <fcntl.h>
538baaf913SWill Andrews #include <vm/vm.h>
54197eca22SWill Andrews #include <kvm.h>
55197eca22SWill Andrews #include <limits.h>
56197eca22SWill Andrews #include <paths.h>
57197eca22SWill Andrews #include <stdint.h>
58197eca22SWill Andrews #include <stdio.h>
59197eca22SWill Andrews #include <stdlib.h>
60197eca22SWill Andrews #include <string.h>
61197eca22SWill Andrews #include <unistd.h>
62197eca22SWill Andrews #include <stdarg.h>
63c9057838SWill Andrews #include <inttypes.h>
64197eca22SWill Andrews 
65197eca22SWill Andrews #include "kvm_private.h"
66197eca22SWill Andrews 
67197eca22SWill Andrews /*
68197eca22SWill Andrews  * Routines private to libkvm.
69197eca22SWill Andrews  */
70197eca22SWill Andrews 
71197eca22SWill Andrews /* from src/lib/libc/gen/nlist.c */
72197eca22SWill Andrews int __fdnlist(int, struct nlist *);
73197eca22SWill Andrews 
74197eca22SWill Andrews /*
75197eca22SWill Andrews  * Report an error using printf style arguments.  "program" is kd->program
76197eca22SWill Andrews  * on hard errors, and 0 on soft errors, so that under sun error emulation,
77197eca22SWill Andrews  * only hard errors are printed out (otherwise, programs like gdb will
78197eca22SWill Andrews  * generate tons of error messages when trying to access bogus pointers).
79197eca22SWill Andrews  */
80197eca22SWill Andrews void
81197eca22SWill Andrews _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
82197eca22SWill Andrews {
83197eca22SWill Andrews 	va_list ap;
84197eca22SWill Andrews 
85197eca22SWill Andrews 	va_start(ap, fmt);
86197eca22SWill Andrews 	if (program != NULL) {
87197eca22SWill Andrews 		(void)fprintf(stderr, "%s: ", program);
88197eca22SWill Andrews 		(void)vfprintf(stderr, fmt, ap);
89197eca22SWill Andrews 		(void)fputc('\n', stderr);
90197eca22SWill Andrews 	} else
91197eca22SWill Andrews 		(void)vsnprintf(kd->errbuf,
92197eca22SWill Andrews 		    sizeof(kd->errbuf), fmt, ap);
93197eca22SWill Andrews 
94197eca22SWill Andrews 	va_end(ap);
95197eca22SWill Andrews }
96197eca22SWill Andrews 
97197eca22SWill Andrews void
98197eca22SWill Andrews _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
99197eca22SWill Andrews {
100197eca22SWill Andrews 	va_list ap;
101197eca22SWill Andrews 	int n;
102197eca22SWill Andrews 
103197eca22SWill Andrews 	va_start(ap, fmt);
104197eca22SWill Andrews 	if (program != NULL) {
105197eca22SWill Andrews 		(void)fprintf(stderr, "%s: ", program);
106197eca22SWill Andrews 		(void)vfprintf(stderr, fmt, ap);
107197eca22SWill Andrews 		(void)fprintf(stderr, ": %s\n", strerror(errno));
108197eca22SWill Andrews 	} else {
109197eca22SWill Andrews 		char *cp = kd->errbuf;
110197eca22SWill Andrews 
111197eca22SWill Andrews 		(void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
112197eca22SWill Andrews 		n = strlen(cp);
113197eca22SWill Andrews 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
114197eca22SWill Andrews 		    strerror(errno));
115197eca22SWill Andrews 	}
116197eca22SWill Andrews 	va_end(ap);
117197eca22SWill Andrews }
118197eca22SWill Andrews 
119197eca22SWill Andrews void *
120197eca22SWill Andrews _kvm_malloc(kvm_t *kd, size_t n)
121197eca22SWill Andrews {
122197eca22SWill Andrews 	void *p;
123197eca22SWill Andrews 
124197eca22SWill Andrews 	if ((p = calloc(n, sizeof(char))) == NULL)
125197eca22SWill Andrews 		_kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
126197eca22SWill Andrews 			 n, strerror(errno));
127197eca22SWill Andrews 	return (p);
128197eca22SWill Andrews }
129197eca22SWill Andrews 
130197eca22SWill Andrews int
131197eca22SWill Andrews _kvm_probe_elf_kernel(kvm_t *kd, int class, int machine)
132197eca22SWill Andrews {
133197eca22SWill Andrews 
134197eca22SWill Andrews 	return (kd->nlehdr.e_ident[EI_CLASS] == class &&
1358024ba45SLeandro Lupori 	    ((machine == EM_PPC || machine == EM_PPC64) ?
1368024ba45SLeandro Lupori 	     kd->nlehdr.e_type == ET_DYN : kd->nlehdr.e_type == ET_EXEC) &&
137197eca22SWill Andrews 	    kd->nlehdr.e_machine == machine);
138197eca22SWill Andrews }
139197eca22SWill Andrews 
140197eca22SWill Andrews int
141197eca22SWill Andrews _kvm_is_minidump(kvm_t *kd)
142197eca22SWill Andrews {
143197eca22SWill Andrews 	char minihdr[8];
144197eca22SWill Andrews 
145197eca22SWill Andrews 	if (kd->rawdump)
146197eca22SWill Andrews 		return (0);
147197eca22SWill Andrews 	if (pread(kd->pmfd, &minihdr, 8, 0) == 8 &&
148197eca22SWill Andrews 	    memcmp(&minihdr, "minidump", 8) == 0)
149197eca22SWill Andrews 		return (1);
150197eca22SWill Andrews 	return (0);
151197eca22SWill Andrews }
152197eca22SWill Andrews 
153197eca22SWill Andrews /*
154197eca22SWill Andrews  * The powerpc backend has a hack to strip a leading kerneldump
155197eca22SWill Andrews  * header from the core before treating it as an ELF header.
156197eca22SWill Andrews  *
157197eca22SWill Andrews  * We can add that here if we can get a change to libelf to support
158197eca22SWill Andrews  * an initial offset into the file.  Alternatively we could patch
159197eca22SWill Andrews  * savecore to extract cores from a regular file instead.
160197eca22SWill Andrews  */
161197eca22SWill Andrews int
162197eca22SWill Andrews _kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp)
163197eca22SWill Andrews {
164197eca22SWill Andrews 	GElf_Ehdr ehdr;
165197eca22SWill Andrews 	GElf_Phdr *phdr;
166197eca22SWill Andrews 	Elf *elf;
167197eca22SWill Andrews 	size_t i, phnum;
168197eca22SWill Andrews 
169197eca22SWill Andrews 	elf = elf_begin(kd->pmfd, ELF_C_READ, NULL);
170197eca22SWill Andrews 	if (elf == NULL) {
171197eca22SWill Andrews 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
172197eca22SWill Andrews 		return (-1);
173197eca22SWill Andrews 	}
174197eca22SWill Andrews 	if (elf_kind(elf) != ELF_K_ELF) {
175197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
176197eca22SWill Andrews 		goto bad;
177197eca22SWill Andrews 	}
178197eca22SWill Andrews 	if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) {
179197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
180197eca22SWill Andrews 		goto bad;
181197eca22SWill Andrews 	}
182197eca22SWill Andrews 	if (gelf_getehdr(elf, &ehdr) == NULL) {
183197eca22SWill Andrews 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
184197eca22SWill Andrews 		goto bad;
185197eca22SWill Andrews 	}
186197eca22SWill Andrews 	if (ehdr.e_type != ET_CORE) {
187197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
188197eca22SWill Andrews 		goto bad;
189197eca22SWill Andrews 	}
190197eca22SWill Andrews 	if (ehdr.e_machine != kd->nlehdr.e_machine) {
191197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
192197eca22SWill Andrews 		goto bad;
193197eca22SWill Andrews 	}
194197eca22SWill Andrews 
195197eca22SWill Andrews 	if (elf_getphdrnum(elf, &phnum) == -1) {
196197eca22SWill Andrews 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
197197eca22SWill Andrews 		goto bad;
198197eca22SWill Andrews 	}
199197eca22SWill Andrews 
200197eca22SWill Andrews 	phdr = calloc(phnum, sizeof(*phdr));
201197eca22SWill Andrews 	if (phdr == NULL) {
202197eca22SWill Andrews 		_kvm_err(kd, kd->program, "failed to allocate phdrs");
203197eca22SWill Andrews 		goto bad;
204197eca22SWill Andrews 	}
205197eca22SWill Andrews 
206197eca22SWill Andrews 	for (i = 0; i < phnum; i++) {
207197eca22SWill Andrews 		if (gelf_getphdr(elf, i, &phdr[i]) == NULL) {
208f6080aabSGleb Smirnoff 			free(phdr);
209197eca22SWill Andrews 			_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
210197eca22SWill Andrews 			goto bad;
211197eca22SWill Andrews 		}
212197eca22SWill Andrews 	}
213197eca22SWill Andrews 	elf_end(elf);
214197eca22SWill Andrews 	*phnump = phnum;
215197eca22SWill Andrews 	*phdrp = phdr;
216197eca22SWill Andrews 	return (0);
217197eca22SWill Andrews 
218197eca22SWill Andrews bad:
219197eca22SWill Andrews 	elf_end(elf);
220197eca22SWill Andrews 	return (-1);
221197eca22SWill Andrews }
222197eca22SWill Andrews 
223ffdeef32SWill Andrews /*
224ffdeef32SWill Andrews  * Transform v such that only bits [bit0, bitN) may be set.  Generates a
225ffdeef32SWill Andrews  * bitmask covering the number of bits, then shifts so +bit0+ is the first.
226ffdeef32SWill Andrews  */
227ffdeef32SWill Andrews static uint64_t
228ffdeef32SWill Andrews bitmask_range(uint64_t v, uint64_t bit0, uint64_t bitN)
229197eca22SWill Andrews {
230ffdeef32SWill Andrews 	if (bit0 == 0 && bitN == BITS_IN(v))
231ffdeef32SWill Andrews 		return (v);
232197eca22SWill Andrews 
233ffdeef32SWill Andrews 	return (v & (((1ULL << (bitN - bit0)) - 1ULL) << bit0));
234197eca22SWill Andrews }
235197eca22SWill Andrews 
236ffdeef32SWill Andrews /*
237ffdeef32SWill Andrews  * Returns the number of bits in a given byte array range starting at a
238ffdeef32SWill Andrews  * given base, from bit0 to bitN.  bit0 may be non-zero in the case of
239ffdeef32SWill Andrews  * counting backwards from bitN.
240ffdeef32SWill Andrews  */
241ffdeef32SWill Andrews static uint64_t
242ffdeef32SWill Andrews popcount_bytes(uint64_t *addr, uint32_t bit0, uint32_t bitN)
243ffdeef32SWill Andrews {
244ffdeef32SWill Andrews 	uint32_t res = bitN - bit0;
245ffdeef32SWill Andrews 	uint64_t count = 0;
246ffdeef32SWill Andrews 	uint32_t bound;
247ffdeef32SWill Andrews 
248ffdeef32SWill Andrews 	/* Align to 64-bit boundary on the left side if needed. */
249ffdeef32SWill Andrews 	if ((bit0 % BITS_IN(*addr)) != 0) {
250ffdeef32SWill Andrews 		bound = MIN(bitN, roundup2(bit0, BITS_IN(*addr)));
251ffdeef32SWill Andrews 		count += __bitcount64(bitmask_range(*addr, bit0, bound));
252ffdeef32SWill Andrews 		res -= (bound - bit0);
253ffdeef32SWill Andrews 		addr++;
254ffdeef32SWill Andrews 	}
255ffdeef32SWill Andrews 
256ffdeef32SWill Andrews 	while (res > 0) {
257ffdeef32SWill Andrews 		bound = MIN(res, BITS_IN(*addr));
258ffdeef32SWill Andrews 		count += __bitcount64(bitmask_range(*addr, 0, bound));
259ffdeef32SWill Andrews 		res -= bound;
260ffdeef32SWill Andrews 		addr++;
261ffdeef32SWill Andrews 	}
262ffdeef32SWill Andrews 
263ffdeef32SWill Andrews 	return (count);
264ffdeef32SWill Andrews }
265ffdeef32SWill Andrews 
266c9057838SWill Andrews void *
2672aa6a4f3SWill Andrews _kvm_pmap_get(kvm_t *kd, u_long idx, size_t len)
268c9057838SWill Andrews {
2698baaf913SWill Andrews 	uintptr_t off = idx * len;
270c9057838SWill Andrews 
2718baaf913SWill Andrews 	if ((off_t)off >= kd->pt_sparse_off)
272c9057838SWill Andrews 		return (NULL);
273c9057838SWill Andrews 	return (void *)((uintptr_t)kd->page_map + off);
274c9057838SWill Andrews }
275c9057838SWill Andrews 
276c9057838SWill Andrews void *
277c9057838SWill Andrews _kvm_map_get(kvm_t *kd, u_long pa, unsigned int page_size)
278c9057838SWill Andrews {
279c9057838SWill Andrews 	off_t off;
280c9057838SWill Andrews 	uintptr_t addr;
281c9057838SWill Andrews 
282c9057838SWill Andrews 	off = _kvm_pt_find(kd, pa, page_size);
283c9057838SWill Andrews 	if (off == -1)
284c9057838SWill Andrews 		return NULL;
285c9057838SWill Andrews 
286c9057838SWill Andrews 	addr = (uintptr_t)kd->page_map + off;
287c9057838SWill Andrews 	if (off >= kd->pt_sparse_off)
288c9057838SWill Andrews 		addr = (uintptr_t)kd->sparse_map + (off - kd->pt_sparse_off);
289c9057838SWill Andrews 	return (void *)addr;
290c9057838SWill Andrews }
291c9057838SWill Andrews 
292ffdeef32SWill Andrews int
29300e66147SD Scott Phillips _kvm_pt_init(kvm_t *kd, size_t dump_avail_size, off_t dump_avail_off,
294b957b185SMark Johnston     size_t map_len, off_t map_off, off_t sparse_off, int page_size)
295197eca22SWill Andrews {
296ffdeef32SWill Andrews 	uint64_t *addr;
297ffdeef32SWill Andrews 	uint32_t *popcount_bin;
298ffdeef32SWill Andrews 	int bin_popcounts = 0;
299ffdeef32SWill Andrews 	uint64_t pc_bins, res;
300ffdeef32SWill Andrews 	ssize_t rd;
301197eca22SWill Andrews 
30200e66147SD Scott Phillips 	kd->dump_avail_size = dump_avail_size;
30300e66147SD Scott Phillips 	if (dump_avail_size > 0) {
30400e66147SD Scott Phillips 		kd->dump_avail = mmap(NULL, kd->dump_avail_size, PROT_READ,
30500e66147SD Scott Phillips 		    MAP_PRIVATE, kd->pmfd, dump_avail_off);
30600e66147SD Scott Phillips 	} else {
30700e66147SD Scott Phillips 		/*
30800e66147SD Scott Phillips 		 * Older version minidumps don't provide dump_avail[],
30900e66147SD Scott Phillips 		 * so the bitmap is fully populated from 0 to
31000e66147SD Scott Phillips 		 * last_pa. Create an implied dump_avail that
31100e66147SD Scott Phillips 		 * expresses this.
31200e66147SD Scott Phillips 		 */
313b957b185SMark Johnston 		kd->dump_avail = calloc(4, sizeof(uint64_t));
314b957b185SMark Johnston 		kd->dump_avail[1] = _kvm64toh(kd, map_len * 8 * page_size);
31500e66147SD Scott Phillips 	}
31600e66147SD Scott Phillips 
317ffdeef32SWill Andrews 	/*
318ffdeef32SWill Andrews 	 * Map the bitmap specified by the arguments.
319ffdeef32SWill Andrews 	 */
320ffdeef32SWill Andrews 	kd->pt_map = _kvm_malloc(kd, map_len);
321ffdeef32SWill Andrews 	if (kd->pt_map == NULL) {
322ffdeef32SWill Andrews 		_kvm_err(kd, kd->program, "cannot allocate %zu bytes for bitmap",
323ffdeef32SWill Andrews 		    map_len);
324197eca22SWill Andrews 		return (-1);
325197eca22SWill Andrews 	}
326ffdeef32SWill Andrews 	rd = pread(kd->pmfd, kd->pt_map, map_len, map_off);
327ffdeef32SWill Andrews 	if (rd < 0 || rd != (ssize_t)map_len) {
328ffdeef32SWill Andrews 		_kvm_err(kd, kd->program, "cannot read %zu bytes for bitmap",
329ffdeef32SWill Andrews 		    map_len);
330ffdeef32SWill Andrews 		return (-1);
331ffdeef32SWill Andrews 	}
332ffdeef32SWill Andrews 	kd->pt_map_size = map_len;
333197eca22SWill Andrews 
334ffdeef32SWill Andrews 	/*
335ffdeef32SWill Andrews 	 * Generate a popcount cache for every POPCOUNT_BITS in the bitmap,
336ffdeef32SWill Andrews 	 * so lookups only have to calculate the number of bits set between
337ffdeef32SWill Andrews 	 * a cache point and their bit.  This reduces lookups to O(1),
338ffdeef32SWill Andrews 	 * without significantly increasing memory requirements.
339ffdeef32SWill Andrews 	 *
340ffdeef32SWill Andrews 	 * Round up the number of bins so that 'upper half' lookups work for
341ffdeef32SWill Andrews 	 * the final bin, if needed.  The first popcount is 0, since no bits
342ffdeef32SWill Andrews 	 * precede bit 0, so add 1 for that also.  Without this, extra work
343ffdeef32SWill Andrews 	 * would be needed to handle the first PTEs in _kvm_pt_find().
344ffdeef32SWill Andrews 	 */
345ffdeef32SWill Andrews 	addr = kd->pt_map;
346ffdeef32SWill Andrews 	res = map_len;
347ffdeef32SWill Andrews 	pc_bins = 1 + (res * NBBY + POPCOUNT_BITS / 2) / POPCOUNT_BITS;
348ffdeef32SWill Andrews 	kd->pt_popcounts = calloc(pc_bins, sizeof(uint32_t));
349c9057838SWill Andrews 	if (kd->pt_popcounts == NULL) {
350c9057838SWill Andrews 		_kvm_err(kd, kd->program, "cannot allocate popcount bins");
351ffdeef32SWill Andrews 		return (-1);
352c9057838SWill Andrews 	}
353ffdeef32SWill Andrews 
354ffdeef32SWill Andrews 	for (popcount_bin = &kd->pt_popcounts[1]; res > 0;
355ffdeef32SWill Andrews 	    addr++, res -= sizeof(*addr)) {
356ffdeef32SWill Andrews 		*popcount_bin += popcount_bytes(addr, 0,
357ffdeef32SWill Andrews 		    MIN(res * NBBY, BITS_IN(*addr)));
358ffdeef32SWill Andrews 		if (++bin_popcounts == POPCOUNTS_IN(*addr)) {
359ffdeef32SWill Andrews 			popcount_bin++;
360ffdeef32SWill Andrews 			*popcount_bin = *(popcount_bin - 1);
361ffdeef32SWill Andrews 			bin_popcounts = 0;
362ffdeef32SWill Andrews 		}
363ffdeef32SWill Andrews 	}
364ffdeef32SWill Andrews 
365ffdeef32SWill Andrews 	assert(pc_bins * sizeof(*popcount_bin) ==
366ffdeef32SWill Andrews 	    ((uintptr_t)popcount_bin - (uintptr_t)kd->pt_popcounts));
367ffdeef32SWill Andrews 
368ffdeef32SWill Andrews 	kd->pt_sparse_off = sparse_off;
369c9057838SWill Andrews 	kd->pt_sparse_size = (uint64_t)*popcount_bin * page_size;
370ffdeef32SWill Andrews 	kd->pt_page_size = page_size;
371c9057838SWill Andrews 
372c9057838SWill Andrews 	/*
373c9057838SWill Andrews 	 * Map the sparse page array.  This is useful for performing point
374c9057838SWill Andrews 	 * lookups of specific pages, e.g. for kvm_walk_pages.  Generally,
375c9057838SWill Andrews 	 * this is much larger than is reasonable to read in up front, so
376c9057838SWill Andrews 	 * mmap it in instead.
377c9057838SWill Andrews 	 */
378c9057838SWill Andrews 	kd->sparse_map = mmap(NULL, kd->pt_sparse_size, PROT_READ,
379c9057838SWill Andrews 	    MAP_PRIVATE, kd->pmfd, kd->pt_sparse_off);
380c9057838SWill Andrews 	if (kd->sparse_map == MAP_FAILED) {
381c9057838SWill Andrews 		_kvm_err(kd, kd->program, "cannot map %" PRIu64
3828baaf913SWill Andrews 		    " bytes from fd %d offset %jd for sparse map: %s",
383c9057838SWill Andrews 		    kd->pt_sparse_size, kd->pmfd,
3848baaf913SWill Andrews 		    (intmax_t)kd->pt_sparse_off, strerror(errno));
385c9057838SWill Andrews 		return (-1);
386c9057838SWill Andrews 	}
387c9057838SWill Andrews 	return (0);
388c9057838SWill Andrews }
389c9057838SWill Andrews 
390c9057838SWill Andrews int
391c9057838SWill Andrews _kvm_pmap_init(kvm_t *kd, uint32_t pmap_size, off_t pmap_off)
392c9057838SWill Andrews {
393c9057838SWill Andrews 	ssize_t exp_len = pmap_size;
394c9057838SWill Andrews 
395c9057838SWill Andrews 	kd->page_map_size = pmap_size;
396c9057838SWill Andrews 	kd->page_map_off = pmap_off;
397c9057838SWill Andrews 	kd->page_map = _kvm_malloc(kd, pmap_size);
398c9057838SWill Andrews 	if (kd->page_map == NULL) {
399c9057838SWill Andrews 		_kvm_err(kd, kd->program, "cannot allocate %u bytes "
400c9057838SWill Andrews 		    "for page map", pmap_size);
401c9057838SWill Andrews 		return (-1);
402c9057838SWill Andrews 	}
403c9057838SWill Andrews 	if (pread(kd->pmfd, kd->page_map, pmap_size, pmap_off) != exp_len) {
404c9057838SWill Andrews 		_kvm_err(kd, kd->program, "cannot read %d bytes from "
4058baaf913SWill Andrews 		    "offset %jd for page map", pmap_size, (intmax_t)pmap_off);
406c9057838SWill Andrews 		return (-1);
407c9057838SWill Andrews 	}
408ffdeef32SWill Andrews 	return (0);
409ffdeef32SWill Andrews }
410ffdeef32SWill Andrews 
41100e66147SD Scott Phillips static inline uint64_t
41200e66147SD Scott Phillips dump_avail_n(kvm_t *kd, long i)
41300e66147SD Scott Phillips {
41400e66147SD Scott Phillips 	return (_kvm64toh(kd, kd->dump_avail[i]));
41500e66147SD Scott Phillips }
41600e66147SD Scott Phillips 
41700e66147SD Scott Phillips uint64_t
41800e66147SD Scott Phillips _kvm_pa_bit_id(kvm_t *kd, uint64_t pa, unsigned int page_size)
41900e66147SD Scott Phillips {
42000e66147SD Scott Phillips 	uint64_t adj;
42100e66147SD Scott Phillips 	long i;
42200e66147SD Scott Phillips 
42300e66147SD Scott Phillips 	adj = 0;
42400e66147SD Scott Phillips 	for (i = 0; dump_avail_n(kd, i + 1) != 0; i += 2) {
42500e66147SD Scott Phillips 		if (pa >= dump_avail_n(kd, i + 1)) {
42600e66147SD Scott Phillips 			adj += howmany(dump_avail_n(kd, i + 1), page_size) -
42700e66147SD Scott Phillips 			    dump_avail_n(kd, i) / page_size;
42800e66147SD Scott Phillips 		} else {
42900e66147SD Scott Phillips 			return (pa / page_size -
43000e66147SD Scott Phillips 			    dump_avail_n(kd, i) / page_size + adj);
43100e66147SD Scott Phillips 		}
43200e66147SD Scott Phillips 	}
43300e66147SD Scott Phillips 	return (_KVM_BIT_ID_INVALID);
43400e66147SD Scott Phillips }
43500e66147SD Scott Phillips 
43600e66147SD Scott Phillips uint64_t
43700e66147SD Scott Phillips _kvm_bit_id_pa(kvm_t *kd, uint64_t bit_id, unsigned int page_size)
43800e66147SD Scott Phillips {
43900e66147SD Scott Phillips 	uint64_t sz;
44000e66147SD Scott Phillips 	long i;
44100e66147SD Scott Phillips 
44200e66147SD Scott Phillips 	for (i = 0; dump_avail_n(kd, i + 1) != 0; i += 2) {
44300e66147SD Scott Phillips 		sz = howmany(dump_avail_n(kd, i + 1), page_size) -
44400e66147SD Scott Phillips 		    dump_avail_n(kd, i) / page_size;
44500e66147SD Scott Phillips 		if (bit_id < sz) {
44600e66147SD Scott Phillips 			return (rounddown2(dump_avail_n(kd, i), page_size) +
44700e66147SD Scott Phillips 			    bit_id * page_size);
44800e66147SD Scott Phillips 		}
44900e66147SD Scott Phillips 		bit_id -= sz;
45000e66147SD Scott Phillips 	}
45100e66147SD Scott Phillips 	return (_KVM_PA_INVALID);
45200e66147SD Scott Phillips }
45300e66147SD Scott Phillips 
454ffdeef32SWill Andrews /*
455ffdeef32SWill Andrews  * Find the offset for the given physical page address; returns -1 otherwise.
456ffdeef32SWill Andrews  *
457ffdeef32SWill Andrews  * A page's offset is represented by the sparse page base offset plus the
458c9057838SWill Andrews  * number of bits set before its bit multiplied by page size.  This means
459ffdeef32SWill Andrews  * that if a page exists in the dump, it's necessary to know how many pages
460ffdeef32SWill Andrews  * in the dump precede it.  Reduce this O(n) counting to O(1) by caching the
461ffdeef32SWill Andrews  * number of bits set at POPCOUNT_BITS intervals.
462ffdeef32SWill Andrews  *
463ffdeef32SWill Andrews  * Then to find the number of pages before the requested address, simply
464ffdeef32SWill Andrews  * index into the cache and count the number of bits set between that cache
465ffdeef32SWill Andrews  * bin and the page's bit.  Halve the number of bytes that have to be
466ffdeef32SWill Andrews  * checked by also counting down from the next higher bin if it's closer.
467ffdeef32SWill Andrews  */
468ffdeef32SWill Andrews off_t
469c9057838SWill Andrews _kvm_pt_find(kvm_t *kd, uint64_t pa, unsigned int page_size)
470197eca22SWill Andrews {
471ffdeef32SWill Andrews 	uint64_t *bitmap = kd->pt_map;
47200e66147SD Scott Phillips 	uint64_t pte_bit_id = _kvm_pa_bit_id(kd, pa, page_size);
473ffdeef32SWill Andrews 	uint64_t pte_u64 = pte_bit_id / BITS_IN(*bitmap);
474ffdeef32SWill Andrews 	uint64_t popcount_id = pte_bit_id / POPCOUNT_BITS;
475ffdeef32SWill Andrews 	uint64_t pte_mask = 1ULL << (pte_bit_id % BITS_IN(*bitmap));
476ffdeef32SWill Andrews 	uint64_t bitN;
477ffdeef32SWill Andrews 	uint32_t count;
478197eca22SWill Andrews 
479ffdeef32SWill Andrews 	/* Check whether the page address requested is in the dump. */
48000e66147SD Scott Phillips 	if (pte_bit_id == _KVM_BIT_ID_INVALID ||
48100e66147SD Scott Phillips 	    pte_bit_id >= (kd->pt_map_size * NBBY) ||
482ffdeef32SWill Andrews 	    (bitmap[pte_u64] & pte_mask) == 0)
483ffdeef32SWill Andrews 		return (-1);
484ffdeef32SWill Andrews 
485ffdeef32SWill Andrews 	/*
486ffdeef32SWill Andrews 	 * Add/sub popcounts from the bitmap until the PTE's bit is reached.
487ffdeef32SWill Andrews 	 * For bits that are in the upper half between the calculated
488ffdeef32SWill Andrews 	 * popcount id and the next one, use the next one and subtract to
489ffdeef32SWill Andrews 	 * minimize the number of popcounts required.
490ffdeef32SWill Andrews 	 */
491ffdeef32SWill Andrews 	if ((pte_bit_id % POPCOUNT_BITS) < (POPCOUNT_BITS / 2)) {
492ffdeef32SWill Andrews 		count = kd->pt_popcounts[popcount_id] + popcount_bytes(
493ffdeef32SWill Andrews 		    bitmap + popcount_id * POPCOUNTS_IN(*bitmap),
494ffdeef32SWill Andrews 		    0, pte_bit_id - popcount_id * POPCOUNT_BITS);
495ffdeef32SWill Andrews 	} else {
496ffdeef32SWill Andrews 		/*
497ffdeef32SWill Andrews 		 * Counting in reverse is trickier, since we must avoid
498ffdeef32SWill Andrews 		 * reading from bytes that are not in range, and invert.
499ffdeef32SWill Andrews 		 */
500ffdeef32SWill Andrews 		uint64_t pte_u64_bit_off = pte_u64 * BITS_IN(*bitmap);
501ffdeef32SWill Andrews 
502ffdeef32SWill Andrews 		popcount_id++;
503ffdeef32SWill Andrews 		bitN = MIN(popcount_id * POPCOUNT_BITS,
504ffdeef32SWill Andrews 		    kd->pt_map_size * BITS_IN(uint8_t));
505ffdeef32SWill Andrews 		count = kd->pt_popcounts[popcount_id] - popcount_bytes(
506ffdeef32SWill Andrews 		    bitmap + pte_u64,
507ffdeef32SWill Andrews 		    pte_bit_id - pte_u64_bit_off, bitN - pte_u64_bit_off);
508197eca22SWill Andrews 	}
509ffdeef32SWill Andrews 
510ffdeef32SWill Andrews 	/*
511ffdeef32SWill Andrews 	 * This can only happen if the core is truncated.  Treat these
512ffdeef32SWill Andrews 	 * entries as if they don't exist, since their backing doesn't.
513ffdeef32SWill Andrews 	 */
514c9057838SWill Andrews 	if (count >= (kd->pt_sparse_size / page_size))
515ffdeef32SWill Andrews 		return (-1);
516ffdeef32SWill Andrews 
517c9057838SWill Andrews 	return (kd->pt_sparse_off + (uint64_t)count * page_size);
518197eca22SWill Andrews }
519197eca22SWill Andrews 
520197eca22SWill Andrews static int
521197eca22SWill Andrews kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list)
522197eca22SWill Andrews {
523197eca22SWill Andrews 	kvaddr_t addr;
524197eca22SWill Andrews 	int error, nfail;
525197eca22SWill Andrews 
526197eca22SWill Andrews 	if (kd->resolve_symbol == NULL) {
527197eca22SWill Andrews 		struct nlist *nl;
528197eca22SWill Andrews 		int count, i;
529197eca22SWill Andrews 
530197eca22SWill Andrews 		for (count = 0; list[count].n_name != NULL &&
531197eca22SWill Andrews 		     list[count].n_name[0] != '\0'; count++)
532197eca22SWill Andrews 			;
533197eca22SWill Andrews 		nl = calloc(count + 1, sizeof(*nl));
534197eca22SWill Andrews 		for (i = 0; i < count; i++)
535197eca22SWill Andrews 			nl[i].n_name = list[i].n_name;
536197eca22SWill Andrews 		nfail = __fdnlist(kd->nlfd, nl);
537197eca22SWill Andrews 		for (i = 0; i < count; i++) {
538197eca22SWill Andrews 			list[i].n_type = nl[i].n_type;
539197eca22SWill Andrews 			list[i].n_value = nl[i].n_value;
540197eca22SWill Andrews 		}
541197eca22SWill Andrews 		free(nl);
542197eca22SWill Andrews 		return (nfail);
543197eca22SWill Andrews 	}
544197eca22SWill Andrews 
545197eca22SWill Andrews 	nfail = 0;
546197eca22SWill Andrews 	while (list->n_name != NULL && list->n_name[0] != '\0') {
547197eca22SWill Andrews 		error = kd->resolve_symbol(list->n_name, &addr);
548197eca22SWill Andrews 		if (error != 0) {
549197eca22SWill Andrews 			nfail++;
550197eca22SWill Andrews 			list->n_value = 0;
551197eca22SWill Andrews 			list->n_type = 0;
552197eca22SWill Andrews 		} else {
553197eca22SWill Andrews 			list->n_value = addr;
554197eca22SWill Andrews 			list->n_type = N_DATA | N_EXT;
555197eca22SWill Andrews 		}
556197eca22SWill Andrews 		list++;
557197eca22SWill Andrews 	}
558197eca22SWill Andrews 	return (nfail);
559197eca22SWill Andrews }
560197eca22SWill Andrews 
561197eca22SWill Andrews /*
562197eca22SWill Andrews  * Walk the list of unresolved symbols, generate a new list and prefix the
563197eca22SWill Andrews  * symbol names, try again, and merge back what we could resolve.
564197eca22SWill Andrews  */
565197eca22SWill Andrews static int
566197eca22SWill Andrews kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing,
567197eca22SWill Andrews     const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t))
568197eca22SWill Andrews {
569197eca22SWill Andrews 	struct kvm_nlist *n, *np, *p;
570197eca22SWill Andrews 	char *cp, *ce;
571197eca22SWill Andrews 	const char *ccp;
572197eca22SWill Andrews 	size_t len;
573197eca22SWill Andrews 	int slen, unresolved;
574197eca22SWill Andrews 
575197eca22SWill Andrews 	/*
576197eca22SWill Andrews 	 * Calculate the space we need to malloc for nlist and names.
577197eca22SWill Andrews 	 * We are going to store the name twice for later lookups: once
578197eca22SWill Andrews 	 * with the prefix and once the unmodified name delmited by \0.
579197eca22SWill Andrews 	 */
580197eca22SWill Andrews 	len = 0;
581197eca22SWill Andrews 	unresolved = 0;
582197eca22SWill Andrews 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
583197eca22SWill Andrews 		if (p->n_type != N_UNDF)
584197eca22SWill Andrews 			continue;
585197eca22SWill Andrews 		len += sizeof(struct kvm_nlist) + strlen(prefix) +
586197eca22SWill Andrews 		    2 * (strlen(p->n_name) + 1);
587197eca22SWill Andrews 		unresolved++;
588197eca22SWill Andrews 	}
589197eca22SWill Andrews 	if (unresolved == 0)
590197eca22SWill Andrews 		return (unresolved);
591197eca22SWill Andrews 	/* Add space for the terminating nlist entry. */
592197eca22SWill Andrews 	len += sizeof(struct kvm_nlist);
593197eca22SWill Andrews 	unresolved++;
594197eca22SWill Andrews 
595197eca22SWill Andrews 	/* Alloc one chunk for (nlist, [names]) and setup pointers. */
596197eca22SWill Andrews 	n = np = malloc(len);
597197eca22SWill Andrews 	bzero(n, len);
598197eca22SWill Andrews 	if (n == NULL)
599197eca22SWill Andrews 		return (missing);
600197eca22SWill Andrews 	cp = ce = (char *)np;
601197eca22SWill Andrews 	cp += unresolved * sizeof(struct kvm_nlist);
602197eca22SWill Andrews 	ce += len;
603197eca22SWill Andrews 
604197eca22SWill Andrews 	/* Generate shortened nlist with special prefix. */
605197eca22SWill Andrews 	unresolved = 0;
606197eca22SWill Andrews 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
607197eca22SWill Andrews 		if (p->n_type != N_UNDF)
608197eca22SWill Andrews 			continue;
609197eca22SWill Andrews 		*np = *p;
610197eca22SWill Andrews 		/* Save the new\0orig. name so we can later match it again. */
611197eca22SWill Andrews 		slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
612197eca22SWill Andrews 		    (prefix[0] != '\0' && p->n_name[0] == '_') ?
613197eca22SWill Andrews 			(p->n_name + 1) : p->n_name, '\0', p->n_name);
614197eca22SWill Andrews 		if (slen < 0 || slen >= ce - cp)
615197eca22SWill Andrews 			continue;
616197eca22SWill Andrews 		np->n_name = cp;
617197eca22SWill Andrews 		cp += slen + 1;
618197eca22SWill Andrews 		np++;
619197eca22SWill Andrews 		unresolved++;
620197eca22SWill Andrews 	}
621197eca22SWill Andrews 
622197eca22SWill Andrews 	/* Do lookup on the reduced list. */
623197eca22SWill Andrews 	np = n;
624197eca22SWill Andrews 	unresolved = kvm_fdnlist(kd, np);
625197eca22SWill Andrews 
626197eca22SWill Andrews 	/* Check if we could resolve further symbols and update the list. */
627197eca22SWill Andrews 	if (unresolved >= 0 && unresolved < missing) {
628197eca22SWill Andrews 		/* Find the first freshly resolved entry. */
629197eca22SWill Andrews 		for (; np->n_name && np->n_name[0]; np++)
630197eca22SWill Andrews 			if (np->n_type != N_UNDF)
631197eca22SWill Andrews 				break;
632197eca22SWill Andrews 		/*
633197eca22SWill Andrews 		 * The lists are both in the same order,
634197eca22SWill Andrews 		 * so we can walk them in parallel.
635197eca22SWill Andrews 		 */
636197eca22SWill Andrews 		for (p = nl; np->n_name && np->n_name[0] &&
637197eca22SWill Andrews 		    p->n_name && p->n_name[0]; ++p) {
638197eca22SWill Andrews 			if (p->n_type != N_UNDF)
639197eca22SWill Andrews 				continue;
640197eca22SWill Andrews 			/* Skip expanded name and compare to orig. one. */
641197eca22SWill Andrews 			ccp = np->n_name + strlen(np->n_name) + 1;
642197eca22SWill Andrews 			if (strcmp(ccp, p->n_name) != 0)
643197eca22SWill Andrews 				continue;
644197eca22SWill Andrews 			/* Update nlist with new, translated results. */
645197eca22SWill Andrews 			p->n_type = np->n_type;
646197eca22SWill Andrews 			if (validate_fn)
647197eca22SWill Andrews 				p->n_value = (*validate_fn)(kd, np->n_value);
648197eca22SWill Andrews 			else
649197eca22SWill Andrews 				p->n_value = np->n_value;
650197eca22SWill Andrews 			missing--;
651197eca22SWill Andrews 			/* Find next freshly resolved entry. */
652197eca22SWill Andrews 			for (np++; np->n_name && np->n_name[0]; np++)
653197eca22SWill Andrews 				if (np->n_type != N_UNDF)
654197eca22SWill Andrews 					break;
655197eca22SWill Andrews 		}
656197eca22SWill Andrews 	}
657197eca22SWill Andrews 	/* We could assert missing = unresolved here. */
658197eca22SWill Andrews 
659197eca22SWill Andrews 	free(n);
660197eca22SWill Andrews 	return (unresolved);
661197eca22SWill Andrews }
662197eca22SWill Andrews 
663197eca22SWill Andrews int
664197eca22SWill Andrews _kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize)
665197eca22SWill Andrews {
666197eca22SWill Andrews 	struct kvm_nlist *p;
667197eca22SWill Andrews 	int nvalid;
668197eca22SWill Andrews 	struct kld_sym_lookup lookup;
669197eca22SWill Andrews 	int error;
670197eca22SWill Andrews 	const char *prefix = "";
671197eca22SWill Andrews 	char symname[1024]; /* XXX-BZ symbol name length limit? */
672197eca22SWill Andrews 	int tried_vnet, tried_dpcpu;
673197eca22SWill Andrews 
674197eca22SWill Andrews 	/*
675197eca22SWill Andrews 	 * If we can't use the kld symbol lookup, revert to the
676197eca22SWill Andrews 	 * slow library call.
677197eca22SWill Andrews 	 */
678197eca22SWill Andrews 	if (!ISALIVE(kd)) {
679197eca22SWill Andrews 		error = kvm_fdnlist(kd, nl);
680197eca22SWill Andrews 		if (error <= 0)			/* Hard error or success. */
681197eca22SWill Andrews 			return (error);
682197eca22SWill Andrews 
683197eca22SWill Andrews 		if (_kvm_vnet_initialized(kd, initialize))
684197eca22SWill Andrews 			error = kvm_fdnlist_prefix(kd, nl, error,
685197eca22SWill Andrews 			    VNET_SYMPREFIX, _kvm_vnet_validaddr);
686197eca22SWill Andrews 
687197eca22SWill Andrews 		if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
688197eca22SWill Andrews 			error = kvm_fdnlist_prefix(kd, nl, error,
689197eca22SWill Andrews 			    DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
690197eca22SWill Andrews 
691197eca22SWill Andrews 		return (error);
692197eca22SWill Andrews 	}
693197eca22SWill Andrews 
694197eca22SWill Andrews 	/*
695197eca22SWill Andrews 	 * We can use the kld lookup syscall.  Go through each nlist entry
696197eca22SWill Andrews 	 * and look it up with a kldsym(2) syscall.
697197eca22SWill Andrews 	 */
698197eca22SWill Andrews 	nvalid = 0;
699197eca22SWill Andrews 	tried_vnet = 0;
700197eca22SWill Andrews 	tried_dpcpu = 0;
701197eca22SWill Andrews again:
702197eca22SWill Andrews 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
703197eca22SWill Andrews 		if (p->n_type != N_UNDF)
704197eca22SWill Andrews 			continue;
705197eca22SWill Andrews 
706197eca22SWill Andrews 		lookup.version = sizeof(lookup);
707197eca22SWill Andrews 		lookup.symvalue = 0;
708197eca22SWill Andrews 		lookup.symsize = 0;
709197eca22SWill Andrews 
710197eca22SWill Andrews 		error = snprintf(symname, sizeof(symname), "%s%s", prefix,
711197eca22SWill Andrews 		    (prefix[0] != '\0' && p->n_name[0] == '_') ?
712197eca22SWill Andrews 			(p->n_name + 1) : p->n_name);
713197eca22SWill Andrews 		if (error < 0 || error >= (int)sizeof(symname))
714197eca22SWill Andrews 			continue;
715197eca22SWill Andrews 		lookup.symname = symname;
716197eca22SWill Andrews 		if (lookup.symname[0] == '_')
717197eca22SWill Andrews 			lookup.symname++;
718197eca22SWill Andrews 
719197eca22SWill Andrews 		if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
720197eca22SWill Andrews 			p->n_type = N_TEXT;
721197eca22SWill Andrews 			if (_kvm_vnet_initialized(kd, initialize) &&
722197eca22SWill Andrews 			    strcmp(prefix, VNET_SYMPREFIX) == 0)
723197eca22SWill Andrews 				p->n_value =
724197eca22SWill Andrews 				    _kvm_vnet_validaddr(kd, lookup.symvalue);
725197eca22SWill Andrews 			else if (_kvm_dpcpu_initialized(kd, initialize) &&
726197eca22SWill Andrews 			    strcmp(prefix, DPCPU_SYMPREFIX) == 0)
727197eca22SWill Andrews 				p->n_value =
728197eca22SWill Andrews 				    _kvm_dpcpu_validaddr(kd, lookup.symvalue);
729197eca22SWill Andrews 			else
730197eca22SWill Andrews 				p->n_value = lookup.symvalue;
731197eca22SWill Andrews 			++nvalid;
732197eca22SWill Andrews 			/* lookup.symsize */
733197eca22SWill Andrews 		}
734197eca22SWill Andrews 	}
735197eca22SWill Andrews 
736197eca22SWill Andrews 	/*
737197eca22SWill Andrews 	 * Check the number of entries that weren't found. If they exist,
738197eca22SWill Andrews 	 * try again with a prefix for virtualized or DPCPU symbol names.
739197eca22SWill Andrews 	 */
740197eca22SWill Andrews 	error = ((p - nl) - nvalid);
741197eca22SWill Andrews 	if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
742197eca22SWill Andrews 		tried_vnet = 1;
743197eca22SWill Andrews 		prefix = VNET_SYMPREFIX;
744197eca22SWill Andrews 		goto again;
745197eca22SWill Andrews 	}
746197eca22SWill Andrews 	if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
747197eca22SWill Andrews 		tried_dpcpu = 1;
748197eca22SWill Andrews 		prefix = DPCPU_SYMPREFIX;
749197eca22SWill Andrews 		goto again;
750197eca22SWill Andrews 	}
751197eca22SWill Andrews 
752197eca22SWill Andrews 	/*
753197eca22SWill Andrews 	 * Return the number of entries that weren't found. If they exist,
754197eca22SWill Andrews 	 * also fill internal error buffer.
755197eca22SWill Andrews 	 */
756197eca22SWill Andrews 	error = ((p - nl) - nvalid);
757197eca22SWill Andrews 	if (error)
758197eca22SWill Andrews 		_kvm_syserr(kd, kd->program, "kvm_nlist");
759197eca22SWill Andrews 	return (error);
760197eca22SWill Andrews }
761c9057838SWill Andrews 
762c9057838SWill Andrews int
7632aa6a4f3SWill Andrews _kvm_bitmap_init(struct kvm_bitmap *bm, u_long bitmapsize, u_long *idx)
764c9057838SWill Andrews {
765c9057838SWill Andrews 
7662aa6a4f3SWill Andrews 	*idx = ULONG_MAX;
767c9057838SWill Andrews 	bm->map = calloc(bitmapsize, sizeof *bm->map);
768c9057838SWill Andrews 	if (bm->map == NULL)
769c9057838SWill Andrews 		return (0);
770c9057838SWill Andrews 	bm->size = bitmapsize;
771c9057838SWill Andrews 	return (1);
772c9057838SWill Andrews }
773c9057838SWill Andrews 
774c9057838SWill Andrews void
77500e66147SD Scott Phillips _kvm_bitmap_set(struct kvm_bitmap *bm, u_long bm_index)
776c9057838SWill Andrews {
777c9057838SWill Andrews 	uint8_t *byte = &bm->map[bm_index / 8];
778c9057838SWill Andrews 
77900e66147SD Scott Phillips 	if (bm_index / 8 < bm->size)
780c9057838SWill Andrews 		*byte |= (1UL << (bm_index % 8));
781c9057838SWill Andrews }
782c9057838SWill Andrews 
783c9057838SWill Andrews int
7842aa6a4f3SWill Andrews _kvm_bitmap_next(struct kvm_bitmap *bm, u_long *idx)
785c9057838SWill Andrews {
786c9057838SWill Andrews 	u_long first_invalid = bm->size * CHAR_BIT;
787c9057838SWill Andrews 
7882aa6a4f3SWill Andrews 	if (*idx == ULONG_MAX)
7892aa6a4f3SWill Andrews 		*idx = 0;
790c9057838SWill Andrews 	else
7912aa6a4f3SWill Andrews 		(*idx)++;
792c9057838SWill Andrews 
7932aa6a4f3SWill Andrews 	/* Find the next valid idx. */
7942aa6a4f3SWill Andrews 	for (; *idx < first_invalid; (*idx)++) {
795*08055452SBora Özarslan 		unsigned int mask = 1U << (*idx % CHAR_BIT);
796*08055452SBora Özarslan 		if ((bm->map[*idx / CHAR_BIT] & mask) != 0)
797c9057838SWill Andrews 			break;
798c9057838SWill Andrews 	}
799c9057838SWill Andrews 
8002aa6a4f3SWill Andrews 	return (*idx < first_invalid);
801c9057838SWill Andrews }
802c9057838SWill Andrews 
803c9057838SWill Andrews void
804c9057838SWill Andrews _kvm_bitmap_deinit(struct kvm_bitmap *bm)
805c9057838SWill Andrews {
806c9057838SWill Andrews 
807c9057838SWill Andrews 	free(bm->map);
808c9057838SWill Andrews }
809c9057838SWill Andrews 
810c9057838SWill Andrews int
811c9057838SWill Andrews _kvm_visit_cb(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg, u_long pa,
812c9057838SWill Andrews     u_long kmap_vaddr, u_long dmap_vaddr, vm_prot_t prot, size_t len,
813c9057838SWill Andrews     unsigned int page_size)
814c9057838SWill Andrews {
815c9057838SWill Andrews 	unsigned int pgsz = page_size ? page_size : len;
816c9057838SWill Andrews 	struct kvm_page p = {
8179dc7ed62SEd Maste 		.kp_version = LIBKVM_WALK_PAGES_VERSION,
8189dc7ed62SEd Maste 		.kp_paddr = pa,
8199dc7ed62SEd Maste 		.kp_kmap_vaddr = kmap_vaddr,
8209dc7ed62SEd Maste 		.kp_dmap_vaddr = dmap_vaddr,
8219dc7ed62SEd Maste 		.kp_prot = prot,
8229dc7ed62SEd Maste 		.kp_offset = _kvm_pt_find(kd, pa, pgsz),
8239dc7ed62SEd Maste 		.kp_len = len,
824c9057838SWill Andrews 	};
825c9057838SWill Andrews 
826c9057838SWill Andrews 	return cb(&p, arg);
827c9057838SWill Andrews }
828