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