kvm.c revision 315785
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 * 4. 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: stable/11/lib/libkvm/kvm.c 315785 2017-03-23 04:50:44Z ngie $"); 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94"; 40#endif 41#endif /* LIBC_SCCS and not lint */ 42 43#include <sys/param.h> 44#include <sys/fnv_hash.h> 45 46#define _WANT_VNET 47 48#include <sys/user.h> 49#include <sys/linker.h> 50#include <sys/pcpu.h> 51#include <sys/stat.h> 52 53#include <net/vnet.h> 54 55#include <fcntl.h> 56#include <kvm.h> 57#include <limits.h> 58#include <paths.h> 59#include <stdint.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <string.h> 63#include <unistd.h> 64 65#include "kvm_private.h" 66 67SET_DECLARE(kvm_arch, struct kvm_arch); 68 69/* from src/lib/libc/gen/nlist.c */ 70int __fdnlist(int, struct nlist *); 71 72static int 73kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list) 74{ 75 kvaddr_t addr; 76 int error, nfail; 77 78 if (kd->resolve_symbol == NULL) { 79 struct nlist *nl; 80 int count, i; 81 82 for (count = 0; list[count].n_name != NULL && 83 list[count].n_name[0] != '\0'; count++) 84 ; 85 nl = calloc(count + 1, sizeof(*nl)); 86 for (i = 0; i < count; i++) 87 nl[i].n_name = list[i].n_name; 88 nfail = __fdnlist(kd->nlfd, nl); 89 for (i = 0; i < count; i++) { 90 list[i].n_type = nl[i].n_type; 91 list[i].n_value = nl[i].n_value; 92 } 93 free(nl); 94 return (nfail); 95 } 96 97 nfail = 0; 98 while (list->n_name != NULL && list->n_name[0] != '\0') { 99 error = kd->resolve_symbol(list->n_name, &addr); 100 if (error != 0) { 101 nfail++; 102 list->n_value = 0; 103 list->n_type = 0; 104 } else { 105 list->n_value = addr; 106 list->n_type = N_DATA | N_EXT; 107 } 108 list++; 109 } 110 return (nfail); 111} 112 113char * 114kvm_geterr(kvm_t *kd) 115{ 116 return (kd->errbuf); 117} 118 119#include <stdarg.h> 120 121/* 122 * Report an error using printf style arguments. "program" is kd->program 123 * on hard errors, and 0 on soft errors, so that under sun error emulation, 124 * only hard errors are printed out (otherwise, programs like gdb will 125 * generate tons of error messages when trying to access bogus pointers). 126 */ 127void 128_kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) 129{ 130 va_list ap; 131 132 va_start(ap, fmt); 133 if (program != NULL) { 134 (void)fprintf(stderr, "%s: ", program); 135 (void)vfprintf(stderr, fmt, ap); 136 (void)fputc('\n', stderr); 137 } else 138 (void)vsnprintf(kd->errbuf, 139 sizeof(kd->errbuf), fmt, ap); 140 141 va_end(ap); 142} 143 144void 145_kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) 146{ 147 va_list ap; 148 int n; 149 150 va_start(ap, fmt); 151 if (program != NULL) { 152 (void)fprintf(stderr, "%s: ", program); 153 (void)vfprintf(stderr, fmt, ap); 154 (void)fprintf(stderr, ": %s\n", strerror(errno)); 155 } else { 156 char *cp = kd->errbuf; 157 158 (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap); 159 n = strlen(cp); 160 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s", 161 strerror(errno)); 162 } 163 va_end(ap); 164} 165 166void * 167_kvm_malloc(kvm_t *kd, size_t n) 168{ 169 void *p; 170 171 if ((p = calloc(n, sizeof(char))) == NULL) 172 _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s", 173 n, strerror(errno)); 174 return (p); 175} 176 177static int 178_kvm_read_kernel_ehdr(kvm_t *kd) 179{ 180 Elf *elf; 181 182 if (elf_version(EV_CURRENT) == EV_NONE) { 183 _kvm_err(kd, kd->program, "Unsupported libelf"); 184 return (-1); 185 } 186 elf = elf_begin(kd->nlfd, ELF_C_READ, NULL); 187 if (elf == NULL) { 188 _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 189 return (-1); 190 } 191 if (elf_kind(elf) != ELF_K_ELF) { 192 _kvm_err(kd, kd->program, "kernel is not an ELF file"); 193 return (-1); 194 } 195 if (gelf_getehdr(elf, &kd->nlehdr) == NULL) { 196 _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 197 elf_end(elf); 198 return (-1); 199 } 200 elf_end(elf); 201 202 switch (kd->nlehdr.e_ident[EI_DATA]) { 203 case ELFDATA2LSB: 204 case ELFDATA2MSB: 205 return (0); 206 default: 207 _kvm_err(kd, kd->program, 208 "unsupported ELF data encoding for kernel"); 209 return (-1); 210 } 211} 212 213int 214_kvm_probe_elf_kernel(kvm_t *kd, int class, int machine) 215{ 216 217 return (kd->nlehdr.e_ident[EI_CLASS] == class && 218 kd->nlehdr.e_type == ET_EXEC && 219 kd->nlehdr.e_machine == machine); 220} 221 222int 223_kvm_is_minidump(kvm_t *kd) 224{ 225 char minihdr[8]; 226 227 if (kd->rawdump) 228 return (0); 229 if (pread(kd->pmfd, &minihdr, 8, 0) == 8 && 230 memcmp(&minihdr, "minidump", 8) == 0) 231 return (1); 232 return (0); 233} 234 235/* 236 * The powerpc backend has a hack to strip a leading kerneldump 237 * header from the core before treating it as an ELF header. 238 * 239 * We can add that here if we can get a change to libelf to support 240 * an initial offset into the file. Alternatively we could patch 241 * savecore to extract cores from a regular file instead. 242 */ 243int 244_kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp) 245{ 246 GElf_Ehdr ehdr; 247 GElf_Phdr *phdr; 248 Elf *elf; 249 size_t i, phnum; 250 251 elf = elf_begin(kd->pmfd, ELF_C_READ, NULL); 252 if (elf == NULL) { 253 _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 254 return (-1); 255 } 256 if (elf_kind(elf) != ELF_K_ELF) { 257 _kvm_err(kd, kd->program, "invalid core"); 258 goto bad; 259 } 260 if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) { 261 _kvm_err(kd, kd->program, "invalid core"); 262 goto bad; 263 } 264 if (gelf_getehdr(elf, &ehdr) == NULL) { 265 _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 266 goto bad; 267 } 268 if (ehdr.e_type != ET_CORE) { 269 _kvm_err(kd, kd->program, "invalid core"); 270 goto bad; 271 } 272 if (ehdr.e_machine != kd->nlehdr.e_machine) { 273 _kvm_err(kd, kd->program, "invalid core"); 274 goto bad; 275 } 276 277 if (elf_getphdrnum(elf, &phnum) == -1) { 278 _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 279 goto bad; 280 } 281 282 phdr = calloc(phnum, sizeof(*phdr)); 283 if (phdr == NULL) { 284 _kvm_err(kd, kd->program, "failed to allocate phdrs"); 285 goto bad; 286 } 287 288 for (i = 0; i < phnum; i++) { 289 if (gelf_getphdr(elf, i, &phdr[i]) == NULL) { 290 _kvm_err(kd, kd->program, "%s", elf_errmsg(0)); 291 goto bad; 292 } 293 } 294 elf_end(elf); 295 *phnump = phnum; 296 *phdrp = phdr; 297 return (0); 298 299bad: 300 elf_end(elf); 301 return (-1); 302} 303 304static void 305_kvm_hpt_insert(struct hpt *hpt, uint64_t pa, off_t off) 306{ 307 struct hpte *hpte; 308 uint32_t fnv = FNV1_32_INIT; 309 310 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 311 fnv &= (HPT_SIZE - 1); 312 hpte = malloc(sizeof(*hpte)); 313 hpte->pa = pa; 314 hpte->off = off; 315 hpte->next = hpt->hpt_head[fnv]; 316 hpt->hpt_head[fnv] = hpte; 317} 318 319void 320_kvm_hpt_init(kvm_t *kd, struct hpt *hpt, void *base, size_t len, off_t off, 321 int page_size, int word_size) 322{ 323 uint64_t bits, idx, pa; 324 uint64_t *base64; 325 uint32_t *base32; 326 327 base64 = base; 328 base32 = base; 329 for (idx = 0; idx < len / word_size; idx++) { 330 if (word_size == sizeof(uint64_t)) 331 bits = _kvm64toh(kd, base64[idx]); 332 else 333 bits = _kvm32toh(kd, base32[idx]); 334 pa = idx * word_size * NBBY * page_size; 335 for (; bits != 0; bits >>= 1, pa += page_size) { 336 if ((bits & 1) == 0) 337 continue; 338 _kvm_hpt_insert(hpt, pa, off); 339 off += page_size; 340 } 341 } 342} 343 344off_t 345_kvm_hpt_find(struct hpt *hpt, uint64_t pa) 346{ 347 struct hpte *hpte; 348 uint32_t fnv = FNV1_32_INIT; 349 350 fnv = fnv_32_buf(&pa, sizeof(pa), fnv); 351 fnv &= (HPT_SIZE - 1); 352 for (hpte = hpt->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) { 353 if (pa == hpte->pa) 354 return (hpte->off); 355 } 356 return (-1); 357} 358 359void 360_kvm_hpt_free(struct hpt *hpt) 361{ 362 struct hpte *hpte, *next; 363 int i; 364 365 for (i = 0; i < HPT_SIZE; i++) { 366 for (hpte = hpt->hpt_head[i]; hpte != NULL; hpte = next) { 367 next = hpte->next; 368 free(hpte); 369 } 370 } 371} 372 373static kvm_t * 374_kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout) 375{ 376 struct kvm_arch **parch; 377 struct stat st; 378 379 kd->vmfd = -1; 380 kd->pmfd = -1; 381 kd->nlfd = -1; 382 kd->vmst = NULL; 383 kd->procbase = NULL; 384 kd->argspc = NULL; 385 kd->argv = NULL; 386 387 if (uf == NULL) 388 uf = getbootfile(); 389 else if (strlen(uf) >= MAXPATHLEN) { 390 _kvm_err(kd, kd->program, "exec file name too long"); 391 goto failed; 392 } 393 if (flag & ~O_RDWR) { 394 _kvm_err(kd, kd->program, "bad flags arg"); 395 goto failed; 396 } 397 if (mf == NULL) 398 mf = _PATH_MEM; 399 400 if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) { 401 _kvm_syserr(kd, kd->program, "%s", mf); 402 goto failed; 403 } 404 if (fstat(kd->pmfd, &st) < 0) { 405 _kvm_syserr(kd, kd->program, "%s", mf); 406 goto failed; 407 } 408 if (S_ISREG(st.st_mode) && st.st_size <= 0) { 409 errno = EINVAL; 410 _kvm_syserr(kd, kd->program, "empty file"); 411 goto failed; 412 } 413 if (S_ISCHR(st.st_mode)) { 414 /* 415 * If this is a character special device, then check that 416 * it's /dev/mem. If so, open kmem too. (Maybe we should 417 * make it work for either /dev/mem or /dev/kmem -- in either 418 * case you're working with a live kernel.) 419 */ 420 if (strcmp(mf, _PATH_DEVNULL) == 0) { 421 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC); 422 return (kd); 423 } else if (strcmp(mf, _PATH_MEM) == 0) { 424 if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) < 425 0) { 426 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM); 427 goto failed; 428 } 429 return (kd); 430 } 431 } 432 433 /* 434 * This is either a crash dump or a remote live system with its physical 435 * memory fully accessible via a special device. 436 * Open the namelist fd and determine the architecture. 437 */ 438 if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) { 439 _kvm_syserr(kd, kd->program, "%s", uf); 440 goto failed; 441 } 442 if (_kvm_read_kernel_ehdr(kd) < 0) 443 goto failed; 444 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 || 445 strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) { 446 kd->rawdump = 1; 447 kd->writable = 1; 448 } 449 SET_FOREACH(parch, kvm_arch) { 450 if ((*parch)->ka_probe(kd)) { 451 kd->arch = *parch; 452 break; 453 } 454 } 455 if (kd->arch == NULL) { 456 _kvm_err(kd, kd->program, "unsupported architecture"); 457 goto failed; 458 } 459 460 /* 461 * Non-native kernels require a symbol resolver. 462 */ 463 if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) { 464 _kvm_err(kd, kd->program, 465 "non-native kernel requires a symbol resolver"); 466 goto failed; 467 } 468 469 /* 470 * Initialize the virtual address translation machinery. 471 */ 472 if (kd->arch->ka_initvtop(kd) < 0) 473 goto failed; 474 return (kd); 475failed: 476 /* 477 * Copy out the error if doing sane error semantics. 478 */ 479 if (errout != NULL) 480 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX); 481 (void)kvm_close(kd); 482 return (NULL); 483} 484 485kvm_t * 486kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag, 487 char *errout) 488{ 489 kvm_t *kd; 490 491 if ((kd = calloc(1, sizeof(*kd))) == NULL) { 492 if (errout != NULL) 493 (void)strlcpy(errout, strerror(errno), 494 _POSIX2_LINE_MAX); 495 return (NULL); 496 } 497 return (_kvm_open(kd, uf, mf, flag, errout)); 498} 499 500kvm_t * 501kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag, 502 const char *errstr) 503{ 504 kvm_t *kd; 505 506 if ((kd = calloc(1, sizeof(*kd))) == NULL) { 507 if (errstr != NULL) 508 (void)fprintf(stderr, "%s: %s\n", 509 errstr, strerror(errno)); 510 return (NULL); 511 } 512 kd->program = errstr; 513 return (_kvm_open(kd, uf, mf, flag, NULL)); 514} 515 516kvm_t * 517kvm_open2(const char *uf, const char *mf, int flag, char *errout, 518 int (*resolver)(const char *, kvaddr_t *)) 519{ 520 kvm_t *kd; 521 522 if ((kd = calloc(1, sizeof(*kd))) == NULL) { 523 if (errout != NULL) 524 (void)strlcpy(errout, strerror(errno), 525 _POSIX2_LINE_MAX); 526 return (NULL); 527 } 528 kd->resolve_symbol = resolver; 529 return (_kvm_open(kd, uf, mf, flag, errout)); 530} 531 532int 533kvm_close(kvm_t *kd) 534{ 535 int error = 0; 536 537 if (kd->vmst != NULL) 538 kd->arch->ka_freevtop(kd); 539 if (kd->pmfd >= 0) 540 error |= close(kd->pmfd); 541 if (kd->vmfd >= 0) 542 error |= close(kd->vmfd); 543 if (kd->nlfd >= 0) 544 error |= close(kd->nlfd); 545 if (kd->procbase != 0) 546 free((void *)kd->procbase); 547 if (kd->argbuf != 0) 548 free((void *) kd->argbuf); 549 if (kd->argspc != 0) 550 free((void *) kd->argspc); 551 if (kd->argv != 0) 552 free((void *)kd->argv); 553 free((void *)kd); 554 555 return (0); 556} 557 558/* 559 * Walk the list of unresolved symbols, generate a new list and prefix the 560 * symbol names, try again, and merge back what we could resolve. 561 */ 562static int 563kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing, 564 const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t)) 565{ 566 struct kvm_nlist *n, *np, *p; 567 char *cp, *ce; 568 const char *ccp; 569 size_t len; 570 int slen, unresolved; 571 572 /* 573 * Calculate the space we need to malloc for nlist and names. 574 * We are going to store the name twice for later lookups: once 575 * with the prefix and once the unmodified name delmited by \0. 576 */ 577 len = 0; 578 unresolved = 0; 579 for (p = nl; p->n_name && p->n_name[0]; ++p) { 580 if (p->n_type != N_UNDF) 581 continue; 582 len += sizeof(struct kvm_nlist) + strlen(prefix) + 583 2 * (strlen(p->n_name) + 1); 584 unresolved++; 585 } 586 if (unresolved == 0) 587 return (unresolved); 588 /* Add space for the terminating nlist entry. */ 589 len += sizeof(struct kvm_nlist); 590 unresolved++; 591 592 /* Alloc one chunk for (nlist, [names]) and setup pointers. */ 593 n = np = malloc(len); 594 bzero(n, len); 595 if (n == NULL) 596 return (missing); 597 cp = ce = (char *)np; 598 cp += unresolved * sizeof(struct kvm_nlist); 599 ce += len; 600 601 /* Generate shortened nlist with special prefix. */ 602 unresolved = 0; 603 for (p = nl; p->n_name && p->n_name[0]; ++p) { 604 if (p->n_type != N_UNDF) 605 continue; 606 *np = *p; 607 /* Save the new\0orig. name so we can later match it again. */ 608 slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix, 609 (prefix[0] != '\0' && p->n_name[0] == '_') ? 610 (p->n_name + 1) : p->n_name, '\0', p->n_name); 611 if (slen < 0 || slen >= ce - cp) 612 continue; 613 np->n_name = cp; 614 cp += slen + 1; 615 np++; 616 unresolved++; 617 } 618 619 /* Do lookup on the reduced list. */ 620 np = n; 621 unresolved = kvm_fdnlist(kd, np); 622 623 /* Check if we could resolve further symbols and update the list. */ 624 if (unresolved >= 0 && unresolved < missing) { 625 /* Find the first freshly resolved entry. */ 626 for (; np->n_name && np->n_name[0]; np++) 627 if (np->n_type != N_UNDF) 628 break; 629 /* 630 * The lists are both in the same order, 631 * so we can walk them in parallel. 632 */ 633 for (p = nl; np->n_name && np->n_name[0] && 634 p->n_name && p->n_name[0]; ++p) { 635 if (p->n_type != N_UNDF) 636 continue; 637 /* Skip expanded name and compare to orig. one. */ 638 ccp = np->n_name + strlen(np->n_name) + 1; 639 if (strcmp(ccp, p->n_name) != 0) 640 continue; 641 /* Update nlist with new, translated results. */ 642 p->n_type = np->n_type; 643 if (validate_fn) 644 p->n_value = (*validate_fn)(kd, np->n_value); 645 else 646 p->n_value = np->n_value; 647 missing--; 648 /* Find next freshly resolved entry. */ 649 for (np++; np->n_name && np->n_name[0]; np++) 650 if (np->n_type != N_UNDF) 651 break; 652 } 653 } 654 /* We could assert missing = unresolved here. */ 655 656 free(n); 657 return (unresolved); 658} 659 660int 661_kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize) 662{ 663 struct kvm_nlist *p; 664 int nvalid; 665 struct kld_sym_lookup lookup; 666 int error; 667 const char *prefix = ""; 668 char symname[1024]; /* XXX-BZ symbol name length limit? */ 669 int tried_vnet, tried_dpcpu; 670 671 /* 672 * If we can't use the kld symbol lookup, revert to the 673 * slow library call. 674 */ 675 if (!ISALIVE(kd)) { 676 error = kvm_fdnlist(kd, nl); 677 if (error <= 0) /* Hard error or success. */ 678 return (error); 679 680 if (_kvm_vnet_initialized(kd, initialize)) 681 error = kvm_fdnlist_prefix(kd, nl, error, 682 VNET_SYMPREFIX, _kvm_vnet_validaddr); 683 684 if (error > 0 && _kvm_dpcpu_initialized(kd, initialize)) 685 error = kvm_fdnlist_prefix(kd, nl, error, 686 DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr); 687 688 return (error); 689 } 690 691 /* 692 * We can use the kld lookup syscall. Go through each nlist entry 693 * and look it up with a kldsym(2) syscall. 694 */ 695 nvalid = 0; 696 tried_vnet = 0; 697 tried_dpcpu = 0; 698again: 699 for (p = nl; p->n_name && p->n_name[0]; ++p) { 700 if (p->n_type != N_UNDF) 701 continue; 702 703 lookup.version = sizeof(lookup); 704 lookup.symvalue = 0; 705 lookup.symsize = 0; 706 707 error = snprintf(symname, sizeof(symname), "%s%s", prefix, 708 (prefix[0] != '\0' && p->n_name[0] == '_') ? 709 (p->n_name + 1) : p->n_name); 710 if (error < 0 || error >= (int)sizeof(symname)) 711 continue; 712 lookup.symname = symname; 713 if (lookup.symname[0] == '_') 714 lookup.symname++; 715 716 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) { 717 p->n_type = N_TEXT; 718 if (_kvm_vnet_initialized(kd, initialize) && 719 strcmp(prefix, VNET_SYMPREFIX) == 0) 720 p->n_value = 721 _kvm_vnet_validaddr(kd, lookup.symvalue); 722 else if (_kvm_dpcpu_initialized(kd, initialize) && 723 strcmp(prefix, DPCPU_SYMPREFIX) == 0) 724 p->n_value = 725 _kvm_dpcpu_validaddr(kd, lookup.symvalue); 726 else 727 p->n_value = lookup.symvalue; 728 ++nvalid; 729 /* lookup.symsize */ 730 } 731 } 732 733 /* 734 * Check the number of entries that weren't found. If they exist, 735 * try again with a prefix for virtualized or DPCPU symbol names. 736 */ 737 error = ((p - nl) - nvalid); 738 if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) { 739 tried_vnet = 1; 740 prefix = VNET_SYMPREFIX; 741 goto again; 742 } 743 if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) { 744 tried_dpcpu = 1; 745 prefix = DPCPU_SYMPREFIX; 746 goto again; 747 } 748 749 /* 750 * Return the number of entries that weren't found. If they exist, 751 * also fill internal error buffer. 752 */ 753 error = ((p - nl) - nvalid); 754 if (error) 755 _kvm_syserr(kd, kd->program, "kvm_nlist"); 756 return (error); 757} 758 759int 760kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl) 761{ 762 763 /* 764 * If called via the public interface, permit initialization of 765 * further virtualized modules on demand. 766 */ 767 return (_kvm_nlist(kd, nl, 1)); 768} 769 770int 771kvm_nlist(kvm_t *kd, struct nlist *nl) 772{ 773 struct kvm_nlist *kl; 774 int count, i, nfail; 775 776 /* 777 * Avoid reporting truncated addresses by failing for non-native 778 * cores. 779 */ 780 if (!kvm_native(kd)) { 781 _kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore"); 782 return (-1); 783 } 784 785 for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0'; 786 count++) 787 ; 788 if (count == 0) 789 return (0); 790 kl = calloc(count + 1, sizeof(*kl)); 791 for (i = 0; i < count; i++) 792 kl[i].n_name = nl[i].n_name; 793 nfail = kvm_nlist2(kd, kl); 794 for (i = 0; i < count; i++) { 795 nl[i].n_type = kl[i].n_type; 796 nl[i].n_other = 0; 797 nl[i].n_desc = 0; 798 nl[i].n_value = kl[i].n_value; 799 } 800 return (nfail); 801} 802 803ssize_t 804kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len) 805{ 806 807 return (kvm_read2(kd, kva, buf, len)); 808} 809 810ssize_t 811kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len) 812{ 813 int cc; 814 ssize_t cr; 815 off_t pa; 816 char *cp; 817 818 if (ISALIVE(kd)) { 819 /* 820 * We're using /dev/kmem. Just read straight from the 821 * device and let the active kernel do the address translation. 822 */ 823 errno = 0; 824 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { 825 _kvm_err(kd, 0, "invalid address (0x%jx)", 826 (uintmax_t)kva); 827 return (-1); 828 } 829 cr = read(kd->vmfd, buf, len); 830 if (cr < 0) { 831 _kvm_syserr(kd, 0, "kvm_read"); 832 return (-1); 833 } else if (cr < (ssize_t)len) 834 _kvm_err(kd, kd->program, "short read"); 835 return (cr); 836 } 837 838 cp = buf; 839 while (len > 0) { 840 cc = kd->arch->ka_kvatop(kd, kva, &pa); 841 if (cc == 0) 842 return (-1); 843 if (cc > (ssize_t)len) 844 cc = len; 845 errno = 0; 846 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) { 847 _kvm_syserr(kd, 0, _PATH_MEM); 848 break; 849 } 850 cr = read(kd->pmfd, cp, cc); 851 if (cr < 0) { 852 _kvm_syserr(kd, kd->program, "kvm_read"); 853 break; 854 } 855 /* 856 * If ka_kvatop returns a bogus value or our core file is 857 * truncated, we might wind up seeking beyond the end of the 858 * core file in which case the read will return 0 (EOF). 859 */ 860 if (cr == 0) 861 break; 862 cp += cr; 863 kva += cr; 864 len -= cr; 865 } 866 867 return (cp - (char *)buf); 868} 869 870ssize_t 871kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len) 872{ 873 int cc; 874 ssize_t cw; 875 off_t pa; 876 const char *cp; 877 878 if (!ISALIVE(kd) && !kd->writable) { 879 _kvm_err(kd, kd->program, 880 "kvm_write not implemented for dead kernels"); 881 return (-1); 882 } 883 884 if (ISALIVE(kd)) { 885 /* 886 * Just like kvm_read, only we write. 887 */ 888 errno = 0; 889 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { 890 _kvm_err(kd, 0, "invalid address (%lx)", kva); 891 return (-1); 892 } 893 cc = write(kd->vmfd, buf, len); 894 if (cc < 0) { 895 _kvm_syserr(kd, 0, "kvm_write"); 896 return (-1); 897 } else if ((size_t)cc < len) 898 _kvm_err(kd, kd->program, "short write"); 899 return (cc); 900 } 901 902 cp = buf; 903 while (len > 0) { 904 cc = kd->arch->ka_kvatop(kd, kva, &pa); 905 if (cc == 0) 906 return (-1); 907 if (cc > (ssize_t)len) 908 cc = len; 909 errno = 0; 910 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) { 911 _kvm_syserr(kd, 0, _PATH_MEM); 912 break; 913 } 914 cw = write(kd->pmfd, cp, cc); 915 if (cw < 0) { 916 _kvm_syserr(kd, kd->program, "kvm_write"); 917 break; 918 } 919 /* 920 * If ka_kvatop returns a bogus value or our core file is 921 * truncated, we might wind up seeking beyond the end of the 922 * core file in which case the read will return 0 (EOF). 923 */ 924 if (cw == 0) 925 break; 926 cp += cw; 927 kva += cw; 928 len -= cw; 929 } 930 931 return (cp - (char *)buf); 932} 933 934int 935kvm_native(kvm_t *kd) 936{ 937 938 if (ISALIVE(kd)) 939 return (1); 940 return (kd->arch->ka_native(kd)); 941} 942