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