1 /*-
2 * Copyright (c) 2006 Peter Wemm
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: releng/8.0/sys/amd64/amd64/minidump_machdep.c 193066 2009-05-29 21:27:12Z jamie $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/conf.h>
33 #include <sys/cons.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
36 #include <sys/msgbuf.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 #include <machine/atomic.h>
40 #include <machine/elf.h>
41 #include <machine/md_var.h>
42 #include <machine/vmparam.h>
43 #include <machine/minidump.h>
44
45 CTASSERT(sizeof(struct kerneldumpheader) == 512);
46
47 /*
48 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
49 * is to protect us from metadata and to protect metadata from us.
50 */
51 #define SIZEOF_METADATA (64*1024)
52
53 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
54 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
55
56 extern uint64_t KPDPphys;
57
58 uint64_t *vm_page_dump;
59 int vm_page_dump_size;
60
61 static struct kerneldumpheader kdh;
62 static off_t dumplo;
63
64 /* Handle chunked writes. */
65 static size_t fragsz;
66 static void *dump_va;
67 static size_t counter, progress;
68
69 CTASSERT(sizeof(*vm_page_dump) == 8);
70
71 static int
72 is_dumpable(vm_paddr_t pa)
73 {
74 int i;
75
76 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
77 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
78 return (1);
79 }
80 return (0);
81 }
82
83 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
84
85 static int
86 blk_flush(struct dumperinfo *di)
87 {
88 int error;
89
90 if (fragsz == 0)
91 return (0);
92
93 error = dump_write(di, dump_va, 0, dumplo, fragsz);
94 dumplo += fragsz;
95 fragsz = 0;
96 return (error);
97 }
98
99 static int
100 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
101 {
102 size_t len;
103 int error, i, c;
104 u_int maxdumpsz;
105
106 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
107 if (maxdumpsz == 0) /* seatbelt */
108 maxdumpsz = PAGE_SIZE;
109 error = 0;
110 if ((sz % PAGE_SIZE) != 0) {
111 printf("size not page aligned\n");
112 return (EINVAL);
113 }
114 if (ptr != NULL && pa != 0) {
115 printf("cant have both va and pa!\n");
116 return (EINVAL);
117 }
118 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
119 printf("address not page aligned\n");
120 return (EINVAL);
121 }
122 if (ptr != NULL) {
123 /* If we're doing a virtual dump, flush any pre-existing pa pages */
124 error = blk_flush(di);
125 if (error)
126 return (error);
127 }
128 while (sz) {
129 len = maxdumpsz - fragsz;
130 if (len > sz)
131 len = sz;
132 counter += len;
133 progress -= len;
134 if (counter >> 24) {
135 printf(" %ld", PG2MB(progress >> PAGE_SHIFT));
136 counter &= (1<<24) - 1;
137 }
138 if (ptr) {
139 error = dump_write(di, ptr, 0, dumplo, len);
140 if (error)
141 return (error);
142 dumplo += len;
143 ptr += len;
144 sz -= len;
145 } else {
146 for (i = 0; i < len; i += PAGE_SIZE)
147 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
148 fragsz += len;
149 pa += len;
150 sz -= len;
151 if (fragsz == maxdumpsz) {
152 error = blk_flush(di);
153 if (error)
154 return (error);
155 }
156 }
157
158 /* Check for user abort. */
159 c = cncheckc();
160 if (c == 0x03)
161 return (ECANCELED);
162 if (c != -1)
163 printf(" (CTRL-C to abort) ");
164 }
165
166 return (0);
167 }
168
169 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
170 static pt_entry_t fakept[NPTEPG];
171
172 void
173 minidumpsys(struct dumperinfo *di)
174 {
175 uint64_t dumpsize;
176 uint32_t ptesize;
177 vm_offset_t va;
178 int error;
179 uint64_t bits;
180 uint64_t *pdp, *pd, *pt, pa;
181 int i, j, k, bit;
182 struct minidumphdr mdhdr;
183
184 counter = 0;
185 /* Walk page table pages, set bits in vm_page_dump */
186 ptesize = 0;
187 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
188 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
189 kernel_vm_end); va += NBPDR) {
190 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
191 /*
192 * We always write a page, even if it is zero. Each
193 * page written corresponds to 2MB of space
194 */
195 ptesize += PAGE_SIZE;
196 if ((pdp[i] & PG_V) == 0)
197 continue;
198 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
199 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
200 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
201 /* This is an entire 2M page. */
202 pa = pd[j] & PG_PS_FRAME;
203 for (k = 0; k < NPTEPG; k++) {
204 if (is_dumpable(pa))
205 dump_add_page(pa);
206 pa += PAGE_SIZE;
207 }
208 continue;
209 }
210 if ((pd[j] & PG_V) == PG_V) {
211 /* set bit for each valid page in this 2MB block */
212 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
213 for (k = 0; k < NPTEPG; k++) {
214 if ((pt[k] & PG_V) == PG_V) {
215 pa = pt[k] & PG_FRAME;
216 if (is_dumpable(pa))
217 dump_add_page(pa);
218 }
219 }
220 } else {
221 /* nothing, we're going to dump a null page */
222 }
223 }
224
225 /* Calculate dump size. */
226 dumpsize = ptesize;
227 dumpsize += round_page(msgbufp->msg_size);
228 dumpsize += round_page(vm_page_dump_size);
229 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
230 bits = vm_page_dump[i];
231 while (bits) {
232 bit = bsfq(bits);
233 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
234 /* Clear out undumpable pages now if needed */
235 if (is_dumpable(pa)) {
236 dumpsize += PAGE_SIZE;
237 } else {
238 dump_drop_page(pa);
239 }
240 bits &= ~(1ul << bit);
241 }
242 }
243 dumpsize += PAGE_SIZE;
244
245 /* Determine dump offset on device. */
246 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
247 error = ENOSPC;
248 goto fail;
249 }
250 dumplo = di->mediaoffset + di->mediasize - dumpsize;
251 dumplo -= sizeof(kdh) * 2;
252 progress = dumpsize;
253
254 /* Initialize mdhdr */
255 bzero(&mdhdr, sizeof(mdhdr));
256 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
257 mdhdr.version = MINIDUMP_VERSION;
258 mdhdr.msgbufsize = msgbufp->msg_size;
259 mdhdr.bitmapsize = vm_page_dump_size;
260 mdhdr.ptesize = ptesize;
261 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
262 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
263 mdhdr.dmapend = DMAP_MAX_ADDRESS;
264
265 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
266
267 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
268 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
269
270 /* Dump leader */
271 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
272 if (error)
273 goto fail;
274 dumplo += sizeof(kdh);
275
276 /* Dump my header */
277 bzero(&fakept, sizeof(fakept));
278 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
279 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
280 if (error)
281 goto fail;
282
283 /* Dump msgbuf up front */
284 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
285 if (error)
286 goto fail;
287
288 /* Dump bitmap */
289 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
290 if (error)
291 goto fail;
292
293 /* Dump kernel page table pages */
294 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
295 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
296 kernel_vm_end); va += NBPDR) {
297 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
298 /* We always write a page, even if it is zero */
299 if ((pdp[i] & PG_V) == 0) {
300 bzero(fakept, sizeof(fakept));
301 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
302 if (error)
303 goto fail;
304 /* flush, in case we reuse fakept in the same block */
305 error = blk_flush(di);
306 if (error)
307 goto fail;
308 continue;
309 }
310 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
311 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
312 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
313 /* This is a single 2M block. Generate a fake PTP */
314 pa = pd[j] & PG_PS_FRAME;
315 for (k = 0; k < NPTEPG; k++) {
316 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
317 }
318 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
319 if (error)
320 goto fail;
321 /* flush, in case we reuse fakept in the same block */
322 error = blk_flush(di);
323 if (error)
324 goto fail;
325 continue;
326 }
327 if ((pd[j] & PG_V) == PG_V) {
328 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
329 error = blk_write(di, (char *)pt, 0, PAGE_SIZE);
330 if (error)
331 goto fail;
332 } else {
333 bzero(fakept, sizeof(fakept));
334 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
335 if (error)
336 goto fail;
337 /* flush, in case we reuse fakept in the same block */
338 error = blk_flush(di);
339 if (error)
340 goto fail;
341 }
342 }
343
344 /* Dump memory chunks */
345 /* XXX cluster it up and use blk_dump() */
346 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
347 bits = vm_page_dump[i];
348 while (bits) {
349 bit = bsfq(bits);
350 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
351 error = blk_write(di, 0, pa, PAGE_SIZE);
352 if (error)
353 goto fail;
354 bits &= ~(1ul << bit);
355 }
356 }
357
358 error = blk_flush(di);
359 if (error)
360 goto fail;
361
362 /* Dump trailer */
363 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
364 if (error)
365 goto fail;
366 dumplo += sizeof(kdh);
367
368 /* Signal completion, signoff and exit stage left. */
369 dump_write(di, NULL, 0, 0, 0);
370 printf("\nDump complete\n");
371 return;
372
373 fail:
374 if (error < 0)
375 error = -error;
376
377 if (error == ECANCELED)
378 printf("\nDump aborted\n");
379 else if (error == ENOSPC)
380 printf("\nDump failed. Partition too small.\n");
381 else
382 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
383 }
384
385 void
386 dump_add_page(vm_paddr_t pa)
387 {
388 int idx, bit;
389
390 pa >>= PAGE_SHIFT;
391 idx = pa >> 6; /* 2^6 = 64 */
392 bit = pa & 63;
393 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
394 }
395
396 void
397 dump_drop_page(vm_paddr_t pa)
398 {
399 int idx, bit;
400
401 pa >>= PAGE_SHIFT;
402 idx = pa >> 6; /* 2^6 = 64 */
403 bit = pa & 63;
404 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);
405 }
Cache object: 9a047ca4cbc5f742290b5fbac582a525
|