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_base.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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2006 Peter Wemm
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  *
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include "opt_watchdog.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/conf.h>
   37 #include <sys/cons.h>
   38 #include <sys/kernel.h>
   39 #include <sys/kerneldump.h>
   40 #include <sys/msgbuf.h>
   41 #include <sys/watchdog.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/vm_dumpset.h>
   47 #include <vm/pmap.h>
   48 #include <machine/atomic.h>
   49 #include <machine/elf.h>
   50 #include <machine/md_var.h>
   51 #include <machine/minidump.h>
   52 
   53 CTASSERT(sizeof(struct kerneldumpheader) == 512);
   54 
   55 #define MD_ALIGN(x)     (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
   56 #define DEV_ALIGN(x)    roundup2((off_t)(x), DEV_BSIZE)
   57 
   58 static struct kerneldumpheader kdh;
   59 
   60 /* Handle chunked writes. */
   61 static size_t fragsz;
   62 static void *dump_va;
   63 static uint64_t counter, progress;
   64 
   65 static int
   66 is_dumpable(vm_paddr_t pa)
   67 {
   68         vm_page_t m;
   69         int i;
   70 
   71         if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
   72                 return ((m->flags & PG_NODUMP) == 0);
   73         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
   74                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
   75                         return (1);
   76         }
   77         return (0);
   78 }
   79 
   80 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
   81 
   82 static int
   83 blk_flush(struct dumperinfo *di)
   84 {
   85         int error;
   86 
   87         if (fragsz == 0)
   88                 return (0);
   89 
   90         error = dump_append(di, dump_va, 0, fragsz);
   91         fragsz = 0;
   92         return (error);
   93 }
   94 
   95 static int
   96 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
   97 {
   98         size_t len;
   99         int error, i, c;
  100         u_int maxdumpsz;
  101 
  102         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
  103         if (maxdumpsz == 0)     /* seatbelt */
  104                 maxdumpsz = PAGE_SIZE;
  105         error = 0;
  106         if ((sz % PAGE_SIZE) != 0) {
  107                 printf("size not page aligned\n");
  108                 return (EINVAL);
  109         }
  110         if (ptr != NULL && pa != 0) {
  111                 printf("cant have both va and pa!\n");
  112                 return (EINVAL);
  113         }
  114         if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
  115                 printf("address not page aligned\n");
  116                 return (EINVAL);
  117         }
  118         if (ptr != NULL) {
  119                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
  120                 error = blk_flush(di);
  121                 if (error)
  122                         return (error);
  123         }
  124         while (sz) {
  125                 len = maxdumpsz - fragsz;
  126                 if (len > sz)
  127                         len = sz;
  128                 counter += len;
  129                 progress -= len;
  130                 if (counter >> 24) {
  131                         printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
  132                         counter &= (1<<24) - 1;
  133                 }
  134 
  135                 wdog_kern_pat(WD_LASTVAL);
  136 
  137                 if (ptr) {
  138                         error = dump_append(di, ptr, 0, len);
  139                         if (error)
  140                                 return (error);
  141                         ptr += len;
  142                         sz -= len;
  143                 } else {
  144                         for (i = 0; i < len; i += PAGE_SIZE)
  145                                 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
  146                         fragsz += len;
  147                         pa += len;
  148                         sz -= len;
  149                         if (fragsz == maxdumpsz) {
  150                                 error = blk_flush(di);
  151                                 if (error)
  152                                         return (error);
  153                         }
  154                 }
  155 
  156                 /* Check for user abort. */
  157                 c = cncheckc();
  158                 if (c == 0x03)
  159                         return (ECANCELED);
  160                 if (c != -1)
  161                         printf(" (CTRL-C to abort) ");
  162         }
  163 
  164         return (0);
  165 }
  166 
  167 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
  168 static pt_entry_t fakept[NPTEPG];
  169 
  170 #ifdef PMAP_PAE_COMP
  171 #define minidumpsys     minidumpsys_pae
  172 #define IdlePTD         IdlePTD_pae
  173 #else
  174 #define minidumpsys     minidumpsys_nopae
  175 #define IdlePTD         IdlePTD_nopae
  176 #endif
  177 
  178 int
  179 minidumpsys(struct dumperinfo *di)
  180 {
  181         uint64_t dumpsize;
  182         uint32_t ptesize;
  183         vm_offset_t va;
  184         int error;
  185         uint64_t pa;
  186         pd_entry_t *pd;
  187         pt_entry_t *pt;
  188         int j, k;
  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 = IdlePTD;   /* 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(sizeof(dump_avail));
  231         dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
  232         VM_PAGE_DUMP_FOREACH(pa) {
  233                 /* Clear out undumpable pages now if needed */
  234                 if (is_dumpable(pa)) {
  235                         dumpsize += PAGE_SIZE;
  236                 } else {
  237                         dump_drop_page(pa);
  238                 }
  239         }
  240         dumpsize += PAGE_SIZE;
  241 
  242         progress = dumpsize;
  243 
  244         /* Initialize mdhdr */
  245         bzero(&mdhdr, sizeof(mdhdr));
  246         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
  247         mdhdr.version = MINIDUMP_VERSION;
  248         mdhdr.msgbufsize = msgbufp->msg_size;
  249         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
  250         mdhdr.ptesize = ptesize;
  251         mdhdr.kernbase = KERNBASE;
  252         mdhdr.paemode = pae_mode;
  253         mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
  254 
  255         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
  256             dumpsize);
  257 
  258         error = dump_start(di, &kdh);
  259         if (error != 0)
  260                 goto fail;
  261 
  262         printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
  263         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
  264 
  265         /* Dump my header */
  266         bzero(&fakept, sizeof(fakept));
  267         bcopy(&mdhdr, &fakept, sizeof(mdhdr));
  268         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  269         if (error)
  270                 goto fail;
  271 
  272         /* Dump msgbuf up front */
  273         error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
  274         if (error)
  275                 goto fail;
  276 
  277         /* Dump dump_avail */
  278         _Static_assert(sizeof(dump_avail) <= sizeof(fakept),
  279             "Large dump_avail not handled");
  280         bzero(fakept, sizeof(fakept));
  281         memcpy(fakept, dump_avail, sizeof(dump_avail));
  282         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  283         if (error)
  284                 goto fail;
  285 
  286         /* Dump bitmap */
  287         error = blk_write(di, (char *)vm_page_dump, 0,
  288             round_page(BITSET_SIZE(vm_page_dump_pages)));
  289         if (error)
  290                 goto fail;
  291 
  292         /* Dump kernel page table pages */
  293         for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
  294                 /* We always write a page, even if it is zero */
  295                 pd = IdlePTD;   /* always mapped! */
  296                 j = va >> PDRSHIFT;
  297                 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
  298                         /* This is a single 2M block. Generate a fake PTP */
  299                         pa = pd[j] & PG_PS_FRAME;
  300                         for (k = 0; k < NPTEPG; k++) {
  301                                 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
  302                         }
  303                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  304                         if (error)
  305                                 goto fail;
  306                         /* flush, in case we reuse fakept in the same block */
  307                         error = blk_flush(di);
  308                         if (error)
  309                                 goto fail;
  310                         continue;
  311                 }
  312                 if ((pd[j] & PG_V) == PG_V) {
  313                         pa = pd[j] & PG_FRAME;
  314                         error = blk_write(di, 0, pa, PAGE_SIZE);
  315                         if (error)
  316                                 goto fail;
  317                 } else {
  318                         bzero(fakept, sizeof(fakept));
  319                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  320                         if (error)
  321                                 goto fail;
  322                         /* flush, in case we reuse fakept in the same block */
  323                         error = blk_flush(di);
  324                         if (error)
  325                                 goto fail;
  326                 }
  327         }
  328 
  329         /* Dump memory chunks */
  330         VM_PAGE_DUMP_FOREACH(pa) {
  331                 error = blk_write(di, 0, pa, PAGE_SIZE);
  332                 if (error)
  333                         goto fail;
  334         }
  335 
  336         error = blk_flush(di);
  337         if (error)
  338                 goto fail;
  339 
  340         error = dump_finish(di, &kdh);
  341         if (error != 0)
  342                 goto fail;
  343 
  344         printf("\nDump complete\n");
  345         return (0);
  346 
  347  fail:
  348         if (error < 0)
  349                 error = -error;
  350 
  351         if (error == ECANCELED)
  352                 printf("\nDump aborted\n");
  353         else if (error == E2BIG || error == ENOSPC) {
  354                 printf("\nDump failed. Partition too small (about %lluMB were "
  355                     "needed this time).\n", (long long)dumpsize >> 20);
  356         } else
  357                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
  358         return (error);
  359 }

Cache object: 7a38693cb9f69194e9c3ab7e135f8792


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