kvm.c revision 1.16 1 1.1 cgd /*-
2 1.9 cgd * Copyright (c) 1993 Christopher G. Demetriou
3 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
4 1.1 cgd * All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer in the
13 1.1 cgd * documentation and/or other materials provided with the distribution.
14 1.1 cgd * 3. All advertising materials mentioning features or use of this software
15 1.1 cgd * must display the following acknowledgement:
16 1.1 cgd * This product includes software developed by the University of
17 1.1 cgd * California, Berkeley and its contributors.
18 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
36 1.12 mycroft /*static char sccsid[] = "from: @(#)kvm.c 5.18 (Berkeley) 5/7/91";*/
37 1.16 mycroft static char rcsid[] = "$Id: kvm.c,v 1.16 1993/08/15 01:54:29 mycroft Exp $";
38 1.1 cgd #endif /* LIBC_SCCS and not lint */
39 1.1 cgd
40 1.1 cgd #include <sys/param.h>
41 1.1 cgd #include <sys/user.h>
42 1.1 cgd #include <sys/proc.h>
43 1.1 cgd #include <sys/ioctl.h>
44 1.1 cgd #include <sys/kinfo.h>
45 1.1 cgd #include <sys/tty.h>
46 1.8 cgd #include <sys/exec.h>
47 1.1 cgd #include <machine/vmparam.h>
48 1.1 cgd #include <fcntl.h>
49 1.1 cgd #include <nlist.h>
50 1.1 cgd #include <kvm.h>
51 1.1 cgd #include <ndbm.h>
52 1.1 cgd #include <limits.h>
53 1.1 cgd #include <paths.h>
54 1.1 cgd #include <stdio.h>
55 1.1 cgd #include <string.h>
56 1.1 cgd
57 1.1 cgd #ifdef SPPWAIT
58 1.1 cgd #define NEWVM
59 1.1 cgd #endif
60 1.1 cgd
61 1.1 cgd #ifdef NEWVM
62 1.1 cgd #define btop(x) (((unsigned)(x)) >> PGSHIFT) /* XXX */
63 1.1 cgd #define ptob(x) ((caddr_t)((x) << PGSHIFT)) /* XXX */
64 1.1 cgd #include <vm/vm.h> /* ??? kinfo_proc currently includes this*/
65 1.3 cgd #include <vm/vm_page.h>
66 1.3 cgd #include <vm/swap_pager.h>
67 1.1 cgd #include <sys/kinfo_proc.h>
68 1.16 mycroft #if defined(hp300) || defined(amiga)
69 1.13 mycroft #include <machine/pte.h>
70 1.16 mycroft #define btos(x) (((unsigned)(x)) >> SEGSHIFT) /* XXX */
71 1.1 cgd #endif
72 1.1 cgd #else /* NEWVM */
73 1.1 cgd #include <machine/pte.h>
74 1.1 cgd #include <sys/vmmac.h>
75 1.1 cgd #include <sys/text.h>
76 1.1 cgd #endif /* NEWVM */
77 1.1 cgd
78 1.1 cgd /*
79 1.1 cgd * files
80 1.1 cgd */
81 1.1 cgd static const char *unixf, *memf, *kmemf, *swapf;
82 1.1 cgd static int unixx, mem, kmem, swap;
83 1.1 cgd static DBM *db;
84 1.1 cgd /*
85 1.1 cgd * flags
86 1.1 cgd */
87 1.1 cgd static int deadkernel;
88 1.1 cgd static int kvminit = 0;
89 1.1 cgd static int kvmfilesopen = 0;
90 1.1 cgd /*
91 1.1 cgd * state
92 1.1 cgd */
93 1.1 cgd static struct kinfo_proc *kvmprocbase, *kvmprocptr;
94 1.1 cgd static int kvmnprocs;
95 1.1 cgd /*
96 1.1 cgd * u. buffer
97 1.1 cgd */
98 1.1 cgd static union {
99 1.1 cgd struct user user;
100 1.1 cgd char upages[UPAGES][NBPG];
101 1.1 cgd } user;
102 1.3 cgd
103 1.3 cgd #ifdef NEWVM
104 1.3 cgd struct swapblk {
105 1.3 cgd long offset; /* offset in swap device */
106 1.3 cgd long size; /* remaining size of block in swap device */
107 1.3 cgd };
108 1.3 cgd #endif
109 1.1 cgd /*
110 1.1 cgd * random other stuff
111 1.1 cgd */
112 1.1 cgd #ifndef NEWVM
113 1.1 cgd static struct pte *Usrptmap, *usrpt;
114 1.1 cgd static struct pte *Sysmap;
115 1.1 cgd static int Syssize;
116 1.1 cgd #endif
117 1.1 cgd static int dmmin, dmmax;
118 1.1 cgd static int pcbpf;
119 1.1 cgd static int nswap;
120 1.1 cgd static char *tmp;
121 1.16 mycroft #if defined(hp300) || defined(amiga)
122 1.1 cgd static int lowram;
123 1.1 cgd static struct ste *Sysseg;
124 1.1 cgd #endif
125 1.1 cgd #if defined(i386)
126 1.1 cgd static struct pde *PTD;
127 1.1 cgd #endif
128 1.1 cgd
129 1.1 cgd #define basename(cp) ((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
130 1.1 cgd #define MAXSYMSIZE 256
131 1.1 cgd
132 1.16 mycroft #if defined(hp300) || defined(amiga)
133 1.1 cgd #define pftoc(f) ((f) - lowram)
134 1.1 cgd #define iskva(v) (1)
135 1.1 cgd #endif
136 1.1 cgd
137 1.1 cgd #ifndef pftoc
138 1.1 cgd #define pftoc(f) (f)
139 1.1 cgd #endif
140 1.1 cgd #ifndef iskva
141 1.1 cgd #define iskva(v) ((u_long)(v) & KERNBASE)
142 1.1 cgd #endif
143 1.1 cgd
144 1.1 cgd static struct nlist nl[] = {
145 1.1 cgd { "_Usrptmap" },
146 1.1 cgd #define X_USRPTMAP 0
147 1.1 cgd { "_usrpt" },
148 1.1 cgd #define X_USRPT 1
149 1.1 cgd { "_nswap" },
150 1.1 cgd #define X_NSWAP 2
151 1.1 cgd { "_dmmin" },
152 1.1 cgd #define X_DMMIN 3
153 1.1 cgd { "_dmmax" },
154 1.1 cgd #define X_DMMAX 4
155 1.3 cgd { "_vm_page_buckets" },
156 1.3 cgd #define X_VM_PAGE_BUCKETS 5
157 1.3 cgd { "_vm_page_hash_mask" },
158 1.3 cgd #define X_VM_PAGE_HASH_MASK 6
159 1.3 cgd { "_page_shift" },
160 1.3 cgd #define X_PAGE_SHIFT 7
161 1.1 cgd /*
162 1.1 cgd * everything here and down, only if a dead kernel
163 1.1 cgd */
164 1.1 cgd { "_Sysmap" },
165 1.3 cgd #define X_SYSMAP 8
166 1.1 cgd #define X_DEADKERNEL X_SYSMAP
167 1.1 cgd { "_Syssize" },
168 1.3 cgd #define X_SYSSIZE 9
169 1.1 cgd { "_allproc" },
170 1.3 cgd #define X_ALLPROC 10
171 1.1 cgd { "_zombproc" },
172 1.3 cgd #define X_ZOMBPROC 11
173 1.1 cgd { "_nproc" },
174 1.3 cgd #define X_NPROC 12
175 1.3 cgd #define X_LAST 12
176 1.16 mycroft #if defined(hp300) || defined(amiga)
177 1.1 cgd { "_Sysseg" },
178 1.1 cgd #define X_SYSSEG (X_LAST+1)
179 1.1 cgd { "_lowram" },
180 1.1 cgd #define X_LOWRAM (X_LAST+2)
181 1.1 cgd #endif
182 1.1 cgd #if defined(i386)
183 1.1 cgd { "_IdlePTD" },
184 1.1 cgd #define X_IdlePTD (X_LAST+1)
185 1.1 cgd #endif
186 1.1 cgd { "" },
187 1.1 cgd };
188 1.1 cgd
189 1.1 cgd static off_t Vtophys();
190 1.1 cgd static void klseek(), seterr(), setsyserr(), vstodb();
191 1.1 cgd static int getkvars(), kvm_doprocs(), kvm_init();
192 1.3 cgd #ifdef NEWVM
193 1.3 cgd static int vatosw();
194 1.3 cgd static int findpage();
195 1.3 cgd #endif
196 1.1 cgd
197 1.1 cgd /*
198 1.1 cgd * returns 0 if files were opened now,
199 1.1 cgd * 1 if files were already opened,
200 1.1 cgd * -1 if files could not be opened.
201 1.1 cgd */
202 1.1 cgd kvm_openfiles(uf, mf, sf)
203 1.1 cgd const char *uf, *mf, *sf;
204 1.1 cgd {
205 1.1 cgd if (kvmfilesopen)
206 1.1 cgd return (1);
207 1.1 cgd unixx = mem = kmem = swap = -1;
208 1.1 cgd unixf = (uf == NULL) ? _PATH_UNIX : uf;
209 1.1 cgd memf = (mf == NULL) ? _PATH_MEM : mf;
210 1.1 cgd
211 1.1 cgd if ((unixx = open(unixf, O_RDONLY, 0)) == -1) {
212 1.1 cgd setsyserr("can't open %s", unixf);
213 1.1 cgd goto failed;
214 1.1 cgd }
215 1.1 cgd if ((mem = open(memf, O_RDONLY, 0)) == -1) {
216 1.1 cgd setsyserr("can't open %s", memf);
217 1.1 cgd goto failed;
218 1.1 cgd }
219 1.1 cgd if (sf != NULL)
220 1.1 cgd swapf = sf;
221 1.1 cgd if (mf != NULL) {
222 1.1 cgd deadkernel++;
223 1.1 cgd kmemf = mf;
224 1.1 cgd kmem = mem;
225 1.1 cgd swap = -1;
226 1.1 cgd } else {
227 1.1 cgd kmemf = _PATH_KMEM;
228 1.1 cgd if ((kmem = open(kmemf, O_RDONLY, 0)) == -1) {
229 1.1 cgd setsyserr("can't open %s", kmemf);
230 1.1 cgd goto failed;
231 1.1 cgd }
232 1.1 cgd swapf = (sf == NULL) ? _PATH_DRUM : sf;
233 1.1 cgd /*
234 1.1 cgd * live kernel - avoid looking up nlist entries
235 1.1 cgd * past X_DEADKERNEL.
236 1.1 cgd */
237 1.1 cgd nl[X_DEADKERNEL].n_name = "";
238 1.1 cgd }
239 1.1 cgd if (swapf != NULL && ((swap = open(swapf, O_RDONLY, 0)) == -1)) {
240 1.1 cgd seterr("can't open %s", swapf);
241 1.1 cgd goto failed;
242 1.1 cgd }
243 1.1 cgd kvmfilesopen++;
244 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1) /*XXX*/
245 1.1 cgd return (-1);
246 1.1 cgd return (0);
247 1.1 cgd failed:
248 1.1 cgd kvm_close();
249 1.1 cgd return (-1);
250 1.1 cgd }
251 1.1 cgd
252 1.1 cgd static
253 1.1 cgd kvm_init(uf, mf, sf)
254 1.1 cgd char *uf, *mf, *sf;
255 1.1 cgd {
256 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
257 1.1 cgd return (-1);
258 1.1 cgd if (getkvars() == -1)
259 1.1 cgd return (-1);
260 1.1 cgd kvminit = 1;
261 1.1 cgd
262 1.1 cgd return (0);
263 1.1 cgd }
264 1.1 cgd
265 1.1 cgd kvm_close()
266 1.1 cgd {
267 1.1 cgd if (unixx != -1) {
268 1.1 cgd close(unixx);
269 1.1 cgd unixx = -1;
270 1.1 cgd }
271 1.1 cgd if (kmem != -1) {
272 1.1 cgd if (kmem != mem)
273 1.1 cgd close(kmem);
274 1.1 cgd /* otherwise kmem is a copy of mem, and will be closed below */
275 1.1 cgd kmem = -1;
276 1.1 cgd }
277 1.1 cgd if (mem != -1) {
278 1.1 cgd close(mem);
279 1.1 cgd mem = -1;
280 1.1 cgd }
281 1.1 cgd if (swap != -1) {
282 1.1 cgd close(swap);
283 1.1 cgd swap = -1;
284 1.1 cgd }
285 1.1 cgd if (db != NULL) {
286 1.1 cgd dbm_close(db);
287 1.1 cgd db = NULL;
288 1.1 cgd }
289 1.1 cgd kvminit = 0;
290 1.1 cgd kvmfilesopen = 0;
291 1.1 cgd deadkernel = 0;
292 1.1 cgd #ifndef NEWVM
293 1.1 cgd if (Sysmap) {
294 1.1 cgd free(Sysmap);
295 1.1 cgd Sysmap = NULL;
296 1.1 cgd }
297 1.1 cgd #endif
298 1.1 cgd }
299 1.1 cgd
300 1.1 cgd kvm_nlist(nl)
301 1.1 cgd struct nlist *nl;
302 1.1 cgd {
303 1.1 cgd datum key, data;
304 1.1 cgd char dbname[MAXPATHLEN];
305 1.1 cgd char dbversion[_POSIX2_LINE_MAX];
306 1.1 cgd char kversion[_POSIX2_LINE_MAX];
307 1.1 cgd int dbversionlen;
308 1.1 cgd char symbuf[MAXSYMSIZE];
309 1.1 cgd struct nlist nbuf, *n;
310 1.1 cgd int num, did;
311 1.1 cgd
312 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
313 1.1 cgd return (-1);
314 1.1 cgd if (deadkernel)
315 1.1 cgd goto hard2;
316 1.1 cgd /*
317 1.1 cgd * initialize key datum
318 1.1 cgd */
319 1.1 cgd key.dptr = symbuf;
320 1.1 cgd
321 1.1 cgd if (db != NULL)
322 1.1 cgd goto win; /* off to the races */
323 1.1 cgd /*
324 1.1 cgd * open database
325 1.1 cgd */
326 1.1 cgd sprintf(dbname, "%s/kvm_%s", _PATH_VARRUN, basename(unixf));
327 1.1 cgd if ((db = dbm_open(dbname, O_RDONLY, 0)) == NULL)
328 1.1 cgd goto hard2;
329 1.1 cgd /*
330 1.1 cgd * read version out of database
331 1.1 cgd */
332 1.1 cgd bcopy("VERSION", symbuf, sizeof ("VERSION")-1);
333 1.2 cgd key.dsize = (sizeof ("VERSION") - 1);
334 1.1 cgd data = dbm_fetch(db, key);
335 1.1 cgd if (data.dptr == NULL)
336 1.1 cgd goto hard1;
337 1.1 cgd bcopy(data.dptr, dbversion, data.dsize);
338 1.1 cgd dbversionlen = data.dsize;
339 1.1 cgd /*
340 1.1 cgd * read version string from kernel memory
341 1.1 cgd */
342 1.1 cgd bcopy("_version", symbuf, sizeof ("_version")-1);
343 1.2 cgd key.dsize = (sizeof ("_version")-1);
344 1.1 cgd data = dbm_fetch(db, key);
345 1.1 cgd if (data.dptr == NULL)
346 1.1 cgd goto hard1;
347 1.1 cgd if (data.dsize != sizeof (struct nlist))
348 1.1 cgd goto hard1;
349 1.1 cgd bcopy(data.dptr, &nbuf, sizeof (struct nlist));
350 1.1 cgd lseek(kmem, nbuf.n_value, 0);
351 1.1 cgd if (read(kmem, kversion, dbversionlen) != dbversionlen)
352 1.1 cgd goto hard1;
353 1.1 cgd /*
354 1.1 cgd * if they match, we win - otherwise do it the hard way
355 1.1 cgd */
356 1.1 cgd if (bcmp(dbversion, kversion, dbversionlen) != 0)
357 1.1 cgd goto hard1;
358 1.1 cgd /*
359 1.1 cgd * getem from the database.
360 1.1 cgd */
361 1.1 cgd win:
362 1.1 cgd num = did = 0;
363 1.1 cgd for (n = nl; n->n_name && n->n_name[0]; n++, num++) {
364 1.1 cgd int len;
365 1.1 cgd /*
366 1.1 cgd * clear out fields from users buffer
367 1.1 cgd */
368 1.1 cgd n->n_type = 0;
369 1.1 cgd n->n_other = 0;
370 1.1 cgd n->n_desc = 0;
371 1.1 cgd n->n_value = 0;
372 1.1 cgd /*
373 1.1 cgd * query db
374 1.1 cgd */
375 1.1 cgd if ((len = strlen(n->n_name)) > MAXSYMSIZE) {
376 1.1 cgd seterr("symbol too large");
377 1.1 cgd return (-1);
378 1.1 cgd }
379 1.1 cgd (void)strcpy(symbuf, n->n_name);
380 1.2 cgd key.dsize = len;
381 1.1 cgd data = dbm_fetch(db, key);
382 1.1 cgd if (data.dptr == NULL || data.dsize != sizeof (struct nlist))
383 1.1 cgd continue;
384 1.1 cgd bcopy(data.dptr, &nbuf, sizeof (struct nlist));
385 1.1 cgd n->n_value = nbuf.n_value;
386 1.1 cgd n->n_type = nbuf.n_type;
387 1.1 cgd n->n_desc = nbuf.n_desc;
388 1.1 cgd n->n_other = nbuf.n_other;
389 1.1 cgd did++;
390 1.1 cgd }
391 1.1 cgd return (num - did);
392 1.1 cgd hard1:
393 1.1 cgd dbm_close(db);
394 1.1 cgd db = NULL;
395 1.1 cgd hard2:
396 1.1 cgd num = nlist(unixf, nl);
397 1.1 cgd if (num == -1)
398 1.1 cgd seterr("nlist (hard way) failed");
399 1.1 cgd return (num);
400 1.1 cgd }
401 1.1 cgd
402 1.1 cgd kvm_getprocs(what, arg)
403 1.1 cgd int what, arg;
404 1.1 cgd {
405 1.3 cgd static int ocopysize = -1;
406 1.3 cgd
407 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
408 1.1 cgd return (NULL);
409 1.1 cgd if (!deadkernel) {
410 1.1 cgd int ret, copysize;
411 1.1 cgd
412 1.1 cgd if ((ret = getkerninfo(what, NULL, NULL, arg)) == -1) {
413 1.1 cgd setsyserr("can't get estimate for kerninfo");
414 1.1 cgd return (-1);
415 1.1 cgd }
416 1.1 cgd copysize = ret;
417 1.5 cgd if (copysize > ocopysize || !kvmprocbase) {
418 1.5 cgd if (ocopysize == -1 || !kvmprocbase)
419 1.4 mycroft kvmprocbase =
420 1.4 mycroft (struct kinfo_proc *)malloc(copysize);
421 1.4 mycroft else
422 1.4 mycroft kvmprocbase =
423 1.4 mycroft (struct kinfo_proc *)realloc(kvmprocbase,
424 1.4 mycroft copysize);
425 1.4 mycroft if (!kvmprocbase) {
426 1.4 mycroft seterr("out of memory");
427 1.4 mycroft return (-1);
428 1.4 mycroft }
429 1.1 cgd }
430 1.3 cgd ocopysize = copysize;
431 1.1 cgd if ((ret = getkerninfo(what, kvmprocbase, ©size,
432 1.1 cgd arg)) == -1) {
433 1.1 cgd setsyserr("can't get proc list");
434 1.1 cgd return (-1);
435 1.1 cgd }
436 1.1 cgd if (copysize % sizeof (struct kinfo_proc)) {
437 1.1 cgd seterr("proc size mismatch (got %d total, kinfo_proc: %d)",
438 1.1 cgd copysize, sizeof (struct kinfo_proc));
439 1.1 cgd return (-1);
440 1.1 cgd }
441 1.1 cgd kvmnprocs = copysize / sizeof (struct kinfo_proc);
442 1.1 cgd } else {
443 1.1 cgd int nproc;
444 1.1 cgd
445 1.1 cgd if (kvm_read((void *) nl[X_NPROC].n_value, &nproc,
446 1.1 cgd sizeof (int)) != sizeof (int)) {
447 1.1 cgd seterr("can't read nproc");
448 1.1 cgd return (-1);
449 1.1 cgd }
450 1.1 cgd if ((kvmprocbase = (struct kinfo_proc *)
451 1.1 cgd malloc(nproc * sizeof (struct kinfo_proc))) == NULL) {
452 1.1 cgd seterr("out of memory (addr: %x nproc = %d)",
453 1.1 cgd nl[X_NPROC].n_value, nproc);
454 1.1 cgd return (-1);
455 1.1 cgd }
456 1.1 cgd kvmnprocs = kvm_doprocs(what, arg, kvmprocbase);
457 1.1 cgd realloc(kvmprocbase, kvmnprocs * sizeof (struct kinfo_proc));
458 1.1 cgd }
459 1.1 cgd kvmprocptr = kvmprocbase;
460 1.1 cgd
461 1.1 cgd return (kvmnprocs);
462 1.1 cgd }
463 1.1 cgd
464 1.1 cgd /*
465 1.1 cgd * XXX - should NOT give up so easily - especially since the kernel
466 1.1 cgd * may be corrupt (it died). Should gather as much information as possible.
467 1.1 cgd * Follows proc ptrs instead of reading table since table may go
468 1.1 cgd * away soon.
469 1.1 cgd */
470 1.1 cgd static
471 1.1 cgd kvm_doprocs(what, arg, buff)
472 1.1 cgd int what, arg;
473 1.1 cgd char *buff;
474 1.1 cgd {
475 1.1 cgd struct proc *p, proc;
476 1.1 cgd register char *bp = buff;
477 1.1 cgd int i = 0;
478 1.1 cgd int doingzomb = 0;
479 1.1 cgd struct eproc eproc;
480 1.1 cgd struct pgrp pgrp;
481 1.1 cgd struct session sess;
482 1.1 cgd struct tty tty;
483 1.1 cgd #ifndef NEWVM
484 1.1 cgd struct text text;
485 1.1 cgd #endif
486 1.1 cgd
487 1.1 cgd /* allproc */
488 1.1 cgd if (kvm_read((void *) nl[X_ALLPROC].n_value, &p,
489 1.1 cgd sizeof (struct proc *)) != sizeof (struct proc *)) {
490 1.1 cgd seterr("can't read allproc");
491 1.1 cgd return (-1);
492 1.1 cgd }
493 1.1 cgd
494 1.1 cgd again:
495 1.1 cgd for (; p; p = proc.p_nxt) {
496 1.1 cgd if (kvm_read(p, &proc, sizeof (struct proc)) !=
497 1.1 cgd sizeof (struct proc)) {
498 1.1 cgd seterr("can't read proc at %x", p);
499 1.1 cgd return (-1);
500 1.1 cgd }
501 1.1 cgd #ifdef NEWVM
502 1.1 cgd if (kvm_read(proc.p_cred, &eproc.e_pcred,
503 1.1 cgd sizeof (struct pcred)) == sizeof (struct pcred))
504 1.1 cgd (void) kvm_read(eproc.e_pcred.pc_ucred, &eproc.e_ucred,
505 1.1 cgd sizeof (struct ucred));
506 1.1 cgd switch(ki_op(what)) {
507 1.1 cgd
508 1.1 cgd case KINFO_PROC_PID:
509 1.1 cgd if (proc.p_pid != (pid_t)arg)
510 1.1 cgd continue;
511 1.1 cgd break;
512 1.1 cgd
513 1.1 cgd
514 1.1 cgd case KINFO_PROC_UID:
515 1.1 cgd if (eproc.e_ucred.cr_uid != (uid_t)arg)
516 1.1 cgd continue;
517 1.1 cgd break;
518 1.1 cgd
519 1.1 cgd case KINFO_PROC_RUID:
520 1.1 cgd if (eproc.e_pcred.p_ruid != (uid_t)arg)
521 1.1 cgd continue;
522 1.1 cgd break;
523 1.1 cgd }
524 1.1 cgd #else
525 1.1 cgd switch(ki_op(what)) {
526 1.1 cgd
527 1.1 cgd case KINFO_PROC_PID:
528 1.1 cgd if (proc.p_pid != (pid_t)arg)
529 1.1 cgd continue;
530 1.1 cgd break;
531 1.1 cgd
532 1.1 cgd
533 1.1 cgd case KINFO_PROC_UID:
534 1.1 cgd if (proc.p_uid != (uid_t)arg)
535 1.1 cgd continue;
536 1.1 cgd break;
537 1.1 cgd
538 1.1 cgd case KINFO_PROC_RUID:
539 1.1 cgd if (proc.p_ruid != (uid_t)arg)
540 1.1 cgd continue;
541 1.1 cgd break;
542 1.1 cgd }
543 1.1 cgd #endif
544 1.1 cgd /*
545 1.1 cgd * gather eproc
546 1.1 cgd */
547 1.1 cgd eproc.e_paddr = p;
548 1.1 cgd if (kvm_read(proc.p_pgrp, &pgrp, sizeof (struct pgrp)) !=
549 1.1 cgd sizeof (struct pgrp)) {
550 1.1 cgd seterr("can't read pgrp at %x", proc.p_pgrp);
551 1.1 cgd return (-1);
552 1.1 cgd }
553 1.1 cgd eproc.e_sess = pgrp.pg_session;
554 1.1 cgd eproc.e_pgid = pgrp.pg_id;
555 1.1 cgd eproc.e_jobc = pgrp.pg_jobc;
556 1.1 cgd if (kvm_read(pgrp.pg_session, &sess, sizeof (struct session))
557 1.1 cgd != sizeof (struct session)) {
558 1.1 cgd seterr("can't read session at %x", pgrp.pg_session);
559 1.1 cgd return (-1);
560 1.1 cgd }
561 1.1 cgd if ((proc.p_flag&SCTTY) && sess.s_ttyp != NULL) {
562 1.1 cgd if (kvm_read(sess.s_ttyp, &tty, sizeof (struct tty))
563 1.1 cgd != sizeof (struct tty)) {
564 1.1 cgd seterr("can't read tty at %x", sess.s_ttyp);
565 1.1 cgd return (-1);
566 1.1 cgd }
567 1.1 cgd eproc.e_tdev = tty.t_dev;
568 1.1 cgd eproc.e_tsess = tty.t_session;
569 1.1 cgd if (tty.t_pgrp != NULL) {
570 1.1 cgd if (kvm_read(tty.t_pgrp, &pgrp, sizeof (struct
571 1.1 cgd pgrp)) != sizeof (struct pgrp)) {
572 1.1 cgd seterr("can't read tpgrp at &x",
573 1.1 cgd tty.t_pgrp);
574 1.1 cgd return (-1);
575 1.1 cgd }
576 1.1 cgd eproc.e_tpgid = pgrp.pg_id;
577 1.1 cgd } else
578 1.1 cgd eproc.e_tpgid = -1;
579 1.1 cgd } else
580 1.1 cgd eproc.e_tdev = NODEV;
581 1.1 cgd if (proc.p_wmesg)
582 1.1 cgd kvm_read(proc.p_wmesg, eproc.e_wmesg, WMESGLEN);
583 1.1 cgd #ifdef NEWVM
584 1.1 cgd (void) kvm_read(proc.p_vmspace, &eproc.e_vm,
585 1.1 cgd sizeof (struct vmspace));
586 1.1 cgd eproc.e_xsize = eproc.e_xrssize =
587 1.1 cgd eproc.e_xccount = eproc.e_xswrss = 0;
588 1.1 cgd #else
589 1.1 cgd if (proc.p_textp) {
590 1.1 cgd kvm_read(proc.p_textp, &text, sizeof (text));
591 1.1 cgd eproc.e_xsize = text.x_size;
592 1.1 cgd eproc.e_xrssize = text.x_rssize;
593 1.1 cgd eproc.e_xccount = text.x_ccount;
594 1.1 cgd eproc.e_xswrss = text.x_swrss;
595 1.1 cgd } else {
596 1.1 cgd eproc.e_xsize = eproc.e_xrssize =
597 1.1 cgd eproc.e_xccount = eproc.e_xswrss = 0;
598 1.1 cgd }
599 1.1 cgd #endif
600 1.1 cgd
601 1.1 cgd switch(ki_op(what)) {
602 1.1 cgd
603 1.1 cgd case KINFO_PROC_PGRP:
604 1.1 cgd if (eproc.e_pgid != (pid_t)arg)
605 1.1 cgd continue;
606 1.1 cgd break;
607 1.1 cgd
608 1.1 cgd case KINFO_PROC_TTY:
609 1.1 cgd if ((proc.p_flag&SCTTY) == 0 ||
610 1.1 cgd eproc.e_tdev != (dev_t)arg)
611 1.1 cgd continue;
612 1.1 cgd break;
613 1.1 cgd }
614 1.1 cgd
615 1.1 cgd i++;
616 1.1 cgd bcopy(&proc, bp, sizeof (struct proc));
617 1.1 cgd bp += sizeof (struct proc);
618 1.1 cgd bcopy(&eproc, bp, sizeof (struct eproc));
619 1.1 cgd bp+= sizeof (struct eproc);
620 1.1 cgd }
621 1.1 cgd if (!doingzomb) {
622 1.1 cgd /* zombproc */
623 1.1 cgd if (kvm_read((void *) nl[X_ZOMBPROC].n_value, &p,
624 1.1 cgd sizeof (struct proc *)) != sizeof (struct proc *)) {
625 1.1 cgd seterr("can't read zombproc");
626 1.1 cgd return (-1);
627 1.1 cgd }
628 1.1 cgd doingzomb = 1;
629 1.1 cgd goto again;
630 1.1 cgd }
631 1.1 cgd
632 1.1 cgd return (i);
633 1.1 cgd }
634 1.1 cgd
635 1.1 cgd struct proc *
636 1.1 cgd kvm_nextproc()
637 1.1 cgd {
638 1.1 cgd
639 1.1 cgd if (!kvmprocbase && kvm_getprocs(0, 0) == -1)
640 1.1 cgd return (NULL);
641 1.1 cgd if (kvmprocptr >= (kvmprocbase + kvmnprocs)) {
642 1.1 cgd seterr("end of proc list");
643 1.1 cgd return (NULL);
644 1.1 cgd }
645 1.1 cgd return((struct proc *)(kvmprocptr++));
646 1.1 cgd }
647 1.1 cgd
648 1.1 cgd struct eproc *
649 1.1 cgd kvm_geteproc(p)
650 1.1 cgd const struct proc *p;
651 1.1 cgd {
652 1.1 cgd return ((struct eproc *)(((char *)p) + sizeof (struct proc)));
653 1.1 cgd }
654 1.1 cgd
655 1.1 cgd kvm_setproc()
656 1.1 cgd {
657 1.1 cgd kvmprocptr = kvmprocbase;
658 1.1 cgd }
659 1.1 cgd
660 1.1 cgd kvm_freeprocs()
661 1.1 cgd {
662 1.1 cgd
663 1.1 cgd if (kvmprocbase) {
664 1.1 cgd free(kvmprocbase);
665 1.1 cgd kvmprocbase = NULL;
666 1.1 cgd }
667 1.1 cgd }
668 1.1 cgd
669 1.1 cgd #ifdef NEWVM
670 1.1 cgd struct user *
671 1.1 cgd kvm_getu(p)
672 1.1 cgd const struct proc *p;
673 1.1 cgd {
674 1.1 cgd register struct kinfo_proc *kp = (struct kinfo_proc *)p;
675 1.1 cgd register int i;
676 1.1 cgd register char *up;
677 1.3 cgd u_int vaddr;
678 1.3 cgd struct swapblk swb;
679 1.1 cgd
680 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
681 1.1 cgd return (NULL);
682 1.1 cgd if (p->p_stat == SZOMB) {
683 1.1 cgd seterr("zombie process");
684 1.1 cgd return (NULL);
685 1.1 cgd }
686 1.3 cgd
687 1.3 cgd if ((p->p_flag & SLOAD) == 0) {
688 1.3 cgd vm_offset_t maddr;
689 1.3 cgd
690 1.3 cgd if (swap < 0) {
691 1.3 cgd seterr("no swap");
692 1.3 cgd return (NULL);
693 1.3 cgd }
694 1.3 cgd /*
695 1.3 cgd * Costly operation, better set enable_swap to zero
696 1.3 cgd * in vm/vm_glue.c, since paging of user pages isn't
697 1.3 cgd * done yet anyway.
698 1.3 cgd */
699 1.3 cgd if (vatosw(p, USRSTACK + i * NBPG, &maddr, &swb) == 0)
700 1.3 cgd return NULL;
701 1.3 cgd
702 1.3 cgd if (maddr == 0 && swb.size < UPAGES * NBPG)
703 1.3 cgd return NULL;
704 1.3 cgd
705 1.3 cgd for (i = 0; i < UPAGES; i++) {
706 1.3 cgd if (maddr) {
707 1.3 cgd (void) lseek(mem, maddr + i * NBPG, 0);
708 1.3 cgd if (read(mem,
709 1.3 cgd (char *)user.upages[i], NBPG) != NBPG) {
710 1.3 cgd seterr(
711 1.3 cgd "can't read u for pid %d from %s",
712 1.3 cgd p->p_pid, swapf);
713 1.3 cgd return NULL;
714 1.3 cgd }
715 1.3 cgd } else {
716 1.3 cgd (void) lseek(swap, swb.offset + i * NBPG, 0);
717 1.3 cgd if (read(swap,
718 1.3 cgd (char *)user.upages[i], NBPG) != NBPG) {
719 1.3 cgd seterr(
720 1.3 cgd "can't read u for pid %d from %s",
721 1.3 cgd p->p_pid, swapf);
722 1.3 cgd return NULL;
723 1.3 cgd }
724 1.3 cgd }
725 1.3 cgd }
726 1.3 cgd return(&user.user);
727 1.3 cgd }
728 1.1 cgd /*
729 1.1 cgd * Read u-area one page at a time for the benefit of post-mortems
730 1.1 cgd */
731 1.1 cgd up = (char *) p->p_addr;
732 1.1 cgd for (i = 0; i < UPAGES; i++) {
733 1.1 cgd klseek(kmem, (long)up, 0);
734 1.1 cgd if (read(kmem, user.upages[i], CLBYTES) != CLBYTES) {
735 1.1 cgd seterr("cant read page %x of u of pid %d from %s",
736 1.1 cgd up, p->p_pid, kmemf);
737 1.1 cgd return(NULL);
738 1.1 cgd }
739 1.1 cgd up += CLBYTES;
740 1.1 cgd }
741 1.1 cgd pcbpf = (int) btop(p->p_addr); /* what should this be really? */
742 1.1 cgd
743 1.1 cgd kp->kp_eproc.e_vm.vm_rssize =
744 1.1 cgd kp->kp_eproc.e_vm.vm_pmap.pm_stats.resident_count; /* XXX */
745 1.1 cgd return(&user.user);
746 1.1 cgd }
747 1.1 cgd #else
748 1.1 cgd struct user *
749 1.1 cgd kvm_getu(p)
750 1.1 cgd const struct proc *p;
751 1.1 cgd {
752 1.1 cgd struct pte *pteaddr, apte;
753 1.1 cgd struct pte arguutl[HIGHPAGES+(CLSIZE*2)];
754 1.1 cgd register int i;
755 1.1 cgd int ncl;
756 1.1 cgd
757 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
758 1.1 cgd return (NULL);
759 1.1 cgd if (p->p_stat == SZOMB) {
760 1.1 cgd seterr("zombie process");
761 1.1 cgd return (NULL);
762 1.1 cgd }
763 1.1 cgd if ((p->p_flag & SLOAD) == 0) {
764 1.1 cgd if (swap < 0) {
765 1.1 cgd seterr("no swap");
766 1.1 cgd return (NULL);
767 1.1 cgd }
768 1.1 cgd (void) lseek(swap, (long)dtob(p->p_swaddr), 0);
769 1.1 cgd if (read(swap, (char *)&user.user, sizeof (struct user)) !=
770 1.1 cgd sizeof (struct user)) {
771 1.1 cgd seterr("can't read u for pid %d from %s",
772 1.1 cgd p->p_pid, swapf);
773 1.1 cgd return (NULL);
774 1.1 cgd }
775 1.1 cgd pcbpf = 0;
776 1.1 cgd argaddr0 = 0;
777 1.1 cgd argaddr1 = 0;
778 1.1 cgd return (&user.user);
779 1.1 cgd }
780 1.1 cgd pteaddr = &Usrptmap[btokmx(p->p_p0br) + p->p_szpt - 1];
781 1.1 cgd klseek(kmem, (long)pteaddr, 0);
782 1.1 cgd if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
783 1.1 cgd seterr("can't read indir pte to get u for pid %d from %s",
784 1.1 cgd p->p_pid, kmemf);
785 1.1 cgd return (NULL);
786 1.1 cgd }
787 1.1 cgd lseek(mem, (long)ctob(pftoc(apte.pg_pfnum+1)) - sizeof(arguutl), 0);
788 1.1 cgd if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
789 1.1 cgd seterr("can't read page table for u of pid %d from %s",
790 1.1 cgd p->p_pid, memf);
791 1.1 cgd return (NULL);
792 1.1 cgd }
793 1.1 cgd if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
794 1.1 cgd argaddr0 = ctob(pftoc(arguutl[0].pg_pfnum));
795 1.1 cgd else
796 1.1 cgd argaddr0 = 0;
797 1.1 cgd if (arguutl[CLSIZE*1].pg_fod == 0 && arguutl[CLSIZE*1].pg_pfnum)
798 1.1 cgd argaddr1 = ctob(pftoc(arguutl[CLSIZE*1].pg_pfnum));
799 1.1 cgd else
800 1.1 cgd argaddr1 = 0;
801 1.1 cgd pcbpf = arguutl[CLSIZE*2].pg_pfnum;
802 1.1 cgd ncl = (sizeof (struct user) + CLBYTES - 1) / CLBYTES;
803 1.1 cgd while (--ncl >= 0) {
804 1.1 cgd i = ncl * CLSIZE;
805 1.1 cgd lseek(mem,
806 1.1 cgd (long)ctob(pftoc(arguutl[(CLSIZE*2)+i].pg_pfnum)), 0);
807 1.1 cgd if (read(mem, user.upages[i], CLBYTES) != CLBYTES) {
808 1.1 cgd seterr("can't read page %d of u of pid %d from %s",
809 1.1 cgd arguutl[(CLSIZE*2)+i].pg_pfnum, p->p_pid, memf);
810 1.1 cgd return(NULL);
811 1.1 cgd }
812 1.1 cgd }
813 1.1 cgd return (&user.user);
814 1.1 cgd }
815 1.1 cgd #endif
816 1.1 cgd
817 1.8 cgd int
818 1.8 cgd kvm_procread(p, addr, buf, len)
819 1.8 cgd const struct proc *p;
820 1.11 mycroft const unsigned addr, len;
821 1.11 mycroft char *buf;
822 1.8 cgd {
823 1.8 cgd register struct kinfo_proc *kp = (struct kinfo_proc *) p;
824 1.8 cgd struct swapblk swb;
825 1.8 cgd vm_offset_t swaddr = 0, memaddr = 0;
826 1.8 cgd unsigned real_len;
827 1.8 cgd
828 1.8 cgd real_len = len < (CLBYTES - (addr & CLOFSET)) ? len : (CLBYTES - (addr & CLOFSET));
829 1.8 cgd
830 1.16 mycroft #if defined(hp300) || defined(amiga)
831 1.16 mycroft if (kp->kp_eproc.e_vm.vm_pmap.pm_stab) {
832 1.16 mycroft unsigned long ste;
833 1.16 mycroft
834 1.16 mycroft /* position at process segment table */
835 1.16 mycroft klseek (kmem,
836 1.16 mycroft (int) kp->kp_eproc.e_vm.vm_pmap.pm_stab
837 1.16 mycroft + btos(addr) * sizeof (struct ste), 0);
838 1.16 mycroft
839 1.16 mycroft if (read (kmem, (char *) &ste, sizeof (ste))
840 1.16 mycroft == sizeof (ste) && (ste & SG_V)) {
841 1.16 mycroft int p, pte;
842 1.16 mycroft
843 1.16 mycroft p = btop(addr & SG_PMASK);
844 1.16 mycroft memaddr = (ste & SG_FRAME) + (p * sizeof(struct pte));
845 1.16 mycroft (void) lseek(mem, memaddr, 0);
846 1.16 mycroft if (read(mem, (char *)&pte, sizeof pte) != sizeof pte) {
847 1.16 mycroft seterr("kvmprocread: cannot locate pte");
848 1.16 mycroft memaddr = 0;
849 1.16 mycroft }
850 1.16 mycroft else {
851 1.16 mycroft memaddr = pte & PG_FRAME;
852 1.16 mycroft if (pte == PG_NV
853 1.16 mycroft || memaddr < (off_t)ptob(lowram)) {
854 1.16 mycroft seterr("kvmprocread: page not valid");
855 1.16 mycroft memaddr = 0;
856 1.16 mycroft }
857 1.16 mycroft else
858 1.16 mycroft memaddr = (memaddr
859 1.16 mycroft - (off_t)ptob(lowram))
860 1.16 mycroft + (addr & PGOFSET);
861 1.16 mycroft }
862 1.14 cgd }
863 1.14 cgd }
864 1.8 cgd #endif
865 1.8 cgd #if defined(i386)
866 1.8 cgd if (kp->kp_eproc.e_vm.vm_pmap.pm_pdir) {
867 1.8 cgd struct pde pde;
868 1.8 cgd
869 1.8 cgd klseek(kmem,
870 1.8 cgd (long)(&kp->kp_eproc.e_vm.vm_pmap.pm_pdir[pdei(addr)]), 0);
871 1.8 cgd
872 1.8 cgd if (read(kmem, (char *)&pde, sizeof pde) == sizeof pde
873 1.8 cgd && pde.pd_v) {
874 1.8 cgd
875 1.8 cgd struct pte pte;
876 1.8 cgd
877 1.8 cgd if (lseek(mem, (long)ctob(pde.pd_pfnum) +
878 1.8 cgd (ptei(addr) * sizeof pte), 0) == -1)
879 1.8 cgd seterr("kvm_procread: lseek");
880 1.8 cgd if (read(mem, (char *)&pte, sizeof pte) == sizeof pte) {
881 1.8 cgd if (pte.pg_v) {
882 1.8 cgd memaddr = (long)ctob(pte.pg_pfnum) +
883 1.15 cgd (addr % (1 << CLSHIFT));
884 1.8 cgd }
885 1.8 cgd } else {
886 1.8 cgd seterr("kvm_procread: read");
887 1.8 cgd }
888 1.8 cgd }
889 1.8 cgd }
890 1.8 cgd #endif /* i386 */
891 1.8 cgd
892 1.8 cgd if (memaddr == 0 && vatosw(p, addr & ~CLOFSET, &memaddr, &swb)) {
893 1.8 cgd if (memaddr != 0) {
894 1.8 cgd memaddr += addr & CLOFSET;
895 1.8 cgd } else {
896 1.11 mycroft swaddr = swb.offset + (addr & CLOFSET);
897 1.8 cgd swb.size -= addr & CLOFSET;
898 1.8 cgd }
899 1.8 cgd }
900 1.8 cgd
901 1.8 cgd if (memaddr) {
902 1.8 cgd if (lseek(mem, memaddr, 0) == -1)
903 1.8 cgd seterr("kvm_getu: lseek");
904 1.11 mycroft real_len = read(mem, buf, real_len);
905 1.8 cgd if (real_len == -1) {
906 1.8 cgd seterr("kvm_procread: read");
907 1.11 mycroft return 0;
908 1.8 cgd }
909 1.8 cgd } else if (swaddr) {
910 1.11 mycroft char bouncebuf[CLBYTES];
911 1.11 mycroft unsigned len;
912 1.11 mycroft if (lseek(swap, swaddr & ~CLOFSET, 0) == -1) {
913 1.11 mycroft seterr("kvm_procread: lseek");
914 1.11 mycroft return 0;
915 1.11 mycroft }
916 1.11 mycroft len = read(swap, bouncebuf, CLBYTES);
917 1.11 mycroft if (len == -1 || len <= (swaddr & CLOFSET)) {
918 1.8 cgd seterr("kvm_procread: read");
919 1.11 mycroft return 0;
920 1.8 cgd }
921 1.11 mycroft len = MIN(len - (swaddr & CLOFSET), real_len);
922 1.11 mycroft memcpy(buf, &bouncebuf[swaddr & CLOFSET], len);
923 1.11 mycroft return len;
924 1.8 cgd } else
925 1.8 cgd real_len = 0;
926 1.8 cgd
927 1.8 cgd return real_len;
928 1.8 cgd }
929 1.8 cgd
930 1.8 cgd int
931 1.8 cgd kvm_procreadstr(p, addr, buf, len)
932 1.8 cgd const struct proc *p;
933 1.11 mycroft const unsigned addr;
934 1.11 mycroft char *buf;
935 1.8 cgd unsigned len;
936 1.8 cgd {
937 1.10 deraadt int done, little;
938 1.10 deraadt char copy[200], *pb;
939 1.11 mycroft char a;
940 1.8 cgd
941 1.8 cgd done = 0;
942 1.15 cgd copy[0] = '\0';
943 1.10 deraadt while (len) {
944 1.10 deraadt little = kvm_procread(p, addr+done, copy, MIN(len, sizeof copy));
945 1.10 deraadt if (little<1)
946 1.10 deraadt break;
947 1.10 deraadt pb = copy;
948 1.10 deraadt while (little--) {
949 1.10 deraadt len--;
950 1.11 mycroft if( (*buf++ = *pb++) == '\0' )
951 1.15 cgd return done;
952 1.15 cgd done++;
953 1.10 deraadt }
954 1.8 cgd }
955 1.8 cgd return done;
956 1.8 cgd }
957 1.8 cgd
958 1.1 cgd char *
959 1.1 cgd kvm_getargs(p, up)
960 1.1 cgd const struct proc *p;
961 1.1 cgd const struct user *up;
962 1.1 cgd {
963 1.8 cgd static char cmdbuf[ARG_MAX + sizeof(p->p_comm) + 5];
964 1.8 cgd register char *cp, *acp;
965 1.8 cgd int left, rv;
966 1.8 cgd struct ps_strings arginfo;
967 1.1 cgd
968 1.1 cgd if (up == NULL || p->p_pid == 0 || p->p_pid == 2)
969 1.1 cgd goto retucomm;
970 1.8 cgd
971 1.11 mycroft if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
972 1.8 cgd sizeof(arginfo))
973 1.8 cgd goto bad;
974 1.8 cgd
975 1.15 cgd cmdbuf[0] = '\0';
976 1.8 cgd cp = cmdbuf;
977 1.8 cgd acp = arginfo.ps_argvstr;
978 1.8 cgd left = ARG_MAX + 1;
979 1.8 cgd while (arginfo.ps_nargvstr--) {
980 1.8 cgd if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
981 1.8 cgd acp += rv + 1;
982 1.8 cgd left -= rv + 1;
983 1.8 cgd cp += rv;
984 1.8 cgd *cp++ = ' ';
985 1.8 cgd *cp = '\0';
986 1.1 cgd } else
987 1.1 cgd goto bad;
988 1.1 cgd }
989 1.8 cgd cp-- ; *cp = '\0';
990 1.2 cgd
991 1.8 cgd if (cmdbuf[0] == '-' || cmdbuf[0] == '?' || cmdbuf[0] <= ' ') {
992 1.1 cgd (void) strcat(cmdbuf, " (");
993 1.1 cgd (void) strncat(cmdbuf, p->p_comm, sizeof(p->p_comm));
994 1.1 cgd (void) strcat(cmdbuf, ")");
995 1.1 cgd }
996 1.1 cgd return (cmdbuf);
997 1.1 cgd
998 1.1 cgd bad:
999 1.8 cgd seterr("error locating command name for pid %d", p->p_pid);
1000 1.1 cgd retucomm:
1001 1.8 cgd (void) strcpy(cmdbuf, "(");
1002 1.1 cgd (void) strncat(cmdbuf, p->p_comm, sizeof (p->p_comm));
1003 1.1 cgd (void) strcat(cmdbuf, ")");
1004 1.1 cgd return (cmdbuf);
1005 1.1 cgd }
1006 1.1 cgd
1007 1.8 cgd char *
1008 1.8 cgd kvm_getenv(p, up)
1009 1.8 cgd const struct proc *p;
1010 1.8 cgd const struct user *up;
1011 1.8 cgd {
1012 1.8 cgd static char envbuf[ARG_MAX + 1];
1013 1.8 cgd register char *cp, *acp;
1014 1.8 cgd int left, rv;
1015 1.8 cgd struct ps_strings arginfo;
1016 1.8 cgd
1017 1.8 cgd if (up == NULL || p->p_pid == 0 || p->p_pid == 2)
1018 1.8 cgd goto retemptyenv;
1019 1.8 cgd
1020 1.11 mycroft if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
1021 1.8 cgd sizeof(arginfo))
1022 1.8 cgd goto bad;
1023 1.8 cgd
1024 1.8 cgd cp = envbuf;
1025 1.8 cgd acp = arginfo.ps_envstr;
1026 1.8 cgd left = ARG_MAX + 1;
1027 1.8 cgd while (arginfo.ps_nenvstr--) {
1028 1.8 cgd if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
1029 1.8 cgd acp += rv + 1;
1030 1.8 cgd left -= rv + 1;
1031 1.8 cgd cp += rv;
1032 1.8 cgd *cp++ = ' ';
1033 1.8 cgd *cp = '\0';
1034 1.8 cgd } else
1035 1.8 cgd goto bad;
1036 1.8 cgd }
1037 1.8 cgd cp-- ; *cp = '\0';
1038 1.8 cgd return (envbuf);
1039 1.8 cgd
1040 1.8 cgd bad:
1041 1.8 cgd seterr("error locating environment for pid %d", p->p_pid);
1042 1.8 cgd retemptyenv:
1043 1.8 cgd envbuf[0] = '\0';
1044 1.8 cgd return (envbuf);
1045 1.8 cgd }
1046 1.1 cgd
1047 1.1 cgd static
1048 1.1 cgd getkvars()
1049 1.1 cgd {
1050 1.1 cgd if (kvm_nlist(nl) == -1)
1051 1.1 cgd return (-1);
1052 1.1 cgd if (deadkernel) {
1053 1.1 cgd /* We must do the sys map first because klseek uses it */
1054 1.1 cgd long addr;
1055 1.1 cgd
1056 1.1 cgd #ifndef NEWVM
1057 1.1 cgd Syssize = nl[X_SYSSIZE].n_value;
1058 1.1 cgd Sysmap = (struct pte *)
1059 1.1 cgd calloc((unsigned) Syssize, sizeof (struct pte));
1060 1.1 cgd if (Sysmap == NULL) {
1061 1.1 cgd seterr("out of space for Sysmap");
1062 1.1 cgd return (-1);
1063 1.1 cgd }
1064 1.1 cgd addr = (long) nl[X_SYSMAP].n_value;
1065 1.1 cgd addr &= ~KERNBASE;
1066 1.1 cgd (void) lseek(kmem, addr, 0);
1067 1.1 cgd if (read(kmem, (char *) Sysmap, Syssize * sizeof (struct pte))
1068 1.1 cgd != Syssize * sizeof (struct pte)) {
1069 1.1 cgd seterr("can't read Sysmap");
1070 1.1 cgd return (-1);
1071 1.1 cgd }
1072 1.1 cgd #endif
1073 1.16 mycroft #if defined(hp300) || defined(amiga)
1074 1.1 cgd addr = (long) nl[X_LOWRAM].n_value;
1075 1.1 cgd (void) lseek(kmem, addr, 0);
1076 1.1 cgd if (read(kmem, (char *) &lowram, sizeof (lowram))
1077 1.1 cgd != sizeof (lowram)) {
1078 1.1 cgd seterr("can't read lowram");
1079 1.1 cgd return (-1);
1080 1.1 cgd }
1081 1.1 cgd lowram = btop(lowram);
1082 1.1 cgd Sysseg = (struct ste *) malloc(NBPG);
1083 1.1 cgd if (Sysseg == NULL) {
1084 1.1 cgd seterr("out of space for Sysseg");
1085 1.1 cgd return (-1);
1086 1.1 cgd }
1087 1.1 cgd addr = (long) nl[X_SYSSEG].n_value;
1088 1.1 cgd (void) lseek(kmem, addr, 0);
1089 1.1 cgd read(kmem, (char *)&addr, sizeof(addr));
1090 1.1 cgd (void) lseek(kmem, (long)addr, 0);
1091 1.1 cgd if (read(kmem, (char *) Sysseg, NBPG) != NBPG) {
1092 1.1 cgd seterr("can't read Sysseg");
1093 1.1 cgd return (-1);
1094 1.1 cgd }
1095 1.1 cgd #endif
1096 1.1 cgd #if defined(i386)
1097 1.1 cgd PTD = (struct pde *) malloc(NBPG);
1098 1.1 cgd if (PTD == NULL) {
1099 1.1 cgd seterr("out of space for PTD");
1100 1.1 cgd return (-1);
1101 1.1 cgd }
1102 1.1 cgd addr = (long) nl[X_IdlePTD].n_value;
1103 1.1 cgd (void) lseek(kmem, addr, 0);
1104 1.1 cgd read(kmem, (char *)&addr, sizeof(addr));
1105 1.1 cgd (void) lseek(kmem, (long)addr, 0);
1106 1.1 cgd if (read(kmem, (char *) PTD, NBPG) != NBPG) {
1107 1.1 cgd seterr("can't read PTD");
1108 1.1 cgd return (-1);
1109 1.1 cgd }
1110 1.1 cgd #endif
1111 1.1 cgd }
1112 1.1 cgd #ifndef NEWVM
1113 1.1 cgd usrpt = (struct pte *)nl[X_USRPT].n_value;
1114 1.1 cgd Usrptmap = (struct pte *)nl[X_USRPTMAP].n_value;
1115 1.1 cgd #endif
1116 1.1 cgd if (kvm_read((void *) nl[X_NSWAP].n_value, &nswap, sizeof (long)) !=
1117 1.1 cgd sizeof (long)) {
1118 1.1 cgd seterr("can't read nswap");
1119 1.1 cgd return (-1);
1120 1.1 cgd }
1121 1.1 cgd if (kvm_read((void *) nl[X_DMMIN].n_value, &dmmin, sizeof (long)) !=
1122 1.1 cgd sizeof (long)) {
1123 1.1 cgd seterr("can't read dmmin");
1124 1.1 cgd return (-1);
1125 1.1 cgd }
1126 1.1 cgd if (kvm_read((void *) nl[X_DMMAX].n_value, &dmmax, sizeof (long)) !=
1127 1.1 cgd sizeof (long)) {
1128 1.1 cgd seterr("can't read dmmax");
1129 1.1 cgd return (-1);
1130 1.1 cgd }
1131 1.1 cgd return (0);
1132 1.1 cgd }
1133 1.1 cgd
1134 1.1 cgd kvm_read(loc, buf, len)
1135 1.1 cgd void *loc;
1136 1.1 cgd void *buf;
1137 1.1 cgd {
1138 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
1139 1.1 cgd return (-1);
1140 1.1 cgd if (iskva(loc)) {
1141 1.1 cgd klseek(kmem, (off_t) loc, 0);
1142 1.1 cgd if (read(kmem, buf, len) != len) {
1143 1.1 cgd seterr("error reading kmem at %x", loc);
1144 1.1 cgd return (-1);
1145 1.1 cgd }
1146 1.1 cgd } else {
1147 1.1 cgd lseek(mem, (off_t) loc, 0);
1148 1.1 cgd if (read(mem, buf, len) != len) {
1149 1.1 cgd seterr("error reading mem at %x", loc);
1150 1.1 cgd return (-1);
1151 1.1 cgd }
1152 1.1 cgd }
1153 1.1 cgd return (len);
1154 1.1 cgd }
1155 1.1 cgd
1156 1.1 cgd static void
1157 1.1 cgd klseek(fd, loc, off)
1158 1.1 cgd int fd;
1159 1.1 cgd off_t loc;
1160 1.1 cgd int off;
1161 1.1 cgd {
1162 1.1 cgd
1163 1.1 cgd if (deadkernel) {
1164 1.1 cgd if ((loc = Vtophys(loc)) == -1)
1165 1.1 cgd return;
1166 1.1 cgd }
1167 1.1 cgd (void) lseek(fd, (off_t)loc, off);
1168 1.1 cgd }
1169 1.1 cgd
1170 1.1 cgd #ifndef NEWVM
1171 1.1 cgd /*
1172 1.1 cgd * Given a base/size pair in virtual swap area,
1173 1.1 cgd * return a physical base/size pair which is the
1174 1.1 cgd * (largest) initial, physically contiguous block.
1175 1.1 cgd */
1176 1.1 cgd static void
1177 1.1 cgd vstodb(vsbase, vssize, dmp, dbp, rev)
1178 1.1 cgd register int vsbase;
1179 1.1 cgd int vssize;
1180 1.1 cgd struct dmap *dmp;
1181 1.1 cgd register struct dblock *dbp;
1182 1.1 cgd {
1183 1.1 cgd register int blk = dmmin;
1184 1.1 cgd register swblk_t *ip = dmp->dm_map;
1185 1.1 cgd
1186 1.1 cgd vsbase = ctod(vsbase);
1187 1.1 cgd vssize = ctod(vssize);
1188 1.1 cgd if (vsbase < 0 || vsbase + vssize > dmp->dm_size)
1189 1.1 cgd /*panic("vstodb")*/;
1190 1.1 cgd while (vsbase >= blk) {
1191 1.1 cgd vsbase -= blk;
1192 1.1 cgd if (blk < dmmax)
1193 1.1 cgd blk *= 2;
1194 1.1 cgd ip++;
1195 1.1 cgd }
1196 1.1 cgd if (*ip <= 0 || *ip + blk > nswap)
1197 1.1 cgd /*panic("vstodb")*/;
1198 1.1 cgd dbp->db_size = MIN(vssize, blk - vsbase);
1199 1.1 cgd dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
1200 1.1 cgd }
1201 1.1 cgd #endif
1202 1.1 cgd
1203 1.1 cgd #ifdef NEWVM
1204 1.1 cgd static off_t
1205 1.1 cgd Vtophys(loc)
1206 1.1 cgd u_long loc;
1207 1.1 cgd {
1208 1.1 cgd off_t newloc = (off_t) -1;
1209 1.16 mycroft #if defined(hp300) || defined(amiga)
1210 1.1 cgd int p, ste, pte;
1211 1.1 cgd
1212 1.16 mycroft ste = *(int *)&Sysseg[btos(loc)];
1213 1.1 cgd if ((ste & SG_V) == 0) {
1214 1.1 cgd seterr("vtophys: segment not valid");
1215 1.1 cgd return((off_t) -1);
1216 1.1 cgd }
1217 1.1 cgd p = btop(loc & SG_PMASK);
1218 1.1 cgd newloc = (ste & SG_FRAME) + (p * sizeof(struct pte));
1219 1.16 mycroft (void) lseek(mem, newloc, 0);
1220 1.16 mycroft if (read(mem, (char *)&pte, sizeof pte) != sizeof pte) {
1221 1.1 cgd seterr("vtophys: cannot locate pte");
1222 1.1 cgd return((off_t) -1);
1223 1.1 cgd }
1224 1.1 cgd newloc = pte & PG_FRAME;
1225 1.1 cgd if (pte == PG_NV || newloc < (off_t)ptob(lowram)) {
1226 1.1 cgd seterr("vtophys: page not valid");
1227 1.1 cgd return((off_t) -1);
1228 1.1 cgd }
1229 1.1 cgd newloc = (newloc - (off_t)ptob(lowram)) + (loc & PGOFSET);
1230 1.1 cgd #endif
1231 1.1 cgd #ifdef i386
1232 1.1 cgd struct pde pde;
1233 1.1 cgd struct pte pte;
1234 1.1 cgd int p;
1235 1.1 cgd
1236 1.1 cgd pde = PTD[loc >> PD_SHIFT];
1237 1.1 cgd if (pde.pd_v == 0) {
1238 1.1 cgd seterr("vtophys: page directory entry not valid");
1239 1.1 cgd return((off_t) -1);
1240 1.1 cgd }
1241 1.1 cgd p = btop(loc & PT_MASK);
1242 1.1 cgd newloc = pde.pd_pfnum + (p * sizeof(struct pte));
1243 1.1 cgd (void) lseek(kmem, (long)newloc, 0);
1244 1.1 cgd if (read(kmem, (char *)&pte, sizeof pte) != sizeof pte) {
1245 1.1 cgd seterr("vtophys: cannot obtain desired pte");
1246 1.1 cgd return((off_t) -1);
1247 1.1 cgd }
1248 1.1 cgd newloc = pte.pg_pfnum;
1249 1.1 cgd if (pte.pg_v == 0) {
1250 1.1 cgd seterr("vtophys: page table entry not valid");
1251 1.1 cgd return((off_t) -1);
1252 1.1 cgd }
1253 1.1 cgd newloc += (loc & PGOFSET);
1254 1.1 cgd #endif
1255 1.1 cgd return((off_t) newloc);
1256 1.1 cgd }
1257 1.1 cgd #else
1258 1.1 cgd static off_t
1259 1.1 cgd vtophys(loc)
1260 1.1 cgd long loc;
1261 1.1 cgd {
1262 1.1 cgd int p;
1263 1.1 cgd off_t newloc;
1264 1.1 cgd register struct pte *pte;
1265 1.1 cgd
1266 1.1 cgd newloc = loc & ~KERNBASE;
1267 1.1 cgd p = btop(newloc);
1268 1.1 cgd #if defined(vax) || defined(tahoe)
1269 1.1 cgd if ((loc & KERNBASE) == 0) {
1270 1.1 cgd seterr("vtophys: translating non-kernel address");
1271 1.1 cgd return((off_t) -1);
1272 1.1 cgd }
1273 1.1 cgd #endif
1274 1.1 cgd if (p >= Syssize) {
1275 1.1 cgd seterr("vtophys: page out of bound (%d>=%d)", p, Syssize);
1276 1.1 cgd return((off_t) -1);
1277 1.1 cgd }
1278 1.1 cgd pte = &Sysmap[p];
1279 1.1 cgd if (pte->pg_v == 0 && (pte->pg_fod || pte->pg_pfnum == 0)) {
1280 1.1 cgd seterr("vtophys: page not valid");
1281 1.1 cgd return((off_t) -1);
1282 1.1 cgd }
1283 1.16 mycroft #if defined(hp300) || defined(amiga)
1284 1.1 cgd if (pte->pg_pfnum < lowram) {
1285 1.1 cgd seterr("vtophys: non-RAM page (%d<%d)", pte->pg_pfnum, lowram);
1286 1.1 cgd return((off_t) -1);
1287 1.1 cgd }
1288 1.1 cgd #endif
1289 1.1 cgd loc = (long) (ptob(pftoc(pte->pg_pfnum)) + (loc & PGOFSET));
1290 1.1 cgd return(loc);
1291 1.1 cgd }
1292 1.1 cgd #endif
1293 1.1 cgd
1294 1.3 cgd
1295 1.3 cgd #ifdef NEWVM
1296 1.3 cgd /*
1297 1.3 cgd * locate address of unwired or swapped page
1298 1.3 cgd */
1299 1.3 cgd
1300 1.3 cgd #define DEBUG 0
1301 1.3 cgd
1302 1.3 cgd #define KREAD(off, addr, len) \
1303 1.3 cgd (kvm_read((void *)(off), (char *)(addr), (len)) == (len))
1304 1.3 cgd
1305 1.3 cgd
1306 1.3 cgd static int
1307 1.3 cgd vatosw(p, vaddr, maddr, swb)
1308 1.3 cgd struct proc *p ;
1309 1.3 cgd vm_offset_t vaddr;
1310 1.3 cgd vm_offset_t *maddr;
1311 1.3 cgd struct swapblk *swb;
1312 1.3 cgd {
1313 1.3 cgd register struct kinfo_proc *kp = (struct kinfo_proc *)p;
1314 1.3 cgd vm_map_t mp = &kp->kp_eproc.e_vm.vm_map;
1315 1.3 cgd struct vm_object vm_object;
1316 1.3 cgd struct vm_map_entry vm_entry;
1317 1.3 cgd struct pager_struct pager;
1318 1.3 cgd struct swpager swpager;
1319 1.3 cgd struct swblock swblock;
1320 1.3 cgd long addr, off;
1321 1.3 cgd int i;
1322 1.3 cgd
1323 1.3 cgd if (p->p_pid == 0 || p->p_pid == 2)
1324 1.3 cgd return 0;
1325 1.3 cgd
1326 1.3 cgd addr = (long)mp->header.next;
1327 1.3 cgd for (i = 0; i < mp->nentries; i++) {
1328 1.3 cgd /* Weed through map entries until vaddr in range */
1329 1.3 cgd if (!KREAD(addr, &vm_entry, sizeof(vm_entry))) {
1330 1.3 cgd setsyserr("vatosw: read vm_map_entry");
1331 1.3 cgd return 0;
1332 1.3 cgd }
1333 1.3 cgd if ((vaddr >= vm_entry.start) && (vaddr <= vm_entry.end) &&
1334 1.3 cgd (vm_entry.object.vm_object != 0))
1335 1.3 cgd break;
1336 1.3 cgd
1337 1.3 cgd addr = (long)vm_entry.next;
1338 1.3 cgd }
1339 1.3 cgd if (i == mp->nentries) {
1340 1.3 cgd seterr("%u: map not found\n", p->p_pid);
1341 1.3 cgd return 0;
1342 1.3 cgd }
1343 1.3 cgd
1344 1.3 cgd if (vm_entry.is_a_map || vm_entry.is_sub_map) {
1345 1.3 cgd seterr("%u: Is a map\n", p->p_pid);
1346 1.3 cgd return 0;
1347 1.3 cgd }
1348 1.3 cgd
1349 1.3 cgd /* Locate memory object */
1350 1.3 cgd off = (vaddr - vm_entry.start) + vm_entry.offset;
1351 1.3 cgd addr = (long)vm_entry.object.vm_object;
1352 1.3 cgd while (1) {
1353 1.3 cgd if (!KREAD(addr, &vm_object, sizeof vm_object)) {
1354 1.3 cgd setsyserr("vatosw: read vm_object");
1355 1.3 cgd return 0;
1356 1.3 cgd }
1357 1.3 cgd
1358 1.3 cgd #if DEBUG
1359 1.3 cgd fprintf(stderr, "%u: find page: object %#x offset %x\n",
1360 1.3 cgd p->p_pid, addr, off);
1361 1.3 cgd #endif
1362 1.3 cgd
1363 1.3 cgd /* Lookup in page queue */
1364 1.3 cgd if (findpage(addr, off, maddr))
1365 1.3 cgd return 1;
1366 1.3 cgd
1367 1.3 cgd if (vm_object.shadow == 0)
1368 1.3 cgd break;
1369 1.3 cgd
1370 1.3 cgd #if DEBUG
1371 1.3 cgd fprintf(stderr, "%u: shadow obj at %x: offset %x+%x\n",
1372 1.3 cgd p->p_pid, addr, off, vm_object.shadow_offset);
1373 1.3 cgd #endif
1374 1.3 cgd
1375 1.3 cgd addr = (long)vm_object.shadow;
1376 1.3 cgd off += vm_object.shadow_offset;
1377 1.3 cgd }
1378 1.3 cgd
1379 1.3 cgd if (!vm_object.pager) {
1380 1.3 cgd seterr("%u: no pager\n", p->p_pid);
1381 1.3 cgd return 0;
1382 1.3 cgd }
1383 1.3 cgd
1384 1.3 cgd /* Find address in swap space */
1385 1.3 cgd if (!KREAD(vm_object.pager, &pager, sizeof pager)) {
1386 1.3 cgd setsyserr("vatosw: read pager");
1387 1.3 cgd return 0;
1388 1.3 cgd }
1389 1.3 cgd if (pager.pg_type != PG_SWAP) {
1390 1.3 cgd seterr("%u: weird pager\n", p->p_pid);
1391 1.3 cgd return 0;
1392 1.3 cgd }
1393 1.3 cgd
1394 1.3 cgd /* Get swap pager data */
1395 1.3 cgd if (!KREAD(pager.pg_data, &swpager, sizeof swpager)) {
1396 1.3 cgd setsyserr("vatosw: read swpager");
1397 1.3 cgd return 0;
1398 1.3 cgd }
1399 1.3 cgd
1400 1.3 cgd off += vm_object.paging_offset;
1401 1.3 cgd
1402 1.3 cgd /* Read swap block array */
1403 1.3 cgd if (!KREAD((long)swpager.sw_blocks +
1404 1.3 cgd (off/dbtob(swpager.sw_bsize)) * sizeof swblock,
1405 1.3 cgd &swblock, sizeof swblock)) {
1406 1.3 cgd setsyserr("vatosw: read swblock");
1407 1.3 cgd return 0;
1408 1.3 cgd }
1409 1.3 cgd swb->offset = dbtob(swblock.swb_block)+ (off % dbtob(swpager.sw_bsize));
1410 1.3 cgd swb->size = dbtob(swpager.sw_bsize) - (off % dbtob(swpager.sw_bsize));
1411 1.3 cgd return 1;
1412 1.3 cgd }
1413 1.3 cgd
1414 1.3 cgd
1415 1.3 cgd #define atop(x) (((unsigned)(x)) >> page_shift)
1416 1.3 cgd #define vm_page_hash(object, offset) \
1417 1.3 cgd (((unsigned)object+(unsigned)atop(offset))&vm_page_hash_mask)
1418 1.3 cgd
1419 1.3 cgd static int
1420 1.3 cgd findpage(object, offset, maddr)
1421 1.3 cgd long object;
1422 1.3 cgd long offset;
1423 1.3 cgd vm_offset_t *maddr;
1424 1.3 cgd {
1425 1.3 cgd static long vm_page_hash_mask;
1426 1.3 cgd static long vm_page_buckets;
1427 1.3 cgd static long page_shift;
1428 1.3 cgd queue_head_t bucket;
1429 1.3 cgd struct vm_page mem;
1430 1.3 cgd long addr, baddr;
1431 1.3 cgd
1432 1.3 cgd if (vm_page_hash_mask == 0 && !KREAD(nl[X_VM_PAGE_HASH_MASK].n_value,
1433 1.3 cgd &vm_page_hash_mask, sizeof (long))) {
1434 1.3 cgd seterr("can't read vm_page_hash_mask");
1435 1.3 cgd return 0;
1436 1.3 cgd }
1437 1.3 cgd if (page_shift == 0 && !KREAD(nl[X_PAGE_SHIFT].n_value,
1438 1.3 cgd &page_shift, sizeof (long))) {
1439 1.3 cgd seterr("can't read page_shift");
1440 1.3 cgd return 0;
1441 1.3 cgd }
1442 1.3 cgd if (vm_page_buckets == 0 && !KREAD(nl[X_VM_PAGE_BUCKETS].n_value,
1443 1.3 cgd &vm_page_buckets, sizeof (long))) {
1444 1.3 cgd seterr("can't read vm_page_buckets");
1445 1.3 cgd return 0;
1446 1.3 cgd }
1447 1.3 cgd
1448 1.3 cgd baddr = vm_page_buckets + vm_page_hash(object,offset) * sizeof(queue_head_t);
1449 1.3 cgd if (!KREAD(baddr, &bucket, sizeof (bucket))) {
1450 1.3 cgd seterr("can't read vm_page_bucket");
1451 1.3 cgd return 0;
1452 1.3 cgd }
1453 1.3 cgd
1454 1.3 cgd addr = (long)bucket.next;
1455 1.3 cgd while (addr != baddr) {
1456 1.3 cgd if (!KREAD(addr, &mem, sizeof (mem))) {
1457 1.3 cgd seterr("can't read vm_page");
1458 1.3 cgd return 0;
1459 1.3 cgd }
1460 1.3 cgd if ((long)mem.object == object && mem.offset == offset) {
1461 1.3 cgd *maddr = (long)mem.phys_addr;
1462 1.3 cgd return 1;
1463 1.3 cgd }
1464 1.3 cgd addr = (long)mem.hashq.next;
1465 1.3 cgd }
1466 1.3 cgd return 0;
1467 1.3 cgd }
1468 1.3 cgd #endif /* NEWVM */
1469 1.3 cgd
1470 1.1 cgd #include <varargs.h>
1471 1.1 cgd static char errbuf[_POSIX2_LINE_MAX];
1472 1.1 cgd
1473 1.1 cgd static void
1474 1.1 cgd seterr(va_alist)
1475 1.1 cgd va_dcl
1476 1.1 cgd {
1477 1.1 cgd char *fmt;
1478 1.1 cgd va_list ap;
1479 1.1 cgd
1480 1.1 cgd va_start(ap);
1481 1.1 cgd fmt = va_arg(ap, char *);
1482 1.1 cgd (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
1483 1.3 cgd #if DEBUG
1484 1.3 cgd (void) vfprintf(stderr, fmt, ap);
1485 1.3 cgd #endif
1486 1.1 cgd va_end(ap);
1487 1.1 cgd }
1488 1.1 cgd
1489 1.1 cgd static void
1490 1.1 cgd setsyserr(va_alist)
1491 1.1 cgd va_dcl
1492 1.1 cgd {
1493 1.1 cgd char *fmt, *cp;
1494 1.1 cgd va_list ap;
1495 1.1 cgd extern int errno;
1496 1.1 cgd
1497 1.1 cgd va_start(ap);
1498 1.1 cgd fmt = va_arg(ap, char *);
1499 1.1 cgd (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
1500 1.1 cgd for (cp=errbuf; *cp; cp++)
1501 1.1 cgd ;
1502 1.1 cgd snprintf(cp, _POSIX2_LINE_MAX - (cp - errbuf), ": %s", strerror(errno));
1503 1.1 cgd va_end(ap);
1504 1.1 cgd }
1505 1.1 cgd
1506 1.1 cgd char *
1507 1.1 cgd kvm_geterr()
1508 1.1 cgd {
1509 1.1 cgd return (errbuf);
1510 1.1 cgd }
1511