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