]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/minidump_machdep.c
Merge ACPICA 20170929.
[FreeBSD/FreeBSD.git] / sys / arm / arm / minidump_machdep.c
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * from: FreeBSD: src/sys/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_watchdog.h"
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 #ifdef SW_WATCHDOG
43 #include <sys/watchdog.h>
44 #endif
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <machine/atomic.h>
48 #include <machine/cpu.h>
49 #include <machine/elf.h>
50 #include <machine/md_var.h>
51 #include <machine/minidump.h>
52 #include <machine/vmparam.h>
53
54 CTASSERT(sizeof(struct kerneldumpheader) == 512);
55
56 uint32_t *vm_page_dump;
57 int vm_page_dump_size;
58
59 static struct kerneldumpheader kdh;
60
61 static off_t dumplo;
62
63 /* Handle chunked writes. */
64 static size_t fragsz;
65 static void *dump_va;
66 static uint64_t counter, progress;
67
68 CTASSERT(sizeof(*vm_page_dump) == 4);
69
70 static int
71 is_dumpable(vm_paddr_t pa)
72 {
73         int i;
74
75         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
76                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
77                         return (1);
78         }
79         return (0);
80 }
81
82 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
83
84 static int
85 blk_flush(struct dumperinfo *di)
86 {
87         int error;
88
89         if (fragsz == 0)
90                 return (0);
91
92         error = dump_write(di, dump_va, 0, dumplo, fragsz);
93         dumplo += fragsz;
94         fragsz = 0;
95         return (error);
96 }
97
98 static int
99 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
100 {
101         size_t len;
102         int error, i, c;
103         u_int maxdumpsz;
104
105         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
106         if (maxdumpsz == 0)     /* seatbelt */
107                 maxdumpsz = PAGE_SIZE;
108         error = 0;
109         if (ptr != NULL && pa != 0) {
110                 printf("cant have both va and pa!\n");
111                 return (EINVAL);
112         }
113         if (pa != 0) {
114                 if ((sz % PAGE_SIZE) != 0) {
115                         printf("size not page aligned\n");
116                         return (EINVAL);
117                 }
118                 if ((pa & PAGE_MASK) != 0) {
119                         printf("address not page aligned\n");
120                         return (EINVAL);
121                 }
122         }
123         if (ptr != NULL) {
124                 /* Flush any pre-existing pa pages before a virtual dump. */
125                 error = blk_flush(di);
126                 if (error)
127                         return (error);
128         }
129         while (sz) {
130                 len = maxdumpsz - fragsz;
131                 if (len > sz)
132                         len = sz;
133                 counter += len;
134                 progress -= len;
135                 if (counter >> 22) {
136                         printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
137                         counter &= (1<<22) - 1;
138                 }
139
140 #ifdef SW_WATCHDOG
141                 wdog_kern_pat(WD_LASTVAL);
142 #endif
143                 if (ptr) {
144                         error = dump_write(di, ptr, 0, dumplo, len);
145                         if (error)
146                                 return (error);
147                         dumplo += len;
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         printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
252         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
253
254         error = dump_start(di, &kdh, &dumplo);
255         if (error != 0)
256                 goto fail;
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, dumplo);
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.\n");
347         else
348                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
349         return (error);
350 }
351
352 void
353 dump_add_page(vm_paddr_t pa)
354 {
355         int idx, bit;
356
357         pa >>= PAGE_SHIFT;
358         idx = pa >> 5;          /* 2^5 = 32 */
359         bit = pa & 31;
360         atomic_set_int(&vm_page_dump[idx], 1ul << bit);
361 }
362
363 void
364 dump_drop_page(vm_paddr_t pa)
365 {
366         int idx, bit;
367
368         pa >>= PAGE_SHIFT;
369         idx = pa >> 5;          /* 2^5 = 32 */
370         bit = pa & 31;
371         atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
372 }