kvm.c revision 1.25 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.25 chopps static char rcsid[] = "$Id: kvm.c,v 1.25 1994/02/14 04:34:44 chopps 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.1 cgd /*
145 1.1 cgd * everything here and down, only if a dead kernel
146 1.1 cgd */
147 1.1 cgd { "_Sysmap" },
148 1.3 cgd #define X_SYSMAP 8
149 1.1 cgd #define X_DEADKERNEL X_SYSMAP
150 1.1 cgd { "_Syssize" },
151 1.3 cgd #define X_SYSSIZE 9
152 1.1 cgd { "_allproc" },
153 1.3 cgd #define X_ALLPROC 10
154 1.1 cgd { "_zombproc" },
155 1.3 cgd #define X_ZOMBPROC 11
156 1.1 cgd { "_nproc" },
157 1.3 cgd #define X_NPROC 12
158 1.3 cgd #define X_LAST 12
159 1.21 cgd #if defined(m68k)
160 1.1 cgd { "_Sysseg" },
161 1.1 cgd #define X_SYSSEG (X_LAST+1)
162 1.1 cgd { "_lowram" },
163 1.1 cgd #define X_LOWRAM (X_LAST+2)
164 1.25 chopps #if defined(amiga)
165 1.25 chopps { "_cpu040" },
166 1.25 chopps #define X_CPU040 (X_LAST+3)
167 1.25 chopps #endif
168 1.1 cgd #endif
169 1.1 cgd #if defined(i386)
170 1.1 cgd { "_IdlePTD" },
171 1.1 cgd #define X_IdlePTD (X_LAST+1)
172 1.1 cgd #endif
173 1.1 cgd { "" },
174 1.1 cgd };
175 1.1 cgd
176 1.1 cgd static off_t Vtophys();
177 1.1 cgd static void klseek(), seterr(), setsyserr(), vstodb();
178 1.1 cgd static int getkvars(), kvm_doprocs(), kvm_init();
179 1.3 cgd static int vatosw();
180 1.19 mycroft static int pager_get();
181 1.3 cgd static int findpage();
182 1.1 cgd
183 1.1 cgd /*
184 1.1 cgd * returns 0 if files were opened now,
185 1.1 cgd * 1 if files were already opened,
186 1.1 cgd * -1 if files could not be opened.
187 1.1 cgd */
188 1.1 cgd kvm_openfiles(uf, mf, sf)
189 1.1 cgd const char *uf, *mf, *sf;
190 1.1 cgd {
191 1.1 cgd if (kvmfilesopen)
192 1.1 cgd return (1);
193 1.1 cgd unixx = mem = kmem = swap = -1;
194 1.1 cgd unixf = (uf == NULL) ? _PATH_UNIX : uf;
195 1.1 cgd memf = (mf == NULL) ? _PATH_MEM : mf;
196 1.1 cgd
197 1.1 cgd if ((unixx = open(unixf, O_RDONLY, 0)) == -1) {
198 1.1 cgd setsyserr("can't open %s", unixf);
199 1.1 cgd goto failed;
200 1.1 cgd }
201 1.1 cgd if ((mem = open(memf, O_RDONLY, 0)) == -1) {
202 1.1 cgd setsyserr("can't open %s", memf);
203 1.1 cgd goto failed;
204 1.1 cgd }
205 1.1 cgd if (sf != NULL)
206 1.1 cgd swapf = sf;
207 1.1 cgd if (mf != NULL) {
208 1.1 cgd deadkernel++;
209 1.1 cgd kmemf = mf;
210 1.1 cgd kmem = mem;
211 1.1 cgd swap = -1;
212 1.1 cgd } else {
213 1.1 cgd kmemf = _PATH_KMEM;
214 1.1 cgd if ((kmem = open(kmemf, O_RDONLY, 0)) == -1) {
215 1.1 cgd setsyserr("can't open %s", kmemf);
216 1.1 cgd goto failed;
217 1.1 cgd }
218 1.1 cgd swapf = (sf == NULL) ? _PATH_DRUM : sf;
219 1.1 cgd /*
220 1.1 cgd * live kernel - avoid looking up nlist entries
221 1.1 cgd * past X_DEADKERNEL.
222 1.1 cgd */
223 1.1 cgd nl[X_DEADKERNEL].n_name = "";
224 1.1 cgd }
225 1.1 cgd if (swapf != NULL && ((swap = open(swapf, O_RDONLY, 0)) == -1)) {
226 1.1 cgd seterr("can't open %s", swapf);
227 1.1 cgd goto failed;
228 1.1 cgd }
229 1.1 cgd kvmfilesopen++;
230 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1) /*XXX*/
231 1.1 cgd return (-1);
232 1.1 cgd return (0);
233 1.1 cgd failed:
234 1.1 cgd kvm_close();
235 1.1 cgd return (-1);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd static
239 1.1 cgd kvm_init(uf, mf, sf)
240 1.1 cgd char *uf, *mf, *sf;
241 1.1 cgd {
242 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
243 1.1 cgd return (-1);
244 1.1 cgd if (getkvars() == -1)
245 1.1 cgd return (-1);
246 1.1 cgd kvminit = 1;
247 1.1 cgd
248 1.1 cgd return (0);
249 1.1 cgd }
250 1.1 cgd
251 1.1 cgd kvm_close()
252 1.1 cgd {
253 1.1 cgd if (unixx != -1) {
254 1.1 cgd close(unixx);
255 1.1 cgd unixx = -1;
256 1.1 cgd }
257 1.1 cgd if (kmem != -1) {
258 1.1 cgd if (kmem != mem)
259 1.1 cgd close(kmem);
260 1.1 cgd /* otherwise kmem is a copy of mem, and will be closed below */
261 1.1 cgd kmem = -1;
262 1.1 cgd }
263 1.1 cgd if (mem != -1) {
264 1.1 cgd close(mem);
265 1.1 cgd mem = -1;
266 1.1 cgd }
267 1.1 cgd if (swap != -1) {
268 1.1 cgd close(swap);
269 1.1 cgd swap = -1;
270 1.1 cgd }
271 1.1 cgd if (db != NULL) {
272 1.1 cgd dbm_close(db);
273 1.1 cgd db = NULL;
274 1.1 cgd }
275 1.1 cgd kvminit = 0;
276 1.1 cgd kvmfilesopen = 0;
277 1.1 cgd deadkernel = 0;
278 1.1 cgd }
279 1.1 cgd
280 1.1 cgd kvm_nlist(nl)
281 1.1 cgd struct nlist *nl;
282 1.1 cgd {
283 1.1 cgd datum key, data;
284 1.1 cgd char dbname[MAXPATHLEN];
285 1.1 cgd char dbversion[_POSIX2_LINE_MAX];
286 1.1 cgd char kversion[_POSIX2_LINE_MAX];
287 1.1 cgd int dbversionlen;
288 1.1 cgd char symbuf[MAXSYMSIZE];
289 1.1 cgd struct nlist nbuf, *n;
290 1.1 cgd int num, did;
291 1.1 cgd
292 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
293 1.1 cgd return (-1);
294 1.1 cgd if (deadkernel)
295 1.1 cgd goto hard2;
296 1.1 cgd /*
297 1.1 cgd * initialize key datum
298 1.1 cgd */
299 1.1 cgd key.dptr = symbuf;
300 1.1 cgd
301 1.1 cgd if (db != NULL)
302 1.1 cgd goto win; /* off to the races */
303 1.1 cgd /*
304 1.1 cgd * open database
305 1.1 cgd */
306 1.1 cgd sprintf(dbname, "%s/kvm_%s", _PATH_VARRUN, basename(unixf));
307 1.1 cgd if ((db = dbm_open(dbname, O_RDONLY, 0)) == NULL)
308 1.1 cgd goto hard2;
309 1.1 cgd /*
310 1.1 cgd * read version out of database
311 1.1 cgd */
312 1.1 cgd bcopy("VERSION", symbuf, sizeof ("VERSION")-1);
313 1.2 cgd key.dsize = (sizeof ("VERSION") - 1);
314 1.1 cgd data = dbm_fetch(db, key);
315 1.1 cgd if (data.dptr == NULL)
316 1.1 cgd goto hard1;
317 1.1 cgd bcopy(data.dptr, dbversion, data.dsize);
318 1.1 cgd dbversionlen = data.dsize;
319 1.1 cgd /*
320 1.1 cgd * read version string from kernel memory
321 1.1 cgd */
322 1.1 cgd bcopy("_version", symbuf, sizeof ("_version")-1);
323 1.2 cgd key.dsize = (sizeof ("_version")-1);
324 1.1 cgd data = dbm_fetch(db, key);
325 1.1 cgd if (data.dptr == NULL)
326 1.1 cgd goto hard1;
327 1.1 cgd if (data.dsize != sizeof (struct nlist))
328 1.1 cgd goto hard1;
329 1.1 cgd bcopy(data.dptr, &nbuf, sizeof (struct nlist));
330 1.1 cgd lseek(kmem, nbuf.n_value, 0);
331 1.1 cgd if (read(kmem, kversion, dbversionlen) != dbversionlen)
332 1.1 cgd goto hard1;
333 1.1 cgd /*
334 1.1 cgd * if they match, we win - otherwise do it the hard way
335 1.1 cgd */
336 1.1 cgd if (bcmp(dbversion, kversion, dbversionlen) != 0)
337 1.1 cgd goto hard1;
338 1.1 cgd /*
339 1.1 cgd * getem from the database.
340 1.1 cgd */
341 1.1 cgd win:
342 1.1 cgd num = did = 0;
343 1.1 cgd for (n = nl; n->n_name && n->n_name[0]; n++, num++) {
344 1.1 cgd int len;
345 1.1 cgd /*
346 1.1 cgd * clear out fields from users buffer
347 1.1 cgd */
348 1.1 cgd n->n_type = 0;
349 1.1 cgd n->n_other = 0;
350 1.1 cgd n->n_desc = 0;
351 1.1 cgd n->n_value = 0;
352 1.1 cgd /*
353 1.1 cgd * query db
354 1.1 cgd */
355 1.1 cgd if ((len = strlen(n->n_name)) > MAXSYMSIZE) {
356 1.1 cgd seterr("symbol too large");
357 1.1 cgd return (-1);
358 1.1 cgd }
359 1.1 cgd (void)strcpy(symbuf, n->n_name);
360 1.2 cgd key.dsize = len;
361 1.1 cgd data = dbm_fetch(db, key);
362 1.1 cgd if (data.dptr == NULL || data.dsize != sizeof (struct nlist))
363 1.1 cgd continue;
364 1.1 cgd bcopy(data.dptr, &nbuf, sizeof (struct nlist));
365 1.1 cgd n->n_value = nbuf.n_value;
366 1.1 cgd n->n_type = nbuf.n_type;
367 1.1 cgd n->n_desc = nbuf.n_desc;
368 1.1 cgd n->n_other = nbuf.n_other;
369 1.1 cgd did++;
370 1.1 cgd }
371 1.1 cgd return (num - did);
372 1.1 cgd hard1:
373 1.1 cgd dbm_close(db);
374 1.1 cgd db = NULL;
375 1.1 cgd hard2:
376 1.1 cgd num = nlist(unixf, nl);
377 1.1 cgd if (num == -1)
378 1.1 cgd seterr("nlist (hard way) failed");
379 1.1 cgd return (num);
380 1.1 cgd }
381 1.1 cgd
382 1.1 cgd kvm_getprocs(what, arg)
383 1.1 cgd int what, arg;
384 1.1 cgd {
385 1.3 cgd static int ocopysize = -1;
386 1.3 cgd
387 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
388 1.1 cgd return (NULL);
389 1.1 cgd if (!deadkernel) {
390 1.1 cgd int ret, copysize;
391 1.1 cgd
392 1.1 cgd if ((ret = getkerninfo(what, NULL, NULL, arg)) == -1) {
393 1.1 cgd setsyserr("can't get estimate for kerninfo");
394 1.1 cgd return (-1);
395 1.1 cgd }
396 1.1 cgd copysize = ret;
397 1.5 cgd if (copysize > ocopysize || !kvmprocbase) {
398 1.5 cgd if (ocopysize == -1 || !kvmprocbase)
399 1.4 mycroft kvmprocbase =
400 1.4 mycroft (struct kinfo_proc *)malloc(copysize);
401 1.4 mycroft else
402 1.4 mycroft kvmprocbase =
403 1.4 mycroft (struct kinfo_proc *)realloc(kvmprocbase,
404 1.4 mycroft copysize);
405 1.4 mycroft if (!kvmprocbase) {
406 1.4 mycroft seterr("out of memory");
407 1.4 mycroft return (-1);
408 1.4 mycroft }
409 1.1 cgd }
410 1.3 cgd ocopysize = copysize;
411 1.1 cgd if ((ret = getkerninfo(what, kvmprocbase, ©size,
412 1.1 cgd arg)) == -1) {
413 1.1 cgd setsyserr("can't get proc list");
414 1.1 cgd return (-1);
415 1.1 cgd }
416 1.1 cgd if (copysize % sizeof (struct kinfo_proc)) {
417 1.1 cgd seterr("proc size mismatch (got %d total, kinfo_proc: %d)",
418 1.1 cgd copysize, sizeof (struct kinfo_proc));
419 1.1 cgd return (-1);
420 1.1 cgd }
421 1.1 cgd kvmnprocs = copysize / sizeof (struct kinfo_proc);
422 1.1 cgd } else {
423 1.1 cgd int nproc;
424 1.1 cgd
425 1.1 cgd if (kvm_read((void *) nl[X_NPROC].n_value, &nproc,
426 1.19 mycroft sizeof (int)) == -1) {
427 1.1 cgd seterr("can't read nproc");
428 1.1 cgd return (-1);
429 1.1 cgd }
430 1.1 cgd if ((kvmprocbase = (struct kinfo_proc *)
431 1.1 cgd malloc(nproc * sizeof (struct kinfo_proc))) == NULL) {
432 1.1 cgd seterr("out of memory (addr: %x nproc = %d)",
433 1.1 cgd nl[X_NPROC].n_value, nproc);
434 1.1 cgd return (-1);
435 1.1 cgd }
436 1.1 cgd kvmnprocs = kvm_doprocs(what, arg, kvmprocbase);
437 1.1 cgd realloc(kvmprocbase, kvmnprocs * sizeof (struct kinfo_proc));
438 1.1 cgd }
439 1.1 cgd kvmprocptr = kvmprocbase;
440 1.1 cgd
441 1.1 cgd return (kvmnprocs);
442 1.1 cgd }
443 1.1 cgd
444 1.1 cgd /*
445 1.1 cgd * XXX - should NOT give up so easily - especially since the kernel
446 1.1 cgd * may be corrupt (it died). Should gather as much information as possible.
447 1.1 cgd * Follows proc ptrs instead of reading table since table may go
448 1.1 cgd * away soon.
449 1.1 cgd */
450 1.1 cgd static
451 1.1 cgd kvm_doprocs(what, arg, buff)
452 1.1 cgd int what, arg;
453 1.1 cgd char *buff;
454 1.1 cgd {
455 1.1 cgd struct proc *p, proc;
456 1.1 cgd register char *bp = buff;
457 1.1 cgd int i = 0;
458 1.1 cgd int doingzomb = 0;
459 1.1 cgd struct eproc eproc;
460 1.1 cgd struct pgrp pgrp;
461 1.1 cgd struct session sess;
462 1.1 cgd struct tty tty;
463 1.1 cgd
464 1.1 cgd /* allproc */
465 1.1 cgd if (kvm_read((void *) nl[X_ALLPROC].n_value, &p,
466 1.19 mycroft sizeof (struct proc *)) == -1) {
467 1.1 cgd seterr("can't read allproc");
468 1.1 cgd return (-1);
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd again:
472 1.1 cgd for (; p; p = proc.p_nxt) {
473 1.19 mycroft if (kvm_read(p, &proc, sizeof (struct proc)) == -1) {
474 1.1 cgd seterr("can't read proc at %x", p);
475 1.1 cgd return (-1);
476 1.1 cgd }
477 1.1 cgd if (kvm_read(proc.p_cred, &eproc.e_pcred,
478 1.19 mycroft sizeof (struct pcred)) != -1)
479 1.1 cgd (void) kvm_read(eproc.e_pcred.pc_ucred, &eproc.e_ucred,
480 1.1 cgd sizeof (struct ucred));
481 1.19 mycroft
482 1.1 cgd switch(ki_op(what)) {
483 1.1 cgd
484 1.1 cgd case KINFO_PROC_PID:
485 1.1 cgd if (proc.p_pid != (pid_t)arg)
486 1.1 cgd continue;
487 1.1 cgd break;
488 1.1 cgd
489 1.1 cgd
490 1.1 cgd case KINFO_PROC_UID:
491 1.1 cgd if (eproc.e_ucred.cr_uid != (uid_t)arg)
492 1.1 cgd continue;
493 1.1 cgd break;
494 1.1 cgd
495 1.1 cgd case KINFO_PROC_RUID:
496 1.1 cgd if (eproc.e_pcred.p_ruid != (uid_t)arg)
497 1.1 cgd continue;
498 1.1 cgd break;
499 1.1 cgd }
500 1.1 cgd /*
501 1.1 cgd * gather eproc
502 1.1 cgd */
503 1.1 cgd eproc.e_paddr = p;
504 1.19 mycroft if (kvm_read(proc.p_pgrp, &pgrp, sizeof (struct pgrp)) == -1) {
505 1.1 cgd seterr("can't read pgrp at %x", proc.p_pgrp);
506 1.1 cgd return (-1);
507 1.1 cgd }
508 1.1 cgd eproc.e_sess = pgrp.pg_session;
509 1.1 cgd eproc.e_pgid = pgrp.pg_id;
510 1.1 cgd eproc.e_jobc = pgrp.pg_jobc;
511 1.1 cgd if (kvm_read(pgrp.pg_session, &sess, sizeof (struct session))
512 1.19 mycroft == -1) {
513 1.1 cgd seterr("can't read session at %x", pgrp.pg_session);
514 1.1 cgd return (-1);
515 1.1 cgd }
516 1.1 cgd if ((proc.p_flag&SCTTY) && sess.s_ttyp != NULL) {
517 1.1 cgd if (kvm_read(sess.s_ttyp, &tty, sizeof (struct tty))
518 1.19 mycroft == -1) {
519 1.1 cgd seterr("can't read tty at %x", sess.s_ttyp);
520 1.1 cgd return (-1);
521 1.1 cgd }
522 1.1 cgd eproc.e_tdev = tty.t_dev;
523 1.1 cgd eproc.e_tsess = tty.t_session;
524 1.1 cgd if (tty.t_pgrp != NULL) {
525 1.1 cgd if (kvm_read(tty.t_pgrp, &pgrp, sizeof (struct
526 1.19 mycroft pgrp)) == -1) {
527 1.1 cgd seterr("can't read tpgrp at &x",
528 1.1 cgd tty.t_pgrp);
529 1.1 cgd return (-1);
530 1.1 cgd }
531 1.1 cgd eproc.e_tpgid = pgrp.pg_id;
532 1.1 cgd } else
533 1.1 cgd eproc.e_tpgid = -1;
534 1.1 cgd } else
535 1.1 cgd eproc.e_tdev = NODEV;
536 1.1 cgd if (proc.p_wmesg)
537 1.19 mycroft (void) kvm_read(proc.p_wmesg, eproc.e_wmesg, WMESGLEN);
538 1.1 cgd (void) kvm_read(proc.p_vmspace, &eproc.e_vm,
539 1.1 cgd sizeof (struct vmspace));
540 1.1 cgd eproc.e_xsize = eproc.e_xrssize =
541 1.1 cgd eproc.e_xccount = eproc.e_xswrss = 0;
542 1.1 cgd
543 1.1 cgd switch(ki_op(what)) {
544 1.1 cgd
545 1.1 cgd case KINFO_PROC_PGRP:
546 1.1 cgd if (eproc.e_pgid != (pid_t)arg)
547 1.1 cgd continue;
548 1.1 cgd break;
549 1.1 cgd
550 1.1 cgd case KINFO_PROC_TTY:
551 1.1 cgd if ((proc.p_flag&SCTTY) == 0 ||
552 1.1 cgd eproc.e_tdev != (dev_t)arg)
553 1.1 cgd continue;
554 1.1 cgd break;
555 1.1 cgd }
556 1.1 cgd
557 1.1 cgd i++;
558 1.1 cgd bcopy(&proc, bp, sizeof (struct proc));
559 1.1 cgd bp += sizeof (struct proc);
560 1.1 cgd bcopy(&eproc, bp, sizeof (struct eproc));
561 1.1 cgd bp+= sizeof (struct eproc);
562 1.1 cgd }
563 1.1 cgd if (!doingzomb) {
564 1.1 cgd /* zombproc */
565 1.1 cgd if (kvm_read((void *) nl[X_ZOMBPROC].n_value, &p,
566 1.19 mycroft sizeof (struct proc *)) == -1) {
567 1.1 cgd seterr("can't read zombproc");
568 1.1 cgd return (-1);
569 1.1 cgd }
570 1.1 cgd doingzomb = 1;
571 1.1 cgd goto again;
572 1.1 cgd }
573 1.1 cgd
574 1.1 cgd return (i);
575 1.1 cgd }
576 1.1 cgd
577 1.1 cgd struct proc *
578 1.1 cgd kvm_nextproc()
579 1.1 cgd {
580 1.1 cgd
581 1.1 cgd if (!kvmprocbase && kvm_getprocs(0, 0) == -1)
582 1.1 cgd return (NULL);
583 1.1 cgd if (kvmprocptr >= (kvmprocbase + kvmnprocs)) {
584 1.1 cgd seterr("end of proc list");
585 1.1 cgd return (NULL);
586 1.1 cgd }
587 1.1 cgd return((struct proc *)(kvmprocptr++));
588 1.1 cgd }
589 1.1 cgd
590 1.1 cgd struct eproc *
591 1.1 cgd kvm_geteproc(p)
592 1.1 cgd const struct proc *p;
593 1.1 cgd {
594 1.1 cgd return ((struct eproc *)(((char *)p) + sizeof (struct proc)));
595 1.1 cgd }
596 1.1 cgd
597 1.1 cgd kvm_setproc()
598 1.1 cgd {
599 1.1 cgd kvmprocptr = kvmprocbase;
600 1.1 cgd }
601 1.1 cgd
602 1.1 cgd kvm_freeprocs()
603 1.1 cgd {
604 1.1 cgd
605 1.1 cgd if (kvmprocbase) {
606 1.1 cgd free(kvmprocbase);
607 1.1 cgd kvmprocbase = NULL;
608 1.1 cgd }
609 1.1 cgd }
610 1.1 cgd
611 1.1 cgd struct user *
612 1.1 cgd kvm_getu(p)
613 1.1 cgd const struct proc *p;
614 1.1 cgd {
615 1.1 cgd register struct kinfo_proc *kp = (struct kinfo_proc *)p;
616 1.1 cgd register int i;
617 1.1 cgd register char *up;
618 1.3 cgd u_int vaddr;
619 1.3 cgd struct swapblk swb;
620 1.1 cgd
621 1.1 cgd if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
622 1.1 cgd return (NULL);
623 1.1 cgd if (p->p_stat == SZOMB) {
624 1.1 cgd seterr("zombie process");
625 1.1 cgd return (NULL);
626 1.1 cgd }
627 1.3 cgd
628 1.3 cgd if ((p->p_flag & SLOAD) == 0) {
629 1.3 cgd vm_offset_t maddr;
630 1.3 cgd
631 1.3 cgd if (swap < 0) {
632 1.3 cgd seterr("no swap");
633 1.3 cgd return (NULL);
634 1.3 cgd }
635 1.3 cgd /*
636 1.3 cgd * Costly operation, better set enable_swap to zero
637 1.3 cgd * in vm/vm_glue.c, since paging of user pages isn't
638 1.3 cgd * done yet anyway.
639 1.3 cgd */
640 1.19 mycroft if (vatosw(&kp->kp_eproc.e_vm.vm_map, USRSTACK + i * NBPG,
641 1.19 mycroft &maddr, &swb) == 0)
642 1.3 cgd return NULL;
643 1.3 cgd
644 1.3 cgd if (maddr == 0 && swb.size < UPAGES * NBPG)
645 1.3 cgd return NULL;
646 1.3 cgd
647 1.3 cgd for (i = 0; i < UPAGES; i++) {
648 1.3 cgd if (maddr) {
649 1.3 cgd (void) lseek(mem, maddr + i * NBPG, 0);
650 1.3 cgd if (read(mem,
651 1.3 cgd (char *)user.upages[i], NBPG) != NBPG) {
652 1.3 cgd seterr(
653 1.3 cgd "can't read u for pid %d from %s",
654 1.3 cgd p->p_pid, swapf);
655 1.3 cgd return NULL;
656 1.3 cgd }
657 1.3 cgd } else {
658 1.3 cgd (void) lseek(swap, swb.offset + i * NBPG, 0);
659 1.3 cgd if (read(swap,
660 1.3 cgd (char *)user.upages[i], NBPG) != NBPG) {
661 1.3 cgd seterr(
662 1.3 cgd "can't read u for pid %d from %s",
663 1.3 cgd p->p_pid, swapf);
664 1.3 cgd return NULL;
665 1.3 cgd }
666 1.3 cgd }
667 1.3 cgd }
668 1.3 cgd return(&user.user);
669 1.3 cgd }
670 1.1 cgd /*
671 1.1 cgd * Read u-area one page at a time for the benefit of post-mortems
672 1.1 cgd */
673 1.1 cgd up = (char *) p->p_addr;
674 1.1 cgd for (i = 0; i < UPAGES; i++) {
675 1.1 cgd klseek(kmem, (long)up, 0);
676 1.1 cgd if (read(kmem, user.upages[i], CLBYTES) != CLBYTES) {
677 1.1 cgd seterr("cant read page %x of u of pid %d from %s",
678 1.1 cgd up, p->p_pid, kmemf);
679 1.1 cgd return(NULL);
680 1.1 cgd }
681 1.1 cgd up += CLBYTES;
682 1.1 cgd }
683 1.1 cgd pcbpf = (int) btop(p->p_addr); /* what should this be really? */
684 1.1 cgd
685 1.1 cgd return(&user.user);
686 1.1 cgd }
687 1.1 cgd
688 1.8 cgd int
689 1.8 cgd kvm_procread(p, addr, buf, len)
690 1.8 cgd const struct proc *p;
691 1.19 mycroft const unsigned addr;
692 1.19 mycroft unsigned len;
693 1.11 mycroft char *buf;
694 1.8 cgd {
695 1.8 cgd register struct kinfo_proc *kp = (struct kinfo_proc *) p;
696 1.8 cgd struct swapblk swb;
697 1.8 cgd vm_offset_t swaddr = 0, memaddr = 0;
698 1.8 cgd unsigned real_len;
699 1.24 mycroft static int last_pid = -1;
700 1.24 mycroft static vm_offset_t last_addr;
701 1.24 mycroft static char bouncebuf[CLBYTES];
702 1.8 cgd
703 1.8 cgd real_len = len < (CLBYTES - (addr & CLOFSET)) ? len : (CLBYTES - (addr & CLOFSET));
704 1.8 cgd
705 1.24 mycroft if (p->p_pid != last_pid || last_addr != (addr & ~CLOFSET)) {
706 1.24 mycroft if (vatosw(&kp->kp_eproc.e_vm.vm_map, addr & ~CLOFSET, &memaddr,
707 1.24 mycroft &swb) == 0)
708 1.11 mycroft return 0;
709 1.24 mycroft
710 1.24 mycroft if (memaddr) {
711 1.24 mycroft if (lseek(mem, memaddr, 0) == -1) {
712 1.24 mycroft seterr("kvm_procread: lseek mem");
713 1.24 mycroft return 0;
714 1.24 mycroft }
715 1.24 mycroft len = read(mem, bouncebuf, CLBYTES);
716 1.24 mycroft if (len == -1 || len < CLBYTES) {
717 1.24 mycroft last_pid = -1;
718 1.24 mycroft seterr("kvm_procread: read mem");
719 1.24 mycroft return 0;
720 1.24 mycroft }
721 1.24 mycroft } else {
722 1.24 mycroft swaddr = swb.offset;
723 1.24 mycroft if (lseek(swap, swaddr, 0) == -1) {
724 1.24 mycroft seterr("kvm_procread: lseek swap");
725 1.24 mycroft return 0;
726 1.24 mycroft }
727 1.24 mycroft len = read(swap, bouncebuf, CLBYTES);
728 1.24 mycroft if (len == -1 || len < CLBYTES) {
729 1.24 mycroft last_pid = -1;
730 1.24 mycroft seterr("kvm_procread: read swap");
731 1.24 mycroft return 0;
732 1.24 mycroft }
733 1.8 cgd }
734 1.19 mycroft }
735 1.8 cgd
736 1.24 mycroft memcpy(buf, &bouncebuf[addr & CLOFSET], real_len);
737 1.24 mycroft last_pid = p->p_pid;
738 1.24 mycroft last_addr = addr & ~CLOFSET;
739 1.24 mycroft return real_len;
740 1.8 cgd }
741 1.8 cgd
742 1.8 cgd int
743 1.8 cgd kvm_procreadstr(p, addr, buf, len)
744 1.8 cgd const struct proc *p;
745 1.11 mycroft const unsigned addr;
746 1.11 mycroft char *buf;
747 1.8 cgd unsigned len;
748 1.8 cgd {
749 1.10 deraadt int done, little;
750 1.10 deraadt char copy[200], *pb;
751 1.11 mycroft char a;
752 1.8 cgd
753 1.8 cgd done = 0;
754 1.15 cgd copy[0] = '\0';
755 1.10 deraadt while (len) {
756 1.10 deraadt little = kvm_procread(p, addr+done, copy, MIN(len, sizeof copy));
757 1.10 deraadt if (little<1)
758 1.10 deraadt break;
759 1.10 deraadt pb = copy;
760 1.10 deraadt while (little--) {
761 1.10 deraadt len--;
762 1.11 mycroft if( (*buf++ = *pb++) == '\0' )
763 1.15 cgd return done;
764 1.15 cgd done++;
765 1.10 deraadt }
766 1.8 cgd }
767 1.8 cgd return done;
768 1.8 cgd }
769 1.8 cgd
770 1.1 cgd char *
771 1.1 cgd kvm_getargs(p, up)
772 1.1 cgd const struct proc *p;
773 1.1 cgd const struct user *up;
774 1.1 cgd {
775 1.24 mycroft static char *cmdbuf = NULL, ucomm[sizeof(p->p_comm) + 4];
776 1.8 cgd register char *cp, *acp;
777 1.8 cgd int left, rv;
778 1.8 cgd struct ps_strings arginfo;
779 1.1 cgd
780 1.24 mycroft if (cmdbuf == NULL) {
781 1.24 mycroft cmdbuf = (char *)malloc(ARG_MAX + sizeof(p->p_comm) + 5);
782 1.24 mycroft if (cmdbuf == NULL)
783 1.24 mycroft cmdbuf = ucomm;
784 1.24 mycroft }
785 1.24 mycroft
786 1.24 mycroft if (cmdbuf == ucomm || up == NULL || p->p_pid == 0 || p->p_pid == 2)
787 1.1 cgd goto retucomm;
788 1.8 cgd
789 1.11 mycroft if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
790 1.8 cgd sizeof(arginfo))
791 1.8 cgd goto bad;
792 1.8 cgd
793 1.15 cgd cmdbuf[0] = '\0';
794 1.8 cgd cp = cmdbuf;
795 1.8 cgd acp = arginfo.ps_argvstr;
796 1.8 cgd left = ARG_MAX + 1;
797 1.8 cgd while (arginfo.ps_nargvstr--) {
798 1.8 cgd if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
799 1.8 cgd acp += rv + 1;
800 1.8 cgd left -= rv + 1;
801 1.8 cgd cp += rv;
802 1.8 cgd *cp++ = ' ';
803 1.8 cgd *cp = '\0';
804 1.1 cgd } else
805 1.1 cgd goto bad;
806 1.1 cgd }
807 1.8 cgd cp-- ; *cp = '\0';
808 1.2 cgd
809 1.8 cgd if (cmdbuf[0] == '-' || cmdbuf[0] == '?' || cmdbuf[0] <= ' ') {
810 1.1 cgd (void) strcat(cmdbuf, " (");
811 1.1 cgd (void) strncat(cmdbuf, p->p_comm, sizeof(p->p_comm));
812 1.1 cgd (void) strcat(cmdbuf, ")");
813 1.1 cgd }
814 1.1 cgd return (cmdbuf);
815 1.1 cgd
816 1.1 cgd bad:
817 1.8 cgd seterr("error locating command name for pid %d", p->p_pid);
818 1.1 cgd retucomm:
819 1.8 cgd (void) strcpy(cmdbuf, "(");
820 1.1 cgd (void) strncat(cmdbuf, p->p_comm, sizeof (p->p_comm));
821 1.1 cgd (void) strcat(cmdbuf, ")");
822 1.1 cgd return (cmdbuf);
823 1.1 cgd }
824 1.1 cgd
825 1.8 cgd char *
826 1.8 cgd kvm_getenv(p, up)
827 1.8 cgd const struct proc *p;
828 1.8 cgd const struct user *up;
829 1.8 cgd {
830 1.24 mycroft static char *envbuf = NULL, emptyenv[1];
831 1.8 cgd register char *cp, *acp;
832 1.8 cgd int left, rv;
833 1.8 cgd struct ps_strings arginfo;
834 1.8 cgd
835 1.24 mycroft if (envbuf == NULL) {
836 1.24 mycroft envbuf = (char *)malloc(ARG_MAX + 1);
837 1.24 mycroft if (envbuf == NULL)
838 1.24 mycroft envbuf = emptyenv;
839 1.24 mycroft }
840 1.24 mycroft
841 1.24 mycroft if (envbuf == emptyenv || up == NULL || p->p_pid == 0 || p->p_pid == 2)
842 1.8 cgd goto retemptyenv;
843 1.8 cgd
844 1.11 mycroft if (kvm_procread(p, PS_STRINGS, (char *)&arginfo, sizeof(arginfo)) !=
845 1.8 cgd sizeof(arginfo))
846 1.8 cgd goto bad;
847 1.8 cgd
848 1.8 cgd cp = envbuf;
849 1.8 cgd acp = arginfo.ps_envstr;
850 1.8 cgd left = ARG_MAX + 1;
851 1.8 cgd while (arginfo.ps_nenvstr--) {
852 1.8 cgd if ((rv = kvm_procreadstr(p, acp, cp, left)) >= 0) {
853 1.8 cgd acp += rv + 1;
854 1.8 cgd left -= rv + 1;
855 1.8 cgd cp += rv;
856 1.8 cgd *cp++ = ' ';
857 1.8 cgd *cp = '\0';
858 1.8 cgd } else
859 1.8 cgd goto bad;
860 1.8 cgd }
861 1.8 cgd cp-- ; *cp = '\0';
862 1.8 cgd return (envbuf);
863 1.8 cgd
864 1.8 cgd bad:
865 1.8 cgd seterr("error locating environment for pid %d", p->p_pid);
866 1.8 cgd retemptyenv:
867 1.8 cgd envbuf[0] = '\0';
868 1.8 cgd return (envbuf);
869 1.8 cgd }
870 1.1 cgd
871 1.1 cgd static
872 1.1 cgd getkvars()
873 1.1 cgd {
874 1.1 cgd if (kvm_nlist(nl) == -1)
875 1.1 cgd return (-1);
876 1.1 cgd if (deadkernel) {
877 1.1 cgd /* We must do the sys map first because klseek uses it */
878 1.1 cgd long addr;
879 1.1 cgd
880 1.21 cgd #if defined(m68k)
881 1.25 chopps #if defined(amiga)
882 1.25 chopps addr = (long) nl[X_CPU040].n_value;
883 1.25 chopps (void) lseek(kmem, addr, 0);
884 1.25 chopps if (read(kmem, (char *) &cpu040, sizeof (cpu040))
885 1.25 chopps != sizeof (cpu040)) {
886 1.25 chopps seterr("can't read cpu040");
887 1.25 chopps return (-1);
888 1.25 chopps }
889 1.25 chopps #endif
890 1.1 cgd addr = (long) nl[X_LOWRAM].n_value;
891 1.1 cgd (void) lseek(kmem, addr, 0);
892 1.1 cgd if (read(kmem, (char *) &lowram, sizeof (lowram))
893 1.1 cgd != sizeof (lowram)) {
894 1.1 cgd seterr("can't read lowram");
895 1.1 cgd return (-1);
896 1.1 cgd }
897 1.1 cgd lowram = btop(lowram);
898 1.1 cgd Sysseg = (struct ste *) malloc(NBPG);
899 1.1 cgd if (Sysseg == NULL) {
900 1.1 cgd seterr("out of space for Sysseg");
901 1.1 cgd return (-1);
902 1.1 cgd }
903 1.1 cgd addr = (long) nl[X_SYSSEG].n_value;
904 1.1 cgd (void) lseek(kmem, addr, 0);
905 1.1 cgd read(kmem, (char *)&addr, sizeof(addr));
906 1.1 cgd (void) lseek(kmem, (long)addr, 0);
907 1.1 cgd if (read(kmem, (char *) Sysseg, NBPG) != NBPG) {
908 1.1 cgd seterr("can't read Sysseg");
909 1.1 cgd return (-1);
910 1.1 cgd }
911 1.1 cgd #endif
912 1.1 cgd #if defined(i386)
913 1.1 cgd PTD = (struct pde *) malloc(NBPG);
914 1.1 cgd if (PTD == NULL) {
915 1.1 cgd seterr("out of space for PTD");
916 1.1 cgd return (-1);
917 1.1 cgd }
918 1.1 cgd addr = (long) nl[X_IdlePTD].n_value;
919 1.1 cgd (void) lseek(kmem, addr, 0);
920 1.1 cgd read(kmem, (char *)&addr, sizeof(addr));
921 1.1 cgd (void) lseek(kmem, (long)addr, 0);
922 1.1 cgd if (read(kmem, (char *) PTD, NBPG) != NBPG) {
923 1.1 cgd seterr("can't read PTD");
924 1.1 cgd return (-1);
925 1.1 cgd }
926 1.1 cgd #endif
927 1.1 cgd }
928 1.19 mycroft if (kvm_read((void *) nl[X_NSWAP].n_value, &nswap, sizeof (long)) == -1) {
929 1.1 cgd seterr("can't read nswap");
930 1.1 cgd return (-1);
931 1.1 cgd }
932 1.19 mycroft if (kvm_read((void *) nl[X_DMMIN].n_value, &dmmin, sizeof (long)) == -1) {
933 1.1 cgd seterr("can't read dmmin");
934 1.1 cgd return (-1);
935 1.1 cgd }
936 1.19 mycroft if (kvm_read((void *) nl[X_DMMAX].n_value, &dmmax, sizeof (long)) == -1) {
937 1.1 cgd seterr("can't read dmmax");
938 1.1 cgd return (-1);
939 1.1 cgd }
940 1.19 mycroft if (kvm_read((void *) nl[X_VM_PAGE_HASH_MASK].n_value,
941 1.19 mycroft &vm_page_hash_mask, sizeof (long)) == -1) {
942 1.19 mycroft seterr("can't read vm_page_hash_mask");
943 1.19 mycroft return (-1);
944 1.19 mycroft }
945 1.19 mycroft if (kvm_read((void *) nl[X_VM_PAGE_BUCKETS].n_value,
946 1.19 mycroft &vm_page_buckets, sizeof (long)) == -1) {
947 1.19 mycroft seterr("can't read vm_page_buckets");
948 1.19 mycroft return (-1);
949 1.19 mycroft }
950 1.19 mycroft if (kvm_read((void *) nl[X_PAGE_SHIFT].n_value,
951 1.19 mycroft &page_shift, sizeof (long)) == -1) {
952 1.19 mycroft seterr("can't read page_shift");
953 1.19 mycroft return (-1);
954 1.19 mycroft }
955 1.19 mycroft
956 1.1 cgd return (0);
957 1.1 cgd }
958 1.1 cgd
959 1.1 cgd kvm_read(loc, buf, len)
960 1.1 cgd void *loc;
961 1.1 cgd void *buf;
962 1.1 cgd {
963 1.1 cgd if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
964 1.1 cgd return (-1);
965 1.19 mycroft klseek(kmem, (off_t) loc, 0);
966 1.19 mycroft if (read(kmem, buf, len) != len) {
967 1.19 mycroft seterr("error reading kmem at %x", loc);
968 1.19 mycroft return (-1);
969 1.1 cgd }
970 1.1 cgd return (len);
971 1.1 cgd }
972 1.1 cgd
973 1.1 cgd static void
974 1.1 cgd klseek(fd, loc, off)
975 1.1 cgd int fd;
976 1.1 cgd off_t loc;
977 1.1 cgd int off;
978 1.1 cgd {
979 1.1 cgd
980 1.1 cgd if (deadkernel) {
981 1.1 cgd if ((loc = Vtophys(loc)) == -1)
982 1.1 cgd return;
983 1.1 cgd }
984 1.1 cgd (void) lseek(fd, (off_t)loc, off);
985 1.1 cgd }
986 1.1 cgd
987 1.1 cgd static off_t
988 1.1 cgd Vtophys(loc)
989 1.1 cgd u_long loc;
990 1.1 cgd {
991 1.1 cgd off_t newloc = (off_t) -1;
992 1.21 cgd #if defined(m68k)
993 1.1 cgd int p, ste, pte;
994 1.1 cgd
995 1.16 mycroft ste = *(int *)&Sysseg[btos(loc)];
996 1.1 cgd if ((ste & SG_V) == 0) {
997 1.1 cgd seterr("vtophys: segment not valid");
998 1.1 cgd return((off_t) -1);
999 1.1 cgd }
1000 1.1 cgd p = btop(loc & SG_PMASK);
1001 1.1 cgd newloc = (ste & SG_FRAME) + (p * sizeof(struct pte));
1002 1.16 mycroft (void) lseek(mem, newloc, 0);
1003 1.16 mycroft if (read(mem, (char *)&pte, sizeof pte) != sizeof pte) {
1004 1.1 cgd seterr("vtophys: cannot locate pte");
1005 1.1 cgd return((off_t) -1);
1006 1.1 cgd }
1007 1.1 cgd newloc = pte & PG_FRAME;
1008 1.1 cgd if (pte == PG_NV || newloc < (off_t)ptob(lowram)) {
1009 1.1 cgd seterr("vtophys: page not valid");
1010 1.1 cgd return((off_t) -1);
1011 1.1 cgd }
1012 1.1 cgd newloc = (newloc - (off_t)ptob(lowram)) + (loc & PGOFSET);
1013 1.1 cgd #endif
1014 1.1 cgd #ifdef i386
1015 1.1 cgd struct pde pde;
1016 1.1 cgd struct pte pte;
1017 1.1 cgd int p;
1018 1.1 cgd
1019 1.22 mycroft pde = PTD[loc >> PDSHIFT];
1020 1.1 cgd if (pde.pd_v == 0) {
1021 1.1 cgd seterr("vtophys: page directory entry not valid");
1022 1.1 cgd return((off_t) -1);
1023 1.1 cgd }
1024 1.1 cgd p = btop(loc & PT_MASK);
1025 1.1 cgd newloc = pde.pd_pfnum + (p * sizeof(struct pte));
1026 1.1 cgd (void) lseek(kmem, (long)newloc, 0);
1027 1.1 cgd if (read(kmem, (char *)&pte, sizeof pte) != sizeof pte) {
1028 1.1 cgd seterr("vtophys: cannot obtain desired pte");
1029 1.1 cgd return((off_t) -1);
1030 1.1 cgd }
1031 1.1 cgd newloc = pte.pg_pfnum;
1032 1.1 cgd if (pte.pg_v == 0) {
1033 1.1 cgd seterr("vtophys: page table entry not valid");
1034 1.1 cgd return((off_t) -1);
1035 1.1 cgd }
1036 1.1 cgd newloc += (loc & PGOFSET);
1037 1.1 cgd #endif
1038 1.1 cgd return((off_t) newloc);
1039 1.1 cgd }
1040 1.1 cgd
1041 1.3 cgd /*
1042 1.3 cgd * locate address of unwired or swapped page
1043 1.3 cgd */
1044 1.3 cgd
1045 1.3 cgd static int
1046 1.19 mycroft vatosw(mp, vaddr, maddr, swb)
1047 1.19 mycroft vm_map_t mp;
1048 1.3 cgd vm_offset_t vaddr;
1049 1.3 cgd vm_offset_t *maddr;
1050 1.3 cgd struct swapblk *swb;
1051 1.3 cgd {
1052 1.3 cgd struct vm_object vm_object;
1053 1.3 cgd struct vm_map_entry vm_entry;
1054 1.19 mycroft long saddr, addr, off;
1055 1.3 cgd int i;
1056 1.3 cgd
1057 1.19 mycroft saddr = addr = (long)mp->header.next;
1058 1.19 mycroft #ifdef DEBUG
1059 1.19 mycroft fprintf(stderr, "vatosw: head=%x\n", &mp->header);
1060 1.19 mycroft #endif
1061 1.19 mycroft for (i = 0; ; i++) {
1062 1.3 cgd /* Weed through map entries until vaddr in range */
1063 1.19 mycroft if (kvm_read((void *) addr, &vm_entry, sizeof(vm_entry)) == -1) {
1064 1.3 cgd setsyserr("vatosw: read vm_map_entry");
1065 1.3 cgd return 0;
1066 1.3 cgd }
1067 1.19 mycroft #ifdef DEBUG
1068 1.19 mycroft fprintf(stderr, "vatosw: %d/%d, vaddr=%x, start=%x, end=%x ",
1069 1.19 mycroft i, mp->nentries, vaddr, vm_entry.start, vm_entry.end);
1070 1.19 mycroft fprintf(stderr, "addr=%x, next=%x\n", addr, vm_entry.next);
1071 1.18 mycroft #endif
1072 1.18 mycroft if ((vaddr >= vm_entry.start) && (vaddr < vm_entry.end))
1073 1.18 mycroft if (vm_entry.object.vm_object != 0)
1074 1.18 mycroft break;
1075 1.18 mycroft else {
1076 1.19 mycroft #ifdef DEBUG
1077 1.19 mycroft fprintf(stderr, "vatosw: no object\n");
1078 1.18 mycroft #endif
1079 1.19 mycroft seterr("vatosw: no object\n");
1080 1.18 mycroft return 0;
1081 1.18 mycroft }
1082 1.3 cgd
1083 1.3 cgd addr = (long)vm_entry.next;
1084 1.19 mycroft
1085 1.19 mycroft if (addr == saddr) {
1086 1.19 mycroft seterr("vatosw: map not found\n");
1087 1.19 mycroft return 0;
1088 1.19 mycroft }
1089 1.3 cgd }
1090 1.3 cgd
1091 1.3 cgd if (vm_entry.is_a_map || vm_entry.is_sub_map) {
1092 1.19 mycroft #ifdef DEBUG
1093 1.19 mycroft fprintf(stderr, "vatosw: is a %smap\n",
1094 1.18 mycroft vm_entry.is_sub_map ? "sub " : "");
1095 1.18 mycroft #endif
1096 1.19 mycroft seterr("vatosw: is a %smap\n",
1097 1.19 mycroft vm_entry.is_sub_map ? "sub " : "");
1098 1.3 cgd return 0;
1099 1.3 cgd }
1100 1.3 cgd
1101 1.3 cgd /* Locate memory object */
1102 1.3 cgd off = (vaddr - vm_entry.start) + vm_entry.offset;
1103 1.3 cgd addr = (long)vm_entry.object.vm_object;
1104 1.3 cgd while (1) {
1105 1.19 mycroft if (kvm_read((void *) addr, &vm_object, sizeof (vm_object)) == -1) {
1106 1.3 cgd setsyserr("vatosw: read vm_object");
1107 1.3 cgd return 0;
1108 1.3 cgd }
1109 1.3 cgd
1110 1.19 mycroft #ifdef DEBUG
1111 1.19 mycroft fprintf(stderr, "vatosw: find page: object %#x offset %x\n",
1112 1.19 mycroft addr, off);
1113 1.3 cgd #endif
1114 1.3 cgd
1115 1.3 cgd /* Lookup in page queue */
1116 1.18 mycroft if ((i = findpage(addr, off, maddr)) != -1)
1117 1.18 mycroft return i;
1118 1.18 mycroft
1119 1.18 mycroft if (vm_object.pager != 0 &&
1120 1.18 mycroft (i = pager_get(&vm_object, off, swb)) != -1)
1121 1.18 mycroft return i;
1122 1.3 cgd
1123 1.18 mycroft if (vm_object.shadow == 0)
1124 1.3 cgd break;
1125 1.3 cgd
1126 1.19 mycroft #ifdef DEBUG
1127 1.19 mycroft fprintf(stderr, "vatosw: shadow obj at %x: offset %x+%x\n",
1128 1.19 mycroft addr, off, vm_object.shadow_offset);
1129 1.3 cgd #endif
1130 1.3 cgd
1131 1.3 cgd addr = (long)vm_object.shadow;
1132 1.3 cgd off += vm_object.shadow_offset;
1133 1.3 cgd }
1134 1.3 cgd
1135 1.19 mycroft seterr("vatosw: page not found\n");
1136 1.18 mycroft return 0;
1137 1.18 mycroft }
1138 1.18 mycroft
1139 1.18 mycroft
1140 1.18 mycroft int
1141 1.18 mycroft pager_get(object, off, swb)
1142 1.18 mycroft struct vm_object *object;
1143 1.18 mycroft long off;
1144 1.18 mycroft struct swapblk *swb;
1145 1.18 mycroft {
1146 1.18 mycroft struct pager_struct pager;
1147 1.18 mycroft struct swpager swpager;
1148 1.18 mycroft struct swblock swblock;
1149 1.18 mycroft
1150 1.3 cgd /* Find address in swap space */
1151 1.19 mycroft if (kvm_read(object->pager, &pager, sizeof (pager)) == -1) {
1152 1.18 mycroft setsyserr("pager_get: read pager");
1153 1.3 cgd return 0;
1154 1.3 cgd }
1155 1.3 cgd if (pager.pg_type != PG_SWAP) {
1156 1.18 mycroft seterr("pager_get: weird pager\n");
1157 1.3 cgd return 0;
1158 1.3 cgd }
1159 1.3 cgd
1160 1.3 cgd /* Get swap pager data */
1161 1.19 mycroft if (kvm_read(pager.pg_data, &swpager, sizeof (swpager)) == -1) {
1162 1.18 mycroft setsyserr("pager_get: read swpager");
1163 1.3 cgd return 0;
1164 1.3 cgd }
1165 1.3 cgd
1166 1.18 mycroft off += object->paging_offset;
1167 1.3 cgd
1168 1.3 cgd /* Read swap block array */
1169 1.19 mycroft if (kvm_read((void *) swpager.sw_blocks +
1170 1.3 cgd (off/dbtob(swpager.sw_bsize)) * sizeof swblock,
1171 1.19 mycroft &swblock, sizeof (swblock)) == -1) {
1172 1.18 mycroft setsyserr("pager_get: read swblock");
1173 1.3 cgd return 0;
1174 1.3 cgd }
1175 1.18 mycroft
1176 1.18 mycroft off %= dbtob(swpager.sw_bsize);
1177 1.3 cgd
1178 1.18 mycroft if (swblock.swb_mask & (1 << atop(off))) {
1179 1.18 mycroft swb->offset = dbtob(swblock.swb_block) + off;
1180 1.18 mycroft swb->size = dbtob(swpager.sw_bsize) - off;
1181 1.18 mycroft return 1;
1182 1.18 mycroft }
1183 1.3 cgd
1184 1.18 mycroft return -1;
1185 1.18 mycroft }
1186 1.3 cgd
1187 1.3 cgd static int
1188 1.3 cgd findpage(object, offset, maddr)
1189 1.3 cgd long object;
1190 1.3 cgd long offset;
1191 1.3 cgd vm_offset_t *maddr;
1192 1.3 cgd {
1193 1.3 cgd queue_head_t bucket;
1194 1.3 cgd struct vm_page mem;
1195 1.3 cgd long addr, baddr;
1196 1.3 cgd
1197 1.19 mycroft baddr = vm_page_buckets +
1198 1.19 mycroft vm_page_hash(object,offset) * sizeof(queue_head_t);
1199 1.3 cgd
1200 1.19 mycroft if (kvm_read((void *) baddr, &bucket, sizeof (bucket)) == -1) {
1201 1.3 cgd seterr("can't read vm_page_bucket");
1202 1.3 cgd return 0;
1203 1.3 cgd }
1204 1.3 cgd
1205 1.3 cgd addr = (long)bucket.next;
1206 1.19 mycroft
1207 1.3 cgd while (addr != baddr) {
1208 1.19 mycroft if (kvm_read((void *) addr, &mem, sizeof (mem)) == -1) {
1209 1.3 cgd seterr("can't read vm_page");
1210 1.3 cgd return 0;
1211 1.3 cgd }
1212 1.19 mycroft
1213 1.3 cgd if ((long)mem.object == object && mem.offset == offset) {
1214 1.3 cgd *maddr = (long)mem.phys_addr;
1215 1.3 cgd return 1;
1216 1.3 cgd }
1217 1.19 mycroft
1218 1.3 cgd addr = (long)mem.hashq.next;
1219 1.3 cgd }
1220 1.18 mycroft
1221 1.18 mycroft return -1;
1222 1.3 cgd }
1223 1.3 cgd
1224 1.1 cgd #include <varargs.h>
1225 1.1 cgd static char errbuf[_POSIX2_LINE_MAX];
1226 1.1 cgd
1227 1.1 cgd static void
1228 1.1 cgd seterr(va_alist)
1229 1.1 cgd va_dcl
1230 1.1 cgd {
1231 1.1 cgd char *fmt;
1232 1.1 cgd va_list ap;
1233 1.1 cgd
1234 1.1 cgd va_start(ap);
1235 1.1 cgd fmt = va_arg(ap, char *);
1236 1.1 cgd (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
1237 1.19 mycroft #ifdef DEBUG
1238 1.19 mycroft (void) printf("%s", errbuf);
1239 1.3 cgd #endif
1240 1.1 cgd va_end(ap);
1241 1.1 cgd }
1242 1.1 cgd
1243 1.1 cgd static void
1244 1.1 cgd setsyserr(va_alist)
1245 1.1 cgd va_dcl
1246 1.1 cgd {
1247 1.1 cgd char *fmt, *cp;
1248 1.1 cgd va_list ap;
1249 1.1 cgd extern int errno;
1250 1.1 cgd
1251 1.1 cgd va_start(ap);
1252 1.1 cgd fmt = va_arg(ap, char *);
1253 1.20 mycroft (void) vsnprintf(cp = errbuf, _POSIX2_LINE_MAX, fmt, ap);
1254 1.19 mycroft cp += strlen(cp);
1255 1.19 mycroft (void) snprintf(cp, _POSIX2_LINE_MAX - (cp - errbuf), ": %s",
1256 1.19 mycroft strerror(errno));
1257 1.19 mycroft #ifdef DEBUG
1258 1.19 mycroft (void) printf("%s", errbuf);
1259 1.19 mycroft #endif
1260 1.1 cgd va_end(ap);
1261 1.1 cgd }
1262 1.1 cgd
1263 1.1 cgd char *
1264 1.1 cgd kvm_geterr()
1265 1.1 cgd {
1266 1.1 cgd return (errbuf);
1267 1.1 cgd }
1268