1197eca22SWill Andrews /*- 2197eca22SWill Andrews * Copyright (c) 1989, 1992, 1993 3197eca22SWill Andrews * The Regents of the University of California. All rights reserved. 4197eca22SWill Andrews * 5197eca22SWill Andrews * This code is derived from software developed by the Computer Systems 6197eca22SWill Andrews * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7197eca22SWill Andrews * BG 91-66 and contributed to Berkeley. 8197eca22SWill Andrews * 9197eca22SWill Andrews * Redistribution and use in source and binary forms, with or without 10197eca22SWill Andrews * modification, are permitted provided that the following conditions 11197eca22SWill Andrews * are met: 12197eca22SWill Andrews * 1. Redistributions of source code must retain the above copyright 13197eca22SWill Andrews * notice, this list of conditions and the following disclaimer. 14197eca22SWill Andrews * 2. Redistributions in binary form must reproduce the above copyright 15197eca22SWill Andrews * notice, this list of conditions and the following disclaimer in the 16197eca22SWill Andrews * documentation and/or other materials provided with the distribution. 17fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 18197eca22SWill Andrews * may be used to endorse or promote products derived from this software 19197eca22SWill Andrews * without specific prior written permission. 20197eca22SWill Andrews * 21197eca22SWill Andrews * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22197eca22SWill Andrews * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23197eca22SWill Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24197eca22SWill Andrews * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25197eca22SWill Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26197eca22SWill Andrews * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27197eca22SWill Andrews * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28197eca22SWill Andrews * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29197eca22SWill Andrews * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30197eca22SWill Andrews * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31197eca22SWill Andrews * SUCH DAMAGE. 32197eca22SWill Andrews */ 33197eca22SWill Andrews 34197eca22SWill Andrews #include <sys/cdefs.h> 35197eca22SWill Andrews __FBSDID("$FreeBSD$"); 36197eca22SWill Andrews 37197eca22SWill Andrews #include <sys/param.h> 38197eca22SWill Andrews #include <sys/fnv_hash.h> 39197eca22SWill Andrews 40197eca22SWill Andrews #define _WANT_VNET 41197eca22SWill Andrews 42197eca22SWill Andrews #include <sys/user.h> 43197eca22SWill Andrews #include <sys/linker.h> 44197eca22SWill Andrews #include <sys/pcpu.h> 45197eca22SWill Andrews #include <sys/stat.h> 46c9057838SWill Andrews #include <sys/mman.h> 47197eca22SWill Andrews 48*10108cb6SBjoern A. Zeeb #include <stdbool.h> 49197eca22SWill Andrews #include <net/vnet.h> 50197eca22SWill Andrews 51ffdeef32SWill Andrews #include <assert.h> 52197eca22SWill Andrews #include <fcntl.h> 538baaf913SWill Andrews #include <vm/vm.h> 54197eca22SWill Andrews #include <kvm.h> 55197eca22SWill Andrews #include <limits.h> 56197eca22SWill Andrews #include <paths.h> 57197eca22SWill Andrews #include <stdint.h> 58197eca22SWill Andrews #include <stdio.h> 59197eca22SWill Andrews #include <stdlib.h> 60197eca22SWill Andrews #include <string.h> 61197eca22SWill Andrews #include <unistd.h> 62197eca22SWill Andrews #include <stdarg.h> 63c9057838SWill Andrews #include <inttypes.h> 64197eca22SWill Andrews 65197eca22SWill Andrews #include "kvm_private.h" 66197eca22SWill Andrews 67197eca22SWill Andrews /* 68197eca22SWill Andrews * Routines private to libkvm. 69197eca22SWill Andrews */ 70197eca22SWill Andrews 71197eca22SWill Andrews /* from src/lib/libc/gen/nlist.c */ 72197eca22SWill Andrews int __fdnlist(int, struct nlist *); 73197eca22SWill Andrews 74197eca22SWill Andrews /* 75197eca22SWill Andrews * Report an error using printf style arguments. "program" is kd->program 76197eca22SWill Andrews * on hard errors, and 0 on soft errors, so that under sun error emulation, 77197eca22SWill Andrews * only hard errors are printed out (otherwise, programs like gdb will 78197eca22SWill Andrews * generate tons of error messages when trying to access bogus pointers). 79197eca22SWill Andrews */ 80197eca22SWill Andrews void 81197eca22SWill Andrews _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) 82197eca22SWill Andrews { 83197eca22SWill Andrews va_list ap; 84197eca22SWill Andrews 85197eca22SWill Andrews va_start(ap, fmt); 86197eca22SWill Andrews if (program != NULL) { 87197eca22SWill Andrews (void)fprintf(stderr, "%s: ", program); 88197eca22SWill Andrews (void)vfprintf(stderr, fmt, ap); 89197eca22SWill Andrews (void)fputc('\n', stderr); 90197eca22SWill Andrews } else 91197eca22SWill Andrews (void)vsnprintf(kd->errbuf, 92197eca22SWill Andrews sizeof(kd->errbuf), fmt, ap); 93197eca22SWill Andrews 94197eca22SWill Andrews va_end(ap); 95197eca22SWill Andrews } 96197eca22SWill Andrews 97197eca22SWill Andrews void 98197eca22SWill Andrews _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) 99197eca22SWill Andrews { 100197eca22SWill Andrews va_list ap; 101197eca22SWill Andrews int n; 102197eca22SWill Andrews 103197eca22SWill Andrews va_start(ap, fmt); 104197eca22SWill Andrews if (program != NULL) { 105197eca22SWill Andrews (void)fprintf(stderr, "%s: ", program); 106197eca22SWill Andrews (void)vfprintf(stderr, fmt, ap); 107197eca22SWill Andrews (void)fprintf(stderr, ": %s\n", strerror(errno)); 108197eca22SWill Andrews } else { 109197eca22SWill Andrews char *cp = kd->errbuf; 110197eca22SWill Andrews 111197eca22SWill Andrews (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap); 112197eca22SWill Andrews n = strlen(cp); 113197eca22SWill Andrews (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s", 114197eca22SWill Andrews strerror(errno)); 115197eca22SWill Andrews } 116197eca22SWill Andrews va_end(ap); 117197eca22SWill Andrews } 118197eca22SWill Andrews 119197eca22SWill Andrews void * 120197eca22SWill Andrews _kvm_malloc(kvm_t *kd, size_t n) 121197eca22SWill Andrews { 122197eca22SWill Andrews void *p; 123197eca22SWill Andrews 124197eca22SWill Andrews if ((p = calloc(n, sizeof(char))) == NULL) 125197eca22SWill Andrews _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s", 126197eca22SWill Andrews n, strerror(errno)); 127197eca22SWill Andrews return (p); 128197eca22SWill Andrews } 129197eca22SWill Andrews 130197eca22SWill Andrews int 131197eca22SWill Andrews _kvm_probe_elf_kernel(kvm_t *kd, int class, int machine) 132197eca22SWill Andrews { 133197eca22SWill Andrews 134197eca22SWill Andrews return (kd->nlehdr.e_ident[EI_CLASS] == class && 1358024ba45SLeandro Lupori ((machine == EM_PPC || machine == EM_PPC64) ? 1368024ba45SLeandro Lupori kd->nlehdr.e_type == ET_DYN : kd->nlehdr.e_type == ET_EXEC) && 137197eca22SWill Andrews kd->nlehdr.e_machine == machine); 138197eca22SWill Andrews } 139197eca22SWill Andrews 140197eca22SWill Andrews int 141197eca22SWill Andrews _kvm_is_minidump(kvm_t *kd) 142197eca22SWill Andrews { 143197eca22SWill Andrews char minihdr[8]; 144197eca22SWill Andrews 145197eca22SWill Andrews if (kd->rawdump) 146197eca22SWill Andrews return (0); 147197eca22SWill Andrews if (pread(kd->pmfd, &minihdr, 8, 0) == 8 && 148197eca22SWill Andrews memcmp(&minihdr, "minidump", 8) == 0) 149197eca22SWill Andrews return (1); 150197eca22SWill Andrews return (0); 151197eca22SWill Andrews } 152197eca22SWill Andrews 153197eca22SWill Andrews /* 154197eca22SWill Andrews * The powerpc backend has a hack to strip a leading kerneldump 155197eca22SWill Andrews * header from the core before treating it as an ELF header. 156197eca22SWill Andrews * 157197eca22SWill Andrews * We can add that here if we can get a change to libelf to support 158197eca22SWill Andrews * an initial offset into the file. Alternatively we could patch 159197eca22SWill Andrews * savecore to extract cores from a regular file instead. 160197eca22SWill Andrews */ 161197eca22SWill Andrews int 162197eca22SWill Andrews _kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp) 163197eca22SWill Andrews { 164197eca22SWill Andrews GElf_Ehdr ehdr; 165197eca22SWill Andrews GElf_Phdr *phdr; 166197eca22SWill Andrews Elf *elf; 167197eca22SWill Andrews size_t i, phnum; 168197eca22SWill Andrews 169197eca22SWill Andrews elf = elf_begin(kd->pmfd, ELF_C_READ, NULL); 170197eca22SWill Andrews if (elf == NULL) { 171197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 172197eca22SWill Andrews return (-1); 173197eca22SWill Andrews } 174197eca22SWill Andrews if (elf_kind(elf) != ELF_K_ELF) { 175197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 176197eca22SWill Andrews goto bad; 177197eca22SWill Andrews } 178197eca22SWill Andrews if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) { 179197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 180197eca22SWill Andrews goto bad; 181197eca22SWill Andrews } 182197eca22SWill Andrews if (gelf_getehdr(elf, &ehdr) == NULL) { 183197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 184197eca22SWill Andrews goto bad; 185197eca22SWill Andrews } 186197eca22SWill Andrews if (ehdr.e_type != ET_CORE) { 187197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 188197eca22SWill Andrews goto bad; 189197eca22SWill Andrews } 190197eca22SWill Andrews if (ehdr.e_machine != kd->nlehdr.e_machine) { 191197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 192197eca22SWill Andrews goto bad; 193197eca22SWill Andrews } 194197eca22SWill Andrews 195197eca22SWill Andrews if (elf_getphdrnum(elf, &phnum) == -1) { 196197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 197197eca22SWill Andrews goto bad; 198197eca22SWill Andrews } 199197eca22SWill Andrews 200197eca22SWill Andrews phdr = calloc(phnum, sizeof(*phdr)); 201197eca22SWill Andrews if (phdr == NULL) { 202197eca22SWill Andrews _kvm_err(kd, kd->program, "failed to allocate phdrs"); 203197eca22SWill Andrews goto bad; 204197eca22SWill Andrews } 205197eca22SWill Andrews 206197eca22SWill Andrews for (i = 0; i < phnum; i++) { 207197eca22SWill Andrews if (gelf_getphdr(elf, i, &phdr[i]) == NULL) { 208f6080aabSGleb Smirnoff free(phdr); 209197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 210197eca22SWill Andrews goto bad; 211197eca22SWill Andrews } 212197eca22SWill Andrews } 213197eca22SWill Andrews elf_end(elf); 214197eca22SWill Andrews *phnump = phnum; 215197eca22SWill Andrews *phdrp = phdr; 216197eca22SWill Andrews return (0); 217197eca22SWill Andrews 218197eca22SWill Andrews bad: 219197eca22SWill Andrews elf_end(elf); 220197eca22SWill Andrews return (-1); 221197eca22SWill Andrews } 222197eca22SWill Andrews 223ffdeef32SWill Andrews /* 224ffdeef32SWill Andrews * Transform v such that only bits [bit0, bitN) may be set. Generates a 225ffdeef32SWill Andrews * bitmask covering the number of bits, then shifts so +bit0+ is the first. 226ffdeef32SWill Andrews */ 227ffdeef32SWill Andrews static uint64_t 228ffdeef32SWill Andrews bitmask_range(uint64_t v, uint64_t bit0, uint64_t bitN) 229197eca22SWill Andrews { 230ffdeef32SWill Andrews if (bit0 == 0 && bitN == BITS_IN(v)) 231ffdeef32SWill Andrews return (v); 232197eca22SWill Andrews 233ffdeef32SWill Andrews return (v & (((1ULL << (bitN - bit0)) - 1ULL) << bit0)); 234197eca22SWill Andrews } 235197eca22SWill Andrews 236ffdeef32SWill Andrews /* 237ffdeef32SWill Andrews * Returns the number of bits in a given byte array range starting at a 238ffdeef32SWill Andrews * given base, from bit0 to bitN. bit0 may be non-zero in the case of 239ffdeef32SWill Andrews * counting backwards from bitN. 240ffdeef32SWill Andrews */ 241ffdeef32SWill Andrews static uint64_t 242ffdeef32SWill Andrews popcount_bytes(uint64_t *addr, uint32_t bit0, uint32_t bitN) 243ffdeef32SWill Andrews { 244ffdeef32SWill Andrews uint32_t res = bitN - bit0; 245ffdeef32SWill Andrews uint64_t count = 0; 246ffdeef32SWill Andrews uint32_t bound; 247ffdeef32SWill Andrews 248ffdeef32SWill Andrews /* Align to 64-bit boundary on the left side if needed. */ 249ffdeef32SWill Andrews if ((bit0 % BITS_IN(*addr)) != 0) { 250ffdeef32SWill Andrews bound = MIN(bitN, roundup2(bit0, BITS_IN(*addr))); 251ffdeef32SWill Andrews count += __bitcount64(bitmask_range(*addr, bit0, bound)); 252ffdeef32SWill Andrews res -= (bound - bit0); 253ffdeef32SWill Andrews addr++; 254ffdeef32SWill Andrews } 255ffdeef32SWill Andrews 256ffdeef32SWill Andrews while (res > 0) { 257ffdeef32SWill Andrews bound = MIN(res, BITS_IN(*addr)); 258ffdeef32SWill Andrews count += __bitcount64(bitmask_range(*addr, 0, bound)); 259ffdeef32SWill Andrews res -= bound; 260ffdeef32SWill Andrews addr++; 261ffdeef32SWill Andrews } 262ffdeef32SWill Andrews 263ffdeef32SWill Andrews return (count); 264ffdeef32SWill Andrews } 265ffdeef32SWill Andrews 266c9057838SWill Andrews void * 2672aa6a4f3SWill Andrews _kvm_pmap_get(kvm_t *kd, u_long idx, size_t len) 268c9057838SWill Andrews { 2698baaf913SWill Andrews uintptr_t off = idx * len; 270c9057838SWill Andrews 2718baaf913SWill Andrews if ((off_t)off >= kd->pt_sparse_off) 272c9057838SWill Andrews return (NULL); 273c9057838SWill Andrews return (void *)((uintptr_t)kd->page_map + off); 274c9057838SWill Andrews } 275c9057838SWill Andrews 276c9057838SWill Andrews void * 277c9057838SWill Andrews _kvm_map_get(kvm_t *kd, u_long pa, unsigned int page_size) 278c9057838SWill Andrews { 279c9057838SWill Andrews off_t off; 280c9057838SWill Andrews uintptr_t addr; 281c9057838SWill Andrews 282c9057838SWill Andrews off = _kvm_pt_find(kd, pa, page_size); 283c9057838SWill Andrews if (off == -1) 284c9057838SWill Andrews return NULL; 285c9057838SWill Andrews 286c9057838SWill Andrews addr = (uintptr_t)kd->page_map + off; 287c9057838SWill Andrews if (off >= kd->pt_sparse_off) 288c9057838SWill Andrews addr = (uintptr_t)kd->sparse_map + (off - kd->pt_sparse_off); 289c9057838SWill Andrews return (void *)addr; 290c9057838SWill Andrews } 291c9057838SWill Andrews 292ffdeef32SWill Andrews int 293ffdeef32SWill Andrews _kvm_pt_init(kvm_t *kd, size_t map_len, off_t map_off, off_t sparse_off, 294197eca22SWill Andrews int page_size, int word_size) 295197eca22SWill Andrews { 296ffdeef32SWill Andrews uint64_t *addr; 297ffdeef32SWill Andrews uint32_t *popcount_bin; 298ffdeef32SWill Andrews int bin_popcounts = 0; 299ffdeef32SWill Andrews uint64_t pc_bins, res; 300ffdeef32SWill Andrews ssize_t rd; 301197eca22SWill Andrews 302ffdeef32SWill Andrews /* 303ffdeef32SWill Andrews * Map the bitmap specified by the arguments. 304ffdeef32SWill Andrews */ 305ffdeef32SWill Andrews kd->pt_map = _kvm_malloc(kd, map_len); 306ffdeef32SWill Andrews if (kd->pt_map == NULL) { 307ffdeef32SWill Andrews _kvm_err(kd, kd->program, "cannot allocate %zu bytes for bitmap", 308ffdeef32SWill Andrews map_len); 309197eca22SWill Andrews return (-1); 310197eca22SWill Andrews } 311ffdeef32SWill Andrews rd = pread(kd->pmfd, kd->pt_map, map_len, map_off); 312ffdeef32SWill Andrews if (rd < 0 || rd != (ssize_t)map_len) { 313ffdeef32SWill Andrews _kvm_err(kd, kd->program, "cannot read %zu bytes for bitmap", 314ffdeef32SWill Andrews map_len); 315ffdeef32SWill Andrews return (-1); 316ffdeef32SWill Andrews } 317ffdeef32SWill Andrews kd->pt_map_size = map_len; 318197eca22SWill Andrews 319ffdeef32SWill Andrews /* 320ffdeef32SWill Andrews * Generate a popcount cache for every POPCOUNT_BITS in the bitmap, 321ffdeef32SWill Andrews * so lookups only have to calculate the number of bits set between 322ffdeef32SWill Andrews * a cache point and their bit. This reduces lookups to O(1), 323ffdeef32SWill Andrews * without significantly increasing memory requirements. 324ffdeef32SWill Andrews * 325ffdeef32SWill Andrews * Round up the number of bins so that 'upper half' lookups work for 326ffdeef32SWill Andrews * the final bin, if needed. The first popcount is 0, since no bits 327ffdeef32SWill Andrews * precede bit 0, so add 1 for that also. Without this, extra work 328ffdeef32SWill Andrews * would be needed to handle the first PTEs in _kvm_pt_find(). 329ffdeef32SWill Andrews */ 330ffdeef32SWill Andrews addr = kd->pt_map; 331ffdeef32SWill Andrews res = map_len; 332ffdeef32SWill Andrews pc_bins = 1 + (res * NBBY + POPCOUNT_BITS / 2) / POPCOUNT_BITS; 333ffdeef32SWill Andrews kd->pt_popcounts = calloc(pc_bins, sizeof(uint32_t)); 334c9057838SWill Andrews if (kd->pt_popcounts == NULL) { 335c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot allocate popcount bins"); 336ffdeef32SWill Andrews return (-1); 337c9057838SWill Andrews } 338ffdeef32SWill Andrews 339ffdeef32SWill Andrews for (popcount_bin = &kd->pt_popcounts[1]; res > 0; 340ffdeef32SWill Andrews addr++, res -= sizeof(*addr)) { 341ffdeef32SWill Andrews *popcount_bin += popcount_bytes(addr, 0, 342ffdeef32SWill Andrews MIN(res * NBBY, BITS_IN(*addr))); 343ffdeef32SWill Andrews if (++bin_popcounts == POPCOUNTS_IN(*addr)) { 344ffdeef32SWill Andrews popcount_bin++; 345ffdeef32SWill Andrews *popcount_bin = *(popcount_bin - 1); 346ffdeef32SWill Andrews bin_popcounts = 0; 347ffdeef32SWill Andrews } 348ffdeef32SWill Andrews } 349ffdeef32SWill Andrews 350ffdeef32SWill Andrews assert(pc_bins * sizeof(*popcount_bin) == 351ffdeef32SWill Andrews ((uintptr_t)popcount_bin - (uintptr_t)kd->pt_popcounts)); 352ffdeef32SWill Andrews 353ffdeef32SWill Andrews kd->pt_sparse_off = sparse_off; 354c9057838SWill Andrews kd->pt_sparse_size = (uint64_t)*popcount_bin * page_size; 355ffdeef32SWill Andrews kd->pt_page_size = page_size; 356ffdeef32SWill Andrews kd->pt_word_size = word_size; 357c9057838SWill Andrews 358c9057838SWill Andrews /* 359c9057838SWill Andrews * Map the sparse page array. This is useful for performing point 360c9057838SWill Andrews * lookups of specific pages, e.g. for kvm_walk_pages. Generally, 361c9057838SWill Andrews * this is much larger than is reasonable to read in up front, so 362c9057838SWill Andrews * mmap it in instead. 363c9057838SWill Andrews */ 364c9057838SWill Andrews kd->sparse_map = mmap(NULL, kd->pt_sparse_size, PROT_READ, 365c9057838SWill Andrews MAP_PRIVATE, kd->pmfd, kd->pt_sparse_off); 366c9057838SWill Andrews if (kd->sparse_map == MAP_FAILED) { 367c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot map %" PRIu64 3688baaf913SWill Andrews " bytes from fd %d offset %jd for sparse map: %s", 369c9057838SWill Andrews kd->pt_sparse_size, kd->pmfd, 3708baaf913SWill Andrews (intmax_t)kd->pt_sparse_off, strerror(errno)); 371c9057838SWill Andrews return (-1); 372c9057838SWill Andrews } 373c9057838SWill Andrews return (0); 374c9057838SWill Andrews } 375c9057838SWill Andrews 376c9057838SWill Andrews int 377c9057838SWill Andrews _kvm_pmap_init(kvm_t *kd, uint32_t pmap_size, off_t pmap_off) 378c9057838SWill Andrews { 379c9057838SWill Andrews ssize_t exp_len = pmap_size; 380c9057838SWill Andrews 381c9057838SWill Andrews kd->page_map_size = pmap_size; 382c9057838SWill Andrews kd->page_map_off = pmap_off; 383c9057838SWill Andrews kd->page_map = _kvm_malloc(kd, pmap_size); 384c9057838SWill Andrews if (kd->page_map == NULL) { 385c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot allocate %u bytes " 386c9057838SWill Andrews "for page map", pmap_size); 387c9057838SWill Andrews return (-1); 388c9057838SWill Andrews } 389c9057838SWill Andrews if (pread(kd->pmfd, kd->page_map, pmap_size, pmap_off) != exp_len) { 390c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot read %d bytes from " 3918baaf913SWill Andrews "offset %jd for page map", pmap_size, (intmax_t)pmap_off); 392c9057838SWill Andrews return (-1); 393c9057838SWill Andrews } 394ffdeef32SWill Andrews return (0); 395ffdeef32SWill Andrews } 396ffdeef32SWill Andrews 397ffdeef32SWill Andrews /* 398ffdeef32SWill Andrews * Find the offset for the given physical page address; returns -1 otherwise. 399ffdeef32SWill Andrews * 400ffdeef32SWill Andrews * A page's offset is represented by the sparse page base offset plus the 401c9057838SWill Andrews * number of bits set before its bit multiplied by page size. This means 402ffdeef32SWill Andrews * that if a page exists in the dump, it's necessary to know how many pages 403ffdeef32SWill Andrews * in the dump precede it. Reduce this O(n) counting to O(1) by caching the 404ffdeef32SWill Andrews * number of bits set at POPCOUNT_BITS intervals. 405ffdeef32SWill Andrews * 406ffdeef32SWill Andrews * Then to find the number of pages before the requested address, simply 407ffdeef32SWill Andrews * index into the cache and count the number of bits set between that cache 408ffdeef32SWill Andrews * bin and the page's bit. Halve the number of bytes that have to be 409ffdeef32SWill Andrews * checked by also counting down from the next higher bin if it's closer. 410ffdeef32SWill Andrews */ 411ffdeef32SWill Andrews off_t 412c9057838SWill Andrews _kvm_pt_find(kvm_t *kd, uint64_t pa, unsigned int page_size) 413197eca22SWill Andrews { 414ffdeef32SWill Andrews uint64_t *bitmap = kd->pt_map; 415c9057838SWill Andrews uint64_t pte_bit_id = pa / page_size; 416ffdeef32SWill Andrews uint64_t pte_u64 = pte_bit_id / BITS_IN(*bitmap); 417ffdeef32SWill Andrews uint64_t popcount_id = pte_bit_id / POPCOUNT_BITS; 418ffdeef32SWill Andrews uint64_t pte_mask = 1ULL << (pte_bit_id % BITS_IN(*bitmap)); 419ffdeef32SWill Andrews uint64_t bitN; 420ffdeef32SWill Andrews uint32_t count; 421197eca22SWill Andrews 422ffdeef32SWill Andrews /* Check whether the page address requested is in the dump. */ 423ffdeef32SWill Andrews if (pte_bit_id >= (kd->pt_map_size * NBBY) || 424ffdeef32SWill Andrews (bitmap[pte_u64] & pte_mask) == 0) 425ffdeef32SWill Andrews return (-1); 426ffdeef32SWill Andrews 427ffdeef32SWill Andrews /* 428ffdeef32SWill Andrews * Add/sub popcounts from the bitmap until the PTE's bit is reached. 429ffdeef32SWill Andrews * For bits that are in the upper half between the calculated 430ffdeef32SWill Andrews * popcount id and the next one, use the next one and subtract to 431ffdeef32SWill Andrews * minimize the number of popcounts required. 432ffdeef32SWill Andrews */ 433ffdeef32SWill Andrews if ((pte_bit_id % POPCOUNT_BITS) < (POPCOUNT_BITS / 2)) { 434ffdeef32SWill Andrews count = kd->pt_popcounts[popcount_id] + popcount_bytes( 435ffdeef32SWill Andrews bitmap + popcount_id * POPCOUNTS_IN(*bitmap), 436ffdeef32SWill Andrews 0, pte_bit_id - popcount_id * POPCOUNT_BITS); 437ffdeef32SWill Andrews } else { 438ffdeef32SWill Andrews /* 439ffdeef32SWill Andrews * Counting in reverse is trickier, since we must avoid 440ffdeef32SWill Andrews * reading from bytes that are not in range, and invert. 441ffdeef32SWill Andrews */ 442ffdeef32SWill Andrews uint64_t pte_u64_bit_off = pte_u64 * BITS_IN(*bitmap); 443ffdeef32SWill Andrews 444ffdeef32SWill Andrews popcount_id++; 445ffdeef32SWill Andrews bitN = MIN(popcount_id * POPCOUNT_BITS, 446ffdeef32SWill Andrews kd->pt_map_size * BITS_IN(uint8_t)); 447ffdeef32SWill Andrews count = kd->pt_popcounts[popcount_id] - popcount_bytes( 448ffdeef32SWill Andrews bitmap + pte_u64, 449ffdeef32SWill Andrews pte_bit_id - pte_u64_bit_off, bitN - pte_u64_bit_off); 450197eca22SWill Andrews } 451ffdeef32SWill Andrews 452ffdeef32SWill Andrews /* 453ffdeef32SWill Andrews * This can only happen if the core is truncated. Treat these 454ffdeef32SWill Andrews * entries as if they don't exist, since their backing doesn't. 455ffdeef32SWill Andrews */ 456c9057838SWill Andrews if (count >= (kd->pt_sparse_size / page_size)) 457ffdeef32SWill Andrews return (-1); 458ffdeef32SWill Andrews 459c9057838SWill Andrews return (kd->pt_sparse_off + (uint64_t)count * page_size); 460197eca22SWill Andrews } 461197eca22SWill Andrews 462197eca22SWill Andrews static int 463197eca22SWill Andrews kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list) 464197eca22SWill Andrews { 465197eca22SWill Andrews kvaddr_t addr; 466197eca22SWill Andrews int error, nfail; 467197eca22SWill Andrews 468197eca22SWill Andrews if (kd->resolve_symbol == NULL) { 469197eca22SWill Andrews struct nlist *nl; 470197eca22SWill Andrews int count, i; 471197eca22SWill Andrews 472197eca22SWill Andrews for (count = 0; list[count].n_name != NULL && 473197eca22SWill Andrews list[count].n_name[0] != '\0'; count++) 474197eca22SWill Andrews ; 475197eca22SWill Andrews nl = calloc(count + 1, sizeof(*nl)); 476197eca22SWill Andrews for (i = 0; i < count; i++) 477197eca22SWill Andrews nl[i].n_name = list[i].n_name; 478197eca22SWill Andrews nfail = __fdnlist(kd->nlfd, nl); 479197eca22SWill Andrews for (i = 0; i < count; i++) { 480197eca22SWill Andrews list[i].n_type = nl[i].n_type; 481197eca22SWill Andrews list[i].n_value = nl[i].n_value; 482197eca22SWill Andrews } 483197eca22SWill Andrews free(nl); 484197eca22SWill Andrews return (nfail); 485197eca22SWill Andrews } 486197eca22SWill Andrews 487197eca22SWill Andrews nfail = 0; 488197eca22SWill Andrews while (list->n_name != NULL && list->n_name[0] != '\0') { 489197eca22SWill Andrews error = kd->resolve_symbol(list->n_name, &addr); 490197eca22SWill Andrews if (error != 0) { 491197eca22SWill Andrews nfail++; 492197eca22SWill Andrews list->n_value = 0; 493197eca22SWill Andrews list->n_type = 0; 494197eca22SWill Andrews } else { 495197eca22SWill Andrews list->n_value = addr; 496197eca22SWill Andrews list->n_type = N_DATA | N_EXT; 497197eca22SWill Andrews } 498197eca22SWill Andrews list++; 499197eca22SWill Andrews } 500197eca22SWill Andrews return (nfail); 501197eca22SWill Andrews } 502197eca22SWill Andrews 503197eca22SWill Andrews /* 504197eca22SWill Andrews * Walk the list of unresolved symbols, generate a new list and prefix the 505197eca22SWill Andrews * symbol names, try again, and merge back what we could resolve. 506197eca22SWill Andrews */ 507197eca22SWill Andrews static int 508197eca22SWill Andrews kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing, 509197eca22SWill Andrews const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t)) 510197eca22SWill Andrews { 511197eca22SWill Andrews struct kvm_nlist *n, *np, *p; 512197eca22SWill Andrews char *cp, *ce; 513197eca22SWill Andrews const char *ccp; 514197eca22SWill Andrews size_t len; 515197eca22SWill Andrews int slen, unresolved; 516197eca22SWill Andrews 517197eca22SWill Andrews /* 518197eca22SWill Andrews * Calculate the space we need to malloc for nlist and names. 519197eca22SWill Andrews * We are going to store the name twice for later lookups: once 520197eca22SWill Andrews * with the prefix and once the unmodified name delmited by \0. 521197eca22SWill Andrews */ 522197eca22SWill Andrews len = 0; 523197eca22SWill Andrews unresolved = 0; 524197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) { 525197eca22SWill Andrews if (p->n_type != N_UNDF) 526197eca22SWill Andrews continue; 527197eca22SWill Andrews len += sizeof(struct kvm_nlist) + strlen(prefix) + 528197eca22SWill Andrews 2 * (strlen(p->n_name) + 1); 529197eca22SWill Andrews unresolved++; 530197eca22SWill Andrews } 531197eca22SWill Andrews if (unresolved == 0) 532197eca22SWill Andrews return (unresolved); 533197eca22SWill Andrews /* Add space for the terminating nlist entry. */ 534197eca22SWill Andrews len += sizeof(struct kvm_nlist); 535197eca22SWill Andrews unresolved++; 536197eca22SWill Andrews 537197eca22SWill Andrews /* Alloc one chunk for (nlist, [names]) and setup pointers. */ 538197eca22SWill Andrews n = np = malloc(len); 539197eca22SWill Andrews bzero(n, len); 540197eca22SWill Andrews if (n == NULL) 541197eca22SWill Andrews return (missing); 542197eca22SWill Andrews cp = ce = (char *)np; 543197eca22SWill Andrews cp += unresolved * sizeof(struct kvm_nlist); 544197eca22SWill Andrews ce += len; 545197eca22SWill Andrews 546197eca22SWill Andrews /* Generate shortened nlist with special prefix. */ 547197eca22SWill Andrews unresolved = 0; 548197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) { 549197eca22SWill Andrews if (p->n_type != N_UNDF) 550197eca22SWill Andrews continue; 551197eca22SWill Andrews *np = *p; 552197eca22SWill Andrews /* Save the new\0orig. name so we can later match it again. */ 553197eca22SWill Andrews slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix, 554197eca22SWill Andrews (prefix[0] != '\0' && p->n_name[0] == '_') ? 555197eca22SWill Andrews (p->n_name + 1) : p->n_name, '\0', p->n_name); 556197eca22SWill Andrews if (slen < 0 || slen >= ce - cp) 557197eca22SWill Andrews continue; 558197eca22SWill Andrews np->n_name = cp; 559197eca22SWill Andrews cp += slen + 1; 560197eca22SWill Andrews np++; 561197eca22SWill Andrews unresolved++; 562197eca22SWill Andrews } 563197eca22SWill Andrews 564197eca22SWill Andrews /* Do lookup on the reduced list. */ 565197eca22SWill Andrews np = n; 566197eca22SWill Andrews unresolved = kvm_fdnlist(kd, np); 567197eca22SWill Andrews 568197eca22SWill Andrews /* Check if we could resolve further symbols and update the list. */ 569197eca22SWill Andrews if (unresolved >= 0 && unresolved < missing) { 570197eca22SWill Andrews /* Find the first freshly resolved entry. */ 571197eca22SWill Andrews for (; np->n_name && np->n_name[0]; np++) 572197eca22SWill Andrews if (np->n_type != N_UNDF) 573197eca22SWill Andrews break; 574197eca22SWill Andrews /* 575197eca22SWill Andrews * The lists are both in the same order, 576197eca22SWill Andrews * so we can walk them in parallel. 577197eca22SWill Andrews */ 578197eca22SWill Andrews for (p = nl; np->n_name && np->n_name[0] && 579197eca22SWill Andrews p->n_name && p->n_name[0]; ++p) { 580197eca22SWill Andrews if (p->n_type != N_UNDF) 581197eca22SWill Andrews continue; 582197eca22SWill Andrews /* Skip expanded name and compare to orig. one. */ 583197eca22SWill Andrews ccp = np->n_name + strlen(np->n_name) + 1; 584197eca22SWill Andrews if (strcmp(ccp, p->n_name) != 0) 585197eca22SWill Andrews continue; 586197eca22SWill Andrews /* Update nlist with new, translated results. */ 587197eca22SWill Andrews p->n_type = np->n_type; 588197eca22SWill Andrews if (validate_fn) 589197eca22SWill Andrews p->n_value = (*validate_fn)(kd, np->n_value); 590197eca22SWill Andrews else 591197eca22SWill Andrews p->n_value = np->n_value; 592197eca22SWill Andrews missing--; 593197eca22SWill Andrews /* Find next freshly resolved entry. */ 594197eca22SWill Andrews for (np++; np->n_name && np->n_name[0]; np++) 595197eca22SWill Andrews if (np->n_type != N_UNDF) 596197eca22SWill Andrews break; 597197eca22SWill Andrews } 598197eca22SWill Andrews } 599197eca22SWill Andrews /* We could assert missing = unresolved here. */ 600197eca22SWill Andrews 601197eca22SWill Andrews free(n); 602197eca22SWill Andrews return (unresolved); 603197eca22SWill Andrews } 604197eca22SWill Andrews 605197eca22SWill Andrews int 606197eca22SWill Andrews _kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize) 607197eca22SWill Andrews { 608197eca22SWill Andrews struct kvm_nlist *p; 609197eca22SWill Andrews int nvalid; 610197eca22SWill Andrews struct kld_sym_lookup lookup; 611197eca22SWill Andrews int error; 612197eca22SWill Andrews const char *prefix = ""; 613197eca22SWill Andrews char symname[1024]; /* XXX-BZ symbol name length limit? */ 614197eca22SWill Andrews int tried_vnet, tried_dpcpu; 615197eca22SWill Andrews 616197eca22SWill Andrews /* 617197eca22SWill Andrews * If we can't use the kld symbol lookup, revert to the 618197eca22SWill Andrews * slow library call. 619197eca22SWill Andrews */ 620197eca22SWill Andrews if (!ISALIVE(kd)) { 621197eca22SWill Andrews error = kvm_fdnlist(kd, nl); 622197eca22SWill Andrews if (error <= 0) /* Hard error or success. */ 623197eca22SWill Andrews return (error); 624197eca22SWill Andrews 625197eca22SWill Andrews if (_kvm_vnet_initialized(kd, initialize)) 626197eca22SWill Andrews error = kvm_fdnlist_prefix(kd, nl, error, 627197eca22SWill Andrews VNET_SYMPREFIX, _kvm_vnet_validaddr); 628197eca22SWill Andrews 629197eca22SWill Andrews if (error > 0 && _kvm_dpcpu_initialized(kd, initialize)) 630197eca22SWill Andrews error = kvm_fdnlist_prefix(kd, nl, error, 631197eca22SWill Andrews DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr); 632197eca22SWill Andrews 633197eca22SWill Andrews return (error); 634197eca22SWill Andrews } 635197eca22SWill Andrews 636197eca22SWill Andrews /* 637197eca22SWill Andrews * We can use the kld lookup syscall. Go through each nlist entry 638197eca22SWill Andrews * and look it up with a kldsym(2) syscall. 639197eca22SWill Andrews */ 640197eca22SWill Andrews nvalid = 0; 641197eca22SWill Andrews tried_vnet = 0; 642197eca22SWill Andrews tried_dpcpu = 0; 643197eca22SWill Andrews again: 644197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) { 645197eca22SWill Andrews if (p->n_type != N_UNDF) 646197eca22SWill Andrews continue; 647197eca22SWill Andrews 648197eca22SWill Andrews lookup.version = sizeof(lookup); 649197eca22SWill Andrews lookup.symvalue = 0; 650197eca22SWill Andrews lookup.symsize = 0; 651197eca22SWill Andrews 652197eca22SWill Andrews error = snprintf(symname, sizeof(symname), "%s%s", prefix, 653197eca22SWill Andrews (prefix[0] != '\0' && p->n_name[0] == '_') ? 654197eca22SWill Andrews (p->n_name + 1) : p->n_name); 655197eca22SWill Andrews if (error < 0 || error >= (int)sizeof(symname)) 656197eca22SWill Andrews continue; 657197eca22SWill Andrews lookup.symname = symname; 658197eca22SWill Andrews if (lookup.symname[0] == '_') 659197eca22SWill Andrews lookup.symname++; 660197eca22SWill Andrews 661197eca22SWill Andrews if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) { 662197eca22SWill Andrews p->n_type = N_TEXT; 663197eca22SWill Andrews if (_kvm_vnet_initialized(kd, initialize) && 664197eca22SWill Andrews strcmp(prefix, VNET_SYMPREFIX) == 0) 665197eca22SWill Andrews p->n_value = 666197eca22SWill Andrews _kvm_vnet_validaddr(kd, lookup.symvalue); 667197eca22SWill Andrews else if (_kvm_dpcpu_initialized(kd, initialize) && 668197eca22SWill Andrews strcmp(prefix, DPCPU_SYMPREFIX) == 0) 669197eca22SWill Andrews p->n_value = 670197eca22SWill Andrews _kvm_dpcpu_validaddr(kd, lookup.symvalue); 671197eca22SWill Andrews else 672197eca22SWill Andrews p->n_value = lookup.symvalue; 673197eca22SWill Andrews ++nvalid; 674197eca22SWill Andrews /* lookup.symsize */ 675197eca22SWill Andrews } 676197eca22SWill Andrews } 677197eca22SWill Andrews 678197eca22SWill Andrews /* 679197eca22SWill Andrews * Check the number of entries that weren't found. If they exist, 680197eca22SWill Andrews * try again with a prefix for virtualized or DPCPU symbol names. 681197eca22SWill Andrews */ 682197eca22SWill Andrews error = ((p - nl) - nvalid); 683197eca22SWill Andrews if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) { 684197eca22SWill Andrews tried_vnet = 1; 685197eca22SWill Andrews prefix = VNET_SYMPREFIX; 686197eca22SWill Andrews goto again; 687197eca22SWill Andrews } 688197eca22SWill Andrews if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) { 689197eca22SWill Andrews tried_dpcpu = 1; 690197eca22SWill Andrews prefix = DPCPU_SYMPREFIX; 691197eca22SWill Andrews goto again; 692197eca22SWill Andrews } 693197eca22SWill Andrews 694197eca22SWill Andrews /* 695197eca22SWill Andrews * Return the number of entries that weren't found. If they exist, 696197eca22SWill Andrews * also fill internal error buffer. 697197eca22SWill Andrews */ 698197eca22SWill Andrews error = ((p - nl) - nvalid); 699197eca22SWill Andrews if (error) 700197eca22SWill Andrews _kvm_syserr(kd, kd->program, "kvm_nlist"); 701197eca22SWill Andrews return (error); 702197eca22SWill Andrews } 703c9057838SWill Andrews 704c9057838SWill Andrews int 7052aa6a4f3SWill Andrews _kvm_bitmap_init(struct kvm_bitmap *bm, u_long bitmapsize, u_long *idx) 706c9057838SWill Andrews { 707c9057838SWill Andrews 7082aa6a4f3SWill Andrews *idx = ULONG_MAX; 709c9057838SWill Andrews bm->map = calloc(bitmapsize, sizeof *bm->map); 710c9057838SWill Andrews if (bm->map == NULL) 711c9057838SWill Andrews return (0); 712c9057838SWill Andrews bm->size = bitmapsize; 713c9057838SWill Andrews return (1); 714c9057838SWill Andrews } 715c9057838SWill Andrews 716c9057838SWill Andrews void 717c9057838SWill Andrews _kvm_bitmap_set(struct kvm_bitmap *bm, u_long pa, unsigned int page_size) 718c9057838SWill Andrews { 719c9057838SWill Andrews u_long bm_index = pa / page_size; 720c9057838SWill Andrews uint8_t *byte = &bm->map[bm_index / 8]; 721c9057838SWill Andrews 722c9057838SWill Andrews *byte |= (1UL << (bm_index % 8)); 723c9057838SWill Andrews } 724c9057838SWill Andrews 725c9057838SWill Andrews int 7262aa6a4f3SWill Andrews _kvm_bitmap_next(struct kvm_bitmap *bm, u_long *idx) 727c9057838SWill Andrews { 728c9057838SWill Andrews u_long first_invalid = bm->size * CHAR_BIT; 729c9057838SWill Andrews 7302aa6a4f3SWill Andrews if (*idx == ULONG_MAX) 7312aa6a4f3SWill Andrews *idx = 0; 732c9057838SWill Andrews else 7332aa6a4f3SWill Andrews (*idx)++; 734c9057838SWill Andrews 7352aa6a4f3SWill Andrews /* Find the next valid idx. */ 7362aa6a4f3SWill Andrews for (; *idx < first_invalid; (*idx)++) { 7372aa6a4f3SWill Andrews unsigned int mask = *idx % CHAR_BIT; 7382aa6a4f3SWill Andrews if ((bm->map[*idx * CHAR_BIT] & mask) == 0) 739c9057838SWill Andrews break; 740c9057838SWill Andrews } 741c9057838SWill Andrews 7422aa6a4f3SWill Andrews return (*idx < first_invalid); 743c9057838SWill Andrews } 744c9057838SWill Andrews 745c9057838SWill Andrews void 746c9057838SWill Andrews _kvm_bitmap_deinit(struct kvm_bitmap *bm) 747c9057838SWill Andrews { 748c9057838SWill Andrews 749c9057838SWill Andrews free(bm->map); 750c9057838SWill Andrews } 751c9057838SWill Andrews 752c9057838SWill Andrews int 753c9057838SWill Andrews _kvm_visit_cb(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg, u_long pa, 754c9057838SWill Andrews u_long kmap_vaddr, u_long dmap_vaddr, vm_prot_t prot, size_t len, 755c9057838SWill Andrews unsigned int page_size) 756c9057838SWill Andrews { 757c9057838SWill Andrews unsigned int pgsz = page_size ? page_size : len; 758c9057838SWill Andrews struct kvm_page p = { 7599dc7ed62SEd Maste .kp_version = LIBKVM_WALK_PAGES_VERSION, 7609dc7ed62SEd Maste .kp_paddr = pa, 7619dc7ed62SEd Maste .kp_kmap_vaddr = kmap_vaddr, 7629dc7ed62SEd Maste .kp_dmap_vaddr = dmap_vaddr, 7639dc7ed62SEd Maste .kp_prot = prot, 7649dc7ed62SEd Maste .kp_offset = _kvm_pt_find(kd, pa, pgsz), 7659dc7ed62SEd Maste .kp_len = len, 766c9057838SWill Andrews }; 767c9057838SWill Andrews 768c9057838SWill Andrews return cb(&p, arg); 769c9057838SWill Andrews } 770