xref: /freebsd/lib/libkvm/kvm_minidump_i386.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 /*
30  * i386 machine dependent routines for kvm and minidumps.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <kvm.h>
40 
41 #include "../../sys/i386/include/minidump.h"
42 
43 #include <limits.h>
44 
45 #include "kvm_private.h"
46 #include "kvm_i386.h"
47 
48 #define	i386_round_page(x)	roundup2((kvaddr_t)(x), I386_PAGE_SIZE)
49 
50 struct vmstate {
51 	struct minidumphdr hdr;
52 	struct hpt hpt;
53 	void *ptemap;
54 };
55 
56 static int
57 _i386_minidump_probe(kvm_t *kd)
58 {
59 
60 	return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_386) &&
61 	    _kvm_is_minidump(kd));
62 }
63 
64 static void
65 _i386_minidump_freevtop(kvm_t *kd)
66 {
67 	struct vmstate *vm = kd->vmst;
68 
69 	_kvm_hpt_free(&vm->hpt);
70 	if (vm->ptemap)
71 		free(vm->ptemap);
72 	free(vm);
73 	kd->vmst = NULL;
74 }
75 
76 static int
77 _i386_minidump_initvtop(kvm_t *kd)
78 {
79 	struct vmstate *vmst;
80 	uint32_t *bitmap;
81 	off_t off;
82 
83 	vmst = _kvm_malloc(kd, sizeof(*vmst));
84 	if (vmst == 0) {
85 		_kvm_err(kd, kd->program, "cannot allocate vm");
86 		return (-1);
87 	}
88 	kd->vmst = vmst;
89 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
90 	    sizeof(vmst->hdr)) {
91 		_kvm_err(kd, kd->program, "cannot read dump header");
92 		return (-1);
93 	}
94 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) {
95 		_kvm_err(kd, kd->program, "not a minidump for this platform");
96 		return (-1);
97 	}
98 	vmst->hdr.version = le32toh(vmst->hdr.version);
99 	if (vmst->hdr.version != MINIDUMP_VERSION) {
100 		_kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d",
101 		    MINIDUMP_VERSION, vmst->hdr.version);
102 		return (-1);
103 	}
104 	vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize);
105 	vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize);
106 	vmst->hdr.ptesize = le32toh(vmst->hdr.ptesize);
107 	vmst->hdr.kernbase = le32toh(vmst->hdr.kernbase);
108 	vmst->hdr.paemode = le32toh(vmst->hdr.paemode);
109 
110 	/* Skip header and msgbuf */
111 	off = I386_PAGE_SIZE + i386_round_page(vmst->hdr.msgbufsize);
112 
113 	bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
114 	if (bitmap == NULL) {
115 		_kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize);
116 		return (-1);
117 	}
118 	if (pread(kd->pmfd, bitmap, vmst->hdr.bitmapsize, off) !=
119 	    (ssize_t)vmst->hdr.bitmapsize) {
120 		_kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize);
121 		free(bitmap);
122 		return (-1);
123 	}
124 	off += i386_round_page(vmst->hdr.bitmapsize);
125 
126 	vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
127 	if (vmst->ptemap == NULL) {
128 		_kvm_err(kd, kd->program, "cannot allocate %d bytes for ptemap", vmst->hdr.ptesize);
129 		free(bitmap);
130 		return (-1);
131 	}
132 	if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
133 	    (ssize_t)vmst->hdr.ptesize) {
134 		_kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", vmst->hdr.ptesize);
135 		free(bitmap);
136 		return (-1);
137 	}
138 	off += vmst->hdr.ptesize;
139 
140 	/* build physical address hash table for sparse pages */
141 	_kvm_hpt_init(kd, &vmst->hpt, bitmap, vmst->hdr.bitmapsize, off,
142 	    I386_PAGE_SIZE, sizeof(*bitmap));
143 	free(bitmap);
144 
145 	return (0);
146 }
147 
148 static int
149 _i386_minidump_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa)
150 {
151 	struct vmstate *vm;
152 	i386_physaddr_pae_t offset;
153 	i386_pte_pae_t pte;
154 	kvaddr_t pteindex;
155 	i386_physaddr_pae_t a;
156 	off_t ofs;
157 	i386_pte_pae_t *ptemap;
158 
159 	vm = kd->vmst;
160 	ptemap = vm->ptemap;
161 	offset = va & I386_PAGE_MASK;
162 
163 	if (va >= vm->hdr.kernbase) {
164 		pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
165 		pte = le64toh(ptemap[pteindex]);
166 		if ((pte & I386_PG_V) == 0) {
167 			_kvm_err(kd, kd->program,
168 			    "_i386_minidump_vatop_pae: pte not valid");
169 			goto invalid;
170 		}
171 		a = pte & I386_PG_FRAME_PAE;
172 		ofs = _kvm_hpt_find(&vm->hpt, a);
173 		if (ofs == -1) {
174 			_kvm_err(kd, kd->program,
175 	    "_i386_minidump_vatop_pae: physical address 0x%jx not in minidump",
176 			    (uintmax_t)a);
177 			goto invalid;
178 		}
179 		*pa = ofs + offset;
180 		return (I386_PAGE_SIZE - offset);
181 	} else {
182 		_kvm_err(kd, kd->program,
183 	    "_i386_minidump_vatop_pae: virtual address 0x%jx not minidumped",
184 		    (uintmax_t)va);
185 		goto invalid;
186 	}
187 
188 invalid:
189 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
190 	return (0);
191 }
192 
193 static int
194 _i386_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
195 {
196 	struct vmstate *vm;
197 	i386_physaddr_t offset;
198 	i386_pte_t pte;
199 	kvaddr_t pteindex;
200 	i386_physaddr_t a;
201 	off_t ofs;
202 	i386_pte_t *ptemap;
203 
204 	vm = kd->vmst;
205 	ptemap = vm->ptemap;
206 	offset = va & I386_PAGE_MASK;
207 
208 	if (va >= vm->hdr.kernbase) {
209 		pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
210 		pte = le32toh(ptemap[pteindex]);
211 		if ((pte & I386_PG_V) == 0) {
212 			_kvm_err(kd, kd->program,
213 			    "_i386_minidump_vatop: pte not valid");
214 			goto invalid;
215 		}
216 		a = pte & I386_PG_FRAME;
217 		ofs = _kvm_hpt_find(&vm->hpt, a);
218 		if (ofs == -1) {
219 			_kvm_err(kd, kd->program,
220 	    "_i386_minidump_vatop: physical address 0x%jx not in minidump",
221 			    (uintmax_t)a);
222 			goto invalid;
223 		}
224 		*pa = ofs + offset;
225 		return (I386_PAGE_SIZE - offset);
226 	} else {
227 		_kvm_err(kd, kd->program,
228 	    "_i386_minidump_vatop: virtual address 0x%jx not minidumped",
229 		    (uintmax_t)va);
230 		goto invalid;
231 	}
232 
233 invalid:
234 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
235 	return (0);
236 }
237 
238 static int
239 _i386_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
240 {
241 
242 	if (ISALIVE(kd)) {
243 		_kvm_err(kd, 0, "_i386_minidump_kvatop called in live kernel!");
244 		return (0);
245 	}
246 	if (kd->vmst->hdr.paemode)
247 		return (_i386_minidump_vatop_pae(kd, va, pa));
248 	else
249 		return (_i386_minidump_vatop(kd, va, pa));
250 }
251 
252 struct kvm_arch kvm_i386_minidump = {
253 	.ka_probe = _i386_minidump_probe,
254 	.ka_initvtop = _i386_minidump_initvtop,
255 	.ka_freevtop = _i386_minidump_freevtop,
256 	.ka_kvatop = _i386_minidump_kvatop,
257 	.ka_native = _i386_native,
258 };
259 
260 KVM_ARCH(kvm_i386_minidump);
261