kvm.c revision 1.26 1 1.1 cgd /*-
2 1.24 mycroft * Copyright (c) 1994 Charles Hannum.
3 1.9 cgd * Copyright (c) 1993 Christopher G. Demetriou
4 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
37 1.12 mycroft /*static char sccsid[] = "from: @(#)kvm.c 5.18 (Berkeley) 5/7/91";*/
38 1.26 pk static char rcsid[] = "$Id: kvm.c,v 1.26 1994/02/14 13:46:01 pk Exp $";
39 1.1 cgd #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.1 cgd #include <sys/param.h>
42 1.1 cgd #include <sys/user.h>
43 1.1 cgd #include <sys/proc.h>
44 1.1 cgd #include <sys/ioctl.h>
45 1.1 cgd #include <sys/kinfo.h>
46 1.1 cgd #include <sys/tty.h>
47 1.8 cgd #include <sys/exec.h>
48 1.1 cgd #include <machine/vmparam.h>
49 1.1 cgd #include <fcntl.h>
50 1.1 cgd #include <nlist.h>
51 1.1 cgd #include <kvm.h>
52 1.1 cgd #include <ndbm.h>
53 1.1 cgd #include <limits.h>
54 1.1 cgd #include <paths.h>
55 1.1 cgd #include <stdio.h>
56 1.1 cgd #include <string.h>
57 1.1 cgd
58 1.1 cgd #define btop(x) (((unsigned)(x)) >> PGSHIFT) /* XXX */
59 1.1 cgd #define ptob(x) ((caddr_t)((x) << PGSHIFT)) /* XXX */
60 1.1 cgd #include <vm/vm.h> /* ??? kinfo_proc currently includes this*/
61 1.3 cgd #include <vm/vm_page.h>
62 1.3 cgd #include <vm/swap_pager.h>
63 1.1 cgd #include <sys/kinfo_proc.h>
64 1.21 cgd #if defined(m68k)
65 1.13 mycroft #include <machine/pte.h>
66 1.16 mycroft #define btos(x) (((unsigned)(x)) >> SEGSHIFT) /* XXX */
67 1.1 cgd #endif
68 1.1 cgd
69 1.1 cgd /*
70 1.1 cgd * files
71 1.1 cgd */
72 1.1 cgd static const char *unixf, *memf, *kmemf, *swapf;
73 1.1 cgd static int unixx, mem, kmem, swap;
74 1.1 cgd static DBM *db;
75 1.1 cgd /*
76 1.1 cgd * flags
77 1.1 cgd */
78 1.1 cgd static int deadkernel;
79 1.1 cgd static int kvminit = 0;
80 1.1 cgd static int kvmfilesopen = 0;
81 1.1 cgd /*
82 1.1 cgd * state
83 1.1 cgd */
84 1.1 cgd static struct kinfo_proc *kvmprocbase, *kvmprocptr;
85 1.1 cgd static int kvmnprocs;
86 1.1 cgd /*
87 1.1 cgd * u. buffer
88 1.1 cgd */
89 1.1 cgd static union {
90 1.1 cgd struct user user;
91 1.1 cgd char upages[UPAGES][NBPG];
92 1.1 cgd } user;
93 1.3 cgd
94 1.3 cgd struct swapblk {
95 1.3 cgd long offset; /* offset in swap device */
96 1.3 cgd long size; /* remaining size of block in swap device */
97 1.3 cgd };
98 1.19 mycroft
99 1.1 cgd /*
100 1.1 cgd * random other stuff
101 1.1 cgd */
102 1.1 cgd static int dmmin, dmmax;
103 1.1 cgd static int pcbpf;
104 1.1 cgd static int nswap;
105 1.19 mycroft static long vm_page_hash_mask;
106 1.19 mycroft static long vm_page_buckets;
107 1.19 mycroft static long page_shift;
108 1.1 cgd static char *tmp;
109 1.21 cgd #if defined(m68k)
110 1.25 chopps #if defined(amiga)
111 1.25 chopps static int cpu040;
112 1.25 chopps #endif
113 1.1 cgd static int lowram;
114 1.1 cgd static struct ste *Sysseg;
115 1.1 cgd #endif
116 1.1 cgd #if defined(i386)
117 1.1 cgd static struct pde *PTD;
118 1.1 cgd #endif
119 1.1 cgd
120 1.19 mycroft #define atop(x) (((unsigned)(x)) >> page_shift)
121 1.19 mycroft #define vm_page_hash(object, offset) \
122 1.19 mycroft (((unsigned)object+(unsigned)atop(offset))&vm_page_hash_mask)
123 1.19 mycroft
124 1.1 cgd #define basename(cp) ((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
125 1.1 cgd #define MAXSYMSIZE 256
126 1.1 cgd
127 1.1 cgd static struct nlist nl[] = {
128 1.1 cgd { "_Usrptmap" },
129 1.1 cgd #define X_USRPTMAP 0
130 1.1 cgd { "_usrpt" },
131 1.1 cgd #define X_USRPT 1
132 1.1 cgd { "_nswap" },
133 1.1 cgd #define X_NSWAP 2
134 1.1 cgd { "_dmmin" },
135 1.1 cgd #define X_DMMIN 3
136 1.1 cgd { "_dmmax" },
137 1.1 cgd #define X_DMMAX 4
138 1.3 cgd { "_vm_page_buckets" },
139 1.3 cgd #define X_VM_PAGE_BUCKETS 5
140 1.3 cgd { "_vm_page_hash_mask" },
141 1.3 cgd #define X_VM_PAGE_HASH_MASK 6
142 1.3 cgd { "_page_shift" },
143 1.3 cgd #define X_PAGE_SHIFT 7
144 1.26 pk
145 1.26 pk #if defined(m68k)
146 1.26 pk #define X_DEADKERNEL 8
147 1.26 pk #endif
148 1.26 pk
149 1.26 pk #if defined(i386)
150 1.26 pk #define X_DEADKERNEL 8
151 1.26 pk #endif
152 1.26 pk
153 1.26 pk #if defined(sparc)
154 1.26 pk { "_pmap_dtos" },
155 1.26 pk #define X_PMAP_DTOS 8
156 1.26 pk #define X_DEADKERNEL 9
157 1.26 pk #endif
158 1.26 pk
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.26 pk #define X_SYSMAP (X_DEADKERNEL + 0)
164 1.1 cgd { "_Syssize" },
165 1.26 pk #define X_SYSSIZE (X_DEADKERNEL + 1)
166 1.1 cgd { "_allproc" },
167 1.26 pk #define X_ALLPROC (X_DEADKERNEL + 2)
168 1.1 cgd { "_zombproc" },
169 1.26 pk #define X_ZOMBPROC (X_DEADKERNEL + 3)
170 1.1 cgd { "_nproc" },
171 1.26 pk #define X_NPROC (X_DEADKERNEL + 4)
172 1.26 pk #define X_LAST (X_DEADKERNEL + 5)
173 1.26 pk
174 1.21 cgd #if defined(m68k)
175 1.1 cgd { "_Sysseg" },
176 1.26 pk #define X_SYSSEG (X_LAST+0)
177 1.1 cgd { "_lowram" },
178 1.26 pk #define X_LOWRAM (X_LAST+1)
179 1.25 chopps #if defined(amiga)
180 1.25 chopps { "_cpu040" },
181 1.26 pk #define X_CPU040 (X_LAST+2)
182 1.25 chopps #endif
183 1.1 cgd #endif
184 1.26 pk
185 1.1 cgd #if defined(i386)
186 1.1 cgd { "_IdlePTD" },
187 1.26 pk #define X_IdlePTD (X_LAST+0)
188 1.1 cgd #endif
189 1.26 pk
190 1.1 cgd { "" },
191 1.1 cgd };
192 1.1 cgd
193 1.1 cgd static off_t Vtophys();
194 1.1 cgd static void klseek(), seterr(), setsyserr(), vstodb();
195 1.1 cgd static int getkvars(), kvm_doprocs(), kvm_init();
196 1.3 cgd static int vatosw();
197 1.19 mycroft static int pager_get();
198 1.3 cgd static int findpage();
199 1.26 pk #if defined(sparc)
200 1.26 pk static vm_offset_t phys2realphys();
201 1.26 pk #endif
202 1.1 cgd
203 1.1 cgd /*
204 1.1 cgd * returns 0 if files were opened now,
205 1.1 cgd * 1 if files were already opened,
206 1.1 cgd * -1 if files could not be opened.
207 1.1 cgd */
208 1.1 cgd kvm_openfiles(uf, mf, sf)
209 1.1 cgd const char *uf, *mf, *sf;
210 1.1 cgd {
211 1.1 cgd if (kvmfilesopen)
212 1.1 cgd return (1);
213 1.1 cgd unixx = mem = kmem = swap = -1;
214 1.1 cgd unixf = (uf == NULL) ? _PATH_UNIX : uf;
215 1.1 cgd memf = (mf == NULL) ? _PATH_MEM : mf;
216 1.1 cgd
217 1.1 cgd if ((unixx = open(unixf, O_RDONLY, 0)) == -1) {
218 1.1 cgd setsyserr("can't open %s", unixf);
219 1.1 cgd goto failed;
220 1.1 cgd }
221 1.1 cgd if ((mem = open(memf, O_RDONLY, 0)) == -1) {
222 1.1 cgd setsyserr("can't open %s", memf);
223 1.1 cgd goto failed;
224 1.1 cgd }
225 1.1 cgd if (sf != NULL)
226 1.1 cgd swapf = sf;
227 1.1 cgd if (mf != NULL) {
228 1.1 cgd deadkernel++;
229 1.1 cgd kmemf = mf;
230 1.1 cgd kmem = mem;
231 1.1 cgd swap = -1;
232 1.1 cgd } else {
233 1.1 cgd kmemf = _PATH_KMEM;
234 1.1 cgd if ((kmem = open(kmemf, O_RDONLY, 0)) == -1) {
235 1.1 cgd setsyserr("can't open %s", kmemf);
236 1.1 cgd goto failed;
237 1.1 cgd }
238 1.1 cgd swapf = (sf == NULL) ? _PATH_DRUM : sf;
239 1.1 cgd /*
240 1.1 cgd * live kernel - avoid looking up nlist entries
241 1.1 cgd * past X_DEADKERNEL.
242 1.1 cgd */
243 1.1 cgd nl[X_DEADKERNEL].n_name = "";
244 1.1 cgd }
245 1.1 cgd if (swapf != NULL && ((swap = open(swapf, O_RDONLY, 0)) == -1)) {
246 1.1 cgd seterr("can't open %s", swapf);
247 1.1 cgd goto failed;
248 1.1 cgd }
249 1.1 cgd kvmfilesopen++;
250 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1) /*XXX*/
251 1.1 cgd return (-1);
252 1.1 cgd return (0);
253 1.1 cgd failed:
254 1.1 cgd kvm_close();
255 1.1 cgd return (-1);
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd static
259 1.1 cgd kvm_init(uf, mf, sf)
260 1.1 cgd char *uf, *mf, *sf;
261 1.1 cgd {
262 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
263 1.1 cgd return (-1);
264 1.1 cgd if (getkvars() == -1)
265 1.1 cgd return (-1);
266 1.1 cgd kvminit = 1;
267 1.1 cgd
268 1.1 cgd return (0);
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd kvm_close()
272 1.1 cgd {
273 1.1 cgd if (unixx != -1) {
274 1.1 cgd close(unixx);
275 1.1 cgd unixx = -1;
276 1.1 cgd }
277 1.1 cgd if (kmem != -1) {
278 1.1 cgd if (kmem != mem)
279 1.1 cgd close(kmem);
280 1.1 cgd /* otherwise kmem is a copy of mem, and will be closed below */
281 1.1 cgd kmem = -1;
282 1.1 cgd }
283 1.1 cgd if (mem != -1) {
284 1.1 cgd close(mem);
285 1.1 cgd mem = -1;
286 1.1 cgd }
287 1.1 cgd if (swap != -1) {
288 1.1 cgd close(swap);
289 1.1 cgd swap = -1;
290 1.1 cgd }
291 1.1 cgd if (db != NULL) {
292 1.1 cgd dbm_close(db);
293 1.1 cgd db = NULL;
294 1.1 cgd }
295 1.1 cgd kvminit = 0;
296 1.1 cgd kvmfilesopen = 0;
297 1.1 cgd deadkernel = 0;
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.19 mycroft sizeof (int)) == -1) {
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
484 1.1 cgd /* allproc */
485 1.1 cgd if (kvm_read((void *) nl[X_ALLPROC].n_value, &p,
486 1.19 mycroft sizeof (struct proc *)) == -1) {
487 1.1 cgd seterr("can't read allproc");
488 1.1 cgd return (-1);
489 1.1 cgd }
490 1.1 cgd
491 1.1 cgd again:
492 1.1 cgd for (; p; p = proc.p_nxt) {
493 1.19 mycroft if (kvm_read(p, &proc, sizeof (struct proc)) == -1) {
494 1.1 cgd seterr("can't read proc at %x", p);
495 1.1 cgd return (-1);
496 1.1 cgd }
497 1.1 cgd if (kvm_read(proc.p_cred, &eproc.e_pcred,
498 1.19 mycroft sizeof (struct pcred)) != -1)
499 1.1 cgd (void) kvm_read(eproc.e_pcred.pc_ucred, &eproc.e_ucred,
500 1.1 cgd sizeof (struct ucred));
501 1.19 mycroft
502 1.1 cgd switch(ki_op(what)) {
503 1.1 cgd
504 1.1 cgd case KINFO_PROC_PID:
505 1.1 cgd if (proc.p_pid != (pid_t)arg)
506 1.1 cgd continue;
507 1.1 cgd break;
508 1.1 cgd
509 1.1 cgd
510 1.1 cgd case KINFO_PROC_UID:
511 1.1 cgd if (eproc.e_ucred.cr_uid != (uid_t)arg)
512 1.1 cgd continue;
513 1.1 cgd break;
514 1.1 cgd
515 1.1 cgd case KINFO_PROC_RUID:
516 1.1 cgd if (eproc.e_pcred.p_ruid != (uid_t)arg)
517 1.1 cgd continue;
518 1.1 cgd break;
519 1.1 cgd }
520 1.1 cgd /*
521 1.1 cgd * gather eproc
522 1.1 cgd */
523 1.1 cgd eproc.e_paddr = p;
524 1.19 mycroft if (kvm_read(proc.p_pgrp, &pgrp, sizeof (struct pgrp)) == -1) {
525 1.1 cgd seterr("can't read pgrp at %x", proc.p_pgrp);
526 1.1 cgd return (-1);
527 1.1 cgd }
528 1.1 cgd eproc.e_sess = pgrp.pg_session;
529 1.1 cgd eproc.e_pgid = pgrp.pg_id;
530 1.1 cgd eproc.e_jobc = pgrp.pg_jobc;
531 1.1 cgd if (kvm_read(pgrp.pg_session, &sess, sizeof (struct session))
532 1.19 mycroft == -1) {
533 1.1 cgd seterr("can't read session at %x", pgrp.pg_session);
534 1.1 cgd return (-1);
535 1.1 cgd }
536 1.1 cgd if ((proc.p_flag&SCTTY) && sess.s_ttyp != NULL) {
537 1.1 cgd if (kvm_read(sess.s_ttyp, &tty, sizeof (struct tty))
538 1.19 mycroft == -1) {
539 1.1 cgd seterr("can't read tty at %x", sess.s_ttyp);
540 1.1 cgd return (-1);
541 1.1 cgd }
542 1.1 cgd eproc.e_tdev = tty.t_dev;
543 1.1 cgd eproc.e_tsess = tty.t_session;
544 1.1 cgd if (tty.t_pgrp != NULL) {
545 1.1 cgd if (kvm_read(tty.t_pgrp, &pgrp, sizeof (struct
546 1.19 mycroft pgrp)) == -1) {
547 1.1 cgd seterr("can't read tpgrp at &x",
548 1.1 cgd tty.t_pgrp);
549 1.1 cgd return (-1);
550 1.1 cgd }
551 1.1 cgd eproc.e_tpgid = pgrp.pg_id;
552 1.1 cgd } else
553 1.1 cgd eproc.e_tpgid = -1;
554 1.1 cgd } else
555 1.1 cgd eproc.e_tdev = NODEV;
556 1.1 cgd if (proc.p_wmesg)
557 1.19 mycroft (void) kvm_read(proc.p_wmesg, eproc.e_wmesg, WMESGLEN);
558 1.1 cgd (void) kvm_read(proc.p_vmspace, &eproc.e_vm,
559 1.1 cgd sizeof (struct vmspace));
560 1.1 cgd eproc.e_xsize = eproc.e_xrssize =
561 1.1 cgd eproc.e_xccount = eproc.e_xswrss = 0;
562 1.1 cgd
563 1.1 cgd switch(ki_op(what)) {
564 1.1 cgd
565 1.1 cgd case KINFO_PROC_PGRP:
566 1.1 cgd if (eproc.e_pgid != (pid_t)arg)
567 1.1 cgd continue;
568 1.1 cgd break;
569 1.1 cgd
570 1.1 cgd case KINFO_PROC_TTY:
571 1.1 cgd if ((proc.p_flag&SCTTY) == 0 ||
572 1.1 cgd eproc.e_tdev != (dev_t)arg)
573 1.1 cgd continue;
574 1.1 cgd break;
575 1.1 cgd }
576 1.1 cgd
577 1.1 cgd i++;
578 1.1 cgd bcopy(&proc, bp, sizeof (struct proc));
579 1.1 cgd bp += sizeof (struct proc);
580 1.1 cgd bcopy(&eproc, bp, sizeof (struct eproc));
581 1.1 cgd bp+= sizeof (struct eproc);
582 1.1 cgd }
583 1.1 cgd if (!doingzomb) {
584 1.1 cgd /* zombproc */
585 1.1 cgd if (kvm_read((void *) nl[X_ZOMBPROC].n_value, &p,
586 1.19 mycroft sizeof (struct proc *)) == -1) {
587 1.1 cgd seterr("can't read zombproc");
588 1.1 cgd return (-1);
589 1.1 cgd }
590 1.1 cgd doingzomb = 1;
591 1.1 cgd goto again;
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd return (i);
595 1.1 cgd }
596 1.1 cgd
597 1.1 cgd struct proc *
598 1.1 cgd kvm_nextproc()
599 1.1 cgd {
600 1.1 cgd
601 1.1 cgd if (!kvmprocbase && kvm_getprocs(0, 0) == -1)
602 1.1 cgd return (NULL);
603 1.1 cgd if (kvmprocptr >= (kvmprocbase + kvmnprocs)) {
604 1.1 cgd seterr("end of proc list");
605 1.1 cgd return (NULL);
606 1.1 cgd }
607 1.1 cgd return((struct proc *)(kvmprocptr++));
608 1.1 cgd }
609 1.1 cgd
610 1.1 cgd struct eproc *
611 1.1 cgd kvm_geteproc(p)
612 1.1 cgd const struct proc *p;
613 1.1 cgd {
614 1.1 cgd return ((struct eproc *)(((char *)p) + sizeof (struct proc)));
615 1.1 cgd }
616 1.1 cgd
617 1.1 cgd kvm_setproc()
618 1.1 cgd {
619 1.1 cgd kvmprocptr = kvmprocbase;
620 1.1 cgd }
621 1.1 cgd
622 1.1 cgd kvm_freeprocs()
623 1.1 cgd {
624 1.1 cgd
625 1.1 cgd if (kvmprocbase) {
626 1.1 cgd free(kvmprocbase);
627 1.1 cgd kvmprocbase = NULL;
628 1.1 cgd }
629 1.1 cgd }
630 1.1 cgd
631 1.1 cgd struct user *
632 1.1 cgd kvm_getu(p)
633 1.1 cgd const struct proc *p;
634 1.1 cgd {
635 1.1 cgd register struct kinfo_proc *kp = (struct kinfo_proc *)p;
636 1.1 cgd register int i;
637 1.1 cgd register char *up;
638 1.3 cgd u_int vaddr;
639 1.3 cgd struct swapblk swb;
640 1.1 cgd
641 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
642 1.1 cgd return (NULL);
643 1.1 cgd if (p->p_stat == SZOMB) {
644 1.1 cgd seterr("zombie process");
645 1.1 cgd return (NULL);
646 1.1 cgd }
647 1.3 cgd
648 1.3 cgd if ((p->p_flag & SLOAD) == 0) {
649 1.3 cgd vm_offset_t maddr;
650 1.3 cgd
651 1.3 cgd if (swap < 0) {
652 1.3 cgd seterr("no swap");
653 1.3 cgd return (NULL);
654 1.3 cgd }
655 1.3 cgd /*
656 1.3 cgd * Costly operation, better set enable_swap to zero
657 1.3 cgd * in vm/vm_glue.c, since paging of user pages isn't
658 1.3 cgd * done yet anyway.
659 1.3 cgd */
660 1.19 mycroft if (vatosw(&kp->kp_eproc.e_vm.vm_map, USRSTACK + i * NBPG,
661 1.19 mycroft &maddr, &swb) == 0)
662 1.3 cgd return NULL;
663 1.3 cgd
664 1.3 cgd if (maddr == 0 && swb.size < UPAGES * NBPG)
665 1.3 cgd return NULL;
666 1.3 cgd
667 1.3 cgd for (i = 0; i < UPAGES; i++) {
668 1.3 cgd if (maddr) {
669 1.3 cgd (void) lseek(mem, maddr + i * NBPG, 0);
670 1.3 cgd if (read(mem,
671 1.3 cgd (char *)user.upages[i], NBPG) != NBPG) {
672 1.26 pk setsyserr(
673 1.3 cgd "can't read u for pid %d from %s",
674 1.3 cgd p->p_pid, swapf);
675 1.3 cgd return NULL;
676 1.3 cgd }
677 1.3 cgd } else {
678 1.3 cgd (void) lseek(swap, swb.offset + i * NBPG, 0);
679 1.3 cgd if (read(swap,
680 1.3 cgd (char *)user.upages[i], NBPG) != NBPG) {
681 1.26 pk setsyserr(
682 1.3 cgd "can't read u for pid %d from %s",
683 1.3 cgd p->p_pid, swapf);
684 1.3 cgd return NULL;
685 1.3 cgd }
686 1.3 cgd }
687 1.3 cgd }
688 1.3 cgd return(&user.user);
689 1.3 cgd }
690 1.1 cgd /*
691 1.1 cgd * Read u-area one page at a time for the benefit of post-mortems
692 1.1 cgd */
693 1.1 cgd up = (char *) p->p_addr;
694 1.1 cgd for (i = 0; i < UPAGES; i++) {
695 1.1 cgd klseek(kmem, (long)up, 0);
696 1.1 cgd if (read(kmem, user.upages[i], CLBYTES) != CLBYTES) {
697 1.26 pk setsyserr("cant read page %x of u of pid %d from %s",
698 1.1 cgd up, p->p_pid, kmemf);
699 1.1 cgd return(NULL);
700 1.1 cgd }
701 1.1 cgd up += CLBYTES;
702 1.1 cgd }
703 1.1 cgd pcbpf = (int) btop(p->p_addr); /* what should this be really? */
704 1.1 cgd
705 1.1 cgd return(&user.user);
706 1.1 cgd }
707 1.1 cgd
708 1.8 cgd int
709 1.8 cgd kvm_procread(p, addr, buf, len)
710 1.8 cgd const struct proc *p;
711 1.19 mycroft const unsigned addr;
712 1.19 mycroft unsigned len;
713 1.11 mycroft char *buf;
714 1.8 cgd {
715 1.8 cgd register struct kinfo_proc *kp = (struct kinfo_proc *) p;
716 1.8 cgd struct swapblk swb;
717 1.8 cgd vm_offset_t swaddr = 0, memaddr = 0;
718 1.8 cgd unsigned real_len;
719 1.24 mycroft static int last_pid = -1;
720 1.24 mycroft static vm_offset_t last_addr;
721 1.24 mycroft static char bouncebuf[CLBYTES];
722 1.8 cgd
723 1.8 cgd real_len = len < (CLBYTES - (addr & CLOFSET)) ? len : (CLBYTES - (addr & CLOFSET));
724 1.8 cgd
725 1.24 mycroft if (p->p_pid != last_pid || last_addr != (addr & ~CLOFSET)) {
726 1.24 mycroft if (vatosw(&kp->kp_eproc.e_vm.vm_map, addr & ~CLOFSET, &memaddr,
727 1.24 mycroft &swb) == 0)
728 1.11 mycroft return 0;
729 1.24 mycroft
730 1.24 mycroft if (memaddr) {
731 1.26 pk #if defined(sparc)
732 1.26 pk memaddr = phys2realphys(memaddr);
733 1.26 pk #endif
734 1.24 mycroft if (lseek(mem, memaddr, 0) == -1) {
735 1.26 pk setsyserr("kvm_procread: lseek mem");
736 1.24 mycroft return 0;
737 1.24 mycroft }
738 1.24 mycroft len = read(mem, bouncebuf, CLBYTES);
739 1.24 mycroft if (len == -1 || len < CLBYTES) {
740 1.24 mycroft last_pid = -1;
741 1.26 pk setsyserr("kvm_procread: read mem");
742 1.24 mycroft return 0;
743 1.24 mycroft }
744 1.24 mycroft } else {
745 1.24 mycroft swaddr = swb.offset;
746 1.24 mycroft if (lseek(swap, swaddr, 0) == -1) {
747 1.26 pk setsyserr("kvm_procread: lseek swap");
748 1.24 mycroft return 0;
749 1.24 mycroft }
750 1.24 mycroft len = read(swap, bouncebuf, CLBYTES);
751 1.24 mycroft if (len == -1 || len < CLBYTES) {
752 1.24 mycroft last_pid = -1;
753 1.26 pk setsyserr("kvm_procread: read swap");
754 1.24 mycroft return 0;
755 1.24 mycroft }
756 1.8 cgd }
757 1.19 mycroft }
758 1.8 cgd
759 1.24 mycroft memcpy(buf, &bouncebuf[addr & CLOFSET], real_len);
760 1.24 mycroft last_pid = p->p_pid;
761 1.24 mycroft last_addr = addr & ~CLOFSET;
762 1.24 mycroft return real_len;
763 1.8 cgd }
764 1.8 cgd
765 1.8 cgd int
766 1.8 cgd kvm_procreadstr(p, addr, buf, len)
767 1.8 cgd const struct proc *p;
768 1.11 mycroft const unsigned addr;
769 1.11 mycroft char *buf;
770 1.8 cgd unsigned len;
771 1.8 cgd {
772 1.10 deraadt int done, little;
773 1.10 deraadt char copy[200], *pb;
774 1.11 mycroft char a;
775 1.8 cgd
776 1.8 cgd done = 0;
777 1.15 cgd copy[0] = '\0';
778 1.10 deraadt while (len) {
779 1.10 deraadt little = kvm_procread(p, addr+done, copy, MIN(len, sizeof copy));
780 1.10 deraadt if (little<1)
781 1.10 deraadt break;
782 1.10 deraadt pb = copy;
783 1.10 deraadt while (little--) {
784 1.10 deraadt len--;
785 1.11 mycroft if( (*buf++ = *pb++) == '\0' )
786 1.15 cgd return done;
787 1.15 cgd done++;
788 1.10 deraadt }
789 1.8 cgd }
790 1.8 cgd return done;
791 1.8 cgd }
792 1.8 cgd
793 1.1 cgd char *
794 1.1 cgd kvm_getargs(p, up)
795 1.1 cgd const struct proc *p;
796 1.1 cgd const struct user *up;
797 1.1 cgd {
798 1.24 mycroft static char *cmdbuf = NULL, ucomm[sizeof(p->p_comm) + 4];
799 1.8 cgd register char *cp, *acp;
800 1.8 cgd int left, rv;
801 1.8 cgd struct ps_strings arginfo;
802 1.1 cgd
803 1.24 mycroft if (cmdbuf == NULL) {
804 1.24 mycroft cmdbuf = (char *)malloc(ARG_MAX + sizeof(p->p_comm) + 5);
805 1.24 mycroft if (cmdbuf == NULL)
806 1.24 mycroft cmdbuf = ucomm;
807 1.24 mycroft }
808 1.24 mycroft
809 1.24 mycroft if (cmdbuf == ucomm || up == NULL || p->p_pid == 0 || p->p_pid == 2)
810 1.1 cgd goto retucomm;
811 1.8 cgd
812 1.11 mycroft if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
813 1.8 cgd sizeof(arginfo))
814 1.8 cgd goto bad;
815 1.8 cgd
816 1.15 cgd cmdbuf[0] = '\0';
817 1.8 cgd cp = cmdbuf;
818 1.8 cgd acp = arginfo.ps_argvstr;
819 1.8 cgd left = ARG_MAX + 1;
820 1.8 cgd while (arginfo.ps_nargvstr--) {
821 1.8 cgd if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
822 1.8 cgd acp += rv + 1;
823 1.8 cgd left -= rv + 1;
824 1.8 cgd cp += rv;
825 1.8 cgd *cp++ = ' ';
826 1.8 cgd *cp = '\0';
827 1.1 cgd } else
828 1.1 cgd goto bad;
829 1.1 cgd }
830 1.8 cgd cp-- ; *cp = '\0';
831 1.2 cgd
832 1.8 cgd if (cmdbuf[0] == '-' || cmdbuf[0] == '?' || cmdbuf[0] <= ' ') {
833 1.1 cgd (void) strcat(cmdbuf, " (");
834 1.1 cgd (void) strncat(cmdbuf, p->p_comm, sizeof(p->p_comm));
835 1.1 cgd (void) strcat(cmdbuf, ")");
836 1.1 cgd }
837 1.1 cgd return (cmdbuf);
838 1.1 cgd
839 1.1 cgd bad:
840 1.8 cgd seterr("error locating command name for pid %d", p->p_pid);
841 1.1 cgd retucomm:
842 1.8 cgd (void) strcpy(cmdbuf, "(");
843 1.1 cgd (void) strncat(cmdbuf, p->p_comm, sizeof (p->p_comm));
844 1.1 cgd (void) strcat(cmdbuf, ")");
845 1.1 cgd return (cmdbuf);
846 1.1 cgd }
847 1.1 cgd
848 1.8 cgd char *
849 1.8 cgd kvm_getenv(p, up)
850 1.8 cgd const struct proc *p;
851 1.8 cgd const struct user *up;
852 1.8 cgd {
853 1.24 mycroft static char *envbuf = NULL, emptyenv[1];
854 1.8 cgd register char *cp, *acp;
855 1.8 cgd int left, rv;
856 1.8 cgd struct ps_strings arginfo;
857 1.8 cgd
858 1.24 mycroft if (envbuf == NULL) {
859 1.24 mycroft envbuf = (char *)malloc(ARG_MAX + 1);
860 1.24 mycroft if (envbuf == NULL)
861 1.24 mycroft envbuf = emptyenv;
862 1.24 mycroft }
863 1.24 mycroft
864 1.24 mycroft if (envbuf == emptyenv || up == NULL || p->p_pid == 0 || p->p_pid == 2)
865 1.8 cgd goto retemptyenv;
866 1.8 cgd
867 1.11 mycroft if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
868 1.8 cgd sizeof(arginfo))
869 1.8 cgd goto bad;
870 1.8 cgd
871 1.8 cgd cp = envbuf;
872 1.8 cgd acp = arginfo.ps_envstr;
873 1.8 cgd left = ARG_MAX + 1;
874 1.8 cgd while (arginfo.ps_nenvstr--) {
875 1.8 cgd if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
876 1.8 cgd acp += rv + 1;
877 1.8 cgd left -= rv + 1;
878 1.8 cgd cp += rv;
879 1.8 cgd *cp++ = ' ';
880 1.8 cgd *cp = '\0';
881 1.8 cgd } else
882 1.8 cgd goto bad;
883 1.8 cgd }
884 1.8 cgd cp-- ; *cp = '\0';
885 1.8 cgd return (envbuf);
886 1.8 cgd
887 1.8 cgd bad:
888 1.8 cgd seterr("error locating environment for pid %d", p->p_pid);
889 1.8 cgd retemptyenv:
890 1.8 cgd envbuf[0] = '\0';
891 1.8 cgd return (envbuf);
892 1.8 cgd }
893 1.1 cgd
894 1.1 cgd static
895 1.1 cgd getkvars()
896 1.1 cgd {
897 1.1 cgd if (kvm_nlist(nl) == -1)
898 1.1 cgd return (-1);
899 1.1 cgd if (deadkernel) {
900 1.1 cgd /* We must do the sys map first because klseek uses it */
901 1.1 cgd long addr;
902 1.1 cgd
903 1.21 cgd #if defined(m68k)
904 1.25 chopps #if defined(amiga)
905 1.25 chopps addr = (long) nl[X_CPU040].n_value;
906 1.25 chopps (void) lseek(kmem, addr, 0);
907 1.25 chopps if (read(kmem, (char *) &cpu040, sizeof (cpu040))
908 1.25 chopps != sizeof (cpu040)) {
909 1.25 chopps seterr("can't read cpu040");
910 1.25 chopps return (-1);
911 1.25 chopps }
912 1.25 chopps #endif
913 1.1 cgd addr = (long) nl[X_LOWRAM].n_value;
914 1.1 cgd (void) lseek(kmem, addr, 0);
915 1.1 cgd if (read(kmem, (char *) &lowram, sizeof (lowram))
916 1.1 cgd != sizeof (lowram)) {
917 1.1 cgd seterr("can't read lowram");
918 1.1 cgd return (-1);
919 1.1 cgd }
920 1.1 cgd lowram = btop(lowram);
921 1.1 cgd Sysseg = (struct ste *) malloc(NBPG);
922 1.1 cgd if (Sysseg == NULL) {
923 1.1 cgd seterr("out of space for Sysseg");
924 1.1 cgd return (-1);
925 1.1 cgd }
926 1.1 cgd addr = (long) nl[X_SYSSEG].n_value;
927 1.1 cgd (void) lseek(kmem, addr, 0);
928 1.1 cgd read(kmem, (char *)&addr, sizeof(addr));
929 1.1 cgd (void) lseek(kmem, (long)addr, 0);
930 1.1 cgd if (read(kmem, (char *) Sysseg, NBPG) != NBPG) {
931 1.1 cgd seterr("can't read Sysseg");
932 1.1 cgd return (-1);
933 1.1 cgd }
934 1.1 cgd #endif
935 1.1 cgd #if defined(i386)
936 1.1 cgd PTD = (struct pde *) malloc(NBPG);
937 1.1 cgd if (PTD == NULL) {
938 1.1 cgd seterr("out of space for PTD");
939 1.1 cgd return (-1);
940 1.1 cgd }
941 1.1 cgd addr = (long) nl[X_IdlePTD].n_value;
942 1.1 cgd (void) lseek(kmem, addr, 0);
943 1.1 cgd read(kmem, (char *)&addr, sizeof(addr));
944 1.1 cgd (void) lseek(kmem, (long)addr, 0);
945 1.1 cgd if (read(kmem, (char *) PTD, NBPG) != NBPG) {
946 1.1 cgd seterr("can't read PTD");
947 1.1 cgd return (-1);
948 1.1 cgd }
949 1.1 cgd #endif
950 1.1 cgd }
951 1.19 mycroft if (kvm_read((void *) nl[X_NSWAP].n_value, &nswap, sizeof (long)) == -1) {
952 1.1 cgd seterr("can't read nswap");
953 1.1 cgd return (-1);
954 1.1 cgd }
955 1.19 mycroft if (kvm_read((void *) nl[X_DMMIN].n_value, &dmmin, sizeof (long)) == -1) {
956 1.1 cgd seterr("can't read dmmin");
957 1.1 cgd return (-1);
958 1.1 cgd }
959 1.19 mycroft if (kvm_read((void *) nl[X_DMMAX].n_value, &dmmax, sizeof (long)) == -1) {
960 1.1 cgd seterr("can't read dmmax");
961 1.1 cgd return (-1);
962 1.1 cgd }
963 1.19 mycroft if (kvm_read((void *) nl[X_VM_PAGE_HASH_MASK].n_value,
964 1.19 mycroft &vm_page_hash_mask, sizeof (long)) == -1) {
965 1.19 mycroft seterr("can't read vm_page_hash_mask");
966 1.19 mycroft return (-1);
967 1.19 mycroft }
968 1.19 mycroft if (kvm_read((void *) nl[X_VM_PAGE_BUCKETS].n_value,
969 1.19 mycroft &vm_page_buckets, sizeof (long)) == -1) {
970 1.19 mycroft seterr("can't read vm_page_buckets");
971 1.19 mycroft return (-1);
972 1.19 mycroft }
973 1.19 mycroft if (kvm_read((void *) nl[X_PAGE_SHIFT].n_value,
974 1.19 mycroft &page_shift, sizeof (long)) == -1) {
975 1.19 mycroft seterr("can't read page_shift");
976 1.19 mycroft return (-1);
977 1.19 mycroft }
978 1.19 mycroft
979 1.1 cgd return (0);
980 1.1 cgd }
981 1.1 cgd
982 1.26 pk int
983 1.1 cgd kvm_read(loc, buf, len)
984 1.1 cgd void *loc;
985 1.1 cgd void *buf;
986 1.1 cgd {
987 1.26 pk int n;
988 1.26 pk
989 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
990 1.1 cgd return (-1);
991 1.19 mycroft klseek(kmem, (off_t) loc, 0);
992 1.26 pk if ((n = read(kmem, buf, len)) != len) {
993 1.26 pk if (n == -1)
994 1.26 pk setsyserr("error reading kmem at %#x", loc);
995 1.26 pk else
996 1.26 pk seterr("short read on kmem at %#x", loc);
997 1.19 mycroft return (-1);
998 1.1 cgd }
999 1.1 cgd return (len);
1000 1.1 cgd }
1001 1.1 cgd
1002 1.1 cgd static void
1003 1.1 cgd klseek(fd, loc, off)
1004 1.1 cgd int fd;
1005 1.1 cgd off_t loc;
1006 1.1 cgd int off;
1007 1.1 cgd {
1008 1.1 cgd
1009 1.1 cgd if (deadkernel) {
1010 1.1 cgd if ((loc = Vtophys(loc)) == -1)
1011 1.1 cgd return;
1012 1.1 cgd }
1013 1.1 cgd (void) lseek(fd, (off_t)loc, off);
1014 1.1 cgd }
1015 1.1 cgd
1016 1.1 cgd static off_t
1017 1.1 cgd Vtophys(loc)
1018 1.1 cgd u_long loc;
1019 1.1 cgd {
1020 1.1 cgd off_t newloc = (off_t) -1;
1021 1.21 cgd #if defined(m68k)
1022 1.1 cgd int p, ste, pte;
1023 1.1 cgd
1024 1.16 mycroft ste = *(int *)&Sysseg[btos(loc)];
1025 1.1 cgd if ((ste & SG_V) == 0) {
1026 1.1 cgd seterr("vtophys: segment not valid");
1027 1.1 cgd return((off_t) -1);
1028 1.1 cgd }
1029 1.1 cgd p = btop(loc & SG_PMASK);
1030 1.1 cgd newloc = (ste & SG_FRAME) + (p * sizeof(struct pte));
1031 1.16 mycroft (void) lseek(mem, newloc, 0);
1032 1.16 mycroft if (read(mem, (char *)&pte, sizeof pte) != sizeof pte) {
1033 1.1 cgd seterr("vtophys: cannot locate pte");
1034 1.1 cgd return((off_t) -1);
1035 1.1 cgd }
1036 1.1 cgd newloc = pte & PG_FRAME;
1037 1.1 cgd if (pte == PG_NV || newloc < (off_t)ptob(lowram)) {
1038 1.1 cgd seterr("vtophys: page not valid");
1039 1.1 cgd return((off_t) -1);
1040 1.1 cgd }
1041 1.1 cgd newloc = (newloc - (off_t)ptob(lowram)) + (loc & PGOFSET);
1042 1.1 cgd #endif
1043 1.26 pk #if defined(i386)
1044 1.1 cgd struct pde pde;
1045 1.1 cgd struct pte pte;
1046 1.1 cgd int p;
1047 1.1 cgd
1048 1.22 mycroft pde = PTD[loc >> PDSHIFT];
1049 1.1 cgd if (pde.pd_v == 0) {
1050 1.1 cgd seterr("vtophys: page directory entry not valid");
1051 1.1 cgd return((off_t) -1);
1052 1.1 cgd }
1053 1.1 cgd p = btop(loc & PT_MASK);
1054 1.1 cgd newloc = pde.pd_pfnum + (p * sizeof(struct pte));
1055 1.1 cgd (void) lseek(kmem, (long)newloc, 0);
1056 1.1 cgd if (read(kmem, (char *)&pte, sizeof pte) != sizeof pte) {
1057 1.1 cgd seterr("vtophys: cannot obtain desired pte");
1058 1.1 cgd return((off_t) -1);
1059 1.1 cgd }
1060 1.1 cgd newloc = pte.pg_pfnum;
1061 1.1 cgd if (pte.pg_v == 0) {
1062 1.1 cgd seterr("vtophys: page table entry not valid");
1063 1.1 cgd return((off_t) -1);
1064 1.1 cgd }
1065 1.1 cgd newloc += (loc & PGOFSET);
1066 1.1 cgd #endif
1067 1.1 cgd return((off_t) newloc);
1068 1.1 cgd }
1069 1.1 cgd
1070 1.3 cgd /*
1071 1.3 cgd * locate address of unwired or swapped page
1072 1.3 cgd */
1073 1.3 cgd
1074 1.3 cgd static int
1075 1.19 mycroft vatosw(mp, vaddr, maddr, swb)
1076 1.19 mycroft vm_map_t mp;
1077 1.3 cgd vm_offset_t vaddr;
1078 1.3 cgd vm_offset_t *maddr;
1079 1.3 cgd struct swapblk *swb;
1080 1.3 cgd {
1081 1.3 cgd struct vm_object vm_object;
1082 1.3 cgd struct vm_map_entry vm_entry;
1083 1.19 mycroft long saddr, addr, off;
1084 1.3 cgd int i;
1085 1.3 cgd
1086 1.19 mycroft saddr = addr = (long)mp->header.next;
1087 1.19 mycroft #ifdef DEBUG
1088 1.19 mycroft fprintf(stderr, "vatosw: head=%x\n", &mp->header);
1089 1.19 mycroft #endif
1090 1.19 mycroft for (i = 0; ; i++) {
1091 1.3 cgd /* Weed through map entries until vaddr in range */
1092 1.19 mycroft if (kvm_read((void *) addr, &vm_entry, sizeof(vm_entry)) == -1) {
1093 1.26 pk seterr("vatosw: can't read vm_map_entry");
1094 1.3 cgd return 0;
1095 1.3 cgd }
1096 1.19 mycroft #ifdef DEBUG
1097 1.19 mycroft fprintf(stderr, "vatosw: %d/%d, vaddr=%x, start=%x, end=%x ",
1098 1.19 mycroft i, mp->nentries, vaddr, vm_entry.start, vm_entry.end);
1099 1.19 mycroft fprintf(stderr, "addr=%x, next=%x\n", addr, vm_entry.next);
1100 1.18 mycroft #endif
1101 1.18 mycroft if ((vaddr >= vm_entry.start) && (vaddr < vm_entry.end))
1102 1.18 mycroft if (vm_entry.object.vm_object != 0)
1103 1.18 mycroft break;
1104 1.18 mycroft else {
1105 1.19 mycroft seterr("vatosw: no object\n");
1106 1.18 mycroft return 0;
1107 1.18 mycroft }
1108 1.3 cgd
1109 1.3 cgd addr = (long)vm_entry.next;
1110 1.19 mycroft
1111 1.19 mycroft if (addr == saddr) {
1112 1.19 mycroft seterr("vatosw: map not found\n");
1113 1.19 mycroft return 0;
1114 1.19 mycroft }
1115 1.3 cgd }
1116 1.3 cgd
1117 1.3 cgd if (vm_entry.is_a_map || vm_entry.is_sub_map) {
1118 1.19 mycroft #ifdef DEBUG
1119 1.19 mycroft fprintf(stderr, "vatosw: is a %smap\n",
1120 1.18 mycroft vm_entry.is_sub_map ? "sub " : "");
1121 1.18 mycroft #endif
1122 1.19 mycroft seterr("vatosw: is a %smap\n",
1123 1.19 mycroft vm_entry.is_sub_map ? "sub " : "");
1124 1.3 cgd return 0;
1125 1.3 cgd }
1126 1.3 cgd
1127 1.3 cgd /* Locate memory object */
1128 1.3 cgd off = (vaddr - vm_entry.start) + vm_entry.offset;
1129 1.3 cgd addr = (long)vm_entry.object.vm_object;
1130 1.3 cgd while (1) {
1131 1.19 mycroft if (kvm_read((void *) addr, &vm_object, sizeof (vm_object)) == -1) {
1132 1.26 pk seterr("vatosw: can't read vm_object");
1133 1.3 cgd return 0;
1134 1.3 cgd }
1135 1.3 cgd
1136 1.19 mycroft #ifdef DEBUG
1137 1.19 mycroft fprintf(stderr, "vatosw: find page: object %#x offset %x\n",
1138 1.19 mycroft addr, off);
1139 1.3 cgd #endif
1140 1.3 cgd
1141 1.3 cgd /* Lookup in page queue */
1142 1.18 mycroft if ((i = findpage(addr, off, maddr)) != -1)
1143 1.18 mycroft return i;
1144 1.18 mycroft
1145 1.18 mycroft if (vm_object.pager != 0 &&
1146 1.18 mycroft (i = pager_get(&vm_object, off, swb)) != -1)
1147 1.18 mycroft return i;
1148 1.3 cgd
1149 1.18 mycroft if (vm_object.shadow == 0)
1150 1.3 cgd break;
1151 1.3 cgd
1152 1.19 mycroft #ifdef DEBUG
1153 1.19 mycroft fprintf(stderr, "vatosw: shadow obj at %x: offset %x+%x\n",
1154 1.19 mycroft addr, off, vm_object.shadow_offset);
1155 1.3 cgd #endif
1156 1.3 cgd
1157 1.3 cgd addr = (long)vm_object.shadow;
1158 1.3 cgd off += vm_object.shadow_offset;
1159 1.3 cgd }
1160 1.3 cgd
1161 1.19 mycroft seterr("vatosw: page not found\n");
1162 1.18 mycroft return 0;
1163 1.18 mycroft }
1164 1.18 mycroft
1165 1.18 mycroft
1166 1.18 mycroft int
1167 1.18 mycroft pager_get(object, off, swb)
1168 1.18 mycroft struct vm_object *object;
1169 1.18 mycroft long off;
1170 1.18 mycroft struct swapblk *swb;
1171 1.18 mycroft {
1172 1.18 mycroft struct pager_struct pager;
1173 1.18 mycroft struct swpager swpager;
1174 1.18 mycroft struct swblock swblock;
1175 1.18 mycroft
1176 1.3 cgd /* Find address in swap space */
1177 1.19 mycroft if (kvm_read(object->pager, &pager, sizeof (pager)) == -1) {
1178 1.26 pk seterr("pager_get: can't read pager");
1179 1.3 cgd return 0;
1180 1.3 cgd }
1181 1.3 cgd if (pager.pg_type != PG_SWAP) {
1182 1.18 mycroft seterr("pager_get: weird pager\n");
1183 1.3 cgd return 0;
1184 1.3 cgd }
1185 1.3 cgd
1186 1.3 cgd /* Get swap pager data */
1187 1.19 mycroft if (kvm_read(pager.pg_data, &swpager, sizeof (swpager)) == -1) {
1188 1.26 pk seterr("pager_get: can't read swpager");
1189 1.3 cgd return 0;
1190 1.3 cgd }
1191 1.3 cgd
1192 1.18 mycroft off += object->paging_offset;
1193 1.3 cgd
1194 1.3 cgd /* Read swap block array */
1195 1.19 mycroft if (kvm_read((void *) swpager.sw_blocks +
1196 1.3 cgd (off/dbtob(swpager.sw_bsize)) * sizeof swblock,
1197 1.19 mycroft &swblock, sizeof (swblock)) == -1) {
1198 1.26 pk seterr("pager_get: can't read swblock");
1199 1.3 cgd return 0;
1200 1.3 cgd }
1201 1.18 mycroft
1202 1.18 mycroft off %= dbtob(swpager.sw_bsize);
1203 1.3 cgd
1204 1.18 mycroft if (swblock.swb_mask & (1 << atop(off))) {
1205 1.18 mycroft swb->offset = dbtob(swblock.swb_block) + off;
1206 1.18 mycroft swb->size = dbtob(swpager.sw_bsize) - off;
1207 1.18 mycroft return 1;
1208 1.18 mycroft }
1209 1.3 cgd
1210 1.18 mycroft return -1;
1211 1.18 mycroft }
1212 1.3 cgd
1213 1.3 cgd static int
1214 1.3 cgd findpage(object, offset, maddr)
1215 1.3 cgd long object;
1216 1.3 cgd long offset;
1217 1.3 cgd vm_offset_t *maddr;
1218 1.3 cgd {
1219 1.3 cgd queue_head_t bucket;
1220 1.3 cgd struct vm_page mem;
1221 1.3 cgd long addr, baddr;
1222 1.3 cgd
1223 1.19 mycroft baddr = vm_page_buckets +
1224 1.19 mycroft vm_page_hash(object,offset) * sizeof(queue_head_t);
1225 1.3 cgd
1226 1.19 mycroft if (kvm_read((void *) baddr, &bucket, sizeof (bucket)) == -1) {
1227 1.3 cgd seterr("can't read vm_page_bucket");
1228 1.3 cgd return 0;
1229 1.3 cgd }
1230 1.3 cgd
1231 1.3 cgd addr = (long)bucket.next;
1232 1.19 mycroft
1233 1.3 cgd while (addr != baddr) {
1234 1.19 mycroft if (kvm_read((void *) addr, &mem, sizeof (mem)) == -1) {
1235 1.3 cgd seterr("can't read vm_page");
1236 1.3 cgd return 0;
1237 1.3 cgd }
1238 1.19 mycroft
1239 1.3 cgd if ((long)mem.object == object && mem.offset == offset) {
1240 1.3 cgd *maddr = (long)mem.phys_addr;
1241 1.3 cgd return 1;
1242 1.3 cgd }
1243 1.19 mycroft
1244 1.3 cgd addr = (long)mem.hashq.next;
1245 1.3 cgd }
1246 1.18 mycroft
1247 1.18 mycroft return -1;
1248 1.3 cgd }
1249 1.3 cgd
1250 1.26 pk #if defined(sparc)
1251 1.26 pk /*
1252 1.26 pk * This comes from the bowels of pmap.c
1253 1.26 pk */
1254 1.26 pk #define MAXMEM (128 * 1024 * 1024) /* no more than 128 MB phys mem */
1255 1.26 pk #define NPGBANK 16 /* 2^4 pages per bank (64K / bank) */
1256 1.26 pk #define BSHIFT 4 /* log2(NPGBANK) */
1257 1.26 pk #define BOFFSET (NPGBANK - 1)
1258 1.26 pk #define BTSIZE (MAXMEM / NBPG / NPGBANK)
1259 1.26 pk
1260 1.26 pk static int pmap_dtos[BTSIZE]; /* dense to sparse */
1261 1.26 pk static int pmap_stod[BTSIZE]; /* sparse to dense */
1262 1.26 pk
1263 1.26 pk #define HWTOSW(pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
1264 1.26 pk #define SWTOHW(pg) (pmap_dtos[(pg) >> BSHIFT] | ((pg) & BOFFSET))
1265 1.26 pk /* -- */
1266 1.26 pk
1267 1.26 pk static int pmap_dtos_valid;
1268 1.26 pk
1269 1.26 pk /*
1270 1.26 pk * Translate a VM physical address to a hardware physical address.
1271 1.26 pk */
1272 1.26 pk static vm_offset_t
1273 1.26 pk phys2realphys(memaddr)
1274 1.26 pk vm_offset_t memaddr;
1275 1.26 pk {
1276 1.26 pk if (nl[X_PMAP_DTOS].n_value == 0)
1277 1.26 pk /* This is possibly a sun4 */
1278 1.26 pk return memaddr;
1279 1.26 pk
1280 1.26 pk if (!pmap_dtos_valid) {
1281 1.26 pk if (kvm_read((void *)nl[X_PMAP_DTOS].n_value,
1282 1.26 pk pmap_dtos, sizeof (pmap_dtos)) == -1) {
1283 1.26 pk seterr("can't read pmap_dtos table");
1284 1.26 pk return -1;
1285 1.26 pk }
1286 1.26 pk pmap_dtos_valid = 1;
1287 1.26 pk }
1288 1.26 pk return (SWTOHW(atop(memaddr)) << PGSHIFT) | (memaddr & PGOFSET);
1289 1.26 pk }
1290 1.26 pk #endif
1291 1.26 pk
1292 1.1 cgd #include <varargs.h>
1293 1.1 cgd static char errbuf[_POSIX2_LINE_MAX];
1294 1.1 cgd
1295 1.1 cgd static void
1296 1.1 cgd seterr(va_alist)
1297 1.1 cgd va_dcl
1298 1.1 cgd {
1299 1.1 cgd char *fmt;
1300 1.1 cgd va_list ap;
1301 1.1 cgd
1302 1.1 cgd va_start(ap);
1303 1.1 cgd fmt = va_arg(ap, char *);
1304 1.1 cgd (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
1305 1.19 mycroft #ifdef DEBUG
1306 1.26 pk (void) fprintf(stderr, "%s\n", errbuf);
1307 1.3 cgd #endif
1308 1.1 cgd va_end(ap);
1309 1.1 cgd }
1310 1.1 cgd
1311 1.1 cgd static void
1312 1.1 cgd setsyserr(va_alist)
1313 1.1 cgd va_dcl
1314 1.1 cgd {
1315 1.1 cgd char *fmt, *cp;
1316 1.1 cgd va_list ap;
1317 1.1 cgd extern int errno;
1318 1.1 cgd
1319 1.1 cgd va_start(ap);
1320 1.1 cgd fmt = va_arg(ap, char *);
1321 1.20 mycroft (void) vsnprintf(cp = errbuf, _POSIX2_LINE_MAX, fmt, ap);
1322 1.19 mycroft cp += strlen(cp);
1323 1.19 mycroft (void) snprintf(cp, _POSIX2_LINE_MAX - (cp - errbuf), ": %s",
1324 1.19 mycroft strerror(errno));
1325 1.19 mycroft #ifdef DEBUG
1326 1.26 pk (void) fprintf(stderr, "%s\n", errbuf);
1327 1.19 mycroft #endif
1328 1.1 cgd va_end(ap);
1329 1.1 cgd }
1330 1.1 cgd
1331 1.1 cgd char *
1332 1.1 cgd kvm_geterr()
1333 1.1 cgd {
1334 1.1 cgd return (errbuf);
1335 1.1 cgd }
1336