The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/minidump_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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$");
   29 
   30 #include "opt_pmap.h"
   31 #include "opt_watchdog.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/conf.h>
   36 #include <sys/cons.h>
   37 #include <sys/kernel.h>
   38 #include <sys/kerneldump.h>
   39 #include <sys/msgbuf.h>
   40 #include <sys/watchdog.h>
   41 #include <sys/vmmeter.h>
   42 #include <vm/vm.h>
   43 #include <vm/vm_param.h>
   44 #include <vm/vm_page.h>
   45 #include <vm/vm_phys.h>
   46 #include <vm/pmap.h>
   47 #include <machine/atomic.h>
   48 #include <machine/elf.h>
   49 #include <machine/md_var.h>
   50 #include <machine/minidump.h>
   51 
   52 CTASSERT(sizeof(struct kerneldumpheader) == 512);
   53 
   54 /*
   55  * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
   56  * is to protect us from metadata and to protect metadata from us.
   57  */
   58 #define SIZEOF_METADATA         (64*1024)
   59 
   60 uint64_t *vm_page_dump;
   61 int vm_page_dump_size;
   62 
   63 static struct kerneldumpheader kdh;
   64 static off_t dumplo;
   65 
   66 /* Handle chunked writes. */
   67 static size_t fragsz;
   68 static void *dump_va;
   69 static size_t counter, progress, dumpsize;
   70 
   71 CTASSERT(sizeof(*vm_page_dump) == 8);
   72 
   73 static int
   74 is_dumpable(vm_paddr_t pa)
   75 {
   76         vm_page_t m;
   77         int i;
   78 
   79         if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
   80                 return ((m->flags & PG_NODUMP) == 0);
   81         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
   82                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
   83                         return (1);
   84         }
   85         return (0);
   86 }
   87 
   88 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
   89 
   90 static int
   91 blk_flush(struct dumperinfo *di)
   92 {
   93         int error;
   94 
   95         if (fragsz == 0)
   96                 return (0);
   97 
   98         error = dump_write(di, dump_va, 0, dumplo, fragsz);
   99         dumplo += fragsz;
  100         fragsz = 0;
  101         return (error);
  102 }
  103 
  104 static struct {
  105         int min_per;
  106         int max_per;
  107         int visited;
  108 } progress_track[10] = {
  109         {  0,  10, 0},
  110         { 10,  20, 0},
  111         { 20,  30, 0},
  112         { 30,  40, 0},
  113         { 40,  50, 0},
  114         { 50,  60, 0},
  115         { 60,  70, 0},
  116         { 70,  80, 0},
  117         { 80,  90, 0},
  118         { 90, 100, 0}
  119 };
  120 
  121 static void
  122 report_progress(size_t progress, size_t dumpsize)
  123 {
  124         int sofar, i;
  125 
  126         sofar = 100 - ((progress * 100) / dumpsize);
  127         for (i = 0; i < nitems(progress_track); i++) {
  128                 if (sofar < progress_track[i].min_per ||
  129                     sofar > progress_track[i].max_per)
  130                         continue;
  131                 if (progress_track[i].visited)
  132                         return;
  133                 progress_track[i].visited = 1;
  134                 printf("..%d%%", sofar);
  135                 return;
  136         }
  137 }
  138 
  139 static int
  140 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
  141 {
  142         size_t len;
  143         int error, i, c;
  144         u_int maxdumpsz;
  145 
  146         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
  147         if (maxdumpsz == 0)     /* seatbelt */
  148                 maxdumpsz = PAGE_SIZE;
  149         error = 0;
  150         if ((sz % PAGE_SIZE) != 0) {
  151                 printf("size not page aligned\n");
  152                 return (EINVAL);
  153         }
  154         if (ptr != NULL && pa != 0) {
  155                 printf("cant have both va and pa!\n");
  156                 return (EINVAL);
  157         }
  158         if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
  159                 printf("address not page aligned %p\n", ptr);
  160                 return (EINVAL);
  161         }
  162         if (ptr != NULL) {
  163                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
  164                 error = blk_flush(di);
  165                 if (error)
  166                         return (error);
  167         }
  168         while (sz) {
  169                 len = maxdumpsz - fragsz;
  170                 if (len > sz)
  171                         len = sz;
  172                 counter += len;
  173                 progress -= len;
  174                 if (counter >> 24) {
  175                         report_progress(progress, dumpsize);
  176                         counter &= (1<<24) - 1;
  177                 }
  178 
  179                 wdog_kern_pat(WD_LASTVAL);
  180 
  181                 if (ptr) {
  182                         error = dump_write(di, ptr, 0, dumplo, len);
  183                         if (error)
  184                                 return (error);
  185                         dumplo += len;
  186                         ptr += len;
  187                         sz -= len;
  188                 } else {
  189                         for (i = 0; i < len; i += PAGE_SIZE)
  190                                 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
  191                         fragsz += len;
  192                         pa += len;
  193                         sz -= len;
  194                         if (fragsz == maxdumpsz) {
  195                                 error = blk_flush(di);
  196                                 if (error)
  197                                         return (error);
  198                         }
  199                 }
  200 
  201                 /* Check for user abort. */
  202                 c = cncheckc();
  203                 if (c == 0x03)
  204                         return (ECANCELED);
  205                 if (c != -1)
  206                         printf(" (CTRL-C to abort) ");
  207         }
  208 
  209         return (0);
  210 }
  211 
  212 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
  213 static pd_entry_t fakepd[NPDEPG];
  214 
  215 int
  216 minidumpsys(struct dumperinfo *di)
  217 {
  218         uint32_t pmapsize;
  219         vm_offset_t va;
  220         int error;
  221         uint64_t bits;
  222         uint64_t *pml4, *pdp, *pd, *pt, pa;
  223         size_t size;
  224         int i, ii, j, k, n, bit;
  225         int retry_count;
  226         struct minidumphdr mdhdr;
  227 
  228         retry_count = 0;
  229  retry:
  230         retry_count++;
  231         counter = 0;
  232         for (i = 0; i < nitems(progress_track); i++)
  233                 progress_track[i].visited = 0;
  234         /* Walk page table pages, set bits in vm_page_dump */
  235         pmapsize = 0;
  236         for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
  237             kernel_vm_end); ) {
  238                 /*
  239                  * We always write a page, even if it is zero. Each
  240                  * page written corresponds to 1GB of space
  241                  */
  242                 pmapsize += PAGE_SIZE;
  243                 ii = pmap_pml4e_index(va);
  244                 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
  245                 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
  246                 i = pmap_pdpe_index(va);
  247                 if ((pdp[i] & PG_V) == 0) {
  248                         va += NBPDP;
  249                         continue;
  250                 }
  251 
  252                 /*
  253                  * 1GB page is represented as 512 2MB pages in a dump.
  254                  */
  255                 if ((pdp[i] & PG_PS) != 0) {
  256                         va += NBPDP;
  257                         pa = pdp[i] & PG_PS_FRAME;
  258                         for (n = 0; n < NPDEPG * NPTEPG; n++) {
  259                                 if (is_dumpable(pa))
  260                                         dump_add_page(pa);
  261                                 pa += PAGE_SIZE;
  262                         }
  263                         continue;
  264                 }
  265 
  266                 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
  267                 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
  268                         j = pmap_pde_index(va);
  269 
  270                         if ((pd[j] & PG_V) == 0)
  271                                 continue;
  272 
  273                         if ((pd[j] & PG_PS) != 0) {
  274                                 /* This is an entire 2M page. */
  275                                 pa = pd[j] & PG_PS_FRAME;
  276                                 for (k = 0; k < NPTEPG; k++) {
  277                                         if (is_dumpable(pa))
  278                                                 dump_add_page(pa);
  279                                         pa += PAGE_SIZE;
  280                                 }
  281                                 continue;
  282                         }
  283 
  284                         pa = pd[j] & PG_FRAME;
  285                         /* set bit for this PTE page */
  286                         if (is_dumpable(pa))
  287                                 dump_add_page(pa);
  288                         /* and for each valid page in this 2MB block */
  289                         pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
  290                         for (k = 0; k < NPTEPG; k++) {
  291                                 if ((pt[k] & PG_V) == 0)
  292                                         continue;
  293                                 pa = pt[k] & PG_FRAME;
  294                                 if (is_dumpable(pa))
  295                                         dump_add_page(pa);
  296                         }
  297                 }
  298         }
  299 
  300         /* Calculate dump size. */
  301         dumpsize = pmapsize;
  302         dumpsize += round_page(msgbufp->msg_size);
  303         dumpsize += round_page(vm_page_dump_size);
  304         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
  305                 bits = vm_page_dump[i];
  306                 while (bits) {
  307                         bit = bsfq(bits);
  308                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
  309                         /* Clear out undumpable pages now if needed */
  310                         if (is_dumpable(pa)) {
  311                                 dumpsize += PAGE_SIZE;
  312                         } else {
  313                                 dump_drop_page(pa);
  314                         }
  315                         bits &= ~(1ul << bit);
  316                 }
  317         }
  318         dumpsize += PAGE_SIZE;
  319 
  320         /* Determine dump offset on device. */
  321         if (di->mediasize < SIZEOF_METADATA + dumpsize + di->blocksize * 2) {
  322                 error = E2BIG;
  323                 goto fail;
  324         }
  325         dumplo = di->mediaoffset + di->mediasize - dumpsize;
  326         dumplo -= di->blocksize * 2;
  327         progress = dumpsize;
  328 
  329         /* Initialize mdhdr */
  330         bzero(&mdhdr, sizeof(mdhdr));
  331         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
  332         mdhdr.version = MINIDUMP_VERSION;
  333         mdhdr.msgbufsize = msgbufp->msg_size;
  334         mdhdr.bitmapsize = vm_page_dump_size;
  335         mdhdr.pmapsize = pmapsize;
  336         mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
  337         mdhdr.dmapbase = DMAP_MIN_ADDRESS;
  338         mdhdr.dmapend = DMAP_MAX_ADDRESS;
  339 
  340         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
  341 
  342         printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
  343             ptoa((uintmax_t)physmem) / 1048576);
  344 
  345         /* Dump leader */
  346         error = dump_write_pad(di, &kdh, 0, dumplo, sizeof(kdh), &size);
  347         if (error)
  348                 goto fail;
  349         dumplo += size;
  350 
  351         /* Dump my header */
  352         bzero(&fakepd, sizeof(fakepd));
  353         bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
  354         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
  355         if (error)
  356                 goto fail;
  357 
  358         /* Dump msgbuf up front */
  359         error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
  360         if (error)
  361                 goto fail;
  362 
  363         /* Dump bitmap */
  364         error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
  365         if (error)
  366                 goto fail;
  367 
  368         /* Dump kernel page directory pages */
  369         bzero(fakepd, sizeof(fakepd));
  370         for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
  371             kernel_vm_end); va += NBPDP) {
  372                 ii = pmap_pml4e_index(va);
  373                 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
  374                 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
  375                 i = pmap_pdpe_index(va);
  376 
  377                 /* We always write a page, even if it is zero */
  378                 if ((pdp[i] & PG_V) == 0) {
  379                         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
  380                         if (error)
  381                                 goto fail;
  382                         /* flush, in case we reuse fakepd in the same block */
  383                         error = blk_flush(di);
  384                         if (error)
  385                                 goto fail;
  386                         continue;
  387                 }
  388 
  389                 /* 1GB page is represented as 512 2MB pages in a dump */
  390                 if ((pdp[i] & PG_PS) != 0) {
  391                         /* PDPE and PDP have identical layout in this case */
  392                         fakepd[0] = pdp[i];
  393                         for (j = 1; j < NPDEPG; j++)
  394                                 fakepd[j] = fakepd[j - 1] + NBPDR;
  395                         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
  396                         if (error)
  397                                 goto fail;
  398                         /* flush, in case we reuse fakepd in the same block */
  399                         error = blk_flush(di);
  400                         if (error)
  401                                 goto fail;
  402                         bzero(fakepd, sizeof(fakepd));
  403                         continue;
  404                 }
  405 
  406                 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
  407                 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
  408                 if (error)
  409                         goto fail;
  410                 error = blk_flush(di);
  411                 if (error)
  412                         goto fail;
  413         }
  414 
  415         /* Dump memory chunks */
  416         /* XXX cluster it up and use blk_dump() */
  417         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
  418                 bits = vm_page_dump[i];
  419                 while (bits) {
  420                         bit = bsfq(bits);
  421                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
  422                         error = blk_write(di, 0, pa, PAGE_SIZE);
  423                         if (error)
  424                                 goto fail;
  425                         bits &= ~(1ul << bit);
  426                 }
  427         }
  428 
  429         error = blk_flush(di);
  430         if (error)
  431                 goto fail;
  432 
  433         /* Dump trailer */
  434         error = dump_write_pad(di, &kdh, 0, dumplo, sizeof(kdh), &size);
  435         if (error)
  436                 goto fail;
  437         dumplo += size;
  438 
  439         /* Signal completion, signoff and exit stage left. */
  440         dump_write(di, NULL, 0, 0, 0);
  441         printf("\nDump complete\n");
  442         return (0);
  443 
  444  fail:
  445         if (error < 0)
  446                 error = -error;
  447 
  448         printf("\n");
  449         if (error == ENOSPC) {
  450                 printf("Dump map grown while dumping. ");
  451                 if (retry_count < 5) {
  452                         printf("Retrying...\n");
  453                         goto retry;
  454                 }
  455                 printf("Dump failed.\n");
  456         }
  457         else if (error == ECANCELED)
  458                 printf("Dump aborted\n");
  459         else if (error == E2BIG)
  460                 printf("Dump failed. Partition too small.\n");
  461         else
  462                 printf("** DUMP FAILED (ERROR %d) **\n", error);
  463         return (error);
  464 }
  465 
  466 void
  467 dump_add_page(vm_paddr_t pa)
  468 {
  469         int idx, bit;
  470 
  471         pa >>= PAGE_SHIFT;
  472         idx = pa >> 6;          /* 2^6 = 64 */
  473         bit = pa & 63;
  474         atomic_set_long(&vm_page_dump[idx], 1ul << bit);
  475 }
  476 
  477 void
  478 dump_drop_page(vm_paddr_t pa)
  479 {
  480         int idx, bit;
  481 
  482         pa >>= PAGE_SHIFT;
  483         idx = pa >> 6;          /* 2^6 = 64 */
  484         bit = pa & 63;
  485         atomic_clear_long(&vm_page_dump[idx], 1ul << bit);
  486 }

Cache object: 97c1eb8c34a4e7f26907d50ca0e031bf


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.