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 | O_CLOEXEC, 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 (S_ISCHR(st.st_mode)) {
184 * If this is a character special device, then check that
185 * it's /dev/mem. If so, open kmem too. (Maybe we should
186 * make it work for either /dev/mem or /dev/kmem -- in either
187 * case you're working with a live kernel.)
189 if (strcmp(mf, _PATH_DEVNULL) == 0) {
190 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
192 } else if (strcmp(mf, _PATH_MEM) == 0) {
193 if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
195 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
202 * This is a crash dump.
203 * Initialize the virtual address translation machinery,
204 * but first setup the namelist fd.
206 if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
207 _kvm_syserr(kd, kd->program, "%s", uf);
210 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
212 if (_kvm_initvtop(kd) < 0)
217 * Copy out the error if doing sane error semantics.
220 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
226 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
231 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
232 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
236 return (_kvm_open(kd, uf, mf, flag, errout));
240 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
245 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
247 (void)fprintf(stderr, "%s: %s\n",
248 errstr, strerror(errno));
251 kd->program = errstr;
252 return (_kvm_open(kd, uf, mf, flag, NULL));
261 error |= close(kd->pmfd);
263 error |= close(kd->vmfd);
265 error |= close(kd->nlfd);
268 if (kd->procbase != 0)
269 free((void *)kd->procbase);
271 free((void *) kd->argbuf);
273 free((void *) kd->argspc);
275 free((void *)kd->argv);
282 * Walk the list of unresolved symbols, generate a new list and prefix the
283 * symbol names, try again, and merge back what we could resolve.
286 kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
287 uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
289 struct nlist *n, *np, *p;
293 int slen, unresolved;
296 * Calculate the space we need to malloc for nlist and names.
297 * We are going to store the name twice for later lookups: once
298 * with the prefix and once the unmodified name delmited by \0.
302 for (p = nl; p->n_name && p->n_name[0]; ++p) {
303 if (p->n_type != N_UNDF)
305 len += sizeof(struct nlist) + strlen(prefix) +
306 2 * (strlen(p->n_name) + 1);
311 /* Add space for the terminating nlist entry. */
312 len += sizeof(struct nlist);
315 /* Alloc one chunk for (nlist, [names]) and setup pointers. */
316 n = np = malloc(len);
320 cp = ce = (char *)np;
321 cp += unresolved * sizeof(struct nlist);
324 /* Generate shortened nlist with special prefix. */
326 for (p = nl; p->n_name && p->n_name[0]; ++p) {
327 if (p->n_type != N_UNDF)
329 bcopy(p, np, sizeof(struct nlist));
330 /* Save the new\0orig. name so we can later match it again. */
331 slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
332 (prefix[0] != '\0' && p->n_name[0] == '_') ?
333 (p->n_name + 1) : p->n_name, '\0', p->n_name);
334 if (slen < 0 || slen >= ce - cp)
342 /* Do lookup on the reduced list. */
344 unresolved = __fdnlist(kd->nlfd, np);
346 /* Check if we could resolve further symbols and update the list. */
347 if (unresolved >= 0 && unresolved < missing) {
348 /* Find the first freshly resolved entry. */
349 for (; np->n_name && np->n_name[0]; np++)
350 if (np->n_type != N_UNDF)
353 * The lists are both in the same order,
354 * so we can walk them in parallel.
356 for (p = nl; np->n_name && np->n_name[0] &&
357 p->n_name && p->n_name[0]; ++p) {
358 if (p->n_type != N_UNDF)
360 /* Skip expanded name and compare to orig. one. */
361 ccp = np->n_name + strlen(np->n_name) + 1;
362 if (strcmp(ccp, p->n_name) != 0)
364 /* Update nlist with new, translated results. */
365 p->n_type = np->n_type;
366 p->n_other = np->n_other;
367 p->n_desc = np->n_desc;
369 p->n_value = (*validate_fn)(kd, np->n_value);
371 p->n_value = np->n_value;
373 /* Find next freshly resolved entry. */
374 for (np++; np->n_name && np->n_name[0]; np++)
375 if (np->n_type != N_UNDF)
379 /* We could assert missing = unresolved here. */
386 _kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
390 struct kld_sym_lookup lookup;
392 const char *prefix = "";
393 char symname[1024]; /* XXX-BZ symbol name length limit? */
394 int tried_vnet, tried_dpcpu;
397 * If we can't use the kld symbol lookup, revert to the
401 error = __fdnlist(kd->nlfd, nl);
402 if (error <= 0) /* Hard error or success. */
405 if (_kvm_vnet_initialized(kd, initialize))
406 error = kvm_fdnlist_prefix(kd, nl, error,
407 VNET_SYMPREFIX, _kvm_vnet_validaddr);
409 if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
410 error = kvm_fdnlist_prefix(kd, nl, error,
411 DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
417 * We can use the kld lookup syscall. Go through each nlist entry
418 * and look it up with a kldsym(2) syscall.
424 for (p = nl; p->n_name && p->n_name[0]; ++p) {
425 if (p->n_type != N_UNDF)
428 lookup.version = sizeof(lookup);
432 error = snprintf(symname, sizeof(symname), "%s%s", prefix,
433 (prefix[0] != '\0' && p->n_name[0] == '_') ?
434 (p->n_name + 1) : p->n_name);
435 if (error < 0 || error >= (int)sizeof(symname))
437 lookup.symname = symname;
438 if (lookup.symname[0] == '_')
441 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
445 if (_kvm_vnet_initialized(kd, initialize) &&
446 strcmp(prefix, VNET_SYMPREFIX) == 0)
448 _kvm_vnet_validaddr(kd, lookup.symvalue);
449 else if (_kvm_dpcpu_initialized(kd, initialize) &&
450 strcmp(prefix, DPCPU_SYMPREFIX) == 0)
452 _kvm_dpcpu_validaddr(kd, lookup.symvalue);
454 p->n_value = lookup.symvalue;
461 * Check the number of entries that weren't found. If they exist,
462 * try again with a prefix for virtualized or DPCPU symbol names.
464 error = ((p - nl) - nvalid);
465 if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
467 prefix = VNET_SYMPREFIX;
470 if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
472 prefix = DPCPU_SYMPREFIX;
477 * Return the number of entries that weren't found. If they exist,
478 * also fill internal error buffer.
480 error = ((p - nl) - nvalid);
482 _kvm_syserr(kd, kd->program, "kvm_nlist");
487 kvm_nlist(kvm_t *kd, struct nlist *nl)
491 * If called via the public interface, permit intialization of
492 * further virtualized modules on demand.
494 return (_kvm_nlist(kd, nl, 1));
498 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
507 * We're using /dev/kmem. Just read straight from the
508 * device and let the active kernel do the address translation.
511 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
512 _kvm_err(kd, 0, "invalid address (%lx)", kva);
515 cr = read(kd->vmfd, buf, len);
517 _kvm_syserr(kd, 0, "kvm_read");
519 } else if (cr < (ssize_t)len)
520 _kvm_err(kd, kd->program, "short read");
526 cc = _kvm_kvatop(kd, kva, &pa);
529 if (cc > (ssize_t)len)
532 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
533 _kvm_syserr(kd, 0, _PATH_MEM);
536 cr = read(kd->pmfd, cp, cc);
538 _kvm_syserr(kd, kd->program, "kvm_read");
542 * If kvm_kvatop returns a bogus value or our core file is
543 * truncated, we might wind up seeking beyond the end of the
544 * core file in which case the read will return 0 (EOF).
553 return (cp - (char *)buf);
557 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
563 * Just like kvm_read, only we write.
566 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
567 _kvm_err(kd, 0, "invalid address (%lx)", kva);
570 cc = write(kd->vmfd, buf, len);
572 _kvm_syserr(kd, 0, "kvm_write");
574 } else if ((size_t)cc < len)
575 _kvm_err(kd, kd->program, "short write");
578 _kvm_err(kd, kd->program,
579 "kvm_write not implemented for dead kernels");