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 48197eca22SWill Andrews #include <net/vnet.h> 49197eca22SWill Andrews 50ffdeef32SWill Andrews #include <assert.h> 51197eca22SWill Andrews #include <fcntl.h> 528baaf913SWill Andrews #include <vm/vm.h> 53197eca22SWill Andrews #include <kvm.h> 54197eca22SWill Andrews #include <limits.h> 55197eca22SWill Andrews #include <paths.h> 56197eca22SWill Andrews #include <stdint.h> 57197eca22SWill Andrews #include <stdio.h> 58197eca22SWill Andrews #include <stdlib.h> 59197eca22SWill Andrews #include <string.h> 60197eca22SWill Andrews #include <unistd.h> 61197eca22SWill Andrews #include <stdarg.h> 62c9057838SWill Andrews #include <inttypes.h> 63197eca22SWill Andrews 64197eca22SWill Andrews #include "kvm_private.h" 65197eca22SWill Andrews 66197eca22SWill Andrews /* 67197eca22SWill Andrews * Routines private to libkvm. 68197eca22SWill Andrews */ 69197eca22SWill Andrews 70197eca22SWill Andrews /* from src/lib/libc/gen/nlist.c */ 71197eca22SWill Andrews int __fdnlist(int, struct nlist *); 72197eca22SWill Andrews 73197eca22SWill Andrews /* 74197eca22SWill Andrews * Report an error using printf style arguments. "program" is kd->program 75197eca22SWill Andrews * on hard errors, and 0 on soft errors, so that under sun error emulation, 76197eca22SWill Andrews * only hard errors are printed out (otherwise, programs like gdb will 77197eca22SWill Andrews * generate tons of error messages when trying to access bogus pointers). 78197eca22SWill Andrews */ 79197eca22SWill Andrews void 80197eca22SWill Andrews _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) 81197eca22SWill Andrews { 82197eca22SWill Andrews va_list ap; 83197eca22SWill Andrews 84197eca22SWill Andrews va_start(ap, fmt); 85197eca22SWill Andrews if (program != NULL) { 86197eca22SWill Andrews (void)fprintf(stderr, "%s: ", program); 87197eca22SWill Andrews (void)vfprintf(stderr, fmt, ap); 88197eca22SWill Andrews (void)fputc('\n', stderr); 89197eca22SWill Andrews } else 90197eca22SWill Andrews (void)vsnprintf(kd->errbuf, 91197eca22SWill Andrews sizeof(kd->errbuf), fmt, ap); 92197eca22SWill Andrews 93197eca22SWill Andrews va_end(ap); 94197eca22SWill Andrews } 95197eca22SWill Andrews 96197eca22SWill Andrews void 97197eca22SWill Andrews _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) 98197eca22SWill Andrews { 99197eca22SWill Andrews va_list ap; 100197eca22SWill Andrews int n; 101197eca22SWill Andrews 102197eca22SWill Andrews va_start(ap, fmt); 103197eca22SWill Andrews if (program != NULL) { 104197eca22SWill Andrews (void)fprintf(stderr, "%s: ", program); 105197eca22SWill Andrews (void)vfprintf(stderr, fmt, ap); 106197eca22SWill Andrews (void)fprintf(stderr, ": %s\n", strerror(errno)); 107197eca22SWill Andrews } else { 108197eca22SWill Andrews char *cp = kd->errbuf; 109197eca22SWill Andrews 110197eca22SWill Andrews (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap); 111197eca22SWill Andrews n = strlen(cp); 112197eca22SWill Andrews (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s", 113197eca22SWill Andrews strerror(errno)); 114197eca22SWill Andrews } 115197eca22SWill Andrews va_end(ap); 116197eca22SWill Andrews } 117197eca22SWill Andrews 118197eca22SWill Andrews void * 119197eca22SWill Andrews _kvm_malloc(kvm_t *kd, size_t n) 120197eca22SWill Andrews { 121197eca22SWill Andrews void *p; 122197eca22SWill Andrews 123197eca22SWill Andrews if ((p = calloc(n, sizeof(char))) == NULL) 124197eca22SWill Andrews _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s", 125197eca22SWill Andrews n, strerror(errno)); 126197eca22SWill Andrews return (p); 127197eca22SWill Andrews } 128197eca22SWill Andrews 129197eca22SWill Andrews int 130197eca22SWill Andrews _kvm_probe_elf_kernel(kvm_t *kd, int class, int machine) 131197eca22SWill Andrews { 132197eca22SWill Andrews 133197eca22SWill Andrews return (kd->nlehdr.e_ident[EI_CLASS] == class && 134*8024ba45SLeandro Lupori ((machine == EM_PPC || machine == EM_PPC64) ? 135*8024ba45SLeandro Lupori kd->nlehdr.e_type == ET_DYN : kd->nlehdr.e_type == ET_EXEC) && 136197eca22SWill Andrews kd->nlehdr.e_machine == machine); 137197eca22SWill Andrews } 138197eca22SWill Andrews 139197eca22SWill Andrews int 140197eca22SWill Andrews _kvm_is_minidump(kvm_t *kd) 141197eca22SWill Andrews { 142197eca22SWill Andrews char minihdr[8]; 143197eca22SWill Andrews 144197eca22SWill Andrews if (kd->rawdump) 145197eca22SWill Andrews return (0); 146197eca22SWill Andrews if (pread(kd->pmfd, &minihdr, 8, 0) == 8 && 147197eca22SWill Andrews memcmp(&minihdr, "minidump", 8) == 0) 148197eca22SWill Andrews return (1); 149197eca22SWill Andrews return (0); 150197eca22SWill Andrews } 151197eca22SWill Andrews 152197eca22SWill Andrews /* 153197eca22SWill Andrews * The powerpc backend has a hack to strip a leading kerneldump 154197eca22SWill Andrews * header from the core before treating it as an ELF header. 155197eca22SWill Andrews * 156197eca22SWill Andrews * We can add that here if we can get a change to libelf to support 157197eca22SWill Andrews * an initial offset into the file. Alternatively we could patch 158197eca22SWill Andrews * savecore to extract cores from a regular file instead. 159197eca22SWill Andrews */ 160197eca22SWill Andrews int 161197eca22SWill Andrews _kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp) 162197eca22SWill Andrews { 163197eca22SWill Andrews GElf_Ehdr ehdr; 164197eca22SWill Andrews GElf_Phdr *phdr; 165197eca22SWill Andrews Elf *elf; 166197eca22SWill Andrews size_t i, phnum; 167197eca22SWill Andrews 168197eca22SWill Andrews elf = elf_begin(kd->pmfd, ELF_C_READ, NULL); 169197eca22SWill Andrews if (elf == NULL) { 170197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 171197eca22SWill Andrews return (-1); 172197eca22SWill Andrews } 173197eca22SWill Andrews if (elf_kind(elf) != ELF_K_ELF) { 174197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 175197eca22SWill Andrews goto bad; 176197eca22SWill Andrews } 177197eca22SWill Andrews if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) { 178197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 179197eca22SWill Andrews goto bad; 180197eca22SWill Andrews } 181197eca22SWill Andrews if (gelf_getehdr(elf, &ehdr) == NULL) { 182197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 183197eca22SWill Andrews goto bad; 184197eca22SWill Andrews } 185197eca22SWill Andrews if (ehdr.e_type != ET_CORE) { 186197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 187197eca22SWill Andrews goto bad; 188197eca22SWill Andrews } 189197eca22SWill Andrews if (ehdr.e_machine != kd->nlehdr.e_machine) { 190197eca22SWill Andrews _kvm_err(kd, kd->program, "invalid core"); 191197eca22SWill Andrews goto bad; 192197eca22SWill Andrews } 193197eca22SWill Andrews 194197eca22SWill Andrews if (elf_getphdrnum(elf, &phnum) == -1) { 195197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 196197eca22SWill Andrews goto bad; 197197eca22SWill Andrews } 198197eca22SWill Andrews 199197eca22SWill Andrews phdr = calloc(phnum, sizeof(*phdr)); 200197eca22SWill Andrews if (phdr == NULL) { 201197eca22SWill Andrews _kvm_err(kd, kd->program, "failed to allocate phdrs"); 202197eca22SWill Andrews goto bad; 203197eca22SWill Andrews } 204197eca22SWill Andrews 205197eca22SWill Andrews for (i = 0; i < phnum; i++) { 206197eca22SWill Andrews if (gelf_getphdr(elf, i, &phdr[i]) == NULL) { 207f6080aabSGleb Smirnoff free(phdr); 208197eca22SWill Andrews _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 209197eca22SWill Andrews goto bad; 210197eca22SWill Andrews } 211197eca22SWill Andrews } 212197eca22SWill Andrews elf_end(elf); 213197eca22SWill Andrews *phnump = phnum; 214197eca22SWill Andrews *phdrp = phdr; 215197eca22SWill Andrews return (0); 216197eca22SWill Andrews 217197eca22SWill Andrews bad: 218197eca22SWill Andrews elf_end(elf); 219197eca22SWill Andrews return (-1); 220197eca22SWill Andrews } 221197eca22SWill Andrews 222ffdeef32SWill Andrews /* 223ffdeef32SWill Andrews * Transform v such that only bits [bit0, bitN) may be set. Generates a 224ffdeef32SWill Andrews * bitmask covering the number of bits, then shifts so +bit0+ is the first. 225ffdeef32SWill Andrews */ 226ffdeef32SWill Andrews static uint64_t 227ffdeef32SWill Andrews bitmask_range(uint64_t v, uint64_t bit0, uint64_t bitN) 228197eca22SWill Andrews { 229ffdeef32SWill Andrews if (bit0 == 0 && bitN == BITS_IN(v)) 230ffdeef32SWill Andrews return (v); 231197eca22SWill Andrews 232ffdeef32SWill Andrews return (v & (((1ULL << (bitN - bit0)) - 1ULL) << bit0)); 233197eca22SWill Andrews } 234197eca22SWill Andrews 235ffdeef32SWill Andrews /* 236ffdeef32SWill Andrews * Returns the number of bits in a given byte array range starting at a 237ffdeef32SWill Andrews * given base, from bit0 to bitN. bit0 may be non-zero in the case of 238ffdeef32SWill Andrews * counting backwards from bitN. 239ffdeef32SWill Andrews */ 240ffdeef32SWill Andrews static uint64_t 241ffdeef32SWill Andrews popcount_bytes(uint64_t *addr, uint32_t bit0, uint32_t bitN) 242ffdeef32SWill Andrews { 243ffdeef32SWill Andrews uint32_t res = bitN - bit0; 244ffdeef32SWill Andrews uint64_t count = 0; 245ffdeef32SWill Andrews uint32_t bound; 246ffdeef32SWill Andrews 247ffdeef32SWill Andrews /* Align to 64-bit boundary on the left side if needed. */ 248ffdeef32SWill Andrews if ((bit0 % BITS_IN(*addr)) != 0) { 249ffdeef32SWill Andrews bound = MIN(bitN, roundup2(bit0, BITS_IN(*addr))); 250ffdeef32SWill Andrews count += __bitcount64(bitmask_range(*addr, bit0, bound)); 251ffdeef32SWill Andrews res -= (bound - bit0); 252ffdeef32SWill Andrews addr++; 253ffdeef32SWill Andrews } 254ffdeef32SWill Andrews 255ffdeef32SWill Andrews while (res > 0) { 256ffdeef32SWill Andrews bound = MIN(res, BITS_IN(*addr)); 257ffdeef32SWill Andrews count += __bitcount64(bitmask_range(*addr, 0, bound)); 258ffdeef32SWill Andrews res -= bound; 259ffdeef32SWill Andrews addr++; 260ffdeef32SWill Andrews } 261ffdeef32SWill Andrews 262ffdeef32SWill Andrews return (count); 263ffdeef32SWill Andrews } 264ffdeef32SWill Andrews 265c9057838SWill Andrews void * 2662aa6a4f3SWill Andrews _kvm_pmap_get(kvm_t *kd, u_long idx, size_t len) 267c9057838SWill Andrews { 2688baaf913SWill Andrews uintptr_t off = idx * len; 269c9057838SWill Andrews 2708baaf913SWill Andrews if ((off_t)off >= kd->pt_sparse_off) 271c9057838SWill Andrews return (NULL); 272c9057838SWill Andrews return (void *)((uintptr_t)kd->page_map + off); 273c9057838SWill Andrews } 274c9057838SWill Andrews 275c9057838SWill Andrews void * 276c9057838SWill Andrews _kvm_map_get(kvm_t *kd, u_long pa, unsigned int page_size) 277c9057838SWill Andrews { 278c9057838SWill Andrews off_t off; 279c9057838SWill Andrews uintptr_t addr; 280c9057838SWill Andrews 281c9057838SWill Andrews off = _kvm_pt_find(kd, pa, page_size); 282c9057838SWill Andrews if (off == -1) 283c9057838SWill Andrews return NULL; 284c9057838SWill Andrews 285c9057838SWill Andrews addr = (uintptr_t)kd->page_map + off; 286c9057838SWill Andrews if (off >= kd->pt_sparse_off) 287c9057838SWill Andrews addr = (uintptr_t)kd->sparse_map + (off - kd->pt_sparse_off); 288c9057838SWill Andrews return (void *)addr; 289c9057838SWill Andrews } 290c9057838SWill Andrews 291ffdeef32SWill Andrews int 292ffdeef32SWill Andrews _kvm_pt_init(kvm_t *kd, size_t map_len, off_t map_off, off_t sparse_off, 293197eca22SWill Andrews int page_size, int word_size) 294197eca22SWill Andrews { 295ffdeef32SWill Andrews uint64_t *addr; 296ffdeef32SWill Andrews uint32_t *popcount_bin; 297ffdeef32SWill Andrews int bin_popcounts = 0; 298ffdeef32SWill Andrews uint64_t pc_bins, res; 299ffdeef32SWill Andrews ssize_t rd; 300197eca22SWill Andrews 301ffdeef32SWill Andrews /* 302ffdeef32SWill Andrews * Map the bitmap specified by the arguments. 303ffdeef32SWill Andrews */ 304ffdeef32SWill Andrews kd->pt_map = _kvm_malloc(kd, map_len); 305ffdeef32SWill Andrews if (kd->pt_map == NULL) { 306ffdeef32SWill Andrews _kvm_err(kd, kd->program, "cannot allocate %zu bytes for bitmap", 307ffdeef32SWill Andrews map_len); 308197eca22SWill Andrews return (-1); 309197eca22SWill Andrews } 310ffdeef32SWill Andrews rd = pread(kd->pmfd, kd->pt_map, map_len, map_off); 311ffdeef32SWill Andrews if (rd < 0 || rd != (ssize_t)map_len) { 312ffdeef32SWill Andrews _kvm_err(kd, kd->program, "cannot read %zu bytes for bitmap", 313ffdeef32SWill Andrews map_len); 314ffdeef32SWill Andrews return (-1); 315ffdeef32SWill Andrews } 316ffdeef32SWill Andrews kd->pt_map_size = map_len; 317197eca22SWill Andrews 318ffdeef32SWill Andrews /* 319ffdeef32SWill Andrews * Generate a popcount cache for every POPCOUNT_BITS in the bitmap, 320ffdeef32SWill Andrews * so lookups only have to calculate the number of bits set between 321ffdeef32SWill Andrews * a cache point and their bit. This reduces lookups to O(1), 322ffdeef32SWill Andrews * without significantly increasing memory requirements. 323ffdeef32SWill Andrews * 324ffdeef32SWill Andrews * Round up the number of bins so that 'upper half' lookups work for 325ffdeef32SWill Andrews * the final bin, if needed. The first popcount is 0, since no bits 326ffdeef32SWill Andrews * precede bit 0, so add 1 for that also. Without this, extra work 327ffdeef32SWill Andrews * would be needed to handle the first PTEs in _kvm_pt_find(). 328ffdeef32SWill Andrews */ 329ffdeef32SWill Andrews addr = kd->pt_map; 330ffdeef32SWill Andrews res = map_len; 331ffdeef32SWill Andrews pc_bins = 1 + (res * NBBY + POPCOUNT_BITS / 2) / POPCOUNT_BITS; 332ffdeef32SWill Andrews kd->pt_popcounts = calloc(pc_bins, sizeof(uint32_t)); 333c9057838SWill Andrews if (kd->pt_popcounts == NULL) { 334c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot allocate popcount bins"); 335ffdeef32SWill Andrews return (-1); 336c9057838SWill Andrews } 337ffdeef32SWill Andrews 338ffdeef32SWill Andrews for (popcount_bin = &kd->pt_popcounts[1]; res > 0; 339ffdeef32SWill Andrews addr++, res -= sizeof(*addr)) { 340ffdeef32SWill Andrews *popcount_bin += popcount_bytes(addr, 0, 341ffdeef32SWill Andrews MIN(res * NBBY, BITS_IN(*addr))); 342ffdeef32SWill Andrews if (++bin_popcounts == POPCOUNTS_IN(*addr)) { 343ffdeef32SWill Andrews popcount_bin++; 344ffdeef32SWill Andrews *popcount_bin = *(popcount_bin - 1); 345ffdeef32SWill Andrews bin_popcounts = 0; 346ffdeef32SWill Andrews } 347ffdeef32SWill Andrews } 348ffdeef32SWill Andrews 349ffdeef32SWill Andrews assert(pc_bins * sizeof(*popcount_bin) == 350ffdeef32SWill Andrews ((uintptr_t)popcount_bin - (uintptr_t)kd->pt_popcounts)); 351ffdeef32SWill Andrews 352ffdeef32SWill Andrews kd->pt_sparse_off = sparse_off; 353c9057838SWill Andrews kd->pt_sparse_size = (uint64_t)*popcount_bin * page_size; 354ffdeef32SWill Andrews kd->pt_page_size = page_size; 355ffdeef32SWill Andrews kd->pt_word_size = word_size; 356c9057838SWill Andrews 357c9057838SWill Andrews /* 358c9057838SWill Andrews * Map the sparse page array. This is useful for performing point 359c9057838SWill Andrews * lookups of specific pages, e.g. for kvm_walk_pages. Generally, 360c9057838SWill Andrews * this is much larger than is reasonable to read in up front, so 361c9057838SWill Andrews * mmap it in instead. 362c9057838SWill Andrews */ 363c9057838SWill Andrews kd->sparse_map = mmap(NULL, kd->pt_sparse_size, PROT_READ, 364c9057838SWill Andrews MAP_PRIVATE, kd->pmfd, kd->pt_sparse_off); 365c9057838SWill Andrews if (kd->sparse_map == MAP_FAILED) { 366c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot map %" PRIu64 3678baaf913SWill Andrews " bytes from fd %d offset %jd for sparse map: %s", 368c9057838SWill Andrews kd->pt_sparse_size, kd->pmfd, 3698baaf913SWill Andrews (intmax_t)kd->pt_sparse_off, strerror(errno)); 370c9057838SWill Andrews return (-1); 371c9057838SWill Andrews } 372c9057838SWill Andrews return (0); 373c9057838SWill Andrews } 374c9057838SWill Andrews 375c9057838SWill Andrews int 376c9057838SWill Andrews _kvm_pmap_init(kvm_t *kd, uint32_t pmap_size, off_t pmap_off) 377c9057838SWill Andrews { 378c9057838SWill Andrews ssize_t exp_len = pmap_size; 379c9057838SWill Andrews 380c9057838SWill Andrews kd->page_map_size = pmap_size; 381c9057838SWill Andrews kd->page_map_off = pmap_off; 382c9057838SWill Andrews kd->page_map = _kvm_malloc(kd, pmap_size); 383c9057838SWill Andrews if (kd->page_map == NULL) { 384c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot allocate %u bytes " 385c9057838SWill Andrews "for page map", pmap_size); 386c9057838SWill Andrews return (-1); 387c9057838SWill Andrews } 388c9057838SWill Andrews if (pread(kd->pmfd, kd->page_map, pmap_size, pmap_off) != exp_len) { 389c9057838SWill Andrews _kvm_err(kd, kd->program, "cannot read %d bytes from " 3908baaf913SWill Andrews "offset %jd for page map", pmap_size, (intmax_t)pmap_off); 391c9057838SWill Andrews return (-1); 392c9057838SWill Andrews } 393ffdeef32SWill Andrews return (0); 394ffdeef32SWill Andrews } 395ffdeef32SWill Andrews 396ffdeef32SWill Andrews /* 397ffdeef32SWill Andrews * Find the offset for the given physical page address; returns -1 otherwise. 398ffdeef32SWill Andrews * 399ffdeef32SWill Andrews * A page's offset is represented by the sparse page base offset plus the 400c9057838SWill Andrews * number of bits set before its bit multiplied by page size. This means 401ffdeef32SWill Andrews * that if a page exists in the dump, it's necessary to know how many pages 402ffdeef32SWill Andrews * in the dump precede it. Reduce this O(n) counting to O(1) by caching the 403ffdeef32SWill Andrews * number of bits set at POPCOUNT_BITS intervals. 404ffdeef32SWill Andrews * 405ffdeef32SWill Andrews * Then to find the number of pages before the requested address, simply 406ffdeef32SWill Andrews * index into the cache and count the number of bits set between that cache 407ffdeef32SWill Andrews * bin and the page's bit. Halve the number of bytes that have to be 408ffdeef32SWill Andrews * checked by also counting down from the next higher bin if it's closer. 409ffdeef32SWill Andrews */ 410ffdeef32SWill Andrews off_t 411c9057838SWill Andrews _kvm_pt_find(kvm_t *kd, uint64_t pa, unsigned int page_size) 412197eca22SWill Andrews { 413ffdeef32SWill Andrews uint64_t *bitmap = kd->pt_map; 414c9057838SWill Andrews uint64_t pte_bit_id = pa / page_size; 415ffdeef32SWill Andrews uint64_t pte_u64 = pte_bit_id / BITS_IN(*bitmap); 416ffdeef32SWill Andrews uint64_t popcount_id = pte_bit_id / POPCOUNT_BITS; 417ffdeef32SWill Andrews uint64_t pte_mask = 1ULL << (pte_bit_id % BITS_IN(*bitmap)); 418ffdeef32SWill Andrews uint64_t bitN; 419ffdeef32SWill Andrews uint32_t count; 420197eca22SWill Andrews 421ffdeef32SWill Andrews /* Check whether the page address requested is in the dump. */ 422ffdeef32SWill Andrews if (pte_bit_id >= (kd->pt_map_size * NBBY) || 423ffdeef32SWill Andrews (bitmap[pte_u64] & pte_mask) == 0) 424ffdeef32SWill Andrews return (-1); 425ffdeef32SWill Andrews 426ffdeef32SWill Andrews /* 427ffdeef32SWill Andrews * Add/sub popcounts from the bitmap until the PTE's bit is reached. 428ffdeef32SWill Andrews * For bits that are in the upper half between the calculated 429ffdeef32SWill Andrews * popcount id and the next one, use the next one and subtract to 430ffdeef32SWill Andrews * minimize the number of popcounts required. 431ffdeef32SWill Andrews */ 432ffdeef32SWill Andrews if ((pte_bit_id % POPCOUNT_BITS) < (POPCOUNT_BITS / 2)) { 433ffdeef32SWill Andrews count = kd->pt_popcounts[popcount_id] + popcount_bytes( 434ffdeef32SWill Andrews bitmap + popcount_id * POPCOUNTS_IN(*bitmap), 435ffdeef32SWill Andrews 0, pte_bit_id - popcount_id * POPCOUNT_BITS); 436ffdeef32SWill Andrews } else { 437ffdeef32SWill Andrews /* 438ffdeef32SWill Andrews * Counting in reverse is trickier, since we must avoid 439ffdeef32SWill Andrews * reading from bytes that are not in range, and invert. 440ffdeef32SWill Andrews */ 441ffdeef32SWill Andrews uint64_t pte_u64_bit_off = pte_u64 * BITS_IN(*bitmap); 442ffdeef32SWill Andrews 443ffdeef32SWill Andrews popcount_id++; 444ffdeef32SWill Andrews bitN = MIN(popcount_id * POPCOUNT_BITS, 445ffdeef32SWill Andrews kd->pt_map_size * BITS_IN(uint8_t)); 446ffdeef32SWill Andrews count = kd->pt_popcounts[popcount_id] - popcount_bytes( 447ffdeef32SWill Andrews bitmap + pte_u64, 448ffdeef32SWill Andrews pte_bit_id - pte_u64_bit_off, bitN - pte_u64_bit_off); 449197eca22SWill Andrews } 450ffdeef32SWill Andrews 451ffdeef32SWill Andrews /* 452ffdeef32SWill Andrews * This can only happen if the core is truncated. Treat these 453ffdeef32SWill Andrews * entries as if they don't exist, since their backing doesn't. 454ffdeef32SWill Andrews */ 455c9057838SWill Andrews if (count >= (kd->pt_sparse_size / page_size)) 456ffdeef32SWill Andrews return (-1); 457ffdeef32SWill Andrews 458c9057838SWill Andrews return (kd->pt_sparse_off + (uint64_t)count * page_size); 459197eca22SWill Andrews } 460197eca22SWill Andrews 461197eca22SWill Andrews static int 462197eca22SWill Andrews kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list) 463197eca22SWill Andrews { 464197eca22SWill Andrews kvaddr_t addr; 465197eca22SWill Andrews int error, nfail; 466197eca22SWill Andrews 467197eca22SWill Andrews if (kd->resolve_symbol == NULL) { 468197eca22SWill Andrews struct nlist *nl; 469197eca22SWill Andrews int count, i; 470197eca22SWill Andrews 471197eca22SWill Andrews for (count = 0; list[count].n_name != NULL && 472197eca22SWill Andrews list[count].n_name[0] != '\0'; count++) 473197eca22SWill Andrews ; 474197eca22SWill Andrews nl = calloc(count + 1, sizeof(*nl)); 475197eca22SWill Andrews for (i = 0; i < count; i++) 476197eca22SWill Andrews nl[i].n_name = list[i].n_name; 477197eca22SWill Andrews nfail = __fdnlist(kd->nlfd, nl); 478197eca22SWill Andrews for (i = 0; i < count; i++) { 479197eca22SWill Andrews list[i].n_type = nl[i].n_type; 480197eca22SWill Andrews list[i].n_value = nl[i].n_value; 481197eca22SWill Andrews } 482197eca22SWill Andrews free(nl); 483197eca22SWill Andrews return (nfail); 484197eca22SWill Andrews } 485197eca22SWill Andrews 486197eca22SWill Andrews nfail = 0; 487197eca22SWill Andrews while (list->n_name != NULL && list->n_name[0] != '\0') { 488197eca22SWill Andrews error = kd->resolve_symbol(list->n_name, &addr); 489197eca22SWill Andrews if (error != 0) { 490197eca22SWill Andrews nfail++; 491197eca22SWill Andrews list->n_value = 0; 492197eca22SWill Andrews list->n_type = 0; 493197eca22SWill Andrews } else { 494197eca22SWill Andrews list->n_value = addr; 495197eca22SWill Andrews list->n_type = N_DATA | N_EXT; 496197eca22SWill Andrews } 497197eca22SWill Andrews list++; 498197eca22SWill Andrews } 499197eca22SWill Andrews return (nfail); 500197eca22SWill Andrews } 501197eca22SWill Andrews 502197eca22SWill Andrews /* 503197eca22SWill Andrews * Walk the list of unresolved symbols, generate a new list and prefix the 504197eca22SWill Andrews * symbol names, try again, and merge back what we could resolve. 505197eca22SWill Andrews */ 506197eca22SWill Andrews static int 507197eca22SWill Andrews kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing, 508197eca22SWill Andrews const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t)) 509197eca22SWill Andrews { 510197eca22SWill Andrews struct kvm_nlist *n, *np, *p; 511197eca22SWill Andrews char *cp, *ce; 512197eca22SWill Andrews const char *ccp; 513197eca22SWill Andrews size_t len; 514197eca22SWill Andrews int slen, unresolved; 515197eca22SWill Andrews 516197eca22SWill Andrews /* 517197eca22SWill Andrews * Calculate the space we need to malloc for nlist and names. 518197eca22SWill Andrews * We are going to store the name twice for later lookups: once 519197eca22SWill Andrews * with the prefix and once the unmodified name delmited by \0. 520197eca22SWill Andrews */ 521197eca22SWill Andrews len = 0; 522197eca22SWill Andrews unresolved = 0; 523197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) { 524197eca22SWill Andrews if (p->n_type != N_UNDF) 525197eca22SWill Andrews continue; 526197eca22SWill Andrews len += sizeof(struct kvm_nlist) + strlen(prefix) + 527197eca22SWill Andrews 2 * (strlen(p->n_name) + 1); 528197eca22SWill Andrews unresolved++; 529197eca22SWill Andrews } 530197eca22SWill Andrews if (unresolved == 0) 531197eca22SWill Andrews return (unresolved); 532197eca22SWill Andrews /* Add space for the terminating nlist entry. */ 533197eca22SWill Andrews len += sizeof(struct kvm_nlist); 534197eca22SWill Andrews unresolved++; 535197eca22SWill Andrews 536197eca22SWill Andrews /* Alloc one chunk for (nlist, [names]) and setup pointers. */ 537197eca22SWill Andrews n = np = malloc(len); 538197eca22SWill Andrews bzero(n, len); 539197eca22SWill Andrews if (n == NULL) 540197eca22SWill Andrews return (missing); 541197eca22SWill Andrews cp = ce = (char *)np; 542197eca22SWill Andrews cp += unresolved * sizeof(struct kvm_nlist); 543197eca22SWill Andrews ce += len; 544197eca22SWill Andrews 545197eca22SWill Andrews /* Generate shortened nlist with special prefix. */ 546197eca22SWill Andrews unresolved = 0; 547197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) { 548197eca22SWill Andrews if (p->n_type != N_UNDF) 549197eca22SWill Andrews continue; 550197eca22SWill Andrews *np = *p; 551197eca22SWill Andrews /* Save the new\0orig. name so we can later match it again. */ 552197eca22SWill Andrews slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix, 553197eca22SWill Andrews (prefix[0] != '\0' && p->n_name[0] == '_') ? 554197eca22SWill Andrews (p->n_name + 1) : p->n_name, '\0', p->n_name); 555197eca22SWill Andrews if (slen < 0 || slen >= ce - cp) 556197eca22SWill Andrews continue; 557197eca22SWill Andrews np->n_name = cp; 558197eca22SWill Andrews cp += slen + 1; 559197eca22SWill Andrews np++; 560197eca22SWill Andrews unresolved++; 561197eca22SWill Andrews } 562197eca22SWill Andrews 563197eca22SWill Andrews /* Do lookup on the reduced list. */ 564197eca22SWill Andrews np = n; 565197eca22SWill Andrews unresolved = kvm_fdnlist(kd, np); 566197eca22SWill Andrews 567197eca22SWill Andrews /* Check if we could resolve further symbols and update the list. */ 568197eca22SWill Andrews if (unresolved >= 0 && unresolved < missing) { 569197eca22SWill Andrews /* Find the first freshly resolved entry. */ 570197eca22SWill Andrews for (; np->n_name && np->n_name[0]; np++) 571197eca22SWill Andrews if (np->n_type != N_UNDF) 572197eca22SWill Andrews break; 573197eca22SWill Andrews /* 574197eca22SWill Andrews * The lists are both in the same order, 575197eca22SWill Andrews * so we can walk them in parallel. 576197eca22SWill Andrews */ 577197eca22SWill Andrews for (p = nl; np->n_name && np->n_name[0] && 578197eca22SWill Andrews p->n_name && p->n_name[0]; ++p) { 579197eca22SWill Andrews if (p->n_type != N_UNDF) 580197eca22SWill Andrews continue; 581197eca22SWill Andrews /* Skip expanded name and compare to orig. one. */ 582197eca22SWill Andrews ccp = np->n_name + strlen(np->n_name) + 1; 583197eca22SWill Andrews if (strcmp(ccp, p->n_name) != 0) 584197eca22SWill Andrews continue; 585197eca22SWill Andrews /* Update nlist with new, translated results. */ 586197eca22SWill Andrews p->n_type = np->n_type; 587197eca22SWill Andrews if (validate_fn) 588197eca22SWill Andrews p->n_value = (*validate_fn)(kd, np->n_value); 589197eca22SWill Andrews else 590197eca22SWill Andrews p->n_value = np->n_value; 591197eca22SWill Andrews missing--; 592197eca22SWill Andrews /* Find next freshly resolved entry. */ 593197eca22SWill Andrews for (np++; np->n_name && np->n_name[0]; np++) 594197eca22SWill Andrews if (np->n_type != N_UNDF) 595197eca22SWill Andrews break; 596197eca22SWill Andrews } 597197eca22SWill Andrews } 598197eca22SWill Andrews /* We could assert missing = unresolved here. */ 599197eca22SWill Andrews 600197eca22SWill Andrews free(n); 601197eca22SWill Andrews return (unresolved); 602197eca22SWill Andrews } 603197eca22SWill Andrews 604197eca22SWill Andrews int 605197eca22SWill Andrews _kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize) 606197eca22SWill Andrews { 607197eca22SWill Andrews struct kvm_nlist *p; 608197eca22SWill Andrews int nvalid; 609197eca22SWill Andrews struct kld_sym_lookup lookup; 610197eca22SWill Andrews int error; 611197eca22SWill Andrews const char *prefix = ""; 612197eca22SWill Andrews char symname[1024]; /* XXX-BZ symbol name length limit? */ 613197eca22SWill Andrews int tried_vnet, tried_dpcpu; 614197eca22SWill Andrews 615197eca22SWill Andrews /* 616197eca22SWill Andrews * If we can't use the kld symbol lookup, revert to the 617197eca22SWill Andrews * slow library call. 618197eca22SWill Andrews */ 619197eca22SWill Andrews if (!ISALIVE(kd)) { 620197eca22SWill Andrews error = kvm_fdnlist(kd, nl); 621197eca22SWill Andrews if (error <= 0) /* Hard error or success. */ 622197eca22SWill Andrews return (error); 623197eca22SWill Andrews 624197eca22SWill Andrews if (_kvm_vnet_initialized(kd, initialize)) 625197eca22SWill Andrews error = kvm_fdnlist_prefix(kd, nl, error, 626197eca22SWill Andrews VNET_SYMPREFIX, _kvm_vnet_validaddr); 627197eca22SWill Andrews 628197eca22SWill Andrews if (error > 0 && _kvm_dpcpu_initialized(kd, initialize)) 629197eca22SWill Andrews error = kvm_fdnlist_prefix(kd, nl, error, 630197eca22SWill Andrews DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr); 631197eca22SWill Andrews 632197eca22SWill Andrews return (error); 633197eca22SWill Andrews } 634197eca22SWill Andrews 635197eca22SWill Andrews /* 636197eca22SWill Andrews * We can use the kld lookup syscall. Go through each nlist entry 637197eca22SWill Andrews * and look it up with a kldsym(2) syscall. 638197eca22SWill Andrews */ 639197eca22SWill Andrews nvalid = 0; 640197eca22SWill Andrews tried_vnet = 0; 641197eca22SWill Andrews tried_dpcpu = 0; 642197eca22SWill Andrews again: 643197eca22SWill Andrews for (p = nl; p->n_name && p->n_name[0]; ++p) { 644197eca22SWill Andrews if (p->n_type != N_UNDF) 645197eca22SWill Andrews continue; 646197eca22SWill Andrews 647197eca22SWill Andrews lookup.version = sizeof(lookup); 648197eca22SWill Andrews lookup.symvalue = 0; 649197eca22SWill Andrews lookup.symsize = 0; 650197eca22SWill Andrews 651197eca22SWill Andrews error = snprintf(symname, sizeof(symname), "%s%s", prefix, 652197eca22SWill Andrews (prefix[0] != '\0' && p->n_name[0] == '_') ? 653197eca22SWill Andrews (p->n_name + 1) : p->n_name); 654197eca22SWill Andrews if (error < 0 || error >= (int)sizeof(symname)) 655197eca22SWill Andrews continue; 656197eca22SWill Andrews lookup.symname = symname; 657197eca22SWill Andrews if (lookup.symname[0] == '_') 658197eca22SWill Andrews lookup.symname++; 659197eca22SWill Andrews 660197eca22SWill Andrews if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) { 661197eca22SWill Andrews p->n_type = N_TEXT; 662197eca22SWill Andrews if (_kvm_vnet_initialized(kd, initialize) && 663197eca22SWill Andrews strcmp(prefix, VNET_SYMPREFIX) == 0) 664197eca22SWill Andrews p->n_value = 665197eca22SWill Andrews _kvm_vnet_validaddr(kd, lookup.symvalue); 666197eca22SWill Andrews else if (_kvm_dpcpu_initialized(kd, initialize) && 667197eca22SWill Andrews strcmp(prefix, DPCPU_SYMPREFIX) == 0) 668197eca22SWill Andrews p->n_value = 669197eca22SWill Andrews _kvm_dpcpu_validaddr(kd, lookup.symvalue); 670197eca22SWill Andrews else 671197eca22SWill Andrews p->n_value = lookup.symvalue; 672197eca22SWill Andrews ++nvalid; 673197eca22SWill Andrews /* lookup.symsize */ 674197eca22SWill Andrews } 675197eca22SWill Andrews } 676197eca22SWill Andrews 677197eca22SWill Andrews /* 678197eca22SWill Andrews * Check the number of entries that weren't found. If they exist, 679197eca22SWill Andrews * try again with a prefix for virtualized or DPCPU symbol names. 680197eca22SWill Andrews */ 681197eca22SWill Andrews error = ((p - nl) - nvalid); 682197eca22SWill Andrews if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) { 683197eca22SWill Andrews tried_vnet = 1; 684197eca22SWill Andrews prefix = VNET_SYMPREFIX; 685197eca22SWill Andrews goto again; 686197eca22SWill Andrews } 687197eca22SWill Andrews if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) { 688197eca22SWill Andrews tried_dpcpu = 1; 689197eca22SWill Andrews prefix = DPCPU_SYMPREFIX; 690197eca22SWill Andrews goto again; 691197eca22SWill Andrews } 692197eca22SWill Andrews 693197eca22SWill Andrews /* 694197eca22SWill Andrews * Return the number of entries that weren't found. If they exist, 695197eca22SWill Andrews * also fill internal error buffer. 696197eca22SWill Andrews */ 697197eca22SWill Andrews error = ((p - nl) - nvalid); 698197eca22SWill Andrews if (error) 699197eca22SWill Andrews _kvm_syserr(kd, kd->program, "kvm_nlist"); 700197eca22SWill Andrews return (error); 701197eca22SWill Andrews } 702c9057838SWill Andrews 703c9057838SWill Andrews int 7042aa6a4f3SWill Andrews _kvm_bitmap_init(struct kvm_bitmap *bm, u_long bitmapsize, u_long *idx) 705c9057838SWill Andrews { 706c9057838SWill Andrews 7072aa6a4f3SWill Andrews *idx = ULONG_MAX; 708c9057838SWill Andrews bm->map = calloc(bitmapsize, sizeof *bm->map); 709c9057838SWill Andrews if (bm->map == NULL) 710c9057838SWill Andrews return (0); 711c9057838SWill Andrews bm->size = bitmapsize; 712c9057838SWill Andrews return (1); 713c9057838SWill Andrews } 714c9057838SWill Andrews 715c9057838SWill Andrews void 716c9057838SWill Andrews _kvm_bitmap_set(struct kvm_bitmap *bm, u_long pa, unsigned int page_size) 717c9057838SWill Andrews { 718c9057838SWill Andrews u_long bm_index = pa / page_size; 719c9057838SWill Andrews uint8_t *byte = &bm->map[bm_index / 8]; 720c9057838SWill Andrews 721c9057838SWill Andrews *byte |= (1UL << (bm_index % 8)); 722c9057838SWill Andrews } 723c9057838SWill Andrews 724c9057838SWill Andrews int 7252aa6a4f3SWill Andrews _kvm_bitmap_next(struct kvm_bitmap *bm, u_long *idx) 726c9057838SWill Andrews { 727c9057838SWill Andrews u_long first_invalid = bm->size * CHAR_BIT; 728c9057838SWill Andrews 7292aa6a4f3SWill Andrews if (*idx == ULONG_MAX) 7302aa6a4f3SWill Andrews *idx = 0; 731c9057838SWill Andrews else 7322aa6a4f3SWill Andrews (*idx)++; 733c9057838SWill Andrews 7342aa6a4f3SWill Andrews /* Find the next valid idx. */ 7352aa6a4f3SWill Andrews for (; *idx < first_invalid; (*idx)++) { 7362aa6a4f3SWill Andrews unsigned int mask = *idx % CHAR_BIT; 7372aa6a4f3SWill Andrews if ((bm->map[*idx * CHAR_BIT] & mask) == 0) 738c9057838SWill Andrews break; 739c9057838SWill Andrews } 740c9057838SWill Andrews 7412aa6a4f3SWill Andrews return (*idx < first_invalid); 742c9057838SWill Andrews } 743c9057838SWill Andrews 744c9057838SWill Andrews void 745c9057838SWill Andrews _kvm_bitmap_deinit(struct kvm_bitmap *bm) 746c9057838SWill Andrews { 747c9057838SWill Andrews 748c9057838SWill Andrews free(bm->map); 749c9057838SWill Andrews } 750c9057838SWill Andrews 751c9057838SWill Andrews int 752c9057838SWill Andrews _kvm_visit_cb(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg, u_long pa, 753c9057838SWill Andrews u_long kmap_vaddr, u_long dmap_vaddr, vm_prot_t prot, size_t len, 754c9057838SWill Andrews unsigned int page_size) 755c9057838SWill Andrews { 756c9057838SWill Andrews unsigned int pgsz = page_size ? page_size : len; 757c9057838SWill Andrews struct kvm_page p = { 758c9057838SWill Andrews .version = LIBKVM_WALK_PAGES_VERSION, 759c9057838SWill Andrews .paddr = pa, 760c9057838SWill Andrews .kmap_vaddr = kmap_vaddr, 761c9057838SWill Andrews .dmap_vaddr = dmap_vaddr, 762c9057838SWill Andrews .prot = prot, 763c9057838SWill Andrews .offset = _kvm_pt_find(kd, pa, pgsz), 764c9057838SWill Andrews .len = len, 765c9057838SWill Andrews }; 766c9057838SWill Andrews 767c9057838SWill Andrews return cb(&p, arg); 768c9057838SWill Andrews } 769