158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
458f0484fSRodney W. Grimes * Copyright (c) 1989, 1992, 1993
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * This code is derived from software developed by the Computer Systems
858f0484fSRodney W. Grimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
958f0484fSRodney W. Grimes * BG 91-66 and contributed to Berkeley.
1058f0484fSRodney W. Grimes *
1158f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
1258f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
1358f0484fSRodney W. Grimes * are met:
1458f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1658f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1758f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1858f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
2058f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
2158f0484fSRodney W. Grimes * without specific prior written permission.
2258f0484fSRodney W. Grimes *
2358f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2458f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2758f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes * SUCH DAMAGE.
3458f0484fSRodney W. Grimes */
3558f0484fSRodney W. Grimes
3658f0484fSRodney W. Grimes
3758f0484fSRodney W. Grimes #include <sys/param.h>
387f911abeSJohn Baldwin #include <sys/fnv_hash.h>
397cf8b4b9SBjoern A. Zeeb
407cf8b4b9SBjoern A. Zeeb #define _WANT_VNET
417cf8b4b9SBjoern A. Zeeb
4258f0484fSRodney W. Grimes #include <sys/user.h>
43c4a7cdb3SPeter Wemm #include <sys/linker.h>
445f67450dSDimitry Andric #include <sys/pcpu.h>
457f911abeSJohn Baldwin #include <sys/stat.h>
4638cf2a43SLeandro Lupori #include <sys/sysctl.h>
47c9057838SWill Andrews #include <sys/mman.h>
4858f0484fSRodney W. Grimes
4910108cb6SBjoern A. Zeeb #include <stdbool.h>
507cf8b4b9SBjoern A. Zeeb #include <net/vnet.h>
517cf8b4b9SBjoern A. Zeeb
5258f0484fSRodney W. Grimes #include <fcntl.h>
5358f0484fSRodney W. Grimes #include <kvm.h>
5458f0484fSRodney W. Grimes #include <limits.h>
5558f0484fSRodney W. Grimes #include <paths.h>
567f911abeSJohn Baldwin #include <stdint.h>
5758f0484fSRodney W. Grimes #include <stdio.h>
5858f0484fSRodney W. Grimes #include <stdlib.h>
5958f0484fSRodney W. Grimes #include <string.h>
6058f0484fSRodney W. Grimes #include <unistd.h>
6158f0484fSRodney W. Grimes
6258f0484fSRodney W. Grimes #include "kvm_private.h"
6358f0484fSRodney W. Grimes
647f911abeSJohn Baldwin SET_DECLARE(kvm_arch, struct kvm_arch);
653f6558c4SMarcel Moolenaar
667d0a282aSEnji Cooper static char _kd_is_null[] = "";
677d0a282aSEnji Cooper
6858f0484fSRodney W. Grimes char *
kvm_geterr(kvm_t * kd)69c10970ddSUlrich Spörlein kvm_geterr(kvm_t *kd)
7058f0484fSRodney W. Grimes {
717d0a282aSEnji Cooper
727d0a282aSEnji Cooper if (kd == NULL)
737d0a282aSEnji Cooper return (_kd_is_null);
7458f0484fSRodney W. Grimes return (kd->errbuf);
7558f0484fSRodney W. Grimes }
7658f0484fSRodney W. Grimes
777f911abeSJohn Baldwin static int
_kvm_read_kernel_ehdr(kvm_t * kd)787f911abeSJohn Baldwin _kvm_read_kernel_ehdr(kvm_t *kd)
797f911abeSJohn Baldwin {
807f911abeSJohn Baldwin Elf *elf;
817f911abeSJohn Baldwin
827f911abeSJohn Baldwin if (elf_version(EV_CURRENT) == EV_NONE) {
837f911abeSJohn Baldwin _kvm_err(kd, kd->program, "Unsupported libelf");
847f911abeSJohn Baldwin return (-1);
857f911abeSJohn Baldwin }
867f911abeSJohn Baldwin elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
877f911abeSJohn Baldwin if (elf == NULL) {
887f911abeSJohn Baldwin _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
897f911abeSJohn Baldwin return (-1);
907f911abeSJohn Baldwin }
917f911abeSJohn Baldwin if (elf_kind(elf) != ELF_K_ELF) {
927f911abeSJohn Baldwin _kvm_err(kd, kd->program, "kernel is not an ELF file");
937f911abeSJohn Baldwin return (-1);
947f911abeSJohn Baldwin }
957f911abeSJohn Baldwin if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
967f911abeSJohn Baldwin _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
977f911abeSJohn Baldwin elf_end(elf);
987f911abeSJohn Baldwin return (-1);
997f911abeSJohn Baldwin }
1007f911abeSJohn Baldwin elf_end(elf);
1017f911abeSJohn Baldwin
1027f911abeSJohn Baldwin switch (kd->nlehdr.e_ident[EI_DATA]) {
1037f911abeSJohn Baldwin case ELFDATA2LSB:
1047f911abeSJohn Baldwin case ELFDATA2MSB:
1057f911abeSJohn Baldwin return (0);
1067f911abeSJohn Baldwin default:
1077f911abeSJohn Baldwin _kvm_err(kd, kd->program,
1087f911abeSJohn Baldwin "unsupported ELF data encoding for kernel");
1097f911abeSJohn Baldwin return (-1);
1107f911abeSJohn Baldwin }
1117f911abeSJohn Baldwin }
1127f911abeSJohn Baldwin
11358f0484fSRodney W. Grimes static kvm_t *
_kvm_open(kvm_t * kd,const char * uf,const char * mf,int flag,char * errout)114c10970ddSUlrich Spörlein _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
11558f0484fSRodney W. Grimes {
1167f911abeSJohn Baldwin struct kvm_arch **parch;
11758f0484fSRodney W. Grimes struct stat st;
11858f0484fSRodney W. Grimes
11958f0484fSRodney W. Grimes kd->vmfd = -1;
12058f0484fSRodney W. Grimes kd->pmfd = -1;
12158f0484fSRodney W. Grimes kd->nlfd = -1;
122fb0e1892SEnji Cooper kd->vmst = NULL;
123fb0e1892SEnji Cooper kd->procbase = NULL;
124fb0e1892SEnji Cooper kd->argspc = NULL;
125fb0e1892SEnji Cooper kd->argv = NULL;
12658f0484fSRodney W. Grimes
127fb0e1892SEnji Cooper if (uf == NULL)
1284be4929cSGarrett Wollman uf = getbootfile();
12958f0484fSRodney W. Grimes else if (strlen(uf) >= MAXPATHLEN) {
13058f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "exec file name too long");
13158f0484fSRodney W. Grimes goto failed;
13258f0484fSRodney W. Grimes }
13358f0484fSRodney W. Grimes if (flag & ~O_RDWR) {
13458f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "bad flags arg");
13558f0484fSRodney W. Grimes goto failed;
13658f0484fSRodney W. Grimes }
137fb0e1892SEnji Cooper if (mf == NULL)
13858f0484fSRodney W. Grimes mf = _PATH_MEM;
13958f0484fSRodney W. Grimes
14076dce67fSJilles Tjoelker if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
14158f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "%s", mf);
14258f0484fSRodney W. Grimes goto failed;
14358f0484fSRodney W. Grimes }
14458f0484fSRodney W. Grimes if (fstat(kd->pmfd, &st) < 0) {
14558f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "%s", mf);
14658f0484fSRodney W. Grimes goto failed;
14758f0484fSRodney W. Grimes }
14865efc5eeSChristian S.J. Peron if (S_ISREG(st.st_mode) && st.st_size <= 0) {
14965efc5eeSChristian S.J. Peron errno = EINVAL;
15065efc5eeSChristian S.J. Peron _kvm_syserr(kd, kd->program, "empty file");
15165efc5eeSChristian S.J. Peron goto failed;
15265efc5eeSChristian S.J. Peron }
15358f0484fSRodney W. Grimes if (S_ISCHR(st.st_mode)) {
15458f0484fSRodney W. Grimes /*
15558f0484fSRodney W. Grimes * If this is a character special device, then check that
15658f0484fSRodney W. Grimes * it's /dev/mem. If so, open kmem too. (Maybe we should
15758f0484fSRodney W. Grimes * make it work for either /dev/mem or /dev/kmem -- in either
15858f0484fSRodney W. Grimes * case you're working with a live kernel.)
15958f0484fSRodney W. Grimes */
16035e6b695SPoul-Henning Kamp if (strcmp(mf, _PATH_DEVNULL) == 0) {
1617b05a799SJilles Tjoelker kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
1627928124aSHidetoshi Shimokawa return (kd);
1637928124aSHidetoshi Shimokawa } else if (strcmp(mf, _PATH_MEM) == 0) {
16476dce67fSJilles Tjoelker if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
16576dce67fSJilles Tjoelker 0) {
166f76b74d6SJacques Vidrine _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
167f76b74d6SJacques Vidrine goto failed;
168f76b74d6SJacques Vidrine }
1697928124aSHidetoshi Shimokawa return (kd);
17035e6b695SPoul-Henning Kamp }
1717928124aSHidetoshi Shimokawa }
1727502cc40SAndriy Gapon
17358f0484fSRodney W. Grimes /*
1747502cc40SAndriy Gapon * This is either a crash dump or a remote live system with its physical
1757502cc40SAndriy Gapon * memory fully accessible via a special device.
1767f911abeSJohn Baldwin * Open the namelist fd and determine the architecture.
17758f0484fSRodney W. Grimes */
17876dce67fSJilles Tjoelker if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
179f76b74d6SJacques Vidrine _kvm_syserr(kd, kd->program, "%s", uf);
180f76b74d6SJacques Vidrine goto failed;
181f76b74d6SJacques Vidrine }
1827f911abeSJohn Baldwin if (_kvm_read_kernel_ehdr(kd) < 0)
1837f911abeSJohn Baldwin goto failed;
1847502cc40SAndriy Gapon if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
1857502cc40SAndriy Gapon strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
186d7dc9f76SHidetoshi Shimokawa kd->rawdump = 1;
1877502cc40SAndriy Gapon kd->writable = 1;
1887502cc40SAndriy Gapon }
1897f911abeSJohn Baldwin SET_FOREACH(parch, kvm_arch) {
1907f911abeSJohn Baldwin if ((*parch)->ka_probe(kd)) {
1917f911abeSJohn Baldwin kd->arch = *parch;
1927f911abeSJohn Baldwin break;
1937f911abeSJohn Baldwin }
1947f911abeSJohn Baldwin }
1957f911abeSJohn Baldwin if (kd->arch == NULL) {
1967f911abeSJohn Baldwin _kvm_err(kd, kd->program, "unsupported architecture");
1977f911abeSJohn Baldwin goto failed;
1987f911abeSJohn Baldwin }
1997f911abeSJohn Baldwin
2007f911abeSJohn Baldwin /*
2017f911abeSJohn Baldwin * Non-native kernels require a symbol resolver.
2027f911abeSJohn Baldwin */
2037f911abeSJohn Baldwin if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
2047f911abeSJohn Baldwin _kvm_err(kd, kd->program,
2057f911abeSJohn Baldwin "non-native kernel requires a symbol resolver");
2067f911abeSJohn Baldwin goto failed;
2077f911abeSJohn Baldwin }
2087f911abeSJohn Baldwin
2097f911abeSJohn Baldwin /*
2107f911abeSJohn Baldwin * Initialize the virtual address translation machinery.
2117f911abeSJohn Baldwin */
2127f911abeSJohn Baldwin if (kd->arch->ka_initvtop(kd) < 0)
21358f0484fSRodney W. Grimes goto failed;
21458f0484fSRodney W. Grimes return (kd);
21558f0484fSRodney W. Grimes failed:
21658f0484fSRodney W. Grimes /*
21758f0484fSRodney W. Grimes * Copy out the error if doing sane error semantics.
21858f0484fSRodney W. Grimes */
219fb0e1892SEnji Cooper if (errout != NULL)
220ba3c0383SKris Kennaway strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
22158f0484fSRodney W. Grimes (void)kvm_close(kd);
22228047d83SEnji Cooper return (NULL);
22358f0484fSRodney W. Grimes }
22458f0484fSRodney W. Grimes
22558f0484fSRodney W. Grimes kvm_t *
kvm_openfiles(const char * uf,const char * mf,const char * sf __unused,int flag,char * errout)226c10970ddSUlrich Spörlein kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
227c10970ddSUlrich Spörlein char *errout)
22858f0484fSRodney W. Grimes {
229be04b6d1SDavid E. O'Brien kvm_t *kd;
23058f0484fSRodney W. Grimes
23101c56ef2SXin LI if ((kd = calloc(1, sizeof(*kd))) == NULL) {
232fb0e1892SEnji Cooper if (errout != NULL)
233fb0e1892SEnji Cooper (void)strlcpy(errout, strerror(errno),
234fb0e1892SEnji Cooper _POSIX2_LINE_MAX);
23528047d83SEnji Cooper return (NULL);
23658f0484fSRodney W. Grimes }
2378771870cSPeter Wemm return (_kvm_open(kd, uf, mf, flag, errout));
23858f0484fSRodney W. Grimes }
23958f0484fSRodney W. Grimes
24058f0484fSRodney W. Grimes kvm_t *
kvm_open(const char * uf,const char * mf,const char * sf __unused,int flag,const char * errstr)241c10970ddSUlrich Spörlein kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
242c10970ddSUlrich Spörlein const char *errstr)
24358f0484fSRodney W. Grimes {
244be04b6d1SDavid E. O'Brien kvm_t *kd;
24558f0484fSRodney W. Grimes
24601c56ef2SXin LI if ((kd = calloc(1, sizeof(*kd))) == NULL) {
24764f14011SBruce Evans if (errstr != NULL)
24812eaa3d5SPoul-Henning Kamp (void)fprintf(stderr, "%s: %s\n",
24964f14011SBruce Evans errstr, strerror(errno));
25028047d83SEnji Cooper return (NULL);
25158f0484fSRodney W. Grimes }
25212eaa3d5SPoul-Henning Kamp kd->program = errstr;
2538771870cSPeter Wemm return (_kvm_open(kd, uf, mf, flag, NULL));
25458f0484fSRodney W. Grimes }
25558f0484fSRodney W. Grimes
2567f911abeSJohn Baldwin kvm_t *
kvm_open2(const char * uf,const char * mf,int flag,char * errout,int (* resolver)(const char *,kvaddr_t *))2577f911abeSJohn Baldwin kvm_open2(const char *uf, const char *mf, int flag, char *errout,
2587f911abeSJohn Baldwin int (*resolver)(const char *, kvaddr_t *))
2597f911abeSJohn Baldwin {
2607f911abeSJohn Baldwin kvm_t *kd;
2617f911abeSJohn Baldwin
2627f911abeSJohn Baldwin if ((kd = calloc(1, sizeof(*kd))) == NULL) {
263fb0e1892SEnji Cooper if (errout != NULL)
264fb0e1892SEnji Cooper (void)strlcpy(errout, strerror(errno),
265fb0e1892SEnji Cooper _POSIX2_LINE_MAX);
26628047d83SEnji Cooper return (NULL);
2677f911abeSJohn Baldwin }
2687f911abeSJohn Baldwin kd->resolve_symbol = resolver;
2697f911abeSJohn Baldwin return (_kvm_open(kd, uf, mf, flag, errout));
2707f911abeSJohn Baldwin }
2717f911abeSJohn Baldwin
27258f0484fSRodney W. Grimes int
kvm_close(kvm_t * kd)273c10970ddSUlrich Spörlein kvm_close(kvm_t *kd)
27458f0484fSRodney W. Grimes {
275be04b6d1SDavid E. O'Brien int error = 0;
27658f0484fSRodney W. Grimes
27754fc00f7SEnji Cooper if (kd == NULL) {
27854fc00f7SEnji Cooper errno = EINVAL;
27954fc00f7SEnji Cooper return (-1);
28054fc00f7SEnji Cooper }
2817f911abeSJohn Baldwin if (kd->vmst != NULL)
2827f911abeSJohn Baldwin kd->arch->ka_freevtop(kd);
28358f0484fSRodney W. Grimes if (kd->pmfd >= 0)
28458f0484fSRodney W. Grimes error |= close(kd->pmfd);
28558f0484fSRodney W. Grimes if (kd->vmfd >= 0)
28658f0484fSRodney W. Grimes error |= close(kd->vmfd);
28758f0484fSRodney W. Grimes if (kd->nlfd >= 0)
28858f0484fSRodney W. Grimes error |= close(kd->nlfd);
28958f0484fSRodney W. Grimes if (kd->procbase != 0)
29058f0484fSRodney W. Grimes free((void *)kd->procbase);
2918b8ffe64SXin LI if (kd->argbuf != 0)
2928b8ffe64SXin LI free((void *) kd->argbuf);
2938b8ffe64SXin LI if (kd->argspc != 0)
2948b8ffe64SXin LI free((void *) kd->argspc);
29558f0484fSRodney W. Grimes if (kd->argv != 0)
29658f0484fSRodney W. Grimes free((void *)kd->argv);
297483c6da3SJung-uk Kim if (kd->dpcpu_initialized != 0)
298483c6da3SJung-uk Kim free(kd->dpcpu_off);
299ffdeef32SWill Andrews if (kd->pt_map != NULL)
300ffdeef32SWill Andrews free(kd->pt_map);
301c9057838SWill Andrews if (kd->page_map != NULL)
302c9057838SWill Andrews free(kd->page_map);
303*e6901a29SAshish SHUKLA if (kd->sparse_map != MAP_FAILED && kd->sparse_map != NULL)
304c9057838SWill Andrews munmap(kd->sparse_map, kd->pt_sparse_size);
30558f0484fSRodney W. Grimes free((void *)kd);
30658f0484fSRodney W. Grimes
3074937896aSEnji Cooper return (error);
30858f0484fSRodney W. Grimes }
30958f0484fSRodney W. Grimes
3107cf8b4b9SBjoern A. Zeeb int
kvm_nlist2(kvm_t * kd,struct kvm_nlist * nl)3117f911abeSJohn Baldwin kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl)
3127cf8b4b9SBjoern A. Zeeb {
3137cf8b4b9SBjoern A. Zeeb
3147cf8b4b9SBjoern A. Zeeb /*
31575f46cf6SPedro F. Giffuni * If called via the public interface, permit initialization of
3167cf8b4b9SBjoern A. Zeeb * further virtualized modules on demand.
3177cf8b4b9SBjoern A. Zeeb */
3187cf8b4b9SBjoern A. Zeeb return (_kvm_nlist(kd, nl, 1));
3197cf8b4b9SBjoern A. Zeeb }
3207cf8b4b9SBjoern A. Zeeb
3217f911abeSJohn Baldwin int
kvm_nlist(kvm_t * kd,struct nlist * nl)3227f911abeSJohn Baldwin kvm_nlist(kvm_t *kd, struct nlist *nl)
3237f911abeSJohn Baldwin {
3247f911abeSJohn Baldwin struct kvm_nlist *kl;
3257f911abeSJohn Baldwin int count, i, nfail;
3267f911abeSJohn Baldwin
3277f911abeSJohn Baldwin /*
3287f911abeSJohn Baldwin * Avoid reporting truncated addresses by failing for non-native
3297f911abeSJohn Baldwin * cores.
3307f911abeSJohn Baldwin */
3317f911abeSJohn Baldwin if (!kvm_native(kd)) {
3327f911abeSJohn Baldwin _kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore");
3337f911abeSJohn Baldwin return (-1);
3347f911abeSJohn Baldwin }
3357f911abeSJohn Baldwin
3367f911abeSJohn Baldwin for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0';
3377f911abeSJohn Baldwin count++)
3387f911abeSJohn Baldwin ;
3397f911abeSJohn Baldwin if (count == 0)
3407f911abeSJohn Baldwin return (0);
3417f911abeSJohn Baldwin kl = calloc(count + 1, sizeof(*kl));
342645eaa2cSJung-uk Kim if (kl == NULL) {
343645eaa2cSJung-uk Kim _kvm_err(kd, kd->program, "cannot allocate memory");
344645eaa2cSJung-uk Kim return (-1);
345645eaa2cSJung-uk Kim }
3467f911abeSJohn Baldwin for (i = 0; i < count; i++)
3477f911abeSJohn Baldwin kl[i].n_name = nl[i].n_name;
3487f911abeSJohn Baldwin nfail = kvm_nlist2(kd, kl);
3497f911abeSJohn Baldwin for (i = 0; i < count; i++) {
3507f911abeSJohn Baldwin nl[i].n_type = kl[i].n_type;
3517f911abeSJohn Baldwin nl[i].n_other = 0;
3527f911abeSJohn Baldwin nl[i].n_desc = 0;
3537f911abeSJohn Baldwin nl[i].n_value = kl[i].n_value;
3547f911abeSJohn Baldwin }
355645eaa2cSJung-uk Kim free(kl);
3567f911abeSJohn Baldwin return (nfail);
3577f911abeSJohn Baldwin }
3587f911abeSJohn Baldwin
35958f0484fSRodney W. Grimes ssize_t
kvm_read(kvm_t * kd,u_long kva,void * buf,size_t len)360c10970ddSUlrich Spörlein kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
36158f0484fSRodney W. Grimes {
3627f911abeSJohn Baldwin
3637f911abeSJohn Baldwin return (kvm_read2(kd, kva, buf, len));
3647f911abeSJohn Baldwin }
3657f911abeSJohn Baldwin
3667f911abeSJohn Baldwin ssize_t
kvm_read2(kvm_t * kd,kvaddr_t kva,void * buf,size_t len)3677f911abeSJohn Baldwin kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
3687f911abeSJohn Baldwin {
369be04b6d1SDavid E. O'Brien int cc;
370c10970ddSUlrich Spörlein ssize_t cr;
371c10970ddSUlrich Spörlein off_t pa;
3721a5ff928SStefan Farfeleder char *cp;
37358f0484fSRodney W. Grimes
37458f0484fSRodney W. Grimes if (ISALIVE(kd)) {
37558f0484fSRodney W. Grimes /*
37658f0484fSRodney W. Grimes * We're using /dev/kmem. Just read straight from the
37758f0484fSRodney W. Grimes * device and let the active kernel do the address translation.
37858f0484fSRodney W. Grimes */
37958f0484fSRodney W. Grimes errno = 0;
38058f0484fSRodney W. Grimes if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
3817f911abeSJohn Baldwin _kvm_err(kd, 0, "invalid address (0x%jx)",
3827f911abeSJohn Baldwin (uintmax_t)kva);
38391a594d8SJacques Vidrine return (-1);
38458f0484fSRodney W. Grimes }
385c10970ddSUlrich Spörlein cr = read(kd->vmfd, buf, len);
386c10970ddSUlrich Spörlein if (cr < 0) {
38758f0484fSRodney W. Grimes _kvm_syserr(kd, 0, "kvm_read");
38891a594d8SJacques Vidrine return (-1);
389c10970ddSUlrich Spörlein } else if (cr < (ssize_t)len)
39058f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "short read");
391c10970ddSUlrich Spörlein return (cr);
392c10970ddSUlrich Spörlein }
393c10970ddSUlrich Spörlein
39458f0484fSRodney W. Grimes cp = buf;
39558f0484fSRodney W. Grimes while (len > 0) {
3967f911abeSJohn Baldwin cc = kd->arch->ka_kvatop(kd, kva, &pa);
39758f0484fSRodney W. Grimes if (cc == 0)
39891a594d8SJacques Vidrine return (-1);
399c10970ddSUlrich Spörlein if (cc > (ssize_t)len)
40058f0484fSRodney W. Grimes cc = len;
40158f0484fSRodney W. Grimes errno = 0;
402e55a0cd8SPeter Wemm if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
40358f0484fSRodney W. Grimes _kvm_syserr(kd, 0, _PATH_MEM);
40458f0484fSRodney W. Grimes break;
40558f0484fSRodney W. Grimes }
406c10970ddSUlrich Spörlein cr = read(kd->pmfd, cp, cc);
407c10970ddSUlrich Spörlein if (cr < 0) {
40858f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "kvm_read");
40958f0484fSRodney W. Grimes break;
41058f0484fSRodney W. Grimes }
41158f0484fSRodney W. Grimes /*
4127f911abeSJohn Baldwin * If ka_kvatop returns a bogus value or our core file is
413c10970ddSUlrich Spörlein * truncated, we might wind up seeking beyond the end of the
414c10970ddSUlrich Spörlein * core file in which case the read will return 0 (EOF).
41558f0484fSRodney W. Grimes */
416c10970ddSUlrich Spörlein if (cr == 0)
41758f0484fSRodney W. Grimes break;
418c10970ddSUlrich Spörlein cp += cr;
419c10970ddSUlrich Spörlein kva += cr;
420c10970ddSUlrich Spörlein len -= cr;
42158f0484fSRodney W. Grimes }
422c10970ddSUlrich Spörlein
4231a5ff928SStefan Farfeleder return (cp - (char *)buf);
42458f0484fSRodney W. Grimes }
42558f0484fSRodney W. Grimes
42658f0484fSRodney W. Grimes ssize_t
kvm_write(kvm_t * kd,u_long kva,const void * buf,size_t len)427c10970ddSUlrich Spörlein kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
42858f0484fSRodney W. Grimes {
429be04b6d1SDavid E. O'Brien int cc;
4307502cc40SAndriy Gapon ssize_t cw;
4317502cc40SAndriy Gapon off_t pa;
4327502cc40SAndriy Gapon const char *cp;
4337502cc40SAndriy Gapon
4347502cc40SAndriy Gapon if (!ISALIVE(kd) && !kd->writable) {
4357502cc40SAndriy Gapon _kvm_err(kd, kd->program,
4367502cc40SAndriy Gapon "kvm_write not implemented for dead kernels");
4377502cc40SAndriy Gapon return (-1);
4387502cc40SAndriy Gapon }
43958f0484fSRodney W. Grimes
44058f0484fSRodney W. Grimes if (ISALIVE(kd)) {
44158f0484fSRodney W. Grimes /*
44258f0484fSRodney W. Grimes * Just like kvm_read, only we write.
44358f0484fSRodney W. Grimes */
44458f0484fSRodney W. Grimes errno = 0;
44558f0484fSRodney W. Grimes if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
446c10970ddSUlrich Spörlein _kvm_err(kd, 0, "invalid address (%lx)", kva);
44791a594d8SJacques Vidrine return (-1);
44858f0484fSRodney W. Grimes }
44958f0484fSRodney W. Grimes cc = write(kd->vmfd, buf, len);
45058f0484fSRodney W. Grimes if (cc < 0) {
45158f0484fSRodney W. Grimes _kvm_syserr(kd, 0, "kvm_write");
45291a594d8SJacques Vidrine return (-1);
453c10970ddSUlrich Spörlein } else if ((size_t)cc < len)
45458f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "short write");
45558f0484fSRodney W. Grimes return (cc);
45658f0484fSRodney W. Grimes }
4577502cc40SAndriy Gapon
4587502cc40SAndriy Gapon cp = buf;
4597502cc40SAndriy Gapon while (len > 0) {
4607502cc40SAndriy Gapon cc = kd->arch->ka_kvatop(kd, kva, &pa);
4617502cc40SAndriy Gapon if (cc == 0)
4627502cc40SAndriy Gapon return (-1);
4637502cc40SAndriy Gapon if (cc > (ssize_t)len)
4647502cc40SAndriy Gapon cc = len;
4657502cc40SAndriy Gapon errno = 0;
4667502cc40SAndriy Gapon if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
4677502cc40SAndriy Gapon _kvm_syserr(kd, 0, _PATH_MEM);
4687502cc40SAndriy Gapon break;
4697502cc40SAndriy Gapon }
4707502cc40SAndriy Gapon cw = write(kd->pmfd, cp, cc);
4717502cc40SAndriy Gapon if (cw < 0) {
4727502cc40SAndriy Gapon _kvm_syserr(kd, kd->program, "kvm_write");
4737502cc40SAndriy Gapon break;
4747502cc40SAndriy Gapon }
4757502cc40SAndriy Gapon /*
4767502cc40SAndriy Gapon * If ka_kvatop returns a bogus value or our core file is
4777502cc40SAndriy Gapon * truncated, we might wind up seeking beyond the end of the
4787502cc40SAndriy Gapon * core file in which case the read will return 0 (EOF).
4797502cc40SAndriy Gapon */
4807502cc40SAndriy Gapon if (cw == 0)
4817502cc40SAndriy Gapon break;
4827502cc40SAndriy Gapon cp += cw;
4837502cc40SAndriy Gapon kva += cw;
4847502cc40SAndriy Gapon len -= cw;
4857502cc40SAndriy Gapon }
4867502cc40SAndriy Gapon
4870f5425a4SEnji Cooper return (cp - (const char *)buf);
48858f0484fSRodney W. Grimes }
4897f911abeSJohn Baldwin
4907f911abeSJohn Baldwin int
kvm_native(kvm_t * kd)4917f911abeSJohn Baldwin kvm_native(kvm_t *kd)
4927f911abeSJohn Baldwin {
4937f911abeSJohn Baldwin
4947f911abeSJohn Baldwin if (ISALIVE(kd))
4957f911abeSJohn Baldwin return (1);
4967f911abeSJohn Baldwin return (kd->arch->ka_native(kd));
4977f911abeSJohn Baldwin }
498c9057838SWill Andrews
499c9057838SWill Andrews int
kvm_walk_pages(kvm_t * kd,kvm_walk_pages_cb_t * cb,void * closure)500c9057838SWill Andrews kvm_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *closure)
501c9057838SWill Andrews {
502c9057838SWill Andrews
503c9057838SWill Andrews if (kd->arch->ka_walk_pages == NULL)
504c9057838SWill Andrews return (0);
505c9057838SWill Andrews
506c9057838SWill Andrews return (kd->arch->ka_walk_pages(kd, cb, closure));
507c9057838SWill Andrews }
50838cf2a43SLeandro Lupori
50938cf2a43SLeandro Lupori kssize_t
kvm_kerndisp(kvm_t * kd)51038cf2a43SLeandro Lupori kvm_kerndisp(kvm_t *kd)
51138cf2a43SLeandro Lupori {
51238cf2a43SLeandro Lupori unsigned long kernbase, rel_kernbase;
51338cf2a43SLeandro Lupori size_t kernbase_len = sizeof(kernbase);
51438cf2a43SLeandro Lupori size_t rel_kernbase_len = sizeof(rel_kernbase);
51538cf2a43SLeandro Lupori
51638cf2a43SLeandro Lupori if (ISALIVE(kd)) {
51738cf2a43SLeandro Lupori if (sysctlbyname("kern.base_address", &kernbase,
51838cf2a43SLeandro Lupori &kernbase_len, NULL, 0) == -1) {
51938cf2a43SLeandro Lupori _kvm_syserr(kd, kd->program,
52038cf2a43SLeandro Lupori "failed to get kernel base address");
52138cf2a43SLeandro Lupori return (0);
52238cf2a43SLeandro Lupori }
52338cf2a43SLeandro Lupori if (sysctlbyname("kern.relbase_address", &rel_kernbase,
52438cf2a43SLeandro Lupori &rel_kernbase_len, NULL, 0) == -1) {
52538cf2a43SLeandro Lupori _kvm_syserr(kd, kd->program,
52638cf2a43SLeandro Lupori "failed to get relocated kernel base address");
52738cf2a43SLeandro Lupori return (0);
52838cf2a43SLeandro Lupori }
52938cf2a43SLeandro Lupori return (rel_kernbase - kernbase);
53038cf2a43SLeandro Lupori }
53138cf2a43SLeandro Lupori
53238cf2a43SLeandro Lupori if (kd->arch->ka_kerndisp == NULL)
53338cf2a43SLeandro Lupori return (0);
53438cf2a43SLeandro Lupori
53538cf2a43SLeandro Lupori return (kd->arch->ka_kerndisp(kd));
53638cf2a43SLeandro Lupori }
537