xref: /freebsd/lib/libkvm/kvm.c (revision 7502cc401b7da56535a38b4f7c8b2d0d86b990c5)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1989, 1992, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software developed by the Computer Systems
658f0484fSRodney W. Grimes  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
758f0484fSRodney W. Grimes  * BG 91-66 and contributed to Berkeley.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
34e67f5b9fSMatthew Dillon #include <sys/cdefs.h>
35e67f5b9fSMatthew Dillon __FBSDID("$FreeBSD$");
36e67f5b9fSMatthew Dillon 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
388771870cSPeter Wemm #if 0
3958f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
408771870cSPeter Wemm #endif
4158f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <sys/param.h>
447f911abeSJohn Baldwin #include <sys/fnv_hash.h>
457cf8b4b9SBjoern A. Zeeb 
467cf8b4b9SBjoern A. Zeeb #define	_WANT_VNET
477cf8b4b9SBjoern A. Zeeb 
4858f0484fSRodney W. Grimes #include <sys/user.h>
49c4a7cdb3SPeter Wemm #include <sys/linker.h>
505f67450dSDimitry Andric #include <sys/pcpu.h>
517f911abeSJohn Baldwin #include <sys/stat.h>
5258f0484fSRodney W. Grimes 
537cf8b4b9SBjoern A. Zeeb #include <net/vnet.h>
547cf8b4b9SBjoern A. Zeeb 
5558f0484fSRodney W. Grimes #include <fcntl.h>
5658f0484fSRodney W. Grimes #include <kvm.h>
5758f0484fSRodney W. Grimes #include <limits.h>
5858f0484fSRodney W. Grimes #include <paths.h>
597f911abeSJohn Baldwin #include <stdint.h>
6058f0484fSRodney W. Grimes #include <stdio.h>
6158f0484fSRodney W. Grimes #include <stdlib.h>
6258f0484fSRodney W. Grimes #include <string.h>
6358f0484fSRodney W. Grimes #include <unistd.h>
6458f0484fSRodney W. Grimes 
6558f0484fSRodney W. Grimes #include "kvm_private.h"
6658f0484fSRodney W. Grimes 
677f911abeSJohn Baldwin SET_DECLARE(kvm_arch, struct kvm_arch);
683f6558c4SMarcel Moolenaar 
6958f0484fSRodney W. Grimes char *
70c10970ddSUlrich Spörlein kvm_geterr(kvm_t *kd)
7158f0484fSRodney W. Grimes {
7258f0484fSRodney W. Grimes 	return (kd->errbuf);
7358f0484fSRodney W. Grimes }
7458f0484fSRodney W. Grimes 
757f911abeSJohn Baldwin static int
767f911abeSJohn Baldwin _kvm_read_kernel_ehdr(kvm_t *kd)
777f911abeSJohn Baldwin {
787f911abeSJohn Baldwin 	Elf *elf;
797f911abeSJohn Baldwin 
807f911abeSJohn Baldwin 	if (elf_version(EV_CURRENT) == EV_NONE) {
817f911abeSJohn Baldwin 		_kvm_err(kd, kd->program, "Unsupported libelf");
827f911abeSJohn Baldwin 		return (-1);
837f911abeSJohn Baldwin 	}
847f911abeSJohn Baldwin 	elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
857f911abeSJohn Baldwin 	if (elf == NULL) {
867f911abeSJohn Baldwin 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
877f911abeSJohn Baldwin 		return (-1);
887f911abeSJohn Baldwin 	}
897f911abeSJohn Baldwin 	if (elf_kind(elf) != ELF_K_ELF) {
907f911abeSJohn Baldwin 		_kvm_err(kd, kd->program, "kernel is not an ELF file");
917f911abeSJohn Baldwin 		return (-1);
927f911abeSJohn Baldwin 	}
937f911abeSJohn Baldwin 	if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
947f911abeSJohn Baldwin 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
957f911abeSJohn Baldwin 		elf_end(elf);
967f911abeSJohn Baldwin 		return (-1);
977f911abeSJohn Baldwin 	}
987f911abeSJohn Baldwin 	elf_end(elf);
997f911abeSJohn Baldwin 
1007f911abeSJohn Baldwin 	switch (kd->nlehdr.e_ident[EI_DATA]) {
1017f911abeSJohn Baldwin 	case ELFDATA2LSB:
1027f911abeSJohn Baldwin 	case ELFDATA2MSB:
1037f911abeSJohn Baldwin 		return (0);
1047f911abeSJohn Baldwin 	default:
1057f911abeSJohn Baldwin 		_kvm_err(kd, kd->program,
1067f911abeSJohn Baldwin 		    "unsupported ELF data encoding for kernel");
1077f911abeSJohn Baldwin 		return (-1);
1087f911abeSJohn Baldwin 	}
1097f911abeSJohn Baldwin }
1107f911abeSJohn Baldwin 
11158f0484fSRodney W. Grimes static kvm_t *
112c10970ddSUlrich Spörlein _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
11358f0484fSRodney W. Grimes {
1147f911abeSJohn Baldwin 	struct kvm_arch **parch;
11558f0484fSRodney W. Grimes 	struct stat st;
11658f0484fSRodney W. Grimes 
11758f0484fSRodney W. Grimes 	kd->vmfd = -1;
11858f0484fSRodney W. Grimes 	kd->pmfd = -1;
11958f0484fSRodney W. Grimes 	kd->nlfd = -1;
120fb0e1892SEnji Cooper 	kd->vmst = NULL;
121fb0e1892SEnji Cooper 	kd->procbase = NULL;
122fb0e1892SEnji Cooper 	kd->argspc = NULL;
123fb0e1892SEnji Cooper 	kd->argv = NULL;
12458f0484fSRodney W. Grimes 
125fb0e1892SEnji Cooper 	if (uf == NULL)
1264be4929cSGarrett Wollman 		uf = getbootfile();
12758f0484fSRodney W. Grimes 	else if (strlen(uf) >= MAXPATHLEN) {
12858f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "exec file name too long");
12958f0484fSRodney W. Grimes 		goto failed;
13058f0484fSRodney W. Grimes 	}
13158f0484fSRodney W. Grimes 	if (flag & ~O_RDWR) {
13258f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "bad flags arg");
13358f0484fSRodney W. Grimes 		goto failed;
13458f0484fSRodney W. Grimes 	}
135fb0e1892SEnji Cooper 	if (mf == NULL)
13658f0484fSRodney W. Grimes 		mf = _PATH_MEM;
13758f0484fSRodney W. Grimes 
13876dce67fSJilles Tjoelker 	if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
13958f0484fSRodney W. Grimes 		_kvm_syserr(kd, kd->program, "%s", mf);
14058f0484fSRodney W. Grimes 		goto failed;
14158f0484fSRodney W. Grimes 	}
14258f0484fSRodney W. Grimes 	if (fstat(kd->pmfd, &st) < 0) {
14358f0484fSRodney W. Grimes 		_kvm_syserr(kd, kd->program, "%s", mf);
14458f0484fSRodney W. Grimes 		goto failed;
14558f0484fSRodney W. Grimes 	}
14665efc5eeSChristian S.J. Peron 	if (S_ISREG(st.st_mode) && st.st_size <= 0) {
14765efc5eeSChristian S.J. Peron 		errno = EINVAL;
14865efc5eeSChristian S.J. Peron 		_kvm_syserr(kd, kd->program, "empty file");
14965efc5eeSChristian S.J. Peron 		goto failed;
15065efc5eeSChristian S.J. Peron 	}
15158f0484fSRodney W. Grimes 	if (S_ISCHR(st.st_mode)) {
15258f0484fSRodney W. Grimes 		/*
15358f0484fSRodney W. Grimes 		 * If this is a character special device, then check that
15458f0484fSRodney W. Grimes 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
15558f0484fSRodney W. Grimes 		 * make it work for either /dev/mem or /dev/kmem -- in either
15658f0484fSRodney W. Grimes 		 * case you're working with a live kernel.)
15758f0484fSRodney W. Grimes 		 */
15835e6b695SPoul-Henning Kamp 		if (strcmp(mf, _PATH_DEVNULL) == 0) {
1597b05a799SJilles Tjoelker 			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
1607928124aSHidetoshi Shimokawa 			return (kd);
1617928124aSHidetoshi Shimokawa 		} else if (strcmp(mf, _PATH_MEM) == 0) {
16276dce67fSJilles Tjoelker 			if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
16376dce67fSJilles Tjoelker 			    0) {
164f76b74d6SJacques Vidrine 				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
165f76b74d6SJacques Vidrine 				goto failed;
166f76b74d6SJacques Vidrine 			}
1677928124aSHidetoshi Shimokawa 			return (kd);
16835e6b695SPoul-Henning Kamp 		}
1697928124aSHidetoshi Shimokawa 	}
170*7502cc40SAndriy Gapon 
17158f0484fSRodney W. Grimes 	/*
172*7502cc40SAndriy Gapon 	 * This is either a crash dump or a remote live system with its physical
173*7502cc40SAndriy Gapon 	 * memory fully accessible via a special device.
1747f911abeSJohn Baldwin 	 * Open the namelist fd and determine the architecture.
17558f0484fSRodney W. Grimes 	 */
17676dce67fSJilles Tjoelker 	if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
177f76b74d6SJacques Vidrine 		_kvm_syserr(kd, kd->program, "%s", uf);
178f76b74d6SJacques Vidrine 		goto failed;
179f76b74d6SJacques Vidrine 	}
1807f911abeSJohn Baldwin 	if (_kvm_read_kernel_ehdr(kd) < 0)
1817f911abeSJohn Baldwin 		goto failed;
182*7502cc40SAndriy Gapon 	if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
183*7502cc40SAndriy Gapon 	    strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
184d7dc9f76SHidetoshi Shimokawa 		kd->rawdump = 1;
185*7502cc40SAndriy Gapon 		kd->writable = 1;
186*7502cc40SAndriy Gapon 	}
1877f911abeSJohn Baldwin 	SET_FOREACH(parch, kvm_arch) {
1887f911abeSJohn Baldwin 		if ((*parch)->ka_probe(kd)) {
1897f911abeSJohn Baldwin 			kd->arch = *parch;
1907f911abeSJohn Baldwin 			break;
1917f911abeSJohn Baldwin 		}
1927f911abeSJohn Baldwin 	}
1937f911abeSJohn Baldwin 	if (kd->arch == NULL) {
1947f911abeSJohn Baldwin 		_kvm_err(kd, kd->program, "unsupported architecture");
1957f911abeSJohn Baldwin 		goto failed;
1967f911abeSJohn Baldwin 	}
1977f911abeSJohn Baldwin 
1987f911abeSJohn Baldwin 	/*
1997f911abeSJohn Baldwin 	 * Non-native kernels require a symbol resolver.
2007f911abeSJohn Baldwin 	 */
2017f911abeSJohn Baldwin 	if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
2027f911abeSJohn Baldwin 		_kvm_err(kd, kd->program,
2037f911abeSJohn Baldwin 		    "non-native kernel requires a symbol resolver");
2047f911abeSJohn Baldwin 		goto failed;
2057f911abeSJohn Baldwin 	}
2067f911abeSJohn Baldwin 
2077f911abeSJohn Baldwin 	/*
2087f911abeSJohn Baldwin 	 * Initialize the virtual address translation machinery.
2097f911abeSJohn Baldwin 	 */
2107f911abeSJohn Baldwin 	if (kd->arch->ka_initvtop(kd) < 0)
21158f0484fSRodney W. Grimes 		goto failed;
21258f0484fSRodney W. Grimes 	return (kd);
21358f0484fSRodney W. Grimes failed:
21458f0484fSRodney W. Grimes 	/*
21558f0484fSRodney W. Grimes 	 * Copy out the error if doing sane error semantics.
21658f0484fSRodney W. Grimes 	 */
217fb0e1892SEnji Cooper 	if (errout != NULL)
218ba3c0383SKris Kennaway 		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
21958f0484fSRodney W. Grimes 	(void)kvm_close(kd);
22058f0484fSRodney W. Grimes 	return (0);
22158f0484fSRodney W. Grimes }
22258f0484fSRodney W. Grimes 
22358f0484fSRodney W. Grimes kvm_t *
224c10970ddSUlrich Spörlein kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
225c10970ddSUlrich Spörlein     char *errout)
22658f0484fSRodney W. Grimes {
227be04b6d1SDavid E. O'Brien 	kvm_t *kd;
22858f0484fSRodney W. Grimes 
22901c56ef2SXin LI 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
230fb0e1892SEnji Cooper 		if (errout != NULL)
231fb0e1892SEnji Cooper 			(void)strlcpy(errout, strerror(errno),
232fb0e1892SEnji Cooper 			    _POSIX2_LINE_MAX);
23358f0484fSRodney W. Grimes 		return (0);
23458f0484fSRodney W. Grimes 	}
2358771870cSPeter Wemm 	return (_kvm_open(kd, uf, mf, flag, errout));
23658f0484fSRodney W. Grimes }
23758f0484fSRodney W. Grimes 
23858f0484fSRodney W. Grimes kvm_t *
239c10970ddSUlrich Spörlein kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
240c10970ddSUlrich Spörlein     const char *errstr)
24158f0484fSRodney W. Grimes {
242be04b6d1SDavid E. O'Brien 	kvm_t *kd;
24358f0484fSRodney W. Grimes 
24401c56ef2SXin LI 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
24564f14011SBruce Evans 		if (errstr != NULL)
24612eaa3d5SPoul-Henning Kamp 			(void)fprintf(stderr, "%s: %s\n",
24764f14011SBruce Evans 				      errstr, strerror(errno));
24858f0484fSRodney W. Grimes 		return (0);
24958f0484fSRodney W. Grimes 	}
25012eaa3d5SPoul-Henning Kamp 	kd->program = errstr;
2518771870cSPeter Wemm 	return (_kvm_open(kd, uf, mf, flag, NULL));
25258f0484fSRodney W. Grimes }
25358f0484fSRodney W. Grimes 
2547f911abeSJohn Baldwin kvm_t *
2557f911abeSJohn Baldwin kvm_open2(const char *uf, const char *mf, int flag, char *errout,
2567f911abeSJohn Baldwin     int (*resolver)(const char *, kvaddr_t *))
2577f911abeSJohn Baldwin {
2587f911abeSJohn Baldwin 	kvm_t *kd;
2597f911abeSJohn Baldwin 
2607f911abeSJohn Baldwin 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
261fb0e1892SEnji Cooper 		if (errout != NULL)
262fb0e1892SEnji Cooper 			(void)strlcpy(errout, strerror(errno),
263fb0e1892SEnji Cooper 			    _POSIX2_LINE_MAX);
2647f911abeSJohn Baldwin 		return (0);
2657f911abeSJohn Baldwin 	}
2667f911abeSJohn Baldwin 	kd->resolve_symbol = resolver;
2677f911abeSJohn Baldwin 	return (_kvm_open(kd, uf, mf, flag, errout));
2687f911abeSJohn Baldwin }
2697f911abeSJohn Baldwin 
27058f0484fSRodney W. Grimes int
271c10970ddSUlrich Spörlein kvm_close(kvm_t *kd)
27258f0484fSRodney W. Grimes {
273be04b6d1SDavid E. O'Brien 	int error = 0;
27458f0484fSRodney W. Grimes 
2757f911abeSJohn Baldwin 	if (kd->vmst != NULL)
2767f911abeSJohn Baldwin 		kd->arch->ka_freevtop(kd);
27758f0484fSRodney W. Grimes 	if (kd->pmfd >= 0)
27858f0484fSRodney W. Grimes 		error |= close(kd->pmfd);
27958f0484fSRodney W. Grimes 	if (kd->vmfd >= 0)
28058f0484fSRodney W. Grimes 		error |= close(kd->vmfd);
28158f0484fSRodney W. Grimes 	if (kd->nlfd >= 0)
28258f0484fSRodney W. Grimes 		error |= close(kd->nlfd);
28358f0484fSRodney W. Grimes 	if (kd->procbase != 0)
28458f0484fSRodney W. Grimes 		free((void *)kd->procbase);
2858b8ffe64SXin LI 	if (kd->argbuf != 0)
2868b8ffe64SXin LI 		free((void *) kd->argbuf);
2878b8ffe64SXin LI 	if (kd->argspc != 0)
2888b8ffe64SXin LI 		free((void *) kd->argspc);
28958f0484fSRodney W. Grimes 	if (kd->argv != 0)
29058f0484fSRodney W. Grimes 		free((void *)kd->argv);
291ffdeef32SWill Andrews 	if (kd->pt_map != NULL)
292ffdeef32SWill Andrews 		free(kd->pt_map);
29358f0484fSRodney W. Grimes 	free((void *)kd);
29458f0484fSRodney W. Grimes 
29558f0484fSRodney W. Grimes 	return (0);
29658f0484fSRodney W. Grimes }
29758f0484fSRodney W. Grimes 
2987cf8b4b9SBjoern A. Zeeb int
2997f911abeSJohn Baldwin kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl)
3007cf8b4b9SBjoern A. Zeeb {
3017cf8b4b9SBjoern A. Zeeb 
3027cf8b4b9SBjoern A. Zeeb 	/*
30375f46cf6SPedro F. Giffuni 	 * If called via the public interface, permit initialization of
3047cf8b4b9SBjoern A. Zeeb 	 * further virtualized modules on demand.
3057cf8b4b9SBjoern A. Zeeb 	 */
3067cf8b4b9SBjoern A. Zeeb 	return (_kvm_nlist(kd, nl, 1));
3077cf8b4b9SBjoern A. Zeeb }
3087cf8b4b9SBjoern A. Zeeb 
3097f911abeSJohn Baldwin int
3107f911abeSJohn Baldwin kvm_nlist(kvm_t *kd, struct nlist *nl)
3117f911abeSJohn Baldwin {
3127f911abeSJohn Baldwin 	struct kvm_nlist *kl;
3137f911abeSJohn Baldwin 	int count, i, nfail;
3147f911abeSJohn Baldwin 
3157f911abeSJohn Baldwin 	/*
3167f911abeSJohn Baldwin 	 * Avoid reporting truncated addresses by failing for non-native
3177f911abeSJohn Baldwin 	 * cores.
3187f911abeSJohn Baldwin 	 */
3197f911abeSJohn Baldwin 	if (!kvm_native(kd)) {
3207f911abeSJohn Baldwin 		_kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore");
3217f911abeSJohn Baldwin 		return (-1);
3227f911abeSJohn Baldwin 	}
3237f911abeSJohn Baldwin 
3247f911abeSJohn Baldwin 	for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0';
3257f911abeSJohn Baldwin 	     count++)
3267f911abeSJohn Baldwin 		;
3277f911abeSJohn Baldwin 	if (count == 0)
3287f911abeSJohn Baldwin 		return (0);
3297f911abeSJohn Baldwin 	kl = calloc(count + 1, sizeof(*kl));
3307f911abeSJohn Baldwin 	for (i = 0; i < count; i++)
3317f911abeSJohn Baldwin 		kl[i].n_name = nl[i].n_name;
3327f911abeSJohn Baldwin 	nfail = kvm_nlist2(kd, kl);
3337f911abeSJohn Baldwin 	for (i = 0; i < count; i++) {
3347f911abeSJohn Baldwin 		nl[i].n_type = kl[i].n_type;
3357f911abeSJohn Baldwin 		nl[i].n_other = 0;
3367f911abeSJohn Baldwin 		nl[i].n_desc = 0;
3377f911abeSJohn Baldwin 		nl[i].n_value = kl[i].n_value;
3387f911abeSJohn Baldwin 	}
3397f911abeSJohn Baldwin 	return (nfail);
3407f911abeSJohn Baldwin }
3417f911abeSJohn Baldwin 
34258f0484fSRodney W. Grimes ssize_t
343c10970ddSUlrich Spörlein kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
34458f0484fSRodney W. Grimes {
3457f911abeSJohn Baldwin 
3467f911abeSJohn Baldwin 	return (kvm_read2(kd, kva, buf, len));
3477f911abeSJohn Baldwin }
3487f911abeSJohn Baldwin 
3497f911abeSJohn Baldwin ssize_t
3507f911abeSJohn Baldwin kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
3517f911abeSJohn Baldwin {
352be04b6d1SDavid E. O'Brien 	int cc;
353c10970ddSUlrich Spörlein 	ssize_t cr;
354c10970ddSUlrich Spörlein 	off_t pa;
3551a5ff928SStefan Farfeleder 	char *cp;
35658f0484fSRodney W. Grimes 
35758f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
35858f0484fSRodney W. Grimes 		/*
35958f0484fSRodney W. Grimes 		 * We're using /dev/kmem.  Just read straight from the
36058f0484fSRodney W. Grimes 		 * device and let the active kernel do the address translation.
36158f0484fSRodney W. Grimes 		 */
36258f0484fSRodney W. Grimes 		errno = 0;
36358f0484fSRodney W. Grimes 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
3647f911abeSJohn Baldwin 			_kvm_err(kd, 0, "invalid address (0x%jx)",
3657f911abeSJohn Baldwin 			    (uintmax_t)kva);
36691a594d8SJacques Vidrine 			return (-1);
36758f0484fSRodney W. Grimes 		}
368c10970ddSUlrich Spörlein 		cr = read(kd->vmfd, buf, len);
369c10970ddSUlrich Spörlein 		if (cr < 0) {
37058f0484fSRodney W. Grimes 			_kvm_syserr(kd, 0, "kvm_read");
37191a594d8SJacques Vidrine 			return (-1);
372c10970ddSUlrich Spörlein 		} else if (cr < (ssize_t)len)
37358f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "short read");
374c10970ddSUlrich Spörlein 		return (cr);
375c10970ddSUlrich Spörlein 	}
376c10970ddSUlrich Spörlein 
37758f0484fSRodney W. Grimes 	cp = buf;
37858f0484fSRodney W. Grimes 	while (len > 0) {
3797f911abeSJohn Baldwin 		cc = kd->arch->ka_kvatop(kd, kva, &pa);
38058f0484fSRodney W. Grimes 		if (cc == 0)
38191a594d8SJacques Vidrine 			return (-1);
382c10970ddSUlrich Spörlein 		if (cc > (ssize_t)len)
38358f0484fSRodney W. Grimes 			cc = len;
38458f0484fSRodney W. Grimes 		errno = 0;
385e55a0cd8SPeter Wemm 		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
38658f0484fSRodney W. Grimes 			_kvm_syserr(kd, 0, _PATH_MEM);
38758f0484fSRodney W. Grimes 			break;
38858f0484fSRodney W. Grimes 		}
389c10970ddSUlrich Spörlein 		cr = read(kd->pmfd, cp, cc);
390c10970ddSUlrich Spörlein 		if (cr < 0) {
39158f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_read");
39258f0484fSRodney W. Grimes 			break;
39358f0484fSRodney W. Grimes 		}
39458f0484fSRodney W. Grimes 		/*
3957f911abeSJohn Baldwin 		 * If ka_kvatop returns a bogus value or our core file is
396c10970ddSUlrich Spörlein 		 * truncated, we might wind up seeking beyond the end of the
397c10970ddSUlrich Spörlein 		 * core file in which case the read will return 0 (EOF).
39858f0484fSRodney W. Grimes 		 */
399c10970ddSUlrich Spörlein 		if (cr == 0)
40058f0484fSRodney W. Grimes 			break;
401c10970ddSUlrich Spörlein 		cp += cr;
402c10970ddSUlrich Spörlein 		kva += cr;
403c10970ddSUlrich Spörlein 		len -= cr;
40458f0484fSRodney W. Grimes 	}
405c10970ddSUlrich Spörlein 
4061a5ff928SStefan Farfeleder 	return (cp - (char *)buf);
40758f0484fSRodney W. Grimes }
40858f0484fSRodney W. Grimes 
40958f0484fSRodney W. Grimes ssize_t
410c10970ddSUlrich Spörlein kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
41158f0484fSRodney W. Grimes {
412be04b6d1SDavid E. O'Brien 	int cc;
413*7502cc40SAndriy Gapon 	ssize_t cw;
414*7502cc40SAndriy Gapon 	off_t pa;
415*7502cc40SAndriy Gapon 	const char *cp;
416*7502cc40SAndriy Gapon 
417*7502cc40SAndriy Gapon 	if (!ISALIVE(kd) && !kd->writable) {
418*7502cc40SAndriy Gapon 		_kvm_err(kd, kd->program,
419*7502cc40SAndriy Gapon 		    "kvm_write not implemented for dead kernels");
420*7502cc40SAndriy Gapon 		return (-1);
421*7502cc40SAndriy Gapon 	}
42258f0484fSRodney W. Grimes 
42358f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
42458f0484fSRodney W. Grimes 		/*
42558f0484fSRodney W. Grimes 		 * Just like kvm_read, only we write.
42658f0484fSRodney W. Grimes 		 */
42758f0484fSRodney W. Grimes 		errno = 0;
42858f0484fSRodney W. Grimes 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
429c10970ddSUlrich Spörlein 			_kvm_err(kd, 0, "invalid address (%lx)", kva);
43091a594d8SJacques Vidrine 			return (-1);
43158f0484fSRodney W. Grimes 		}
43258f0484fSRodney W. Grimes 		cc = write(kd->vmfd, buf, len);
43358f0484fSRodney W. Grimes 		if (cc < 0) {
43458f0484fSRodney W. Grimes 			_kvm_syserr(kd, 0, "kvm_write");
43591a594d8SJacques Vidrine 			return (-1);
436c10970ddSUlrich Spörlein 		} else if ((size_t)cc < len)
43758f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "short write");
43858f0484fSRodney W. Grimes 		return (cc);
43958f0484fSRodney W. Grimes 	}
440*7502cc40SAndriy Gapon 
441*7502cc40SAndriy Gapon 	cp = buf;
442*7502cc40SAndriy Gapon 	while (len > 0) {
443*7502cc40SAndriy Gapon 		cc = kd->arch->ka_kvatop(kd, kva, &pa);
444*7502cc40SAndriy Gapon 		if (cc == 0)
445*7502cc40SAndriy Gapon 			return (-1);
446*7502cc40SAndriy Gapon 		if (cc > (ssize_t)len)
447*7502cc40SAndriy Gapon 			cc = len;
448*7502cc40SAndriy Gapon 		errno = 0;
449*7502cc40SAndriy Gapon 		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
450*7502cc40SAndriy Gapon 			_kvm_syserr(kd, 0, _PATH_MEM);
451*7502cc40SAndriy Gapon 			break;
452*7502cc40SAndriy Gapon 		}
453*7502cc40SAndriy Gapon 		cw = write(kd->pmfd, cp, cc);
454*7502cc40SAndriy Gapon 		if (cw < 0) {
455*7502cc40SAndriy Gapon 			_kvm_syserr(kd, kd->program, "kvm_write");
456*7502cc40SAndriy Gapon 			break;
457*7502cc40SAndriy Gapon 		}
458*7502cc40SAndriy Gapon 		/*
459*7502cc40SAndriy Gapon 		 * If ka_kvatop returns a bogus value or our core file is
460*7502cc40SAndriy Gapon 		 * truncated, we might wind up seeking beyond the end of the
461*7502cc40SAndriy Gapon 		 * core file in which case the read will return 0 (EOF).
462*7502cc40SAndriy Gapon 		 */
463*7502cc40SAndriy Gapon 		if (cw == 0)
464*7502cc40SAndriy Gapon 			break;
465*7502cc40SAndriy Gapon 		cp += cw;
466*7502cc40SAndriy Gapon 		kva += cw;
467*7502cc40SAndriy Gapon 		len -= cw;
468*7502cc40SAndriy Gapon 	}
469*7502cc40SAndriy Gapon 
470*7502cc40SAndriy Gapon 	return (cp - (char *)buf);
47158f0484fSRodney W. Grimes }
4727f911abeSJohn Baldwin 
4737f911abeSJohn Baldwin int
4747f911abeSJohn Baldwin kvm_native(kvm_t *kd)
4757f911abeSJohn Baldwin {
4767f911abeSJohn Baldwin 
4777f911abeSJohn Baldwin 	if (ISALIVE(kd))
4787f911abeSJohn Baldwin 		return (1);
4797f911abeSJohn Baldwin 	return (kd->arch->ka_native(kd));
4807f911abeSJohn Baldwin }
481