xref: /freebsd/sys/kern/kern_dump.c (revision d06955f9bdb1416d9196043ed781f9b36dae9adc)
1 /*-
2  * Copyright (c) 2002 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_watchdog.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/cons.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/kerneldump.h>
39 #ifdef SW_WATCHDOG
40 #include <sys/watchdog.h>
41 #endif
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/pmap.h>
45 #include <machine/dump.h>
46 #include <machine/elf.h>
47 #include <machine/md_var.h>
48 #include <machine/pcb.h>
49 
50 CTASSERT(sizeof(struct kerneldumpheader) == 512);
51 
52 #define	MD_ALIGN(x)	roundup2((off_t)(x), PAGE_SIZE)
53 
54 /* Handle buffered writes. */
55 static size_t fragsz;
56 
57 struct dump_pa dump_map[DUMPSYS_MD_PA_NPAIRS];
58 
59 #if !defined(__powerpc__) && !defined(__sparc__)
60 void
61 dumpsys_gen_pa_init(void)
62 {
63 	int n, idx;
64 
65 	bzero(dump_map, sizeof(dump_map));
66 	for (n = 0; n < nitems(dump_map); n++) {
67 		idx = n * 2;
68 		if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0)
69 			break;
70 		dump_map[n].pa_start = dump_avail[idx];
71 		dump_map[n].pa_size = dump_avail[idx + 1] - dump_avail[idx];
72 	}
73 }
74 #endif
75 
76 struct dump_pa *
77 dumpsys_gen_pa_next(struct dump_pa *mdp)
78 {
79 
80 	if (mdp == NULL)
81 		return (&dump_map[0]);
82 
83 	mdp++;
84 	if (mdp->pa_size == 0)
85 		mdp = NULL;
86 	return (mdp);
87 }
88 
89 void
90 dumpsys_gen_wbinv_all(void)
91 {
92 
93 }
94 
95 void
96 dumpsys_gen_unmap_chunk(vm_paddr_t pa __unused, size_t chunk __unused,
97     void *va __unused)
98 {
99 
100 }
101 
102 #if !defined(__sparc__)
103 int
104 dumpsys_gen_write_aux_headers(struct dumperinfo *di)
105 {
106 
107 	return (0);
108 }
109 #endif
110 
111 int
112 dumpsys_buf_seek(struct dumperinfo *di, size_t sz)
113 {
114 	static uint8_t buf[DEV_BSIZE];
115 	size_t nbytes;
116 	int error;
117 
118 	bzero(buf, sizeof(buf));
119 
120 	while (sz > 0) {
121 		nbytes = MIN(sz, sizeof(buf));
122 
123 		error = dump_append(di, buf, 0, nbytes);
124 		if (error)
125 			return (error);
126 		sz -= nbytes;
127 	}
128 
129 	return (0);
130 }
131 
132 int
133 dumpsys_buf_write(struct dumperinfo *di, char *ptr, size_t sz)
134 {
135 	size_t len;
136 	int error;
137 
138 	while (sz) {
139 		len = di->blocksize - fragsz;
140 		if (len > sz)
141 			len = sz;
142 		memcpy((char *)di->blockbuf + fragsz, ptr, len);
143 		fragsz += len;
144 		ptr += len;
145 		sz -= len;
146 		if (fragsz == di->blocksize) {
147 			error = dump_append(di, di->blockbuf, 0, di->blocksize);
148 			if (error)
149 				return (error);
150 			fragsz = 0;
151 		}
152 	}
153 	return (0);
154 }
155 
156 int
157 dumpsys_buf_flush(struct dumperinfo *di)
158 {
159 	int error;
160 
161 	if (fragsz == 0)
162 		return (0);
163 
164 	error = dump_append(di, di->blockbuf, 0, di->blocksize);
165 	fragsz = 0;
166 	return (error);
167 }
168 
169 CTASSERT(PAGE_SHIFT < 20);
170 #define PG2MB(pgs) ((pgs + (1 << (20 - PAGE_SHIFT)) - 1) >> (20 - PAGE_SHIFT))
171 
172 int
173 dumpsys_cb_dumpdata(struct dump_pa *mdp, int seqnr, void *arg)
174 {
175 	struct dumperinfo *di = (struct dumperinfo*)arg;
176 	vm_paddr_t pa;
177 	void *va;
178 	uint64_t pgs;
179 	size_t counter, sz, chunk;
180 	int c, error;
181 	u_int maxdumppgs;
182 
183 	error = 0;	/* catch case in which chunk size is 0 */
184 	counter = 0;	/* Update twiddle every 16MB */
185 	va = NULL;
186 	pgs = mdp->pa_size / PAGE_SIZE;
187 	pa = mdp->pa_start;
188 	maxdumppgs = min(di->maxiosize / PAGE_SIZE, MAXDUMPPGS);
189 	if (maxdumppgs == 0)	/* seatbelt */
190 		maxdumppgs = 1;
191 
192 	printf("  chunk %d: %juMB (%ju pages)", seqnr, (uintmax_t)PG2MB(pgs),
193 	    (uintmax_t)pgs);
194 
195 	dumpsys_wbinv_all();
196 	while (pgs) {
197 		chunk = pgs;
198 		if (chunk > maxdumppgs)
199 			chunk = maxdumppgs;
200 		sz = chunk << PAGE_SHIFT;
201 		counter += sz;
202 		if (counter >> 24) {
203 			printf(" %ju", (uintmax_t)PG2MB(pgs));
204 			counter &= (1 << 24) - 1;
205 		}
206 
207 		dumpsys_map_chunk(pa, chunk, &va);
208 #ifdef SW_WATCHDOG
209 		wdog_kern_pat(WD_LASTVAL);
210 #endif
211 
212 		error = dump_append(di, va, 0, sz);
213 		dumpsys_unmap_chunk(pa, chunk, va);
214 		if (error)
215 			break;
216 		pgs -= chunk;
217 		pa += sz;
218 
219 		/* Check for user abort. */
220 		c = cncheckc();
221 		if (c == 0x03)
222 			return (ECANCELED);
223 		if (c != -1)
224 			printf(" (CTRL-C to abort) ");
225 	}
226 	printf(" ... %s\n", (error) ? "fail" : "ok");
227 	return (error);
228 }
229 
230 int
231 dumpsys_foreach_chunk(dumpsys_callback_t cb, void *arg)
232 {
233 	struct dump_pa *mdp;
234 	int error, seqnr;
235 
236 	seqnr = 0;
237 	mdp = dumpsys_pa_next(NULL);
238 	while (mdp != NULL) {
239 		error = (*cb)(mdp, seqnr++, arg);
240 		if (error)
241 			return (-error);
242 		mdp = dumpsys_pa_next(mdp);
243 	}
244 	return (seqnr);
245 }
246 
247 #if !defined(__sparc__)
248 static off_t fileofs;
249 
250 static int
251 cb_dumphdr(struct dump_pa *mdp, int seqnr, void *arg)
252 {
253 	struct dumperinfo *di = (struct dumperinfo*)arg;
254 	Elf_Phdr phdr;
255 	uint64_t size;
256 	int error;
257 
258 	size = mdp->pa_size;
259 	bzero(&phdr, sizeof(phdr));
260 	phdr.p_type = PT_LOAD;
261 	phdr.p_flags = PF_R;			/* XXX */
262 	phdr.p_offset = fileofs;
263 #ifdef __powerpc__
264 	phdr.p_vaddr = (do_minidump? mdp->pa_start : ~0L);
265 	phdr.p_paddr = (do_minidump? ~0L : mdp->pa_start);
266 #else
267 	phdr.p_vaddr = mdp->pa_start;
268 	phdr.p_paddr = mdp->pa_start;
269 #endif
270 	phdr.p_filesz = size;
271 	phdr.p_memsz = size;
272 	phdr.p_align = PAGE_SIZE;
273 
274 	error = dumpsys_buf_write(di, (char*)&phdr, sizeof(phdr));
275 	fileofs += phdr.p_filesz;
276 	return (error);
277 }
278 
279 static int
280 cb_size(struct dump_pa *mdp, int seqnr, void *arg)
281 {
282 	uint64_t *sz;
283 
284 	sz = (uint64_t *)arg;
285 	*sz += (uint64_t)mdp->pa_size;
286 	return (0);
287 }
288 
289 int
290 dumpsys_generic(struct dumperinfo *di)
291 {
292 	static struct kerneldumpheader kdh;
293 	Elf_Ehdr ehdr;
294 	uint64_t dumpsize;
295 	off_t hdrgap;
296 	size_t hdrsz;
297 	int error;
298 
299 #ifndef __powerpc__
300 	if (do_minidump)
301 		return (minidumpsys(di));
302 #endif
303 
304 	bzero(&ehdr, sizeof(ehdr));
305 	ehdr.e_ident[EI_MAG0] = ELFMAG0;
306 	ehdr.e_ident[EI_MAG1] = ELFMAG1;
307 	ehdr.e_ident[EI_MAG2] = ELFMAG2;
308 	ehdr.e_ident[EI_MAG3] = ELFMAG3;
309 	ehdr.e_ident[EI_CLASS] = ELF_CLASS;
310 #if BYTE_ORDER == LITTLE_ENDIAN
311 	ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
312 #else
313 	ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
314 #endif
315 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
316 	ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;	/* XXX big picture? */
317 	ehdr.e_type = ET_CORE;
318 	ehdr.e_machine = EM_VALUE;
319 	ehdr.e_phoff = sizeof(ehdr);
320 	ehdr.e_flags = 0;
321 	ehdr.e_ehsize = sizeof(ehdr);
322 	ehdr.e_phentsize = sizeof(Elf_Phdr);
323 	ehdr.e_shentsize = sizeof(Elf_Shdr);
324 
325 	dumpsys_pa_init();
326 
327 	/* Calculate dump size. */
328 	dumpsize = 0L;
329 	ehdr.e_phnum = dumpsys_foreach_chunk(cb_size, &dumpsize) +
330 	    DUMPSYS_NUM_AUX_HDRS;
331 	hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
332 	fileofs = MD_ALIGN(hdrsz);
333 	dumpsize += fileofs;
334 	hdrgap = fileofs - roundup2((off_t)hdrsz, di->blocksize);
335 
336 	dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARCH_VERSION,
337 	    dumpsize);
338 
339 	printf("Dumping %ju MB (%d chunks)\n", (uintmax_t)dumpsize >> 20,
340 	    ehdr.e_phnum - DUMPSYS_NUM_AUX_HDRS);
341 
342 	error = dump_start(di, &kdh);
343 	if (error != 0)
344 		goto fail;
345 
346 	/* Dump ELF header */
347 	error = dumpsys_buf_write(di, (char*)&ehdr, sizeof(ehdr));
348 	if (error)
349 		goto fail;
350 
351 	/* Dump program headers */
352 	error = dumpsys_foreach_chunk(cb_dumphdr, di);
353 	if (error < 0)
354 		goto fail;
355 	error = dumpsys_write_aux_headers(di);
356 	if (error < 0)
357 		goto fail;
358 	dumpsys_buf_flush(di);
359 
360 	/*
361 	 * All headers are written using blocked I/O, so we know the
362 	 * current offset is (still) block aligned. Skip the alignement
363 	 * in the file to have the segment contents aligned at page
364 	 * boundary.
365 	 */
366 	error = dumpsys_buf_seek(di, (size_t)hdrgap);
367 	if (error)
368 		goto fail;
369 
370 	/* Dump memory chunks. */
371 	error = dumpsys_foreach_chunk(dumpsys_cb_dumpdata, di);
372 	if (error < 0)
373 		goto fail;
374 
375 	error = dump_finish(di, &kdh);
376 	if (error != 0)
377 		goto fail;
378 
379 	printf("\nDump complete\n");
380 	return (0);
381 
382  fail:
383 	if (error < 0)
384 		error = -error;
385 
386 	if (error == ECANCELED)
387 		printf("\nDump aborted\n");
388 	else if (error == E2BIG || error == ENOSPC)
389 		printf("\nDump failed. Partition too small.\n");
390 	else
391 		printf("\n** DUMP FAILED (ERROR %d) **\n", error);
392 	return (error);
393 }
394 #endif
395