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  * $FreeBSD: src/sys/amd64/amd64/dump_machdep.c,v 1.18 2009/05/29 21:27:12 jamie Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/cons.h>
33 #include <sys/sysctl.h>
34 #include <sys/device.h>
35 #include <sys/kernel.h>
36 #include <sys/kerneldump.h>
37 #include <sys/kbio.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 #include <machine/elf.h>
41 #include <machine/md_var.h>
42 #include <machine/thread.h>
43 #include <machine/vmparam.h>
44 
45 CTASSERT(sizeof(struct kerneldumpheader) == 512);
46 
47 static int do_minidump = 1;
48 TUNABLE_INT("debug.minidump", &do_minidump);
49 SYSCTL_INT(_debug, OID_AUTO, minidump, CTLFLAG_RW, &do_minidump, 0,
50     "Enable mini crash dumps");
51 
52 /*
53  * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
54  * is to protect us from metadata and to protect metadata from us.
55  */
56 #define	SIZEOF_METADATA		(64*1024)
57 
58 #define	MD_ALIGN(x)	(((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
59 #define	DEV_ALIGN(x)	roundup2((off_t)(x), DEV_BSIZE)
60 
61 struct md_pa {
62 	vm_paddr_t md_start;
63 	vm_paddr_t md_size;
64 };
65 
66 typedef int callback_t(struct md_pa *, int, void *);
67 
68 static struct kerneldumpheader kdh;
69 static off_t dumplo, fileofs;
70 
71 /* Handle buffered writes. */
72 static char buffer[DEV_BSIZE];
73 static size_t fragsz;
74 
75 static struct md_pa dump_map[VM_PHYSSEG_MAX+1];
76 
77 static void
78 md_pa_init(void)
79 {
80 	int n;
81 
82 	bzero(dump_map, sizeof(dump_map));
83 	for (n = 0; n < NELEM(dump_map); n++) {
84 		if (dump_avail[n].phys_beg == 0 && dump_avail[n].phys_end == 0)
85 			break;
86 		dump_map[n].md_start = dump_avail[n].phys_beg;
87 		dump_map[n].md_size = dump_avail[n].phys_end -
88 				      dump_avail[n].phys_beg;
89 	}
90 }
91 
92 static struct md_pa *
93 md_pa_first(void)
94 {
95 
96 	return (&dump_map[0]);
97 }
98 
99 static struct md_pa *
100 md_pa_next(struct md_pa *mdp)
101 {
102 
103 	mdp++;
104 	if (mdp->md_size == 0)
105 		mdp = NULL;
106 	return (mdp);
107 }
108 
109 static int
110 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
111 {
112 	size_t len;
113 	int error;
114 
115 	while (sz) {
116 		len = DEV_BSIZE - fragsz;
117 		if (len > sz)
118 			len = sz;
119 		bcopy(ptr, buffer + fragsz, len);
120 		fragsz += len;
121 		ptr += len;
122 		sz -= len;
123 		if (fragsz == DEV_BSIZE) {
124 			error = dev_ddump(di->priv, buffer, 0, dumplo,
125 			    DEV_BSIZE);
126 			if (error)
127 				return error;
128 			dumplo += DEV_BSIZE;
129 			fragsz = 0;
130 		}
131 	}
132 
133 	return (0);
134 }
135 
136 static int
137 buf_flush(struct dumperinfo *di)
138 {
139 	int error;
140 
141 	if (fragsz == 0)
142 		return (0);
143 
144 	error = dev_ddump(di->priv, buffer, 0, dumplo, DEV_BSIZE);
145 	dumplo += DEV_BSIZE;
146 	fragsz = 0;
147 	return (error);
148 }
149 
150 #define PG2MB(pgs) ((pgs + (1 << 8) - 1) >> 8)
151 
152 static int
153 cb_dumpdata(struct md_pa *mdp, int seqnr, void *arg)
154 {
155 	struct dumperinfo *di = (struct dumperinfo*)arg;
156 	vm_paddr_t a, pa;
157 	void *va;
158 	uint64_t pgs;
159 	size_t counter, sz, chunk;
160 	int i, c, error;
161 	int max_iosize;
162 
163 	error = 0;	/* catch case in which chunk size is 0 */
164 	counter = 0;	/* Update twiddle every 16MB */
165 	va = NULL;
166 	pgs = mdp->md_size / PAGE_SIZE;
167 	pa = mdp->md_start;
168 	max_iosize = min(MAXPHYS, di->maxiosize);
169 
170 	kprintf("  chunk %d: %ldMB (%ld pages)", seqnr, PG2MB(pgs), pgs);
171 
172 	cnpoll(TRUE);
173 	while (pgs) {
174 		chunk = pgs;
175 		if (chunk > (max_iosize/PAGE_SIZE))
176 			chunk = max_iosize/PAGE_SIZE;
177 		sz = chunk << PAGE_SHIFT;
178 		counter += sz;
179 		if (counter >> 24) {
180 			kprintf(" %ld", PG2MB(pgs));
181 			counter &= (1<<24) - 1;
182 		}
183 		for (i = 0; i < chunk; i++) {
184 			a = pa + i * PAGE_SIZE;
185 			va = pmap_kenter_temporary(trunc_page(a), i);
186 		}
187 		smp_invltlb();
188 		error = dev_ddump(di->priv, va, 0, dumplo, sz);
189 		if (error)
190 			break;
191 		dumplo += sz;
192 		pgs -= chunk;
193 		pa += sz;
194 
195 		/* Check for user abort. */
196 		c = cncheckc();
197 		if (c == 0x03) {
198 			error = ECANCELED;
199 			goto done;
200 		}
201 		if (c != -1 && c != NOKEY)
202 			kprintf(" (CTRL-C to abort) ");
203 	}
204 	kprintf(" ... %s\n", (error) ? "fail" : "ok");
205 done:
206 	cnpoll(FALSE);
207 	return (error);
208 }
209 
210 static int
211 cb_dumphdr(struct md_pa *mdp, int seqnr, void *arg)
212 {
213 	struct dumperinfo *di = (struct dumperinfo*)arg;
214 	Elf_Phdr phdr;
215 	uint64_t size;
216 	int error;
217 
218 	size = mdp->md_size;
219 	bzero(&phdr, sizeof(phdr));
220 	phdr.p_type = PT_LOAD;
221 	phdr.p_flags = PF_R;			/* XXX */
222 	phdr.p_offset = fileofs;
223 	phdr.p_vaddr = mdp->md_start;
224 	phdr.p_paddr = mdp->md_start;
225 	phdr.p_filesz = size;
226 	phdr.p_memsz = size;
227 	phdr.p_align = PAGE_SIZE;
228 
229 	error = buf_write(di, (char*)&phdr, sizeof(phdr));
230 	fileofs += phdr.p_filesz;
231 	return (error);
232 }
233 
234 static int
235 cb_size(struct md_pa *mdp, int seqnr, void *arg)
236 {
237 	uint64_t *sz = (uint64_t*)arg;
238 
239 	*sz += (uint64_t)mdp->md_size;
240 	return (0);
241 }
242 
243 static int
244 foreach_chunk(callback_t cb, void *arg)
245 {
246 	struct md_pa *mdp;
247 	int error, seqnr;
248 
249 	seqnr = 0;
250 	mdp = md_pa_first();
251 	while (mdp != NULL) {
252 		error = (*cb)(mdp, seqnr++, arg);
253 		if (error)
254 			return (-error);
255 		mdp = md_pa_next(mdp);
256 	}
257 	return (seqnr);
258 }
259 
260 void
261 md_dumpsys(struct dumperinfo *di)
262 {
263 	Elf_Ehdr ehdr;
264 	uint64_t dumpsize;
265 	off_t hdrgap;
266 	size_t hdrsz;
267 	int error;
268 
269 	/*
270 	 * Save context if dump called without panic.
271 	 */
272 	if (dumpthread == NULL) {
273 		savectx(&dumppcb);
274 		dumpthread = curthread;
275 	}
276 
277 	if (do_minidump) {
278 		minidumpsys(di);
279 		return;
280 	}
281 	bzero(&ehdr, sizeof(ehdr));
282 	ehdr.e_ident[EI_MAG0] = ELFMAG0;
283 	ehdr.e_ident[EI_MAG1] = ELFMAG1;
284 	ehdr.e_ident[EI_MAG2] = ELFMAG2;
285 	ehdr.e_ident[EI_MAG3] = ELFMAG3;
286 	ehdr.e_ident[EI_CLASS] = ELF_CLASS;
287 #if BYTE_ORDER == LITTLE_ENDIAN
288 	ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
289 #else
290 	ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
291 #endif
292 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
293 	ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;	/* XXX big picture? */
294 	ehdr.e_type = ET_CORE;
295 	ehdr.e_machine = EM_X86_64;
296 	ehdr.e_phoff = sizeof(ehdr);
297 	ehdr.e_flags = 0;
298 	ehdr.e_ehsize = sizeof(ehdr);
299 	ehdr.e_phentsize = sizeof(Elf_Phdr);
300 	ehdr.e_shentsize = sizeof(Elf_Shdr);
301 
302 	md_pa_init();
303 
304 	/* Calculate dump size. */
305 	dumpsize = 0L;
306 	ehdr.e_phnum = foreach_chunk(cb_size, &dumpsize);
307 	hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
308 	fileofs = MD_ALIGN(hdrsz);
309 	dumpsize += fileofs;
310 	hdrgap = fileofs - DEV_ALIGN(hdrsz);
311 
312 	/* Determine dump offset on device. */
313 	if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
314 		error = ENOSPC;
315 		goto fail;
316 	}
317 	dumplo = di->mediaoffset + di->mediasize - dumpsize;
318 	dumplo -= sizeof(kdh) * 2;
319 
320 	mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION,
321 	    dumpsize, di->blocksize);
322 
323 	kprintf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20,
324 	    ehdr.e_phnum);
325 
326 	/* Dump leader */
327 	error = dev_ddump(di->priv, &kdh, 0, dumplo, sizeof(kdh));
328 	if (error)
329 		goto fail;
330 	dumplo += sizeof(kdh);
331 
332 	/* Dump ELF header */
333 	error = buf_write(di, (char*)&ehdr, sizeof(ehdr));
334 	if (error)
335 		goto fail;
336 
337 	/* Dump program headers */
338 	error = foreach_chunk(cb_dumphdr, di);
339 	if (error < 0)
340 		goto fail;
341 	buf_flush(di);
342 
343 	/*
344 	 * All headers are written using blocked I/O, so we know the
345 	 * current offset is (still) block aligned. Skip the alignement
346 	 * in the file to have the segment contents aligned at page
347 	 * boundary. We cannot use MD_ALIGN on dumplo, because we don't
348 	 * care and may very well be unaligned within the dump device.
349 	 */
350 	dumplo += hdrgap;
351 
352 	/* Dump memory chunks (updates dumplo) */
353 	error = foreach_chunk(cb_dumpdata, di);
354 	if (error < 0)
355 		goto fail;
356 
357 	/* Dump trailer */
358 	error = dev_ddump(di->priv, &kdh, 0, dumplo, sizeof(kdh));
359 	if (error)
360 		goto fail;
361 
362 	/* Signal completion, signoff and exit stage left. */
363 	dev_ddump(di->priv, NULL, 0, 0, 0);
364 	kprintf("\nDump complete\n");
365 	return;
366 
367  fail:
368 	if (error < 0)
369 		error = -error;
370 
371 	if (error == ECANCELED)
372 		kprintf("\nDump aborted\n");
373 	else if (error == ENOSPC)
374 		kprintf("\nDump failed. Partition too small.\n");
375 	else
376 		kprintf("\n** DUMP FAILED (ERROR %d) **\n", error);
377 }
378