Home | History | Annotate | Line # | Download | only in libkvm
kvm.c revision 1.75
      1  1.75  drochner /*	$NetBSD: kvm.c,v 1.75 2002/09/17 19:50:48 drochner Exp $	*/
      2  1.42   thorpej 
      3   1.1       cgd /*-
      4  1.35       cgd  * Copyright (c) 1989, 1992, 1993
      5  1.35       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.35       cgd  *
      7  1.35       cgd  * This code is derived from software developed by the Computer Systems
      8  1.35       cgd  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
      9  1.35       cgd  * BG 91-66 and contributed to Berkeley.
     10   1.1       cgd  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1       cgd  *    must display the following acknowledgement:
     21   1.1       cgd  *	This product includes software developed by the University of
     22   1.1       cgd  *	California, Berkeley and its contributors.
     23   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       cgd  *    may be used to endorse or promote products derived from this software
     25   1.1       cgd  *    without specific prior written permission.
     26   1.1       cgd  *
     27   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       cgd  * SUCH DAMAGE.
     38   1.1       cgd  */
     39   1.1       cgd 
     40  1.53     mikel #include <sys/cdefs.h>
     41   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     42  1.42   thorpej #if 0
     43  1.35       cgd static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
     44  1.42   thorpej #else
     45  1.75  drochner __RCSID("$NetBSD: kvm.c,v 1.75 2002/09/17 19:50:48 drochner Exp $");
     46  1.42   thorpej #endif
     47   1.1       cgd #endif /* LIBC_SCCS and not lint */
     48   1.1       cgd 
     49   1.1       cgd #include <sys/param.h>
     50   1.1       cgd #include <sys/user.h>
     51   1.1       cgd #include <sys/proc.h>
     52   1.1       cgd #include <sys/ioctl.h>
     53  1.35       cgd #include <sys/stat.h>
     54  1.35       cgd #include <sys/sysctl.h>
     55  1.35       cgd 
     56  1.40       leo #include <sys/core.h>
     57  1.40       leo #include <sys/exec_aout.h>
     58  1.40       leo #include <sys/kcore.h>
     59  1.40       leo 
     60  1.67       mrg #include <uvm/uvm_extern.h>
     61  1.35       cgd 
     62  1.35       cgd #include <ctype.h>
     63  1.35       cgd #include <db.h>
     64   1.1       cgd #include <fcntl.h>
     65   1.1       cgd #include <limits.h>
     66  1.35       cgd #include <nlist.h>
     67   1.1       cgd #include <paths.h>
     68  1.71       wiz #include <stdarg.h>
     69   1.1       cgd #include <stdio.h>
     70  1.35       cgd #include <stdlib.h>
     71   1.1       cgd #include <string.h>
     72  1.35       cgd #include <unistd.h>
     73  1.41       leo #include <kvm.h>
     74   1.1       cgd 
     75  1.35       cgd #include "kvm_private.h"
     76   1.1       cgd 
     77  1.48       cgd static int	kvm_dbopen __P((kvm_t *));
     78  1.40       leo static int	_kvm_get_header __P((kvm_t *));
     79  1.39       cgd static kvm_t	*_kvm_open __P((kvm_t *, const char *, const char *,
     80  1.39       cgd 		    const char *, int, char *));
     81  1.40       leo static int	clear_gap __P((kvm_t *, FILE *, int));
     82  1.73  christos static int	open_cloexec  __P((const char *, int, int));
     83  1.40       leo static off_t	Lseek __P((kvm_t *, int, off_t, int));
     84  1.58   thorpej static ssize_t	Pread __P((kvm_t *, int, void *, size_t, off_t));
     85  1.19   mycroft 
     86  1.35       cgd char *
     87  1.35       cgd kvm_geterr(kd)
     88  1.35       cgd 	kvm_t *kd;
     89  1.35       cgd {
     90  1.35       cgd 	return (kd->errbuf);
     91  1.35       cgd }
     92   1.1       cgd 
     93  1.35       cgd /*
     94  1.35       cgd  * Report an error using printf style arguments.  "program" is kd->program
     95  1.35       cgd  * on hard errors, and 0 on soft errors, so that under sun error emulation,
     96  1.35       cgd  * only hard errors are printed out (otherwise, programs like gdb will
     97  1.35       cgd  * generate tons of error messages when trying to access bogus pointers).
     98  1.35       cgd  */
     99  1.35       cgd void
    100  1.35       cgd _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
    101  1.35       cgd {
    102  1.35       cgd 	va_list ap;
    103  1.27      phil 
    104  1.35       cgd 	va_start(ap, fmt);
    105  1.35       cgd 	if (program != NULL) {
    106  1.35       cgd 		(void)fprintf(stderr, "%s: ", program);
    107  1.35       cgd 		(void)vfprintf(stderr, fmt, ap);
    108  1.35       cgd 		(void)fputc('\n', stderr);
    109  1.35       cgd 	} else
    110  1.35       cgd 		(void)vsnprintf(kd->errbuf,
    111  1.60   thorpej 		    sizeof(kd->errbuf), fmt, ap);
    112  1.26        pk 
    113  1.35       cgd 	va_end(ap);
    114  1.35       cgd }
    115  1.26        pk 
    116  1.35       cgd void
    117  1.35       cgd _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
    118  1.35       cgd {
    119  1.35       cgd 	va_list ap;
    120  1.60   thorpej 	size_t n;
    121  1.26        pk 
    122  1.35       cgd 	va_start(ap, fmt);
    123  1.35       cgd 	if (program != NULL) {
    124  1.35       cgd 		(void)fprintf(stderr, "%s: ", program);
    125  1.35       cgd 		(void)vfprintf(stderr, fmt, ap);
    126  1.35       cgd 		(void)fprintf(stderr, ": %s\n", strerror(errno));
    127  1.35       cgd 	} else {
    128  1.55     perry 		char *cp = kd->errbuf;
    129  1.26        pk 
    130  1.60   thorpej 		(void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
    131  1.35       cgd 		n = strlen(cp);
    132  1.35       cgd 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
    133  1.35       cgd 		    strerror(errno));
    134  1.35       cgd 	}
    135  1.35       cgd 	va_end(ap);
    136  1.35       cgd }
    137   1.1       cgd 
    138  1.35       cgd void *
    139  1.35       cgd _kvm_malloc(kd, n)
    140  1.55     perry 	kvm_t *kd;
    141  1.55     perry 	size_t n;
    142  1.35       cgd {
    143  1.35       cgd 	void *p;
    144  1.35       cgd 
    145  1.35       cgd 	if ((p = malloc(n)) == NULL)
    146  1.68  sommerfe 		_kvm_err(kd, kd->program, "%s", strerror(errno));
    147  1.35       cgd 	return (p);
    148  1.35       cgd }
    149  1.35       cgd 
    150  1.40       leo /*
    151  1.73  christos  * Open a file setting the close on exec bit.
    152  1.73  christos  */
    153  1.73  christos static int
    154  1.73  christos open_cloexec(fname, flags, mode)
    155  1.73  christos 	const char *fname;
    156  1.73  christos 	int flags, mode;
    157  1.73  christos {
    158  1.73  christos 	int fd;
    159  1.73  christos 
    160  1.73  christos 	if ((fd = open(fname, flags, mode)) == -1)
    161  1.73  christos 		return fd;
    162  1.74  drochner 	if (fcntl(fd, F_SETFD, (void *)1) == -1)
    163  1.73  christos 		goto error;
    164  1.73  christos 
    165  1.73  christos 	return fd;
    166  1.73  christos error:
    167  1.73  christos 	flags = errno;
    168  1.73  christos 	(void)close(fd);
    169  1.73  christos 	errno = flags;
    170  1.73  christos 	return -1;
    171  1.73  christos }
    172  1.73  christos 
    173  1.73  christos /*
    174  1.58   thorpej  * Wrapper around the lseek(2) system call; calls _kvm_syserr() for us
    175  1.58   thorpej  * in the event of emergency.
    176  1.40       leo  */
    177  1.40       leo static off_t
    178  1.40       leo Lseek(kd, fd, offset, whence)
    179  1.58   thorpej 	kvm_t *kd;
    180  1.58   thorpej 	int fd;
    181  1.58   thorpej 	off_t offset;
    182  1.58   thorpej 	int whence;
    183  1.40       leo {
    184  1.58   thorpej 	off_t off;
    185  1.40       leo 
    186  1.40       leo 	errno = 0;
    187  1.58   thorpej 
    188  1.40       leo 	if ((off = lseek(fd, offset, whence)) == -1 && errno != 0) {
    189  1.40       leo 		_kvm_syserr(kd, kd->program, "Lseek");
    190  1.58   thorpej 		return ((off_t)-1);
    191  1.40       leo 	}
    192  1.40       leo 	return (off);
    193  1.40       leo }
    194  1.40       leo 
    195  1.58   thorpej /*
    196  1.58   thorpej  * Wrapper around the pread(2) system call; calls _kvm_syserr() for us
    197  1.58   thorpej  * in the event of emergency.
    198  1.58   thorpej  */
    199  1.40       leo static ssize_t
    200  1.58   thorpej Pread(kd, fd, buf, nbytes, offset)
    201  1.58   thorpej 	kvm_t *kd;
    202  1.58   thorpej 	int fd;
    203  1.58   thorpej 	void *buf;
    204  1.58   thorpej 	size_t nbytes;
    205  1.58   thorpej 	off_t offset;
    206  1.40       leo {
    207  1.58   thorpej 	ssize_t rv;
    208  1.40       leo 
    209  1.40       leo 	errno = 0;
    210  1.40       leo 
    211  1.58   thorpej 	if ((rv = pread(fd, buf, nbytes, offset)) != nbytes &&
    212  1.58   thorpej 	    errno != 0)
    213  1.58   thorpej 		_kvm_syserr(kd, kd->program, "Pread");
    214  1.40       leo 	return (rv);
    215  1.40       leo }
    216  1.40       leo 
    217  1.35       cgd static kvm_t *
    218  1.35       cgd _kvm_open(kd, uf, mf, sf, flag, errout)
    219  1.55     perry 	kvm_t *kd;
    220  1.35       cgd 	const char *uf;
    221  1.35       cgd 	const char *mf;
    222  1.35       cgd 	const char *sf;
    223  1.35       cgd 	int flag;
    224  1.35       cgd 	char *errout;
    225  1.35       cgd {
    226  1.35       cgd 	struct stat st;
    227  1.48       cgd 	int ufgiven;
    228  1.35       cgd 
    229  1.37   mycroft 	kd->db = 0;
    230  1.37   mycroft 	kd->pmfd = -1;
    231  1.35       cgd 	kd->vmfd = -1;
    232  1.35       cgd 	kd->swfd = -1;
    233  1.35       cgd 	kd->nlfd = -1;
    234  1.65    simonb 	kd->alive = KVM_ALIVE_DEAD;
    235  1.35       cgd 	kd->procbase = 0;
    236  1.65    simonb 	kd->procbase2 = 0;
    237  1.36   mycroft 	kd->nbpg = getpagesize();
    238  1.36   mycroft 	kd->swapspc = 0;
    239  1.35       cgd 	kd->argspc = 0;
    240  1.69   msaitoh 	kd->arglen = 0;
    241  1.38   mycroft 	kd->argbuf = 0;
    242  1.35       cgd 	kd->argv = 0;
    243  1.37   mycroft 	kd->vmst = 0;
    244  1.37   mycroft 	kd->vm_page_buckets = 0;
    245  1.40       leo 	kd->kcore_hdr = 0;
    246  1.43       gwr 	kd->cpu_dsize = 0;
    247  1.43       gwr 	kd->cpu_data = 0;
    248  1.40       leo 	kd->dump_off = 0;
    249  1.52       gwr 
    250  1.65    simonb 	if (flag & KVM_NO_FILES) {
    251  1.65    simonb 		kd->alive = KVM_ALIVE_SYSCTL;
    252  1.65    simonb 		return(kd);
    253  1.65    simonb 	}
    254  1.65    simonb 
    255  1.52       gwr 	/*
    256  1.52       gwr 	 * Call the MD open hook.  This sets:
    257  1.52       gwr 	 *	usrstack, min_uva, max_uva
    258  1.52       gwr 	 */
    259  1.52       gwr 	if (_kvm_mdopen(kd)) {
    260  1.52       gwr 		_kvm_err(kd, kd->program, "md init failed");
    261  1.52       gwr 		goto failed;
    262  1.52       gwr 	}
    263  1.35       cgd 
    264  1.48       cgd 	ufgiven = (uf != NULL);
    265  1.48       cgd 	if (!ufgiven)
    266  1.35       cgd 		uf = _PATH_UNIX;
    267  1.35       cgd 	else if (strlen(uf) >= MAXPATHLEN) {
    268  1.35       cgd 		_kvm_err(kd, kd->program, "exec file name too long");
    269   1.1       cgd 		goto failed;
    270   1.1       cgd 	}
    271  1.35       cgd 	if (flag & ~O_RDWR) {
    272  1.35       cgd 		_kvm_err(kd, kd->program, "bad flags arg");
    273   1.1       cgd 		goto failed;
    274   1.1       cgd 	}
    275  1.35       cgd 	if (mf == 0)
    276  1.35       cgd 		mf = _PATH_MEM;
    277  1.35       cgd 	if (sf == 0)
    278  1.35       cgd 		sf = _PATH_DRUM;
    279  1.35       cgd 
    280  1.73  christos 	if ((kd->pmfd = open_cloexec(mf, flag, 0)) < 0) {
    281  1.35       cgd 		_kvm_syserr(kd, kd->program, "%s", mf);
    282  1.35       cgd 		goto failed;
    283   1.1       cgd 	}
    284  1.35       cgd 	if (fstat(kd->pmfd, &st) < 0) {
    285  1.35       cgd 		_kvm_syserr(kd, kd->program, "%s", mf);
    286   1.1       cgd 		goto failed;
    287   1.1       cgd 	}
    288  1.35       cgd 	if (S_ISCHR(st.st_mode)) {
    289   1.1       cgd 		/*
    290  1.35       cgd 		 * If this is a character special device, then check that
    291  1.35       cgd 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
    292  1.35       cgd 		 * make it work for either /dev/mem or /dev/kmem -- in either
    293  1.35       cgd 		 * case you're working with a live kernel.)
    294   1.1       cgd 		 */
    295  1.35       cgd 		if (strcmp(mf, _PATH_MEM) != 0) {	/* XXX */
    296  1.35       cgd 			_kvm_err(kd, kd->program,
    297  1.35       cgd 				 "%s: not physical memory device", mf);
    298  1.35       cgd 			goto failed;
    299   1.1       cgd 		}
    300  1.73  christos 		if ((kd->vmfd = open_cloexec(_PATH_KMEM, flag, 0)) < 0) {
    301  1.35       cgd 			_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
    302  1.35       cgd 			goto failed;
    303   1.1       cgd 		}
    304  1.65    simonb 		kd->alive = KVM_ALIVE_FILES;
    305  1.73  christos 		if ((kd->swfd = open_cloexec(sf, flag, 0)) < 0) {
    306  1.35       cgd 			_kvm_syserr(kd, kd->program, "%s", sf);
    307  1.35       cgd 			goto failed;
    308   1.1       cgd 		}
    309   1.1       cgd 		/*
    310  1.48       cgd 		 * Open kvm nlist database.  We only try to use
    311  1.48       cgd 		 * the pre-built database if the namelist file name
    312  1.48       cgd 		 * pointer is NULL.  If the database cannot or should
    313  1.48       cgd 		 * not be opened, open the namelist argument so we
    314  1.48       cgd 		 * revert to slow nlist() calls.
    315   1.1       cgd 		 */
    316  1.64    simonb 		if ((ufgiven || kvm_dbopen(kd) < 0) &&
    317  1.73  christos 		    (kd->nlfd = open_cloexec(uf, O_RDONLY, 0)) < 0) {
    318  1.35       cgd 			_kvm_syserr(kd, kd->program, "%s", uf);
    319  1.35       cgd 			goto failed;
    320   1.3       cgd 		}
    321  1.35       cgd 	} else {
    322   1.3       cgd 		/*
    323  1.35       cgd 		 * This is a crash dump.
    324  1.70       wiz 		 * Initialize the virtual address translation machinery,
    325  1.35       cgd 		 * but first setup the namelist fd.
    326   1.3       cgd 		 */
    327  1.73  christos 		if ((kd->nlfd = open_cloexec(uf, O_RDONLY, 0)) < 0) {
    328  1.35       cgd 			_kvm_syserr(kd, kd->program, "%s", uf);
    329  1.35       cgd 			goto failed;
    330   1.3       cgd 		}
    331  1.40       leo 
    332  1.40       leo 		/*
    333  1.40       leo 		 * If there is no valid core header, fail silently here.
    334  1.64    simonb 		 * The address translations however will fail without
    335  1.40       leo 		 * header. Things can be made to run by calling
    336  1.40       leo 		 * kvm_dump_mkheader() before doing any translation.
    337  1.40       leo 		 */
    338  1.40       leo 		if (_kvm_get_header(kd) == 0) {
    339  1.40       leo 			if (_kvm_initvtop(kd) < 0)
    340  1.40       leo 				goto failed;
    341  1.40       leo 		}
    342   1.3       cgd 	}
    343  1.35       cgd 	return (kd);
    344  1.35       cgd failed:
    345   1.1       cgd 	/*
    346  1.35       cgd 	 * Copy out the error if doing sane error semantics.
    347   1.1       cgd 	 */
    348  1.35       cgd 	if (errout != 0)
    349  1.50       mrg 		(void)strncpy(errout, kd->errbuf, _POSIX2_LINE_MAX - 1);
    350  1.35       cgd 	(void)kvm_close(kd);
    351  1.35       cgd 	return (0);
    352   1.1       cgd }
    353   1.1       cgd 
    354  1.43       gwr /*
    355  1.43       gwr  * The kernel dump file (from savecore) contains:
    356  1.43       gwr  *    kcore_hdr_t kcore_hdr;
    357  1.43       gwr  *    kcore_seg_t cpu_hdr;
    358  1.43       gwr  *    (opaque)    cpu_data; (size is cpu_hdr.c_size)
    359  1.43       gwr  *	  kcore_seg_t mem_hdr;
    360  1.43       gwr  *    (memory)    mem_data; (size is mem_hdr.c_size)
    361  1.64    simonb  *
    362  1.43       gwr  * Note: khdr is padded to khdr.c_hdrsize;
    363  1.43       gwr  * cpu_hdr and mem_hdr are padded to khdr.c_seghdrsize
    364  1.43       gwr  */
    365  1.40       leo static int
    366  1.40       leo _kvm_get_header(kd)
    367  1.43       gwr 	kvm_t	*kd;
    368  1.40       leo {
    369  1.43       gwr 	kcore_hdr_t	kcore_hdr;
    370  1.43       gwr 	kcore_seg_t	cpu_hdr;
    371  1.43       gwr 	kcore_seg_t	mem_hdr;
    372  1.43       gwr 	size_t		offset;
    373  1.43       gwr 	ssize_t		sz;
    374  1.40       leo 
    375  1.43       gwr 	/*
    376  1.43       gwr 	 * Read the kcore_hdr_t
    377  1.43       gwr 	 */
    378  1.58   thorpej 	sz = Pread(kd, kd->pmfd, &kcore_hdr, sizeof(kcore_hdr), (off_t)0);
    379  1.43       gwr 	if (sz != sizeof(kcore_hdr))
    380  1.40       leo 		return (-1);
    381  1.40       leo 
    382  1.40       leo 	/*
    383  1.40       leo 	 * Currently, we only support dump-files made by the current
    384  1.40       leo 	 * architecture...
    385  1.40       leo 	 */
    386  1.43       gwr 	if ((CORE_GETMAGIC(kcore_hdr) != KCORE_MAGIC) ||
    387  1.43       gwr 	    (CORE_GETMID(kcore_hdr) != MID_MACHINE))
    388  1.43       gwr 		return (-1);
    389  1.40       leo 
    390  1.40       leo 	/*
    391  1.40       leo 	 * Currently, we only support exactly 2 segments: cpu-segment
    392  1.40       leo 	 * and data-segment in exactly that order.
    393  1.40       leo 	 */
    394  1.43       gwr 	if (kcore_hdr.c_nseg != 2)
    395  1.40       leo 		return (-1);
    396  1.40       leo 
    397  1.40       leo 	/*
    398  1.43       gwr 	 * Save away the kcore_hdr.  All errors after this
    399  1.43       gwr 	 * should do a to "goto fail" to deallocate things.
    400  1.43       gwr 	 */
    401  1.43       gwr 	kd->kcore_hdr = _kvm_malloc(kd, sizeof(kcore_hdr));
    402  1.43       gwr 	memcpy(kd->kcore_hdr, &kcore_hdr, sizeof(kcore_hdr));
    403  1.43       gwr 	offset = kcore_hdr.c_hdrsize;
    404  1.43       gwr 
    405  1.43       gwr 	/*
    406  1.43       gwr 	 * Read the CPU segment header
    407  1.40       leo 	 */
    408  1.58   thorpej 	sz = Pread(kd, kd->pmfd, &cpu_hdr, sizeof(cpu_hdr), (off_t)offset);
    409  1.43       gwr 	if (sz != sizeof(cpu_hdr))
    410  1.43       gwr 		goto fail;
    411  1.43       gwr 	if ((CORE_GETMAGIC(cpu_hdr) != KCORESEG_MAGIC) ||
    412  1.43       gwr 	    (CORE_GETFLAG(cpu_hdr) != CORE_CPU))
    413  1.43       gwr 		goto fail;
    414  1.43       gwr 	offset += kcore_hdr.c_seghdrsize;
    415  1.43       gwr 
    416  1.43       gwr 	/*
    417  1.43       gwr 	 * Read the CPU segment DATA.
    418  1.43       gwr 	 */
    419  1.43       gwr 	kd->cpu_dsize = cpu_hdr.c_size;
    420  1.43       gwr 	kd->cpu_data = _kvm_malloc(kd, cpu_hdr.c_size);
    421  1.43       gwr 	if (kd->cpu_data == NULL)
    422  1.43       gwr 		goto fail;
    423  1.58   thorpej 	sz = Pread(kd, kd->pmfd, kd->cpu_data, cpu_hdr.c_size, (off_t)offset);
    424  1.43       gwr 	if (sz != cpu_hdr.c_size)
    425  1.43       gwr 		goto fail;
    426  1.43       gwr 	offset += cpu_hdr.c_size;
    427  1.40       leo 
    428  1.40       leo 	/*
    429  1.40       leo 	 * Read the next segment header: data segment
    430  1.40       leo 	 */
    431  1.58   thorpej 	sz = Pread(kd, kd->pmfd, &mem_hdr, sizeof(mem_hdr), (off_t)offset);
    432  1.43       gwr 	if (sz != sizeof(mem_hdr))
    433  1.43       gwr 		goto fail;
    434  1.43       gwr 	offset += kcore_hdr.c_seghdrsize;
    435  1.43       gwr 
    436  1.43       gwr 	if ((CORE_GETMAGIC(mem_hdr) != KCORESEG_MAGIC) ||
    437  1.43       gwr 	    (CORE_GETFLAG(mem_hdr) != CORE_DATA))
    438  1.43       gwr 		goto fail;
    439  1.40       leo 
    440  1.43       gwr 	kd->dump_off = offset;
    441  1.43       gwr 	return (0);
    442  1.40       leo 
    443  1.43       gwr fail:
    444  1.43       gwr 	if (kd->kcore_hdr != NULL) {
    445  1.43       gwr 		free(kd->kcore_hdr);
    446  1.40       leo 		kd->kcore_hdr = NULL;
    447  1.43       gwr 	}
    448  1.43       gwr 	if (kd->cpu_data != NULL) {
    449  1.43       gwr 		free(kd->cpu_data);
    450  1.43       gwr 		kd->cpu_data = NULL;
    451  1.43       gwr 		kd->cpu_dsize = 0;
    452  1.40       leo 	}
    453  1.54       mrg 	return (-1);
    454  1.40       leo }
    455  1.40       leo 
    456  1.40       leo /*
    457  1.43       gwr  * The format while on the dump device is: (new format)
    458  1.47       cgd  *	kcore_seg_t cpu_hdr;
    459  1.47       cgd  *	(opaque)    cpu_data; (size is cpu_hdr.c_size)
    460  1.47       cgd  *	kcore_seg_t mem_hdr;
    461  1.47       cgd  *	(memory)    mem_data; (size is mem_hdr.c_size)
    462  1.40       leo  */
    463  1.40       leo int
    464  1.41       leo kvm_dump_mkheader(kd, dump_off)
    465  1.41       leo kvm_t	*kd;
    466  1.40       leo off_t	dump_off;
    467  1.40       leo {
    468  1.43       gwr 	kcore_seg_t	cpu_hdr;
    469  1.60   thorpej 	size_t hdr_size;
    470  1.60   thorpej 	ssize_t sz;
    471  1.40       leo 
    472  1.41       leo 	if (kd->kcore_hdr != NULL) {
    473  1.41       leo 	    _kvm_err(kd, kd->program, "already has a dump header");
    474  1.40       leo 	    return (-1);
    475  1.40       leo 	}
    476  1.41       leo 	if (ISALIVE(kd)) {
    477  1.41       leo 		_kvm_err(kd, kd->program, "don't use on live kernel");
    478  1.40       leo 		return (-1);
    479  1.40       leo 	}
    480  1.40       leo 
    481  1.40       leo 	/*
    482  1.43       gwr 	 * Validate new format crash dump
    483  1.40       leo 	 */
    484  1.58   thorpej 	sz = Pread(kd, kd->pmfd, &cpu_hdr, sizeof(cpu_hdr), dump_off);
    485  1.43       gwr 	if (sz != sizeof(cpu_hdr))
    486  1.43       gwr 		return (-1);
    487  1.44       leo 	if ((CORE_GETMAGIC(cpu_hdr) != KCORE_MAGIC)
    488  1.44       leo 		|| (CORE_GETMID(cpu_hdr) != MID_MACHINE)) {
    489  1.44       leo 		_kvm_err(kd, 0, "invalid magic in cpu_hdr");
    490  1.45       leo 		return (0);
    491  1.44       leo 	}
    492  1.43       gwr 	hdr_size = ALIGN(sizeof(cpu_hdr));
    493  1.43       gwr 
    494  1.43       gwr 	/*
    495  1.43       gwr 	 * Read the CPU segment.
    496  1.43       gwr 	 */
    497  1.43       gwr 	kd->cpu_dsize = cpu_hdr.c_size;
    498  1.43       gwr 	kd->cpu_data = _kvm_malloc(kd, kd->cpu_dsize);
    499  1.43       gwr 	if (kd->cpu_data == NULL)
    500  1.43       gwr 		goto fail;
    501  1.58   thorpej 	sz = Pread(kd, kd->pmfd, kd->cpu_data, cpu_hdr.c_size,
    502  1.58   thorpej 	    dump_off + hdr_size);
    503  1.43       gwr 	if (sz != cpu_hdr.c_size)
    504  1.43       gwr 		goto fail;
    505  1.43       gwr 	hdr_size += kd->cpu_dsize;
    506  1.43       gwr 
    507  1.43       gwr 	/*
    508  1.43       gwr 	 * Leave phys mem pointer at beginning of memory data
    509  1.43       gwr 	 */
    510  1.43       gwr 	kd->dump_off = dump_off + hdr_size;
    511  1.43       gwr 	if (Lseek(kd, kd->pmfd, kd->dump_off, SEEK_SET) == -1)
    512  1.43       gwr 		goto fail;
    513  1.40       leo 
    514  1.40       leo 	/*
    515  1.40       leo 	 * Create a kcore_hdr.
    516  1.40       leo 	 */
    517  1.43       gwr 	kd->kcore_hdr = _kvm_malloc(kd, sizeof(kcore_hdr_t));
    518  1.43       gwr 	if (kd->kcore_hdr == NULL)
    519  1.43       gwr 		goto fail;
    520  1.40       leo 
    521  1.41       leo 	kd->kcore_hdr->c_hdrsize    = ALIGN(sizeof(kcore_hdr_t));
    522  1.41       leo 	kd->kcore_hdr->c_seghdrsize = ALIGN(sizeof(kcore_seg_t));
    523  1.41       leo 	kd->kcore_hdr->c_nseg       = 2;
    524  1.41       leo 	CORE_SETMAGIC(*(kd->kcore_hdr), KCORE_MAGIC, MID_MACHINE,0);
    525  1.40       leo 
    526  1.40       leo 	/*
    527  1.40       leo 	 * Now that we have a valid header, enable translations.
    528  1.40       leo 	 */
    529  1.49        pk 	if (_kvm_initvtop(kd) == 0)
    530  1.49        pk 		/* Success */
    531  1.49        pk 		return (hdr_size);
    532  1.43       gwr 
    533  1.43       gwr fail:
    534  1.43       gwr 	if (kd->kcore_hdr != NULL) {
    535  1.43       gwr 		free(kd->kcore_hdr);
    536  1.43       gwr 		kd->kcore_hdr = NULL;
    537  1.43       gwr 	}
    538  1.43       gwr 	if (kd->cpu_data != NULL) {
    539  1.43       gwr 		free(kd->cpu_data);
    540  1.43       gwr 		kd->cpu_data = NULL;
    541  1.43       gwr 		kd->cpu_dsize = 0;
    542  1.43       gwr 	}
    543  1.43       gwr 	return (-1);
    544  1.40       leo }
    545  1.40       leo 
    546  1.40       leo static int
    547  1.40       leo clear_gap(kd, fp, size)
    548  1.40       leo kvm_t	*kd;
    549  1.40       leo FILE	*fp;
    550  1.40       leo int	size;
    551  1.40       leo {
    552  1.40       leo 	if (size <= 0) /* XXX - < 0 should never happen */
    553  1.40       leo 		return (0);
    554  1.40       leo 	while (size-- > 0) {
    555  1.40       leo 		if (fputc(0, fp) == EOF) {
    556  1.40       leo 			_kvm_syserr(kd, kd->program, "clear_gap");
    557  1.40       leo 			return (-1);
    558  1.40       leo 		}
    559  1.40       leo 	}
    560  1.40       leo 	return (0);
    561  1.40       leo }
    562  1.40       leo 
    563  1.40       leo /*
    564  1.40       leo  * Write the dump header info to 'fp'. Note that we can't use fseek(3) here
    565  1.40       leo  * because 'fp' might be a file pointer obtained by zopen().
    566  1.40       leo  */
    567  1.40       leo int
    568  1.40       leo kvm_dump_wrtheader(kd, fp, dumpsize)
    569  1.40       leo kvm_t	*kd;
    570  1.40       leo FILE	*fp;
    571  1.40       leo int	dumpsize;
    572  1.40       leo {
    573  1.40       leo 	kcore_seg_t	seghdr;
    574  1.40       leo 	long		offset;
    575  1.40       leo 	int		gap;
    576  1.40       leo 
    577  1.43       gwr 	if (kd->kcore_hdr == NULL || kd->cpu_data == NULL) {
    578  1.40       leo 		_kvm_err(kd, kd->program, "no valid dump header(s)");
    579  1.40       leo 		return (-1);
    580  1.40       leo 	}
    581  1.40       leo 
    582  1.40       leo 	/*
    583  1.40       leo 	 * Write the generic header
    584  1.40       leo 	 */
    585  1.40       leo 	offset = 0;
    586  1.60   thorpej 	if (fwrite((void*)kd->kcore_hdr, sizeof(kcore_hdr_t), 1, fp) == 0) {
    587  1.40       leo 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    588  1.40       leo 		return (-1);
    589  1.40       leo 	}
    590  1.40       leo 	offset += kd->kcore_hdr->c_hdrsize;
    591  1.40       leo 	gap     = kd->kcore_hdr->c_hdrsize - sizeof(kcore_hdr_t);
    592  1.40       leo 	if (clear_gap(kd, fp, gap) == -1)
    593  1.40       leo 		return (-1);
    594  1.40       leo 
    595  1.40       leo 	/*
    596  1.40       leo 	 * Write the cpu header
    597  1.40       leo 	 */
    598  1.40       leo 	CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_CPU);
    599  1.43       gwr 	seghdr.c_size = ALIGN(kd->cpu_dsize);
    600  1.60   thorpej 	if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) == 0) {
    601  1.40       leo 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    602  1.40       leo 		return (-1);
    603  1.40       leo 	}
    604  1.40       leo 	offset += kd->kcore_hdr->c_seghdrsize;
    605  1.40       leo 	gap     = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
    606  1.40       leo 	if (clear_gap(kd, fp, gap) == -1)
    607  1.40       leo 		return (-1);
    608  1.40       leo 
    609  1.60   thorpej 	if (fwrite((void*)kd->cpu_data, kd->cpu_dsize, 1, fp) == 0) {
    610  1.40       leo 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    611  1.40       leo 		return (-1);
    612  1.40       leo 	}
    613  1.40       leo 	offset += seghdr.c_size;
    614  1.43       gwr 	gap     = seghdr.c_size - kd->cpu_dsize;
    615  1.40       leo 	if (clear_gap(kd, fp, gap) == -1)
    616  1.40       leo 		return (-1);
    617  1.40       leo 
    618  1.40       leo 	/*
    619  1.40       leo 	 * Write the actual dump data segment header
    620  1.40       leo 	 */
    621  1.40       leo 	CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_DATA);
    622  1.40       leo 	seghdr.c_size = dumpsize;
    623  1.60   thorpej 	if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) == 0) {
    624  1.40       leo 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    625  1.40       leo 		return (-1);
    626  1.40       leo 	}
    627  1.40       leo 	offset += kd->kcore_hdr->c_seghdrsize;
    628  1.40       leo 	gap     = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
    629  1.40       leo 	if (clear_gap(kd, fp, gap) == -1)
    630  1.40       leo 		return (-1);
    631  1.40       leo 
    632  1.62  christos 	return (int)offset;
    633  1.40       leo }
    634  1.40       leo 
    635  1.35       cgd kvm_t *
    636  1.35       cgd kvm_openfiles(uf, mf, sf, flag, errout)
    637  1.35       cgd 	const char *uf;
    638  1.35       cgd 	const char *mf;
    639  1.35       cgd 	const char *sf;
    640  1.35       cgd 	int flag;
    641  1.35       cgd 	char *errout;
    642  1.35       cgd {
    643  1.55     perry 	kvm_t *kd;
    644  1.35       cgd 
    645  1.35       cgd 	if ((kd = malloc(sizeof(*kd))) == NULL) {
    646  1.50       mrg 		(void)strncpy(errout, strerror(errno), _POSIX2_LINE_MAX - 1);
    647  1.35       cgd 		return (0);
    648  1.35       cgd 	}
    649  1.35       cgd 	kd->program = 0;
    650  1.35       cgd 	return (_kvm_open(kd, uf, mf, sf, flag, errout));
    651  1.35       cgd }
    652  1.35       cgd 
    653  1.35       cgd kvm_t *
    654  1.35       cgd kvm_open(uf, mf, sf, flag, program)
    655  1.35       cgd 	const char *uf;
    656  1.35       cgd 	const char *mf;
    657  1.35       cgd 	const char *sf;
    658  1.35       cgd 	int flag;
    659  1.35       cgd 	const char *program;
    660  1.35       cgd {
    661  1.55     perry 	kvm_t *kd;
    662  1.35       cgd 
    663  1.35       cgd 	if ((kd = malloc(sizeof(*kd))) == NULL && program != NULL) {
    664  1.51   thorpej 		(void)fprintf(stderr, "%s: %s\n", program, strerror(errno));
    665  1.35       cgd 		return (0);
    666  1.19   mycroft 	}
    667  1.35       cgd 	kd->program = program;
    668  1.35       cgd 	return (_kvm_open(kd, uf, mf, sf, flag, NULL));
    669   1.8       cgd }
    670   1.8       cgd 
    671   1.8       cgd int
    672  1.35       cgd kvm_close(kd)
    673  1.35       cgd 	kvm_t *kd;
    674   1.1       cgd {
    675  1.55     perry 	int error = 0;
    676  1.24   mycroft 
    677  1.35       cgd 	if (kd->pmfd >= 0)
    678  1.35       cgd 		error |= close(kd->pmfd);
    679  1.35       cgd 	if (kd->vmfd >= 0)
    680  1.35       cgd 		error |= close(kd->vmfd);
    681  1.35       cgd 	if (kd->nlfd >= 0)
    682  1.35       cgd 		error |= close(kd->nlfd);
    683  1.35       cgd 	if (kd->swfd >= 0)
    684  1.35       cgd 		error |= close(kd->swfd);
    685  1.35       cgd 	if (kd->db != 0)
    686  1.35       cgd 		error |= (kd->db->close)(kd->db);
    687  1.35       cgd 	if (kd->vmst)
    688  1.35       cgd 		_kvm_freevtop(kd);
    689  1.43       gwr 	kd->cpu_dsize = 0;
    690  1.43       gwr 	if (kd->cpu_data != NULL)
    691  1.43       gwr 		free((void *)kd->cpu_data);
    692  1.40       leo 	if (kd->kcore_hdr != NULL)
    693  1.40       leo 		free((void *)kd->kcore_hdr);
    694  1.35       cgd 	if (kd->procbase != 0)
    695  1.35       cgd 		free((void *)kd->procbase);
    696  1.65    simonb 	if (kd->procbase2 != 0)
    697  1.65    simonb 		free((void *)kd->procbase2);
    698  1.36   mycroft 	if (kd->swapspc != 0)
    699  1.36   mycroft 		free((void *)kd->swapspc);
    700  1.36   mycroft 	if (kd->argspc != 0)
    701  1.36   mycroft 		free((void *)kd->argspc);
    702  1.38   mycroft 	if (kd->argbuf != 0)
    703  1.38   mycroft 		free((void *)kd->argbuf);
    704  1.35       cgd 	if (kd->argv != 0)
    705  1.35       cgd 		free((void *)kd->argv);
    706  1.35       cgd 	free((void *)kd);
    707  1.19   mycroft 
    708   1.1       cgd 	return (0);
    709   1.1       cgd }
    710   1.1       cgd 
    711   1.3       cgd /*
    712  1.35       cgd  * Set up state necessary to do queries on the kernel namelist
    713  1.64    simonb  * data base.  If the data base is out-of-data/incompatible with
    714  1.35       cgd  * given executable, set up things so we revert to standard nlist call.
    715  1.35       cgd  * Only called for live kernels.  Return 0 on success, -1 on failure.
    716   1.3       cgd  */
    717   1.3       cgd static int
    718  1.48       cgd kvm_dbopen(kd)
    719  1.35       cgd 	kvm_t *kd;
    720   1.3       cgd {
    721  1.35       cgd 	DBT rec;
    722  1.62  christos 	size_t dbversionlen;
    723  1.35       cgd 	struct nlist nitem;
    724  1.35       cgd 	char dbversion[_POSIX2_LINE_MAX];
    725  1.35       cgd 	char kversion[_POSIX2_LINE_MAX];
    726  1.75  drochner 	int fd;
    727  1.19   mycroft 
    728  1.48       cgd 	kd->db = dbopen(_PATH_KVMDB, O_RDONLY, 0, DB_HASH, NULL);
    729  1.35       cgd 	if (kd->db == 0)
    730  1.35       cgd 		return (-1);
    731  1.73  christos 	if ((fd = (*kd->db->fd)(kd->db)) >= 0) {
    732  1.75  drochner 		if (fcntl(fd, F_SETFD, (void *)1) == -1) {
    733  1.73  christos 		       (*kd->db->close)(kd->db);
    734  1.73  christos 		       return (-1);
    735  1.73  christos 		}
    736  1.73  christos 	}
    737  1.35       cgd 	/*
    738  1.35       cgd 	 * read version out of database
    739  1.35       cgd 	 */
    740  1.35       cgd 	rec.data = VRS_KEY;
    741  1.35       cgd 	rec.size = sizeof(VRS_KEY) - 1;
    742  1.35       cgd 	if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
    743  1.35       cgd 		goto close;
    744  1.35       cgd 	if (rec.data == 0 || rec.size > sizeof(dbversion))
    745  1.35       cgd 		goto close;
    746   1.3       cgd 
    747  1.61     perry 	memcpy(dbversion, rec.data, rec.size);
    748  1.35       cgd 	dbversionlen = rec.size;
    749  1.35       cgd 	/*
    750  1.35       cgd 	 * Read version string from kernel memory.
    751  1.35       cgd 	 * Since we are dealing with a live kernel, we can call kvm_read()
    752  1.35       cgd 	 * at this point.
    753  1.35       cgd 	 */
    754  1.35       cgd 	rec.data = VRS_SYM;
    755  1.35       cgd 	rec.size = sizeof(VRS_SYM) - 1;
    756  1.35       cgd 	if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
    757  1.35       cgd 		goto close;
    758  1.35       cgd 	if (rec.data == 0 || rec.size != sizeof(struct nlist))
    759  1.35       cgd 		goto close;
    760  1.62  christos 	memcpy(&nitem, rec.data, sizeof(nitem));
    761  1.64    simonb 	if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) !=
    762  1.35       cgd 	    dbversionlen)
    763  1.35       cgd 		goto close;
    764  1.35       cgd 	/*
    765  1.35       cgd 	 * If they match, we win - otherwise clear out kd->db so
    766  1.35       cgd 	 * we revert to slow nlist().
    767  1.35       cgd 	 */
    768  1.61     perry 	if (memcmp(dbversion, kversion, dbversionlen) == 0)
    769  1.35       cgd 		return (0);
    770  1.35       cgd close:
    771  1.35       cgd 	(void)(kd->db->close)(kd->db);
    772  1.35       cgd 	kd->db = 0;
    773   1.3       cgd 
    774  1.35       cgd 	return (-1);
    775  1.18   mycroft }
    776  1.18   mycroft 
    777  1.18   mycroft int
    778  1.35       cgd kvm_nlist(kd, nl)
    779  1.35       cgd 	kvm_t *kd;
    780  1.35       cgd 	struct nlist *nl;
    781  1.18   mycroft {
    782  1.55     perry 	struct nlist *p;
    783  1.55     perry 	int nvalid, rv;
    784   1.3       cgd 
    785  1.35       cgd 	/*
    786  1.64    simonb 	 * If we can't use the data base, revert to the
    787  1.35       cgd 	 * slow library call.
    788  1.35       cgd 	 */
    789  1.46       cgd 	if (kd->db == 0) {
    790  1.46       cgd 		rv = __fdnlist(kd->nlfd, nl);
    791  1.46       cgd 		if (rv == -1)
    792  1.46       cgd 			_kvm_err(kd, 0, "bad namelist");
    793  1.46       cgd 		return (rv);
    794  1.46       cgd 	}
    795   1.3       cgd 
    796  1.35       cgd 	/*
    797  1.35       cgd 	 * We can use the kvm data base.  Go through each nlist entry
    798  1.35       cgd 	 * and look it up with a db query.
    799  1.35       cgd 	 */
    800  1.35       cgd 	nvalid = 0;
    801  1.35       cgd 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
    802  1.55     perry 		int len;
    803  1.35       cgd 		DBT rec;
    804  1.35       cgd 
    805  1.35       cgd 		if ((len = strlen(p->n_name)) > 4096) {
    806  1.35       cgd 			/* sanity */
    807  1.35       cgd 			_kvm_err(kd, kd->program, "symbol too large");
    808  1.35       cgd 			return (-1);
    809  1.35       cgd 		}
    810  1.59   mycroft 		rec.data = (char *)p->n_name;
    811  1.35       cgd 		rec.size = len;
    812  1.40       leo 
    813  1.40       leo 		/*
    814  1.40       leo 		 * Make sure that n_value = 0 when the symbol isn't found
    815  1.40       leo 		 */
    816  1.40       leo 		p->n_value = 0;
    817  1.40       leo 
    818  1.35       cgd 		if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
    819  1.35       cgd 			continue;
    820  1.35       cgd 		if (rec.data == 0 || rec.size != sizeof(struct nlist))
    821  1.35       cgd 			continue;
    822  1.35       cgd 		++nvalid;
    823  1.35       cgd 		/*
    824  1.35       cgd 		 * Avoid alignment issues.
    825  1.35       cgd 		 */
    826  1.62  christos 		(void)memcpy(&p->n_type, &((struct nlist *)rec.data)->n_type,
    827  1.35       cgd 		      sizeof(p->n_type));
    828  1.62  christos 		(void)memcpy(&p->n_value, &((struct nlist *)rec.data)->n_value,
    829  1.35       cgd 		      sizeof(p->n_value));
    830   1.3       cgd 	}
    831  1.35       cgd 	/*
    832  1.35       cgd 	 * Return the number of entries that weren't found.
    833  1.35       cgd 	 */
    834  1.35       cgd 	return ((p - nl) - nvalid);
    835  1.18   mycroft }
    836   1.3       cgd 
    837  1.40       leo int kvm_dump_inval(kd)
    838  1.40       leo kvm_t	*kd;
    839  1.40       leo {
    840  1.60   thorpej 	struct nlist	nl[2];
    841  1.57   thorpej 	u_long		pa, val;
    842  1.40       leo 
    843  1.40       leo 	if (ISALIVE(kd)) {
    844  1.40       leo 		_kvm_err(kd, kd->program, "clearing dump on live kernel");
    845  1.40       leo 		return (-1);
    846  1.40       leo 	}
    847  1.60   thorpej 	nl[0].n_name = "_dumpmag";
    848  1.60   thorpej 	nl[1].n_name = NULL;
    849  1.40       leo 
    850  1.60   thorpej 	if (kvm_nlist(kd, nl) == -1) {
    851  1.40       leo 		_kvm_err(kd, 0, "bad namelist");
    852  1.40       leo 		return (-1);
    853  1.40       leo 	}
    854  1.60   thorpej 	if (_kvm_kvatop(kd, (u_long)nl[0].n_value, &pa) == 0)
    855  1.40       leo 		return (-1);
    856  1.40       leo 
    857  1.40       leo 	errno = 0;
    858  1.57   thorpej 	val = 0;
    859  1.62  christos 	if (pwrite(kd->pmfd, (void *) &val, sizeof(val),
    860  1.62  christos 	    _kvm_pa2off(kd, pa)) == -1) {
    861  1.57   thorpej 		_kvm_syserr(kd, 0, "cannot invalidate dump - pwrite");
    862  1.40       leo 		return (-1);
    863  1.40       leo 	}
    864  1.40       leo 	return (0);
    865  1.40       leo }
    866  1.40       leo 
    867  1.35       cgd ssize_t
    868  1.35       cgd kvm_read(kd, kva, buf, len)
    869  1.35       cgd 	kvm_t *kd;
    870  1.55     perry 	u_long kva;
    871  1.55     perry 	void *buf;
    872  1.55     perry 	size_t len;
    873   1.3       cgd {
    874  1.55     perry 	int cc;
    875  1.55     perry 	void *cp;
    876   1.3       cgd 
    877  1.65    simonb 	if (ISKMEM(kd)) {
    878  1.35       cgd 		/*
    879  1.35       cgd 		 * We're using /dev/kmem.  Just read straight from the
    880  1.35       cgd 		 * device and let the active kernel do the address translation.
    881  1.35       cgd 		 */
    882  1.35       cgd 		errno = 0;
    883  1.57   thorpej 		cc = pread(kd->vmfd, buf, len, (off_t)kva);
    884  1.35       cgd 		if (cc < 0) {
    885  1.35       cgd 			_kvm_syserr(kd, 0, "kvm_read");
    886  1.56   msaitoh 			return (-1);
    887  1.35       cgd 		} else if (cc < len)
    888  1.35       cgd 			_kvm_err(kd, kd->program, "short read");
    889  1.35       cgd 		return (cc);
    890  1.65    simonb 	} else if (ISSYSCTL(kd)) {
    891  1.65    simonb 		_kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
    892  1.65    simonb 		    "can't use kvm_read");
    893  1.65    simonb 		return (-1);
    894  1.35       cgd 	} else {
    895  1.43       gwr 		if ((kd->kcore_hdr == NULL) || (kd->cpu_data == NULL)) {
    896  1.40       leo 			_kvm_err(kd, kd->program, "no valid dump header");
    897  1.56   msaitoh 			return (-1);
    898  1.40       leo 		}
    899  1.35       cgd 		cp = buf;
    900  1.35       cgd 		while (len > 0) {
    901  1.40       leo 			u_long	pa;
    902  1.40       leo 			off_t	foff;
    903  1.64    simonb 
    904  1.35       cgd 			cc = _kvm_kvatop(kd, kva, &pa);
    905  1.35       cgd 			if (cc == 0)
    906  1.56   msaitoh 				return (-1);
    907  1.35       cgd 			if (cc > len)
    908  1.35       cgd 				cc = len;
    909  1.40       leo 			foff = _kvm_pa2off(kd, pa);
    910  1.35       cgd 			errno = 0;
    911  1.62  christos 			cc = pread(kd->pmfd, cp, (size_t)cc, foff);
    912  1.35       cgd 			if (cc < 0) {
    913  1.35       cgd 				_kvm_syserr(kd, kd->program, "kvm_read");
    914  1.35       cgd 				break;
    915  1.35       cgd 			}
    916  1.35       cgd 			/*
    917  1.35       cgd 			 * If kvm_kvatop returns a bogus value or our core
    918  1.35       cgd 			 * file is truncated, we might wind up seeking beyond
    919  1.35       cgd 			 * the end of the core file in which case the read will
    920  1.35       cgd 			 * return 0 (EOF).
    921  1.35       cgd 			 */
    922  1.35       cgd 			if (cc == 0)
    923  1.35       cgd 				break;
    924  1.39       cgd 			cp = (char *)cp + cc;
    925  1.35       cgd 			kva += cc;
    926  1.35       cgd 			len -= cc;
    927   1.3       cgd 		}
    928  1.35       cgd 		return ((char *)cp - (char *)buf);
    929   1.3       cgd 	}
    930  1.35       cgd 	/* NOTREACHED */
    931   1.3       cgd }
    932   1.3       cgd 
    933  1.35       cgd ssize_t
    934  1.35       cgd kvm_write(kd, kva, buf, len)
    935  1.35       cgd 	kvm_t *kd;
    936  1.55     perry 	u_long kva;
    937  1.55     perry 	const void *buf;
    938  1.55     perry 	size_t len;
    939  1.35       cgd {
    940  1.55     perry 	int cc;
    941  1.26        pk 
    942  1.65    simonb 	if (ISKMEM(kd)) {
    943  1.35       cgd 		/*
    944  1.35       cgd 		 * Just like kvm_read, only we write.
    945  1.35       cgd 		 */
    946  1.35       cgd 		errno = 0;
    947  1.57   thorpej 		cc = pwrite(kd->vmfd, buf, len, (off_t)kva);
    948  1.35       cgd 		if (cc < 0) {
    949  1.35       cgd 			_kvm_syserr(kd, 0, "kvm_write");
    950  1.56   msaitoh 			return (-1);
    951  1.35       cgd 		} else if (cc < len)
    952  1.35       cgd 			_kvm_err(kd, kd->program, "short write");
    953  1.35       cgd 		return (cc);
    954  1.65    simonb 	} else if (ISSYSCTL(kd)) {
    955  1.65    simonb 		_kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
    956  1.65    simonb 		    "can't use kvm_write");
    957  1.65    simonb 		return (-1);
    958  1.35       cgd 	} else {
    959  1.35       cgd 		_kvm_err(kd, kd->program,
    960  1.35       cgd 		    "kvm_write not implemented for dead kernels");
    961  1.56   msaitoh 		return (-1);
    962  1.26        pk 	}
    963  1.35       cgd 	/* NOTREACHED */
    964   1.1       cgd }
    965