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 /*
27  * AMD64 machine dependent routines for kvm and minidumps.
28  */
29 
30 #include <sys/user.h>	   /* MUST BE FIRST */
31 #include <sys/param.h>
32 #include <sys/proc.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/fnv_hash.h>
36 #include <strings.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <nlist.h>
41 #include <kvm.h>
42 
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 
46 #include <machine/elf.h>
47 #include <machine/cpufunc.h>
48 #include <machine/minidump.h>
49 
50 #include <limits.h>
51 
52 #include "kvm_private.h"
53 
54 struct hpte {
55 	struct hpte *next;
56 	vm_paddr_t pa;
57 	int64_t off;
58 };
59 
60 #define HPT_SIZE 1024
61 
62 /* minidump must be the first item! */
63 struct vmstate {
64 	int minidump;		/* 1 = minidump mode */
65 	struct minidumphdr hdr;
66 	void *hpt_head[HPT_SIZE];
67 	uint64_t *bitmap;
68 	uint64_t *ptemap;
69 };
70 
71 static void
72 hpt_insert(kvm_t *kd, vm_paddr_t pa, int64_t off)
73 {
74 	struct hpte *hpte;
75 	uint32_t fnv = FNV1_32_INIT;
76 
77 	fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
78 	fnv &= (HPT_SIZE - 1);
79 	hpte = malloc(sizeof(*hpte));
80 	hpte->pa = pa;
81 	hpte->off = off;
82 	hpte->next = kd->vmst->hpt_head[fnv];
83 	kd->vmst->hpt_head[fnv] = hpte;
84 }
85 
86 static int64_t
87 hpt_find(kvm_t *kd, vm_paddr_t pa)
88 {
89 	struct hpte *hpte;
90 	uint32_t fnv = FNV1_32_INIT;
91 
92 	fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
93 	fnv &= (HPT_SIZE - 1);
94 	for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) {
95 		if (pa == hpte->pa)
96 			return (hpte->off);
97 	}
98 	return (-1);
99 }
100 
101 static int
102 inithash(kvm_t *kd, uint64_t *base, int len, off_t off)
103 {
104 	uint64_t idx;
105 	uint64_t bit, bits;
106 	vm_paddr_t pa;
107 
108 	for (idx = 0; idx < len / sizeof(*base); idx++) {
109 		bits = base[idx];
110 		while (bits) {
111 			bit = bsfq(bits);
112 			bits &= ~(1ul << bit);
113 			pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
114 			hpt_insert(kd, pa, off);
115 			off += PAGE_SIZE;
116 		}
117 	}
118 	return (off);
119 }
120 
121 void
122 _kvm_minidump_freevtop(kvm_t *kd)
123 {
124 	struct vmstate *vm = kd->vmst;
125 
126 	if (vm->bitmap)
127 		free(vm->bitmap);
128 	if (vm->ptemap)
129 		free(vm->ptemap);
130 	free(vm);
131 	kd->vmst = NULL;
132 }
133 
134 int
135 _kvm_minidump_initvtop(kvm_t *kd)
136 {
137 	struct vmstate *vmst;
138 	off_t off;
139 
140 	vmst = _kvm_malloc(kd, sizeof(*vmst));
141 	if (vmst == NULL) {
142 		_kvm_err(kd, kd->program, "cannot allocate vm");
143 		return (-1);
144 	}
145 	kd->vmst = vmst;
146 	bzero(vmst, sizeof(*vmst));
147 	vmst->minidump = 1;
148 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
149 	    sizeof(vmst->hdr)) {
150 		_kvm_err(kd, kd->program, "cannot read dump header");
151 		return (-1);
152 	}
153 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) {
154 		_kvm_err(kd, kd->program, "not a minidump for this platform");
155 		return (-1);
156 	}
157 	if (vmst->hdr.version != MINIDUMP_VERSION) {
158 		_kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d",
159 		    MINIDUMP_VERSION, vmst->hdr.version);
160 		return (-1);
161 	}
162 
163 	/* Skip header and msgbuf */
164 	off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
165 
166 	vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
167 	if (vmst->bitmap == NULL) {
168 		_kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize);
169 		return (-1);
170 	}
171 	if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
172 	    vmst->hdr.bitmapsize) {
173 		_kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize);
174 		return (-1);
175 	}
176 	off += round_page(vmst->hdr.bitmapsize);
177 
178 	vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
179 	if (vmst->ptemap == NULL) {
180 		_kvm_err(kd, kd->program, "cannot allocate %d bytes for ptemap", vmst->hdr.ptesize);
181 		return (-1);
182 	}
183 	if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
184 	    vmst->hdr.ptesize) {
185 		_kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", vmst->hdr.ptesize);
186 		return (-1);
187 	}
188 	off += vmst->hdr.ptesize;
189 
190 	/* build physical address hash table for sparse pages */
191 	inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
192 
193 	return (0);
194 }
195 
196 static int
197 _kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa)
198 {
199 	struct vmstate *vm;
200 	u_long offset;
201 	pt_entry_t pte;
202 	u_long pteindex;
203 	u_long a;
204 	off_t ofs;
205 
206 	vm = kd->vmst;
207 	offset = va & (PAGE_SIZE - 1);
208 
209 	if (va >= vm->hdr.kernbase) {
210 		pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
211 		pte = vm->ptemap[pteindex];
212 		if (((u_long)pte & X86_PG_V) == 0) {
213 			_kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
214 			goto invalid;
215 		}
216 		a = pte & PG_FRAME;
217 		ofs = hpt_find(kd, a);
218 		if (ofs == -1) {
219 			_kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a);
220 			goto invalid;
221 		}
222 		*pa = ofs + offset;
223 		return (PAGE_SIZE - offset);
224 	} else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
225 		a = (va - vm->hdr.dmapbase) & ~PAGE_MASK;
226 		ofs = hpt_find(kd, a);
227 		if (ofs == -1) {
228 			_kvm_err(kd, kd->program, "_kvm_vatop: direct map address 0x%lx not in minidump", va);
229 			goto invalid;
230 		}
231 		*pa = ofs + offset;
232 		return (PAGE_SIZE - offset);
233 	} else {
234 		_kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va);
235 		goto invalid;
236 	}
237 
238 invalid:
239 	_kvm_err(kd, 0, "invalid address (0x%lx)", va);
240 	return (0);
241 }
242 
243 int
244 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
245 {
246 	if (kvm_ishost(kd)) {
247 		_kvm_err(kd, 0, "kvm_vatop called in live kernel!");
248 		return((off_t)0);
249 	}
250 
251 	return (_kvm_minidump_vatop(kd, va, pa));
252 }
253