xref: /freebsd/sys/arm/arm/minidump_machdep.c (revision 266f97b5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Peter Wemm
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/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_watchdog.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/cons.h>
41 #include <sys/kernel.h>
42 #include <sys/kerneldump.h>
43 #include <sys/msgbuf.h>
44 #ifdef SW_WATCHDOG
45 #include <sys/watchdog.h>
46 #endif
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_phys.h>
51 #include <vm/vm_dumpset.h>
52 #include <vm/pmap.h>
53 #include <machine/atomic.h>
54 #include <machine/cpu.h>
55 #include <machine/elf.h>
56 #include <machine/md_var.h>
57 #include <machine/minidump.h>
58 
59 CTASSERT(sizeof(struct kerneldumpheader) == 512);
60 
61 static struct kerneldumpheader kdh;
62 
63 /* Handle chunked writes. */
64 static size_t fragsz;
65 static void *dump_va;
66 
67 static int
68 blk_flush(struct dumperinfo *di)
69 {
70 	int error;
71 
72 	if (fragsz == 0)
73 		return (0);
74 
75 	error = dump_append(di, dump_va, 0, fragsz);
76 	fragsz = 0;
77 	return (error);
78 }
79 
80 static int
81 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
82 {
83 	size_t len;
84 	int error, i, c;
85 	u_int maxdumpsz;
86 
87 	maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
88 	if (maxdumpsz == 0)	/* seatbelt */
89 		maxdumpsz = PAGE_SIZE;
90 	error = 0;
91 	if (ptr != NULL && pa != 0) {
92 		printf("cant have both va and pa!\n");
93 		return (EINVAL);
94 	}
95 	if (pa != 0) {
96 		if ((sz % PAGE_SIZE) != 0) {
97 			printf("size not page aligned\n");
98 			return (EINVAL);
99 		}
100 		if ((pa & PAGE_MASK) != 0) {
101 			printf("address not page aligned\n");
102 			return (EINVAL);
103 		}
104 	}
105 	if (ptr != NULL) {
106 		/* Flush any pre-existing pa pages before a virtual dump. */
107 		error = blk_flush(di);
108 		if (error)
109 			return (error);
110 	}
111 	while (sz) {
112 		len = maxdumpsz - fragsz;
113 		if (len > sz)
114 			len = sz;
115 
116 		dumpsys_pb_progress(len);
117 #ifdef SW_WATCHDOG
118 		wdog_kern_pat(WD_LASTVAL);
119 #endif
120 		if (ptr) {
121 			error = dump_append(di, ptr, 0, len);
122 			if (error)
123 				return (error);
124 			ptr += len;
125 			sz -= len;
126 		} else {
127 			for (i = 0; i < len; i += PAGE_SIZE)
128 				dump_va = pmap_kenter_temporary(pa + i,
129 				    (i + fragsz) >> PAGE_SHIFT);
130 			fragsz += len;
131 			pa += len;
132 			sz -= len;
133 			if (fragsz == maxdumpsz) {
134 				error = blk_flush(di);
135 				if (error)
136 					return (error);
137 			}
138 		}
139 
140 		/* Check for user abort. */
141 		c = cncheckc();
142 		if (c == 0x03)
143 			return (ECANCELED);
144 		if (c != -1)
145 			printf(" (CTRL-C to abort) ");
146 	}
147 
148 	return (0);
149 }
150 
151 /* A buffer for general use. Its size must be one page at least. */
152 static char dumpbuf[PAGE_SIZE] __aligned(sizeof(uint64_t));
153 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
154 
155 int
156 cpu_minidumpsys(struct dumperinfo *di, const struct minidumpstate *state)
157 {
158 	struct minidumphdr mdhdr;
159 	struct msgbuf *mbp;
160 	uint64_t dumpsize, *dump_avail_buf;
161 	uint32_t ptesize;
162 	uint32_t pa, prev_pa = 0, count = 0;
163 	vm_offset_t va, kva_end;
164 	int error, i;
165 	char *addr;
166 
167 	/*
168 	 * Flush caches.  Note that in the SMP case this operates only on the
169 	 * current CPU's L1 cache.  Before we reach this point, code in either
170 	 * the system shutdown or kernel debugger has called stop_cpus() to stop
171 	 * all cores other than this one.  Part of the ARM handling of
172 	 * stop_cpus() is to call wbinv_all() on that core's local L1 cache.  So
173 	 * by time we get to here, all that remains is to flush the L1 for the
174 	 * current CPU, then the L2.
175 	 */
176 	dcache_wbinv_poc_all();
177 
178 	/* Snapshot the KVA upper bound in case it grows. */
179 	kva_end = kernel_vm_end;
180 
181 	/*
182 	 * Walk the kernel page table pages, setting the active entries in the
183 	 * dump bitmap.
184 	 */
185 	ptesize = 0;
186 	for (va = KERNBASE; va < kva_end; va += PAGE_SIZE) {
187 		pa = pmap_dump_kextract(va, NULL);
188 		if (pa != 0 && vm_phys_is_dumpable(pa))
189 			vm_page_dump_add(state->dump_bitset, pa);
190 		ptesize += sizeof(pt2_entry_t);
191 	}
192 
193 	/* Calculate dump size. */
194 	mbp = state->msgbufp;
195 	dumpsize = ptesize;
196 	dumpsize += round_page(mbp->msg_size);
197 	dumpsize += round_page(nitems(dump_avail) * sizeof(uint64_t));
198 	dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
199 	VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
200 		/* Clear out undumpable pages now if needed */
201 		if (vm_phys_is_dumpable(pa))
202 			dumpsize += PAGE_SIZE;
203 		else
204 			vm_page_dump_drop(state->dump_bitset, pa);
205 	}
206 	dumpsize += PAGE_SIZE;
207 
208 	dumpsys_pb_init(dumpsize);
209 
210 	/* Initialize mdhdr */
211 	bzero(&mdhdr, sizeof(mdhdr));
212 	strcpy(mdhdr.magic, MINIDUMP_MAGIC);
213 	mdhdr.version = MINIDUMP_VERSION;
214 	mdhdr.msgbufsize = mbp->msg_size;
215 	mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
216 	mdhdr.ptesize = ptesize;
217 	mdhdr.kernbase = KERNBASE;
218 	mdhdr.arch = __ARM_ARCH;
219 	mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
220 	mdhdr.dumpavailsize = round_page(nitems(dump_avail) * sizeof(uint64_t));
221 
222 	dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
223 	    dumpsize);
224 
225 	error = dump_start(di, &kdh);
226 	if (error != 0)
227 		goto fail;
228 
229 	printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
230 	printf("Dumping %llu MB:", (long long)dumpsize >> 20);
231 
232 	/* Dump my header */
233 	bzero(dumpbuf, sizeof(dumpbuf));
234 	bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
235 	error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
236 	if (error)
237 		goto fail;
238 
239 	/* Dump msgbuf up front */
240 	error = blk_write(di, mbp->msg_ptr, 0, round_page(mbp->msg_size));
241 	if (error)
242 		goto fail;
243 
244 	/* Dump dump_avail.  Make a copy using 64-bit physical addresses. */
245 	_Static_assert(nitems(dump_avail) * sizeof(uint64_t) <= sizeof(dumpbuf),
246 	    "Large dump_avail not handled");
247 	bzero(dumpbuf, sizeof(dumpbuf));
248 	dump_avail_buf = (uint64_t *)dumpbuf;
249 	for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
250 		dump_avail_buf[i] = dump_avail[i];
251 		dump_avail_buf[i + 1] = dump_avail[i + 1];
252 	}
253 	error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
254 	if (error)
255 		goto fail;
256 
257 	/* Dump bitmap */
258 	error = blk_write(di, (char *)state->dump_bitset, 0,
259 	    round_page(BITSET_SIZE(vm_page_dump_pages)));
260 	if (error)
261 		goto fail;
262 
263 	/* Dump kernel page table pages */
264 	addr = dumpbuf;
265 	for (va = KERNBASE; va < kva_end; va += PAGE_SIZE) {
266 		pmap_dump_kextract(va, (pt2_entry_t *)addr);
267 		addr += sizeof(pt2_entry_t);
268 		if (addr == dumpbuf + sizeof(dumpbuf)) {
269 			error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
270 			if (error != 0)
271 				goto fail;
272 			addr = dumpbuf;
273 		}
274 	}
275 	if (addr != dumpbuf) {
276 		error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
277 		if (error != 0)
278 			goto fail;
279 	}
280 
281 	/* Dump memory chunks */
282 	VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
283 		if (!count) {
284 			prev_pa = pa;
285 			count++;
286 		} else {
287 			if (pa == (prev_pa + count * PAGE_SIZE))
288 				count++;
289 			else {
290 				error = blk_write(di, NULL, prev_pa,
291 				    count * PAGE_SIZE);
292 				if (error)
293 					goto fail;
294 				count = 1;
295 				prev_pa = pa;
296 			}
297 		}
298 	}
299 	if (count) {
300 		error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
301 		if (error)
302 			goto fail;
303 		count = 0;
304 		prev_pa = 0;
305 	}
306 
307 	error = blk_flush(di);
308 	if (error)
309 		goto fail;
310 
311 	error = dump_finish(di, &kdh);
312 	if (error != 0)
313 		goto fail;
314 
315 	printf("\nDump complete\n");
316 	return (0);
317 
318 fail:
319 	if (error < 0)
320 		error = -error;
321 
322 	if (error == ECANCELED)
323 		printf("\nDump aborted\n");
324 	else if (error == E2BIG || error == ENOSPC) {
325 		printf("\nDump failed. Partition too small (about %lluMB were "
326 		    "needed this time).\n", (long long)dumpsize >> 20);
327 	} else
328 		printf("\n** DUMP FAILED (ERROR %d) **\n", error);
329 	return (error);
330 }
331