1bdb9ab0dSMark Johnston /*- 2bdb9ab0dSMark Johnston * Copyright (c) 2002 Marcel Moolenaar 3bdb9ab0dSMark Johnston * All rights reserved. 4bdb9ab0dSMark Johnston * 5bdb9ab0dSMark Johnston * Redistribution and use in source and binary forms, with or without 6bdb9ab0dSMark Johnston * modification, are permitted provided that the following conditions 7bdb9ab0dSMark Johnston * are met: 8bdb9ab0dSMark Johnston * 9bdb9ab0dSMark Johnston * 1. Redistributions of source code must retain the above copyright 10bdb9ab0dSMark Johnston * notice, this list of conditions and the following disclaimer. 11bdb9ab0dSMark Johnston * 2. Redistributions in binary form must reproduce the above copyright 12bdb9ab0dSMark Johnston * notice, this list of conditions and the following disclaimer in the 13bdb9ab0dSMark Johnston * documentation and/or other materials provided with the distribution. 14bdb9ab0dSMark Johnston * 15bdb9ab0dSMark Johnston * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16bdb9ab0dSMark Johnston * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17bdb9ab0dSMark Johnston * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18bdb9ab0dSMark Johnston * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19bdb9ab0dSMark Johnston * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20bdb9ab0dSMark Johnston * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21bdb9ab0dSMark Johnston * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22bdb9ab0dSMark Johnston * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23bdb9ab0dSMark Johnston * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24bdb9ab0dSMark Johnston * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25bdb9ab0dSMark Johnston */ 26bdb9ab0dSMark Johnston 27bdb9ab0dSMark Johnston #include <sys/cdefs.h> 28bdb9ab0dSMark Johnston __FBSDID("$FreeBSD$"); 29bdb9ab0dSMark Johnston 30bdb9ab0dSMark Johnston #include <sys/param.h> 31bdb9ab0dSMark Johnston #include <sys/systm.h> 32bdb9ab0dSMark Johnston #include <sys/conf.h> 33bdb9ab0dSMark Johnston #include <sys/cons.h> 34bdb9ab0dSMark Johnston #include <sys/kernel.h> 35bdb9ab0dSMark Johnston #include <sys/proc.h> 36bdb9ab0dSMark Johnston #include <sys/kerneldump.h> 37bdb9ab0dSMark Johnston #include <sys/watchdog.h> 38bdb9ab0dSMark Johnston #include <vm/vm.h> 39bdb9ab0dSMark Johnston #include <vm/vm_param.h> 4021943937SJeff Roberson #include <vm/vm_page.h> 4121943937SJeff Roberson #include <vm/vm_phys.h> 426f3b523cSKonstantin Belousov #include <vm/vm_dumpset.h> 43bdb9ab0dSMark Johnston #include <vm/pmap.h> 44bdb9ab0dSMark Johnston #include <machine/dump.h> 45bdb9ab0dSMark Johnston #include <machine/elf.h> 46bdb9ab0dSMark Johnston #include <machine/md_var.h> 47bdb9ab0dSMark Johnston #include <machine/pcb.h> 48bdb9ab0dSMark Johnston 49bdb9ab0dSMark Johnston CTASSERT(sizeof(struct kerneldumpheader) == 512); 50bdb9ab0dSMark Johnston 515dc5dab6SConrad Meyer #define MD_ALIGN(x) roundup2((off_t)(x), PAGE_SIZE) 52bdb9ab0dSMark Johnston 53bdb9ab0dSMark Johnston /* Handle buffered writes. */ 54bdb9ab0dSMark Johnston static size_t fragsz; 55bdb9ab0dSMark Johnston 56bdb9ab0dSMark Johnston struct dump_pa dump_map[DUMPSYS_MD_PA_NPAIRS]; 57bdb9ab0dSMark Johnston 588f11c997SYoshihiro Takahashi #if !defined(__powerpc__) 59bdb9ab0dSMark Johnston void 60bdb9ab0dSMark Johnston dumpsys_gen_pa_init(void) 61bdb9ab0dSMark Johnston { 62bdb9ab0dSMark Johnston int n, idx; 63bdb9ab0dSMark Johnston 64bdb9ab0dSMark Johnston bzero(dump_map, sizeof(dump_map)); 6502abd400SPedro F. Giffuni for (n = 0; n < nitems(dump_map); n++) { 66bdb9ab0dSMark Johnston idx = n * 2; 67bdb9ab0dSMark Johnston if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0) 68bdb9ab0dSMark Johnston break; 69bdb9ab0dSMark Johnston dump_map[n].pa_start = dump_avail[idx]; 70bdb9ab0dSMark Johnston dump_map[n].pa_size = dump_avail[idx + 1] - dump_avail[idx]; 71bdb9ab0dSMark Johnston } 72bdb9ab0dSMark Johnston } 737888a51fSMarius Strobl #endif 74bdb9ab0dSMark Johnston 75bdb9ab0dSMark Johnston struct dump_pa * 76bdb9ab0dSMark Johnston dumpsys_gen_pa_next(struct dump_pa *mdp) 77bdb9ab0dSMark Johnston { 78bdb9ab0dSMark Johnston 79bdb9ab0dSMark Johnston if (mdp == NULL) 80bdb9ab0dSMark Johnston return (&dump_map[0]); 81bdb9ab0dSMark Johnston 82bdb9ab0dSMark Johnston mdp++; 83bdb9ab0dSMark Johnston if (mdp->pa_size == 0) 84bdb9ab0dSMark Johnston mdp = NULL; 85bdb9ab0dSMark Johnston return (mdp); 86bdb9ab0dSMark Johnston } 87bdb9ab0dSMark Johnston 88bdb9ab0dSMark Johnston void 89bdb9ab0dSMark Johnston dumpsys_gen_wbinv_all(void) 90bdb9ab0dSMark Johnston { 917888a51fSMarius Strobl 92bdb9ab0dSMark Johnston } 93bdb9ab0dSMark Johnston 94bdb9ab0dSMark Johnston void 95bdb9ab0dSMark Johnston dumpsys_gen_unmap_chunk(vm_paddr_t pa __unused, size_t chunk __unused, 96bdb9ab0dSMark Johnston void *va __unused) 97bdb9ab0dSMark Johnston { 987888a51fSMarius Strobl 99bdb9ab0dSMark Johnston } 100bdb9ab0dSMark Johnston 101bdb9ab0dSMark Johnston int 102bdb9ab0dSMark Johnston dumpsys_gen_write_aux_headers(struct dumperinfo *di) 103bdb9ab0dSMark Johnston { 104bdb9ab0dSMark Johnston 105bdb9ab0dSMark Johnston return (0); 106bdb9ab0dSMark Johnston } 107bdb9ab0dSMark Johnston 108bdb9ab0dSMark Johnston int 109480f31c2SKonrad Witaszczyk dumpsys_buf_seek(struct dumperinfo *di, size_t sz) 110480f31c2SKonrad Witaszczyk { 111480f31c2SKonrad Witaszczyk static uint8_t buf[DEV_BSIZE]; 112480f31c2SKonrad Witaszczyk size_t nbytes; 113480f31c2SKonrad Witaszczyk int error; 114480f31c2SKonrad Witaszczyk 115480f31c2SKonrad Witaszczyk bzero(buf, sizeof(buf)); 116480f31c2SKonrad Witaszczyk 117480f31c2SKonrad Witaszczyk while (sz > 0) { 118480f31c2SKonrad Witaszczyk nbytes = MIN(sz, sizeof(buf)); 119480f31c2SKonrad Witaszczyk 12046fcd1afSMark Johnston error = dump_append(di, buf, 0, nbytes); 121480f31c2SKonrad Witaszczyk if (error) 122480f31c2SKonrad Witaszczyk return (error); 123480f31c2SKonrad Witaszczyk sz -= nbytes; 124480f31c2SKonrad Witaszczyk } 125480f31c2SKonrad Witaszczyk 126480f31c2SKonrad Witaszczyk return (0); 127480f31c2SKonrad Witaszczyk } 128480f31c2SKonrad Witaszczyk 129480f31c2SKonrad Witaszczyk int 130bdb9ab0dSMark Johnston dumpsys_buf_write(struct dumperinfo *di, char *ptr, size_t sz) 131bdb9ab0dSMark Johnston { 132bdb9ab0dSMark Johnston size_t len; 133bdb9ab0dSMark Johnston int error; 134bdb9ab0dSMark Johnston 135bdb9ab0dSMark Johnston while (sz) { 1365dc5dab6SConrad Meyer len = di->blocksize - fragsz; 137bdb9ab0dSMark Johnston if (len > sz) 138bdb9ab0dSMark Johnston len = sz; 1395dc5dab6SConrad Meyer memcpy((char *)di->blockbuf + fragsz, ptr, len); 140bdb9ab0dSMark Johnston fragsz += len; 141bdb9ab0dSMark Johnston ptr += len; 142bdb9ab0dSMark Johnston sz -= len; 1435dc5dab6SConrad Meyer if (fragsz == di->blocksize) { 14446fcd1afSMark Johnston error = dump_append(di, di->blockbuf, 0, di->blocksize); 145bdb9ab0dSMark Johnston if (error) 146bdb9ab0dSMark Johnston return (error); 147bdb9ab0dSMark Johnston fragsz = 0; 148bdb9ab0dSMark Johnston } 149bdb9ab0dSMark Johnston } 150bdb9ab0dSMark Johnston return (0); 151bdb9ab0dSMark Johnston } 152bdb9ab0dSMark Johnston 153bdb9ab0dSMark Johnston int 154bdb9ab0dSMark Johnston dumpsys_buf_flush(struct dumperinfo *di) 155bdb9ab0dSMark Johnston { 156bdb9ab0dSMark Johnston int error; 157bdb9ab0dSMark Johnston 158bdb9ab0dSMark Johnston if (fragsz == 0) 159bdb9ab0dSMark Johnston return (0); 160bdb9ab0dSMark Johnston 16146fcd1afSMark Johnston error = dump_append(di, di->blockbuf, 0, di->blocksize); 162bdb9ab0dSMark Johnston fragsz = 0; 163bdb9ab0dSMark Johnston return (error); 164bdb9ab0dSMark Johnston } 165bdb9ab0dSMark Johnston 166bdb9ab0dSMark Johnston CTASSERT(PAGE_SHIFT < 20); 167bdb9ab0dSMark Johnston #define PG2MB(pgs) ((pgs + (1 << (20 - PAGE_SHIFT)) - 1) >> (20 - PAGE_SHIFT)) 168bdb9ab0dSMark Johnston 169bdb9ab0dSMark Johnston int 170bdb9ab0dSMark Johnston dumpsys_cb_dumpdata(struct dump_pa *mdp, int seqnr, void *arg) 171bdb9ab0dSMark Johnston { 172bdb9ab0dSMark Johnston struct dumperinfo *di = (struct dumperinfo*)arg; 173bdb9ab0dSMark Johnston vm_paddr_t pa; 174bdb9ab0dSMark Johnston void *va; 175bdb9ab0dSMark Johnston uint64_t pgs; 176bdb9ab0dSMark Johnston size_t counter, sz, chunk; 177bdb9ab0dSMark Johnston int c, error; 178bdb9ab0dSMark Johnston u_int maxdumppgs; 179bdb9ab0dSMark Johnston 180bdb9ab0dSMark Johnston error = 0; /* catch case in which chunk size is 0 */ 181bdb9ab0dSMark Johnston counter = 0; /* Update twiddle every 16MB */ 182b85f65afSPedro F. Giffuni va = NULL; 183bdb9ab0dSMark Johnston pgs = mdp->pa_size / PAGE_SIZE; 184bdb9ab0dSMark Johnston pa = mdp->pa_start; 185bdb9ab0dSMark Johnston maxdumppgs = min(di->maxiosize / PAGE_SIZE, MAXDUMPPGS); 186bdb9ab0dSMark Johnston if (maxdumppgs == 0) /* seatbelt */ 187bdb9ab0dSMark Johnston maxdumppgs = 1; 188bdb9ab0dSMark Johnston 189bdb9ab0dSMark Johnston printf(" chunk %d: %juMB (%ju pages)", seqnr, (uintmax_t)PG2MB(pgs), 190bdb9ab0dSMark Johnston (uintmax_t)pgs); 191bdb9ab0dSMark Johnston 192bdb9ab0dSMark Johnston dumpsys_wbinv_all(); 193bdb9ab0dSMark Johnston while (pgs) { 194bdb9ab0dSMark Johnston chunk = pgs; 195bdb9ab0dSMark Johnston if (chunk > maxdumppgs) 196bdb9ab0dSMark Johnston chunk = maxdumppgs; 197bdb9ab0dSMark Johnston sz = chunk << PAGE_SHIFT; 198bdb9ab0dSMark Johnston counter += sz; 199bdb9ab0dSMark Johnston if (counter >> 24) { 200bdb9ab0dSMark Johnston printf(" %ju", (uintmax_t)PG2MB(pgs)); 201bdb9ab0dSMark Johnston counter &= (1 << 24) - 1; 202bdb9ab0dSMark Johnston } 203bdb9ab0dSMark Johnston 204bdb9ab0dSMark Johnston dumpsys_map_chunk(pa, chunk, &va); 205bdb9ab0dSMark Johnston wdog_kern_pat(WD_LASTVAL); 206bdb9ab0dSMark Johnston 20746fcd1afSMark Johnston error = dump_append(di, va, 0, sz); 208bdb9ab0dSMark Johnston dumpsys_unmap_chunk(pa, chunk, va); 209bdb9ab0dSMark Johnston if (error) 210bdb9ab0dSMark Johnston break; 211bdb9ab0dSMark Johnston pgs -= chunk; 212bdb9ab0dSMark Johnston pa += sz; 213bdb9ab0dSMark Johnston 214bdb9ab0dSMark Johnston /* Check for user abort. */ 215bdb9ab0dSMark Johnston c = cncheckc(); 216bdb9ab0dSMark Johnston if (c == 0x03) 217bdb9ab0dSMark Johnston return (ECANCELED); 218bdb9ab0dSMark Johnston if (c != -1) 219bdb9ab0dSMark Johnston printf(" (CTRL-C to abort) "); 220bdb9ab0dSMark Johnston } 221bdb9ab0dSMark Johnston printf(" ... %s\n", (error) ? "fail" : "ok"); 222bdb9ab0dSMark Johnston return (error); 223bdb9ab0dSMark Johnston } 224bdb9ab0dSMark Johnston 225bdb9ab0dSMark Johnston int 226bdb9ab0dSMark Johnston dumpsys_foreach_chunk(dumpsys_callback_t cb, void *arg) 227bdb9ab0dSMark Johnston { 228bdb9ab0dSMark Johnston struct dump_pa *mdp; 229bdb9ab0dSMark Johnston int error, seqnr; 230bdb9ab0dSMark Johnston 231bdb9ab0dSMark Johnston seqnr = 0; 232bdb9ab0dSMark Johnston mdp = dumpsys_pa_next(NULL); 233bdb9ab0dSMark Johnston while (mdp != NULL) { 234bdb9ab0dSMark Johnston error = (*cb)(mdp, seqnr++, arg); 235bdb9ab0dSMark Johnston if (error) 236bdb9ab0dSMark Johnston return (-error); 237bdb9ab0dSMark Johnston mdp = dumpsys_pa_next(mdp); 238bdb9ab0dSMark Johnston } 239bdb9ab0dSMark Johnston return (seqnr); 240bdb9ab0dSMark Johnston } 241bdb9ab0dSMark Johnston 242bdb9ab0dSMark Johnston static off_t fileofs; 243bdb9ab0dSMark Johnston 244bdb9ab0dSMark Johnston static int 245bdb9ab0dSMark Johnston cb_dumphdr(struct dump_pa *mdp, int seqnr, void *arg) 246bdb9ab0dSMark Johnston { 247bdb9ab0dSMark Johnston struct dumperinfo *di = (struct dumperinfo*)arg; 248bdb9ab0dSMark Johnston Elf_Phdr phdr; 249bdb9ab0dSMark Johnston uint64_t size; 250bdb9ab0dSMark Johnston int error; 251bdb9ab0dSMark Johnston 252bdb9ab0dSMark Johnston size = mdp->pa_size; 253bdb9ab0dSMark Johnston bzero(&phdr, sizeof(phdr)); 254bdb9ab0dSMark Johnston phdr.p_type = PT_LOAD; 255bdb9ab0dSMark Johnston phdr.p_flags = PF_R; /* XXX */ 256bdb9ab0dSMark Johnston phdr.p_offset = fileofs; 257bdb9ab0dSMark Johnston #ifdef __powerpc__ 258bdb9ab0dSMark Johnston phdr.p_vaddr = (do_minidump? mdp->pa_start : ~0L); 259bdb9ab0dSMark Johnston phdr.p_paddr = (do_minidump? ~0L : mdp->pa_start); 260bdb9ab0dSMark Johnston #else 261bdb9ab0dSMark Johnston phdr.p_vaddr = mdp->pa_start; 262bdb9ab0dSMark Johnston phdr.p_paddr = mdp->pa_start; 263bdb9ab0dSMark Johnston #endif 264bdb9ab0dSMark Johnston phdr.p_filesz = size; 265bdb9ab0dSMark Johnston phdr.p_memsz = size; 266bdb9ab0dSMark Johnston phdr.p_align = PAGE_SIZE; 267bdb9ab0dSMark Johnston 268bdb9ab0dSMark Johnston error = dumpsys_buf_write(di, (char*)&phdr, sizeof(phdr)); 269bdb9ab0dSMark Johnston fileofs += phdr.p_filesz; 270bdb9ab0dSMark Johnston return (error); 271bdb9ab0dSMark Johnston } 272bdb9ab0dSMark Johnston 273bdb9ab0dSMark Johnston static int 274bdb9ab0dSMark Johnston cb_size(struct dump_pa *mdp, int seqnr, void *arg) 275bdb9ab0dSMark Johnston { 276bdb9ab0dSMark Johnston uint64_t *sz; 277bdb9ab0dSMark Johnston 278bdb9ab0dSMark Johnston sz = (uint64_t *)arg; 279bdb9ab0dSMark Johnston *sz += (uint64_t)mdp->pa_size; 280bdb9ab0dSMark Johnston return (0); 281bdb9ab0dSMark Johnston } 282bdb9ab0dSMark Johnston 283bdb9ab0dSMark Johnston int 284bdb9ab0dSMark Johnston dumpsys_generic(struct dumperinfo *di) 285bdb9ab0dSMark Johnston { 286bdb9ab0dSMark Johnston static struct kerneldumpheader kdh; 287bdb9ab0dSMark Johnston Elf_Ehdr ehdr; 288bdb9ab0dSMark Johnston uint64_t dumpsize; 289bdb9ab0dSMark Johnston off_t hdrgap; 290480f31c2SKonrad Witaszczyk size_t hdrsz; 291bdb9ab0dSMark Johnston int error; 292bdb9ab0dSMark Johnston 293b957b185SMark Johnston #if MINIDUMP_PAGE_TRACKING == 1 294bdb9ab0dSMark Johnston if (do_minidump) 295bdb9ab0dSMark Johnston return (minidumpsys(di)); 296bdb9ab0dSMark Johnston #endif 297bdb9ab0dSMark Johnston 298bdb9ab0dSMark Johnston bzero(&ehdr, sizeof(ehdr)); 299bdb9ab0dSMark Johnston ehdr.e_ident[EI_MAG0] = ELFMAG0; 300bdb9ab0dSMark Johnston ehdr.e_ident[EI_MAG1] = ELFMAG1; 301bdb9ab0dSMark Johnston ehdr.e_ident[EI_MAG2] = ELFMAG2; 302bdb9ab0dSMark Johnston ehdr.e_ident[EI_MAG3] = ELFMAG3; 303bdb9ab0dSMark Johnston ehdr.e_ident[EI_CLASS] = ELF_CLASS; 304bdb9ab0dSMark Johnston #if BYTE_ORDER == LITTLE_ENDIAN 305bdb9ab0dSMark Johnston ehdr.e_ident[EI_DATA] = ELFDATA2LSB; 306bdb9ab0dSMark Johnston #else 307bdb9ab0dSMark Johnston ehdr.e_ident[EI_DATA] = ELFDATA2MSB; 308bdb9ab0dSMark Johnston #endif 309bdb9ab0dSMark Johnston ehdr.e_ident[EI_VERSION] = EV_CURRENT; 310bdb9ab0dSMark Johnston ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE; /* XXX big picture? */ 311bdb9ab0dSMark Johnston ehdr.e_type = ET_CORE; 312bdb9ab0dSMark Johnston ehdr.e_machine = EM_VALUE; 313bdb9ab0dSMark Johnston ehdr.e_phoff = sizeof(ehdr); 314bdb9ab0dSMark Johnston ehdr.e_flags = 0; 315bdb9ab0dSMark Johnston ehdr.e_ehsize = sizeof(ehdr); 316bdb9ab0dSMark Johnston ehdr.e_phentsize = sizeof(Elf_Phdr); 317bdb9ab0dSMark Johnston ehdr.e_shentsize = sizeof(Elf_Shdr); 318bdb9ab0dSMark Johnston 319bdb9ab0dSMark Johnston dumpsys_pa_init(); 320bdb9ab0dSMark Johnston 321bdb9ab0dSMark Johnston /* Calculate dump size. */ 322bdb9ab0dSMark Johnston dumpsize = 0L; 323bdb9ab0dSMark Johnston ehdr.e_phnum = dumpsys_foreach_chunk(cb_size, &dumpsize) + 324bdb9ab0dSMark Johnston DUMPSYS_NUM_AUX_HDRS; 325bdb9ab0dSMark Johnston hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize; 326bdb9ab0dSMark Johnston fileofs = MD_ALIGN(hdrsz); 327bdb9ab0dSMark Johnston dumpsize += fileofs; 3285dc5dab6SConrad Meyer hdrgap = fileofs - roundup2((off_t)hdrsz, di->blocksize); 329bdb9ab0dSMark Johnston 33001938d36SMark Johnston dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARCH_VERSION, 33101938d36SMark Johnston dumpsize); 332bdb9ab0dSMark Johnston 33346fcd1afSMark Johnston error = dump_start(di, &kdh); 33450ef60daSMark Johnston if (error != 0) 335bdb9ab0dSMark Johnston goto fail; 336bdb9ab0dSMark Johnston 33720f85b1dSMark Johnston printf("Dumping %ju MB (%d chunks)\n", (uintmax_t)dumpsize >> 20, 33820f85b1dSMark Johnston ehdr.e_phnum - DUMPSYS_NUM_AUX_HDRS); 33920f85b1dSMark Johnston 340bdb9ab0dSMark Johnston /* Dump ELF header */ 341bdb9ab0dSMark Johnston error = dumpsys_buf_write(di, (char*)&ehdr, sizeof(ehdr)); 342bdb9ab0dSMark Johnston if (error) 343bdb9ab0dSMark Johnston goto fail; 344bdb9ab0dSMark Johnston 345bdb9ab0dSMark Johnston /* Dump program headers */ 346bdb9ab0dSMark Johnston error = dumpsys_foreach_chunk(cb_dumphdr, di); 347bdb9ab0dSMark Johnston if (error < 0) 348bdb9ab0dSMark Johnston goto fail; 349bdb9ab0dSMark Johnston error = dumpsys_write_aux_headers(di); 350bdb9ab0dSMark Johnston if (error < 0) 351bdb9ab0dSMark Johnston goto fail; 352bdb9ab0dSMark Johnston dumpsys_buf_flush(di); 353bdb9ab0dSMark Johnston 354bdb9ab0dSMark Johnston /* 355bdb9ab0dSMark Johnston * All headers are written using blocked I/O, so we know the 356bdb9ab0dSMark Johnston * current offset is (still) block aligned. Skip the alignement 357bdb9ab0dSMark Johnston * in the file to have the segment contents aligned at page 35846fcd1afSMark Johnston * boundary. 359bdb9ab0dSMark Johnston */ 360480f31c2SKonrad Witaszczyk error = dumpsys_buf_seek(di, (size_t)hdrgap); 361480f31c2SKonrad Witaszczyk if (error) 362480f31c2SKonrad Witaszczyk goto fail; 363bdb9ab0dSMark Johnston 36446fcd1afSMark Johnston /* Dump memory chunks. */ 365bdb9ab0dSMark Johnston error = dumpsys_foreach_chunk(dumpsys_cb_dumpdata, di); 366bdb9ab0dSMark Johnston if (error < 0) 367bdb9ab0dSMark Johnston goto fail; 368bdb9ab0dSMark Johnston 36946fcd1afSMark Johnston error = dump_finish(di, &kdh); 37050ef60daSMark Johnston if (error != 0) 371bdb9ab0dSMark Johnston goto fail; 372bdb9ab0dSMark Johnston 373bdb9ab0dSMark Johnston printf("\nDump complete\n"); 374bdb9ab0dSMark Johnston return (0); 375bdb9ab0dSMark Johnston 376bdb9ab0dSMark Johnston fail: 377bdb9ab0dSMark Johnston if (error < 0) 378bdb9ab0dSMark Johnston error = -error; 379bdb9ab0dSMark Johnston 380bdb9ab0dSMark Johnston if (error == ECANCELED) 381bdb9ab0dSMark Johnston printf("\nDump aborted\n"); 38250ef60daSMark Johnston else if (error == E2BIG || error == ENOSPC) 383bdb9ab0dSMark Johnston printf("\nDump failed. Partition too small.\n"); 384bdb9ab0dSMark Johnston else 385bdb9ab0dSMark Johnston printf("\n** DUMP FAILED (ERROR %d) **\n", error); 386bdb9ab0dSMark Johnston return (error); 387bdb9ab0dSMark Johnston } 388*ab4ed843SMitchell Horne 389*ab4ed843SMitchell Horne /* Minidump progress bar */ 390*ab4ed843SMitchell Horne static struct { 391*ab4ed843SMitchell Horne const int min_per; 392*ab4ed843SMitchell Horne const int max_per; 393*ab4ed843SMitchell Horne bool visited; 394*ab4ed843SMitchell Horne } progress_track[10] = { 395*ab4ed843SMitchell Horne { 0, 10, false}, 396*ab4ed843SMitchell Horne { 10, 20, false}, 397*ab4ed843SMitchell Horne { 20, 30, false}, 398*ab4ed843SMitchell Horne { 30, 40, false}, 399*ab4ed843SMitchell Horne { 40, 50, false}, 400*ab4ed843SMitchell Horne { 50, 60, false}, 401*ab4ed843SMitchell Horne { 60, 70, false}, 402*ab4ed843SMitchell Horne { 70, 80, false}, 403*ab4ed843SMitchell Horne { 80, 90, false}, 404*ab4ed843SMitchell Horne { 90, 100, false} 405*ab4ed843SMitchell Horne }; 406*ab4ed843SMitchell Horne 407*ab4ed843SMitchell Horne static uint64_t dumpsys_pb_size; 408*ab4ed843SMitchell Horne static uint64_t dumpsys_pb_remaining; 409*ab4ed843SMitchell Horne static uint64_t dumpsys_pb_check; 410*ab4ed843SMitchell Horne 411*ab4ed843SMitchell Horne /* Reset the progress bar for a dump of dumpsize. */ 412*ab4ed843SMitchell Horne void 413*ab4ed843SMitchell Horne dumpsys_pb_init(uint64_t dumpsize) 414*ab4ed843SMitchell Horne { 415*ab4ed843SMitchell Horne int i; 416*ab4ed843SMitchell Horne 417*ab4ed843SMitchell Horne dumpsys_pb_size = dumpsys_pb_remaining = dumpsize; 418*ab4ed843SMitchell Horne dumpsys_pb_check = 0; 419*ab4ed843SMitchell Horne 420*ab4ed843SMitchell Horne for (i = 0; i < nitems(progress_track); i++) 421*ab4ed843SMitchell Horne progress_track[i].visited = false; 422*ab4ed843SMitchell Horne } 423*ab4ed843SMitchell Horne 424*ab4ed843SMitchell Horne /* 425*ab4ed843SMitchell Horne * Update the progress according to the delta bytes that were written out. 426*ab4ed843SMitchell Horne * Check and print the progress percentage. 427*ab4ed843SMitchell Horne */ 428*ab4ed843SMitchell Horne void 429*ab4ed843SMitchell Horne dumpsys_pb_progress(size_t delta) 430*ab4ed843SMitchell Horne { 431*ab4ed843SMitchell Horne int sofar, i; 432*ab4ed843SMitchell Horne 433*ab4ed843SMitchell Horne dumpsys_pb_remaining -= delta; 434*ab4ed843SMitchell Horne dumpsys_pb_check += delta; 435*ab4ed843SMitchell Horne 436*ab4ed843SMitchell Horne /* 437*ab4ed843SMitchell Horne * To save time while dumping, only loop through progress_track 438*ab4ed843SMitchell Horne * occasionally. 439*ab4ed843SMitchell Horne */ 440*ab4ed843SMitchell Horne if ((dumpsys_pb_check >> DUMPSYS_PB_CHECK_BITS) == 0) 441*ab4ed843SMitchell Horne return; 442*ab4ed843SMitchell Horne else 443*ab4ed843SMitchell Horne dumpsys_pb_check &= (1 << DUMPSYS_PB_CHECK_BITS) - 1; 444*ab4ed843SMitchell Horne 445*ab4ed843SMitchell Horne sofar = 100 - ((dumpsys_pb_remaining * 100) / dumpsys_pb_size); 446*ab4ed843SMitchell Horne for (i = 0; i < nitems(progress_track); i++) { 447*ab4ed843SMitchell Horne if (sofar < progress_track[i].min_per || 448*ab4ed843SMitchell Horne sofar > progress_track[i].max_per) 449*ab4ed843SMitchell Horne continue; 450*ab4ed843SMitchell Horne if (!progress_track[i].visited) { 451*ab4ed843SMitchell Horne progress_track[i].visited = true; 452*ab4ed843SMitchell Horne printf("..%d%%", sofar); 453*ab4ed843SMitchell Horne } 454*ab4ed843SMitchell Horne break; 455*ab4ed843SMitchell Horne } 456*ab4ed843SMitchell Horne } 457