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