minidump_machdep.c revision 331017
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: stable/11/sys/mips/mips/minidump_machdep.c 331017 2018-03-15 19:08:33Z kevans $");
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/vmmeter.h>
43#include <vm/vm.h>
44#include <vm/pmap.h>
45#include <machine/atomic.h>
46#include <machine/elf.h>
47#include <machine/md_var.h>
48#include <machine/vmparam.h>
49#include <machine/minidump.h>
50#include <machine/cache.h>
51
52CTASSERT(sizeof(struct kerneldumpheader) == 512);
53
54/*
55 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
56 * is to protect us from metadata and to protect metadata from us.
57 */
58#define	SIZEOF_METADATA		(64*1024)
59
60uint32_t *vm_page_dump;
61int vm_page_dump_size;
62
63static struct kerneldumpheader kdh;
64static off_t dumplo;
65static off_t origdumplo;
66
67/* Handle chunked writes. */
68static uint64_t counter, progress;
69/* Just auxiliary bufffer */
70static char tmpbuffer[PAGE_SIZE];
71
72extern pd_entry_t *kernel_segmap;
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
88void
89dump_add_page(vm_paddr_t pa)
90{
91	int idx, bit;
92
93	pa >>= PAGE_SHIFT;
94	idx = pa >> 5;		/* 2^5 = 32 */
95	bit = pa & 31;
96	atomic_set_int(&vm_page_dump[idx], 1ul << bit);
97}
98
99void
100dump_drop_page(vm_paddr_t pa)
101{
102	int idx, bit;
103
104	pa >>= PAGE_SHIFT;
105	idx = pa >> 5;		/* 2^5 = 32 */
106	bit = pa & 31;
107	atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
108}
109
110#define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
111
112static int
113write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
114{
115	size_t len;
116	int error, c;
117	u_int maxdumpsz;
118
119	maxdumpsz = di->maxiosize;
120
121	if (maxdumpsz == 0)	/* seatbelt */
122		maxdumpsz = PAGE_SIZE;
123
124	error = 0;
125
126	while (sz) {
127		len = min(maxdumpsz, sz);
128		counter += len;
129		progress -= len;
130
131		if (counter >> 22) {
132			printf(" %jd", PG2MB(progress >> PAGE_SHIFT));
133			counter &= (1<<22) - 1;
134		}
135
136		if (ptr) {
137			error = dump_write(di, ptr, 0, dumplo, len);
138			if (error)
139				return (error);
140			dumplo += len;
141			ptr += len;
142			sz -= len;
143		} else {
144			panic("pa is not supported");
145		}
146
147		/* Check for user abort. */
148		c = cncheckc();
149		if (c == 0x03)
150			return (ECANCELED);
151		if (c != -1)
152			printf(" (CTRL-C to abort) ");
153	}
154
155	return (0);
156}
157
158int
159minidumpsys(struct dumperinfo *di)
160{
161	struct minidumphdr mdhdr;
162	uint64_t dumpsize;
163	uint32_t ptesize;
164	uint32_t bits;
165	vm_paddr_t pa;
166	vm_offset_t prev_pte = 0;
167	uint32_t count = 0;
168	vm_offset_t va;
169	pt_entry_t *pte;
170	int i, bit, error;
171	void *dump_va;
172
173	/* Flush cache */
174	mips_dcache_wbinv_all();
175
176	counter = 0;
177	/* Walk page table pages, set bits in vm_page_dump */
178	ptesize = 0;
179
180	for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
181		ptesize += PAGE_SIZE;
182		pte = pmap_pte(kernel_pmap, va);
183		KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
184		for (i = 0; i < NPTEPG; i++) {
185			if (pte_test(&pte[i], PTE_V)) {
186				pa = TLBLO_PTE_TO_PA(pte[i]);
187				if (is_dumpable(pa))
188					dump_add_page(pa);
189			}
190		}
191	}
192
193	/*
194	 * Now mark pages from 0 to phys_avail[0], that's where kernel
195	 * and pages allocated by pmap_steal reside
196	 */
197	for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
198		if (is_dumpable(pa))
199			dump_add_page(pa);
200	}
201
202	/* Calculate dump size. */
203	dumpsize = ptesize;
204	dumpsize += round_page(msgbufp->msg_size);
205	dumpsize += round_page(vm_page_dump_size);
206
207	for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
208		bits = vm_page_dump[i];
209		while (bits) {
210			bit = ffs(bits) - 1;
211			pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
212			    bit) * PAGE_SIZE;
213			/* Clear out undumpable pages now if needed */
214			if (is_dumpable(pa))
215				dumpsize += PAGE_SIZE;
216			else
217				dump_drop_page(pa);
218			bits &= ~(1ul << bit);
219		}
220	}
221
222	dumpsize += PAGE_SIZE;
223
224	/* Determine dump offset on device. */
225	if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
226		error = ENOSPC;
227		goto fail;
228	}
229
230	origdumplo = dumplo = di->mediaoffset + di->mediasize - dumpsize;
231	dumplo -= sizeof(kdh) * 2;
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 = VM_MIN_KERNEL_ADDRESS;
242
243	mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION, dumpsize,
244	    di->blocksize);
245
246	printf("Physical memory: %ju MB\n",
247	    (uintmax_t)ptoa((uintmax_t)physmem) / 1048576);
248	printf("Dumping %llu MB:", (long long)dumpsize >> 20);
249
250	/* Dump leader */
251	error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
252	if (error)
253		goto fail;
254	dumplo += sizeof(kdh);
255
256	/* Dump my header */
257	bzero(tmpbuffer, sizeof(tmpbuffer));
258	bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
259	error = write_buffer(di, tmpbuffer, PAGE_SIZE);
260	if (error)
261		goto fail;
262
263	/* Dump msgbuf up front */
264	error = write_buffer(di, (char *)msgbufp->msg_ptr,
265	    round_page(msgbufp->msg_size));
266	if (error)
267		goto fail;
268
269	/* Dump bitmap */
270	error = write_buffer(di, (char *)vm_page_dump,
271	    round_page(vm_page_dump_size));
272	if (error)
273		goto fail;
274
275	/* Dump kernel page table pages */
276	for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
277		pte = pmap_pte(kernel_pmap, va);
278		KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
279		if (!count) {
280			prev_pte = (vm_offset_t)pte;
281			count++;
282		}
283		else {
284			if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
285				count++;
286			else {
287				error = write_buffer(di, (char*)prev_pte,
288				    count * PAGE_SIZE);
289				if (error)
290					goto fail;
291				count = 1;
292				prev_pte = (vm_offset_t)pte;
293			}
294		}
295	}
296
297	if (count) {
298		error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
299		if (error)
300			goto fail;
301		count = 0;
302		prev_pte = 0;
303	}
304
305	/* Dump memory chunks  page by page*/
306	for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
307		bits = vm_page_dump[i];
308		while (bits) {
309			bit = ffs(bits) - 1;
310			pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
311			    bit) * PAGE_SIZE;
312			dump_va = pmap_kenter_temporary(pa, 0);
313			error = write_buffer(di, dump_va, PAGE_SIZE);
314			if (error)
315				goto fail;
316			pmap_kenter_temporary_free(pa);
317			bits &= ~(1ul << bit);
318		}
319	}
320
321	/* Dump trailer */
322	error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
323	if (error)
324		goto fail;
325	dumplo += sizeof(kdh);
326
327	/* Signal completion, signoff and exit stage left. */
328	dump_write(di, NULL, 0, 0, 0);
329	printf("\nDump complete\n");
330	return (0);
331
332fail:
333	if (error < 0)
334		error = -error;
335
336	if (error == ECANCELED)
337		printf("\nDump aborted\n");
338	else if (error == ENOSPC)
339		printf("\nDump failed. Partition too small.\n");
340	else
341		printf("\n** DUMP FAILED (ERROR %d) **\n", error);
342	return (error);
343}
344