kvm.c revision 316070
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: stable/10/lib/libkvm/kvm.c 316070 2017-03-28 05:57:20Z ngie $");
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 | O_CLOEXEC, 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 (S_ISCHR(st.st_mode)) {
183		/*
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.)
188		 */
189		if (strcmp(mf, _PATH_DEVNULL) == 0) {
190			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
191			return (kd);
192		} else if (strcmp(mf, _PATH_MEM) == 0) {
193			if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
194			    0) {
195				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
196				goto failed;
197			}
198			return (kd);
199		}
200	}
201
202	/*
203	 * This is either a crash dump or a remote live system with its physical
204	 * memory fully accessible via a special device.
205	 * Initialize the virtual address translation machinery,
206	 * but first setup the namelist fd.
207	 */
208	if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
209		_kvm_syserr(kd, kd->program, "%s", uf);
210		goto failed;
211	}
212	if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
213	    strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
214		kd->rawdump = 1;
215		kd->writable = 1;
216	}
217	if (_kvm_initvtop(kd) < 0)
218		goto failed;
219	return (kd);
220failed:
221	/*
222	 * Copy out the error if doing sane error semantics.
223	 */
224	if (errout != 0)
225		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
226	(void)kvm_close(kd);
227	return (NULL);
228}
229
230kvm_t *
231kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
232    char *errout)
233{
234	kvm_t *kd;
235
236	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
237		(void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
238		return (NULL);
239	}
240	kd->program = 0;
241	return (_kvm_open(kd, uf, mf, flag, errout));
242}
243
244kvm_t *
245kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
246    const char *errstr)
247{
248	kvm_t *kd;
249
250	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
251		if (errstr != NULL)
252			(void)fprintf(stderr, "%s: %s\n",
253				      errstr, strerror(errno));
254		return (NULL);
255	}
256	kd->program = errstr;
257	return (_kvm_open(kd, uf, mf, flag, NULL));
258}
259
260int
261kvm_close(kvm_t *kd)
262{
263	int error = 0;
264
265	if (kd == NULL) {
266		errno = EINVAL;
267		return (-1);
268	}
269	if (kd->pmfd >= 0)
270		error |= close(kd->pmfd);
271	if (kd->vmfd >= 0)
272		error |= close(kd->vmfd);
273	if (kd->nlfd >= 0)
274		error |= close(kd->nlfd);
275	if (kd->vmst)
276		_kvm_freevtop(kd);
277	if (kd->procbase != 0)
278		free((void *)kd->procbase);
279	if (kd->argbuf != 0)
280		free((void *) kd->argbuf);
281	if (kd->argspc != 0)
282		free((void *) kd->argspc);
283	if (kd->argv != 0)
284		free((void *)kd->argv);
285	free((void *)kd);
286
287	return (error);
288}
289
290/*
291 * Walk the list of unresolved symbols, generate a new list and prefix the
292 * symbol names, try again, and merge back what we could resolve.
293 */
294static int
295kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
296    uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
297{
298	struct nlist *n, *np, *p;
299	char *cp, *ce;
300	const char *ccp;
301	size_t len;
302	int slen, unresolved;
303
304	/*
305	 * Calculate the space we need to malloc for nlist and names.
306	 * We are going to store the name twice for later lookups: once
307	 * with the prefix and once the unmodified name delmited by \0.
308	 */
309	len = 0;
310	unresolved = 0;
311	for (p = nl; p->n_name && p->n_name[0]; ++p) {
312		if (p->n_type != N_UNDF)
313			continue;
314		len += sizeof(struct nlist) + strlen(prefix) +
315		    2 * (strlen(p->n_name) + 1);
316		unresolved++;
317	}
318	if (unresolved == 0)
319		return (unresolved);
320	/* Add space for the terminating nlist entry. */
321	len += sizeof(struct nlist);
322	unresolved++;
323
324	/* Alloc one chunk for (nlist, [names]) and setup pointers. */
325	n = np = malloc(len);
326	bzero(n, len);
327	if (n == NULL)
328		return (missing);
329	cp = ce = (char *)np;
330	cp += unresolved * sizeof(struct nlist);
331	ce += len;
332
333	/* Generate shortened nlist with special prefix. */
334	unresolved = 0;
335	for (p = nl; p->n_name && p->n_name[0]; ++p) {
336		if (p->n_type != N_UNDF)
337			continue;
338		bcopy(p, np, sizeof(struct nlist));
339		/* Save the new\0orig. name so we can later match it again. */
340		slen = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
341		    (prefix[0] != '\0' && p->n_name[0] == '_') ?
342			(p->n_name + 1) : p->n_name, '\0', p->n_name);
343		if (slen < 0 || slen >= ce - cp)
344			continue;
345		np->n_name = cp;
346		cp += slen + 1;
347		np++;
348		unresolved++;
349	}
350
351	/* Do lookup on the reduced list. */
352	np = n;
353	unresolved = __fdnlist(kd->nlfd, np);
354
355	/* Check if we could resolve further symbols and update the list. */
356	if (unresolved >= 0 && unresolved < missing) {
357		/* Find the first freshly resolved entry. */
358		for (; np->n_name && np->n_name[0]; np++)
359			if (np->n_type != N_UNDF)
360				break;
361		/*
362		 * The lists are both in the same order,
363		 * so we can walk them in parallel.
364		 */
365		for (p = nl; np->n_name && np->n_name[0] &&
366		    p->n_name && p->n_name[0]; ++p) {
367			if (p->n_type != N_UNDF)
368				continue;
369			/* Skip expanded name and compare to orig. one. */
370			ccp = np->n_name + strlen(np->n_name) + 1;
371			if (strcmp(ccp, p->n_name) != 0)
372				continue;
373			/* Update nlist with new, translated results. */
374			p->n_type = np->n_type;
375			p->n_other = np->n_other;
376			p->n_desc = np->n_desc;
377			if (validate_fn)
378				p->n_value = (*validate_fn)(kd, np->n_value);
379			else
380				p->n_value = np->n_value;
381			missing--;
382			/* Find next freshly resolved entry. */
383			for (np++; np->n_name && np->n_name[0]; np++)
384				if (np->n_type != N_UNDF)
385					break;
386		}
387	}
388	/* We could assert missing = unresolved here. */
389
390	free(n);
391	return (unresolved);
392}
393
394int
395_kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
396{
397	struct nlist *p;
398	int nvalid;
399	struct kld_sym_lookup lookup;
400	int error;
401	const char *prefix = "";
402	char symname[1024]; /* XXX-BZ symbol name length limit? */
403	int tried_vnet, tried_dpcpu;
404
405	/*
406	 * If we can't use the kld symbol lookup, revert to the
407	 * slow library call.
408	 */
409	if (!ISALIVE(kd)) {
410		error = __fdnlist(kd->nlfd, nl);
411		if (error <= 0)			/* Hard error or success. */
412			return (error);
413
414		if (_kvm_vnet_initialized(kd, initialize))
415			error = kvm_fdnlist_prefix(kd, nl, error,
416			    VNET_SYMPREFIX, _kvm_vnet_validaddr);
417
418		if (error > 0 && _kvm_dpcpu_initialized(kd, initialize))
419			error = kvm_fdnlist_prefix(kd, nl, error,
420			    DPCPU_SYMPREFIX, _kvm_dpcpu_validaddr);
421
422		return (error);
423	}
424
425	/*
426	 * We can use the kld lookup syscall.  Go through each nlist entry
427	 * and look it up with a kldsym(2) syscall.
428	 */
429	nvalid = 0;
430	tried_vnet = 0;
431	tried_dpcpu = 0;
432again:
433	for (p = nl; p->n_name && p->n_name[0]; ++p) {
434		if (p->n_type != N_UNDF)
435			continue;
436
437		lookup.version = sizeof(lookup);
438		lookup.symvalue = 0;
439		lookup.symsize = 0;
440
441		error = snprintf(symname, sizeof(symname), "%s%s", prefix,
442		    (prefix[0] != '\0' && p->n_name[0] == '_') ?
443			(p->n_name + 1) : p->n_name);
444		if (error < 0 || error >= (int)sizeof(symname))
445			continue;
446		lookup.symname = symname;
447		if (lookup.symname[0] == '_')
448			lookup.symname++;
449
450		if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
451			p->n_type = N_TEXT;
452			p->n_other = 0;
453			p->n_desc = 0;
454			if (_kvm_vnet_initialized(kd, initialize) &&
455			    strcmp(prefix, VNET_SYMPREFIX) == 0)
456				p->n_value =
457				    _kvm_vnet_validaddr(kd, lookup.symvalue);
458			else if (_kvm_dpcpu_initialized(kd, initialize) &&
459			    strcmp(prefix, DPCPU_SYMPREFIX) == 0)
460				p->n_value =
461				    _kvm_dpcpu_validaddr(kd, lookup.symvalue);
462			else
463				p->n_value = lookup.symvalue;
464			++nvalid;
465			/* lookup.symsize */
466		}
467	}
468
469	/*
470	 * Check the number of entries that weren't found. If they exist,
471	 * try again with a prefix for virtualized or DPCPU symbol names.
472	 */
473	error = ((p - nl) - nvalid);
474	if (error && _kvm_vnet_initialized(kd, initialize) && !tried_vnet) {
475		tried_vnet = 1;
476		prefix = VNET_SYMPREFIX;
477		goto again;
478	}
479	if (error && _kvm_dpcpu_initialized(kd, initialize) && !tried_dpcpu) {
480		tried_dpcpu = 1;
481		prefix = DPCPU_SYMPREFIX;
482		goto again;
483	}
484
485	/*
486	 * Return the number of entries that weren't found. If they exist,
487	 * also fill internal error buffer.
488	 */
489	error = ((p - nl) - nvalid);
490	if (error)
491		_kvm_syserr(kd, kd->program, "kvm_nlist");
492	return (error);
493}
494
495int
496kvm_nlist(kvm_t *kd, struct nlist *nl)
497{
498
499	/*
500	 * If called via the public interface, permit intialization of
501	 * further virtualized modules on demand.
502	 */
503	return (_kvm_nlist(kd, nl, 1));
504}
505
506ssize_t
507kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
508{
509	int cc;
510	ssize_t cr;
511	off_t pa;
512	char *cp;
513
514	if (ISALIVE(kd)) {
515		/*
516		 * We're using /dev/kmem.  Just read straight from the
517		 * device and let the active kernel do the address translation.
518		 */
519		errno = 0;
520		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
521			_kvm_err(kd, 0, "invalid address (%lx)", kva);
522			return (-1);
523		}
524		cr = read(kd->vmfd, buf, len);
525		if (cr < 0) {
526			_kvm_syserr(kd, 0, "kvm_read");
527			return (-1);
528		} else if (cr < (ssize_t)len)
529			_kvm_err(kd, kd->program, "short read");
530		return (cr);
531	}
532
533	cp = buf;
534	while (len > 0) {
535		cc = _kvm_kvatop(kd, kva, &pa);
536		if (cc == 0)
537			return (-1);
538		if (cc > (ssize_t)len)
539			cc = len;
540		errno = 0;
541		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
542			_kvm_syserr(kd, 0, _PATH_MEM);
543			break;
544		}
545		cr = read(kd->pmfd, cp, cc);
546		if (cr < 0) {
547			_kvm_syserr(kd, kd->program, "kvm_read");
548			break;
549		}
550		/*
551		 * If kvm_kvatop returns a bogus value or our core file is
552		 * truncated, we might wind up seeking beyond the end of the
553		 * core file in which case the read will return 0 (EOF).
554		 */
555		if (cr == 0)
556			break;
557		cp += cr;
558		kva += cr;
559		len -= cr;
560	}
561
562	return (cp - (char *)buf);
563}
564
565ssize_t
566kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
567{
568	int cc;
569	ssize_t cw;
570	off_t pa;
571	const char *cp;
572
573	if (!ISALIVE(kd) && !kd->writable) {
574		_kvm_err(kd, kd->program,
575		    "kvm_write not implemented for dead kernels");
576		return (-1);
577	}
578
579	if (ISALIVE(kd)) {
580		/*
581		 * Just like kvm_read, only we write.
582		 */
583		errno = 0;
584		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
585			_kvm_err(kd, 0, "invalid address (%lx)", kva);
586			return (-1);
587		}
588		cc = write(kd->vmfd, buf, len);
589		if (cc < 0) {
590			_kvm_syserr(kd, 0, "kvm_write");
591			return (-1);
592		} else if ((size_t)cc < len)
593			_kvm_err(kd, kd->program, "short write");
594		return (cc);
595	}
596
597	cp = buf;
598	while (len > 0) {
599		cc = _kvm_kvatop(kd, kva, &pa);
600		if (cc == 0)
601			return (-1);
602		if (cc > (ssize_t)len)
603			cc = len;
604		errno = 0;
605		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
606			_kvm_syserr(kd, 0, _PATH_MEM);
607			break;
608		}
609		cw = write(kd->pmfd, cp, cc);
610		if (cw < 0) {
611			_kvm_syserr(kd, kd->program, "kvm_write");
612			break;
613		}
614		/*
615		 * If ka_kvatop returns a bogus value or our core file is
616		 * truncated, we might wind up seeking beyond the end of the
617		 * core file in which case the read will return 0 (EOF).
618		 */
619		if (cw == 0)
620			break;
621		cp += cw;
622		kva += cw;
623		len -= cw;
624	}
625
626	return (cp - (char *)buf);
627}
628