17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
271320cafBryan Cantrill/*
281320cafBryan Cantrill * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
291320cafBryan Cantrill */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#include <kvm.h>
327c478bdstevel@tonic-gate#include <stdio.h>
337c478bdstevel@tonic-gate#include <stdlib.h>
347c478bdstevel@tonic-gate#include <stdarg.h>
357c478bdstevel@tonic-gate#include <unistd.h>
367c478bdstevel@tonic-gate#include <limits.h>
377c478bdstevel@tonic-gate#include <fcntl.h>
387c478bdstevel@tonic-gate#include <strings.h>
391320cafBryan Cantrill#include <errno.h>
407c478bdstevel@tonic-gate#include <sys/mem.h>
417c478bdstevel@tonic-gate#include <sys/stat.h>
427c478bdstevel@tonic-gate#include <sys/mman.h>
437c478bdstevel@tonic-gate#include <sys/dumphdr.h>
447c478bdstevel@tonic-gate#include <sys/sysmacros.h>
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gatestruct _kvmd {
477c478bdstevel@tonic-gate	struct dumphdr	kvm_dump;
487c478bdstevel@tonic-gate	char		*kvm_debug;
497c478bdstevel@tonic-gate	int		kvm_openflag;
507c478bdstevel@tonic-gate	int		kvm_corefd;
517c478bdstevel@tonic-gate	int		kvm_kmemfd;
527c478bdstevel@tonic-gate	int		kvm_memfd;
537c478bdstevel@tonic-gate	size_t		kvm_coremapsize;
547c478bdstevel@tonic-gate	char		*kvm_core;
557c478bdstevel@tonic-gate	dump_map_t	*kvm_map;
567c478bdstevel@tonic-gate	pfn_t		*kvm_pfn;
577c478bdstevel@tonic-gate	struct as	*kvm_kas;
587c478bdstevel@tonic-gate	proc_t		*kvm_practive;
597c478bdstevel@tonic-gate	pid_t		kvm_pid;
607c478bdstevel@tonic-gate	char		kvm_namelist[MAXNAMELEN + 1];
611320cafBryan Cantrill	boolean_t	kvm_namelist_core;
627c478bdstevel@tonic-gate	proc_t		kvm_proc;
637c478bdstevel@tonic-gate};
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate#define	PREAD	(ssize_t (*)(int, void *, size_t, offset_t))pread64
667c478bdstevel@tonic-gate#define	PWRITE	(ssize_t (*)(int, void *, size_t, offset_t))pwrite64
677c478bdstevel@tonic-gate
681320cafBryan Cantrillstatic int kvm_nlist_core(kvm_t *kd, struct nlist nl[], const char *err);
691320cafBryan Cantrill
707c478bdstevel@tonic-gatestatic kvm_t *
717c478bdstevel@tonic-gatefail(kvm_t *kd, const char *err, const char *message, ...)
727c478bdstevel@tonic-gate{
737c478bdstevel@tonic-gate	va_list args;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	va_start(args, message);
767c478bdstevel@tonic-gate	if (err || (kd && kd->kvm_debug)) {
777c478bdstevel@tonic-gate		(void) fprintf(stderr, "%s: ", err ? err : "KVM_DEBUG");
787c478bdstevel@tonic-gate		(void) vfprintf(stderr, message, args);
797c478bdstevel@tonic-gate		(void) fprintf(stderr, "\n");
807c478bdstevel@tonic-gate	}
817c478bdstevel@tonic-gate	va_end(args);
827c478bdstevel@tonic-gate	if (kd != NULL)
837c478bdstevel@tonic-gate		(void) kvm_close(kd);
847c478bdstevel@tonic-gate	return (NULL);
857c478bdstevel@tonic-gate}
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate/*ARGSUSED*/
887c478bdstevel@tonic-gatekvm_t *
897c478bdstevel@tonic-gatekvm_open(const char *namelist, const char *corefile, const char *swapfile,
907c478bdstevel@tonic-gate	int flag, const char *err)
917c478bdstevel@tonic-gate{
927c478bdstevel@tonic-gate	kvm_t *kd;
937c478bdstevel@tonic-gate	struct stat64 memstat, kmemstat, allkmemstat, corestat;
947c478bdstevel@tonic-gate	struct nlist nl[3] = { { "kas" }, { "practive" }, { "" } };
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gate	if ((kd = calloc(1, sizeof (kvm_t))) == NULL)
977c478bdstevel@tonic-gate		return (fail(NULL, err, "cannot allocate space for kvm_t"));
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	kd->kvm_corefd = kd->kvm_kmemfd = kd->kvm_memfd = -1;
1007c478bdstevel@tonic-gate	kd->kvm_debug = getenv("KVM_DEBUG");
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate	if ((kd->kvm_openflag = flag) != O_RDONLY && flag != O_RDWR)
1037c478bdstevel@tonic-gate		return (fail(kd, err, "illegal flag 0x%x to kvm_open()", flag));
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gate	if (corefile == NULL)
1067c478bdstevel@tonic-gate		corefile = "/dev/kmem";
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate	if (stat64(corefile, &corestat) == -1)
1097c478bdstevel@tonic-gate		return (fail(kd, err, "cannot stat %s", corefile));
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate	if (S_ISCHR(corestat.st_mode)) {
1127c478bdstevel@tonic-gate		if (stat64("/dev/mem", &memstat) == -1)
1137c478bdstevel@tonic-gate			return (fail(kd, err, "cannot stat /dev/mem"));
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate		if (stat64("/dev/kmem", &kmemstat) == -1)
1167c478bdstevel@tonic-gate			return (fail(kd, err, "cannot stat /dev/kmem"));
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gate		if (stat64("/dev/allkmem", &allkmemstat) == -1)
1197c478bdstevel@tonic-gate			return (fail(kd, err, "cannot stat /dev/allkmem"));
1207c478bdstevel@tonic-gate		if (corestat.st_rdev == memstat.st_rdev ||
1217c478bdstevel@tonic-gate		    corestat.st_rdev == kmemstat.st_rdev ||
1227c478bdstevel@tonic-gate		    corestat.st_rdev == allkmemstat.st_rdev) {
1237c478bdstevel@tonic-gate			char *kmem = (corestat.st_rdev == allkmemstat.st_rdev ?
1247c478bdstevel@tonic-gate			    "/dev/allkmem" : "/dev/kmem");
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate			if ((kd->kvm_kmemfd = open64(kmem, flag)) == -1)
1277c478bdstevel@tonic-gate				return (fail(kd, err, "cannot open %s", kmem));
1287c478bdstevel@tonic-gate			if ((kd->kvm_memfd = open64("/dev/mem", flag)) == -1)
1297c478bdstevel@tonic-gate				return (fail(kd, err, "cannot open /dev/mem"));
1307c478bdstevel@tonic-gate		}
1317c478bdstevel@tonic-gate	} else {
1327c478bdstevel@tonic-gate		if ((kd->kvm_corefd = open64(corefile, flag)) == -1)
1337c478bdstevel@tonic-gate			return (fail(kd, err, "cannot open %s", corefile));
1347c478bdstevel@tonic-gate		if (pread64(kd->kvm_corefd, &kd->kvm_dump,
1357c478bdstevel@tonic-gate		    sizeof (kd->kvm_dump), 0) != sizeof (kd->kvm_dump))
1367c478bdstevel@tonic-gate			return (fail(kd, err, "cannot read dump header"));
1377c478bdstevel@tonic-gate		if (kd->kvm_dump.dump_magic != DUMP_MAGIC)
1387c478bdstevel@tonic-gate			return (fail(kd, err, "%s is not a kernel core file "
1397c478bdstevel@tonic-gate			    "(bad magic number %x)", corefile,
1407c478bdstevel@tonic-gate			    kd->kvm_dump.dump_magic));
1417c478bdstevel@tonic-gate		if (kd->kvm_dump.dump_version != DUMP_VERSION)
1427c478bdstevel@tonic-gate			return (fail(kd, err,
1437c478bdstevel@tonic-gate			    "libkvm version (%u) != corefile version (%u)",
1447c478bdstevel@tonic-gate			    DUMP_VERSION, kd->kvm_dump.dump_version));
1457c478bdstevel@tonic-gate		if (kd->kvm_dump.dump_wordsize != DUMP_WORDSIZE)
1467c478bdstevel@tonic-gate			return (fail(kd, err, "%s is a %d-bit core file - "
1477c478bdstevel@tonic-gate			    "cannot examine with %d-bit libkvm", corefile,
1487c478bdstevel@tonic-gate			    kd->kvm_dump.dump_wordsize, DUMP_WORDSIZE));
1497c478bdstevel@tonic-gate		/*
1507c478bdstevel@tonic-gate		 * We try to mmap(2) the entire corefile for performance
1517c478bdstevel@tonic-gate		 * (so we can use bcopy(3C) rather than pread(2)).  Failing
1527c478bdstevel@tonic-gate		 * that, we insist on at least mmap(2)ing the dump map.
1537c478bdstevel@tonic-gate		 */
1547c478bdstevel@tonic-gate		kd->kvm_coremapsize = (size_t)corestat.st_size;
1557c478bdstevel@tonic-gate		if (corestat.st_size > LONG_MAX ||
1567c478bdstevel@tonic-gate		    (kd->kvm_core = mmap64(0, kd->kvm_coremapsize,
1577c478bdstevel@tonic-gate		    PROT_READ, MAP_SHARED, kd->kvm_corefd, 0)) == MAP_FAILED) {
1587c478bdstevel@tonic-gate			kd->kvm_coremapsize = kd->kvm_dump.dump_data;
1597c478bdstevel@tonic-gate			if ((kd->kvm_core = mmap64(0, kd->kvm_coremapsize,
1607c478bdstevel@tonic-gate			    PROT_READ, MAP_SHARED, kd->kvm_corefd, 0)) ==
1617c478bdstevel@tonic-gate			    MAP_FAILED)
1627c478bdstevel@tonic-gate				return (fail(kd, err, "cannot mmap corefile"));
1637c478bdstevel@tonic-gate		}
1647c478bdstevel@tonic-gate		kd->kvm_map = (void *)(kd->kvm_core + kd->kvm_dump.dump_map);
1657c478bdstevel@tonic-gate		kd->kvm_pfn = (void *)(kd->kvm_core + kd->kvm_dump.dump_pfn);
1667c478bdstevel@tonic-gate	}
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate	if (namelist == NULL)
1697c478bdstevel@tonic-gate		namelist = "/dev/ksyms";
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate	(void) strncpy(kd->kvm_namelist, namelist, MAXNAMELEN);
1727c478bdstevel@tonic-gate
1731320cafBryan Cantrill	if (kvm_nlist(kd, nl) == -1) {
1741320cafBryan Cantrill		if (kd->kvm_corefd == -1) {
1751320cafBryan Cantrill			return (fail(kd, err, "%s is not a %d-bit "
1761320cafBryan Cantrill			    "kernel namelist", namelist, DUMP_WORDSIZE));
1771320cafBryan Cantrill		}
1781320cafBryan Cantrill
1791320cafBryan Cantrill		if (kvm_nlist_core(kd, nl, err) == -1)
1801320cafBryan Cantrill			return (NULL);		/* fail() already called */
1811320cafBryan Cantrill	}
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate	kd->kvm_kas = (struct as *)nl[0].n_value;
1847c478bdstevel@tonic-gate	kd->kvm_practive = (proc_t *)nl[1].n_value;
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate	(void) kvm_setproc(kd);
1877c478bdstevel@tonic-gate	return (kd);
1887c478bdstevel@tonic-gate}
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gateint
1917c478bdstevel@tonic-gatekvm_close(kvm_t *kd)
1927c478bdstevel@tonic-gate{
1937c478bdstevel@tonic-gate	if (kd->kvm_core != NULL && kd->kvm_core != MAP_FAILED)
1947c478bdstevel@tonic-gate		(void) munmap(kd->kvm_core, kd->kvm_coremapsize);
1957c478bdstevel@tonic-gate	if (kd->kvm_corefd != -1)
1967c478bdstevel@tonic-gate		(void) close(kd->kvm_corefd);
1977c478bdstevel@tonic-gate	if (kd->kvm_kmemfd != -1)
1987c478bdstevel@tonic-gate		(void) close(kd->kvm_kmemfd);
1997c478bdstevel@tonic-gate	if (kd->kvm_memfd != -1)
2007c478bdstevel@tonic-gate		(void) close(kd->kvm_memfd);
2011320cafBryan Cantrill	if (kd->kvm_namelist_core)
2021320cafBryan Cantrill		(void) unlink(kd->kvm_namelist);
2037c478bdstevel@tonic-gate	free(kd);
2047c478bdstevel@tonic-gate	return (0);
2057c478bdstevel@tonic-gate}
2067c478bdstevel@tonic-gate
2071320cafBryan Cantrillconst char *
2081320cafBryan Cantrillkvm_namelist(kvm_t *kd)
2091320cafBryan Cantrill{
2101320cafBryan Cantrill	return (kd->kvm_namelist);
2111320cafBryan Cantrill}
2121320cafBryan Cantrill
2137c478bdstevel@tonic-gateint
2147c478bdstevel@tonic-gatekvm_nlist(kvm_t *kd, struct nlist nl[])
2157c478bdstevel@tonic-gate{
2167c478bdstevel@tonic-gate	return (nlist(kd->kvm_namelist, nl));
2177c478bdstevel@tonic-gate}
2187c478bdstevel@tonic-gate
2191320cafBryan Cantrill/*
2201320cafBryan Cantrill * If we don't have a name list, try to dig it out of the kernel crash dump.
2211320cafBryan Cantrill * (The symbols have been present in the dump, uncompressed, for nearly a
2221320cafBryan Cantrill * decade as of this writing -- and it is frankly surprising that the archaic
2231320cafBryan Cantrill * notion of a disjoint symbol table managed to survive that change.)
2241320cafBryan Cantrill */
2251320cafBryan Cantrillstatic int
2261320cafBryan Cantrillkvm_nlist_core(kvm_t *kd, struct nlist nl[], const char *err)
2271320cafBryan Cantrill{
2281320cafBryan Cantrill	dumphdr_t *dump = &kd->kvm_dump;
2291320cafBryan Cantrill	char *msg = "couldn't extract symbols from dump";
2301320cafBryan Cantrill	char *template = "/tmp/.libkvm.kvm_nlist_core.pid%d.XXXXXX";
2311320cafBryan Cantrill	int fd, rval;
2321320cafBryan Cantrill
2331320cafBryan Cantrill	if (dump->dump_ksyms_size != dump->dump_ksyms_csize) {
2341320cafBryan Cantrill		(void) fail(kd, err, "%s: kernel symbols are compressed", msg);
2351320cafBryan Cantrill		return (-1);
2361320cafBryan Cantrill	}
2371320cafBryan Cantrill
2381320cafBryan Cantrill	if (dump->dump_ksyms + dump->dump_ksyms_size > kd->kvm_coremapsize) {
2391320cafBryan Cantrill		(void) fail(kd, err, "%s: kernel symbols not mapped", msg);
2401320cafBryan Cantrill		return (-1);
2411320cafBryan Cantrill	}
2421320cafBryan Cantrill
2431320cafBryan Cantrill	/*
2441320cafBryan Cantrill	 * Beause this temporary file may be left as a turd if the caller
2451320cafBryan Cantrill	 * does not properly call kvm_close(), we make sure that it clearly
2461320cafBryan Cantrill	 * indicates its origins.
2471320cafBryan Cantrill	 */
2481320cafBryan Cantrill	(void) snprintf(kd->kvm_namelist, MAXNAMELEN, template, getpid());
2491320cafBryan Cantrill
2501320cafBryan Cantrill	if ((fd = mkstemp(kd->kvm_namelist)) == -1) {
2511320cafBryan Cantrill		(void) fail(kd, err, "%s: couldn't create temporary "
2521320cafBryan Cantrill		    "symbols file: %s", msg, strerror(errno));
2531320cafBryan Cantrill		return (-1);
2541320cafBryan Cantrill	}
2551320cafBryan Cantrill
2561320cafBryan Cantrill	kd->kvm_namelist_core = B_TRUE;
2571320cafBryan Cantrill
2581320cafBryan Cantrill	do {
2591320cafBryan Cantrill		rval = write(fd, (caddr_t)((uintptr_t)kd->kvm_core +
2601320cafBryan Cantrill		    (uintptr_t)dump->dump_ksyms), dump->dump_ksyms_size);
2611320cafBryan Cantrill	} while (rval < dump->dump_ksyms_size && errno == EINTR);
2621320cafBryan Cantrill
2631320cafBryan Cantrill	if (rval < dump->dump_ksyms_size) {
2641320cafBryan Cantrill		(void) fail(kd, err, "%s: couldn't write to temporary "
2651320cafBryan Cantrill		    "symbols file: %s", msg, strerror(errno));
2661320cafBryan Cantrill		(void) close(fd);
2671320cafBryan Cantrill		return (-1);
2681320cafBryan Cantrill	}
2691320cafBryan Cantrill
2701320cafBryan Cantrill	(void) close(fd);
2711320cafBryan Cantrill
2721320cafBryan Cantrill	if (kvm_nlist(kd, nl) == -1) {
2731320cafBryan Cantrill		(void) fail(kd, err, "%s: symbols not valid", msg);
2741320cafBryan Cantrill		return (-1);
2751320cafBryan Cantrill	}
2761320cafBryan Cantrill
2771320cafBryan Cantrill	return (0);
2781320cafBryan Cantrill}
2791320cafBryan Cantrill
2807c478bdstevel@tonic-gatestatic offset_t
2817c478bdstevel@tonic-gatekvm_lookup(kvm_t *kd, struct as *as, uint64_t addr)
2827c478bdstevel@tonic-gate{
2837c478bdstevel@tonic-gate	uintptr_t pageoff = addr & (kd->kvm_dump.dump_pagesize - 1);
2847c478bdstevel@tonic-gate	uint64_t page = addr - pageoff;
2857c478bdstevel@tonic-gate	offset_t off = 0;
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate	if (kd->kvm_debug)
2887c478bdstevel@tonic-gate		fprintf(stderr, "kvm_lookup(%p, %llx):", (void *)as, addr);
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate	if (as == NULL) {		/* physical addressing mode */
2917c478bdstevel@tonic-gate		long first = 0;
2927c478bdstevel@tonic-gate		long last = kd->kvm_dump.dump_npages - 1;
2937c478bdstevel@tonic-gate		pfn_t target = (pfn_t)(page >> kd->kvm_dump.dump_pageshift);
2947c478bdstevel@tonic-gate		while (last >= first) {
2957c478bdstevel@tonic-gate			long middle = (first + last) / 2;
2967c478bdstevel@tonic-gate			pfn_t pfn = kd->kvm_pfn[middle];
2977c478bdstevel@tonic-gate			if (kd->kvm_debug)
2987c478bdstevel@tonic-gate				fprintf(stderr, " %ld ->", middle);
2997c478bdstevel@tonic-gate			if (pfn == target) {
3007c478bdstevel@tonic-gate				off = kd->kvm_dump.dump_data + pageoff +
3017c478bdstevel@tonic-gate				    ((uint64_t)middle <<
3027c478bdstevel@tonic-gate				    kd->kvm_dump.dump_pageshift);
3037c478bdstevel@tonic-gate				break;
3047c478bdstevel@tonic-gate			}
3057c478bdstevel@tonic-gate			if (pfn < target)
3067c478bdstevel@tonic-gate				first = middle + 1;
3077c478bdstevel@tonic-gate			else
3087c478bdstevel@tonic-gate				last = middle - 1;
3097c478bdstevel@tonic-gate		}
3107c478bdstevel@tonic-gate	} else {
3117c478bdstevel@tonic-gate		long hash = DUMP_HASH(&kd->kvm_dump, as, page);
3127c478bdstevel@tonic-gate		off = kd->kvm_map[hash].dm_first;
3137c478bdstevel@tonic-gate		while (off != 0) {
3147c478bdstevel@tonic-gate			dump_map_t *dmp = (void *)(kd->kvm_core + off);
3157c478bdstevel@tonic-gate			if (kd->kvm_debug)
3167c478bdstevel@tonic-gate				fprintf(stderr, " %llx ->", off);
3177c478bdstevel@tonic-gate			if (dmp < kd->kvm_map ||
3187c478bdstevel@tonic-gate			    dmp > kd->kvm_map + kd->kvm_dump.dump_hashmask ||
3197c478bdstevel@tonic-gate			    (off & (sizeof (offset_t) - 1)) != 0 ||
3207c478bdstevel@tonic-gate			    DUMP_HASH(&kd->kvm_dump, dmp->dm_as, dmp->dm_va) !=
3217c478bdstevel@tonic-gate			    hash) {
3227c478bdstevel@tonic-gate				if (kd->kvm_debug)
3237c478bdstevel@tonic-gate					fprintf(stderr, " dump map corrupt\n");
3247c478bdstevel@tonic-gate				return (0);
3257c478bdstevel@tonic-gate			}
3267c478bdstevel@tonic-gate			if (dmp->dm_va == page && dmp->dm_as == as) {
3277c478bdstevel@tonic-gate				off = dmp->dm_data + pageoff;
3287c478bdstevel@tonic-gate				break;
3297c478bdstevel@tonic-gate			}
3307c478bdstevel@tonic-gate			off = dmp->dm_next;
3317c478bdstevel@tonic-gate		}
3327c478bdstevel@tonic-gate	}
3337c478bdstevel@tonic-gate	if (kd->kvm_debug)
3347c478bdstevel@tonic-gate		fprintf(stderr, "%s found: %llx\n", off ? "" : " not", off);
3357c478bdstevel@tonic-gate	return (off);
3367c478bdstevel@tonic-gate}
3377c478bdstevel@tonic-gate
3387c478bdstevel@tonic-gatestatic ssize_t
3397c478bdstevel@tonic-gatekvm_rw(kvm_t *kd, uint64_t addr, void *buf, size_t size,
3407c478bdstevel@tonic-gate	struct as *as, ssize_t (*prw)(int, void *, size_t, offset_t))
3417c478bdstevel@tonic-gate{
3427c478bdstevel@tonic-gate	offset_t off;
3437c478bdstevel@tonic-gate	size_t resid = size;
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gate	/*
3467c478bdstevel@tonic-gate	 * read/write of zero bytes always succeeds
3477c478bdstevel@tonic-gate	 */
3487c478bdstevel@tonic-gate	if (size == 0)
3497c478bdstevel@tonic-gate		return (0);
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	if (kd->kvm_core == NULL) {
3527c478bdstevel@tonic-gate		char procbuf[100];
3537c478bdstevel@tonic-gate		int procfd;
3547c478bdstevel@tonic-gate		ssize_t rval;
3557c478bdstevel@tonic-gate
3567c478bdstevel@tonic-gate		if (as == kd->kvm_kas)
3577c478bdstevel@tonic-gate			return (prw(kd->kvm_kmemfd, buf, size, addr));
3587c478bdstevel@tonic-gate		if (as == NULL)
3597c478bdstevel@tonic-gate			return (prw(kd->kvm_memfd, buf, size, addr));
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate		(void) sprintf(procbuf, "/proc/%ld/as", kd->kvm_pid);
3627c478bdstevel@tonic-gate		if ((procfd = open64(procbuf, kd->kvm_openflag)) == -1)
3637c478bdstevel@tonic-gate			return (-1);
3647c478bdstevel@tonic-gate		rval = prw(procfd, buf, size, addr);
3657c478bdstevel@tonic-gate		(void) close(procfd);
3667c478bdstevel@tonic-gate		return (rval);
3677c478bdstevel@tonic-gate	}
3687c478bdstevel@tonic-gate
3697c478bdstevel@tonic-gate	while (resid != 0) {
3707c478bdstevel@tonic-gate		uintptr_t pageoff = addr & (kd->kvm_dump.dump_pagesize - 1);
3717c478bdstevel@tonic-gate		ssize_t len = MIN(resid, kd->kvm_dump.dump_pagesize - pageoff);
3727c478bdstevel@tonic-gate
3737c478bdstevel@tonic-gate		if ((off = kvm_lookup(kd, as, addr)) == 0)
3747c478bdstevel@tonic-gate			break;
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate		if (prw == PREAD && off < kd->kvm_coremapsize)
3777c478bdstevel@tonic-gate			bcopy(kd->kvm_core + off, buf, len);
3787c478bdstevel@tonic-gate		else if ((len = prw(kd->kvm_corefd, buf, len, off)) <= 0)
3797c478bdstevel@tonic-gate			break;
3807c478bdstevel@tonic-gate		resid -= len;
3817c478bdstevel@tonic-gate		addr += len;
3827c478bdstevel@tonic-gate		buf = (char *)buf + len;
3837c478bdstevel@tonic-gate	}
3847c478bdstevel@tonic-gate	return (resid < size ? size - resid : -1);
3857c478bdstevel@tonic-gate}
3867c478bdstevel@tonic-gate
3877c478bdstevel@tonic-gatessize_t
3887c478bdstevel@tonic-gatekvm_read(kvm_t *kd, uintptr_t addr, void *buf, size_t size)
3897c478bdstevel@tonic-gate{
3907c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, buf, size, kd->kvm_kas, PREAD));
3917c478bdstevel@tonic-gate}
3927c478bdstevel@tonic-gate
3937c478bdstevel@tonic-gatessize_t
3947c478bdstevel@tonic-gatekvm_kread(kvm_t *kd, uintptr_t addr, void *buf, size_t size)
3957c478bdstevel@tonic-gate{
3967c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, buf, size, kd->kvm_kas, PREAD));
3977c478bdstevel@tonic-gate}
3987c478bdstevel@tonic-gate
3997c478bdstevel@tonic-gatessize_t
4007c478bdstevel@tonic-gatekvm_uread(kvm_t *kd, uintptr_t addr, void *buf, size_t size)
4017c478bdstevel@tonic-gate{
4027c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, buf, size, kd->kvm_proc.p_as, PREAD));
4037c478bdstevel@tonic-gate}
4047c478bdstevel@tonic-gate
4057c478bdstevel@tonic-gatessize_t
4067c478bdstevel@tonic-gatekvm_aread(kvm_t *kd, uintptr_t addr, void *buf, size_t size, struct as *as)
4077c478bdstevel@tonic-gate{
4087c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, buf, size, as, PREAD));
4097c478bdstevel@tonic-gate}
4107c478bdstevel@tonic-gate
4117c478bdstevel@tonic-gatessize_t
4127c478bdstevel@tonic-gatekvm_pread(kvm_t *kd, uint64_t addr, void *buf, size_t size)
4137c478bdstevel@tonic-gate{
4147c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, buf, size, NULL, PREAD));
4157c478bdstevel@tonic-gate}
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gatessize_t
4187c478bdstevel@tonic-gatekvm_write(kvm_t *kd, uintptr_t addr, const void *buf, size_t size)
4197c478bdstevel@tonic-gate{
4207c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, (void *)buf, size, kd->kvm_kas, PWRITE));
4217c478bdstevel@tonic-gate}
4227c478bdstevel@tonic-gate
4237c478bdstevel@tonic-gatessize_t
4247c478bdstevel@tonic-gatekvm_kwrite(kvm_t *kd, uintptr_t addr, const void *buf, size_t size)
4257c478bdstevel@tonic-gate{
4267c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, (void *)buf, size, kd->kvm_kas, PWRITE));
4277c478bdstevel@tonic-gate}
4287c478bdstevel@tonic-gate
4297c478bdstevel@tonic-gatessize_t
4307c478bdstevel@tonic-gatekvm_uwrite(kvm_t *kd, uintptr_t addr, const void *buf, size_t size)
4317c478bdstevel@tonic-gate{
4327c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, (void *)buf, size, kd->kvm_proc.p_as, PWRITE));
4337c478bdstevel@tonic-gate}
4347c478bdstevel@tonic-gate
4357c478bdstevel@tonic-gatessize_t
4367c478bdstevel@tonic-gatekvm_awrite(kvm_t *kd, uintptr_t addr, const void *buf, size_t size,
4377c478bdstevel@tonic-gate    struct as *as)
4387c478bdstevel@tonic-gate{
4397c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, (void *)buf, size, as, PWRITE));
4407c478bdstevel@tonic-gate}
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gatessize_t
4437c478bdstevel@tonic-gatekvm_pwrite(kvm_t *kd, uint64_t addr, const void *buf, size_t size)
4447c478bdstevel@tonic-gate{
4457c478bdstevel@tonic-gate	return (kvm_rw(kd, addr, (void *)buf, size, NULL, PWRITE));
4467c478bdstevel@tonic-gate}
4477c478bdstevel@tonic-gate
4487c478bdstevel@tonic-gateuint64_t
4497c478bdstevel@tonic-gatekvm_physaddr(kvm_t *kd, struct as *as, uintptr_t addr)
4507c478bdstevel@tonic-gate{
4517c478bdstevel@tonic-gate	mem_vtop_t mem_vtop;
4527c478bdstevel@tonic-gate	offset_t off;
4537c478bdstevel@tonic-gate
4547c478bdstevel@tonic-gate	if (kd->kvm_core == NULL) {
4557c478bdstevel@tonic-gate		mem_vtop.m_as = as;
4567c478bdstevel@tonic-gate		mem_vtop.m_va = (void *)addr;
4577c478bdstevel@tonic-gate		if (ioctl(kd->kvm_kmemfd, MEM_VTOP, &mem_vtop) == 0)
4587c478bdstevel@tonic-gate			return ((uint64_t)mem_vtop.m_pfn * getpagesize() +
4597c478bdstevel@tonic-gate			    (addr & (getpagesize() - 1)));
4607c478bdstevel@tonic-gate	} else {
4617c478bdstevel@tonic-gate		if ((off = kvm_lookup(kd, as, addr)) != 0) {
4627c478bdstevel@tonic-gate			long pfn_index =
4637c478bdstevel@tonic-gate			    (u_offset_t)(off - kd->kvm_dump.dump_data) >>
4647c478bdstevel@tonic-gate			    kd->kvm_dump.dump_pageshift;
4657c478bdstevel@tonic-gate			return (((uint64_t)kd->kvm_pfn[pfn_index] <<
4667c478bdstevel@tonic-gate			    kd->kvm_dump.dump_pageshift) +
4677c478bdstevel@tonic-gate			    (addr & (kd->kvm_dump.dump_pagesize - 1)));
4687c478bdstevel@tonic-gate		}
4697c478bdstevel@tonic-gate	}
4707c478bdstevel@tonic-gate	return (-1ULL);
4717c478bdstevel@tonic-gate}
4727c478bdstevel@tonic-gate
4737c478bdstevel@tonic-gatestruct proc *
4747c478bdstevel@tonic-gatekvm_getproc(kvm_t *kd, pid_t pid)
4757c478bdstevel@tonic-gate{
4767c478bdstevel@tonic-gate	(void) kvm_setproc(kd);
4777c478bdstevel@tonic-gate	while (kvm_nextproc(kd) != NULL)
4787c478bdstevel@tonic-gate		if (kd->kvm_pid == pid)
4797c478bdstevel@tonic-gate			return (&kd->kvm_proc);
4807c478bdstevel@tonic-gate	return (NULL);
4817c478bdstevel@tonic-gate}
4827c478bdstevel@tonic-gate
4837c478bdstevel@tonic-gatestruct proc *
4847c478bdstevel@tonic-gatekvm_nextproc(kvm_t *kd)
4857c478bdstevel@tonic-gate{
4867c478bdstevel@tonic-gate	if (kd->kvm_proc.p_next == NULL ||
4877c478bdstevel@tonic-gate	    kvm_kread(kd, (uintptr_t)kd->kvm_proc.p_next,
4887c478bdstevel@tonic-gate	    &kd->kvm_proc, sizeof (proc_t)) != sizeof (proc_t) ||
4897c478bdstevel@tonic-gate	    kvm_kread(kd, (uintptr_t)&kd->kvm_proc.p_pidp->pid_id,
4907c478bdstevel@tonic-gate	    &kd->kvm_pid, sizeof (pid_t)) != sizeof (pid_t))
4917c478bdstevel@tonic-gate		return (NULL);
4927c478bdstevel@tonic-gate
4937c478bdstevel@tonic-gate	return (&kd->kvm_proc);
4947c478bdstevel@tonic-gate}
4957c478bdstevel@tonic-gate
4967c478bdstevel@tonic-gateint
4977c478bdstevel@tonic-gatekvm_setproc(kvm_t *kd)
4987c478bdstevel@tonic-gate{
4997c478bdstevel@tonic-gate	(void) kvm_kread(kd, (uintptr_t)kd->kvm_practive,
5007c478bdstevel@tonic-gate	    &kd->kvm_proc.p_next, sizeof (proc_t *));
5017c478bdstevel@tonic-gate	kd->kvm_pid = -1;
5027c478bdstevel@tonic-gate	return (0);
5037c478bdstevel@tonic-gate}
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gate/*ARGSUSED*/
5067c478bdstevel@tonic-gatestruct user *
5077c478bdstevel@tonic-gatekvm_getu(kvm_t *kd, struct proc *p)
5087c478bdstevel@tonic-gate{
5097c478bdstevel@tonic-gate	return (&p->p_user);
5107c478bdstevel@tonic-gate}
511