kernel - SWAP CACHE part 22/many - Fix counter overflow introduced in part 21
[dragonfly.git] / lib / libkvm / kvm_minidump_x86_64.c
blob6a1484948eb945dabf2ed1f326937c9a3aab69e5
1 /*-
2 * Copyright (c) 2006 Peter Wemm
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.
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.
26 #include <sys/cdefs.h>
29 * AMD64 machine dependent routines for kvm and minidumps.
32 #include <sys/param.h>
33 #include <sys/user.h>
34 #include <sys/proc.h>
35 #include <sys/stat.h>
36 #include <sys/mman.h>
37 #include <sys/fnv_hash.h>
38 #include <strings.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <nlist.h>
43 #include <kvm.h>
45 #include <vm/vm.h>
46 #include <vm/vm_param.h>
48 #include <machine/elf.h>
49 #include <machine/cpufunc.h>
50 #include <machine/minidump.h>
52 #include <limits.h>
54 #include "kvm_private.h"
56 struct hpte {
57 struct hpte *next;
58 vm_paddr_t pa;
59 int64_t off;
62 #define HPT_SIZE 1024
64 /* minidump must be the first item! */
65 struct vmstate {
66 int minidump; /* 1 = minidump mode */
67 struct minidumphdr hdr;
68 void *hpt_head[HPT_SIZE];
69 uint64_t *bitmap;
70 uint64_t *ptemap;
73 static void
74 hpt_insert(kvm_t *kd, vm_paddr_t pa, int64_t off)
76 struct hpte *hpte;
77 uint32_t fnv = FNV1_32_INIT;
79 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
80 fnv &= (HPT_SIZE - 1);
81 hpte = malloc(sizeof(*hpte));
82 hpte->pa = pa;
83 hpte->off = off;
84 hpte->next = kd->vmst->hpt_head[fnv];
85 kd->vmst->hpt_head[fnv] = hpte;
88 static int64_t
89 hpt_find(kvm_t *kd, vm_paddr_t pa)
91 struct hpte *hpte;
92 uint32_t fnv = FNV1_32_INIT;
94 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
95 fnv &= (HPT_SIZE - 1);
96 for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) {
97 if (pa == hpte->pa)
98 return (hpte->off);
100 return (-1);
103 static int
104 inithash(kvm_t *kd, uint64_t *base, int len, off_t off)
106 uint64_t idx;
107 uint64_t bit, bits;
108 vm_paddr_t pa;
110 for (idx = 0; idx < len / sizeof(*base); idx++) {
111 bits = base[idx];
112 while (bits) {
113 bit = bsfq(bits);
114 bits &= ~(1ul << bit);
115 pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
116 hpt_insert(kd, pa, off);
117 off += PAGE_SIZE;
120 return (off);
123 void
124 _kvm_minidump_freevtop(kvm_t *kd)
126 struct vmstate *vm = kd->vmst;
128 if (vm->bitmap)
129 free(vm->bitmap);
130 if (vm->ptemap)
131 free(vm->ptemap);
132 free(vm);
133 kd->vmst = NULL;
137 _kvm_minidump_initvtop(kvm_t *kd)
139 struct vmstate *vmst;
140 off_t off;
142 vmst = _kvm_malloc(kd, sizeof(*vmst));
143 if (vmst == 0) {
144 _kvm_err(kd, kd->program, "cannot allocate vm");
145 return (-1);
147 kd->vmst = vmst;
148 bzero(vmst, sizeof(*vmst));
149 vmst->minidump = 1;
150 if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
151 sizeof(vmst->hdr)) {
152 _kvm_err(kd, kd->program, "cannot read dump header");
153 return (-1);
155 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) {
156 _kvm_err(kd, kd->program, "not a minidump for this platform");
157 return (-1);
159 if (vmst->hdr.version != MINIDUMP_VERSION) {
160 _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d",
161 MINIDUMP_VERSION, vmst->hdr.version);
162 return (-1);
165 /* Skip header and msgbuf */
166 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
168 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
169 if (vmst->bitmap == NULL) {
170 _kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize);
171 return (-1);
173 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
174 vmst->hdr.bitmapsize) {
175 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize);
176 return (-1);
178 off += round_page(vmst->hdr.bitmapsize);
180 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
181 if (vmst->ptemap == NULL) {
182 _kvm_err(kd, kd->program, "cannot allocate %d bytes for ptemap", vmst->hdr.ptesize);
183 return (-1);
185 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
186 vmst->hdr.ptesize) {
187 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", vmst->hdr.ptesize);
188 return (-1);
190 off += vmst->hdr.ptesize;
192 /* build physical address hash table for sparse pages */
193 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
195 return (0);
198 static int
199 _kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa)
201 struct vmstate *vm;
202 u_long offset;
203 pt_entry_t pte;
204 u_long pteindex;
205 u_long a;
206 off_t ofs;
208 vm = kd->vmst;
209 offset = va & (PAGE_SIZE - 1);
211 if (va >= vm->hdr.kernbase) {
212 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
213 pte = vm->ptemap[pteindex];
214 if (((u_long)pte & PG_V) == 0) {
215 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
216 goto invalid;
218 a = pte & PG_FRAME;
219 ofs = hpt_find(kd, a);
220 if (ofs == -1) {
221 _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a);
222 goto invalid;
224 *pa = ofs + offset;
225 return (PAGE_SIZE - offset);
226 } else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
227 a = (va - vm->hdr.dmapbase) & ~PAGE_MASK;
228 ofs = hpt_find(kd, a);
229 if (ofs == -1) {
230 _kvm_err(kd, kd->program, "_kvm_vatop: direct map address 0x%lx not in minidump", va);
231 goto invalid;
233 *pa = ofs + offset;
234 return (PAGE_SIZE - offset);
235 } else {
236 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va);
237 goto invalid;
240 invalid:
241 _kvm_err(kd, 0, "invalid address (0x%lx)", va);
242 return (0);
246 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
248 if (kvm_ishost(kd)) {
249 _kvm_err(kd, 0, "kvm_vatop called in live kernel!");
250 return((off_t)0);
253 return (_kvm_minidump_vatop(kd, va, pa));