xref: /freebsd/lib/libkvm/kvm.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #if defined(LIBC_SCCS) && !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46 
47 #include <sys/param.h>
48 #include <sys/user.h>
49 #include <sys/proc.h>
50 #include <sys/ioctl.h>
51 #include <sys/stat.h>
52 #include <sys/sysctl.h>
53 #include <sys/linker.h>
54 
55 #include <vm/vm.h>
56 #include <vm/vm_param.h>
57 #include <vm/swap_pager.h>
58 
59 #include <machine/vmparam.h>
60 
61 #include <ctype.h>
62 #include <fcntl.h>
63 #include <kvm.h>
64 #include <limits.h>
65 #include <nlist.h>
66 #include <paths.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 
72 #include "kvm_private.h"
73 
74 /* from src/lib/libc/gen/nlist.c */
75 int __fdnlist(int, struct nlist *);
76 
77 char *
78 kvm_geterr(kd)
79 	kvm_t *kd;
80 {
81 	return (kd->errbuf);
82 }
83 
84 #include <stdarg.h>
85 
86 /*
87  * Report an error using printf style arguments.  "program" is kd->program
88  * on hard errors, and 0 on soft errors, so that under sun error emulation,
89  * only hard errors are printed out (otherwise, programs like gdb will
90  * generate tons of error messages when trying to access bogus pointers).
91  */
92 void
93 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
94 {
95 	va_list ap;
96 
97 	va_start(ap, fmt);
98 	if (program != NULL) {
99 		(void)fprintf(stderr, "%s: ", program);
100 		(void)vfprintf(stderr, fmt, ap);
101 		(void)fputc('\n', stderr);
102 	} else
103 		(void)vsnprintf(kd->errbuf,
104 		    sizeof(kd->errbuf), (char *)fmt, ap);
105 
106 	va_end(ap);
107 }
108 
109 void
110 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
111 {
112 	va_list ap;
113 	int n;
114 
115 	va_start(ap, fmt);
116 	if (program != NULL) {
117 		(void)fprintf(stderr, "%s: ", program);
118 		(void)vfprintf(stderr, fmt, ap);
119 		(void)fprintf(stderr, ": %s\n", strerror(errno));
120 	} else {
121 		char *cp = kd->errbuf;
122 
123 		(void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
124 		n = strlen(cp);
125 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
126 		    strerror(errno));
127 	}
128 	va_end(ap);
129 }
130 
131 void *
132 _kvm_malloc(kd, n)
133 	kvm_t *kd;
134 	size_t n;
135 {
136 	void *p;
137 
138 	if ((p = calloc(n, sizeof(char))) == NULL)
139 		_kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
140 			 n, strerror(errno));
141 	return (p);
142 }
143 
144 static kvm_t *
145 _kvm_open(kd, uf, mf, flag, errout)
146 	kvm_t *kd;
147 	const char *uf;
148 	const char *mf;
149 	int flag;
150 	char *errout;
151 {
152 	struct stat st;
153 
154 	kd->vmfd = -1;
155 	kd->pmfd = -1;
156 	kd->nlfd = -1;
157 	kd->vmst = 0;
158 	kd->procbase = 0;
159 	kd->argspc = 0;
160 	kd->argv = 0;
161 
162 	if (uf == 0)
163 		uf = getbootfile();
164 	else if (strlen(uf) >= MAXPATHLEN) {
165 		_kvm_err(kd, kd->program, "exec file name too long");
166 		goto failed;
167 	}
168 	if (flag & ~O_RDWR) {
169 		_kvm_err(kd, kd->program, "bad flags arg");
170 		goto failed;
171 	}
172 	if (mf == 0)
173 		mf = _PATH_MEM;
174 
175 	if ((kd->pmfd = open(mf, flag, 0)) < 0) {
176 		_kvm_syserr(kd, kd->program, "%s", mf);
177 		goto failed;
178 	}
179 	if (fstat(kd->pmfd, &st) < 0) {
180 		_kvm_syserr(kd, kd->program, "%s", mf);
181 		goto failed;
182 	}
183 	if (S_ISCHR(st.st_mode)) {
184 		/*
185 		 * If this is a character special device, then check that
186 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
187 		 * make it work for either /dev/mem or /dev/kmem -- in either
188 		 * case you're working with a live kernel.)
189 		 */
190 		if (strcmp(mf, _PATH_DEVNULL) == 0) {
191 			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
192 		} else if (strcmp(mf, _PATH_MEM) != 0) {
193 			_kvm_err(kd, kd->program,
194 				 "%s: not physical memory device", mf);
195 			goto failed;
196 		} else {
197 			if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
198 				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
199 				goto failed;
200 			}
201 		}
202 	} else {
203 		/*
204 		 * This is a crash dump.
205 		 * Initialize the virtual address translation machinery,
206 		 * but first setup the namelist fd.
207 		 */
208 		if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
209 			_kvm_syserr(kd, kd->program, "%s", uf);
210 			goto failed;
211 		}
212 		if (_kvm_initvtop(kd) < 0)
213 			goto failed;
214 	}
215 	return (kd);
216 failed:
217 	/*
218 	 * Copy out the error if doing sane error semantics.
219 	 */
220 	if (errout != 0)
221 		strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
222 	(void)kvm_close(kd);
223 	return (0);
224 }
225 
226 kvm_t *
227 kvm_openfiles(uf, mf, sf, flag, errout)
228 	const char *uf;
229 	const char *mf;
230 	const char *sf __unused;
231 	int flag;
232 	char *errout;
233 {
234 	kvm_t *kd;
235 
236 	if ((kd = malloc(sizeof(*kd))) == NULL) {
237 		(void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
238 		return (0);
239 	}
240 	memset(kd, 0, sizeof(*kd));
241 	kd->program = 0;
242 	return (_kvm_open(kd, uf, mf, flag, errout));
243 }
244 
245 kvm_t *
246 kvm_open(uf, mf, sf, flag, errstr)
247 	const char *uf;
248 	const char *mf;
249 	const char *sf __unused;
250 	int flag;
251 	const char *errstr;
252 {
253 	kvm_t *kd;
254 
255 	if ((kd = malloc(sizeof(*kd))) == NULL) {
256 		if (errstr != NULL)
257 			(void)fprintf(stderr, "%s: %s\n",
258 				      errstr, strerror(errno));
259 		return (0);
260 	}
261 	memset(kd, 0, sizeof(*kd));
262 	kd->program = errstr;
263 	return (_kvm_open(kd, uf, mf, flag, NULL));
264 }
265 
266 int
267 kvm_close(kd)
268 	kvm_t *kd;
269 {
270 	int error = 0;
271 
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->vmst)
279 		_kvm_freevtop(kd);
280 	if (kd->procbase != 0)
281 		free((void *)kd->procbase);
282 	if (kd->argv != 0)
283 		free((void *)kd->argv);
284 	free((void *)kd);
285 
286 	return (0);
287 }
288 
289 int
290 kvm_nlist(kd, nl)
291 	kvm_t *kd;
292 	struct nlist *nl;
293 {
294 	struct nlist *p;
295 	int nvalid;
296 	struct kld_sym_lookup lookup;
297 
298 	/*
299 	 * If we can't use the kld symbol lookup, revert to the
300 	 * slow library call.
301 	 */
302 	if (!ISALIVE(kd))
303 		return (__fdnlist(kd->nlfd, nl));
304 
305 	/*
306 	 * We can use the kld lookup syscall.  Go through each nlist entry
307 	 * and look it up with a kldsym(2) syscall.
308 	 */
309 	nvalid = 0;
310 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
311 		lookup.version = sizeof(lookup);
312 		lookup.symname = p->n_name;
313 		lookup.symvalue = 0;
314 		lookup.symsize = 0;
315 
316 		if (lookup.symname[0] == '_')
317 			lookup.symname++;
318 
319 		if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
320 			p->n_type = N_TEXT;
321 			p->n_other = 0;
322 			p->n_desc = 0;
323 			p->n_value = lookup.symvalue;
324 			++nvalid;
325 			/* lookup.symsize */
326 		}
327 	}
328 	/*
329 	 * Return the number of entries that weren't found.
330 	 */
331 	return ((p - nl) - nvalid);
332 }
333 
334 ssize_t
335 kvm_read(kd, kva, buf, len)
336 	kvm_t *kd;
337 	u_long kva;
338 	void *buf;
339 	size_t len;
340 {
341 	int cc;
342 	void *cp;
343 
344 	if (ISALIVE(kd)) {
345 		/*
346 		 * We're using /dev/kmem.  Just read straight from the
347 		 * device and let the active kernel do the address translation.
348 		 */
349 		errno = 0;
350 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
351 			_kvm_err(kd, 0, "invalid address (%x)", kva);
352 			return (-1);
353 		}
354 		cc = read(kd->vmfd, buf, len);
355 		if (cc < 0) {
356 			_kvm_syserr(kd, 0, "kvm_read");
357 			return (-1);
358 		} else if (cc < len)
359 			_kvm_err(kd, kd->program, "short read");
360 		return (cc);
361 	} else {
362 		cp = buf;
363 		while (len > 0) {
364 			u_long pa;
365 
366 			cc = _kvm_kvatop(kd, kva, &pa);
367 			if (cc == 0)
368 				return (-1);
369 			if (cc > len)
370 				cc = len;
371 			errno = 0;
372 			if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
373 				_kvm_syserr(kd, 0, _PATH_MEM);
374 				break;
375 			}
376 			cc = read(kd->pmfd, cp, cc);
377 			if (cc < 0) {
378 				_kvm_syserr(kd, kd->program, "kvm_read");
379 				break;
380 			}
381 			/*
382 			 * If kvm_kvatop returns a bogus value or our core
383 			 * file is truncated, we might wind up seeking beyond
384 			 * the end of the core file in which case the read will
385 			 * return 0 (EOF).
386 			 */
387 			if (cc == 0)
388 				break;
389 			(char *)cp += cc;
390 			kva += cc;
391 			len -= cc;
392 		}
393 		return ((char *)cp - (char *)buf);
394 	}
395 	/* NOTREACHED */
396 }
397 
398 ssize_t
399 kvm_write(kd, kva, buf, len)
400 	kvm_t *kd;
401 	u_long kva;
402 	const void *buf;
403 	size_t len;
404 {
405 	int cc;
406 
407 	if (ISALIVE(kd)) {
408 		/*
409 		 * Just like kvm_read, only we write.
410 		 */
411 		errno = 0;
412 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
413 			_kvm_err(kd, 0, "invalid address (%x)", kva);
414 			return (-1);
415 		}
416 		cc = write(kd->vmfd, buf, len);
417 		if (cc < 0) {
418 			_kvm_syserr(kd, 0, "kvm_write");
419 			return (-1);
420 		} else if (cc < len)
421 			_kvm_err(kd, kd->program, "short write");
422 		return (cc);
423 	} else {
424 		_kvm_err(kd, kd->program,
425 		    "kvm_write not implemented for dead kernels");
426 		return (-1);
427 	}
428 	/* NOTREACHED */
429 }
430