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
54CTASSERT(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
62uint32_t *vm_page_dump;
63int vm_page_dump_size;
64
65static struct kerneldumpheader kdh;
66
67static off_t dumplo;
68
69/* Handle chunked writes. */
70static size_t fragsz;
71static void *dump_va;
72static uint64_t counter, progress;
73
74CTASSERT(sizeof(*vm_page_dump) == 4);
75
76static int
77is_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
90static int
91blk_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
104static int
105blk_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. */
182static char dumpbuf[PAGE_SIZE];
183CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
184
185int
186minidumpsys(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 + sizeof(kdh) * 2) {
242		error = ENOSPC;
243		goto fail;
244	}
245
246	dumplo = di->mediaoffset + di->mediasize - dumpsize;
247	dumplo -= sizeof(kdh) * 2;
248	progress = dumpsize;
249
250	/* Initialize mdhdr */
251	bzero(&mdhdr, sizeof(mdhdr));
252	strcpy(mdhdr.magic, MINIDUMP_MAGIC);
253	mdhdr.version = MINIDUMP_VERSION;
254	mdhdr.msgbufsize = msgbufp->msg_size;
255	mdhdr.bitmapsize = vm_page_dump_size;
256	mdhdr.ptesize = ptesize;
257	mdhdr.kernbase = KERNBASE;
258	mdhdr.arch = __ARM_ARCH;
259#if __ARM_ARCH >= 6
260	mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
261#else
262	mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
263#endif
264	mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION, dumpsize,
265	    di->blocksize);
266
267	printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
268	printf("Dumping %llu MB:", (long long)dumpsize >> 20);
269
270	/* Dump leader */
271	error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
272	if (error)
273		goto fail;
274	dumplo += sizeof(kdh);
275
276	/* Dump my header */
277	bzero(dumpbuf, sizeof(dumpbuf));
278	bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
279	error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
280	if (error)
281		goto fail;
282
283	/* Dump msgbuf up front */
284	error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
285	    round_page(msgbufp->msg_size));
286	if (error)
287		goto fail;
288
289	/* Dump bitmap */
290	error = blk_write(di, (char *)vm_page_dump, 0,
291	    round_page(vm_page_dump_size));
292	if (error)
293		goto fail;
294
295	/* Dump kernel page table pages */
296	addr = dumpbuf;
297	for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
298		pmap_dump_kextract(va, (pt2_entry_t *)addr);
299		addr += sizeof(pt2_entry_t);
300		if (addr == dumpbuf + sizeof(dumpbuf)) {
301			error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
302			if (error != 0)
303				goto fail;
304			addr = dumpbuf;
305		}
306	}
307	if (addr != dumpbuf) {
308		error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
309		if (error != 0)
310			goto fail;
311	}
312
313	/* Dump memory chunks */
314	for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
315		bits = vm_page_dump[i];
316		while (bits) {
317			bit = ffs(bits) - 1;
318			pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
319			    bit) * PAGE_SIZE;
320			if (!count) {
321				prev_pa = pa;
322				count++;
323			} else {
324				if (pa == (prev_pa + count * PAGE_SIZE))
325					count++;
326				else {
327					error = blk_write(di, NULL, prev_pa,
328					    count * PAGE_SIZE);
329					if (error)
330						goto fail;
331					count = 1;
332					prev_pa = pa;
333				}
334			}
335			bits &= ~(1ul << bit);
336		}
337	}
338	if (count) {
339		error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
340		if (error)
341			goto fail;
342		count = 0;
343		prev_pa = 0;
344	}
345
346	error = blk_flush(di);
347	if (error)
348		goto fail;
349
350	/* Dump trailer */
351	error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
352	if (error)
353		goto fail;
354	dumplo += sizeof(kdh);
355
356	/* Signal completion, signoff and exit stage left. */
357	dump_write(di, NULL, 0, 0, 0);
358	printf("\nDump complete\n");
359	return (0);
360
361fail:
362	if (error < 0)
363		error = -error;
364
365	if (error == ECANCELED)
366		printf("\nDump aborted\n");
367	else if (error == ENOSPC)
368		printf("\nDump failed. Partition too small.\n");
369	else
370		printf("\n** DUMP FAILED (ERROR %d) **\n", error);
371	return (error);
372}
373
374void
375dump_add_page(vm_paddr_t pa)
376{
377	int idx, bit;
378
379	pa >>= PAGE_SHIFT;
380	idx = pa >> 5;		/* 2^5 = 32 */
381	bit = pa & 31;
382	atomic_set_int(&vm_page_dump[idx], 1ul << bit);
383}
384
385void
386dump_drop_page(vm_paddr_t pa)
387{
388	int idx, bit;
389
390	pa >>= PAGE_SHIFT;
391	idx = pa >> 5;		/* 2^5 = 32 */
392	bit = pa & 31;
393	atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
394}
395