1 /*-
2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/fnv_hash.h>
39
40 #define _WANT_VNET
41
42 #include <sys/user.h>
43 #include <sys/linker.h>
44 #include <sys/pcpu.h>
45 #include <sys/stat.h>
46 #include <sys/mman.h>
47
48 #include <net/vnet.h>
49
50 #include <assert.h>
51 #include <fcntl.h>
52 #include <vm/vm.h>
53 #include <kvm.h>
54 #include <limits.h>
55 #include <paths.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <stdarg.h>
62 #include <inttypes.h>
63
64 #include "kvm_private.h"
65
66 /*
67 * Routines private to libkvm.
68 */
69
70 /* from src/lib/libc/gen/nlist.c */
71 int __fdnlist(int, struct nlist *);
72
73 /*
74 * Report an error using printf style arguments. "program" is kd->program
75 * on hard errors, and 0 on soft errors, so that under sun error emulation,
76 * only hard errors are printed out (otherwise, programs like gdb will
77 * generate tons of error messages when trying to access bogus pointers).
78 */
79 void
_kvm_err(kvm_t * kd,const char * program,const char * fmt,...)80 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
81 {
82 va_list ap;
83
84 va_start(ap, fmt);
85 if (program != NULL) {
86 (void)fprintf(stderr, "%s: ", program);
87 (void)vfprintf(stderr, fmt, ap);
88 (void)fputc('\n', stderr);
89 } else
90 (void)vsnprintf(kd->errbuf,
91 sizeof(kd->errbuf), fmt, ap);
92
93 va_end(ap);
94 }
95
96 void
_kvm_syserr(kvm_t * kd,const char * program,const char * fmt,...)97 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
98 {
99 va_list ap;
100 int n;
101
102 va_start(ap, fmt);
103 if (program != NULL) {
104 (void)fprintf(stderr, "%s: ", program);
105 (void)vfprintf(stderr, fmt, ap);
106 (void)fprintf(stderr, ": %s\n", strerror(errno));
107 } else {
108 char *cp = kd->errbuf;
109
110 (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
111 n = strlen(cp);
112 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
113 strerror(errno));
114 }
115 va_end(ap);
116 }
117
118 void *
_kvm_malloc(kvm_t * kd,size_t n)119 _kvm_malloc(kvm_t *kd, size_t n)
120 {
121 void *p;
122
123 if ((p = calloc(n, sizeof(char))) == NULL)
124 _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
125 n, strerror(errno));
126 return (p);
127 }
128
129 int
_kvm_probe_elf_kernel(kvm_t * kd,int class,int machine)130 _kvm_probe_elf_kernel(kvm_t *kd, int class, int machine)
131 {
132
133 return (kd->nlehdr.e_ident[EI_CLASS] == class &&
134 kd->nlehdr.e_type == ET_EXEC &&
135 kd->nlehdr.e_machine == machine);
136 }
137
138 int
_kvm_is_minidump(kvm_t * kd)139 _kvm_is_minidump(kvm_t *kd)
140 {
141 char minihdr[8];
142
143 if (kd->rawdump)
144 return (0);
145 if (pread(kd->pmfd, &minihdr, 8, 0) == 8 &&
146 memcmp(&minihdr, "minidump", 8) == 0)
147 return (1);
148 return (0);
149 }
150
151 /*
152 * The powerpc backend has a hack to strip a leading kerneldump
153 * header from the core before treating it as an ELF header.
154 *
155 * We can add that here if we can get a change to libelf to support
156 * an initial offset into the file. Alternatively we could patch
157 * savecore to extract cores from a regular file instead.
158 */
159 int
_kvm_read_core_phdrs(kvm_t * kd,size_t * phnump,GElf_Phdr ** phdrp)160 _kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp)
161 {
162 GElf_Ehdr ehdr;
163 GElf_Phdr *phdr;
164 Elf *elf;
165 size_t i, phnum;
166
167 elf = elf_begin(kd->pmfd, ELF_C_READ, NULL);
168 if (elf == NULL) {
169 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
170 return (-1);
171 }
172 if (elf_kind(elf) != ELF_K_ELF) {
173 _kvm_err(kd, kd->program, "invalid core");
174 goto bad;
175 }
176 if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) {
177 _kvm_err(kd, kd->program, "invalid core");
178 goto bad;
179 }
180 if (gelf_getehdr(elf, &ehdr) == NULL) {
181 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
182 goto bad;
183 }
184 if (ehdr.e_type != ET_CORE) {
185 _kvm_err(kd, kd->program, "invalid core");
186 goto bad;
187 }
188 if (ehdr.e_machine != kd->nlehdr.e_machine) {
189 _kvm_err(kd, kd->program, "invalid core");
190 goto bad;
191 }
192
193 if (elf_getphdrnum(elf, &phnum) == -1) {
194 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
195 goto bad;
196 }
197
198 phdr = calloc(phnum, sizeof(*phdr));
199 if (phdr == NULL) {
200 _kvm_err(kd, kd->program, "failed to allocate phdrs");
201 goto bad;
202 }
203
204 for (i = 0; i < phnum; i++) {
205 if (gelf_getphdr(elf, i, &phdr[i]) == NULL) {
206 free(phdr);
207 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
208 goto bad;
209 }
210 }
211 elf_end(elf);
212 *phnump = phnum;
213 *phdrp = phdr;
214 return (0);
215
216 bad:
217 elf_end(elf);
218 return (-1);
219 }
220
221 /*
222 * Transform v such that only bits [bit0, bitN) may be set. Generates a
223 * bitmask covering the number of bits, then shifts so +bit0+ is the first.
224 */
225 static uint64_t
bitmask_range(uint64_t v,uint64_t bit0,uint64_t bitN)226 bitmask_range(uint64_t v, uint64_t bit0, uint64_t bitN)
227 {
228 if (bit0 == 0 && bitN == BITS_IN(v))
229 return (v);
230
231 return (v & (((1ULL << (bitN - bit0)) - 1ULL) << bit0));
232 }
233
234 /*
235 * Returns the number of bits in a given byte array range starting at a
236 * given base, from bit0 to bitN. bit0 may be non-zero in the case of
237 * counting backwards from bitN.
238 */
239 static uint64_t
popcount_bytes(uint64_t * addr,uint32_t bit0,uint32_t bitN)240 popcount_bytes(uint64_t *addr, uint32_t bit0, uint32_t bitN)
241 {
242 uint32_t res = bitN - bit0;
243 uint64_t count = 0;
244 uint32_t bound;
245
246 /* Align to 64-bit boundary on the left side if needed. */
247 if ((bit0 % BITS_IN(*addr)) != 0) {
248 bound = MIN(bitN, roundup2(bit0, BITS_IN(*addr)));
249 count += __bitcount64(bitmask_range(*addr, bit0, bound));
250 res -= (bound - bit0);
251 addr++;
252 }
253
254 while (res > 0) {
255 bound = MIN(res, BITS_IN(*addr));
256 count += __bitcount64(bitmask_range(*addr, 0, bound));
257 res -= bound;
258 addr++;
259 }
260
261 return (count);
262 }
263
264 void *
_kvm_pmap_get(kvm_t * kd,u_long idx,size_t len)265 _kvm_pmap_get(kvm_t *kd, u_long idx, size_t len)
266 {
267 uintptr_t off = idx * len;
268
269 if ((off_t)off >= kd->pt_sparse_off)
270 return (NULL);
271 return (void *)((uintptr_t)kd->page_map + off);
272 }
273
274 void *
_kvm_map_get(kvm_t * kd,u_long pa,unsigned int page_size)275 _kvm_map_get(kvm_t *kd, u_long pa, unsigned int page_size)
276 {
277 off_t off;
278 uintptr_t addr;
279
280 off = _kvm_pt_find(kd, pa, page_size);
281 if (off == -1)
282 return NULL;
283
284 addr = (uintptr_t)kd->page_map + off;
285 if (off >= kd->pt_sparse_off)
286 addr = (uintptr_t)kd->sparse_map + (off - kd->pt_sparse_off);
287 return (void *)addr;
288 }
289
290 int
_kvm_pt_init(kvm_t * kd,size_t map_len,off_t map_off,off_t sparse_off,int page_size,int word_size)291 _kvm_pt_init(kvm_t *kd, size_t map_len, off_t map_off, off_t sparse_off,
292 int page_size, int word_size)
293 {
294 uint64_t *addr;
295 uint32_t *popcount_bin;
296 int bin_popcounts = 0;
297 uint64_t pc_bins, res;
298 ssize_t rd;
299
300 /*
301 * Map the bitmap specified by the arguments.
302 */
303 kd->pt_map = _kvm_malloc(kd, map_len);
304 if (kd->pt_map == NULL) {
305 _kvm_err(kd, kd->program, "cannot allocate %zu bytes for bitmap",
306 map_len);
307 return (-1);
308 }
309 rd = pread(kd->pmfd, kd->pt_map, map_len, map_off);
310 if (rd < 0 || rd != (ssize_t)map_len) {
311 _kvm_err(kd, kd->program, "cannot read %zu bytes for bitmap",
312 map_len);
313 return (-1);
314 }
315 kd->pt_map_size = map_len;
316
317 /*
318 * Generate a popcount cache for every POPCOUNT_BITS in the bitmap,
319 * so lookups only have to calculate the number of bits set between
320 * a cache point and their bit. This reduces lookups to O(1),
321 * without significantly increasing memory requirements.
322 *
323 * Round up the number of bins so that 'upper half' lookups work for
324 * the final bin, if needed. The first popcount is 0, since no bits
325 * precede bit 0, so add 1 for that also. Without this, extra work
326 * would be needed to handle the first PTEs in _kvm_pt_find().
327 */
328 addr = kd->pt_map;
329 res = map_len;
330 pc_bins = 1 + (res * NBBY + POPCOUNT_BITS / 2) / POPCOUNT_BITS;
331 kd->pt_popcounts = calloc(pc_bins, sizeof(uint32_t));
332 if (kd->pt_popcounts == NULL) {
333 _kvm_err(kd, kd->program, "cannot allocate popcount bins");
334 return (-1);
335 }
336
337 for (popcount_bin = &kd->pt_popcounts[1]; res > 0;
338 addr++, res -= sizeof(*addr)) {
339 *popcount_bin += popcount_bytes(addr, 0,
340 MIN(res * NBBY, BITS_IN(*addr)));
341 if (++bin_popcounts == POPCOUNTS_IN(*addr)) {
342 popcount_bin++;
343 *popcount_bin = *(popcount_bin - 1);
344 bin_popcounts = 0;
345 }
346 }
347
348 assert(pc_bins * sizeof(*popcount_bin) ==
349 ((uintptr_t)popcount_bin - (uintptr_t)kd->pt_popcounts));
350
351 kd->pt_sparse_off = sparse_off;
352 kd->pt_sparse_size = (uint64_t)*popcount_bin * page_size;
353 kd->pt_page_size = page_size;
354 kd->pt_word_size = word_size;
355
356 /*
357 * Map the sparse page array. This is useful for performing point
358 * lookups of specific pages, e.g. for kvm_walk_pages. Generally,
359 * this is much larger than is reasonable to read in up front, so
360 * mmap it in instead.
361 */
362 kd->sparse_map = mmap(NULL, kd->pt_sparse_size, PROT_READ,
363 MAP_PRIVATE, kd->pmfd, kd->pt_sparse_off);
364 if (kd->sparse_map == MAP_FAILED) {
365 _kvm_err(kd, kd->program, "cannot map %" PRIu64
366 " bytes from fd %d offset %jd for sparse map: %s",
367 kd->pt_sparse_size, kd->pmfd,
368 (intmax_t)kd->pt_sparse_off, strerror(errno));
369 return (-1);
370 }
371 return (0);
372 }
373
374 int
_kvm_pmap_init(kvm_t * kd,uint32_t pmap_size,off_t pmap_off)375 _kvm_pmap_init(kvm_t *kd, uint32_t pmap_size, off_t pmap_off)
376 {
377 ssize_t exp_len = pmap_size;
378
379 kd->page_map_size = pmap_size;
380 kd->page_map_off = pmap_off;
381 kd->page_map = _kvm_malloc(kd, pmap_size);
382 if (kd->page_map == NULL) {
383 _kvm_err(kd, kd->program, "cannot allocate %u bytes "
384 "for page map", pmap_size);
385 return (-1);
386 }
387 if (pread(kd->pmfd, kd->page_map, pmap_size, pmap_off) != exp_len) {
388 _kvm_err(kd, kd->program, "cannot read %d bytes from "
389 "offset %jd for page map", pmap_size, (intmax_t)pmap_off);
390 return (-1);
391 }
392 return (0);
393 }
394
395 /*
396 * Find the offset for the given physical page address; returns -1 otherwise.
397 *
398 * A page's offset is represented by the sparse page base offset plus the
399 * number of bits set before its bit multiplied by page size. This means
400 * that if a page exists in the dump, it's necessary to know how many pages
401 * in the dump precede it. Reduce this O(n) counting to O(1) by caching the
402 * number of bits set at POPCOUNT_BITS intervals.
403 *
404 * Then to find the number of pages before the requested address, simply
405 * index into the cache and count the number of bits set between that cache
406 * bin and the page's bit. Halve the number of bytes that have to be
407 * checked by also counting down from the next higher bin if it's closer.
408 */
409 off_t
_kvm_pt_find(kvm_t * kd,uint64_t pa,unsigned int page_size)410 _kvm_pt_find(kvm_t *kd, uint64_t pa, unsigned int page_size)
411 {
412 uint64_t *bitmap = kd->pt_map;
413 uint64_t pte_bit_id = pa / page_size;
414 uint64_t pte_u64 = pte_bit_id / BITS_IN(*bitmap);
415 uint64_t popcount_id = pte_bit_id / POPCOUNT_BITS;
416 uint64_t pte_mask = 1ULL << (pte_bit_id % BITS_IN(*bitmap));
417 uint64_t bitN;
418 uint32_t count;
419
420 /* Check whether the page address requested is in the dump. */
421 if (pte_bit_id >= (kd->pt_map_size * NBBY) ||
422 (bitmap[pte_u64] & pte_mask) == 0)
423 return (-1);
424
425 /*
426 * Add/sub popcounts from the bitmap until the PTE's bit is reached.
427 * For bits that are in the upper half between the calculated
428 * popcount id and the next one, use the next one and subtract to
429 * minimize the number of popcounts required.
430 */
431 if ((pte_bit_id % POPCOUNT_BITS) < (POPCOUNT_BITS / 2)) {
432 count = kd->pt_popcounts[popcount_id] + popcount_bytes(
433 bitmap + popcount_id * POPCOUNTS_IN(*bitmap),
434 0, pte_bit_id - popcount_id * POPCOUNT_BITS);
435 } else {
436 /*
437 * Counting in reverse is trickier, since we must avoid
438 * reading from bytes that are not in range, and invert.
439 */
440 uint64_t pte_u64_bit_off = pte_u64 * BITS_IN(*bitmap);
441
442 popcount_id++;
443 bitN = MIN(popcount_id * POPCOUNT_BITS,
444 kd->pt_map_size * BITS_IN(uint8_t));
445 count = kd->pt_popcounts[popcount_id] - popcount_bytes(
446 bitmap + pte_u64,
447 pte_bit_id - pte_u64_bit_off, bitN - pte_u64_bit_off);
448 }
449
450 /*
451 * This can only happen if the core is truncated. Treat these
452 * entries as if they don't exist, since their backing doesn't.
453 */
454 if (count >= (kd->pt_sparse_size / page_size))
455 return (-1);
456
457 return (kd->pt_sparse_off + (uint64_t)count * page_size);
458 }
459
460 static int
kvm_fdnlist(kvm_t * kd,struct kvm_nlist * list)461 kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list)
462 {
463 kvaddr_t addr;
464 int error, nfail;
465
466 if (kd->resolve_symbol == NULL) {
467 struct nlist *nl;
468 int count, i;
469
470 for (count = 0; list[count].n_name != NULL &&
471 list[count].n_name[0] != '\0'; count++)
472 ;
473 nl = calloc(count + 1, sizeof(*nl));
474 for (i = 0; i < count; i++)
475 nl[i].n_name = list[i].n_name;
476 nfail = __fdnlist(kd->nlfd, nl);
477 for (i = 0; i < count; i++) {
478 list[i].n_type = nl[i].n_type;
479 list[i].n_value = nl[i].n_value;
480 }
481 free(nl);
482 return (nfail);
483 }
484
485 nfail = 0;
486 while (list->n_name != NULL && list->n_name[0] != '\0') {
487 error = kd->resolve_symbol(list->n_name, &addr);
488 if (error != 0) {
489 nfail++;
490 list->n_value = 0;
491 list->n_type = 0;
492 } else {
493 list->n_value = addr;
494 list->n_type = N_DATA | N_EXT;
495 }
496 list++;
497 }
498 return (nfail);
499 }
500
501 /*
502 * Walk the list of unresolved symbols, generate a new list and prefix the
503 * symbol names, try again, and merge back what we could resolve.
504 */
505 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))506 kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing,
507 const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t))
508 {
509 struct kvm_nlist *n, *np, *p;
510 char *cp, *ce;
511 const char *ccp;
512 size_t len;
513 int slen, unresolved;
514
515 /*
516 * Calculate the space we need to malloc for nlist and names.
517 * We are going to store the name twice for later lookups: once
518 * with the prefix and once the unmodified name delmited by \0.
519 */
520 len = 0;
521 unresolved = 0;
522 for (p = nl; p->n_name && p->n_name[0]; ++p) {
523 if (p->n_type != N_UNDF)
524 continue;
525 len += sizeof(struct kvm_nlist) + strlen(prefix) +
526 2 * (strlen(p->n_name) + 1);
527 unresolved++;
528 }
529 if (unresolved == 0)
530 return (unresolved);
531 /* Add space for the terminating nlist entry. */
532 len += sizeof(struct kvm_nlist);
533 unresolved++;
534
535 /* Alloc one chunk for (nlist, [names]) and setup pointers. */
536 n = np = malloc(len);
537 bzero(n, len);
538 if (n == NULL)
539 return (missing);
540 cp = ce = (char *)np;
541 cp += unresolved * sizeof(struct kvm_nlist);
542 ce += len;
543
544 /* Generate shortened nlist with special prefix. */
545 unresolved = 0;
546 for (p = nl; p->n_name && p->n_name[0]; ++p) {
547 if (p->n_type != N_UNDF)
548 continue;
549 *np = *p;
550 /* Save the new\0orig. name so we can later match it again. */
551 slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
552 (prefix[0] != '\0' && p->n_name[0] == '_') ?
553 (p->n_name + 1) : p->n_name, '\0', p->n_name);
554 if (slen < 0 || slen >= ce - cp)
555 continue;
556 np->n_name = cp;
557 cp += slen + 1;
558 np++;
559 unresolved++;
560 }
561
562 /* Do lookup on the reduced list. */
563 np = n;
564 unresolved = kvm_fdnlist(kd, np);
565
566 /* Check if we could resolve further symbols and update the list. */
567 if (unresolved >= 0 && unresolved < missing) {
568 /* Find the first freshly resolved entry. */
569 for (; np->n_name && np->n_name[0]; np++)
570 if (np->n_type != N_UNDF)
571 break;
572 /*
573 * The lists are both in the same order,
574 * so we can walk them in parallel.
575 */
576 for (p = nl; np->n_name && np->n_name[0] &&
577 p->n_name && p->n_name[0]; ++p) {
578 if (p->n_type != N_UNDF)
579 continue;
580 /* Skip expanded name and compare to orig. one. */
581 ccp = np->n_name + strlen(np->n_name) + 1;
582 if (strcmp(ccp, p->n_name) != 0)
583 continue;
584 /* Update nlist with new, translated results. */
585 p->n_type = np->n_type;
586 if (validate_fn)
587 p->n_value = (*validate_fn)(kd, np->n_value);
588 else
589 p->n_value = np->n_value;
590 missing--;
591 /* Find next freshly resolved entry. */
592 for (np++; np->n_name && np->n_name[0]; np++)
593 if (np->n_type != N_UNDF)
594 break;
595 }
596 }
597 /* We could assert missing = unresolved here. */
598
599 free(n);
600 return (unresolved);
601 }
602
603 int
_kvm_nlist(kvm_t * kd,struct kvm_nlist * nl,int initialize)604 _kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize)
605 {
606 struct kvm_nlist *p;
607 int nvalid;
608 struct kld_sym_lookup lookup;
609 int error;
610 const char *prefix = "";
611 char symname[1024]; /* XXX-BZ symbol name length limit? */
612 int tried_vnet, tried_dpcpu;
613
614 /*
615 * If we can't use the kld symbol lookup, revert to the
616 * slow library call.
617 */
618 if (!ISALIVE(kd)) {
619 error = kvm_fdnlist(kd, nl);
620 if (error <= 0) /* Hard error or success. */
621 return (error);
622
623 if (_kvm_vnet_initialized(kd, initialize))
624 error = kvm_fdnlist_prefix(kd, nl, error,
625 VNET_SYMPREFIX, _kvm_vnet_validaddr);
626
627 if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
628 error = kvm_fdnlist_prefix(kd, nl, error,
629 DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
630
631 return (error);
632 }
633
634 /*
635 * We can use the kld lookup syscall. Go through each nlist entry
636 * and look it up with a kldsym(2) syscall.
637 */
638 nvalid = 0;
639 tried_vnet = 0;
640 tried_dpcpu = 0;
641 again:
642 for (p = nl; p->n_name && p->n_name[0]; ++p) {
643 if (p->n_type != N_UNDF)
644 continue;
645
646 lookup.version = sizeof(lookup);
647 lookup.symvalue = 0;
648 lookup.symsize = 0;
649
650 error = snprintf(symname, sizeof(symname), "%s%s", prefix,
651 (prefix[0] != '\0' && p->n_name[0] == '_') ?
652 (p->n_name + 1) : p->n_name);
653 if (error < 0 || error >= (int)sizeof(symname))
654 continue;
655 lookup.symname = symname;
656 if (lookup.symname[0] == '_')
657 lookup.symname++;
658
659 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
660 p->n_type = N_TEXT;
661 if (_kvm_vnet_initialized(kd, initialize) &&
662 strcmp(prefix, VNET_SYMPREFIX) == 0)
663 p->n_value =
664 _kvm_vnet_validaddr(kd, lookup.symvalue);
665 else if (_kvm_dpcpu_initialized(kd, initialize) &&
666 strcmp(prefix, DPCPU_SYMPREFIX) == 0)
667 p->n_value =
668 _kvm_dpcpu_validaddr(kd, lookup.symvalue);
669 else
670 p->n_value = lookup.symvalue;
671 ++nvalid;
672 /* lookup.symsize */
673 }
674 }
675
676 /*
677 * Check the number of entries that weren't found. If they exist,
678 * try again with a prefix for virtualized or DPCPU symbol names.
679 */
680 error = ((p - nl) - nvalid);
681 if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
682 tried_vnet = 1;
683 prefix = VNET_SYMPREFIX;
684 goto again;
685 }
686 if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
687 tried_dpcpu = 1;
688 prefix = DPCPU_SYMPREFIX;
689 goto again;
690 }
691
692 /*
693 * Return the number of entries that weren't found. If they exist,
694 * also fill internal error buffer.
695 */
696 error = ((p - nl) - nvalid);
697 if (error)
698 _kvm_syserr(kd, kd->program, "kvm_nlist");
699 return (error);
700 }
701
702 int
_kvm_bitmap_init(struct kvm_bitmap * bm,u_long bitmapsize,u_long * idx)703 _kvm_bitmap_init(struct kvm_bitmap *bm, u_long bitmapsize, u_long *idx)
704 {
705
706 *idx = ULONG_MAX;
707 bm->map = calloc(bitmapsize, sizeof *bm->map);
708 if (bm->map == NULL)
709 return (0);
710 bm->size = bitmapsize;
711 return (1);
712 }
713
714 void
_kvm_bitmap_set(struct kvm_bitmap * bm,u_long pa,unsigned int page_size)715 _kvm_bitmap_set(struct kvm_bitmap *bm, u_long pa, unsigned int page_size)
716 {
717 u_long bm_index = pa / page_size;
718 uint8_t *byte = &bm->map[bm_index / 8];
719
720 *byte |= (1UL << (bm_index % 8));
721 }
722
723 int
_kvm_bitmap_next(struct kvm_bitmap * bm,u_long * idx)724 _kvm_bitmap_next(struct kvm_bitmap *bm, u_long *idx)
725 {
726 u_long first_invalid = bm->size * CHAR_BIT;
727
728 if (*idx == ULONG_MAX)
729 *idx = 0;
730 else
731 (*idx)++;
732
733 /* Find the next valid idx. */
734 for (; *idx < first_invalid; (*idx)++) {
735 unsigned int mask = *idx % CHAR_BIT;
736 if ((bm->map[*idx * CHAR_BIT] & mask) == 0)
737 break;
738 }
739
740 return (*idx < first_invalid);
741 }
742
743 void
_kvm_bitmap_deinit(struct kvm_bitmap * bm)744 _kvm_bitmap_deinit(struct kvm_bitmap *bm)
745 {
746
747 free(bm->map);
748 }
749
750 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)751 _kvm_visit_cb(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg, u_long pa,
752 u_long kmap_vaddr, u_long dmap_vaddr, vm_prot_t prot, size_t len,
753 unsigned int page_size)
754 {
755 unsigned int pgsz = page_size ? page_size : len;
756 struct kvm_page p = {
757 .version = LIBKVM_WALK_PAGES_VERSION,
758 .paddr = pa,
759 .kmap_vaddr = kmap_vaddr,
760 .dmap_vaddr = dmap_vaddr,
761 .prot = prot,
762 .offset = _kvm_pt_find(kd, pa, pgsz),
763 .len = len,
764 };
765
766 return cb(&p, arg);
767 }
768