2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
41 #endif /* LIBC_SCCS and not lint */
43 #include <sys/param.h>
44 #include <sys/fnv_hash.h>
49 #include <sys/linker.h>
65 #include "kvm_private.h"
67 SET_DECLARE(kvm_arch, struct kvm_arch);
69 static char _kd_is_null[] = "";
81 _kvm_read_kernel_ehdr(kvm_t *kd)
85 if (elf_version(EV_CURRENT) == EV_NONE) {
86 _kvm_err(kd, kd->program, "Unsupported libelf");
89 elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
91 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
94 if (elf_kind(elf) != ELF_K_ELF) {
95 _kvm_err(kd, kd->program, "kernel is not an ELF file");
98 if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
99 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
105 switch (kd->nlehdr.e_ident[EI_DATA]) {
110 _kvm_err(kd, kd->program,
111 "unsupported ELF data encoding for kernel");
117 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
119 struct kvm_arch **parch;
132 else if (strlen(uf) >= MAXPATHLEN) {
133 _kvm_err(kd, kd->program, "exec file name too long");
136 if (flag & ~O_RDWR) {
137 _kvm_err(kd, kd->program, "bad flags arg");
143 if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
144 _kvm_syserr(kd, kd->program, "%s", mf);
147 if (fstat(kd->pmfd, &st) < 0) {
148 _kvm_syserr(kd, kd->program, "%s", mf);
151 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
153 _kvm_syserr(kd, kd->program, "empty file");
156 if (S_ISCHR(st.st_mode)) {
158 * If this is a character special device, then check that
159 * it's /dev/mem. If so, open kmem too. (Maybe we should
160 * make it work for either /dev/mem or /dev/kmem -- in either
161 * case you're working with a live kernel.)
163 if (strcmp(mf, _PATH_DEVNULL) == 0) {
164 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
166 } else if (strcmp(mf, _PATH_MEM) == 0) {
167 if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
169 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
177 * This is either a crash dump or a remote live system with its physical
178 * memory fully accessible via a special device.
179 * Open the namelist fd and determine the architecture.
181 if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
182 _kvm_syserr(kd, kd->program, "%s", uf);
185 if (_kvm_read_kernel_ehdr(kd) < 0)
187 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
188 strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
192 SET_FOREACH(parch, kvm_arch) {
193 if ((*parch)->ka_probe(kd)) {
198 if (kd->arch == NULL) {
199 _kvm_err(kd, kd->program, "unsupported architecture");
204 * Non-native kernels require a symbol resolver.
206 if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
207 _kvm_err(kd, kd->program,
208 "non-native kernel requires a symbol resolver");
213 * Initialize the virtual address translation machinery.
215 if (kd->arch->ka_initvtop(kd) < 0)
220 * Copy out the error if doing sane error semantics.
223 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
229 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
234 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
236 (void)strlcpy(errout, strerror(errno),
240 return (_kvm_open(kd, uf, mf, flag, errout));
244 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
249 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
251 (void)fprintf(stderr, "%s: %s\n",
252 errstr, strerror(errno));
255 kd->program = errstr;
256 return (_kvm_open(kd, uf, mf, flag, NULL));
260 kvm_open2(const char *uf, const char *mf, int flag, char *errout,
261 int (*resolver)(const char *, kvaddr_t *))
265 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
267 (void)strlcpy(errout, strerror(errno),
271 kd->resolve_symbol = resolver;
272 return (_kvm_open(kd, uf, mf, flag, errout));
284 if (kd->vmst != NULL)
285 kd->arch->ka_freevtop(kd);
287 error |= close(kd->pmfd);
289 error |= close(kd->vmfd);
291 error |= close(kd->nlfd);
292 if (kd->procbase != 0)
293 free((void *)kd->procbase);
295 free((void *) kd->argbuf);
297 free((void *) kd->argspc);
299 free((void *)kd->argv);
300 if (kd->pt_map != NULL)
308 kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl)
312 * If called via the public interface, permit initialization of
313 * further virtualized modules on demand.
315 return (_kvm_nlist(kd, nl, 1));
319 kvm_nlist(kvm_t *kd, struct nlist *nl)
321 struct kvm_nlist *kl;
325 * Avoid reporting truncated addresses by failing for non-native
328 if (!kvm_native(kd)) {
329 _kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore");
333 for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0';
338 kl = calloc(count + 1, sizeof(*kl));
339 for (i = 0; i < count; i++)
340 kl[i].n_name = nl[i].n_name;
341 nfail = kvm_nlist2(kd, kl);
342 for (i = 0; i < count; i++) {
343 nl[i].n_type = kl[i].n_type;
346 nl[i].n_value = kl[i].n_value;
352 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
355 return (kvm_read2(kd, kva, buf, len));
359 kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
368 * We're using /dev/kmem. Just read straight from the
369 * device and let the active kernel do the address translation.
372 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
373 _kvm_err(kd, 0, "invalid address (0x%jx)",
377 cr = read(kd->vmfd, buf, len);
379 _kvm_syserr(kd, 0, "kvm_read");
381 } else if (cr < (ssize_t)len)
382 _kvm_err(kd, kd->program, "short read");
388 cc = kd->arch->ka_kvatop(kd, kva, &pa);
391 if (cc > (ssize_t)len)
394 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
395 _kvm_syserr(kd, 0, _PATH_MEM);
398 cr = read(kd->pmfd, cp, cc);
400 _kvm_syserr(kd, kd->program, "kvm_read");
404 * If ka_kvatop returns a bogus value or our core file is
405 * truncated, we might wind up seeking beyond the end of the
406 * core file in which case the read will return 0 (EOF).
415 return (cp - (char *)buf);
419 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
426 if (!ISALIVE(kd) && !kd->writable) {
427 _kvm_err(kd, kd->program,
428 "kvm_write not implemented for dead kernels");
434 * Just like kvm_read, only we write.
437 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
438 _kvm_err(kd, 0, "invalid address (%lx)", kva);
441 cc = write(kd->vmfd, buf, len);
443 _kvm_syserr(kd, 0, "kvm_write");
445 } else if ((size_t)cc < len)
446 _kvm_err(kd, kd->program, "short write");
452 cc = kd->arch->ka_kvatop(kd, kva, &pa);
455 if (cc > (ssize_t)len)
458 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
459 _kvm_syserr(kd, 0, _PATH_MEM);
462 cw = write(kd->pmfd, cp, cc);
464 _kvm_syserr(kd, kd->program, "kvm_write");
468 * If ka_kvatop returns a bogus value or our core file is
469 * truncated, we might wind up seeking beyond the end of the
470 * core file in which case the read will return 0 (EOF).
479 return (cp - (const char *)buf);
483 kvm_native(kvm_t *kd)
488 return (kd->arch->ka_native(kd));