2 * Copyright (c) 2006 Peter Wemm
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/sys/amd64/amd64/minidump_machdep.c,v 1.10 2009/05/29 21:27:12 jamie Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/globaldata.h>
35 #include <sys/kernel.h>
36 #include <sys/kerneldump.h>
37 #include <sys/msgbuf.h>
40 #include <vm/vm_kern.h>
42 #include <machine/atomic.h>
43 #include <machine/elf.h>
44 #include <machine/globaldata.h>
45 #include <machine/md_var.h>
46 #include <machine/vmparam.h>
47 #include <machine/minidump.h>
49 CTASSERT(sizeof(struct kerneldumpheader) == 512);
52 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
53 * is to protect us from metadata and to protect metadata from us.
55 #define SIZEOF_METADATA (64*1024)
57 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
58 #define DEV_ALIGN(x) roundup2((off_t)(x), DEV_BSIZE)
60 extern uint64_t KPDPphys;
62 uint64_t *vm_page_dump;
63 vm_offset_t vm_page_dump_size;
65 static struct kerneldumpheader kdh;
68 /* Handle chunked writes. */
71 static size_t counter, progress;
73 CTASSERT(sizeof(*vm_page_dump) == 8);
76 is_dumpable(vm_paddr_t pa)
80 for (i = 0; dump_avail[i].phys_beg || dump_avail[i].phys_end; ++i) {
81 if (pa >= dump_avail[i].phys_beg && pa < dump_avail[i].phys_end)
87 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
90 blk_flush(struct dumperinfo *di)
97 error = dev_ddump(di->priv, dump_va, 0, dumplo, fragsz);
104 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
111 if ((sz & PAGE_MASK)) {
112 kprintf("size not page aligned\n");
115 if (ptr != NULL && pa != 0) {
116 kprintf("can't have both va and pa!\n");
119 if (pa != 0 && (((uintptr_t)pa) & PAGE_MASK) != 0) {
120 kprintf("address not page aligned\n");
125 * If we're doing a virtual dump, flush any
126 * pre-existing pa pages
128 error = blk_flush(di);
132 max_iosize = min(MAXPHYS, di->maxiosize);
134 len = max_iosize - fragsz;
140 kprintf(" %ld", PG2MB(progress >> PAGE_SHIFT));
141 counter &= (1<<24) - 1;
145 error = dev_ddump(di->priv, ptr, 0, dumplo, len);
153 for (i = 0; i < len; i += PAGE_SIZE) {
154 dump_va = pmap_kenter_temporary(pa + i,
155 (i + fragsz) >> PAGE_SHIFT);
161 if (fragsz == max_iosize) {
162 error = blk_flush(di);
169 /* Check for user abort. */
173 if (c != -1 && c != NOKEY)
174 kprintf(" (CTRL-C to abort) ");
179 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
180 static pt_entry_t fakept[NPTEPG];
183 minidumpsys(struct dumperinfo *di)
188 vm_offset_t kern_end;
191 uint64_t *pdp, *pd, *pt, pa;
196 struct minidumphdr2 mdhdr;
197 struct mdglobaldata *md;
203 * minidump page table format is an array of PD entries (1GB pte's),
204 * representing the entire user and kernel virtual address space
207 * However, we will only dump the KVM portion of this space. And we
208 * only copy the PDP pages for direct access, the PD and PT pages
209 * will be included in the dump as part of the physical map.
211 ptesize = NPML4EPG * NPDPEPG * 8;
214 * Walk page table pages, set bits in vm_page_dump.
216 * NOTE: kernel_vm_end can actually be below KERNBASE.
217 * Just use KvaEnd. Also note that loops which go
218 * all the way to the end of the address space might
219 * overflow the loop variable.
221 md = (struct mdglobaldata *)globaldata_find(0);
224 if (kern_end < (vm_offset_t)&(md[ncpus]))
225 kern_end = (vm_offset_t)&(md[ncpus]);
227 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
228 for (va = VM_MIN_KERNEL_ADDRESS; va < kern_end; va += NBPDR) {
230 * The loop probably overflows a 64-bit int due to NBPDR.
232 if (va < VM_MIN_KERNEL_ADDRESS)
236 * KPDPphys[] is relative to VM_MIN_KERNEL_ADDRESS. It
237 * contains NKPML4E PDP pages (so we can get to all kernel
238 * PD entries from this array).
240 i = ((va - VM_MIN_KERNEL_ADDRESS) >> PDPSHIFT) &
241 (NPML4EPG * NPDPEPG - 1);
248 * Calculate the PD index in the PDP. Each PD represents 1GB.
249 * KVA space can cover multiple PDP pages. The PDP array
250 * has been initialized for the entire kernel address space.
252 * We include the PD entries in the PDP in the dump
254 i = ((va - VM_MIN_KERNEL_ADDRESS) >> PDPSHIFT) &
255 (NPML4EPG * NPDPEPG - 1);
256 if ((pdp[i] & kernel_pmap.pmap_bits[PG_V_IDX]) == 0)
260 * Add the PD page from the PDP to the dump
262 dump_add_page(pdp[i] & PG_FRAME);
263 lpdpttl += PAGE_SIZE;
265 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
266 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
267 if ((pd[j] & (kernel_pmap.pmap_bits[PG_PS_IDX] | kernel_pmap.pmap_bits[PG_V_IDX])) ==
268 (kernel_pmap.pmap_bits[PG_PS_IDX] | kernel_pmap.pmap_bits[PG_V_IDX])) {
269 /* This is an entire 2M page. */
270 lpdpttl += PAGE_SIZE * NPTEPG;
271 pa = pd[j] & PG_PS_FRAME;
272 for (k = 0; k < NPTEPG; k++) {
279 if ((pd[j] & kernel_pmap.pmap_bits[PG_V_IDX]) ==
280 kernel_pmap.pmap_bits[PG_V_IDX]) {
282 * Add the PT page from the PD to the dump (it is no
283 * longer included in the ptemap.
285 dump_add_page(pd[j] & PG_FRAME);
286 lpdpttl += PAGE_SIZE;
288 /* set bit for each valid page in this 2MB block */
289 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
290 for (k = 0; k < NPTEPG; k++) {
291 if ((pt[k] & kernel_pmap.pmap_bits[PG_V_IDX]) == kernel_pmap.pmap_bits[PG_V_IDX]) {
292 pa = pt[k] & PG_FRAME;
293 lpdpttl += PAGE_SIZE;
299 /* nothing, we're going to dump a null page */
303 /* Calculate dump size. */
305 dumpsize += round_page(msgbufp->msg_size);
306 dumpsize += round_page(vm_page_dump_size);
308 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
309 bits = vm_page_dump[i];
312 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
313 /* Clear out undumpable pages now if needed */
314 if (is_dumpable(pa)) {
315 dumpsize += PAGE_SIZE;
319 bits &= ~(1ul << bit);
322 dumpsize += PAGE_SIZE;
324 /* Determine dump offset on device. */
325 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
329 dumplo = di->mediaoffset + di->mediasize - dumpsize;
330 dumplo -= sizeof(kdh) * 2;
333 /* Initialize mdhdr */
334 bzero(&mdhdr, sizeof(mdhdr));
335 strcpy(mdhdr.magic, MINIDUMP2_MAGIC);
336 mdhdr.version = MINIDUMP2_VERSION;
337 mdhdr.msgbufsize = msgbufp->msg_size;
338 mdhdr.bitmapsize = vm_page_dump_size;
339 mdhdr.ptesize = ptesize;
340 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
341 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
342 mdhdr.dmapend = DMAP_MAX_ADDRESS;
344 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION,
345 dumpsize, di->blocksize);
347 kprintf("Physical memory: %jd MB\n", (intmax_t)ptoa(physmem) / 1048576);
348 kprintf("Dumping %jd MB:", (intmax_t)dumpsize >> 20);
351 error = dev_ddump(di->priv, &kdh, 0, dumplo, sizeof(kdh));
354 dumplo += sizeof(kdh);
357 bzero(fakept, sizeof(fakept));
358 bcopy(&mdhdr, fakept, sizeof(mdhdr));
359 error = blk_write(di, (char *)fakept, 0, PAGE_SIZE);
363 /* Dump msgbuf up front */
364 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
369 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
374 * Dump a full PDP array for the entire KVM space, user and kernel.
375 * This is 512*512 1G PD entries (512*512*8 = 2MB).
377 * The minidump only dumps PD entries related to KVA space. Also
378 * note that pdp[] (aka KPDPphys[]) only covers VM_MIN_KERNEL_ADDRESS
379 * to VM_MAX_KERNEL_ADDRESS.
381 * The actual KPDPphys[] array covers a KVA space starting at KVA
384 * By dumping a PDP[] array of PDs representing the entire virtual
385 * address space we can expand what we dump in the future.
387 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
388 kpdp = (KPDPPHYS_KVA >> PDPSHIFT) &
389 (NPML4EPG * NPDPEPG - 1);
390 klo = (int)(VM_MIN_KERNEL_ADDRESS >> PDPSHIFT) &
391 (NPML4EPG * NPDPEPG - 1);
392 khi = (int)(VM_MAX_KERNEL_ADDRESS >> PDPSHIFT) &
393 (NPML4EPG * NPDPEPG - 1);
395 for (i = 0; i < NPML4EPG * NPDPEPG; ++i) {
396 if (i < klo || i > khi) {
397 fakept[i & (NPDPEPG - 1)] = 0;
399 fakept[i & (NPDPEPG - 1)] = pdp[i - kpdp];
401 if ((i & (NPDPEPG - 1)) == (NPDPEPG - 1)) {
402 error = blk_write(di, (char *)fakept, 0, PAGE_SIZE);
405 error = blk_flush(di);
411 /* Dump memory chunks */
412 /* XXX cluster it up and use blk_dump() */
413 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
414 bits = vm_page_dump[i];
417 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
418 error = blk_write(di, 0, pa, PAGE_SIZE);
421 bits &= ~(1ul << bit);
425 error = blk_flush(di);
430 error = dev_ddump(di->priv, &kdh, 0, dumplo, sizeof(kdh));
433 dumplo += sizeof(kdh);
435 /* Signal completion, signoff and exit stage left. */
436 dev_ddump(di->priv, NULL, 0, 0, 0);
437 kprintf("\nDump complete\n");
446 if (error == ECANCELED)
447 kprintf("\nDump aborted\n");
448 else if (error == ENOSPC)
449 kprintf("\nDump failed. Partition too small.\n");
451 kprintf("\n** DUMP FAILED (ERROR %d) **\n", error);
455 dump_add_page(vm_paddr_t pa)
460 idx = pa >> 6; /* 2^6 = 64 */
462 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
466 dump_drop_page(vm_paddr_t pa)
471 idx = pa >> 6; /* 2^6 = 64 */
473 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);