amd64 port: mainly on the pmap headers, identify_cpu and initcpu
[dragonfly/port-amd64.git] / lib / libkvm / kvm.c
blob3660c99c79557a98cf886c54912df441234ab391
1 /*-
2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * @(#)kvm.c 8.2 (Berkeley) 2/13/94
38 * $FreeBSD: src/lib/libkvm/kvm.c,v 1.12.2.3 2002/09/13 14:53:43 nectar Exp $
39 * $DragonFly: src/lib/libkvm/kvm.c,v 1.10 2007/04/29 01:36:04 dillon Exp $
42 #include <sys/user.h> /* MUST BE FIRST */
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/sysctl.h>
48 #include <sys/linker.h>
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #include <vm/swap_pager.h>
54 #include <machine/vmparam.h>
56 #include <ctype.h>
57 #include <fcntl.h>
58 #include <kvm.h>
59 #include <limits.h>
60 #include <nlist.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <stdarg.h>
66 #include <unistd.h>
68 #include "kvm_private.h"
70 /* from src/lib/libc/gen/nlist.c */
71 int __fdnlist (int, struct nlist *);
73 char *
74 kvm_geterr(kvm_t *kd)
76 return (kd->errbuf);
80 * Report an error using printf style arguments. "program" is kd->program
81 * on hard errors, and 0 on soft errors, so that under sun error emulation,
82 * only hard errors are printed out (otherwise, programs like gdb will
83 * generate tons of error messages when trying to access bogus pointers).
85 void
86 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
88 va_list ap;
90 va_start(ap, fmt);
91 if (program != NULL) {
92 (void)fprintf(stderr, "%s: ", program);
93 (void)vfprintf(stderr, fmt, ap);
94 (void)fputc('\n', stderr);
95 } else
96 (void)vsnprintf(kd->errbuf,
97 sizeof(kd->errbuf), (char *)fmt, ap);
99 va_end(ap);
102 void
103 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
105 va_list ap;
106 int n;
108 va_start(ap, fmt);
109 if (program != NULL) {
110 (void)fprintf(stderr, "%s: ", program);
111 (void)vfprintf(stderr, fmt, ap);
112 (void)fprintf(stderr, ": %s\n", strerror(errno));
113 } else {
114 char *cp = kd->errbuf;
116 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
117 n = strlen(cp);
118 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
119 strerror(errno));
121 va_end(ap);
124 void *
125 _kvm_malloc(kvm_t *kd, size_t n)
127 void *p;
129 if ((p = calloc(n, sizeof(char))) == NULL)
130 _kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
131 n, strerror(errno));
132 return (p);
135 static kvm_t *
136 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
138 struct stat st;
140 kd->vmfd = -1;
141 kd->pmfd = -1;
142 kd->nlfd = -1;
143 kd->vmst = 0;
144 kd->procbase = NULL;
145 kd->procend = NULL;
146 kd->argspc = 0;
147 kd->argv = 0;
149 if (uf == 0)
150 uf = getbootfile();
151 else if (strlen(uf) >= MAXPATHLEN) {
152 _kvm_err(kd, kd->program, "exec file name too long");
153 goto failed;
155 if (flag & ~O_RDWR) {
156 _kvm_err(kd, kd->program, "bad flags arg");
157 goto failed;
159 if (mf == 0)
160 mf = _PATH_MEM;
162 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
163 _kvm_syserr(kd, kd->program, "%s", mf);
164 goto failed;
166 if (fstat(kd->pmfd, &st) < 0) {
167 _kvm_syserr(kd, kd->program, "%s", mf);
168 goto failed;
170 if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
171 _kvm_syserr(kd, kd->program, "%s", mf);
172 goto failed;
174 if (S_ISCHR(st.st_mode)) {
176 * If this is a character special device, then check that
177 * it's /dev/mem. If so, open kmem too. (Maybe we should
178 * make it work for either /dev/mem or /dev/kmem -- in either
179 * case you're working with a live kernel.)
181 if (strcmp(mf, _PATH_DEVNULL) == 0) {
182 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
183 } else if (strcmp(mf, _PATH_MEM) != 0) {
184 _kvm_err(kd, kd->program,
185 "%s: not physical memory device", mf);
186 goto failed;
187 } else {
188 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
189 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
190 goto failed;
192 if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
193 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
194 goto failed;
197 } else {
199 * This is a crash dump.
200 * Initialize the virtual address translation machinery,
201 * but first setup the namelist fd.
203 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
204 _kvm_syserr(kd, kd->program, "%s", uf);
205 goto failed;
207 if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
208 _kvm_syserr(kd, kd->program, "%s", uf);
209 goto failed;
211 if (_kvm_initvtop(kd) < 0)
212 goto failed;
214 return (kd);
215 failed:
217 * Copy out the error if doing sane error semantics.
219 if (errout != 0)
220 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
221 (void)kvm_close(kd);
222 return (0);
225 kvm_t *
226 kvm_openfiles(const char *uf, const char *mf, const char *sf, int flag,
227 char *errout)
229 kvm_t *kd;
231 if ((kd = malloc(sizeof(*kd))) == NULL) {
232 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
233 return (0);
235 memset(kd, 0, sizeof(*kd));
236 kd->program = 0;
237 return (_kvm_open(kd, uf, mf, flag, errout));
240 kvm_t *
241 kvm_open(const char *uf, const char *mf, const char *sf, int flag,
242 const char *errstr)
244 kvm_t *kd;
246 if ((kd = malloc(sizeof(*kd))) == NULL) {
247 if (errstr != NULL)
248 (void)fprintf(stderr, "%s: %s\n",
249 errstr, strerror(errno));
250 return (0);
252 memset(kd, 0, sizeof(*kd));
253 kd->program = errstr;
254 return (_kvm_open(kd, uf, mf, flag, NULL));
258 kvm_close(kvm_t *kd)
260 int error = 0;
262 if (kd->pmfd >= 0)
263 error |= close(kd->pmfd);
264 if (kd->vmfd >= 0)
265 error |= close(kd->vmfd);
266 if (kd->nlfd >= 0)
267 error |= close(kd->nlfd);
268 if (kd->vmst)
269 _kvm_freevtop(kd);
270 if (kd->procbase != NULL)
271 free(kd->procbase);
272 if (kd->argv != 0)
273 free((void *)kd->argv);
274 free((void *)kd);
276 return (0);
280 kvm_nlist(kvm_t *kd, struct nlist *nl)
282 struct nlist *p;
283 int nvalid;
284 struct kld_sym_lookup lookup;
287 * If we can't use the kld symbol lookup, revert to the
288 * slow library call.
290 if (!ISALIVE(kd))
291 return (__fdnlist(kd->nlfd, nl));
294 * We can use the kld lookup syscall. Go through each nlist entry
295 * and look it up with a kldsym(2) syscall.
297 nvalid = 0;
298 for (p = nl; p->n_name && p->n_name[0]; ++p) {
299 lookup.version = sizeof(lookup);
300 lookup.symname = p->n_name;
301 lookup.symvalue = 0;
302 lookup.symsize = 0;
304 if (lookup.symname[0] == '_')
305 lookup.symname++;
307 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
308 p->n_type = N_TEXT;
309 p->n_other = 0;
310 p->n_desc = 0;
311 p->n_value = lookup.symvalue;
312 ++nvalid;
313 /* lookup.symsize */
317 * Return the number of entries that weren't found.
319 return ((p - nl) - nvalid);
322 ssize_t
323 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
325 int cc;
326 void *cp;
328 if (ISALIVE(kd)) {
330 * We're using /dev/kmem. Just read straight from the
331 * device and let the active kernel do the address translation.
333 errno = 0;
334 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
335 _kvm_err(kd, 0, "invalid address (%x)", kva);
336 return (-1);
340 * Try to pre-fault the user memory to reduce instances of
341 * races within the kernel. XXX workaround for kernel bug
342 * where kernel does a sanity check, but user faults during
343 * the copy can block and race against another kernel entity
344 * unmapping the memory in question.
346 bzero(buf, len);
347 cc = read(kd->vmfd, buf, len);
348 if (cc < 0) {
349 _kvm_syserr(kd, 0, "kvm_read");
350 return (-1);
351 } else if (cc < len)
352 _kvm_err(kd, kd->program, "short read");
353 return (cc);
354 } else {
355 cp = buf;
356 while (len > 0) {
357 u_long pa;
359 cc = _kvm_kvatop(kd, kva, &pa);
360 if (cc == 0)
361 return (-1);
362 if (cc > len)
363 cc = len;
364 errno = 0;
365 if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
366 _kvm_syserr(kd, 0, _PATH_MEM);
367 break;
369 bzero(cp, cc);
370 cc = read(kd->pmfd, cp, cc);
371 if (cc < 0) {
372 _kvm_syserr(kd, kd->program, "kvm_read");
373 break;
376 * If kvm_kvatop returns a bogus value or our core
377 * file is truncated, we might wind up seeking beyond
378 * the end of the core file in which case the read will
379 * return 0 (EOF).
381 if (cc == 0)
382 break;
383 cp = (char *)cp + cc;
384 kva += cc;
385 len -= cc;
387 return ((char *)cp - (char *)buf);
389 /* NOTREACHED */
392 char *
393 kvm_readstr(kvm_t *kd, u_long kva, char *buf, size_t *lenp)
395 size_t len, cc, pos;
396 char ch;
397 int asize = -1;
399 if (buf == NULL) {
400 asize = len = 16;
401 buf = malloc(len);
402 if (buf == NULL) {
403 _kvm_syserr(kd, kd->program, "kvm_readstr");
404 return NULL;
406 } else {
407 len = *lenp;
410 if (ISALIVE(kd)) {
412 * We're using /dev/kmem. Just read straight from the
413 * device and let the active kernel do the address translation.
415 errno = 0;
416 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
417 _kvm_err(kd, 0, "invalid address (%x)", kva);
418 return NULL;
421 for (pos = 0, ch = -1; ch != 0; pos++) {
422 cc = read(kd->vmfd, &ch, 1);
423 if ((ssize_t)cc < 0) {
424 _kvm_syserr(kd, 0, "kvm_readstr");
425 return NULL;
426 } else if (cc < 1)
427 _kvm_err(kd, kd->program, "short read");
428 if (pos == asize) {
429 buf = realloc(buf, asize *= 2);
430 if (buf == NULL) {
431 _kvm_syserr(kd, kd->program, "kvm_readstr");
432 return NULL;
434 len = asize;
436 if (pos < len)
437 buf[pos] = ch;
440 if (lenp != NULL)
441 *lenp = pos;
442 if (pos > len)
443 return NULL;
444 else
445 return buf;
446 } else {
447 size_t left = 0;
448 for (pos = 0, ch = -1; ch != 0; pos++, left--, kva++) {
449 if (left == 0) {
450 u_long pa;
452 left = _kvm_kvatop(kd, kva, &pa);
453 if (left == 0)
454 return NULL;
455 errno = 0;
456 if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
457 _kvm_syserr(kd, 0, _PATH_MEM);
458 return NULL;
461 cc = read(kd->vmfd, &ch, 1);
462 if ((ssize_t)cc < 0) {
463 _kvm_syserr(kd, 0, "kvm_readstr");
464 return NULL;
465 } else if (cc < 1)
466 _kvm_err(kd, kd->program, "short read");
467 if (pos == asize) {
468 buf = realloc(buf, asize *= 2);
469 if (buf == NULL) {
470 _kvm_syserr(kd, kd->program, "kvm_readstr");
471 return NULL;
473 len = asize;
475 if (pos < len)
476 buf[pos] = ch;
479 if (lenp != NULL)
480 *lenp = pos;
481 if (pos > len)
482 return NULL;
483 else
484 return buf;
486 /* NOTREACHED */
489 ssize_t
490 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
492 int cc;
494 if (ISALIVE(kd)) {
496 * Just like kvm_read, only we write.
498 errno = 0;
499 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
500 _kvm_err(kd, 0, "invalid address (%x)", kva);
501 return (-1);
503 cc = write(kd->vmfd, buf, len);
504 if (cc < 0) {
505 _kvm_syserr(kd, 0, "kvm_write");
506 return (-1);
507 } else if (cc < len)
508 _kvm_err(kd, kd->program, "short write");
509 return (cc);
510 } else {
511 _kvm_err(kd, kd->program,
512 "kvm_write not implemented for dead kernels");
513 return (-1);
515 /* NOTREACHED */