BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / libutil / kvm.c
index dce06ce..dd58e7d 100644 (file)
@@ -2,52 +2,77 @@
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted provided
- * that: (1) source distributions retain this entire copyright notice and
- * comment, and (2) distributions including binaries display the following
- * acknowledgement:  ``This product includes software developed by the
- * University of California, Berkeley and its contributors'' in the
- * documentation or other materials provided with the distribution and in
- * all advertising materials mentioning features or use of this software.
- * Neither the name of the University nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)kvm.c      5.9 (Berkeley) 6/27/90";
+static char sccsid[] = "@(#)kvm.c      5.18 (Berkeley) 5/7/91";
 #endif /* LIBC_SCCS and not lint */
 
 #endif /* LIBC_SCCS and not lint */
 
-#include <machine/pte.h>
-#include <machine/vmparam.h>
 #include <sys/param.h>
 #include <sys/user.h>
 #include <sys/proc.h>
 #include <sys/param.h>
 #include <sys/user.h>
 #include <sys/proc.h>
-#include <sys/file.h>
-#include <sys/text.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/vmmac.h>
 #include <sys/ioctl.h>
 #include <sys/ioctl.h>
+#include <sys/kinfo.h>
 #include <sys/tty.h>
 #include <sys/tty.h>
-#include <kvm.h>
-#include <ctype.h>
-#include <vis.h>
+#include <machine/vmparam.h>
+#include <fcntl.h>
 #include <nlist.h>
 #include <nlist.h>
-#include <pwd.h>
-#include <string.h>
+#include <kvm.h>
 #include <ndbm.h>
 #include <limits.h>
 #include <paths.h>
 #include <stdio.h>
 #include <ndbm.h>
 #include <limits.h>
 #include <paths.h>
 #include <stdio.h>
+#include <string.h>
+
+#ifdef SPPWAIT
+#define NEWVM
+#endif
+
+#ifdef NEWVM
+#define        btop(x)         (((unsigned)(x)) >> PGSHIFT)    /* XXX */
+#define        ptob(x)         ((caddr_t)((x) << PGSHIFT))     /* XXX */
+#include <vm/vm.h>     /* ??? kinfo_proc currently includes this*/
+#include <sys/kinfo_proc.h>
+#ifdef hp300
+#include <hp300/hp300/pte.h>
+#endif
+#else /* NEWVM */
+#include <machine/pte.h>
+#include <sys/vmmac.h>
+#include <sys/text.h>
+#endif /* NEWVM */
 
 /*
  * files
  */
 
 /*
  * files
  */
-static char *unixf, *memf, *kmemf, *swapf;
+static const char *unixf, *memf, *kmemf, *swapf;
 static int unixx, mem, kmem, swap;
 static DBM *db;
 /*
 static int unixx, mem, kmem, swap;
 static DBM *db;
 /*
@@ -71,10 +96,12 @@ static union {
 /*
  * random other stuff
  */
 /*
  * random other stuff
  */
+#ifndef NEWVM
 static struct pte *Usrptmap, *usrpt;
 static struct pte *Usrptmap, *usrpt;
-static int     dmmin, dmmax;
 static struct  pte *Sysmap;
 static int     Syssize;
 static struct  pte *Sysmap;
 static int     Syssize;
+#endif
+static int     dmmin, dmmax;
 static int     pcbpf;
 static int     argaddr0;       /* XXX */
 static int     argaddr1;
 static int     pcbpf;
 static int     argaddr0;       /* XXX */
 static int     argaddr1;
@@ -82,6 +109,7 @@ static       int     nswap;
 static char    *tmp;
 #if defined(hp300)
 static int     lowram;
 static char    *tmp;
 #if defined(hp300)
 static int     lowram;
+static struct ste *Sysseg;
 #endif
 
 #define basename(cp)   ((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
 #endif
 
 #define basename(cp)   ((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
@@ -96,7 +124,7 @@ static       int     lowram;
 #define pftoc(f)       (f)
 #endif
 #ifndef iskva
 #define pftoc(f)       (f)
 #endif
 #ifndef iskva
-#define iskva(v)       ((v) & KERNBASE)
+#define iskva(v)       ((u_long)(v) & KERNBASE)
 #endif
 
 static struct nlist nl[] = {
 #endif
 
 static struct nlist nl[] = {
@@ -126,19 +154,25 @@ static struct nlist nl[] = {
 #define        X_NPROC         9
 #define        X_LAST          9
 #if defined(hp300)
 #define        X_NPROC         9
 #define        X_LAST          9
 #if defined(hp300)
+       { "_Sysseg" },
+#define        X_SYSSEG        (X_LAST+1)
        { "_lowram" },
        { "_lowram" },
-#define        X_LOWRAM        (X_LAST+1)
+#define        X_LOWRAM        (X_LAST+2)
 #endif
        { "" },
 };
 
 #endif
        { "" },
 };
 
+static off_t vtophys();
+static void klseek(), seterr(), setsyserr(), vstodb();
+static int getkvars(), kvm_doprocs(), kvm_init();
+
 /*
  * returns     0 if files were opened now,
  *             1 if files were already opened,
  *             -1 if files could not be opened.
  */
 kvm_openfiles(uf, mf, sf)
 /*
  * returns     0 if files were opened now,
  *             1 if files were already opened,
  *             -1 if files could not be opened.
  */
 kvm_openfiles(uf, mf, sf)
-       char *uf, *mf, *sf; 
+       const char *uf, *mf, *sf; 
 {
        if (kvmfilesopen)
                return (1);
 {
        if (kvmfilesopen)
                return (1);
@@ -227,10 +261,12 @@ kvm_close()
        kvminit = 0;
        kvmfilesopen = 0;
        deadkernel = 0;
        kvminit = 0;
        kvmfilesopen = 0;
        deadkernel = 0;
+#ifndef NEWVM
        if (Sysmap) {
                free(Sysmap);
                Sysmap = NULL;
        }
        if (Sysmap) {
                free(Sysmap);
                Sysmap = NULL;
        }
+#endif
 }
 
 kvm_nlist(nl)
 }
 
 kvm_nlist(nl)
@@ -238,10 +274,10 @@ kvm_nlist(nl)
 {
        datum key, data;
        char dbname[MAXPATHLEN];
 {
        datum key, data;
        char dbname[MAXPATHLEN];
-       char dbversion[_BSD_LINE_MAX];
-       char kversion[_BSD_LINE_MAX];
+       char dbversion[_POSIX2_LINE_MAX];
+       char kversion[_POSIX2_LINE_MAX];
        int dbversionlen;
        int dbversionlen;
-       char symbuf[MAXSYMSIZE+1];
+       char symbuf[MAXSYMSIZE];
        struct nlist nbuf, *n;
        int num, did;
 
        struct nlist nbuf, *n;
        int num, did;
 
@@ -253,20 +289,19 @@ kvm_nlist(nl)
         * initialize key datum
         */
        key.dptr = symbuf;
         * initialize key datum
         */
        key.dptr = symbuf;
-       symbuf[0] = KVMDB_NLIST;
 
        if (db != NULL)
                goto win;       /* off to the races */
        /*
         * open database
         */
 
        if (db != NULL)
                goto win;       /* off to the races */
        /*
         * open database
         */
-       sprintf(dbname, "%s/kvm_%s", KVMDBDIR, basename(unixf));
+       sprintf(dbname, "%s/kvm_%s", _PATH_VARRUN, basename(unixf));
        if ((db = dbm_open(dbname, O_RDONLY, 0)) == NULL)
                goto hard2;
        /*
         * read version out of database
         */
        if ((db = dbm_open(dbname, O_RDONLY, 0)) == NULL)
                goto hard2;
        /*
         * read version out of database
         */
-       bcopy("VERSION", symbuf+1, sizeof ("VERSION")-1);
+       bcopy("VERSION", symbuf, sizeof ("VERSION")-1);
        key.dsize = (sizeof ("VERSION") - 1) + 1;
        data = dbm_fetch(db, key);
        if (data.dptr == NULL)
        key.dsize = (sizeof ("VERSION") - 1) + 1;
        data = dbm_fetch(db, key);
        if (data.dptr == NULL)
@@ -276,7 +311,7 @@ kvm_nlist(nl)
        /*
         * read version string from kernel memory
         */
        /*
         * read version string from kernel memory
         */
-       bcopy("_version", symbuf+1, sizeof ("_version")-1);
+       bcopy("_version", symbuf, sizeof ("_version")-1);
        key.dsize = (sizeof ("_version")-1) + 1;
        data = dbm_fetch(db, key);
        if (data.dptr == NULL)
        key.dsize = (sizeof ("_version")-1) + 1;
        data = dbm_fetch(db, key);
        if (data.dptr == NULL)
@@ -310,10 +345,10 @@ win:
                 * query db
                 */
                if ((len = strlen(n->n_name)) > MAXSYMSIZE) {
                 * query db
                 */
                if ((len = strlen(n->n_name)) > MAXSYMSIZE) {
-                       seterr("kvm_nlist: symbol too large");
+                       seterr("symbol too large");
                        return (-1);
                }
                        return (-1);
                }
-               strcpy(symbuf+1, n->n_name);
+               (void)strcpy(symbuf, n->n_name);
                key.dsize = len + 1;
                data = dbm_fetch(db, key);
                if (data.dptr == NULL || data.dsize != sizeof (struct nlist))
                key.dsize = len + 1;
                data = dbm_fetch(db, key);
                if (data.dptr == NULL || data.dsize != sizeof (struct nlist))
@@ -330,10 +365,14 @@ hard1:
        dbm_close(db);
        db = NULL;
 hard2:
        dbm_close(db);
        db = NULL;
 hard2:
-       return (nlist(unixf, nl));      /* XXX seterr if -1 */
+       num = nlist(unixf, nl);
+       if (num == -1)
+               seterr("nlist (hard way) failed");
+       return (num);
 }
 
 kvm_getprocs(what, arg)
 }
 
 kvm_getprocs(what, arg)
+       int what, arg;
 {
        if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
                return (NULL);
 {
        if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
                return (NULL);
@@ -356,16 +395,16 @@ kvm_getprocs(what, arg)
                        return (-1);
                }
                if (copysize % sizeof (struct kinfo_proc)) {
                        return (-1);
                }
                if (copysize % sizeof (struct kinfo_proc)) {
-                       seterr("proc size mismatch (kinfo_proc: %d)",
-                               sizeof (struct kinfo_proc));
+                       seterr("proc size mismatch (got %d total, kinfo_proc: %d)",
+                               copysize, sizeof (struct kinfo_proc));
                        return (-1);
                }
                kvmnprocs = copysize / sizeof (struct kinfo_proc);
        } else {
                int nproc;
 
                        return (-1);
                }
                kvmnprocs = copysize / sizeof (struct kinfo_proc);
        } else {
                int nproc;
 
-               if (kvm_read(nl[X_NPROC].n_value, &nproc, sizeof (int)) !=
-                   sizeof (int)) {
+               if (kvm_read((void *) nl[X_NPROC].n_value, &nproc,
+                   sizeof (int)) != sizeof (int)) {
                        seterr("can't read nproc");
                        return (-1);
                }
                        seterr("can't read nproc");
                        return (-1);
                }
@@ -402,10 +441,12 @@ kvm_doprocs(what, arg, buff)
        struct pgrp pgrp;
        struct session sess;
        struct tty tty;
        struct pgrp pgrp;
        struct session sess;
        struct tty tty;
+#ifndef NEWVM
        struct text text;
        struct text text;
+#endif
 
        /* allproc */
 
        /* allproc */
-       if (kvm_read(nl[X_ALLPROC].n_value, &p, 
+       if (kvm_read((void *) nl[X_ALLPROC].n_value, &p, 
            sizeof (struct proc *)) != sizeof (struct proc *)) {
                seterr("can't read allproc");
                return (-1);
            sizeof (struct proc *)) != sizeof (struct proc *)) {
                seterr("can't read allproc");
                return (-1);
@@ -418,6 +459,30 @@ again:
                        seterr("can't read proc at %x", p);
                        return (-1);
                }
                        seterr("can't read proc at %x", p);
                        return (-1);
                }
+#ifdef NEWVM
+               if (kvm_read(proc.p_cred, &eproc.e_pcred,
+                   sizeof (struct pcred)) == sizeof (struct pcred))
+                       (void) kvm_read(eproc.e_pcred.pc_ucred, &eproc.e_ucred,
+                           sizeof (struct ucred));
+               switch(ki_op(what)) {
+                       
+               case KINFO_PROC_PID:
+                       if (proc.p_pid != (pid_t)arg)
+                               continue;
+                       break;
+
+
+               case KINFO_PROC_UID:
+                       if (eproc.e_ucred.cr_uid != (uid_t)arg)
+                               continue;
+                       break;
+
+               case KINFO_PROC_RUID:
+                       if (eproc.e_pcred.p_ruid != (uid_t)arg)
+                               continue;
+                       break;
+               }
+#else
                switch(ki_op(what)) {
                        
                case KINFO_PROC_PID:
                switch(ki_op(what)) {
                        
                case KINFO_PROC_PID:
@@ -436,6 +501,7 @@ again:
                                continue;
                        break;
                }
                                continue;
                        break;
                }
+#endif
                /*
                 * gather eproc
                 */
                /*
                 * gather eproc
                 */
@@ -475,6 +541,12 @@ again:
                        eproc.e_tdev = NODEV;
                if (proc.p_wmesg)
                        kvm_read(proc.p_wmesg, eproc.e_wmesg, WMESGLEN);
                        eproc.e_tdev = NODEV;
                if (proc.p_wmesg)
                        kvm_read(proc.p_wmesg, eproc.e_wmesg, WMESGLEN);
+#ifdef NEWVM
+               (void) kvm_read(proc.p_vmspace, &eproc.e_vm,
+                   sizeof (struct vmspace));
+               eproc.e_xsize = eproc.e_xrssize =
+                       eproc.e_xccount = eproc.e_xswrss = 0;
+#else
                if (proc.p_textp) {
                        kvm_read(proc.p_textp, &text, sizeof (text));
                        eproc.e_xsize = text.x_size;
                if (proc.p_textp) {
                        kvm_read(proc.p_textp, &text, sizeof (text));
                        eproc.e_xsize = text.x_size;
@@ -485,6 +557,7 @@ again:
                        eproc.e_xsize = eproc.e_xrssize =
                          eproc.e_xccount = eproc.e_xswrss = 0;
                }
                        eproc.e_xsize = eproc.e_xrssize =
                          eproc.e_xccount = eproc.e_xswrss = 0;
                }
+#endif
 
                switch(ki_op(what)) {
 
 
                switch(ki_op(what)) {
 
@@ -508,7 +581,7 @@ again:
        }
        if (!doingzomb) {
                /* zombproc */
        }
        if (!doingzomb) {
                /* zombproc */
-               if (kvm_read(nl[X_ZOMBPROC].n_value, &p, 
+               if (kvm_read((void *) nl[X_ZOMBPROC].n_value, &p, 
                    sizeof (struct proc *)) != sizeof (struct proc *)) {
                        seterr("can't read zombproc");
                        return (-1);
                    sizeof (struct proc *)) != sizeof (struct proc *)) {
                        seterr("can't read zombproc");
                        return (-1);
@@ -535,14 +608,13 @@ kvm_nextproc()
 
 struct eproc *
 kvm_geteproc(p)
 
 struct eproc *
 kvm_geteproc(p)
-       struct proc *p;
+       const struct proc *p;
 {
        return ((struct eproc *)(((char *)p) + sizeof (struct proc)));
 }
 
 kvm_setproc()
 {
 {
        return ((struct eproc *)(((char *)p) + sizeof (struct proc)));
 }
 
 kvm_setproc()
 {
-
        kvmprocptr = kvmprocbase;
 }
 
        kvmprocptr = kvmprocbase;
 }
 
@@ -555,9 +627,67 @@ kvm_freeprocs()
        }
 }
 
        }
 }
 
+#ifdef NEWVM
 struct user *
 kvm_getu(p)
 struct user *
 kvm_getu(p)
-       struct proc *p;
+       const struct proc *p;
+{
+       register struct kinfo_proc *kp = (struct kinfo_proc *)p;
+       register int i;
+       register char *up;
+
+       if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
+               return (NULL);
+       if (p->p_stat == SZOMB) {
+               seterr("zombie process");
+               return (NULL);
+       }
+       /*
+        * Reading from swap is too complicated right now.
+        */
+       if ((p->p_flag & SLOAD) == 0)
+               return(NULL);
+       /*
+        * Read u-area one page at a time for the benefit of post-mortems
+        */
+       up = (char *) p->p_addr;
+       for (i = 0; i < UPAGES; i++) {
+               klseek(kmem, (long)up, 0);
+               if (read(kmem, user.upages[i], CLBYTES) != CLBYTES) {
+                       seterr("cant read page %x of u of pid %d from %s",
+                           up, p->p_pid, kmemf);
+                       return(NULL);
+               }
+               up += CLBYTES;
+       }
+       pcbpf = (int) btop(p->p_addr);  /* what should this be really? */
+       /*
+        * Conjure up a physical address for the arguments.
+        */
+       argaddr0 = argaddr1 = 0;
+#ifdef hp300
+       if (kp->kp_eproc.e_vm.vm_pmap.pm_ptab) {
+               struct pte pte[CLSIZE*2];
+
+               klseek(kmem,
+                   (long)&kp->kp_eproc.e_vm.vm_pmap.pm_ptab
+                   [btoc(USRSTACK-CLBYTES*2)], 0);
+               if (read(kmem, (char *)&pte, sizeof(pte)) == sizeof(pte)) {
+#if CLBYTES < 2048
+                       argaddr0 = ctob(pftoc(pte[CLSIZE*0].pg_pfnum));
+#endif
+                       argaddr1 = ctob(pftoc(pte[CLSIZE*1].pg_pfnum));
+               }
+       }
+       kp->kp_eproc.e_vm.vm_rssize =
+           kp->kp_eproc.e_vm.vm_pmap.pm_stats.resident_count; /* XXX */
+#endif
+       return(&user.user);
+}
+#else
+struct user *
+kvm_getu(p)
+       const struct proc *p;
 {
        struct pte *pteaddr, apte;
        struct pte arguutl[HIGHPAGES+(CLSIZE*2)];
 {
        struct pte *pteaddr, apte;
        struct pte arguutl[HIGHPAGES+(CLSIZE*2)];
@@ -578,7 +708,7 @@ kvm_getu(p)
                (void) lseek(swap, (long)dtob(p->p_swaddr), 0);
                if (read(swap, (char *)&user.user, sizeof (struct user)) != 
                    sizeof (struct user)) {
                (void) lseek(swap, (long)dtob(p->p_swaddr), 0);
                if (read(swap, (char *)&user.user, sizeof (struct user)) != 
                    sizeof (struct user)) {
-                       seterr("can't read u for pid %d from %s\n",
+                       seterr("can't read u for pid %d from %s",
                            p->p_pid, swapf);
                        return (NULL);
                }
                            p->p_pid, swapf);
                        return (NULL);
                }
@@ -622,11 +752,12 @@ kvm_getu(p)
        }
        return (&user.user);
 }
        }
        return (&user.user);
 }
+#endif
 
 char *
 kvm_getargs(p, up)
 
 char *
 kvm_getargs(p, up)
-       struct proc *p;
-       struct user *up;
+       const struct proc *p;
+       const struct user *up;
 {
        char cmdbuf[CLBYTES*2];
        union {
 {
        char cmdbuf[CLBYTES*2];
        union {
@@ -637,12 +768,21 @@ kvm_getargs(p, up)
        register int *ip;
        char c;
        int nbad;
        register int *ip;
        char c;
        int nbad;
+#ifndef NEWVM
        struct dblock db;
        struct dblock db;
-       char *file;
+#endif
+       const char *file;
+       int stkoff = 0;
 
 
+#if defined(NEWVM) && defined(hp300)
+       stkoff = 20;                    /* XXX for sigcode */
+#endif
        if (up == NULL || p->p_pid == 0 || p->p_pid == 2)
                goto retucomm;
        if ((p->p_flag & SLOAD) == 0 || argaddr1 == 0) {
        if (up == NULL || p->p_pid == 0 || p->p_pid == 2)
                goto retucomm;
        if ((p->p_flag & SLOAD) == 0 || argaddr1 == 0) {
+#ifdef NEWVM
+               goto retucomm;  /* XXX for now */
+#else
                if (swap < 0 || p->p_ssize == 0)
                        goto retucomm;
                vstodb(0, CLSIZE, &up->u_smap, &db, 1);
                if (swap < 0 || p->p_ssize == 0)
                        goto retucomm;
                vstodb(0, CLSIZE, &up->u_smap, &db, 1);
@@ -655,6 +795,7 @@ kvm_getargs(p, up)
                if (read(swap, (char *)&argspac.argc[0], CLBYTES) != CLBYTES)
                        goto bad;
                file = swapf;
                if (read(swap, (char *)&argspac.argc[0], CLBYTES) != CLBYTES)
                        goto bad;
                file = swapf;
+#endif
        } else {
                if (argaddr0) {
                        lseek(mem, (long)argaddr0, 0);
        } else {
                if (argaddr0) {
                        lseek(mem, (long)argaddr0, 0);
@@ -665,10 +806,11 @@ kvm_getargs(p, up)
                lseek(mem, (long)argaddr1, 0);
                if (read(mem, &argspac.argc[CLBYTES], CLBYTES) != CLBYTES)
                        goto bad;
                lseek(mem, (long)argaddr1, 0);
                if (read(mem, &argspac.argc[CLBYTES], CLBYTES) != CLBYTES)
                        goto bad;
-               file = memf;
+               file = (char *) memf;
        }
        ip = &argspac.argi[CLBYTES*2/sizeof (int)];
        }
        ip = &argspac.argi[CLBYTES*2/sizeof (int)];
-       ip -= 2;                /* last arg word and .long 0 */
+       ip -= 2;                /* last arg word and .long 0 */
+       ip -= stkoff / sizeof (int);
        while (*--ip) {
                if (ip == argspac.argi)
                        goto retucomm;
        while (*--ip) {
                if (ip == argspac.argi)
                        goto retucomm;
@@ -676,7 +818,7 @@ kvm_getargs(p, up)
        *(char *)ip = ' ';
        ip++;
        nbad = 0;
        *(char *)ip = ' ';
        ip++;
        nbad = 0;
-       for (cp = (char *)ip; cp < &argspac.argc[CLBYTES*2]; cp++) {
+       for (cp = (char *)ip; cp < &argspac.argc[CLBYTES*2-stkoff]; cp++) {
                c = *cp & 0177;
                if (c == 0)
                        *cp = ' ';
                c = *cp & 0177;
                if (c == 0)
                        *cp = ' ';
@@ -706,7 +848,7 @@ kvm_getargs(p, up)
        return (cmdbuf);
 
 bad:
        return (cmdbuf);
 
 bad:
-       seterr("error locating command name for pid %d from %s\n",
+       seterr("error locating command name for pid %d from %s",
            p->p_pid, file);
 retucomm:
        (void) strcpy(cmdbuf, " (");
            p->p_pid, file);
 retucomm:
        (void) strcpy(cmdbuf, " (");
@@ -719,13 +861,13 @@ retucomm:
 static
 getkvars()
 {
 static
 getkvars()
 {
-
        if (kvm_nlist(nl) == -1)
                return (-1);
        if (deadkernel) {
                /* We must do the sys map first because klseek uses it */
                long    addr;
 
        if (kvm_nlist(nl) == -1)
                return (-1);
        if (deadkernel) {
                /* We must do the sys map first because klseek uses it */
                long    addr;
 
+#ifndef NEWVM
                Syssize = nl[X_SYSSIZE].n_value;
                Sysmap = (struct pte *)
                        calloc((unsigned) Syssize, sizeof (struct pte));
                Syssize = nl[X_SYSSIZE].n_value;
                Sysmap = (struct pte *)
                        calloc((unsigned) Syssize, sizeof (struct pte));
@@ -741,6 +883,7 @@ getkvars()
                        seterr("can't read Sysmap");
                        return (-1);
                }
                        seterr("can't read Sysmap");
                        return (-1);
                }
+#endif
 #if defined(hp300)
                addr = (long) nl[X_LOWRAM].n_value;
                (void) lseek(kmem, addr, 0);
 #if defined(hp300)
                addr = (long) nl[X_LOWRAM].n_value;
                (void) lseek(kmem, addr, 0);
@@ -750,21 +893,36 @@ getkvars()
                        return (-1);
                }
                lowram = btop(lowram);
                        return (-1);
                }
                lowram = btop(lowram);
+               Sysseg = (struct ste *) malloc(NBPG);
+               if (Sysseg == NULL) {
+                       seterr("out of space for Sysseg");
+                       return (-1);
+               }
+               addr = (long) nl[X_SYSSEG].n_value;
+               (void) lseek(kmem, addr, 0);
+               read(kmem, (char *)&addr, sizeof(addr));
+               (void) lseek(kmem, (long)addr, 0);
+               if (read(kmem, (char *) Sysseg, NBPG) != NBPG) {
+                       seterr("can't read Sysseg");
+                       return (-1);
+               }
 #endif
        }
 #endif
        }
+#ifndef NEWVM
        usrpt = (struct pte *)nl[X_USRPT].n_value;
        Usrptmap = (struct pte *)nl[X_USRPTMAP].n_value;
        usrpt = (struct pte *)nl[X_USRPT].n_value;
        Usrptmap = (struct pte *)nl[X_USRPTMAP].n_value;
-       if (kvm_read((long)nl[X_NSWAP].n_value, &nswap, sizeof (long)) !=
+#endif
+       if (kvm_read((void *) nl[X_NSWAP].n_value, &nswap, sizeof (long)) !=
            sizeof (long)) {
                seterr("can't read nswap");
                return (-1);
        }
            sizeof (long)) {
                seterr("can't read nswap");
                return (-1);
        }
-       if (kvm_read((long)nl[X_DMMIN].n_value, &dmmin, sizeof (long)) !=
+       if (kvm_read((void *) nl[X_DMMIN].n_value, &dmmin, sizeof (long)) !=
            sizeof (long)) {
                seterr("can't read dmmin");
                return (-1);
        }
            sizeof (long)) {
                seterr("can't read dmmin");
                return (-1);
        }
-       if (kvm_read((long)nl[X_DMMAX].n_value, &dmmax, sizeof (long)) !=
+       if (kvm_read((void *) nl[X_DMMAX].n_value, &dmmax, sizeof (long)) !=
            sizeof (long)) {
                seterr("can't read dmmax");
                return (-1);
            sizeof (long)) {
                seterr("can't read dmmax");
                return (-1);
@@ -773,28 +931,28 @@ getkvars()
 }
 
 kvm_read(loc, buf, len)
 }
 
 kvm_read(loc, buf, len)
-       unsigned long loc;
-       char *buf;
+       void *loc;
+       void *buf;
 {
        if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
                return (-1);
        if (iskva(loc)) {
 {
        if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
                return (-1);
        if (iskva(loc)) {
-               klseek(kmem, loc, 0);
+               klseek(kmem, (off_t) loc, 0);
                if (read(kmem, buf, len) != len) {
                if (read(kmem, buf, len) != len) {
-                       seterr("error reading kmem at %x\n", loc);
+                       seterr("error reading kmem at %x", loc);
                        return (-1);
                }
        } else {
                        return (-1);
                }
        } else {
-               lseek(mem, loc, 0);
+               lseek(mem, (off_t) loc, 0);
                if (read(mem, buf, len) != len) {
                if (read(mem, buf, len) != len) {
-                       seterr("error reading mem at %x\n", loc);
+                       seterr("error reading mem at %x", loc);
                        return (-1);
                }
        }
        return (len);
 }
 
                        return (-1);
                }
        }
        return (len);
 }
 
-static
+static void
 klseek(fd, loc, off)
        int fd;
        off_t loc;
 klseek(fd, loc, off)
        int fd;
        off_t loc;
@@ -802,20 +960,19 @@ klseek(fd, loc, off)
 {
 
        if (deadkernel) {
 {
 
        if (deadkernel) {
-               off_t vtophys();
-
                if ((loc = vtophys(loc)) == -1)
                        return;
        }
        (void) lseek(fd, (off_t)loc, off);
 }
 
                if ((loc = vtophys(loc)) == -1)
                        return;
        }
        (void) lseek(fd, (off_t)loc, off);
 }
 
+#ifndef NEWVM
 /*
  * Given a base/size pair in virtual swap area,
  * return a physical base/size pair which is the
  * (largest) initial, physically contiguous block.
  */
 /*
  * Given a base/size pair in virtual swap area,
  * return a physical base/size pair which is the
  * (largest) initial, physically contiguous block.
  */
-static
+static void
 vstodb(vsbase, vssize, dmp, dbp, rev)
        register int vsbase;
        int vssize;
 vstodb(vsbase, vssize, dmp, dbp, rev)
        register int vsbase;
        int vssize;
@@ -840,7 +997,39 @@ vstodb(vsbase, vssize, dmp, dbp, rev)
        dbp->db_size = MIN(vssize, blk - vsbase);
        dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
 }
        dbp->db_size = MIN(vssize, blk - vsbase);
        dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
 }
+#endif
 
 
+#ifdef NEWVM
+static off_t
+vtophys(loc)
+       long    loc;
+{
+       off_t newloc = (off_t) -1;
+#ifdef hp300
+       int p, ste, pte;
+
+       ste = *(int *)&Sysseg[loc >> SG_ISHIFT];
+       if ((ste & SG_V) == 0) {
+               seterr("vtophys: segment not valid");
+               return((off_t) -1);
+       }
+       p = btop(loc & SG_PMASK);
+       newloc = (ste & SG_FRAME) + (p * sizeof(struct pte));
+       (void) lseek(kmem, (long)(newloc-(off_t)ptob(lowram)), 0);
+       if (read(kmem, (char *)&pte, sizeof pte) != sizeof pte) {
+               seterr("vtophys: cannot locate pte");
+               return((off_t) -1);
+       }
+       newloc = pte & PG_FRAME;
+       if (pte == PG_NV || newloc < (off_t)ptob(lowram)) {
+               seterr("vtophys: page not valid");
+               return((off_t) -1);
+       }
+       newloc = (newloc - (off_t)ptob(lowram)) + (loc & PGOFSET);
+#endif
+       return((off_t) newloc);
+}
+#else
 static off_t
 vtophys(loc)
        long loc;
 static off_t
 vtophys(loc)
        long loc;
@@ -875,11 +1064,12 @@ vtophys(loc)
        loc = (long) (ptob(pftoc(pte->pg_pfnum)) + (loc & PGOFSET));
        return(loc);
 }
        loc = (long) (ptob(pftoc(pte->pg_pfnum)) + (loc & PGOFSET));
        return(loc);
 }
+#endif
 
 #include <varargs.h>
 
 #include <varargs.h>
-static char errbuf[_BSD_LINE_MAX];
+static char errbuf[_POSIX2_LINE_MAX];
 
 
-static
+static void
 seterr(va_alist)
        va_dcl
 {
 seterr(va_alist)
        va_dcl
 {
@@ -888,11 +1078,11 @@ seterr(va_alist)
 
        va_start(ap);
        fmt = va_arg(ap, char *);
 
        va_start(ap);
        fmt = va_arg(ap, char *);
-       (void) vsprintf(errbuf, fmt, ap);
+       (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
        va_end(ap);
 }
 
        va_end(ap);
 }
 
-static
+static void
 setsyserr(va_alist)
        va_dcl
 {
 setsyserr(va_alist)
        va_dcl
 {
@@ -902,10 +1092,10 @@ setsyserr(va_alist)
 
        va_start(ap);
        fmt = va_arg(ap, char *);
 
        va_start(ap);
        fmt = va_arg(ap, char *);
-       (void) vsprintf(errbuf, fmt, ap);
+       (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
        for (cp=errbuf; *cp; cp++)
                ;
        for (cp=errbuf; *cp; cp++)
                ;
-       sprintf(cp, ": %s", strerror(errno));
+       snprintf(cp, _POSIX2_LINE_MAX - (cp - errbuf), ": %s", strerror(errno));
        va_end(ap);
 }
 
        va_end(ap);
 }