Author: markj
Date: Wed Jan  7 01:01:39 2015
New Revision: 276772
URL: https://svnweb.freebsd.org/changeset/base/276772

Log:
  Factor out duplicated code from dumpsys() on each architecture into generic
  code in sys/kern/kern_dump.c. Most dumpsys() implementations are nearly
  identical and simply redefine a number of constants and helper subroutines;
  a generic implementation will make it easier to implement features around
  kernel core dumps. This change does not alter any minidump code and should
  have no functional impact.
  
  PR:           193873
  Differential Revision:        https://reviews.freebsd.org/D904
  Submitted by: Conrad Meyer <conrad.me...@isilon.com>
  Reviewed by:  jhibbits (earlier version)
  Sponsored by: EMC / Isilon Storage Division

Added:
  head/sys/amd64/include/dump.h   (contents, props changed)
  head/sys/arm/include/dump.h   (contents, props changed)
  head/sys/i386/include/dump.h   (contents, props changed)
  head/sys/kern/kern_dump.c   (contents, props changed)
  head/sys/mips/include/dump.h   (contents, props changed)
  head/sys/pc98/include/dump.h   (contents, props changed)
  head/sys/powerpc/include/dump.h   (contents, props changed)
  head/sys/sparc64/include/dump.h   (contents, props changed)
  head/sys/x86/include/dump.h   (contents, props changed)
Modified:
  head/sys/arm/arm/dump_machdep.c
  head/sys/conf/files
  head/sys/kern/kern_shutdown.c
  head/sys/mips/include/md_var.h
  head/sys/mips/mips/dump_machdep.c
  head/sys/powerpc/aim/mmu_oea.c
  head/sys/powerpc/aim/mmu_oea64.c
  head/sys/powerpc/booke/pmap.c
  head/sys/powerpc/include/pmap.h
  head/sys/powerpc/powerpc/dump_machdep.c
  head/sys/powerpc/powerpc/mmu_if.m
  head/sys/powerpc/powerpc/pmap_dispatch.c
  head/sys/sparc64/sparc64/dump_machdep.c
  head/sys/sys/conf.h
  head/sys/sys/kerneldump.h
  head/sys/x86/x86/dump_machdep.c

Added: head/sys/amd64/include/dump.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/amd64/include/dump.h       Wed Jan  7 01:01:39 2015        
(r276772)
@@ -0,0 +1,6 @@
+/*-
+ * This file is in the public domain.
+ */
+/* $FreeBSD$ */
+
+#include <x86/dump.h>

Modified: head/sys/arm/arm/dump_machdep.c
==============================================================================
--- head/sys/arm/arm/dump_machdep.c     Tue Jan  6 23:40:39 2015        
(r276771)
+++ head/sys/arm/arm/dump_machdep.c     Wed Jan  7 01:01:39 2015        
(r276772)
@@ -32,148 +32,26 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/conf.h>
-#include <sys/cons.h>
 #include <sys/sysctl.h>
 #include <sys/kernel.h>
 #include <sys/proc.h>
 #include <sys/kerneldump.h>
-#ifdef SW_WATCHDOG
-#include <sys/watchdog.h>
-#endif
+
 #include <vm/vm.h>
 #include <vm/pmap.h>
+#include <machine/dump.h>
 #include <machine/elf.h>
 #include <machine/md_var.h>
 #include <machine/pcb.h>
 #include <machine/armreg.h>
 
-CTASSERT(sizeof(struct kerneldumpheader) == 512);
-
 int do_minidump = 1;
 SYSCTL_INT(_debug, OID_AUTO, minidump, CTLFLAG_RWTUN, &do_minidump, 0,
     "Enable mini crash dumps");
 
-/*
- * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
- * is to protect us from metadata and to protect metadata from us.
- */
-#define        SIZEOF_METADATA         (64*1024)
-
-#define        MD_ALIGN(x)     (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
-#define        DEV_ALIGN(x)    (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
-extern struct pcb dumppcb;
-
-struct md_pa {
-       vm_paddr_t md_start;
-       vm_paddr_t md_size;
-};
-
-typedef int callback_t(struct md_pa *, int, void *);
-
-static struct kerneldumpheader kdh;
-static off_t dumplo, fileofs;
-
-/* Handle buffered writes. */
-static char buffer[DEV_BSIZE];
-static size_t fragsz;
-
-/* XXX: I suppose 20 should be enough. */
-static struct md_pa dump_map[20];
-
-static void
-md_pa_init(void)
+void
+dumpsys_wbinv_all(void)
 {
-       int n, idx;
-
-       bzero(dump_map, sizeof(dump_map));
-       for (n = 0; n < sizeof(dump_map) / sizeof(dump_map[0]); n++) {
-               idx = n * 2;
-               if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0)
-                       break;
-               dump_map[n].md_start = dump_avail[idx];
-               dump_map[n].md_size = dump_avail[idx + 1] - dump_avail[idx];
-       }
-}
-
-static struct md_pa *
-md_pa_first(void)
-{
-
-       return (&dump_map[0]);
-}
-
-static struct md_pa *
-md_pa_next(struct md_pa *mdp)
-{
-
-       mdp++;
-       if (mdp->md_size == 0)
-               mdp = NULL;
-       return (mdp);
-}
-
-static int
-buf_write(struct dumperinfo *di, char *ptr, size_t sz)
-{
-       size_t len;
-       int error;
-
-       while (sz) {
-               len = DEV_BSIZE - fragsz;
-               if (len > sz)
-                       len = sz;
-               bcopy(ptr, buffer + fragsz, len);
-               fragsz += len;
-               ptr += len;
-               sz -= len;
-               if (fragsz == DEV_BSIZE) {
-                       error = dump_write(di, buffer, 0, dumplo,
-                           DEV_BSIZE);
-                       if (error)
-                               return error;
-                       dumplo += DEV_BSIZE;
-                       fragsz = 0;
-               }
-       }
-
-       return (0);
-}
-
-static int
-buf_flush(struct dumperinfo *di)
-{
-       int error;
-
-       if (fragsz == 0)
-               return (0);
-
-       error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
-       dumplo += DEV_BSIZE;
-       fragsz = 0;
-       return (error);
-}
-
-extern vm_offset_t kernel_l1kva;
-extern char *pouet2;
-
-static int
-cb_dumpdata(struct md_pa *mdp, int seqnr, void *arg)
-{
-       struct dumperinfo *di = (struct dumperinfo*)arg;
-       vm_paddr_t a, pa;
-       void *va;
-       uint32_t pgs;
-       size_t counter, sz, chunk;
-       int i, c, error;
-
-       va = 0;
-       error = 0;      /* catch case in which chunk size is 0 */
-       counter = 0;
-       pgs = mdp->md_size / PAGE_SIZE;
-       pa = mdp->md_start;
-
-       printf("  chunk %d: %dMB (%d pages)", seqnr, pgs * PAGE_SIZE / (
-           1024*1024), pgs);
 
        /*
         * Make sure we write coherent data.  Note that in the SMP case this
@@ -186,70 +64,25 @@ cb_dumpdata(struct md_pa *mdp, int seqnr
 #ifdef __XSCALE__
        xscale_cache_clean_minidata();
 #endif
-       while (pgs) {
-               chunk = pgs;
-               if (chunk > MAXDUMPPGS)
-                       chunk = MAXDUMPPGS;
-               sz = chunk << PAGE_SHIFT;
-               counter += sz;
-               if (counter >> 24) {
-                       printf(" %d", pgs * PAGE_SIZE);
-                       counter &= (1<<24) - 1;
-               }
-               for (i = 0; i < chunk; i++) {
-                       a = pa + i * PAGE_SIZE;
-                       va = pmap_kenter_temporary(trunc_page(a), i);
-               }
-#ifdef SW_WATCHDOG
-               wdog_kern_pat(WD_LASTVAL);
-#endif
-               error = dump_write(di, va, 0, dumplo, sz);
-               if (error)
-                       break;
-               dumplo += sz;
-               pgs -= chunk;
-               pa += sz;
-
-               /* Check for user abort. */
-               c = cncheckc();
-               if (c == 0x03)
-                       return (ECANCELED);
-               if (c != -1)
-                       printf(" (CTRL-C to abort) ");
-       }
-       printf(" ... %s\n", (error) ? "fail" : "ok");
-       return (error);
 }
 
-static int
-cb_dumphdr(struct md_pa *mdp, int seqnr, void *arg)
+void
+dumpsys_map_chunk(vm_paddr_t pa, size_t chunk, void **va)
 {
-       struct dumperinfo *di = (struct dumperinfo*)arg;
-       Elf_Phdr phdr;
-       uint64_t size;
-       int error;
-
-       size = mdp->md_size;
-       bzero(&phdr, sizeof(phdr));
-       phdr.p_type = PT_LOAD;
-       phdr.p_flags = PF_R;                    /* XXX */
-       phdr.p_offset = fileofs;
-       phdr.p_vaddr = mdp->md_start;
-       phdr.p_paddr = mdp->md_start;
-       phdr.p_filesz = size;
-       phdr.p_memsz = size;
-       phdr.p_align = PAGE_SIZE;
+       vm_paddr_t a;
+       int i;
 
-       error = buf_write(di, (char*)&phdr, sizeof(phdr));
-       fileofs += phdr.p_filesz;
-       return (error);
+       for (i = 0; i < chunk; i++) {
+               a = pa + i * PAGE_SIZE;
+               *va = pmap_kenter_temporary(trunc_page(a), i);
+       }
 }
 
 /*
  * Add a header to be used by libkvm to get the va to pa delta
  */
-static int
-dump_os_header(struct dumperinfo *di)
+int
+dumpsys_write_aux_headers(struct dumperinfo *di)
 {
        Elf_Phdr phdr;
        int error;
@@ -264,144 +97,6 @@ dump_os_header(struct dumperinfo *di)
        phdr.p_memsz = 0;
        phdr.p_align = PAGE_SIZE;
 
-       error = buf_write(di, (char*)&phdr, sizeof(phdr));
-       return (error);
-}
-
-static int
-cb_size(struct md_pa *mdp, int seqnr, void *arg)
-{
-       uint32_t *sz = (uint32_t*)arg;
-
-       *sz += (uint32_t)mdp->md_size;
-       return (0);
-}
-
-static int
-foreach_chunk(callback_t cb, void *arg)
-{
-       struct md_pa *mdp;
-       int error, seqnr;
-
-       seqnr = 0;
-       mdp = md_pa_first();
-       while (mdp != NULL) {
-               error = (*cb)(mdp, seqnr++, arg);
-               if (error)
-                       return (-error);
-               mdp = md_pa_next(mdp);
-       }
-       return (seqnr);
-}
-
-int
-dumpsys(struct dumperinfo *di)
-{
-       Elf_Ehdr ehdr;
-       uint32_t dumpsize;
-       off_t hdrgap;
-       size_t hdrsz;
-       int error;
-
-       if (do_minidump)
-               return (minidumpsys(di));
-
-       bzero(&ehdr, sizeof(ehdr));
-       ehdr.e_ident[EI_MAG0] = ELFMAG0;
-       ehdr.e_ident[EI_MAG1] = ELFMAG1;
-       ehdr.e_ident[EI_MAG2] = ELFMAG2;
-       ehdr.e_ident[EI_MAG3] = ELFMAG3;
-       ehdr.e_ident[EI_CLASS] = ELF_CLASS;
-#if BYTE_ORDER == LITTLE_ENDIAN
-       ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
-#else
-       ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
-#endif
-       ehdr.e_ident[EI_VERSION] = EV_CURRENT;
-       ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;   /* XXX big picture? */
-       ehdr.e_type = ET_CORE;
-       ehdr.e_machine = EM_ARM;
-       ehdr.e_phoff = sizeof(ehdr);
-       ehdr.e_flags = 0;
-       ehdr.e_ehsize = sizeof(ehdr);
-       ehdr.e_phentsize = sizeof(Elf_Phdr);
-       ehdr.e_shentsize = sizeof(Elf_Shdr);
-
-       md_pa_init();
-
-       /* Calculate dump size. */
-       dumpsize = 0L;
-       ehdr.e_phnum = foreach_chunk(cb_size, &dumpsize) + 1;
-       hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
-       fileofs = MD_ALIGN(hdrsz);
-       dumpsize += fileofs;
-       hdrgap = fileofs - DEV_ALIGN(hdrsz);
-
-       /* Determine dump offset on device. */
-       if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
-               error = ENOSPC;
-               goto fail;
-       }
-       dumplo = di->mediaoffset + di->mediasize - dumpsize;
-       dumplo -= sizeof(kdh) * 2;
-
-       mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION, dumpsize, 
di->blocksize);
-
-       printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20,
-           ehdr.e_phnum - 1);
-
-       /* Dump leader */
-       error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
-       if (error)
-               goto fail;
-       dumplo += sizeof(kdh);
-
-       /* Dump ELF header */
-       error = buf_write(di, (char*)&ehdr, sizeof(ehdr));
-       if (error)
-               goto fail;
-
-       /* Dump program headers */
-       error = foreach_chunk(cb_dumphdr, di);
-       if (error >= 0)
-               error = dump_os_header(di);
-       if (error < 0)
-               goto fail;
-       buf_flush(di);
-
-       /*
-        * All headers are written using blocked I/O, so we know the
-        * current offset is (still) block aligned. Skip the alignement
-        * in the file to have the segment contents aligned at page
-        * boundary. We cannot use MD_ALIGN on dumplo, because we don't
-        * care and may very well be unaligned within the dump device.
-        */
-       dumplo += hdrgap;
-
-       /* Dump memory chunks (updates dumplo) */
-       error = foreach_chunk(cb_dumpdata, di);
-       if (error < 0)
-               goto fail;
-
-       /* Dump trailer */
-       error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
-       if (error)
-               goto fail;
-
-       /* Signal completion, signoff and exit stage left. */
-       dump_write(di, NULL, 0, 0, 0);
-       printf("\nDump complete\n");
-       return (0);
-
- fail:
-       if (error < 0)
-               error = -error;
-
-       if (error == ECANCELED)
-               printf("\nDump aborted\n");
-       else if (error == ENOSPC)
-               printf("\nDump failed. Partition too small.\n");
-       else
-               printf("\n** DUMP FAILED (ERROR %d) **\n", error);
+       error = dumpsys_buf_write(di, (char*)&phdr, sizeof(phdr));
        return (error);
 }

Added: head/sys/arm/include/dump.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/arm/include/dump.h Wed Jan  7 01:01:39 2015        (r276772)
@@ -0,0 +1,70 @@
+/*-
+ * Copyright (c) 2014 EMC Corp.
+ * Author: Conrad Meyer <conrad.me...@isilon.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _MACHINE_DUMP_H_
+#define        _MACHINE_DUMP_H_
+
+#define        KERNELDUMP_ARCH_VERSION KERNELDUMP_ARM_VERSION
+#define        EM_VALUE                EM_ARM
+/* XXX: I suppose 20 should be enough. */
+#define        DUMPSYS_MD_PA_NPAIRS    20
+#define        DUMPSYS_NUM_AUX_HDRS    1
+
+void dumpsys_wbinv_all(void);
+int dumpsys_write_aux_headers(struct dumperinfo *di);
+
+static inline void
+dumpsys_pa_init(void)
+{
+
+       dumpsys_gen_pa_init();
+}
+
+static inline struct dump_pa *
+dumpsys_pa_next(struct dump_pa *p)
+{
+
+       return (dumpsys_gen_pa_next(p));
+}
+
+static inline void
+dumpsys_unmap_chunk(vm_paddr_t pa, size_t s, void *va)
+{
+
+       dumpsys_gen_unmap_chunk(pa, s, va);
+}
+
+static inline int
+dumpsys(struct dumperinfo *di)
+{
+
+       return (dumpsys_generic(di));
+}
+
+#endif  /* !_MACHINE_DUMP_H_ */

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files Tue Jan  6 23:40:39 2015        (r276771)
+++ head/sys/conf/files Wed Jan  7 01:01:39 2015        (r276772)
@@ -2947,6 +2947,7 @@ kern/kern_cpuset.c                standard
 kern/kern_context.c            standard
 kern/kern_descrip.c            standard
 kern/kern_dtrace.c             optional kdtrace_hooks
+kern/kern_dump.c               standard
 kern/kern_environment.c                standard
 kern/kern_et.c                 standard
 kern/kern_event.c              standard

Added: head/sys/i386/include/dump.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/i386/include/dump.h        Wed Jan  7 01:01:39 2015        
(r276772)
@@ -0,0 +1,6 @@
+/*-
+ * This file is in the public domain.
+ */
+/* $FreeBSD$ */
+
+#include <x86/dump.h>

Added: head/sys/kern/kern_dump.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/kern/kern_dump.c   Wed Jan  7 01:01:39 2015        (r276772)
@@ -0,0 +1,393 @@
+/*-
+ * Copyright (c) 2002 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "opt_watchdog.h"
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/conf.h>
+#include <sys/cons.h>
+#include <sys/kernel.h>
+#include <sys/proc.h>
+#include <sys/kerneldump.h>
+#ifdef SW_WATCHDOG
+#include <sys/watchdog.h>
+#endif
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+#include <vm/pmap.h>
+#include <machine/dump.h>
+#include <machine/elf.h>
+#include <machine/md_var.h>
+#include <machine/pcb.h>
+
+CTASSERT(sizeof(struct kerneldumpheader) == 512);
+
+/*
+ * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
+ * is to protect us from metadata and to protect metadata from us.
+ */
+#define        SIZEOF_METADATA         (64*1024)
+
+#define        MD_ALIGN(x)     (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
+#define        DEV_ALIGN(x)    (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
+
+off_t dumplo;
+
+/* Handle buffered writes. */
+static char buffer[DEV_BSIZE];
+static size_t fragsz;
+
+struct dump_pa dump_map[DUMPSYS_MD_PA_NPAIRS];
+
+void
+dumpsys_gen_pa_init(void)
+{
+#if !defined(__sparc__) && !defined(__powerpc__)
+       int n, idx;
+
+       bzero(dump_map, sizeof(dump_map));
+       for (n = 0; n < sizeof(dump_map) / sizeof(dump_map[0]); n++) {
+               idx = n * 2;
+               if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0)
+                       break;
+               dump_map[n].pa_start = dump_avail[idx];
+               dump_map[n].pa_size = dump_avail[idx + 1] - dump_avail[idx];
+       }
+#endif
+}
+
+struct dump_pa *
+dumpsys_gen_pa_next(struct dump_pa *mdp)
+{
+
+       if (mdp == NULL)
+               return (&dump_map[0]);
+
+       mdp++;
+       if (mdp->pa_size == 0)
+               mdp = NULL;
+       return (mdp);
+}
+
+void
+dumpsys_gen_wbinv_all(void)
+{
+}
+
+void
+dumpsys_gen_unmap_chunk(vm_paddr_t pa __unused, size_t chunk __unused,
+    void *va __unused)
+{
+}
+
+int
+dumpsys_gen_write_aux_headers(struct dumperinfo *di)
+{
+
+       return (0);
+}
+
+int
+dumpsys_buf_write(struct dumperinfo *di, char *ptr, size_t sz)
+{
+       size_t len;
+       int error;
+
+       while (sz) {
+               len = DEV_BSIZE - fragsz;
+               if (len > sz)
+                       len = sz;
+               bcopy(ptr, buffer + fragsz, len);
+               fragsz += len;
+               ptr += len;
+               sz -= len;
+               if (fragsz == DEV_BSIZE) {
+                       error = dump_write(di, buffer, 0, dumplo,
+                           DEV_BSIZE);
+                       if (error)
+                               return (error);
+                       dumplo += DEV_BSIZE;
+                       fragsz = 0;
+               }
+       }
+       return (0);
+}
+
+int
+dumpsys_buf_flush(struct dumperinfo *di)
+{
+       int error;
+
+       if (fragsz == 0)
+               return (0);
+
+       error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
+       dumplo += DEV_BSIZE;
+       fragsz = 0;
+       return (error);
+}
+
+CTASSERT(PAGE_SHIFT < 20);
+#define PG2MB(pgs) ((pgs + (1 << (20 - PAGE_SHIFT)) - 1) >> (20 - PAGE_SHIFT))
+
+int
+dumpsys_cb_dumpdata(struct dump_pa *mdp, int seqnr, void *arg)
+{
+       struct dumperinfo *di = (struct dumperinfo*)arg;
+       vm_paddr_t pa;
+       void *va;
+       uint64_t pgs;
+       size_t counter, sz, chunk;
+       int c, error;
+       u_int maxdumppgs;
+
+       error = 0;      /* catch case in which chunk size is 0 */
+       counter = 0;    /* Update twiddle every 16MB */
+       va = 0;
+       pgs = mdp->pa_size / PAGE_SIZE;
+       pa = mdp->pa_start;
+       maxdumppgs = min(di->maxiosize / PAGE_SIZE, MAXDUMPPGS);
+       if (maxdumppgs == 0)    /* seatbelt */
+               maxdumppgs = 1;
+
+       printf("  chunk %d: %juMB (%ju pages)", seqnr, (uintmax_t)PG2MB(pgs),
+           (uintmax_t)pgs);
+
+       dumpsys_wbinv_all();
+       while (pgs) {
+               chunk = pgs;
+               if (chunk > maxdumppgs)
+                       chunk = maxdumppgs;
+               sz = chunk << PAGE_SHIFT;
+               counter += sz;
+               if (counter >> 24) {
+                       printf(" %ju", (uintmax_t)PG2MB(pgs));
+                       counter &= (1 << 24) - 1;
+               }
+
+               dumpsys_map_chunk(pa, chunk, &va);
+#ifdef SW_WATCHDOG
+               wdog_kern_pat(WD_LASTVAL);
+#endif
+
+               error = dump_write(di, va, 0, dumplo, sz);
+               dumpsys_unmap_chunk(pa, chunk, va);
+               if (error)
+                       break;
+               dumplo += sz;
+               pgs -= chunk;
+               pa += sz;
+
+               /* Check for user abort. */
+               c = cncheckc();
+               if (c == 0x03)
+                       return (ECANCELED);
+               if (c != -1)
+                       printf(" (CTRL-C to abort) ");
+       }
+       printf(" ... %s\n", (error) ? "fail" : "ok");
+       return (error);
+}
+
+int
+dumpsys_foreach_chunk(dumpsys_callback_t cb, void *arg)
+{
+       struct dump_pa *mdp;
+       int error, seqnr;
+
+       seqnr = 0;
+       mdp = dumpsys_pa_next(NULL);
+       while (mdp != NULL) {
+               error = (*cb)(mdp, seqnr++, arg);
+               if (error)
+                       return (-error);
+               mdp = dumpsys_pa_next(mdp);
+       }
+       return (seqnr);
+}
+
+static off_t fileofs;
+
+static int
+cb_dumphdr(struct dump_pa *mdp, int seqnr, void *arg)
+{
+       struct dumperinfo *di = (struct dumperinfo*)arg;
+       Elf_Phdr phdr;
+       uint64_t size;
+       int error;
+
+       size = mdp->pa_size;
+       bzero(&phdr, sizeof(phdr));
+       phdr.p_type = PT_LOAD;
+       phdr.p_flags = PF_R;                    /* XXX */
+       phdr.p_offset = fileofs;
+#ifdef __powerpc__
+       phdr.p_vaddr = (do_minidump? mdp->pa_start : ~0L);
+       phdr.p_paddr = (do_minidump? ~0L : mdp->pa_start);
+#else
+       phdr.p_vaddr = mdp->pa_start;
+       phdr.p_paddr = mdp->pa_start;
+#endif
+       phdr.p_filesz = size;
+       phdr.p_memsz = size;
+       phdr.p_align = PAGE_SIZE;
+
+       error = dumpsys_buf_write(di, (char*)&phdr, sizeof(phdr));
+       fileofs += phdr.p_filesz;
+       return (error);
+}
+
+static int
+cb_size(struct dump_pa *mdp, int seqnr, void *arg)
+{
+       uint64_t *sz;
+
+       sz = (uint64_t *)arg;
+       *sz += (uint64_t)mdp->pa_size;
+       return (0);
+}
+
+int
+dumpsys_generic(struct dumperinfo *di)
+{
+       static struct kerneldumpheader kdh;
+       Elf_Ehdr ehdr;
+       uint64_t dumpsize;
+       off_t hdrgap;
+       size_t hdrsz;
+       int error;
+
+#ifndef __powerpc__
+       if (do_minidump)
+               return (minidumpsys(di));
+#endif
+
+       bzero(&ehdr, sizeof(ehdr));
+       ehdr.e_ident[EI_MAG0] = ELFMAG0;
+       ehdr.e_ident[EI_MAG1] = ELFMAG1;
+       ehdr.e_ident[EI_MAG2] = ELFMAG2;
+       ehdr.e_ident[EI_MAG3] = ELFMAG3;
+       ehdr.e_ident[EI_CLASS] = ELF_CLASS;
+#if BYTE_ORDER == LITTLE_ENDIAN
+       ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
+#else
+       ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
+#endif
+       ehdr.e_ident[EI_VERSION] = EV_CURRENT;
+       ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;   /* XXX big picture? */
+       ehdr.e_type = ET_CORE;
+       ehdr.e_machine = EM_VALUE;
+       ehdr.e_phoff = sizeof(ehdr);
+       ehdr.e_flags = 0;
+       ehdr.e_ehsize = sizeof(ehdr);
+       ehdr.e_phentsize = sizeof(Elf_Phdr);
+       ehdr.e_shentsize = sizeof(Elf_Shdr);
+
+       dumpsys_pa_init();
+
+       /* Calculate dump size. */
+       dumpsize = 0L;
+       ehdr.e_phnum = dumpsys_foreach_chunk(cb_size, &dumpsize) +
+           DUMPSYS_NUM_AUX_HDRS;
+       hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
+       fileofs = MD_ALIGN(hdrsz);
+       dumpsize += fileofs;
+       hdrgap = fileofs - DEV_ALIGN(hdrsz);
+
+       /* Determine dump offset on device. */
+       if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
+               error = ENOSPC;
+               goto fail;
+       }
+       dumplo = di->mediaoffset + di->mediasize - dumpsize;
+       dumplo -= sizeof(kdh) * 2;
+
+       mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_ARCH_VERSION, dumpsize,
+           di->blocksize);
+
+       printf("Dumping %ju MB (%d chunks)\n", (uintmax_t)dumpsize >> 20,
+           ehdr.e_phnum - DUMPSYS_NUM_AUX_HDRS);
+
+       /* Dump leader */
+       error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
+       if (error)
+               goto fail;
+       dumplo += sizeof(kdh);
+
+       /* Dump ELF header */
+       error = dumpsys_buf_write(di, (char*)&ehdr, sizeof(ehdr));
+       if (error)
+               goto fail;
+
+       /* Dump program headers */
+       error = dumpsys_foreach_chunk(cb_dumphdr, di);
+       if (error < 0)
+               goto fail;
+       error = dumpsys_write_aux_headers(di);
+       if (error < 0)
+               goto fail;
+       dumpsys_buf_flush(di);
+
+       /*
+        * All headers are written using blocked I/O, so we know the
+        * current offset is (still) block aligned. Skip the alignement
+        * in the file to have the segment contents aligned at page
+        * boundary. We cannot use MD_ALIGN on dumplo, because we don't
+        * care and may very well be unaligned within the dump device.
+        */
+       dumplo += hdrgap;
+
+       /* Dump memory chunks (updates dumplo) */
+       error = dumpsys_foreach_chunk(dumpsys_cb_dumpdata, di);
+       if (error < 0)
+               goto fail;
+
+       /* Dump trailer */
+       error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
+       if (error)
+               goto fail;
+
+       /* Signal completion, signoff and exit stage left. */
+       dump_write(di, NULL, 0, 0, 0);
+       printf("\nDump complete\n");
+       return (0);
+
+ fail:
+       if (error < 0)
+               error = -error;
+
+       if (error == ECANCELED)
+               printf("\nDump aborted\n");
+       else if (error == ENOSPC)
+               printf("\nDump failed. Partition too small.\n");
+       else
+               printf("\n** DUMP FAILED (ERROR %d) **\n", error);
+       return (error);
+}

Modified: head/sys/kern/kern_shutdown.c
==============================================================================
--- head/sys/kern/kern_shutdown.c       Tue Jan  6 23:40:39 2015        
(r276771)
+++ head/sys/kern/kern_shutdown.c       Wed Jan  7 01:01:39 2015        
(r276772)
@@ -73,6 +73,7 @@ __FBSDID("$FreeBSD$");
 #include <ddb/ddb.h>
 
 #include <machine/cpu.h>
+#include <machine/dump.h>
 #include <machine/pcb.h>
 #include <machine/smp.h>
 

Added: head/sys/mips/include/dump.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/mips/include/dump.h        Wed Jan  7 01:01:39 2015        
(r276772)
@@ -0,0 +1,76 @@
+/*-
+ * Copyright (c) 2014 EMC Corp.
+ * Author: Conrad Meyer <conrad.me...@isilon.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _MACHINE_DUMP_H_
+#define        _MACHINE_DUMP_H_
+
+#define        KERNELDUMP_ARCH_VERSION KERNELDUMP_MIPS_VERSION
+#define        EM_VALUE                EM_MIPS
+/* XXX: I suppose 20 should be enough. */
+#define        DUMPSYS_MD_PA_NPAIRS    20
+#define        DUMPSYS_NUM_AUX_HDRS    0
+
+void dumpsys_wbinv_all(void);
+
+static inline void
+dumpsys_pa_init(void)
+{
+
+       dumpsys_gen_pa_init();
+}
+
+static inline struct dump_pa *
+dumpsys_pa_next(struct dump_pa *p)
+{
+
+       return (dumpsys_gen_pa_next(p));
+}
+
+static inline void
+dumpsys_unmap_chunk(vm_paddr_t pa, size_t s, void *va)
+{
+
+       dumpsys_gen_unmap_chunk(pa, s, va);
+}
+
+static inline int
+dumpsys_write_aux_headers(struct dumperinfo *di)
+{
+
+       return (dumpsys_gen_write_aux_headers(di));
+}
+
+static inline int
+dumpsys(struct dumperinfo *di)
+{
+
+       return (dumpsys_generic(di));
+}
+
+#endif  /* !_MACHINE_DUMP_H_ */

Modified: head/sys/mips/include/md_var.h
==============================================================================
--- head/sys/mips/include/md_var.h      Tue Jan  6 23:40:39 2015        
(r276771)
+++ head/sys/mips/include/md_var.h      Wed Jan  7 01:01:39 2015        
(r276772)
@@ -80,4 +80,5 @@ struct        dumperinfo;
 void   dump_add_page(vm_paddr_t);
 void   dump_drop_page(vm_paddr_t);
 int    minidumpsys(struct dumperinfo *);
+
 #endif /* !_MACHINE_MD_VAR_H_ */

Modified: head/sys/mips/mips/dump_machdep.c
==============================================================================
--- head/sys/mips/mips/dump_machdep.c   Tue Jan  6 23:40:39 2015        
(r276771)
+++ head/sys/mips/mips/dump_machdep.c   Wed Jan  7 01:01:39 2015        
(r276772)
@@ -27,342 +27,30 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include "opt_watchdog.h"
-
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/conf.h>
-#include <sys/cons.h>
-#include <sys/sysctl.h>
-#include <sys/kernel.h>
-#include <sys/proc.h>
 #include <sys/kerneldump.h>
-#ifdef SW_WATCHDOG
-#include <sys/watchdog.h>
-#endif
-#include <vm/vm.h>
-#include <vm/pmap.h>
-#include <machine/elf.h>
-#include <machine/md_var.h>
-#include <machine/pcb.h>

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to