xref: /freebsd/lib/libkvm/kvm.c (revision 815b7436a7c6302365b6514194d27d41cb736227)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software developed by the Computer Systems
8  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9  * BG 91-66 and contributed to Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __SCCSID("@(#)kvm.c	8.2 (Berkeley) 2/13/94");
38 
39 #include <sys/param.h>
40 #include <sys/fnv_hash.h>
41 
42 #define	_WANT_VNET
43 
44 #include <sys/user.h>
45 #include <sys/linker.h>
46 #include <sys/pcpu.h>
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <sys/mman.h>
50 
51 #include <stdbool.h>
52 #include <net/vnet.h>
53 
54 #include <fcntl.h>
55 #include <kvm.h>
56 #include <limits.h>
57 #include <paths.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "kvm_private.h"
65 
66 SET_DECLARE(kvm_arch, struct kvm_arch);
67 
68 static char _kd_is_null[] = "";
69 
70 char *
71 kvm_geterr(kvm_t *kd)
72 {
73 
74 	if (kd == NULL)
75 		return (_kd_is_null);
76 	return (kd->errbuf);
77 }
78 
79 static int
80 _kvm_read_kernel_ehdr(kvm_t *kd)
81 {
82 	Elf *elf;
83 
84 	if (elf_version(EV_CURRENT) == EV_NONE) {
85 		_kvm_err(kd, kd->program, "Unsupported libelf");
86 		return (-1);
87 	}
88 	elf = elf_begin(kd->nlfd, ELF_C_READ, NULL);
89 	if (elf == NULL) {
90 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
91 		return (-1);
92 	}
93 	if (elf_kind(elf) != ELF_K_ELF) {
94 		_kvm_err(kd, kd->program, "kernel is not an ELF file");
95 		return (-1);
96 	}
97 	if (gelf_getehdr(elf, &kd->nlehdr) == NULL) {
98 		_kvm_err(kd, kd->program, "%s", elf_errmsg(0));
99 		elf_end(elf);
100 		return (-1);
101 	}
102 	elf_end(elf);
103 
104 	switch (kd->nlehdr.e_ident[EI_DATA]) {
105 	case ELFDATA2LSB:
106 	case ELFDATA2MSB:
107 		return (0);
108 	default:
109 		_kvm_err(kd, kd->program,
110 		    "unsupported ELF data encoding for kernel");
111 		return (-1);
112 	}
113 }
114 
115 static kvm_t *
116 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
117 {
118 	struct kvm_arch **parch;
119 	struct stat st;
120 
121 	kd->vmfd = -1;
122 	kd->pmfd = -1;
123 	kd->nlfd = -1;
124 	kd->vmst = NULL;
125 	kd->procbase = NULL;
126 	kd->argspc = NULL;
127 	kd->argv = NULL;
128 
129 	if (uf == NULL)
130 		uf = getbootfile();
131 	else if (strlen(uf) >= MAXPATHLEN) {
132 		_kvm_err(kd, kd->program, "exec file name too long");
133 		goto failed;
134 	}
135 	if (flag & ~O_RDWR) {
136 		_kvm_err(kd, kd->program, "bad flags arg");
137 		goto failed;
138 	}
139 	if (mf == NULL)
140 		mf = _PATH_MEM;
141 
142 	if ((kd->pmfd = open(mf, flag | O_CLOEXEC, 0)) < 0) {
143 		_kvm_syserr(kd, kd->program, "%s", mf);
144 		goto failed;
145 	}
146 	if (fstat(kd->pmfd, &st) < 0) {
147 		_kvm_syserr(kd, kd->program, "%s", mf);
148 		goto failed;
149 	}
150 	if (S_ISREG(st.st_mode) && st.st_size <= 0) {
151 		errno = EINVAL;
152 		_kvm_syserr(kd, kd->program, "empty file");
153 		goto failed;
154 	}
155 	if (S_ISCHR(st.st_mode)) {
156 		/*
157 		 * If this is a character special device, then check that
158 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
159 		 * make it work for either /dev/mem or /dev/kmem -- in either
160 		 * case you're working with a live kernel.)
161 		 */
162 		if (strcmp(mf, _PATH_DEVNULL) == 0) {
163 			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC);
164 			return (kd);
165 		} else if (strcmp(mf, _PATH_MEM) == 0) {
166 			if ((kd->vmfd = open(_PATH_KMEM, flag | O_CLOEXEC)) <
167 			    0) {
168 				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
169 				goto failed;
170 			}
171 			return (kd);
172 		}
173 	}
174 
175 	/*
176 	 * This is either a crash dump or a remote live system with its physical
177 	 * memory fully accessible via a special device.
178 	 * Open the namelist fd and determine the architecture.
179 	 */
180 	if ((kd->nlfd = open(uf, O_RDONLY | O_CLOEXEC, 0)) < 0) {
181 		_kvm_syserr(kd, kd->program, "%s", uf);
182 		goto failed;
183 	}
184 	if (_kvm_read_kernel_ehdr(kd) < 0)
185 		goto failed;
186 	if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0 ||
187 	    strncmp(mf, _PATH_DEVVMM, strlen(_PATH_DEVVMM)) == 0) {
188 		kd->rawdump = 1;
189 		kd->writable = 1;
190 	}
191 	SET_FOREACH(parch, kvm_arch) {
192 		if ((*parch)->ka_probe(kd)) {
193 			kd->arch = *parch;
194 			break;
195 		}
196 	}
197 	if (kd->arch == NULL) {
198 		_kvm_err(kd, kd->program, "unsupported architecture");
199 		goto failed;
200 	}
201 
202 	/*
203 	 * Non-native kernels require a symbol resolver.
204 	 */
205 	if (!kd->arch->ka_native(kd) && kd->resolve_symbol == NULL) {
206 		_kvm_err(kd, kd->program,
207 		    "non-native kernel requires a symbol resolver");
208 		goto failed;
209 	}
210 
211 	/*
212 	 * Initialize the virtual address translation machinery.
213 	 */
214 	if (kd->arch->ka_initvtop(kd) < 0)
215 		goto failed;
216 	return (kd);
217 failed:
218 	/*
219 	 * Copy out the error if doing sane error semantics.
220 	 */
221 	if (errout != NULL)
222 		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
223 	(void)kvm_close(kd);
224 	return (NULL);
225 }
226 
227 kvm_t *
228 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
229     char *errout)
230 {
231 	kvm_t *kd;
232 
233 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
234 		if (errout != NULL)
235 			(void)strlcpy(errout, strerror(errno),
236 			    _POSIX2_LINE_MAX);
237 		return (NULL);
238 	}
239 	return (_kvm_open(kd, uf, mf, flag, errout));
240 }
241 
242 kvm_t *
243 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
244     const char *errstr)
245 {
246 	kvm_t *kd;
247 
248 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
249 		if (errstr != NULL)
250 			(void)fprintf(stderr, "%s: %s\n",
251 				      errstr, strerror(errno));
252 		return (NULL);
253 	}
254 	kd->program = errstr;
255 	return (_kvm_open(kd, uf, mf, flag, NULL));
256 }
257 
258 kvm_t *
259 kvm_open2(const char *uf, const char *mf, int flag, char *errout,
260     int (*resolver)(const char *, kvaddr_t *))
261 {
262 	kvm_t *kd;
263 
264 	if ((kd = calloc(1, sizeof(*kd))) == NULL) {
265 		if (errout != NULL)
266 			(void)strlcpy(errout, strerror(errno),
267 			    _POSIX2_LINE_MAX);
268 		return (NULL);
269 	}
270 	kd->resolve_symbol = resolver;
271 	return (_kvm_open(kd, uf, mf, flag, errout));
272 }
273 
274 int
275 kvm_close(kvm_t *kd)
276 {
277 	int error = 0;
278 
279 	if (kd == NULL) {
280 		errno = EINVAL;
281 		return (-1);
282 	}
283 	if (kd->vmst != NULL)
284 		kd->arch->ka_freevtop(kd);
285 	if (kd->pmfd >= 0)
286 		error |= close(kd->pmfd);
287 	if (kd->vmfd >= 0)
288 		error |= close(kd->vmfd);
289 	if (kd->nlfd >= 0)
290 		error |= close(kd->nlfd);
291 	if (kd->procbase != 0)
292 		free((void *)kd->procbase);
293 	if (kd->argbuf != 0)
294 		free((void *) kd->argbuf);
295 	if (kd->argspc != 0)
296 		free((void *) kd->argspc);
297 	if (kd->argv != 0)
298 		free((void *)kd->argv);
299 	if (kd->dpcpu_initialized != 0)
300 		free(kd->dpcpu_off);
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 && kd->sparse_map != NULL)
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 	if (kl == NULL) {
345 		_kvm_err(kd, kd->program, "cannot allocate memory");
346 		return (-1);
347 	}
348 	for (i = 0; i < count; i++)
349 		kl[i].n_name = nl[i].n_name;
350 	nfail = kvm_nlist2(kd, kl);
351 	for (i = 0; i < count; i++) {
352 		nl[i].n_type = kl[i].n_type;
353 		nl[i].n_other = 0;
354 		nl[i].n_desc = 0;
355 		nl[i].n_value = kl[i].n_value;
356 	}
357 	free(kl);
358 	return (nfail);
359 }
360 
361 ssize_t
362 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
363 {
364 
365 	return (kvm_read2(kd, kva, buf, len));
366 }
367 
368 ssize_t
369 kvm_read2(kvm_t *kd, kvaddr_t kva, void *buf, size_t len)
370 {
371 	int cc;
372 	ssize_t cr;
373 	off_t pa;
374 	char *cp;
375 
376 	if (ISALIVE(kd)) {
377 		/*
378 		 * We're using /dev/kmem.  Just read straight from the
379 		 * device and let the active kernel do the address translation.
380 		 */
381 		errno = 0;
382 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
383 			_kvm_err(kd, 0, "invalid address (0x%jx)",
384 			    (uintmax_t)kva);
385 			return (-1);
386 		}
387 		cr = read(kd->vmfd, buf, len);
388 		if (cr < 0) {
389 			_kvm_syserr(kd, 0, "kvm_read");
390 			return (-1);
391 		} else if (cr < (ssize_t)len)
392 			_kvm_err(kd, kd->program, "short read");
393 		return (cr);
394 	}
395 
396 	cp = buf;
397 	while (len > 0) {
398 		cc = kd->arch->ka_kvatop(kd, kva, &pa);
399 		if (cc == 0)
400 			return (-1);
401 		if (cc > (ssize_t)len)
402 			cc = len;
403 		errno = 0;
404 		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
405 			_kvm_syserr(kd, 0, _PATH_MEM);
406 			break;
407 		}
408 		cr = read(kd->pmfd, cp, cc);
409 		if (cr < 0) {
410 			_kvm_syserr(kd, kd->program, "kvm_read");
411 			break;
412 		}
413 		/*
414 		 * If ka_kvatop returns a bogus value or our core file is
415 		 * truncated, we might wind up seeking beyond the end of the
416 		 * core file in which case the read will return 0 (EOF).
417 		 */
418 		if (cr == 0)
419 			break;
420 		cp += cr;
421 		kva += cr;
422 		len -= cr;
423 	}
424 
425 	return (cp - (char *)buf);
426 }
427 
428 ssize_t
429 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
430 {
431 	int cc;
432 	ssize_t cw;
433 	off_t pa;
434 	const char *cp;
435 
436 	if (!ISALIVE(kd) && !kd->writable) {
437 		_kvm_err(kd, kd->program,
438 		    "kvm_write not implemented for dead kernels");
439 		return (-1);
440 	}
441 
442 	if (ISALIVE(kd)) {
443 		/*
444 		 * Just like kvm_read, only we write.
445 		 */
446 		errno = 0;
447 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
448 			_kvm_err(kd, 0, "invalid address (%lx)", kva);
449 			return (-1);
450 		}
451 		cc = write(kd->vmfd, buf, len);
452 		if (cc < 0) {
453 			_kvm_syserr(kd, 0, "kvm_write");
454 			return (-1);
455 		} else if ((size_t)cc < len)
456 			_kvm_err(kd, kd->program, "short write");
457 		return (cc);
458 	}
459 
460 	cp = buf;
461 	while (len > 0) {
462 		cc = kd->arch->ka_kvatop(kd, kva, &pa);
463 		if (cc == 0)
464 			return (-1);
465 		if (cc > (ssize_t)len)
466 			cc = len;
467 		errno = 0;
468 		if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
469 			_kvm_syserr(kd, 0, _PATH_MEM);
470 			break;
471 		}
472 		cw = write(kd->pmfd, cp, cc);
473 		if (cw < 0) {
474 			_kvm_syserr(kd, kd->program, "kvm_write");
475 			break;
476 		}
477 		/*
478 		 * If ka_kvatop returns a bogus value or our core file is
479 		 * truncated, we might wind up seeking beyond the end of the
480 		 * core file in which case the read will return 0 (EOF).
481 		 */
482 		if (cw == 0)
483 			break;
484 		cp += cw;
485 		kva += cw;
486 		len -= cw;
487 	}
488 
489 	return (cp - (const char *)buf);
490 }
491 
492 int
493 kvm_native(kvm_t *kd)
494 {
495 
496 	if (ISALIVE(kd))
497 		return (1);
498 	return (kd->arch->ka_native(kd));
499 }
500 
501 int
502 kvm_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *closure)
503 {
504 
505 	if (kd->arch->ka_walk_pages == NULL)
506 		return (0);
507 
508 	return (kd->arch->ka_walk_pages(kd, cb, closure));
509 }
510 
511 kssize_t
512 kvm_kerndisp(kvm_t *kd)
513 {
514 	unsigned long kernbase, rel_kernbase;
515 	size_t kernbase_len = sizeof(kernbase);
516 	size_t rel_kernbase_len = sizeof(rel_kernbase);
517 
518 	if (ISALIVE(kd)) {
519 		if (sysctlbyname("kern.base_address", &kernbase,
520 		    &kernbase_len, NULL, 0) == -1) {
521 			_kvm_syserr(kd, kd->program,
522 				"failed to get kernel base address");
523 			return (0);
524 		}
525 		if (sysctlbyname("kern.relbase_address", &rel_kernbase,
526 		    &rel_kernbase_len, NULL, 0) == -1) {
527 			_kvm_syserr(kd, kd->program,
528 				"failed to get relocated kernel base address");
529 			return (0);
530 		}
531 		return (rel_kernbase - kernbase);
532 	}
533 
534 	if (kd->arch->ka_kerndisp == NULL)
535 		return (0);
536 
537 	return (kd->arch->ka_kerndisp(kd));
538 }
539