2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Peter Wemm
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_watchdog.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/kerneldump.h>
40 #include <sys/msgbuf.h>
41 #include <sys/watchdog.h>
44 #include <machine/atomic.h>
45 #include <machine/elf.h>
46 #include <machine/md_var.h>
47 #include <machine/vmparam.h>
48 #include <machine/minidump.h>
50 CTASSERT(sizeof(struct kerneldumpheader) == 512);
52 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
53 #define DEV_ALIGN(x) roundup2((off_t)(x), DEV_BSIZE)
55 uint32_t *vm_page_dump;
56 int vm_page_dump_size;
58 static struct kerneldumpheader kdh;
60 /* Handle chunked writes. */
63 static uint64_t counter, progress;
65 CTASSERT(sizeof(*vm_page_dump) == 4);
69 is_dumpable(vm_paddr_t pa)
73 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
74 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
80 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
83 blk_flush(struct dumperinfo *di)
90 error = dump_append(di, dump_va, 0, fragsz);
96 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
102 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
103 if (maxdumpsz == 0) /* seatbelt */
104 maxdumpsz = PAGE_SIZE;
106 if ((sz % PAGE_SIZE) != 0) {
107 printf("size not page aligned\n");
110 if (ptr != NULL && pa != 0) {
111 printf("cant have both va and pa!\n");
114 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
115 printf("address not page aligned\n");
119 /* If we're doing a virtual dump, flush any pre-existing pa pages */
120 error = blk_flush(di);
125 len = maxdumpsz - fragsz;
131 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
132 counter &= (1<<24) - 1;
135 wdog_kern_pat(WD_LASTVAL);
138 error = dump_append(di, ptr, 0, len);
144 for (i = 0; i < len; i += PAGE_SIZE)
145 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
149 if (fragsz == maxdumpsz) {
150 error = blk_flush(di);
156 /* Check for user abort. */
161 printf(" (CTRL-C to abort) ");
167 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
168 static pt_entry_t fakept[NPTEPG];
171 minidumpsys(struct dumperinfo *di)
182 struct minidumphdr mdhdr;
185 /* Walk page table pages, set bits in vm_page_dump */
187 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
189 * We always write a page, even if it is zero. Each
190 * page written corresponds to 2MB of space
192 ptesize += PAGE_SIZE;
193 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
195 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
196 /* This is an entire 2M page. */
197 pa = pd[j] & PG_PS_FRAME;
198 for (k = 0; k < NPTEPG; k++) {
205 if ((pd[j] & PG_V) == PG_V) {
206 /* set bit for each valid page in this 2MB block */
207 pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
208 for (k = 0; k < NPTEPG; k++) {
209 if ((pt[k] & PG_V) == PG_V) {
210 pa = pt[k] & PG_FRAME;
216 /* nothing, we're going to dump a null page */
220 /* Calculate dump size. */
222 dumpsize += round_page(msgbufp->msg_size);
223 dumpsize += round_page(vm_page_dump_size);
224 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
225 bits = vm_page_dump[i];
228 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
229 /* Clear out undumpable pages now if needed */
230 if (is_dumpable(pa)) {
231 dumpsize += PAGE_SIZE;
235 bits &= ~(1ul << bit);
238 dumpsize += PAGE_SIZE;
242 /* Initialize mdhdr */
243 bzero(&mdhdr, sizeof(mdhdr));
244 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
245 mdhdr.version = MINIDUMP_VERSION;
246 mdhdr.msgbufsize = msgbufp->msg_size;
247 mdhdr.bitmapsize = vm_page_dump_size;
248 mdhdr.ptesize = ptesize;
249 mdhdr.kernbase = KERNBASE;
250 #if defined(PAE) || defined(PAE_TABLES)
254 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
257 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
258 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
260 error = dump_start(di, &kdh);
265 bzero(&fakept, sizeof(fakept));
266 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
267 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
271 /* Dump msgbuf up front */
272 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
277 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
281 /* Dump kernel page table pages */
282 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
283 /* We always write a page, even if it is zero */
284 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
286 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
287 /* This is a single 2M block. Generate a fake PTP */
288 pa = pd[j] & PG_PS_FRAME;
289 for (k = 0; k < NPTEPG; k++) {
290 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
292 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
295 /* flush, in case we reuse fakept in the same block */
296 error = blk_flush(di);
301 if ((pd[j] & PG_V) == PG_V) {
302 pa = pd[j] & PG_FRAME;
303 error = blk_write(di, 0, pa, PAGE_SIZE);
307 bzero(fakept, sizeof(fakept));
308 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
311 /* flush, in case we reuse fakept in the same block */
312 error = blk_flush(di);
318 /* Dump memory chunks */
319 /* XXX cluster it up and use blk_dump() */
320 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
321 bits = vm_page_dump[i];
324 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
325 error = blk_write(di, 0, pa, PAGE_SIZE);
328 bits &= ~(1ul << bit);
332 error = blk_flush(di);
336 error = dump_finish(di, &kdh);
340 printf("\nDump complete\n");
347 if (error == ECANCELED)
348 printf("\nDump aborted\n");
349 else if (error == E2BIG || error == ENOSPC)
350 printf("\nDump failed. Partition too small.\n");
352 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
357 dump_add_page(vm_paddr_t pa)
362 idx = pa >> 5; /* 2^5 = 32 */
364 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
368 dump_drop_page(vm_paddr_t pa)
373 idx = pa >> 5; /* 2^5 = 32 */
375 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);