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