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/param.h>
35197eca22SWill Andrews #include <sys/fnv_hash.h>
36197eca22SWill Andrews
37197eca22SWill Andrews #define _WANT_VNET
38197eca22SWill Andrews
39197eca22SWill Andrews #include <sys/user.h>
40197eca22SWill Andrews #include <sys/linker.h>
41197eca22SWill Andrews #include <sys/pcpu.h>
42197eca22SWill Andrews #include <sys/stat.h>
43c9057838SWill Andrews #include <sys/mman.h>
44197eca22SWill Andrews
4510108cb6SBjoern A. Zeeb #include <stdbool.h>
46197eca22SWill Andrews #include <net/vnet.h>
47197eca22SWill Andrews
48ffdeef32SWill Andrews #include <assert.h>
49197eca22SWill Andrews #include <fcntl.h>
508baaf913SWill Andrews #include <vm/vm.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>
60c9057838SWill Andrews #include <inttypes.h>
61197eca22SWill Andrews
62197eca22SWill Andrews #include "kvm_private.h"
63197eca22SWill Andrews
64197eca22SWill Andrews /*
65197eca22SWill Andrews * Routines private to libkvm.
66197eca22SWill Andrews */
67197eca22SWill Andrews
68197eca22SWill Andrews /* from src/lib/libc/gen/nlist.c */
69197eca22SWill Andrews int __fdnlist(int, struct nlist *);
70197eca22SWill Andrews
71197eca22SWill Andrews /*
72197eca22SWill Andrews * Report an error using printf style arguments. "program" is kd->program
73197eca22SWill Andrews * on hard errors, and 0 on soft errors, so that under sun error emulation,
74197eca22SWill Andrews * only hard errors are printed out (otherwise, programs like gdb will
75197eca22SWill Andrews * generate tons of error messages when trying to access bogus pointers).
76197eca22SWill Andrews */
77197eca22SWill Andrews void
_kvm_err(kvm_t * kd,const char * program,const char * fmt,...)78197eca22SWill Andrews _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
79197eca22SWill Andrews {
80197eca22SWill Andrews va_list ap;
81197eca22SWill Andrews
82197eca22SWill Andrews va_start(ap, fmt);
83197eca22SWill Andrews if (program != NULL) {
84197eca22SWill Andrews (void)fprintf(stderr, "%s: ", program);
85197eca22SWill Andrews (void)vfprintf(stderr, fmt, ap);
86197eca22SWill Andrews (void)fputc('\n', stderr);
87197eca22SWill Andrews } else
88197eca22SWill Andrews (void)vsnprintf(kd->errbuf,
89197eca22SWill Andrews sizeof(kd->errbuf), fmt, ap);
90197eca22SWill Andrews
91197eca22SWill Andrews va_end(ap);
92197eca22SWill Andrews }
93197eca22SWill Andrews
94197eca22SWill Andrews void
_kvm_syserr(kvm_t * kd,const char * program,const char * fmt,...)95197eca22SWill Andrews _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
96197eca22SWill Andrews {
97197eca22SWill Andrews va_list ap;
98197eca22SWill Andrews int n;
99197eca22SWill Andrews
100197eca22SWill Andrews va_start(ap, fmt);
101197eca22SWill Andrews if (program != NULL) {
102197eca22SWill Andrews (void)fprintf(stderr, "%s: ", program);
103197eca22SWill Andrews (void)vfprintf(stderr, fmt, ap);
104197eca22SWill Andrews (void)fprintf(stderr, ": %s\n", strerror(errno));
105197eca22SWill Andrews } else {
106197eca22SWill Andrews char *cp = kd->errbuf;
107197eca22SWill Andrews
108197eca22SWill Andrews (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
109197eca22SWill Andrews n = strlen(cp);
110197eca22SWill Andrews (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
111197eca22SWill Andrews strerror(errno));
112197eca22SWill Andrews }
113197eca22SWill Andrews va_end(ap);
114197eca22SWill Andrews }
115197eca22SWill Andrews
116197eca22SWill Andrews void *
_kvm_malloc(kvm_t * kd,size_t n)117197eca22SWill Andrews _kvm_malloc(kvm_t *kd, size_t n)
118197eca22SWill Andrews {
119197eca22SWill Andrews void *p;
120197eca22SWill Andrews
121197eca22SWill Andrews if ((p = calloc(n, sizeof(char))) == NULL)
122197eca22SWill Andrews _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
123197eca22SWill Andrews n, strerror(errno));
124197eca22SWill Andrews return (p);
125197eca22SWill Andrews }
126197eca22SWill Andrews
127197eca22SWill Andrews int
_kvm_probe_elf_kernel(kvm_t * kd,int class,int machine)128197eca22SWill Andrews _kvm_probe_elf_kernel(kvm_t *kd, int class, int machine)
129197eca22SWill Andrews {
130197eca22SWill Andrews
131197eca22SWill Andrews return (kd->nlehdr.e_ident[EI_CLASS] == class &&
1328024ba45SLeandro Lupori ((machine == EM_PPC || machine == EM_PPC64) ?
1338024ba45SLeandro Lupori kd->nlehdr.e_type == ET_DYN : kd->nlehdr.e_type == ET_EXEC) &&
134197eca22SWill Andrews kd->nlehdr.e_machine == machine);
135197eca22SWill Andrews }
136197eca22SWill Andrews
137197eca22SWill Andrews int
_kvm_is_minidump(kvm_t * kd)138197eca22SWill Andrews _kvm_is_minidump(kvm_t *kd)
139197eca22SWill Andrews {
140197eca22SWill Andrews char minihdr[8];
141197eca22SWill Andrews
142197eca22SWill Andrews if (kd->rawdump)
143197eca22SWill Andrews return (0);
144197eca22SWill Andrews if (pread(kd->pmfd, &minihdr, 8, 0) == 8 &&
145197eca22SWill Andrews memcmp(&minihdr, "minidump", 8) == 0)
146197eca22SWill Andrews return (1);
147197eca22SWill Andrews return (0);
148197eca22SWill Andrews }
149197eca22SWill Andrews
150197eca22SWill Andrews /*
151197eca22SWill Andrews * The powerpc backend has a hack to strip a leading kerneldump
152197eca22SWill Andrews * header from the core before treating it as an ELF header.
153197eca22SWill Andrews *
154197eca22SWill Andrews * We can add that here if we can get a change to libelf to support
155197eca22SWill Andrews * an initial offset into the file. Alternatively we could patch
156197eca22SWill Andrews * savecore to extract cores from a regular file instead.
157197eca22SWill Andrews */
158197eca22SWill Andrews int
_kvm_read_core_phdrs(kvm_t * kd,size_t * phnump,GElf_Phdr ** phdrp)159197eca22SWill Andrews _kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp)
160197eca22SWill Andrews {
161197eca22SWill Andrews GElf_Ehdr ehdr;
162197eca22SWill Andrews GElf_Phdr *phdr;
163197eca22SWill Andrews Elf *elf;
164197eca22SWill Andrews size_t i, phnum;
165197eca22SWill Andrews
166197eca22SWill Andrews elf = elf_begin(kd->pmfd, ELF_C_READ, NULL);
167197eca22SWill Andrews if (elf == NULL) {
168197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
169197eca22SWill Andrews return (-1);
170197eca22SWill Andrews }
171197eca22SWill Andrews if (elf_kind(elf) != ELF_K_ELF) {
172197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core");
173197eca22SWill Andrews goto bad;
174197eca22SWill Andrews }
175197eca22SWill Andrews if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) {
176197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core");
177197eca22SWill Andrews goto bad;
178197eca22SWill Andrews }
179197eca22SWill Andrews if (gelf_getehdr(elf, &ehdr) == NULL) {
180197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
181197eca22SWill Andrews goto bad;
182197eca22SWill Andrews }
183197eca22SWill Andrews if (ehdr.e_type != ET_CORE) {
184197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core");
185197eca22SWill Andrews goto bad;
186197eca22SWill Andrews }
187197eca22SWill Andrews if (ehdr.e_machine != kd->nlehdr.e_machine) {
188197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core");
189197eca22SWill Andrews goto bad;
190197eca22SWill Andrews }
191197eca22SWill Andrews
192197eca22SWill Andrews if (elf_getphdrnum(elf, &phnum) == -1) {
193197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
194197eca22SWill Andrews goto bad;
195197eca22SWill Andrews }
196197eca22SWill Andrews
197197eca22SWill Andrews phdr = calloc(phnum, sizeof(*phdr));
198197eca22SWill Andrews if (phdr == NULL) {
199197eca22SWill Andrews _kvm_err(kd, kd->program, "failed to allocate phdrs");
200197eca22SWill Andrews goto bad;
201197eca22SWill Andrews }
202197eca22SWill Andrews
203197eca22SWill Andrews for (i = 0; i < phnum; i++) {
204197eca22SWill Andrews if (gelf_getphdr(elf, i, &phdr[i]) == NULL) {
205f6080aabSGleb Smirnoff free(phdr);
206197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
207197eca22SWill Andrews goto bad;
208197eca22SWill Andrews }
209197eca22SWill Andrews }
210197eca22SWill Andrews elf_end(elf);
211197eca22SWill Andrews *phnump = phnum;
212197eca22SWill Andrews *phdrp = phdr;
213197eca22SWill Andrews return (0);
214197eca22SWill Andrews
215197eca22SWill Andrews bad:
216197eca22SWill Andrews elf_end(elf);
217197eca22SWill Andrews return (-1);
218197eca22SWill Andrews }
219197eca22SWill Andrews
220ffdeef32SWill Andrews /*
221ffdeef32SWill Andrews * Transform v such that only bits [bit0, bitN) may be set. Generates a
222ffdeef32SWill Andrews * bitmask covering the number of bits, then shifts so +bit0+ is the first.
223ffdeef32SWill Andrews */
224ffdeef32SWill Andrews static uint64_t
bitmask_range(uint64_t v,uint64_t bit0,uint64_t bitN)225ffdeef32SWill Andrews bitmask_range(uint64_t v, uint64_t bit0, uint64_t bitN)
226197eca22SWill Andrews {
227ffdeef32SWill Andrews if (bit0 == 0 && bitN == BITS_IN(v))
228ffdeef32SWill Andrews return (v);
229197eca22SWill Andrews
230ffdeef32SWill Andrews return (v & (((1ULL << (bitN - bit0)) - 1ULL) << bit0));
231197eca22SWill Andrews }
232197eca22SWill Andrews
233ffdeef32SWill Andrews /*
234ffdeef32SWill Andrews * Returns the number of bits in a given byte array range starting at a
235ffdeef32SWill Andrews * given base, from bit0 to bitN. bit0 may be non-zero in the case of
236ffdeef32SWill Andrews * counting backwards from bitN.
237ffdeef32SWill Andrews */
238ffdeef32SWill Andrews static uint64_t
popcount_bytes(uint64_t * addr,uint32_t bit0,uint32_t bitN)239ffdeef32SWill Andrews popcount_bytes(uint64_t *addr, uint32_t bit0, uint32_t bitN)
240ffdeef32SWill Andrews {
241ffdeef32SWill Andrews uint32_t res = bitN - bit0;
242ffdeef32SWill Andrews uint64_t count = 0;
243ffdeef32SWill Andrews uint32_t bound;
244ffdeef32SWill Andrews
245ffdeef32SWill Andrews /* Align to 64-bit boundary on the left side if needed. */
246ffdeef32SWill Andrews if ((bit0 % BITS_IN(*addr)) != 0) {
247ffdeef32SWill Andrews bound = MIN(bitN, roundup2(bit0, BITS_IN(*addr)));
248ffdeef32SWill Andrews count += __bitcount64(bitmask_range(*addr, bit0, bound));
249ffdeef32SWill Andrews res -= (bound - bit0);
250ffdeef32SWill Andrews addr++;
251ffdeef32SWill Andrews }
252ffdeef32SWill Andrews
253ffdeef32SWill Andrews while (res > 0) {
254ffdeef32SWill Andrews bound = MIN(res, BITS_IN(*addr));
255ffdeef32SWill Andrews count += __bitcount64(bitmask_range(*addr, 0, bound));
256ffdeef32SWill Andrews res -= bound;
257ffdeef32SWill Andrews addr++;
258ffdeef32SWill Andrews }
259ffdeef32SWill Andrews
260ffdeef32SWill Andrews return (count);
261ffdeef32SWill Andrews }
262ffdeef32SWill Andrews
263c9057838SWill Andrews void *
_kvm_pmap_get(kvm_t * kd,u_long idx,size_t len)2642aa6a4f3SWill Andrews _kvm_pmap_get(kvm_t *kd, u_long idx, size_t len)
265c9057838SWill Andrews {
2668baaf913SWill Andrews uintptr_t off = idx * len;
267c9057838SWill Andrews
2688baaf913SWill Andrews if ((off_t)off >= kd->pt_sparse_off)
269c9057838SWill Andrews return (NULL);
270c9057838SWill Andrews return (void *)((uintptr_t)kd->page_map + off);
271c9057838SWill Andrews }
272c9057838SWill Andrews
273c9057838SWill Andrews void *
_kvm_map_get(kvm_t * kd,u_long pa,unsigned int page_size)274c9057838SWill Andrews _kvm_map_get(kvm_t *kd, u_long pa, unsigned int page_size)
275c9057838SWill Andrews {
276c9057838SWill Andrews off_t off;
277c9057838SWill Andrews uintptr_t addr;
278c9057838SWill Andrews
279c9057838SWill Andrews off = _kvm_pt_find(kd, pa, page_size);
280c9057838SWill Andrews if (off == -1)
281c9057838SWill Andrews return NULL;
282c9057838SWill Andrews
283c9057838SWill Andrews addr = (uintptr_t)kd->page_map + off;
284c9057838SWill Andrews if (off >= kd->pt_sparse_off)
285c9057838SWill Andrews addr = (uintptr_t)kd->sparse_map + (off - kd->pt_sparse_off);
286c9057838SWill Andrews return (void *)addr;
287c9057838SWill Andrews }
288c9057838SWill Andrews
289ffdeef32SWill Andrews int
_kvm_pt_init(kvm_t * kd,size_t dump_avail_size,off_t dump_avail_off,size_t map_len,off_t map_off,off_t sparse_off,int page_size)29000e66147SD Scott Phillips _kvm_pt_init(kvm_t *kd, size_t dump_avail_size, off_t dump_avail_off,
291b957b185SMark Johnston size_t map_len, off_t map_off, off_t sparse_off, int page_size)
292197eca22SWill Andrews {
293ffdeef32SWill Andrews uint64_t *addr;
294ffdeef32SWill Andrews uint32_t *popcount_bin;
295ffdeef32SWill Andrews int bin_popcounts = 0;
296ffdeef32SWill Andrews uint64_t pc_bins, res;
297ffdeef32SWill Andrews ssize_t rd;
298197eca22SWill Andrews
29900e66147SD Scott Phillips kd->dump_avail_size = dump_avail_size;
30000e66147SD Scott Phillips if (dump_avail_size > 0) {
30100e66147SD Scott Phillips kd->dump_avail = mmap(NULL, kd->dump_avail_size, PROT_READ,
30200e66147SD Scott Phillips MAP_PRIVATE, kd->pmfd, dump_avail_off);
30300e66147SD Scott Phillips } else {
30400e66147SD Scott Phillips /*
30500e66147SD Scott Phillips * Older version minidumps don't provide dump_avail[],
30600e66147SD Scott Phillips * so the bitmap is fully populated from 0 to
30700e66147SD Scott Phillips * last_pa. Create an implied dump_avail that
30800e66147SD Scott Phillips * expresses this.
30900e66147SD Scott Phillips */
310b957b185SMark Johnston kd->dump_avail = calloc(4, sizeof(uint64_t));
311b957b185SMark Johnston kd->dump_avail[1] = _kvm64toh(kd, map_len * 8 * page_size);
31200e66147SD Scott Phillips }
31300e66147SD Scott Phillips
314ffdeef32SWill Andrews /*
315ffdeef32SWill Andrews * Map the bitmap specified by the arguments.
316ffdeef32SWill Andrews */
317ffdeef32SWill Andrews kd->pt_map = _kvm_malloc(kd, map_len);
318ffdeef32SWill Andrews if (kd->pt_map == NULL) {
319ffdeef32SWill Andrews _kvm_err(kd, kd->program, "cannot allocate %zu bytes for bitmap",
320ffdeef32SWill Andrews map_len);
321197eca22SWill Andrews return (-1);
322197eca22SWill Andrews }
323ffdeef32SWill Andrews rd = pread(kd->pmfd, kd->pt_map, map_len, map_off);
324ffdeef32SWill Andrews if (rd < 0 || rd != (ssize_t)map_len) {
325ffdeef32SWill Andrews _kvm_err(kd, kd->program, "cannot read %zu bytes for bitmap",
326ffdeef32SWill Andrews map_len);
327ffdeef32SWill Andrews return (-1);
328ffdeef32SWill Andrews }
329ffdeef32SWill Andrews kd->pt_map_size = map_len;
330197eca22SWill Andrews
331ffdeef32SWill Andrews /*
332ffdeef32SWill Andrews * Generate a popcount cache for every POPCOUNT_BITS in the bitmap,
333ffdeef32SWill Andrews * so lookups only have to calculate the number of bits set between
334ffdeef32SWill Andrews * a cache point and their bit. This reduces lookups to O(1),
335ffdeef32SWill Andrews * without significantly increasing memory requirements.
336ffdeef32SWill Andrews *
337ffdeef32SWill Andrews * Round up the number of bins so that 'upper half' lookups work for
338ffdeef32SWill Andrews * the final bin, if needed. The first popcount is 0, since no bits
339ffdeef32SWill Andrews * precede bit 0, so add 1 for that also. Without this, extra work
340ffdeef32SWill Andrews * would be needed to handle the first PTEs in _kvm_pt_find().
341ffdeef32SWill Andrews */
342ffdeef32SWill Andrews addr = kd->pt_map;
343ffdeef32SWill Andrews res = map_len;
344ffdeef32SWill Andrews pc_bins = 1 + (res * NBBY + POPCOUNT_BITS / 2) / POPCOUNT_BITS;
345ffdeef32SWill Andrews kd->pt_popcounts = calloc(pc_bins, sizeof(uint32_t));
346c9057838SWill Andrews if (kd->pt_popcounts == NULL) {
347c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot allocate popcount bins");
348ffdeef32SWill Andrews return (-1);
349c9057838SWill Andrews }
350ffdeef32SWill Andrews
351ffdeef32SWill Andrews for (popcount_bin = &kd->pt_popcounts[1]; res > 0;
352ffdeef32SWill Andrews addr++, res -= sizeof(*addr)) {
353ffdeef32SWill Andrews *popcount_bin += popcount_bytes(addr, 0,
354ffdeef32SWill Andrews MIN(res * NBBY, BITS_IN(*addr)));
355ffdeef32SWill Andrews if (++bin_popcounts == POPCOUNTS_IN(*addr)) {
356ffdeef32SWill Andrews popcount_bin++;
357ffdeef32SWill Andrews *popcount_bin = *(popcount_bin - 1);
358ffdeef32SWill Andrews bin_popcounts = 0;
359ffdeef32SWill Andrews }
360ffdeef32SWill Andrews }
361ffdeef32SWill Andrews
362ffdeef32SWill Andrews assert(pc_bins * sizeof(*popcount_bin) ==
363ffdeef32SWill Andrews ((uintptr_t)popcount_bin - (uintptr_t)kd->pt_popcounts));
364ffdeef32SWill Andrews
365ffdeef32SWill Andrews kd->pt_sparse_off = sparse_off;
366c9057838SWill Andrews kd->pt_sparse_size = (uint64_t)*popcount_bin * page_size;
367ffdeef32SWill Andrews kd->pt_page_size = page_size;
368c9057838SWill Andrews
369c9057838SWill Andrews /*
370c9057838SWill Andrews * Map the sparse page array. This is useful for performing point
371c9057838SWill Andrews * lookups of specific pages, e.g. for kvm_walk_pages. Generally,
372c9057838SWill Andrews * this is much larger than is reasonable to read in up front, so
373c9057838SWill Andrews * mmap it in instead.
374c9057838SWill Andrews */
375c9057838SWill Andrews kd->sparse_map = mmap(NULL, kd->pt_sparse_size, PROT_READ,
376c9057838SWill Andrews MAP_PRIVATE, kd->pmfd, kd->pt_sparse_off);
377c9057838SWill Andrews if (kd->sparse_map == MAP_FAILED) {
378c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot map %" PRIu64
3798baaf913SWill Andrews " bytes from fd %d offset %jd for sparse map: %s",
380c9057838SWill Andrews kd->pt_sparse_size, kd->pmfd,
3818baaf913SWill Andrews (intmax_t)kd->pt_sparse_off, strerror(errno));
382c9057838SWill Andrews return (-1);
383c9057838SWill Andrews }
384c9057838SWill Andrews return (0);
385c9057838SWill Andrews }
386c9057838SWill Andrews
387c9057838SWill Andrews int
_kvm_pmap_init(kvm_t * kd,uint32_t pmap_size,off_t pmap_off)388c9057838SWill Andrews _kvm_pmap_init(kvm_t *kd, uint32_t pmap_size, off_t pmap_off)
389c9057838SWill Andrews {
390c9057838SWill Andrews ssize_t exp_len = pmap_size;
391c9057838SWill Andrews
392c9057838SWill Andrews kd->page_map_size = pmap_size;
393c9057838SWill Andrews kd->page_map_off = pmap_off;
394c9057838SWill Andrews kd->page_map = _kvm_malloc(kd, pmap_size);
395c9057838SWill Andrews if (kd->page_map == NULL) {
396c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot allocate %u bytes "
397c9057838SWill Andrews "for page map", pmap_size);
398c9057838SWill Andrews return (-1);
399c9057838SWill Andrews }
400c9057838SWill Andrews if (pread(kd->pmfd, kd->page_map, pmap_size, pmap_off) != exp_len) {
401c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot read %d bytes from "
4028baaf913SWill Andrews "offset %jd for page map", pmap_size, (intmax_t)pmap_off);
403c9057838SWill Andrews return (-1);
404c9057838SWill Andrews }
405ffdeef32SWill Andrews return (0);
406ffdeef32SWill Andrews }
407ffdeef32SWill Andrews
40800e66147SD Scott Phillips static inline uint64_t
dump_avail_n(kvm_t * kd,long i)40900e66147SD Scott Phillips dump_avail_n(kvm_t *kd, long i)
41000e66147SD Scott Phillips {
41100e66147SD Scott Phillips return (_kvm64toh(kd, kd->dump_avail[i]));
41200e66147SD Scott Phillips }
41300e66147SD Scott Phillips
41400e66147SD Scott Phillips uint64_t
_kvm_pa_bit_id(kvm_t * kd,uint64_t pa,unsigned int page_size)41500e66147SD Scott Phillips _kvm_pa_bit_id(kvm_t *kd, uint64_t pa, unsigned int page_size)
41600e66147SD Scott Phillips {
41700e66147SD Scott Phillips uint64_t adj;
41800e66147SD Scott Phillips long i;
41900e66147SD Scott Phillips
42000e66147SD Scott Phillips adj = 0;
42100e66147SD Scott Phillips for (i = 0; dump_avail_n(kd, i + 1) != 0; i += 2) {
42200e66147SD Scott Phillips if (pa >= dump_avail_n(kd, i + 1)) {
42300e66147SD Scott Phillips adj += howmany(dump_avail_n(kd, i + 1), page_size) -
42400e66147SD Scott Phillips dump_avail_n(kd, i) / page_size;
42500e66147SD Scott Phillips } else {
42600e66147SD Scott Phillips return (pa / page_size -
42700e66147SD Scott Phillips dump_avail_n(kd, i) / page_size + adj);
42800e66147SD Scott Phillips }
42900e66147SD Scott Phillips }
43000e66147SD Scott Phillips return (_KVM_BIT_ID_INVALID);
43100e66147SD Scott Phillips }
43200e66147SD Scott Phillips
43300e66147SD Scott Phillips uint64_t
_kvm_bit_id_pa(kvm_t * kd,uint64_t bit_id,unsigned int page_size)43400e66147SD Scott Phillips _kvm_bit_id_pa(kvm_t *kd, uint64_t bit_id, unsigned int page_size)
43500e66147SD Scott Phillips {
43600e66147SD Scott Phillips uint64_t sz;
43700e66147SD Scott Phillips long i;
43800e66147SD Scott Phillips
43900e66147SD Scott Phillips for (i = 0; dump_avail_n(kd, i + 1) != 0; i += 2) {
44000e66147SD Scott Phillips sz = howmany(dump_avail_n(kd, i + 1), page_size) -
44100e66147SD Scott Phillips dump_avail_n(kd, i) / page_size;
44200e66147SD Scott Phillips if (bit_id < sz) {
44300e66147SD Scott Phillips return (rounddown2(dump_avail_n(kd, i), page_size) +
44400e66147SD Scott Phillips bit_id * page_size);
44500e66147SD Scott Phillips }
44600e66147SD Scott Phillips bit_id -= sz;
44700e66147SD Scott Phillips }
44800e66147SD Scott Phillips return (_KVM_PA_INVALID);
44900e66147SD Scott Phillips }
45000e66147SD Scott Phillips
451ffdeef32SWill Andrews /*
452ffdeef32SWill Andrews * Find the offset for the given physical page address; returns -1 otherwise.
453ffdeef32SWill Andrews *
454ffdeef32SWill Andrews * A page's offset is represented by the sparse page base offset plus the
455c9057838SWill Andrews * number of bits set before its bit multiplied by page size. This means
456ffdeef32SWill Andrews * that if a page exists in the dump, it's necessary to know how many pages
457ffdeef32SWill Andrews * in the dump precede it. Reduce this O(n) counting to O(1) by caching the
458ffdeef32SWill Andrews * number of bits set at POPCOUNT_BITS intervals.
459ffdeef32SWill Andrews *
460ffdeef32SWill Andrews * Then to find the number of pages before the requested address, simply
461ffdeef32SWill Andrews * index into the cache and count the number of bits set between that cache
462ffdeef32SWill Andrews * bin and the page's bit. Halve the number of bytes that have to be
463ffdeef32SWill Andrews * checked by also counting down from the next higher bin if it's closer.
464ffdeef32SWill Andrews */
465ffdeef32SWill Andrews off_t
_kvm_pt_find(kvm_t * kd,uint64_t pa,unsigned int page_size)466c9057838SWill Andrews _kvm_pt_find(kvm_t *kd, uint64_t pa, unsigned int page_size)
467197eca22SWill Andrews {
468ffdeef32SWill Andrews uint64_t *bitmap = kd->pt_map;
46900e66147SD Scott Phillips uint64_t pte_bit_id = _kvm_pa_bit_id(kd, pa, page_size);
470ffdeef32SWill Andrews uint64_t pte_u64 = pte_bit_id / BITS_IN(*bitmap);
471ffdeef32SWill Andrews uint64_t popcount_id = pte_bit_id / POPCOUNT_BITS;
472ffdeef32SWill Andrews uint64_t pte_mask = 1ULL << (pte_bit_id % BITS_IN(*bitmap));
473ffdeef32SWill Andrews uint64_t bitN;
474ffdeef32SWill Andrews uint32_t count;
475197eca22SWill Andrews
476ffdeef32SWill Andrews /* Check whether the page address requested is in the dump. */
47700e66147SD Scott Phillips if (pte_bit_id == _KVM_BIT_ID_INVALID ||
47800e66147SD Scott Phillips pte_bit_id >= (kd->pt_map_size * NBBY) ||
479ffdeef32SWill Andrews (bitmap[pte_u64] & pte_mask) == 0)
480ffdeef32SWill Andrews return (-1);
481ffdeef32SWill Andrews
482ffdeef32SWill Andrews /*
483ffdeef32SWill Andrews * Add/sub popcounts from the bitmap until the PTE's bit is reached.
484ffdeef32SWill Andrews * For bits that are in the upper half between the calculated
485ffdeef32SWill Andrews * popcount id and the next one, use the next one and subtract to
486ffdeef32SWill Andrews * minimize the number of popcounts required.
487ffdeef32SWill Andrews */
488ffdeef32SWill Andrews if ((pte_bit_id % POPCOUNT_BITS) < (POPCOUNT_BITS / 2)) {
489ffdeef32SWill Andrews count = kd->pt_popcounts[popcount_id] + popcount_bytes(
490ffdeef32SWill Andrews bitmap + popcount_id * POPCOUNTS_IN(*bitmap),
491ffdeef32SWill Andrews 0, pte_bit_id - popcount_id * POPCOUNT_BITS);
492ffdeef32SWill Andrews } else {
493ffdeef32SWill Andrews /*
494ffdeef32SWill Andrews * Counting in reverse is trickier, since we must avoid
495ffdeef32SWill Andrews * reading from bytes that are not in range, and invert.
496ffdeef32SWill Andrews */
497ffdeef32SWill Andrews uint64_t pte_u64_bit_off = pte_u64 * BITS_IN(*bitmap);
498ffdeef32SWill Andrews
499ffdeef32SWill Andrews popcount_id++;
500ffdeef32SWill Andrews bitN = MIN(popcount_id * POPCOUNT_BITS,
501ffdeef32SWill Andrews kd->pt_map_size * BITS_IN(uint8_t));
502ffdeef32SWill Andrews count = kd->pt_popcounts[popcount_id] - popcount_bytes(
503ffdeef32SWill Andrews bitmap + pte_u64,
504ffdeef32SWill Andrews pte_bit_id - pte_u64_bit_off, bitN - pte_u64_bit_off);
505197eca22SWill Andrews }
506ffdeef32SWill Andrews
507ffdeef32SWill Andrews /*
508ffdeef32SWill Andrews * This can only happen if the core is truncated. Treat these
509ffdeef32SWill Andrews * entries as if they don't exist, since their backing doesn't.
510ffdeef32SWill Andrews */
511c9057838SWill Andrews if (count >= (kd->pt_sparse_size / page_size))
512ffdeef32SWill Andrews return (-1);
513ffdeef32SWill Andrews
514c9057838SWill Andrews return (kd->pt_sparse_off + (uint64_t)count * page_size);
515197eca22SWill Andrews }
516197eca22SWill Andrews
517197eca22SWill Andrews static int
kvm_fdnlist(kvm_t * kd,struct kvm_nlist * list)518197eca22SWill Andrews kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list)
519197eca22SWill Andrews {
520197eca22SWill Andrews kvaddr_t addr;
521197eca22SWill Andrews int error, nfail;
522197eca22SWill Andrews
523197eca22SWill Andrews if (kd->resolve_symbol == NULL) {
524197eca22SWill Andrews struct nlist *nl;
525197eca22SWill Andrews int count, i;
526197eca22SWill Andrews
527197eca22SWill Andrews for (count = 0; list[count].n_name != NULL &&
528197eca22SWill Andrews list[count].n_name[0] != '\0'; count++)
529197eca22SWill Andrews ;
530197eca22SWill Andrews nl = calloc(count + 1, sizeof(*nl));
531197eca22SWill Andrews for (i = 0; i < count; i++)
532197eca22SWill Andrews nl[i].n_name = list[i].n_name;
533197eca22SWill Andrews nfail = __fdnlist(kd->nlfd, nl);
534197eca22SWill Andrews for (i = 0; i < count; i++) {
535197eca22SWill Andrews list[i].n_type = nl[i].n_type;
536197eca22SWill Andrews list[i].n_value = nl[i].n_value;
537197eca22SWill Andrews }
538197eca22SWill Andrews free(nl);
539197eca22SWill Andrews return (nfail);
540197eca22SWill Andrews }
541197eca22SWill Andrews
542197eca22SWill Andrews nfail = 0;
543197eca22SWill Andrews while (list->n_name != NULL && list->n_name[0] != '\0') {
544197eca22SWill Andrews error = kd->resolve_symbol(list->n_name, &addr);
545197eca22SWill Andrews if (error != 0) {
546197eca22SWill Andrews nfail++;
547197eca22SWill Andrews list->n_value = 0;
548197eca22SWill Andrews list->n_type = 0;
549197eca22SWill Andrews } else {
550197eca22SWill Andrews list->n_value = addr;
551197eca22SWill Andrews list->n_type = N_DATA | N_EXT;
552197eca22SWill Andrews }
553197eca22SWill Andrews list++;
554197eca22SWill Andrews }
555197eca22SWill Andrews return (nfail);
556197eca22SWill Andrews }
557197eca22SWill Andrews
558197eca22SWill Andrews /*
559197eca22SWill Andrews * Walk the list of unresolved symbols, generate a new list and prefix the
560197eca22SWill Andrews * symbol names, try again, and merge back what we could resolve.
561197eca22SWill Andrews */
562197eca22SWill Andrews static int
kvm_fdnlist_prefix(kvm_t * kd,struct kvm_nlist * nl,int missing,const char * prefix,kvaddr_t (* validate_fn)(kvm_t *,kvaddr_t))563197eca22SWill Andrews kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing,
564197eca22SWill Andrews const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t))
565197eca22SWill Andrews {
566197eca22SWill Andrews struct kvm_nlist *n, *np, *p;
567197eca22SWill Andrews char *cp, *ce;
568197eca22SWill Andrews const char *ccp;
569197eca22SWill Andrews size_t len;
570197eca22SWill Andrews int slen, unresolved;
571197eca22SWill Andrews
572197eca22SWill Andrews /*
573197eca22SWill Andrews * Calculate the space we need to malloc for nlist and names.
574197eca22SWill Andrews * We are going to store the name twice for later lookups: once
575197eca22SWill Andrews * with the prefix and once the unmodified name delmited by \0.
576197eca22SWill Andrews */
577197eca22SWill Andrews len = 0;
578197eca22SWill Andrews unresolved = 0;
579197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) {
580197eca22SWill Andrews if (p->n_type != N_UNDF)
581197eca22SWill Andrews continue;
582197eca22SWill Andrews len += sizeof(struct kvm_nlist) + strlen(prefix) +
583197eca22SWill Andrews 2 * (strlen(p->n_name) + 1);
584197eca22SWill Andrews unresolved++;
585197eca22SWill Andrews }
586197eca22SWill Andrews if (unresolved == 0)
587197eca22SWill Andrews return (unresolved);
588197eca22SWill Andrews /* Add space for the terminating nlist entry. */
589197eca22SWill Andrews len += sizeof(struct kvm_nlist);
590197eca22SWill Andrews unresolved++;
591197eca22SWill Andrews
592197eca22SWill Andrews /* Alloc one chunk for (nlist, [names]) and setup pointers. */
593197eca22SWill Andrews n = np = malloc(len);
594197eca22SWill Andrews bzero(n, len);
595197eca22SWill Andrews if (n == NULL)
596197eca22SWill Andrews return (missing);
597197eca22SWill Andrews cp = ce = (char *)np;
598197eca22SWill Andrews cp += unresolved * sizeof(struct kvm_nlist);
599197eca22SWill Andrews ce += len;
600197eca22SWill Andrews
601197eca22SWill Andrews /* Generate shortened nlist with special prefix. */
602197eca22SWill Andrews unresolved = 0;
603197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) {
604197eca22SWill Andrews if (p->n_type != N_UNDF)
605197eca22SWill Andrews continue;
606197eca22SWill Andrews *np = *p;
607197eca22SWill Andrews /* Save the new\0orig. name so we can later match it again. */
608197eca22SWill Andrews slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
609197eca22SWill Andrews (prefix[0] != '\0' && p->n_name[0] == '_') ?
610197eca22SWill Andrews (p->n_name + 1) : p->n_name, '\0', p->n_name);
611197eca22SWill Andrews if (slen < 0 || slen >= ce - cp)
612197eca22SWill Andrews continue;
613197eca22SWill Andrews np->n_name = cp;
614197eca22SWill Andrews cp += slen + 1;
615197eca22SWill Andrews np++;
616197eca22SWill Andrews unresolved++;
617197eca22SWill Andrews }
618197eca22SWill Andrews
619197eca22SWill Andrews /* Do lookup on the reduced list. */
620197eca22SWill Andrews np = n;
621197eca22SWill Andrews unresolved = kvm_fdnlist(kd, np);
622197eca22SWill Andrews
623197eca22SWill Andrews /* Check if we could resolve further symbols and update the list. */
624197eca22SWill Andrews if (unresolved >= 0 && unresolved < missing) {
625197eca22SWill Andrews /* Find the first freshly resolved entry. */
626197eca22SWill Andrews for (; np->n_name && np->n_name[0]; np++)
627197eca22SWill Andrews if (np->n_type != N_UNDF)
628197eca22SWill Andrews break;
629197eca22SWill Andrews /*
630197eca22SWill Andrews * The lists are both in the same order,
631197eca22SWill Andrews * so we can walk them in parallel.
632197eca22SWill Andrews */
633197eca22SWill Andrews for (p = nl; np->n_name && np->n_name[0] &&
634197eca22SWill Andrews p->n_name && p->n_name[0]; ++p) {
635197eca22SWill Andrews if (p->n_type != N_UNDF)
636197eca22SWill Andrews continue;
637197eca22SWill Andrews /* Skip expanded name and compare to orig. one. */
638197eca22SWill Andrews ccp = np->n_name + strlen(np->n_name) + 1;
639197eca22SWill Andrews if (strcmp(ccp, p->n_name) != 0)
640197eca22SWill Andrews continue;
641197eca22SWill Andrews /* Update nlist with new, translated results. */
642197eca22SWill Andrews p->n_type = np->n_type;
643197eca22SWill Andrews if (validate_fn)
644197eca22SWill Andrews p->n_value = (*validate_fn)(kd, np->n_value);
645197eca22SWill Andrews else
646197eca22SWill Andrews p->n_value = np->n_value;
647197eca22SWill Andrews missing--;
648197eca22SWill Andrews /* Find next freshly resolved entry. */
649197eca22SWill Andrews for (np++; np->n_name && np->n_name[0]; np++)
650197eca22SWill Andrews if (np->n_type != N_UNDF)
651197eca22SWill Andrews break;
652197eca22SWill Andrews }
653197eca22SWill Andrews }
654197eca22SWill Andrews /* We could assert missing = unresolved here. */
655197eca22SWill Andrews
656197eca22SWill Andrews free(n);
657197eca22SWill Andrews return (unresolved);
658197eca22SWill Andrews }
659197eca22SWill Andrews
660197eca22SWill Andrews int
_kvm_nlist(kvm_t * kd,struct kvm_nlist * nl,int initialize)661197eca22SWill Andrews _kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize)
662197eca22SWill Andrews {
663197eca22SWill Andrews struct kvm_nlist *p;
664197eca22SWill Andrews int nvalid;
665197eca22SWill Andrews struct kld_sym_lookup lookup;
666197eca22SWill Andrews int error;
667197eca22SWill Andrews const char *prefix = "";
668197eca22SWill Andrews char symname[1024]; /* XXX-BZ symbol name length limit? */
669197eca22SWill Andrews int tried_vnet, tried_dpcpu;
670197eca22SWill Andrews
671197eca22SWill Andrews /*
672197eca22SWill Andrews * If we can't use the kld symbol lookup, revert to the
673197eca22SWill Andrews * slow library call.
674197eca22SWill Andrews */
675197eca22SWill Andrews if (!ISALIVE(kd)) {
676197eca22SWill Andrews error = kvm_fdnlist(kd, nl);
677197eca22SWill Andrews if (error <= 0) /* Hard error or success. */
678197eca22SWill Andrews return (error);
679197eca22SWill Andrews
680197eca22SWill Andrews if (_kvm_vnet_initialized(kd, initialize))
681197eca22SWill Andrews error = kvm_fdnlist_prefix(kd, nl, error,
682197eca22SWill Andrews VNET_SYMPREFIX, _kvm_vnet_validaddr);
683197eca22SWill Andrews
684197eca22SWill Andrews if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
685197eca22SWill Andrews error = kvm_fdnlist_prefix(kd, nl, error,
686197eca22SWill Andrews DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
687197eca22SWill Andrews
688197eca22SWill Andrews return (error);
689197eca22SWill Andrews }
690197eca22SWill Andrews
691197eca22SWill Andrews /*
692197eca22SWill Andrews * We can use the kld lookup syscall. Go through each nlist entry
693197eca22SWill Andrews * and look it up with a kldsym(2) syscall.
694197eca22SWill Andrews */
695197eca22SWill Andrews nvalid = 0;
696197eca22SWill Andrews tried_vnet = 0;
697197eca22SWill Andrews tried_dpcpu = 0;
698197eca22SWill Andrews again:
699197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) {
700197eca22SWill Andrews if (p->n_type != N_UNDF)
701197eca22SWill Andrews continue;
702197eca22SWill Andrews
703197eca22SWill Andrews lookup.version = sizeof(lookup);
704197eca22SWill Andrews lookup.symvalue = 0;
705197eca22SWill Andrews lookup.symsize = 0;
706197eca22SWill Andrews
707197eca22SWill Andrews error = snprintf(symname, sizeof(symname), "%s%s", prefix,
708197eca22SWill Andrews (prefix[0] != '\0' && p->n_name[0] == '_') ?
709197eca22SWill Andrews (p->n_name + 1) : p->n_name);
710197eca22SWill Andrews if (error < 0 || error >= (int)sizeof(symname))
711197eca22SWill Andrews continue;
712197eca22SWill Andrews lookup.symname = symname;
713197eca22SWill Andrews if (lookup.symname[0] == '_')
714197eca22SWill Andrews lookup.symname++;
715197eca22SWill Andrews
716197eca22SWill Andrews if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
717197eca22SWill Andrews p->n_type = N_TEXT;
718197eca22SWill Andrews if (_kvm_vnet_initialized(kd, initialize) &&
719197eca22SWill Andrews strcmp(prefix, VNET_SYMPREFIX) == 0)
720197eca22SWill Andrews p->n_value =
721197eca22SWill Andrews _kvm_vnet_validaddr(kd, lookup.symvalue);
722197eca22SWill Andrews else if (_kvm_dpcpu_initialized(kd, initialize) &&
723197eca22SWill Andrews strcmp(prefix, DPCPU_SYMPREFIX) == 0)
724197eca22SWill Andrews p->n_value =
725197eca22SWill Andrews _kvm_dpcpu_validaddr(kd, lookup.symvalue);
726197eca22SWill Andrews else
727197eca22SWill Andrews p->n_value = lookup.symvalue;
728197eca22SWill Andrews ++nvalid;
729197eca22SWill Andrews /* lookup.symsize */
730197eca22SWill Andrews }
731197eca22SWill Andrews }
732197eca22SWill Andrews
733197eca22SWill Andrews /*
734197eca22SWill Andrews * Check the number of entries that weren't found. If they exist,
735197eca22SWill Andrews * try again with a prefix for virtualized or DPCPU symbol names.
736197eca22SWill Andrews */
737197eca22SWill Andrews error = ((p - nl) - nvalid);
738197eca22SWill Andrews if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
739197eca22SWill Andrews tried_vnet = 1;
740197eca22SWill Andrews prefix = VNET_SYMPREFIX;
741197eca22SWill Andrews goto again;
742197eca22SWill Andrews }
743197eca22SWill Andrews if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
744197eca22SWill Andrews tried_dpcpu = 1;
745197eca22SWill Andrews prefix = DPCPU_SYMPREFIX;
746197eca22SWill Andrews goto again;
747197eca22SWill Andrews }
748197eca22SWill Andrews
749197eca22SWill Andrews /*
750197eca22SWill Andrews * Return the number of entries that weren't found. If they exist,
751197eca22SWill Andrews * also fill internal error buffer.
752197eca22SWill Andrews */
753197eca22SWill Andrews error = ((p - nl) - nvalid);
754197eca22SWill Andrews if (error)
755197eca22SWill Andrews _kvm_syserr(kd, kd->program, "kvm_nlist");
756197eca22SWill Andrews return (error);
757197eca22SWill Andrews }
758c9057838SWill Andrews
759c9057838SWill Andrews int
_kvm_bitmap_init(struct kvm_bitmap * bm,u_long bitmapsize,u_long * idx)7602aa6a4f3SWill Andrews _kvm_bitmap_init(struct kvm_bitmap *bm, u_long bitmapsize, u_long *idx)
761c9057838SWill Andrews {
762c9057838SWill Andrews
7632aa6a4f3SWill Andrews *idx = ULONG_MAX;
764c9057838SWill Andrews bm->map = calloc(bitmapsize, sizeof *bm->map);
765c9057838SWill Andrews if (bm->map == NULL)
766c9057838SWill Andrews return (0);
767c9057838SWill Andrews bm->size = bitmapsize;
768c9057838SWill Andrews return (1);
769c9057838SWill Andrews }
770c9057838SWill Andrews
771c9057838SWill Andrews void
_kvm_bitmap_set(struct kvm_bitmap * bm,u_long bm_index)77200e66147SD Scott Phillips _kvm_bitmap_set(struct kvm_bitmap *bm, u_long bm_index)
773c9057838SWill Andrews {
774c9057838SWill Andrews uint8_t *byte = &bm->map[bm_index / 8];
775c9057838SWill Andrews
77600e66147SD Scott Phillips if (bm_index / 8 < bm->size)
777c9057838SWill Andrews *byte |= (1UL << (bm_index % 8));
778c9057838SWill Andrews }
779c9057838SWill Andrews
780c9057838SWill Andrews int
_kvm_bitmap_next(struct kvm_bitmap * bm,u_long * idx)7812aa6a4f3SWill Andrews _kvm_bitmap_next(struct kvm_bitmap *bm, u_long *idx)
782c9057838SWill Andrews {
783c9057838SWill Andrews u_long first_invalid = bm->size * CHAR_BIT;
784c9057838SWill Andrews
7852aa6a4f3SWill Andrews if (*idx == ULONG_MAX)
7862aa6a4f3SWill Andrews *idx = 0;
787c9057838SWill Andrews else
7882aa6a4f3SWill Andrews (*idx)++;
789c9057838SWill Andrews
7902aa6a4f3SWill Andrews /* Find the next valid idx. */
7912aa6a4f3SWill Andrews for (; *idx < first_invalid; (*idx)++) {
792*08055452SBora Özarslan unsigned int mask = 1U << (*idx % CHAR_BIT);
793*08055452SBora Özarslan if ((bm->map[*idx / CHAR_BIT] & mask) != 0)
794c9057838SWill Andrews break;
795c9057838SWill Andrews }
796c9057838SWill Andrews
7972aa6a4f3SWill Andrews return (*idx < first_invalid);
798c9057838SWill Andrews }
799c9057838SWill Andrews
800c9057838SWill Andrews void
_kvm_bitmap_deinit(struct kvm_bitmap * bm)801c9057838SWill Andrews _kvm_bitmap_deinit(struct kvm_bitmap *bm)
802c9057838SWill Andrews {
803c9057838SWill Andrews
804c9057838SWill Andrews free(bm->map);
805c9057838SWill Andrews }
806c9057838SWill Andrews
807c9057838SWill Andrews int
_kvm_visit_cb(kvm_t * kd,kvm_walk_pages_cb_t * cb,void * arg,u_long pa,u_long kmap_vaddr,u_long dmap_vaddr,vm_prot_t prot,size_t len,unsigned int page_size)808c9057838SWill Andrews _kvm_visit_cb(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg, u_long pa,
809c9057838SWill Andrews u_long kmap_vaddr, u_long dmap_vaddr, vm_prot_t prot, size_t len,
810c9057838SWill Andrews unsigned int page_size)
811c9057838SWill Andrews {
812c9057838SWill Andrews unsigned int pgsz = page_size ? page_size : len;
813c9057838SWill Andrews struct kvm_page p = {
8149dc7ed62SEd Maste .kp_version = LIBKVM_WALK_PAGES_VERSION,
8159dc7ed62SEd Maste .kp_paddr = pa,
8169dc7ed62SEd Maste .kp_kmap_vaddr = kmap_vaddr,
8179dc7ed62SEd Maste .kp_dmap_vaddr = dmap_vaddr,
8189dc7ed62SEd Maste .kp_prot = prot,
8199dc7ed62SEd Maste .kp_offset = _kvm_pt_find(kd, pa, pgsz),
8209dc7ed62SEd Maste .kp_len = len,
821c9057838SWill Andrews };
822c9057838SWill Andrews
823c9057838SWill Andrews return cb(&p, arg);
824c9057838SWill Andrews }
825