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