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