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

Cache object: ad97367dde81c7cb67f967ea4487c30a


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