]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/minidump_machdep.c
Move vm_page_dump bitset array definition to MI code
[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(BITSET_SIZE(vm_page_dump_pages));
209         VM_PAGE_DUMP_FOREACH(pa) {
210                 /* Clear out undumpable pages now if needed */
211                 if (is_dumpable(pa))
212                         dumpsize += PAGE_SIZE;
213                 else
214                         dump_drop_page(pa);
215         }
216         dumpsize += PAGE_SIZE;
217
218         progress = dumpsize;
219
220         /* Initialize mdhdr */
221         bzero(&mdhdr, sizeof(mdhdr));
222         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
223         mdhdr.version = MINIDUMP_VERSION;
224         mdhdr.msgbufsize = msgbufp->msg_size;
225         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
226         mdhdr.ptesize = ptesize;
227         mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
228
229         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION,
230             dumpsize);
231
232         error = dump_start(di, &kdh);
233         if (error != 0)
234                 goto fail;
235
236         printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
237             ptoa((uintmax_t)physmem) / 1048576);
238
239         /* Dump my header */
240         bzero(tmpbuffer, sizeof(tmpbuffer));
241         bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
242         error = write_buffer(di, tmpbuffer, PAGE_SIZE);
243         if (error)
244                 goto fail;
245
246         /* Dump msgbuf up front */
247         error = write_buffer(di, (char *)msgbufp->msg_ptr, 
248             round_page(msgbufp->msg_size));
249         if (error)
250                 goto fail;
251
252         /* Dump bitmap */
253         error = write_buffer(di, (char *)vm_page_dump,
254             round_page(BITSET_SIZE(vm_page_dump_pages)));
255         if (error)
256                 goto fail;
257
258         /* Dump kernel page table pages */
259         for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
260                 pte = pmap_pte(kernel_pmap, va);
261                 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
262                 if (!count) {
263                         prev_pte = (vm_offset_t)pte;
264                         count++;
265                 } else {
266                         if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
267                                 count++;
268                         else {
269                                 error = write_buffer(di, (char*)prev_pte,
270                                     count * PAGE_SIZE);
271                                 if (error)
272                                         goto fail;
273                                 count = 1;
274                                 prev_pte = (vm_offset_t)pte;
275                         }
276                 }
277         }
278
279         if (count) {
280                 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
281                 if (error)
282                         goto fail;
283                 count = 0;
284                 prev_pte = 0;
285         }
286
287         /* Dump memory chunks  page by page*/
288         VM_PAGE_DUMP_FOREACH(pa) {
289                 dump_va = pmap_kenter_temporary(pa, 0);
290                 error = write_buffer(di, dump_va, PAGE_SIZE);
291                 if (error)
292                         goto fail;
293                 pmap_kenter_temporary_free(pa);
294         }
295
296         error = dump_finish(di, &kdh);
297         if (error != 0)
298                 goto fail;
299
300         printf("\nDump complete\n");
301         return (0);
302
303 fail:
304         if (error < 0)
305                 error = -error;
306
307         if (error == ECANCELED)
308                 printf("\nDump aborted\n");
309         else if (error == E2BIG || error == ENOSPC) {
310                 printf("\nDump failed. Partition too small (about %lluMB were "
311                     "needed this time).\n", (long long)dumpsize >> 20);
312         } else
313                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
314         return (error);
315 }