]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/minidump_machdep.c
Sparsify the vm_page_dump bitmap
[FreeBSD/FreeBSD.git] / sys / mips / mips / minidump_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Oleksandr Tymoshenko <gonzo@freebsd.org>
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/arm/arm/minidump_machdep.c v214223
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/cons.h>
39 #include <sys/kernel.h>
40 #include <sys/kerneldump.h>
41 #include <sys/msgbuf.h>
42 #include <sys/watchdog.h>
43 #include <sys/vmmeter.h>
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_phys.h>
48 #include <machine/atomic.h>
49 #include <machine/elf.h>
50 #include <machine/md_var.h>
51 #include <machine/vmparam.h>
52 #include <machine/minidump.h>
53 #include <machine/cache.h>
54
55 CTASSERT(sizeof(struct kerneldumpheader) == 512);
56
57 static struct kerneldumpheader kdh;
58
59 /* Handle chunked writes. */
60 static uint64_t counter, progress, dumpsize;
61 /* Just auxiliary bufffer */
62 static char tmpbuffer[PAGE_SIZE];
63
64 extern pd_entry_t *kernel_segmap;
65
66 static int
67 is_dumpable(vm_paddr_t pa)
68 {
69         vm_page_t m;
70         int i;
71
72         if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
73                 return ((m->flags & PG_NODUMP) == 0);
74         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
75                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
76                         return (1);
77         }
78         return (0);
79 }
80
81 static struct {
82         int min_per;
83         int max_per;
84         int visited;
85 } progress_track[10] = {
86         {  0,  10, 0},
87         { 10,  20, 0},
88         { 20,  30, 0},
89         { 30,  40, 0},
90         { 40,  50, 0},
91         { 50,  60, 0},
92         { 60,  70, 0},
93         { 70,  80, 0},
94         { 80,  90, 0},
95         { 90, 100, 0}
96 };
97
98 static void
99 report_progress(uint64_t progress, uint64_t dumpsize)
100 {
101         int sofar, i;
102
103         sofar = 100 - ((progress * 100) / dumpsize);
104         for (i = 0; i < nitems(progress_track); i++) {
105                 if (sofar < progress_track[i].min_per ||
106                     sofar > progress_track[i].max_per)
107                         continue;
108                 if (progress_track[i].visited)
109                         return;
110                 progress_track[i].visited = 1;
111                 printf("..%d%%", sofar);
112                 return;
113         }
114 }
115
116 static int
117 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
118 {
119         size_t len;
120         int error, c;
121         u_int maxdumpsz;
122
123         maxdumpsz = di->maxiosize;
124
125         if (maxdumpsz == 0)     /* seatbelt */
126                 maxdumpsz = PAGE_SIZE;
127
128         error = 0;
129
130         while (sz) {
131                 len = min(maxdumpsz, sz);
132                 counter += len;
133                 progress -= len;
134
135                 if (counter >> 22) {
136                         report_progress(progress, dumpsize);
137                         counter &= (1<<22) - 1;
138                 }
139
140                 wdog_kern_pat(WD_LASTVAL);
141
142                 if (ptr) {
143                         error = dump_append(di, ptr, 0, len);
144                         if (error)
145                                 return (error);
146                         ptr += len;
147                         sz -= len;
148                 } else {
149                         panic("pa is not supported");
150                 }
151
152                 /* Check for user abort. */
153                 c = cncheckc();
154                 if (c == 0x03)
155                         return (ECANCELED);
156                 if (c != -1)
157                         printf(" (CTRL-C to abort) ");
158         }
159
160         return (0);
161 }
162
163 int
164 minidumpsys(struct dumperinfo *di)
165 {
166         struct minidumphdr mdhdr;
167         uint32_t ptesize;
168         vm_paddr_t pa;
169         vm_offset_t prev_pte = 0;
170         uint32_t count = 0;
171         vm_offset_t va;
172         pt_entry_t *pte;
173         int i, error;
174         void *dump_va;
175
176         /* Flush cache */
177         mips_dcache_wbinv_all();
178
179         counter = 0;
180         /* Walk page table pages, set bits in vm_page_dump */
181         ptesize = 0;
182
183         for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
184                 ptesize += PAGE_SIZE;
185                 pte = pmap_pte(kernel_pmap, va);
186                 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
187                 for (i = 0; i < NPTEPG; i++) {
188                         if (pte_test(&pte[i], PTE_V)) {
189                                 pa = TLBLO_PTE_TO_PA(pte[i]);
190                                 if (is_dumpable(pa))
191                                         dump_add_page(pa);
192                         }
193                 }
194         }
195
196         /*
197          * Now mark pages from 0 to phys_avail[0], that's where kernel 
198          * and pages allocated by pmap_steal reside
199          */
200         for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
201                 if (is_dumpable(pa))
202                         dump_add_page(pa);
203         }
204
205         /* Calculate dump size. */
206         dumpsize = ptesize;
207         dumpsize += round_page(msgbufp->msg_size);
208         dumpsize += round_page(sizeof(dump_avail));
209         dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
210         VM_PAGE_DUMP_FOREACH(pa) {
211                 /* Clear out undumpable pages now if needed */
212                 if (is_dumpable(pa))
213                         dumpsize += PAGE_SIZE;
214                 else
215                         dump_drop_page(pa);
216         }
217         dumpsize += PAGE_SIZE;
218
219         progress = dumpsize;
220
221         /* Initialize mdhdr */
222         bzero(&mdhdr, sizeof(mdhdr));
223         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
224         mdhdr.version = MINIDUMP_VERSION;
225         mdhdr.msgbufsize = msgbufp->msg_size;
226         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
227         mdhdr.ptesize = ptesize;
228         mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
229         mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
230
231         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION,
232             dumpsize);
233
234         error = dump_start(di, &kdh);
235         if (error != 0)
236                 goto fail;
237
238         printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
239             ptoa((uintmax_t)physmem) / 1048576);
240
241         /* Dump my header */
242         bzero(tmpbuffer, sizeof(tmpbuffer));
243         bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
244         error = write_buffer(di, tmpbuffer, PAGE_SIZE);
245         if (error)
246                 goto fail;
247
248         /* Dump msgbuf up front */
249         error = write_buffer(di, (char *)msgbufp->msg_ptr, 
250             round_page(msgbufp->msg_size));
251         if (error)
252                 goto fail;
253
254         /* Dump dump_avail */
255         _Static_assert(sizeof(dump_avail) <= sizeof(tmpbuffer),
256             "Large dump_avail not handled");
257         bzero(tmpbuffer, sizeof(tmpbuffer));
258         memcpy(tmpbuffer, dump_avail, sizeof(dump_avail));
259         error = write_buffer(di, tmpbuffer, PAGE_SIZE);
260         if (error)
261                 goto fail;
262
263         /* Dump bitmap */
264         error = write_buffer(di, (char *)vm_page_dump,
265             round_page(BITSET_SIZE(vm_page_dump_pages)));
266         if (error)
267                 goto fail;
268
269         /* Dump kernel page table pages */
270         for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
271                 pte = pmap_pte(kernel_pmap, va);
272                 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
273                 if (!count) {
274                         prev_pte = (vm_offset_t)pte;
275                         count++;
276                 } else {
277                         if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
278                                 count++;
279                         else {
280                                 error = write_buffer(di, (char*)prev_pte,
281                                     count * PAGE_SIZE);
282                                 if (error)
283                                         goto fail;
284                                 count = 1;
285                                 prev_pte = (vm_offset_t)pte;
286                         }
287                 }
288         }
289
290         if (count) {
291                 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
292                 if (error)
293                         goto fail;
294                 count = 0;
295                 prev_pte = 0;
296         }
297
298         /* Dump memory chunks  page by page*/
299         VM_PAGE_DUMP_FOREACH(pa) {
300                 dump_va = pmap_kenter_temporary(pa, 0);
301                 error = write_buffer(di, dump_va, PAGE_SIZE);
302                 if (error)
303                         goto fail;
304                 pmap_kenter_temporary_free(pa);
305         }
306
307         error = dump_finish(di, &kdh);
308         if (error != 0)
309                 goto fail;
310
311         printf("\nDump complete\n");
312         return (0);
313
314 fail:
315         if (error < 0)
316                 error = -error;
317
318         if (error == ECANCELED)
319                 printf("\nDump aborted\n");
320         else if (error == E2BIG || error == ENOSPC) {
321                 printf("\nDump failed. Partition too small (about %lluMB were "
322                     "needed this time).\n", (long long)dumpsize >> 20);
323         } else
324                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
325         return (error);
326 }