xref: /freebsd/sys/kern/kern_dump.c (revision 20f85b1ddd3dfbaab9131f03da56762291495ff2)
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/proc.h>
36bdb9ab0dSMark Johnston #include <sys/kerneldump.h>
37bdb9ab0dSMark Johnston #include <sys/watchdog.h>
38bdb9ab0dSMark Johnston #include <vm/vm.h>
39bdb9ab0dSMark Johnston #include <vm/vm_param.h>
40bdb9ab0dSMark Johnston #include <vm/pmap.h>
41bdb9ab0dSMark Johnston #include <machine/dump.h>
42bdb9ab0dSMark Johnston #include <machine/elf.h>
43bdb9ab0dSMark Johnston #include <machine/md_var.h>
44bdb9ab0dSMark Johnston #include <machine/pcb.h>
45bdb9ab0dSMark Johnston 
46bdb9ab0dSMark Johnston CTASSERT(sizeof(struct kerneldumpheader) == 512);
47bdb9ab0dSMark Johnston 
485dc5dab6SConrad Meyer #define	MD_ALIGN(x)	roundup2((off_t)(x), PAGE_SIZE)
49bdb9ab0dSMark Johnston 
50bdb9ab0dSMark Johnston /* Handle buffered writes. */
51bdb9ab0dSMark Johnston static size_t fragsz;
52bdb9ab0dSMark Johnston 
53bdb9ab0dSMark Johnston struct dump_pa dump_map[DUMPSYS_MD_PA_NPAIRS];
54bdb9ab0dSMark Johnston 
557888a51fSMarius Strobl #if !defined(__powerpc__) && !defined(__sparc__)
56bdb9ab0dSMark Johnston void
57bdb9ab0dSMark Johnston dumpsys_gen_pa_init(void)
58bdb9ab0dSMark Johnston {
59bdb9ab0dSMark Johnston 	int n, idx;
60bdb9ab0dSMark Johnston 
61bdb9ab0dSMark Johnston 	bzero(dump_map, sizeof(dump_map));
6202abd400SPedro F. Giffuni 	for (n = 0; n < nitems(dump_map); n++) {
63bdb9ab0dSMark Johnston 		idx = n * 2;
64bdb9ab0dSMark Johnston 		if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0)
65bdb9ab0dSMark Johnston 			break;
66bdb9ab0dSMark Johnston 		dump_map[n].pa_start = dump_avail[idx];
67bdb9ab0dSMark Johnston 		dump_map[n].pa_size = dump_avail[idx + 1] - dump_avail[idx];
68bdb9ab0dSMark Johnston 	}
69bdb9ab0dSMark Johnston }
707888a51fSMarius Strobl #endif
71bdb9ab0dSMark Johnston 
72bdb9ab0dSMark Johnston struct dump_pa *
73bdb9ab0dSMark Johnston dumpsys_gen_pa_next(struct dump_pa *mdp)
74bdb9ab0dSMark Johnston {
75bdb9ab0dSMark Johnston 
76bdb9ab0dSMark Johnston 	if (mdp == NULL)
77bdb9ab0dSMark Johnston 		return (&dump_map[0]);
78bdb9ab0dSMark Johnston 
79bdb9ab0dSMark Johnston 	mdp++;
80bdb9ab0dSMark Johnston 	if (mdp->pa_size == 0)
81bdb9ab0dSMark Johnston 		mdp = NULL;
82bdb9ab0dSMark Johnston 	return (mdp);
83bdb9ab0dSMark Johnston }
84bdb9ab0dSMark Johnston 
85bdb9ab0dSMark Johnston void
86bdb9ab0dSMark Johnston dumpsys_gen_wbinv_all(void)
87bdb9ab0dSMark Johnston {
887888a51fSMarius Strobl 
89bdb9ab0dSMark Johnston }
90bdb9ab0dSMark Johnston 
91bdb9ab0dSMark Johnston void
92bdb9ab0dSMark Johnston dumpsys_gen_unmap_chunk(vm_paddr_t pa __unused, size_t chunk __unused,
93bdb9ab0dSMark Johnston     void *va __unused)
94bdb9ab0dSMark Johnston {
957888a51fSMarius Strobl 
96bdb9ab0dSMark Johnston }
97bdb9ab0dSMark Johnston 
987888a51fSMarius Strobl #if !defined(__sparc__)
99bdb9ab0dSMark Johnston int
100bdb9ab0dSMark Johnston dumpsys_gen_write_aux_headers(struct dumperinfo *di)
101bdb9ab0dSMark Johnston {
102bdb9ab0dSMark Johnston 
103bdb9ab0dSMark Johnston 	return (0);
104bdb9ab0dSMark Johnston }
1057888a51fSMarius Strobl #endif
106bdb9ab0dSMark Johnston 
107bdb9ab0dSMark Johnston int
108480f31c2SKonrad Witaszczyk dumpsys_buf_seek(struct dumperinfo *di, size_t sz)
109480f31c2SKonrad Witaszczyk {
110480f31c2SKonrad Witaszczyk 	static uint8_t buf[DEV_BSIZE];
111480f31c2SKonrad Witaszczyk 	size_t nbytes;
112480f31c2SKonrad Witaszczyk 	int error;
113480f31c2SKonrad Witaszczyk 
114480f31c2SKonrad Witaszczyk 	bzero(buf, sizeof(buf));
115480f31c2SKonrad Witaszczyk 
116480f31c2SKonrad Witaszczyk 	while (sz > 0) {
117480f31c2SKonrad Witaszczyk 		nbytes = MIN(sz, sizeof(buf));
118480f31c2SKonrad Witaszczyk 
11946fcd1afSMark Johnston 		error = dump_append(di, buf, 0, nbytes);
120480f31c2SKonrad Witaszczyk 		if (error)
121480f31c2SKonrad Witaszczyk 			return (error);
122480f31c2SKonrad Witaszczyk 		sz -= nbytes;
123480f31c2SKonrad Witaszczyk 	}
124480f31c2SKonrad Witaszczyk 
125480f31c2SKonrad Witaszczyk 	return (0);
126480f31c2SKonrad Witaszczyk }
127480f31c2SKonrad Witaszczyk 
128480f31c2SKonrad Witaszczyk int
129bdb9ab0dSMark Johnston dumpsys_buf_write(struct dumperinfo *di, char *ptr, size_t sz)
130bdb9ab0dSMark Johnston {
131bdb9ab0dSMark Johnston 	size_t len;
132bdb9ab0dSMark Johnston 	int error;
133bdb9ab0dSMark Johnston 
134bdb9ab0dSMark Johnston 	while (sz) {
1355dc5dab6SConrad Meyer 		len = di->blocksize - fragsz;
136bdb9ab0dSMark Johnston 		if (len > sz)
137bdb9ab0dSMark Johnston 			len = sz;
1385dc5dab6SConrad Meyer 		memcpy((char *)di->blockbuf + fragsz, ptr, len);
139bdb9ab0dSMark Johnston 		fragsz += len;
140bdb9ab0dSMark Johnston 		ptr += len;
141bdb9ab0dSMark Johnston 		sz -= len;
1425dc5dab6SConrad Meyer 		if (fragsz == di->blocksize) {
14346fcd1afSMark Johnston 			error = dump_append(di, di->blockbuf, 0, di->blocksize);
144bdb9ab0dSMark Johnston 			if (error)
145bdb9ab0dSMark Johnston 				return (error);
146bdb9ab0dSMark Johnston 			fragsz = 0;
147bdb9ab0dSMark Johnston 		}
148bdb9ab0dSMark Johnston 	}
149bdb9ab0dSMark Johnston 	return (0);
150bdb9ab0dSMark Johnston }
151bdb9ab0dSMark Johnston 
152bdb9ab0dSMark Johnston int
153bdb9ab0dSMark Johnston dumpsys_buf_flush(struct dumperinfo *di)
154bdb9ab0dSMark Johnston {
155bdb9ab0dSMark Johnston 	int error;
156bdb9ab0dSMark Johnston 
157bdb9ab0dSMark Johnston 	if (fragsz == 0)
158bdb9ab0dSMark Johnston 		return (0);
159bdb9ab0dSMark Johnston 
16046fcd1afSMark Johnston 	error = dump_append(di, di->blockbuf, 0, di->blocksize);
161bdb9ab0dSMark Johnston 	fragsz = 0;
162bdb9ab0dSMark Johnston 	return (error);
163bdb9ab0dSMark Johnston }
164bdb9ab0dSMark Johnston 
165bdb9ab0dSMark Johnston CTASSERT(PAGE_SHIFT < 20);
166bdb9ab0dSMark Johnston #define PG2MB(pgs) ((pgs + (1 << (20 - PAGE_SHIFT)) - 1) >> (20 - PAGE_SHIFT))
167bdb9ab0dSMark Johnston 
168bdb9ab0dSMark Johnston int
169bdb9ab0dSMark Johnston dumpsys_cb_dumpdata(struct dump_pa *mdp, int seqnr, void *arg)
170bdb9ab0dSMark Johnston {
171bdb9ab0dSMark Johnston 	struct dumperinfo *di = (struct dumperinfo*)arg;
172bdb9ab0dSMark Johnston 	vm_paddr_t pa;
173bdb9ab0dSMark Johnston 	void *va;
174bdb9ab0dSMark Johnston 	uint64_t pgs;
175bdb9ab0dSMark Johnston 	size_t counter, sz, chunk;
176bdb9ab0dSMark Johnston 	int c, error;
177bdb9ab0dSMark Johnston 	u_int maxdumppgs;
178bdb9ab0dSMark Johnston 
179bdb9ab0dSMark Johnston 	error = 0;	/* catch case in which chunk size is 0 */
180bdb9ab0dSMark Johnston 	counter = 0;	/* Update twiddle every 16MB */
181b85f65afSPedro F. Giffuni 	va = NULL;
182bdb9ab0dSMark Johnston 	pgs = mdp->pa_size / PAGE_SIZE;
183bdb9ab0dSMark Johnston 	pa = mdp->pa_start;
184bdb9ab0dSMark Johnston 	maxdumppgs = min(di->maxiosize / PAGE_SIZE, MAXDUMPPGS);
185bdb9ab0dSMark Johnston 	if (maxdumppgs == 0)	/* seatbelt */
186bdb9ab0dSMark Johnston 		maxdumppgs = 1;
187bdb9ab0dSMark Johnston 
188bdb9ab0dSMark Johnston 	printf("  chunk %d: %juMB (%ju pages)", seqnr, (uintmax_t)PG2MB(pgs),
189bdb9ab0dSMark Johnston 	    (uintmax_t)pgs);
190bdb9ab0dSMark Johnston 
191bdb9ab0dSMark Johnston 	dumpsys_wbinv_all();
192bdb9ab0dSMark Johnston 	while (pgs) {
193bdb9ab0dSMark Johnston 		chunk = pgs;
194bdb9ab0dSMark Johnston 		if (chunk > maxdumppgs)
195bdb9ab0dSMark Johnston 			chunk = maxdumppgs;
196bdb9ab0dSMark Johnston 		sz = chunk << PAGE_SHIFT;
197bdb9ab0dSMark Johnston 		counter += sz;
198bdb9ab0dSMark Johnston 		if (counter >> 24) {
199bdb9ab0dSMark Johnston 			printf(" %ju", (uintmax_t)PG2MB(pgs));
200bdb9ab0dSMark Johnston 			counter &= (1 << 24) - 1;
201bdb9ab0dSMark Johnston 		}
202bdb9ab0dSMark Johnston 
203bdb9ab0dSMark Johnston 		dumpsys_map_chunk(pa, chunk, &va);
204bdb9ab0dSMark Johnston 		wdog_kern_pat(WD_LASTVAL);
205bdb9ab0dSMark Johnston 
20646fcd1afSMark Johnston 		error = dump_append(di, va, 0, sz);
207bdb9ab0dSMark Johnston 		dumpsys_unmap_chunk(pa, chunk, va);
208bdb9ab0dSMark Johnston 		if (error)
209bdb9ab0dSMark Johnston 			break;
210bdb9ab0dSMark Johnston 		pgs -= chunk;
211bdb9ab0dSMark Johnston 		pa += sz;
212bdb9ab0dSMark Johnston 
213bdb9ab0dSMark Johnston 		/* Check for user abort. */
214bdb9ab0dSMark Johnston 		c = cncheckc();
215bdb9ab0dSMark Johnston 		if (c == 0x03)
216bdb9ab0dSMark Johnston 			return (ECANCELED);
217bdb9ab0dSMark Johnston 		if (c != -1)
218bdb9ab0dSMark Johnston 			printf(" (CTRL-C to abort) ");
219bdb9ab0dSMark Johnston 	}
220bdb9ab0dSMark Johnston 	printf(" ... %s\n", (error) ? "fail" : "ok");
221bdb9ab0dSMark Johnston 	return (error);
222bdb9ab0dSMark Johnston }
223bdb9ab0dSMark Johnston 
224bdb9ab0dSMark Johnston int
225bdb9ab0dSMark Johnston dumpsys_foreach_chunk(dumpsys_callback_t cb, void *arg)
226bdb9ab0dSMark Johnston {
227bdb9ab0dSMark Johnston 	struct dump_pa *mdp;
228bdb9ab0dSMark Johnston 	int error, seqnr;
229bdb9ab0dSMark Johnston 
230bdb9ab0dSMark Johnston 	seqnr = 0;
231bdb9ab0dSMark Johnston 	mdp = dumpsys_pa_next(NULL);
232bdb9ab0dSMark Johnston 	while (mdp != NULL) {
233bdb9ab0dSMark Johnston 		error = (*cb)(mdp, seqnr++, arg);
234bdb9ab0dSMark Johnston 		if (error)
235bdb9ab0dSMark Johnston 			return (-error);
236bdb9ab0dSMark Johnston 		mdp = dumpsys_pa_next(mdp);
237bdb9ab0dSMark Johnston 	}
238bdb9ab0dSMark Johnston 	return (seqnr);
239bdb9ab0dSMark Johnston }
240bdb9ab0dSMark Johnston 
2417888a51fSMarius Strobl #if !defined(__sparc__)
242bdb9ab0dSMark Johnston static off_t fileofs;
243bdb9ab0dSMark Johnston 
244bdb9ab0dSMark Johnston static int
245bdb9ab0dSMark Johnston cb_dumphdr(struct dump_pa *mdp, int seqnr, void *arg)
246bdb9ab0dSMark Johnston {
247bdb9ab0dSMark Johnston 	struct dumperinfo *di = (struct dumperinfo*)arg;
248bdb9ab0dSMark Johnston 	Elf_Phdr phdr;
249bdb9ab0dSMark Johnston 	uint64_t size;
250bdb9ab0dSMark Johnston 	int error;
251bdb9ab0dSMark Johnston 
252bdb9ab0dSMark Johnston 	size = mdp->pa_size;
253bdb9ab0dSMark Johnston 	bzero(&phdr, sizeof(phdr));
254bdb9ab0dSMark Johnston 	phdr.p_type = PT_LOAD;
255bdb9ab0dSMark Johnston 	phdr.p_flags = PF_R;			/* XXX */
256bdb9ab0dSMark Johnston 	phdr.p_offset = fileofs;
257bdb9ab0dSMark Johnston #ifdef __powerpc__
258bdb9ab0dSMark Johnston 	phdr.p_vaddr = (do_minidump? mdp->pa_start : ~0L);
259bdb9ab0dSMark Johnston 	phdr.p_paddr = (do_minidump? ~0L : mdp->pa_start);
260bdb9ab0dSMark Johnston #else
261bdb9ab0dSMark Johnston 	phdr.p_vaddr = mdp->pa_start;
262bdb9ab0dSMark Johnston 	phdr.p_paddr = mdp->pa_start;
263bdb9ab0dSMark Johnston #endif
264bdb9ab0dSMark Johnston 	phdr.p_filesz = size;
265bdb9ab0dSMark Johnston 	phdr.p_memsz = size;
266bdb9ab0dSMark Johnston 	phdr.p_align = PAGE_SIZE;
267bdb9ab0dSMark Johnston 
268bdb9ab0dSMark Johnston 	error = dumpsys_buf_write(di, (char*)&phdr, sizeof(phdr));
269bdb9ab0dSMark Johnston 	fileofs += phdr.p_filesz;
270bdb9ab0dSMark Johnston 	return (error);
271bdb9ab0dSMark Johnston }
272bdb9ab0dSMark Johnston 
273bdb9ab0dSMark Johnston static int
274bdb9ab0dSMark Johnston cb_size(struct dump_pa *mdp, int seqnr, void *arg)
275bdb9ab0dSMark Johnston {
276bdb9ab0dSMark Johnston 	uint64_t *sz;
277bdb9ab0dSMark Johnston 
278bdb9ab0dSMark Johnston 	sz = (uint64_t *)arg;
279bdb9ab0dSMark Johnston 	*sz += (uint64_t)mdp->pa_size;
280bdb9ab0dSMark Johnston 	return (0);
281bdb9ab0dSMark Johnston }
282bdb9ab0dSMark Johnston 
283bdb9ab0dSMark Johnston int
284bdb9ab0dSMark Johnston dumpsys_generic(struct dumperinfo *di)
285bdb9ab0dSMark Johnston {
286bdb9ab0dSMark Johnston 	static struct kerneldumpheader kdh;
287bdb9ab0dSMark Johnston 	Elf_Ehdr ehdr;
288bdb9ab0dSMark Johnston 	uint64_t dumpsize;
289bdb9ab0dSMark Johnston 	off_t hdrgap;
290480f31c2SKonrad Witaszczyk 	size_t hdrsz;
291bdb9ab0dSMark Johnston 	int error;
292bdb9ab0dSMark Johnston 
293bdb9ab0dSMark Johnston #ifndef __powerpc__
294bdb9ab0dSMark Johnston 	if (do_minidump)
295bdb9ab0dSMark Johnston 		return (minidumpsys(di));
296bdb9ab0dSMark Johnston #endif
297bdb9ab0dSMark Johnston 
298bdb9ab0dSMark Johnston 	bzero(&ehdr, sizeof(ehdr));
299bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG0] = ELFMAG0;
300bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG1] = ELFMAG1;
301bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG2] = ELFMAG2;
302bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_MAG3] = ELFMAG3;
303bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_CLASS] = ELF_CLASS;
304bdb9ab0dSMark Johnston #if BYTE_ORDER == LITTLE_ENDIAN
305bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
306bdb9ab0dSMark Johnston #else
307bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
308bdb9ab0dSMark Johnston #endif
309bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
310bdb9ab0dSMark Johnston 	ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;	/* XXX big picture? */
311bdb9ab0dSMark Johnston 	ehdr.e_type = ET_CORE;
312bdb9ab0dSMark Johnston 	ehdr.e_machine = EM_VALUE;
313bdb9ab0dSMark Johnston 	ehdr.e_phoff = sizeof(ehdr);
314bdb9ab0dSMark Johnston 	ehdr.e_flags = 0;
315bdb9ab0dSMark Johnston 	ehdr.e_ehsize = sizeof(ehdr);
316bdb9ab0dSMark Johnston 	ehdr.e_phentsize = sizeof(Elf_Phdr);
317bdb9ab0dSMark Johnston 	ehdr.e_shentsize = sizeof(Elf_Shdr);
318bdb9ab0dSMark Johnston 
319bdb9ab0dSMark Johnston 	dumpsys_pa_init();
320bdb9ab0dSMark Johnston 
321bdb9ab0dSMark Johnston 	/* Calculate dump size. */
322bdb9ab0dSMark Johnston 	dumpsize = 0L;
323bdb9ab0dSMark Johnston 	ehdr.e_phnum = dumpsys_foreach_chunk(cb_size, &dumpsize) +
324bdb9ab0dSMark Johnston 	    DUMPSYS_NUM_AUX_HDRS;
325bdb9ab0dSMark Johnston 	hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
326bdb9ab0dSMark Johnston 	fileofs = MD_ALIGN(hdrsz);
327bdb9ab0dSMark Johnston 	dumpsize += fileofs;
3285dc5dab6SConrad Meyer 	hdrgap = fileofs - roundup2((off_t)hdrsz, di->blocksize);
329bdb9ab0dSMark Johnston 
33001938d36SMark Johnston 	dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARCH_VERSION,
33101938d36SMark Johnston 	    dumpsize);
332bdb9ab0dSMark Johnston 
33346fcd1afSMark Johnston 	error = dump_start(di, &kdh);
33450ef60daSMark Johnston 	if (error != 0)
335bdb9ab0dSMark Johnston 		goto fail;
336bdb9ab0dSMark Johnston 
337*20f85b1dSMark Johnston 	printf("Dumping %ju MB (%d chunks)\n", (uintmax_t)dumpsize >> 20,
338*20f85b1dSMark Johnston 	    ehdr.e_phnum - DUMPSYS_NUM_AUX_HDRS);
339*20f85b1dSMark Johnston 
340bdb9ab0dSMark Johnston 	/* Dump ELF header */
341bdb9ab0dSMark Johnston 	error = dumpsys_buf_write(di, (char*)&ehdr, sizeof(ehdr));
342bdb9ab0dSMark Johnston 	if (error)
343bdb9ab0dSMark Johnston 		goto fail;
344bdb9ab0dSMark Johnston 
345bdb9ab0dSMark Johnston 	/* Dump program headers */
346bdb9ab0dSMark Johnston 	error = dumpsys_foreach_chunk(cb_dumphdr, di);
347bdb9ab0dSMark Johnston 	if (error < 0)
348bdb9ab0dSMark Johnston 		goto fail;
349bdb9ab0dSMark Johnston 	error = dumpsys_write_aux_headers(di);
350bdb9ab0dSMark Johnston 	if (error < 0)
351bdb9ab0dSMark Johnston 		goto fail;
352bdb9ab0dSMark Johnston 	dumpsys_buf_flush(di);
353bdb9ab0dSMark Johnston 
354bdb9ab0dSMark Johnston 	/*
355bdb9ab0dSMark Johnston 	 * All headers are written using blocked I/O, so we know the
356bdb9ab0dSMark Johnston 	 * current offset is (still) block aligned. Skip the alignement
357bdb9ab0dSMark Johnston 	 * in the file to have the segment contents aligned at page
35846fcd1afSMark Johnston 	 * boundary.
359bdb9ab0dSMark Johnston 	 */
360480f31c2SKonrad Witaszczyk 	error = dumpsys_buf_seek(di, (size_t)hdrgap);
361480f31c2SKonrad Witaszczyk 	if (error)
362480f31c2SKonrad Witaszczyk 		goto fail;
363bdb9ab0dSMark Johnston 
36446fcd1afSMark Johnston 	/* Dump memory chunks. */
365bdb9ab0dSMark Johnston 	error = dumpsys_foreach_chunk(dumpsys_cb_dumpdata, di);
366bdb9ab0dSMark Johnston 	if (error < 0)
367bdb9ab0dSMark Johnston 		goto fail;
368bdb9ab0dSMark Johnston 
36946fcd1afSMark Johnston 	error = dump_finish(di, &kdh);
37050ef60daSMark Johnston 	if (error != 0)
371bdb9ab0dSMark Johnston 		goto fail;
372bdb9ab0dSMark Johnston 
373bdb9ab0dSMark Johnston 	printf("\nDump complete\n");
374bdb9ab0dSMark Johnston 	return (0);
375bdb9ab0dSMark Johnston 
376bdb9ab0dSMark Johnston  fail:
377bdb9ab0dSMark Johnston 	if (error < 0)
378bdb9ab0dSMark Johnston 		error = -error;
379bdb9ab0dSMark Johnston 
380bdb9ab0dSMark Johnston 	if (error == ECANCELED)
381bdb9ab0dSMark Johnston 		printf("\nDump aborted\n");
38250ef60daSMark Johnston 	else if (error == E2BIG || error == ENOSPC)
383bdb9ab0dSMark Johnston 		printf("\nDump failed. Partition too small.\n");
384bdb9ab0dSMark Johnston 	else
385bdb9ab0dSMark Johnston 		printf("\n** DUMP FAILED (ERROR %d) **\n", error);
386bdb9ab0dSMark Johnston 	return (error);
387bdb9ab0dSMark Johnston }
3887888a51fSMarius Strobl #endif
389