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