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