1 /*-
2 * Copyright (c) 2006 Peter Wemm
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/7.3/sys/amd64/amd64/minidump_machdep.c 187414 2009-01-18 23:34:17Z alc $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/conf.h>
33 #include <sys/cons.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
36 #include <sys/msgbuf.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 #include <machine/atomic.h>
40 #include <machine/elf.h>
41 #include <machine/md_var.h>
42 #include <machine/vmparam.h>
43 #include <machine/minidump.h>
44
45 CTASSERT(sizeof(struct kerneldumpheader) == 512);
46
47 /*
48 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
49 * is to protect us from metadata and to protect metadata from us.
50 */
51 #define SIZEOF_METADATA (64*1024)
52
53 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
54 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
55
56 extern uint64_t KPDPphys;
57
58 uint64_t *vm_page_dump;
59 int vm_page_dump_size;
60
61 static struct kerneldumpheader kdh;
62 static off_t dumplo;
63
64 /* Handle chunked writes. */
65 static size_t fragsz;
66 static void *dump_va;
67 static size_t counter, progress;
68
69 CTASSERT(sizeof(*vm_page_dump) == 8);
70
71 static int
72 is_dumpable(vm_paddr_t pa)
73 {
74 int i;
75
76 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
77 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
78 return (1);
79 }
80 return (0);
81 }
82
83 /* XXX should be MI */
84 static void
85 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen,
86 uint32_t blksz)
87 {
88
89 bzero(kdh, sizeof(*kdh));
90 strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic));
91 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
92 kdh->version = htod32(KERNELDUMPVERSION);
93 kdh->architectureversion = htod32(archver);
94 kdh->dumplength = htod64(dumplen);
95 kdh->dumptime = htod64(time_second);
96 kdh->blocksize = htod32(blksz);
97 strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
98 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
99 if (panicstr != NULL)
100 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
101 kdh->parity = kerneldump_parity(kdh);
102 }
103
104 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
105
106 static int
107 blk_flush(struct dumperinfo *di)
108 {
109 int error;
110
111 if (fragsz == 0)
112 return (0);
113
114 error = dump_write(di, dump_va, 0, dumplo, fragsz);
115 dumplo += fragsz;
116 fragsz = 0;
117 return (error);
118 }
119
120 static int
121 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
122 {
123 size_t len;
124 int error, i, c;
125
126 error = 0;
127 if ((sz % PAGE_SIZE) != 0) {
128 printf("size not page aligned\n");
129 return (EINVAL);
130 }
131 if (ptr != NULL && pa != 0) {
132 printf("cant have both va and pa!\n");
133 return (EINVAL);
134 }
135 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
136 printf("address not page aligned\n");
137 return (EINVAL);
138 }
139 if (ptr != NULL) {
140 /* If we're doing a virtual dump, flush any pre-existing pa pages */
141 error = blk_flush(di);
142 if (error)
143 return (error);
144 }
145 while (sz) {
146 len = (MAXDUMPPGS * PAGE_SIZE) - fragsz;
147 if (len > sz)
148 len = sz;
149 counter += len;
150 progress -= len;
151 if (counter >> 24) {
152 printf(" %ld", PG2MB(progress >> PAGE_SHIFT));
153 counter &= (1<<24) - 1;
154 }
155 if (ptr) {
156 error = dump_write(di, ptr, 0, dumplo, len);
157 if (error)
158 return (error);
159 dumplo += len;
160 ptr += len;
161 sz -= len;
162 } else {
163 for (i = 0; i < len; i += PAGE_SIZE)
164 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
165 fragsz += len;
166 pa += len;
167 sz -= len;
168 if (fragsz == (MAXDUMPPGS * PAGE_SIZE)) {
169 error = blk_flush(di);
170 if (error)
171 return (error);
172 }
173 }
174
175 /* Check for user abort. */
176 c = cncheckc();
177 if (c == 0x03)
178 return (ECANCELED);
179 if (c != -1)
180 printf(" (CTRL-C to abort) ");
181 }
182
183 return (0);
184 }
185
186 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
187 static pt_entry_t fakept[NPTEPG];
188
189 void
190 minidumpsys(struct dumperinfo *di)
191 {
192 uint64_t dumpsize;
193 uint32_t ptesize;
194 vm_offset_t va;
195 int error;
196 uint64_t bits;
197 uint64_t *pdp, *pd, *pt, pa;
198 int i, j, k, bit;
199 struct minidumphdr mdhdr;
200
201 counter = 0;
202 /* Walk page table pages, set bits in vm_page_dump */
203 ptesize = 0;
204 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
205 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
206 kernel_vm_end); va += NBPDR) {
207 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
208 /*
209 * We always write a page, even if it is zero. Each
210 * page written corresponds to 2MB of space
211 */
212 ptesize += PAGE_SIZE;
213 if ((pdp[i] & PG_V) == 0)
214 continue;
215 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
216 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
217 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
218 /* This is an entire 2M page. */
219 pa = pd[j] & PG_PS_FRAME;
220 for (k = 0; k < NPTEPG; k++) {
221 if (is_dumpable(pa))
222 dump_add_page(pa);
223 pa += PAGE_SIZE;
224 }
225 continue;
226 }
227 if ((pd[j] & PG_V) == PG_V) {
228 /* set bit for each valid page in this 2MB block */
229 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
230 for (k = 0; k < NPTEPG; k++) {
231 if ((pt[k] & PG_V) == PG_V) {
232 pa = pt[k] & PG_FRAME;
233 if (is_dumpable(pa))
234 dump_add_page(pa);
235 }
236 }
237 } else {
238 /* nothing, we're going to dump a null page */
239 }
240 }
241
242 /* Calculate dump size. */
243 dumpsize = ptesize;
244 dumpsize += round_page(msgbufp->msg_size);
245 dumpsize += round_page(vm_page_dump_size);
246 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
247 bits = vm_page_dump[i];
248 while (bits) {
249 bit = bsfq(bits);
250 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
251 /* Clear out undumpable pages now if needed */
252 if (is_dumpable(pa)) {
253 dumpsize += PAGE_SIZE;
254 } else {
255 dump_drop_page(pa);
256 }
257 bits &= ~(1ul << bit);
258 }
259 }
260 dumpsize += PAGE_SIZE;
261
262 /* Determine dump offset on device. */
263 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
264 error = ENOSPC;
265 goto fail;
266 }
267 dumplo = di->mediaoffset + di->mediasize - dumpsize;
268 dumplo -= sizeof(kdh) * 2;
269 progress = dumpsize;
270
271 /* Initialize mdhdr */
272 bzero(&mdhdr, sizeof(mdhdr));
273 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
274 mdhdr.version = MINIDUMP_VERSION;
275 mdhdr.msgbufsize = msgbufp->msg_size;
276 mdhdr.bitmapsize = vm_page_dump_size;
277 mdhdr.ptesize = ptesize;
278 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
279 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
280 mdhdr.dmapend = DMAP_MAX_ADDRESS;
281
282 mkdumpheader(&kdh, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
283
284 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
285 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
286
287 /* Dump leader */
288 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
289 if (error)
290 goto fail;
291 dumplo += sizeof(kdh);
292
293 /* Dump my header */
294 bzero(&fakept, sizeof(fakept));
295 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
296 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
297 if (error)
298 goto fail;
299
300 /* Dump msgbuf up front */
301 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
302 if (error)
303 goto fail;
304
305 /* Dump bitmap */
306 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
307 if (error)
308 goto fail;
309
310 /* Dump kernel page table pages */
311 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
312 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
313 kernel_vm_end); va += NBPDR) {
314 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
315 /* We always write a page, even if it is zero */
316 if ((pdp[i] & PG_V) == 0) {
317 bzero(fakept, sizeof(fakept));
318 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
319 if (error)
320 goto fail;
321 /* flush, in case we reuse fakept in the same block */
322 error = blk_flush(di);
323 if (error)
324 goto fail;
325 continue;
326 }
327 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
328 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
329 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
330 /* This is a single 2M block. Generate a fake PTP */
331 pa = pd[j] & PG_PS_FRAME;
332 for (k = 0; k < NPTEPG; k++) {
333 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
334 }
335 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
336 if (error)
337 goto fail;
338 /* flush, in case we reuse fakept in the same block */
339 error = blk_flush(di);
340 if (error)
341 goto fail;
342 continue;
343 }
344 if ((pd[j] & PG_V) == PG_V) {
345 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
346 error = blk_write(di, (char *)pt, 0, PAGE_SIZE);
347 if (error)
348 goto fail;
349 } else {
350 bzero(fakept, sizeof(fakept));
351 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
352 if (error)
353 goto fail;
354 /* flush, in case we reuse fakept in the same block */
355 error = blk_flush(di);
356 if (error)
357 goto fail;
358 }
359 }
360
361 /* Dump memory chunks */
362 /* XXX cluster it up and use blk_dump() */
363 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
364 bits = vm_page_dump[i];
365 while (bits) {
366 bit = bsfq(bits);
367 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
368 error = blk_write(di, 0, pa, PAGE_SIZE);
369 if (error)
370 goto fail;
371 bits &= ~(1ul << bit);
372 }
373 }
374
375 error = blk_flush(di);
376 if (error)
377 goto fail;
378
379 /* Dump trailer */
380 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
381 if (error)
382 goto fail;
383 dumplo += sizeof(kdh);
384
385 /* Signal completion, signoff and exit stage left. */
386 dump_write(di, NULL, 0, 0, 0);
387 printf("\nDump complete\n");
388 return;
389
390 fail:
391 if (error < 0)
392 error = -error;
393
394 if (error == ECANCELED)
395 printf("\nDump aborted\n");
396 else if (error == ENOSPC)
397 printf("\nDump failed. Partition too small.\n");
398 else
399 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
400 }
401
402 void
403 dump_add_page(vm_paddr_t pa)
404 {
405 int idx, bit;
406
407 pa >>= PAGE_SHIFT;
408 idx = pa >> 6; /* 2^6 = 64 */
409 bit = pa & 63;
410 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
411 }
412
413 void
414 dump_drop_page(vm_paddr_t pa)
415 {
416 int idx, bit;
417
418 pa >>= PAGE_SHIFT;
419 idx = pa >> 6; /* 2^6 = 64 */
420 bit = pa & 63;
421 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);
422 }
Cache object: c1b29e0335d0cd9a25fc7e82de71db65
|