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
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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
41 #endif /* LIBC_SCCS and not lint */
43 #include <sys/param.h>
49 #include <sys/ioctl.h>
51 #include <sys/sysctl.h>
52 #include <sys/linker.h>
58 #include <vm/vm_param.h>
60 #include <machine/vmparam.h>
74 #include "kvm_private.h"
78 /* from src/lib/libc/gen/nlist.c */
79 int __fdnlist(int, struct nlist *);
81 #define kvm_fdnlist __fdnlist
85 #include <proc_service.h>
88 kvm_fdnlist(int fd, struct nlist *list)
95 while (list->n_name != NULL && list->n_name[0] != '\0') {
98 pserr = ps_pglobal_lookup(NULL, NULL, list->n_name, &addr);
104 list->n_value = addr;
105 list->n_type = N_DATA | N_EXT;
112 #endif /* CROSS_LIBKVM */
115 kvm_geterr(kvm_t *kd)
123 * Report an error using printf style arguments. "program" is kd->program
124 * on hard errors, and 0 on soft errors, so that under sun error emulation,
125 * only hard errors are printed out (otherwise, programs like gdb will
126 * generate tons of error messages when trying to access bogus pointers).
129 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
134 if (program != NULL) {
135 (void)fprintf(stderr, "%s: ", program);
136 (void)vfprintf(stderr, fmt, ap);
137 (void)fputc('\n', stderr);
139 (void)vsnprintf(kd->errbuf,
140 sizeof(kd->errbuf), fmt, ap);
146 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
152 if (program != NULL) {
153 (void)fprintf(stderr, "%s: ", program);
154 (void)vfprintf(stderr, fmt, ap);
155 (void)fprintf(stderr, ": %s\n", strerror(errno));
157 char *cp = kd->errbuf;
159 (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
161 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
168 _kvm_malloc(kvm_t *kd, size_t n)
172 if ((p = calloc(n, sizeof(char))) == NULL)
173 _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
179 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
193 else if (strlen(uf) >= MAXPATHLEN) {
194 _kvm_err(kd, kd->program, "exec file name too long");
197 if (flag & ~O_RDWR) {
198 _kvm_err(kd, kd->program, "bad flags arg");
204 if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
205 _kvm_syserr(kd, kd->program, "%s", mf);
208 if (fstat(kd->pmfd, &st) < 0) {
209 _kvm_syserr(kd, kd->program, "%s", mf);
212 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
214 _kvm_syserr(kd, kd->program, "empty file");
217 if (S_ISCHR(st.st_mode)) {
219 * If this is a character special device, then check that
220 * it's /dev/mem. If so, open kmem too. (Maybe we should
221 * make it work for either /dev/mem or /dev/kmem -- in either
222 * case you're working with a live kernel.)
224 if (strcmp(mf, _PATH_DEVNULL) == 0) {
225 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
227 } else if (strcmp(mf, _PATH_MEM) == 0) {
228 if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
230 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
237 * This is a crash dump.
238 * Initialize the virtual address translation machinery,
239 * but first setup the namelist fd.
241 if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
242 _kvm_syserr(kd, kd->program, "%s", uf);
245 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
247 if (_kvm_initvtop(kd) < 0)
252 * Copy out the error if doing sane error semantics.
255 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
261 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
266 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
267 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
271 return (_kvm_open(kd, uf, mf, flag, errout));
275 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
280 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
282 (void)fprintf(stderr, "%s: %s\n",
283 errstr, strerror(errno));
286 kd->program = errstr;
287 return (_kvm_open(kd, uf, mf, flag, NULL));
296 error |= close(kd->pmfd);
298 error |= close(kd->vmfd);
300 error |= close(kd->nlfd);
303 if (kd->procbase != 0)
304 free((void *)kd->procbase);
306 free((void *) kd->argbuf);
308 free((void *) kd->argspc);
310 free((void *)kd->argv);
317 * Walk the list of unresolved symbols, generate a new list and prefix the
318 * symbol names, try again, and merge back what we could resolve.
321 kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
322 uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
324 struct nlist *n, *np, *p;
328 int slen, unresolved;
331 * Calculate the space we need to malloc for nlist and names.
332 * We are going to store the name twice for later lookups: once
333 * with the prefix and once the unmodified name delmited by \0.
337 for (p = nl; p->n_name && p->n_name[0]; ++p) {
338 if (p->n_type != N_UNDF)
340 len += sizeof(struct nlist) + strlen(prefix) +
341 2 * (strlen(p->n_name) + 1);
346 /* Add space for the terminating nlist entry. */
347 len += sizeof(struct nlist);
350 /* Alloc one chunk for (nlist, [names]) and setup pointers. */
351 n = np = malloc(len);
355 cp = ce = (char *)np;
356 cp += unresolved * sizeof(struct nlist);
359 /* Generate shortened nlist with special prefix. */
361 for (p = nl; p->n_name && p->n_name[0]; ++p) {
362 if (p->n_type != N_UNDF)
364 bcopy(p, np, sizeof(struct nlist));
365 /* Save the new\0orig. name so we can later match it again. */
366 slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
367 (prefix[0] != '\0' && p->n_name[0] == '_') ?
368 (p->n_name + 1) : p->n_name, '\0', p->n_name);
369 if (slen < 0 || slen >= ce - cp)
377 /* Do lookup on the reduced list. */
379 unresolved = kvm_fdnlist(kd->nlfd, np);
381 /* Check if we could resolve further symbols and update the list. */
382 if (unresolved >= 0 && unresolved < missing) {
383 /* Find the first freshly resolved entry. */
384 for (; np->n_name && np->n_name[0]; np++)
385 if (np->n_type != N_UNDF)
388 * The lists are both in the same order,
389 * so we can walk them in parallel.
391 for (p = nl; np->n_name && np->n_name[0] &&
392 p->n_name && p->n_name[0]; ++p) {
393 if (p->n_type != N_UNDF)
395 /* Skip expanded name and compare to orig. one. */
396 ccp = np->n_name + strlen(np->n_name) + 1;
397 if (strcmp(ccp, p->n_name) != 0)
399 /* Update nlist with new, translated results. */
400 p->n_type = np->n_type;
401 p->n_other = np->n_other;
402 p->n_desc = np->n_desc;
404 p->n_value = (*validate_fn)(kd, np->n_value);
406 p->n_value = np->n_value;
408 /* Find next freshly resolved entry. */
409 for (np++; np->n_name && np->n_name[0]; np++)
410 if (np->n_type != N_UNDF)
414 /* We could assert missing = unresolved here. */
421 _kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
425 struct kld_sym_lookup lookup;
427 const char *prefix = "";
428 char symname[1024]; /* XXX-BZ symbol name length limit? */
429 int tried_vnet, tried_dpcpu;
432 * If we can't use the kld symbol lookup, revert to the
436 error = kvm_fdnlist(kd->nlfd, nl);
437 if (error <= 0) /* Hard error or success. */
440 if (_kvm_vnet_initialized(kd, initialize))
441 error = kvm_fdnlist_prefix(kd, nl, error,
442 VNET_SYMPREFIX, _kvm_vnet_validaddr);
444 if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
445 error = kvm_fdnlist_prefix(kd, nl, error,
446 DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
452 * We can use the kld lookup syscall. Go through each nlist entry
453 * and look it up with a kldsym(2) syscall.
459 for (p = nl; p->n_name && p->n_name[0]; ++p) {
460 if (p->n_type != N_UNDF)
463 lookup.version = sizeof(lookup);
467 error = snprintf(symname, sizeof(symname), "%s%s", prefix,
468 (prefix[0] != '\0' && p->n_name[0] == '_') ?
469 (p->n_name + 1) : p->n_name);
470 if (error < 0 || error >= (int)sizeof(symname))
472 lookup.symname = symname;
473 if (lookup.symname[0] == '_')
476 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
480 if (_kvm_vnet_initialized(kd, initialize) &&
481 strcmp(prefix, VNET_SYMPREFIX) == 0)
483 _kvm_vnet_validaddr(kd, lookup.symvalue);
484 else if (_kvm_dpcpu_initialized(kd, initialize) &&
485 strcmp(prefix, DPCPU_SYMPREFIX) == 0)
487 _kvm_dpcpu_validaddr(kd, lookup.symvalue);
489 p->n_value = lookup.symvalue;
496 * Check the number of entries that weren't found. If they exist,
497 * try again with a prefix for virtualized or DPCPU symbol names.
499 error = ((p - nl) - nvalid);
500 if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
502 prefix = VNET_SYMPREFIX;
505 if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
507 prefix = DPCPU_SYMPREFIX;
512 * Return the number of entries that weren't found. If they exist,
513 * also fill internal error buffer.
515 error = ((p - nl) - nvalid);
517 _kvm_syserr(kd, kd->program, "kvm_nlist");
522 kvm_nlist(kvm_t *kd, struct nlist *nl)
526 * If called via the public interface, permit intialization of
527 * further virtualized modules on demand.
529 return (_kvm_nlist(kd, nl, 1));
533 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
542 * We're using /dev/kmem. Just read straight from the
543 * device and let the active kernel do the address translation.
546 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
547 _kvm_err(kd, 0, "invalid address (%lx)", kva);
550 cr = read(kd->vmfd, buf, len);
552 _kvm_syserr(kd, 0, "kvm_read");
554 } else if (cr < (ssize_t)len)
555 _kvm_err(kd, kd->program, "short read");
561 cc = _kvm_kvatop(kd, kva, &pa);
564 if (cc > (ssize_t)len)
567 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
568 _kvm_syserr(kd, 0, _PATH_MEM);
571 cr = read(kd->pmfd, cp, cc);
573 _kvm_syserr(kd, kd->program, "kvm_read");
577 * If kvm_kvatop returns a bogus value or our core file is
578 * truncated, we might wind up seeking beyond the end of the
579 * core file in which case the read will return 0 (EOF).
588 return (cp - (char *)buf);
592 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
598 * Just like kvm_read, only we write.
601 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
602 _kvm_err(kd, 0, "invalid address (%lx)", kva);
605 cc = write(kd->vmfd, buf, len);
607 _kvm_syserr(kd, 0, "kvm_write");
609 } else if ((size_t)cc < len)
610 _kvm_err(kd, kd->program, "short write");
613 _kvm_err(kd, kd->program,
614 "kvm_write not implemented for dead kernels");