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