2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software developed by the Computer Systems
8 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 * BG 91-66 and contributed to Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
43 #endif /* LIBC_SCCS and not lint */
45 #include <sys/param.h>
46 #include <sys/fnv_hash.h>
51 #include <sys/linker.h>
68 #include "kvm_private.h"
70 SET_DECLARE(kvm_arch, struct kvm_arch);
72 static char _kd_is_null[] = "";
84 _kvm_read_kernel_ehdr(kvm_t *kd)
88 if (elf_version(EV_CURRENT) == EV_NONE) {
89 _kvm_err(kd, kd->program, "Unsupported libelf");
92 elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
94 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
97 if (elf_kind(elf) != ELF_K_ELF) {
98 _kvm_err(kd, kd->program, "kernel is not an ELF file");
101 if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
102 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
108 switch (kd->nlehdr.e_ident[EI_DATA]) {
113 _kvm_err(kd, kd->program,
114 "unsupported ELF data encoding for kernel");
120 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
122 struct kvm_arch **parch;
135 else if (strlen(uf) >= MAXPATHLEN) {
136 _kvm_err(kd, kd->program, "exec file name too long");
139 if (flag & ~O_RDWR) {
140 _kvm_err(kd, kd->program, "bad flags arg");
146 if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
147 _kvm_syserr(kd, kd->program, "%s", mf);
150 if (fstat(kd->pmfd, &st) < 0) {
151 _kvm_syserr(kd, kd->program, "%s", mf);
154 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
156 _kvm_syserr(kd, kd->program, "empty file");
159 if (S_ISCHR(st.st_mode)) {
161 * If this is a character special device, then check that
162 * it's /dev/mem. If so, open kmem too. (Maybe we should
163 * make it work for either /dev/mem or /dev/kmem -- in either
164 * case you're working with a live kernel.)
166 if (strcmp(mf, _PATH_DEVNULL) == 0) {
167 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
169 } else if (strcmp(mf, _PATH_MEM) == 0) {
170 if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
172 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
180 * This is either a crash dump or a remote live system with its physical
181 * memory fully accessible via a special device.
182 * Open the namelist fd and determine the architecture.
184 if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
185 _kvm_syserr(kd, kd->program, "%s", uf);
188 if (_kvm_read_kernel_ehdr(kd) < 0)
190 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
191 strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
195 SET_FOREACH(parch, kvm_arch) {
196 if ((*parch)->ka_probe(kd)) {
201 if (kd->arch == NULL) {
202 _kvm_err(kd, kd->program, "unsupported architecture");
207 * Non-native kernels require a symbol resolver.
209 if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
210 _kvm_err(kd, kd->program,
211 "non-native kernel requires a symbol resolver");
216 * Initialize the virtual address translation machinery.
218 if (kd->arch->ka_initvtop(kd) < 0)
223 * Copy out the error if doing sane error semantics.
226 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
232 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
237 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
239 (void)strlcpy(errout, strerror(errno),
243 return (_kvm_open(kd, uf, mf, flag, errout));
247 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
252 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
254 (void)fprintf(stderr, "%s: %s\n",
255 errstr, strerror(errno));
258 kd->program = errstr;
259 return (_kvm_open(kd, uf, mf, flag, NULL));
263 kvm_open2(const char *uf, const char *mf, int flag, char *errout,
264 int (*resolver)(const char *, kvaddr_t *))
268 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
270 (void)strlcpy(errout, strerror(errno),
274 kd->resolve_symbol = resolver;
275 return (_kvm_open(kd, uf, mf, flag, errout));
287 if (kd->vmst != NULL)
288 kd->arch->ka_freevtop(kd);
290 error |= close(kd->pmfd);
292 error |= close(kd->vmfd);
294 error |= close(kd->nlfd);
295 if (kd->procbase != 0)
296 free((void *)kd->procbase);
298 free((void *) kd->argbuf);
300 free((void *) kd->argspc);
302 free((void *)kd->argv);
303 if (kd->pt_map != NULL)
305 if (kd->page_map != NULL)
307 if (kd->sparse_map != MAP_FAILED)
308 munmap(kd->sparse_map, kd->pt_sparse_size);
315 kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl)
319 * If called via the public interface, permit initialization of
320 * further virtualized modules on demand.
322 return (_kvm_nlist(kd, nl, 1));
326 kvm_nlist(kvm_t *kd, struct nlist *nl)
328 struct kvm_nlist *kl;
332 * Avoid reporting truncated addresses by failing for non-native
335 if (!kvm_native(kd)) {
336 _kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore");
340 for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0';
345 kl = calloc(count + 1, sizeof(*kl));
346 for (i = 0; i < count; i++)
347 kl[i].n_name = nl[i].n_name;
348 nfail = kvm_nlist2(kd, kl);
349 for (i = 0; i < count; i++) {
350 nl[i].n_type = kl[i].n_type;
353 nl[i].n_value = kl[i].n_value;
359 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
362 return (kvm_read2(kd, kva, buf, len));
366 kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
375 * We're using /dev/kmem. Just read straight from the
376 * device and let the active kernel do the address translation.
379 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
380 _kvm_err(kd, 0, "invalid address (0x%jx)",
384 cr = read(kd->vmfd, buf, len);
386 _kvm_syserr(kd, 0, "kvm_read");
388 } else if (cr < (ssize_t)len)
389 _kvm_err(kd, kd->program, "short read");
395 cc = kd->arch->ka_kvatop(kd, kva, &pa);
398 if (cc > (ssize_t)len)
401 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
402 _kvm_syserr(kd, 0, _PATH_MEM);
405 cr = read(kd->pmfd, cp, cc);
407 _kvm_syserr(kd, kd->program, "kvm_read");
411 * If ka_kvatop returns a bogus value or our core file is
412 * truncated, we might wind up seeking beyond the end of the
413 * core file in which case the read will return 0 (EOF).
422 return (cp - (char *)buf);
426 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
433 if (!ISALIVE(kd) && !kd->writable) {
434 _kvm_err(kd, kd->program,
435 "kvm_write not implemented for dead kernels");
441 * Just like kvm_read, only we write.
444 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
445 _kvm_err(kd, 0, "invalid address (%lx)", kva);
448 cc = write(kd->vmfd, buf, len);
450 _kvm_syserr(kd, 0, "kvm_write");
452 } else if ((size_t)cc < len)
453 _kvm_err(kd, kd->program, "short write");
459 cc = kd->arch->ka_kvatop(kd, kva, &pa);
462 if (cc > (ssize_t)len)
465 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
466 _kvm_syserr(kd, 0, _PATH_MEM);
469 cw = write(kd->pmfd, cp, cc);
471 _kvm_syserr(kd, kd->program, "kvm_write");
475 * If ka_kvatop returns a bogus value or our core file is
476 * truncated, we might wind up seeking beyond the end of the
477 * core file in which case the read will return 0 (EOF).
486 return (cp - (const char *)buf);
490 kvm_native(kvm_t *kd)
495 return (kd->arch->ka_native(kd));
499 kvm_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *closure)
502 if (kd->arch->ka_walk_pages == NULL)
505 return (kd->arch->ka_walk_pages(kd, cb, closure));