diff options
Diffstat (limited to 'lib/libkvm')
-rw-r--r-- | lib/libkvm/Makefile | 21 | ||||
-rw-r--r-- | lib/libkvm/kvm.3 | 120 | ||||
-rw-r--r-- | lib/libkvm/kvm.c | 442 | ||||
-rw-r--r-- | lib/libkvm/kvm.h | 95 | ||||
-rw-r--r-- | lib/libkvm/kvm_alpha.c | 213 | ||||
-rw-r--r-- | lib/libkvm/kvm_amd64.c | 352 | ||||
-rw-r--r-- | lib/libkvm/kvm_arm.c | 247 | ||||
-rw-r--r-- | lib/libkvm/kvm_file.c | 192 | ||||
-rw-r--r-- | lib/libkvm/kvm_geterr.3 | 82 | ||||
-rw-r--r-- | lib/libkvm/kvm_getfiles.3 | 91 | ||||
-rw-r--r-- | lib/libkvm/kvm_getloadavg.3 | 66 | ||||
-rw-r--r-- | lib/libkvm/kvm_getloadavg.c | 106 | ||||
-rw-r--r-- | lib/libkvm/kvm_getprocs.3 | 185 | ||||
-rw-r--r-- | lib/libkvm/kvm_getswapinfo.3 | 111 | ||||
-rw-r--r-- | lib/libkvm/kvm_getswapinfo.c | 188 | ||||
-rw-r--r-- | lib/libkvm/kvm_i386.c | 448 | ||||
-rw-r--r-- | lib/libkvm/kvm_ia64.c | 209 | ||||
-rw-r--r-- | lib/libkvm/kvm_minidump_amd64.c | 255 | ||||
-rw-r--r-- | lib/libkvm/kvm_minidump_i386.c | 294 | ||||
-rw-r--r-- | lib/libkvm/kvm_nlist.3 | 89 | ||||
-rw-r--r-- | lib/libkvm/kvm_open.3 | 209 | ||||
-rw-r--r-- | lib/libkvm/kvm_powerpc.c | 103 | ||||
-rw-r--r-- | lib/libkvm/kvm_private.h | 89 | ||||
-rw-r--r-- | lib/libkvm/kvm_proc.c | 1024 | ||||
-rw-r--r-- | lib/libkvm/kvm_read.3 | 96 | ||||
-rw-r--r-- | lib/libkvm/kvm_sparc.c | 240 | ||||
-rw-r--r-- | lib/libkvm/kvm_sparc64.c | 225 |
27 files changed, 5792 insertions, 0 deletions
diff --git a/lib/libkvm/Makefile b/lib/libkvm/Makefile new file mode 100644 index 0000000..8008c89 --- /dev/null +++ b/lib/libkvm/Makefile @@ -0,0 +1,21 @@ +# @(#)Makefile 8.1 (Berkeley) 6/4/93 +# $FreeBSD$ + +LIB= kvm +SHLIBDIR?= /lib +CFLAGS+=-DLIBC_SCCS -I${.CURDIR} +SRCS= kvm.c kvm_${MACHINE_ARCH}.c kvm_file.c kvm_getloadavg.c \ + kvm_getswapinfo.c kvm_proc.c +.if ${MACHINE_ARCH} == "amd64" || ${MACHINE_ARCH} == "i386" +SRCS+= kvm_minidump_${MACHINE_ARCH}.c +.endif +INCS= kvm.h + +MAN= kvm.3 kvm_geterr.3 kvm_getfiles.3 kvm_getloadavg.3 kvm_getprocs.3 \ + kvm_getswapinfo.3 kvm_nlist.3 kvm_open.3 kvm_read.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 + +.include <bsd.lib.mk> diff --git a/lib/libkvm/kvm.3 b/lib/libkvm/kvm.3 new file mode 100644 index 0000000..1d3c5eb --- /dev/null +++ b/lib/libkvm/kvm.3 @@ -0,0 +1,120 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software developed by the Computer Systems +.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract +.\" BG 91-66 and contributed to Berkeley. +.\" +.\" 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd January 29, 2004 +.Dt KVM 3 +.Os +.Sh NAME +.Nm kvm +.Nd kernel memory interface +.Sh LIBRARY +.Lb libkvm +.Sh DESCRIPTION +The +.Nm +library provides a uniform interface for accessing kernel virtual memory +images, including live systems and crash dumps. +Access to live systems is via +.Xr mem 4 +and +.Xr kmem 4 +while crash dumps can be examined via the core file generated by +.Xr savecore 8 . +The interface behaves identically in both cases. +Memory can be read and written, kernel symbol addresses can be +looked up efficiently, and information about user processes can +be gathered. +.Pp +The +.Fn kvm_open +function is first called to obtain a descriptor for all subsequent calls. +.Sh COMPATIBILITY +The kvm interface was first introduced in SunOS. +A considerable +number of programs have been developed that use this interface, +making backward compatibility highly desirable. +In most respects, the Sun kvm interface is consistent and clean. +Accordingly, the generic portion of the interface (i.e., +.Fn kvm_open , +.Fn kvm_close , +.Fn kvm_read , +.Fn kvm_write , +and +.Fn kvm_nlist ) +has been incorporated into the +.Bx +interface. +Indeed, many kvm +applications (i.e., debuggers and statistical monitors) use only +this subset of the interface. +.Pp +The process interface was not kept. +This is not a portability +issue since any code that manipulates processes is inherently +machine dependent. +.Pp +Finally, the Sun kvm error reporting semantics are poorly defined. +The library can be configured either to print errors to +.Dv stderr +automatically, +or to print no error messages at all. +In the latter case, the nature of the error cannot be determined. +To overcome this, the +.Bx +interface includes a +routine, +.Xr kvm_geterr 3 , +to return (not print out) the error message +corresponding to the most recent error condition on the +given descriptor. +.Sh SEE ALSO +.Xr kvm_close 3 , +.Xr kvm_getargv 3 , +.Xr kvm_getenvv 3 , +.Xr kvm_geterr 3 , +.Xr kvm_getfiles 3 , +.Xr kvm_getloadavg 3 , +.Xr kvm_getprocs 3 , +.Xr kvm_getswapinfo 3 , +.Xr kvm_nlist 3 , +.Xr kvm_open 3 , +.Xr kvm_openfiles 3 , +.Xr kvm_read 3 , +.Xr kvm_write 3 , +.Xr kmem 4 , +.Xr mem 4 diff --git a/lib/libkvm/kvm.c b/lib/libkvm/kvm.c new file mode 100644 index 0000000..1bea13c --- /dev/null +++ b/lib/libkvm/kvm.c @@ -0,0 +1,442 @@ +/*- + * Copyright (c) 1989, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software developed by the Computer Systems + * Engineering group at Lawrence Berkeley Laboratory under DARPA contract + * BG 91-66 and contributed to Berkeley. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#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 <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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "kvm_private.h" + +/* from src/lib/libc/gen/nlist.c */ +int __fdnlist(int, struct nlist *); + +char * +kvm_geterr(kd) + kvm_t *kd; +{ + return (kd->errbuf); +} + +#include <stdarg.h> + +/* + * Report an error using printf style arguments. "program" is kd->program + * on hard errors, and 0 on soft errors, so that under sun error emulation, + * only hard errors are printed out (otherwise, programs like gdb will + * generate tons of error messages when trying to access bogus pointers). + */ +void +_kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + if (program != NULL) { + (void)fprintf(stderr, "%s: ", program); + (void)vfprintf(stderr, fmt, ap); + (void)fputc('\n', stderr); + } else + (void)vsnprintf(kd->errbuf, + sizeof(kd->errbuf), (char *)fmt, ap); + + va_end(ap); +} + +void +_kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) +{ + va_list ap; + int n; + + va_start(ap, fmt); + if (program != NULL) { + (void)fprintf(stderr, "%s: ", program); + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, ": %s\n", strerror(errno)); + } else { + char *cp = kd->errbuf; + + (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap); + n = strlen(cp); + (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s", + strerror(errno)); + } + va_end(ap); +} + +void * +_kvm_malloc(kd, n) + kvm_t *kd; + size_t n; +{ + void *p; + + if ((p = calloc(n, sizeof(char))) == NULL) + _kvm_err(kd, kd->program, "can't allocate %u bytes: %s", + n, strerror(errno)); + return (p); +} + +static kvm_t * +_kvm_open(kd, uf, mf, flag, errout) + kvm_t *kd; + const char *uf; + const char *mf; + int flag; + char *errout; +{ + struct stat st; + + kd->vmfd = -1; + kd->pmfd = -1; + kd->nlfd = -1; + kd->vmst = 0; + kd->procbase = 0; + kd->argspc = 0; + kd->argv = 0; + + if (uf == 0) + uf = getbootfile(); + else if (strlen(uf) >= MAXPATHLEN) { + _kvm_err(kd, kd->program, "exec file name too long"); + goto failed; + } + if (flag & ~O_RDWR) { + _kvm_err(kd, kd->program, "bad flags arg"); + goto failed; + } + if (mf == 0) + mf = _PATH_MEM; + + if ((kd->pmfd = open(mf, flag, 0)) < 0) { + _kvm_syserr(kd, kd->program, "%s", mf); + goto failed; + } + if (fstat(kd->pmfd, &st) < 0) { + _kvm_syserr(kd, kd->program, "%s", mf); + goto failed; + } + if (S_ISREG(st.st_mode) && st.st_size <= 0) { + errno = EINVAL; + _kvm_syserr(kd, kd->program, "empty file"); + goto failed; + } + if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) { + _kvm_syserr(kd, kd->program, "%s", mf); + goto failed; + } + if (S_ISCHR(st.st_mode)) { + /* + * If this is a character special device, then check that + * it's /dev/mem. If so, open kmem too. (Maybe we should + * make it work for either /dev/mem or /dev/kmem -- in either + * case you're working with a live kernel.) + */ + if (strcmp(mf, _PATH_DEVNULL) == 0) { + kd->vmfd = open(_PATH_DEVNULL, O_RDONLY); + return (kd); + } else if (strcmp(mf, _PATH_MEM) == 0) { + if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) { + _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM); + goto failed; + } + if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) { + _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM); + goto failed; + } + return (kd); + } + } + /* + * This is a crash dump. + * Initialize the virtual address translation machinery, + * but first setup the namelist fd. + */ + if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) { + _kvm_syserr(kd, kd->program, "%s", uf); + goto failed; + } + if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) { + _kvm_syserr(kd, kd->program, "%s", uf); + goto failed; + } + if (_kvm_initvtop(kd) < 0) + goto failed; + return (kd); +failed: + /* + * Copy out the error if doing sane error semantics. + */ + if (errout != 0) + strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX); + (void)kvm_close(kd); + return (0); +} + +kvm_t * +kvm_openfiles(uf, mf, sf, flag, errout) + const char *uf; + const char *mf; + const char *sf __unused; + int flag; + char *errout; +{ + kvm_t *kd; + + if ((kd = malloc(sizeof(*kd))) == NULL) { + (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX); + return (0); + } + memset(kd, 0, sizeof(*kd)); + kd->program = 0; + return (_kvm_open(kd, uf, mf, flag, errout)); +} + +kvm_t * +kvm_open(uf, mf, sf, flag, errstr) + const char *uf; + const char *mf; + const char *sf __unused; + int flag; + const char *errstr; +{ + kvm_t *kd; + + if ((kd = malloc(sizeof(*kd))) == NULL) { + if (errstr != NULL) + (void)fprintf(stderr, "%s: %s\n", + errstr, strerror(errno)); + return (0); + } + memset(kd, 0, sizeof(*kd)); + kd->program = errstr; + return (_kvm_open(kd, uf, mf, flag, NULL)); +} + +int +kvm_close(kd) + kvm_t *kd; +{ + int error = 0; + + 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->argv != 0) + free((void *)kd->argv); + free((void *)kd); + + return (0); +} + +int +kvm_nlist(kd, nl) + kvm_t *kd; + struct nlist *nl; +{ + struct nlist *p; + int nvalid; + struct kld_sym_lookup lookup; + + /* + * If we can't use the kld symbol lookup, revert to the + * slow library call. + */ + if (!ISALIVE(kd)) + return (__fdnlist(kd->nlfd, nl)); + + /* + * We can use the kld lookup syscall. Go through each nlist entry + * and look it up with a kldsym(2) syscall. + */ + nvalid = 0; + for (p = nl; p->n_name && p->n_name[0]; ++p) { + lookup.version = sizeof(lookup); + lookup.symname = p->n_name; + lookup.symvalue = 0; + lookup.symsize = 0; + + if (lookup.symname[0] == '_') + lookup.symname++; + + if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) { + p->n_type = N_TEXT; + p->n_other = 0; + p->n_desc = 0; + p->n_value = lookup.symvalue; + ++nvalid; + /* lookup.symsize */ + } + } + /* + * Return the number of entries that weren't found. + */ + return ((p - nl) - nvalid); +} + +ssize_t +kvm_read(kd, kva, buf, len) + kvm_t *kd; + u_long kva; + void *buf; + size_t len; +{ + int cc; + char *cp; + + if (ISALIVE(kd)) { + /* + * We're using /dev/kmem. Just read straight from the + * device and let the active kernel do the address translation. + */ + errno = 0; + if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { + _kvm_err(kd, 0, "invalid address (%x)", kva); + return (-1); + } + cc = read(kd->vmfd, buf, len); + if (cc < 0) { + _kvm_syserr(kd, 0, "kvm_read"); + return (-1); + } else if (cc < len) + _kvm_err(kd, kd->program, "short read"); + return (cc); + } else { + cp = buf; + while (len > 0) { + off_t pa; + + cc = _kvm_kvatop(kd, kva, &pa); + if (cc == 0) + return (-1); + if (cc > len) + cc = len; + errno = 0; + if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) { + _kvm_syserr(kd, 0, _PATH_MEM); + break; + } + cc = read(kd->pmfd, cp, cc); + if (cc < 0) { + _kvm_syserr(kd, kd->program, "kvm_read"); + break; + } + /* + * If kvm_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). + */ + if (cc == 0) + break; + cp += cc; + kva += cc; + len -= cc; + } + return (cp - (char *)buf); + } + /* NOTREACHED */ +} + +ssize_t +kvm_write(kd, kva, buf, len) + kvm_t *kd; + u_long kva; + const void *buf; + size_t len; +{ + int cc; + + if (ISALIVE(kd)) { + /* + * Just like kvm_read, only we write. + */ + errno = 0; + if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { + _kvm_err(kd, 0, "invalid address (%x)", kva); + return (-1); + } + cc = write(kd->vmfd, buf, len); + if (cc < 0) { + _kvm_syserr(kd, 0, "kvm_write"); + return (-1); + } else if (cc < len) + _kvm_err(kd, kd->program, "short write"); + return (cc); + } else { + _kvm_err(kd, kd->program, + "kvm_write not implemented for dead kernels"); + return (-1); + } + /* NOTREACHED */ +} diff --git a/lib/libkvm/kvm.h b/lib/libkvm/kvm.h new file mode 100644 index 0000000..0886c77 --- /dev/null +++ b/lib/libkvm/kvm.h @@ -0,0 +1,95 @@ +/*- + * Copyright (c) 1989, 1993 + * The Regents of the University of California. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)kvm.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD$ + */ + +#ifndef _KVM_H_ +#define _KVM_H_ + +#include <sys/cdefs.h> +#include <sys/_types.h> +#include <nlist.h> + +/* Default version symbol. */ +#define VRS_SYM "_version" +#define VRS_KEY "VERSION" + +#ifndef _SIZE_T_DECLARED +typedef __size_t size_t; +#define _SIZE_T_DECLARED +#endif + +#ifndef _SSIZE_T_DECLARED +typedef __ssize_t ssize_t; +#define _SSIZE_T_DECLARED +#endif + +typedef struct __kvm kvm_t; + +struct kinfo_proc; +struct proc; + +struct kvm_swap { + char ksw_devname[32]; + int ksw_used; + int ksw_total; + int ksw_flags; + int ksw_reserved1; + int ksw_reserved2; +}; + +#define SWIF_DEV_PREFIX 0x0002 + +__BEGIN_DECLS +int kvm_close(kvm_t *); +char **kvm_getargv(kvm_t *, const struct kinfo_proc *, int); +char **kvm_getenvv(kvm_t *, const struct kinfo_proc *, int); +char *kvm_geterr(kvm_t *); +char *kvm_getfiles(kvm_t *, int, int, int *); +int kvm_getloadavg(kvm_t *, double [], int); +struct kinfo_proc * + kvm_getprocs(kvm_t *, int, int, int *); +int kvm_getswapinfo(kvm_t *, struct kvm_swap *, int, int); +int kvm_nlist(kvm_t *, struct 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 *); +ssize_t kvm_read(kvm_t *, unsigned long, void *, size_t); +ssize_t kvm_uread + (kvm_t *, struct kinfo_proc *, unsigned long, char *, size_t); +ssize_t kvm_write(kvm_t *, unsigned long, const void *, size_t); +__END_DECLS + +#endif /* !_KVM_H_ */ diff --git a/lib/libkvm/kvm_alpha.c b/lib/libkvm/kvm_alpha.c new file mode 100644 index 0000000..011c397 --- /dev/null +++ b/lib/libkvm/kvm_alpha.c @@ -0,0 +1,213 @@ +/* $NetBSD: kvm_alpha.c,v 1.7.2.1 1997/11/02 20:34:26 mellon Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/uio.h> +#include <unistd.h> +#include <nlist.h> +#include <kvm.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> + +#include <limits.h> +#include <stdlib.h> +#include <machine/pmap.h> +#include "kvm_private.h" + +static off_t _kvm_pa2off(kvm_t *kd, u_long pa); + +struct vmstate { + u_int64_t lev1map_pa; /* PA of Lev1map */ + u_int64_t page_size; /* Page size */ + u_int64_t nmemsegs; /* Number of RAM segm */ +}; + +void +_kvm_freevtop(kd) + kvm_t *kd; +{ + + /* Not actually used for anything right now, but safe. */ + if (kd->vmst != 0) + free(kd->vmst); +} + +int +_kvm_initvtop(kd) + kvm_t *kd; +{ + struct vmstate *vm; + struct nlist nlist[2]; + u_long pa; + + vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); + if (vm == 0) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + kd->vmst = vm; + vm->page_size = ALPHA_PGBYTES; + + nlist[0].n_name = "_Lev1map"; + nlist[1].n_name = 0; + + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "bad namelist"); + return (-1); + } + + if(!ISALIVE(kd)) { + if (kvm_read(kd, (nlist[0].n_value), &pa, sizeof(pa)) != sizeof(pa)) { + _kvm_err(kd, kd->program, "cannot read Lev1map"); + return (-1); + } + } else + if (kvm_read(kd, (nlist[0].n_value), &pa, sizeof(pa)) != sizeof(pa)) { + _kvm_err(kd, kd->program, "cannot read Lev1map"); + return (-1); + } + vm->lev1map_pa = pa; + return (0); + +} + +int +_kvm_kvatop(kd, va, pa) + kvm_t *kd; + u_long va; + off_t *pa; +{ + u_int64_t lev1map_pa; /* PA of Lev1map */ + u_int64_t page_size; + int rv, page_off; + alpha_pt_entry_t pte; + off_t pteoff; + struct vmstate *vm; + vm = kd->vmst ; + + + if (ISALIVE(kd)) { + _kvm_err(kd, 0, "vatop called in live kernel!"); + return(0); + } + lev1map_pa = vm->lev1map_pa; + page_size = vm->page_size; + + page_off = va & (page_size - 1); + if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) { + /* + * Direct-mapped address: just convert it. + */ + + *pa = ALPHA_K0SEG_TO_PHYS(va); + rv = page_size - page_off; + } else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) { + /* + * Real kernel virtual address: do the translation. + */ +#define PTMASK ((1 << ALPHA_PTSHIFT) - 1) +#define pmap_lev1_index(va) (((va) >> ALPHA_L1SHIFT) & PTMASK) +#define pmap_lev2_index(va) (((va) >> ALPHA_L2SHIFT) & PTMASK) +#define pmap_lev3_index(va) (((va) >> ALPHA_L3SHIFT) & PTMASK) + + /* Find and read the L1 PTE. */ + pteoff = lev1map_pa + + pmap_lev1_index(va) * sizeof(alpha_pt_entry_t); + if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 || + read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) { + _kvm_syserr(kd, 0, "could not read L1 PTE"); + goto lose; + } + + /* Find and read the L2 PTE. */ + if ((pte & ALPHA_PTE_VALID) == 0) { + _kvm_err(kd, 0, "invalid translation (invalid L1 PTE)"); + goto lose; + } + pteoff = ALPHA_PTE_TO_PFN(pte) * page_size + + pmap_lev2_index(va) * sizeof(alpha_pt_entry_t); + if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 || + read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) { + _kvm_syserr(kd, 0, "could not read L2 PTE"); + goto lose; + } + + /* Find and read the L3 PTE. */ + if ((pte & ALPHA_PTE_VALID) == 0) { + _kvm_err(kd, 0, "invalid translation (invalid L2 PTE)"); + goto lose; + } + pteoff = ALPHA_PTE_TO_PFN(pte) * page_size + + pmap_lev3_index(va) * sizeof(alpha_pt_entry_t); + if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 || + read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) { + _kvm_syserr(kd, 0, "could not read L3 PTE"); + goto lose; + } + + /* Fill in the PA. */ + if ((pte & ALPHA_PTE_VALID) == 0) { + _kvm_err(kd, 0, "invalid translation (invalid L3 PTE)"); + goto lose; + } + *pa = ALPHA_PTE_TO_PFN(pte) * page_size + page_off; + rv = page_size - page_off; + } else { + /* + * Bogus address (not in KV space): punt. + */ + + _kvm_err(kd, 0, "invalid kernel virtual address"); +lose: + *pa = -1; + rv = 0; + } + + return (rv); +} + +/* + * Translate a physical address to a file-offset in the crash-dump. + */ +off_t +_kvm_pa2off(kd, pa) + kvm_t *kd; + u_long pa; +{ + return ALPHA_K0SEG_TO_PHYS(pa); +} + diff --git a/lib/libkvm/kvm_amd64.c b/lib/libkvm/kvm_amd64.c new file mode 100644 index 0000000..af5eb28 --- /dev/null +++ b/lib/libkvm/kvm_amd64.c @@ -0,0 +1,352 @@ +/*- + * Copyright (c) 1989, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software developed by the Computer Systems + * Engineering group at Lawrence Berkeley Laboratory under DARPA contract + * BG 91-66 and contributed to Berkeley. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * AMD64 machine dependent routines for kvm. Hopefully, the forthcoming + * vm code will one day obsolete this module. + */ + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <stdlib.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" + +#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; +}; + +/* + * 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. (Taken from kvm_ia64.c) + */ +static int +_kvm_maphdrs(kvm_t *kd, size_t sz) +{ + struct vmstate *vm = kd->vmst; + + /* munmap() previous mmap(). */ + if (vm->mmapbase != NULL) { + munmap(vm->mmapbase, vm->mmapsize); + vm->mmapbase = NULL; + } + + vm->mmapsize = sz; + vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0); + if (vm->mmapbase == MAP_FAILED) { + _kvm_err(kd, kd->program, "cannot mmap corefile"); + return (-1); + } + return (0); +} + +/* + * Translate a physical memory address to a file-offset in the crash-dump. + * (Taken from kvm_ia64.c) + */ +static size_t +_kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs) +{ + Elf_Ehdr *e = kd->vmst->mmapbase; + Elf_Phdr *p = (Elf_Phdr*)((char*)e + e->e_phoff); + int n = e->e_phnum; + + while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) + p++, n--; + if (n == 0) + return (0); + *ofs = (pa - p->p_paddr) + p->p_offset; + return (PAGE_SIZE - ((size_t)pa & PAGE_MASK)); +} + +void +_kvm_freevtop(kvm_t *kd) +{ + struct vmstate *vm = kd->vmst; + + if (kd->vmst->minidump) + return (_kvm_minidump_freevtop(kd)); + if (vm->mmapbase != NULL) + munmap(vm->mmapbase, vm->mmapsize); + if (vm->PML4) + free(vm->PML4); + free(vm); + kd->vmst = NULL; +} + +int +_kvm_initvtop(kvm_t *kd) +{ + struct nlist nlist[2]; + u_long pa; + u_long kernbase; + pml4_entry_t *PML4; + Elf_Ehdr *ehdr; + size_t hdrsz; + char minihdr[8]; + + if (pread(kd->pmfd, &minihdr, 8, 0) == 8) + if (memcmp(&minihdr, "minidump", 8) == 0) + return (_kvm_minidump_initvtop(kd)); + + kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); + if (kd->vmst == 0) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + kd->vmst->PML4 = 0; + + if (_kvm_maphdrs(kd, sizeof(Elf_Ehdr)) == -1) + return (-1); + + ehdr = kd->vmst->mmapbase; + hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum; + if (_kvm_maphdrs(kd, hdrsz) == -1) + return (-1); + + nlist[0].n_name = "kernbase"; + nlist[1].n_name = 0; + + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "bad namelist - no kernbase"); + return (-1); + } + kernbase = nlist[0].n_value; + + nlist[0].n_name = "KPML4phys"; + nlist[1].n_name = 0; + + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "bad namelist - no KPML4phys"); + return (-1); + } + if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, sizeof(pa)) != + sizeof(pa)) { + _kvm_err(kd, kd->program, "cannot read KPML4phys"); + return (-1); + } + PML4 = _kvm_malloc(kd, PAGE_SIZE); + if (kvm_read(kd, pa, PML4, PAGE_SIZE) != PAGE_SIZE) { + _kvm_err(kd, kd->program, "cannot read KPML4phys"); + return (-1); + } + kd->vmst->PML4 = PML4; + return (0); +} + +static int +_kvm_vatop(kvm_t *kd, u_long va, off_t *pa) +{ + struct vmstate *vm; + u_long offset; + u_long pdpe_pa; + u_long pde_pa; + u_long pte_pa; + pml4_entry_t pml4e; + pdp_entry_t pdpe; + pd_entry_t pde; + pt_entry_t pte; + u_long pml4eindex; + u_long pdpeindex; + u_long pdeindex; + u_long pteindex; + int i; + u_long a; + off_t ofs; + size_t s; + + vm = kd->vmst; + offset = va & (PAGE_SIZE - 1); + + /* + * If we are initializing (kernel page table descriptor pointer + * not yet set) then return pa == va to avoid infinite recursion. + */ + if (vm->PML4 == 0) { + s = _kvm_pa2off(kd, va, pa); + if (s == 0) { + _kvm_err(kd, kd->program, + "_kvm_vatop: bootstrap data not in dump"); + goto invalid; + } else + return (PAGE_SIZE - offset); + } + + pml4eindex = (va >> PML4SHIFT) & (NPML4EPG - 1); + pml4e = vm->PML4[pml4eindex]; + if (((u_long)pml4e & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pml4e not valid"); + goto invalid; + } + + pdpeindex = (va >> PDPSHIFT) & (NPDPEPG-1); + pdpe_pa = ((u_long)pml4e & PG_FRAME) + + (pdpeindex * sizeof(pdp_entry_t)); + + s = _kvm_pa2off(kd, pdpe_pa, &ofs); + if (s < sizeof pdpe) { + _kvm_err(kd, kd->program, "_kvm_vatop: pdpe_pa not found"); + goto invalid; + } + if (lseek(kd->pmfd, ofs, 0) == -1) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek pdpe_pa"); + goto invalid; + } + if (read(kd->pmfd, &pdpe, sizeof pdpe) != sizeof pdpe) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: read pdpe"); + goto invalid; + } + if (((u_long)pdpe & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pdpe not valid"); + goto invalid; + } + + pdeindex = (va >> PDRSHIFT) & (NPDEPG-1); + pde_pa = ((u_long)pdpe & PG_FRAME) + (pdeindex * sizeof(pd_entry_t)); + + s = _kvm_pa2off(kd, pde_pa, &ofs); + if (s < sizeof pde) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: pde_pa not found"); + goto invalid; + } + if (lseek(kd->pmfd, ofs, 0) == -1) { + _kvm_err(kd, kd->program, "_kvm_vatop: lseek pde_pa"); + goto invalid; + } + if (read(kd->pmfd, &pde, sizeof pde) != sizeof pde) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: read pde"); + goto invalid; + } + if (((u_long)pde & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid"); + goto invalid; + } + + if ((u_long)pde & PG_PS) { + /* + * No final-level page table; ptd describes one 2MB page. + */ +#define PAGE2M_MASK (NBPDR - 1) +#define PG_FRAME2M (~PAGE2M_MASK) + a = ((u_long)pde & PG_FRAME2M) + (va & PAGE2M_MASK); + s = _kvm_pa2off(kd, a, pa); + if (s == 0) { + _kvm_err(kd, kd->program, + "_kvm_vatop: 2MB page address not in dump"); + goto invalid; + } else + return (NBPDR - (va & PAGE2M_MASK)); + } + + pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1); + pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t)); + + s = _kvm_pa2off(kd, pte_pa, &ofs); + if (s < sizeof pte) { + _kvm_err(kd, kd->program, "_kvm_vatop: pte_pa not found"); + goto invalid; + } + if (lseek(kd->pmfd, ofs, 0) == -1) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek"); + goto invalid; + } + if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: read"); + goto invalid; + } + if (((u_long)pte & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); + goto invalid; + } + + a = ((u_long)pte & PG_FRAME) + offset; + s = _kvm_pa2off(kd, a, pa); + if (s == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: address not in dump"); + goto invalid; + } else + return (PAGE_SIZE - offset); + +invalid: + _kvm_err(kd, 0, "invalid address (0x%lx)", va); + return (0); +} + +int +_kvm_kvatop(kvm_t *kd, u_long va, off_t *pa) +{ + + if (kd->vmst->minidump) + return (_kvm_minidump_kvatop(kd, va, pa)); + if (ISALIVE(kd)) { + _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); + return (0); + } + return (_kvm_vatop(kd, va, pa)); +} diff --git a/lib/libkvm/kvm_arm.c b/lib/libkvm/kvm_arm.c new file mode 100644 index 0000000..0bde0bf --- /dev/null +++ b/lib/libkvm/kvm_arm.c @@ -0,0 +1,247 @@ +/* $NetBSD: kvm_powerpc.c,v 1.4 1998/02/03 06:50:07 mycroft Exp $ */ + +/*- + * Copyright (C) 1996 Wolfgang Solfrank. + * Copyright (C) 1996 TooLs GmbH. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by TooLs GmbH. + * 4. The name of TooLs GmbH may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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. + */ + +/* + * ARM machine dependent routines for kvm. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/elf32.h> +#include <sys/mman.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> +#include <vm/pmap.h> + +#include <machine/pmap.h> + +#include <db.h> +#include <limits.h> +#include <kvm.h> +#include <stdlib.h> + +#include "kvm_private.h" + +struct vmstate { + pd_entry_t *l1pt; + void *mmapbase; + size_t mmapsize; +}; + +static int +_kvm_maphdrs(kvm_t *kd, size_t sz) +{ + struct vmstate *vm = kd->vmst; + + /* munmap() previous mmap(). */ + if (vm->mmapbase != NULL) { + munmap(vm->mmapbase, vm->mmapsize); + vm->mmapbase = NULL; + } + + vm->mmapsize = sz; + vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0); + if (vm->mmapbase == MAP_FAILED) { + _kvm_err(kd, kd->program, "cannot mmap corefile"); + return (-1); + } + + return (0); +} + +/* + * Translate a physical memory address to a file-offset in the crash-dump. + */ +static size_t +_kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz) +{ + Elf32_Ehdr *e = kd->vmst->mmapbase; + Elf32_Phdr *p = (Elf32_Phdr*)((char*)e + e->e_phoff); + int n = e->e_phnum; + + while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) + p++, n--; + if (n == 0) + return (0); + + *ofs = (pa - p->p_paddr) + p->p_offset; + if (pgsz == 0) + return (p->p_memsz - (pa - p->p_paddr)); + return (pgsz - ((size_t)pa & (pgsz - 1))); +} + +void +_kvm_freevtop(kvm_t *kd) +{ + if (kd->vmst != 0) { + if (kd->vmst->mmapbase != NULL) + munmap(kd->vmst->mmapbase, kd->vmst->mmapsize); + free(kd->vmst); + kd->vmst = NULL; + } +} + +int +_kvm_initvtop(kvm_t *kd) +{ + struct vmstate *vm = _kvm_malloc(kd, sizeof(*vm)); + struct nlist nlist[2]; + u_long kernbase, physaddr, pa; + pd_entry_t *l1pt; + Elf32_Ehdr *ehdr; + size_t hdrsz; + + if (vm == 0) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + kd->vmst = vm; + vm->l1pt = NULL; + if (_kvm_maphdrs(kd, sizeof(Elf32_Ehdr)) == -1) + return (-1); + ehdr = kd->vmst->mmapbase; + hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum; + if (_kvm_maphdrs(kd, hdrsz) == -1) + return (-1); + nlist[0].n_name = "kernbase"; + nlist[1].n_name = NULL; + if (kvm_nlist(kd, nlist) != 0) + kernbase = KERNBASE; + else + kernbase = nlist[0].n_value; + + nlist[0].n_name = "physaddr"; + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "couldn't get phys addr"); + return (-1); + } + physaddr = nlist[0].n_value; + nlist[0].n_name = "kernel_l1pa"; + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "bad namelist"); + return (-1); + } + if (kvm_read(kd, (nlist[0].n_value - kernbase + physaddr), &pa, + sizeof(pa)) != sizeof(pa)) { + _kvm_err(kd, kd->program, "cannot read kernel_l1pa"); + return (-1); + } + l1pt = _kvm_malloc(kd, L1_TABLE_SIZE); + if (kvm_read(kd, pa, l1pt, L1_TABLE_SIZE) != L1_TABLE_SIZE) { + _kvm_err(kd, kd->program, "cannot read l1pt"); + free(l1pt); + return (-1); + } + vm->l1pt = l1pt; + return 0; +} + +/* from arm/pmap.c */ +#define L1_IDX(va) (((vm_offset_t)(va)) >> L1_S_SHIFT) +/* from arm/pmap.h */ +#define L1_TYPE_INV 0x00 /* Invalid (fault) */ +#define L1_TYPE_C 0x01 /* Coarse L2 */ +#define L1_TYPE_S 0x02 /* Section */ +#define L1_TYPE_F 0x03 /* Fine L2 */ +#define L1_TYPE_MASK 0x03 /* mask of type bits */ + +#define l1pte_section_p(pde) (((pde) & L1_TYPE_MASK) == L1_TYPE_S) +#define l1pte_valid(pde) ((pde) != 0) +#define l2pte_valid(pte) ((pte) != 0) +#define l2pte_index(v) (((v) & L2_ADDR_BITS) >> L2_S_SHIFT) + + +int +_kvm_kvatop(kvm_t *kd, u_long va, off_t *pa) +{ + u_long offset = va & (PAGE_SIZE - 1); + struct vmstate *vm = kd->vmst; + pd_entry_t pd; + pt_entry_t pte; + u_long pte_pa; + + if (vm->l1pt == NULL) + return (_kvm_pa2off(kd, va, pa, PAGE_SIZE)); + pd = vm->l1pt[L1_IDX(va)]; + if (!l1pte_valid(pd)) + goto invalid; + if (l1pte_section_p(pd)) { + /* 1MB section mapping. */ + *pa = ((u_long)pd & L1_S_ADDR_MASK) + (va & L1_S_OFFSET); + return (_kvm_pa2off(kd, *pa, pa, L1_S_SIZE)); + } + pte_pa = (pd & L1_ADDR_MASK) + l2pte_index(va) * sizeof(pte); + _kvm_pa2off(kd, pte_pa, (off_t *)&pte_pa, L1_S_SIZE); + if (lseek(kd->pmfd, pte_pa, 0) == -1) { + _kvm_syserr(kd, kd->program, "_kvm_kvatop: lseek"); + goto invalid; + } + if (read(kd->pmfd, &pte, sizeof(pte)) != sizeof (pte)) { + _kvm_syserr(kd, kd->program, "_kvm_kvatop: read"); + goto invalid; + } + if (!l2pte_valid(pte)) { + goto invalid; + } + if ((pte & L2_TYPE_MASK) == L2_TYPE_L) { + *pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET); + return (_kvm_pa2off(kd, *pa, pa, L2_L_SIZE)); + } + *pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET); + return (_kvm_pa2off(kd, *pa, pa, PAGE_SIZE)); +invalid: + _kvm_err(kd, 0, "Invalid address (%x)", va); + return 0; +} + +/* + * Machine-dependent initialization for ALL open kvm descriptors, + * not just those for a kernel crash dump. Some architectures + * have to deal with these NOT being constants! (i.e. m68k) + */ +int +_kvm_mdopen(kd) + kvm_t *kd; +{ + +#ifdef FBSD_NOT_YET + kd->usrstack = USRSTACK; + kd->min_uva = VM_MIN_ADDRESS; + kd->max_uva = VM_MAXUSER_ADDRESS; +#endif + + return (0); +} diff --git a/lib/libkvm/kvm_file.c b/lib/libkvm/kvm_file.c new file mode 100644 index 0000000..4a2e20c --- /dev/null +++ b/lib/libkvm/kvm_file.c @@ -0,0 +1,192 @@ +/*- + * Copyright (c) 1989, 1992, 1993 + * The Regents of the University of California. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)kvm_file.c 8.1 (Berkeley) 6/4/93"; +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * File list interface for kvm. pstat, fstat and netstat are + * users of this code, so we've factored it out into a separate module. + * Thus, we keep this grunge out of the other kvm applications (i.e., + * most other applications are interested only in open/close/read/nlist). + */ + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#define _KERNEL +#include <sys/file.h> +#undef _KERNEL +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <nlist.h> +#include <kvm.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> + +#include <sys/sysctl.h> + +#include <limits.h> +#include <ndbm.h> +#include <paths.h> + +#include "kvm_private.h" + +#define KREAD(kd, addr, obj) \ + (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj)) + +/* + * Get file structures. + */ +static int +kvm_deadfiles(kd, op, arg, filehead_o, nfiles) + kvm_t *kd; + int op, arg, nfiles; + long filehead_o; +{ + int buflen = kd->arglen, n = 0; + struct file *fp; + char *where = kd->argspc; + struct filelist filehead; + + /* + * first copyout filehead + */ + if (buflen > sizeof (filehead)) { + if (KREAD(kd, filehead_o, &filehead)) { + _kvm_err(kd, kd->program, "can't read filehead"); + return (0); + } + buflen -= sizeof (filehead); + where += sizeof (filehead); + *(struct filelist *)kd->argspc = filehead; + } + /* + * followed by an array of file structures + */ + LIST_FOREACH(fp, &filehead, f_list) { + if (buflen > sizeof (struct file)) { + if (KREAD(kd, (long)fp, ((struct file *)where))) { + _kvm_err(kd, kd->program, "can't read kfp"); + return (0); + } + buflen -= sizeof (struct file); + fp = (struct file *)where; + where += sizeof (struct file); + n++; + } + } + if (n != nfiles) { + _kvm_err(kd, kd->program, "inconsistant nfiles"); + return (0); + } + return (nfiles); +} + +char * +kvm_getfiles(kd, op, arg, cnt) + kvm_t *kd; + int op, arg; + int *cnt; +{ + int mib[2], st, nfiles; + size_t size; + struct file *fp, *fplim; + struct filelist filehead; + + if (ISALIVE(kd)) { + size = 0; + mib[0] = CTL_KERN; + mib[1] = KERN_FILE; + st = sysctl(mib, 2, NULL, &size, NULL, 0); + if (st == -1) { + _kvm_syserr(kd, kd->program, "kvm_getfiles"); + return (0); + } + if (kd->argspc == 0) + kd->argspc = (char *)_kvm_malloc(kd, size); + else if (kd->arglen < size) + kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size); + if (kd->argspc == 0) + return (0); + kd->arglen = size; + st = sysctl(mib, 2, kd->argspc, &size, NULL, 0); + if (st == -1 || size < sizeof(filehead)) { + _kvm_syserr(kd, kd->program, "kvm_getfiles"); + return (0); + } + filehead = *(struct filelist *)kd->argspc; + fp = (struct file *)(kd->argspc + sizeof (filehead)); + fplim = (struct file *)(kd->argspc + size); + for (nfiles = 0; LIST_FIRST(&filehead) && (fp < fplim); nfiles++, fp++) + LIST_FIRST(&filehead) = LIST_NEXT(fp, f_list); + } else { + struct nlist nl[3], *p; + + nl[0].n_name = "_filehead"; + nl[1].n_name = "_nfiles"; + nl[2].n_name = 0; + + if (kvm_nlist(kd, nl) != 0) { + for (p = nl; p->n_type != 0; ++p) + ; + _kvm_err(kd, kd->program, + "%s: no such symbol", p->n_name); + return (0); + } + if (KREAD(kd, nl[0].n_value, &nfiles)) { + _kvm_err(kd, kd->program, "can't read nfiles"); + return (0); + } + size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file); + if (kd->argspc == 0) + kd->argspc = (char *)_kvm_malloc(kd, size); + else if (kd->arglen < size) + kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size); + if (kd->argspc == 0) + return (0); + kd->arglen = size; + nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles); + if (nfiles == 0) + return (0); + } + *cnt = nfiles; + return (kd->argspc); +} diff --git a/lib/libkvm/kvm_geterr.3 b/lib/libkvm/kvm_geterr.3 new file mode 100644 index 0000000..49c6420 --- /dev/null +++ b/lib/libkvm/kvm_geterr.3 @@ -0,0 +1,82 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software developed by the Computer Systems +.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract +.\" BG 91-66 and contributed to Berkeley. +.\" +.\" 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_geterr.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt KVM_GETERR 3 +.Os +.Sh NAME +.Nm kvm_geterr +.Nd get error message on kvm descriptor +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In kvm.h +.Ft char * +.Fn kvm_geterr "kvm_t *kd" +.Sh DESCRIPTION +This function returns a string describing the most recent error condition +on the descriptor +.Fa kd . +The results are undefined if the most recent +.Xr kvm 3 +library call did not produce an error. +The string returned is stored in memory owned by +.Xr kvm 3 +so the message should be copied out and saved elsewhere if necessary. +.Sh SEE ALSO +.Xr kvm 3 , +.Xr kvm_close 3 , +.Xr kvm_getargv 3 , +.Xr kvm_getenvv 3 , +.Xr kvm_getprocs 3 , +.Xr kvm_nlist 3 , +.Xr kvm_open 3 , +.Xr kvm_openfiles 3 , +.Xr kvm_read 3 , +.Xr kvm_write 3 +.Sh BUGS +This routine cannot be used to access error conditions due to a failed +.Fn kvm_openfiles +call, since failure is indicated by returning a +.Dv NULL +descriptor. +Therefore, errors on open are output to the special error buffer +passed to +.Fn kvm_openfiles . +This option is not available to +.Fn kvm_open . diff --git a/lib/libkvm/kvm_getfiles.3 b/lib/libkvm/kvm_getfiles.3 new file mode 100644 index 0000000..b13fede --- /dev/null +++ b/lib/libkvm/kvm_getfiles.3 @@ -0,0 +1,91 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software developed by the Computer Systems +.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract +.\" BG 91-66 and contributed to Berkeley. +.\" +.\" 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_getfiles.3 8.2 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt KVM_GETFILES 3 +.Os +.Sh NAME +.Nm kvm_getfiles +.Nd survey open files +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In kvm.h +.In sys/types.h +.Fd #define _KERNEL +.In sys/file.h +.Fd #undef _KERNEL +.\" .Fa kvm_t *kd +.Ft char * +.Fn kvm_getfiles "kvm_t *kd" "int op" "int arg" "int *cnt" +.Sh DESCRIPTION +The +.Fn kvm_getfiles +function returns a (sub-)set of the open files in the kernel indicated by +.Fa kd . +The +.Fa op +and +.Fa arg +arguments constitute a predicate which limits the set of files +returned. +No predicates are currently defined. +.Pp +The number of files found is returned in the reference parameter +.Fa cnt . +The files are returned as a contiguous array of file structures, +preceded by the address of the first file entry in the kernel. +This memory is owned by kvm and is not guaranteed to be persistent across +subsequent kvm library calls. +Data should be copied out if it needs to be +saved. +.Sh RETURN VALUES +The +.Fn kvm_getfiles +function will return NULL on failure. +.Sh SEE ALSO +.Xr kvm 3 , +.Xr kvm_close 3 , +.Xr kvm_geterr 3 , +.Xr kvm_nlist 3 , +.Xr kvm_open 3 , +.Xr kvm_openfiles 3 , +.Xr kvm_read 3 , +.Xr kvm_write 3 +.Sh BUGS +This routine does not belong in the kvm interface. diff --git a/lib/libkvm/kvm_getloadavg.3 b/lib/libkvm/kvm_getloadavg.3 new file mode 100644 index 0000000..23310bc --- /dev/null +++ b/lib/libkvm/kvm_getloadavg.3 @@ -0,0 +1,66 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_getloadavg.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt KVM_GETLOADAVG 3 +.Os +.Sh NAME +.Nm kvm_getloadavg +.Nd get load average of the system +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In kvm.h +.Ft int +.Fn kvm_getloadavg "kvm_t *kd" "double loadavg[]" "int nelem" +.Sh DESCRIPTION +The +.Fn kvm_getloadavg +function returns the number of processes in the system run queue +of the kernel indicated by +.Fa kd , +averaged over various periods of time. +Up to +.Fa nelem +samples are retrieved and assigned to successive elements of +.Fa loadavg Ns Bq . +The system imposes a maximum of 3 samples, representing averages +over the last 1, 5, and 15 minutes, respectively. +.Sh DIAGNOSTICS +If the load average was unobtainable, \-1 is returned; otherwise, +the number of samples actually retrieved is returned. +.Sh SEE ALSO +.Xr uptime 1 , +.Xr getloadavg 3 , +.Xr kvm 3 diff --git a/lib/libkvm/kvm_getloadavg.c b/lib/libkvm/kvm_getloadavg.c new file mode 100644 index 0000000..72d59cd --- /dev/null +++ b/lib/libkvm/kvm_getloadavg.c @@ -0,0 +1,106 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)kvm_getloadavg.c 8.1 (Berkeley) 6/4/93"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/resource.h> + +#include <stdlib.h> +#include <limits.h> +#include <nlist.h> +#include <kvm.h> + +#include "kvm_private.h" + +static struct nlist nl[] = { + { "_averunnable" }, +#define X_AVERUNNABLE 0 + { "_fscale" }, +#define X_FSCALE 1 + { "" }, +}; + +/* + * kvm_getloadavg() -- Get system load averages, from live or dead kernels. + * + * Put `nelem' samples into `loadavg' array. + * Return number of samples retrieved, or -1 on error. + */ +int +kvm_getloadavg(kd, loadavg, nelem) + kvm_t *kd; + double loadavg[]; + int nelem; +{ + struct loadavg loadinfo; + struct nlist *p; + int fscale, i; + + if (ISALIVE(kd)) + return (getloadavg(loadavg, nelem)); + + if (kvm_nlist(kd, nl) != 0) { + for (p = nl; p->n_type != 0; ++p); + _kvm_err(kd, kd->program, + "%s: no such symbol", p->n_name); + return (-1); + } + +#define KREAD(kd, addr, obj) \ + (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) + if (KREAD(kd, nl[X_AVERUNNABLE].n_value, &loadinfo)) { + _kvm_err(kd, kd->program, "can't read averunnable"); + return (-1); + } + + /* + * Old kernels have fscale separately; if not found assume + * running new format. + */ + if (!KREAD(kd, nl[X_FSCALE].n_value, &fscale)) + loadinfo.fscale = fscale; + + nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t)); + for (i = 0; i < nelem; i++) + loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale; + return (nelem); +} diff --git a/lib/libkvm/kvm_getprocs.3 b/lib/libkvm/kvm_getprocs.3 new file mode 100644 index 0000000..107f6a8 --- /dev/null +++ b/lib/libkvm/kvm_getprocs.3 @@ -0,0 +1,185 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software developed by the Computer Systems +.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract +.\" BG 91-66 and contributed to Berkeley. +.\" +.\" 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_getprocs.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd September 27, 2003 +.Dt KVM_GETPROCS 3 +.Os +.Sh NAME +.Nm kvm_getprocs , +.Nm kvm_getargv , +.Nm kvm_getenvv +.Nd access user process state +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In kvm.h +.In sys/param.h +.In sys/sysctl.h +.In sys/user.h +.\" .Fa kvm_t *kd +.Ft struct kinfo_proc * +.Fn kvm_getprocs "kvm_t *kd" "int op" "int arg" "int *cnt" +.Ft char ** +.Fn kvm_getargv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr" +.Ft char ** +.Fn kvm_getenvv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr" +.Sh DESCRIPTION +The +.Fn kvm_getprocs +function returns a (sub-)set of active processes in the kernel indicated by +.Fa kd . +The +.Fa op +and +.Fa arg +arguments constitute a predicate which limits the set of processes +returned. +The value of +.Fa op +describes the filtering predicate as follows: +.Pp +.Bl -tag -width 20n -offset indent -compact +.It Dv KERN_PROC_ALL +all processes and kernel visible threads +.It Dv KERN_PROC_PROC +all processes, without threads +.It Dv KERN_PROC_PID +processes with process ID +.Fa arg +.It Dv KERN_PROC_PGRP +processes with process group +.Fa arg +.It Dv KERN_PROC_SESSION +processes with session +.Fa arg +.It Dv KERN_PROC_TTY +processes with TTY +.Fa arg +.It Dv KERN_PROC_UID +processes with effective user ID +.Fa arg +.It Dv KERN_PROC_RUID +processes with real user ID +.Fa arg +.It Dv KERN_PROC_INC_THREAD +modifier to return all kernel visible threads when filtering +by process ID, process group, TTY, user ID, and real user ID +.El +.Pp +The number of processes found is returned in the reference parameter +.Fa cnt . +The processes are returned as a contiguous array of kinfo_proc structures. +This memory is locally allocated, and subsequent calls to +.Fn kvm_getprocs +and +.Fn kvm_close +will overwrite this storage. +.Pp +The +.Fn kvm_getargv +function returns a null-terminated argument vector that corresponds to the +command line arguments passed to process indicated by +.Fa p . +Most likely, these arguments correspond to the values passed to +.Xr exec 3 +on process creation. +This information is, however, +deliberately under control of the process itself. +Note that the original command name can be found, unaltered, +in the p_comm field of the process structure returned by +.Fn kvm_getprocs . +.Pp +The +.Fa nchr +argument indicates the maximum number of characters, including null bytes, +to use in building the strings. +If this amount is exceeded, the string +causing the overflow is truncated and the partial result is returned. +This is handy for programs like +.Xr ps 1 +and +.Xr w 1 +that print only a one line summary of a command and should not copy +out large amounts of text only to ignore it. +If +.Fa nchr +is zero, no limit is imposed and all argument strings are returned in +their entirety. +.Pp +The memory allocated to the argv pointers and string storage +is owned by the kvm library. +Subsequent +.Fn kvm_getprocs +and +.Xr kvm_close 3 +calls will clobber this storage. +.Pp +The +.Fn kvm_getenvv +function is similar to +.Fn kvm_getargv +but returns the vector of environment strings. +This data is +also alterable by the process. +.Sh RETURN VALUES +The +.Fn kvm_getprocs , +.Fn kvm_getargv , +and +.Fn kvm_getenvv +functions return +.Dv NULL +on failure. +.Sh SEE ALSO +.Xr kvm 3 , +.Xr kvm_close 3 , +.Xr kvm_geterr 3 , +.Xr kvm_nlist 3 , +.Xr kvm_open 3 , +.Xr kvm_openfiles 3 , +.Xr kvm_read 3 , +.Xr kvm_write 3 +.Sh BUGS +These routines do not belong in the kvm interface. +.Pp +In order for +.Xr kvm_getenvv 3 +to function correctly, +.Xr procfs 5 +must be mounted on +.Pa /proc . diff --git a/lib/libkvm/kvm_getswapinfo.3 b/lib/libkvm/kvm_getswapinfo.3 new file mode 100644 index 0000000..edd2068 --- /dev/null +++ b/lib/libkvm/kvm_getswapinfo.3 @@ -0,0 +1,111 @@ +.\" Copyright (C) 1999 Matthew Dillon. 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 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 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$ +.\" +.Dd January 22, 1999 +.Dt KVM_SWAPINFO 3 +.Os +.Sh NAME +.Nm kvm_getswapinfo +.Nd return swap summary statistics for the system +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In kvm.h +.Ft int +.Fn kvm_getswapinfo "kvm_t *kd" "struct kvm_swap *" "int maxswap" "int flags" +.Sh DESCRIPTION +The +.Fn kvm_getswapinfo +function fills an array of +.Vt kvm_swap +structures with swap summary +information for each swap device, for up to +.Fa maxswap +\- 1 devices. +The number of devices, up to +.Fa maxswap +\- 1, is returned. +A grand +total of all swap devices (including any devices that go beyond +.Fa maxswap +\- 1) is returned in one additional array entry. +This +entry is not counted in the return value. +Thus, if you specify a +.Fa maxswap +value of 1, the function will typically return the +value 0 and the single +.Vt kvm_swap +structure will be filled with +the grand total over all swap devices. +The grand total is calculated +from all available swap devices whether or not you made room +for them all in the array. +The grand total is returned. +.Pp +The flags argument is currently unused and must be passed as 0. +.Pp +If an error occurs, -1 is returned. +.Pp +Each swap partition and the grand total is summarized in the +.Vt kvm_swap +structure. +This structure contains the following fields: +.Pp +.Bl -item -offset indent -compact +.It +.Va char ksw_devname[] ; +.It +.Va int ksw_total ; +.It +.Va int ksw_used ; +.It +.Va int ksw_flags ; +.El +.Pp +Values are in +.Dv PAGE_SIZE Ns 'd +chunks (see +.Xr getpagesize 3 ) . +.Va ksw_flags +contains +a copy of the swap device flags. +.Sh CACHING +This function caches the nlist values for various kernel variables which +it reuses in successive calls. +You may call the function with +.Fa kd +== +.Dv NULL +to clear the cache. +.Sh DIAGNOSTICS +If the load average was unobtainable, \-1 is returned; otherwise, +the number of swap devices actually retrieved is returned. +.Pp +If the name of the swap device does not fit in the static char buffer +in the structure, it is truncated. +The buffer is always zero terminated. +.Sh SEE ALSO +.Xr kvm 3 diff --git a/lib/libkvm/kvm_getswapinfo.c b/lib/libkvm/kvm_getswapinfo.c new file mode 100644 index 0000000..9ba7a5c --- /dev/null +++ b/lib/libkvm/kvm_getswapinfo.c @@ -0,0 +1,188 @@ +/* + * Copyright (c) 1999, Matthew Dillon. All Rights Reserved. + * Copyright (c) 2001, Thomas Moestl. 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 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 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/stat.h> +#include <sys/blist.h> +#include <sys/sysctl.h> + +#include <vm/vm_param.h> + +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <kvm.h> +#include <nlist.h> +#include <paths.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <limits.h> + +#include "kvm_private.h" + +#define NL_SWAPBLIST 0 +#define NL_SWDEVT 1 +#define NL_NSWDEV 2 +#define NL_DMMAX 3 + +static int kvm_swap_nl_cached = 0; +static int unswdev; /* number of found swap dev's */ +static int dmmax; + +static int kvm_getswapinfo_sysctl(kvm_t *, struct kvm_swap *, int, int); +static int getsysctl(kvm_t *, char *, void *, size_t); + +#define GETSWDEVNAME(dev, str, flags) \ + if (dev == NODEV) { \ + strlcpy(str, "[NFS swap]", sizeof(str)); \ + } else { \ + snprintf( \ + str, sizeof(str),"%s%s", \ + ((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""), \ + devname(dev, S_IFCHR) \ + ); \ + } + +int +kvm_getswapinfo( + kvm_t *kd, + struct kvm_swap *swap_ary, + int swap_max, + int flags +) { + + /* + * clear cache + */ + if (kd == NULL) { + kvm_swap_nl_cached = 0; + return(0); + } + + if (ISALIVE(kd)) { + return kvm_getswapinfo_sysctl(kd, swap_ary, swap_max, flags); + } else { + return -1; + } +} + +#define GETSYSCTL(kd, name, var) \ + getsysctl(kd, name, &(var), sizeof(var)) + +/* The maximum MIB length for vm.swap_info and an additional device number */ +#define SWI_MAXMIB 3 + +int +kvm_getswapinfo_sysctl( + kvm_t *kd, + struct kvm_swap *swap_ary, + int swap_max, + int flags +) { + int ti, ttl; + size_t mibi, len; + int soid[SWI_MAXMIB]; + struct xswdev xsd; + struct kvm_swap tot; + + if (!GETSYSCTL(kd, "vm.dmmax", dmmax)) + return -1; + + mibi = SWI_MAXMIB - 1; + if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) { + _kvm_err(kd, kd->program, "sysctlnametomib failed: %s", + strerror(errno)); + return -1; + } + bzero(&tot, sizeof(tot)); + for (unswdev = 0;; unswdev++) { + soid[mibi] = unswdev; + len = sizeof(xsd); + if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) { + if (errno == ENOENT) + break; + _kvm_err(kd, kd->program, "cannot read sysctl: %s.", + strerror(errno)); + return -1; + } + if (len != sizeof(xsd)) { + _kvm_err(kd, kd->program, "struct xswdev has unexpected " + "size; kernel and libkvm out of sync?"); + return -1; + } + if (xsd.xsw_version != XSWDEV_VERSION) { + _kvm_err(kd, kd->program, "struct xswdev version " + "mismatch; kernel and libkvm out of sync?"); + return -1; + } + + ttl = xsd.xsw_nblks - dmmax; + if (unswdev < swap_max - 1) { + bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev])); + swap_ary[unswdev].ksw_total = ttl; + swap_ary[unswdev].ksw_used = xsd.xsw_used; + swap_ary[unswdev].ksw_flags = xsd.xsw_flags; + GETSWDEVNAME(xsd.xsw_dev, swap_ary[unswdev].ksw_devname, + flags); + } + tot.ksw_total += ttl; + tot.ksw_used += xsd.xsw_used; + } + + ti = unswdev; + if (ti >= swap_max) + ti = swap_max - 1; + if (ti >= 0) + swap_ary[ti] = tot; + + return(ti); +} + +static int +getsysctl ( + kvm_t *kd, + char *name, + void *ptr, + size_t len +) { + size_t nlen = len; + if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { + _kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name, + strerror(errno)); + return (0); + } + if (nlen != len) { + _kvm_err(kd, kd->program, "sysctl %s has unexpected size", name); + return (0); + } + return (1); +} diff --git a/lib/libkvm/kvm_i386.c b/lib/libkvm/kvm_i386.c new file mode 100644 index 0000000..9560cc9 --- /dev/null +++ b/lib/libkvm/kvm_i386.c @@ -0,0 +1,448 @@ +/*- + * Copyright (c) 1989, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software developed by the Computer Systems + * Engineering group at Lawrence Berkeley Laboratory under DARPA contract + * BG 91-66 and contributed to Berkeley. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * i386 machine dependent routines for kvm. Hopefully, the forthcoming + * vm code will one day obsolete this module. + */ + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <stdlib.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" + +#ifndef btop +#define btop(x) (i386_btop(x)) +#define ptob(x) (i386_ptob(x)) +#endif + +#define PG_FRAME_PAE (~((uint64_t)PAGE_MASK)) +#define PDRSHIFT_PAE 21 +#define NPTEPG_PAE (PAGE_SIZE/sizeof(uint64_t)) +#define NBPDR_PAE (1<<PDRSHIFT_PAE) + +/* minidump must be the first item! */ +struct vmstate { + int minidump; /* 1 = minidump mode */ + void *mmapbase; + size_t mmapsize; + void *PTD; + int pae; +}; + +/* + * 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. (Taken from kvm_ia64.c) + */ +static int +_kvm_maphdrs(kvm_t *kd, size_t sz) +{ + struct vmstate *vm = kd->vmst; + + /* munmap() previous mmap(). */ + if (vm->mmapbase != NULL) { + munmap(vm->mmapbase, vm->mmapsize); + vm->mmapbase = NULL; + } + + vm->mmapsize = sz; + vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0); + if (vm->mmapbase == MAP_FAILED) { + _kvm_err(kd, kd->program, "cannot mmap corefile"); + return (-1); + } + return (0); +} + +/* + * Translate a physical memory address to a file-offset in the crash-dump. + * (Taken from kvm_ia64.c) + */ +static size_t +_kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs) +{ + Elf_Ehdr *e = kd->vmst->mmapbase; + Elf_Phdr *p = (Elf_Phdr*)((char*)e + e->e_phoff); + int n = e->e_phnum; + + while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) + p++, n--; + if (n == 0) + return (0); + *ofs = (pa - p->p_paddr) + p->p_offset; + return (PAGE_SIZE - ((size_t)pa & PAGE_MASK)); +} + +void +_kvm_freevtop(kvm_t *kd) +{ + struct vmstate *vm = kd->vmst; + + if (kd->vmst->minidump) + return (_kvm_minidump_freevtop(kd)); + if (vm->mmapbase != NULL) + munmap(vm->mmapbase, vm->mmapsize); + if (vm->PTD) + free(vm->PTD); + free(vm); + kd->vmst = NULL; +} + +int +_kvm_initvtop(kvm_t *kd) +{ + struct nlist nlist[2]; + u_long pa; + u_long kernbase; + char *PTD; + Elf_Ehdr *ehdr; + size_t hdrsz; + int i; + char minihdr[8]; + + if (pread(kd->pmfd, &minihdr, 8, 0) == 8) + if (memcmp(&minihdr, "minidump", 8) == 0) + return (_kvm_minidump_initvtop(kd)); + + kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); + if (kd->vmst == 0) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + kd->vmst->PTD = 0; + + if (_kvm_maphdrs(kd, sizeof(Elf_Ehdr)) == -1) + return (-1); + + ehdr = kd->vmst->mmapbase; + hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum; + if (_kvm_maphdrs(kd, hdrsz) == -1) + return (-1); + + nlist[0].n_name = "kernbase"; + nlist[1].n_name = 0; + + if (kvm_nlist(kd, nlist) != 0) + kernbase = KERNBASE; /* for old kernels */ + else + kernbase = nlist[0].n_value; + + nlist[0].n_name = "IdlePDPT"; + nlist[1].n_name = 0; + + if (kvm_nlist(kd, nlist) == 0) { + uint64_t pa64; + + if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, + sizeof(pa)) != sizeof(pa)) { + _kvm_err(kd, kd->program, "cannot read IdlePDPT"); + return (-1); + } + PTD = _kvm_malloc(kd, 4 * PAGE_SIZE); + for (i = 0; i < 4; i++) { + if (kvm_read(kd, pa + (i * sizeof(pa64)), &pa64, + sizeof(pa64)) != sizeof(pa64)) { + _kvm_err(kd, kd->program, "Cannot read PDPT"); + free(PTD); + return (-1); + } + if (kvm_read(kd, pa64 & PG_FRAME_PAE, + PTD + (i * PAGE_SIZE), PAGE_SIZE) != (PAGE_SIZE)) { + _kvm_err(kd, kd->program, "cannot read PDPT"); + free(PTD); + return (-1); + } + } + kd->vmst->PTD = PTD; + kd->vmst->pae = 1; + } else { + nlist[0].n_name = "IdlePTD"; + nlist[1].n_name = 0; + + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "bad namelist"); + return (-1); + } + if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, + sizeof(pa)) != sizeof(pa)) { + _kvm_err(kd, kd->program, "cannot read IdlePTD"); + return (-1); + } + PTD = _kvm_malloc(kd, PAGE_SIZE); + if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) { + _kvm_err(kd, kd->program, "cannot read PTD"); + return (-1); + } + kd->vmst->PTD = PTD; + return (0); + kd->vmst->pae = 0; + } + return (0); +} + +static int +_kvm_vatop(kvm_t *kd, u_long va, off_t *pa) +{ + struct vmstate *vm; + u_long offset; + u_long pte_pa; + u_long pde_pa; + pd_entry_t pde; + pt_entry_t pte; + u_long pdeindex; + u_long pteindex; + size_t s; + u_long a; + off_t ofs; + uint32_t *PTD; + + vm = kd->vmst; + PTD = (uint32_t *)vm->PTD; + offset = va & (PAGE_SIZE - 1); + + /* + * If we are initializing (kernel page table descriptor pointer + * not yet set) then return pa == va to avoid infinite recursion. + */ + if (PTD == 0) { + s = _kvm_pa2off(kd, va, pa); + if (s == 0) { + _kvm_err(kd, kd->program, + "_kvm_vatop: bootstrap data not in dump"); + goto invalid; + } else + return (PAGE_SIZE - offset); + } + + pdeindex = va >> PDRSHIFT; + pde = PTD[pdeindex]; + if (((u_long)pde & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid"); + goto invalid; + } + + if ((u_long)pde & PG_PS) { + /* + * No second-level page table; ptd describes one 4MB page. + * (We assume that the kernel wouldn't set PG_PS without enabling + * it cr0). + */ +#define PAGE4M_MASK (NBPDR - 1) +#define PG_FRAME4M (~PAGE4M_MASK) + pde_pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK); + s = _kvm_pa2off(kd, pde_pa, &ofs); + if (s < sizeof pde) { + _kvm_syserr(kd, kd->program, + "_kvm_vatop: pde_pa not found"); + goto invalid; + } + *pa = ofs; + return (NBPDR - (va & PAGE4M_MASK)); + } + + pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1); + pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pde)); + + s = _kvm_pa2off(kd, pte_pa, &ofs); + if (s < sizeof pte) { + _kvm_err(kd, kd->program, "_kvm_vatop: pdpe_pa not found"); + goto invalid; + } + + /* XXX This has to be a physical address read, kvm_read is virtual */ + if (lseek(kd->pmfd, ofs, 0) == -1) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek"); + goto invalid; + } + if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { + _kvm_syserr(kd, kd->program, "_kvm_vatop: read"); + goto invalid; + } + if (((u_long)pte & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid"); + goto invalid; + } + + a = ((u_long)pte & PG_FRAME) + offset; + s =_kvm_pa2off(kd, a, pa); + if (s == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: address not in dump"); + goto invalid; + } else + return (PAGE_SIZE - offset); + +invalid: + _kvm_err(kd, 0, "invalid address (0x%lx)", va); + return (0); +} + +static int +_kvm_vatop_pae(kvm_t *kd, u_long va, off_t *pa) +{ + struct vmstate *vm; + uint64_t offset; + uint64_t pte_pa; + uint64_t pde_pa; + uint64_t pde; + uint64_t pte; + u_long pdeindex; + u_long pteindex; + size_t s; + uint64_t a; + off_t ofs; + uint64_t *PTD; + + vm = kd->vmst; + PTD = (uint64_t *)vm->PTD; + offset = va & (PAGE_SIZE - 1); + + /* + * If we are initializing (kernel page table descriptor pointer + * not yet set) then return pa == va to avoid infinite recursion. + */ + if (PTD == 0) { + s = _kvm_pa2off(kd, va, pa); + if (s == 0) { + _kvm_err(kd, kd->program, + "_kvm_vatop_pae: bootstrap data not in dump"); + goto invalid; + } else + return (PAGE_SIZE - offset); + } + + pdeindex = va >> PDRSHIFT_PAE; + pde = PTD[pdeindex]; + if (((u_long)pde & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_kvatop_pae: pde not valid"); + goto invalid; + } + + if ((u_long)pde & PG_PS) { + /* + * No second-level page table; ptd describes one 2MB page. + * (We assume that the kernel wouldn't set PG_PS without enabling + * it cr0). + */ +#define PAGE2M_MASK (NBPDR_PAE - 1) +#define PG_FRAME2M (~PAGE2M_MASK) + pde_pa = ((u_long)pde & PG_FRAME2M) + (va & PAGE2M_MASK); + s = _kvm_pa2off(kd, pde_pa, &ofs); + if (s < sizeof pde) { + _kvm_syserr(kd, kd->program, + "_kvm_vatop_pae: pde_pa not found"); + goto invalid; + } + *pa = ofs; + return (NBPDR_PAE - (va & PAGE2M_MASK)); + } + + pteindex = (va >> PAGE_SHIFT) & (NPTEPG_PAE-1); + pte_pa = ((uint64_t)pde & PG_FRAME_PAE) + (pteindex * sizeof(pde)); + + s = _kvm_pa2off(kd, pte_pa, &ofs); + if (s < sizeof pte) { + _kvm_err(kd, kd->program, "_kvm_vatop_pae: pdpe_pa not found"); + goto invalid; + } + + /* XXX This has to be a physical address read, kvm_read is virtual */ + if (lseek(kd->pmfd, ofs, 0) == -1) { + _kvm_syserr(kd, kd->program, "_kvm_vatop_pae: lseek"); + goto invalid; + } + if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { + _kvm_syserr(kd, kd->program, "_kvm_vatop_pae: read"); + goto invalid; + } + if (((uint64_t)pte & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop_pae: pte not valid"); + goto invalid; + } + + a = ((uint64_t)pte & PG_FRAME_PAE) + offset; + s =_kvm_pa2off(kd, a, pa); + if (s == 0) { + _kvm_err(kd, kd->program, + "_kvm_vatop_pae: address not in dump"); + goto invalid; + } else + return (PAGE_SIZE - offset); + +invalid: + _kvm_err(kd, 0, "invalid address (0x%lx)", va); + return (0); +} + +int +_kvm_kvatop(kvm_t *kd, u_long va, off_t *pa) +{ + + if (kd->vmst->minidump) + return (_kvm_minidump_kvatop(kd, va, pa)); + if (ISALIVE(kd)) { + _kvm_err(kd, 0, "vatop called in live kernel!"); + return (0); + } + if (kd->vmst->pae) + return (_kvm_vatop_pae(kd, va, pa)); + else + return (_kvm_vatop(kd, va, pa)); +} diff --git a/lib/libkvm/kvm_ia64.c b/lib/libkvm/kvm_ia64.c new file mode 100644 index 0000000..fbee551 --- /dev/null +++ b/lib/libkvm/kvm_ia64.c @@ -0,0 +1,209 @@ +/* $FreeBSD$ */ +/* $NetBSD: kvm_alpha.c,v 1.7.2.1 1997/11/02 20:34:26 mellon Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include <sys/types.h> +#include <sys/elf64.h> +#include <sys/mman.h> + +#include <machine/pte.h> + +#include <kvm.h> +#include <limits.h> +#include <stdlib.h> +#include <unistd.h> + +#include "kvm_private.h" + +#define REGION_BASE(n) (((uint64_t)(n)) << 61) +#define REGION_ADDR(x) ((x) & ((1LL<<61)-1LL)) + +#define NKPTEPG(ps) ((ps) / sizeof(struct ia64_lpte)) +#define KPTE_PTE_INDEX(va,ps) (((va)/(ps)) % NKPTEPG(ps)) +#define KPTE_DIR_INDEX(va,ps) (((va)/(ps)) / NKPTEPG(ps)) + +struct vmstate { + void *mmapbase; + size_t mmapsize; + size_t pagesize; + u_long kptdir; +}; + +/* + * 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) +{ + struct vmstate *vm = kd->vmst; + + /* munmap() previous mmap(). */ + if (vm->mmapbase != NULL) { + munmap(vm->mmapbase, vm->mmapsize); + vm->mmapbase = NULL; + } + + vm->mmapsize = sz; + vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0); + if (vm->mmapbase == MAP_FAILED) { + _kvm_err(kd, kd->program, "cannot mmap corefile"); + return (-1); + } + + return (0); +} + +/* + * Translate a physical memory address to a file-offset in the crash-dump. + */ +static size_t +_kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz) +{ + Elf64_Ehdr *e = kd->vmst->mmapbase; + Elf64_Phdr *p = (Elf64_Phdr*)((char*)e + e->e_phoff); + int n = e->e_phnum; + + if (pa != REGION_ADDR(pa)) { + _kvm_err(kd, kd->program, "internal error"); + return (0); + } + + while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) + p++, n--; + if (n == 0) + return (0); + + *ofs = (pa - p->p_paddr) + p->p_offset; + if (pgsz == 0) + return (p->p_memsz - (pa - p->p_paddr)); + return (pgsz - ((size_t)pa & (pgsz - 1))); +} + +void +_kvm_freevtop(kvm_t *kd) +{ + struct vmstate *vm = kd->vmst; + + if (vm->mmapbase != NULL) + munmap(vm->mmapbase, vm->mmapsize); + free(vm); + kd->vmst = NULL; +} + +int +_kvm_initvtop(kvm_t *kd) +{ + struct nlist nlist[2]; + uint64_t va; + Elf64_Ehdr *ehdr; + size_t hdrsz; + + kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); + if (kd->vmst == NULL) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + + kd->vmst->pagesize = getpagesize(); + + if (_kvm_maphdrs(kd, sizeof(Elf64_Ehdr)) == -1) + return (-1); + + ehdr = kd->vmst->mmapbase; + hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum; + if (_kvm_maphdrs(kd, hdrsz) == -1) + return (-1); + + /* + * At this point we've got enough information to use kvm_read() for + * direct mapped (ie region 6 and region 7) address, such as symbol + * addresses/values. + */ + + nlist[0].n_name = "ia64_kptdir"; + nlist[1].n_name = 0; + + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "bad namelist"); + return (-1); + } + + if (kvm_read(kd, (nlist[0].n_value), &va, sizeof(va)) != sizeof(va)) { + _kvm_err(kd, kd->program, "cannot read kptdir"); + return (-1); + } + + if (va < REGION_BASE(6)) { + _kvm_err(kd, kd->program, "kptdir is itself virtual"); + return (-1); + } + + kd->vmst->kptdir = va; + return (0); +} + +int +_kvm_kvatop(kvm_t *kd, u_long va, off_t *pa) +{ + struct ia64_lpte pte; + uint64_t pgaddr, ptaddr; + size_t pgno, pgsz, ptno; + + if (va >= REGION_BASE(6)) { + /* Regions 6 and 7: direct mapped. */ + return (_kvm_pa2off(kd, REGION_ADDR(va), pa, 0)); + } else if (va >= REGION_BASE(5)) { + /* Region 5: virtual. */ + va = REGION_ADDR(va); + pgsz = kd->vmst->pagesize; + ptno = KPTE_DIR_INDEX(va, pgsz); + pgno = KPTE_PTE_INDEX(va, pgsz); + if (ptno >= (pgsz >> 3)) + goto fail; + ptaddr = kd->vmst->kptdir + (ptno << 3); + if (kvm_read(kd, ptaddr, &pgaddr, 8) != 8) + goto fail; + if (pgaddr == 0) + goto fail; + pgaddr += (pgno * sizeof(pte)); + if (kvm_read(kd, pgaddr, &pte, sizeof(pte)) != sizeof(pte)) + goto fail; + if (!(pte.pte & PTE_PRESENT)) + goto fail; + va = (pte.pte & PTE_PPN_MASK) + (va & (pgsz - 1)); + return (_kvm_pa2off(kd, va, pa, pgsz)); + } + + fail: + _kvm_err(kd, kd->program, "invalid kernel virtual address"); + *pa = ~0UL; + return (0); +} diff --git a/lib/libkvm/kvm_minidump_amd64.c b/lib/libkvm/kvm_minidump_amd64.c new file mode 100644 index 0000000..c431756 --- /dev/null +++ b/lib/libkvm/kvm_minidump_amd64.c @@ -0,0 +1,255 @@ +/*- + * Copyright (c) 2006 Peter Wemm + * + * 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * AMD64 machine dependent routines for kvm and minidumps. + */ + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <sys/fnv_hash.h> +#include <stdlib.h> +#include <unistd.h> +#include <nlist.h> +#include <kvm.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> + +#include <machine/elf.h> +#include <machine/cpufunc.h> +#include <machine/minidump.h> + +#include <limits.h> + +#include "kvm_private.h" + +struct hpte { + struct hpte *next; + vm_paddr_t pa; + int64_t off; +}; + +#define HPT_SIZE 1024 + +/* minidump must be the first item! */ +struct vmstate { + int minidump; /* 1 = minidump mode */ + struct minidumphdr hdr; + void *hpt_head[HPT_SIZE]; + uint64_t *bitmap; + uint64_t *ptemap; +}; + +static void +hpt_insert(kvm_t *kd, vm_paddr_t pa, int64_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 = kd->vmst->hpt_head[fnv]; + kd->vmst->hpt_head[fnv] = hpte; +} + +static int64_t +hpt_find(kvm_t *kd, vm_paddr_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 = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) { + if (pa == hpte->pa) + return (hpte->off); + } + return (-1); +} + +static int +inithash(kvm_t *kd, uint64_t *base, int len, off_t off) +{ + uint64_t idx; + uint64_t bit, bits; + vm_paddr_t pa; + + for (idx = 0; idx < len / sizeof(*base); idx++) { + bits = base[idx]; + while (bits) { + bit = bsfq(bits); + bits &= ~(1ul << bit); + pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE; + hpt_insert(kd, pa, off); + off += PAGE_SIZE; + } + } + return (off); +} + +void +_kvm_minidump_freevtop(kvm_t *kd) +{ + struct vmstate *vm = kd->vmst; + + if (vm->bitmap) + free(vm->bitmap); + if (vm->ptemap) + free(vm->ptemap); + free(vm); + kd->vmst = NULL; +} + +int +_kvm_minidump_initvtop(kvm_t *kd) +{ + u_long pa; + struct vmstate *vmst; + off_t off; + + vmst = _kvm_malloc(kd, sizeof(*vmst)); + if (vmst == 0) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + kd->vmst = vmst; + bzero(vmst, sizeof(*vmst)); + vmst->minidump = 1; + if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != + sizeof(vmst->hdr)) { + _kvm_err(kd, kd->program, "cannot read dump header"); + return (-1); + } + if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) { + _kvm_err(kd, kd->program, "not a minidump for this platform"); + return (-1); + } + if (vmst->hdr.version != MINIDUMP_VERSION) { + _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d", + MINIDUMP_VERSION, vmst->hdr.version); + return (-1); + } + + /* Skip header and msgbuf */ + off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize); + + vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); + if (vmst->bitmap == NULL) { + _kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize); + return (-1); + } + if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) != + vmst->hdr.bitmapsize) { + _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize); + return (-1); + } + off += round_page(vmst->hdr.bitmapsize); + + vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize); + if (vmst->ptemap == NULL) { + _kvm_err(kd, kd->program, "cannot allocate %d bytes for ptemap", vmst->hdr.ptesize); + return (-1); + } + if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) != + vmst->hdr.ptesize) { + _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", vmst->hdr.ptesize); + return (-1); + } + off += vmst->hdr.ptesize; + + /* build physical address hash table for sparse pages */ + inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off); + + return (0); +} + +static int +_kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa) +{ + struct vmstate *vm; + u_long offset; + pt_entry_t pte; + u_long pteindex; + int i; + u_long a; + off_t ofs; + + vm = kd->vmst; + offset = va & (PAGE_SIZE - 1); + + if (va >= vm->hdr.kernbase) { + pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT; + pte = vm->ptemap[pteindex]; + if (((u_long)pte & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); + goto invalid; + } + a = pte & PG_FRAME; + ofs = hpt_find(kd, a); + if (ofs == -1) { + _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a); + goto invalid; + } + *pa = ofs + offset; + return (PAGE_SIZE - offset); + } else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) { + a = (va - vm->hdr.dmapbase) & ~PAGE_MASK; + ofs = hpt_find(kd, a); + if (ofs == -1) { + _kvm_err(kd, kd->program, "_kvm_vatop: direct map address 0x%lx not in minidump", va); + goto invalid; + } + *pa = ofs + offset; + return (PAGE_SIZE - offset); + } else { + _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va); + goto invalid; + } + +invalid: + _kvm_err(kd, 0, "invalid address (0x%lx)", va); + return (0); +} + +int +_kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa) +{ + + if (ISALIVE(kd)) { + _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); + return (0); + } + return (_kvm_minidump_vatop(kd, va, pa)); +} diff --git a/lib/libkvm/kvm_minidump_i386.c b/lib/libkvm/kvm_minidump_i386.c new file mode 100644 index 0000000..c7d1f91 --- /dev/null +++ b/lib/libkvm/kvm_minidump_i386.c @@ -0,0 +1,294 @@ +/*- + * Copyright (c) 2006 Peter Wemm + * + * 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * AMD64 machine dependent routines for kvm and minidumps. + */ + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <sys/fnv_hash.h> +#include <stdlib.h> +#include <unistd.h> +#include <nlist.h> +#include <kvm.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> + +#include <machine/elf.h> +#include <machine/cpufunc.h> +#include <machine/minidump.h> + +#include <limits.h> + +#include "kvm_private.h" + +#define PG_FRAME_PAE (~((uint64_t)PAGE_MASK)) + +struct hpte { + struct hpte *next; + uint64_t pa; + int64_t off; +}; + +#define HPT_SIZE 1024 + +/* minidump must be the first item! */ +struct vmstate { + int minidump; /* 1 = minidump mode */ + struct minidumphdr hdr; + void *hpt_head[HPT_SIZE]; + uint32_t *bitmap; + void *ptemap; +}; + +static void +hpt_insert(kvm_t *kd, uint64_t pa, int64_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 = kd->vmst->hpt_head[fnv]; + kd->vmst->hpt_head[fnv] = hpte; +} + +static int64_t +hpt_find(kvm_t *kd, 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 = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) { + if (pa == hpte->pa) + return (hpte->off); + } + return (-1); +} + +static int +inithash(kvm_t *kd, uint32_t *base, int len, off_t off) +{ + uint64_t idx; + uint32_t bit, bits; + uint64_t pa; + + for (idx = 0; idx < len / sizeof(*base); idx++) { + bits = base[idx]; + while (bits) { + bit = bsfl(bits); + bits &= ~(1ul << bit); + pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE; + hpt_insert(kd, pa, off); + off += PAGE_SIZE; + } + } + return (off); +} + +void +_kvm_minidump_freevtop(kvm_t *kd) +{ + struct vmstate *vm = kd->vmst; + + if (vm->bitmap) + free(vm->bitmap); + if (vm->ptemap) + free(vm->ptemap); + free(vm); + kd->vmst = NULL; +} + +int +_kvm_minidump_initvtop(kvm_t *kd) +{ + u_long pa; + struct vmstate *vmst; + off_t off; + + vmst = _kvm_malloc(kd, sizeof(*vmst)); + if (vmst == 0) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + kd->vmst = vmst; + bzero(vmst, sizeof(*vmst)); + vmst->minidump = 1; + if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != + sizeof(vmst->hdr)) { + _kvm_err(kd, kd->program, "cannot read dump header"); + return (-1); + } + if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) { + _kvm_err(kd, kd->program, "not a minidump for this platform"); + return (-1); + } + if (vmst->hdr.version != MINIDUMP_VERSION) { + _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d", + MINIDUMP_VERSION, vmst->hdr.version); + return (-1); + } + + /* Skip header and msgbuf */ + off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize); + + vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); + if (vmst->bitmap == NULL) { + _kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize); + return (-1); + } + if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) != + vmst->hdr.bitmapsize) { + _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize); + return (-1); + } + off += round_page(vmst->hdr.bitmapsize); + + vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize); + if (vmst->ptemap == NULL) { + _kvm_err(kd, kd->program, "cannot allocate %d bytes for ptemap", vmst->hdr.ptesize); + return (-1); + } + if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) != + vmst->hdr.ptesize) { + _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", vmst->hdr.ptesize); + return (-1); + } + off += vmst->hdr.ptesize; + + /* build physical address hash table for sparse pages */ + inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off); + + return (0); +} + +static int +_kvm_minidump_vatop_pae(kvm_t *kd, u_long va, off_t *pa) +{ + struct vmstate *vm; + uint64_t offset; + uint64_t pte; + u_long pteindex; + int i; + uint64_t a; + off_t ofs; + uint64_t *ptemap; + + vm = kd->vmst; + ptemap = vm->ptemap; + offset = va & (PAGE_SIZE - 1); + + if (va >= vm->hdr.kernbase) { + pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT; + pte = ptemap[pteindex]; + if ((pte & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); + goto invalid; + } + a = pte & PG_FRAME_PAE; + ofs = hpt_find(kd, a); + if (ofs == -1) { + _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%llx not in minidump", a); + goto invalid; + } + *pa = ofs + offset; + return (PAGE_SIZE - offset); + } else { + _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va); + goto invalid; + } + +invalid: + _kvm_err(kd, 0, "invalid address (0x%lx)", va); + return (0); +} + +static int +_kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa) +{ + struct vmstate *vm; + u_long offset; + pt_entry_t pte; + u_long pteindex; + int i; + u_long a; + off_t ofs; + uint32_t *ptemap; + + vm = kd->vmst; + ptemap = vm->ptemap; + offset = va & (PAGE_SIZE - 1); + + if (va >= vm->hdr.kernbase) { + pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT; + pte = ptemap[pteindex]; + if ((pte & PG_V) == 0) { + _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid"); + goto invalid; + } + a = pte & PG_FRAME; + ofs = hpt_find(kd, a); + if (ofs == -1) { + _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a); + goto invalid; + } + *pa = ofs + offset; + return (PAGE_SIZE - offset); + } else { + _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va); + goto invalid; + } + +invalid: + _kvm_err(kd, 0, "invalid address (0x%lx)", va); + return (0); +} + +int +_kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa) +{ + + if (ISALIVE(kd)) { + _kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); + return (0); + } + if (kd->vmst->hdr.paemode) + return (_kvm_minidump_vatop_pae(kd, va, pa)); + else + return (_kvm_minidump_vatop(kd, va, pa)); +} diff --git a/lib/libkvm/kvm_nlist.3 b/lib/libkvm/kvm_nlist.3 new file mode 100644 index 0000000..c1dd8b7 --- /dev/null +++ b/lib/libkvm/kvm_nlist.3 @@ -0,0 +1,89 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software developed by the Computer Systems +.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract +.\" BG 91-66 and contributed to Berkeley. +.\" +.\" 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_nlist.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt KVM_NLIST 3 +.Os +.Sh NAME +.Nm kvm_nlist +.Nd retrieve symbol table names from a kernel image +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In kvm.h +.In nlist.h +.Ft int +.Fn kvm_nlist "kvm_t *kd" "struct nlist *nl" +.Sh DESCRIPTION +The +.Fn kvm_nlist +function retrieves the symbol table entries indicated by the name list argument +.Fa \&nl . +This argument points to an array of nlist structures, terminated by +an entry whose n_name field is +.Dv NULL +(see +.Xr nlist 3 ) . +Each symbol is looked up using the n_name field, and if found, the +corresponding n_type and n_value fields are filled in. +These fields are set +to 0 if the symbol is not found. +.Pp +The +.Xr kldsym 2 +system call is used to locate the symbol. +This is a less than perfect +emulation of the nlist values but has the advantage of being aware of kernel +modules and is reasonably fast. +.Sh RETURN VALUES +The +.Fn kvm_nlist +function returns the number of invalid entries found. +If the kernel symbol table was unreadable, -1 is returned. +.Sh SEE ALSO +.Xr kldsym 2 , +.Xr kvm 3 , +.Xr kvm_close 3 , +.Xr kvm_getargv 3 , +.Xr kvm_getenvv 3 , +.Xr kvm_geterr 3 , +.Xr kvm_getprocs 3 , +.Xr kvm_open 3 , +.Xr kvm_openfiles 3 , +.Xr kvm_read 3 , +.Xr kvm_write 3 diff --git a/lib/libkvm/kvm_open.3 b/lib/libkvm/kvm_open.3 new file mode 100644 index 0000000..f9fa557 --- /dev/null +++ b/lib/libkvm/kvm_open.3 @@ -0,0 +1,209 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software developed by the Computer Systems +.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract +.\" BG 91-66 and contributed to Berkeley. +.\" +.\" 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_open.3 8.3 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd January 29, 2004 +.Dt KVM_OPEN 3 +.Os +.Sh NAME +.Nm kvm_open , +.Nm kvm_openfiles , +.Nm kvm_close +.Nd initialize kernel virtual memory access +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In fcntl.h +.In kvm.h +.Ft kvm_t * +.Fn kvm_open "const char *execfile" "const char *corefile" "const char *swapfile" "int flags" "const char *errstr" +.Ft kvm_t * +.Fn kvm_openfiles "const char *execfile" "const char *corefile" "const char *swapfile" "int flags" "char *errbuf" +.Ft int +.Fn kvm_close "kvm_t *kd" +.Sh DESCRIPTION +The functions +.Fn kvm_open +and +.Fn kvm_openfiles +return a descriptor used to access kernel virtual memory +via the +.Xr kvm 3 +library routines. +Both active kernels and crash dumps are accessible +through this interface. +.Pp +The +.Fa execfile +argument is the executable image of the kernel being examined. +This file must contain a symbol table. +If this argument is +.Dv NULL , +the currently running system is assumed, +as determined from +.Xr getbootfile 3 . +.Pp +The +.Fa corefile +argument is the kernel memory device file. +It can be either +.Pa /dev/mem +or a crash dump core generated by +.Xr savecore 8 . +If +.Fa corefile +is +.Dv NULL , +the default indicated by +.Dv _PATH_MEM +from +.In paths.h +is used. +It can also be set to a special value +.Pa /dev/null +by utilities like +.Xr ps 1 +that do not directly access kernel memory. +.Pp +The +.Fa swapfile +argument is currently unused. +.Pp +The +.Fa flags +argument indicates read/write access as in +.Xr open 2 +and applies only to the core file. +Only +.Dv O_RDONLY , +.Dv O_WRONLY , +and +.Dv O_RDWR +are permitted. +.Pp +There are two open routines which differ only with respect to +the error mechanism. +One provides backward compatibility with the SunOS kvm library, while the +other provides an improved error reporting framework. +.Pp +The +.Fn kvm_open +function is the Sun kvm compatible open call. +Here, the +.Fa errstr +argument indicates how errors should be handled. +If it is +.Dv NULL , +no errors are reported and the application cannot know the +specific nature of the failed kvm call. +If it is not +.Dv NULL , +errors are printed to +.Dv stderr +with +.Fa errstr +prepended to the message, as in +.Xr perror 3 . +Normally, the name of the program is used here. +The string is assumed to persist at least until the corresponding +.Fn kvm_close +call. +.Pp +The +.Fn kvm_openfiles +function provides +.Bx +style error reporting. +Here, error messages are not printed out by the library. +Instead, the application obtains the error message +corresponding to the most recent kvm library call using +.Fn kvm_geterr +(see +.Xr kvm_geterr 3 ) . +The results are undefined if the most recent kvm call did not produce +an error. +Since +.Fn kvm_geterr +requires a kvm descriptor, but the open routines return +.Dv NULL +on failure, +.Fn kvm_geterr +cannot be used to get the error message if open fails. +Thus, +.Fn kvm_openfiles +will place any error message in the +.Fa errbuf +argument. +This buffer should be _POSIX2_LINE_MAX characters large (from +<limits.h>). +.Sh RETURN VALUES +The +.Fn kvm_open +and +.Fn kvm_openfiles +functions both return a descriptor to be used +in all subsequent kvm library calls. +The library is fully re-entrant. +On failure, +.Dv NULL +is returned, in which case +.Fn kvm_openfiles +writes the error message into +.Fa errbuf . +.Pp +The +.Fn kvm_close +function returns 0 on success and -1 on failure. +.Sh SEE ALSO +.Xr open 2 , +.Xr kvm 3 , +.Xr kvm_getargv 3 , +.Xr kvm_getenvv 3 , +.Xr kvm_geterr 3 , +.Xr kvm_getprocs 3 , +.Xr kvm_nlist 3 , +.Xr kvm_read 3 , +.Xr kvm_write 3 , +.Xr kmem 4 , +.Xr mem 4 +.Sh BUGS +There should not be two open calls. +The ill-defined error semantics +of the Sun library and the desire to have a backward-compatible library +for +.Bx +left little choice. diff --git a/lib/libkvm/kvm_powerpc.c b/lib/libkvm/kvm_powerpc.c new file mode 100644 index 0000000..24ab7fb --- /dev/null +++ b/lib/libkvm/kvm_powerpc.c @@ -0,0 +1,103 @@ +/* $NetBSD: kvm_powerpc.c,v 1.4 1998/02/03 06:50:07 mycroft Exp $ */ + +/*- + * Copyright (C) 1996 Wolfgang Solfrank. + * Copyright (C) 1996 TooLs GmbH. + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by TooLs GmbH. + * 4. The name of TooLs GmbH may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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. + */ + +/* + * PowerPC machine dependent routines for kvm. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> + +#include <vm/vm.h> + +#include <db.h> +#include <limits.h> +#include <kvm.h> +#include <stdlib.h> + +#include "kvm_private.h" + +void +_kvm_freevtop(kd) + kvm_t *kd; +{ + if (kd->vmst != 0) + free(kd->vmst); +} + +int +_kvm_initvtop(kd) + kvm_t *kd; +{ + return 0; +} + +int +_kvm_kvatop(kd, va, pa) + kvm_t *kd; + u_long va; + off_t *pa; +{ + _kvm_err(kd, 0, "vatop not yet implemented!"); + return 0; +} + +off_t +_kvm_pa2off(kd, pa) + kvm_t *kd; + u_long pa; +{ + _kvm_err(kd, 0, "pa2off not yet implemented!"); + return 0; +} + +/* + * Machine-dependent initialization for ALL open kvm descriptors, + * not just those for a kernel crash dump. Some architectures + * have to deal with these NOT being constants! (i.e. m68k) + */ +int +_kvm_mdopen(kd) + kvm_t *kd; +{ + +#ifdef FBSD_NOT_YET + kd->usrstack = USRSTACK; + kd->min_uva = VM_MIN_ADDRESS; + kd->max_uva = VM_MAXUSER_ADDRESS; +#endif + + return (0); +} diff --git a/lib/libkvm/kvm_private.h b/lib/libkvm/kvm_private.h new file mode 100644 index 0000000..9bbeb43 --- /dev/null +++ b/lib/libkvm/kvm_private.h @@ -0,0 +1,89 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software developed by the Computer Systems + * Engineering group at Lawrence Berkeley Laboratory under DARPA contract + * BG 91-66 and contributed to Berkeley. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)kvm_private.h 8.1 (Berkeley) 6/4/93 + * $FreeBSD$ + */ + +struct __kvm { + /* + * a string to be prepended to error messages + * provided for compatibility with sun's interface + * if this value is null, errors are saved in errbuf[] + */ + const char *program; + char *errp; /* XXX this can probably go away */ + char errbuf[_POSIX2_LINE_MAX]; +#define ISALIVE(kd) ((kd)->vmfd >= 0) + int pmfd; /* physical memory file (or crashdump) */ + int vmfd; /* virtual memory file (-1 if crashdump) */ + int unused; /* was: swap file (e.g., /dev/drum) */ + int nlfd; /* namelist file (e.g., /kernel) */ + struct kinfo_proc *procbase; + char *argspc; /* (dynamic) storage for argv strings */ + int arglen; /* length of the above */ + char **argv; /* (dynamic) storage for argv pointers */ + int argc; /* length of above (not actual # present) */ + char *argbuf; /* (dynamic) temporary storage */ + /* + * Kernel virtual address translation state. This only gets filled + * in for dead kernels; otherwise, the running kernel (i.e. kmem) + * will do the translations for us. It could be big, so we + * only allocate it if necessary. + */ + struct vmstate *vmst; +}; + +/* + * Functions used internally by kvm, but across kvm modules. + */ +void _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) + __printflike(3, 4); +void _kvm_freeprocs(kvm_t *kd); +void _kvm_freevtop(kvm_t *); +int _kvm_initvtop(kvm_t *); +int _kvm_kvatop(kvm_t *, u_long, off_t *); +void *_kvm_malloc(kvm_t *kd, size_t); +void *_kvm_realloc(kvm_t *kd, void *, size_t); +void _kvm_syserr (kvm_t *kd, const char *program, const char *fmt, ...) + __printflike(3, 4); +int _kvm_uvatop(kvm_t *, const struct proc *, u_long, u_long *); + +#if defined(__amd64__) || defined(__i386__) +void _kvm_minidump_freevtop(kvm_t *); +int _kvm_minidump_initvtop(kvm_t *); +int _kvm_minidump_kvatop(kvm_t *, u_long, off_t *); +#endif diff --git a/lib/libkvm/kvm_proc.c b/lib/libkvm/kvm_proc.c new file mode 100644 index 0000000..250cb44 --- /dev/null +++ b/lib/libkvm/kvm_proc.c @@ -0,0 +1,1024 @@ +/*- + * Copyright (c) 1989, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software developed by the Computer Systems + * Engineering group at Lawrence Berkeley Laboratory under DARPA contract + * BG 91-66 and contributed to Berkeley. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if 0 +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; +#endif /* LIBC_SCCS and not lint */ +#endif + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * Proc traversal interface for kvm. ps and w are (probably) the exclusive + * users of this code, so we've factored it out into a separate module. + * Thus, we keep this grunge out of the other kvm applications (i.e., + * most other applications are interested only in open/close/read/nlist). + */ + +#include <sys/param.h> +#define _WANT_UCRED /* make ucred.h give us 'struct ucred' */ +#include <sys/ucred.h> +#include <sys/queue.h> +#include <sys/_lock.h> +#include <sys/_mutex.h> +#include <sys/_task.h> +#define _WANT_PRISON /* make jail.h give us 'struct prison' */ +#include <sys/jail.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/exec.h> +#include <sys/stat.h> +#include <sys/sysent.h> +#include <sys/ioctl.h> +#include <sys/tty.h> +#include <sys/file.h> +#include <sys/conf.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <nlist.h> +#include <kvm.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> + +#include <sys/sysctl.h> + +#include <limits.h> +#include <memory.h> +#include <paths.h> + +#include "kvm_private.h" + +#define KREAD(kd, addr, obj) \ + (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) + +/* + * Read proc's from memory file into buffer bp, which has space to hold + * at most maxcnt procs. + */ +static int +kvm_proclist(kd, what, arg, p, bp, maxcnt) + kvm_t *kd; + int what, arg; + struct proc *p; + struct kinfo_proc *bp; + int maxcnt; +{ + int cnt = 0; + struct kinfo_proc kinfo_proc, *kp; + struct pgrp pgrp; + struct session sess; + struct cdev t_cdev; + struct tty tty; + struct vmspace vmspace; + struct sigacts sigacts; + struct pstats pstats; + struct ucred ucred; + struct prison pr; + struct thread mtd; + /*struct kse mke;*/ + struct ksegrp mkg; + struct proc proc; + struct proc pproc; + struct timeval tv; + struct sysentvec sysent; + char svname[KI_EMULNAMELEN]; + + kp = &kinfo_proc; + kp->ki_structsize = sizeof(kinfo_proc); + for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { + memset(kp, 0, sizeof *kp); + if (KREAD(kd, (u_long)p, &proc)) { + _kvm_err(kd, kd->program, "can't read proc at %x", p); + return (-1); + } + if (proc.p_state != PRS_ZOMBIE) { + if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads), + &mtd)) { + _kvm_err(kd, kd->program, + "can't read thread at %x", + TAILQ_FIRST(&proc.p_threads)); + return (-1); + } + if ((proc.p_flag & P_SA) == 0) { + if (KREAD(kd, + (u_long)TAILQ_FIRST(&proc.p_ksegrps), + &mkg)) { + _kvm_err(kd, kd->program, + "can't read ksegrp at %x", + TAILQ_FIRST(&proc.p_ksegrps)); + return (-1); + } +#if 0 + if (KREAD(kd, + (u_long)TAILQ_FIRST(&mkg.kg_kseq), &mke)) { + _kvm_err(kd, kd->program, + "can't read kse at %x", + TAILQ_FIRST(&mkg.kg_kseq)); + return (-1); + } +#endif + } + } + if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { + kp->ki_ruid = ucred.cr_ruid; + kp->ki_svuid = ucred.cr_svuid; + kp->ki_rgid = ucred.cr_rgid; + kp->ki_svgid = ucred.cr_svgid; + kp->ki_ngroups = ucred.cr_ngroups; + bcopy(ucred.cr_groups, kp->ki_groups, + NGROUPS * sizeof(gid_t)); + kp->ki_uid = ucred.cr_uid; + if (ucred.cr_prison != NULL) { + if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) { + _kvm_err(kd, kd->program, + "can't read prison at %x", + ucred.cr_prison); + return (-1); + } + kp->ki_jid = pr.pr_id; + } + } + + switch(what & ~KERN_PROC_INC_THREAD) { + + case KERN_PROC_GID: + if (kp->ki_groups[0] != (gid_t)arg) + continue; + break; + + case KERN_PROC_PID: + if (proc.p_pid != (pid_t)arg) + continue; + break; + + case KERN_PROC_RGID: + if (kp->ki_rgid != (gid_t)arg) + continue; + break; + + case KERN_PROC_UID: + if (kp->ki_uid != (uid_t)arg) + continue; + break; + + case KERN_PROC_RUID: + if (kp->ki_ruid != (uid_t)arg) + continue; + break; + } + /* + * We're going to add another proc to the set. If this + * will overflow the buffer, assume the reason is because + * nprocs (or the proc list) is corrupt and declare an error. + */ + if (cnt >= maxcnt) { + _kvm_err(kd, kd->program, "nprocs corrupt"); + return (-1); + } + /* + * gather kinfo_proc + */ + kp->ki_paddr = p; + kp->ki_addr = 0; /* XXX uarea */ + /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ + kp->ki_args = proc.p_args; + kp->ki_tracep = proc.p_tracevp; + kp->ki_textvp = proc.p_textvp; + kp->ki_fd = proc.p_fd; + kp->ki_vmspace = proc.p_vmspace; + if (proc.p_sigacts != NULL) { + if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { + _kvm_err(kd, kd->program, + "can't read sigacts at %x", proc.p_sigacts); + return (-1); + } + kp->ki_sigignore = sigacts.ps_sigignore; + kp->ki_sigcatch = sigacts.ps_sigcatch; + } + if ((proc.p_sflag & PS_INMEM) && proc.p_stats != NULL) { + if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { + _kvm_err(kd, kd->program, + "can't read stats at %x", proc.p_stats); + return (-1); + } + kp->ki_start = pstats.p_start; + + /* + * XXX: The times here are probably zero and need + * to be calculated from the raw data in p_rux and + * p_crux. + */ + kp->ki_rusage = pstats.p_ru; + kp->ki_childstime = pstats.p_cru.ru_stime; + kp->ki_childutime = pstats.p_cru.ru_utime; + /* Some callers want child-times in a single value */ + timeradd(&kp->ki_childstime, &kp->ki_childutime, + &kp->ki_childtime); + } + if (proc.p_oppid) + kp->ki_ppid = proc.p_oppid; + else if (proc.p_pptr) { + if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { + _kvm_err(kd, kd->program, + "can't read pproc at %x", proc.p_pptr); + return (-1); + } + kp->ki_ppid = pproc.p_pid; + } else + kp->ki_ppid = 0; + if (proc.p_pgrp == NULL) + goto nopgrp; + if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { + _kvm_err(kd, kd->program, "can't read pgrp at %x", + proc.p_pgrp); + return (-1); + } + kp->ki_pgid = pgrp.pg_id; + kp->ki_jobc = pgrp.pg_jobc; + if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { + _kvm_err(kd, kd->program, "can't read session at %x", + pgrp.pg_session); + return (-1); + } + kp->ki_sid = sess.s_sid; + (void)memcpy(kp->ki_login, sess.s_login, + sizeof(kp->ki_login)); + kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; + if (sess.s_leader == p) + kp->ki_kiflag |= KI_SLEADER; + if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { + if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { + _kvm_err(kd, kd->program, + "can't read tty at %x", sess.s_ttyp); + return (-1); + } + if (tty.t_dev != NULL) { + if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) { + _kvm_err(kd, kd->program, + "can't read cdev at %x", + tty.t_dev); + return (-1); + } +#if 0 + kp->ki_tdev = t_cdev.si_udev; +#else + kp->ki_tdev = NODEV; +#endif + } + if (tty.t_pgrp != NULL) { + if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { + _kvm_err(kd, kd->program, + "can't read tpgrp at %x", + tty.t_pgrp); + return (-1); + } + kp->ki_tpgid = pgrp.pg_id; + } else + kp->ki_tpgid = -1; + if (tty.t_session != NULL) { + if (KREAD(kd, (u_long)tty.t_session, &sess)) { + _kvm_err(kd, kd->program, + "can't read session at %x", + tty.t_session); + return (-1); + } + kp->ki_tsid = sess.s_sid; + } + } else { +nopgrp: + kp->ki_tdev = NODEV; + } + if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) + (void)kvm_read(kd, (u_long)mtd.td_wmesg, + kp->ki_wmesg, WMESGLEN); + + (void)kvm_read(kd, (u_long)proc.p_vmspace, + (char *)&vmspace, sizeof(vmspace)); + kp->ki_size = vmspace.vm_map.size; + kp->ki_rssize = vmspace.vm_swrss; /* XXX */ + kp->ki_swrss = vmspace.vm_swrss; + kp->ki_tsize = vmspace.vm_tsize; + kp->ki_dsize = vmspace.vm_dsize; + kp->ki_ssize = vmspace.vm_ssize; + + switch (what & ~KERN_PROC_INC_THREAD) { + + case KERN_PROC_PGRP: + if (kp->ki_pgid != (pid_t)arg) + continue; + break; + + case KERN_PROC_SESSION: + if (kp->ki_sid != (pid_t)arg) + continue; + break; + + case KERN_PROC_TTY: + if ((proc.p_flag & P_CONTROLT) == 0 || + kp->ki_tdev != (dev_t)arg) + continue; + break; + } + if (proc.p_comm[0] != 0) + strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); + (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, + sizeof(sysent)); + (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, + sizeof(svname)); + if (svname[0] != 0) + strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); + if ((proc.p_state != PRS_ZOMBIE) && + (mtd.td_blocked != 0)) { + kp->ki_kiflag |= KI_LOCKBLOCK; + if (mtd.td_lockname) + (void)kvm_read(kd, + (u_long)mtd.td_lockname, + kp->ki_lockname, LOCKNAMELEN); + kp->ki_lockname[LOCKNAMELEN] = 0; + } + bintime2timeval(&proc.p_rux.rux_runtime, &tv); + kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; + kp->ki_pid = proc.p_pid; + kp->ki_siglist = proc.p_siglist; + SIGSETOR(kp->ki_siglist, mtd.td_siglist); + kp->ki_sigmask = mtd.td_sigmask; + kp->ki_xstat = proc.p_xstat; + kp->ki_acflag = proc.p_acflag; + kp->ki_lock = proc.p_lock; + if (proc.p_state != PRS_ZOMBIE) { + kp->ki_swtime = proc.p_swtime; + kp->ki_flag = proc.p_flag; + kp->ki_sflag = proc.p_sflag; + kp->ki_nice = proc.p_nice; + kp->ki_traceflag = proc.p_traceflag; + if (proc.p_state == PRS_NORMAL) { + if (TD_ON_RUNQ(&mtd) || + TD_CAN_RUN(&mtd) || + TD_IS_RUNNING(&mtd)) { + kp->ki_stat = SRUN; + } else if (mtd.td_state == + TDS_INHIBITED) { + if (P_SHOULDSTOP(&proc)) { + kp->ki_stat = SSTOP; + } else if ( + TD_IS_SLEEPING(&mtd)) { + kp->ki_stat = SSLEEP; + } else if (TD_ON_LOCK(&mtd)) { + kp->ki_stat = SLOCK; + } else { + kp->ki_stat = SWAIT; + } + } + } else { + kp->ki_stat = SIDL; + } + /* Stuff from the thread */ + kp->ki_pri.pri_level = mtd.td_priority; + kp->ki_pri.pri_native = mtd.td_base_pri; + kp->ki_lastcpu = mtd.td_lastcpu; + kp->ki_wchan = mtd.td_wchan; + kp->ki_oncpu = mtd.td_oncpu; + + if (!(proc.p_flag & P_SA)) { + /* stuff from the ksegrp */ + kp->ki_slptime = mkg.kg_slptime; + kp->ki_pri.pri_class = mkg.kg_pri_class; + kp->ki_pri.pri_user = mkg.kg_user_pri; + kp->ki_estcpu = mkg.kg_estcpu; + +#if 0 + /* Stuff from the kse */ + kp->ki_pctcpu = mke.ke_pctcpu; + kp->ki_rqindex = mke.ke_rqindex; +#else + kp->ki_pctcpu = 0; + kp->ki_rqindex = 0; +#endif + } else { + kp->ki_tdflags = -1; + /* All the rest are 0 for now */ + } + } else { + kp->ki_stat = SZOMB; + } + bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); + ++bp; + ++cnt; + } + return (cnt); +} + +/* + * Build proc info array by reading in proc list from a crash dump. + * Return number of procs read. maxcnt is the max we will read. + */ +static int +kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) + kvm_t *kd; + int what, arg; + u_long a_allproc; + u_long a_zombproc; + int maxcnt; +{ + struct kinfo_proc *bp = kd->procbase; + int acnt, zcnt; + struct proc *p; + + if (KREAD(kd, a_allproc, &p)) { + _kvm_err(kd, kd->program, "cannot read allproc"); + return (-1); + } + acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); + if (acnt < 0) + return (acnt); + + if (KREAD(kd, a_zombproc, &p)) { + _kvm_err(kd, kd->program, "cannot read zombproc"); + return (-1); + } + zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); + if (zcnt < 0) + zcnt = 0; + + return (acnt + zcnt); +} + +struct kinfo_proc * +kvm_getprocs(kd, op, arg, cnt) + kvm_t *kd; + int op, arg; + int *cnt; +{ + int mib[4], st, nprocs; + size_t size; + int temp_op; + + if (kd->procbase != 0) { + free((void *)kd->procbase); + /* + * Clear this pointer in case this call fails. Otherwise, + * kvm_close() will free it again. + */ + kd->procbase = 0; + } + if (ISALIVE(kd)) { + size = 0; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = op; + mib[3] = arg; + temp_op = op & ~KERN_PROC_INC_THREAD; + st = sysctl(mib, + temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ? + 3 : 4, NULL, &size, NULL, 0); + if (st == -1) { + _kvm_syserr(kd, kd->program, "kvm_getprocs"); + return (0); + } + /* + * We can't continue with a size of 0 because we pass + * it to realloc() (via _kvm_realloc()), and passing 0 + * to realloc() results in undefined behavior. + */ + if (size == 0) { + /* + * XXX: We should probably return an invalid, + * but non-NULL, pointer here so any client + * program trying to dereference it will + * crash. However, _kvm_freeprocs() calls + * free() on kd->procbase if it isn't NULL, + * and free()'ing a junk pointer isn't good. + * Then again, _kvm_freeprocs() isn't used + * anywhere . . . + */ + kd->procbase = _kvm_malloc(kd, 1); + goto liveout; + } + do { + size += size / 10; + kd->procbase = (struct kinfo_proc *) + _kvm_realloc(kd, kd->procbase, size); + if (kd->procbase == 0) + return (0); + st = sysctl(mib, temp_op == KERN_PROC_ALL || + temp_op == KERN_PROC_PROC ? 3 : 4, + kd->procbase, &size, NULL, 0); + } while (st == -1 && errno == ENOMEM); + if (st == -1) { + _kvm_syserr(kd, kd->program, "kvm_getprocs"); + return (0); + } + /* + * We have to check the size again because sysctl() + * may "round up" oldlenp if oldp is NULL; hence it + * might've told us that there was data to get when + * there really isn't any. + */ + if (size > 0 && + kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { + _kvm_err(kd, kd->program, + "kinfo_proc size mismatch (expected %d, got %d)", + sizeof(struct kinfo_proc), + kd->procbase->ki_structsize); + return (0); + } +liveout: + nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; + } else { + struct nlist nl[4], *p; + + nl[0].n_name = "_nprocs"; + nl[1].n_name = "_allproc"; + nl[2].n_name = "_zombproc"; + nl[3].n_name = 0; + + if (kvm_nlist(kd, nl) != 0) { + for (p = nl; p->n_type != 0; ++p) + ; + _kvm_err(kd, kd->program, + "%s: no such symbol", p->n_name); + return (0); + } + if (KREAD(kd, nl[0].n_value, &nprocs)) { + _kvm_err(kd, kd->program, "can't read nprocs"); + return (0); + } + size = nprocs * sizeof(struct kinfo_proc); + kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); + if (kd->procbase == 0) + return (0); + + nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, + nl[2].n_value, nprocs); +#ifdef notdef + size = nprocs * sizeof(struct kinfo_proc); + (void)realloc(kd->procbase, size); +#endif + } + *cnt = nprocs; + return (kd->procbase); +} + +void +_kvm_freeprocs(kd) + kvm_t *kd; +{ + if (kd->procbase) { + free(kd->procbase); + kd->procbase = 0; + } +} + +void * +_kvm_realloc(kd, p, n) + kvm_t *kd; + void *p; + size_t n; +{ + void *np = (void *)realloc(p, n); + + if (np == 0) { + free(p); + _kvm_err(kd, kd->program, "out of memory"); + } + return (np); +} + +#ifndef MAX +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif + +/* + * Read in an argument vector from the user address space of process kp. + * addr if the user-space base address of narg null-terminated contiguous + * strings. This is used to read in both the command arguments and + * environment strings. Read at most maxcnt characters of strings. + */ +static char ** +kvm_argv(kd, kp, addr, narg, maxcnt) + kvm_t *kd; + struct kinfo_proc *kp; + u_long addr; + int narg; + int maxcnt; +{ + char *np, *cp, *ep, *ap; + u_long oaddr = -1; + int len, cc; + char **argv; + + /* + * Check that there aren't an unreasonable number of agruments, + * and that the address is in user space. + */ + if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) + return (0); + + /* + * kd->argv : work space for fetching the strings from the target + * process's space, and is converted for returning to caller + */ + if (kd->argv == 0) { + /* + * Try to avoid reallocs. + */ + kd->argc = MAX(narg + 1, 32); + kd->argv = (char **)_kvm_malloc(kd, kd->argc * + sizeof(*kd->argv)); + if (kd->argv == 0) + return (0); + } else if (narg + 1 > kd->argc) { + kd->argc = MAX(2 * kd->argc, narg + 1); + kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * + sizeof(*kd->argv)); + if (kd->argv == 0) + return (0); + } + /* + * kd->argspc : returned to user, this is where the kd->argv + * arrays are left pointing to the collected strings. + */ + if (kd->argspc == 0) { + kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE); + if (kd->argspc == 0) + return (0); + kd->arglen = PAGE_SIZE; + } + /* + * kd->argbuf : used to pull in pages from the target process. + * the strings are copied out of here. + */ + if (kd->argbuf == 0) { + kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE); + if (kd->argbuf == 0) + return (0); + } + + /* Pull in the target process'es argv vector */ + cc = sizeof(char *) * narg; + if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc) + return (0); + /* + * ap : saved start address of string we're working on in kd->argspc + * np : pointer to next place to write in kd->argspc + * len: length of data in kd->argspc + * argv: pointer to the argv vector that we are hunting around the + * target process space for, and converting to addresses in + * our address space (kd->argspc). + */ + ap = np = kd->argspc; + argv = kd->argv; + len = 0; + /* + * Loop over pages, filling in the argument vector. + * Note that the argv strings could be pointing *anywhere* in + * the user address space and are no longer contiguous. + * Note that *argv is modified when we are going to fetch a string + * that crosses a page boundary. We copy the next part of the string + * into to "np" and eventually convert the pointer. + */ + while (argv < kd->argv + narg && *argv != 0) { + + /* get the address that the current argv string is on */ + addr = (u_long)*argv & ~(PAGE_SIZE - 1); + + /* is it the same page as the last one? */ + if (addr != oaddr) { + if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) != + PAGE_SIZE) + return (0); + oaddr = addr; + } + + /* offset within the page... kd->argbuf */ + addr = (u_long)*argv & (PAGE_SIZE - 1); + + /* cp = start of string, cc = count of chars in this chunk */ + cp = kd->argbuf + addr; + cc = PAGE_SIZE - addr; + + /* dont get more than asked for by user process */ + if (maxcnt > 0 && cc > maxcnt - len) + cc = maxcnt - len; + + /* pointer to end of string if we found it in this page */ + ep = memchr(cp, '\0', cc); + if (ep != 0) + cc = ep - cp + 1; + /* + * at this point, cc is the count of the chars that we are + * going to retrieve this time. we may or may not have found + * the end of it. (ep points to the null if the end is known) + */ + + /* will we exceed the malloc/realloced buffer? */ + if (len + cc > kd->arglen) { + int off; + char **pp; + char *op = kd->argspc; + + kd->arglen *= 2; + kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, + kd->arglen); + if (kd->argspc == 0) + return (0); + /* + * Adjust argv pointers in case realloc moved + * the string space. + */ + off = kd->argspc - op; + for (pp = kd->argv; pp < argv; pp++) + *pp += off; + ap += off; + np += off; + } + /* np = where to put the next part of the string in kd->argspc*/ + /* np is kinda redundant.. could use "kd->argspc + len" */ + memcpy(np, cp, cc); + np += cc; /* inc counters */ + len += cc; + + /* + * if end of string found, set the *argv pointer to the + * saved beginning of string, and advance. argv points to + * somewhere in kd->argv.. This is initially relative + * to the target process, but when we close it off, we set + * it to point in our address space. + */ + if (ep != 0) { + *argv++ = ap; + ap = np; + } else { + /* update the address relative to the target process */ + *argv += cc; + } + + if (maxcnt > 0 && len >= maxcnt) { + /* + * We're stopping prematurely. Terminate the + * current string. + */ + if (ep == 0) { + *np = '\0'; + *argv++ = ap; + } + break; + } + } + /* Make sure argv is terminated. */ + *argv = 0; + return (kd->argv); +} + +static void +ps_str_a(p, addr, n) + struct ps_strings *p; + u_long *addr; + int *n; +{ + *addr = (u_long)p->ps_argvstr; + *n = p->ps_nargvstr; +} + +static void +ps_str_e(p, addr, n) + struct ps_strings *p; + u_long *addr; + int *n; +{ + *addr = (u_long)p->ps_envstr; + *n = p->ps_nenvstr; +} + +/* + * Determine if the proc indicated by p is still active. + * This test is not 100% foolproof in theory, but chances of + * being wrong are very low. + */ +static int +proc_verify(curkp) + struct kinfo_proc *curkp; +{ + struct kinfo_proc newkp; + int mib[4]; + size_t len; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = curkp->ki_pid; + len = sizeof(newkp); + if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1) + return (0); + return (curkp->ki_pid == newkp.ki_pid && + (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB)); +} + +static char ** +kvm_doargv(kd, kp, nchr, info) + kvm_t *kd; + struct kinfo_proc *kp; + int nchr; + void (*info)(struct ps_strings *, u_long *, int *); +{ + char **ap; + u_long addr; + int cnt; + static struct ps_strings arginfo; + static u_long ps_strings; + size_t len; + + if (ps_strings == 0) { + len = sizeof(ps_strings); + if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL, + 0) == -1) + ps_strings = PS_STRINGS; + } + + /* + * Pointers are stored at the top of the user stack. + */ + if (kp->ki_stat == SZOMB || + kvm_uread(kd, kp, ps_strings, (char *)&arginfo, + sizeof(arginfo)) != sizeof(arginfo)) + return (0); + + (*info)(&arginfo, &addr, &cnt); + if (cnt == 0) + return (0); + ap = kvm_argv(kd, kp, addr, cnt, nchr); + /* + * For live kernels, make sure this process didn't go away. + */ + if (ap != 0 && ISALIVE(kd) && !proc_verify(kp)) + ap = 0; + return (ap); +} + +/* + * Get the command args. This code is now machine independent. + */ +char ** +kvm_getargv(kd, kp, nchr) + kvm_t *kd; + const struct kinfo_proc *kp; + int nchr; +{ + int oid[4]; + int i; + size_t bufsz; + static unsigned long buflen; + static char *buf, *p; + static char **bufp; + static int argc; + + if (!ISALIVE(kd)) { + _kvm_err(kd, kd->program, + "cannot read user space from dead kernel"); + return (0); + } + + if (!buflen) { + bufsz = sizeof(buflen); + i = sysctlbyname("kern.ps_arg_cache_limit", + &buflen, &bufsz, NULL, 0); + if (i == -1) { + buflen = 0; + } else { + buf = malloc(buflen); + if (buf == NULL) + buflen = 0; + argc = 32; + bufp = malloc(sizeof(char *) * argc); + } + } + if (buf != NULL) { + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = kp->ki_pid; + bufsz = buflen; + i = sysctl(oid, 4, buf, &bufsz, 0, 0); + if (i == 0 && bufsz > 0) { + i = 0; + p = buf; + do { + bufp[i++] = p; + p += strlen(p) + 1; + if (i >= argc) { + argc += argc; + bufp = realloc(bufp, + sizeof(char *) * argc); + } + } while (p < buf + bufsz); + bufp[i++] = 0; + return (bufp); + } + } + if (kp->ki_flag & P_SYSTEM) + return (NULL); + return (kvm_doargv(kd, kp, nchr, ps_str_a)); +} + +char ** +kvm_getenvv(kd, kp, nchr) + kvm_t *kd; + const struct kinfo_proc *kp; + int nchr; +{ + return (kvm_doargv(kd, kp, nchr, ps_str_e)); +} + +/* + * Read from user space. The user context is given by p. + */ +ssize_t +kvm_uread(kd, kp, uva, buf, len) + kvm_t *kd; + struct kinfo_proc *kp; + u_long uva; + char *buf; + size_t len; +{ + char *cp; + char procfile[MAXPATHLEN]; + ssize_t amount; + int fd; + + if (!ISALIVE(kd)) { + _kvm_err(kd, kd->program, + "cannot read user space from dead kernel"); + return (0); + } + + sprintf(procfile, "/proc/%d/mem", kp->ki_pid); + fd = open(procfile, O_RDONLY, 0); + if (fd < 0) { + _kvm_err(kd, kd->program, "cannot open %s", procfile); + return (0); + } + + cp = buf; + while (len > 0) { + errno = 0; + if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) { + _kvm_err(kd, kd->program, "invalid address (%x) in %s", + uva, procfile); + break; + } + amount = read(fd, cp, len); + if (amount < 0) { + _kvm_syserr(kd, kd->program, "error reading %s", + procfile); + break; + } + if (amount == 0) { + _kvm_err(kd, kd->program, "EOF reading %s", procfile); + break; + } + cp += amount; + uva += amount; + len -= amount; + } + + close(fd); + return ((ssize_t)(cp - buf)); +} diff --git a/lib/libkvm/kvm_read.3 b/lib/libkvm/kvm_read.3 new file mode 100644 index 0000000..a2a2485 --- /dev/null +++ b/lib/libkvm/kvm_read.3 @@ -0,0 +1,96 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software developed by the Computer Systems +.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract +.\" BG 91-66 and contributed to Berkeley. +.\" +.\" 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_read.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt KVM_READ 3 +.Os +.Sh NAME +.Nm kvm_read , +.Nm kvm_write +.Nd read or write kernel virtual memory +.Sh LIBRARY +.Lb libkvm +.Sh SYNOPSIS +.In kvm.h +.Ft ssize_t +.Fn kvm_read "kvm_t *kd" "unsigned long addr" "void *buf" "size_t nbytes" +.Ft ssize_t +.Fn kvm_write "kvm_t *kd" "unsigned long addr" "const void *buf" "size_t nbytes" +.Sh DESCRIPTION +The +.Fn kvm_read +and +.Fn kvm_write +functions are used to read and write kernel virtual memory (or a crash +dump file). +See +.Fn kvm_open 3 +or +.Fn kvm_openfiles 3 +for information regarding opening kernel virtual memory and crash dumps. +.Pp +The +.Fn kvm_read +function transfers +.Fa nbytes +bytes of data from +the kernel space address +.Fa addr +to +.Fa buf . +Conversely, +.Fn kvm_write +transfers data from +.Fa buf +to +.Fa addr . +Unlike their SunOS counterparts, these functions cannot be used to +read or write process address spaces. +.Sh RETURN VALUES +Upon success, the number of bytes actually transferred is returned. +Otherwise, -1 is returned. +.Sh SEE ALSO +.Xr kvm 3 , +.Xr kvm_close 3 , +.Xr kvm_getargv 3 , +.Xr kvm_getenvv 3 , +.Xr kvm_geterr 3 , +.Xr kvm_getprocs 3 , +.Xr kvm_nlist 3 , +.Xr kvm_open 3 , +.Xr kvm_openfiles 3 diff --git a/lib/libkvm/kvm_sparc.c b/lib/libkvm/kvm_sparc.c new file mode 100644 index 0000000..57b881d --- /dev/null +++ b/lib/libkvm/kvm_sparc.c @@ -0,0 +1,240 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software developed by the Computer Systems + * Engineering group at Lawrence Berkeley Laboratory under DARPA contract + * BG 91-66 and contributed to Berkeley. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93"; +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Sparc machine dependent routines for kvm. Hopefully, the forthcoming + * vm code will one day obsolete this module. + */ + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <unistd.h> +#include <nlist.h> +#include <kvm.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> + +#include <limits.h> + +#include "kvm_private.h" + +#define NPMEG 128 + +/* XXX from sparc/pmap.c */ +#define MAXMEM (128 * 1024 * 1024) /* no more than 128 MB phys mem */ +#define NPGBANK 16 /* 2^4 pages per bank (64K / bank) */ +#define BSHIFT 4 /* log2(NPGBANK) */ +#define BOFFSET (NPGBANK - 1) +#define BTSIZE (MAXMEM / NBPG / NPGBANK) +#define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET)) + +struct vmstate { + pmeg_t segmap[NKSEG]; + int pmeg[NPMEG][NPTESG]; + int pmap_stod[BTSIZE]; /* dense to sparse */ +}; + +void +_kvm_freevtop(kd) + kvm_t *kd; +{ + if (kd->vmst != 0) + free(kd->vmst); +} + +int +_kvm_initvtop(kd) + kvm_t *kd; +{ + int i; + int off; + struct vmstate *vm; + struct stat st; + struct nlist nlist[2]; + + vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); + if (vm == 0) + return (-1); + + kd->vmst = vm; + + if (fstat(kd->pmfd, &st) < 0) + return (-1); + /* + * Read segment table. + */ + off = st.st_size - ctob(btoc(sizeof(vm->segmap))); + errno = 0; + if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || + read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) { + _kvm_err(kd, kd->program, "cannot read segment map"); + return (-1); + } + /* + * Read PMEGs. + */ + off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) + + btoc(sizeof(vm->segmap))); + errno = 0; + if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || + read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) { + _kvm_err(kd, kd->program, "cannot read PMEG table"); + return (-1); + } + /* + * Make pmap_stod be an identity map so we can bootstrap it in. + * We assume it's in the first contiguous chunk of physical memory. + */ + for (i = 0; i < BTSIZE; ++i) + vm->pmap_stod[i] = i << 4; + + /* + * It's okay to do this nlist separately from the one kvm_getprocs() + * does, since the only time we could gain anything by combining + * them is if we do a kvm_getprocs() on a dead kernel, which is + * not too common. + */ + nlist[0].n_name = "_pmap_stod"; + nlist[1].n_name = 0; + if (kvm_nlist(kd, nlist) != 0) { + _kvm_err(kd, kd->program, "pmap_stod: no such symbol"); + return (-1); + } + if (kvm_read(kd, (u_long)nlist[0].n_value, + (char *)vm->pmap_stod, sizeof(vm->pmap_stod)) + != sizeof(vm->pmap_stod)) { + _kvm_err(kd, kd->program, "cannot read pmap_stod"); + return (-1); + } + return (0); +} + +#define VA_OFF(va) (va & (NBPG - 1)) + +/* + * Translate a user virtual address to a physical address. + */ +int +_kvm_uvatop(kd, p, va, pa) + kvm_t *kd; + const struct proc *p; + u_long va; + u_long *pa; +{ + int kva, pte; + int off, frame; + struct vmspace *vms = p->p_vmspace; + + if ((u_long)vms < KERNBASE) { + _kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc"); + return (0); + } + if (va >= KERNBASE) + return (0); + /* + * Get the PTE. This takes two steps. We read the + * base address of the table, then we index it. + * Note that the index pte table is indexed by + * virtual segment rather than physical segment. + */ + kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)]; + if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0) + goto invalid; + kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va); + if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) { + off = VA_OFF(va); + /* + * /dev/mem adheres to the hardware model of physical memory + * (with holes in the address space), while crashdumps + * adhere to the contiguous software model. + */ + if (ISALIVE(kd)) + frame = pte & PG_PFNUM; + else + frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM); + *pa = (frame << PGSHIFT) | off; + return (NBPG - off); + } +invalid: + _kvm_err(kd, 0, "invalid address (%x)", va); + return (0); +} + +/* + * Translate a kernel virtual address to a physical address using the + * mapping information in kd->vm. Returns the result in pa, and returns + * the number of bytes that are contiguously available from this + * physical address. This routine is used only for crashdumps. + */ +int +_kvm_kvatop(kd, va, pa) + kvm_t *kd; + u_long va; + uint64_t *pa; +{ + struct vmstate *vm; + int s; + int pte; + int off; + + if (va >= KERNBASE) { + vm = kd->vmst; + s = vm->segmap[VA_VSEG(va) - NUSEG]; + pte = vm->pmeg[s][VA_VPG(va)]; + if ((pte & PG_V) != 0) { + off = VA_OFF(va); + *pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM) + << PGSHIFT) | off; + + return (NBPG - off); + } + } + _kvm_err(kd, 0, "invalid address (%x)", va); + return (0); +} diff --git a/lib/libkvm/kvm_sparc64.c b/lib/libkvm/kvm_sparc64.c new file mode 100644 index 0000000..6e0cd37 --- /dev/null +++ b/lib/libkvm/kvm_sparc64.c @@ -0,0 +1,225 @@ +/*- + * Copyright (c) 1989, 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software developed by the Computer Systems + * Engineering group at Lawrence Berkeley Laboratory under DARPA contract + * BG 91-66 and contributed to Berkeley. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * from: FreeBSD: src/lib/libkvm/kvm_i386.c,v 1.15 2001/10/10 17:48:43 + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * sparc64 machine dependent routines for kvm. + */ + +#include <sys/param.h> +#include <sys/user.h> +#include <sys/proc.h> +#include <sys/stat.h> +#include <stdlib.h> +#include <unistd.h> +#include <nlist.h> +#include <kvm.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> + +#include <machine/kerneldump.h> +#include <machine/tte.h> +#include <machine/tlb.h> +#include <machine/tsb.h> + +#include <limits.h> + +#include "kvm_private.h" + +#ifndef btop +#define btop(x) (sparc64_btop(x)) +#define ptob(x) (sparc64_ptob(x)) +#endif + +struct vmstate { + off_t vm_tsb_off; + vm_size_t vm_tsb_mask; + int vm_nregions; + struct sparc64_dump_reg *vm_regions; +}; + +void +_kvm_freevtop(kvm_t *kd) +{ + if (kd->vmst != 0) { + free(kd->vmst->vm_regions); + free(kd->vmst); + } +} + +static int +_kvm_read_phys(kvm_t *kd, off_t pos, void *buf, size_t size) +{ + + /* XXX This has to be a raw file read, kvm_read is virtual. */ + if (lseek(kd->pmfd, pos, SEEK_SET) == -1) { + _kvm_syserr(kd, kd->program, "_kvm_read_phys: lseek"); + return (0); + } + if (read(kd->pmfd, buf, size) != size) { + _kvm_syserr(kd, kd->program, "_kvm_read_phys: read"); + return (0); + } + return (1); +} + +static int +_kvm_reg_cmp(const void *a, const void *b) +{ + const struct sparc64_dump_reg *ra, *rb; + + ra = a; + rb = b; + if (ra->dr_pa < rb->dr_pa) + return (-1); + else if (ra->dr_pa >= rb->dr_pa + rb->dr_size) + return (1); + else + return (0); +} + +#define KVM_OFF_NOTFOUND 0 + +static off_t +_kvm_find_off(struct vmstate *vm, vm_offset_t pa, vm_size_t size) +{ + struct sparc64_dump_reg *reg, key; + vm_offset_t o; + + key.dr_pa = pa; + reg = bsearch(&key, vm->vm_regions, vm->vm_nregions, + sizeof(*vm->vm_regions), _kvm_reg_cmp); + if (reg == NULL) + return (KVM_OFF_NOTFOUND); + o = pa - reg->dr_pa; + if (o + size > reg->dr_size) + return (KVM_OFF_NOTFOUND); + return (reg->dr_offs + o); +} + +int +_kvm_initvtop(kvm_t *kd) +{ + struct sparc64_dump_hdr hdr; + struct sparc64_dump_reg *regs; + struct vmstate *vm; + size_t regsz; + vm_offset_t pa; + vm_size_t mask; + + vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); + if (vm == NULL) { + _kvm_err(kd, kd->program, "cannot allocate vm"); + return (-1); + } + kd->vmst = vm; + + if (!_kvm_read_phys(kd, 0, &hdr, sizeof(hdr))) + goto fail_vm; + pa = hdr.dh_tsb_pa; + + regsz = hdr.dh_nregions * sizeof(*regs); + regs = _kvm_malloc(kd, regsz); + if (regs == NULL) { + _kvm_err(kd, kd->program, "cannot allocate regions"); + goto fail_vm; + } + if (!_kvm_read_phys(kd, sizeof(hdr), regs, regsz)) + goto fail_regs; + qsort(regs, hdr.dh_nregions, sizeof(*regs), _kvm_reg_cmp); + + vm->vm_tsb_mask = hdr.dh_tsb_mask; + vm->vm_regions = regs; + vm->vm_nregions = hdr.dh_nregions; + vm->vm_tsb_off = _kvm_find_off(vm, hdr.dh_tsb_pa, hdr.dh_tsb_size); + if (vm->vm_tsb_off == KVM_OFF_NOTFOUND) { + _kvm_err(kd, kd->program, "tsb not found in dump"); + goto fail_regs; + } + return (0); + +fail_regs: + free(regs); +fail_vm: + free(vm); + return (-1); +} + +int +_kvm_kvatop(kvm_t *kd, u_long va, off_t *pa) +{ + struct vmstate *vm; + struct tte tte; + off_t tte_off, pa_off; + u_long pg_off, vpn; + int rest; + + pg_off = va & PAGE_MASK; + if (va >= VM_MIN_DIRECT_ADDRESS) + pa_off = TLB_DIRECT_TO_PHYS(va) & ~PAGE_MASK; + else { + vpn = btop(va); + tte_off = kd->vmst->vm_tsb_off + + ((vpn & kd->vmst->vm_tsb_mask) << TTE_SHIFT); + if (!_kvm_read_phys(kd, tte_off, &tte, sizeof(tte))) + goto invalid; + if (!tte_match(&tte, va)) + goto invalid; + pa_off = TTE_GET_PA(&tte); + } + rest = PAGE_SIZE - pg_off; + pa_off = _kvm_find_off(kd->vmst, pa_off, rest); + if (pa_off == KVM_OFF_NOTFOUND) + goto invalid; + *pa = pa_off + pg_off; + return (rest); + +invalid: + _kvm_err(kd, 0, "invalid address (%x)", va); + return (0); +} |