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"
76 /* from src/lib/libc/gen/nlist.c */
77 int __fdnlist(int, struct nlist *);
88 * Report an error using printf style arguments. "program" is kd->program
89 * on hard errors, and 0 on soft errors, so that under sun error emulation,
90 * only hard errors are printed out (otherwise, programs like gdb will
91 * generate tons of error messages when trying to access bogus pointers).
94 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
99 if (program != NULL) {
100 (void)fprintf(stderr, "%s: ", program);
101 (void)vfprintf(stderr, fmt, ap);
102 (void)fputc('\n', stderr);
104 (void)vsnprintf(kd->errbuf,
105 sizeof(kd->errbuf), fmt, ap);
111 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
117 if (program != NULL) {
118 (void)fprintf(stderr, "%s: ", program);
119 (void)vfprintf(stderr, fmt, ap);
120 (void)fprintf(stderr, ": %s\n", strerror(errno));
122 char *cp = kd->errbuf;
124 (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
126 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
133 _kvm_malloc(kvm_t *kd, size_t n)
137 if ((p = calloc(n, sizeof(char))) == NULL)
138 _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
144 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
158 else if (strlen(uf) >= MAXPATHLEN) {
159 _kvm_err(kd, kd->program, "exec file name too long");
162 if (flag & ~O_RDWR) {
163 _kvm_err(kd, kd->program, "bad flags arg");
169 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
170 _kvm_syserr(kd, kd->program, "%s", mf);
173 if (fstat(kd->pmfd, &st) < 0) {
174 _kvm_syserr(kd, kd->program, "%s", mf);
177 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
179 _kvm_syserr(kd, kd->program, "empty file");
182 if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
183 _kvm_syserr(kd, kd->program, "%s", mf);
186 if (S_ISCHR(st.st_mode)) {
188 * If this is a character special device, then check that
189 * it's /dev/mem. If so, open kmem too. (Maybe we should
190 * make it work for either /dev/mem or /dev/kmem -- in either
191 * case you're working with a live kernel.)
193 if (strcmp(mf, _PATH_DEVNULL) == 0) {
194 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
196 } else if (strcmp(mf, _PATH_MEM) == 0) {
197 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
198 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
201 if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
202 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
209 * This is a crash dump.
210 * Initialize the virtual address translation machinery,
211 * but first setup the namelist fd.
213 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
214 _kvm_syserr(kd, kd->program, "%s", uf);
217 if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
218 _kvm_syserr(kd, kd->program, "%s", uf);
221 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
223 if (_kvm_initvtop(kd) < 0)
228 * Copy out the error if doing sane error semantics.
231 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
237 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
242 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
243 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
247 return (_kvm_open(kd, uf, mf, flag, errout));
251 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
256 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
258 (void)fprintf(stderr, "%s: %s\n",
259 errstr, strerror(errno));
262 kd->program = errstr;
263 return (_kvm_open(kd, uf, mf, flag, NULL));
272 error |= close(kd->pmfd);
274 error |= close(kd->vmfd);
276 error |= close(kd->nlfd);
279 if (kd->procbase != 0)
280 free((void *)kd->procbase);
282 free((void *) kd->argbuf);
284 free((void *) kd->argspc);
286 free((void *)kd->argv);
293 * Walk the list of unresolved symbols, generate a new list and prefix the
294 * symbol names, try again, and merge back what we could resolve.
297 kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
298 uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
300 struct nlist *n, *np, *p;
304 int slen, unresolved;
307 * Calculate the space we need to malloc for nlist and names.
308 * We are going to store the name twice for later lookups: once
309 * with the prefix and once the unmodified name delmited by \0.
313 for (p = nl; p->n_name && p->n_name[0]; ++p) {
314 if (p->n_type != N_UNDF)
316 len += sizeof(struct nlist) + strlen(prefix) +
317 2 * (strlen(p->n_name) + 1);
322 /* Add space for the terminating nlist entry. */
323 len += sizeof(struct nlist);
326 /* Alloc one chunk for (nlist, [names]) and setup pointers. */
327 n = np = malloc(len);
331 cp = ce = (char *)np;
332 cp += unresolved * sizeof(struct nlist);
335 /* Generate shortened nlist with special prefix. */
337 for (p = nl; p->n_name && p->n_name[0]; ++p) {
338 if (p->n_type != N_UNDF)
340 bcopy(p, np, sizeof(struct nlist));
341 /* Save the new\0orig. name so we can later match it again. */
342 slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
343 (prefix[0] != '\0' && p->n_name[0] == '_') ?
344 (p->n_name + 1) : p->n_name, '\0', p->n_name);
345 if (slen < 0 || slen >= ce - cp)
353 /* Do lookup on the reduced list. */
355 unresolved = __fdnlist(kd->nlfd, np);
357 /* Check if we could resolve further symbols and update the list. */
358 if (unresolved >= 0 && unresolved < missing) {
359 /* Find the first freshly resolved entry. */
360 for (; np->n_name && np->n_name[0]; np++)
361 if (np->n_type != N_UNDF)
364 * The lists are both in the same order,
365 * so we can walk them in parallel.
367 for (p = nl; np->n_name && np->n_name[0] &&
368 p->n_name && p->n_name[0]; ++p) {
369 if (p->n_type != N_UNDF)
371 /* Skip expanded name and compare to orig. one. */
372 ccp = np->n_name + strlen(np->n_name) + 1;
373 if (strcmp(ccp, p->n_name) != 0)
375 /* Update nlist with new, translated results. */
376 p->n_type = np->n_type;
377 p->n_other = np->n_other;
378 p->n_desc = np->n_desc;
380 p->n_value = (*validate_fn)(kd, np->n_value);
382 p->n_value = np->n_value;
384 /* Find next freshly resolved entry. */
385 for (np++; np->n_name && np->n_name[0]; np++)
386 if (np->n_type != N_UNDF)
390 /* We could assert missing = unresolved here. */
397 _kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
401 struct kld_sym_lookup lookup;
403 const char *prefix = "";
404 char symname[1024]; /* XXX-BZ symbol name length limit? */
405 int tried_vnet, tried_dpcpu;
408 * If we can't use the kld symbol lookup, revert to the
412 error = __fdnlist(kd->nlfd, nl);
413 if (error <= 0) /* Hard error or success. */
416 if (_kvm_vnet_initialized(kd, initialize))
417 error = kvm_fdnlist_prefix(kd, nl, error,
418 VNET_SYMPREFIX, _kvm_vnet_validaddr);
420 if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
421 error = kvm_fdnlist_prefix(kd, nl, error,
422 DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
428 * We can use the kld lookup syscall. Go through each nlist entry
429 * and look it up with a kldsym(2) syscall.
435 for (p = nl; p->n_name && p->n_name[0]; ++p) {
436 if (p->n_type != N_UNDF)
439 lookup.version = sizeof(lookup);
443 error = snprintf(symname, sizeof(symname), "%s%s", prefix,
444 (prefix[0] != '\0' && p->n_name[0] == '_') ?
445 (p->n_name + 1) : p->n_name);
446 if (error < 0 || error >= (int)sizeof(symname))
448 lookup.symname = symname;
449 if (lookup.symname[0] == '_')
452 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
456 if (_kvm_vnet_initialized(kd, initialize) &&
457 strcmp(prefix, VNET_SYMPREFIX) == 0)
459 _kvm_vnet_validaddr(kd, lookup.symvalue);
460 else if (_kvm_dpcpu_initialized(kd, initialize) &&
461 strcmp(prefix, DPCPU_SYMPREFIX) == 0)
463 _kvm_dpcpu_validaddr(kd, lookup.symvalue);
465 p->n_value = lookup.symvalue;
472 * Check the number of entries that weren't found. If they exist,
473 * try again with a prefix for virtualized or DPCPU symbol names.
475 error = ((p - nl) - nvalid);
476 if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
478 prefix = VNET_SYMPREFIX;
481 if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
483 prefix = DPCPU_SYMPREFIX;
488 * Return the number of entries that weren't found. If they exist,
489 * also fill internal error buffer.
491 error = ((p - nl) - nvalid);
493 _kvm_syserr(kd, kd->program, "kvm_nlist");
498 kvm_nlist(kvm_t *kd, struct nlist *nl)
502 * If called via the public interface, permit intialization of
503 * further virtualized modules on demand.
505 return (_kvm_nlist(kd, nl, 1));
509 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
518 * We're using /dev/kmem. Just read straight from the
519 * device and let the active kernel do the address translation.
522 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
523 _kvm_err(kd, 0, "invalid address (%lx)", kva);
526 cr = read(kd->vmfd, buf, len);
528 _kvm_syserr(kd, 0, "kvm_read");
530 } else if (cr < (ssize_t)len)
531 _kvm_err(kd, kd->program, "short read");
537 cc = _kvm_kvatop(kd, kva, &pa);
540 if (cc > (ssize_t)len)
543 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
544 _kvm_syserr(kd, 0, _PATH_MEM);
547 cr = read(kd->pmfd, cp, cc);
549 _kvm_syserr(kd, kd->program, "kvm_read");
553 * If kvm_kvatop returns a bogus value or our core file is
554 * truncated, we might wind up seeking beyond the end of the
555 * core file in which case the read will return 0 (EOF).
564 return (cp - (char *)buf);
568 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
574 * Just like kvm_read, only we write.
577 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
578 _kvm_err(kd, 0, "invalid address (%lx)", kva);
581 cc = write(kd->vmfd, buf, len);
583 _kvm_syserr(kd, 0, "kvm_write");
585 } else if ((size_t)cc < len)
586 _kvm_err(kd, kd->program, "short write");
589 _kvm_err(kd, kd->program,
590 "kvm_write not implemented for dead kernels");