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>
66 #include "kvm_private.h"
68 SET_DECLARE(kvm_arch, struct kvm_arch);
70 static char _kd_is_null[] = "";
82 _kvm_read_kernel_ehdr(kvm_t *kd)
86 if (elf_version(EV_CURRENT) == EV_NONE) {
87 _kvm_err(kd, kd->program, "Unsupported libelf");
90 elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
92 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
95 if (elf_kind(elf) != ELF_K_ELF) {
96 _kvm_err(kd, kd->program, "kernel is not an ELF file");
99 if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
100 _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
106 switch (kd->nlehdr.e_ident[EI_DATA]) {
111 _kvm_err(kd, kd->program,
112 "unsupported ELF data encoding for kernel");
118 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
120 struct kvm_arch **parch;
133 else if (strlen(uf) >= MAXPATHLEN) {
134 _kvm_err(kd, kd->program, "exec file name too long");
137 if (flag & ~O_RDWR) {
138 _kvm_err(kd, kd->program, "bad flags arg");
144 if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
145 _kvm_syserr(kd, kd->program, "%s", mf);
148 if (fstat(kd->pmfd, &st) < 0) {
149 _kvm_syserr(kd, kd->program, "%s", mf);
152 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
154 _kvm_syserr(kd, kd->program, "empty file");
157 if (S_ISCHR(st.st_mode)) {
159 * If this is a character special device, then check that
160 * it's /dev/mem. If so, open kmem too. (Maybe we should
161 * make it work for either /dev/mem or /dev/kmem -- in either
162 * case you're working with a live kernel.)
164 if (strcmp(mf, _PATH_DEVNULL) == 0) {
165 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
167 } else if (strcmp(mf, _PATH_MEM) == 0) {
168 if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
170 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
178 * This is either a crash dump or a remote live system with its physical
179 * memory fully accessible via a special device.
180 * Open the namelist fd and determine the architecture.
182 if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
183 _kvm_syserr(kd, kd->program, "%s", uf);
186 if (_kvm_read_kernel_ehdr(kd) < 0)
188 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
189 strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
193 SET_FOREACH(parch, kvm_arch) {
194 if ((*parch)->ka_probe(kd)) {
199 if (kd->arch == NULL) {
200 _kvm_err(kd, kd->program, "unsupported architecture");
205 * Non-native kernels require a symbol resolver.
207 if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
208 _kvm_err(kd, kd->program,
209 "non-native kernel requires a symbol resolver");
214 * Initialize the virtual address translation machinery.
216 if (kd->arch->ka_initvtop(kd) < 0)
221 * Copy out the error if doing sane error semantics.
224 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
230 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
235 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
237 (void)strlcpy(errout, strerror(errno),
241 return (_kvm_open(kd, uf, mf, flag, errout));
245 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
250 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
252 (void)fprintf(stderr, "%s: %s\n",
253 errstr, strerror(errno));
256 kd->program = errstr;
257 return (_kvm_open(kd, uf, mf, flag, NULL));
261 kvm_open2(const char *uf, const char *mf, int flag, char *errout,
262 int (*resolver)(const char *, kvaddr_t *))
266 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
268 (void)strlcpy(errout, strerror(errno),
272 kd->resolve_symbol = resolver;
273 return (_kvm_open(kd, uf, mf, flag, errout));
285 if (kd->vmst != NULL)
286 kd->arch->ka_freevtop(kd);
288 error |= close(kd->pmfd);
290 error |= close(kd->vmfd);
292 error |= close(kd->nlfd);
293 if (kd->procbase != 0)
294 free((void *)kd->procbase);
296 free((void *) kd->argbuf);
298 free((void *) kd->argspc);
300 free((void *)kd->argv);
301 if (kd->pt_map != NULL)
303 if (kd->page_map != NULL)
305 if (kd->sparse_map != MAP_FAILED)
306 munmap(kd->sparse_map, kd->pt_sparse_size);
313 kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl)
317 * If called via the public interface, permit initialization of
318 * further virtualized modules on demand.
320 return (_kvm_nlist(kd, nl, 1));
324 kvm_nlist(kvm_t *kd, struct nlist *nl)
326 struct kvm_nlist *kl;
330 * Avoid reporting truncated addresses by failing for non-native
333 if (!kvm_native(kd)) {
334 _kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore");
338 for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0';
343 kl = calloc(count + 1, sizeof(*kl));
344 for (i = 0; i < count; i++)
345 kl[i].n_name = nl[i].n_name;
346 nfail = kvm_nlist2(kd, kl);
347 for (i = 0; i < count; i++) {
348 nl[i].n_type = kl[i].n_type;
351 nl[i].n_value = kl[i].n_value;
357 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
360 return (kvm_read2(kd, kva, buf, len));
364 kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
373 * We're using /dev/kmem. Just read straight from the
374 * device and let the active kernel do the address translation.
377 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
378 _kvm_err(kd, 0, "invalid address (0x%jx)",
382 cr = read(kd->vmfd, buf, len);
384 _kvm_syserr(kd, 0, "kvm_read");
386 } else if (cr < (ssize_t)len)
387 _kvm_err(kd, kd->program, "short read");
393 cc = kd->arch->ka_kvatop(kd, kva, &pa);
396 if (cc > (ssize_t)len)
399 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
400 _kvm_syserr(kd, 0, _PATH_MEM);
403 cr = read(kd->pmfd, cp, cc);
405 _kvm_syserr(kd, kd->program, "kvm_read");
409 * If ka_kvatop returns a bogus value or our core file is
410 * truncated, we might wind up seeking beyond the end of the
411 * core file in which case the read will return 0 (EOF).
420 return (cp - (char *)buf);
424 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
431 if (!ISALIVE(kd) && !kd->writable) {
432 _kvm_err(kd, kd->program,
433 "kvm_write not implemented for dead kernels");
439 * Just like kvm_read, only we write.
442 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
443 _kvm_err(kd, 0, "invalid address (%lx)", kva);
446 cc = write(kd->vmfd, buf, len);
448 _kvm_syserr(kd, 0, "kvm_write");
450 } else if ((size_t)cc < len)
451 _kvm_err(kd, kd->program, "short write");
457 cc = kd->arch->ka_kvatop(kd, kva, &pa);
460 if (cc > (ssize_t)len)
463 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
464 _kvm_syserr(kd, 0, _PATH_MEM);
467 cw = write(kd->pmfd, cp, cc);
469 _kvm_syserr(kd, kd->program, "kvm_write");
473 * If ka_kvatop returns a bogus value or our core file is
474 * truncated, we might wind up seeking beyond the end of the
475 * core file in which case the read will return 0 (EOF).
484 return (cp - (const char *)buf);
488 kvm_native(kvm_t *kd)
493 return (kd->arch->ka_native(kd));
497 kvm_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *closure)
500 if (kd->arch->ka_walk_pages == NULL)
503 return (kd->arch->ka_walk_pages(kd, cb, closure));