1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010 Oleksandr Tymoshenko <gonzo@freebsd.org>
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/arm/arm/minidump_machdep.c v214223
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/cons.h>
39 #include <sys/kernel.h>
40 #include <sys/kerneldump.h>
41 #include <sys/msgbuf.h>
42 #include <sys/watchdog.h>
43 #include <sys/vmmeter.h>
44 #include <vm/vm.h>
45 #include <vm/vm_param.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_phys.h>
49 #include <vm/vm_dumpset.h>
50 #include <machine/atomic.h>
51 #include <machine/elf.h>
52 #include <machine/md_var.h>
53 #include <machine/minidump.h>
54 #include <machine/cache.h>
55
56 CTASSERT(sizeof(struct kerneldumpheader) == 512);
57
58 static struct kerneldumpheader kdh;
59
60 /* Handle chunked writes. */
61 static uint64_t dumpsize;
62 /* Just auxiliary bufffer */
63 static char tmpbuffer[PAGE_SIZE] __aligned(sizeof(uint64_t));
64
65 extern pd_entry_t *kernel_segmap;
66
67 static int
68 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
69 {
70 size_t len;
71 int error, c;
72 u_int maxdumpsz;
73
74 maxdumpsz = di->maxiosize;
75
76 if (maxdumpsz == 0) /* seatbelt */
77 maxdumpsz = PAGE_SIZE;
78
79 error = 0;
80
81 while (sz) {
82 len = min(maxdumpsz, sz);
83
84 dumpsys_pb_progress(len);
85 wdog_kern_pat(WD_LASTVAL);
86
87 if (ptr) {
88 error = dump_append(di, ptr, 0, len);
89 if (error)
90 return (error);
91 ptr += len;
92 sz -= len;
93 } else {
94 panic("pa is not supported");
95 }
96
97 /* Check for user abort. */
98 c = cncheckc();
99 if (c == 0x03)
100 return (ECANCELED);
101 if (c != -1)
102 printf(" (CTRL-C to abort) ");
103 }
104
105 return (0);
106 }
107
108 int
109 cpu_minidumpsys(struct dumperinfo *di, const struct minidumpstate *state)
110 {
111 struct minidumphdr mdhdr;
112 struct msgbuf *mbp;
113 uint64_t *dump_avail_buf;
114 uint32_t ptesize;
115 vm_paddr_t pa;
116 vm_offset_t prev_pte = 0;
117 uint32_t count = 0;
118 vm_offset_t va;
119 pt_entry_t *pte;
120 int i, error;
121 void *dump_va;
122
123 /* Live dumps are untested. */
124 if (!dumping)
125 return (EOPNOTSUPP);
126
127 /* Flush cache */
128 mips_dcache_wbinv_all();
129
130 /* Walk page table pages, set bits in vm_page_dump */
131 ptesize = 0;
132
133 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
134 ptesize += PAGE_SIZE;
135 pte = pmap_pte(kernel_pmap, va);
136 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
137 for (i = 0; i < NPTEPG; i++) {
138 if (pte_test(&pte[i], PTE_V)) {
139 pa = TLBLO_PTE_TO_PA(pte[i]);
140 if (vm_phys_is_dumpable(pa))
141 vm_page_dump_add(state->dump_bitset,
142 pa);
143 }
144 }
145 }
146
147 /*
148 * Now mark pages from 0 to phys_avail[0], that's where kernel
149 * and pages allocated by pmap_steal reside
150 */
151 for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
152 if (vm_phys_is_dumpable(pa))
153 vm_page_dump_add(state->dump_bitset, pa);
154 }
155
156 /* Calculate dump size. */
157 mbp = state->msgbufp;
158 dumpsize = ptesize;
159 dumpsize += round_page(mbp->msg_size);
160 dumpsize += round_page(nitems(dump_avail) * sizeof(uint64_t));
161 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
162 VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
163 /* Clear out undumpable pages now if needed */
164 if (vm_phys_is_dumpable(pa))
165 dumpsize += PAGE_SIZE;
166 else
167 vm_page_dump_drop(state->dump_bitset, pa);
168 }
169 dumpsize += PAGE_SIZE;
170
171 dumpsys_pb_init(dumpsize);
172
173 /* Initialize mdhdr */
174 bzero(&mdhdr, sizeof(mdhdr));
175 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
176 mdhdr.version = MINIDUMP_VERSION;
177 mdhdr.msgbufsize = mbp->msg_size;
178 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
179 mdhdr.ptesize = ptesize;
180 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
181 mdhdr.dumpavailsize = round_page(nitems(dump_avail) * sizeof(uint64_t));
182
183 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION,
184 dumpsize);
185
186 error = dump_start(di, &kdh);
187 if (error != 0)
188 goto fail;
189
190 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
191 ptoa((uintmax_t)physmem) / 1048576);
192
193 /* Dump my header */
194 bzero(tmpbuffer, sizeof(tmpbuffer));
195 bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
196 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
197 if (error)
198 goto fail;
199
200 /* Dump msgbuf up front */
201 error = write_buffer(di, mbp->msg_ptr, round_page(mbp->msg_size));
202 if (error)
203 goto fail;
204
205 /* Dump dump_avail. Make a copy using 64-bit physical addresses. */
206 _Static_assert(nitems(dump_avail) * sizeof(uint64_t) <=
207 sizeof(tmpbuffer), "Large dump_avail not handled");
208 bzero(tmpbuffer, sizeof(tmpbuffer));
209 if (sizeof(dump_avail[0]) != sizeof(uint64_t)) {
210 dump_avail_buf = (uint64_t *)tmpbuffer;
211 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i++) {
212 dump_avail_buf[i] = dump_avail[i];
213 dump_avail_buf[i + 1] = dump_avail[i + 1];
214 }
215 } else {
216 memcpy(tmpbuffer, dump_avail, sizeof(dump_avail));
217 }
218 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
219 if (error)
220 goto fail;
221
222 /* Dump bitmap */
223 error = write_buffer(di, (char *)vm_page_dump,
224 round_page(BITSET_SIZE(vm_page_dump_pages)));
225 if (error)
226 goto fail;
227
228 /* Dump kernel page table pages */
229 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
230 pte = pmap_pte(kernel_pmap, va);
231 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
232 if (!count) {
233 prev_pte = (vm_offset_t)pte;
234 count++;
235 } else {
236 if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
237 count++;
238 else {
239 error = write_buffer(di, (char*)prev_pte,
240 count * PAGE_SIZE);
241 if (error)
242 goto fail;
243 count = 1;
244 prev_pte = (vm_offset_t)pte;
245 }
246 }
247 }
248
249 if (count) {
250 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
251 if (error)
252 goto fail;
253 count = 0;
254 prev_pte = 0;
255 }
256
257 /* Dump memory chunks page by page */
258 VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
259 dump_va = pmap_kenter_temporary(pa, 0);
260 error = write_buffer(di, dump_va, PAGE_SIZE);
261 if (error)
262 goto fail;
263 pmap_kenter_temporary_free(pa);
264 }
265
266 error = dump_finish(di, &kdh);
267 if (error != 0)
268 goto fail;
269
270 printf("\nDump complete\n");
271 return (0);
272
273 fail:
274 if (error < 0)
275 error = -error;
276
277 if (error == ECANCELED)
278 printf("\nDump aborted\n");
279 else if (error == E2BIG || error == ENOSPC) {
280 printf("\nDump failed. Partition too small (about %lluMB were "
281 "needed this time).\n", (long long)dumpsize >> 20);
282 } else
283 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
284 return (error);
285 }
Cache object: a100531cfd82c2ee4404d0b132379bbe
|