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/arm/arm/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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2006 Peter Wemm
    5  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  *
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   28  *
   29  * from: FreeBSD: src/sys/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include "opt_watchdog.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/conf.h>
   40 #include <sys/cons.h>
   41 #include <sys/kernel.h>
   42 #include <sys/kerneldump.h>
   43 #include <sys/msgbuf.h>
   44 #ifdef SW_WATCHDOG
   45 #include <sys/watchdog.h>
   46 #endif
   47 #include <vm/vm.h>
   48 #include <vm/vm_param.h>
   49 #include <vm/vm_page.h>
   50 #include <vm/vm_phys.h>
   51 #include <vm/vm_dumpset.h>
   52 #include <vm/pmap.h>
   53 #include <machine/atomic.h>
   54 #include <machine/cpu.h>
   55 #include <machine/elf.h>
   56 #include <machine/md_var.h>
   57 #include <machine/minidump.h>
   58 
   59 CTASSERT(sizeof(struct kerneldumpheader) == 512);
   60 
   61 static struct kerneldumpheader kdh;
   62 
   63 /* Handle chunked writes. */
   64 static size_t fragsz;
   65 static void *dump_va;
   66 static uint64_t counter, progress;
   67 
   68 static int
   69 is_dumpable(vm_paddr_t pa)
   70 {
   71         vm_page_t m;
   72         int i;
   73 
   74         if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
   75                 return ((m->flags & PG_NODUMP) == 0);
   76         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
   77                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
   78                         return (1);
   79         }
   80         return (0);
   81 }
   82 
   83 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
   84 
   85 static int
   86 blk_flush(struct dumperinfo *di)
   87 {
   88         int error;
   89 
   90         if (fragsz == 0)
   91                 return (0);
   92 
   93         error = dump_append(di, dump_va, 0, fragsz);
   94         fragsz = 0;
   95         return (error);
   96 }
   97 
   98 static int
   99 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
  100 {
  101         size_t len;
  102         int error, i, c;
  103         u_int maxdumpsz;
  104 
  105         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
  106         if (maxdumpsz == 0)     /* seatbelt */
  107                 maxdumpsz = PAGE_SIZE;
  108         error = 0;
  109         if (ptr != NULL && pa != 0) {
  110                 printf("cant have both va and pa!\n");
  111                 return (EINVAL);
  112         }
  113         if (pa != 0) {
  114                 if ((sz % PAGE_SIZE) != 0) {
  115                         printf("size not page aligned\n");
  116                         return (EINVAL);
  117                 }
  118                 if ((pa & PAGE_MASK) != 0) {
  119                         printf("address not page aligned\n");
  120                         return (EINVAL);
  121                 }
  122         }
  123         if (ptr != NULL) {
  124                 /* Flush any pre-existing pa pages before a virtual dump. */
  125                 error = blk_flush(di);
  126                 if (error)
  127                         return (error);
  128         }
  129         while (sz) {
  130                 len = maxdumpsz - fragsz;
  131                 if (len > sz)
  132                         len = sz;
  133                 counter += len;
  134                 progress -= len;
  135                 if (counter >> 22) {
  136                         printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
  137                         counter &= (1<<22) - 1;
  138                 }
  139 
  140 #ifdef SW_WATCHDOG
  141                 wdog_kern_pat(WD_LASTVAL);
  142 #endif
  143                 if (ptr) {
  144                         error = dump_append(di, ptr, 0, len);
  145                         if (error)
  146                                 return (error);
  147                         ptr += len;
  148                         sz -= len;
  149                 } else {
  150                         for (i = 0; i < len; i += PAGE_SIZE)
  151                                 dump_va = pmap_kenter_temporary(pa + i,
  152                                     (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 buffer for general use. Its size must be one page at least. */
  175 static char dumpbuf[PAGE_SIZE] __aligned(sizeof(uint64_t));
  176 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
  177 
  178 int
  179 minidumpsys(struct dumperinfo *di)
  180 {
  181         struct minidumphdr mdhdr;
  182         uint64_t dumpsize, *dump_avail_buf;
  183         uint32_t ptesize;
  184         uint32_t pa, prev_pa = 0, count = 0;
  185         vm_offset_t va;
  186         int error, i;
  187         char *addr;
  188 
  189         /*
  190          * Flush caches.  Note that in the SMP case this operates only on the
  191          * current CPU's L1 cache.  Before we reach this point, code in either
  192          * the system shutdown or kernel debugger has called stop_cpus() to stop
  193          * all cores other than this one.  Part of the ARM handling of
  194          * stop_cpus() is to call wbinv_all() on that core's local L1 cache.  So
  195          * by time we get to here, all that remains is to flush the L1 for the
  196          * current CPU, then the L2.
  197          */
  198         dcache_wbinv_poc_all();
  199 
  200         counter = 0;
  201         /* Walk page table pages, set bits in vm_page_dump */
  202         ptesize = 0;
  203         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
  204                 pa = pmap_dump_kextract(va, NULL);
  205                 if (pa != 0 && is_dumpable(pa))
  206                         dump_add_page(pa);
  207                 ptesize += sizeof(pt2_entry_t);
  208         }
  209 
  210         /* Calculate dump size. */
  211         dumpsize = ptesize;
  212         dumpsize += round_page(msgbufp->msg_size);
  213         dumpsize += round_page(nitems(dump_avail) * sizeof(uint64_t));
  214         dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
  215         VM_PAGE_DUMP_FOREACH(pa) {
  216                 /* Clear out undumpable pages now if needed */
  217                 if (is_dumpable(pa))
  218                         dumpsize += PAGE_SIZE;
  219                 else
  220                         dump_drop_page(pa);
  221         }
  222         dumpsize += PAGE_SIZE;
  223 
  224         progress = dumpsize;
  225 
  226         /* Initialize mdhdr */
  227         bzero(&mdhdr, sizeof(mdhdr));
  228         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
  229         mdhdr.version = MINIDUMP_VERSION;
  230         mdhdr.msgbufsize = msgbufp->msg_size;
  231         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
  232         mdhdr.ptesize = ptesize;
  233         mdhdr.kernbase = KERNBASE;
  234         mdhdr.arch = __ARM_ARCH;
  235         mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
  236         mdhdr.dumpavailsize = round_page(nitems(dump_avail) * sizeof(uint64_t));
  237 
  238         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
  239             dumpsize);
  240 
  241         error = dump_start(di, &kdh);
  242         if (error != 0)
  243                 goto fail;
  244 
  245         printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
  246         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
  247 
  248         /* Dump my header */
  249         bzero(dumpbuf, sizeof(dumpbuf));
  250         bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
  251         error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
  252         if (error)
  253                 goto fail;
  254 
  255         /* Dump msgbuf up front */
  256         error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
  257             round_page(msgbufp->msg_size));
  258         if (error)
  259                 goto fail;
  260 
  261         /* Dump dump_avail.  Make a copy using 64-bit physical addresses. */
  262         _Static_assert(nitems(dump_avail) * sizeof(uint64_t) <= sizeof(dumpbuf),
  263             "Large dump_avail not handled");
  264         bzero(dumpbuf, sizeof(dumpbuf));
  265         dump_avail_buf = (uint64_t *)dumpbuf;
  266         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
  267                 dump_avail_buf[i] = dump_avail[i];
  268                 dump_avail_buf[i + 1] = dump_avail[i + 1];
  269         }
  270         error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
  271         if (error)
  272                 goto fail;
  273 
  274         /* Dump bitmap */
  275         error = blk_write(di, (char *)vm_page_dump, 0,
  276             round_page(BITSET_SIZE(vm_page_dump_pages)));
  277         if (error)
  278                 goto fail;
  279 
  280         /* Dump kernel page table pages */
  281         addr = dumpbuf;
  282         for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
  283                 pmap_dump_kextract(va, (pt2_entry_t *)addr);
  284                 addr += sizeof(pt2_entry_t);
  285                 if (addr == dumpbuf + sizeof(dumpbuf)) {
  286                         error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
  287                         if (error != 0)
  288                                 goto fail;
  289                         addr = dumpbuf;
  290                 }
  291         }
  292         if (addr != dumpbuf) {
  293                 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
  294                 if (error != 0)
  295                         goto fail;
  296         }
  297 
  298         /* Dump memory chunks */
  299         VM_PAGE_DUMP_FOREACH(pa) {
  300                 if (!count) {
  301                         prev_pa = pa;
  302                         count++;
  303                 } else {
  304                         if (pa == (prev_pa + count * PAGE_SIZE))
  305                                 count++;
  306                         else {
  307                                 error = blk_write(di, NULL, prev_pa,
  308                                     count * PAGE_SIZE);
  309                                 if (error)
  310                                         goto fail;
  311                                 count = 1;
  312                                 prev_pa = pa;
  313                         }
  314                 }
  315         }
  316         if (count) {
  317                 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
  318                 if (error)
  319                         goto fail;
  320                 count = 0;
  321                 prev_pa = 0;
  322         }
  323 
  324         error = blk_flush(di);
  325         if (error)
  326                 goto fail;
  327 
  328         error = dump_finish(di, &kdh);
  329         if (error != 0)
  330                 goto fail;
  331 
  332         printf("\nDump complete\n");
  333         return (0);
  334 
  335 fail:
  336         if (error < 0)
  337                 error = -error;
  338 
  339         if (error == ECANCELED)
  340                 printf("\nDump aborted\n");
  341         else if (error == E2BIG || error == ENOSPC) {
  342                 printf("\nDump failed. Partition too small (about %lluMB were "
  343                     "needed this time).\n", (long long)dumpsize >> 20);
  344         } else
  345                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
  346         return (error);
  347 }

Cache object: 2dcfeaf728b195eb468211933850d9d3


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