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 
   64 static int
   65 blk_flush(struct dumperinfo *di)
   66 {
   67         int error;
   68 
   69         if (fragsz == 0)
   70                 return (0);
   71 
   72         error = dump_append(di, dump_va, 0, fragsz);
   73         fragsz = 0;
   74         return (error);
   75 }
   76 
   77 static int
   78 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
   79 {
   80         size_t len;
   81         int error, i, c;
   82         u_int maxdumpsz;
   83 
   84         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
   85         if (maxdumpsz == 0)     /* seatbelt */
   86                 maxdumpsz = PAGE_SIZE;
   87         error = 0;
   88         if ((sz % PAGE_SIZE) != 0) {
   89                 printf("size not page aligned\n");
   90                 return (EINVAL);
   91         }
   92         if (ptr != NULL && pa != 0) {
   93                 printf("cant have both va and pa!\n");
   94                 return (EINVAL);
   95         }
   96         if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
   97                 printf("address not page aligned\n");
   98                 return (EINVAL);
   99         }
  100         if (ptr != NULL) {
  101                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
  102                 error = blk_flush(di);
  103                 if (error)
  104                         return (error);
  105         }
  106         while (sz) {
  107                 len = maxdumpsz - fragsz;
  108                 if (len > sz)
  109                         len = sz;
  110 
  111                 dumpsys_pb_progress(len);
  112                 wdog_kern_pat(WD_LASTVAL);
  113 
  114                 if (ptr) {
  115                         error = dump_append(di, ptr, 0, len);
  116                         if (error)
  117                                 return (error);
  118                         ptr += len;
  119                         sz -= len;
  120                 } else {
  121                         for (i = 0; i < len; i += PAGE_SIZE)
  122                                 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
  123                         fragsz += len;
  124                         pa += len;
  125                         sz -= len;
  126                         if (fragsz == maxdumpsz) {
  127                                 error = blk_flush(di);
  128                                 if (error)
  129                                         return (error);
  130                         }
  131                 }
  132 
  133                 /* Check for user abort. */
  134                 c = cncheckc();
  135                 if (c == 0x03)
  136                         return (ECANCELED);
  137                 if (c != -1)
  138                         printf(" (CTRL-C to abort) ");
  139         }
  140 
  141         return (0);
  142 }
  143 
  144 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
  145 static pt_entry_t fakept[NPTEPG];
  146 
  147 #ifdef PMAP_PAE_COMP
  148 #define cpu_minidumpsys         cpu_minidumpsys_pae
  149 #define IdlePTD                 IdlePTD_pae
  150 #else
  151 #define cpu_minidumpsys         cpu_minidumpsys_nopae
  152 #define IdlePTD                 IdlePTD_nopae
  153 #endif
  154 
  155 int
  156 cpu_minidumpsys(struct dumperinfo *di, const struct minidumpstate *state)
  157 {
  158         uint64_t dumpsize;
  159         uint32_t ptesize;
  160         vm_offset_t va, kva_end;
  161         int error;
  162         uint64_t pa;
  163         pd_entry_t *pd, pde;
  164         pt_entry_t *pt, pte;
  165         int j, k;
  166         struct minidumphdr mdhdr;
  167         struct msgbuf *mbp;
  168 
  169         /* Snapshot the KVA upper bound in case it grows. */
  170         kva_end = kernel_vm_end;
  171 
  172         /*
  173          * Walk the kernel page table pages, setting the active entries in the
  174          * dump bitmap.
  175          *
  176          * NB: for a live dump, we may be racing with updates to the page
  177          * tables, so care must be taken to read each entry only once.
  178          */
  179         ptesize = 0;
  180         for (va = KERNBASE; va < kva_end; va += NBPDR) {
  181                 /*
  182                  * We always write a page, even if it is zero. Each
  183                  * page written corresponds to 2MB of space
  184                  */
  185                 ptesize += PAGE_SIZE;
  186                 pd = IdlePTD;   /* always mapped! */
  187                 j = va >> PDRSHIFT;
  188                 pde = pte_load(&pd[va >> PDRSHIFT]);
  189                 if ((pde & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
  190                         /* This is an entire 2M page. */
  191                         pa = pde & PG_PS_FRAME;
  192                         for (k = 0; k < NPTEPG; k++) {
  193                                 if (vm_phys_is_dumpable(pa))
  194                                         vm_page_dump_add(state->dump_bitset,
  195                                             pa);
  196                                 pa += PAGE_SIZE;
  197                         }
  198                         continue;
  199                 }
  200                 if ((pde & PG_V) == PG_V) {
  201                         /* set bit for each valid page in this 2MB block */
  202                         pt = pmap_kenter_temporary(pde & PG_FRAME, 0);
  203                         for (k = 0; k < NPTEPG; k++) {
  204                                 pte = pte_load(&pt[k]);
  205                                 if ((pte & PG_V) == PG_V) {
  206                                         pa = pte & PG_FRAME;
  207                                         if (vm_phys_is_dumpable(pa))
  208                                                 vm_page_dump_add(
  209                                                     state->dump_bitset, pa);
  210                                 }
  211                         }
  212                 } else {
  213                         /* nothing, we're going to dump a null page */
  214                 }
  215         }
  216 
  217         /* Calculate dump size. */
  218         mbp = state->msgbufp;
  219         dumpsize = ptesize;
  220         dumpsize += round_page(mbp->msg_size);
  221         dumpsize += round_page(sizeof(dump_avail));
  222         dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
  223         VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
  224                 /* Clear out undumpable pages now if needed */
  225                 if (vm_phys_is_dumpable(pa)) {
  226                         dumpsize += PAGE_SIZE;
  227                 } else {
  228                         vm_page_dump_drop(state->dump_bitset, pa);
  229                 }
  230         }
  231         dumpsize += PAGE_SIZE;
  232 
  233         dumpsys_pb_init(dumpsize);
  234 
  235         /* Initialize mdhdr */
  236         bzero(&mdhdr, sizeof(mdhdr));
  237         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
  238         mdhdr.version = MINIDUMP_VERSION;
  239         mdhdr.msgbufsize = mbp->msg_size;
  240         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
  241         mdhdr.ptesize = ptesize;
  242         mdhdr.kernbase = KERNBASE;
  243         mdhdr.paemode = pae_mode;
  244         mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
  245 
  246         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
  247             dumpsize);
  248 
  249         error = dump_start(di, &kdh);
  250         if (error != 0)
  251                 goto fail;
  252 
  253         printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
  254         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
  255 
  256         /* Dump my header */
  257         bzero(&fakept, sizeof(fakept));
  258         bcopy(&mdhdr, &fakept, sizeof(mdhdr));
  259         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  260         if (error)
  261                 goto fail;
  262 
  263         /* Dump msgbuf up front */
  264         error = blk_write(di, (char *)mbp->msg_ptr, 0,
  265             round_page(mbp->msg_size));
  266         if (error)
  267                 goto fail;
  268 
  269         /* Dump dump_avail */
  270         _Static_assert(sizeof(dump_avail) <= sizeof(fakept),
  271             "Large dump_avail not handled");
  272         bzero(fakept, sizeof(fakept));
  273         memcpy(fakept, dump_avail, sizeof(dump_avail));
  274         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  275         if (error)
  276                 goto fail;
  277 
  278         /* Dump bitmap */
  279         error = blk_write(di, (char *)vm_page_dump, 0,
  280             round_page(BITSET_SIZE(vm_page_dump_pages)));
  281         if (error)
  282                 goto fail;
  283 
  284         /* Dump kernel page table pages */
  285         for (va = KERNBASE; va < kva_end; va += NBPDR) {
  286                 /* We always write a page, even if it is zero */
  287                 pd = IdlePTD;   /* always mapped! */
  288                 pde = pte_load(&pd[va >> PDRSHIFT]);
  289                 if ((pde & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
  290                         /* This is a single 2M block. Generate a fake PTP */
  291                         pa = pde & PG_PS_FRAME;
  292                         for (k = 0; k < NPTEPG; k++) {
  293                                 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
  294                         }
  295                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  296                         if (error)
  297                                 goto fail;
  298                         /* flush, in case we reuse fakept in the same block */
  299                         error = blk_flush(di);
  300                         if (error)
  301                                 goto fail;
  302                         continue;
  303                 }
  304                 if ((pde & PG_V) == PG_V) {
  305                         pa = pde & PG_FRAME;
  306                         error = blk_write(di, 0, pa, PAGE_SIZE);
  307                         if (error)
  308                                 goto fail;
  309                 } else {
  310                         bzero(fakept, sizeof(fakept));
  311                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
  312                         if (error)
  313                                 goto fail;
  314                         /* flush, in case we reuse fakept in the same block */
  315                         error = blk_flush(di);
  316                         if (error)
  317                                 goto fail;
  318                 }
  319         }
  320 
  321         /* Dump memory chunks */
  322         VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
  323                 error = blk_write(di, 0, pa, PAGE_SIZE);
  324                 if (error)
  325                         goto fail;
  326         }
  327 
  328         error = blk_flush(di);
  329         if (error)
  330                 goto fail;
  331 
  332         error = dump_finish(di, &kdh);
  333         if (error != 0)
  334                 goto fail;
  335 
  336         printf("\nDump complete\n");
  337         return (0);
  338 
  339  fail:
  340         if (error < 0)
  341                 error = -error;
  342 
  343         if (error == ECANCELED)
  344                 printf("\nDump aborted\n");
  345         else if (error == E2BIG || error == ENOSPC) {
  346                 printf("\nDump failed. Partition too small (about %lluMB were "
  347                     "needed this time).\n", (long long)dumpsize >> 20);
  348         } else
  349                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
  350         return (error);
  351 }

Cache object: e8d123b81b88631dca5ab0e5c09fa77a


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