xref: /freebsd/lib/libkvm/kvm.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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  * 3. 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$");
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static 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 #include <sys/fnv_hash.h>
45 
46 #define	_WANT_VNET
47 
48 #include <sys/user.h>
49 #include <sys/linker.h>
50 #include <sys/pcpu.h>
51 #include <sys/stat.h>
52 #include <sys/mman.h>
53 
54 #include <net/vnet.h>
55 
56 #include <fcntl.h>
57 #include <kvm.h>
58 #include <limits.h>
59 #include <paths.h>
60 #include <stdint.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 
66 #include "kvm_private.h"
67 
68 SET_DECLARE(kvm_arch, struct kvm_arch);
69 
70 static char _kd_is_null[] = "";
71 
72 char *
73 kvm_geterr(kvm_t *kd)
74 {
75 
76 	if (kd == NULL)
77 		return (_kd_is_null);
78 	return (kd->errbuf);
79 }
80 
81 static int
82 _kvm_read_kernel_ehdr(kvm_t *kd)
83 {
84 	Elf *elf;
85 
86 	if (elf_version(EV_CURRENT) == EV_NONE) {
87 		_kvm_err(kd, kd->program, "Unsupported libelf");
88 		return (-1);
89 	}
90 	elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
91 	if (elf == NULL) {
92 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
93 		return (-1);
94 	}
95 	if (elf_kind(elf) != ELF_K_ELF) {
96 		_kvm_err(kd, kd->program, "kernel is not an ELF file");
97 		return (-1);
98 	}
99 	if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
100 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
101 		elf_end(elf);
102 		return (-1);
103 	}
104 	elf_end(elf);
105 
106 	switch (kd->nlehdr.e_ident[EI_DATA]) {
107 	case ELFDATA2LSB:
108 	case ELFDATA2MSB:
109 		return (0);
110 	default:
111 		_kvm_err(kd, kd->program,
112 		    "unsupported ELF data encoding for kernel");
113 		return (-1);
114 	}
115 }
116 
117 static kvm_t *
118 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
119 {
120 	struct kvm_arch **parch;
121 	struct stat st;
122 
123 	kd->vmfd = -1;
124 	kd->pmfd = -1;
125 	kd->nlfd = -1;
126 	kd->vmst = NULL;
127 	kd->procbase = NULL;
128 	kd->argspc = NULL;
129 	kd->argv = NULL;
130 
131 	if (uf == NULL)
132 		uf = getbootfile();
133 	else if (strlen(uf) >= MAXPATHLEN) {
134 		_kvm_err(kd, kd->program, "exec file name too long");
135 		goto failed;
136 	}
137 	if (flag & ~O_RDWR) {
138 		_kvm_err(kd, kd->program, "bad flags arg");
139 		goto failed;
140 	}
141 	if (mf == NULL)
142 		mf = _PATH_MEM;
143 
144 	if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
145 		_kvm_syserr(kd, kd->program, "%s", mf);
146 		goto failed;
147 	}
148 	if (fstat(kd->pmfd, &st) < 0) {
149 		_kvm_syserr(kd, kd->program, "%s", mf);
150 		goto failed;
151 	}
152 	if (S_ISREG(st.st_mode) && st.st_size <= 0) {
153 		errno = EINVAL;
154 		_kvm_syserr(kd, kd->program, "empty file");
155 		goto failed;
156 	}
157 	if (S_ISCHR(st.st_mode)) {
158 		/*
159 		 * If this is a character special device, then check that
160 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
161 		 * make it work for either /dev/mem or /dev/kmem -- in either
162 		 * case you're working with a live kernel.)
163 		 */
164 		if (strcmp(mf, _PATH_DEVNULL) == 0) {
165 			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
166 			return (kd);
167 		} else if (strcmp(mf, _PATH_MEM) == 0) {
168 			if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
169 			    0) {
170 				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
171 				goto failed;
172 			}
173 			return (kd);
174 		}
175 	}
176 
177 	/*
178 	 * This is either a crash dump or a remote live system with its physical
179 	 * memory fully accessible via a special device.
180 	 * Open the namelist fd and determine the architecture.
181 	 */
182 	if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
183 		_kvm_syserr(kd, kd->program, "%s", uf);
184 		goto failed;
185 	}
186 	if (_kvm_read_kernel_ehdr(kd) < 0)
187 		goto failed;
188 	if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
189 	    strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
190 		kd->rawdump = 1;
191 		kd->writable = 1;
192 	}
193 	SET_FOREACH(parch, kvm_arch) {
194 		if ((*parch)->ka_probe(kd)) {
195 			kd->arch = *parch;
196 			break;
197 		}
198 	}
199 	if (kd->arch == NULL) {
200 		_kvm_err(kd, kd->program, "unsupported architecture");
201 		goto failed;
202 	}
203 
204 	/*
205 	 * Non-native kernels require a symbol resolver.
206 	 */
207 	if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
208 		_kvm_err(kd, kd->program,
209 		    "non-native kernel requires a symbol resolver");
210 		goto failed;
211 	}
212 
213 	/*
214 	 * Initialize the virtual address translation machinery.
215 	 */
216 	if (kd->arch->ka_initvtop(kd) < 0)
217 		goto failed;
218 	return (kd);
219 failed:
220 	/*
221 	 * Copy out the error if doing sane error semantics.
222 	 */
223 	if (errout != NULL)
224 		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
225 	(void)kvm_close(kd);
226 	return (NULL);
227 }
228 
229 kvm_t *
230 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
231     char *errout)
232 {
233 	kvm_t *kd;
234 
235 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
236 		if (errout != NULL)
237 			(void)strlcpy(errout, strerror(errno),
238 			    _POSIX2_LINE_MAX);
239 		return (NULL);
240 	}
241 	return (_kvm_open(kd, uf, mf, flag, errout));
242 }
243 
244 kvm_t *
245 kvm_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 
260 kvm_t *
261 kvm_open2(const char *uf, const char *mf, int flag, char *errout,
262     int (*resolver)(const char *, kvaddr_t *))
263 {
264 	kvm_t *kd;
265 
266 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
267 		if (errout != NULL)
268 			(void)strlcpy(errout, strerror(errno),
269 			    _POSIX2_LINE_MAX);
270 		return (NULL);
271 	}
272 	kd->resolve_symbol = resolver;
273 	return (_kvm_open(kd, uf, mf, flag, errout));
274 }
275 
276 int
277 kvm_close(kvm_t *kd)
278 {
279 	int error = 0;
280 
281 	if (kd == NULL) {
282 		errno = EINVAL;
283 		return (-1);
284 	}
285 	if (kd->vmst != NULL)
286 		kd->arch->ka_freevtop(kd);
287 	if (kd->pmfd >= 0)
288 		error |= close(kd->pmfd);
289 	if (kd->vmfd >= 0)
290 		error |= close(kd->vmfd);
291 	if (kd->nlfd >= 0)
292 		error |= close(kd->nlfd);
293 	if (kd->procbase != 0)
294 		free((void *)kd->procbase);
295 	if (kd->argbuf != 0)
296 		free((void *) kd->argbuf);
297 	if (kd->argspc != 0)
298 		free((void *) kd->argspc);
299 	if (kd->argv != 0)
300 		free((void *)kd->argv);
301 	if (kd->pt_map != NULL)
302 		free(kd->pt_map);
303 	if (kd->page_map != NULL)
304 		free(kd->page_map);
305 	if (kd->sparse_map != MAP_FAILED)
306 		munmap(kd->sparse_map, kd->pt_sparse_size);
307 	free((void *)kd);
308 
309 	return (error);
310 }
311 
312 int
313 kvm_nlist2(kvm_t *kd, struct kvm_nlist *nl)
314 {
315 
316 	/*
317 	 * If called via the public interface, permit initialization of
318 	 * further virtualized modules on demand.
319 	 */
320 	return (_kvm_nlist(kd, nl, 1));
321 }
322 
323 int
324 kvm_nlist(kvm_t *kd, struct nlist *nl)
325 {
326 	struct kvm_nlist *kl;
327 	int count, i, nfail;
328 
329 	/*
330 	 * Avoid reporting truncated addresses by failing for non-native
331 	 * cores.
332 	 */
333 	if (!kvm_native(kd)) {
334 		_kvm_err(kd, kd->program, "kvm_nlist of non-native vmcore");
335 		return (-1);
336 	}
337 
338 	for (count = 0; nl[count].n_name != NULL && nl[count].n_name[0] != '\0';
339 	     count++)
340 		;
341 	if (count == 0)
342 		return (0);
343 	kl = calloc(count + 1, sizeof(*kl));
344 	for (i = 0; i < count; i++)
345 		kl[i].n_name = nl[i].n_name;
346 	nfail = kvm_nlist2(kd, kl);
347 	for (i = 0; i < count; i++) {
348 		nl[i].n_type = kl[i].n_type;
349 		nl[i].n_other = 0;
350 		nl[i].n_desc = 0;
351 		nl[i].n_value = kl[i].n_value;
352 	}
353 	return (nfail);
354 }
355 
356 ssize_t
357 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
358 {
359 
360 	return (kvm_read2(kd, kva, buf, len));
361 }
362 
363 ssize_t
364 kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
365 {
366 	int cc;
367 	ssize_t cr;
368 	off_t pa;
369 	char *cp;
370 
371 	if (ISALIVE(kd)) {
372 		/*
373 		 * We're using /dev/kmem.  Just read straight from the
374 		 * device and let the active kernel do the address translation.
375 		 */
376 		errno = 0;
377 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
378 			_kvm_err(kd, 0, "invalid address (0x%jx)",
379 			    (uintmax_t)kva);
380 			return (-1);
381 		}
382 		cr = read(kd->vmfd, buf, len);
383 		if (cr < 0) {
384 			_kvm_syserr(kd, 0, "kvm_read");
385 			return (-1);
386 		} else if (cr < (ssize_t)len)
387 			_kvm_err(kd, kd->program, "short read");
388 		return (cr);
389 	}
390 
391 	cp = buf;
392 	while (len > 0) {
393 		cc = kd->arch->ka_kvatop(kd, kva, &pa);
394 		if (cc == 0)
395 			return (-1);
396 		if (cc > (ssize_t)len)
397 			cc = len;
398 		errno = 0;
399 		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
400 			_kvm_syserr(kd, 0, _PATH_MEM);
401 			break;
402 		}
403 		cr = read(kd->pmfd, cp, cc);
404 		if (cr < 0) {
405 			_kvm_syserr(kd, kd->program, "kvm_read");
406 			break;
407 		}
408 		/*
409 		 * If ka_kvatop returns a bogus value or our core file is
410 		 * truncated, we might wind up seeking beyond the end of the
411 		 * core file in which case the read will return 0 (EOF).
412 		 */
413 		if (cr == 0)
414 			break;
415 		cp += cr;
416 		kva += cr;
417 		len -= cr;
418 	}
419 
420 	return (cp - (char *)buf);
421 }
422 
423 ssize_t
424 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
425 {
426 	int cc;
427 	ssize_t cw;
428 	off_t pa;
429 	const char *cp;
430 
431 	if (!ISALIVE(kd) && !kd->writable) {
432 		_kvm_err(kd, kd->program,
433 		    "kvm_write not implemented for dead kernels");
434 		return (-1);
435 	}
436 
437 	if (ISALIVE(kd)) {
438 		/*
439 		 * Just like kvm_read, only we write.
440 		 */
441 		errno = 0;
442 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
443 			_kvm_err(kd, 0, "invalid address (%lx)", kva);
444 			return (-1);
445 		}
446 		cc = write(kd->vmfd, buf, len);
447 		if (cc < 0) {
448 			_kvm_syserr(kd, 0, "kvm_write");
449 			return (-1);
450 		} else if ((size_t)cc < len)
451 			_kvm_err(kd, kd->program, "short write");
452 		return (cc);
453 	}
454 
455 	cp = buf;
456 	while (len > 0) {
457 		cc = kd->arch->ka_kvatop(kd, kva, &pa);
458 		if (cc == 0)
459 			return (-1);
460 		if (cc > (ssize_t)len)
461 			cc = len;
462 		errno = 0;
463 		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
464 			_kvm_syserr(kd, 0, _PATH_MEM);
465 			break;
466 		}
467 		cw = write(kd->pmfd, cp, cc);
468 		if (cw < 0) {
469 			_kvm_syserr(kd, kd->program, "kvm_write");
470 			break;
471 		}
472 		/*
473 		 * If ka_kvatop returns a bogus value or our core file is
474 		 * truncated, we might wind up seeking beyond the end of the
475 		 * core file in which case the read will return 0 (EOF).
476 		 */
477 		if (cw == 0)
478 			break;
479 		cp += cw;
480 		kva += cw;
481 		len -= cw;
482 	}
483 
484 	return (cp - (const char *)buf);
485 }
486 
487 int
488 kvm_native(kvm_t *kd)
489 {
490 
491 	if (ISALIVE(kd))
492 		return (1);
493 	return (kd->arch->ka_native(kd));
494 }
495 
496 int
497 kvm_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *closure)
498 {
499 
500 	if (kd->arch->ka_walk_pages == NULL)
501 		return (0);
502 
503 	return (kd->arch->ka_walk_pages(kd, cb, closure));
504 }
505