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