]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/minidump_machdep.c
Merge bmake-20200517
[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 uint32_t *vm_page_dump;
61 int vm_page_dump_size;
62
63 static struct kerneldumpheader kdh;
64
65 /* Handle chunked writes. */
66 static size_t fragsz;
67 static void *dump_va;
68 static uint64_t counter, progress;
69
70 CTASSERT(sizeof(*vm_page_dump) == 4);
71
72 static int
73 is_dumpable(vm_paddr_t pa)
74 {
75         int i;
76
77         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
78                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
79                         return (1);
80         }
81         return (0);
82 }
83
84 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
85
86 static int
87 blk_flush(struct dumperinfo *di)
88 {
89         int error;
90
91         if (fragsz == 0)
92                 return (0);
93
94         error = dump_append(di, dump_va, 0, fragsz);
95         fragsz = 0;
96         return (error);
97 }
98
99 static int
100 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
101 {
102         size_t len;
103         int error, i, c;
104         u_int maxdumpsz;
105
106         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
107         if (maxdumpsz == 0)     /* seatbelt */
108                 maxdumpsz = PAGE_SIZE;
109         error = 0;
110         if (ptr != NULL && pa != 0) {
111                 printf("cant have both va and pa!\n");
112                 return (EINVAL);
113         }
114         if (pa != 0) {
115                 if ((sz % PAGE_SIZE) != 0) {
116                         printf("size not page aligned\n");
117                         return (EINVAL);
118                 }
119                 if ((pa & PAGE_MASK) != 0) {
120                         printf("address not page aligned\n");
121                         return (EINVAL);
122                 }
123         }
124         if (ptr != NULL) {
125                 /* Flush any pre-existing pa pages before a virtual dump. */
126                 error = blk_flush(di);
127                 if (error)
128                         return (error);
129         }
130         while (sz) {
131                 len = maxdumpsz - fragsz;
132                 if (len > sz)
133                         len = sz;
134                 counter += len;
135                 progress -= len;
136                 if (counter >> 22) {
137                         printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
138                         counter &= (1<<22) - 1;
139                 }
140
141 #ifdef SW_WATCHDOG
142                 wdog_kern_pat(WD_LASTVAL);
143 #endif
144                 if (ptr) {
145                         error = dump_append(di, ptr, 0, len);
146                         if (error)
147                                 return (error);
148                         ptr += len;
149                         sz -= len;
150                 } else {
151                         for (i = 0; i < len; i += PAGE_SIZE)
152                                 dump_va = pmap_kenter_temporary(pa + i,
153                                     (i + fragsz) >> PAGE_SHIFT);
154                         fragsz += len;
155                         pa += len;
156                         sz -= len;
157                         if (fragsz == maxdumpsz) {
158                                 error = blk_flush(di);
159                                 if (error)
160                                         return (error);
161                         }
162                 }
163
164                 /* Check for user abort. */
165                 c = cncheckc();
166                 if (c == 0x03)
167                         return (ECANCELED);
168                 if (c != -1)
169                         printf(" (CTRL-C to abort) ");
170         }
171
172         return (0);
173 }
174
175 /* A buffer for general use. Its size must be one page at least. */
176 static char dumpbuf[PAGE_SIZE];
177 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
178
179 int
180 minidumpsys(struct dumperinfo *di)
181 {
182         struct minidumphdr mdhdr;
183         uint64_t dumpsize;
184         uint32_t ptesize;
185         uint32_t bits;
186         uint32_t pa, prev_pa = 0, count = 0;
187         vm_offset_t va;
188         int i, bit, error;
189         char *addr;
190
191         /*
192          * Flush caches.  Note that in the SMP case this operates only on the
193          * current CPU's L1 cache.  Before we reach this point, code in either
194          * the system shutdown or kernel debugger has called stop_cpus() to stop
195          * all cores other than this one.  Part of the ARM handling of
196          * stop_cpus() is to call wbinv_all() on that core's local L1 cache.  So
197          * by time we get to here, all that remains is to flush the L1 for the
198          * current CPU, then the L2.
199          */
200         dcache_wbinv_poc_all();
201
202         counter = 0;
203         /* Walk page table pages, set bits in vm_page_dump */
204         ptesize = 0;
205         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
206                 pa = pmap_dump_kextract(va, NULL);
207                 if (pa != 0 && is_dumpable(pa))
208                         dump_add_page(pa);
209                 ptesize += sizeof(pt2_entry_t);
210         }
211
212         /* Calculate dump size. */
213         dumpsize = ptesize;
214         dumpsize += round_page(msgbufp->msg_size);
215         dumpsize += round_page(vm_page_dump_size);
216         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
217                 bits = vm_page_dump[i];
218                 while (bits) {
219                         bit = ffs(bits) - 1;
220                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
221                             bit) * PAGE_SIZE;
222                         /* Clear out undumpable pages now if needed */
223                         if (is_dumpable(pa))
224                                 dumpsize += PAGE_SIZE;
225                         else
226                                 dump_drop_page(pa);
227                         bits &= ~(1ul << bit);
228                 }
229         }
230         dumpsize += PAGE_SIZE;
231
232         progress = dumpsize;
233
234         /* Initialize mdhdr */
235         bzero(&mdhdr, sizeof(mdhdr));
236         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
237         mdhdr.version = MINIDUMP_VERSION;
238         mdhdr.msgbufsize = msgbufp->msg_size;
239         mdhdr.bitmapsize = vm_page_dump_size;
240         mdhdr.ptesize = ptesize;
241         mdhdr.kernbase = KERNBASE;
242         mdhdr.arch = __ARM_ARCH;
243 #if __ARM_ARCH >= 6
244         mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
245 #else
246         mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
247 #endif
248         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
249             dumpsize);
250
251         error = dump_start(di, &kdh);
252         if (error != 0)
253                 goto fail;
254
255         printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
256         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
257
258         /* Dump my header */
259         bzero(dumpbuf, sizeof(dumpbuf));
260         bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
261         error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
262         if (error)
263                 goto fail;
264
265         /* Dump msgbuf up front */
266         error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
267             round_page(msgbufp->msg_size));
268         if (error)
269                 goto fail;
270
271         /* Dump bitmap */
272         error = blk_write(di, (char *)vm_page_dump, 0,
273             round_page(vm_page_dump_size));
274         if (error)
275                 goto fail;
276
277         /* Dump kernel page table pages */
278         addr = dumpbuf;
279         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
280                 pmap_dump_kextract(va, (pt2_entry_t *)addr);
281                 addr += sizeof(pt2_entry_t);
282                 if (addr == dumpbuf + sizeof(dumpbuf)) {
283                         error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
284                         if (error != 0)
285                                 goto fail;
286                         addr = dumpbuf;
287                 }
288         }
289         if (addr != dumpbuf) {
290                 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
291                 if (error != 0)
292                         goto fail;
293         }
294
295         /* Dump memory chunks */
296         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
297                 bits = vm_page_dump[i];
298                 while (bits) {
299                         bit = ffs(bits) - 1;
300                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
301                             bit) * PAGE_SIZE;
302                         if (!count) {
303                                 prev_pa = pa;
304                                 count++;
305                         } else {
306                                 if (pa == (prev_pa + count * PAGE_SIZE))
307                                         count++;
308                                 else {
309                                         error = blk_write(di, NULL, prev_pa,
310                                             count * PAGE_SIZE);
311                                         if (error)
312                                                 goto fail;
313                                         count = 1;
314                                         prev_pa = pa;
315                                 }
316                         }
317                         bits &= ~(1ul << bit);
318                 }
319         }
320         if (count) {
321                 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
322                 if (error)
323                         goto fail;
324                 count = 0;
325                 prev_pa = 0;
326         }
327
328         error = blk_flush(di);
329         if (error)
330                 goto fail;
331
332         error = dump_finish(di, &kdh);
333         if (error != 0)
334                 goto fail;
335
336         printf("\nDump complete\n");
337         return (0);
338
339 fail:
340         if (error < 0)
341                 error = -error;
342
343         if (error == ECANCELED)
344                 printf("\nDump aborted\n");
345         else if (error == E2BIG || error == ENOSPC) {
346                 printf("\nDump failed. Partition too small (about %lluMB were "
347                     "needed this time).\n", (long long)dumpsize >> 20);
348         } else
349                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
350         return (error);
351 }
352
353 void
354 dump_add_page(vm_paddr_t pa)
355 {
356         int idx, bit;
357
358         pa >>= PAGE_SHIFT;
359         idx = pa >> 5;          /* 2^5 = 32 */
360         bit = pa & 31;
361         atomic_set_int(&vm_page_dump[idx], 1ul << bit);
362 }
363
364 void
365 dump_drop_page(vm_paddr_t pa)
366 {
367         int idx, bit;
368
369         pa >>= PAGE_SHIFT;
370         idx = pa >> 5;          /* 2^5 = 32 */
371         bit = pa & 31;
372         atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
373 }