svn commit: r291406 - in head: . gnu/usr.bin/gdb/kgdb lib/libkvm rescue/rescue share/mk sys/sparc64/include
John Baldwin
jhb at FreeBSD.org
Fri Nov 27 18:58:28 UTC 2015
Author: jhb
Date: Fri Nov 27 18:58:26 2015
New Revision: 291406
URL: https://svnweb.freebsd.org/changeset/base/291406
Log:
Add support to libkvm for reading vmcores from other architectures.
- Add a kvaddr_type to represent kernel virtual addresses instead of
unsigned long.
- Add a struct kvm_nlist which is a stripped down version of struct nlist
that uses kvaddr_t for n_value.
- Add a kvm_native() routine that returns true if an open kvm descriptor
is for a native kernel and memory image.
- Add a kvm_open2() function similar to kvm_openfiles(). It drops the
unused 'swapfile' argument and adds a new function pointer argument for
a symbol resolving function. Native kernels still use _fdnlist() from
libc to resolve symbols if a resolver function is not supplied, but cross
kernels require a resolver.
- Add a kvm_nlist2() function similar to kvm_nlist() except that it uses
struct kvm_nlist instead of struct nlist.
- Add a kvm_read2() function similar to kvm_read() except that it uses
kvaddr_t instead of unsigned long for the kernel virtual address.
- Add a new kvm_arch switch of routines needed by a vmcore backend.
Each backend is responsible for implementing kvm_read2() for a given
vmcore format.
- Use libelf to read headers from ELF kernels and cores (except for
powerpc cores).
- Add internal helper routines for the common page offset hash table used
by the minidump backends.
- Port all of the existing kvm backends to implement a kvm_arch switch and
to be cross-friendly by using private constants instead of ones that
vary by platform (e.g. PAGE_SIZE). Static assertions are present when
a given backend is compiled natively to ensure the private constants
match the real ones.
- Enable all of the existing vmcore backends on all platforms. This means
that libkvm on any platform should be able to perform KVA translation
and read data from a vmcore of any platform.
Tested on: amd64, i386, sparc64 (marius)
Differential Revision: https://reviews.freebsd.org/D3341
Added:
head/lib/libkvm/kvm_aarch64.h (contents, props changed)
head/lib/libkvm/kvm_amd64.h (contents, props changed)
head/lib/libkvm/kvm_arm.h (contents, props changed)
head/lib/libkvm/kvm_i386.h (contents, props changed)
head/lib/libkvm/kvm_mips.h (contents, props changed)
head/lib/libkvm/kvm_native.3 (contents, props changed)
head/lib/libkvm/kvm_sparc64.h (contents, props changed)
Deleted:
head/lib/libkvm/kvm_aarch64.c
head/lib/libkvm/kvm_mips.c
Modified:
head/Makefile.inc1
head/gnu/usr.bin/gdb/kgdb/Makefile
head/gnu/usr.bin/gdb/kgdb/main.c
head/gnu/usr.bin/gdb/kgdb/trgt.c
head/lib/libkvm/Makefile
head/lib/libkvm/Makefile.depend
head/lib/libkvm/kvm.3
head/lib/libkvm/kvm.c
head/lib/libkvm/kvm.h
head/lib/libkvm/kvm_amd64.c
head/lib/libkvm/kvm_arm.c
head/lib/libkvm/kvm_cptime.c
head/lib/libkvm/kvm_getloadavg.c
head/lib/libkvm/kvm_getswapinfo.c
head/lib/libkvm/kvm_i386.c
head/lib/libkvm/kvm_minidump_aarch64.c
head/lib/libkvm/kvm_minidump_amd64.c
head/lib/libkvm/kvm_minidump_arm.c
head/lib/libkvm/kvm_minidump_i386.c
head/lib/libkvm/kvm_minidump_mips.c
head/lib/libkvm/kvm_nlist.3
head/lib/libkvm/kvm_open.3
head/lib/libkvm/kvm_pcpu.c
head/lib/libkvm/kvm_powerpc.c
head/lib/libkvm/kvm_powerpc64.c
head/lib/libkvm/kvm_private.h
head/lib/libkvm/kvm_proc.c
head/lib/libkvm/kvm_read.3
head/lib/libkvm/kvm_sparc64.c
head/lib/libkvm/kvm_vnet.c
head/rescue/rescue/Makefile
head/share/mk/src.libnames.mk
head/sys/sparc64/include/kerneldump.h
Modified: head/Makefile.inc1
==============================================================================
--- head/Makefile.inc1 Fri Nov 27 18:22:04 2015 (r291405)
+++ head/Makefile.inc1 Fri Nov 27 18:58:26 2015 (r291406)
@@ -1819,6 +1819,7 @@ _prebuild_libs+= lib/libc++
.endif
lib/libgeom__L: lib/libexpat__L
+lib/libkvm__L: lib/libelf__L
.if ${MK_LIBTHR} != "no"
_lib_libthr= lib/libthr
Modified: head/gnu/usr.bin/gdb/kgdb/Makefile
==============================================================================
--- head/gnu/usr.bin/gdb/kgdb/Makefile Fri Nov 27 18:22:04 2015 (r291405)
+++ head/gnu/usr.bin/gdb/kgdb/Makefile Fri Nov 27 18:58:26 2015 (r291406)
@@ -9,11 +9,7 @@ BULIBS= ${OBJ_BU}/libbfd/libbfd.a ${OBJ_
GDBLIBS= ${OBJ_GDB}/libgdb/libgdb.a
DPADD= ${GDBLIBS} ${BULIBS} ${LIBKVM}
-LDADD= ${GDBLIBS} ${BULIBS} -lkvm${GDB_SUFFIX}
+LDADD= ${GDBLIBS} ${BULIBS} -lkvm
LIBADD+= m readline ncursesw gnuregex
-.if defined(GDB_CROSS_DEBUGGER)
-CFLAGS+= -Wl,-export-dynamic
-.endif
-
.include <bsd.prog.mk>
Modified: head/gnu/usr.bin/gdb/kgdb/main.c
==============================================================================
--- head/gnu/usr.bin/gdb/kgdb/main.c Fri Nov 27 18:22:04 2015 (r291405)
+++ head/gnu/usr.bin/gdb/kgdb/main.c Fri Nov 27 18:58:26 2015 (r291406)
@@ -41,9 +41,6 @@ __FBSDID("$FreeBSD$");
#include <kvm.h>
#include <limits.h>
#include <paths.h>
-#ifdef CROSS_DEBUGGER
-#include <proc_service.h>
-#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -81,24 +78,6 @@ static struct ui_file *parse_gdberr;
static void (*kgdb_new_objfile_chain)(struct objfile * objfile);
-#ifdef CROSS_DEBUGGER
-ps_err_e
-ps_pglobal_lookup(struct ps_prochandle *ph, const char *obj, const char *name,
- psaddr_t *sym_addr)
-{
- struct minimal_symbol *ms;
- CORE_ADDR addr;
-
- ms = lookup_minimal_symbol (name, NULL, NULL);
- if (ms == NULL)
- return PS_NOSYM;
-
- addr = SYMBOL_VALUE_ADDRESS (ms);
- store_typed_address(sym_addr, builtin_type_void_data_ptr, addr);
- return PS_OK;
-}
-#endif
-
static void
usage(void)
{
Modified: head/gnu/usr.bin/gdb/kgdb/trgt.c
==============================================================================
--- head/gnu/usr.bin/gdb/kgdb/trgt.c Fri Nov 27 18:22:04 2015 (r291405)
+++ head/gnu/usr.bin/gdb/kgdb/trgt.c Fri Nov 27 18:58:26 2015 (r291406)
@@ -78,6 +78,19 @@ static char kvm_err[_POSIX2_LINE_MAX];
#define KERNOFF (kgdb_kernbase ())
#define PINKERNEL(x) ((x) >= KERNOFF)
+static int
+kgdb_resolve_symbol(const char *name, kvaddr_t *kva)
+{
+ struct minimal_symbol *ms;
+
+ ms = lookup_minimal_symbol (name, NULL, NULL);
+ if (ms == NULL)
+ return (1);
+
+ *kva = SYMBOL_VALUE_ADDRESS (ms);
+ return (0);;
+}
+
static CORE_ADDR
kgdb_kernbase (void)
{
@@ -120,8 +133,8 @@ kgdb_trgt_open(char *filename, int from_
old_chain = make_cleanup (xfree, filename);
- nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL,
- write_files ? O_RDWR : O_RDONLY, kvm_err);
+ nkvm = kvm_open2(bfd_get_filename(exec_bfd), filename,
+ write_files ? O_RDWR : O_RDONLY, kvm_err, kgdb_resolve_symbol);
if (nkvm == NULL)
error ("Failed to open vmcore: %s", kvm_err);
@@ -254,7 +267,7 @@ kgdb_trgt_xfer_memory(CORE_ADDR memaddr,
if (len == 0)
return (0);
if (!write)
- return (kvm_read(kvm, memaddr, myaddr, len));
+ return (kvm_read2(kvm, memaddr, myaddr, len));
else
return (kvm_write(kvm, memaddr, myaddr, len));
}
Modified: head/lib/libkvm/Makefile
==============================================================================
--- head/lib/libkvm/Makefile Fri Nov 27 18:22:04 2015 (r291405)
+++ head/lib/libkvm/Makefile Fri Nov 27 18:58:26 2015 (r291406)
@@ -1,50 +1,38 @@
# @(#)Makefile 8.1 (Berkeley) 6/4/93
# $FreeBSD$
-.if defined(TARGET_ARCH) && !defined(COMPAT_32BIT)
-KVM_XARCH=${TARGET_ARCH}
-KVM_XCPUARCH=${KVM_XARCH:C/mips(n32|64)?(el)?/mips/:C/arm(v6)?(eb|hf)?/arm/:C/powerpc64/powerpc/}
-.else
-KVM_XARCH=${MACHINE_ARCH}
-KVM_XCPUARCH=${MACHINE_CPUARCH}
-.endif
-
-.if ${KVM_XARCH} != ${MACHINE_ARCH}
-LIB= kvm-${KVM_XARCH}
-CFLAGS+=-DCROSS_LIBKVM
-.else
LIB= kvm
-.endif
SHLIBDIR?= /lib
SHLIB_MAJOR= 6
CFLAGS+=-DLIBC_SCCS -I${.CURDIR}
-.if exists(${.CURDIR}/kvm_${KVM_XARCH}.c)
-KVM_ARCH=${KVM_XARCH}
-.else
-KVM_ARCH=${KVM_XCPUARCH}
-.endif
-
WARNS?= 3
-SRCS= kvm.c kvm_${KVM_ARCH}.c kvm_cptime.c kvm_file.c kvm_getloadavg.c \
- kvm_getswapinfo.c kvm_pcpu.c kvm_proc.c kvm_vnet.c
-.if exists(${.CURDIR}/kvm_minidump_${KVM_ARCH}.c)
-SRCS+= kvm_minidump_${KVM_ARCH}.c
-.endif
+SRCS= kvm.c kvm_cptime.c kvm_file.c kvm_getloadavg.c \
+ kvm_getswapinfo.c kvm_pcpu.c kvm_proc.c kvm_vnet.c \
+ kvm_minidump_aarch64.c \
+ kvm_amd64.c kvm_minidump_amd64.c \
+ kvm_arm.c kvm_minidump_arm.c \
+ kvm_i386.c kvm_minidump_i386.c \
+ kvm_minidump_mips.c \
+ kvm_powerpc.c kvm_powerpc64.c \
+ kvm_sparc64.c
INCS= kvm.h
+LIBADD= elf
+
MAN= kvm.3 kvm_getcptime.3 kvm_geterr.3 kvm_getfiles.3 kvm_getloadavg.3 \
- kvm_getpcpu.3 kvm_getprocs.3 kvm_getswapinfo.3 kvm_nlist.3 kvm_open.3 \
- kvm_read.3
+ kvm_getpcpu.3 kvm_getprocs.3 kvm_getswapinfo.3 kvm_native.3 \
+ kvm_nlist.3 kvm_open.3 kvm_read.3
MLINKS+=kvm_getpcpu.3 kvm_getmaxcpu.3 \
kvm_getpcpu.3 kvm_dpcpu_setcpu.3 \
kvm_getpcpu.3 kvm_read_zpcpu.3 \
kvm_getpcpu.3 kvm_counter_u64_fetch.3
MLINKS+=kvm_getprocs.3 kvm_getargv.3 kvm_getprocs.3 kvm_getenvv.3
-MLINKS+=kvm_open.3 kvm_close.3 kvm_open.3 kvm_openfiles.3
-MLINKS+=kvm_read.3 kvm_write.3
+MLINKS+=kvm_nlist.3 kvm_nlist2.3
+MLINKS+=kvm_open.3 kvm_close.3 kvm_open.3 kvm_open2.3 kvm_open.3 kvm_openfiles.3
+MLINKS+=kvm_read.3 kvm_read2.3 kvm_read.3 kvm_write.3
.include <bsd.lib.mk>
Modified: head/lib/libkvm/Makefile.depend
==============================================================================
--- head/lib/libkvm/Makefile.depend Fri Nov 27 18:22:04 2015 (r291405)
+++ head/lib/libkvm/Makefile.depend Fri Nov 27 18:58:26 2015 (r291406)
@@ -9,6 +9,7 @@ DIRDEPS = \
lib/${CSU_DIR} \
lib/libc \
lib/libcompiler_rt \
+ lib/libelf \
.include <dirdeps.mk>
Modified: head/lib/libkvm/kvm.3
==============================================================================
--- head/lib/libkvm/kvm.3 Fri Nov 27 18:22:04 2015 (r291405)
+++ head/lib/libkvm/kvm.3 Fri Nov 27 18:58:26 2015 (r291406)
@@ -32,7 +32,7 @@
.\" @(#)kvm.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
-.Dd April 25, 2010
+.Dd November 27, 2015
.Dt KVM 3
.Os
.Sh NAME
@@ -101,6 +101,44 @@ routine,
to return (not print out) the error message
corresponding to the most recent error condition on the
given descriptor.
+.Sh CROSS DEBUGGING
+The
+.Nm
+library supports inspection of crash dumps from non-native kernels.
+Only a limited subset of the kvm interface is supported for these dumps.
+To inspect a crash dump of a non-native kernel,
+the caller must provide a
+.Fa resolver
+function when opening a descriptor via
+.Fn kvm_open2 .
+In addition,
+the kvm interface defines an integer type
+.Pq Vt kvaddr_t
+that is large enough to hold all valid addresses of all supported
+architectures.
+The interface also defines a new namelist structure type
+.Pq Vt "struct kvm_nlist"
+for use with
+.Fn kvm_nlist2 .
+To avoid address truncation issues,
+the caller should use
+.Fn kvm_nlist2
+and
+.Fn kvm_read2
+in place of
+.Fn kvm_nlist
+and
+.Fn kvm_read ,
+respectively.
+Finally, only a limited subset of operations are supported for non-native
+crash dumps:
+.Fn kvm_close ,
+.Fn kvm_geterr
+.Fn kvm_open2 ,
+.Fn kvm_native ,
+.Fn kvm_nlist2 ,
+and
+.Fn kvm_read2 .
.Sh SEE ALSO
.Xr kvm_close 3 ,
.Xr kvm_getargv 3 ,
@@ -110,10 +148,14 @@ given descriptor.
.Xr kvm_getloadavg 3 ,
.Xr kvm_getprocs 3 ,
.Xr kvm_getswapinfo 3 ,
+.Xr kvm_native 3 ,
.Xr kvm_nlist 3 ,
+.Xr kvm_nlist2 3 ,
.Xr kvm_open 3 ,
+.Xr kvm_open2 3 ,
.Xr kvm_openfiles 3 ,
.Xr kvm_read 3 ,
+.Xr kvm_read2 3 ,
.Xr kvm_write 3 ,
.Xr sysctl 3 ,
.Xr kmem 4 ,
Modified: head/lib/libkvm/kvm.c
==============================================================================
--- head/lib/libkvm/kvm.c Fri Nov 27 18:22:04 2015 (r291405)
+++ head/lib/libkvm/kvm.c Fri Nov 27 18:58:26 2015 (r291406)
@@ -41,62 +41,63 @@ static char sccsid[] = "@(#)kvm.c 8.2 (B
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
+#include <sys/fnv_hash.h>
#define _WANT_VNET
#include <sys/user.h>
-#include <sys/proc.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/sysctl.h>
#include <sys/linker.h>
#include <sys/pcpu.h>
+#include <sys/stat.h>
#include <net/vnet.h>
-#include <vm/vm.h>
-#include <vm/vm_param.h>
-
-#include <machine/vmparam.h>
-
-#include <ctype.h>
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
-#include <nlist.h>
#include <paths.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <strings.h>
#include <unistd.h>
#include "kvm_private.h"
-#ifndef CROSS_LIBKVM
+SET_DECLARE(kvm_arch, struct kvm_arch);
/* from src/lib/libc/gen/nlist.c */
int __fdnlist(int, struct nlist *);
-#define kvm_fdnlist __fdnlist
-
-#else
-
-#include <proc_service.h>
-
static int
-kvm_fdnlist(int fd, struct nlist *list)
+kvm_fdnlist(kvm_t *kd, struct kvm_nlist *list)
{
- psaddr_t addr;
- ps_err_e pserr;
- int nfail;
+ kvaddr_t addr;
+ int error, nfail;
+
+ if (kd->resolve_symbol == NULL) {
+ struct nlist *nl;
+ int count, i;
+
+ for (count = 0; list[count].n_name != NULL &&
+ list[count].n_name[0] != '\0'; count++)
+ ;
+ nl = calloc(count + 1, sizeof(*nl));
+ for (i = 0; i < count; i++)
+ nl[i].n_name = list[i].n_name;
+ nfail = __fdnlist(kd->nlfd, nl);
+ for (i = 0; i < count; i++) {
+ list[i].n_type = nl[i].n_type;
+ list[i].n_value = nl[i].n_value;
+ }
+ free(nl);
+ return (nfail);
+ }
nfail = 0;
while (list->n_name != NULL && list->n_name[0] != '\0') {
- list->n_other = 0;
- list->n_desc = 0;
- pserr = ps_pglobal_lookup(NULL, NULL, list->n_name, &addr);
- if (pserr != PS_OK) {
+ error = kd->resolve_symbol(list->n_name, &addr);
+ if (error != 0) {
nfail++;
list->n_value = 0;
list->n_type = 0;
@@ -109,8 +110,6 @@ kvm_fdnlist(int fd, struct nlist *list)
return (nfail);
}
-#endif /* CROSS_LIBKVM */
-
char *
kvm_geterr(kvm_t *kd)
{
@@ -175,9 +174,206 @@ _kvm_malloc(kvm_t *kd, size_t n)
return (p);
}
+static int
+_kvm_read_kernel_ehdr(kvm_t *kd)
+{
+ Elf *elf;
+
+ if (elf_version(EV_CURRENT) == EV_NONE) {
+ _kvm_err(kd, kd->program, "Unsupported libelf");
+ return (-1);
+ }
+ elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
+ if (elf == NULL) {
+ _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
+ return (-1);
+ }
+ if (elf_kind(elf) != ELF_K_ELF) {
+ _kvm_err(kd, kd->program, "kernel is not an ELF file");
+ return (-1);
+ }
+ if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
+ _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
+ elf_end(elf);
+ return (-1);
+ }
+ elf_end(elf);
+
+ switch (kd->nlehdr.e_ident[EI_DATA]) {
+ case ELFDATA2LSB:
+ case ELFDATA2MSB:
+ return (0);
+ default:
+ _kvm_err(kd, kd->program,
+ "unsupported ELF data encoding for kernel");
+ return (-1);
+ }
+}
+
+int
+_kvm_probe_elf_kernel(kvm_t *kd, int class, int machine)
+{
+
+ return (kd->nlehdr.e_ident[EI_CLASS] == class &&
+ kd->nlehdr.e_type == ET_EXEC &&
+ kd->nlehdr.e_machine == machine);
+}
+
+int
+_kvm_is_minidump(kvm_t *kd)
+{
+ char minihdr[8];
+
+ if (kd->rawdump)
+ return (0);
+ if (pread(kd->pmfd, &minihdr, 8, 0) == 8 &&
+ memcmp(&minihdr, "minidump", 8) == 0)
+ return (1);
+ return (0);
+}
+
+/*
+ * The powerpc backend has a hack to strip a leading kerneldump
+ * header from the core before treating it as an ELF header.
+ *
+ * We can add that here if we can get a change to libelf to support
+ * an inital offset into the file. Alternatively we could patch
+ * savecore to extract cores from a regular file instead.
+ */
+int
+_kvm_read_core_phdrs(kvm_t *kd, size_t *phnump, GElf_Phdr **phdrp)
+{
+ GElf_Ehdr ehdr;
+ GElf_Phdr *phdr;
+ Elf *elf;
+ size_t i, phnum;
+
+ elf = elf_begin(kd->pmfd, ELF_C_READ, NULL);
+ if (elf == NULL) {
+ _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
+ return (-1);
+ }
+ if (elf_kind(elf) != ELF_K_ELF) {
+ _kvm_err(kd, kd->program, "invalid core");
+ goto bad;
+ }
+ if (gelf_getclass(elf) != kd->nlehdr.e_ident[EI_CLASS]) {
+ _kvm_err(kd, kd->program, "invalid core");
+ goto bad;
+ }
+ if (gelf_getehdr(elf, &ehdr) == NULL) {
+ _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
+ goto bad;
+ }
+ if (ehdr.e_type != ET_CORE) {
+ _kvm_err(kd, kd->program, "invalid core");
+ goto bad;
+ }
+ if (ehdr.e_machine != kd->nlehdr.e_machine) {
+ _kvm_err(kd, kd->program, "invalid core");
+ goto bad;
+ }
+
+ if (elf_getphdrnum(elf, &phnum) == -1) {
+ _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
+ goto bad;
+ }
+
+ phdr = calloc(phnum, sizeof(*phdr));
+ if (phdr == NULL) {
+ _kvm_err(kd, kd->program, "failed to allocate phdrs");
+ goto bad;
+ }
+
+ for (i = 0; i < phnum; i++) {
+ if (gelf_getphdr(elf, i, &phdr[i]) == NULL) {
+ _kvm_err(kd, kd->program, "%s", elf_errmsg(0));
+ goto bad;
+ }
+ }
+ elf_end(elf);
+ *phnump = phnum;
+ *phdrp = phdr;
+ return (0);
+
+bad:
+ elf_end(elf);
+ return (-1);
+}
+
+static void
+_kvm_hpt_insert(struct hpt *hpt, uint64_t pa, off_t off)
+{
+ struct hpte *hpte;
+ uint32_t fnv = FNV1_32_INIT;
+
+ fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
+ fnv &= (HPT_SIZE - 1);
+ hpte = malloc(sizeof(*hpte));
+ hpte->pa = pa;
+ hpte->off = off;
+ hpte->next = hpt->hpt_head[fnv];
+ hpt->hpt_head[fnv] = hpte;
+}
+
+void
+_kvm_hpt_init(kvm_t *kd, struct hpt *hpt, void *base, size_t len, off_t off,
+ int page_size, int word_size)
+{
+ uint64_t bits, idx, pa;
+ uint64_t *base64;
+ uint32_t *base32;
+
+ base64 = base;
+ base32 = base;
+ for (idx = 0; idx < len / word_size; idx++) {
+ if (word_size == sizeof(uint64_t))
+ bits = _kvm64toh(kd, base64[idx]);
+ else
+ bits = _kvm32toh(kd, base32[idx]);
+ pa = idx * word_size * NBBY * page_size;
+ for (; bits != 0; bits >>= 1, pa += page_size) {
+ if ((bits & 1) == 0)
+ continue;
+ _kvm_hpt_insert(hpt, pa, off);
+ off += page_size;
+ }
+ }
+}
+
+off_t
+_kvm_hpt_find(struct hpt *hpt, uint64_t pa)
+{
+ struct hpte *hpte;
+ uint32_t fnv = FNV1_32_INIT;
+
+ fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
+ fnv &= (HPT_SIZE - 1);
+ for (hpte = hpt->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) {
+ if (pa == hpte->pa)
+ return (hpte->off);
+ }
+ return (-1);
+}
+
+void
+_kvm_hpt_free(struct hpt *hpt)
+{
+ struct hpte *hpte, *next;
+ int i;
+
+ for (i = 0; i < HPT_SIZE; i++) {
+ for (hpte = hpt->hpt_head[i]; hpte != NULL; hpte = next) {
+ next = hpte->next;
+ free(hpte);
+ }
+ }
+}
+
static kvm_t *
_kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
{
+ struct kvm_arch **parch;
struct stat st;
kd->vmfd = -1;
@@ -235,16 +431,40 @@ _kvm_open(kvm_t *kd, const char *uf, con
}
/*
* This is a crash dump.
- * Initialize the virtual address translation machinery,
- * but first setup the namelist fd.
+ * Open the namelist fd and determine the architecture.
*/
if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
_kvm_syserr(kd, kd->program, "%s", uf);
goto failed;
}
+ if (_kvm_read_kernel_ehdr(kd) < 0)
+ goto failed;
if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
kd->rawdump = 1;
- if (_kvm_initvtop(kd) < 0)
+ SET_FOREACH(parch, kvm_arch) {
+ if ((*parch)->ka_probe(kd)) {
+ kd->arch = *parch;
+ break;
+ }
+ }
+ if (kd->arch == NULL) {
+ _kvm_err(kd, kd->program, "unsupported architecture");
+ goto failed;
+ }
+
+ /*
+ * Non-native kernels require a symbol resolver.
+ */
+ if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
+ _kvm_err(kd, kd->program,
+ "non-native kernel requires a symbol resolver");
+ goto failed;
+ }
+
+ /*
+ * Initialize the virtual address translation machinery.
+ */
+ if (kd->arch->ka_initvtop(kd) < 0)
goto failed;
return (kd);
failed:
@@ -267,7 +487,6 @@ kvm_openfiles(const char *uf, const char
(void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
return (0);
}
- kd->program = 0;
return (_kvm_open(kd, uf, mf, flag, errout));
}
@@ -287,19 +506,33 @@ kvm_open(const char *uf, const char *mf,
return (_kvm_open(kd, uf, mf, flag, NULL));
}
+kvm_t *
+kvm_open2(const char *uf, const char *mf, int flag, char *errout,
+ int (*resolver)(const char *, kvaddr_t *))
+{
+ kvm_t *kd;
+
+ if ((kd = calloc(1, sizeof(*kd))) == NULL) {
+ (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
+ return (0);
+ }
+ kd->resolve_symbol = resolver;
+ return (_kvm_open(kd, uf, mf, flag, errout));
+}
+
int
kvm_close(kvm_t *kd)
{
int error = 0;
+ if (kd->vmst != NULL)
+ kd->arch->ka_freevtop(kd);
if (kd->pmfd >= 0)
error |= close(kd->pmfd);
if (kd->vmfd >= 0)
error |= close(kd->vmfd);
if (kd->nlfd >= 0)
error |= close(kd->nlfd);
- if (kd->vmst)
- _kvm_freevtop(kd);
if (kd->procbase != 0)
free((void *)kd->procbase);
if (kd->argbuf != 0)
@@ -318,10 +551,10 @@ kvm_close(kvm_t *kd)
* symbol names, try again, and merge back what we could resolve.
*/
static int
-kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
- uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
+kvm_fdnlist_prefix(kvm_t *kd, struct kvm_nlist *nl, int missing,
+ const char *prefix, kvaddr_t (*validate_fn)(kvm_t *, kvaddr_t))
{
- struct nlist *n, *np, *p;
+ struct kvm_nlist *n, *np, *p;
char *cp, *ce;
const char *ccp;
size_t len;
@@ -337,14 +570,14 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nli
for (p = nl; p->n_name && p->n_name[0]; ++p) {
if (p->n_type != N_UNDF)
continue;
- len += sizeof(struct nlist) + strlen(prefix) +
+ len += sizeof(struct kvm_nlist) + strlen(prefix) +
2 * (strlen(p->n_name) + 1);
unresolved++;
}
if (unresolved == 0)
return (unresolved);
/* Add space for the terminating nlist entry. */
- len += sizeof(struct nlist);
+ len += sizeof(struct kvm_nlist);
unresolved++;
/* Alloc one chunk for (nlist, [names]) and setup pointers. */
@@ -353,7 +586,7 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nli
if (n == NULL)
return (missing);
cp = ce = (char *)np;
- cp += unresolved * sizeof(struct nlist);
+ cp += unresolved * sizeof(struct kvm_nlist);
ce += len;
/* Generate shortened nlist with special prefix. */
@@ -361,7 +594,7 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nli
for (p = nl; p->n_name && p->n_name[0]; ++p) {
if (p->n_type != N_UNDF)
continue;
- bcopy(p, np, sizeof(struct nlist));
+ *np = *p;
/* Save the new\0orig. name so we can later match it again. */
slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
(prefix[0] != '\0' && p->n_name[0] == '_') ?
@@ -376,7 +609,7 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nli
/* Do lookup on the reduced list. */
np = n;
- unresolved = kvm_fdnlist(kd->nlfd, np);
+ unresolved = kvm_fdnlist(kd, np);
/* Check if we could resolve further symbols and update the list. */
if (unresolved >= 0 && unresolved < missing) {
@@ -398,8 +631,6 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nli
continue;
/* Update nlist with new, translated results. */
p->n_type = np->n_type;
- p->n_other = np->n_other;
- p->n_desc = np->n_desc;
if (validate_fn)
p->n_value = (*validate_fn)(kd, np->n_value);
else
@@ -418,9 +649,9 @@ kvm_fdnlist_prefix(kvm_t *kd, struct nli
}
int
-_kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
+_kvm_nlist(kvm_t *kd, struct kvm_nlist *nl, int initialize)
{
- struct nlist *p;
+ struct kvm_nlist *p;
int nvalid;
struct kld_sym_lookup lookup;
int error;
@@ -433,7 +664,7 @@ _kvm_nlist(kvm_t *kd, struct nlist *nl,
* slow library call.
*/
if (!ISALIVE(kd)) {
- error = kvm_fdnlist(kd->nlfd, nl);
+ error = kvm_fdnlist(kd, nl);
if (error <= 0) /* Hard error or success. */
return (error);
@@ -475,8 +706,6 @@ again:
if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
p->n_type = N_TEXT;
- p->n_other = 0;
- p->n_desc = 0;
if (_kvm_vnet_initialized(kd, initialize) &&
strcmp(prefix, VNET_SYMPREFIX) == 0)
p->n_value =
@@ -519,7 +748,7 @@ again:
}
int
-kvm_nlist(kvm_t *kd, struct nlist *nl)
+kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl)
{
/*
@@ -529,9 +758,49 @@ kvm_nlist(kvm_t *kd, struct nlist *nl)
return (_kvm_nlist(kd, nl, 1));
}
+int
+kvm_nlist(kvm_t *kd, struct nlist *nl)
+{
+ struct kvm_nlist *kl;
+ int count, i, nfail;
+
+ /*
+ * Avoid reporting truncated addresses by failing for non-native
+ * cores.
+ */
+ if (!kvm_native(kd)) {
+ _kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore");
+ return (-1);
+ }
+
+ for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0';
+ count++)
+ ;
+ if (count == 0)
+ return (0);
+ kl = calloc(count + 1, sizeof(*kl));
+ for (i = 0; i < count; i++)
+ kl[i].n_name = nl[i].n_name;
+ nfail = kvm_nlist2(kd, kl);
+ for (i = 0; i < count; i++) {
+ nl[i].n_type = kl[i].n_type;
+ nl[i].n_other = 0;
+ nl[i].n_desc = 0;
+ nl[i].n_value = kl[i].n_value;
+ }
+ return (nfail);
+}
+
ssize_t
kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
{
+
+ return (kvm_read2(kd, kva, buf, len));
+}
+
+ssize_t
+kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
+{
int cc;
ssize_t cr;
off_t pa;
@@ -544,7 +813,8 @@ kvm_read(kvm_t *kd, u_long kva, void *bu
*/
errno = 0;
if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
- _kvm_err(kd, 0, "invalid address (%lx)", kva);
+ _kvm_err(kd, 0, "invalid address (0x%jx)",
+ (uintmax_t)kva);
return (-1);
}
cr = read(kd->vmfd, buf, len);
@@ -558,7 +828,7 @@ kvm_read(kvm_t *kd, u_long kva, void *bu
cp = buf;
while (len > 0) {
- cc = _kvm_kvatop(kd, kva, &pa);
+ cc = kd->arch->ka_kvatop(kd, kva, &pa);
if (cc == 0)
return (-1);
if (cc > (ssize_t)len)
@@ -574,7 +844,7 @@ kvm_read(kvm_t *kd, u_long kva, void *bu
break;
}
/*
- * If kvm_kvatop returns a bogus value or our core file is
+ * If ka_kvatop returns a bogus value or our core file is
* truncated, we might wind up seeking beyond the end of the
* core file in which case the read will return 0 (EOF).
*/
@@ -616,3 +886,12 @@ kvm_write(kvm_t *kd, u_long kva, const v
}
/* NOTREACHED */
}
+
+int
+kvm_native(kvm_t *kd)
+{
+
+ if (ISALIVE(kd))
+ return (1);
+ return (kd->arch->ka_native(kd));
+}
Modified: head/lib/libkvm/kvm.h
==============================================================================
--- head/lib/libkvm/kvm.h Fri Nov 27 18:22:04 2015 (r291405)
+++ head/lib/libkvm/kvm.h Fri Nov 27 18:58:26 2015 (r291406)
@@ -51,6 +51,14 @@ typedef __ssize_t ssize_t;
#define _SSIZE_T_DECLARED
#endif
+typedef uint64_t kvaddr_t; /* An address in a target image. */
+
+struct kvm_nlist {
+ const char *n_name;
+ unsigned char n_type;
+ kvaddr_t n_value;
+};
+
typedef struct __kvm kvm_t;
struct kinfo_proc;
@@ -83,13 +91,19 @@ uint64_t kvm_counter_u64_fetch(kvm_t *,
struct kinfo_proc *
kvm_getprocs(kvm_t *, int, int, int *);
int kvm_getswapinfo(kvm_t *, struct kvm_swap *, int, int);
+int kvm_native(kvm_t *);
int kvm_nlist(kvm_t *, struct nlist *);
+int kvm_nlist2(kvm_t *, struct kvm_nlist *);
kvm_t *kvm_open
(const char *, const char *, const char *, int, const char *);
kvm_t *kvm_openfiles
(const char *, const char *, const char *, int, char *);
+kvm_t *kvm_open2
+ (const char *, const char *, int, char *,
+ int (*)(const char *, kvaddr_t *));
ssize_t kvm_read(kvm_t *, unsigned long, void *, size_t);
ssize_t kvm_read_zpcpu(kvm_t *, unsigned long, void *, size_t, int);
+ssize_t kvm_read2(kvm_t *, kvaddr_t, void *, size_t);
ssize_t kvm_write(kvm_t *, unsigned long, const void *, size_t);
__END_DECLS
Added: head/lib/libkvm/kvm_aarch64.h
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ head/lib/libkvm/kvm_aarch64.h Fri Nov 27 18:58:26 2015 (r291406)
@@ -0,0 +1,61 @@
+/*-
+ * Copyright (c) 2015 John H. Baldwin <jhb at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef __KVM_AARCH64_H__
+#define __KVM_AARCH64_H__
+
+#ifdef __aarch64__
+#include <machine/pte.h>
+#endif
+
+typedef uint64_t aarch64_physaddr_t;
+typedef uint64_t aarch64_pte_t;
+
+#define AARCH64_PAGE_SHIFT 12
+#define AARCH64_PAGE_SIZE (1 << AARCH64_PAGE_SHIFT)
+#define AARCH64_PAGE_MASK (AARCH64_PAGE_SIZE - 1)
+
+#define AARCH64_ATTR_MASK 0xfff0000000000fff
+
+#define AARCH64_ATTR_DESCR_MASK 3
+
+#define AARCH64_L3_SHIFT 12
+#define AARCH64_L3_PAGE 0x3
+
+#ifdef __aarch64__
+_Static_assert(PAGE_SHIFT == AARCH64_PAGE_SHIFT, "PAGE_SHIFT mismatch");
+_Static_assert(PAGE_SIZE == AARCH64_PAGE_SIZE, "PAGE_SIZE mismatch");
+_Static_assert(PAGE_MASK == AARCH64_PAGE_MASK, "PAGE_MASK mismatch");
+_Static_assert(ATTR_MASK == AARCH64_ATTR_MASK, "ATTR_MASK mismatch");
+_Static_assert(ATTR_DESCR_MASK == AARCH64_ATTR_DESCR_MASK,
+ "ATTR_DESCR_MASK mismatch");
+_Static_assert(L3_SHIFT == AARCH64_L3_SHIFT, "L3_SHIFT mismatch");
+_Static_assert(L3_PAGE == AARCH64_L3_PAGE, "L3_PAGE mismatch");
+#endif
+
+#endif /* !__KVM_AARCH64_H__ */
Modified: head/lib/libkvm/kvm_amd64.c
==============================================================================
--- head/lib/libkvm/kvm_amd64.c Fri Nov 27 18:22:04 2015 (r291405)
+++ head/lib/libkvm/kvm_amd64.c Fri Nov 27 18:58:26 2015 (r291406)
@@ -46,117 +46,76 @@ static char sccsid[] = "@(#)kvm_hp300.c
*/
#include <sys/param.h>
-#include <sys/user.h>
-#include <sys/proc.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
+#include <sys/endian.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <nlist.h>
#include <kvm.h>
-#include <vm/vm.h>
-#include <vm/vm_param.h>
-
-#include <machine/elf.h>
-
#include <limits.h>
#include "kvm_private.h"
+#include "kvm_amd64.h"
-#ifndef btop
-#define btop(x) (amd64_btop(x))
-#define ptob(x) (amd64_ptob(x))
-#endif
-
-/* minidump must be the first item! */
struct vmstate {
- int minidump; /* 1 = minidump mode */
- void *mmapbase;
- size_t mmapsize;
- pml4_entry_t *PML4;
+ size_t phnum;
+ GElf_Phdr *phdr;
+ amd64_pml4e_t *PML4;
};
/*
- * Map the ELF headers into the process' address space. We do this in two
- * steps: first the ELF header itself and using that information the whole
- * set of headers.
- */
-static int
-_kvm_maphdrs(kvm_t *kd, size_t sz)
*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
More information about the svn-src-all
mailing list