Home | History | Annotate | Line # | Download | only in libkvm
kvm.c revision 1.19
      1   1.1      cgd /*-
      2   1.9      cgd  * Copyright (c) 1993 Christopher G. Demetriou
      3   1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      4   1.1      cgd  * All rights reserved.
      5   1.1      cgd  *
      6   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      7   1.1      cgd  * modification, are permitted provided that the following conditions
      8   1.1      cgd  * are met:
      9   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     10   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     11   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     13   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     14   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     15   1.1      cgd  *    must display the following acknowledgement:
     16   1.1      cgd  *	This product includes software developed by the University of
     17   1.1      cgd  *	California, Berkeley and its contributors.
     18   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     19   1.1      cgd  *    may be used to endorse or promote products derived from this software
     20   1.1      cgd  *    without specific prior written permission.
     21   1.1      cgd  *
     22   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1      cgd  * SUCH DAMAGE.
     33   1.1      cgd  */
     34   1.1      cgd 
     35   1.1      cgd #if defined(LIBC_SCCS) && !defined(lint)
     36  1.12  mycroft /*static char sccsid[] = "from: @(#)kvm.c	5.18 (Berkeley) 5/7/91";*/
     37  1.19  mycroft static char rcsid[] = "$Id: kvm.c,v 1.19 1993/10/02 07:07:43 mycroft Exp $";
     38   1.1      cgd #endif /* LIBC_SCCS and not lint */
     39   1.1      cgd 
     40   1.1      cgd #include <sys/param.h>
     41   1.1      cgd #include <sys/user.h>
     42   1.1      cgd #include <sys/proc.h>
     43   1.1      cgd #include <sys/ioctl.h>
     44   1.1      cgd #include <sys/kinfo.h>
     45   1.1      cgd #include <sys/tty.h>
     46   1.8      cgd #include <sys/exec.h>
     47   1.1      cgd #include <machine/vmparam.h>
     48   1.1      cgd #include <fcntl.h>
     49   1.1      cgd #include <nlist.h>
     50   1.1      cgd #include <kvm.h>
     51   1.1      cgd #include <ndbm.h>
     52   1.1      cgd #include <limits.h>
     53   1.1      cgd #include <paths.h>
     54   1.1      cgd #include <stdio.h>
     55   1.1      cgd #include <string.h>
     56   1.1      cgd 
     57   1.1      cgd #define	btop(x)		(((unsigned)(x)) >> PGSHIFT)	/* XXX */
     58   1.1      cgd #define	ptob(x)		((caddr_t)((x) << PGSHIFT))	/* XXX */
     59   1.1      cgd #include <vm/vm.h>	/* ??? kinfo_proc currently includes this*/
     60   1.3      cgd #include <vm/vm_page.h>
     61   1.3      cgd #include <vm/swap_pager.h>
     62   1.1      cgd #include <sys/kinfo_proc.h>
     63  1.16  mycroft #if defined(hp300) || defined(amiga)
     64  1.13  mycroft #include <machine/pte.h>
     65  1.16  mycroft #define	btos(x)		(((unsigned)(x)) >> SEGSHIFT)	/* XXX */
     66   1.1      cgd #endif
     67   1.1      cgd 
     68   1.1      cgd /*
     69   1.1      cgd  * files
     70   1.1      cgd  */
     71   1.1      cgd static	const char *unixf, *memf, *kmemf, *swapf;
     72   1.1      cgd static	int unixx, mem, kmem, swap;
     73   1.1      cgd static	DBM *db;
     74   1.1      cgd /*
     75   1.1      cgd  * flags
     76   1.1      cgd  */
     77   1.1      cgd static	int deadkernel;
     78   1.1      cgd static	int kvminit = 0;
     79   1.1      cgd static	int kvmfilesopen = 0;
     80   1.1      cgd /*
     81   1.1      cgd  * state
     82   1.1      cgd  */
     83   1.1      cgd static	struct kinfo_proc *kvmprocbase, *kvmprocptr;
     84   1.1      cgd static	int kvmnprocs;
     85   1.1      cgd /*
     86   1.1      cgd  * u. buffer
     87   1.1      cgd  */
     88   1.1      cgd static union {
     89   1.1      cgd 	struct	user user;
     90   1.1      cgd 	char	upages[UPAGES][NBPG];
     91   1.1      cgd } user;
     92   1.3      cgd 
     93   1.3      cgd struct swapblk {
     94   1.3      cgd 	long	offset;		/* offset in swap device */
     95   1.3      cgd 	long	size;		/* remaining size of block in swap device */
     96   1.3      cgd };
     97  1.19  mycroft 
     98   1.1      cgd /*
     99   1.1      cgd  * random other stuff
    100   1.1      cgd  */
    101   1.1      cgd static	int	dmmin, dmmax;
    102   1.1      cgd static	int	pcbpf;
    103   1.1      cgd static	int	nswap;
    104  1.19  mycroft static	long	vm_page_hash_mask;
    105  1.19  mycroft static	long	vm_page_buckets;
    106  1.19  mycroft static	long	page_shift;
    107   1.1      cgd static	char	*tmp;
    108  1.16  mycroft #if defined(hp300) || defined(amiga)
    109   1.1      cgd static	int	lowram;
    110   1.1      cgd static	struct ste *Sysseg;
    111   1.1      cgd #endif
    112   1.1      cgd #if defined(i386)
    113   1.1      cgd static	struct pde *PTD;
    114   1.1      cgd #endif
    115   1.1      cgd 
    116  1.19  mycroft #define atop(x)		(((unsigned)(x)) >> page_shift)
    117  1.19  mycroft #define vm_page_hash(object, offset) \
    118  1.19  mycroft         (((unsigned)object+(unsigned)atop(offset))&vm_page_hash_mask)
    119  1.19  mycroft 
    120   1.1      cgd #define basename(cp)	((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
    121   1.1      cgd #define	MAXSYMSIZE	256
    122   1.1      cgd 
    123   1.1      cgd static struct nlist nl[] = {
    124   1.1      cgd 	{ "_Usrptmap" },
    125   1.1      cgd #define	X_USRPTMAP	0
    126   1.1      cgd 	{ "_usrpt" },
    127   1.1      cgd #define	X_USRPT		1
    128   1.1      cgd 	{ "_nswap" },
    129   1.1      cgd #define	X_NSWAP		2
    130   1.1      cgd 	{ "_dmmin" },
    131   1.1      cgd #define	X_DMMIN		3
    132   1.1      cgd 	{ "_dmmax" },
    133   1.1      cgd #define	X_DMMAX		4
    134   1.3      cgd 	{ "_vm_page_buckets" },
    135   1.3      cgd #define X_VM_PAGE_BUCKETS	5
    136   1.3      cgd 	{ "_vm_page_hash_mask" },
    137   1.3      cgd #define X_VM_PAGE_HASH_MASK	6
    138   1.3      cgd 	{ "_page_shift" },
    139   1.3      cgd #define X_PAGE_SHIFT	7
    140   1.1      cgd 	/*
    141   1.1      cgd 	 * everything here and down, only if a dead kernel
    142   1.1      cgd 	 */
    143   1.1      cgd 	{ "_Sysmap" },
    144   1.3      cgd #define	X_SYSMAP	8
    145   1.1      cgd #define	X_DEADKERNEL	X_SYSMAP
    146   1.1      cgd 	{ "_Syssize" },
    147   1.3      cgd #define	X_SYSSIZE	9
    148   1.1      cgd 	{ "_allproc" },
    149   1.3      cgd #define X_ALLPROC	10
    150   1.1      cgd 	{ "_zombproc" },
    151   1.3      cgd #define X_ZOMBPROC	11
    152   1.1      cgd 	{ "_nproc" },
    153   1.3      cgd #define	X_NPROC		12
    154   1.3      cgd #define	X_LAST		12
    155  1.16  mycroft #if defined(hp300) || defined(amiga)
    156   1.1      cgd 	{ "_Sysseg" },
    157   1.1      cgd #define	X_SYSSEG	(X_LAST+1)
    158   1.1      cgd 	{ "_lowram" },
    159   1.1      cgd #define	X_LOWRAM	(X_LAST+2)
    160   1.1      cgd #endif
    161   1.1      cgd #if defined(i386)
    162   1.1      cgd 	{ "_IdlePTD" },
    163   1.1      cgd #define	X_IdlePTD	(X_LAST+1)
    164   1.1      cgd #endif
    165   1.1      cgd 	{ "" },
    166   1.1      cgd };
    167   1.1      cgd 
    168   1.1      cgd static off_t Vtophys();
    169   1.1      cgd static void klseek(), seterr(), setsyserr(), vstodb();
    170   1.1      cgd static int getkvars(), kvm_doprocs(), kvm_init();
    171   1.3      cgd static int vatosw();
    172  1.19  mycroft static int pager_get();
    173   1.3      cgd static int findpage();
    174   1.1      cgd 
    175   1.1      cgd /*
    176   1.1      cgd  * returns 	0 if files were opened now,
    177   1.1      cgd  * 		1 if files were already opened,
    178   1.1      cgd  *		-1 if files could not be opened.
    179   1.1      cgd  */
    180   1.1      cgd kvm_openfiles(uf, mf, sf)
    181   1.1      cgd 	const char *uf, *mf, *sf;
    182   1.1      cgd {
    183   1.1      cgd 	if (kvmfilesopen)
    184   1.1      cgd 		return (1);
    185   1.1      cgd 	unixx = mem = kmem = swap = -1;
    186   1.1      cgd 	unixf = (uf == NULL) ? _PATH_UNIX : uf;
    187   1.1      cgd 	memf = (mf == NULL) ? _PATH_MEM : mf;
    188   1.1      cgd 
    189   1.1      cgd 	if ((unixx = open(unixf, O_RDONLY, 0)) == -1) {
    190   1.1      cgd 		setsyserr("can't open %s", unixf);
    191   1.1      cgd 		goto failed;
    192   1.1      cgd 	}
    193   1.1      cgd 	if ((mem = open(memf, O_RDONLY, 0)) == -1) {
    194   1.1      cgd 		setsyserr("can't open %s", memf);
    195   1.1      cgd 		goto failed;
    196   1.1      cgd 	}
    197   1.1      cgd 	if (sf != NULL)
    198   1.1      cgd 		swapf = sf;
    199   1.1      cgd 	if (mf != NULL) {
    200   1.1      cgd 		deadkernel++;
    201   1.1      cgd 		kmemf = mf;
    202   1.1      cgd 		kmem = mem;
    203   1.1      cgd 		swap = -1;
    204   1.1      cgd 	} else {
    205   1.1      cgd 		kmemf = _PATH_KMEM;
    206   1.1      cgd 		if ((kmem = open(kmemf, O_RDONLY, 0)) == -1) {
    207   1.1      cgd 			setsyserr("can't open %s", kmemf);
    208   1.1      cgd 			goto failed;
    209   1.1      cgd 		}
    210   1.1      cgd 		swapf = (sf == NULL) ?  _PATH_DRUM : sf;
    211   1.1      cgd 		/*
    212   1.1      cgd 		 * live kernel - avoid looking up nlist entries
    213   1.1      cgd 		 * past X_DEADKERNEL.
    214   1.1      cgd 		 */
    215   1.1      cgd 		nl[X_DEADKERNEL].n_name = "";
    216   1.1      cgd 	}
    217   1.1      cgd 	if (swapf != NULL && ((swap = open(swapf, O_RDONLY, 0)) == -1)) {
    218   1.1      cgd 		seterr("can't open %s", swapf);
    219   1.1      cgd 		goto failed;
    220   1.1      cgd 	}
    221   1.1      cgd 	kvmfilesopen++;
    222   1.1      cgd 	if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1) /*XXX*/
    223   1.1      cgd 		return (-1);
    224   1.1      cgd 	return (0);
    225   1.1      cgd failed:
    226   1.1      cgd 	kvm_close();
    227   1.1      cgd 	return (-1);
    228   1.1      cgd }
    229   1.1      cgd 
    230   1.1      cgd static
    231   1.1      cgd kvm_init(uf, mf, sf)
    232   1.1      cgd 	char *uf, *mf, *sf;
    233   1.1      cgd {
    234   1.1      cgd 	if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
    235   1.1      cgd 		return (-1);
    236   1.1      cgd 	if (getkvars() == -1)
    237   1.1      cgd 		return (-1);
    238   1.1      cgd 	kvminit = 1;
    239   1.1      cgd 
    240   1.1      cgd 	return (0);
    241   1.1      cgd }
    242   1.1      cgd 
    243   1.1      cgd kvm_close()
    244   1.1      cgd {
    245   1.1      cgd 	if (unixx != -1) {
    246   1.1      cgd 		close(unixx);
    247   1.1      cgd 		unixx = -1;
    248   1.1      cgd 	}
    249   1.1      cgd 	if (kmem != -1) {
    250   1.1      cgd 		if (kmem != mem)
    251   1.1      cgd 			close(kmem);
    252   1.1      cgd 		/* otherwise kmem is a copy of mem, and will be closed below */
    253   1.1      cgd 		kmem = -1;
    254   1.1      cgd 	}
    255   1.1      cgd 	if (mem != -1) {
    256   1.1      cgd 		close(mem);
    257   1.1      cgd 		mem = -1;
    258   1.1      cgd 	}
    259   1.1      cgd 	if (swap != -1) {
    260   1.1      cgd 		close(swap);
    261   1.1      cgd 		swap = -1;
    262   1.1      cgd 	}
    263   1.1      cgd 	if (db != NULL) {
    264   1.1      cgd 		dbm_close(db);
    265   1.1      cgd 		db = NULL;
    266   1.1      cgd 	}
    267   1.1      cgd 	kvminit = 0;
    268   1.1      cgd 	kvmfilesopen = 0;
    269   1.1      cgd 	deadkernel = 0;
    270   1.1      cgd }
    271   1.1      cgd 
    272   1.1      cgd kvm_nlist(nl)
    273   1.1      cgd 	struct nlist *nl;
    274   1.1      cgd {
    275   1.1      cgd 	datum key, data;
    276   1.1      cgd 	char dbname[MAXPATHLEN];
    277   1.1      cgd 	char dbversion[_POSIX2_LINE_MAX];
    278   1.1      cgd 	char kversion[_POSIX2_LINE_MAX];
    279   1.1      cgd 	int dbversionlen;
    280   1.1      cgd 	char symbuf[MAXSYMSIZE];
    281   1.1      cgd 	struct nlist nbuf, *n;
    282   1.1      cgd 	int num, did;
    283   1.1      cgd 
    284   1.1      cgd 	if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
    285   1.1      cgd 		return (-1);
    286   1.1      cgd 	if (deadkernel)
    287   1.1      cgd 		goto hard2;
    288   1.1      cgd 	/*
    289   1.1      cgd 	 * initialize key datum
    290   1.1      cgd 	 */
    291   1.1      cgd 	key.dptr = symbuf;
    292   1.1      cgd 
    293   1.1      cgd 	if (db != NULL)
    294   1.1      cgd 		goto win;	/* off to the races */
    295   1.1      cgd 	/*
    296   1.1      cgd 	 * open database
    297   1.1      cgd 	 */
    298   1.1      cgd 	sprintf(dbname, "%s/kvm_%s", _PATH_VARRUN, basename(unixf));
    299   1.1      cgd 	if ((db = dbm_open(dbname, O_RDONLY, 0)) == NULL)
    300   1.1      cgd 		goto hard2;
    301   1.1      cgd 	/*
    302   1.1      cgd 	 * read version out of database
    303   1.1      cgd 	 */
    304   1.1      cgd 	bcopy("VERSION", symbuf, sizeof ("VERSION")-1);
    305   1.2      cgd 	key.dsize = (sizeof ("VERSION") - 1);
    306   1.1      cgd 	data = dbm_fetch(db, key);
    307   1.1      cgd 	if (data.dptr == NULL)
    308   1.1      cgd 		goto hard1;
    309   1.1      cgd 	bcopy(data.dptr, dbversion, data.dsize);
    310   1.1      cgd 	dbversionlen = data.dsize;
    311   1.1      cgd 	/*
    312   1.1      cgd 	 * read version string from kernel memory
    313   1.1      cgd 	 */
    314   1.1      cgd 	bcopy("_version", symbuf, sizeof ("_version")-1);
    315   1.2      cgd 	key.dsize = (sizeof ("_version")-1);
    316   1.1      cgd 	data = dbm_fetch(db, key);
    317   1.1      cgd 	if (data.dptr == NULL)
    318   1.1      cgd 		goto hard1;
    319   1.1      cgd 	if (data.dsize != sizeof (struct nlist))
    320   1.1      cgd 		goto hard1;
    321   1.1      cgd 	bcopy(data.dptr, &nbuf, sizeof (struct nlist));
    322   1.1      cgd 	lseek(kmem, nbuf.n_value, 0);
    323   1.1      cgd 	if (read(kmem, kversion, dbversionlen) != dbversionlen)
    324   1.1      cgd 		goto hard1;
    325   1.1      cgd 	/*
    326   1.1      cgd 	 * if they match, we win - otherwise do it the hard way
    327   1.1      cgd 	 */
    328   1.1      cgd 	if (bcmp(dbversion, kversion, dbversionlen) != 0)
    329   1.1      cgd 		goto hard1;
    330   1.1      cgd 	/*
    331   1.1      cgd 	 * getem from the database.
    332   1.1      cgd 	 */
    333   1.1      cgd win:
    334   1.1      cgd 	num = did = 0;
    335   1.1      cgd 	for (n = nl; n->n_name && n->n_name[0]; n++, num++) {
    336   1.1      cgd 		int len;
    337   1.1      cgd 		/*
    338   1.1      cgd 		 * clear out fields from users buffer
    339   1.1      cgd 		 */
    340   1.1      cgd 		n->n_type = 0;
    341   1.1      cgd 		n->n_other = 0;
    342   1.1      cgd 		n->n_desc = 0;
    343   1.1      cgd 		n->n_value = 0;
    344   1.1      cgd 		/*
    345   1.1      cgd 		 * query db
    346   1.1      cgd 		 */
    347   1.1      cgd 		if ((len = strlen(n->n_name)) > MAXSYMSIZE) {
    348   1.1      cgd 			seterr("symbol too large");
    349   1.1      cgd 			return (-1);
    350   1.1      cgd 		}
    351   1.1      cgd 		(void)strcpy(symbuf, n->n_name);
    352   1.2      cgd 		key.dsize = len;
    353   1.1      cgd 		data = dbm_fetch(db, key);
    354   1.1      cgd 		if (data.dptr == NULL || data.dsize != sizeof (struct nlist))
    355   1.1      cgd 			continue;
    356   1.1      cgd 		bcopy(data.dptr, &nbuf, sizeof (struct nlist));
    357   1.1      cgd 		n->n_value = nbuf.n_value;
    358   1.1      cgd 		n->n_type = nbuf.n_type;
    359   1.1      cgd 		n->n_desc = nbuf.n_desc;
    360   1.1      cgd 		n->n_other = nbuf.n_other;
    361   1.1      cgd 		did++;
    362   1.1      cgd 	}
    363   1.1      cgd 	return (num - did);
    364   1.1      cgd hard1:
    365   1.1      cgd 	dbm_close(db);
    366   1.1      cgd 	db = NULL;
    367   1.1      cgd hard2:
    368   1.1      cgd 	num = nlist(unixf, nl);
    369   1.1      cgd 	if (num == -1)
    370   1.1      cgd 		seterr("nlist (hard way) failed");
    371   1.1      cgd 	return (num);
    372   1.1      cgd }
    373   1.1      cgd 
    374   1.1      cgd kvm_getprocs(what, arg)
    375   1.1      cgd 	int what, arg;
    376   1.1      cgd {
    377   1.3      cgd 	static int	ocopysize = -1;
    378   1.3      cgd 
    379   1.1      cgd 	if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
    380   1.1      cgd 		return (NULL);
    381   1.1      cgd 	if (!deadkernel) {
    382   1.1      cgd 		int ret, copysize;
    383   1.1      cgd 
    384   1.1      cgd 		if ((ret = getkerninfo(what, NULL, NULL, arg)) == -1) {
    385   1.1      cgd 			setsyserr("can't get estimate for kerninfo");
    386   1.1      cgd 			return (-1);
    387   1.1      cgd 		}
    388   1.1      cgd 		copysize = ret;
    389   1.5      cgd 		if (copysize > ocopysize || !kvmprocbase) {
    390   1.5      cgd 			if (ocopysize == -1 || !kvmprocbase)
    391   1.4  mycroft 				kvmprocbase =
    392   1.4  mycroft 					(struct kinfo_proc *)malloc(copysize);
    393   1.4  mycroft 			else
    394   1.4  mycroft 				kvmprocbase =
    395   1.4  mycroft 					(struct kinfo_proc *)realloc(kvmprocbase,
    396   1.4  mycroft 								copysize);
    397   1.4  mycroft 			if (!kvmprocbase) {
    398   1.4  mycroft 				seterr("out of memory");
    399   1.4  mycroft 				return (-1);
    400   1.4  mycroft 			}
    401   1.1      cgd 		}
    402   1.3      cgd 		ocopysize = copysize;
    403   1.1      cgd 		if ((ret = getkerninfo(what, kvmprocbase, &copysize,
    404   1.1      cgd 		     arg)) == -1) {
    405   1.1      cgd 			setsyserr("can't get proc list");
    406   1.1      cgd 			return (-1);
    407   1.1      cgd 		}
    408   1.1      cgd 		if (copysize % sizeof (struct kinfo_proc)) {
    409   1.1      cgd 			seterr("proc size mismatch (got %d total, kinfo_proc: %d)",
    410   1.1      cgd 				copysize, sizeof (struct kinfo_proc));
    411   1.1      cgd 			return (-1);
    412   1.1      cgd 		}
    413   1.1      cgd 		kvmnprocs = copysize / sizeof (struct kinfo_proc);
    414   1.1      cgd 	} else {
    415   1.1      cgd 		int nproc;
    416   1.1      cgd 
    417   1.1      cgd 		if (kvm_read((void *) nl[X_NPROC].n_value, &nproc,
    418  1.19  mycroft 		    sizeof (int)) == -1) {
    419   1.1      cgd 			seterr("can't read nproc");
    420   1.1      cgd 			return (-1);
    421   1.1      cgd 		}
    422   1.1      cgd 		if ((kvmprocbase = (struct kinfo_proc *)
    423   1.1      cgd 		     malloc(nproc * sizeof (struct kinfo_proc))) == NULL) {
    424   1.1      cgd 			seterr("out of memory (addr: %x nproc = %d)",
    425   1.1      cgd 				nl[X_NPROC].n_value, nproc);
    426   1.1      cgd 			return (-1);
    427   1.1      cgd 		}
    428   1.1      cgd 		kvmnprocs = kvm_doprocs(what, arg, kvmprocbase);
    429   1.1      cgd 		realloc(kvmprocbase, kvmnprocs * sizeof (struct kinfo_proc));
    430   1.1      cgd 	}
    431   1.1      cgd 	kvmprocptr = kvmprocbase;
    432   1.1      cgd 
    433   1.1      cgd 	return (kvmnprocs);
    434   1.1      cgd }
    435   1.1      cgd 
    436   1.1      cgd /*
    437   1.1      cgd  * XXX - should NOT give up so easily - especially since the kernel
    438   1.1      cgd  * may be corrupt (it died).  Should gather as much information as possible.
    439   1.1      cgd  * Follows proc ptrs instead of reading table since table may go
    440   1.1      cgd  * away soon.
    441   1.1      cgd  */
    442   1.1      cgd static
    443   1.1      cgd kvm_doprocs(what, arg, buff)
    444   1.1      cgd 	int what, arg;
    445   1.1      cgd 	char *buff;
    446   1.1      cgd {
    447   1.1      cgd 	struct proc *p, proc;
    448   1.1      cgd 	register char *bp = buff;
    449   1.1      cgd 	int i = 0;
    450   1.1      cgd 	int doingzomb = 0;
    451   1.1      cgd 	struct eproc eproc;
    452   1.1      cgd 	struct pgrp pgrp;
    453   1.1      cgd 	struct session sess;
    454   1.1      cgd 	struct tty tty;
    455   1.1      cgd 
    456   1.1      cgd 	/* allproc */
    457   1.1      cgd 	if (kvm_read((void *) nl[X_ALLPROC].n_value, &p,
    458  1.19  mycroft 	    sizeof (struct proc *)) == -1) {
    459   1.1      cgd 		seterr("can't read allproc");
    460   1.1      cgd 		return (-1);
    461   1.1      cgd 	}
    462   1.1      cgd 
    463   1.1      cgd again:
    464   1.1      cgd 	for (; p; p = proc.p_nxt) {
    465  1.19  mycroft 		if (kvm_read(p, &proc, sizeof (struct proc)) == -1) {
    466   1.1      cgd 			seterr("can't read proc at %x", p);
    467   1.1      cgd 			return (-1);
    468   1.1      cgd 		}
    469   1.1      cgd 		if (kvm_read(proc.p_cred, &eproc.e_pcred,
    470  1.19  mycroft 		    sizeof (struct pcred)) != -1)
    471   1.1      cgd 			(void) kvm_read(eproc.e_pcred.pc_ucred, &eproc.e_ucred,
    472   1.1      cgd 			    sizeof (struct ucred));
    473  1.19  mycroft 
    474   1.1      cgd 		switch(ki_op(what)) {
    475   1.1      cgd 
    476   1.1      cgd 		case KINFO_PROC_PID:
    477   1.1      cgd 			if (proc.p_pid != (pid_t)arg)
    478   1.1      cgd 				continue;
    479   1.1      cgd 			break;
    480   1.1      cgd 
    481   1.1      cgd 
    482   1.1      cgd 		case KINFO_PROC_UID:
    483   1.1      cgd 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
    484   1.1      cgd 				continue;
    485   1.1      cgd 			break;
    486   1.1      cgd 
    487   1.1      cgd 		case KINFO_PROC_RUID:
    488   1.1      cgd 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
    489   1.1      cgd 				continue;
    490   1.1      cgd 			break;
    491   1.1      cgd 		}
    492   1.1      cgd 		/*
    493   1.1      cgd 		 * gather eproc
    494   1.1      cgd 		 */
    495   1.1      cgd 		eproc.e_paddr = p;
    496  1.19  mycroft 		if (kvm_read(proc.p_pgrp, &pgrp, sizeof (struct pgrp)) == -1) {
    497   1.1      cgd 			seterr("can't read pgrp at %x", proc.p_pgrp);
    498   1.1      cgd 			return (-1);
    499   1.1      cgd 		}
    500   1.1      cgd 		eproc.e_sess = pgrp.pg_session;
    501   1.1      cgd 		eproc.e_pgid = pgrp.pg_id;
    502   1.1      cgd 		eproc.e_jobc = pgrp.pg_jobc;
    503   1.1      cgd 		if (kvm_read(pgrp.pg_session, &sess, sizeof (struct session))
    504  1.19  mycroft 		    == -1) {
    505   1.1      cgd 			seterr("can't read session at %x", pgrp.pg_session);
    506   1.1      cgd 			return (-1);
    507   1.1      cgd 		}
    508   1.1      cgd 		if ((proc.p_flag&SCTTY) && sess.s_ttyp != NULL) {
    509   1.1      cgd 			if (kvm_read(sess.s_ttyp, &tty, sizeof (struct tty))
    510  1.19  mycroft 			    == -1) {
    511   1.1      cgd 				seterr("can't read tty at %x", sess.s_ttyp);
    512   1.1      cgd 				return (-1);
    513   1.1      cgd 			}
    514   1.1      cgd 			eproc.e_tdev = tty.t_dev;
    515   1.1      cgd 			eproc.e_tsess = tty.t_session;
    516   1.1      cgd 			if (tty.t_pgrp != NULL) {
    517   1.1      cgd 				if (kvm_read(tty.t_pgrp, &pgrp, sizeof (struct
    518  1.19  mycroft 				    pgrp)) == -1) {
    519   1.1      cgd 					seterr("can't read tpgrp at &x",
    520   1.1      cgd 						tty.t_pgrp);
    521   1.1      cgd 					return (-1);
    522   1.1      cgd 				}
    523   1.1      cgd 				eproc.e_tpgid = pgrp.pg_id;
    524   1.1      cgd 			} else
    525   1.1      cgd 				eproc.e_tpgid = -1;
    526   1.1      cgd 		} else
    527   1.1      cgd 			eproc.e_tdev = NODEV;
    528   1.1      cgd 		if (proc.p_wmesg)
    529  1.19  mycroft 			(void) kvm_read(proc.p_wmesg, eproc.e_wmesg, WMESGLEN);
    530   1.1      cgd 		(void) kvm_read(proc.p_vmspace, &eproc.e_vm,
    531   1.1      cgd 		    sizeof (struct vmspace));
    532   1.1      cgd 		eproc.e_xsize = eproc.e_xrssize =
    533   1.1      cgd 			eproc.e_xccount = eproc.e_xswrss = 0;
    534   1.1      cgd 
    535   1.1      cgd 		switch(ki_op(what)) {
    536   1.1      cgd 
    537   1.1      cgd 		case KINFO_PROC_PGRP:
    538   1.1      cgd 			if (eproc.e_pgid != (pid_t)arg)
    539   1.1      cgd 				continue;
    540   1.1      cgd 			break;
    541   1.1      cgd 
    542   1.1      cgd 		case KINFO_PROC_TTY:
    543   1.1      cgd 			if ((proc.p_flag&SCTTY) == 0 ||
    544   1.1      cgd 			     eproc.e_tdev != (dev_t)arg)
    545   1.1      cgd 				continue;
    546   1.1      cgd 			break;
    547   1.1      cgd 		}
    548   1.1      cgd 
    549   1.1      cgd 		i++;
    550   1.1      cgd 		bcopy(&proc, bp, sizeof (struct proc));
    551   1.1      cgd 		bp += sizeof (struct proc);
    552   1.1      cgd 		bcopy(&eproc, bp, sizeof (struct eproc));
    553   1.1      cgd 		bp+= sizeof (struct eproc);
    554   1.1      cgd 	}
    555   1.1      cgd 	if (!doingzomb) {
    556   1.1      cgd 		/* zombproc */
    557   1.1      cgd 		if (kvm_read((void *) nl[X_ZOMBPROC].n_value, &p,
    558  1.19  mycroft 		    sizeof (struct proc *)) == -1) {
    559   1.1      cgd 			seterr("can't read zombproc");
    560   1.1      cgd 			return (-1);
    561   1.1      cgd 		}
    562   1.1      cgd 		doingzomb = 1;
    563   1.1      cgd 		goto again;
    564   1.1      cgd 	}
    565   1.1      cgd 
    566   1.1      cgd 	return (i);
    567   1.1      cgd }
    568   1.1      cgd 
    569   1.1      cgd struct proc *
    570   1.1      cgd kvm_nextproc()
    571   1.1      cgd {
    572   1.1      cgd 
    573   1.1      cgd 	if (!kvmprocbase && kvm_getprocs(0, 0) == -1)
    574   1.1      cgd 		return (NULL);
    575   1.1      cgd 	if (kvmprocptr >= (kvmprocbase + kvmnprocs)) {
    576   1.1      cgd 		seterr("end of proc list");
    577   1.1      cgd 		return (NULL);
    578   1.1      cgd 	}
    579   1.1      cgd 	return((struct proc *)(kvmprocptr++));
    580   1.1      cgd }
    581   1.1      cgd 
    582   1.1      cgd struct eproc *
    583   1.1      cgd kvm_geteproc(p)
    584   1.1      cgd 	const struct proc *p;
    585   1.1      cgd {
    586   1.1      cgd 	return ((struct eproc *)(((char *)p) + sizeof (struct proc)));
    587   1.1      cgd }
    588   1.1      cgd 
    589   1.1      cgd kvm_setproc()
    590   1.1      cgd {
    591   1.1      cgd 	kvmprocptr = kvmprocbase;
    592   1.1      cgd }
    593   1.1      cgd 
    594   1.1      cgd kvm_freeprocs()
    595   1.1      cgd {
    596   1.1      cgd 
    597   1.1      cgd 	if (kvmprocbase) {
    598   1.1      cgd 		free(kvmprocbase);
    599   1.1      cgd 		kvmprocbase = NULL;
    600   1.1      cgd 	}
    601   1.1      cgd }
    602   1.1      cgd 
    603   1.1      cgd struct user *
    604   1.1      cgd kvm_getu(p)
    605   1.1      cgd 	const struct proc *p;
    606   1.1      cgd {
    607   1.1      cgd 	register struct kinfo_proc *kp = (struct kinfo_proc *)p;
    608   1.1      cgd 	register int i;
    609   1.1      cgd 	register char *up;
    610   1.3      cgd 	u_int vaddr;
    611   1.3      cgd 	struct swapblk swb;
    612   1.1      cgd 
    613   1.1      cgd 	if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
    614   1.1      cgd 		return (NULL);
    615   1.1      cgd 	if (p->p_stat == SZOMB) {
    616   1.1      cgd 		seterr("zombie process");
    617   1.1      cgd 		return (NULL);
    618   1.1      cgd 	}
    619   1.3      cgd 
    620   1.3      cgd 	if ((p->p_flag & SLOAD) == 0) {
    621   1.3      cgd 		vm_offset_t	maddr;
    622   1.3      cgd 
    623   1.3      cgd 		if (swap < 0) {
    624   1.3      cgd 			seterr("no swap");
    625   1.3      cgd 			return (NULL);
    626   1.3      cgd 		}
    627   1.3      cgd 		/*
    628   1.3      cgd 		 * Costly operation, better set enable_swap to zero
    629   1.3      cgd 		 * in vm/vm_glue.c, since paging of user pages isn't
    630   1.3      cgd 		 * done yet anyway.
    631   1.3      cgd 		 */
    632  1.19  mycroft 		if (vatosw(&kp->kp_eproc.e_vm.vm_map, USRSTACK + i * NBPG,
    633  1.19  mycroft 			   &maddr, &swb) == 0)
    634   1.3      cgd 			return NULL;
    635   1.3      cgd 
    636   1.3      cgd 		if (maddr == 0 && swb.size < UPAGES * NBPG)
    637   1.3      cgd 			return NULL;
    638   1.3      cgd 
    639   1.3      cgd 		for (i = 0; i < UPAGES; i++) {
    640   1.3      cgd 			if (maddr) {
    641   1.3      cgd 				(void) lseek(mem, maddr + i * NBPG, 0);
    642   1.3      cgd 				if (read(mem,
    643   1.3      cgd 				    (char *)user.upages[i], NBPG) != NBPG) {
    644   1.3      cgd 					seterr(
    645   1.3      cgd 					    "can't read u for pid %d from %s",
    646   1.3      cgd 					    p->p_pid, swapf);
    647   1.3      cgd 					return NULL;
    648   1.3      cgd 				}
    649   1.3      cgd 			} else {
    650   1.3      cgd 				(void) lseek(swap, swb.offset + i * NBPG, 0);
    651   1.3      cgd 				if (read(swap,
    652   1.3      cgd 				    (char *)user.upages[i], NBPG) != NBPG) {
    653   1.3      cgd 					seterr(
    654   1.3      cgd 					    "can't read u for pid %d from %s",
    655   1.3      cgd 					    p->p_pid, swapf);
    656   1.3      cgd 					return NULL;
    657   1.3      cgd 				}
    658   1.3      cgd 			}
    659   1.3      cgd 		}
    660   1.3      cgd 		return(&user.user);
    661   1.3      cgd 	}
    662   1.1      cgd 	/*
    663   1.1      cgd 	 * Read u-area one page at a time for the benefit of post-mortems
    664   1.1      cgd 	 */
    665   1.1      cgd 	up = (char *) p->p_addr;
    666   1.1      cgd 	for (i = 0; i < UPAGES; i++) {
    667   1.1      cgd 		klseek(kmem, (long)up, 0);
    668   1.1      cgd 		if (read(kmem, user.upages[i], CLBYTES) != CLBYTES) {
    669   1.1      cgd 			seterr("cant read page %x of u of pid %d from %s",
    670   1.1      cgd 			    up, p->p_pid, kmemf);
    671   1.1      cgd 			return(NULL);
    672   1.1      cgd 		}
    673   1.1      cgd 		up += CLBYTES;
    674   1.1      cgd 	}
    675   1.1      cgd 	pcbpf = (int) btop(p->p_addr);	/* what should this be really? */
    676   1.1      cgd 
    677   1.1      cgd 	kp->kp_eproc.e_vm.vm_rssize =
    678   1.1      cgd 	    kp->kp_eproc.e_vm.vm_pmap.pm_stats.resident_count; /* XXX */
    679   1.1      cgd 	return(&user.user);
    680   1.1      cgd }
    681   1.1      cgd 
    682   1.8      cgd int
    683   1.8      cgd kvm_procread(p, addr, buf, len)
    684   1.8      cgd 	const struct proc *p;
    685  1.19  mycroft 	const unsigned addr;
    686  1.19  mycroft 	unsigned len;
    687  1.11  mycroft 	char *buf;
    688   1.8      cgd {
    689   1.8      cgd 	register struct kinfo_proc *kp = (struct kinfo_proc *) p;
    690   1.8      cgd 	struct swapblk swb;
    691   1.8      cgd 	vm_offset_t swaddr = 0, memaddr = 0;
    692   1.8      cgd 	unsigned real_len;
    693   1.8      cgd 
    694   1.8      cgd 	real_len = len < (CLBYTES - (addr & CLOFSET)) ? len : (CLBYTES - (addr & CLOFSET));
    695   1.8      cgd 
    696  1.19  mycroft         if (vatosw(&kp->kp_eproc.e_vm.vm_map, addr & ~CLOFSET, &memaddr,
    697  1.19  mycroft 		   &swb) == 0)
    698  1.19  mycroft 		return 0;
    699   1.8      cgd 
    700   1.8      cgd 	if (memaddr) {
    701  1.19  mycroft 		memaddr += addr & CLOFSET;
    702   1.8      cgd 		if (lseek(mem, memaddr, 0) == -1)
    703  1.19  mycroft 			seterr("kvm_procread: lseek mem");
    704  1.19  mycroft 		len = read(mem, buf, real_len);
    705  1.19  mycroft 		if (len == -1) {
    706  1.19  mycroft 			seterr("kvm_procread: read mem");
    707  1.11  mycroft 			return 0;
    708   1.8      cgd 		}
    709  1.19  mycroft 	} else {
    710  1.11  mycroft 		char bouncebuf[CLBYTES];
    711  1.19  mycroft 		swaddr = swb.offset + (addr & CLOFSET);
    712  1.19  mycroft 		swb.size -= addr & CLOFSET;
    713  1.11  mycroft 		if (lseek(swap, swaddr & ~CLOFSET, 0) == -1) {
    714  1.19  mycroft 			seterr("kvm_procread: lseek swap");
    715  1.11  mycroft 			return 0;
    716  1.11  mycroft 		}
    717  1.11  mycroft 		len = read(swap, bouncebuf, CLBYTES);
    718  1.11  mycroft 		if (len == -1 || len <= (swaddr & CLOFSET)) {
    719  1.19  mycroft 			seterr("kvm_procread: read swap");
    720  1.11  mycroft 			return 0;
    721   1.8      cgd 		}
    722  1.11  mycroft 		len = MIN(len - (swaddr & CLOFSET), real_len);
    723  1.11  mycroft 		memcpy(buf, &bouncebuf[swaddr & CLOFSET], len);
    724  1.19  mycroft 	}
    725   1.8      cgd 
    726  1.19  mycroft 	return len;
    727   1.8      cgd }
    728   1.8      cgd 
    729   1.8      cgd int
    730   1.8      cgd kvm_procreadstr(p, addr, buf, len)
    731   1.8      cgd         const struct proc *p;
    732  1.11  mycroft         const unsigned addr;
    733  1.11  mycroft 	char *buf;
    734   1.8      cgd 	unsigned len;
    735   1.8      cgd {
    736  1.10  deraadt 	int	done, little;
    737  1.10  deraadt 	char	copy[200], *pb;
    738  1.11  mycroft 	char	a;
    739   1.8      cgd 
    740   1.8      cgd 	done = 0;
    741  1.15      cgd 	copy[0] = '\0';
    742  1.10  deraadt 	while (len) {
    743  1.10  deraadt 		little = kvm_procread(p, addr+done, copy, MIN(len, sizeof copy));
    744  1.10  deraadt 		if (little<1)
    745  1.10  deraadt 			break;
    746  1.10  deraadt 		pb = copy;
    747  1.10  deraadt 		while (little--) {
    748  1.10  deraadt 			len--;
    749  1.11  mycroft 			if( (*buf++ = *pb++) == '\0' )
    750  1.15      cgd 				return done;
    751  1.15      cgd 			done++;
    752  1.10  deraadt 		}
    753   1.8      cgd 	}
    754   1.8      cgd 	return done;
    755   1.8      cgd }
    756   1.8      cgd 
    757   1.1      cgd char *
    758   1.1      cgd kvm_getargs(p, up)
    759   1.1      cgd 	const struct proc *p;
    760   1.1      cgd 	const struct user *up;
    761   1.1      cgd {
    762   1.8      cgd 	static char cmdbuf[ARG_MAX + sizeof(p->p_comm) + 5];
    763   1.8      cgd 	register char *cp, *acp;
    764   1.8      cgd 	int left, rv;
    765   1.8      cgd 	struct ps_strings arginfo;
    766   1.1      cgd 
    767   1.1      cgd 	if (up == NULL || p->p_pid == 0 || p->p_pid == 2)
    768   1.1      cgd 		goto retucomm;
    769   1.8      cgd 
    770  1.11  mycroft 	if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
    771   1.8      cgd 		sizeof(arginfo))
    772   1.8      cgd 		goto bad;
    773   1.8      cgd 
    774  1.15      cgd 	cmdbuf[0] = '\0';
    775   1.8      cgd 	cp = cmdbuf;
    776   1.8      cgd 	acp = arginfo.ps_argvstr;
    777   1.8      cgd 	left = ARG_MAX + 1;
    778   1.8      cgd 	while (arginfo.ps_nargvstr--) {
    779   1.8      cgd 		if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
    780   1.8      cgd 			acp += rv + 1;
    781   1.8      cgd 			left -= rv + 1;
    782   1.8      cgd 			cp += rv;
    783   1.8      cgd 			*cp++ = ' ';
    784   1.8      cgd 			*cp = '\0';
    785   1.1      cgd 		} else
    786   1.1      cgd 			goto bad;
    787   1.1      cgd 	}
    788   1.8      cgd 	cp-- ; *cp = '\0';
    789   1.2      cgd 
    790   1.8      cgd 	if (cmdbuf[0] == '-' || cmdbuf[0] == '?' || cmdbuf[0] <= ' ') {
    791   1.1      cgd 		(void) strcat(cmdbuf, " (");
    792   1.1      cgd 		(void) strncat(cmdbuf, p->p_comm, sizeof(p->p_comm));
    793   1.1      cgd 		(void) strcat(cmdbuf, ")");
    794   1.1      cgd 	}
    795   1.1      cgd 	return (cmdbuf);
    796   1.1      cgd 
    797   1.1      cgd bad:
    798   1.8      cgd 	seterr("error locating command name for pid %d", p->p_pid);
    799   1.1      cgd retucomm:
    800   1.8      cgd 	(void) strcpy(cmdbuf, "(");
    801   1.1      cgd 	(void) strncat(cmdbuf, p->p_comm, sizeof (p->p_comm));
    802   1.1      cgd 	(void) strcat(cmdbuf, ")");
    803   1.1      cgd 	return (cmdbuf);
    804   1.1      cgd }
    805   1.1      cgd 
    806   1.8      cgd char *
    807   1.8      cgd kvm_getenv(p, up)
    808   1.8      cgd 	const struct proc *p;
    809   1.8      cgd 	const struct user *up;
    810   1.8      cgd {
    811   1.8      cgd 	static char envbuf[ARG_MAX + 1];
    812   1.8      cgd 	register char *cp, *acp;
    813   1.8      cgd 	int left, rv;
    814   1.8      cgd 	struct ps_strings arginfo;
    815   1.8      cgd 
    816   1.8      cgd 	if (up == NULL || p->p_pid == 0 || p->p_pid == 2)
    817   1.8      cgd 		goto retemptyenv;
    818   1.8      cgd 
    819  1.11  mycroft 	if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
    820   1.8      cgd 		sizeof(arginfo))
    821   1.8      cgd 		goto bad;
    822   1.8      cgd 
    823   1.8      cgd 	cp = envbuf;
    824   1.8      cgd 	acp = arginfo.ps_envstr;
    825   1.8      cgd 	left = ARG_MAX + 1;
    826   1.8      cgd 	while (arginfo.ps_nenvstr--) {
    827   1.8      cgd 		if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
    828   1.8      cgd 			acp += rv + 1;
    829   1.8      cgd 			left -= rv + 1;
    830   1.8      cgd 			cp += rv;
    831   1.8      cgd 			*cp++ = ' ';
    832   1.8      cgd 			*cp = '\0';
    833   1.8      cgd 		} else
    834   1.8      cgd 			goto bad;
    835   1.8      cgd 	}
    836   1.8      cgd 	cp-- ; *cp = '\0';
    837   1.8      cgd 	return (envbuf);
    838   1.8      cgd 
    839   1.8      cgd bad:
    840   1.8      cgd 	seterr("error locating environment for pid %d", p->p_pid);
    841   1.8      cgd retemptyenv:
    842   1.8      cgd 	envbuf[0] = '\0';
    843   1.8      cgd 	return (envbuf);
    844   1.8      cgd }
    845   1.1      cgd 
    846   1.1      cgd static
    847   1.1      cgd getkvars()
    848   1.1      cgd {
    849   1.1      cgd 	if (kvm_nlist(nl) == -1)
    850   1.1      cgd 		return (-1);
    851   1.1      cgd 	if (deadkernel) {
    852   1.1      cgd 		/* We must do the sys map first because klseek uses it */
    853   1.1      cgd 		long	addr;
    854   1.1      cgd 
    855  1.16  mycroft #if defined(hp300) || defined(amiga)
    856   1.1      cgd 		addr = (long) nl[X_LOWRAM].n_value;
    857   1.1      cgd 		(void) lseek(kmem, addr, 0);
    858   1.1      cgd 		if (read(kmem, (char *) &lowram, sizeof (lowram))
    859   1.1      cgd 		    != sizeof (lowram)) {
    860   1.1      cgd 			seterr("can't read lowram");
    861   1.1      cgd 			return (-1);
    862   1.1      cgd 		}
    863   1.1      cgd 		lowram = btop(lowram);
    864   1.1      cgd 		Sysseg = (struct ste *) malloc(NBPG);
    865   1.1      cgd 		if (Sysseg == NULL) {
    866   1.1      cgd 			seterr("out of space for Sysseg");
    867   1.1      cgd 			return (-1);
    868   1.1      cgd 		}
    869   1.1      cgd 		addr = (long) nl[X_SYSSEG].n_value;
    870   1.1      cgd 		(void) lseek(kmem, addr, 0);
    871   1.1      cgd 		read(kmem, (char *)&addr, sizeof(addr));
    872   1.1      cgd 		(void) lseek(kmem, (long)addr, 0);
    873   1.1      cgd 		if (read(kmem, (char *) Sysseg, NBPG) != NBPG) {
    874   1.1      cgd 			seterr("can't read Sysseg");
    875   1.1      cgd 			return (-1);
    876   1.1      cgd 		}
    877   1.1      cgd #endif
    878   1.1      cgd #if defined(i386)
    879   1.1      cgd 		PTD = (struct pde *) malloc(NBPG);
    880   1.1      cgd 		if (PTD == NULL) {
    881   1.1      cgd 			seterr("out of space for PTD");
    882   1.1      cgd 			return (-1);
    883   1.1      cgd 		}
    884   1.1      cgd 		addr = (long) nl[X_IdlePTD].n_value;
    885   1.1      cgd 		(void) lseek(kmem, addr, 0);
    886   1.1      cgd 		read(kmem, (char *)&addr, sizeof(addr));
    887   1.1      cgd 		(void) lseek(kmem, (long)addr, 0);
    888   1.1      cgd 		if (read(kmem, (char *) PTD, NBPG) != NBPG) {
    889   1.1      cgd 			seterr("can't read PTD");
    890   1.1      cgd 			return (-1);
    891   1.1      cgd 		}
    892   1.1      cgd #endif
    893   1.1      cgd 	}
    894  1.19  mycroft 	if (kvm_read((void *) nl[X_NSWAP].n_value, &nswap, sizeof (long)) == -1) {
    895   1.1      cgd 		seterr("can't read nswap");
    896   1.1      cgd 		return (-1);
    897   1.1      cgd 	}
    898  1.19  mycroft 	if (kvm_read((void *) nl[X_DMMIN].n_value, &dmmin, sizeof (long)) == -1) {
    899   1.1      cgd 		seterr("can't read dmmin");
    900   1.1      cgd 		return (-1);
    901   1.1      cgd 	}
    902  1.19  mycroft 	if (kvm_read((void *) nl[X_DMMAX].n_value, &dmmax, sizeof (long)) == -1) {
    903   1.1      cgd 		seterr("can't read dmmax");
    904   1.1      cgd 		return (-1);
    905   1.1      cgd 	}
    906  1.19  mycroft 	if (kvm_read((void *) nl[X_VM_PAGE_HASH_MASK].n_value,
    907  1.19  mycroft 		     &vm_page_hash_mask, sizeof (long)) == -1) {
    908  1.19  mycroft 		seterr("can't read vm_page_hash_mask");
    909  1.19  mycroft 		return (-1);
    910  1.19  mycroft 	}
    911  1.19  mycroft 	if (kvm_read((void *) nl[X_VM_PAGE_BUCKETS].n_value,
    912  1.19  mycroft 		     &vm_page_buckets, sizeof (long)) == -1) {
    913  1.19  mycroft 		seterr("can't read vm_page_buckets");
    914  1.19  mycroft 		return (-1);
    915  1.19  mycroft 	}
    916  1.19  mycroft 	if (kvm_read((void *) nl[X_PAGE_SHIFT].n_value,
    917  1.19  mycroft 		     &page_shift, sizeof (long)) == -1) {
    918  1.19  mycroft 		seterr("can't read page_shift");
    919  1.19  mycroft 		return (-1);
    920  1.19  mycroft 	}
    921  1.19  mycroft 
    922   1.1      cgd 	return (0);
    923   1.1      cgd }
    924   1.1      cgd 
    925   1.1      cgd kvm_read(loc, buf, len)
    926   1.1      cgd 	void *loc;
    927   1.1      cgd 	void *buf;
    928   1.1      cgd {
    929   1.1      cgd 	if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
    930   1.1      cgd 		return (-1);
    931  1.19  mycroft 	klseek(kmem, (off_t) loc, 0);
    932  1.19  mycroft 	if (read(kmem, buf, len) != len) {
    933  1.19  mycroft 		seterr("error reading kmem at %x", loc);
    934  1.19  mycroft 		return (-1);
    935   1.1      cgd 	}
    936   1.1      cgd 	return (len);
    937   1.1      cgd }
    938   1.1      cgd 
    939   1.1      cgd static void
    940   1.1      cgd klseek(fd, loc, off)
    941   1.1      cgd 	int fd;
    942   1.1      cgd 	off_t loc;
    943   1.1      cgd 	int off;
    944   1.1      cgd {
    945   1.1      cgd 
    946   1.1      cgd 	if (deadkernel) {
    947   1.1      cgd 		if ((loc = Vtophys(loc)) == -1)
    948   1.1      cgd 			return;
    949   1.1      cgd 	}
    950   1.1      cgd 	(void) lseek(fd, (off_t)loc, off);
    951   1.1      cgd }
    952   1.1      cgd 
    953   1.1      cgd static off_t
    954   1.1      cgd Vtophys(loc)
    955   1.1      cgd 	u_long	loc;
    956   1.1      cgd {
    957   1.1      cgd 	off_t newloc = (off_t) -1;
    958  1.16  mycroft #if defined(hp300) || defined(amiga)
    959   1.1      cgd 	int p, ste, pte;
    960   1.1      cgd 
    961  1.16  mycroft 	ste = *(int *)&Sysseg[btos(loc)];
    962   1.1      cgd 	if ((ste & SG_V) == 0) {
    963   1.1      cgd 		seterr("vtophys: segment not valid");
    964   1.1      cgd 		return((off_t) -1);
    965   1.1      cgd 	}
    966   1.1      cgd 	p = btop(loc & SG_PMASK);
    967   1.1      cgd 	newloc = (ste & SG_FRAME) + (p * sizeof(struct pte));
    968  1.16  mycroft 	(void) lseek(mem, newloc, 0);
    969  1.16  mycroft 	if (read(mem, (char *)&pte, sizeof pte) != sizeof pte) {
    970   1.1      cgd 		seterr("vtophys: cannot locate pte");
    971   1.1      cgd 		return((off_t) -1);
    972   1.1      cgd 	}
    973   1.1      cgd 	newloc = pte & PG_FRAME;
    974   1.1      cgd 	if (pte == PG_NV || newloc < (off_t)ptob(lowram)) {
    975   1.1      cgd 		seterr("vtophys: page not valid");
    976   1.1      cgd 		return((off_t) -1);
    977   1.1      cgd 	}
    978   1.1      cgd 	newloc = (newloc - (off_t)ptob(lowram)) + (loc & PGOFSET);
    979   1.1      cgd #endif
    980   1.1      cgd #ifdef i386
    981   1.1      cgd 	struct pde pde;
    982   1.1      cgd 	struct pte pte;
    983   1.1      cgd 	int p;
    984   1.1      cgd 
    985   1.1      cgd 	pde = PTD[loc >> PD_SHIFT];
    986   1.1      cgd 	if (pde.pd_v == 0) {
    987   1.1      cgd 		seterr("vtophys: page directory entry not valid");
    988   1.1      cgd 		return((off_t) -1);
    989   1.1      cgd 	}
    990   1.1      cgd 	p = btop(loc & PT_MASK);
    991   1.1      cgd 	newloc = pde.pd_pfnum + (p * sizeof(struct pte));
    992   1.1      cgd 	(void) lseek(kmem, (long)newloc, 0);
    993   1.1      cgd 	if (read(kmem, (char *)&pte, sizeof pte) != sizeof pte) {
    994   1.1      cgd 		seterr("vtophys: cannot obtain desired pte");
    995   1.1      cgd 		return((off_t) -1);
    996   1.1      cgd 	}
    997   1.1      cgd 	newloc = pte.pg_pfnum;
    998   1.1      cgd 	if (pte.pg_v == 0) {
    999   1.1      cgd 		seterr("vtophys: page table entry not valid");
   1000   1.1      cgd 		return((off_t) -1);
   1001   1.1      cgd 	}
   1002   1.1      cgd 	newloc += (loc & PGOFSET);
   1003   1.1      cgd #endif
   1004   1.1      cgd 	return((off_t) newloc);
   1005   1.1      cgd }
   1006   1.1      cgd 
   1007   1.3      cgd /*
   1008   1.3      cgd  * locate address of unwired or swapped page
   1009   1.3      cgd  */
   1010   1.3      cgd 
   1011   1.3      cgd static int
   1012  1.19  mycroft vatosw(mp, vaddr, maddr, swb)
   1013  1.19  mycroft vm_map_t	mp;
   1014   1.3      cgd vm_offset_t	vaddr;
   1015   1.3      cgd vm_offset_t	*maddr;
   1016   1.3      cgd struct swapblk	*swb;
   1017   1.3      cgd {
   1018   1.3      cgd 	struct vm_object	vm_object;
   1019   1.3      cgd 	struct vm_map_entry	vm_entry;
   1020  1.19  mycroft 	long			saddr, addr, off;
   1021   1.3      cgd 	int			i;
   1022   1.3      cgd 
   1023  1.19  mycroft 	saddr = addr = (long)mp->header.next;
   1024  1.19  mycroft #ifdef DEBUG
   1025  1.19  mycroft 	fprintf(stderr, "vatosw: head=%x\n", &mp->header);
   1026  1.19  mycroft #endif
   1027  1.19  mycroft 	for (i = 0; ; i++) {
   1028   1.3      cgd 		/* Weed through map entries until vaddr in range */
   1029  1.19  mycroft 		if (kvm_read((void *) addr, &vm_entry, sizeof(vm_entry)) == -1) {
   1030   1.3      cgd 			setsyserr("vatosw: read vm_map_entry");
   1031   1.3      cgd 			return 0;
   1032   1.3      cgd 		}
   1033  1.19  mycroft #ifdef DEBUG
   1034  1.19  mycroft 		fprintf(stderr, "vatosw: %d/%d, vaddr=%x, start=%x, end=%x ",
   1035  1.19  mycroft 			i, mp->nentries, vaddr, vm_entry.start, vm_entry.end);
   1036  1.19  mycroft 		fprintf(stderr, "addr=%x, next=%x\n", addr, vm_entry.next);
   1037  1.18  mycroft #endif
   1038  1.18  mycroft 		if ((vaddr >= vm_entry.start) && (vaddr < vm_entry.end))
   1039  1.18  mycroft 			if (vm_entry.object.vm_object != 0)
   1040  1.18  mycroft 				break;
   1041  1.18  mycroft 			else {
   1042  1.19  mycroft #ifdef DEBUG
   1043  1.19  mycroft 				fprintf(stderr, "vatosw: no object\n");
   1044  1.18  mycroft #endif
   1045  1.19  mycroft 				seterr("vatosw: no object\n");
   1046  1.18  mycroft 				return 0;
   1047  1.18  mycroft 			}
   1048   1.3      cgd 
   1049   1.3      cgd 		addr = (long)vm_entry.next;
   1050  1.19  mycroft 
   1051  1.19  mycroft 		if (addr == saddr) {
   1052  1.19  mycroft 			seterr("vatosw: map not found\n");
   1053  1.19  mycroft 			return 0;
   1054  1.19  mycroft 		}
   1055   1.3      cgd 	}
   1056   1.3      cgd 
   1057   1.3      cgd 	if (vm_entry.is_a_map || vm_entry.is_sub_map) {
   1058  1.19  mycroft #ifdef DEBUG
   1059  1.19  mycroft 		fprintf(stderr, "vatosw: is a %smap\n",
   1060  1.18  mycroft 			vm_entry.is_sub_map ? "sub " : "");
   1061  1.18  mycroft #endif
   1062  1.19  mycroft 		seterr("vatosw: is a %smap\n",
   1063  1.19  mycroft 		       vm_entry.is_sub_map ? "sub " : "");
   1064   1.3      cgd 		return 0;
   1065   1.3      cgd 	}
   1066   1.3      cgd 
   1067   1.3      cgd 	/* Locate memory object */
   1068   1.3      cgd 	off = (vaddr - vm_entry.start) + vm_entry.offset;
   1069   1.3      cgd 	addr = (long)vm_entry.object.vm_object;
   1070   1.3      cgd 	while (1) {
   1071  1.19  mycroft 		if (kvm_read((void *) addr, &vm_object, sizeof (vm_object)) == -1) {
   1072   1.3      cgd 			setsyserr("vatosw: read vm_object");
   1073   1.3      cgd 			return 0;
   1074   1.3      cgd 		}
   1075   1.3      cgd 
   1076  1.19  mycroft #ifdef DEBUG
   1077  1.19  mycroft 		fprintf(stderr, "vatosw: find page: object %#x offset %x\n",
   1078  1.19  mycroft 			addr, off);
   1079   1.3      cgd #endif
   1080   1.3      cgd 
   1081   1.3      cgd 		/* Lookup in page queue */
   1082  1.18  mycroft 		if ((i = findpage(addr, off, maddr)) != -1)
   1083  1.18  mycroft 			return i;
   1084  1.18  mycroft 
   1085  1.18  mycroft 		if (vm_object.pager != 0 &&
   1086  1.18  mycroft 		    (i = pager_get(&vm_object, off, swb)) != -1)
   1087  1.18  mycroft 			return i;
   1088   1.3      cgd 
   1089  1.18  mycroft 		if (vm_object.shadow == 0)
   1090   1.3      cgd 			break;
   1091   1.3      cgd 
   1092  1.19  mycroft #ifdef DEBUG
   1093  1.19  mycroft 		fprintf(stderr, "vatosw: shadow obj at %x: offset %x+%x\n",
   1094  1.19  mycroft 			addr, off, vm_object.shadow_offset);
   1095   1.3      cgd #endif
   1096   1.3      cgd 
   1097   1.3      cgd 		addr = (long)vm_object.shadow;
   1098   1.3      cgd 		off += vm_object.shadow_offset;
   1099   1.3      cgd 	}
   1100   1.3      cgd 
   1101  1.19  mycroft 	seterr("vatosw: page not found\n");
   1102  1.18  mycroft 	return 0;
   1103  1.18  mycroft }
   1104  1.18  mycroft 
   1105  1.18  mycroft 
   1106  1.18  mycroft int
   1107  1.18  mycroft pager_get(object, off, swb)
   1108  1.18  mycroft struct vm_object *object;
   1109  1.18  mycroft long off;
   1110  1.18  mycroft struct swapblk	*swb;
   1111  1.18  mycroft {
   1112  1.18  mycroft 	struct pager_struct	pager;
   1113  1.18  mycroft 	struct swpager		swpager;
   1114  1.18  mycroft 	struct swblock		swblock;
   1115  1.18  mycroft 
   1116   1.3      cgd 	/* Find address in swap space */
   1117  1.19  mycroft 	if (kvm_read(object->pager, &pager, sizeof (pager)) == -1) {
   1118  1.18  mycroft 		setsyserr("pager_get: read pager");
   1119   1.3      cgd 		return 0;
   1120   1.3      cgd 	}
   1121   1.3      cgd 	if (pager.pg_type != PG_SWAP) {
   1122  1.18  mycroft 		seterr("pager_get: weird pager\n");
   1123   1.3      cgd 		return 0;
   1124   1.3      cgd 	}
   1125   1.3      cgd 
   1126   1.3      cgd 	/* Get swap pager data */
   1127  1.19  mycroft 	if (kvm_read(pager.pg_data, &swpager, sizeof (swpager)) == -1) {
   1128  1.18  mycroft 		setsyserr("pager_get: read swpager");
   1129   1.3      cgd 		return 0;
   1130   1.3      cgd 	}
   1131   1.3      cgd 
   1132  1.18  mycroft 	off += object->paging_offset;
   1133   1.3      cgd 
   1134   1.3      cgd 	/* Read swap block array */
   1135  1.19  mycroft 	if (kvm_read((void *) swpager.sw_blocks +
   1136   1.3      cgd 			(off/dbtob(swpager.sw_bsize)) * sizeof swblock,
   1137  1.19  mycroft 			&swblock, sizeof (swblock)) == -1) {
   1138  1.18  mycroft 		setsyserr("pager_get: read swblock");
   1139   1.3      cgd 		return 0;
   1140   1.3      cgd 	}
   1141  1.18  mycroft 
   1142  1.18  mycroft 	off %= dbtob(swpager.sw_bsize);
   1143   1.3      cgd 
   1144  1.18  mycroft 	if (swblock.swb_mask & (1 << atop(off))) {
   1145  1.18  mycroft 		swb->offset = dbtob(swblock.swb_block) + off;
   1146  1.18  mycroft 		swb->size = dbtob(swpager.sw_bsize) - off;
   1147  1.18  mycroft 		return 1;
   1148  1.18  mycroft 	}
   1149   1.3      cgd 
   1150  1.18  mycroft 	return -1;
   1151  1.18  mycroft }
   1152   1.3      cgd 
   1153   1.3      cgd static int
   1154   1.3      cgd findpage(object, offset, maddr)
   1155   1.3      cgd long			object;
   1156   1.3      cgd long			offset;
   1157   1.3      cgd vm_offset_t		*maddr;
   1158   1.3      cgd {
   1159   1.3      cgd 	queue_head_t	bucket;
   1160   1.3      cgd 	struct vm_page	mem;
   1161   1.3      cgd 	long		addr, baddr;
   1162   1.3      cgd 
   1163  1.19  mycroft 	baddr = vm_page_buckets +
   1164  1.19  mycroft 		vm_page_hash(object,offset) * sizeof(queue_head_t);
   1165   1.3      cgd 
   1166  1.19  mycroft 	if (kvm_read((void *) baddr, &bucket, sizeof (bucket)) == -1) {
   1167   1.3      cgd 		seterr("can't read vm_page_bucket");
   1168   1.3      cgd 		return 0;
   1169   1.3      cgd 	}
   1170   1.3      cgd 
   1171   1.3      cgd 	addr = (long)bucket.next;
   1172  1.19  mycroft 
   1173   1.3      cgd 	while (addr != baddr) {
   1174  1.19  mycroft 		if (kvm_read((void *) addr, &mem, sizeof (mem)) == -1) {
   1175   1.3      cgd 			seterr("can't read vm_page");
   1176   1.3      cgd 			return 0;
   1177   1.3      cgd 		}
   1178  1.19  mycroft 
   1179   1.3      cgd 		if ((long)mem.object == object && mem.offset == offset) {
   1180   1.3      cgd 			*maddr = (long)mem.phys_addr;
   1181   1.3      cgd 			return 1;
   1182   1.3      cgd 		}
   1183  1.19  mycroft 
   1184   1.3      cgd 		addr = (long)mem.hashq.next;
   1185   1.3      cgd 	}
   1186  1.18  mycroft 
   1187  1.18  mycroft 	return -1;
   1188   1.3      cgd }
   1189   1.3      cgd 
   1190   1.1      cgd #include <varargs.h>
   1191   1.1      cgd static char errbuf[_POSIX2_LINE_MAX];
   1192   1.1      cgd 
   1193   1.1      cgd static void
   1194   1.1      cgd seterr(va_alist)
   1195   1.1      cgd 	va_dcl
   1196   1.1      cgd {
   1197   1.1      cgd 	char *fmt;
   1198   1.1      cgd 	va_list ap;
   1199   1.1      cgd 
   1200   1.1      cgd 	va_start(ap);
   1201   1.1      cgd 	fmt = va_arg(ap, char *);
   1202   1.1      cgd 	(void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
   1203  1.19  mycroft #ifdef DEBUG
   1204  1.19  mycroft 	(void) printf("%s", errbuf);
   1205   1.3      cgd #endif
   1206   1.1      cgd 	va_end(ap);
   1207   1.1      cgd }
   1208   1.1      cgd 
   1209   1.1      cgd static void
   1210   1.1      cgd setsyserr(va_alist)
   1211   1.1      cgd 	va_dcl
   1212   1.1      cgd {
   1213   1.1      cgd 	char *fmt, *cp;
   1214   1.1      cgd 	va_list ap;
   1215   1.1      cgd 	extern int errno;
   1216   1.1      cgd 
   1217   1.1      cgd 	va_start(ap);
   1218   1.1      cgd 	fmt = va_arg(ap, char *);
   1219   1.1      cgd 	(void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
   1220  1.19  mycroft 	cp += strlen(cp);
   1221  1.19  mycroft 	(void) snprintf(cp, _POSIX2_LINE_MAX - (cp - errbuf), ": %s",
   1222  1.19  mycroft 			strerror(errno));
   1223  1.19  mycroft #ifdef DEBUG
   1224  1.19  mycroft 	(void) printf("%s", errbuf);
   1225  1.19  mycroft #endif
   1226   1.1      cgd 	va_end(ap);
   1227   1.1      cgd }
   1228   1.1      cgd 
   1229   1.1      cgd char *
   1230   1.1      cgd kvm_geterr()
   1231   1.1      cgd {
   1232   1.1      cgd 	return (errbuf);
   1233   1.1      cgd }
   1234