kvm.c revision 217744
1/*-
2 * Copyright (c) 1989, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
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.
8 *
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 * 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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/lib/libkvm/kvm.c 217744 2011-01-23 11:08:28Z uqs $");
36
37#if defined(LIBC_SCCS) && !defined(lint)
38#if 0
39static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
40#endif
41#endif /* LIBC_SCCS and not lint */
42
43#include <sys/param.h>
44
45#define	_WANT_VNET
46
47#include <sys/user.h>
48#include <sys/proc.h>
49#include <sys/ioctl.h>
50#include <sys/stat.h>
51#include <sys/sysctl.h>
52#include <sys/linker.h>
53#include <sys/pcpu.h>
54
55#include <net/vnet.h>
56
57#include <vm/vm.h>
58#include <vm/vm_param.h>
59
60#include <machine/vmparam.h>
61
62#include <ctype.h>
63#include <fcntl.h>
64#include <kvm.h>
65#include <limits.h>
66#include <nlist.h>
67#include <paths.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <strings.h>
72#include <unistd.h>
73
74#include "kvm_private.h"
75
76/* from src/lib/libc/gen/nlist.c */
77int __fdnlist(int, struct nlist *);
78
79char *
80kvm_geterr(kvm_t *kd)
81{
82	return (kd->errbuf);
83}
84
85#include <stdarg.h>
86
87/*
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).
92 */
93void
94_kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
95{
96	va_list ap;
97
98	va_start(ap, fmt);
99	if (program != NULL) {
100		(void)fprintf(stderr, "%s: ", program);
101		(void)vfprintf(stderr, fmt, ap);
102		(void)fputc('\n', stderr);
103	} else
104		(void)vsnprintf(kd->errbuf,
105		    sizeof(kd->errbuf), fmt, ap);
106
107	va_end(ap);
108}
109
110void
111_kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
112{
113	va_list ap;
114	int n;
115
116	va_start(ap, 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));
121	} else {
122		char *cp = kd->errbuf;
123
124		(void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
125		n = strlen(cp);
126		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
127		    strerror(errno));
128	}
129	va_end(ap);
130}
131
132void *
133_kvm_malloc(kvm_t *kd, size_t n)
134{
135	void *p;
136
137	if ((p = calloc(n, sizeof(char))) == NULL)
138		_kvm_err(kd, kd->program, "can't allocate %zu bytes: %s",
139			 n, strerror(errno));
140	return (p);
141}
142
143static kvm_t *
144_kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
145{
146	struct stat st;
147
148	kd->vmfd = -1;
149	kd->pmfd = -1;
150	kd->nlfd = -1;
151	kd->vmst = 0;
152	kd->procbase = 0;
153	kd->argspc = 0;
154	kd->argv = 0;
155
156	if (uf == 0)
157		uf = getbootfile();
158	else if (strlen(uf) >= MAXPATHLEN) {
159		_kvm_err(kd, kd->program, "exec file name too long");
160		goto failed;
161	}
162	if (flag & ~O_RDWR) {
163		_kvm_err(kd, kd->program, "bad flags arg");
164		goto failed;
165	}
166	if (mf == 0)
167		mf = _PATH_MEM;
168
169	if ((kd->pmfd = open(mf, flag, 0)) < 0) {
170		_kvm_syserr(kd, kd->program, "%s", mf);
171		goto failed;
172	}
173	if (fstat(kd->pmfd, &st) < 0) {
174		_kvm_syserr(kd, kd->program, "%s", mf);
175		goto failed;
176	}
177	if (S_ISREG(st.st_mode) && st.st_size <= 0) {
178		errno = EINVAL;
179		_kvm_syserr(kd, kd->program, "empty file");
180		goto failed;
181	}
182	if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
183		_kvm_syserr(kd, kd->program, "%s", mf);
184		goto failed;
185	}
186	if (S_ISCHR(st.st_mode)) {
187		/*
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.)
192		 */
193		if (strcmp(mf, _PATH_DEVNULL) == 0) {
194			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
195			return (kd);
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);
199				goto failed;
200			}
201			if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
202				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
203				goto failed;
204			}
205			return (kd);
206		}
207	}
208	/*
209	 * This is a crash dump.
210	 * Initialize the virtual address translation machinery,
211	 * but first setup the namelist fd.
212	 */
213	if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
214		_kvm_syserr(kd, kd->program, "%s", uf);
215		goto failed;
216	}
217	if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
218		_kvm_syserr(kd, kd->program, "%s", uf);
219		goto failed;
220	}
221	if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
222		kd->rawdump = 1;
223	if (_kvm_initvtop(kd) < 0)
224		goto failed;
225	return (kd);
226failed:
227	/*
228	 * Copy out the error if doing sane error semantics.
229	 */
230	if (errout != 0)
231		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
232	(void)kvm_close(kd);
233	return (0);
234}
235
236kvm_t *
237kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
238    char *errout)
239{
240	kvm_t *kd;
241
242	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
243		(void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
244		return (0);
245	}
246	kd->program = 0;
247	return (_kvm_open(kd, uf, mf, flag, errout));
248}
249
250kvm_t *
251kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
252    const char *errstr)
253{
254	kvm_t *kd;
255
256	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
257		if (errstr != NULL)
258			(void)fprintf(stderr, "%s: %s\n",
259				      errstr, strerror(errno));
260		return (0);
261	}
262	kd->program = errstr;
263	return (_kvm_open(kd, uf, mf, flag, NULL));
264}
265
266int
267kvm_close(kvm_t *kd)
268{
269	int error = 0;
270
271	if (kd->pmfd >= 0)
272		error |= close(kd->pmfd);
273	if (kd->vmfd >= 0)
274		error |= close(kd->vmfd);
275	if (kd->nlfd >= 0)
276		error |= close(kd->nlfd);
277	if (kd->vmst)
278		_kvm_freevtop(kd);
279	if (kd->procbase != 0)
280		free((void *)kd->procbase);
281	if (kd->argbuf != 0)
282		free((void *) kd->argbuf);
283	if (kd->argspc != 0)
284		free((void *) kd->argspc);
285	if (kd->argv != 0)
286		free((void *)kd->argv);
287	free((void *)kd);
288
289	return (0);
290}
291
292/*
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.
295 */
296static int
297kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
298    uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
299{
300	struct nlist *n, *np, *p;
301	char *cp, *ce;
302	const char *ccp;
303	size_t len;
304	int slen, unresolved;
305
306	/*
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.
310	 */
311	len = 0;
312	unresolved = 0;
313	for (p = nl; p->n_name && p->n_name[0]; ++p) {
314		if (p->n_type != N_UNDF)
315			continue;
316		len += sizeof(struct nlist) + strlen(prefix) +
317		    2 * (strlen(p->n_name) + 1);
318		unresolved++;
319	}
320	if (unresolved == 0)
321		return (unresolved);
322	/* Add space for the terminating nlist entry. */
323	len += sizeof(struct nlist);
324	unresolved++;
325
326	/* Alloc one chunk for (nlist, [names]) and setup pointers. */
327	n = np = malloc(len);
328	bzero(n, len);
329	if (n == NULL)
330		return (missing);
331	cp = ce = (char *)np;
332	cp += unresolved * sizeof(struct nlist);
333	ce += len;
334
335	/* Generate shortened nlist with special prefix. */
336	unresolved = 0;
337	for (p = nl; p->n_name && p->n_name[0]; ++p) {
338		if (p->n_type != N_UNDF)
339			continue;
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)
346			continue;
347		np->n_name = cp;
348		cp += slen + 1;
349		np++;
350		unresolved++;
351	}
352
353	/* Do lookup on the reduced list. */
354	np = n;
355	unresolved = __fdnlist(kd->nlfd, np);
356
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)
362				break;
363		/*
364		 * The lists are both in the same order,
365		 * so we can walk them in parallel.
366		 */
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)
370				continue;
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)
374				continue;
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;
379			if (validate_fn)
380				p->n_value = (*validate_fn)(kd, np->n_value);
381			else
382				p->n_value = np->n_value;
383			missing--;
384			/* Find next freshly resolved entry. */
385			for (np++; np->n_name && np->n_name[0]; np++)
386				if (np->n_type != N_UNDF)
387					break;
388		}
389	}
390	/* We could assert missing = unresolved here. */
391
392	free(n);
393	return (unresolved);
394}
395
396int
397_kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
398{
399	struct nlist *p;
400	int nvalid;
401	struct kld_sym_lookup lookup;
402	int error;
403	const char *prefix = "";
404	char symname[1024]; /* XXX-BZ symbol name length limit? */
405	int tried_vnet, tried_dpcpu;
406
407	/*
408	 * If we can't use the kld symbol lookup, revert to the
409	 * slow library call.
410	 */
411	if (!ISALIVE(kd)) {
412		error = __fdnlist(kd->nlfd, nl);
413		if (error <= 0)			/* Hard error or success. */
414			return (error);
415
416		if (_kvm_vnet_initialized(kd, initialize))
417			error = kvm_fdnlist_prefix(kd, nl, error,
418			    VNET_SYMPREFIX, _kvm_vnet_validaddr);
419
420		if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
421			error = kvm_fdnlist_prefix(kd, nl, error,
422			    DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
423
424		return (error);
425	}
426
427	/*
428	 * We can use the kld lookup syscall.  Go through each nlist entry
429	 * and look it up with a kldsym(2) syscall.
430	 */
431	nvalid = 0;
432	tried_vnet = 0;
433	tried_dpcpu = 0;
434again:
435	for (p = nl; p->n_name && p->n_name[0]; ++p) {
436		if (p->n_type != N_UNDF)
437			continue;
438
439		lookup.version = sizeof(lookup);
440		lookup.symvalue = 0;
441		lookup.symsize = 0;
442
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))
447			continue;
448		lookup.symname = symname;
449		if (lookup.symname[0] == '_')
450			lookup.symname++;
451
452		if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
453			p->n_type = N_TEXT;
454			p->n_other = 0;
455			p->n_desc = 0;
456			if (_kvm_vnet_initialized(kd, initialize) &&
457			    !strcmp(prefix, VNET_SYMPREFIX) == 0)
458				p->n_value =
459				    _kvm_vnet_validaddr(kd, lookup.symvalue);
460			else if (_kvm_dpcpu_initialized(kd, initialize) &&
461			    !strcmp(prefix, DPCPU_SYMPREFIX) == 0)
462				p->n_value =
463				    _kvm_dpcpu_validaddr(kd, lookup.symvalue);
464			else
465				p->n_value = lookup.symvalue;
466			++nvalid;
467			/* lookup.symsize */
468		}
469	}
470
471	/*
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.
474	 */
475	error = ((p - nl) - nvalid);
476	if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
477		tried_vnet = 1;
478		prefix = VNET_SYMPREFIX;
479		goto again;
480	}
481	if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
482		tried_dpcpu = 1;
483		prefix = DPCPU_SYMPREFIX;
484		goto again;
485	}
486
487	/*
488	 * Return the number of entries that weren't found. If they exist,
489	 * also fill internal error buffer.
490	 */
491	error = ((p - nl) - nvalid);
492	if (error)
493		_kvm_syserr(kd, kd->program, "kvm_nlist");
494	return (error);
495}
496
497int
498kvm_nlist(kvm_t *kd, struct nlist *nl)
499{
500
501	/*
502	 * If called via the public interface, permit intialization of
503	 * further virtualized modules on demand.
504	 */
505	return (_kvm_nlist(kd, nl, 1));
506}
507
508ssize_t
509kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
510{
511	int cc;
512	ssize_t cr;
513	off_t pa;
514	char *cp;
515
516	if (ISALIVE(kd)) {
517		/*
518		 * We're using /dev/kmem.  Just read straight from the
519		 * device and let the active kernel do the address translation.
520		 */
521		errno = 0;
522		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
523			_kvm_err(kd, 0, "invalid address (%lx)", kva);
524			return (-1);
525		}
526		cr = read(kd->vmfd, buf, len);
527		if (cr < 0) {
528			_kvm_syserr(kd, 0, "kvm_read");
529			return (-1);
530		} else if (cr < (ssize_t)len)
531			_kvm_err(kd, kd->program, "short read");
532		return (cr);
533	}
534
535	cp = buf;
536	while (len > 0) {
537		cc = _kvm_kvatop(kd, kva, &pa);
538		if (cc == 0)
539			return (-1);
540		if (cc > (ssize_t)len)
541			cc = len;
542		errno = 0;
543		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
544			_kvm_syserr(kd, 0, _PATH_MEM);
545			break;
546		}
547		cr = read(kd->pmfd, cp, cc);
548		if (cr < 0) {
549			_kvm_syserr(kd, kd->program, "kvm_read");
550			break;
551		}
552		/*
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).
556		 */
557		if (cr == 0)
558			break;
559		cp += cr;
560		kva += cr;
561		len -= cr;
562	}
563
564	return (cp - (char *)buf);
565}
566
567ssize_t
568kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
569{
570	int cc;
571
572	if (ISALIVE(kd)) {
573		/*
574		 * Just like kvm_read, only we write.
575		 */
576		errno = 0;
577		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
578			_kvm_err(kd, 0, "invalid address (%lx)", kva);
579			return (-1);
580		}
581		cc = write(kd->vmfd, buf, len);
582		if (cc < 0) {
583			_kvm_syserr(kd, 0, "kvm_write");
584			return (-1);
585		} else if ((size_t)cc < len)
586			_kvm_err(kd, kd->program, "short write");
587		return (cc);
588	} else {
589		_kvm_err(kd, kd->program,
590		    "kvm_write not implemented for dead kernels");
591		return (-1);
592	}
593	/* NOTREACHED */
594}
595