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

Cache object: 99cb9ca186e2bf99e1dacd3e881b0cec


[ 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.