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: releng/12.0/sys/mips/mips/minidump_machdep.c 333145 2018-05-01 17:32:43Z markj $");
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 uint32_t *vm_page_dump;
58 int vm_page_dump_size;
59
60 static struct kerneldumpheader kdh;
61
62 /* Handle chunked writes. */
63 static uint64_t counter, progress, dumpsize;
64 /* Just auxiliary bufffer */
65 static char tmpbuffer[PAGE_SIZE];
66
67 extern pd_entry_t *kernel_segmap;
68
69 CTASSERT(sizeof(*vm_page_dump) == 4);
70
71 static int
72 is_dumpable(vm_paddr_t pa)
73 {
74 vm_page_t m;
75 int i;
76
77 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
78 return ((m->flags & PG_NODUMP) == 0);
79 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
80 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
81 return (1);
82 }
83 return (0);
84 }
85
86 void
87 dump_add_page(vm_paddr_t pa)
88 {
89 int idx, bit;
90
91 pa >>= PAGE_SHIFT;
92 idx = pa >> 5; /* 2^5 = 32 */
93 bit = pa & 31;
94 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
95 }
96
97 void
98 dump_drop_page(vm_paddr_t pa)
99 {
100 int idx, bit;
101
102 pa >>= PAGE_SHIFT;
103 idx = pa >> 5; /* 2^5 = 32 */
104 bit = pa & 31;
105 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
106 }
107
108 static struct {
109 int min_per;
110 int max_per;
111 int visited;
112 } progress_track[10] = {
113 { 0, 10, 0},
114 { 10, 20, 0},
115 { 20, 30, 0},
116 { 30, 40, 0},
117 { 40, 50, 0},
118 { 50, 60, 0},
119 { 60, 70, 0},
120 { 70, 80, 0},
121 { 80, 90, 0},
122 { 90, 100, 0}
123 };
124
125 static void
126 report_progress(uint64_t progress, uint64_t dumpsize)
127 {
128 int sofar, i;
129
130 sofar = 100 - ((progress * 100) / dumpsize);
131 for (i = 0; i < nitems(progress_track); i++) {
132 if (sofar < progress_track[i].min_per ||
133 sofar > progress_track[i].max_per)
134 continue;
135 if (progress_track[i].visited)
136 return;
137 progress_track[i].visited = 1;
138 printf("..%d%%", sofar);
139 return;
140 }
141 }
142
143 static int
144 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
145 {
146 size_t len;
147 int error, c;
148 u_int maxdumpsz;
149
150 maxdumpsz = di->maxiosize;
151
152 if (maxdumpsz == 0) /* seatbelt */
153 maxdumpsz = PAGE_SIZE;
154
155 error = 0;
156
157 while (sz) {
158 len = min(maxdumpsz, sz);
159 counter += len;
160 progress -= len;
161
162 if (counter >> 22) {
163 report_progress(progress, dumpsize);
164 counter &= (1<<22) - 1;
165 }
166
167 wdog_kern_pat(WD_LASTVAL);
168
169 if (ptr) {
170 error = dump_append(di, ptr, 0, len);
171 if (error)
172 return (error);
173 ptr += len;
174 sz -= len;
175 } else {
176 panic("pa is not supported");
177 }
178
179 /* Check for user abort. */
180 c = cncheckc();
181 if (c == 0x03)
182 return (ECANCELED);
183 if (c != -1)
184 printf(" (CTRL-C to abort) ");
185 }
186
187 return (0);
188 }
189
190 int
191 minidumpsys(struct dumperinfo *di)
192 {
193 struct minidumphdr mdhdr;
194 uint32_t ptesize;
195 uint32_t bits;
196 vm_paddr_t pa;
197 vm_offset_t prev_pte = 0;
198 uint32_t count = 0;
199 vm_offset_t va;
200 pt_entry_t *pte;
201 int i, bit, error;
202 void *dump_va;
203
204 /* Flush cache */
205 mips_dcache_wbinv_all();
206
207 counter = 0;
208 /* Walk page table pages, set bits in vm_page_dump */
209 ptesize = 0;
210
211 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
212 ptesize += PAGE_SIZE;
213 pte = pmap_pte(kernel_pmap, va);
214 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
215 for (i = 0; i < NPTEPG; i++) {
216 if (pte_test(&pte[i], PTE_V)) {
217 pa = TLBLO_PTE_TO_PA(pte[i]);
218 if (is_dumpable(pa))
219 dump_add_page(pa);
220 }
221 }
222 }
223
224 /*
225 * Now mark pages from 0 to phys_avail[0], that's where kernel
226 * and pages allocated by pmap_steal reside
227 */
228 for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
229 if (is_dumpable(pa))
230 dump_add_page(pa);
231 }
232
233 /* Calculate dump size. */
234 dumpsize = ptesize;
235 dumpsize += round_page(msgbufp->msg_size);
236 dumpsize += round_page(vm_page_dump_size);
237 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
238 bits = vm_page_dump[i];
239 while (bits) {
240 bit = ffs(bits) - 1;
241 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
242 bit) * PAGE_SIZE;
243 /* Clear out undumpable pages now if needed */
244 if (is_dumpable(pa))
245 dumpsize += PAGE_SIZE;
246 else
247 dump_drop_page(pa);
248 bits &= ~(1ul << bit);
249 }
250 }
251 dumpsize += PAGE_SIZE;
252
253 progress = dumpsize;
254
255 /* Initialize mdhdr */
256 bzero(&mdhdr, sizeof(mdhdr));
257 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
258 mdhdr.version = MINIDUMP_VERSION;
259 mdhdr.msgbufsize = msgbufp->msg_size;
260 mdhdr.bitmapsize = vm_page_dump_size;
261 mdhdr.ptesize = ptesize;
262 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
263
264 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION,
265 dumpsize);
266
267 error = dump_start(di, &kdh);
268 if (error != 0)
269 goto fail;
270
271 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
272 ptoa((uintmax_t)physmem) / 1048576);
273
274 /* Dump my header */
275 bzero(tmpbuffer, sizeof(tmpbuffer));
276 bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
277 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
278 if (error)
279 goto fail;
280
281 /* Dump msgbuf up front */
282 error = write_buffer(di, (char *)msgbufp->msg_ptr,
283 round_page(msgbufp->msg_size));
284 if (error)
285 goto fail;
286
287 /* Dump bitmap */
288 error = write_buffer(di, (char *)vm_page_dump,
289 round_page(vm_page_dump_size));
290 if (error)
291 goto fail;
292
293 /* Dump kernel page table pages */
294 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
295 pte = pmap_pte(kernel_pmap, va);
296 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
297 if (!count) {
298 prev_pte = (vm_offset_t)pte;
299 count++;
300 } else {
301 if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
302 count++;
303 else {
304 error = write_buffer(di, (char*)prev_pte,
305 count * PAGE_SIZE);
306 if (error)
307 goto fail;
308 count = 1;
309 prev_pte = (vm_offset_t)pte;
310 }
311 }
312 }
313
314 if (count) {
315 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
316 if (error)
317 goto fail;
318 count = 0;
319 prev_pte = 0;
320 }
321
322 /* Dump memory chunks page by page*/
323 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
324 bits = vm_page_dump[i];
325 while (bits) {
326 bit = ffs(bits) - 1;
327 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
328 bit) * PAGE_SIZE;
329 dump_va = pmap_kenter_temporary(pa, 0);
330 error = write_buffer(di, dump_va, PAGE_SIZE);
331 if (error)
332 goto fail;
333 pmap_kenter_temporary_free(pa);
334 bits &= ~(1ul << bit);
335 }
336 }
337
338 error = dump_finish(di, &kdh);
339 if (error != 0)
340 goto fail;
341
342 printf("\nDump complete\n");
343 return (0);
344
345 fail:
346 if (error < 0)
347 error = -error;
348
349 if (error == ECANCELED)
350 printf("\nDump aborted\n");
351 else if (error == E2BIG || error == ENOSPC)
352 printf("\nDump failed. Partition too small.\n");
353 else
354 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
355 return (error);
356 }
Cache object: 6edf4556a1cdf6bc806007f021b7f5fb
|