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