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 *);
79 static char _kd_is_null[] = "";
93 * Report an error using printf style arguments. "program" is kd->program
94 * on hard errors, and 0 on soft errors, so that under sun error emulation,
95 * only hard errors are printed out (otherwise, programs like gdb will
96 * generate tons of error messages when trying to access bogus pointers).
99 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
104 if (program != NULL) {
105 (void)fprintf(stderr, "%s: ", program);
106 (void)vfprintf(stderr, fmt, ap);
107 (void)fputc('\n', stderr);
109 (void)vsnprintf(kd->errbuf,
110 sizeof(kd->errbuf), fmt, ap);
116 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
122 if (program != NULL) {
123 (void)fprintf(stderr, "%s: ", program);
124 (void)vfprintf(stderr, fmt, ap);
125 (void)fprintf(stderr, ": %s\n", strerror(errno));
127 char *cp = kd->errbuf;
129 (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
131 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
138 _kvm_malloc(kvm_t *kd, size_t n)
142 if ((p = calloc(n, sizeof(char))) == NULL)
143 _kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
149 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
163 else if (strlen(uf) >= MAXPATHLEN) {
164 _kvm_err(kd, kd->program, "exec file name too long");
167 if (flag & ~O_RDWR) {
168 _kvm_err(kd, kd->program, "bad flags arg");
174 if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
175 _kvm_syserr(kd, kd->program, "%s", mf);
178 if (fstat(kd->pmfd, &st) < 0) {
179 _kvm_syserr(kd, kd->program, "%s", mf);
182 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
184 _kvm_syserr(kd, kd->program, "empty file");
187 if (S_ISCHR(st.st_mode)) {
189 * If this is a character special device, then check that
190 * it's /dev/mem. If so, open kmem too. (Maybe we should
191 * make it work for either /dev/mem or /dev/kmem -- in either
192 * case you're working with a live kernel.)
194 if (strcmp(mf, _PATH_DEVNULL) == 0) {
195 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
197 } else if (strcmp(mf, _PATH_MEM) == 0) {
198 if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
200 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
208 * This is either a crash dump or a remote live system with its physical
209 * memory fully accessible via a special device.
210 * Initialize the virtual address translation machinery,
211 * but first setup the namelist fd.
213 if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
214 _kvm_syserr(kd, kd->program, "%s", uf);
217 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
218 strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
222 if (_kvm_initvtop(kd) < 0)
227 * Copy out the error if doing sane error semantics.
230 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
236 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
241 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
242 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
246 return (_kvm_open(kd, uf, mf, flag, errout));
250 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
255 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
257 (void)fprintf(stderr, "%s: %s\n",
258 errstr, strerror(errno));
261 kd->program = errstr;
262 return (_kvm_open(kd, uf, mf, flag, NULL));
275 error |= close(kd->pmfd);
277 error |= close(kd->vmfd);
279 error |= close(kd->nlfd);
282 if (kd->procbase != 0)
283 free((void *)kd->procbase);
285 free((void *) kd->argbuf);
287 free((void *) kd->argspc);
289 free((void *)kd->argv);
296 * Walk the list of unresolved symbols, generate a new list and prefix the
297 * symbol names, try again, and merge back what we could resolve.
300 kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
301 uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
303 struct nlist *n, *np, *p;
307 int slen, unresolved;
310 * Calculate the space we need to malloc for nlist and names.
311 * We are going to store the name twice for later lookups: once
312 * with the prefix and once the unmodified name delmited by \0.
316 for (p = nl; p->n_name && p->n_name[0]; ++p) {
317 if (p->n_type != N_UNDF)
319 len += sizeof(struct nlist) + strlen(prefix) +
320 2 * (strlen(p->n_name) + 1);
325 /* Add space for the terminating nlist entry. */
326 len += sizeof(struct nlist);
329 /* Alloc one chunk for (nlist, [names]) and setup pointers. */
330 n = np = malloc(len);
334 cp = ce = (char *)np;
335 cp += unresolved * sizeof(struct nlist);
338 /* Generate shortened nlist with special prefix. */
340 for (p = nl; p->n_name && p->n_name[0]; ++p) {
341 if (p->n_type != N_UNDF)
343 bcopy(p, np, sizeof(struct nlist));
344 /* Save the new\0orig. name so we can later match it again. */
345 slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
346 (prefix[0] != '\0' && p->n_name[0] == '_') ?
347 (p->n_name + 1) : p->n_name, '\0', p->n_name);
348 if (slen < 0 || slen >= ce - cp)
356 /* Do lookup on the reduced list. */
358 unresolved = __fdnlist(kd->nlfd, np);
360 /* Check if we could resolve further symbols and update the list. */
361 if (unresolved >= 0 && unresolved < missing) {
362 /* Find the first freshly resolved entry. */
363 for (; np->n_name && np->n_name[0]; np++)
364 if (np->n_type != N_UNDF)
367 * The lists are both in the same order,
368 * so we can walk them in parallel.
370 for (p = nl; np->n_name && np->n_name[0] &&
371 p->n_name && p->n_name[0]; ++p) {
372 if (p->n_type != N_UNDF)
374 /* Skip expanded name and compare to orig. one. */
375 ccp = np->n_name + strlen(np->n_name) + 1;
376 if (strcmp(ccp, p->n_name) != 0)
378 /* Update nlist with new, translated results. */
379 p->n_type = np->n_type;
380 p->n_other = np->n_other;
381 p->n_desc = np->n_desc;
383 p->n_value = (*validate_fn)(kd, np->n_value);
385 p->n_value = np->n_value;
387 /* Find next freshly resolved entry. */
388 for (np++; np->n_name && np->n_name[0]; np++)
389 if (np->n_type != N_UNDF)
393 /* We could assert missing = unresolved here. */
400 _kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
404 struct kld_sym_lookup lookup;
406 const char *prefix = "";
407 char symname[1024]; /* XXX-BZ symbol name length limit? */
408 int tried_vnet, tried_dpcpu;
411 * If we can't use the kld symbol lookup, revert to the
415 error = __fdnlist(kd->nlfd, nl);
416 if (error <= 0) /* Hard error or success. */
419 if (_kvm_vnet_initialized(kd, initialize))
420 error = kvm_fdnlist_prefix(kd, nl, error,
421 VNET_SYMPREFIX, _kvm_vnet_validaddr);
423 if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
424 error = kvm_fdnlist_prefix(kd, nl, error,
425 DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
431 * We can use the kld lookup syscall. Go through each nlist entry
432 * and look it up with a kldsym(2) syscall.
438 for (p = nl; p->n_name && p->n_name[0]; ++p) {
439 if (p->n_type != N_UNDF)
442 lookup.version = sizeof(lookup);
446 error = snprintf(symname, sizeof(symname), "%s%s", prefix,
447 (prefix[0] != '\0' && p->n_name[0] == '_') ?
448 (p->n_name + 1) : p->n_name);
449 if (error < 0 || error >= (int)sizeof(symname))
451 lookup.symname = symname;
452 if (lookup.symname[0] == '_')
455 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
459 if (_kvm_vnet_initialized(kd, initialize) &&
460 strcmp(prefix, VNET_SYMPREFIX) == 0)
462 _kvm_vnet_validaddr(kd, lookup.symvalue);
463 else if (_kvm_dpcpu_initialized(kd, initialize) &&
464 strcmp(prefix, DPCPU_SYMPREFIX) == 0)
466 _kvm_dpcpu_validaddr(kd, lookup.symvalue);
468 p->n_value = lookup.symvalue;
475 * Check the number of entries that weren't found. If they exist,
476 * try again with a prefix for virtualized or DPCPU symbol names.
478 error = ((p - nl) - nvalid);
479 if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
481 prefix = VNET_SYMPREFIX;
484 if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
486 prefix = DPCPU_SYMPREFIX;
491 * Return the number of entries that weren't found. If they exist,
492 * also fill internal error buffer.
494 error = ((p - nl) - nvalid);
496 _kvm_syserr(kd, kd->program, "kvm_nlist");
501 kvm_nlist(kvm_t *kd, struct nlist *nl)
505 * If called via the public interface, permit intialization of
506 * further virtualized modules on demand.
508 return (_kvm_nlist(kd, nl, 1));
512 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
521 * We're using /dev/kmem. Just read straight from the
522 * device and let the active kernel do the address translation.
525 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
526 _kvm_err(kd, 0, "invalid address (%lx)", kva);
529 cr = read(kd->vmfd, buf, len);
531 _kvm_syserr(kd, 0, "kvm_read");
533 } else if (cr < (ssize_t)len)
534 _kvm_err(kd, kd->program, "short read");
540 cc = _kvm_kvatop(kd, kva, &pa);
543 if (cc > (ssize_t)len)
546 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
547 _kvm_syserr(kd, 0, _PATH_MEM);
550 cr = read(kd->pmfd, cp, cc);
552 _kvm_syserr(kd, kd->program, "kvm_read");
556 * If kvm_kvatop returns a bogus value or our core file is
557 * truncated, we might wind up seeking beyond the end of the
558 * core file in which case the read will return 0 (EOF).
567 return (cp - (char *)buf);
571 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
578 if (!ISALIVE(kd) && !kd->writable) {
579 _kvm_err(kd, kd->program,
580 "kvm_write not implemented for dead kernels");
586 * Just like kvm_read, only we write.
589 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
590 _kvm_err(kd, 0, "invalid address (%lx)", kva);
593 cc = write(kd->vmfd, buf, len);
595 _kvm_syserr(kd, 0, "kvm_write");
597 } else if ((size_t)cc < len)
598 _kvm_err(kd, kd->program, "short write");
604 cc = _kvm_kvatop(kd, kva, &pa);
607 if (cc > (ssize_t)len)
610 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
611 _kvm_syserr(kd, 0, _PATH_MEM);
614 cw = write(kd->pmfd, cp, cc);
616 _kvm_syserr(kd, kd->program, "kvm_write");
620 * If ka_kvatop returns a bogus value or our core file is
621 * truncated, we might wind up seeking beyond the end of the
622 * core file in which case the read will return 0 (EOF).
631 return (cp - (const char *)buf);