xref: /freebsd/sys/kern/kern_dump.c (revision 1adebe3cd6b1eb0973969e2c4c5d41f174caa304)
1bdb9ab0dSMark Johnston /*-
2bdb9ab0dSMark Johnston  * Copyright (c) 2002 Marcel Moolenaar
3bdb9ab0dSMark Johnston  * All rights reserved.
4bdb9ab0dSMark Johnston  *
5bdb9ab0dSMark Johnston  * Redistribution and use in source and binary forms, with or without
6bdb9ab0dSMark Johnston  * modification, are permitted provided that the following conditions
7bdb9ab0dSMark Johnston  * are met:
8bdb9ab0dSMark Johnston  *
9bdb9ab0dSMark Johnston  * 1. Redistributions of source code must retain the above copyright
10bdb9ab0dSMark Johnston  *    notice, this list of conditions and the following disclaimer.
11bdb9ab0dSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
12bdb9ab0dSMark Johnston  *    notice, this list of conditions and the following disclaimer in the
13bdb9ab0dSMark Johnston  *    documentation and/or other materials provided with the distribution.
14bdb9ab0dSMark Johnston  *
15bdb9ab0dSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16bdb9ab0dSMark Johnston  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17bdb9ab0dSMark Johnston  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18bdb9ab0dSMark Johnston  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19bdb9ab0dSMark Johnston  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20bdb9ab0dSMark Johnston  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21bdb9ab0dSMark Johnston  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22bdb9ab0dSMark Johnston  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23bdb9ab0dSMark Johnston  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24bdb9ab0dSMark Johnston  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25bdb9ab0dSMark Johnston  */
26bdb9ab0dSMark Johnston 
27bdb9ab0dSMark Johnston #include <sys/cdefs.h>
28bdb9ab0dSMark Johnston __FBSDID("$FreeBSD$");
29bdb9ab0dSMark Johnston 
30bdb9ab0dSMark Johnston #include <sys/param.h>
31bdb9ab0dSMark Johnston #include <sys/systm.h>
32bdb9ab0dSMark Johnston #include <sys/conf.h>
33bdb9ab0dSMark Johnston #include <sys/cons.h>
34bdb9ab0dSMark Johnston #include <sys/kernel.h>
35bdb9ab0dSMark Johnston #include <sys/kerneldump.h>
36*1adebe3cSMitchell Horne #include <sys/msgbuf.h>
37*1adebe3cSMitchell Horne #include <sys/proc.h>
38bdb9ab0dSMark Johnston #include <sys/watchdog.h>
39*1adebe3cSMitchell Horne 
40bdb9ab0dSMark Johnston #include <vm/vm.h>
41bdb9ab0dSMark Johnston #include <vm/vm_param.h>
4221943937SJeff Roberson #include <vm/vm_page.h>
4321943937SJeff Roberson #include <vm/vm_phys.h>
446f3b523cSKonstantin Belousov #include <vm/vm_dumpset.h>
45bdb9ab0dSMark Johnston #include <vm/pmap.h>
46*1adebe3cSMitchell Horne 
47bdb9ab0dSMark Johnston #include <machine/dump.h>
48bdb9ab0dSMark Johnston #include <machine/elf.h>
49bdb9ab0dSMark Johnston #include <machine/md_var.h>
50bdb9ab0dSMark Johnston #include <machine/pcb.h>
51bdb9ab0dSMark Johnston 
52bdb9ab0dSMark Johnston CTASSERT(sizeof(struct kerneldumpheader) == 512);
53bdb9ab0dSMark Johnston 
545dc5dab6SConrad Meyer #define	MD_ALIGN(x)	roundup2((off_t)(x), PAGE_SIZE)
55bdb9ab0dSMark Johnston 
56bdb9ab0dSMark Johnston /* Handle buffered writes. */
57bdb9ab0dSMark Johnston static size_t fragsz;
58bdb9ab0dSMark Johnston 
59bdb9ab0dSMark Johnston struct dump_pa dump_map[DUMPSYS_MD_PA_NPAIRS];
60bdb9ab0dSMark Johnston 
618f11c997SYoshihiro Takahashi #if !defined(__powerpc__)
62bdb9ab0dSMark Johnston void
63bdb9ab0dSMark Johnston dumpsys_gen_pa_init(void)
64bdb9ab0dSMark Johnston {
65bdb9ab0dSMark Johnston 	int n, idx;
66bdb9ab0dSMark Johnston 
67bdb9ab0dSMark Johnston 	bzero(dump_map, sizeof(dump_map));
6802abd400SPedro F. Giffuni 	for (n = 0; n < nitems(dump_map); n++) {
69bdb9ab0dSMark Johnston 		idx = n * 2;
70bdb9ab0dSMark Johnston 		if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0)
71bdb9ab0dSMark Johnston 			break;
72bdb9ab0dSMark Johnston 		dump_map[n].pa_start = dump_avail[idx];
73bdb9ab0dSMark Johnston 		dump_map[n].pa_size = dump_avail[idx + 1] - dump_avail[idx];
74bdb9ab0dSMark Johnston 	}
75bdb9ab0dSMark Johnston }
767888a51fSMarius Strobl #endif
77bdb9ab0dSMark Johnston 
78bdb9ab0dSMark Johnston struct dump_pa *
79bdb9ab0dSMark Johnston dumpsys_gen_pa_next(struct dump_pa *mdp)
80bdb9ab0dSMark Johnston {
81bdb9ab0dSMark Johnston 
82bdb9ab0dSMark Johnston 	if (mdp == NULL)
83bdb9ab0dSMark Johnston 		return (&dump_map[0]);
84bdb9ab0dSMark Johnston 
85bdb9ab0dSMark Johnston 	mdp++;
86bdb9ab0dSMark Johnston 	if (mdp->pa_size == 0)
87bdb9ab0dSMark Johnston 		mdp = NULL;
88bdb9ab0dSMark Johnston 	return (mdp);
89bdb9ab0dSMark Johnston }
90bdb9ab0dSMark Johnston 
91bdb9ab0dSMark Johnston void
92bdb9ab0dSMark Johnston dumpsys_gen_wbinv_all(void)
93bdb9ab0dSMark Johnston {
947888a51fSMarius Strobl 
95bdb9ab0dSMark Johnston }
96bdb9ab0dSMark Johnston 
97bdb9ab0dSMark Johnston void
98bdb9ab0dSMark Johnston dumpsys_gen_unmap_chunk(vm_paddr_t pa __unused, size_t chunk __unused,
99bdb9ab0dSMark Johnston     void *va __unused)
100bdb9ab0dSMark Johnston {
1017888a51fSMarius Strobl 
102bdb9ab0dSMark Johnston }
103bdb9ab0dSMark Johnston 
104bdb9ab0dSMark Johnston int
105bdb9ab0dSMark Johnston dumpsys_gen_write_aux_headers(struct dumperinfo *di)
106bdb9ab0dSMark Johnston {
107bdb9ab0dSMark Johnston 
108bdb9ab0dSMark Johnston 	return (0);
109bdb9ab0dSMark Johnston }
110bdb9ab0dSMark Johnston 
111bdb9ab0dSMark Johnston int
112480f31c2SKonrad Witaszczyk dumpsys_buf_seek(struct dumperinfo *di, size_t sz)
113480f31c2SKonrad Witaszczyk {
114480f31c2SKonrad Witaszczyk 	static uint8_t buf[DEV_BSIZE];
115480f31c2SKonrad Witaszczyk 	size_t nbytes;
116480f31c2SKonrad Witaszczyk 	int error;
117480f31c2SKonrad Witaszczyk 
118480f31c2SKonrad Witaszczyk 	bzero(buf, sizeof(buf));
119480f31c2SKonrad Witaszczyk 
120480f31c2SKonrad Witaszczyk 	while (sz > 0) {
121480f31c2SKonrad Witaszczyk 		nbytes = MIN(sz, sizeof(buf));
122480f31c2SKonrad Witaszczyk 
12346fcd1afSMark Johnston 		error = dump_append(di, buf, 0, nbytes);
124480f31c2SKonrad Witaszczyk 		if (error)
125480f31c2SKonrad Witaszczyk 			return (error);
126480f31c2SKonrad Witaszczyk 		sz -= nbytes;
127480f31c2SKonrad Witaszczyk 	}
128480f31c2SKonrad Witaszczyk 
129480f31c2SKonrad Witaszczyk 	return (0);
130480f31c2SKonrad Witaszczyk }
131480f31c2SKonrad Witaszczyk 
132480f31c2SKonrad Witaszczyk int
133bdb9ab0dSMark Johnston dumpsys_buf_write(struct dumperinfo *di, char *ptr, size_t sz)
134bdb9ab0dSMark Johnston {
135bdb9ab0dSMark Johnston 	size_t len;
136bdb9ab0dSMark Johnston 	int error;
137bdb9ab0dSMark Johnston 
138bdb9ab0dSMark Johnston 	while (sz) {
1395dc5dab6SConrad Meyer 		len = di->blocksize - fragsz;
140bdb9ab0dSMark Johnston 		if (len > sz)
141bdb9ab0dSMark Johnston 			len = sz;
1425dc5dab6SConrad Meyer 		memcpy((char *)di->blockbuf + fragsz, ptr, len);
143bdb9ab0dSMark Johnston 		fragsz += len;
144bdb9ab0dSMark Johnston 		ptr += len;
145bdb9ab0dSMark Johnston 		sz -= len;
1465dc5dab6SConrad Meyer 		if (fragsz == di->blocksize) {
14746fcd1afSMark Johnston 			error = dump_append(di, di->blockbuf, 0, di->blocksize);
148bdb9ab0dSMark Johnston 			if (error)
149bdb9ab0dSMark Johnston 				return (error);
150bdb9ab0dSMark Johnston 			fragsz = 0;
151bdb9ab0dSMark Johnston 		}
152bdb9ab0dSMark Johnston 	}
153bdb9ab0dSMark Johnston 	return (0);
154bdb9ab0dSMark Johnston }
155bdb9ab0dSMark Johnston 
156bdb9ab0dSMark Johnston int
157bdb9ab0dSMark Johnston dumpsys_buf_flush(struct dumperinfo *di)
158bdb9ab0dSMark Johnston {
159bdb9ab0dSMark Johnston 	int error;
160bdb9ab0dSMark Johnston 
161bdb9ab0dSMark Johnston 	if (fragsz == 0)
162bdb9ab0dSMark Johnston 		return (0);
163bdb9ab0dSMark Johnston 
16446fcd1afSMark Johnston 	error = dump_append(di, di->blockbuf, 0, di->blocksize);
165bdb9ab0dSMark Johnston 	fragsz = 0;
166bdb9ab0dSMark Johnston 	return (error);
167bdb9ab0dSMark Johnston }
168bdb9ab0dSMark Johnston 
169bdb9ab0dSMark Johnston CTASSERT(PAGE_SHIFT < 20);
170bdb9ab0dSMark Johnston #define PG2MB(pgs) ((pgs + (1 << (20 - PAGE_SHIFT)) - 1) >> (20 - PAGE_SHIFT))
171bdb9ab0dSMark Johnston 
172bdb9ab0dSMark Johnston int
173bdb9ab0dSMark Johnston dumpsys_cb_dumpdata(struct dump_pa *mdp, int seqnr, void *arg)
174bdb9ab0dSMark Johnston {
175bdb9ab0dSMark Johnston 	struct dumperinfo *di = (struct dumperinfo*)arg;
176bdb9ab0dSMark Johnston 	vm_paddr_t pa;
177bdb9ab0dSMark Johnston 	void *va;
178bdb9ab0dSMark Johnston 	uint64_t pgs;
179bdb9ab0dSMark Johnston 	size_t counter, sz, chunk;
180bdb9ab0dSMark Johnston 	int c, error;
181bdb9ab0dSMark Johnston 	u_int maxdumppgs;
182bdb9ab0dSMark Johnston 
183bdb9ab0dSMark Johnston 	error = 0;	/* catch case in which chunk size is 0 */
184bdb9ab0dSMark Johnston 	counter = 0;	/* Update twiddle every 16MB */
185b85f65afSPedro F. Giffuni 	va = NULL;
186bdb9ab0dSMark Johnston 	pgs = mdp->pa_size / PAGE_SIZE;
187bdb9ab0dSMark Johnston 	pa = mdp->pa_start;
188bdb9ab0dSMark Johnston 	maxdumppgs = min(di->maxiosize / PAGE_SIZE, MAXDUMPPGS);
189bdb9ab0dSMark Johnston 	if (maxdumppgs == 0)	/* seatbelt */
190bdb9ab0dSMark Johnston 		maxdumppgs = 1;
191bdb9ab0dSMark Johnston 
192bdb9ab0dSMark Johnston 	printf("  chunk %d: %juMB (%ju pages)", seqnr, (uintmax_t)PG2MB(pgs),
193bdb9ab0dSMark Johnston 	    (uintmax_t)pgs);
194bdb9ab0dSMark Johnston 
195bdb9ab0dSMark Johnston 	dumpsys_wbinv_all();
196bdb9ab0dSMark Johnston 	while (pgs) {
197bdb9ab0dSMark Johnston 		chunk = pgs;
198bdb9ab0dSMark Johnston 		if (chunk > maxdumppgs)
199bdb9ab0dSMark Johnston 			chunk = maxdumppgs;
200bdb9ab0dSMark Johnston 		sz = chunk << PAGE_SHIFT;
201bdb9ab0dSMark Johnston 		counter += sz;
202bdb9ab0dSMark Johnston 		if (counter >> 24) {
203bdb9ab0dSMark Johnston 			printf(" %ju", (uintmax_t)PG2MB(pgs));
204bdb9ab0dSMark Johnston 			counter &= (1 << 24) - 1;
205bdb9ab0dSMark Johnston 		}
206bdb9ab0dSMark Johnston 
207bdb9ab0dSMark Johnston 		dumpsys_map_chunk(pa, chunk, &va);
208bdb9ab0dSMark Johnston 		wdog_kern_pat(WD_LASTVAL);
209bdb9ab0dSMark Johnston 
21046fcd1afSMark Johnston 		error = dump_append(di, va, 0, sz);
211bdb9ab0dSMark Johnston 		dumpsys_unmap_chunk(pa, chunk, va);
212bdb9ab0dSMark Johnston 		if (error)
213bdb9ab0dSMark Johnston 			break;
214bdb9ab0dSMark Johnston 		pgs -= chunk;
215bdb9ab0dSMark Johnston 		pa += sz;
216bdb9ab0dSMark Johnston 
217bdb9ab0dSMark Johnston 		/* Check for user abort. */
218bdb9ab0dSMark Johnston 		c = cncheckc();
219bdb9ab0dSMark Johnston 		if (c == 0x03)
220bdb9ab0dSMark Johnston 			return (ECANCELED);
221bdb9ab0dSMark Johnston 		if (c != -1)
222bdb9ab0dSMark Johnston 			printf(" (CTRL-C to abort) ");
223bdb9ab0dSMark Johnston 	}
224bdb9ab0dSMark Johnston 	printf(" ... %s\n", (error) ? "fail" : "ok");
225bdb9ab0dSMark Johnston 	return (error);
226bdb9ab0dSMark Johnston }
227bdb9ab0dSMark Johnston 
228bdb9ab0dSMark Johnston int
229bdb9ab0dSMark Johnston dumpsys_foreach_chunk(dumpsys_callback_t cb, void *arg)
230bdb9ab0dSMark Johnston {
231bdb9ab0dSMark Johnston 	struct dump_pa *mdp;
232bdb9ab0dSMark Johnston 	int error, seqnr;
233bdb9ab0dSMark Johnston 
234bdb9ab0dSMark Johnston 	seqnr = 0;
235bdb9ab0dSMark Johnston 	mdp = dumpsys_pa_next(NULL);
236bdb9ab0dSMark Johnston 	while (mdp != NULL) {
237bdb9ab0dSMark Johnston 		error = (*cb)(mdp, seqnr++, arg);
238bdb9ab0dSMark Johnston 		if (error)
239bdb9ab0dSMark Johnston 			return (-error);
240bdb9ab0dSMark Johnston 		mdp = dumpsys_pa_next(mdp);
241bdb9ab0dSMark Johnston 	}
242bdb9ab0dSMark Johnston 	return (seqnr);
243bdb9ab0dSMark Johnston }
244bdb9ab0dSMark Johnston 
245bdb9ab0dSMark Johnston static off_t fileofs;
246bdb9ab0dSMark Johnston 
247bdb9ab0dSMark Johnston static int
248bdb9ab0dSMark Johnston cb_dumphdr(struct dump_pa *mdp, int seqnr, void *arg)
249bdb9ab0dSMark Johnston {
250bdb9ab0dSMark Johnston 	struct dumperinfo *di = (struct dumperinfo*)arg;
251bdb9ab0dSMark Johnston 	Elf_Phdr phdr;
252bdb9ab0dSMark Johnston 	uint64_t size;
253bdb9ab0dSMark Johnston 	int error;
254bdb9ab0dSMark Johnston 
255bdb9ab0dSMark Johnston 	size = mdp->pa_size;
256bdb9ab0dSMark Johnston 	bzero(&phdr, sizeof(phdr));
257bdb9ab0dSMark Johnston 	phdr.p_type = PT_LOAD;
258bdb9ab0dSMark Johnston 	phdr.p_flags = PF_R;			/* XXX */
259bdb9ab0dSMark Johnston 	phdr.p_offset = fileofs;
260bdb9ab0dSMark Johnston #ifdef __powerpc__
261bdb9ab0dSMark Johnston 	phdr.p_vaddr = (do_minidump? mdp->pa_start : ~0L);
262bdb9ab0dSMark Johnston 	phdr.p_paddr = (do_minidump? ~0L : mdp->pa_start);
263bdb9ab0dSMark Johnston #else
264bdb9ab0dSMark Johnston 	phdr.p_vaddr = mdp->pa_start;
265bdb9ab0dSMark Johnston 	phdr.p_paddr = mdp->pa_start;
266bdb9ab0dSMark Johnston #endif
267bdb9ab0dSMark Johnston 	phdr.p_filesz = size;
268bdb9ab0dSMark Johnston 	phdr.p_memsz = size;
269bdb9ab0dSMark Johnston 	phdr.p_align = PAGE_SIZE;
270bdb9ab0dSMark Johnston 
271bdb9ab0dSMark Johnston 	error = dumpsys_buf_write(di, (char*)&phdr, sizeof(phdr));
272bdb9ab0dSMark Johnston 	fileofs += phdr.p_filesz;
273bdb9ab0dSMark Johnston 	return (error);
274bdb9ab0dSMark Johnston }
275bdb9ab0dSMark Johnston 
276bdb9ab0dSMark Johnston static int
277bdb9ab0dSMark Johnston cb_size(struct dump_pa *mdp, int seqnr, void *arg)
278bdb9ab0dSMark Johnston {
279bdb9ab0dSMark Johnston 	uint64_t *sz;
280bdb9ab0dSMark Johnston 
281bdb9ab0dSMark Johnston 	sz = (uint64_t *)arg;
282bdb9ab0dSMark Johnston 	*sz += (uint64_t)mdp->pa_size;
283bdb9ab0dSMark Johnston 	return (0);
284bdb9ab0dSMark Johnston }
285bdb9ab0dSMark Johnston 
286bdb9ab0dSMark Johnston int
287bdb9ab0dSMark Johnston dumpsys_generic(struct dumperinfo *di)
288bdb9ab0dSMark Johnston {
289bdb9ab0dSMark Johnston 	static struct kerneldumpheader kdh;
290bdb9ab0dSMark Johnston 	Elf_Ehdr ehdr;
291bdb9ab0dSMark Johnston 	uint64_t dumpsize;
292bdb9ab0dSMark Johnston 	off_t hdrgap;
293480f31c2SKonrad Witaszczyk 	size_t hdrsz;
294bdb9ab0dSMark Johnston 	int error;
295bdb9ab0dSMark Johnston 
296b957b185SMark Johnston #if MINIDUMP_PAGE_TRACKING == 1
297bdb9ab0dSMark Johnston 	if (do_minidump)
298bdb9ab0dSMark Johnston 		return (minidumpsys(di));
299bdb9ab0dSMark Johnston #endif
300bdb9ab0dSMark Johnston 
301bdb9ab0dSMark Johnston 	bzero(&ehdr, sizeof(ehdr));
302bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG0] = ELFMAG0;
303bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG1] = ELFMAG1;
304bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG2] = ELFMAG2;
305bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG3] = ELFMAG3;
306bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_CLASS] = ELF_CLASS;
307bdb9ab0dSMark Johnston #if BYTE_ORDER == LITTLE_ENDIAN
308bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
309bdb9ab0dSMark Johnston #else
310bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
311bdb9ab0dSMark Johnston #endif
312bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
313bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;	/* XXX big picture? */
314bdb9ab0dSMark Johnston 	ehdr.e_type = ET_CORE;
315bdb9ab0dSMark Johnston 	ehdr.e_machine = EM_VALUE;
316bdb9ab0dSMark Johnston 	ehdr.e_phoff = sizeof(ehdr);
317bdb9ab0dSMark Johnston 	ehdr.e_flags = 0;
318bdb9ab0dSMark Johnston 	ehdr.e_ehsize = sizeof(ehdr);
319bdb9ab0dSMark Johnston 	ehdr.e_phentsize = sizeof(Elf_Phdr);
320bdb9ab0dSMark Johnston 	ehdr.e_shentsize = sizeof(Elf_Shdr);
321bdb9ab0dSMark Johnston 
322bdb9ab0dSMark Johnston 	dumpsys_pa_init();
323bdb9ab0dSMark Johnston 
324bdb9ab0dSMark Johnston 	/* Calculate dump size. */
325bdb9ab0dSMark Johnston 	dumpsize = 0L;
326bdb9ab0dSMark Johnston 	ehdr.e_phnum = dumpsys_foreach_chunk(cb_size, &dumpsize) +
327bdb9ab0dSMark Johnston 	    DUMPSYS_NUM_AUX_HDRS;
328bdb9ab0dSMark Johnston 	hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
329bdb9ab0dSMark Johnston 	fileofs = MD_ALIGN(hdrsz);
330bdb9ab0dSMark Johnston 	dumpsize += fileofs;
3315dc5dab6SConrad Meyer 	hdrgap = fileofs - roundup2((off_t)hdrsz, di->blocksize);
332bdb9ab0dSMark Johnston 
33301938d36SMark Johnston 	dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARCH_VERSION,
33401938d36SMark Johnston 	    dumpsize);
335bdb9ab0dSMark Johnston 
33646fcd1afSMark Johnston 	error = dump_start(di, &kdh);
33750ef60daSMark Johnston 	if (error != 0)
338bdb9ab0dSMark Johnston 		goto fail;
339bdb9ab0dSMark Johnston 
34020f85b1dSMark Johnston 	printf("Dumping %ju MB (%d chunks)\n", (uintmax_t)dumpsize >> 20,
34120f85b1dSMark Johnston 	    ehdr.e_phnum - DUMPSYS_NUM_AUX_HDRS);
34220f85b1dSMark Johnston 
343bdb9ab0dSMark Johnston 	/* Dump ELF header */
344bdb9ab0dSMark Johnston 	error = dumpsys_buf_write(di, (char*)&ehdr, sizeof(ehdr));
345bdb9ab0dSMark Johnston 	if (error)
346bdb9ab0dSMark Johnston 		goto fail;
347bdb9ab0dSMark Johnston 
348bdb9ab0dSMark Johnston 	/* Dump program headers */
349bdb9ab0dSMark Johnston 	error = dumpsys_foreach_chunk(cb_dumphdr, di);
350bdb9ab0dSMark Johnston 	if (error < 0)
351bdb9ab0dSMark Johnston 		goto fail;
352bdb9ab0dSMark Johnston 	error = dumpsys_write_aux_headers(di);
353bdb9ab0dSMark Johnston 	if (error < 0)
354bdb9ab0dSMark Johnston 		goto fail;
355bdb9ab0dSMark Johnston 	dumpsys_buf_flush(di);
356bdb9ab0dSMark Johnston 
357bdb9ab0dSMark Johnston 	/*
358bdb9ab0dSMark Johnston 	 * All headers are written using blocked I/O, so we know the
359bdb9ab0dSMark Johnston 	 * current offset is (still) block aligned. Skip the alignement
360bdb9ab0dSMark Johnston 	 * in the file to have the segment contents aligned at page
36146fcd1afSMark Johnston 	 * boundary.
362bdb9ab0dSMark Johnston 	 */
363480f31c2SKonrad Witaszczyk 	error = dumpsys_buf_seek(di, (size_t)hdrgap);
364480f31c2SKonrad Witaszczyk 	if (error)
365480f31c2SKonrad Witaszczyk 		goto fail;
366bdb9ab0dSMark Johnston 
36746fcd1afSMark Johnston 	/* Dump memory chunks. */
368bdb9ab0dSMark Johnston 	error = dumpsys_foreach_chunk(dumpsys_cb_dumpdata, di);
369bdb9ab0dSMark Johnston 	if (error < 0)
370bdb9ab0dSMark Johnston 		goto fail;
371bdb9ab0dSMark Johnston 
37246fcd1afSMark Johnston 	error = dump_finish(di, &kdh);
37350ef60daSMark Johnston 	if (error != 0)
374bdb9ab0dSMark Johnston 		goto fail;
375bdb9ab0dSMark Johnston 
376bdb9ab0dSMark Johnston 	printf("\nDump complete\n");
377bdb9ab0dSMark Johnston 	return (0);
378bdb9ab0dSMark Johnston 
379bdb9ab0dSMark Johnston  fail:
380bdb9ab0dSMark Johnston 	if (error < 0)
381bdb9ab0dSMark Johnston 		error = -error;
382bdb9ab0dSMark Johnston 
383bdb9ab0dSMark Johnston 	if (error == ECANCELED)
384bdb9ab0dSMark Johnston 		printf("\nDump aborted\n");
38550ef60daSMark Johnston 	else if (error == E2BIG || error == ENOSPC)
386bdb9ab0dSMark Johnston 		printf("\nDump failed. Partition too small.\n");
387bdb9ab0dSMark Johnston 	else
388bdb9ab0dSMark Johnston 		printf("\n** DUMP FAILED (ERROR %d) **\n", error);
389bdb9ab0dSMark Johnston 	return (error);
390bdb9ab0dSMark Johnston }
391ab4ed843SMitchell Horne 
392*1adebe3cSMitchell Horne #if MINIDUMP_PAGE_TRACKING == 1
393*1adebe3cSMitchell Horne 
394ab4ed843SMitchell Horne /* Minidump progress bar */
395ab4ed843SMitchell Horne static struct {
396ab4ed843SMitchell Horne 	const int min_per;
397ab4ed843SMitchell Horne 	const int max_per;
398ab4ed843SMitchell Horne 	bool visited;
399ab4ed843SMitchell Horne } progress_track[10] = {
400ab4ed843SMitchell Horne 	{  0,  10, false},
401ab4ed843SMitchell Horne 	{ 10,  20, false},
402ab4ed843SMitchell Horne 	{ 20,  30, false},
403ab4ed843SMitchell Horne 	{ 30,  40, false},
404ab4ed843SMitchell Horne 	{ 40,  50, false},
405ab4ed843SMitchell Horne 	{ 50,  60, false},
406ab4ed843SMitchell Horne 	{ 60,  70, false},
407ab4ed843SMitchell Horne 	{ 70,  80, false},
408ab4ed843SMitchell Horne 	{ 80,  90, false},
409ab4ed843SMitchell Horne 	{ 90, 100, false}
410ab4ed843SMitchell Horne };
411ab4ed843SMitchell Horne 
412ab4ed843SMitchell Horne static uint64_t dumpsys_pb_size;
413ab4ed843SMitchell Horne static uint64_t dumpsys_pb_remaining;
414ab4ed843SMitchell Horne static uint64_t dumpsys_pb_check;
415ab4ed843SMitchell Horne 
416ab4ed843SMitchell Horne /* Reset the progress bar for a dump of dumpsize. */
417ab4ed843SMitchell Horne void
418ab4ed843SMitchell Horne dumpsys_pb_init(uint64_t dumpsize)
419ab4ed843SMitchell Horne {
420ab4ed843SMitchell Horne 	int i;
421ab4ed843SMitchell Horne 
422ab4ed843SMitchell Horne 	dumpsys_pb_size = dumpsys_pb_remaining = dumpsize;
423ab4ed843SMitchell Horne 	dumpsys_pb_check = 0;
424ab4ed843SMitchell Horne 
425ab4ed843SMitchell Horne 	for (i = 0; i < nitems(progress_track); i++)
426ab4ed843SMitchell Horne 		progress_track[i].visited = false;
427ab4ed843SMitchell Horne }
428ab4ed843SMitchell Horne 
429ab4ed843SMitchell Horne /*
430ab4ed843SMitchell Horne  * Update the progress according to the delta bytes that were written out.
431ab4ed843SMitchell Horne  * Check and print the progress percentage.
432ab4ed843SMitchell Horne  */
433ab4ed843SMitchell Horne void
434ab4ed843SMitchell Horne dumpsys_pb_progress(size_t delta)
435ab4ed843SMitchell Horne {
436ab4ed843SMitchell Horne 	int sofar, i;
437ab4ed843SMitchell Horne 
438ab4ed843SMitchell Horne 	dumpsys_pb_remaining -= delta;
439ab4ed843SMitchell Horne 	dumpsys_pb_check += delta;
440ab4ed843SMitchell Horne 
441ab4ed843SMitchell Horne 	/*
442ab4ed843SMitchell Horne 	 * To save time while dumping, only loop through progress_track
443ab4ed843SMitchell Horne 	 * occasionally.
444ab4ed843SMitchell Horne 	 */
445ab4ed843SMitchell Horne 	if ((dumpsys_pb_check >> DUMPSYS_PB_CHECK_BITS) == 0)
446ab4ed843SMitchell Horne 		return;
447ab4ed843SMitchell Horne 	else
448ab4ed843SMitchell Horne 		dumpsys_pb_check &= (1 << DUMPSYS_PB_CHECK_BITS) - 1;
449ab4ed843SMitchell Horne 
450ab4ed843SMitchell Horne 	sofar = 100 - ((dumpsys_pb_remaining * 100) / dumpsys_pb_size);
451ab4ed843SMitchell Horne 	for (i = 0; i < nitems(progress_track); i++) {
452ab4ed843SMitchell Horne 		if (sofar < progress_track[i].min_per ||
453ab4ed843SMitchell Horne 		    sofar > progress_track[i].max_per)
454ab4ed843SMitchell Horne 			continue;
455ab4ed843SMitchell Horne 		if (!progress_track[i].visited) {
456ab4ed843SMitchell Horne 			progress_track[i].visited = true;
457ab4ed843SMitchell Horne 			printf("..%d%%", sofar);
458ab4ed843SMitchell Horne 		}
459ab4ed843SMitchell Horne 		break;
460ab4ed843SMitchell Horne 	}
461ab4ed843SMitchell Horne }
462*1adebe3cSMitchell Horne 
463*1adebe3cSMitchell Horne int
464*1adebe3cSMitchell Horne minidumpsys(struct dumperinfo *di)
465*1adebe3cSMitchell Horne {
466*1adebe3cSMitchell Horne 	struct minidumpstate state;
467*1adebe3cSMitchell Horne 	int error;
468*1adebe3cSMitchell Horne 
469*1adebe3cSMitchell Horne 	state.msgbufp = msgbufp;
470*1adebe3cSMitchell Horne 	state.dump_bitset = vm_page_dump;
471*1adebe3cSMitchell Horne 
472*1adebe3cSMitchell Horne 	error = cpu_minidumpsys(di, &state);
473*1adebe3cSMitchell Horne 
474*1adebe3cSMitchell Horne 	return (error);
475*1adebe3cSMitchell Horne }
476*1adebe3cSMitchell Horne #endif /* MINIDUMP_PAGE_TRACKING == 1 */
477