xref: /freebsd/lib/libkvm/kvm_private.c (revision ffdeef3234496b79624f4ba34e3d0a6893f41404)
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.
17197eca22SWill Andrews  * 4. 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>
46197eca22SWill Andrews 
47197eca22SWill Andrews #include <net/vnet.h>
48197eca22SWill Andrews 
49*ffdeef32SWill Andrews #include <assert.h>
50197eca22SWill Andrews #include <fcntl.h>
51197eca22SWill Andrews #include <kvm.h>
52197eca22SWill Andrews #include <limits.h>
53197eca22SWill Andrews #include <paths.h>
54197eca22SWill Andrews #include <stdint.h>
55197eca22SWill Andrews #include <stdio.h>
56197eca22SWill Andrews #include <stdlib.h>
57197eca22SWill Andrews #include <string.h>
58197eca22SWill Andrews #include <unistd.h>
59197eca22SWill Andrews #include <stdarg.h>
60197eca22SWill Andrews 
61197eca22SWill Andrews #include "kvm_private.h"
62197eca22SWill Andrews 
63197eca22SWill Andrews /*
64197eca22SWill Andrews  * Routines private to libkvm.
65197eca22SWill Andrews  */
66197eca22SWill Andrews 
67197eca22SWill Andrews /* from src/lib/libc/gen/nlist.c */
68197eca22SWill Andrews int __fdnlist(int, struct nlist *);
69197eca22SWill Andrews 
70197eca22SWill Andrews /*
71197eca22SWill Andrews  * Report an error using printf style arguments.  "program" is kd->program
72197eca22SWill Andrews  * on hard errors, and 0 on soft errors, so that under sun error emulation,
73197eca22SWill Andrews  * only hard errors are printed out (otherwise, programs like gdb will
74197eca22SWill Andrews  * generate tons of error messages when trying to access bogus pointers).
75197eca22SWill Andrews  */
76197eca22SWill Andrews void
77197eca22SWill Andrews _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
78197eca22SWill Andrews {
79197eca22SWill Andrews 	va_list ap;
80197eca22SWill Andrews 
81197eca22SWill Andrews 	va_start(ap, fmt);
82197eca22SWill Andrews 	if (program != NULL) {
83197eca22SWill Andrews 		(void)fprintf(stderr, "%s: ", program);
84197eca22SWill Andrews 		(void)vfprintf(stderr, fmt, ap);
85197eca22SWill Andrews 		(void)fputc('\n', stderr);
86197eca22SWill Andrews 	} else
87197eca22SWill Andrews 		(void)vsnprintf(kd->errbuf,
88197eca22SWill Andrews 		    sizeof(kd->errbuf), fmt, ap);
89197eca22SWill Andrews 
90197eca22SWill Andrews 	va_end(ap);
91197eca22SWill Andrews }
92197eca22SWill Andrews 
93197eca22SWill Andrews void
94197eca22SWill Andrews _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
95197eca22SWill Andrews {
96197eca22SWill Andrews 	va_list ap;
97197eca22SWill Andrews 	int n;
98197eca22SWill Andrews 
99197eca22SWill Andrews 	va_start(ap, fmt);
100197eca22SWill Andrews 	if (program != NULL) {
101197eca22SWill Andrews 		(void)fprintf(stderr, "%s: ", program);
102197eca22SWill Andrews 		(void)vfprintf(stderr, fmt, ap);
103197eca22SWill Andrews 		(void)fprintf(stderr, ": %s\n", strerror(errno));
104197eca22SWill Andrews 	} else {
105197eca22SWill Andrews 		char *cp = kd->errbuf;
106197eca22SWill Andrews 
107197eca22SWill Andrews 		(void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
108197eca22SWill Andrews 		n = strlen(cp);
109197eca22SWill Andrews 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
110197eca22SWill Andrews 		    strerror(errno));
111197eca22SWill Andrews 	}
112197eca22SWill Andrews 	va_end(ap);
113197eca22SWill Andrews }
114197eca22SWill Andrews 
115197eca22SWill Andrews void *
116197eca22SWill Andrews _kvm_malloc(kvm_t *kd, size_t n)
117197eca22SWill Andrews {
118197eca22SWill Andrews 	void *p;
119197eca22SWill Andrews 
120197eca22SWill Andrews 	if ((p = calloc(n, sizeof(char))) == NULL)
121197eca22SWill Andrews 		_kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
122197eca22SWill Andrews 			 n, strerror(errno));
123197eca22SWill Andrews 	return (p);
124197eca22SWill Andrews }
125197eca22SWill Andrews 
126197eca22SWill Andrews int
127197eca22SWill Andrews _kvm_probe_elf_kernel(kvm_t *kd, int class, int machine)
128197eca22SWill Andrews {
129197eca22SWill Andrews 
130197eca22SWill Andrews 	return (kd->nlehdr.e_ident[EI_CLASS] == class &&
131197eca22SWill Andrews 	    kd->nlehdr.e_type == ET_EXEC &&
132197eca22SWill Andrews 	    kd->nlehdr.e_machine == machine);
133197eca22SWill Andrews }
134197eca22SWill Andrews 
135197eca22SWill Andrews int
136197eca22SWill Andrews _kvm_is_minidump(kvm_t *kd)
137197eca22SWill Andrews {
138197eca22SWill Andrews 	char minihdr[8];
139197eca22SWill Andrews 
140197eca22SWill Andrews 	if (kd->rawdump)
141197eca22SWill Andrews 		return (0);
142197eca22SWill Andrews 	if (pread(kd->pmfd, &minihdr, 8, 0) == 8 &&
143197eca22SWill Andrews 	    memcmp(&minihdr, "minidump", 8) == 0)
144197eca22SWill Andrews 		return (1);
145197eca22SWill Andrews 	return (0);
146197eca22SWill Andrews }
147197eca22SWill Andrews 
148197eca22SWill Andrews /*
149197eca22SWill Andrews  * The powerpc backend has a hack to strip a leading kerneldump
150197eca22SWill Andrews  * header from the core before treating it as an ELF header.
151197eca22SWill Andrews  *
152197eca22SWill Andrews  * We can add that here if we can get a change to libelf to support
153197eca22SWill Andrews  * an initial offset into the file.  Alternatively we could patch
154197eca22SWill Andrews  * savecore to extract cores from a regular file instead.
155197eca22SWill Andrews  */
156197eca22SWill Andrews int
157197eca22SWill Andrews _kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp)
158197eca22SWill Andrews {
159197eca22SWill Andrews 	GElf_Ehdr ehdr;
160197eca22SWill Andrews 	GElf_Phdr *phdr;
161197eca22SWill Andrews 	Elf *elf;
162197eca22SWill Andrews 	size_t i, phnum;
163197eca22SWill Andrews 
164197eca22SWill Andrews 	elf = elf_begin(kd->pmfd, ELF_C_READ, NULL);
165197eca22SWill Andrews 	if (elf == NULL) {
166197eca22SWill Andrews 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
167197eca22SWill Andrews 		return (-1);
168197eca22SWill Andrews 	}
169197eca22SWill Andrews 	if (elf_kind(elf) != ELF_K_ELF) {
170197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
171197eca22SWill Andrews 		goto bad;
172197eca22SWill Andrews 	}
173197eca22SWill Andrews 	if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) {
174197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
175197eca22SWill Andrews 		goto bad;
176197eca22SWill Andrews 	}
177197eca22SWill Andrews 	if (gelf_getehdr(elf, &ehdr) == NULL) {
178197eca22SWill Andrews 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
179197eca22SWill Andrews 		goto bad;
180197eca22SWill Andrews 	}
181197eca22SWill Andrews 	if (ehdr.e_type != ET_CORE) {
182197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
183197eca22SWill Andrews 		goto bad;
184197eca22SWill Andrews 	}
185197eca22SWill Andrews 	if (ehdr.e_machine != kd->nlehdr.e_machine) {
186197eca22SWill Andrews 		_kvm_err(kd, kd->program, "invalid core");
187197eca22SWill Andrews 		goto bad;
188197eca22SWill Andrews 	}
189197eca22SWill Andrews 
190197eca22SWill Andrews 	if (elf_getphdrnum(elf, &phnum) == -1) {
191197eca22SWill Andrews 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
192197eca22SWill Andrews 		goto bad;
193197eca22SWill Andrews 	}
194197eca22SWill Andrews 
195197eca22SWill Andrews 	phdr = calloc(phnum, sizeof(*phdr));
196197eca22SWill Andrews 	if (phdr == NULL) {
197197eca22SWill Andrews 		_kvm_err(kd, kd->program, "failed to allocate phdrs");
198197eca22SWill Andrews 		goto bad;
199197eca22SWill Andrews 	}
200197eca22SWill Andrews 
201197eca22SWill Andrews 	for (i = 0; i < phnum; i++) {
202197eca22SWill Andrews 		if (gelf_getphdr(elf, i, &phdr[i]) == NULL) {
203197eca22SWill Andrews 			_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
204197eca22SWill Andrews 			goto bad;
205197eca22SWill Andrews 		}
206197eca22SWill Andrews 	}
207197eca22SWill Andrews 	elf_end(elf);
208197eca22SWill Andrews 	*phnump = phnum;
209197eca22SWill Andrews 	*phdrp = phdr;
210197eca22SWill Andrews 	return (0);
211197eca22SWill Andrews 
212197eca22SWill Andrews bad:
213197eca22SWill Andrews 	elf_end(elf);
214197eca22SWill Andrews 	return (-1);
215197eca22SWill Andrews }
216197eca22SWill Andrews 
217*ffdeef32SWill Andrews /*
218*ffdeef32SWill Andrews  * Transform v such that only bits [bit0, bitN) may be set.  Generates a
219*ffdeef32SWill Andrews  * bitmask covering the number of bits, then shifts so +bit0+ is the first.
220*ffdeef32SWill Andrews  */
221*ffdeef32SWill Andrews static uint64_t
222*ffdeef32SWill Andrews bitmask_range(uint64_t v, uint64_t bit0, uint64_t bitN)
223197eca22SWill Andrews {
224*ffdeef32SWill Andrews 	if (bit0 == 0 && bitN == BITS_IN(v))
225*ffdeef32SWill Andrews 		return (v);
226197eca22SWill Andrews 
227*ffdeef32SWill Andrews 	return (v & (((1ULL << (bitN - bit0)) - 1ULL) << bit0));
228197eca22SWill Andrews }
229197eca22SWill Andrews 
230*ffdeef32SWill Andrews /*
231*ffdeef32SWill Andrews  * Returns the number of bits in a given byte array range starting at a
232*ffdeef32SWill Andrews  * given base, from bit0 to bitN.  bit0 may be non-zero in the case of
233*ffdeef32SWill Andrews  * counting backwards from bitN.
234*ffdeef32SWill Andrews  */
235*ffdeef32SWill Andrews static uint64_t
236*ffdeef32SWill Andrews popcount_bytes(uint64_t *addr, uint32_t bit0, uint32_t bitN)
237*ffdeef32SWill Andrews {
238*ffdeef32SWill Andrews 	uint32_t res = bitN - bit0;
239*ffdeef32SWill Andrews 	uint64_t count = 0;
240*ffdeef32SWill Andrews 	uint32_t bound;
241*ffdeef32SWill Andrews 
242*ffdeef32SWill Andrews 	/* Align to 64-bit boundary on the left side if needed. */
243*ffdeef32SWill Andrews 	if ((bit0 % BITS_IN(*addr)) != 0) {
244*ffdeef32SWill Andrews 		bound = MIN(bitN, roundup2(bit0, BITS_IN(*addr)));
245*ffdeef32SWill Andrews 		count += __bitcount64(bitmask_range(*addr, bit0, bound));
246*ffdeef32SWill Andrews 		res -= (bound - bit0);
247*ffdeef32SWill Andrews 		addr++;
248*ffdeef32SWill Andrews 	}
249*ffdeef32SWill Andrews 
250*ffdeef32SWill Andrews 	while (res > 0) {
251*ffdeef32SWill Andrews 		bound = MIN(res, BITS_IN(*addr));
252*ffdeef32SWill Andrews 		count += __bitcount64(bitmask_range(*addr, 0, bound));
253*ffdeef32SWill Andrews 		res -= bound;
254*ffdeef32SWill Andrews 		addr++;
255*ffdeef32SWill Andrews 	}
256*ffdeef32SWill Andrews 
257*ffdeef32SWill Andrews 	return (count);
258*ffdeef32SWill Andrews }
259*ffdeef32SWill Andrews 
260*ffdeef32SWill Andrews int
261*ffdeef32SWill Andrews _kvm_pt_init(kvm_t *kd, size_t map_len, off_t map_off, off_t sparse_off,
262197eca22SWill Andrews     int page_size, int word_size)
263197eca22SWill Andrews {
264*ffdeef32SWill Andrews 	uint64_t *addr;
265*ffdeef32SWill Andrews 	uint32_t *popcount_bin;
266*ffdeef32SWill Andrews 	int bin_popcounts = 0;
267*ffdeef32SWill Andrews 	uint64_t pc_bins, res;
268*ffdeef32SWill Andrews 	ssize_t rd;
269197eca22SWill Andrews 
270*ffdeef32SWill Andrews 	/*
271*ffdeef32SWill Andrews 	 * Map the bitmap specified by the arguments.
272*ffdeef32SWill Andrews 	 */
273*ffdeef32SWill Andrews 	kd->pt_map = _kvm_malloc(kd, map_len);
274*ffdeef32SWill Andrews 	if (kd->pt_map == NULL) {
275*ffdeef32SWill Andrews 		_kvm_err(kd, kd->program, "cannot allocate %zu bytes for bitmap",
276*ffdeef32SWill Andrews 		    map_len);
277197eca22SWill Andrews 		return (-1);
278197eca22SWill Andrews 	}
279*ffdeef32SWill Andrews 	rd = pread(kd->pmfd, kd->pt_map, map_len, map_off);
280*ffdeef32SWill Andrews 	if (rd < 0 || rd != (ssize_t)map_len) {
281*ffdeef32SWill Andrews 		_kvm_err(kd, kd->program, "cannot read %zu bytes for bitmap",
282*ffdeef32SWill Andrews 		    map_len);
283*ffdeef32SWill Andrews 		return (-1);
284*ffdeef32SWill Andrews 	}
285*ffdeef32SWill Andrews 	kd->pt_map_size = map_len;
286197eca22SWill Andrews 
287*ffdeef32SWill Andrews 	/*
288*ffdeef32SWill Andrews 	 * Generate a popcount cache for every POPCOUNT_BITS in the bitmap,
289*ffdeef32SWill Andrews 	 * so lookups only have to calculate the number of bits set between
290*ffdeef32SWill Andrews 	 * a cache point and their bit.  This reduces lookups to O(1),
291*ffdeef32SWill Andrews 	 * without significantly increasing memory requirements.
292*ffdeef32SWill Andrews 	 *
293*ffdeef32SWill Andrews 	 * Round up the number of bins so that 'upper half' lookups work for
294*ffdeef32SWill Andrews 	 * the final bin, if needed.  The first popcount is 0, since no bits
295*ffdeef32SWill Andrews 	 * precede bit 0, so add 1 for that also.  Without this, extra work
296*ffdeef32SWill Andrews 	 * would be needed to handle the first PTEs in _kvm_pt_find().
297*ffdeef32SWill Andrews 	 */
298*ffdeef32SWill Andrews 	addr = kd->pt_map;
299*ffdeef32SWill Andrews 	res = map_len;
300*ffdeef32SWill Andrews 	pc_bins = 1 + (res * NBBY + POPCOUNT_BITS / 2) / POPCOUNT_BITS;
301*ffdeef32SWill Andrews 	kd->pt_popcounts = calloc(pc_bins, sizeof(uint32_t));
302*ffdeef32SWill Andrews 	if (kd->pt_popcounts == NULL)
303*ffdeef32SWill Andrews 		return (-1);
304*ffdeef32SWill Andrews 
305*ffdeef32SWill Andrews 	for (popcount_bin = &kd->pt_popcounts[1]; res > 0;
306*ffdeef32SWill Andrews 	    addr++, res -= sizeof(*addr)) {
307*ffdeef32SWill Andrews 		*popcount_bin += popcount_bytes(addr, 0,
308*ffdeef32SWill Andrews 		    MIN(res * NBBY, BITS_IN(*addr)));
309*ffdeef32SWill Andrews 		if (++bin_popcounts == POPCOUNTS_IN(*addr)) {
310*ffdeef32SWill Andrews 			popcount_bin++;
311*ffdeef32SWill Andrews 			*popcount_bin = *(popcount_bin - 1);
312*ffdeef32SWill Andrews 			bin_popcounts = 0;
313*ffdeef32SWill Andrews 		}
314*ffdeef32SWill Andrews 	}
315*ffdeef32SWill Andrews 
316*ffdeef32SWill Andrews 	assert(pc_bins * sizeof(*popcount_bin) ==
317*ffdeef32SWill Andrews 	    ((uintptr_t)popcount_bin - (uintptr_t)kd->pt_popcounts));
318*ffdeef32SWill Andrews 
319*ffdeef32SWill Andrews 	kd->pt_sparse_off = sparse_off;
320*ffdeef32SWill Andrews 	kd->pt_sparse_size = (uint64_t)*popcount_bin * PAGE_SIZE;
321*ffdeef32SWill Andrews 	kd->pt_page_size = page_size;
322*ffdeef32SWill Andrews 	kd->pt_word_size = word_size;
323*ffdeef32SWill Andrews 	return (0);
324*ffdeef32SWill Andrews }
325*ffdeef32SWill Andrews 
326*ffdeef32SWill Andrews /*
327*ffdeef32SWill Andrews  * Find the offset for the given physical page address; returns -1 otherwise.
328*ffdeef32SWill Andrews  *
329*ffdeef32SWill Andrews  * A page's offset is represented by the sparse page base offset plus the
330*ffdeef32SWill Andrews  * number of bits set before its bit multiplied by PAGE_SIZE.  This means
331*ffdeef32SWill Andrews  * that if a page exists in the dump, it's necessary to know how many pages
332*ffdeef32SWill Andrews  * in the dump precede it.  Reduce this O(n) counting to O(1) by caching the
333*ffdeef32SWill Andrews  * number of bits set at POPCOUNT_BITS intervals.
334*ffdeef32SWill Andrews  *
335*ffdeef32SWill Andrews  * Then to find the number of pages before the requested address, simply
336*ffdeef32SWill Andrews  * index into the cache and count the number of bits set between that cache
337*ffdeef32SWill Andrews  * bin and the page's bit.  Halve the number of bytes that have to be
338*ffdeef32SWill Andrews  * checked by also counting down from the next higher bin if it's closer.
339*ffdeef32SWill Andrews  */
340*ffdeef32SWill Andrews off_t
341*ffdeef32SWill Andrews _kvm_pt_find(kvm_t *kd, uint64_t pa)
342197eca22SWill Andrews {
343*ffdeef32SWill Andrews 	uint64_t *bitmap = kd->pt_map;
344*ffdeef32SWill Andrews 	uint64_t pte_bit_id = pa / PAGE_SIZE;
345*ffdeef32SWill Andrews 	uint64_t pte_u64 = pte_bit_id / BITS_IN(*bitmap);
346*ffdeef32SWill Andrews 	uint64_t popcount_id = pte_bit_id / POPCOUNT_BITS;
347*ffdeef32SWill Andrews 	uint64_t pte_mask = 1ULL << (pte_bit_id % BITS_IN(*bitmap));
348*ffdeef32SWill Andrews 	uint64_t bitN;
349*ffdeef32SWill Andrews 	uint32_t count;
350197eca22SWill Andrews 
351*ffdeef32SWill Andrews 	/* Check whether the page address requested is in the dump. */
352*ffdeef32SWill Andrews 	if (pte_bit_id >= (kd->pt_map_size * NBBY) ||
353*ffdeef32SWill Andrews 	    (bitmap[pte_u64] & pte_mask) == 0)
354*ffdeef32SWill Andrews 		return (-1);
355*ffdeef32SWill Andrews 
356*ffdeef32SWill Andrews 	/*
357*ffdeef32SWill Andrews 	 * Add/sub popcounts from the bitmap until the PTE's bit is reached.
358*ffdeef32SWill Andrews 	 * For bits that are in the upper half between the calculated
359*ffdeef32SWill Andrews 	 * popcount id and the next one, use the next one and subtract to
360*ffdeef32SWill Andrews 	 * minimize the number of popcounts required.
361*ffdeef32SWill Andrews 	 */
362*ffdeef32SWill Andrews 	if ((pte_bit_id % POPCOUNT_BITS) < (POPCOUNT_BITS / 2)) {
363*ffdeef32SWill Andrews 		count = kd->pt_popcounts[popcount_id] + popcount_bytes(
364*ffdeef32SWill Andrews 		    bitmap + popcount_id * POPCOUNTS_IN(*bitmap),
365*ffdeef32SWill Andrews 		    0, pte_bit_id - popcount_id * POPCOUNT_BITS);
366*ffdeef32SWill Andrews 	} else {
367*ffdeef32SWill Andrews 		/*
368*ffdeef32SWill Andrews 		 * Counting in reverse is trickier, since we must avoid
369*ffdeef32SWill Andrews 		 * reading from bytes that are not in range, and invert.
370*ffdeef32SWill Andrews 		 */
371*ffdeef32SWill Andrews 		uint64_t pte_u64_bit_off = pte_u64 * BITS_IN(*bitmap);
372*ffdeef32SWill Andrews 
373*ffdeef32SWill Andrews 		popcount_id++;
374*ffdeef32SWill Andrews 		bitN = MIN(popcount_id * POPCOUNT_BITS,
375*ffdeef32SWill Andrews 		    kd->pt_map_size * BITS_IN(uint8_t));
376*ffdeef32SWill Andrews 		count = kd->pt_popcounts[popcount_id] - popcount_bytes(
377*ffdeef32SWill Andrews 		    bitmap + pte_u64,
378*ffdeef32SWill Andrews 		    pte_bit_id - pte_u64_bit_off, bitN - pte_u64_bit_off);
379197eca22SWill Andrews 	}
380*ffdeef32SWill Andrews 
381*ffdeef32SWill Andrews 	/*
382*ffdeef32SWill Andrews 	 * This can only happen if the core is truncated.  Treat these
383*ffdeef32SWill Andrews 	 * entries as if they don't exist, since their backing doesn't.
384*ffdeef32SWill Andrews 	 */
385*ffdeef32SWill Andrews 	if (count >= (kd->pt_sparse_size / PAGE_SIZE))
386*ffdeef32SWill Andrews 		return (-1);
387*ffdeef32SWill Andrews 
388*ffdeef32SWill Andrews 	return (kd->pt_sparse_off + (uint64_t)count * PAGE_SIZE);
389197eca22SWill Andrews }
390197eca22SWill Andrews 
391197eca22SWill Andrews static int
392197eca22SWill Andrews kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list)
393197eca22SWill Andrews {
394197eca22SWill Andrews 	kvaddr_t addr;
395197eca22SWill Andrews 	int error, nfail;
396197eca22SWill Andrews 
397197eca22SWill Andrews 	if (kd->resolve_symbol == NULL) {
398197eca22SWill Andrews 		struct nlist *nl;
399197eca22SWill Andrews 		int count, i;
400197eca22SWill Andrews 
401197eca22SWill Andrews 		for (count = 0; list[count].n_name != NULL &&
402197eca22SWill Andrews 		     list[count].n_name[0] != '\0'; count++)
403197eca22SWill Andrews 			;
404197eca22SWill Andrews 		nl = calloc(count + 1, sizeof(*nl));
405197eca22SWill Andrews 		for (i = 0; i < count; i++)
406197eca22SWill Andrews 			nl[i].n_name = list[i].n_name;
407197eca22SWill Andrews 		nfail = __fdnlist(kd->nlfd, nl);
408197eca22SWill Andrews 		for (i = 0; i < count; i++) {
409197eca22SWill Andrews 			list[i].n_type = nl[i].n_type;
410197eca22SWill Andrews 			list[i].n_value = nl[i].n_value;
411197eca22SWill Andrews 		}
412197eca22SWill Andrews 		free(nl);
413197eca22SWill Andrews 		return (nfail);
414197eca22SWill Andrews 	}
415197eca22SWill Andrews 
416197eca22SWill Andrews 	nfail = 0;
417197eca22SWill Andrews 	while (list->n_name != NULL && list->n_name[0] != '\0') {
418197eca22SWill Andrews 		error = kd->resolve_symbol(list->n_name, &addr);
419197eca22SWill Andrews 		if (error != 0) {
420197eca22SWill Andrews 			nfail++;
421197eca22SWill Andrews 			list->n_value = 0;
422197eca22SWill Andrews 			list->n_type = 0;
423197eca22SWill Andrews 		} else {
424197eca22SWill Andrews 			list->n_value = addr;
425197eca22SWill Andrews 			list->n_type = N_DATA | N_EXT;
426197eca22SWill Andrews 		}
427197eca22SWill Andrews 		list++;
428197eca22SWill Andrews 	}
429197eca22SWill Andrews 	return (nfail);
430197eca22SWill Andrews }
431197eca22SWill Andrews 
432197eca22SWill Andrews /*
433197eca22SWill Andrews  * Walk the list of unresolved symbols, generate a new list and prefix the
434197eca22SWill Andrews  * symbol names, try again, and merge back what we could resolve.
435197eca22SWill Andrews  */
436197eca22SWill Andrews static int
437197eca22SWill Andrews kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing,
438197eca22SWill Andrews     const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t))
439197eca22SWill Andrews {
440197eca22SWill Andrews 	struct kvm_nlist *n, *np, *p;
441197eca22SWill Andrews 	char *cp, *ce;
442197eca22SWill Andrews 	const char *ccp;
443197eca22SWill Andrews 	size_t len;
444197eca22SWill Andrews 	int slen, unresolved;
445197eca22SWill Andrews 
446197eca22SWill Andrews 	/*
447197eca22SWill Andrews 	 * Calculate the space we need to malloc for nlist and names.
448197eca22SWill Andrews 	 * We are going to store the name twice for later lookups: once
449197eca22SWill Andrews 	 * with the prefix and once the unmodified name delmited by \0.
450197eca22SWill Andrews 	 */
451197eca22SWill Andrews 	len = 0;
452197eca22SWill Andrews 	unresolved = 0;
453197eca22SWill Andrews 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
454197eca22SWill Andrews 		if (p->n_type != N_UNDF)
455197eca22SWill Andrews 			continue;
456197eca22SWill Andrews 		len += sizeof(struct kvm_nlist) + strlen(prefix) +
457197eca22SWill Andrews 		    2 * (strlen(p->n_name) + 1);
458197eca22SWill Andrews 		unresolved++;
459197eca22SWill Andrews 	}
460197eca22SWill Andrews 	if (unresolved == 0)
461197eca22SWill Andrews 		return (unresolved);
462197eca22SWill Andrews 	/* Add space for the terminating nlist entry. */
463197eca22SWill Andrews 	len += sizeof(struct kvm_nlist);
464197eca22SWill Andrews 	unresolved++;
465197eca22SWill Andrews 
466197eca22SWill Andrews 	/* Alloc one chunk for (nlist, [names]) and setup pointers. */
467197eca22SWill Andrews 	n = np = malloc(len);
468197eca22SWill Andrews 	bzero(n, len);
469197eca22SWill Andrews 	if (n == NULL)
470197eca22SWill Andrews 		return (missing);
471197eca22SWill Andrews 	cp = ce = (char *)np;
472197eca22SWill Andrews 	cp += unresolved * sizeof(struct kvm_nlist);
473197eca22SWill Andrews 	ce += len;
474197eca22SWill Andrews 
475197eca22SWill Andrews 	/* Generate shortened nlist with special prefix. */
476197eca22SWill Andrews 	unresolved = 0;
477197eca22SWill Andrews 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
478197eca22SWill Andrews 		if (p->n_type != N_UNDF)
479197eca22SWill Andrews 			continue;
480197eca22SWill Andrews 		*np = *p;
481197eca22SWill Andrews 		/* Save the new\0orig. name so we can later match it again. */
482197eca22SWill Andrews 		slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
483197eca22SWill Andrews 		    (prefix[0] != '\0' && p->n_name[0] == '_') ?
484197eca22SWill Andrews 			(p->n_name + 1) : p->n_name, '\0', p->n_name);
485197eca22SWill Andrews 		if (slen < 0 || slen >= ce - cp)
486197eca22SWill Andrews 			continue;
487197eca22SWill Andrews 		np->n_name = cp;
488197eca22SWill Andrews 		cp += slen + 1;
489197eca22SWill Andrews 		np++;
490197eca22SWill Andrews 		unresolved++;
491197eca22SWill Andrews 	}
492197eca22SWill Andrews 
493197eca22SWill Andrews 	/* Do lookup on the reduced list. */
494197eca22SWill Andrews 	np = n;
495197eca22SWill Andrews 	unresolved = kvm_fdnlist(kd, np);
496197eca22SWill Andrews 
497197eca22SWill Andrews 	/* Check if we could resolve further symbols and update the list. */
498197eca22SWill Andrews 	if (unresolved >= 0 && unresolved < missing) {
499197eca22SWill Andrews 		/* Find the first freshly resolved entry. */
500197eca22SWill Andrews 		for (; np->n_name && np->n_name[0]; np++)
501197eca22SWill Andrews 			if (np->n_type != N_UNDF)
502197eca22SWill Andrews 				break;
503197eca22SWill Andrews 		/*
504197eca22SWill Andrews 		 * The lists are both in the same order,
505197eca22SWill Andrews 		 * so we can walk them in parallel.
506197eca22SWill Andrews 		 */
507197eca22SWill Andrews 		for (p = nl; np->n_name && np->n_name[0] &&
508197eca22SWill Andrews 		    p->n_name && p->n_name[0]; ++p) {
509197eca22SWill Andrews 			if (p->n_type != N_UNDF)
510197eca22SWill Andrews 				continue;
511197eca22SWill Andrews 			/* Skip expanded name and compare to orig. one. */
512197eca22SWill Andrews 			ccp = np->n_name + strlen(np->n_name) + 1;
513197eca22SWill Andrews 			if (strcmp(ccp, p->n_name) != 0)
514197eca22SWill Andrews 				continue;
515197eca22SWill Andrews 			/* Update nlist with new, translated results. */
516197eca22SWill Andrews 			p->n_type = np->n_type;
517197eca22SWill Andrews 			if (validate_fn)
518197eca22SWill Andrews 				p->n_value = (*validate_fn)(kd, np->n_value);
519197eca22SWill Andrews 			else
520197eca22SWill Andrews 				p->n_value = np->n_value;
521197eca22SWill Andrews 			missing--;
522197eca22SWill Andrews 			/* Find next freshly resolved entry. */
523197eca22SWill Andrews 			for (np++; np->n_name && np->n_name[0]; np++)
524197eca22SWill Andrews 				if (np->n_type != N_UNDF)
525197eca22SWill Andrews 					break;
526197eca22SWill Andrews 		}
527197eca22SWill Andrews 	}
528197eca22SWill Andrews 	/* We could assert missing = unresolved here. */
529197eca22SWill Andrews 
530197eca22SWill Andrews 	free(n);
531197eca22SWill Andrews 	return (unresolved);
532197eca22SWill Andrews }
533197eca22SWill Andrews 
534197eca22SWill Andrews int
535197eca22SWill Andrews _kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize)
536197eca22SWill Andrews {
537197eca22SWill Andrews 	struct kvm_nlist *p;
538197eca22SWill Andrews 	int nvalid;
539197eca22SWill Andrews 	struct kld_sym_lookup lookup;
540197eca22SWill Andrews 	int error;
541197eca22SWill Andrews 	const char *prefix = "";
542197eca22SWill Andrews 	char symname[1024]; /* XXX-BZ symbol name length limit? */
543197eca22SWill Andrews 	int tried_vnet, tried_dpcpu;
544197eca22SWill Andrews 
545197eca22SWill Andrews 	/*
546197eca22SWill Andrews 	 * If we can't use the kld symbol lookup, revert to the
547197eca22SWill Andrews 	 * slow library call.
548197eca22SWill Andrews 	 */
549197eca22SWill Andrews 	if (!ISALIVE(kd)) {
550197eca22SWill Andrews 		error = kvm_fdnlist(kd, nl);
551197eca22SWill Andrews 		if (error <= 0)			/* Hard error or success. */
552197eca22SWill Andrews 			return (error);
553197eca22SWill Andrews 
554197eca22SWill Andrews 		if (_kvm_vnet_initialized(kd, initialize))
555197eca22SWill Andrews 			error = kvm_fdnlist_prefix(kd, nl, error,
556197eca22SWill Andrews 			    VNET_SYMPREFIX, _kvm_vnet_validaddr);
557197eca22SWill Andrews 
558197eca22SWill Andrews 		if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
559197eca22SWill Andrews 			error = kvm_fdnlist_prefix(kd, nl, error,
560197eca22SWill Andrews 			    DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
561197eca22SWill Andrews 
562197eca22SWill Andrews 		return (error);
563197eca22SWill Andrews 	}
564197eca22SWill Andrews 
565197eca22SWill Andrews 	/*
566197eca22SWill Andrews 	 * We can use the kld lookup syscall.  Go through each nlist entry
567197eca22SWill Andrews 	 * and look it up with a kldsym(2) syscall.
568197eca22SWill Andrews 	 */
569197eca22SWill Andrews 	nvalid = 0;
570197eca22SWill Andrews 	tried_vnet = 0;
571197eca22SWill Andrews 	tried_dpcpu = 0;
572197eca22SWill Andrews again:
573197eca22SWill Andrews 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
574197eca22SWill Andrews 		if (p->n_type != N_UNDF)
575197eca22SWill Andrews 			continue;
576197eca22SWill Andrews 
577197eca22SWill Andrews 		lookup.version = sizeof(lookup);
578197eca22SWill Andrews 		lookup.symvalue = 0;
579197eca22SWill Andrews 		lookup.symsize = 0;
580197eca22SWill Andrews 
581197eca22SWill Andrews 		error = snprintf(symname, sizeof(symname), "%s%s", prefix,
582197eca22SWill Andrews 		    (prefix[0] != '\0' && p->n_name[0] == '_') ?
583197eca22SWill Andrews 			(p->n_name + 1) : p->n_name);
584197eca22SWill Andrews 		if (error < 0 || error >= (int)sizeof(symname))
585197eca22SWill Andrews 			continue;
586197eca22SWill Andrews 		lookup.symname = symname;
587197eca22SWill Andrews 		if (lookup.symname[0] == '_')
588197eca22SWill Andrews 			lookup.symname++;
589197eca22SWill Andrews 
590197eca22SWill Andrews 		if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
591197eca22SWill Andrews 			p->n_type = N_TEXT;
592197eca22SWill Andrews 			if (_kvm_vnet_initialized(kd, initialize) &&
593197eca22SWill Andrews 			    strcmp(prefix, VNET_SYMPREFIX) == 0)
594197eca22SWill Andrews 				p->n_value =
595197eca22SWill Andrews 				    _kvm_vnet_validaddr(kd, lookup.symvalue);
596197eca22SWill Andrews 			else if (_kvm_dpcpu_initialized(kd, initialize) &&
597197eca22SWill Andrews 			    strcmp(prefix, DPCPU_SYMPREFIX) == 0)
598197eca22SWill Andrews 				p->n_value =
599197eca22SWill Andrews 				    _kvm_dpcpu_validaddr(kd, lookup.symvalue);
600197eca22SWill Andrews 			else
601197eca22SWill Andrews 				p->n_value = lookup.symvalue;
602197eca22SWill Andrews 			++nvalid;
603197eca22SWill Andrews 			/* lookup.symsize */
604197eca22SWill Andrews 		}
605197eca22SWill Andrews 	}
606197eca22SWill Andrews 
607197eca22SWill Andrews 	/*
608197eca22SWill Andrews 	 * Check the number of entries that weren't found. If they exist,
609197eca22SWill Andrews 	 * try again with a prefix for virtualized or DPCPU symbol names.
610197eca22SWill Andrews 	 */
611197eca22SWill Andrews 	error = ((p - nl) - nvalid);
612197eca22SWill Andrews 	if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
613197eca22SWill Andrews 		tried_vnet = 1;
614197eca22SWill Andrews 		prefix = VNET_SYMPREFIX;
615197eca22SWill Andrews 		goto again;
616197eca22SWill Andrews 	}
617197eca22SWill Andrews 	if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
618197eca22SWill Andrews 		tried_dpcpu = 1;
619197eca22SWill Andrews 		prefix = DPCPU_SYMPREFIX;
620197eca22SWill Andrews 		goto again;
621197eca22SWill Andrews 	}
622197eca22SWill Andrews 
623197eca22SWill Andrews 	/*
624197eca22SWill Andrews 	 * Return the number of entries that weren't found. If they exist,
625197eca22SWill Andrews 	 * also fill internal error buffer.
626197eca22SWill Andrews 	 */
627197eca22SWill Andrews 	error = ((p - nl) - nvalid);
628197eca22SWill Andrews 	if (error)
629197eca22SWill Andrews 		_kvm_syserr(kd, kd->program, "kvm_nlist");
630197eca22SWill Andrews 	return (error);
631197eca22SWill Andrews }
632