11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1989, 1992, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software developed by the Computer Systems 61573Srgrimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 71573Srgrimes * BG 91-66 and contributed to Berkeley. 81573Srgrimes * 91573Srgrimes * Redistribution and use in source and binary forms, with or without 101573Srgrimes * modification, are permitted provided that the following conditions 111573Srgrimes * are met: 121573Srgrimes * 1. Redistributions of source code must retain the above copyright 131573Srgrimes * notice, this list of conditions and the following disclaimer. 141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151573Srgrimes * notice, this list of conditions and the following disclaimer in the 161573Srgrimes * documentation and/or other materials provided with the distribution. 171573Srgrimes * 4. Neither the name of the University nor the names of its contributors 181573Srgrimes * may be used to endorse or promote products derived from this software 191573Srgrimes * without specific prior written permission. 201573Srgrimes * 211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311573Srgrimes * SUCH DAMAGE. 321573Srgrimes */ 331573Srgrimes 3483551Sdillon#include <sys/cdefs.h> 3583551Sdillon__FBSDID("$FreeBSD: releng/10.2/lib/libkvm/kvm.c 250231 2013-05-04 11:45:48Z jilles $"); 3683551Sdillon 371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 3852117Speter#if 0 391573Srgrimesstatic char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94"; 4052117Speter#endif 411573Srgrimes#endif /* LIBC_SCCS and not lint */ 421573Srgrimes 431573Srgrimes#include <sys/param.h> 44195838Sbz 45195838Sbz#define _WANT_VNET 46195838Sbz 471573Srgrimes#include <sys/user.h> 481573Srgrimes#include <sys/proc.h> 491573Srgrimes#include <sys/ioctl.h> 501573Srgrimes#include <sys/stat.h> 511573Srgrimes#include <sys/sysctl.h> 5255127Speter#include <sys/linker.h> 53215315Sdim#include <sys/pcpu.h> 541573Srgrimes 55195838Sbz#include <net/vnet.h> 56195838Sbz 571573Srgrimes#include <vm/vm.h> 581573Srgrimes#include <vm/vm_param.h> 591573Srgrimes 601573Srgrimes#include <machine/vmparam.h> 611573Srgrimes 621573Srgrimes#include <ctype.h> 631573Srgrimes#include <fcntl.h> 641573Srgrimes#include <kvm.h> 651573Srgrimes#include <limits.h> 661573Srgrimes#include <nlist.h> 671573Srgrimes#include <paths.h> 681573Srgrimes#include <stdio.h> 691573Srgrimes#include <stdlib.h> 701573Srgrimes#include <string.h> 71195838Sbz#include <strings.h> 721573Srgrimes#include <unistd.h> 731573Srgrimes 741573Srgrimes#include "kvm_private.h" 751573Srgrimes 7658642Sobrien/* from src/lib/libc/gen/nlist.c */ 7792917Sobrienint __fdnlist(int, struct nlist *); 7858642Sobrien 791573Srgrimeschar * 80217744Suqskvm_geterr(kvm_t *kd) 811573Srgrimes{ 821573Srgrimes return (kd->errbuf); 831573Srgrimes} 841573Srgrimes 851573Srgrimes#include <stdarg.h> 861573Srgrimes 871573Srgrimes/* 881573Srgrimes * Report an error using printf style arguments. "program" is kd->program 891573Srgrimes * on hard errors, and 0 on soft errors, so that under sun error emulation, 901573Srgrimes * only hard errors are printed out (otherwise, programs like gdb will 911573Srgrimes * generate tons of error messages when trying to access bogus pointers). 921573Srgrimes */ 931573Srgrimesvoid 941573Srgrimes_kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) 951573Srgrimes{ 961573Srgrimes va_list ap; 971573Srgrimes 981573Srgrimes va_start(ap, fmt); 991573Srgrimes if (program != NULL) { 1001573Srgrimes (void)fprintf(stderr, "%s: ", program); 1011573Srgrimes (void)vfprintf(stderr, fmt, ap); 1021573Srgrimes (void)fputc('\n', stderr); 1031573Srgrimes } else 1041573Srgrimes (void)vsnprintf(kd->errbuf, 105217744Suqs sizeof(kd->errbuf), fmt, ap); 1061573Srgrimes 1071573Srgrimes va_end(ap); 1081573Srgrimes} 1091573Srgrimes 1101573Srgrimesvoid 1111573Srgrimes_kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) 1121573Srgrimes{ 1131573Srgrimes va_list ap; 11492913Sobrien int n; 1151573Srgrimes 1161573Srgrimes va_start(ap, fmt); 1171573Srgrimes if (program != NULL) { 1181573Srgrimes (void)fprintf(stderr, "%s: ", program); 1191573Srgrimes (void)vfprintf(stderr, fmt, ap); 1201573Srgrimes (void)fprintf(stderr, ": %s\n", strerror(errno)); 1211573Srgrimes } else { 12292913Sobrien char *cp = kd->errbuf; 1231573Srgrimes 124217744Suqs (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap); 1251573Srgrimes n = strlen(cp); 1261573Srgrimes (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s", 1271573Srgrimes strerror(errno)); 1281573Srgrimes } 1291573Srgrimes va_end(ap); 1301573Srgrimes} 1311573Srgrimes 1321573Srgrimesvoid * 133217744Suqs_kvm_malloc(kvm_t *kd, size_t n) 1341573Srgrimes{ 1351573Srgrimes void *p; 1361573Srgrimes 13719629Sache if ((p = calloc(n, sizeof(char))) == NULL) 138217744Suqs _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s", 13919629Sache n, strerror(errno)); 1401573Srgrimes return (p); 1411573Srgrimes} 1421573Srgrimes 1431573Srgrimesstatic kvm_t * 144217744Suqs_kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout) 1451573Srgrimes{ 1461573Srgrimes struct stat st; 1471573Srgrimes 1481573Srgrimes kd->vmfd = -1; 1491573Srgrimes kd->pmfd = -1; 1501573Srgrimes kd->nlfd = -1; 1511573Srgrimes kd->vmst = 0; 1521573Srgrimes kd->procbase = 0; 1531573Srgrimes kd->argspc = 0; 1541573Srgrimes kd->argv = 0; 1551573Srgrimes 1561573Srgrimes if (uf == 0) 1573041Swollman uf = getbootfile(); 1581573Srgrimes else if (strlen(uf) >= MAXPATHLEN) { 1591573Srgrimes _kvm_err(kd, kd->program, "exec file name too long"); 1601573Srgrimes goto failed; 1611573Srgrimes } 1621573Srgrimes if (flag & ~O_RDWR) { 1631573Srgrimes _kvm_err(kd, kd->program, "bad flags arg"); 1641573Srgrimes goto failed; 1651573Srgrimes } 1661573Srgrimes if (mf == 0) 1671573Srgrimes mf = _PATH_MEM; 1681573Srgrimes 169250230Sjilles if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) { 1701573Srgrimes _kvm_syserr(kd, kd->program, "%s", mf); 1711573Srgrimes goto failed; 1721573Srgrimes } 1731573Srgrimes if (fstat(kd->pmfd, &st) < 0) { 1741573Srgrimes _kvm_syserr(kd, kd->program, "%s", mf); 1751573Srgrimes goto failed; 1761573Srgrimes } 177154404Scsjp if (S_ISREG(st.st_mode) && st.st_size <= 0) { 178154404Scsjp errno = EINVAL; 179154404Scsjp _kvm_syserr(kd, kd->program, "empty file"); 180154404Scsjp goto failed; 181154404Scsjp } 1821573Srgrimes if (S_ISCHR(st.st_mode)) { 1831573Srgrimes /* 1841573Srgrimes * If this is a character special device, then check that 1851573Srgrimes * it's /dev/mem. If so, open kmem too. (Maybe we should 1861573Srgrimes * make it work for either /dev/mem or /dev/kmem -- in either 1871573Srgrimes * case you're working with a live kernel.) 1881573Srgrimes */ 18937316Sphk if (strcmp(mf, _PATH_DEVNULL) == 0) { 190250231Sjilles kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC); 191121678Ssimokawa return (kd); 192121678Ssimokawa } else if (strcmp(mf, _PATH_MEM) == 0) { 193250230Sjilles if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) < 194250230Sjilles 0) { 19537316Sphk _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM); 19637316Sphk goto failed; 19737316Sphk } 198121678Ssimokawa return (kd); 1991573Srgrimes } 2001573Srgrimes } 201121678Ssimokawa /* 202121678Ssimokawa * This is a crash dump. 203121678Ssimokawa * Initialize the virtual address translation machinery, 204121678Ssimokawa * but first setup the namelist fd. 205121678Ssimokawa */ 206250230Sjilles if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) { 207121678Ssimokawa _kvm_syserr(kd, kd->program, "%s", uf); 208121678Ssimokawa goto failed; 209121678Ssimokawa } 210170772Ssimokawa if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0) 211170772Ssimokawa kd->rawdump = 1; 212121678Ssimokawa if (_kvm_initvtop(kd) < 0) 213121678Ssimokawa goto failed; 2141573Srgrimes return (kd); 2151573Srgrimesfailed: 2161573Srgrimes /* 2171573Srgrimes * Copy out the error if doing sane error semantics. 2181573Srgrimes */ 2191573Srgrimes if (errout != 0) 22064233Skris strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX); 2211573Srgrimes (void)kvm_close(kd); 2221573Srgrimes return (0); 2231573Srgrimes} 2241573Srgrimes 2251573Srgrimeskvm_t * 226217744Suqskvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag, 227217744Suqs char *errout) 2281573Srgrimes{ 22992913Sobrien kvm_t *kd; 2301573Srgrimes 231183986Sdelphij if ((kd = calloc(1, sizeof(*kd))) == NULL) { 23264233Skris (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX); 2331573Srgrimes return (0); 2341573Srgrimes } 2351573Srgrimes kd->program = 0; 23652117Speter return (_kvm_open(kd, uf, mf, flag, errout)); 2371573Srgrimes} 2381573Srgrimes 2391573Srgrimeskvm_t * 240217744Suqskvm_open(const char *uf, const char *mf, const char *sf __unused, int flag, 241217744Suqs const char *errstr) 2421573Srgrimes{ 24392913Sobrien kvm_t *kd; 2441573Srgrimes 245183986Sdelphij if ((kd = calloc(1, sizeof(*kd))) == NULL) { 2466701Sbde if (errstr != NULL) 2476701Sbde (void)fprintf(stderr, "%s: %s\n", 2486701Sbde errstr, strerror(errno)); 2491573Srgrimes return (0); 2501573Srgrimes } 2516683Sphk kd->program = errstr; 25252117Speter return (_kvm_open(kd, uf, mf, flag, NULL)); 2531573Srgrimes} 2541573Srgrimes 2551573Srgrimesint 256217744Suqskvm_close(kvm_t *kd) 2571573Srgrimes{ 25892913Sobrien int error = 0; 2591573Srgrimes 2601573Srgrimes if (kd->pmfd >= 0) 2611573Srgrimes error |= close(kd->pmfd); 2621573Srgrimes if (kd->vmfd >= 0) 2631573Srgrimes error |= close(kd->vmfd); 2641573Srgrimes if (kd->nlfd >= 0) 2651573Srgrimes error |= close(kd->nlfd); 2661573Srgrimes if (kd->vmst) 2671573Srgrimes _kvm_freevtop(kd); 2681573Srgrimes if (kd->procbase != 0) 2691573Srgrimes free((void *)kd->procbase); 270175243Sdelphij if (kd->argbuf != 0) 271175243Sdelphij free((void *) kd->argbuf); 272175243Sdelphij if (kd->argspc != 0) 273175243Sdelphij free((void *) kd->argspc); 2741573Srgrimes if (kd->argv != 0) 2751573Srgrimes free((void *)kd->argv); 2761573Srgrimes free((void *)kd); 2771573Srgrimes 2781573Srgrimes return (0); 2791573Srgrimes} 2801573Srgrimes 281195838Sbz/* 282195838Sbz * Walk the list of unresolved symbols, generate a new list and prefix the 283195838Sbz * symbol names, try again, and merge back what we could resolve. 284195838Sbz */ 285195838Sbzstatic int 286195838Sbzkvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix, 287195838Sbz uintptr_t (*validate_fn)(kvm_t *, uintptr_t)) 288195838Sbz{ 289195838Sbz struct nlist *n, *np, *p; 290195838Sbz char *cp, *ce; 291217744Suqs const char *ccp; 292195838Sbz size_t len; 293217744Suqs int slen, unresolved; 294195838Sbz 295195838Sbz /* 296195838Sbz * Calculate the space we need to malloc for nlist and names. 297195838Sbz * We are going to store the name twice for later lookups: once 298195838Sbz * with the prefix and once the unmodified name delmited by \0. 299195838Sbz */ 300195838Sbz len = 0; 301195838Sbz unresolved = 0; 302195838Sbz for (p = nl; p->n_name && p->n_name[0]; ++p) { 303195838Sbz if (p->n_type != N_UNDF) 304195838Sbz continue; 305195838Sbz len += sizeof(struct nlist) + strlen(prefix) + 306195838Sbz 2 * (strlen(p->n_name) + 1); 307195838Sbz unresolved++; 308195838Sbz } 309195838Sbz if (unresolved == 0) 310195838Sbz return (unresolved); 311195838Sbz /* Add space for the terminating nlist entry. */ 312195838Sbz len += sizeof(struct nlist); 313195838Sbz unresolved++; 314195838Sbz 315195838Sbz /* Alloc one chunk for (nlist, [names]) and setup pointers. */ 316195838Sbz n = np = malloc(len); 317195838Sbz bzero(n, len); 318195838Sbz if (n == NULL) 319195838Sbz return (missing); 320195838Sbz cp = ce = (char *)np; 321195838Sbz cp += unresolved * sizeof(struct nlist); 322195838Sbz ce += len; 323195838Sbz 324195838Sbz /* Generate shortened nlist with special prefix. */ 325195838Sbz unresolved = 0; 326195838Sbz for (p = nl; p->n_name && p->n_name[0]; ++p) { 327195838Sbz if (p->n_type != N_UNDF) 328195838Sbz continue; 329195838Sbz bcopy(p, np, sizeof(struct nlist)); 330195838Sbz /* Save the new\0orig. name so we can later match it again. */ 331217744Suqs slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix, 332195838Sbz (prefix[0] != '\0' && p->n_name[0] == '_') ? 333195838Sbz (p->n_name + 1) : p->n_name, '\0', p->n_name); 334217744Suqs if (slen < 0 || slen >= ce - cp) 335195838Sbz continue; 336195838Sbz np->n_name = cp; 337217744Suqs cp += slen + 1; 338195838Sbz np++; 339195838Sbz unresolved++; 340195838Sbz } 341195838Sbz 342195838Sbz /* Do lookup on the reduced list. */ 343195838Sbz np = n; 344195838Sbz unresolved = __fdnlist(kd->nlfd, np); 345195838Sbz 346195838Sbz /* Check if we could resolve further symbols and update the list. */ 347195838Sbz if (unresolved >= 0 && unresolved < missing) { 348195838Sbz /* Find the first freshly resolved entry. */ 349195838Sbz for (; np->n_name && np->n_name[0]; np++) 350195838Sbz if (np->n_type != N_UNDF) 351195838Sbz break; 352195838Sbz /* 353195838Sbz * The lists are both in the same order, 354195838Sbz * so we can walk them in parallel. 355195838Sbz */ 356195838Sbz for (p = nl; np->n_name && np->n_name[0] && 357195838Sbz p->n_name && p->n_name[0]; ++p) { 358195838Sbz if (p->n_type != N_UNDF) 359195838Sbz continue; 360195838Sbz /* Skip expanded name and compare to orig. one. */ 361217744Suqs ccp = np->n_name + strlen(np->n_name) + 1; 362217744Suqs if (strcmp(ccp, p->n_name) != 0) 363195838Sbz continue; 364195838Sbz /* Update nlist with new, translated results. */ 365195838Sbz p->n_type = np->n_type; 366195838Sbz p->n_other = np->n_other; 367195838Sbz p->n_desc = np->n_desc; 368195838Sbz if (validate_fn) 369195838Sbz p->n_value = (*validate_fn)(kd, np->n_value); 370195838Sbz else 371195838Sbz p->n_value = np->n_value; 372195838Sbz missing--; 373195838Sbz /* Find next freshly resolved entry. */ 374195838Sbz for (np++; np->n_name && np->n_name[0]; np++) 375195838Sbz if (np->n_type != N_UNDF) 376195838Sbz break; 377195838Sbz } 378195838Sbz } 379195838Sbz /* We could assert missing = unresolved here. */ 380195838Sbz 381195838Sbz free(n); 382195838Sbz return (unresolved); 383195838Sbz} 384195838Sbz 3851573Srgrimesint 386195838Sbz_kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize) 3871573Srgrimes{ 38892913Sobrien struct nlist *p; 38992913Sobrien int nvalid; 39055127Speter struct kld_sym_lookup lookup; 391162462Swkoszek int error; 392217744Suqs const char *prefix = ""; 393217744Suqs char symname[1024]; /* XXX-BZ symbol name length limit? */ 394204494Srwatson int tried_vnet, tried_dpcpu; 395204494Srwatson 3961573Srgrimes /* 39755127Speter * If we can't use the kld symbol lookup, revert to the 3981573Srgrimes * slow library call. 3991573Srgrimes */ 400195838Sbz if (!ISALIVE(kd)) { 401195838Sbz error = __fdnlist(kd->nlfd, nl); 402195838Sbz if (error <= 0) /* Hard error or success. */ 403195838Sbz return (error); 4041573Srgrimes 405195838Sbz if (_kvm_vnet_initialized(kd, initialize)) 406195838Sbz error = kvm_fdnlist_prefix(kd, nl, error, 407195838Sbz VNET_SYMPREFIX, _kvm_vnet_validaddr); 408195838Sbz 409204494Srwatson if (error > 0 && _kvm_dpcpu_initialized(kd, initialize)) 410204494Srwatson error = kvm_fdnlist_prefix(kd, nl, error, 411215315Sdim DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr); 412204494Srwatson 413195838Sbz return (error); 414195838Sbz } 415195838Sbz 4161573Srgrimes /* 41755127Speter * We can use the kld lookup syscall. Go through each nlist entry 41855127Speter * and look it up with a kldsym(2) syscall. 4191573Srgrimes */ 4201573Srgrimes nvalid = 0; 421204494Srwatson tried_vnet = 0; 422204494Srwatson tried_dpcpu = 0; 423195838Sbzagain: 4241573Srgrimes for (p = nl; p->n_name && p->n_name[0]; ++p) { 425195838Sbz if (p->n_type != N_UNDF) 426195838Sbz continue; 427195838Sbz 42855127Speter lookup.version = sizeof(lookup); 42955127Speter lookup.symvalue = 0; 43055127Speter lookup.symsize = 0; 4311573Srgrimes 432195838Sbz error = snprintf(symname, sizeof(symname), "%s%s", prefix, 433195838Sbz (prefix[0] != '\0' && p->n_name[0] == '_') ? 434195838Sbz (p->n_name + 1) : p->n_name); 435217744Suqs if (error < 0 || error >= (int)sizeof(symname)) 436195838Sbz continue; 437195838Sbz lookup.symname = symname; 43855127Speter if (lookup.symname[0] == '_') 43955127Speter lookup.symname++; 44055127Speter 44155127Speter if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) { 44255127Speter p->n_type = N_TEXT; 44355127Speter p->n_other = 0; 44455127Speter p->n_desc = 0; 445195838Sbz if (_kvm_vnet_initialized(kd, initialize) && 446218196Suqs strcmp(prefix, VNET_SYMPREFIX) == 0) 447195838Sbz p->n_value = 448195838Sbz _kvm_vnet_validaddr(kd, lookup.symvalue); 449204494Srwatson else if (_kvm_dpcpu_initialized(kd, initialize) && 450218196Suqs strcmp(prefix, DPCPU_SYMPREFIX) == 0) 451204494Srwatson p->n_value = 452204494Srwatson _kvm_dpcpu_validaddr(kd, lookup.symvalue); 453195838Sbz else 454195838Sbz p->n_value = lookup.symvalue; 45555127Speter ++nvalid; 45655127Speter /* lookup.symsize */ 4571573Srgrimes } 4581573Srgrimes } 459195838Sbz 4601573Srgrimes /* 461195838Sbz * Check the number of entries that weren't found. If they exist, 462204494Srwatson * try again with a prefix for virtualized or DPCPU symbol names. 463195838Sbz */ 464195838Sbz error = ((p - nl) - nvalid); 465204494Srwatson if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) { 466204494Srwatson tried_vnet = 1; 467195838Sbz prefix = VNET_SYMPREFIX; 468195838Sbz goto again; 469195838Sbz } 470204494Srwatson if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) { 471204494Srwatson tried_dpcpu = 1; 472215315Sdim prefix = DPCPU_SYMPREFIX; 473204494Srwatson goto again; 474204494Srwatson } 475195838Sbz 476195838Sbz /* 477162462Swkoszek * Return the number of entries that weren't found. If they exist, 478162462Swkoszek * also fill internal error buffer. 4791573Srgrimes */ 480162462Swkoszek error = ((p - nl) - nvalid); 481162462Swkoszek if (error) 482162462Swkoszek _kvm_syserr(kd, kd->program, "kvm_nlist"); 483162462Swkoszek return (error); 4841573Srgrimes} 4851573Srgrimes 486195838Sbzint 487217744Suqskvm_nlist(kvm_t *kd, struct nlist *nl) 488195838Sbz{ 489195838Sbz 490195838Sbz /* 491195838Sbz * If called via the public interface, permit intialization of 492195838Sbz * further virtualized modules on demand. 493195838Sbz */ 494195838Sbz return (_kvm_nlist(kd, nl, 1)); 495195838Sbz} 496195838Sbz 4971573Srgrimesssize_t 498217744Suqskvm_read(kvm_t *kd, u_long kva, void *buf, size_t len) 4991573Srgrimes{ 50092913Sobrien int cc; 501217744Suqs ssize_t cr; 502217744Suqs off_t pa; 503130246Sstefanf char *cp; 5041573Srgrimes 5051573Srgrimes if (ISALIVE(kd)) { 5061573Srgrimes /* 5071573Srgrimes * We're using /dev/kmem. Just read straight from the 5081573Srgrimes * device and let the active kernel do the address translation. 5091573Srgrimes */ 5101573Srgrimes errno = 0; 5111573Srgrimes if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { 512217744Suqs _kvm_err(kd, 0, "invalid address (%lx)", kva); 51358870Snectar return (-1); 5141573Srgrimes } 515217744Suqs cr = read(kd->vmfd, buf, len); 516217744Suqs if (cr < 0) { 5171573Srgrimes _kvm_syserr(kd, 0, "kvm_read"); 51858870Snectar return (-1); 519217744Suqs } else if (cr < (ssize_t)len) 5201573Srgrimes _kvm_err(kd, kd->program, "short read"); 521217744Suqs return (cr); 522217744Suqs } 5238870Srgrimes 524217744Suqs cp = buf; 525217744Suqs while (len > 0) { 526217744Suqs cc = _kvm_kvatop(kd, kva, &pa); 527217744Suqs if (cc == 0) 528217744Suqs return (-1); 529217744Suqs if (cc > (ssize_t)len) 530217744Suqs cc = len; 531217744Suqs errno = 0; 532217744Suqs if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) { 533217744Suqs _kvm_syserr(kd, 0, _PATH_MEM); 534217744Suqs break; 5351573Srgrimes } 536217744Suqs cr = read(kd->pmfd, cp, cc); 537217744Suqs if (cr < 0) { 538217744Suqs _kvm_syserr(kd, kd->program, "kvm_read"); 539217744Suqs break; 540217744Suqs } 541217744Suqs /* 542217744Suqs * If kvm_kvatop returns a bogus value or our core file is 543217744Suqs * truncated, we might wind up seeking beyond the end of the 544217744Suqs * core file in which case the read will return 0 (EOF). 545217744Suqs */ 546217744Suqs if (cr == 0) 547217744Suqs break; 548217744Suqs cp += cr; 549217744Suqs kva += cr; 550217744Suqs len -= cr; 5511573Srgrimes } 552217744Suqs 553217744Suqs return (cp - (char *)buf); 5541573Srgrimes} 5551573Srgrimes 5561573Srgrimesssize_t 557217744Suqskvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len) 5581573Srgrimes{ 55992913Sobrien int cc; 5601573Srgrimes 5611573Srgrimes if (ISALIVE(kd)) { 5621573Srgrimes /* 5631573Srgrimes * Just like kvm_read, only we write. 5641573Srgrimes */ 5651573Srgrimes errno = 0; 5661573Srgrimes if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { 567217744Suqs _kvm_err(kd, 0, "invalid address (%lx)", kva); 56858870Snectar return (-1); 5691573Srgrimes } 5701573Srgrimes cc = write(kd->vmfd, buf, len); 5711573Srgrimes if (cc < 0) { 5721573Srgrimes _kvm_syserr(kd, 0, "kvm_write"); 57358870Snectar return (-1); 574217744Suqs } else if ((size_t)cc < len) 5751573Srgrimes _kvm_err(kd, kd->program, "short write"); 5761573Srgrimes return (cc); 5771573Srgrimes } else { 5781573Srgrimes _kvm_err(kd, kd->program, 5791573Srgrimes "kvm_write not implemented for dead kernels"); 58058870Snectar return (-1); 5811573Srgrimes } 5821573Srgrimes /* NOTREACHED */ 5831573Srgrimes} 584