]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/minidump_machdep.c
dts: Update our device tree sources file fomr Linux 4.13
[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 /* Handle chunked writes. */
62 static size_t fragsz;
63 static void *dump_va;
64 static uint64_t counter, progress;
65
66 CTASSERT(sizeof(*vm_page_dump) == 4);
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];
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;
180         uint32_t ptesize;
181         uint32_t bits;
182         uint32_t pa, prev_pa = 0, count = 0;
183         vm_offset_t va;
184         int i, bit, error;
185         char *addr;
186
187         /*
188          * Flush caches.  Note that in the SMP case this operates only on the
189          * current CPU's L1 cache.  Before we reach this point, code in either
190          * the system shutdown or kernel debugger has called stop_cpus() to stop
191          * all cores other than this one.  Part of the ARM handling of
192          * stop_cpus() is to call wbinv_all() on that core's local L1 cache.  So
193          * by time we get to here, all that remains is to flush the L1 for the
194          * current CPU, then the L2.
195          */
196         dcache_wbinv_poc_all();
197
198         counter = 0;
199         /* Walk page table pages, set bits in vm_page_dump */
200         ptesize = 0;
201         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
202                 pa = pmap_dump_kextract(va, NULL);
203                 if (pa != 0 && is_dumpable(pa))
204                         dump_add_page(pa);
205                 ptesize += sizeof(pt2_entry_t);
206         }
207
208         /* Calculate dump size. */
209         dumpsize = ptesize;
210         dumpsize += round_page(msgbufp->msg_size);
211         dumpsize += round_page(vm_page_dump_size);
212         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
213                 bits = vm_page_dump[i];
214                 while (bits) {
215                         bit = ffs(bits) - 1;
216                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
217                             bit) * PAGE_SIZE;
218                         /* Clear out undumpable pages now if needed */
219                         if (is_dumpable(pa))
220                                 dumpsize += PAGE_SIZE;
221                         else
222                                 dump_drop_page(pa);
223                         bits &= ~(1ul << bit);
224                 }
225         }
226         dumpsize += PAGE_SIZE;
227
228         progress = dumpsize;
229
230         /* Initialize mdhdr */
231         bzero(&mdhdr, sizeof(mdhdr));
232         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
233         mdhdr.version = MINIDUMP_VERSION;
234         mdhdr.msgbufsize = msgbufp->msg_size;
235         mdhdr.bitmapsize = vm_page_dump_size;
236         mdhdr.ptesize = ptesize;
237         mdhdr.kernbase = KERNBASE;
238         mdhdr.arch = __ARM_ARCH;
239 #if __ARM_ARCH >= 6
240         mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
241 #else
242         mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
243 #endif
244         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
245             dumpsize);
246
247         printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
248         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
249
250         error = dump_start(di, &kdh);
251         if (error != 0)
252                 goto fail;
253
254         /* Dump my header */
255         bzero(dumpbuf, sizeof(dumpbuf));
256         bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
257         error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
258         if (error)
259                 goto fail;
260
261         /* Dump msgbuf up front */
262         error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
263             round_page(msgbufp->msg_size));
264         if (error)
265                 goto fail;
266
267         /* Dump bitmap */
268         error = blk_write(di, (char *)vm_page_dump, 0,
269             round_page(vm_page_dump_size));
270         if (error)
271                 goto fail;
272
273         /* Dump kernel page table pages */
274         addr = dumpbuf;
275         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
276                 pmap_dump_kextract(va, (pt2_entry_t *)addr);
277                 addr += sizeof(pt2_entry_t);
278                 if (addr == dumpbuf + sizeof(dumpbuf)) {
279                         error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
280                         if (error != 0)
281                                 goto fail;
282                         addr = dumpbuf;
283                 }
284         }
285         if (addr != dumpbuf) {
286                 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
287                 if (error != 0)
288                         goto fail;
289         }
290
291         /* Dump memory chunks */
292         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
293                 bits = vm_page_dump[i];
294                 while (bits) {
295                         bit = ffs(bits) - 1;
296                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
297                             bit) * PAGE_SIZE;
298                         if (!count) {
299                                 prev_pa = pa;
300                                 count++;
301                         } else {
302                                 if (pa == (prev_pa + count * PAGE_SIZE))
303                                         count++;
304                                 else {
305                                         error = blk_write(di, NULL, prev_pa,
306                                             count * PAGE_SIZE);
307                                         if (error)
308                                                 goto fail;
309                                         count = 1;
310                                         prev_pa = pa;
311                                 }
312                         }
313                         bits &= ~(1ul << bit);
314                 }
315         }
316         if (count) {
317                 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
318                 if (error)
319                         goto fail;
320                 count = 0;
321                 prev_pa = 0;
322         }
323
324         error = blk_flush(di);
325         if (error)
326                 goto fail;
327
328         error = dump_finish(di, &kdh);
329         if (error != 0)
330                 goto fail;
331
332         printf("\nDump complete\n");
333         return (0);
334
335 fail:
336         if (error < 0)
337                 error = -error;
338
339         if (error == ECANCELED)
340                 printf("\nDump aborted\n");
341         else if (error == E2BIG || error == ENOSPC)
342                 printf("\nDump failed. Partition too small.\n");
343         else
344                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
345         return (error);
346 }
347
348 void
349 dump_add_page(vm_paddr_t pa)
350 {
351         int idx, bit;
352
353         pa >>= PAGE_SHIFT;
354         idx = pa >> 5;          /* 2^5 = 32 */
355         bit = pa & 31;
356         atomic_set_int(&vm_page_dump[idx], 1ul << bit);
357 }
358
359 void
360 dump_drop_page(vm_paddr_t pa)
361 {
362         int idx, bit;
363
364         pa >>= PAGE_SHIFT;
365         idx = pa >> 5;          /* 2^5 = 32 */
366         bit = pa & 31;
367         atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
368 }