Home | History | Annotate | Line # | Download | only in libkvm
kvm.c revision 1.103
      1  1.103      maxv /*	$NetBSD: kvm.c,v 1.103 2018/02/07 14:03:18 maxv 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.81       agc  * 3. Neither the name of the University nor the names of its contributors
     20    1.1       cgd  *    may be used to endorse or promote products derived from this software
     21    1.1       cgd  *    without specific prior written permission.
     22    1.1       cgd  *
     23    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33    1.1       cgd  * SUCH DAMAGE.
     34    1.1       cgd  */
     35    1.1       cgd 
     36   1.53     mikel #include <sys/cdefs.h>
     37    1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     38   1.42   thorpej #if 0
     39   1.35       cgd static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
     40   1.42   thorpej #else
     41  1.103      maxv __RCSID("$NetBSD: kvm.c,v 1.103 2018/02/07 14:03:18 maxv Exp $");
     42   1.42   thorpej #endif
     43    1.1       cgd #endif /* LIBC_SCCS and not lint */
     44    1.1       cgd 
     45    1.1       cgd #include <sys/param.h>
     46   1.79   thorpej #include <sys/lwp.h>
     47    1.1       cgd #include <sys/proc.h>
     48    1.1       cgd #include <sys/ioctl.h>
     49   1.35       cgd #include <sys/stat.h>
     50   1.35       cgd #include <sys/sysctl.h>
     51   1.35       cgd 
     52   1.40       leo #include <sys/core.h>
     53   1.93        he #include <sys/exec.h>
     54   1.40       leo #include <sys/kcore.h>
     55   1.80     ragge #include <sys/ksyms.h>
     56   1.95       jym #include <sys/types.h>
     57   1.40       leo 
     58   1.67       mrg #include <uvm/uvm_extern.h>
     59   1.35       cgd 
     60   1.76    atatat #include <machine/cpu.h>
     61   1.76    atatat 
     62   1.35       cgd #include <ctype.h>
     63   1.87      yamt #include <errno.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.88     joerg static int	_kvm_get_header(kvm_t *);
     78   1.88     joerg static kvm_t	*_kvm_open(kvm_t *, const char *, const char *,
     79   1.88     joerg 		    const char *, int, char *);
     80   1.89     joerg static int	clear_gap(kvm_t *, bool (*)(void *, const void *, size_t),
     81   1.89     joerg 		    void *, size_t);
     82   1.88     joerg static off_t	Lseek(kvm_t *, int, off_t, int);
     83   1.88     joerg static ssize_t	Pread(kvm_t *, int, void *, size_t, off_t);
     84   1.19   mycroft 
     85   1.35       cgd char *
     86   1.88     joerg kvm_geterr(kvm_t *kd)
     87   1.35       cgd {
     88   1.35       cgd 	return (kd->errbuf);
     89   1.35       cgd }
     90    1.1       cgd 
     91   1.98  christos const char *
     92   1.98  christos kvm_getkernelname(kvm_t *kd)
     93   1.98  christos {
     94   1.98  christos 	return kd->kernelname;
     95   1.98  christos }
     96   1.98  christos 
     97   1.35       cgd /*
     98   1.35       cgd  * Report an error using printf style arguments.  "program" is kd->program
     99   1.35       cgd  * on hard errors, and 0 on soft errors, so that under sun error emulation,
    100   1.35       cgd  * only hard errors are printed out (otherwise, programs like gdb will
    101   1.35       cgd  * generate tons of error messages when trying to access bogus pointers).
    102   1.35       cgd  */
    103   1.35       cgd void
    104   1.35       cgd _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
    105   1.35       cgd {
    106   1.35       cgd 	va_list ap;
    107   1.27      phil 
    108   1.35       cgd 	va_start(ap, fmt);
    109   1.35       cgd 	if (program != NULL) {
    110   1.35       cgd 		(void)fprintf(stderr, "%s: ", program);
    111   1.35       cgd 		(void)vfprintf(stderr, fmt, ap);
    112   1.35       cgd 		(void)fputc('\n', stderr);
    113   1.35       cgd 	} else
    114   1.35       cgd 		(void)vsnprintf(kd->errbuf,
    115   1.60   thorpej 		    sizeof(kd->errbuf), fmt, ap);
    116   1.26        pk 
    117   1.35       cgd 	va_end(ap);
    118   1.35       cgd }
    119   1.26        pk 
    120   1.35       cgd void
    121   1.35       cgd _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
    122   1.35       cgd {
    123   1.35       cgd 	va_list ap;
    124   1.60   thorpej 	size_t n;
    125   1.26        pk 
    126   1.35       cgd 	va_start(ap, fmt);
    127   1.35       cgd 	if (program != NULL) {
    128   1.35       cgd 		(void)fprintf(stderr, "%s: ", program);
    129   1.35       cgd 		(void)vfprintf(stderr, fmt, ap);
    130   1.35       cgd 		(void)fprintf(stderr, ": %s\n", strerror(errno));
    131   1.35       cgd 	} else {
    132   1.55     perry 		char *cp = kd->errbuf;
    133   1.26        pk 
    134   1.60   thorpej 		(void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
    135   1.35       cgd 		n = strlen(cp);
    136   1.35       cgd 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
    137   1.35       cgd 		    strerror(errno));
    138   1.35       cgd 	}
    139   1.35       cgd 	va_end(ap);
    140   1.35       cgd }
    141    1.1       cgd 
    142   1.35       cgd void *
    143   1.88     joerg _kvm_malloc(kvm_t *kd, size_t n)
    144   1.35       cgd {
    145   1.35       cgd 	void *p;
    146   1.35       cgd 
    147   1.35       cgd 	if ((p = malloc(n)) == NULL)
    148   1.68  sommerfe 		_kvm_err(kd, kd->program, "%s", strerror(errno));
    149   1.35       cgd 	return (p);
    150   1.35       cgd }
    151   1.35       cgd 
    152   1.40       leo /*
    153   1.58   thorpej  * Wrapper around the lseek(2) system call; calls _kvm_syserr() for us
    154   1.58   thorpej  * in the event of emergency.
    155   1.40       leo  */
    156   1.40       leo static off_t
    157   1.88     joerg Lseek(kvm_t *kd, int fd, off_t offset, int whence)
    158   1.40       leo {
    159   1.58   thorpej 	off_t off;
    160   1.40       leo 
    161   1.40       leo 	errno = 0;
    162   1.58   thorpej 
    163   1.40       leo 	if ((off = lseek(fd, offset, whence)) == -1 && errno != 0) {
    164   1.40       leo 		_kvm_syserr(kd, kd->program, "Lseek");
    165   1.58   thorpej 		return ((off_t)-1);
    166   1.40       leo 	}
    167   1.40       leo 	return (off);
    168   1.40       leo }
    169   1.40       leo 
    170   1.91        ad ssize_t
    171   1.91        ad _kvm_pread(kvm_t *kd, int fd, void *buf, size_t size, off_t off)
    172   1.91        ad {
    173   1.91        ad 	ptrdiff_t moff;
    174   1.91        ad 	void *newbuf;
    175   1.91        ad 	size_t dsize;
    176   1.91        ad 	ssize_t rv;
    177   1.91        ad 	off_t doff;
    178   1.91        ad 
    179   1.91        ad 	/* If aligned nothing to do. */
    180   1.91        ad  	if (((off % kd->fdalign) | (size % kd->fdalign)) == 0) {
    181   1.91        ad 		return pread(fd, buf, size, off);
    182   1.91        ad  	}
    183   1.91        ad 
    184   1.91        ad 	/*
    185   1.91        ad 	 * Otherwise must buffer.  We can't tolerate short reads in this
    186   1.91        ad 	 * case (lazy bum).
    187   1.91        ad 	 */
    188   1.91        ad 	moff = (ptrdiff_t)off % kd->fdalign;
    189   1.91        ad 	doff = off - moff;
    190   1.91        ad 	dsize = moff + size + kd->fdalign - 1;
    191   1.91        ad 	dsize -= dsize % kd->fdalign;
    192   1.91        ad 	if (kd->iobufsz < dsize) {
    193   1.91        ad 		newbuf = realloc(kd->iobuf, dsize);
    194   1.91        ad 		if (newbuf == NULL) {
    195   1.91        ad 			_kvm_syserr(kd, 0, "cannot allocate I/O buffer");
    196   1.91        ad 			return (-1);
    197   1.91        ad 		}
    198   1.91        ad 		kd->iobuf = newbuf;
    199   1.91        ad 		kd->iobufsz = dsize;
    200   1.91        ad 	}
    201   1.91        ad 	rv = pread(fd, kd->iobuf, dsize, doff);
    202   1.96  stacktic 	if (rv < size + moff)
    203   1.91        ad 		return -1;
    204   1.91        ad 	memcpy(buf, kd->iobuf + moff, size);
    205   1.91        ad 	return size;
    206   1.91        ad }
    207   1.91        ad 
    208   1.58   thorpej /*
    209   1.58   thorpej  * Wrapper around the pread(2) system call; calls _kvm_syserr() for us
    210   1.58   thorpej  * in the event of emergency.
    211   1.58   thorpej  */
    212   1.40       leo static ssize_t
    213   1.88     joerg Pread(kvm_t *kd, int fd, void *buf, size_t nbytes, off_t offset)
    214   1.40       leo {
    215   1.58   thorpej 	ssize_t rv;
    216   1.40       leo 
    217   1.40       leo 	errno = 0;
    218   1.40       leo 
    219   1.91        ad 	if ((rv = _kvm_pread(kd, fd, buf, nbytes, offset)) != nbytes &&
    220   1.58   thorpej 	    errno != 0)
    221   1.58   thorpej 		_kvm_syserr(kd, kd->program, "Pread");
    222   1.40       leo 	return (rv);
    223   1.40       leo }
    224   1.40       leo 
    225   1.35       cgd static kvm_t *
    226   1.88     joerg _kvm_open(kvm_t *kd, const char *uf, const char *mf, const char *sf, int flag,
    227   1.88     joerg     char *errout)
    228   1.35       cgd {
    229   1.35       cgd 	struct stat st;
    230   1.48       cgd 	int ufgiven;
    231   1.35       cgd 
    232   1.37   mycroft 	kd->pmfd = -1;
    233   1.35       cgd 	kd->vmfd = -1;
    234   1.35       cgd 	kd->swfd = -1;
    235   1.35       cgd 	kd->nlfd = -1;
    236   1.65    simonb 	kd->alive = KVM_ALIVE_DEAD;
    237   1.85  christos 	kd->procbase = NULL;
    238   1.85  christos 	kd->procbase_len = 0;
    239   1.85  christos 	kd->procbase2 = NULL;
    240   1.85  christos 	kd->procbase2_len = 0;
    241   1.85  christos 	kd->lwpbase = NULL;
    242   1.85  christos 	kd->lwpbase_len = 0;
    243   1.36   mycroft 	kd->nbpg = getpagesize();
    244   1.85  christos 	kd->swapspc = NULL;
    245   1.85  christos 	kd->argspc = NULL;
    246   1.85  christos 	kd->argspc_len = 0;
    247   1.85  christos 	kd->argbuf = NULL;
    248   1.85  christos 	kd->argv = NULL;
    249   1.85  christos 	kd->vmst = NULL;
    250   1.85  christos 	kd->vm_page_buckets = NULL;
    251   1.85  christos 	kd->kcore_hdr = NULL;
    252   1.43       gwr 	kd->cpu_dsize = 0;
    253   1.85  christos 	kd->cpu_data = NULL;
    254   1.40       leo 	kd->dump_off = 0;
    255   1.91        ad 	kd->fdalign = 1;
    256   1.91        ad 	kd->iobuf = NULL;
    257   1.91        ad 	kd->iobufsz = 0;
    258   1.52       gwr 
    259   1.65    simonb 	if (flag & KVM_NO_FILES) {
    260   1.65    simonb 		kd->alive = KVM_ALIVE_SYSCTL;
    261   1.65    simonb 		return(kd);
    262   1.65    simonb 	}
    263   1.65    simonb 
    264   1.52       gwr 	/*
    265   1.52       gwr 	 * Call the MD open hook.  This sets:
    266   1.52       gwr 	 *	usrstack, min_uva, max_uva
    267   1.52       gwr 	 */
    268   1.52       gwr 	if (_kvm_mdopen(kd)) {
    269   1.52       gwr 		_kvm_err(kd, kd->program, "md init failed");
    270   1.52       gwr 		goto failed;
    271   1.52       gwr 	}
    272   1.35       cgd 
    273   1.48       cgd 	ufgiven = (uf != NULL);
    274   1.76    atatat 	if (!ufgiven) {
    275   1.76    atatat #ifdef CPU_BOOTED_KERNEL
    276   1.76    atatat 		/* 130 is 128 + '/' + '\0' */
    277   1.76    atatat 		static char booted_kernel[130];
    278   1.76    atatat 		int mib[2], rc;
    279   1.76    atatat 		size_t len;
    280   1.76    atatat 
    281   1.76    atatat 		mib[0] = CTL_MACHDEP;
    282   1.76    atatat 		mib[1] = CPU_BOOTED_KERNEL;
    283   1.76    atatat 		booted_kernel[0] = '/';
    284   1.76    atatat 		booted_kernel[1] = '\0';
    285   1.76    atatat 		len = sizeof(booted_kernel) - 2;
    286   1.76    atatat 		rc = sysctl(&mib[0], 2, &booted_kernel[1], &len, NULL, 0);
    287   1.76    atatat 		booted_kernel[sizeof(booted_kernel) - 1] = '\0';
    288   1.76    atatat 		uf = (booted_kernel[1] == '/') ?
    289   1.76    atatat 		    &booted_kernel[1] : &booted_kernel[0];
    290   1.76    atatat 		if (rc != -1)
    291   1.76    atatat 			rc = stat(uf, &st);
    292   1.76    atatat 		if (rc != -1 && !S_ISREG(st.st_mode))
    293   1.76    atatat 			rc = -1;
    294   1.76    atatat 		if (rc == -1)
    295   1.76    atatat #endif /* CPU_BOOTED_KERNEL */
    296   1.76    atatat 			uf = _PATH_UNIX;
    297   1.76    atatat 	}
    298   1.35       cgd 	else if (strlen(uf) >= MAXPATHLEN) {
    299   1.35       cgd 		_kvm_err(kd, kd->program, "exec file name too long");
    300    1.1       cgd 		goto failed;
    301    1.1       cgd 	}
    302   1.35       cgd 	if (flag & ~O_RDWR) {
    303   1.35       cgd 		_kvm_err(kd, kd->program, "bad flags arg");
    304    1.1       cgd 		goto failed;
    305    1.1       cgd 	}
    306   1.35       cgd 	if (mf == 0)
    307   1.35       cgd 		mf = _PATH_MEM;
    308   1.35       cgd 	if (sf == 0)
    309   1.35       cgd 		sf = _PATH_DRUM;
    310   1.35       cgd 
    311   1.94       apb 	/*
    312   1.94       apb 	 * Open the kernel namelist.  If /dev/ksyms doesn't
    313   1.94       apb 	 * exist, open the current kernel.
    314   1.94       apb 	 */
    315   1.94       apb 	if (ufgiven == 0)
    316   1.99  christos 		kd->nlfd = open(_PATH_KSYMS, O_RDONLY | O_CLOEXEC, 0);
    317   1.94       apb 	if (kd->nlfd < 0) {
    318   1.99  christos 		if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
    319   1.94       apb 			_kvm_syserr(kd, kd->program, "%s", uf);
    320   1.94       apb 			goto failed;
    321   1.94       apb 		}
    322   1.98  christos 		strlcpy(kd->kernelname, uf, sizeof(kd->kernelname));
    323   1.94       apb 	} else {
    324   1.98  christos 		strlcpy(kd->kernelname, _PATH_KSYMS, sizeof(kd->kernelname));
    325   1.94       apb 	}
    326   1.94       apb 
    327   1.99  christos 	if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
    328   1.35       cgd 		_kvm_syserr(kd, kd->program, "%s", mf);
    329   1.35       cgd 		goto failed;
    330    1.1       cgd 	}
    331   1.35       cgd 	if (fstat(kd->pmfd, &st) < 0) {
    332   1.35       cgd 		_kvm_syserr(kd, kd->program, "%s", mf);
    333    1.1       cgd 		goto failed;
    334    1.1       cgd 	}
    335   1.91        ad 	if (S_ISCHR(st.st_mode) && strcmp(mf, _PATH_MEM) == 0) {
    336    1.1       cgd 		/*
    337   1.91        ad 		 * If this is /dev/mem, open kmem too.  (Maybe we should
    338   1.35       cgd 		 * make it work for either /dev/mem or /dev/kmem -- in either
    339   1.35       cgd 		 * case you're working with a live kernel.)
    340    1.1       cgd 		 */
    341   1.99  christos 		if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC, 0)) < 0) {
    342   1.35       cgd 			_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
    343   1.35       cgd 			goto failed;
    344    1.1       cgd 		}
    345   1.65    simonb 		kd->alive = KVM_ALIVE_FILES;
    346   1.99  christos 		if ((kd->swfd = open(sf, flag | O_CLOEXEC, 0)) < 0) {
    347   1.84      yamt 			if (errno != ENXIO) {
    348   1.84      yamt 				_kvm_syserr(kd, kd->program, "%s", sf);
    349   1.84      yamt 				goto failed;
    350   1.84      yamt 			}
    351   1.84      yamt 			/* swap is not configured?  not fatal */
    352    1.1       cgd 		}
    353   1.35       cgd 	} else {
    354   1.91        ad 		kd->fdalign = DEV_BSIZE;	/* XXX */
    355    1.3       cgd 		/*
    356   1.35       cgd 		 * This is a crash dump.
    357   1.94       apb 		 * Initialize the virtual address translation machinery.
    358   1.94       apb 		 *
    359   1.40       leo 		 * If there is no valid core header, fail silently here.
    360   1.64    simonb 		 * The address translations however will fail without
    361   1.40       leo 		 * header. Things can be made to run by calling
    362   1.40       leo 		 * kvm_dump_mkheader() before doing any translation.
    363   1.40       leo 		 */
    364   1.40       leo 		if (_kvm_get_header(kd) == 0) {
    365   1.40       leo 			if (_kvm_initvtop(kd) < 0)
    366   1.40       leo 				goto failed;
    367   1.40       leo 		}
    368    1.3       cgd 	}
    369   1.35       cgd 	return (kd);
    370   1.35       cgd failed:
    371    1.1       cgd 	/*
    372   1.35       cgd 	 * Copy out the error if doing sane error semantics.
    373    1.1       cgd 	 */
    374   1.35       cgd 	if (errout != 0)
    375   1.78    itojun 		(void)strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
    376   1.35       cgd 	(void)kvm_close(kd);
    377   1.35       cgd 	return (0);
    378    1.1       cgd }
    379    1.1       cgd 
    380   1.43       gwr /*
    381   1.43       gwr  * The kernel dump file (from savecore) contains:
    382   1.43       gwr  *    kcore_hdr_t kcore_hdr;
    383   1.43       gwr  *    kcore_seg_t cpu_hdr;
    384   1.43       gwr  *    (opaque)    cpu_data; (size is cpu_hdr.c_size)
    385   1.43       gwr  *	  kcore_seg_t mem_hdr;
    386   1.43       gwr  *    (memory)    mem_data; (size is mem_hdr.c_size)
    387   1.64    simonb  *
    388   1.43       gwr  * Note: khdr is padded to khdr.c_hdrsize;
    389   1.43       gwr  * cpu_hdr and mem_hdr are padded to khdr.c_seghdrsize
    390   1.43       gwr  */
    391   1.40       leo static int
    392   1.88     joerg _kvm_get_header(kvm_t *kd)
    393   1.40       leo {
    394   1.43       gwr 	kcore_hdr_t	kcore_hdr;
    395   1.43       gwr 	kcore_seg_t	cpu_hdr;
    396   1.43       gwr 	kcore_seg_t	mem_hdr;
    397   1.43       gwr 	size_t		offset;
    398   1.43       gwr 	ssize_t		sz;
    399   1.40       leo 
    400   1.43       gwr 	/*
    401   1.43       gwr 	 * Read the kcore_hdr_t
    402   1.43       gwr 	 */
    403   1.58   thorpej 	sz = Pread(kd, kd->pmfd, &kcore_hdr, sizeof(kcore_hdr), (off_t)0);
    404   1.43       gwr 	if (sz != sizeof(kcore_hdr))
    405   1.40       leo 		return (-1);
    406   1.40       leo 
    407   1.40       leo 	/*
    408   1.40       leo 	 * Currently, we only support dump-files made by the current
    409   1.40       leo 	 * architecture...
    410   1.40       leo 	 */
    411   1.43       gwr 	if ((CORE_GETMAGIC(kcore_hdr) != KCORE_MAGIC) ||
    412   1.43       gwr 	    (CORE_GETMID(kcore_hdr) != MID_MACHINE))
    413   1.43       gwr 		return (-1);
    414   1.40       leo 
    415   1.40       leo 	/*
    416   1.40       leo 	 * Currently, we only support exactly 2 segments: cpu-segment
    417   1.40       leo 	 * and data-segment in exactly that order.
    418   1.40       leo 	 */
    419   1.43       gwr 	if (kcore_hdr.c_nseg != 2)
    420   1.40       leo 		return (-1);
    421   1.40       leo 
    422   1.40       leo 	/*
    423   1.43       gwr 	 * Save away the kcore_hdr.  All errors after this
    424   1.43       gwr 	 * should do a to "goto fail" to deallocate things.
    425   1.43       gwr 	 */
    426   1.43       gwr 	kd->kcore_hdr = _kvm_malloc(kd, sizeof(kcore_hdr));
    427   1.43       gwr 	memcpy(kd->kcore_hdr, &kcore_hdr, sizeof(kcore_hdr));
    428   1.43       gwr 	offset = kcore_hdr.c_hdrsize;
    429   1.43       gwr 
    430   1.43       gwr 	/*
    431   1.43       gwr 	 * Read the CPU segment header
    432   1.40       leo 	 */
    433   1.58   thorpej 	sz = Pread(kd, kd->pmfd, &cpu_hdr, sizeof(cpu_hdr), (off_t)offset);
    434   1.43       gwr 	if (sz != sizeof(cpu_hdr))
    435   1.43       gwr 		goto fail;
    436   1.43       gwr 	if ((CORE_GETMAGIC(cpu_hdr) != KCORESEG_MAGIC) ||
    437   1.43       gwr 	    (CORE_GETFLAG(cpu_hdr) != CORE_CPU))
    438   1.43       gwr 		goto fail;
    439   1.43       gwr 	offset += kcore_hdr.c_seghdrsize;
    440   1.43       gwr 
    441   1.43       gwr 	/*
    442   1.43       gwr 	 * Read the CPU segment DATA.
    443   1.43       gwr 	 */
    444   1.43       gwr 	kd->cpu_dsize = cpu_hdr.c_size;
    445   1.43       gwr 	kd->cpu_data = _kvm_malloc(kd, cpu_hdr.c_size);
    446   1.43       gwr 	if (kd->cpu_data == NULL)
    447   1.43       gwr 		goto fail;
    448   1.58   thorpej 	sz = Pread(kd, kd->pmfd, kd->cpu_data, cpu_hdr.c_size, (off_t)offset);
    449   1.43       gwr 	if (sz != cpu_hdr.c_size)
    450   1.43       gwr 		goto fail;
    451   1.43       gwr 	offset += cpu_hdr.c_size;
    452   1.40       leo 
    453   1.40       leo 	/*
    454   1.40       leo 	 * Read the next segment header: data segment
    455   1.40       leo 	 */
    456   1.58   thorpej 	sz = Pread(kd, kd->pmfd, &mem_hdr, sizeof(mem_hdr), (off_t)offset);
    457   1.43       gwr 	if (sz != sizeof(mem_hdr))
    458   1.43       gwr 		goto fail;
    459   1.43       gwr 	offset += kcore_hdr.c_seghdrsize;
    460   1.43       gwr 
    461   1.43       gwr 	if ((CORE_GETMAGIC(mem_hdr) != KCORESEG_MAGIC) ||
    462   1.43       gwr 	    (CORE_GETFLAG(mem_hdr) != CORE_DATA))
    463   1.43       gwr 		goto fail;
    464   1.40       leo 
    465   1.43       gwr 	kd->dump_off = offset;
    466   1.43       gwr 	return (0);
    467   1.40       leo 
    468   1.43       gwr fail:
    469   1.43       gwr 	if (kd->kcore_hdr != NULL) {
    470   1.43       gwr 		free(kd->kcore_hdr);
    471   1.40       leo 		kd->kcore_hdr = NULL;
    472   1.43       gwr 	}
    473   1.43       gwr 	if (kd->cpu_data != NULL) {
    474   1.43       gwr 		free(kd->cpu_data);
    475   1.43       gwr 		kd->cpu_data = NULL;
    476   1.43       gwr 		kd->cpu_dsize = 0;
    477   1.40       leo 	}
    478   1.54       mrg 	return (-1);
    479   1.40       leo }
    480   1.40       leo 
    481   1.40       leo /*
    482   1.43       gwr  * The format while on the dump device is: (new format)
    483   1.47       cgd  *	kcore_seg_t cpu_hdr;
    484   1.47       cgd  *	(opaque)    cpu_data; (size is cpu_hdr.c_size)
    485   1.47       cgd  *	kcore_seg_t mem_hdr;
    486   1.47       cgd  *	(memory)    mem_data; (size is mem_hdr.c_size)
    487   1.40       leo  */
    488   1.40       leo int
    489   1.88     joerg kvm_dump_mkheader(kvm_t *kd, off_t dump_off)
    490   1.40       leo {
    491   1.43       gwr 	kcore_seg_t	cpu_hdr;
    492   1.60   thorpej 	size_t hdr_size;
    493   1.60   thorpej 	ssize_t sz;
    494   1.40       leo 
    495   1.41       leo 	if (kd->kcore_hdr != NULL) {
    496   1.41       leo 	    _kvm_err(kd, kd->program, "already has a dump header");
    497   1.40       leo 	    return (-1);
    498   1.40       leo 	}
    499   1.41       leo 	if (ISALIVE(kd)) {
    500   1.41       leo 		_kvm_err(kd, kd->program, "don't use on live kernel");
    501   1.40       leo 		return (-1);
    502   1.40       leo 	}
    503   1.40       leo 
    504   1.40       leo 	/*
    505   1.43       gwr 	 * Validate new format crash dump
    506   1.40       leo 	 */
    507   1.58   thorpej 	sz = Pread(kd, kd->pmfd, &cpu_hdr, sizeof(cpu_hdr), dump_off);
    508  1.100    martin 	if (sz != sizeof(cpu_hdr)) {
    509  1.102       mrg 		if (sz == -1)
    510  1.102       mrg 			_kvm_err(kd, 0, "read %zx bytes at offset %"PRIx64
    511  1.102       mrg 			    " for cpu_hdr failed: %s", sizeof(cpu_hdr),
    512  1.102       mrg 			    dump_off, strerror(errno));
    513  1.102       mrg 		else
    514  1.102       mrg 			_kvm_err(kd, 0, "read %zx bytes at offset %"PRIx64
    515  1.102       mrg 			    " for cpu_hdr instead of requested %zu",
    516  1.102       mrg 			    sz, dump_off, sizeof(cpu_hdr));
    517   1.43       gwr 		return (-1);
    518  1.100    martin 	}
    519   1.44       leo 	if ((CORE_GETMAGIC(cpu_hdr) != KCORE_MAGIC)
    520   1.44       leo 		|| (CORE_GETMID(cpu_hdr) != MID_MACHINE)) {
    521   1.44       leo 		_kvm_err(kd, 0, "invalid magic in cpu_hdr");
    522   1.45       leo 		return (0);
    523   1.44       leo 	}
    524   1.43       gwr 	hdr_size = ALIGN(sizeof(cpu_hdr));
    525   1.43       gwr 
    526   1.43       gwr 	/*
    527   1.43       gwr 	 * Read the CPU segment.
    528   1.43       gwr 	 */
    529   1.43       gwr 	kd->cpu_dsize = cpu_hdr.c_size;
    530   1.43       gwr 	kd->cpu_data = _kvm_malloc(kd, kd->cpu_dsize);
    531  1.100    martin 	if (kd->cpu_data == NULL) {
    532  1.100    martin 		_kvm_err(kd, kd->program, "no cpu_data");
    533   1.43       gwr 		goto fail;
    534  1.100    martin 	}
    535   1.58   thorpej 	sz = Pread(kd, kd->pmfd, kd->cpu_data, cpu_hdr.c_size,
    536   1.58   thorpej 	    dump_off + hdr_size);
    537  1.100    martin 	if (sz != cpu_hdr.c_size) {
    538  1.100    martin 		_kvm_err(kd, kd->program, "size %zu != cpu_hdr.csize %"PRIu32,
    539  1.100    martin 		    sz, cpu_hdr.c_size);
    540   1.43       gwr 		goto fail;
    541  1.100    martin 	}
    542   1.43       gwr 	hdr_size += kd->cpu_dsize;
    543   1.43       gwr 
    544   1.43       gwr 	/*
    545   1.43       gwr 	 * Leave phys mem pointer at beginning of memory data
    546   1.43       gwr 	 */
    547   1.43       gwr 	kd->dump_off = dump_off + hdr_size;
    548  1.100    martin 	if (Lseek(kd, kd->pmfd, kd->dump_off, SEEK_SET) == -1) {
    549  1.100    martin 		_kvm_err(kd, kd->program, "failed to seek to %" PRId64,
    550  1.100    martin 		    (int64_t)kd->dump_off);
    551   1.43       gwr 		goto fail;
    552  1.100    martin 	}
    553   1.40       leo 
    554   1.40       leo 	/*
    555   1.40       leo 	 * Create a kcore_hdr.
    556   1.40       leo 	 */
    557   1.43       gwr 	kd->kcore_hdr = _kvm_malloc(kd, sizeof(kcore_hdr_t));
    558  1.100    martin 	if (kd->kcore_hdr == NULL) {
    559  1.100    martin 		_kvm_err(kd, kd->program, "failed to allocate header");
    560   1.43       gwr 		goto fail;
    561  1.100    martin 	}
    562   1.40       leo 
    563   1.41       leo 	kd->kcore_hdr->c_hdrsize    = ALIGN(sizeof(kcore_hdr_t));
    564   1.41       leo 	kd->kcore_hdr->c_seghdrsize = ALIGN(sizeof(kcore_seg_t));
    565   1.41       leo 	kd->kcore_hdr->c_nseg       = 2;
    566   1.41       leo 	CORE_SETMAGIC(*(kd->kcore_hdr), KCORE_MAGIC, MID_MACHINE,0);
    567   1.40       leo 
    568   1.40       leo 	/*
    569   1.40       leo 	 * Now that we have a valid header, enable translations.
    570   1.40       leo 	 */
    571   1.49        pk 	if (_kvm_initvtop(kd) == 0)
    572   1.49        pk 		/* Success */
    573   1.49        pk 		return (hdr_size);
    574   1.43       gwr 
    575   1.43       gwr fail:
    576   1.43       gwr 	if (kd->kcore_hdr != NULL) {
    577   1.43       gwr 		free(kd->kcore_hdr);
    578   1.43       gwr 		kd->kcore_hdr = NULL;
    579   1.43       gwr 	}
    580   1.43       gwr 	if (kd->cpu_data != NULL) {
    581   1.43       gwr 		free(kd->cpu_data);
    582   1.43       gwr 		kd->cpu_data = NULL;
    583   1.43       gwr 		kd->cpu_dsize = 0;
    584   1.43       gwr 	}
    585   1.43       gwr 	return (-1);
    586   1.40       leo }
    587   1.40       leo 
    588   1.40       leo static int
    589   1.89     joerg clear_gap(kvm_t *kd, bool (*write_buf)(void *, const void *, size_t),
    590   1.89     joerg     void *cookie, size_t size)
    591   1.40       leo {
    592   1.89     joerg 	char buf[1024];
    593   1.89     joerg 	size_t len;
    594   1.89     joerg 
    595   1.89     joerg 	(void)memset(buf, 0, size > sizeof(buf) ? sizeof(buf) : size);
    596   1.89     joerg 
    597   1.89     joerg 	while (size > 0) {
    598   1.89     joerg 		len = size > sizeof(buf) ? sizeof(buf) : size;
    599   1.89     joerg 		if (!(*write_buf)(cookie, buf, len)) {
    600   1.40       leo 			_kvm_syserr(kd, kd->program, "clear_gap");
    601   1.89     joerg 			return -1;
    602   1.40       leo 		}
    603   1.89     joerg 		size -= len;
    604   1.89     joerg 	}
    605   1.89     joerg 
    606   1.89     joerg 	return 0;
    607   1.40       leo }
    608   1.40       leo 
    609   1.40       leo /*
    610   1.89     joerg  * Write the dump header by calling write_buf with cookie as first argument.
    611   1.40       leo  */
    612   1.40       leo int
    613   1.89     joerg kvm_dump_header(kvm_t *kd, bool (*write_buf)(void *, const void *, size_t),
    614   1.89     joerg     void *cookie, int dumpsize)
    615   1.40       leo {
    616   1.40       leo 	kcore_seg_t	seghdr;
    617   1.40       leo 	long		offset;
    618   1.89     joerg 	size_t		gap;
    619   1.40       leo 
    620   1.43       gwr 	if (kd->kcore_hdr == NULL || kd->cpu_data == NULL) {
    621   1.40       leo 		_kvm_err(kd, kd->program, "no valid dump header(s)");
    622   1.40       leo 		return (-1);
    623   1.40       leo 	}
    624   1.40       leo 
    625   1.40       leo 	/*
    626   1.40       leo 	 * Write the generic header
    627   1.40       leo 	 */
    628   1.40       leo 	offset = 0;
    629   1.89     joerg 	if (!(*write_buf)(cookie, kd->kcore_hdr, sizeof(kcore_hdr_t))) {
    630   1.89     joerg 		_kvm_syserr(kd, kd->program, "kvm_dump_header");
    631   1.40       leo 		return (-1);
    632   1.40       leo 	}
    633   1.40       leo 	offset += kd->kcore_hdr->c_hdrsize;
    634   1.40       leo 	gap     = kd->kcore_hdr->c_hdrsize - sizeof(kcore_hdr_t);
    635   1.89     joerg 	if (clear_gap(kd, write_buf, cookie, gap) == -1)
    636   1.40       leo 		return (-1);
    637   1.40       leo 
    638   1.40       leo 	/*
    639   1.83       wiz 	 * Write the CPU header
    640   1.40       leo 	 */
    641   1.40       leo 	CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_CPU);
    642   1.43       gwr 	seghdr.c_size = ALIGN(kd->cpu_dsize);
    643   1.89     joerg 	if (!(*write_buf)(cookie, &seghdr, sizeof(seghdr))) {
    644   1.89     joerg 		_kvm_syserr(kd, kd->program, "kvm_dump_header");
    645   1.40       leo 		return (-1);
    646   1.40       leo 	}
    647   1.40       leo 	offset += kd->kcore_hdr->c_seghdrsize;
    648   1.40       leo 	gap     = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
    649   1.89     joerg 	if (clear_gap(kd, write_buf, cookie, gap) == -1)
    650   1.40       leo 		return (-1);
    651   1.40       leo 
    652   1.89     joerg 	if (!(*write_buf)(cookie, kd->cpu_data, kd->cpu_dsize)) {
    653   1.89     joerg 		_kvm_syserr(kd, kd->program, "kvm_dump_header");
    654   1.40       leo 		return (-1);
    655   1.40       leo 	}
    656   1.40       leo 	offset += seghdr.c_size;
    657   1.43       gwr 	gap     = seghdr.c_size - kd->cpu_dsize;
    658   1.90     joerg 	if (clear_gap(kd, write_buf, cookie, gap) == -1)
    659   1.40       leo 		return (-1);
    660   1.40       leo 
    661   1.40       leo 	/*
    662   1.40       leo 	 * Write the actual dump data segment header
    663   1.40       leo 	 */
    664   1.40       leo 	CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_DATA);
    665   1.40       leo 	seghdr.c_size = dumpsize;
    666   1.89     joerg 	if (!(*write_buf)(cookie, &seghdr, sizeof(seghdr))) {
    667   1.89     joerg 		_kvm_syserr(kd, kd->program, "kvm_dump_header");
    668   1.40       leo 		return (-1);
    669   1.40       leo 	}
    670   1.40       leo 	offset += kd->kcore_hdr->c_seghdrsize;
    671   1.40       leo 	gap     = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
    672   1.89     joerg 	if (clear_gap(kd, write_buf, cookie, gap) == -1)
    673   1.40       leo 		return (-1);
    674   1.40       leo 
    675   1.62  christos 	return (int)offset;
    676   1.40       leo }
    677   1.40       leo 
    678   1.89     joerg static bool
    679   1.89     joerg kvm_dump_header_stdio(void *cookie, const void *buf, size_t len)
    680   1.89     joerg {
    681   1.89     joerg 	return fwrite(buf, len, 1, (FILE *)cookie) == 1;
    682   1.89     joerg }
    683   1.89     joerg 
    684   1.89     joerg int
    685   1.89     joerg kvm_dump_wrtheader(kvm_t *kd, FILE *fp, int dumpsize)
    686   1.89     joerg {
    687   1.89     joerg 	return kvm_dump_header(kd, kvm_dump_header_stdio, fp, dumpsize);
    688   1.89     joerg }
    689   1.89     joerg 
    690   1.35       cgd kvm_t *
    691   1.88     joerg kvm_openfiles(const char *uf, const char *mf, const char *sf,
    692   1.88     joerg     int flag, char *errout)
    693   1.35       cgd {
    694   1.55     perry 	kvm_t *kd;
    695   1.35       cgd 
    696   1.35       cgd 	if ((kd = malloc(sizeof(*kd))) == NULL) {
    697   1.78    itojun 		(void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
    698   1.35       cgd 		return (0);
    699   1.35       cgd 	}
    700   1.35       cgd 	kd->program = 0;
    701   1.35       cgd 	return (_kvm_open(kd, uf, mf, sf, flag, errout));
    702   1.35       cgd }
    703   1.35       cgd 
    704   1.35       cgd kvm_t *
    705   1.88     joerg kvm_open(const char *uf, const char *mf, const char *sf, int flag,
    706   1.88     joerg     const char *program)
    707   1.35       cgd {
    708   1.55     perry 	kvm_t *kd;
    709   1.35       cgd 
    710   1.86  christos 	if ((kd = malloc(sizeof(*kd))) == NULL) {
    711   1.86  christos 		(void)fprintf(stderr, "%s: %s\n",
    712   1.86  christos 		    program ? program : getprogname(), strerror(errno));
    713   1.35       cgd 		return (0);
    714   1.19   mycroft 	}
    715   1.35       cgd 	kd->program = program;
    716   1.35       cgd 	return (_kvm_open(kd, uf, mf, sf, flag, NULL));
    717    1.8       cgd }
    718    1.8       cgd 
    719    1.8       cgd int
    720   1.88     joerg kvm_close(kvm_t *kd)
    721    1.1       cgd {
    722   1.55     perry 	int error = 0;
    723   1.24   mycroft 
    724   1.35       cgd 	if (kd->pmfd >= 0)
    725   1.35       cgd 		error |= close(kd->pmfd);
    726   1.35       cgd 	if (kd->vmfd >= 0)
    727   1.35       cgd 		error |= close(kd->vmfd);
    728   1.35       cgd 	if (kd->nlfd >= 0)
    729   1.35       cgd 		error |= close(kd->nlfd);
    730   1.35       cgd 	if (kd->swfd >= 0)
    731   1.35       cgd 		error |= close(kd->swfd);
    732   1.35       cgd 	if (kd->vmst)
    733   1.35       cgd 		_kvm_freevtop(kd);
    734   1.43       gwr 	kd->cpu_dsize = 0;
    735   1.43       gwr 	if (kd->cpu_data != NULL)
    736   1.91        ad 		free(kd->cpu_data);
    737   1.40       leo 	if (kd->kcore_hdr != NULL)
    738   1.91        ad 		free(kd->kcore_hdr);
    739   1.35       cgd 	if (kd->procbase != 0)
    740   1.91        ad 		free(kd->procbase);
    741   1.65    simonb 	if (kd->procbase2 != 0)
    742   1.91        ad 		free(kd->procbase2);
    743   1.79   thorpej 	if (kd->lwpbase != 0)
    744   1.91        ad 		free(kd->lwpbase);
    745   1.36   mycroft 	if (kd->swapspc != 0)
    746   1.91        ad 		free(kd->swapspc);
    747   1.36   mycroft 	if (kd->argspc != 0)
    748   1.91        ad 		free(kd->argspc);
    749   1.38   mycroft 	if (kd->argbuf != 0)
    750   1.91        ad 		free(kd->argbuf);
    751   1.35       cgd 	if (kd->argv != 0)
    752   1.91        ad 		free(kd->argv);
    753   1.91        ad 	if (kd->iobuf != 0)
    754   1.91        ad 		free(kd->iobuf);
    755   1.91        ad 	free(kd);
    756   1.19   mycroft 
    757   1.97  dholland 	return (error);
    758    1.1       cgd }
    759    1.1       cgd 
    760   1.18   mycroft int
    761   1.88     joerg kvm_nlist(kvm_t *kd, struct nlist *nl)
    762   1.18   mycroft {
    763  1.103      maxv 	int rv;
    764    1.3       cgd 
    765   1.35       cgd 	/*
    766   1.80     ragge 	 * Call the nlist(3) routines to retrieve the given namelist.
    767   1.35       cgd 	 */
    768  1.103      maxv 	rv = __fdnlist(kd->nlfd, nl);
    769   1.82      cube 
    770   1.80     ragge 	if (rv == -1)
    771   1.80     ragge 		_kvm_err(kd, 0, "bad namelist");
    772   1.82      cube 
    773   1.80     ragge 	return (rv);
    774   1.18   mycroft }
    775    1.3       cgd 
    776   1.88     joerg int
    777   1.88     joerg kvm_dump_inval(kvm_t *kd)
    778   1.40       leo {
    779   1.60   thorpej 	struct nlist	nl[2];
    780   1.95       jym 	paddr_t		pa;
    781   1.92        ad 	size_t		dsize;
    782   1.92        ad 	off_t		doff;
    783   1.92        ad 	void		*newbuf;
    784   1.40       leo 
    785   1.40       leo 	if (ISALIVE(kd)) {
    786   1.40       leo 		_kvm_err(kd, kd->program, "clearing dump on live kernel");
    787   1.40       leo 		return (-1);
    788   1.40       leo 	}
    789   1.60   thorpej 	nl[0].n_name = "_dumpmag";
    790   1.60   thorpej 	nl[1].n_name = NULL;
    791   1.40       leo 
    792   1.60   thorpej 	if (kvm_nlist(kd, nl) == -1) {
    793   1.40       leo 		_kvm_err(kd, 0, "bad namelist");
    794   1.40       leo 		return (-1);
    795   1.40       leo 	}
    796   1.95       jym 	if (_kvm_kvatop(kd, (vaddr_t)nl[0].n_value, &pa) == 0)
    797   1.40       leo 		return (-1);
    798   1.40       leo 
    799   1.40       leo 	errno = 0;
    800   1.92        ad 	dsize = MAX(kd->fdalign, sizeof(u_long));
    801   1.92        ad 	if (kd->iobufsz < dsize) {
    802   1.92        ad 		newbuf = realloc(kd->iobuf, dsize);
    803   1.92        ad 		if (newbuf == NULL) {
    804   1.92        ad 			_kvm_syserr(kd, 0, "cannot allocate I/O buffer");
    805   1.92        ad 			return (-1);
    806   1.92        ad 		}
    807   1.92        ad 		kd->iobuf = newbuf;
    808   1.92        ad 		kd->iobufsz = dsize;
    809   1.92        ad 	}
    810   1.92        ad 	memset(kd->iobuf, 0, dsize);
    811   1.92        ad 	doff = _kvm_pa2off(kd, pa);
    812   1.92        ad 	doff -= doff % kd->fdalign;
    813   1.92        ad 	if (pwrite(kd->pmfd, kd->iobuf, dsize, doff) == -1) {
    814   1.57   thorpej 		_kvm_syserr(kd, 0, "cannot invalidate dump - pwrite");
    815   1.40       leo 		return (-1);
    816   1.40       leo 	}
    817   1.40       leo 	return (0);
    818   1.40       leo }
    819   1.40       leo 
    820   1.35       cgd ssize_t
    821   1.88     joerg kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
    822    1.3       cgd {
    823   1.55     perry 	int cc;
    824   1.55     perry 	void *cp;
    825    1.3       cgd 
    826   1.65    simonb 	if (ISKMEM(kd)) {
    827   1.35       cgd 		/*
    828   1.35       cgd 		 * We're using /dev/kmem.  Just read straight from the
    829   1.35       cgd 		 * device and let the active kernel do the address translation.
    830   1.35       cgd 		 */
    831   1.35       cgd 		errno = 0;
    832   1.91        ad 		cc = _kvm_pread(kd, kd->vmfd, buf, len, (off_t)kva);
    833   1.35       cgd 		if (cc < 0) {
    834   1.35       cgd 			_kvm_syserr(kd, 0, "kvm_read");
    835   1.56   msaitoh 			return (-1);
    836   1.35       cgd 		} else if (cc < len)
    837   1.35       cgd 			_kvm_err(kd, kd->program, "short read");
    838   1.35       cgd 		return (cc);
    839   1.65    simonb 	} else if (ISSYSCTL(kd)) {
    840   1.65    simonb 		_kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
    841   1.65    simonb 		    "can't use kvm_read");
    842   1.65    simonb 		return (-1);
    843   1.35       cgd 	} else {
    844   1.43       gwr 		if ((kd->kcore_hdr == NULL) || (kd->cpu_data == NULL)) {
    845   1.40       leo 			_kvm_err(kd, kd->program, "no valid dump header");
    846   1.56   msaitoh 			return (-1);
    847   1.40       leo 		}
    848   1.35       cgd 		cp = buf;
    849   1.35       cgd 		while (len > 0) {
    850   1.95       jym 			paddr_t	pa;
    851   1.40       leo 			off_t	foff;
    852   1.64    simonb 
    853   1.95       jym 			cc = _kvm_kvatop(kd, (vaddr_t)kva, &pa);
    854   1.35       cgd 			if (cc == 0)
    855   1.56   msaitoh 				return (-1);
    856   1.35       cgd 			if (cc > len)
    857   1.35       cgd 				cc = len;
    858   1.40       leo 			foff = _kvm_pa2off(kd, pa);
    859   1.35       cgd 			errno = 0;
    860   1.91        ad 			cc = _kvm_pread(kd, kd->pmfd, cp, (size_t)cc, foff);
    861   1.35       cgd 			if (cc < 0) {
    862   1.35       cgd 				_kvm_syserr(kd, kd->program, "kvm_read");
    863   1.35       cgd 				break;
    864   1.35       cgd 			}
    865   1.35       cgd 			/*
    866   1.35       cgd 			 * If kvm_kvatop returns a bogus value or our core
    867   1.35       cgd 			 * file is truncated, we might wind up seeking beyond
    868   1.35       cgd 			 * the end of the core file in which case the read will
    869   1.35       cgd 			 * return 0 (EOF).
    870   1.35       cgd 			 */
    871   1.35       cgd 			if (cc == 0)
    872   1.35       cgd 				break;
    873   1.39       cgd 			cp = (char *)cp + cc;
    874   1.35       cgd 			kva += cc;
    875   1.35       cgd 			len -= cc;
    876    1.3       cgd 		}
    877   1.35       cgd 		return ((char *)cp - (char *)buf);
    878    1.3       cgd 	}
    879   1.35       cgd 	/* NOTREACHED */
    880    1.3       cgd }
    881    1.3       cgd 
    882   1.35       cgd ssize_t
    883   1.88     joerg kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
    884   1.35       cgd {
    885   1.55     perry 	int cc;
    886   1.26        pk 
    887   1.65    simonb 	if (ISKMEM(kd)) {
    888   1.35       cgd 		/*
    889   1.35       cgd 		 * Just like kvm_read, only we write.
    890   1.35       cgd 		 */
    891   1.35       cgd 		errno = 0;
    892   1.57   thorpej 		cc = pwrite(kd->vmfd, buf, len, (off_t)kva);
    893   1.35       cgd 		if (cc < 0) {
    894   1.35       cgd 			_kvm_syserr(kd, 0, "kvm_write");
    895   1.56   msaitoh 			return (-1);
    896   1.35       cgd 		} else if (cc < len)
    897   1.35       cgd 			_kvm_err(kd, kd->program, "short write");
    898   1.35       cgd 		return (cc);
    899   1.65    simonb 	} else if (ISSYSCTL(kd)) {
    900   1.65    simonb 		_kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
    901   1.65    simonb 		    "can't use kvm_write");
    902   1.65    simonb 		return (-1);
    903   1.35       cgd 	} else {
    904   1.35       cgd 		_kvm_err(kd, kd->program,
    905   1.35       cgd 		    "kvm_write not implemented for dead kernels");
    906   1.56   msaitoh 		return (-1);
    907   1.26        pk 	}
    908   1.35       cgd 	/* NOTREACHED */
    909    1.1       cgd }
    910