]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/minidump_machdep.c
Sparsify the vm_page_dump bitmap
[FreeBSD/FreeBSD.git] / sys / arm / arm / minidump_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Peter Wemm
5  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * from: FreeBSD: src/sys/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_watchdog.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/cons.h>
41 #include <sys/kernel.h>
42 #include <sys/kerneldump.h>
43 #include <sys/msgbuf.h>
44 #ifdef SW_WATCHDOG
45 #include <sys/watchdog.h>
46 #endif
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_phys.h>
51 #include <vm/pmap.h>
52 #include <machine/atomic.h>
53 #include <machine/cpu.h>
54 #include <machine/elf.h>
55 #include <machine/md_var.h>
56 #include <machine/minidump.h>
57
58 CTASSERT(sizeof(struct kerneldumpheader) == 512);
59
60 static struct kerneldumpheader kdh;
61
62 /* Handle chunked writes. */
63 static size_t fragsz;
64 static void *dump_va;
65 static uint64_t counter, progress;
66
67 static int
68 is_dumpable(vm_paddr_t pa)
69 {
70         int i;
71
72         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
73                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
74                         return (1);
75         }
76         return (0);
77 }
78
79 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
80
81 static int
82 blk_flush(struct dumperinfo *di)
83 {
84         int error;
85
86         if (fragsz == 0)
87                 return (0);
88
89         error = dump_append(di, dump_va, 0, fragsz);
90         fragsz = 0;
91         return (error);
92 }
93
94 static int
95 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
96 {
97         size_t len;
98         int error, i, c;
99         u_int maxdumpsz;
100
101         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
102         if (maxdumpsz == 0)     /* seatbelt */
103                 maxdumpsz = PAGE_SIZE;
104         error = 0;
105         if (ptr != NULL && pa != 0) {
106                 printf("cant have both va and pa!\n");
107                 return (EINVAL);
108         }
109         if (pa != 0) {
110                 if ((sz % PAGE_SIZE) != 0) {
111                         printf("size not page aligned\n");
112                         return (EINVAL);
113                 }
114                 if ((pa & PAGE_MASK) != 0) {
115                         printf("address not page aligned\n");
116                         return (EINVAL);
117                 }
118         }
119         if (ptr != NULL) {
120                 /* Flush any pre-existing pa pages before a virtual dump. */
121                 error = blk_flush(di);
122                 if (error)
123                         return (error);
124         }
125         while (sz) {
126                 len = maxdumpsz - fragsz;
127                 if (len > sz)
128                         len = sz;
129                 counter += len;
130                 progress -= len;
131                 if (counter >> 22) {
132                         printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
133                         counter &= (1<<22) - 1;
134                 }
135
136 #ifdef SW_WATCHDOG
137                 wdog_kern_pat(WD_LASTVAL);
138 #endif
139                 if (ptr) {
140                         error = dump_append(di, ptr, 0, len);
141                         if (error)
142                                 return (error);
143                         ptr += len;
144                         sz -= len;
145                 } else {
146                         for (i = 0; i < len; i += PAGE_SIZE)
147                                 dump_va = pmap_kenter_temporary(pa + i,
148                                     (i + fragsz) >> PAGE_SHIFT);
149                         fragsz += len;
150                         pa += len;
151                         sz -= len;
152                         if (fragsz == maxdumpsz) {
153                                 error = blk_flush(di);
154                                 if (error)
155                                         return (error);
156                         }
157                 }
158
159                 /* Check for user abort. */
160                 c = cncheckc();
161                 if (c == 0x03)
162                         return (ECANCELED);
163                 if (c != -1)
164                         printf(" (CTRL-C to abort) ");
165         }
166
167         return (0);
168 }
169
170 /* A buffer for general use. Its size must be one page at least. */
171 static char dumpbuf[PAGE_SIZE];
172 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
173
174 int
175 minidumpsys(struct dumperinfo *di)
176 {
177         struct minidumphdr mdhdr;
178         uint64_t dumpsize;
179         uint32_t ptesize;
180         uint32_t pa, prev_pa = 0, count = 0;
181         vm_offset_t va;
182         int error;
183         char *addr;
184
185         /*
186          * Flush caches.  Note that in the SMP case this operates only on the
187          * current CPU's L1 cache.  Before we reach this point, code in either
188          * the system shutdown or kernel debugger has called stop_cpus() to stop
189          * all cores other than this one.  Part of the ARM handling of
190          * stop_cpus() is to call wbinv_all() on that core's local L1 cache.  So
191          * by time we get to here, all that remains is to flush the L1 for the
192          * current CPU, then the L2.
193          */
194         dcache_wbinv_poc_all();
195
196         counter = 0;
197         /* Walk page table pages, set bits in vm_page_dump */
198         ptesize = 0;
199         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
200                 pa = pmap_dump_kextract(va, NULL);
201                 if (pa != 0 && is_dumpable(pa))
202                         dump_add_page(pa);
203                 ptesize += sizeof(pt2_entry_t);
204         }
205
206         /* Calculate dump size. */
207         dumpsize = ptesize;
208         dumpsize += round_page(msgbufp->msg_size);
209         dumpsize += round_page(sizeof(dump_avail));
210         dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
211         VM_PAGE_DUMP_FOREACH(pa) {
212                 /* Clear out undumpable pages now if needed */
213                 if (is_dumpable(pa))
214                         dumpsize += PAGE_SIZE;
215                 else
216                         dump_drop_page(pa);
217         }
218         dumpsize += PAGE_SIZE;
219
220         progress = dumpsize;
221
222         /* Initialize mdhdr */
223         bzero(&mdhdr, sizeof(mdhdr));
224         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
225         mdhdr.version = MINIDUMP_VERSION;
226         mdhdr.msgbufsize = msgbufp->msg_size;
227         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
228         mdhdr.ptesize = ptesize;
229         mdhdr.kernbase = KERNBASE;
230         mdhdr.arch = __ARM_ARCH;
231 #if __ARM_ARCH >= 6
232         mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
233 #else
234         mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
235 #endif
236         mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
237         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
238             dumpsize);
239
240         error = dump_start(di, &kdh);
241         if (error != 0)
242                 goto fail;
243
244         printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
245         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
246
247         /* Dump my header */
248         bzero(dumpbuf, sizeof(dumpbuf));
249         bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
250         error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
251         if (error)
252                 goto fail;
253
254         /* Dump msgbuf up front */
255         error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
256             round_page(msgbufp->msg_size));
257         if (error)
258                 goto fail;
259
260         /* Dump dump_avail */
261         _Static_assert(sizeof(dump_avail) <= sizeof(dumpbuf),
262             "Large dump_avail not handled");
263         bzero(dumpbuf, sizeof(dumpbuf));
264         memcpy(dumpbuf, dump_avail, sizeof(dump_avail));
265         error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
266         if (error)
267                 goto fail;
268
269         /* Dump bitmap */
270         error = blk_write(di, (char *)vm_page_dump, 0,
271             round_page(BITSET_SIZE(vm_page_dump_pages)));
272         if (error)
273                 goto fail;
274
275         /* Dump kernel page table pages */
276         addr = dumpbuf;
277         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
278                 pmap_dump_kextract(va, (pt2_entry_t *)addr);
279                 addr += sizeof(pt2_entry_t);
280                 if (addr == dumpbuf + sizeof(dumpbuf)) {
281                         error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
282                         if (error != 0)
283                                 goto fail;
284                         addr = dumpbuf;
285                 }
286         }
287         if (addr != dumpbuf) {
288                 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
289                 if (error != 0)
290                         goto fail;
291         }
292
293         /* Dump memory chunks */
294         VM_PAGE_DUMP_FOREACH(pa) {
295                 if (!count) {
296                         prev_pa = pa;
297                         count++;
298                 } else {
299                         if (pa == (prev_pa + count * PAGE_SIZE))
300                                 count++;
301                         else {
302                                 error = blk_write(di, NULL, prev_pa,
303                                     count * PAGE_SIZE);
304                                 if (error)
305                                         goto fail;
306                                 count = 1;
307                                 prev_pa = pa;
308                         }
309                 }
310         }
311         if (count) {
312                 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
313                 if (error)
314                         goto fail;
315                 count = 0;
316                 prev_pa = 0;
317         }
318
319         error = blk_flush(di);
320         if (error)
321                 goto fail;
322
323         error = dump_finish(di, &kdh);
324         if (error != 0)
325                 goto fail;
326
327         printf("\nDump complete\n");
328         return (0);
329
330 fail:
331         if (error < 0)
332                 error = -error;
333
334         if (error == ECANCELED)
335                 printf("\nDump aborted\n");
336         else if (error == E2BIG || error == ENOSPC) {
337                 printf("\nDump failed. Partition too small (about %lluMB were "
338                     "needed this time).\n", (long long)dumpsize >> 20);
339         } else
340                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
341         return (error);
342 }