17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ae115bc7Smrj * Common Development and Distribution License (the "License"). 6ae115bc7Smrj * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21342440ecSPrasad Singamsetty 227c478bd9Sstevel@tonic-gate /* 2386f21945SDave Plauger * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 24*2fb0949cSJoshua M. Clulow * Copyright 2016 Joyent, Inc. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/types.h> 287c478bd9Sstevel@tonic-gate #include <sys/param.h> 297c478bd9Sstevel@tonic-gate #include <sys/systm.h> 307c478bd9Sstevel@tonic-gate #include <sys/vm.h> 317c478bd9Sstevel@tonic-gate #include <sys/proc.h> 327c478bd9Sstevel@tonic-gate #include <sys/file.h> 337c478bd9Sstevel@tonic-gate #include <sys/conf.h> 347c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 357c478bd9Sstevel@tonic-gate #include <sys/mem.h> 367c478bd9Sstevel@tonic-gate #include <sys/mman.h> 377c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 387c478bd9Sstevel@tonic-gate #include <sys/errno.h> 397c478bd9Sstevel@tonic-gate #include <sys/memlist.h> 407c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h> 417c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h> 427c478bd9Sstevel@tonic-gate #include <sys/ksyms.h> 437c478bd9Sstevel@tonic-gate #include <sys/compress.h> 447c478bd9Sstevel@tonic-gate #include <sys/stream.h> 457c478bd9Sstevel@tonic-gate #include <sys/strsun.h> 467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 477c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 487c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 497c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 507c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 517c478bd9Sstevel@tonic-gate #include <sys/vmem.h> 527c478bd9Sstevel@tonic-gate #include <sys/log.h> 537c478bd9Sstevel@tonic-gate #include <sys/var.h> 547c478bd9Sstevel@tonic-gate #include <sys/debug.h> 557c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 567c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h> 577c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h> 587c478bd9Sstevel@tonic-gate #include <sys/ontrap.h> 597c478bd9Sstevel@tonic-gate #include <sys/panic.h> 607c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 617c478bd9Sstevel@tonic-gate #include <sys/vtoc.h> 627c478bd9Sstevel@tonic-gate #include <sys/errorq.h> 637c478bd9Sstevel@tonic-gate #include <sys/fm/util.h> 64e7cbe64fSgw #include <sys/fs/zfs.h> 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #include <vm/hat.h> 677c478bd9Sstevel@tonic-gate #include <vm/as.h> 687c478bd9Sstevel@tonic-gate #include <vm/page.h> 69ca3e8d88SDave Plauger #include <vm/pvn.h> 707c478bd9Sstevel@tonic-gate #include <vm/seg.h> 717c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 72d3d50737SRafael Vanoni #include <sys/clock_impl.h> 73b280b2a3SStuart Maybee #include <sys/hold_page.h> 747c478bd9Sstevel@tonic-gate 75ca3e8d88SDave Plauger #include <bzip2/bzlib.h> 76ca3e8d88SDave Plauger 77ca3e8d88SDave Plauger /* 78ca3e8d88SDave Plauger * Crash dump time is dominated by disk write time. To reduce this, 79ca3e8d88SDave Plauger * the stronger compression method bzip2 is applied to reduce the dump 80ca3e8d88SDave Plauger * size and hence reduce I/O time. However, bzip2 is much more 81ca3e8d88SDave Plauger * computationally expensive than the existing lzjb algorithm, so to 82ca3e8d88SDave Plauger * avoid increasing compression time, CPUs that are otherwise idle 83ca3e8d88SDave Plauger * during panic are employed to parallelize the compression task. 84ca3e8d88SDave Plauger * Many helper CPUs are needed to prevent bzip2 from being a 85ca3e8d88SDave Plauger * bottleneck, and on systems with too few CPUs, the lzjb algorithm is 86ca3e8d88SDave Plauger * parallelized instead. Lastly, I/O and compression are performed by 87ca3e8d88SDave Plauger * different CPUs, and are hence overlapped in time, unlike the older 88ca3e8d88SDave Plauger * serial code. 89ca3e8d88SDave Plauger * 90ca3e8d88SDave Plauger * Another important consideration is the speed of the dump 91ca3e8d88SDave Plauger * device. Faster disks need less CPUs in order to benefit from 92ca3e8d88SDave Plauger * parallel lzjb versus parallel bzip2. Therefore, the CPU count 93ca3e8d88SDave Plauger * threshold for switching from parallel lzjb to paralled bzip2 is 94ca3e8d88SDave Plauger * elevated for faster disks. The dump device speed is adduced from 95ca3e8d88SDave Plauger * the setting for dumpbuf.iosize, see dump_update_clevel. 96ca3e8d88SDave Plauger */ 97ca3e8d88SDave Plauger 98ca3e8d88SDave Plauger /* 99ca3e8d88SDave Plauger * exported vars 100ca3e8d88SDave Plauger */ 101ca3e8d88SDave Plauger kmutex_t dump_lock; /* lock for dump configuration */ 102ca3e8d88SDave Plauger dumphdr_t *dumphdr; /* dump header */ 1037c478bd9Sstevel@tonic-gate int dump_conflags = DUMP_KERNEL; /* dump configuration flags */ 104ca3e8d88SDave Plauger vnode_t *dumpvp; /* dump device vnode pointer */ 105ca3e8d88SDave Plauger u_offset_t dumpvp_size; /* size of dump device, in bytes */ 106ca3e8d88SDave Plauger char *dumppath; /* pathname of dump device */ 107ca3e8d88SDave Plauger int dump_timeout = 120; /* timeout for dumping pages */ 108ca3e8d88SDave Plauger int dump_timeleft; /* portion of dump_timeout remaining */ 109ca3e8d88SDave Plauger int dump_ioerr; /* dump i/o error */ 110ca3e8d88SDave Plauger int dump_check_used; /* enable check for used pages */ 111f6e214c7SGavin Maltby char *dump_stack_scratch; /* scratch area for saving stack summary */ 112ca3e8d88SDave Plauger 113ca3e8d88SDave Plauger /* 114ca3e8d88SDave Plauger * Tunables for dump compression and parallelism. These can be set via 115ca3e8d88SDave Plauger * /etc/system. 116ca3e8d88SDave Plauger * 117ca3e8d88SDave Plauger * dump_ncpu_low number of helpers for parallel lzjb 118ca3e8d88SDave Plauger * This is also the minimum configuration. 119ca3e8d88SDave Plauger * 120ca3e8d88SDave Plauger * dump_bzip2_level bzip2 compression level: 1-9 121ca3e8d88SDave Plauger * Higher numbers give greater compression, but take more memory 122ca3e8d88SDave Plauger * and time. Memory used per helper is ~(dump_bzip2_level * 1MB). 123ca3e8d88SDave Plauger * 124ca3e8d88SDave Plauger * dump_plat_mincpu the cross-over limit for using bzip2 (per platform): 125ca3e8d88SDave Plauger * if dump_plat_mincpu == 0, then always do single threaded dump 126ca3e8d88SDave Plauger * if ncpu >= dump_plat_mincpu then try to use bzip2 127ca3e8d88SDave Plauger * 128ca3e8d88SDave Plauger * dump_metrics_on if set, metrics are collected in the kernel, passed 129ca3e8d88SDave Plauger * to savecore via the dump file, and recorded by savecore in 130ca3e8d88SDave Plauger * METRICS.txt. 131ca3e8d88SDave Plauger */ 132ca3e8d88SDave Plauger uint_t dump_ncpu_low = 4; /* minimum config for parallel lzjb */ 133ca3e8d88SDave Plauger uint_t dump_bzip2_level = 1; /* bzip2 level (1-9) */ 134ca3e8d88SDave Plauger 1354cca9c84SDave Plauger /* Use dump_plat_mincpu_default unless this variable is set by /etc/system */ 1364cca9c84SDave Plauger #define MINCPU_NOT_SET ((uint_t)-1) 1374cca9c84SDave Plauger uint_t dump_plat_mincpu = MINCPU_NOT_SET; 1384cca9c84SDave Plauger 1399dd77bc8SDave Plauger /* tunables for pre-reserved heap */ 1409dd77bc8SDave Plauger uint_t dump_kmem_permap = 1024; 1419dd77bc8SDave Plauger uint_t dump_kmem_pages = 8; 1429dd77bc8SDave Plauger 143ca3e8d88SDave Plauger /* Define multiple buffers per helper to avoid stalling */ 144ca3e8d88SDave Plauger #define NCBUF_PER_HELPER 2 145ca3e8d88SDave Plauger #define NCMAP_PER_HELPER 4 146ca3e8d88SDave Plauger 147ca3e8d88SDave Plauger /* minimum number of helpers configured */ 148ca3e8d88SDave Plauger #define MINHELPERS (dump_ncpu_low) 149ca3e8d88SDave Plauger #define MINCBUFS (MINHELPERS * NCBUF_PER_HELPER) 150ca3e8d88SDave Plauger 151ca3e8d88SDave Plauger /* 152ca3e8d88SDave Plauger * Define constant parameters. 153ca3e8d88SDave Plauger * 154ca3e8d88SDave Plauger * CBUF_SIZE size of an output buffer 155ca3e8d88SDave Plauger * 156ca3e8d88SDave Plauger * CBUF_MAPSIZE size of virtual range for mapping pages 157ca3e8d88SDave Plauger * 158ca3e8d88SDave Plauger * CBUF_MAPNP size of virtual range in pages 159ca3e8d88SDave Plauger * 160ca3e8d88SDave Plauger */ 161ca3e8d88SDave Plauger #define DUMP_1KB ((size_t)1 << 10) 162ca3e8d88SDave Plauger #define DUMP_1MB ((size_t)1 << 20) 163ca3e8d88SDave Plauger #define CBUF_SIZE ((size_t)1 << 17) 164ca3e8d88SDave Plauger #define CBUF_MAPSHIFT (22) 165ca3e8d88SDave Plauger #define CBUF_MAPSIZE ((size_t)1 << CBUF_MAPSHIFT) 166ca3e8d88SDave Plauger #define CBUF_MAPNP ((size_t)1 << (CBUF_MAPSHIFT - PAGESHIFT)) 167ca3e8d88SDave Plauger 168ca3e8d88SDave Plauger /* 169ca3e8d88SDave Plauger * Compression metrics are accumulated nano-second subtotals. The 170ca3e8d88SDave Plauger * results are normalized by the number of pages dumped. A report is 171ca3e8d88SDave Plauger * generated when dumpsys() completes and is saved in the dump image 172ca3e8d88SDave Plauger * after the trailing dump header. 173ca3e8d88SDave Plauger * 174ca3e8d88SDave Plauger * Metrics are always collected. Set the variable dump_metrics_on to 175ca3e8d88SDave Plauger * cause metrics to be saved in the crash file, where savecore will 176ca3e8d88SDave Plauger * save it in the file METRICS.txt. 177ca3e8d88SDave Plauger */ 178ca3e8d88SDave Plauger #define PERPAGES \ 179ca3e8d88SDave Plauger PERPAGE(bitmap) PERPAGE(map) PERPAGE(unmap) \ 180ca3e8d88SDave Plauger PERPAGE(copy) PERPAGE(compress) \ 181ca3e8d88SDave Plauger PERPAGE(write) \ 182ca3e8d88SDave Plauger PERPAGE(inwait) PERPAGE(outwait) 183ca3e8d88SDave Plauger 184ca3e8d88SDave Plauger typedef struct perpage { 185ca3e8d88SDave Plauger #define PERPAGE(x) hrtime_t x; 186ca3e8d88SDave Plauger PERPAGES 187ca3e8d88SDave Plauger #undef PERPAGE 188ca3e8d88SDave Plauger } perpage_t; 189ca3e8d88SDave Plauger 190ca3e8d88SDave Plauger /* 191ca3e8d88SDave Plauger * This macro controls the code generation for collecting dump 192ca3e8d88SDave Plauger * performance information. By default, the code is generated, but 193ca3e8d88SDave Plauger * automatic saving of the information is disabled. If dump_metrics_on 194ca3e8d88SDave Plauger * is set to 1, the timing information is passed to savecore via the 195ca3e8d88SDave Plauger * crash file, where it is appended to the file dump-dir/METRICS.txt. 196ca3e8d88SDave Plauger */ 197ca3e8d88SDave Plauger #define COLLECT_METRICS 198ca3e8d88SDave Plauger 199ca3e8d88SDave Plauger #ifdef COLLECT_METRICS 200ca3e8d88SDave Plauger uint_t dump_metrics_on = 0; /* set to 1 to enable recording metrics */ 201ca3e8d88SDave Plauger 202ca3e8d88SDave Plauger #define HRSTART(v, m) v##ts.m = gethrtime() 203ca3e8d88SDave Plauger #define HRSTOP(v, m) v.m += gethrtime() - v##ts.m 204ca3e8d88SDave Plauger #define HRBEGIN(v, m, s) v##ts.m = gethrtime(); v.size += s 205ca3e8d88SDave Plauger #define HREND(v, m) v.m += gethrtime() - v##ts.m 206ca3e8d88SDave Plauger #define HRNORM(v, m, n) v.m /= (n) 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate #else 209ca3e8d88SDave Plauger #define HRSTART(v, m) 210ca3e8d88SDave Plauger #define HRSTOP(v, m) 211ca3e8d88SDave Plauger #define HRBEGIN(v, m, s) 212ca3e8d88SDave Plauger #define HREND(v, m) 213ca3e8d88SDave Plauger #define HRNORM(v, m, n) 214ca3e8d88SDave Plauger #endif /* COLLECT_METRICS */ 215ca3e8d88SDave Plauger 216ca3e8d88SDave Plauger /* 217ca3e8d88SDave Plauger * Buffers for copying and compressing memory pages. 218ca3e8d88SDave Plauger * 219ca3e8d88SDave Plauger * cbuf_t buffer controllers: used for both input and output. 220ca3e8d88SDave Plauger * 221ca3e8d88SDave Plauger * The buffer state indicates how it is being used: 222ca3e8d88SDave Plauger * 223ca3e8d88SDave Plauger * CBUF_FREEMAP: CBUF_MAPSIZE virtual address range is available for 224ca3e8d88SDave Plauger * mapping input pages. 225ca3e8d88SDave Plauger * 226ca3e8d88SDave Plauger * CBUF_INREADY: input pages are mapped and ready for compression by a 227ca3e8d88SDave Plauger * helper. 228ca3e8d88SDave Plauger * 229ca3e8d88SDave Plauger * CBUF_USEDMAP: mapping has been consumed by a helper. Needs unmap. 230ca3e8d88SDave Plauger * 231ca3e8d88SDave Plauger * CBUF_FREEBUF: CBUF_SIZE output buffer, which is available. 232ca3e8d88SDave Plauger * 233ca3e8d88SDave Plauger * CBUF_WRITE: CBUF_SIZE block of compressed pages from a helper, 234ca3e8d88SDave Plauger * ready to write out. 235ca3e8d88SDave Plauger * 236ca3e8d88SDave Plauger * CBUF_ERRMSG: CBUF_SIZE block of error messages from a helper 237ca3e8d88SDave Plauger * (reports UE errors.) 238ca3e8d88SDave Plauger */ 239ca3e8d88SDave Plauger 240ca3e8d88SDave Plauger typedef enum cbufstate { 241ca3e8d88SDave Plauger CBUF_FREEMAP, 242ca3e8d88SDave Plauger CBUF_INREADY, 243ca3e8d88SDave Plauger CBUF_USEDMAP, 244ca3e8d88SDave Plauger CBUF_FREEBUF, 245ca3e8d88SDave Plauger CBUF_WRITE, 246ca3e8d88SDave Plauger CBUF_ERRMSG 247ca3e8d88SDave Plauger } cbufstate_t; 248ca3e8d88SDave Plauger 249ca3e8d88SDave Plauger typedef struct cbuf cbuf_t; 250ca3e8d88SDave Plauger 251ca3e8d88SDave Plauger struct cbuf { 252ca3e8d88SDave Plauger cbuf_t *next; /* next in list */ 253ca3e8d88SDave Plauger cbufstate_t state; /* processing state */ 254ca3e8d88SDave Plauger size_t used; /* amount used */ 255ca3e8d88SDave Plauger size_t size; /* mem size */ 256ca3e8d88SDave Plauger char *buf; /* kmem or vmem */ 257ca3e8d88SDave Plauger pgcnt_t pagenum; /* index to pfn map */ 258ca3e8d88SDave Plauger pgcnt_t bitnum; /* first set bitnum */ 259ca3e8d88SDave Plauger pfn_t pfn; /* first pfn in mapped range */ 260ca3e8d88SDave Plauger int off; /* byte offset to first pfn */ 261ca3e8d88SDave Plauger }; 262ca3e8d88SDave Plauger 263f6e214c7SGavin Maltby static char dump_osimage_uuid[36 + 1]; 264f6e214c7SGavin Maltby 265f6e214c7SGavin Maltby #define isdigit(ch) ((ch) >= '0' && (ch) <= '9') 266f6e214c7SGavin Maltby #define isxdigit(ch) (isdigit(ch) || ((ch) >= 'a' && (ch) <= 'f') || \ 267f6e214c7SGavin Maltby ((ch) >= 'A' && (ch) <= 'F')) 268f6e214c7SGavin Maltby 269ca3e8d88SDave Plauger /* 270ca3e8d88SDave Plauger * cqueue_t queues: a uni-directional channel for communication 271ca3e8d88SDave Plauger * from the master to helper tasks or vice-versa using put and 272ca3e8d88SDave Plauger * get primitives. Both mappings and data buffers are passed via 273ca3e8d88SDave Plauger * queues. Producers close a queue when done. The number of 274ca3e8d88SDave Plauger * active producers is reference counted so the consumer can 275ca3e8d88SDave Plauger * detect end of data. Concurrent access is mediated by atomic 276ca3e8d88SDave Plauger * operations for panic dump, or mutex/cv for live dump. 277ca3e8d88SDave Plauger * 278ca3e8d88SDave Plauger * There a four queues, used as follows: 279ca3e8d88SDave Plauger * 280ca3e8d88SDave Plauger * Queue Dataflow NewState 281ca3e8d88SDave Plauger * -------------------------------------------------- 282ca3e8d88SDave Plauger * mainq master -> master FREEMAP 283ca3e8d88SDave Plauger * master has initialized or unmapped an input buffer 284ca3e8d88SDave Plauger * -------------------------------------------------- 285ca3e8d88SDave Plauger * helperq master -> helper INREADY 286ca3e8d88SDave Plauger * master has mapped input for use by helper 287ca3e8d88SDave Plauger * -------------------------------------------------- 288ca3e8d88SDave Plauger * mainq master <- helper USEDMAP 289ca3e8d88SDave Plauger * helper is done with input 290ca3e8d88SDave Plauger * -------------------------------------------------- 291ca3e8d88SDave Plauger * freebufq master -> helper FREEBUF 292ca3e8d88SDave Plauger * master has initialized or written an output buffer 293ca3e8d88SDave Plauger * -------------------------------------------------- 294ca3e8d88SDave Plauger * mainq master <- helper WRITE 295ca3e8d88SDave Plauger * block of compressed pages from a helper 296ca3e8d88SDave Plauger * -------------------------------------------------- 297ca3e8d88SDave Plauger * mainq master <- helper ERRMSG 298ca3e8d88SDave Plauger * error messages from a helper (memory error case) 299ca3e8d88SDave Plauger * -------------------------------------------------- 300ca3e8d88SDave Plauger * writerq master <- master WRITE 301ca3e8d88SDave Plauger * non-blocking queue of blocks to write 302ca3e8d88SDave Plauger * -------------------------------------------------- 303ca3e8d88SDave Plauger */ 304ca3e8d88SDave Plauger typedef struct cqueue { 305ca3e8d88SDave Plauger cbuf_t *volatile first; /* first in list */ 306ca3e8d88SDave Plauger cbuf_t *last; /* last in list */ 307ca3e8d88SDave Plauger hrtime_t ts; /* timestamp */ 308ca3e8d88SDave Plauger hrtime_t empty; /* total time empty */ 309ca3e8d88SDave Plauger kmutex_t mutex; /* live state lock */ 310ca3e8d88SDave Plauger kcondvar_t cv; /* live wait var */ 311ca3e8d88SDave Plauger lock_t spinlock; /* panic mode spin lock */ 312ca3e8d88SDave Plauger volatile uint_t open; /* producer ref count */ 313ca3e8d88SDave Plauger } cqueue_t; 314ca3e8d88SDave Plauger 315ca3e8d88SDave Plauger /* 316ca3e8d88SDave Plauger * Convenience macros for using the cqueue functions 317ca3e8d88SDave Plauger * Note that the caller must have defined "dumpsync_t *ds" 318ca3e8d88SDave Plauger */ 319ca3e8d88SDave Plauger #define CQ_IS_EMPTY(q) \ 320ca3e8d88SDave Plauger (ds->q.first == NULL) 321ca3e8d88SDave Plauger 322ca3e8d88SDave Plauger #define CQ_OPEN(q) \ 323ca3e8d88SDave Plauger atomic_inc_uint(&ds->q.open) 324ca3e8d88SDave Plauger 325ca3e8d88SDave Plauger #define CQ_CLOSE(q) \ 326ca3e8d88SDave Plauger dumpsys_close_cq(&ds->q, ds->live) 327ca3e8d88SDave Plauger 328ca3e8d88SDave Plauger #define CQ_PUT(q, cp, st) \ 329ca3e8d88SDave Plauger dumpsys_put_cq(&ds->q, cp, st, ds->live) 330ca3e8d88SDave Plauger 331ca3e8d88SDave Plauger #define CQ_GET(q) \ 332ca3e8d88SDave Plauger dumpsys_get_cq(&ds->q, ds->live) 333ca3e8d88SDave Plauger 334ca3e8d88SDave Plauger /* 335ca3e8d88SDave Plauger * Dynamic state when dumpsys() is running. 336ca3e8d88SDave Plauger */ 337ca3e8d88SDave Plauger typedef struct dumpsync { 338ca3e8d88SDave Plauger pgcnt_t npages; /* subtotal of pages dumped */ 339ca3e8d88SDave Plauger pgcnt_t pages_mapped; /* subtotal of pages mapped */ 340ca3e8d88SDave Plauger pgcnt_t pages_used; /* subtotal of pages used per map */ 341ca3e8d88SDave Plauger size_t nwrite; /* subtotal of bytes written */ 342ca3e8d88SDave Plauger uint_t live; /* running live dump */ 343ca3e8d88SDave Plauger uint_t neednl; /* will need to print a newline */ 344ca3e8d88SDave Plauger uint_t percent; /* dump progress */ 345ca3e8d88SDave Plauger uint_t percent_done; /* dump progress reported */ 346*2fb0949cSJoshua M. Clulow int sec_done; /* dump progress last report time */ 347ca3e8d88SDave Plauger cqueue_t freebufq; /* free kmem bufs for writing */ 348ca3e8d88SDave Plauger cqueue_t mainq; /* input for main task */ 349ca3e8d88SDave Plauger cqueue_t helperq; /* input for helpers */ 350ca3e8d88SDave Plauger cqueue_t writerq; /* input for writer */ 351ca3e8d88SDave Plauger hrtime_t start; /* start time */ 352ca3e8d88SDave Plauger hrtime_t elapsed; /* elapsed time when completed */ 353ca3e8d88SDave Plauger hrtime_t iotime; /* time spent writing nwrite bytes */ 354ca3e8d88SDave Plauger hrtime_t iowait; /* time spent waiting for output */ 355ca3e8d88SDave Plauger hrtime_t iowaitts; /* iowait timestamp */ 356ca3e8d88SDave Plauger perpage_t perpage; /* metrics */ 357ca3e8d88SDave Plauger perpage_t perpagets; 358ca3e8d88SDave Plauger int dumpcpu; /* master cpu */ 359ca3e8d88SDave Plauger } dumpsync_t; 360ca3e8d88SDave Plauger 361ca3e8d88SDave Plauger static dumpsync_t dumpsync; /* synchronization vars */ 362ca3e8d88SDave Plauger 363ca3e8d88SDave Plauger /* 364ca3e8d88SDave Plauger * helper_t helpers: contains the context for a stream. CPUs run in 365ca3e8d88SDave Plauger * parallel at dump time; each CPU creates a single stream of 366ca3e8d88SDave Plauger * compression data. Stream data is divided into CBUF_SIZE blocks. 367ca3e8d88SDave Plauger * The blocks are written in order within a stream. But, blocks from 368ca3e8d88SDave Plauger * multiple streams can be interleaved. Each stream is identified by a 369ca3e8d88SDave Plauger * unique tag. 370ca3e8d88SDave Plauger */ 371ca3e8d88SDave Plauger typedef struct helper { 372ca3e8d88SDave Plauger int helper; /* bound helper id */ 373ca3e8d88SDave Plauger int tag; /* compression stream tag */ 374ca3e8d88SDave Plauger perpage_t perpage; /* per page metrics */ 375ca3e8d88SDave Plauger perpage_t perpagets; /* per page metrics (timestamps) */ 376ca3e8d88SDave Plauger taskqid_t taskqid; /* live dump task ptr */ 377ca3e8d88SDave Plauger int in, out; /* buffer offsets */ 378ca3e8d88SDave Plauger cbuf_t *cpin, *cpout, *cperr; /* cbuf objects in process */ 379ca3e8d88SDave Plauger dumpsync_t *ds; /* pointer to sync vars */ 380ca3e8d88SDave Plauger size_t used; /* counts input consumed */ 381ca3e8d88SDave Plauger char *page; /* buffer for page copy */ 382ca3e8d88SDave Plauger char *lzbuf; /* lzjb output */ 383ca3e8d88SDave Plauger bz_stream bzstream; /* bzip2 state */ 384ca3e8d88SDave Plauger } helper_t; 385ca3e8d88SDave Plauger 386ca3e8d88SDave Plauger #define MAINHELPER (-1) /* helper is also the main task */ 387ca3e8d88SDave Plauger #define FREEHELPER (-2) /* unbound helper */ 388ca3e8d88SDave Plauger #define DONEHELPER (-3) /* helper finished */ 3897c478bd9Sstevel@tonic-gate 390ca3e8d88SDave Plauger /* 391ca3e8d88SDave Plauger * configuration vars for dumpsys 392ca3e8d88SDave Plauger */ 393ca3e8d88SDave Plauger typedef struct dumpcfg { 394ca3e8d88SDave Plauger int threshold; /* ncpu threshold for bzip2 */ 395ca3e8d88SDave Plauger int nhelper; /* number of helpers */ 396ca3e8d88SDave Plauger int nhelper_used; /* actual number of helpers used */ 397ca3e8d88SDave Plauger int ncmap; /* number VA pages for compression */ 398ca3e8d88SDave Plauger int ncbuf; /* number of bufs for compression */ 399ca3e8d88SDave Plauger int ncbuf_used; /* number of bufs in use */ 400ca3e8d88SDave Plauger uint_t clevel; /* dump compression level */ 401ca3e8d88SDave Plauger helper_t *helper; /* array of helpers */ 402ca3e8d88SDave Plauger cbuf_t *cmap; /* array of input (map) buffers */ 403ca3e8d88SDave Plauger cbuf_t *cbuf; /* array of output buffers */ 404ca3e8d88SDave Plauger ulong_t *helpermap; /* set of dumpsys helper CPU ids */ 405ca3e8d88SDave Plauger ulong_t *bitmap; /* bitmap for marking pages to dump */ 406ca3e8d88SDave Plauger ulong_t *rbitmap; /* bitmap for used CBUF_MAPSIZE ranges */ 407ca3e8d88SDave Plauger pgcnt_t bitmapsize; /* size of bitmap */ 408ca3e8d88SDave Plauger pgcnt_t rbitmapsize; /* size of bitmap for ranges */ 409ca3e8d88SDave Plauger pgcnt_t found4m; /* number ranges allocated by dump */ 410ca3e8d88SDave Plauger pgcnt_t foundsm; /* number small pages allocated by dump */ 411ca3e8d88SDave Plauger pid_t *pids; /* list of process IDs at dump time */ 412ca3e8d88SDave Plauger size_t maxsize; /* memory size needed at dump time */ 413ca3e8d88SDave Plauger size_t maxvmsize; /* size of reserved VM */ 414ca3e8d88SDave Plauger char *maxvm; /* reserved VM for spare pages */ 415ca3e8d88SDave Plauger lock_t helper_lock; /* protect helper state */ 416ca3e8d88SDave Plauger char helpers_wanted; /* flag to enable parallelism */ 417ca3e8d88SDave Plauger } dumpcfg_t; 418ca3e8d88SDave Plauger 419ca3e8d88SDave Plauger static dumpcfg_t dumpcfg; /* config vars */ 420ca3e8d88SDave Plauger 421ca3e8d88SDave Plauger /* 422ca3e8d88SDave Plauger * The dump I/O buffer. 423ca3e8d88SDave Plauger * 424ca3e8d88SDave Plauger * There is one I/O buffer used by dumpvp_write and dumvp_flush. It is 425ca3e8d88SDave Plauger * sized according to the optimum device transfer speed. 426ca3e8d88SDave Plauger */ 427ca3e8d88SDave Plauger typedef struct dumpbuf { 428ca3e8d88SDave Plauger vnode_t *cdev_vp; /* VCHR open of the dump device */ 429ca3e8d88SDave Plauger len_t vp_limit; /* maximum write offset */ 430ca3e8d88SDave Plauger offset_t vp_off; /* current dump device offset */ 431ca3e8d88SDave Plauger char *cur; /* dump write pointer */ 432ca3e8d88SDave Plauger char *start; /* dump buffer address */ 433ca3e8d88SDave Plauger char *end; /* dump buffer end */ 434ca3e8d88SDave Plauger size_t size; /* size of dumpbuf in bytes */ 435ca3e8d88SDave Plauger size_t iosize; /* best transfer size for device */ 436ca3e8d88SDave Plauger } dumpbuf_t; 437ca3e8d88SDave Plauger 438ca3e8d88SDave Plauger dumpbuf_t dumpbuf; /* I/O buffer */ 439ca3e8d88SDave Plauger 440ca3e8d88SDave Plauger /* 441ca3e8d88SDave Plauger * The dump I/O buffer must be at least one page, at most xfer_size 442ca3e8d88SDave Plauger * bytes, and should scale with physmem in between. The transfer size 443ca3e8d88SDave Plauger * passed in will either represent a global default (maxphys) or the 444ca3e8d88SDave Plauger * best size for the device. The size of the dumpbuf I/O buffer is 445ca3e8d88SDave Plauger * limited by dumpbuf_limit (8MB by default) because the dump 446ca3e8d88SDave Plauger * performance saturates beyond a certain size. The default is to 447ca3e8d88SDave Plauger * select 1/4096 of the memory. 4487c478bd9Sstevel@tonic-gate */ 449ca3e8d88SDave Plauger static int dumpbuf_fraction = 12; /* memory size scale factor */ 450ca3e8d88SDave Plauger static size_t dumpbuf_limit = 8 * DUMP_1MB; /* max I/O buf size */ 451ca3e8d88SDave Plauger 4527c478bd9Sstevel@tonic-gate static size_t 4537c478bd9Sstevel@tonic-gate dumpbuf_iosize(size_t xfer_size) 4547c478bd9Sstevel@tonic-gate { 455ca3e8d88SDave Plauger size_t iosize = ptob(physmem >> dumpbuf_fraction); 456ca3e8d88SDave Plauger 457ca3e8d88SDave Plauger if (iosize < PAGESIZE) 458ca3e8d88SDave Plauger iosize = PAGESIZE; 459ca3e8d88SDave Plauger else if (iosize > xfer_size) 460ca3e8d88SDave Plauger iosize = xfer_size; 461ca3e8d88SDave Plauger if (iosize > dumpbuf_limit) 462ca3e8d88SDave Plauger iosize = dumpbuf_limit; 463ca3e8d88SDave Plauger return (iosize & PAGEMASK); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate 466ca3e8d88SDave Plauger /* 467ca3e8d88SDave Plauger * resize the I/O buffer 468ca3e8d88SDave Plauger */ 4697c478bd9Sstevel@tonic-gate static void 4707c478bd9Sstevel@tonic-gate dumpbuf_resize(void) 4717c478bd9Sstevel@tonic-gate { 472ca3e8d88SDave Plauger char *old_buf = dumpbuf.start; 473ca3e8d88SDave Plauger size_t old_size = dumpbuf.size; 4747c478bd9Sstevel@tonic-gate char *new_buf; 4757c478bd9Sstevel@tonic-gate size_t new_size; 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&dump_lock)); 4787c478bd9Sstevel@tonic-gate 479ca3e8d88SDave Plauger new_size = dumpbuf_iosize(MAX(dumpbuf.iosize, maxphys)); 480ca3e8d88SDave Plauger if (new_size <= old_size) 4817c478bd9Sstevel@tonic-gate return; /* no need to reallocate buffer */ 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate new_buf = kmem_alloc(new_size, KM_SLEEP); 484ca3e8d88SDave Plauger dumpbuf.size = new_size; 485ca3e8d88SDave Plauger dumpbuf.start = new_buf; 486ca3e8d88SDave Plauger dumpbuf.end = new_buf + new_size; 4877c478bd9Sstevel@tonic-gate kmem_free(old_buf, old_size); 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate 490ca3e8d88SDave Plauger /* 491ca3e8d88SDave Plauger * dump_update_clevel is called when dumpadm configures the dump device. 492ca3e8d88SDave Plauger * Calculate number of helpers and buffers. 493ca3e8d88SDave Plauger * Allocate the minimum configuration for now. 494ca3e8d88SDave Plauger * 495ca3e8d88SDave Plauger * When the dump file is configured we reserve a minimum amount of 496ca3e8d88SDave Plauger * memory for use at crash time. But we reserve VA for all the memory 497ca3e8d88SDave Plauger * we really want in order to do the fastest dump possible. The VA is 498ca3e8d88SDave Plauger * backed by pages not being dumped, according to the bitmap. If 499ca3e8d88SDave Plauger * there is insufficient spare memory, however, we fall back to the 500ca3e8d88SDave Plauger * minimum. 501ca3e8d88SDave Plauger * 502ca3e8d88SDave Plauger * Live dump (savecore -L) always uses the minimum config. 503ca3e8d88SDave Plauger * 504ca3e8d88SDave Plauger * clevel 0 is single threaded lzjb 505ca3e8d88SDave Plauger * clevel 1 is parallel lzjb 506ca3e8d88SDave Plauger * clevel 2 is parallel bzip2 507ca3e8d88SDave Plauger * 508ca3e8d88SDave Plauger * The ncpu threshold is selected with dump_plat_mincpu. 509ca3e8d88SDave Plauger * On OPL, set_platform_defaults() overrides the sun4u setting. 510ca3e8d88SDave Plauger * The actual values are defined via DUMP_PLAT_*_MINCPU macros. 511ca3e8d88SDave Plauger * 512ca3e8d88SDave Plauger * Architecture Threshold Algorithm 513ca3e8d88SDave Plauger * sun4u < 51 parallel lzjb 514ca3e8d88SDave Plauger * sun4u >= 51 parallel bzip2(*) 515ca3e8d88SDave Plauger * sun4u OPL < 8 parallel lzjb 516ca3e8d88SDave Plauger * sun4u OPL >= 8 parallel bzip2(*) 517ca3e8d88SDave Plauger * sun4v < 128 parallel lzjb 518ca3e8d88SDave Plauger * sun4v >= 128 parallel bzip2(*) 519ca3e8d88SDave Plauger * x86 < 11 parallel lzjb 520ca3e8d88SDave Plauger * x86 >= 11 parallel bzip2(*) 521ca3e8d88SDave Plauger * 32-bit N/A single-threaded lzjb 522ca3e8d88SDave Plauger * 523ca3e8d88SDave Plauger * (*) bzip2 is only chosen if there is sufficient available 524ca3e8d88SDave Plauger * memory for buffers at dump time. See dumpsys_get_maxmem(). 525ca3e8d88SDave Plauger * 526ca3e8d88SDave Plauger * Faster dump devices have larger I/O buffers. The threshold value is 527ca3e8d88SDave Plauger * increased according to the size of the dump I/O buffer, because 528ca3e8d88SDave Plauger * parallel lzjb performs better with faster disks. For buffers >= 1MB 529ca3e8d88SDave Plauger * the threshold is 3X; for buffers >= 256K threshold is 2X. 530ca3e8d88SDave Plauger * 531ca3e8d88SDave Plauger * For parallel dumps, the number of helpers is ncpu-1. The CPU 532ca3e8d88SDave Plauger * running panic runs the main task. For single-threaded dumps, the 533ca3e8d88SDave Plauger * panic CPU does lzjb compression (it is tagged as MAINHELPER.) 534ca3e8d88SDave Plauger * 535ca3e8d88SDave Plauger * Need multiple buffers per helper so that they do not block waiting 536ca3e8d88SDave Plauger * for the main task. 537ca3e8d88SDave Plauger * parallel single-threaded 538ca3e8d88SDave Plauger * Number of output buffers: nhelper*2 1 539ca3e8d88SDave Plauger * Number of mapping buffers: nhelper*4 1 540ca3e8d88SDave Plauger * 541ca3e8d88SDave Plauger */ 542ca3e8d88SDave Plauger static void 543ca3e8d88SDave Plauger dump_update_clevel() 544ca3e8d88SDave Plauger { 545ca3e8d88SDave Plauger int tag; 546ca3e8d88SDave Plauger size_t bz2size; 547ca3e8d88SDave Plauger helper_t *hp, *hpend; 548ca3e8d88SDave Plauger cbuf_t *cp, *cpend; 549ca3e8d88SDave Plauger dumpcfg_t *old = &dumpcfg; 550ca3e8d88SDave Plauger dumpcfg_t newcfg = *old; 551ca3e8d88SDave Plauger dumpcfg_t *new = &newcfg; 552ca3e8d88SDave Plauger 553ca3e8d88SDave Plauger ASSERT(MUTEX_HELD(&dump_lock)); 554ca3e8d88SDave Plauger 555ca3e8d88SDave Plauger /* 556ca3e8d88SDave Plauger * Free the previously allocated bufs and VM. 557ca3e8d88SDave Plauger */ 558ca3e8d88SDave Plauger if (old->helper != NULL) { 559ca3e8d88SDave Plauger 560ca3e8d88SDave Plauger /* helpers */ 561ca3e8d88SDave Plauger hpend = &old->helper[old->nhelper]; 562ca3e8d88SDave Plauger for (hp = old->helper; hp != hpend; hp++) { 563ca3e8d88SDave Plauger if (hp->lzbuf != NULL) 564ca3e8d88SDave Plauger kmem_free(hp->lzbuf, PAGESIZE); 565ca3e8d88SDave Plauger if (hp->page != NULL) 566ca3e8d88SDave Plauger kmem_free(hp->page, PAGESIZE); 567ca3e8d88SDave Plauger } 568ca3e8d88SDave Plauger kmem_free(old->helper, old->nhelper * sizeof (helper_t)); 569ca3e8d88SDave Plauger 570ca3e8d88SDave Plauger /* VM space for mapping pages */ 571ca3e8d88SDave Plauger cpend = &old->cmap[old->ncmap]; 572ca3e8d88SDave Plauger for (cp = old->cmap; cp != cpend; cp++) 573ca3e8d88SDave Plauger vmem_xfree(heap_arena, cp->buf, CBUF_MAPSIZE); 574ca3e8d88SDave Plauger kmem_free(old->cmap, old->ncmap * sizeof (cbuf_t)); 575ca3e8d88SDave Plauger 576ca3e8d88SDave Plauger /* output bufs */ 577ca3e8d88SDave Plauger cpend = &old->cbuf[old->ncbuf]; 578ca3e8d88SDave Plauger for (cp = old->cbuf; cp != cpend; cp++) 579ca3e8d88SDave Plauger if (cp->buf != NULL) 580ca3e8d88SDave Plauger kmem_free(cp->buf, cp->size); 581ca3e8d88SDave Plauger kmem_free(old->cbuf, old->ncbuf * sizeof (cbuf_t)); 582ca3e8d88SDave Plauger 583ca3e8d88SDave Plauger /* reserved VM for dumpsys_get_maxmem */ 584ca3e8d88SDave Plauger if (old->maxvmsize > 0) 585ca3e8d88SDave Plauger vmem_xfree(heap_arena, old->maxvm, old->maxvmsize); 586ca3e8d88SDave Plauger } 587ca3e8d88SDave Plauger 588ca3e8d88SDave Plauger /* 589ca3e8d88SDave Plauger * Allocate memory and VM. 590ca3e8d88SDave Plauger * One CPU runs dumpsys, the rest are helpers. 591ca3e8d88SDave Plauger */ 592ca3e8d88SDave Plauger new->nhelper = ncpus - 1; 593ca3e8d88SDave Plauger if (new->nhelper < 1) 594ca3e8d88SDave Plauger new->nhelper = 1; 595ca3e8d88SDave Plauger 596ca3e8d88SDave Plauger if (new->nhelper > DUMP_MAX_NHELPER) 597ca3e8d88SDave Plauger new->nhelper = DUMP_MAX_NHELPER; 598ca3e8d88SDave Plauger 5994cca9c84SDave Plauger /* use platform default, unless /etc/system overrides */ 6004cca9c84SDave Plauger if (dump_plat_mincpu == MINCPU_NOT_SET) 6014cca9c84SDave Plauger dump_plat_mincpu = dump_plat_mincpu_default; 6024cca9c84SDave Plauger 603ca3e8d88SDave Plauger /* increase threshold for faster disks */ 604ca3e8d88SDave Plauger new->threshold = dump_plat_mincpu; 605ca3e8d88SDave Plauger if (dumpbuf.iosize >= DUMP_1MB) 606ca3e8d88SDave Plauger new->threshold *= 3; 607ca3e8d88SDave Plauger else if (dumpbuf.iosize >= (256 * DUMP_1KB)) 608ca3e8d88SDave Plauger new->threshold *= 2; 609ca3e8d88SDave Plauger 610ca3e8d88SDave Plauger /* figure compression level based upon the computed threshold. */ 611ca3e8d88SDave Plauger if (dump_plat_mincpu == 0 || new->nhelper < 2) { 612ca3e8d88SDave Plauger new->clevel = 0; 613ca3e8d88SDave Plauger new->nhelper = 1; 614ca3e8d88SDave Plauger } else if ((new->nhelper + 1) >= new->threshold) { 615ca3e8d88SDave Plauger new->clevel = DUMP_CLEVEL_BZIP2; 616ca3e8d88SDave Plauger } else { 617ca3e8d88SDave Plauger new->clevel = DUMP_CLEVEL_LZJB; 618ca3e8d88SDave Plauger } 619ca3e8d88SDave Plauger 620ca3e8d88SDave Plauger if (new->clevel == 0) { 621ca3e8d88SDave Plauger new->ncbuf = 1; 622ca3e8d88SDave Plauger new->ncmap = 1; 623ca3e8d88SDave Plauger } else { 624ca3e8d88SDave Plauger new->ncbuf = NCBUF_PER_HELPER * new->nhelper; 625ca3e8d88SDave Plauger new->ncmap = NCMAP_PER_HELPER * new->nhelper; 626ca3e8d88SDave Plauger } 627ca3e8d88SDave Plauger 628ca3e8d88SDave Plauger /* 629ca3e8d88SDave Plauger * Allocate new data structures and buffers for MINHELPERS, 630ca3e8d88SDave Plauger * and also figure the max desired size. 631ca3e8d88SDave Plauger */ 632ca3e8d88SDave Plauger bz2size = BZ2_bzCompressInitSize(dump_bzip2_level); 633ca3e8d88SDave Plauger new->maxsize = 0; 634ca3e8d88SDave Plauger new->maxvmsize = 0; 635ca3e8d88SDave Plauger new->maxvm = NULL; 636ca3e8d88SDave Plauger tag = 1; 637ca3e8d88SDave Plauger new->helper = kmem_zalloc(new->nhelper * sizeof (helper_t), KM_SLEEP); 638ca3e8d88SDave Plauger hpend = &new->helper[new->nhelper]; 639ca3e8d88SDave Plauger for (hp = new->helper; hp != hpend; hp++) { 640ca3e8d88SDave Plauger hp->tag = tag++; 641ca3e8d88SDave Plauger if (hp < &new->helper[MINHELPERS]) { 642ca3e8d88SDave Plauger hp->lzbuf = kmem_alloc(PAGESIZE, KM_SLEEP); 643ca3e8d88SDave Plauger hp->page = kmem_alloc(PAGESIZE, KM_SLEEP); 644ca3e8d88SDave Plauger } else if (new->clevel < DUMP_CLEVEL_BZIP2) { 645ca3e8d88SDave Plauger new->maxsize += 2 * PAGESIZE; 646ca3e8d88SDave Plauger } else { 647ca3e8d88SDave Plauger new->maxsize += PAGESIZE; 648ca3e8d88SDave Plauger } 649ca3e8d88SDave Plauger if (new->clevel >= DUMP_CLEVEL_BZIP2) 650ca3e8d88SDave Plauger new->maxsize += bz2size; 651ca3e8d88SDave Plauger } 652ca3e8d88SDave Plauger 653ca3e8d88SDave Plauger new->cbuf = kmem_zalloc(new->ncbuf * sizeof (cbuf_t), KM_SLEEP); 654ca3e8d88SDave Plauger cpend = &new->cbuf[new->ncbuf]; 655ca3e8d88SDave Plauger for (cp = new->cbuf; cp != cpend; cp++) { 656ca3e8d88SDave Plauger cp->state = CBUF_FREEBUF; 657ca3e8d88SDave Plauger cp->size = CBUF_SIZE; 658ca3e8d88SDave Plauger if (cp < &new->cbuf[MINCBUFS]) 659ca3e8d88SDave Plauger cp->buf = kmem_alloc(cp->size, KM_SLEEP); 660ca3e8d88SDave Plauger else 661ca3e8d88SDave Plauger new->maxsize += cp->size; 662ca3e8d88SDave Plauger } 663ca3e8d88SDave Plauger 664ca3e8d88SDave Plauger new->cmap = kmem_zalloc(new->ncmap * sizeof (cbuf_t), KM_SLEEP); 665ca3e8d88SDave Plauger cpend = &new->cmap[new->ncmap]; 666ca3e8d88SDave Plauger for (cp = new->cmap; cp != cpend; cp++) { 667ca3e8d88SDave Plauger cp->state = CBUF_FREEMAP; 668ca3e8d88SDave Plauger cp->size = CBUF_MAPSIZE; 669ca3e8d88SDave Plauger cp->buf = vmem_xalloc(heap_arena, CBUF_MAPSIZE, CBUF_MAPSIZE, 670ca3e8d88SDave Plauger 0, 0, NULL, NULL, VM_SLEEP); 671ca3e8d88SDave Plauger } 672ca3e8d88SDave Plauger 673ca3e8d88SDave Plauger /* reserve VA to be backed with spare pages at crash time */ 674ca3e8d88SDave Plauger if (new->maxsize > 0) { 675ca3e8d88SDave Plauger new->maxsize = P2ROUNDUP(new->maxsize, PAGESIZE); 676ca3e8d88SDave Plauger new->maxvmsize = P2ROUNDUP(new->maxsize, CBUF_MAPSIZE); 677ca3e8d88SDave Plauger new->maxvm = vmem_xalloc(heap_arena, new->maxvmsize, 678ca3e8d88SDave Plauger CBUF_MAPSIZE, 0, 0, NULL, NULL, VM_SLEEP); 679ca3e8d88SDave Plauger } 680ca3e8d88SDave Plauger 6819dd77bc8SDave Plauger /* 6829dd77bc8SDave Plauger * Reserve memory for kmem allocation calls made during crash 6839dd77bc8SDave Plauger * dump. The hat layer allocates memory for each mapping 6849dd77bc8SDave Plauger * created, and the I/O path allocates buffers and data structs. 6859dd77bc8SDave Plauger * Add a few pages for safety. 6869dd77bc8SDave Plauger */ 6879dd77bc8SDave Plauger kmem_dump_init((new->ncmap * dump_kmem_permap) + 6889dd77bc8SDave Plauger (dump_kmem_pages * PAGESIZE)); 6899dd77bc8SDave Plauger 690ca3e8d88SDave Plauger /* set new config pointers */ 691ca3e8d88SDave Plauger *old = *new; 692ca3e8d88SDave Plauger } 693ca3e8d88SDave Plauger 694ca3e8d88SDave Plauger /* 695ca3e8d88SDave Plauger * Define a struct memlist walker to optimize bitnum to pfn 696ca3e8d88SDave Plauger * lookup. The walker maintains the state of the list traversal. 697ca3e8d88SDave Plauger */ 698ca3e8d88SDave Plauger typedef struct dumpmlw { 699ca3e8d88SDave Plauger struct memlist *mp; /* current memlist */ 700ca3e8d88SDave Plauger pgcnt_t basenum; /* bitnum base offset */ 701ca3e8d88SDave Plauger pgcnt_t mppages; /* current memlist size */ 702ca3e8d88SDave Plauger pgcnt_t mpleft; /* size to end of current memlist */ 703ca3e8d88SDave Plauger pfn_t mpaddr; /* first pfn in memlist */ 704ca3e8d88SDave Plauger } dumpmlw_t; 705ca3e8d88SDave Plauger 706ca3e8d88SDave Plauger /* initialize the walker */ 707ca3e8d88SDave Plauger static inline void 708ca3e8d88SDave Plauger dump_init_memlist_walker(dumpmlw_t *pw) 709ca3e8d88SDave Plauger { 710ca3e8d88SDave Plauger pw->mp = phys_install; 711ca3e8d88SDave Plauger pw->basenum = 0; 71256f33205SJonathan Adams pw->mppages = pw->mp->ml_size >> PAGESHIFT; 713ca3e8d88SDave Plauger pw->mpleft = pw->mppages; 71456f33205SJonathan Adams pw->mpaddr = pw->mp->ml_address >> PAGESHIFT; 715ca3e8d88SDave Plauger } 716ca3e8d88SDave Plauger 717ca3e8d88SDave Plauger /* 718ca3e8d88SDave Plauger * Lookup pfn given bitnum. The memlist can be quite long on some 719ca3e8d88SDave Plauger * systems (e.g.: one per board). To optimize sequential lookups, the 720ca3e8d88SDave Plauger * caller initializes and presents a memlist walker. 721ca3e8d88SDave Plauger */ 722ca3e8d88SDave Plauger static pfn_t 723ca3e8d88SDave Plauger dump_bitnum_to_pfn(pgcnt_t bitnum, dumpmlw_t *pw) 724ca3e8d88SDave Plauger { 725ca3e8d88SDave Plauger bitnum -= pw->basenum; 726ca3e8d88SDave Plauger while (pw->mp != NULL) { 727ca3e8d88SDave Plauger if (bitnum < pw->mppages) { 728ca3e8d88SDave Plauger pw->mpleft = pw->mppages - bitnum; 729ca3e8d88SDave Plauger return (pw->mpaddr + bitnum); 730ca3e8d88SDave Plauger } 731ca3e8d88SDave Plauger bitnum -= pw->mppages; 732ca3e8d88SDave Plauger pw->basenum += pw->mppages; 73356f33205SJonathan Adams pw->mp = pw->mp->ml_next; 734ca3e8d88SDave Plauger if (pw->mp != NULL) { 73556f33205SJonathan Adams pw->mppages = pw->mp->ml_size >> PAGESHIFT; 736ca3e8d88SDave Plauger pw->mpleft = pw->mppages; 73756f33205SJonathan Adams pw->mpaddr = pw->mp->ml_address >> PAGESHIFT; 738ca3e8d88SDave Plauger } 739ca3e8d88SDave Plauger } 740ca3e8d88SDave Plauger return (PFN_INVALID); 741ca3e8d88SDave Plauger } 742ca3e8d88SDave Plauger 743ca3e8d88SDave Plauger static pgcnt_t 744ca3e8d88SDave Plauger dump_pfn_to_bitnum(pfn_t pfn) 745ca3e8d88SDave Plauger { 746ca3e8d88SDave Plauger struct memlist *mp; 747ca3e8d88SDave Plauger pgcnt_t bitnum = 0; 748ca3e8d88SDave Plauger 74956f33205SJonathan Adams for (mp = phys_install; mp != NULL; mp = mp->ml_next) { 75056f33205SJonathan Adams if (pfn >= (mp->ml_address >> PAGESHIFT) && 75156f33205SJonathan Adams pfn < ((mp->ml_address + mp->ml_size) >> PAGESHIFT)) 75256f33205SJonathan Adams return (bitnum + pfn - (mp->ml_address >> PAGESHIFT)); 75356f33205SJonathan Adams bitnum += mp->ml_size >> PAGESHIFT; 754ca3e8d88SDave Plauger } 755ca3e8d88SDave Plauger return ((pgcnt_t)-1); 756ca3e8d88SDave Plauger } 757ca3e8d88SDave Plauger 758ca3e8d88SDave Plauger /* 759ca3e8d88SDave Plauger * Set/test bitmap for a CBUF_MAPSIZE range which includes pfn. The 760ca3e8d88SDave Plauger * mapping of pfn to range index is imperfect because pfn and bitnum 761ca3e8d88SDave Plauger * do not have the same phase. To make sure a CBUF_MAPSIZE range is 762ca3e8d88SDave Plauger * covered, call this for both ends: 763ca3e8d88SDave Plauger * dump_set_used(base) 764ca3e8d88SDave Plauger * dump_set_used(base+CBUF_MAPNP-1) 765ca3e8d88SDave Plauger * 766ca3e8d88SDave Plauger * This is used during a panic dump to mark pages allocated by 767ca3e8d88SDave Plauger * dumpsys_get_maxmem(). The macro IS_DUMP_PAGE(pp) is used by 768ca3e8d88SDave Plauger * page_get_mnode_freelist() to make sure pages used by dump are never 769ca3e8d88SDave Plauger * allocated. 770ca3e8d88SDave Plauger */ 771ca3e8d88SDave Plauger #define CBUF_MAPP2R(pfn) ((pfn) >> (CBUF_MAPSHIFT - PAGESHIFT)) 772ca3e8d88SDave Plauger 773ca3e8d88SDave Plauger static void 774ca3e8d88SDave Plauger dump_set_used(pfn_t pfn) 775ca3e8d88SDave Plauger { 776ca3e8d88SDave Plauger 777ca3e8d88SDave Plauger pgcnt_t bitnum, rbitnum; 778ca3e8d88SDave Plauger 779ca3e8d88SDave Plauger bitnum = dump_pfn_to_bitnum(pfn); 780ca3e8d88SDave Plauger ASSERT(bitnum != (pgcnt_t)-1); 781ca3e8d88SDave Plauger 782ca3e8d88SDave Plauger rbitnum = CBUF_MAPP2R(bitnum); 783ca3e8d88SDave Plauger ASSERT(rbitnum < dumpcfg.rbitmapsize); 784ca3e8d88SDave Plauger 785ca3e8d88SDave Plauger BT_SET(dumpcfg.rbitmap, rbitnum); 786ca3e8d88SDave Plauger } 787ca3e8d88SDave Plauger 788ca3e8d88SDave Plauger int 789ca3e8d88SDave Plauger dump_test_used(pfn_t pfn) 790ca3e8d88SDave Plauger { 791ca3e8d88SDave Plauger pgcnt_t bitnum, rbitnum; 792ca3e8d88SDave Plauger 793ca3e8d88SDave Plauger bitnum = dump_pfn_to_bitnum(pfn); 794ca3e8d88SDave Plauger ASSERT(bitnum != (pgcnt_t)-1); 795ca3e8d88SDave Plauger 796ca3e8d88SDave Plauger rbitnum = CBUF_MAPP2R(bitnum); 797ca3e8d88SDave Plauger ASSERT(rbitnum < dumpcfg.rbitmapsize); 798ca3e8d88SDave Plauger 799ca3e8d88SDave Plauger return (BT_TEST(dumpcfg.rbitmap, rbitnum)); 800ca3e8d88SDave Plauger } 801ca3e8d88SDave Plauger 802ca3e8d88SDave Plauger /* 803ca3e8d88SDave Plauger * dumpbzalloc and dumpbzfree are callbacks from the bzip2 library. 804ca3e8d88SDave Plauger * dumpsys_get_maxmem() uses them for BZ2_bzCompressInit(). 805ca3e8d88SDave Plauger */ 806ca3e8d88SDave Plauger static void * 807ca3e8d88SDave Plauger dumpbzalloc(void *opaque, int items, int size) 808ca3e8d88SDave Plauger { 809ca3e8d88SDave Plauger size_t *sz; 810ca3e8d88SDave Plauger char *ret; 811ca3e8d88SDave Plauger 812ca3e8d88SDave Plauger ASSERT(opaque != NULL); 813ca3e8d88SDave Plauger sz = opaque; 814ca3e8d88SDave Plauger ret = dumpcfg.maxvm + *sz; 815ca3e8d88SDave Plauger *sz += items * size; 816ca3e8d88SDave Plauger *sz = P2ROUNDUP(*sz, BZ2_BZALLOC_ALIGN); 817ca3e8d88SDave Plauger ASSERT(*sz <= dumpcfg.maxvmsize); 818ca3e8d88SDave Plauger return (ret); 819ca3e8d88SDave Plauger } 820ca3e8d88SDave Plauger 821ca3e8d88SDave Plauger /*ARGSUSED*/ 822ca3e8d88SDave Plauger static void 823ca3e8d88SDave Plauger dumpbzfree(void *opaque, void *addr) 824ca3e8d88SDave Plauger { 825ca3e8d88SDave Plauger } 826ca3e8d88SDave Plauger 827ca3e8d88SDave Plauger /* 828ca3e8d88SDave Plauger * Perform additional checks on the page to see if we can really use 829ca3e8d88SDave Plauger * it. The kernel (kas) pages are always set in the bitmap. However, 830ca3e8d88SDave Plauger * boot memory pages (prom_ppages or P_BOOTPAGES) are not in the 831ca3e8d88SDave Plauger * bitmap. So we check for them. 832ca3e8d88SDave Plauger */ 833ca3e8d88SDave Plauger static inline int 834ca3e8d88SDave Plauger dump_pfn_check(pfn_t pfn) 835ca3e8d88SDave Plauger { 836ca3e8d88SDave Plauger page_t *pp = page_numtopp_nolock(pfn); 837ca3e8d88SDave Plauger if (pp == NULL || pp->p_pagenum != pfn || 838ca3e8d88SDave Plauger #if defined(__sparc) 839af4c679fSSean McEnroe pp->p_vnode == &promvp || 840ca3e8d88SDave Plauger #else 841ca3e8d88SDave Plauger PP_ISBOOTPAGES(pp) || 842ca3e8d88SDave Plauger #endif 843ca3e8d88SDave Plauger pp->p_toxic != 0) 844ca3e8d88SDave Plauger return (0); 845ca3e8d88SDave Plauger return (1); 846ca3e8d88SDave Plauger } 847ca3e8d88SDave Plauger 848ca3e8d88SDave Plauger /* 849ca3e8d88SDave Plauger * Check a range to see if all contained pages are available and 850ca3e8d88SDave Plauger * return non-zero if the range can be used. 851ca3e8d88SDave Plauger */ 852ca3e8d88SDave Plauger static inline int 853ca3e8d88SDave Plauger dump_range_check(pgcnt_t start, pgcnt_t end, pfn_t pfn) 854ca3e8d88SDave Plauger { 855ca3e8d88SDave Plauger for (; start < end; start++, pfn++) { 856ca3e8d88SDave Plauger if (BT_TEST(dumpcfg.bitmap, start)) 857ca3e8d88SDave Plauger return (0); 858ca3e8d88SDave Plauger if (!dump_pfn_check(pfn)) 859ca3e8d88SDave Plauger return (0); 860ca3e8d88SDave Plauger } 861ca3e8d88SDave Plauger return (1); 862ca3e8d88SDave Plauger } 863ca3e8d88SDave Plauger 864ca3e8d88SDave Plauger /* 865ca3e8d88SDave Plauger * dumpsys_get_maxmem() is called during panic. Find unused ranges 866ca3e8d88SDave Plauger * and use them for buffers. If we find enough memory switch to 867ca3e8d88SDave Plauger * parallel bzip2, otherwise use parallel lzjb. 868ca3e8d88SDave Plauger * 869ca3e8d88SDave Plauger * It searches the dump bitmap in 2 passes. The first time it looks 870ca3e8d88SDave Plauger * for CBUF_MAPSIZE ranges. On the second pass it uses small pages. 871ca3e8d88SDave Plauger */ 872ca3e8d88SDave Plauger static void 873ca3e8d88SDave Plauger dumpsys_get_maxmem() 874ca3e8d88SDave Plauger { 875ca3e8d88SDave Plauger dumpcfg_t *cfg = &dumpcfg; 876ca3e8d88SDave Plauger cbuf_t *endcp = &cfg->cbuf[cfg->ncbuf]; 877ca3e8d88SDave Plauger helper_t *endhp = &cfg->helper[cfg->nhelper]; 878ca3e8d88SDave Plauger pgcnt_t bitnum, end; 879ca3e8d88SDave Plauger size_t sz, endsz, bz2size; 880ca3e8d88SDave Plauger pfn_t pfn, off; 881ca3e8d88SDave Plauger cbuf_t *cp; 882ca3e8d88SDave Plauger helper_t *hp, *ohp; 883ca3e8d88SDave Plauger dumpmlw_t mlw; 884ca3e8d88SDave Plauger int k; 885ca3e8d88SDave Plauger 8864cca9c84SDave Plauger /* 8879c84d1a2SDave Plauger * Setting dump_plat_mincpu to 0 at any time forces a serial 8889c84d1a2SDave Plauger * dump. 8894cca9c84SDave Plauger */ 8909c84d1a2SDave Plauger if (dump_plat_mincpu == 0) { 8914cca9c84SDave Plauger cfg->clevel = 0; 8924cca9c84SDave Plauger return; 8934cca9c84SDave Plauger } 8944cca9c84SDave Plauger 8954cca9c84SDave Plauger /* 8964cca9c84SDave Plauger * There may be no point in looking for spare memory. If 8974cca9c84SDave Plauger * dumping all memory, then none is spare. If doing a serial 8984cca9c84SDave Plauger * dump, then already have buffers. 8994cca9c84SDave Plauger */ 900ca3e8d88SDave Plauger if (cfg->maxsize == 0 || cfg->clevel < DUMP_CLEVEL_LZJB || 90186f21945SDave Plauger (dump_conflags & DUMP_ALL) != 0) { 90286f21945SDave Plauger if (cfg->clevel > DUMP_CLEVEL_LZJB) 90386f21945SDave Plauger cfg->clevel = DUMP_CLEVEL_LZJB; 904ca3e8d88SDave Plauger return; 90586f21945SDave Plauger } 906ca3e8d88SDave Plauger 907ca3e8d88SDave Plauger sz = 0; 908ca3e8d88SDave Plauger cfg->found4m = 0; 909ca3e8d88SDave Plauger cfg->foundsm = 0; 910ca3e8d88SDave Plauger 911ca3e8d88SDave Plauger /* bitmap of ranges used to estimate which pfns are being used */ 912ca3e8d88SDave Plauger bzero(dumpcfg.rbitmap, BT_SIZEOFMAP(dumpcfg.rbitmapsize)); 913ca3e8d88SDave Plauger 914ca3e8d88SDave Plauger /* find ranges that are not being dumped to use for buffers */ 915ca3e8d88SDave Plauger dump_init_memlist_walker(&mlw); 916ca3e8d88SDave Plauger for (bitnum = 0; bitnum < dumpcfg.bitmapsize; bitnum = end) { 917ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 918ca3e8d88SDave Plauger end = bitnum + CBUF_MAPNP; 919ca3e8d88SDave Plauger pfn = dump_bitnum_to_pfn(bitnum, &mlw); 920ca3e8d88SDave Plauger ASSERT(pfn != PFN_INVALID); 921ca3e8d88SDave Plauger 922ca3e8d88SDave Plauger /* skip partial range at end of mem segment */ 923ca3e8d88SDave Plauger if (mlw.mpleft < CBUF_MAPNP) { 924ca3e8d88SDave Plauger end = bitnum + mlw.mpleft; 925ca3e8d88SDave Plauger continue; 926ca3e8d88SDave Plauger } 927ca3e8d88SDave Plauger 928ca3e8d88SDave Plauger /* skip non aligned pages */ 929ca3e8d88SDave Plauger off = P2PHASE(pfn, CBUF_MAPNP); 930ca3e8d88SDave Plauger if (off != 0) { 931ca3e8d88SDave Plauger end -= off; 932ca3e8d88SDave Plauger continue; 933ca3e8d88SDave Plauger } 934ca3e8d88SDave Plauger 935ca3e8d88SDave Plauger if (!dump_range_check(bitnum, end, pfn)) 936ca3e8d88SDave Plauger continue; 937ca3e8d88SDave Plauger 938ca3e8d88SDave Plauger ASSERT((sz + CBUF_MAPSIZE) <= cfg->maxvmsize); 939ca3e8d88SDave Plauger hat_devload(kas.a_hat, cfg->maxvm + sz, CBUF_MAPSIZE, pfn, 940ca3e8d88SDave Plauger PROT_READ | PROT_WRITE, HAT_LOAD_NOCONSIST); 941ca3e8d88SDave Plauger sz += CBUF_MAPSIZE; 942ca3e8d88SDave Plauger cfg->found4m++; 943ca3e8d88SDave Plauger 944ca3e8d88SDave Plauger /* set the bitmap for both ends to be sure to cover the range */ 945ca3e8d88SDave Plauger dump_set_used(pfn); 946ca3e8d88SDave Plauger dump_set_used(pfn + CBUF_MAPNP - 1); 947ca3e8d88SDave Plauger 948ca3e8d88SDave Plauger if (sz >= cfg->maxsize) 949ca3e8d88SDave Plauger goto foundmax; 950ca3e8d88SDave Plauger } 951ca3e8d88SDave Plauger 952ca3e8d88SDave Plauger /* Add small pages if we can't find enough large pages. */ 953ca3e8d88SDave Plauger dump_init_memlist_walker(&mlw); 954ca3e8d88SDave Plauger for (bitnum = 0; bitnum < dumpcfg.bitmapsize; bitnum = end) { 955ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 956ca3e8d88SDave Plauger end = bitnum + CBUF_MAPNP; 957ca3e8d88SDave Plauger pfn = dump_bitnum_to_pfn(bitnum, &mlw); 958ca3e8d88SDave Plauger ASSERT(pfn != PFN_INVALID); 959ca3e8d88SDave Plauger 960ca3e8d88SDave Plauger /* Find any non-aligned pages at start and end of segment. */ 961ca3e8d88SDave Plauger off = P2PHASE(pfn, CBUF_MAPNP); 962ca3e8d88SDave Plauger if (mlw.mpleft < CBUF_MAPNP) { 963ca3e8d88SDave Plauger end = bitnum + mlw.mpleft; 964ca3e8d88SDave Plauger } else if (off != 0) { 965ca3e8d88SDave Plauger end -= off; 966ca3e8d88SDave Plauger } else if (cfg->found4m && dump_test_used(pfn)) { 967ca3e8d88SDave Plauger continue; 968ca3e8d88SDave Plauger } 969ca3e8d88SDave Plauger 970ca3e8d88SDave Plauger for (; bitnum < end; bitnum++, pfn++) { 971ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 972ca3e8d88SDave Plauger if (BT_TEST(dumpcfg.bitmap, bitnum)) 973ca3e8d88SDave Plauger continue; 974ca3e8d88SDave Plauger if (!dump_pfn_check(pfn)) 975ca3e8d88SDave Plauger continue; 976ca3e8d88SDave Plauger ASSERT((sz + PAGESIZE) <= cfg->maxvmsize); 977ca3e8d88SDave Plauger hat_devload(kas.a_hat, cfg->maxvm + sz, PAGESIZE, pfn, 978ca3e8d88SDave Plauger PROT_READ | PROT_WRITE, HAT_LOAD_NOCONSIST); 979ca3e8d88SDave Plauger sz += PAGESIZE; 980ca3e8d88SDave Plauger cfg->foundsm++; 981ca3e8d88SDave Plauger dump_set_used(pfn); 982ca3e8d88SDave Plauger if (sz >= cfg->maxsize) 983ca3e8d88SDave Plauger goto foundmax; 984ca3e8d88SDave Plauger } 985ca3e8d88SDave Plauger } 986ca3e8d88SDave Plauger 987ca3e8d88SDave Plauger /* Fall back to lzjb if we did not get enough memory for bzip2. */ 988ca3e8d88SDave Plauger endsz = (cfg->maxsize * cfg->threshold) / cfg->nhelper; 989ca3e8d88SDave Plauger if (sz < endsz) { 990ca3e8d88SDave Plauger cfg->clevel = DUMP_CLEVEL_LZJB; 991ca3e8d88SDave Plauger } 992ca3e8d88SDave Plauger 993ca3e8d88SDave Plauger /* Allocate memory for as many helpers as we can. */ 994ca3e8d88SDave Plauger foundmax: 995ca3e8d88SDave Plauger 996ca3e8d88SDave Plauger /* Byte offsets into memory found and mapped above */ 997ca3e8d88SDave Plauger endsz = sz; 998ca3e8d88SDave Plauger sz = 0; 999ca3e8d88SDave Plauger 1000ca3e8d88SDave Plauger /* Set the size for bzip2 state. Only bzip2 needs it. */ 1001ca3e8d88SDave Plauger bz2size = BZ2_bzCompressInitSize(dump_bzip2_level); 1002ca3e8d88SDave Plauger 1003ca3e8d88SDave Plauger /* Skip the preallocate output buffers. */ 1004ca3e8d88SDave Plauger cp = &cfg->cbuf[MINCBUFS]; 1005ca3e8d88SDave Plauger 1006ca3e8d88SDave Plauger /* Use this to move memory up from the preallocated helpers. */ 1007ca3e8d88SDave Plauger ohp = cfg->helper; 1008ca3e8d88SDave Plauger 1009ca3e8d88SDave Plauger /* Loop over all helpers and allocate memory. */ 1010ca3e8d88SDave Plauger for (hp = cfg->helper; hp < endhp; hp++) { 1011ca3e8d88SDave Plauger 1012ca3e8d88SDave Plauger /* Skip preallocated helpers by checking hp->page. */ 1013ca3e8d88SDave Plauger if (hp->page == NULL) { 1014ca3e8d88SDave Plauger if (cfg->clevel <= DUMP_CLEVEL_LZJB) { 1015ca3e8d88SDave Plauger /* lzjb needs 2 1-page buffers */ 1016ca3e8d88SDave Plauger if ((sz + (2 * PAGESIZE)) > endsz) 1017ca3e8d88SDave Plauger break; 1018ca3e8d88SDave Plauger hp->page = cfg->maxvm + sz; 1019ca3e8d88SDave Plauger sz += PAGESIZE; 1020ca3e8d88SDave Plauger hp->lzbuf = cfg->maxvm + sz; 1021ca3e8d88SDave Plauger sz += PAGESIZE; 1022ca3e8d88SDave Plauger 1023ca3e8d88SDave Plauger } else if (ohp->lzbuf != NULL) { 1024ca3e8d88SDave Plauger /* re-use the preallocted lzjb page for bzip2 */ 1025ca3e8d88SDave Plauger hp->page = ohp->lzbuf; 1026ca3e8d88SDave Plauger ohp->lzbuf = NULL; 1027ca3e8d88SDave Plauger ++ohp; 1028ca3e8d88SDave Plauger 1029ca3e8d88SDave Plauger } else { 1030ca3e8d88SDave Plauger /* bzip2 needs a 1-page buffer */ 1031ca3e8d88SDave Plauger if ((sz + PAGESIZE) > endsz) 1032ca3e8d88SDave Plauger break; 1033ca3e8d88SDave Plauger hp->page = cfg->maxvm + sz; 1034ca3e8d88SDave Plauger sz += PAGESIZE; 1035ca3e8d88SDave Plauger } 1036ca3e8d88SDave Plauger } 1037ca3e8d88SDave Plauger 1038ca3e8d88SDave Plauger /* 1039ca3e8d88SDave Plauger * Add output buffers per helper. The number of 1040ca3e8d88SDave Plauger * buffers per helper is determined by the ratio of 1041ca3e8d88SDave Plauger * ncbuf to nhelper. 1042ca3e8d88SDave Plauger */ 1043ca3e8d88SDave Plauger for (k = 0; cp < endcp && (sz + CBUF_SIZE) <= endsz && 1044ca3e8d88SDave Plauger k < NCBUF_PER_HELPER; k++) { 1045ca3e8d88SDave Plauger cp->state = CBUF_FREEBUF; 1046ca3e8d88SDave Plauger cp->size = CBUF_SIZE; 1047ca3e8d88SDave Plauger cp->buf = cfg->maxvm + sz; 1048ca3e8d88SDave Plauger sz += CBUF_SIZE; 1049ca3e8d88SDave Plauger ++cp; 1050ca3e8d88SDave Plauger } 1051ca3e8d88SDave Plauger 1052ca3e8d88SDave Plauger /* 1053ca3e8d88SDave Plauger * bzip2 needs compression state. Use the dumpbzalloc 1054ca3e8d88SDave Plauger * and dumpbzfree callbacks to allocate the memory. 1055ca3e8d88SDave Plauger * bzip2 does allocation only at init time. 1056ca3e8d88SDave Plauger */ 1057ca3e8d88SDave Plauger if (cfg->clevel >= DUMP_CLEVEL_BZIP2) { 1058ca3e8d88SDave Plauger if ((sz + bz2size) > endsz) { 1059ca3e8d88SDave Plauger hp->page = NULL; 1060ca3e8d88SDave Plauger break; 1061ca3e8d88SDave Plauger } else { 1062ca3e8d88SDave Plauger hp->bzstream.opaque = &sz; 1063ca3e8d88SDave Plauger hp->bzstream.bzalloc = dumpbzalloc; 1064ca3e8d88SDave Plauger hp->bzstream.bzfree = dumpbzfree; 1065ca3e8d88SDave Plauger (void) BZ2_bzCompressInit(&hp->bzstream, 1066ca3e8d88SDave Plauger dump_bzip2_level, 0, 0); 1067ca3e8d88SDave Plauger hp->bzstream.opaque = NULL; 1068ca3e8d88SDave Plauger } 1069ca3e8d88SDave Plauger } 1070ca3e8d88SDave Plauger } 1071ca3e8d88SDave Plauger 1072ca3e8d88SDave Plauger /* Finish allocating output buffers */ 1073ca3e8d88SDave Plauger for (; cp < endcp && (sz + CBUF_SIZE) <= endsz; cp++) { 1074ca3e8d88SDave Plauger cp->state = CBUF_FREEBUF; 1075ca3e8d88SDave Plauger cp->size = CBUF_SIZE; 1076ca3e8d88SDave Plauger cp->buf = cfg->maxvm + sz; 1077ca3e8d88SDave Plauger sz += CBUF_SIZE; 1078ca3e8d88SDave Plauger } 1079ca3e8d88SDave Plauger 1080ca3e8d88SDave Plauger /* Enable IS_DUMP_PAGE macro, which checks for pages we took. */ 1081ca3e8d88SDave Plauger if (cfg->found4m || cfg->foundsm) 1082ca3e8d88SDave Plauger dump_check_used = 1; 1083ca3e8d88SDave Plauger 1084ca3e8d88SDave Plauger ASSERT(sz <= endsz); 1085ca3e8d88SDave Plauger } 1086ca3e8d88SDave Plauger 10877c478bd9Sstevel@tonic-gate static void 10887c478bd9Sstevel@tonic-gate dumphdr_init(void) 10897c478bd9Sstevel@tonic-gate { 10907c478bd9Sstevel@tonic-gate pgcnt_t npages = 0; 10917c478bd9Sstevel@tonic-gate 10927c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&dump_lock)); 10937c478bd9Sstevel@tonic-gate 10947c478bd9Sstevel@tonic-gate if (dumphdr == NULL) { 10957c478bd9Sstevel@tonic-gate dumphdr = kmem_zalloc(sizeof (dumphdr_t), KM_SLEEP); 10967c478bd9Sstevel@tonic-gate dumphdr->dump_magic = DUMP_MAGIC; 10977c478bd9Sstevel@tonic-gate dumphdr->dump_version = DUMP_VERSION; 10987c478bd9Sstevel@tonic-gate dumphdr->dump_wordsize = DUMP_WORDSIZE; 10997c478bd9Sstevel@tonic-gate dumphdr->dump_pageshift = PAGESHIFT; 11007c478bd9Sstevel@tonic-gate dumphdr->dump_pagesize = PAGESIZE; 11017c478bd9Sstevel@tonic-gate dumphdr->dump_utsname = utsname; 11027c478bd9Sstevel@tonic-gate (void) strcpy(dumphdr->dump_platform, platform); 1103ca3e8d88SDave Plauger dumpbuf.size = dumpbuf_iosize(maxphys); 1104ca3e8d88SDave Plauger dumpbuf.start = kmem_alloc(dumpbuf.size, KM_SLEEP); 1105ca3e8d88SDave Plauger dumpbuf.end = dumpbuf.start + dumpbuf.size; 1106ca3e8d88SDave Plauger dumpcfg.pids = kmem_alloc(v.v_proc * sizeof (pid_t), KM_SLEEP); 1107ca3e8d88SDave Plauger dumpcfg.helpermap = kmem_zalloc(BT_SIZEOFMAP(NCPU), KM_SLEEP); 1108ca3e8d88SDave Plauger LOCK_INIT_HELD(&dumpcfg.helper_lock); 1109f6e214c7SGavin Maltby dump_stack_scratch = kmem_alloc(STACK_BUF_SIZE, KM_SLEEP); 1110f6e214c7SGavin Maltby (void) strncpy(dumphdr->dump_uuid, dump_get_uuid(), 1111f6e214c7SGavin Maltby sizeof (dumphdr->dump_uuid)); 11127c478bd9Sstevel@tonic-gate } 11137c478bd9Sstevel@tonic-gate 1114843e1988Sjohnlev npages = num_phys_pages(); 11157c478bd9Sstevel@tonic-gate 1116ca3e8d88SDave Plauger if (dumpcfg.bitmapsize != npages) { 1117ca3e8d88SDave Plauger size_t rlen = CBUF_MAPP2R(P2ROUNDUP(npages, CBUF_MAPNP)); 11187c478bd9Sstevel@tonic-gate void *map = kmem_alloc(BT_SIZEOFMAP(npages), KM_SLEEP); 1119ca3e8d88SDave Plauger void *rmap = kmem_alloc(BT_SIZEOFMAP(rlen), KM_SLEEP); 1120ca3e8d88SDave Plauger 1121ca3e8d88SDave Plauger if (dumpcfg.bitmap != NULL) 1122ca3e8d88SDave Plauger kmem_free(dumpcfg.bitmap, BT_SIZEOFMAP(dumpcfg. 1123ca3e8d88SDave Plauger bitmapsize)); 1124ca3e8d88SDave Plauger if (dumpcfg.rbitmap != NULL) 1125ca3e8d88SDave Plauger kmem_free(dumpcfg.rbitmap, BT_SIZEOFMAP(dumpcfg. 1126ca3e8d88SDave Plauger rbitmapsize)); 1127ca3e8d88SDave Plauger dumpcfg.bitmap = map; 1128ca3e8d88SDave Plauger dumpcfg.bitmapsize = npages; 1129ca3e8d88SDave Plauger dumpcfg.rbitmap = rmap; 1130ca3e8d88SDave Plauger dumpcfg.rbitmapsize = rlen; 11317c478bd9Sstevel@tonic-gate } 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate /* 11357c478bd9Sstevel@tonic-gate * Establish a new dump device. 11367c478bd9Sstevel@tonic-gate */ 11377c478bd9Sstevel@tonic-gate int 11387c478bd9Sstevel@tonic-gate dumpinit(vnode_t *vp, char *name, int justchecking) 11397c478bd9Sstevel@tonic-gate { 11407c478bd9Sstevel@tonic-gate vnode_t *cvp; 11417c478bd9Sstevel@tonic-gate vattr_t vattr; 11427c478bd9Sstevel@tonic-gate vnode_t *cdev_vp; 11437c478bd9Sstevel@tonic-gate int error = 0; 11447c478bd9Sstevel@tonic-gate 11457c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&dump_lock)); 11467c478bd9Sstevel@tonic-gate 11477c478bd9Sstevel@tonic-gate dumphdr_init(); 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate cvp = common_specvp(vp); 11507c478bd9Sstevel@tonic-gate if (cvp == dumpvp) 11517c478bd9Sstevel@tonic-gate return (0); 11527c478bd9Sstevel@tonic-gate 11537c478bd9Sstevel@tonic-gate /* 11547c478bd9Sstevel@tonic-gate * Determine whether this is a plausible dump device. We want either: 11557c478bd9Sstevel@tonic-gate * (1) a real device that's not mounted and has a cb_dump routine, or 11567c478bd9Sstevel@tonic-gate * (2) a swapfile on some filesystem that has a vop_dump routine. 11577c478bd9Sstevel@tonic-gate */ 1158da6c28aaSamw if ((error = VOP_OPEN(&cvp, FREAD | FWRITE, kcred, NULL)) != 0) 11597c478bd9Sstevel@tonic-gate return (error); 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate vattr.va_mask = AT_SIZE | AT_TYPE | AT_RDEV; 1162da6c28aaSamw if ((error = VOP_GETATTR(cvp, &vattr, 0, kcred, NULL)) == 0) { 11637c478bd9Sstevel@tonic-gate if (vattr.va_type == VBLK || vattr.va_type == VCHR) { 11647c478bd9Sstevel@tonic-gate if (devopsp[getmajor(vattr.va_rdev)]-> 11657c478bd9Sstevel@tonic-gate devo_cb_ops->cb_dump == nodev) 11667c478bd9Sstevel@tonic-gate error = ENOTSUP; 11677c478bd9Sstevel@tonic-gate else if (vfs_devismounted(vattr.va_rdev)) 11687c478bd9Sstevel@tonic-gate error = EBUSY; 1169681d9761SEric Taylor if (strcmp(ddi_driver_name(VTOS(cvp)->s_dip), 1170681d9761SEric Taylor ZFS_DRIVER) == 0 && 1171681d9761SEric Taylor IS_SWAPVP(common_specvp(cvp))) 1172681d9761SEric Taylor error = EBUSY; 11737c478bd9Sstevel@tonic-gate } else { 11747c478bd9Sstevel@tonic-gate if (vn_matchopval(cvp, VOPNAME_DUMP, fs_nosys) || 11757c478bd9Sstevel@tonic-gate !IS_SWAPVP(cvp)) 11767c478bd9Sstevel@tonic-gate error = ENOTSUP; 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate 11807c478bd9Sstevel@tonic-gate if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE) 11817c478bd9Sstevel@tonic-gate error = ENOSPC; 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate if (error || justchecking) { 1184da6c28aaSamw (void) VOP_CLOSE(cvp, FREAD | FWRITE, 1, (offset_t)0, 1185da6c28aaSamw kcred, NULL); 11867c478bd9Sstevel@tonic-gate return (error); 11877c478bd9Sstevel@tonic-gate } 11887c478bd9Sstevel@tonic-gate 11897c478bd9Sstevel@tonic-gate VN_HOLD(cvp); 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate if (dumpvp != NULL) 11927c478bd9Sstevel@tonic-gate dumpfini(); /* unconfigure the old dump device */ 11937c478bd9Sstevel@tonic-gate 11947c478bd9Sstevel@tonic-gate dumpvp = cvp; 11957c478bd9Sstevel@tonic-gate dumpvp_size = vattr.va_size & -DUMP_OFFSET; 11967c478bd9Sstevel@tonic-gate dumppath = kmem_alloc(strlen(name) + 1, KM_SLEEP); 11977c478bd9Sstevel@tonic-gate (void) strcpy(dumppath, name); 1198ca3e8d88SDave Plauger dumpbuf.iosize = 0; 11997c478bd9Sstevel@tonic-gate 12007c478bd9Sstevel@tonic-gate /* 12017c478bd9Sstevel@tonic-gate * If the dump device is a block device, attempt to open up the 12027c478bd9Sstevel@tonic-gate * corresponding character device and determine its maximum transfer 12037c478bd9Sstevel@tonic-gate * size. We use this information to potentially resize dumpbuf to a 12047c478bd9Sstevel@tonic-gate * larger and more optimal size for performing i/o to the dump device. 12057c478bd9Sstevel@tonic-gate */ 12067c478bd9Sstevel@tonic-gate if (cvp->v_type == VBLK && 12077c478bd9Sstevel@tonic-gate (cdev_vp = makespecvp(VTOS(cvp)->s_dev, VCHR)) != NULL) { 1208da6c28aaSamw if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) { 12097c478bd9Sstevel@tonic-gate size_t blk_size; 12107c478bd9Sstevel@tonic-gate struct dk_cinfo dki; 121165908c77Syu, larry liu - Sun Microsystems - Beijing China struct dk_minfo minf; 12127c478bd9Sstevel@tonic-gate 121365908c77Syu, larry liu - Sun Microsystems - Beijing China if (VOP_IOCTL(cdev_vp, DKIOCGMEDIAINFO, 121465908c77Syu, larry liu - Sun Microsystems - Beijing China (intptr_t)&minf, FKIOCTL, kcred, NULL, NULL) 121565908c77Syu, larry liu - Sun Microsystems - Beijing China == 0 && minf.dki_lbsize != 0) 121665908c77Syu, larry liu - Sun Microsystems - Beijing China blk_size = minf.dki_lbsize; 12177c478bd9Sstevel@tonic-gate else 12187c478bd9Sstevel@tonic-gate blk_size = DEV_BSIZE; 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate if (VOP_IOCTL(cdev_vp, DKIOCINFO, (intptr_t)&dki, 1221da6c28aaSamw FKIOCTL, kcred, NULL, NULL) == 0) { 1222ca3e8d88SDave Plauger dumpbuf.iosize = dki.dki_maxtransfer * blk_size; 12237c478bd9Sstevel@tonic-gate dumpbuf_resize(); 12247c478bd9Sstevel@tonic-gate } 1225e7cbe64fSgw /* 1226681d9761SEric Taylor * If we are working with a zvol then dumpify it 1227681d9761SEric Taylor * if it's not being used as swap. 1228e7cbe64fSgw */ 1229e7cbe64fSgw if (strcmp(dki.dki_dname, ZVOL_DRIVER) == 0) { 1230681d9761SEric Taylor if (IS_SWAPVP(common_specvp(cvp))) 1231681d9761SEric Taylor error = EBUSY; 1232681d9761SEric Taylor else if ((error = VOP_IOCTL(cdev_vp, 1233e7cbe64fSgw DKIOCDUMPINIT, NULL, FKIOCTL, kcred, 1234681d9761SEric Taylor NULL, NULL)) != 0) 1235e7cbe64fSgw dumpfini(); 1236e7cbe64fSgw } 12377c478bd9Sstevel@tonic-gate 1238da6c28aaSamw (void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0, 1239da6c28aaSamw kcred, NULL); 12407c478bd9Sstevel@tonic-gate } 12417c478bd9Sstevel@tonic-gate 12427c478bd9Sstevel@tonic-gate VN_RELE(cdev_vp); 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate 12457c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?dump on %s size %llu MB\n", name, dumpvp_size >> 20); 12467c478bd9Sstevel@tonic-gate 1247ca3e8d88SDave Plauger dump_update_clevel(); 1248ca3e8d88SDave Plauger 1249e7cbe64fSgw return (error); 12507c478bd9Sstevel@tonic-gate } 12517c478bd9Sstevel@tonic-gate 12527c478bd9Sstevel@tonic-gate void 12537c478bd9Sstevel@tonic-gate dumpfini(void) 12547c478bd9Sstevel@tonic-gate { 1255e7cbe64fSgw vattr_t vattr; 1256e7cbe64fSgw boolean_t is_zfs = B_FALSE; 1257e7cbe64fSgw vnode_t *cdev_vp; 12587c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&dump_lock)); 12597c478bd9Sstevel@tonic-gate 12607c478bd9Sstevel@tonic-gate kmem_free(dumppath, strlen(dumppath) + 1); 12617c478bd9Sstevel@tonic-gate 1262e7cbe64fSgw /* 1263e7cbe64fSgw * Determine if we are using zvols for our dump device 1264e7cbe64fSgw */ 1265e7cbe64fSgw vattr.va_mask = AT_RDEV; 1266e7cbe64fSgw if (VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL) == 0) { 1267e7cbe64fSgw is_zfs = (getmajor(vattr.va_rdev) == 1268e7cbe64fSgw ddi_name_to_major(ZFS_DRIVER)) ? B_TRUE : B_FALSE; 1269e7cbe64fSgw } 1270e7cbe64fSgw 1271e7cbe64fSgw /* 1272e7cbe64fSgw * If we have a zvol dump device then we call into zfs so 1273e7cbe64fSgw * that it may have a chance to cleanup. 1274e7cbe64fSgw */ 1275e7cbe64fSgw if (is_zfs && 1276e7cbe64fSgw (cdev_vp = makespecvp(VTOS(dumpvp)->s_dev, VCHR)) != NULL) { 1277e7cbe64fSgw if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) { 1278e7cbe64fSgw (void) VOP_IOCTL(cdev_vp, DKIOCDUMPFINI, NULL, FKIOCTL, 1279e7cbe64fSgw kcred, NULL, NULL); 1280e7cbe64fSgw (void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0, 1281e7cbe64fSgw kcred, NULL); 1282e7cbe64fSgw } 1283e7cbe64fSgw VN_RELE(cdev_vp); 1284e7cbe64fSgw } 1285e7cbe64fSgw 1286da6c28aaSamw (void) VOP_CLOSE(dumpvp, FREAD | FWRITE, 1, (offset_t)0, kcred, NULL); 12877c478bd9Sstevel@tonic-gate 12887c478bd9Sstevel@tonic-gate VN_RELE(dumpvp); 12897c478bd9Sstevel@tonic-gate 12907c478bd9Sstevel@tonic-gate dumpvp = NULL; 12917c478bd9Sstevel@tonic-gate dumpvp_size = 0; 12927c478bd9Sstevel@tonic-gate dumppath = NULL; 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate 1295ca3e8d88SDave Plauger static offset_t 1296ca3e8d88SDave Plauger dumpvp_flush(void) 12977c478bd9Sstevel@tonic-gate { 1298ca3e8d88SDave Plauger size_t size = P2ROUNDUP(dumpbuf.cur - dumpbuf.start, PAGESIZE); 1299ca3e8d88SDave Plauger hrtime_t iotime; 1300ca3e8d88SDave Plauger int err; 13017c478bd9Sstevel@tonic-gate 1302ca3e8d88SDave Plauger if (dumpbuf.vp_off + size > dumpbuf.vp_limit) { 1303ca3e8d88SDave Plauger dump_ioerr = ENOSPC; 1304ca3e8d88SDave Plauger dumpbuf.vp_off = dumpbuf.vp_limit; 1305ca3e8d88SDave Plauger } else if (size != 0) { 1306ca3e8d88SDave Plauger iotime = gethrtime(); 1307ca3e8d88SDave Plauger dumpsync.iowait += iotime - dumpsync.iowaitts; 1308ca3e8d88SDave Plauger if (panicstr) 1309ca3e8d88SDave Plauger err = VOP_DUMP(dumpvp, dumpbuf.start, 1310ca3e8d88SDave Plauger lbtodb(dumpbuf.vp_off), btod(size), NULL); 1311ca3e8d88SDave Plauger else 1312ca3e8d88SDave Plauger err = vn_rdwr(UIO_WRITE, dumpbuf.cdev_vp != NULL ? 1313ca3e8d88SDave Plauger dumpbuf.cdev_vp : dumpvp, dumpbuf.start, size, 1314ca3e8d88SDave Plauger dumpbuf.vp_off, UIO_SYSSPACE, 0, dumpbuf.vp_limit, 1315ca3e8d88SDave Plauger kcred, 0); 1316ca3e8d88SDave Plauger if (err && dump_ioerr == 0) 1317ca3e8d88SDave Plauger dump_ioerr = err; 1318ca3e8d88SDave Plauger dumpsync.iowaitts = gethrtime(); 1319ca3e8d88SDave Plauger dumpsync.iotime += dumpsync.iowaitts - iotime; 1320ca3e8d88SDave Plauger dumpsync.nwrite += size; 1321ca3e8d88SDave Plauger dumpbuf.vp_off += size; 13227c478bd9Sstevel@tonic-gate } 1323ca3e8d88SDave Plauger dumpbuf.cur = dumpbuf.start; 1324ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 1325ca3e8d88SDave Plauger return (dumpbuf.vp_off); 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate 1328ca3e8d88SDave Plauger /* maximize write speed by keeping seek offset aligned with size */ 1329ca3e8d88SDave Plauger void 1330ca3e8d88SDave Plauger dumpvp_write(const void *va, size_t size) 13317c478bd9Sstevel@tonic-gate { 1332ca3e8d88SDave Plauger size_t len, off, sz; 13337c478bd9Sstevel@tonic-gate 1334ca3e8d88SDave Plauger while (size != 0) { 1335ca3e8d88SDave Plauger len = MIN(size, dumpbuf.end - dumpbuf.cur); 1336ca3e8d88SDave Plauger if (len == 0) { 1337ca3e8d88SDave Plauger off = P2PHASE(dumpbuf.vp_off, dumpbuf.size); 1338ca3e8d88SDave Plauger if (off == 0 || !ISP2(dumpbuf.size)) { 1339ca3e8d88SDave Plauger (void) dumpvp_flush(); 1340ca3e8d88SDave Plauger } else { 1341ca3e8d88SDave Plauger sz = dumpbuf.size - off; 1342ca3e8d88SDave Plauger dumpbuf.cur = dumpbuf.start + sz; 1343ca3e8d88SDave Plauger (void) dumpvp_flush(); 1344ca3e8d88SDave Plauger ovbcopy(dumpbuf.start + sz, dumpbuf.start, off); 1345ca3e8d88SDave Plauger dumpbuf.cur += off; 1346ca3e8d88SDave Plauger } 1347ca3e8d88SDave Plauger } else { 1348ca3e8d88SDave Plauger bcopy(va, dumpbuf.cur, len); 1349ca3e8d88SDave Plauger va = (char *)va + len; 1350ca3e8d88SDave Plauger dumpbuf.cur += len; 1351ca3e8d88SDave Plauger size -= len; 1352ca3e8d88SDave Plauger } 13537c478bd9Sstevel@tonic-gate } 13547c478bd9Sstevel@tonic-gate } 13557c478bd9Sstevel@tonic-gate 1356ca3e8d88SDave Plauger /*ARGSUSED*/ 1357ca3e8d88SDave Plauger static void 1358ca3e8d88SDave Plauger dumpvp_ksyms_write(const void *src, void *dst, size_t size) 13597c478bd9Sstevel@tonic-gate { 1360ca3e8d88SDave Plauger dumpvp_write(src, size); 1361ca3e8d88SDave Plauger } 13627c478bd9Sstevel@tonic-gate 1363ca3e8d88SDave Plauger /* 1364ca3e8d88SDave Plauger * Mark 'pfn' in the bitmap and dump its translation table entry. 1365ca3e8d88SDave Plauger */ 1366ca3e8d88SDave Plauger void 1367ca3e8d88SDave Plauger dump_addpage(struct as *as, void *va, pfn_t pfn) 1368ca3e8d88SDave Plauger { 1369ca3e8d88SDave Plauger mem_vtop_t mem_vtop; 1370ca3e8d88SDave Plauger pgcnt_t bitnum; 1371ca3e8d88SDave Plauger 1372ca3e8d88SDave Plauger if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) { 1373ca3e8d88SDave Plauger if (!BT_TEST(dumpcfg.bitmap, bitnum)) { 1374ca3e8d88SDave Plauger dumphdr->dump_npages++; 1375ca3e8d88SDave Plauger BT_SET(dumpcfg.bitmap, bitnum); 1376ca3e8d88SDave Plauger } 1377ca3e8d88SDave Plauger dumphdr->dump_nvtop++; 1378ca3e8d88SDave Plauger mem_vtop.m_as = as; 1379ca3e8d88SDave Plauger mem_vtop.m_va = va; 1380ca3e8d88SDave Plauger mem_vtop.m_pfn = pfn; 1381ca3e8d88SDave Plauger dumpvp_write(&mem_vtop, sizeof (mem_vtop_t)); 1382ca3e8d88SDave Plauger } 1383ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 1384ca3e8d88SDave Plauger } 1385ca3e8d88SDave Plauger 1386ca3e8d88SDave Plauger /* 1387ca3e8d88SDave Plauger * Mark 'pfn' in the bitmap 1388ca3e8d88SDave Plauger */ 1389ca3e8d88SDave Plauger void 1390ca3e8d88SDave Plauger dump_page(pfn_t pfn) 1391ca3e8d88SDave Plauger { 1392ca3e8d88SDave Plauger pgcnt_t bitnum; 1393ca3e8d88SDave Plauger 1394ca3e8d88SDave Plauger if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) { 1395ca3e8d88SDave Plauger if (!BT_TEST(dumpcfg.bitmap, bitnum)) { 1396ca3e8d88SDave Plauger dumphdr->dump_npages++; 1397ca3e8d88SDave Plauger BT_SET(dumpcfg.bitmap, bitnum); 1398ca3e8d88SDave Plauger } 1399ca3e8d88SDave Plauger } 1400ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 1401ca3e8d88SDave Plauger } 1402ca3e8d88SDave Plauger 1403ca3e8d88SDave Plauger /* 1404ca3e8d88SDave Plauger * Dump the <as, va, pfn> information for a given address space. 1405ca3e8d88SDave Plauger * SEGOP_DUMP() will call dump_addpage() for each page in the segment. 1406ca3e8d88SDave Plauger */ 1407ca3e8d88SDave Plauger static void 1408ca3e8d88SDave Plauger dump_as(struct as *as) 1409ca3e8d88SDave Plauger { 1410ca3e8d88SDave Plauger struct seg *seg; 1411ca3e8d88SDave Plauger 1412dc32d872SJosef 'Jeff' Sipek AS_LOCK_ENTER(as, RW_READER); 1413ca3e8d88SDave Plauger for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) { 1414ca3e8d88SDave Plauger if (seg->s_as != as) 1415ca3e8d88SDave Plauger break; 1416ca3e8d88SDave Plauger if (seg->s_ops == NULL) 1417ca3e8d88SDave Plauger continue; 1418ca3e8d88SDave Plauger SEGOP_DUMP(seg); 1419ca3e8d88SDave Plauger } 1420dc32d872SJosef 'Jeff' Sipek AS_LOCK_EXIT(as); 1421ca3e8d88SDave Plauger 1422ca3e8d88SDave Plauger if (seg != NULL) 1423ca3e8d88SDave Plauger cmn_err(CE_WARN, "invalid segment %p in address space %p", 1424ca3e8d88SDave Plauger (void *)seg, (void *)as); 1425ca3e8d88SDave Plauger } 1426ca3e8d88SDave Plauger 1427ca3e8d88SDave Plauger static int 1428ca3e8d88SDave Plauger dump_process(pid_t pid) 1429ca3e8d88SDave Plauger { 1430ca3e8d88SDave Plauger proc_t *p = sprlock(pid); 1431ca3e8d88SDave Plauger 1432ca3e8d88SDave Plauger if (p == NULL) 1433ca3e8d88SDave Plauger return (-1); 1434ca3e8d88SDave Plauger if (p->p_as != &kas) { 1435ca3e8d88SDave Plauger mutex_exit(&p->p_lock); 1436ca3e8d88SDave Plauger dump_as(p->p_as); 1437ca3e8d88SDave Plauger mutex_enter(&p->p_lock); 1438ca3e8d88SDave Plauger } 1439ca3e8d88SDave Plauger 1440ca3e8d88SDave Plauger sprunlock(p); 1441ca3e8d88SDave Plauger 1442ca3e8d88SDave Plauger return (0); 1443ca3e8d88SDave Plauger } 1444ca3e8d88SDave Plauger 1445f6e214c7SGavin Maltby /* 1446f6e214c7SGavin Maltby * The following functions (dump_summary(), dump_ereports(), and 1447f6e214c7SGavin Maltby * dump_messages()), write data to an uncompressed area within the 1448f6e214c7SGavin Maltby * crashdump. The layout of these is 1449f6e214c7SGavin Maltby * 1450f6e214c7SGavin Maltby * +------------------------------------------------------------+ 1451f6e214c7SGavin Maltby * | compressed pages | summary | ereports | messages | 1452f6e214c7SGavin Maltby * +------------------------------------------------------------+ 1453f6e214c7SGavin Maltby * 1454f6e214c7SGavin Maltby * With the advent of saving a compressed crash dump by default, we 1455f6e214c7SGavin Maltby * need to save a little more data to describe the failure mode in 1456f6e214c7SGavin Maltby * an uncompressed buffer available before savecore uncompresses 1457f6e214c7SGavin Maltby * the dump. Initially this is a copy of the stack trace. Additional 1458f6e214c7SGavin Maltby * summary information should be added here. 1459f6e214c7SGavin Maltby */ 1460f6e214c7SGavin Maltby 1461f6e214c7SGavin Maltby void 1462f6e214c7SGavin Maltby dump_summary(void) 1463f6e214c7SGavin Maltby { 1464f6e214c7SGavin Maltby u_offset_t dumpvp_start; 1465f6e214c7SGavin Maltby summary_dump_t sd; 1466f6e214c7SGavin Maltby 1467f6e214c7SGavin Maltby if (dumpvp == NULL || dumphdr == NULL) 1468f6e214c7SGavin Maltby return; 1469f6e214c7SGavin Maltby 1470f6e214c7SGavin Maltby dumpbuf.cur = dumpbuf.start; 1471f6e214c7SGavin Maltby 1472f6e214c7SGavin Maltby dumpbuf.vp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE + 1473f6e214c7SGavin Maltby DUMP_ERPTSIZE); 1474f6e214c7SGavin Maltby dumpvp_start = dumpbuf.vp_limit - DUMP_SUMMARYSIZE; 1475f6e214c7SGavin Maltby dumpbuf.vp_off = dumpvp_start; 1476f6e214c7SGavin Maltby 1477f6e214c7SGavin Maltby sd.sd_magic = SUMMARY_MAGIC; 1478f6e214c7SGavin Maltby sd.sd_ssum = checksum32(dump_stack_scratch, STACK_BUF_SIZE); 1479f6e214c7SGavin Maltby dumpvp_write(&sd, sizeof (sd)); 1480f6e214c7SGavin Maltby dumpvp_write(dump_stack_scratch, STACK_BUF_SIZE); 1481f6e214c7SGavin Maltby 1482f6e214c7SGavin Maltby sd.sd_magic = 0; /* indicate end of summary */ 1483f6e214c7SGavin Maltby dumpvp_write(&sd, sizeof (sd)); 1484f6e214c7SGavin Maltby (void) dumpvp_flush(); 1485f6e214c7SGavin Maltby } 1486f6e214c7SGavin Maltby 1487ca3e8d88SDave Plauger void 1488ca3e8d88SDave Plauger dump_ereports(void) 1489ca3e8d88SDave Plauger { 1490ca3e8d88SDave Plauger u_offset_t dumpvp_start; 1491ca3e8d88SDave Plauger erpt_dump_t ed; 1492ca3e8d88SDave Plauger 1493ca3e8d88SDave Plauger if (dumpvp == NULL || dumphdr == NULL) 1494ca3e8d88SDave Plauger return; 1495ca3e8d88SDave Plauger 1496ca3e8d88SDave Plauger dumpbuf.cur = dumpbuf.start; 1497ca3e8d88SDave Plauger dumpbuf.vp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE); 1498ca3e8d88SDave Plauger dumpvp_start = dumpbuf.vp_limit - DUMP_ERPTSIZE; 1499ca3e8d88SDave Plauger dumpbuf.vp_off = dumpvp_start; 1500ca3e8d88SDave Plauger 1501ca3e8d88SDave Plauger fm_ereport_dump(); 1502ca3e8d88SDave Plauger if (panicstr) 1503ca3e8d88SDave Plauger errorq_dump(); 1504ca3e8d88SDave Plauger 1505ca3e8d88SDave Plauger bzero(&ed, sizeof (ed)); /* indicate end of ereports */ 1506ca3e8d88SDave Plauger dumpvp_write(&ed, sizeof (ed)); 1507ca3e8d88SDave Plauger (void) dumpvp_flush(); 1508ca3e8d88SDave Plauger 1509ca3e8d88SDave Plauger if (!panicstr) { 1510ca3e8d88SDave Plauger (void) VOP_PUTPAGE(dumpvp, dumpvp_start, 1511ca3e8d88SDave Plauger (size_t)(dumpbuf.vp_off - dumpvp_start), 1512ca3e8d88SDave Plauger B_INVAL | B_FORCE, kcred, NULL); 1513ca3e8d88SDave Plauger } 1514ca3e8d88SDave Plauger } 1515ca3e8d88SDave Plauger 1516ca3e8d88SDave Plauger void 1517ca3e8d88SDave Plauger dump_messages(void) 1518ca3e8d88SDave Plauger { 1519ca3e8d88SDave Plauger log_dump_t ld; 1520ca3e8d88SDave Plauger mblk_t *mctl, *mdata; 1521ca3e8d88SDave Plauger queue_t *q, *qlast; 1522ca3e8d88SDave Plauger u_offset_t dumpvp_start; 1523ca3e8d88SDave Plauger 1524ca3e8d88SDave Plauger if (dumpvp == NULL || dumphdr == NULL || log_consq == NULL) 1525ca3e8d88SDave Plauger return; 1526ca3e8d88SDave Plauger 1527ca3e8d88SDave Plauger dumpbuf.cur = dumpbuf.start; 1528ca3e8d88SDave Plauger dumpbuf.vp_limit = dumpvp_size - DUMP_OFFSET; 1529ca3e8d88SDave Plauger dumpvp_start = dumpbuf.vp_limit - DUMP_LOGSIZE; 1530ca3e8d88SDave Plauger dumpbuf.vp_off = dumpvp_start; 1531ca3e8d88SDave Plauger 1532ca3e8d88SDave Plauger qlast = NULL; 1533ca3e8d88SDave Plauger do { 1534ca3e8d88SDave Plauger for (q = log_consq; q->q_next != qlast; q = q->q_next) 1535ca3e8d88SDave Plauger continue; 1536ca3e8d88SDave Plauger for (mctl = q->q_first; mctl != NULL; mctl = mctl->b_next) { 1537ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 1538ca3e8d88SDave Plauger mdata = mctl->b_cont; 1539ca3e8d88SDave Plauger ld.ld_magic = LOG_MAGIC; 1540ca3e8d88SDave Plauger ld.ld_msgsize = MBLKL(mctl->b_cont); 1541ca3e8d88SDave Plauger ld.ld_csum = checksum32(mctl->b_rptr, MBLKL(mctl)); 1542ca3e8d88SDave Plauger ld.ld_msum = checksum32(mdata->b_rptr, MBLKL(mdata)); 1543ca3e8d88SDave Plauger dumpvp_write(&ld, sizeof (ld)); 1544ca3e8d88SDave Plauger dumpvp_write(mctl->b_rptr, MBLKL(mctl)); 1545ca3e8d88SDave Plauger dumpvp_write(mdata->b_rptr, MBLKL(mdata)); 1546ca3e8d88SDave Plauger } 1547ca3e8d88SDave Plauger } while ((qlast = q) != log_consq); 1548ca3e8d88SDave Plauger 1549ca3e8d88SDave Plauger ld.ld_magic = 0; /* indicate end of messages */ 1550ca3e8d88SDave Plauger dumpvp_write(&ld, sizeof (ld)); 1551ca3e8d88SDave Plauger (void) dumpvp_flush(); 1552ca3e8d88SDave Plauger if (!panicstr) { 1553ca3e8d88SDave Plauger (void) VOP_PUTPAGE(dumpvp, dumpvp_start, 1554ca3e8d88SDave Plauger (size_t)(dumpbuf.vp_off - dumpvp_start), 1555ca3e8d88SDave Plauger B_INVAL | B_FORCE, kcred, NULL); 1556ca3e8d88SDave Plauger } 1557ca3e8d88SDave Plauger } 1558ca3e8d88SDave Plauger 1559ca3e8d88SDave Plauger /* 1560ca3e8d88SDave Plauger * The following functions are called on multiple CPUs during dump. 1561ca3e8d88SDave Plauger * They must not use most kernel services, because all cross-calls are 1562ca3e8d88SDave Plauger * disabled during panic. Therefore, blocking locks and cache flushes 1563ca3e8d88SDave Plauger * will not work. 1564ca3e8d88SDave Plauger */ 1565ca3e8d88SDave Plauger 15669dd77bc8SDave Plauger /* 15679dd77bc8SDave Plauger * Copy pages, trapping ECC errors. Also, for robustness, trap data 15689dd77bc8SDave Plauger * access in case something goes wrong in the hat layer and the 15699dd77bc8SDave Plauger * mapping is broken. 15709dd77bc8SDave Plauger */ 1571ca3e8d88SDave Plauger static int 1572ca3e8d88SDave Plauger dump_pagecopy(void *src, void *dst) 1573ca3e8d88SDave Plauger { 1574ca3e8d88SDave Plauger long *wsrc = (long *)src; 1575ca3e8d88SDave Plauger long *wdst = (long *)dst; 1576ca3e8d88SDave Plauger const ulong_t ncopies = PAGESIZE / sizeof (long); 1577ca3e8d88SDave Plauger volatile int w = 0; 1578ca3e8d88SDave Plauger volatile int ueoff = -1; 1579ca3e8d88SDave Plauger on_trap_data_t otd; 1580ca3e8d88SDave Plauger 15819dd77bc8SDave Plauger if (on_trap(&otd, OT_DATA_EC | OT_DATA_ACCESS)) { 1582ca3e8d88SDave Plauger if (ueoff == -1) 1583ca3e8d88SDave Plauger ueoff = w * sizeof (long); 15849dd77bc8SDave Plauger /* report "bad ECC" or "bad address" */ 1585ca3e8d88SDave Plauger #ifdef _LP64 15869dd77bc8SDave Plauger if (otd.ot_trap & OT_DATA_EC) 15879dd77bc8SDave Plauger wdst[w++] = 0x00badecc00badecc; 15889dd77bc8SDave Plauger else 15899dd77bc8SDave Plauger wdst[w++] = 0x00badadd00badadd; 1590ca3e8d88SDave Plauger #else 15919dd77bc8SDave Plauger if (otd.ot_trap & OT_DATA_EC) 15929dd77bc8SDave Plauger wdst[w++] = 0x00badecc; 15939dd77bc8SDave Plauger else 15949dd77bc8SDave Plauger wdst[w++] = 0x00badadd; 1595ca3e8d88SDave Plauger #endif 1596ca3e8d88SDave Plauger } 1597ca3e8d88SDave Plauger while (w < ncopies) { 1598ca3e8d88SDave Plauger wdst[w] = wsrc[w]; 1599ca3e8d88SDave Plauger w++; 1600ca3e8d88SDave Plauger } 1601ca3e8d88SDave Plauger no_trap(); 1602ca3e8d88SDave Plauger return (ueoff); 1603ca3e8d88SDave Plauger } 1604ca3e8d88SDave Plauger 1605ca3e8d88SDave Plauger static void 1606ca3e8d88SDave Plauger dumpsys_close_cq(cqueue_t *cq, int live) 1607ca3e8d88SDave Plauger { 1608ca3e8d88SDave Plauger if (live) { 1609ca3e8d88SDave Plauger mutex_enter(&cq->mutex); 1610ca3e8d88SDave Plauger atomic_dec_uint(&cq->open); 1611ca3e8d88SDave Plauger cv_signal(&cq->cv); 1612ca3e8d88SDave Plauger mutex_exit(&cq->mutex); 1613ca3e8d88SDave Plauger } else { 1614ca3e8d88SDave Plauger atomic_dec_uint(&cq->open); 1615ca3e8d88SDave Plauger } 1616ca3e8d88SDave Plauger } 1617ca3e8d88SDave Plauger 1618ca3e8d88SDave Plauger static inline void 1619ca3e8d88SDave Plauger dumpsys_spinlock(lock_t *lp) 1620ca3e8d88SDave Plauger { 1621ca3e8d88SDave Plauger uint_t backoff = 0; 1622ca3e8d88SDave Plauger int loop_count = 0; 1623ca3e8d88SDave Plauger 1624ca3e8d88SDave Plauger while (LOCK_HELD(lp) || !lock_spin_try(lp)) { 1625ca3e8d88SDave Plauger if (++loop_count >= ncpus) { 1626ca3e8d88SDave Plauger backoff = mutex_lock_backoff(0); 1627ca3e8d88SDave Plauger loop_count = 0; 1628ca3e8d88SDave Plauger } else { 1629ca3e8d88SDave Plauger backoff = mutex_lock_backoff(backoff); 1630ca3e8d88SDave Plauger } 1631ca3e8d88SDave Plauger mutex_lock_delay(backoff); 1632ca3e8d88SDave Plauger } 1633ca3e8d88SDave Plauger } 1634ca3e8d88SDave Plauger 1635ca3e8d88SDave Plauger static inline void 1636ca3e8d88SDave Plauger dumpsys_spinunlock(lock_t *lp) 1637ca3e8d88SDave Plauger { 1638ca3e8d88SDave Plauger lock_clear(lp); 1639ca3e8d88SDave Plauger } 1640ca3e8d88SDave Plauger 1641ca3e8d88SDave Plauger static inline void 1642ca3e8d88SDave Plauger dumpsys_lock(cqueue_t *cq, int live) 1643ca3e8d88SDave Plauger { 1644ca3e8d88SDave Plauger if (live) 1645ca3e8d88SDave Plauger mutex_enter(&cq->mutex); 1646ca3e8d88SDave Plauger else 1647ca3e8d88SDave Plauger dumpsys_spinlock(&cq->spinlock); 1648ca3e8d88SDave Plauger } 1649ca3e8d88SDave Plauger 1650ca3e8d88SDave Plauger static inline void 1651ca3e8d88SDave Plauger dumpsys_unlock(cqueue_t *cq, int live, int signal) 1652ca3e8d88SDave Plauger { 1653ca3e8d88SDave Plauger if (live) { 1654ca3e8d88SDave Plauger if (signal) 1655ca3e8d88SDave Plauger cv_signal(&cq->cv); 1656ca3e8d88SDave Plauger mutex_exit(&cq->mutex); 1657ca3e8d88SDave Plauger } else { 1658ca3e8d88SDave Plauger dumpsys_spinunlock(&cq->spinlock); 1659ca3e8d88SDave Plauger } 1660ca3e8d88SDave Plauger } 1661ca3e8d88SDave Plauger 1662ca3e8d88SDave Plauger static void 1663ca3e8d88SDave Plauger dumpsys_wait_cq(cqueue_t *cq, int live) 1664ca3e8d88SDave Plauger { 1665ca3e8d88SDave Plauger if (live) { 1666ca3e8d88SDave Plauger cv_wait(&cq->cv, &cq->mutex); 1667ca3e8d88SDave Plauger } else { 1668ca3e8d88SDave Plauger dumpsys_spinunlock(&cq->spinlock); 1669ca3e8d88SDave Plauger while (cq->open) 1670ca3e8d88SDave Plauger if (cq->first) 1671ca3e8d88SDave Plauger break; 1672ca3e8d88SDave Plauger dumpsys_spinlock(&cq->spinlock); 1673ca3e8d88SDave Plauger } 1674ca3e8d88SDave Plauger } 1675ca3e8d88SDave Plauger 1676ca3e8d88SDave Plauger static void 1677ca3e8d88SDave Plauger dumpsys_put_cq(cqueue_t *cq, cbuf_t *cp, int newstate, int live) 1678ca3e8d88SDave Plauger { 1679ca3e8d88SDave Plauger if (cp == NULL) 1680ca3e8d88SDave Plauger return; 1681ca3e8d88SDave Plauger 1682ca3e8d88SDave Plauger dumpsys_lock(cq, live); 1683ca3e8d88SDave Plauger 1684ca3e8d88SDave Plauger if (cq->ts != 0) { 1685ca3e8d88SDave Plauger cq->empty += gethrtime() - cq->ts; 1686ca3e8d88SDave Plauger cq->ts = 0; 1687ca3e8d88SDave Plauger } 1688ca3e8d88SDave Plauger 1689ca3e8d88SDave Plauger cp->state = newstate; 1690ca3e8d88SDave Plauger cp->next = NULL; 1691ca3e8d88SDave Plauger if (cq->last == NULL) 1692ca3e8d88SDave Plauger cq->first = cp; 1693ca3e8d88SDave Plauger else 1694ca3e8d88SDave Plauger cq->last->next = cp; 1695ca3e8d88SDave Plauger cq->last = cp; 1696ca3e8d88SDave Plauger 1697ca3e8d88SDave Plauger dumpsys_unlock(cq, live, 1); 1698ca3e8d88SDave Plauger } 1699ca3e8d88SDave Plauger 1700ca3e8d88SDave Plauger static cbuf_t * 1701ca3e8d88SDave Plauger dumpsys_get_cq(cqueue_t *cq, int live) 1702ca3e8d88SDave Plauger { 1703ca3e8d88SDave Plauger cbuf_t *cp; 1704ca3e8d88SDave Plauger hrtime_t now = gethrtime(); 1705ca3e8d88SDave Plauger 1706ca3e8d88SDave Plauger dumpsys_lock(cq, live); 1707ca3e8d88SDave Plauger 1708ca3e8d88SDave Plauger /* CONSTCOND */ 1709ca3e8d88SDave Plauger while (1) { 1710ca3e8d88SDave Plauger cp = (cbuf_t *)cq->first; 1711ca3e8d88SDave Plauger if (cp == NULL) { 1712ca3e8d88SDave Plauger if (cq->open == 0) 1713ca3e8d88SDave Plauger break; 1714ca3e8d88SDave Plauger dumpsys_wait_cq(cq, live); 1715ca3e8d88SDave Plauger continue; 1716ca3e8d88SDave Plauger } 1717ca3e8d88SDave Plauger cq->first = cp->next; 1718ca3e8d88SDave Plauger if (cq->first == NULL) { 1719ca3e8d88SDave Plauger cq->last = NULL; 1720ca3e8d88SDave Plauger cq->ts = now; 1721ca3e8d88SDave Plauger } 1722ca3e8d88SDave Plauger break; 1723ca3e8d88SDave Plauger } 1724ca3e8d88SDave Plauger 1725ca3e8d88SDave Plauger dumpsys_unlock(cq, live, cq->first != NULL || cq->open == 0); 1726ca3e8d88SDave Plauger return (cp); 1727ca3e8d88SDave Plauger } 1728ca3e8d88SDave Plauger 1729ca3e8d88SDave Plauger /* 1730ca3e8d88SDave Plauger * Send an error message to the console. If the main task is running 1731ca3e8d88SDave Plauger * just write the message via uprintf. If a helper is running the 1732ca3e8d88SDave Plauger * message has to be put on a queue for the main task. Setting fmt to 1733ca3e8d88SDave Plauger * NULL means flush the error message buffer. If fmt is not NULL, just 1734ca3e8d88SDave Plauger * add the text to the existing buffer. 1735ca3e8d88SDave Plauger */ 1736ca3e8d88SDave Plauger static void 1737ca3e8d88SDave Plauger dumpsys_errmsg(helper_t *hp, const char *fmt, ...) 1738ca3e8d88SDave Plauger { 1739ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 1740ca3e8d88SDave Plauger cbuf_t *cp = hp->cperr; 1741ca3e8d88SDave Plauger va_list adx; 1742ca3e8d88SDave Plauger 1743ca3e8d88SDave Plauger if (hp->helper == MAINHELPER) { 1744ca3e8d88SDave Plauger if (fmt != NULL) { 1745ca3e8d88SDave Plauger if (ds->neednl) { 1746ca3e8d88SDave Plauger uprintf("\n"); 1747ca3e8d88SDave Plauger ds->neednl = 0; 1748ca3e8d88SDave Plauger } 1749ca3e8d88SDave Plauger va_start(adx, fmt); 1750ca3e8d88SDave Plauger vuprintf(fmt, adx); 1751ca3e8d88SDave Plauger va_end(adx); 1752ca3e8d88SDave Plauger } 1753ca3e8d88SDave Plauger } else if (fmt == NULL) { 1754ca3e8d88SDave Plauger if (cp != NULL) { 1755ca3e8d88SDave Plauger CQ_PUT(mainq, cp, CBUF_ERRMSG); 1756ca3e8d88SDave Plauger hp->cperr = NULL; 1757ca3e8d88SDave Plauger } 1758ca3e8d88SDave Plauger } else { 1759ca3e8d88SDave Plauger if (hp->cperr == NULL) { 1760ca3e8d88SDave Plauger cp = CQ_GET(freebufq); 1761ca3e8d88SDave Plauger hp->cperr = cp; 1762ca3e8d88SDave Plauger cp->used = 0; 1763ca3e8d88SDave Plauger } 1764ca3e8d88SDave Plauger va_start(adx, fmt); 1765ca3e8d88SDave Plauger cp->used += vsnprintf(cp->buf + cp->used, cp->size - cp->used, 1766ca3e8d88SDave Plauger fmt, adx); 1767ca3e8d88SDave Plauger va_end(adx); 1768ca3e8d88SDave Plauger if ((cp->used + LOG_MSGSIZE) > cp->size) { 1769ca3e8d88SDave Plauger CQ_PUT(mainq, cp, CBUF_ERRMSG); 1770ca3e8d88SDave Plauger hp->cperr = NULL; 1771ca3e8d88SDave Plauger } 1772ca3e8d88SDave Plauger } 1773ca3e8d88SDave Plauger } 1774ca3e8d88SDave Plauger 1775ca3e8d88SDave Plauger /* 1776ca3e8d88SDave Plauger * Write an output buffer to the dump file. If the main task is 1777ca3e8d88SDave Plauger * running just write the data. If a helper is running the output is 1778ca3e8d88SDave Plauger * placed on a queue for the main task. 1779ca3e8d88SDave Plauger */ 1780ca3e8d88SDave Plauger static void 1781ca3e8d88SDave Plauger dumpsys_swrite(helper_t *hp, cbuf_t *cp, size_t used) 1782ca3e8d88SDave Plauger { 1783ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 1784ca3e8d88SDave Plauger 1785ca3e8d88SDave Plauger if (hp->helper == MAINHELPER) { 1786ca3e8d88SDave Plauger HRSTART(ds->perpage, write); 1787ca3e8d88SDave Plauger dumpvp_write(cp->buf, used); 1788ca3e8d88SDave Plauger HRSTOP(ds->perpage, write); 1789ca3e8d88SDave Plauger CQ_PUT(freebufq, cp, CBUF_FREEBUF); 1790ca3e8d88SDave Plauger } else { 1791ca3e8d88SDave Plauger cp->used = used; 1792ca3e8d88SDave Plauger CQ_PUT(mainq, cp, CBUF_WRITE); 1793ca3e8d88SDave Plauger } 1794ca3e8d88SDave Plauger } 1795ca3e8d88SDave Plauger 1796ca3e8d88SDave Plauger /* 1797ca3e8d88SDave Plauger * Copy one page within the mapped range. The offset starts at 0 and 1798ca3e8d88SDave Plauger * is relative to the first pfn. cp->buf + cp->off is the address of 1799ca3e8d88SDave Plauger * the first pfn. If dump_pagecopy returns a UE offset, create an 1800ca3e8d88SDave Plauger * error message. Returns the offset to the next pfn in the range 1801ca3e8d88SDave Plauger * selected by the bitmap. 1802ca3e8d88SDave Plauger */ 1803ca3e8d88SDave Plauger static int 1804ca3e8d88SDave Plauger dumpsys_copy_page(helper_t *hp, int offset) 1805ca3e8d88SDave Plauger { 1806ca3e8d88SDave Plauger cbuf_t *cp = hp->cpin; 1807ca3e8d88SDave Plauger int ueoff; 1808ca3e8d88SDave Plauger 1809ca3e8d88SDave Plauger ASSERT(cp->off + offset + PAGESIZE <= cp->size); 1810ca3e8d88SDave Plauger ASSERT(BT_TEST(dumpcfg.bitmap, cp->bitnum)); 1811ca3e8d88SDave Plauger 1812ca3e8d88SDave Plauger ueoff = dump_pagecopy(cp->buf + cp->off + offset, hp->page); 1813ca3e8d88SDave Plauger 1814ca3e8d88SDave Plauger /* ueoff is the offset in the page to a UE error */ 1815ca3e8d88SDave Plauger if (ueoff != -1) { 1816ca3e8d88SDave Plauger uint64_t pa = ptob(cp->pfn) + offset + ueoff; 1817ca3e8d88SDave Plauger 18189dd77bc8SDave Plauger dumpsys_errmsg(hp, "cpu %d: memory error at PA 0x%08x.%08x\n", 18199dd77bc8SDave Plauger CPU->cpu_id, (uint32_t)(pa >> 32), (uint32_t)pa); 1820ca3e8d88SDave Plauger } 1821ca3e8d88SDave Plauger 1822ca3e8d88SDave Plauger /* 1823ca3e8d88SDave Plauger * Advance bitnum and offset to the next input page for the 1824ca3e8d88SDave Plauger * next call to this function. 1825ca3e8d88SDave Plauger */ 1826ca3e8d88SDave Plauger offset += PAGESIZE; 1827ca3e8d88SDave Plauger cp->bitnum++; 1828ca3e8d88SDave Plauger while (cp->off + offset < cp->size) { 1829ca3e8d88SDave Plauger if (BT_TEST(dumpcfg.bitmap, cp->bitnum)) 1830ca3e8d88SDave Plauger break; 1831ca3e8d88SDave Plauger offset += PAGESIZE; 1832ca3e8d88SDave Plauger cp->bitnum++; 1833ca3e8d88SDave Plauger } 1834ca3e8d88SDave Plauger 1835ca3e8d88SDave Plauger return (offset); 1836ca3e8d88SDave Plauger } 1837ca3e8d88SDave Plauger 1838ca3e8d88SDave Plauger /* 1839ca3e8d88SDave Plauger * Read the helper queue, and copy one mapped page. Return 0 when 1840ca3e8d88SDave Plauger * done. Return 1 when a page has been copied into hp->page. 1841ca3e8d88SDave Plauger */ 1842ca3e8d88SDave Plauger static int 1843ca3e8d88SDave Plauger dumpsys_sread(helper_t *hp) 1844ca3e8d88SDave Plauger { 1845ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 1846ca3e8d88SDave Plauger 1847ca3e8d88SDave Plauger /* CONSTCOND */ 1848ca3e8d88SDave Plauger while (1) { 1849ca3e8d88SDave Plauger 1850ca3e8d88SDave Plauger /* Find the next input buffer. */ 1851ca3e8d88SDave Plauger if (hp->cpin == NULL) { 1852ca3e8d88SDave Plauger HRSTART(hp->perpage, inwait); 1853ca3e8d88SDave Plauger 1854ca3e8d88SDave Plauger /* CONSTCOND */ 1855ca3e8d88SDave Plauger while (1) { 1856ca3e8d88SDave Plauger hp->cpin = CQ_GET(helperq); 1857ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 1858ca3e8d88SDave Plauger 1859ca3e8d88SDave Plauger /* 1860ca3e8d88SDave Plauger * NULL return means the helper queue 1861ca3e8d88SDave Plauger * is closed and empty. 1862ca3e8d88SDave Plauger */ 1863ca3e8d88SDave Plauger if (hp->cpin == NULL) 1864ca3e8d88SDave Plauger break; 1865ca3e8d88SDave Plauger 1866ca3e8d88SDave Plauger /* Have input, check for dump I/O error. */ 1867ca3e8d88SDave Plauger if (!dump_ioerr) 1868ca3e8d88SDave Plauger break; 1869ca3e8d88SDave Plauger 1870ca3e8d88SDave Plauger /* 1871ca3e8d88SDave Plauger * If an I/O error occurs, stay in the 1872ca3e8d88SDave Plauger * loop in order to empty the helper 1873ca3e8d88SDave Plauger * queue. Return the buffers to the 1874ca3e8d88SDave Plauger * main task to unmap and free it. 1875ca3e8d88SDave Plauger */ 1876ca3e8d88SDave Plauger hp->cpin->used = 0; 1877ca3e8d88SDave Plauger CQ_PUT(mainq, hp->cpin, CBUF_USEDMAP); 1878ca3e8d88SDave Plauger } 1879ca3e8d88SDave Plauger HRSTOP(hp->perpage, inwait); 1880ca3e8d88SDave Plauger 1881ca3e8d88SDave Plauger /* Stop here when the helper queue is closed. */ 1882ca3e8d88SDave Plauger if (hp->cpin == NULL) 1883ca3e8d88SDave Plauger break; 1884ca3e8d88SDave Plauger 1885ca3e8d88SDave Plauger /* Set the offset=0 to get the first pfn. */ 1886ca3e8d88SDave Plauger hp->in = 0; 1887ca3e8d88SDave Plauger 1888ca3e8d88SDave Plauger /* Set the total processed to 0 */ 1889ca3e8d88SDave Plauger hp->used = 0; 1890ca3e8d88SDave Plauger } 1891ca3e8d88SDave Plauger 1892ca3e8d88SDave Plauger /* Process the next page. */ 1893ca3e8d88SDave Plauger if (hp->used < hp->cpin->used) { 1894ca3e8d88SDave Plauger 1895ca3e8d88SDave Plauger /* 1896ca3e8d88SDave Plauger * Get the next page from the input buffer and 1897ca3e8d88SDave Plauger * return a copy. 1898ca3e8d88SDave Plauger */ 1899ca3e8d88SDave Plauger ASSERT(hp->in != -1); 1900ca3e8d88SDave Plauger HRSTART(hp->perpage, copy); 1901ca3e8d88SDave Plauger hp->in = dumpsys_copy_page(hp, hp->in); 1902ca3e8d88SDave Plauger hp->used += PAGESIZE; 1903ca3e8d88SDave Plauger HRSTOP(hp->perpage, copy); 1904ca3e8d88SDave Plauger break; 1905ca3e8d88SDave Plauger 1906ca3e8d88SDave Plauger } else { 1907ca3e8d88SDave Plauger 1908ca3e8d88SDave Plauger /* 1909ca3e8d88SDave Plauger * Done with the input. Flush the VM and 1910ca3e8d88SDave Plauger * return the buffer to the main task. 1911ca3e8d88SDave Plauger */ 1912ca3e8d88SDave Plauger if (panicstr && hp->helper != MAINHELPER) 1913ca3e8d88SDave Plauger hat_flush_range(kas.a_hat, 1914ca3e8d88SDave Plauger hp->cpin->buf, hp->cpin->size); 1915ca3e8d88SDave Plauger dumpsys_errmsg(hp, NULL); 1916ca3e8d88SDave Plauger CQ_PUT(mainq, hp->cpin, CBUF_USEDMAP); 1917ca3e8d88SDave Plauger hp->cpin = NULL; 1918ca3e8d88SDave Plauger } 1919ca3e8d88SDave Plauger } 1920ca3e8d88SDave Plauger 1921ca3e8d88SDave Plauger return (hp->cpin != NULL); 1922ca3e8d88SDave Plauger } 1923ca3e8d88SDave Plauger 1924ca3e8d88SDave Plauger /* 1925ca3e8d88SDave Plauger * Compress size bytes starting at buf with bzip2 1926ca3e8d88SDave Plauger * mode: 1927ca3e8d88SDave Plauger * BZ_RUN add one more compressed page 1928ca3e8d88SDave Plauger * BZ_FINISH no more input, flush the state 1929ca3e8d88SDave Plauger */ 1930ca3e8d88SDave Plauger static void 1931ca3e8d88SDave Plauger dumpsys_bzrun(helper_t *hp, void *buf, size_t size, int mode) 1932ca3e8d88SDave Plauger { 1933ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 1934ca3e8d88SDave Plauger const int CSIZE = sizeof (dumpcsize_t); 1935ca3e8d88SDave Plauger bz_stream *ps = &hp->bzstream; 1936ca3e8d88SDave Plauger int rc = 0; 1937ca3e8d88SDave Plauger uint32_t csize; 1938ca3e8d88SDave Plauger dumpcsize_t cs; 1939ca3e8d88SDave Plauger 1940ca3e8d88SDave Plauger /* Set input pointers to new input page */ 1941ca3e8d88SDave Plauger if (size > 0) { 1942ca3e8d88SDave Plauger ps->avail_in = size; 1943ca3e8d88SDave Plauger ps->next_in = buf; 1944ca3e8d88SDave Plauger } 1945ca3e8d88SDave Plauger 1946ca3e8d88SDave Plauger /* CONSTCOND */ 1947ca3e8d88SDave Plauger while (1) { 1948ca3e8d88SDave Plauger 1949ca3e8d88SDave Plauger /* Quit when all input has been consumed */ 1950ca3e8d88SDave Plauger if (ps->avail_in == 0 && mode == BZ_RUN) 1951ca3e8d88SDave Plauger break; 1952ca3e8d88SDave Plauger 1953ca3e8d88SDave Plauger /* Get a new output buffer */ 1954ca3e8d88SDave Plauger if (hp->cpout == NULL) { 1955ca3e8d88SDave Plauger HRSTART(hp->perpage, outwait); 1956ca3e8d88SDave Plauger hp->cpout = CQ_GET(freebufq); 1957ca3e8d88SDave Plauger HRSTOP(hp->perpage, outwait); 1958ca3e8d88SDave Plauger ps->avail_out = hp->cpout->size - CSIZE; 1959ca3e8d88SDave Plauger ps->next_out = hp->cpout->buf + CSIZE; 1960ca3e8d88SDave Plauger } 1961ca3e8d88SDave Plauger 1962ca3e8d88SDave Plauger /* Compress input, or finalize */ 1963ca3e8d88SDave Plauger HRSTART(hp->perpage, compress); 1964ca3e8d88SDave Plauger rc = BZ2_bzCompress(ps, mode); 1965ca3e8d88SDave Plauger HRSTOP(hp->perpage, compress); 1966ca3e8d88SDave Plauger 1967ca3e8d88SDave Plauger /* Check for error */ 1968ca3e8d88SDave Plauger if (mode == BZ_RUN && rc != BZ_RUN_OK) { 1969ca3e8d88SDave Plauger dumpsys_errmsg(hp, "%d: BZ_RUN error %s at page %lx\n", 1970ca3e8d88SDave Plauger hp->helper, BZ2_bzErrorString(rc), 1971ca3e8d88SDave Plauger hp->cpin->pagenum); 1972ca3e8d88SDave Plauger break; 1973ca3e8d88SDave Plauger } 1974ca3e8d88SDave Plauger 1975ca3e8d88SDave Plauger /* Write the buffer if it is full, or we are flushing */ 1976ca3e8d88SDave Plauger if (ps->avail_out == 0 || mode == BZ_FINISH) { 1977ca3e8d88SDave Plauger csize = hp->cpout->size - CSIZE - ps->avail_out; 1978ca3e8d88SDave Plauger cs = DUMP_SET_TAG(csize, hp->tag); 1979ca3e8d88SDave Plauger if (csize > 0) { 1980ca3e8d88SDave Plauger (void) memcpy(hp->cpout->buf, &cs, CSIZE); 1981ca3e8d88SDave Plauger dumpsys_swrite(hp, hp->cpout, csize + CSIZE); 1982ca3e8d88SDave Plauger hp->cpout = NULL; 1983ca3e8d88SDave Plauger } 1984ca3e8d88SDave Plauger } 1985ca3e8d88SDave Plauger 1986ca3e8d88SDave Plauger /* Check for final complete */ 1987ca3e8d88SDave Plauger if (mode == BZ_FINISH) { 1988ca3e8d88SDave Plauger if (rc == BZ_STREAM_END) 1989ca3e8d88SDave Plauger break; 1990ca3e8d88SDave Plauger if (rc != BZ_FINISH_OK) { 1991ca3e8d88SDave Plauger dumpsys_errmsg(hp, "%d: BZ_FINISH error %s\n", 1992ca3e8d88SDave Plauger hp->helper, BZ2_bzErrorString(rc)); 1993ca3e8d88SDave Plauger break; 1994ca3e8d88SDave Plauger } 1995ca3e8d88SDave Plauger } 1996ca3e8d88SDave Plauger } 1997ca3e8d88SDave Plauger 1998ca3e8d88SDave Plauger /* Cleanup state and buffers */ 1999ca3e8d88SDave Plauger if (mode == BZ_FINISH) { 2000ca3e8d88SDave Plauger 2001ca3e8d88SDave Plauger /* Reset state so that it is re-usable. */ 2002ca3e8d88SDave Plauger (void) BZ2_bzCompressReset(&hp->bzstream); 2003ca3e8d88SDave Plauger 2004ca3e8d88SDave Plauger /* Give any unused outout buffer to the main task */ 2005ca3e8d88SDave Plauger if (hp->cpout != NULL) { 2006ca3e8d88SDave Plauger hp->cpout->used = 0; 2007ca3e8d88SDave Plauger CQ_PUT(mainq, hp->cpout, CBUF_ERRMSG); 2008ca3e8d88SDave Plauger hp->cpout = NULL; 2009ca3e8d88SDave Plauger } 2010ca3e8d88SDave Plauger } 2011ca3e8d88SDave Plauger } 2012ca3e8d88SDave Plauger 2013ca3e8d88SDave Plauger static void 2014ca3e8d88SDave Plauger dumpsys_bz2compress(helper_t *hp) 2015ca3e8d88SDave Plauger { 2016ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 2017ca3e8d88SDave Plauger dumpstreamhdr_t sh; 2018ca3e8d88SDave Plauger 2019ca3e8d88SDave Plauger (void) strcpy(sh.stream_magic, DUMP_STREAM_MAGIC); 2020ca3e8d88SDave Plauger sh.stream_pagenum = (pgcnt_t)-1; 2021ca3e8d88SDave Plauger sh.stream_npages = 0; 2022ca3e8d88SDave Plauger hp->cpin = NULL; 2023ca3e8d88SDave Plauger hp->cpout = NULL; 2024ca3e8d88SDave Plauger hp->cperr = NULL; 2025ca3e8d88SDave Plauger hp->in = 0; 2026ca3e8d88SDave Plauger hp->out = 0; 2027ca3e8d88SDave Plauger hp->bzstream.avail_in = 0; 2028ca3e8d88SDave Plauger 2029ca3e8d88SDave Plauger /* Bump reference to mainq while we are running */ 2030ca3e8d88SDave Plauger CQ_OPEN(mainq); 2031ca3e8d88SDave Plauger 2032ca3e8d88SDave Plauger /* Get one page at a time */ 2033ca3e8d88SDave Plauger while (dumpsys_sread(hp)) { 2034ca3e8d88SDave Plauger if (sh.stream_pagenum != hp->cpin->pagenum) { 2035ca3e8d88SDave Plauger sh.stream_pagenum = hp->cpin->pagenum; 2036ca3e8d88SDave Plauger sh.stream_npages = btop(hp->cpin->used); 2037ca3e8d88SDave Plauger dumpsys_bzrun(hp, &sh, sizeof (sh), BZ_RUN); 2038ca3e8d88SDave Plauger } 2039ca3e8d88SDave Plauger dumpsys_bzrun(hp, hp->page, PAGESIZE, 0); 2040ca3e8d88SDave Plauger } 2041ca3e8d88SDave Plauger 2042ca3e8d88SDave Plauger /* Done with input, flush any partial buffer */ 2043ca3e8d88SDave Plauger if (sh.stream_pagenum != (pgcnt_t)-1) { 2044ca3e8d88SDave Plauger dumpsys_bzrun(hp, NULL, 0, BZ_FINISH); 2045ca3e8d88SDave Plauger dumpsys_errmsg(hp, NULL); 2046ca3e8d88SDave Plauger } 2047ca3e8d88SDave Plauger 2048ca3e8d88SDave Plauger ASSERT(hp->cpin == NULL && hp->cpout == NULL && hp->cperr == NULL); 2049ca3e8d88SDave Plauger 2050ca3e8d88SDave Plauger /* Decrement main queue count, we are done */ 2051ca3e8d88SDave Plauger CQ_CLOSE(mainq); 2052ca3e8d88SDave Plauger } 2053ca3e8d88SDave Plauger 2054ca3e8d88SDave Plauger /* 2055ca3e8d88SDave Plauger * Compress with lzjb 2056ca3e8d88SDave Plauger * write stream block if full or size==0 2057ca3e8d88SDave Plauger * if csize==0 write stream header, else write <csize, data> 2058ca3e8d88SDave Plauger * size==0 is a call to flush a buffer 2059ca3e8d88SDave Plauger * hp->cpout is the buffer we are flushing or filling 2060ca3e8d88SDave Plauger * hp->out is the next index to fill data 2061ca3e8d88SDave Plauger * osize is either csize+data, or the size of a stream header 2062ca3e8d88SDave Plauger */ 2063ca3e8d88SDave Plauger static void 2064ca3e8d88SDave Plauger dumpsys_lzjbrun(helper_t *hp, size_t csize, void *buf, size_t size) 2065ca3e8d88SDave Plauger { 2066ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 2067ca3e8d88SDave Plauger const int CSIZE = sizeof (dumpcsize_t); 2068ca3e8d88SDave Plauger dumpcsize_t cs; 2069ca3e8d88SDave Plauger size_t osize = csize > 0 ? CSIZE + size : size; 2070ca3e8d88SDave Plauger 2071ca3e8d88SDave Plauger /* If flush, and there is no buffer, just return */ 2072ca3e8d88SDave Plauger if (size == 0 && hp->cpout == NULL) 2073ca3e8d88SDave Plauger return; 2074ca3e8d88SDave Plauger 2075ca3e8d88SDave Plauger /* If flush, or cpout is full, write it out */ 2076ca3e8d88SDave Plauger if (size == 0 || 2077ca3e8d88SDave Plauger hp->cpout != NULL && hp->out + osize > hp->cpout->size) { 2078ca3e8d88SDave Plauger 2079ca3e8d88SDave Plauger /* Set tag+size word at the front of the stream block. */ 2080ca3e8d88SDave Plauger cs = DUMP_SET_TAG(hp->out - CSIZE, hp->tag); 2081ca3e8d88SDave Plauger (void) memcpy(hp->cpout->buf, &cs, CSIZE); 2082ca3e8d88SDave Plauger 2083ca3e8d88SDave Plauger /* Write block to dump file. */ 2084ca3e8d88SDave Plauger dumpsys_swrite(hp, hp->cpout, hp->out); 2085ca3e8d88SDave Plauger 2086ca3e8d88SDave Plauger /* Clear pointer to indicate we need a new buffer */ 2087ca3e8d88SDave Plauger hp->cpout = NULL; 2088ca3e8d88SDave Plauger 2089ca3e8d88SDave Plauger /* flushing, we are done */ 2090ca3e8d88SDave Plauger if (size == 0) 2091ca3e8d88SDave Plauger return; 20927c478bd9Sstevel@tonic-gate } 20937c478bd9Sstevel@tonic-gate 2094ca3e8d88SDave Plauger /* Get an output buffer if we dont have one. */ 2095ca3e8d88SDave Plauger if (hp->cpout == NULL) { 2096ca3e8d88SDave Plauger HRSTART(hp->perpage, outwait); 2097ca3e8d88SDave Plauger hp->cpout = CQ_GET(freebufq); 2098ca3e8d88SDave Plauger HRSTOP(hp->perpage, outwait); 2099ca3e8d88SDave Plauger hp->out = CSIZE; 21007c478bd9Sstevel@tonic-gate } 2101ca3e8d88SDave Plauger 2102ca3e8d88SDave Plauger /* Store csize word. This is the size of compressed data. */ 2103ca3e8d88SDave Plauger if (csize > 0) { 2104ca3e8d88SDave Plauger cs = DUMP_SET_TAG(csize, 0); 2105ca3e8d88SDave Plauger (void) memcpy(hp->cpout->buf + hp->out, &cs, CSIZE); 2106ca3e8d88SDave Plauger hp->out += CSIZE; 2107ca3e8d88SDave Plauger } 2108ca3e8d88SDave Plauger 2109ca3e8d88SDave Plauger /* Store the data. */ 2110ca3e8d88SDave Plauger (void) memcpy(hp->cpout->buf + hp->out, buf, size); 2111ca3e8d88SDave Plauger hp->out += size; 21127c478bd9Sstevel@tonic-gate } 21137c478bd9Sstevel@tonic-gate 21147c478bd9Sstevel@tonic-gate static void 2115ca3e8d88SDave Plauger dumpsys_lzjbcompress(helper_t *hp) 21167c478bd9Sstevel@tonic-gate { 2117ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 2118ca3e8d88SDave Plauger size_t csize; 2119ca3e8d88SDave Plauger dumpstreamhdr_t sh; 2120ca3e8d88SDave Plauger 2121ca3e8d88SDave Plauger (void) strcpy(sh.stream_magic, DUMP_STREAM_MAGIC); 2122ca3e8d88SDave Plauger sh.stream_pagenum = (pfn_t)-1; 2123ca3e8d88SDave Plauger sh.stream_npages = 0; 2124ca3e8d88SDave Plauger hp->cpin = NULL; 2125ca3e8d88SDave Plauger hp->cpout = NULL; 2126ca3e8d88SDave Plauger hp->cperr = NULL; 2127ca3e8d88SDave Plauger hp->in = 0; 2128ca3e8d88SDave Plauger hp->out = 0; 2129ca3e8d88SDave Plauger 2130ca3e8d88SDave Plauger /* Bump reference to mainq while we are running */ 2131ca3e8d88SDave Plauger CQ_OPEN(mainq); 2132ca3e8d88SDave Plauger 2133ca3e8d88SDave Plauger /* Get one page at a time */ 2134ca3e8d88SDave Plauger while (dumpsys_sread(hp)) { 2135ca3e8d88SDave Plauger 2136ca3e8d88SDave Plauger /* Create a stream header for each new input map */ 2137ca3e8d88SDave Plauger if (sh.stream_pagenum != hp->cpin->pagenum) { 2138ca3e8d88SDave Plauger sh.stream_pagenum = hp->cpin->pagenum; 2139ca3e8d88SDave Plauger sh.stream_npages = btop(hp->cpin->used); 2140ca3e8d88SDave Plauger dumpsys_lzjbrun(hp, 0, &sh, sizeof (sh)); 2141ca3e8d88SDave Plauger } 2142ca3e8d88SDave Plauger 2143ca3e8d88SDave Plauger /* Compress one page */ 2144ca3e8d88SDave Plauger HRSTART(hp->perpage, compress); 2145ca3e8d88SDave Plauger csize = compress(hp->page, hp->lzbuf, PAGESIZE); 2146ca3e8d88SDave Plauger HRSTOP(hp->perpage, compress); 2147ca3e8d88SDave Plauger 2148ca3e8d88SDave Plauger /* Add csize+data to output block */ 2149ca3e8d88SDave Plauger ASSERT(csize > 0 && csize <= PAGESIZE); 2150ca3e8d88SDave Plauger dumpsys_lzjbrun(hp, csize, hp->lzbuf, csize); 2151ca3e8d88SDave Plauger } 2152ca3e8d88SDave Plauger 2153ca3e8d88SDave Plauger /* Done with input, flush any partial buffer */ 2154ca3e8d88SDave Plauger if (sh.stream_pagenum != (pfn_t)-1) { 2155ca3e8d88SDave Plauger dumpsys_lzjbrun(hp, 0, NULL, 0); 2156ca3e8d88SDave Plauger dumpsys_errmsg(hp, NULL); 2157ca3e8d88SDave Plauger } 2158ca3e8d88SDave Plauger 2159ca3e8d88SDave Plauger ASSERT(hp->cpin == NULL && hp->cpout == NULL && hp->cperr == NULL); 2160ca3e8d88SDave Plauger 2161ca3e8d88SDave Plauger /* Decrement main queue count, we are done */ 2162ca3e8d88SDave Plauger CQ_CLOSE(mainq); 21637c478bd9Sstevel@tonic-gate } 21647c478bd9Sstevel@tonic-gate 21657c478bd9Sstevel@tonic-gate /* 2166ca3e8d88SDave Plauger * Dump helper called from panic_idle() to compress pages. CPUs in 2167ca3e8d88SDave Plauger * this path must not call most kernel services. 2168ca3e8d88SDave Plauger * 2169ca3e8d88SDave Plauger * During panic, all but one of the CPUs is idle. These CPUs are used 2170ca3e8d88SDave Plauger * as helpers working in parallel to copy and compress memory 2171ca3e8d88SDave Plauger * pages. During a panic, however, these processors cannot call any 2172ca3e8d88SDave Plauger * kernel services. This is because mutexes become no-ops during 2173ca3e8d88SDave Plauger * panic, and, cross-call interrupts are inhibited. Therefore, during 2174ca3e8d88SDave Plauger * panic dump the helper CPUs communicate with the panic CPU using 2175ca3e8d88SDave Plauger * memory variables. All memory mapping and I/O is performed by the 2176ca3e8d88SDave Plauger * panic CPU. 217780f5ed81SDave Plauger * 217880f5ed81SDave Plauger * At dump configuration time, helper_lock is set and helpers_wanted 217980f5ed81SDave Plauger * is 0. dumpsys() decides whether to set helpers_wanted before 218080f5ed81SDave Plauger * clearing helper_lock. 218180f5ed81SDave Plauger * 218280f5ed81SDave Plauger * At panic time, idle CPUs spin-wait on helper_lock, then alternately 218380f5ed81SDave Plauger * take the lock and become a helper, or return. 21847c478bd9Sstevel@tonic-gate */ 21857c478bd9Sstevel@tonic-gate void 2186ca3e8d88SDave Plauger dumpsys_helper() 21877c478bd9Sstevel@tonic-gate { 2188ca3e8d88SDave Plauger dumpsys_spinlock(&dumpcfg.helper_lock); 2189ca3e8d88SDave Plauger if (dumpcfg.helpers_wanted) { 2190ca3e8d88SDave Plauger helper_t *hp, *hpend = &dumpcfg.helper[dumpcfg.nhelper]; 21917c478bd9Sstevel@tonic-gate 2192ca3e8d88SDave Plauger for (hp = dumpcfg.helper; hp != hpend; hp++) { 2193ca3e8d88SDave Plauger if (hp->helper == FREEHELPER) { 2194ca3e8d88SDave Plauger hp->helper = CPU->cpu_id; 2195ca3e8d88SDave Plauger BT_SET(dumpcfg.helpermap, CPU->cpu_seqid); 2196ca3e8d88SDave Plauger 2197ca3e8d88SDave Plauger dumpsys_spinunlock(&dumpcfg.helper_lock); 2198ca3e8d88SDave Plauger 2199ca3e8d88SDave Plauger if (dumpcfg.clevel < DUMP_CLEVEL_BZIP2) 2200ca3e8d88SDave Plauger dumpsys_lzjbcompress(hp); 2201ca3e8d88SDave Plauger else 2202ca3e8d88SDave Plauger dumpsys_bz2compress(hp); 2203ca3e8d88SDave Plauger 2204ca3e8d88SDave Plauger hp->helper = DONEHELPER; 2205ca3e8d88SDave Plauger return; 2206ca3e8d88SDave Plauger } 22077c478bd9Sstevel@tonic-gate } 220880f5ed81SDave Plauger 220980f5ed81SDave Plauger /* No more helpers are needed. */ 221080f5ed81SDave Plauger dumpcfg.helpers_wanted = 0; 221180f5ed81SDave Plauger 22127c478bd9Sstevel@tonic-gate } 2213ca3e8d88SDave Plauger dumpsys_spinunlock(&dumpcfg.helper_lock); 22147c478bd9Sstevel@tonic-gate } 22157c478bd9Sstevel@tonic-gate 221680f5ed81SDave Plauger /* 221780f5ed81SDave Plauger * No-wait helper callable in spin loops. 221880f5ed81SDave Plauger * 221980f5ed81SDave Plauger * Do not wait for helper_lock. Just check helpers_wanted. The caller 222080f5ed81SDave Plauger * may decide to continue. This is the "c)ontinue, s)ync, r)eset? s" 222180f5ed81SDave Plauger * case. 222280f5ed81SDave Plauger */ 222380f5ed81SDave Plauger void 222480f5ed81SDave Plauger dumpsys_helper_nw() 222580f5ed81SDave Plauger { 222680f5ed81SDave Plauger if (dumpcfg.helpers_wanted) 222780f5ed81SDave Plauger dumpsys_helper(); 222880f5ed81SDave Plauger } 222980f5ed81SDave Plauger 22307c478bd9Sstevel@tonic-gate /* 2231ca3e8d88SDave Plauger * Dump helper for live dumps. 2232ca3e8d88SDave Plauger * These run as a system task. 22337c478bd9Sstevel@tonic-gate */ 2234ca3e8d88SDave Plauger static void 2235ca3e8d88SDave Plauger dumpsys_live_helper(void *arg) 22367c478bd9Sstevel@tonic-gate { 2237ca3e8d88SDave Plauger helper_t *hp = arg; 22387c478bd9Sstevel@tonic-gate 2239ca3e8d88SDave Plauger BT_ATOMIC_SET(dumpcfg.helpermap, CPU->cpu_seqid); 2240ca3e8d88SDave Plauger if (dumpcfg.clevel < DUMP_CLEVEL_BZIP2) 2241ca3e8d88SDave Plauger dumpsys_lzjbcompress(hp); 2242ca3e8d88SDave Plauger else 2243ca3e8d88SDave Plauger dumpsys_bz2compress(hp); 22447c478bd9Sstevel@tonic-gate } 22457c478bd9Sstevel@tonic-gate 22467c478bd9Sstevel@tonic-gate /* 2247ca3e8d88SDave Plauger * Compress one page with lzjb (single threaded case) 22487c478bd9Sstevel@tonic-gate */ 22497c478bd9Sstevel@tonic-gate static void 2250ca3e8d88SDave Plauger dumpsys_lzjb_page(helper_t *hp, cbuf_t *cp) 22517c478bd9Sstevel@tonic-gate { 2252ca3e8d88SDave Plauger dumpsync_t *ds = hp->ds; 2253ca3e8d88SDave Plauger uint32_t csize; 22547c478bd9Sstevel@tonic-gate 2255ca3e8d88SDave Plauger hp->helper = MAINHELPER; 2256ca3e8d88SDave Plauger hp->in = 0; 2257ca3e8d88SDave Plauger hp->used = 0; 2258ca3e8d88SDave Plauger hp->cpin = cp; 2259ca3e8d88SDave Plauger while (hp->used < cp->used) { 2260ca3e8d88SDave Plauger HRSTART(hp->perpage, copy); 2261ca3e8d88SDave Plauger hp->in = dumpsys_copy_page(hp, hp->in); 2262ca3e8d88SDave Plauger hp->used += PAGESIZE; 2263ca3e8d88SDave Plauger HRSTOP(hp->perpage, copy); 2264ca3e8d88SDave Plauger 2265ca3e8d88SDave Plauger HRSTART(hp->perpage, compress); 2266ca3e8d88SDave Plauger csize = compress(hp->page, hp->lzbuf, PAGESIZE); 2267ca3e8d88SDave Plauger HRSTOP(hp->perpage, compress); 2268ca3e8d88SDave Plauger 2269ca3e8d88SDave Plauger HRSTART(hp->perpage, write); 2270ca3e8d88SDave Plauger dumpvp_write(&csize, sizeof (csize)); 2271ca3e8d88SDave Plauger dumpvp_write(hp->lzbuf, csize); 2272ca3e8d88SDave Plauger HRSTOP(hp->perpage, write); 22737c478bd9Sstevel@tonic-gate } 2274ca3e8d88SDave Plauger CQ_PUT(mainq, hp->cpin, CBUF_USEDMAP); 2275ca3e8d88SDave Plauger hp->cpin = NULL; 22767c478bd9Sstevel@tonic-gate } 22777c478bd9Sstevel@tonic-gate 2278ca3e8d88SDave Plauger /* 2279ca3e8d88SDave Plauger * Main task to dump pages. This is called on the dump CPU. 2280ca3e8d88SDave Plauger */ 2281ca3e8d88SDave Plauger static void 2282ca3e8d88SDave Plauger dumpsys_main_task(void *arg) 22837c478bd9Sstevel@tonic-gate { 2284ca3e8d88SDave Plauger dumpsync_t *ds = arg; 2285ca3e8d88SDave Plauger pgcnt_t pagenum = 0, bitnum = 0, hibitnum; 2286ca3e8d88SDave Plauger dumpmlw_t mlw; 2287ca3e8d88SDave Plauger cbuf_t *cp; 2288ca3e8d88SDave Plauger pgcnt_t baseoff, pfnoff; 2289ca3e8d88SDave Plauger pfn_t base, pfn; 2290*2fb0949cSJoshua M. Clulow int i, dumpserial; 22919c84d1a2SDave Plauger 22929c84d1a2SDave Plauger /* 22939c84d1a2SDave Plauger * Fall back to serial mode if there are no helpers. 22949c84d1a2SDave Plauger * dump_plat_mincpu can be set to 0 at any time. 22959c84d1a2SDave Plauger * dumpcfg.helpermap must contain at least one member. 22969c84d1a2SDave Plauger */ 22979c84d1a2SDave Plauger dumpserial = 1; 22989c84d1a2SDave Plauger 22999c84d1a2SDave Plauger if (dump_plat_mincpu != 0 && dumpcfg.clevel != 0) { 23009c84d1a2SDave Plauger for (i = 0; i < BT_BITOUL(NCPU); ++i) { 23019c84d1a2SDave Plauger if (dumpcfg.helpermap[i] != 0) { 23029c84d1a2SDave Plauger dumpserial = 0; 23039c84d1a2SDave Plauger break; 23049c84d1a2SDave Plauger } 23059c84d1a2SDave Plauger } 23069c84d1a2SDave Plauger } 23079c84d1a2SDave Plauger 23089c84d1a2SDave Plauger if (dumpserial) { 23099c84d1a2SDave Plauger dumpcfg.clevel = 0; 23109c84d1a2SDave Plauger if (dumpcfg.helper[0].lzbuf == NULL) 23119c84d1a2SDave Plauger dumpcfg.helper[0].lzbuf = dumpcfg.helper[1].page; 23129c84d1a2SDave Plauger } 2313ca3e8d88SDave Plauger 2314ca3e8d88SDave Plauger dump_init_memlist_walker(&mlw); 2315ca3e8d88SDave Plauger 2316*2fb0949cSJoshua M. Clulow for (;;) { 2317*2fb0949cSJoshua M. Clulow int sec = (gethrtime() - ds->start) / NANOSEC; 2318ca3e8d88SDave Plauger 2319*2fb0949cSJoshua M. Clulow /* 2320*2fb0949cSJoshua M. Clulow * Render a simple progress display on the system console to 2321*2fb0949cSJoshua M. Clulow * make clear to the operator that the system has not hung. 2322*2fb0949cSJoshua M. Clulow * Emit an update when dump progress has advanced by one 2323*2fb0949cSJoshua M. Clulow * percent, or when no update has been drawn in the last 2324*2fb0949cSJoshua M. Clulow * second. 2325*2fb0949cSJoshua M. Clulow */ 2326*2fb0949cSJoshua M. Clulow if (ds->percent > ds->percent_done || sec > ds->sec_done) { 2327*2fb0949cSJoshua M. Clulow ds->sec_done = sec; 2328ca3e8d88SDave Plauger ds->percent_done = ds->percent; 2329*2fb0949cSJoshua M. Clulow uprintf("^\rdumping: %2d:%02d %3d%% done", 2330ca3e8d88SDave Plauger sec / 60, sec % 60, ds->percent); 2331ca3e8d88SDave Plauger ds->neednl = 1; 2332ca3e8d88SDave Plauger } 23337c478bd9Sstevel@tonic-gate 2334ca3e8d88SDave Plauger while (CQ_IS_EMPTY(mainq) && !CQ_IS_EMPTY(writerq)) { 23357c478bd9Sstevel@tonic-gate 2336ca3e8d88SDave Plauger /* the writerq never blocks */ 2337ca3e8d88SDave Plauger cp = CQ_GET(writerq); 2338ca3e8d88SDave Plauger if (cp == NULL) 2339ca3e8d88SDave Plauger break; 23407c478bd9Sstevel@tonic-gate 2341ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 23427c478bd9Sstevel@tonic-gate 2343ca3e8d88SDave Plauger HRSTART(ds->perpage, write); 2344ca3e8d88SDave Plauger dumpvp_write(cp->buf, cp->used); 2345ca3e8d88SDave Plauger HRSTOP(ds->perpage, write); 23467c478bd9Sstevel@tonic-gate 2347ca3e8d88SDave Plauger CQ_PUT(freebufq, cp, CBUF_FREEBUF); 2348ca3e8d88SDave Plauger } 23497c478bd9Sstevel@tonic-gate 2350ca3e8d88SDave Plauger /* 2351ca3e8d88SDave Plauger * Wait here for some buffers to process. Returns NULL 2352ca3e8d88SDave Plauger * when all helpers have terminated and all buffers 2353ca3e8d88SDave Plauger * have been processed. 2354ca3e8d88SDave Plauger */ 2355ca3e8d88SDave Plauger cp = CQ_GET(mainq); 23567c478bd9Sstevel@tonic-gate 2357ca3e8d88SDave Plauger if (cp == NULL) { 23587c478bd9Sstevel@tonic-gate 2359ca3e8d88SDave Plauger /* Drain the write queue. */ 2360ca3e8d88SDave Plauger if (!CQ_IS_EMPTY(writerq)) 2361ca3e8d88SDave Plauger continue; 23627c478bd9Sstevel@tonic-gate 2363ca3e8d88SDave Plauger /* Main task exits here. */ 2364ca3e8d88SDave Plauger break; 2365ca3e8d88SDave Plauger } 23667c478bd9Sstevel@tonic-gate 2367ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 23687c478bd9Sstevel@tonic-gate 2369ca3e8d88SDave Plauger switch (cp->state) { 23707c478bd9Sstevel@tonic-gate 2371ca3e8d88SDave Plauger case CBUF_FREEMAP: 23727c478bd9Sstevel@tonic-gate 2373ca3e8d88SDave Plauger /* 2374ca3e8d88SDave Plauger * Note that we drop CBUF_FREEMAP buffers on 2375ca3e8d88SDave Plauger * the floor (they will not be on any cqueue) 2376ca3e8d88SDave Plauger * when we no longer need them. 2377ca3e8d88SDave Plauger */ 2378ca3e8d88SDave Plauger if (bitnum >= dumpcfg.bitmapsize) 2379ca3e8d88SDave Plauger break; 2380ca3e8d88SDave Plauger 2381ca3e8d88SDave Plauger if (dump_ioerr) { 2382ca3e8d88SDave Plauger bitnum = dumpcfg.bitmapsize; 2383ca3e8d88SDave Plauger CQ_CLOSE(helperq); 2384ca3e8d88SDave Plauger break; 2385ca3e8d88SDave Plauger } 2386ca3e8d88SDave Plauger 2387ca3e8d88SDave Plauger HRSTART(ds->perpage, bitmap); 2388ca3e8d88SDave Plauger for (; bitnum < dumpcfg.bitmapsize; bitnum++) 2389ca3e8d88SDave Plauger if (BT_TEST(dumpcfg.bitmap, bitnum)) 2390ca3e8d88SDave Plauger break; 2391ca3e8d88SDave Plauger HRSTOP(ds->perpage, bitmap); 23927c478bd9Sstevel@tonic-gate dump_timeleft = dump_timeout; 23937c478bd9Sstevel@tonic-gate 2394ca3e8d88SDave Plauger if (bitnum >= dumpcfg.bitmapsize) { 2395ca3e8d88SDave Plauger CQ_CLOSE(helperq); 2396ca3e8d88SDave Plauger break; 2397ca3e8d88SDave Plauger } 2398ca3e8d88SDave Plauger 2399ca3e8d88SDave Plauger /* 2400ca3e8d88SDave Plauger * Try to map CBUF_MAPSIZE ranges. Can't 2401ca3e8d88SDave Plauger * assume that memory segment size is a 2402ca3e8d88SDave Plauger * multiple of CBUF_MAPSIZE. Can't assume that 2403ca3e8d88SDave Plauger * the segment starts on a CBUF_MAPSIZE 2404ca3e8d88SDave Plauger * boundary. 2405ca3e8d88SDave Plauger */ 2406ca3e8d88SDave Plauger pfn = dump_bitnum_to_pfn(bitnum, &mlw); 2407ca3e8d88SDave Plauger ASSERT(pfn != PFN_INVALID); 2408ca3e8d88SDave Plauger ASSERT(bitnum + mlw.mpleft <= dumpcfg.bitmapsize); 2409ca3e8d88SDave Plauger 2410ca3e8d88SDave Plauger base = P2ALIGN(pfn, CBUF_MAPNP); 2411ca3e8d88SDave Plauger if (base < mlw.mpaddr) { 2412ca3e8d88SDave Plauger base = mlw.mpaddr; 2413ca3e8d88SDave Plauger baseoff = P2PHASE(base, CBUF_MAPNP); 2414ca3e8d88SDave Plauger } else { 2415ca3e8d88SDave Plauger baseoff = 0; 2416ca3e8d88SDave Plauger } 2417ca3e8d88SDave Plauger 2418ca3e8d88SDave Plauger pfnoff = pfn - base; 2419ca3e8d88SDave Plauger if (pfnoff + mlw.mpleft < CBUF_MAPNP) { 2420ca3e8d88SDave Plauger hibitnum = bitnum + mlw.mpleft; 2421ca3e8d88SDave Plauger cp->size = ptob(pfnoff + mlw.mpleft); 2422ca3e8d88SDave Plauger } else { 2423ca3e8d88SDave Plauger hibitnum = bitnum - pfnoff + CBUF_MAPNP - 2424ca3e8d88SDave Plauger baseoff; 2425ca3e8d88SDave Plauger cp->size = CBUF_MAPSIZE - ptob(baseoff); 2426ca3e8d88SDave Plauger } 2427ca3e8d88SDave Plauger 2428ca3e8d88SDave Plauger cp->pfn = pfn; 2429ca3e8d88SDave Plauger cp->bitnum = bitnum++; 2430ca3e8d88SDave Plauger cp->pagenum = pagenum++; 2431ca3e8d88SDave Plauger cp->off = ptob(pfnoff); 2432ca3e8d88SDave Plauger 2433ca3e8d88SDave Plauger for (; bitnum < hibitnum; bitnum++) 2434ca3e8d88SDave Plauger if (BT_TEST(dumpcfg.bitmap, bitnum)) 2435ca3e8d88SDave Plauger pagenum++; 2436ca3e8d88SDave Plauger 2437ca3e8d88SDave Plauger dump_timeleft = dump_timeout; 2438ca3e8d88SDave Plauger cp->used = ptob(pagenum - cp->pagenum); 2439ca3e8d88SDave Plauger 2440ca3e8d88SDave Plauger HRSTART(ds->perpage, map); 2441ca3e8d88SDave Plauger hat_devload(kas.a_hat, cp->buf, cp->size, base, 2442ca3e8d88SDave Plauger PROT_READ, HAT_LOAD_NOCONSIST); 2443ca3e8d88SDave Plauger HRSTOP(ds->perpage, map); 2444ca3e8d88SDave Plauger 2445ca3e8d88SDave Plauger ds->pages_mapped += btop(cp->size); 2446ca3e8d88SDave Plauger ds->pages_used += pagenum - cp->pagenum; 2447ca3e8d88SDave Plauger 2448ca3e8d88SDave Plauger CQ_OPEN(mainq); 2449ca3e8d88SDave Plauger 2450ca3e8d88SDave Plauger /* 2451ca3e8d88SDave Plauger * If there are no helpers the main task does 2452ca3e8d88SDave Plauger * non-streams lzjb compress. 2453ca3e8d88SDave Plauger */ 24549c84d1a2SDave Plauger if (dumpserial) { 2455ca3e8d88SDave Plauger dumpsys_lzjb_page(dumpcfg.helper, cp); 2456ca3e8d88SDave Plauger break; 2457ca3e8d88SDave Plauger } 2458ca3e8d88SDave Plauger 2459ca3e8d88SDave Plauger /* pass mapped pages to a helper */ 2460ca3e8d88SDave Plauger CQ_PUT(helperq, cp, CBUF_INREADY); 2461ca3e8d88SDave Plauger 2462ca3e8d88SDave Plauger /* the last page was done */ 2463ca3e8d88SDave Plauger if (bitnum >= dumpcfg.bitmapsize) 2464ca3e8d88SDave Plauger CQ_CLOSE(helperq); 2465ca3e8d88SDave Plauger 2466ca3e8d88SDave Plauger break; 2467ca3e8d88SDave Plauger 2468ca3e8d88SDave Plauger case CBUF_USEDMAP: 2469ca3e8d88SDave Plauger 2470ca3e8d88SDave Plauger ds->npages += btop(cp->used); 2471ca3e8d88SDave Plauger 2472ca3e8d88SDave Plauger HRSTART(ds->perpage, unmap); 2473ca3e8d88SDave Plauger hat_unload(kas.a_hat, cp->buf, cp->size, HAT_UNLOAD); 2474ca3e8d88SDave Plauger HRSTOP(ds->perpage, unmap); 2475ca3e8d88SDave Plauger 2476ca3e8d88SDave Plauger if (bitnum < dumpcfg.bitmapsize) 2477ca3e8d88SDave Plauger CQ_PUT(mainq, cp, CBUF_FREEMAP); 2478ca3e8d88SDave Plauger CQ_CLOSE(mainq); 2479ca3e8d88SDave Plauger 2480ca3e8d88SDave Plauger ASSERT(ds->npages <= dumphdr->dump_npages); 2481ca3e8d88SDave Plauger ds->percent = ds->npages * 100LL / dumphdr->dump_npages; 2482ca3e8d88SDave Plauger break; 2483ca3e8d88SDave Plauger 2484ca3e8d88SDave Plauger case CBUF_WRITE: 2485ca3e8d88SDave Plauger 2486ca3e8d88SDave Plauger CQ_PUT(writerq, cp, CBUF_WRITE); 2487ca3e8d88SDave Plauger break; 2488ca3e8d88SDave Plauger 2489ca3e8d88SDave Plauger case CBUF_ERRMSG: 2490ca3e8d88SDave Plauger 2491ca3e8d88SDave Plauger if (cp->used > 0) { 2492ca3e8d88SDave Plauger cp->buf[cp->size - 2] = '\n'; 2493ca3e8d88SDave Plauger cp->buf[cp->size - 1] = '\0'; 2494ca3e8d88SDave Plauger if (ds->neednl) { 2495ca3e8d88SDave Plauger uprintf("\n%s", cp->buf); 2496ca3e8d88SDave Plauger ds->neednl = 0; 2497ca3e8d88SDave Plauger } else { 2498ca3e8d88SDave Plauger uprintf("%s", cp->buf); 2499ca3e8d88SDave Plauger } 25009dd77bc8SDave Plauger /* wait for console output */ 25019dd77bc8SDave Plauger drv_usecwait(200000); 25029dd77bc8SDave Plauger dump_timeleft = dump_timeout; 2503ca3e8d88SDave Plauger } 2504ca3e8d88SDave Plauger CQ_PUT(freebufq, cp, CBUF_FREEBUF); 2505ca3e8d88SDave Plauger break; 2506ca3e8d88SDave Plauger 2507ca3e8d88SDave Plauger default: 2508ca3e8d88SDave Plauger uprintf("dump: unexpected buffer state %d, " 2509ca3e8d88SDave Plauger "buffer will be lost\n", cp->state); 2510ca3e8d88SDave Plauger break; 2511ca3e8d88SDave Plauger 2512ca3e8d88SDave Plauger } /* end switch */ 2513*2fb0949cSJoshua M. Clulow } 25147c478bd9Sstevel@tonic-gate } 25157c478bd9Sstevel@tonic-gate 2516ca3e8d88SDave Plauger #ifdef COLLECT_METRICS 2517ca3e8d88SDave Plauger size_t 2518ca3e8d88SDave Plauger dumpsys_metrics(dumpsync_t *ds, char *buf, size_t size) 25197c478bd9Sstevel@tonic-gate { 2520ca3e8d88SDave Plauger dumpcfg_t *cfg = &dumpcfg; 2521ca3e8d88SDave Plauger int myid = CPU->cpu_seqid; 2522ca3e8d88SDave Plauger int i, compress_ratio; 2523ca3e8d88SDave Plauger int sec, iorate; 2524ca3e8d88SDave Plauger helper_t *hp, *hpend = &cfg->helper[cfg->nhelper]; 2525ca3e8d88SDave Plauger char *e = buf + size; 2526ca3e8d88SDave Plauger char *p = buf; 2527ca3e8d88SDave Plauger 2528ca3e8d88SDave Plauger sec = ds->elapsed / (1000 * 1000 * 1000ULL); 2529ca3e8d88SDave Plauger if (sec < 1) 2530ca3e8d88SDave Plauger sec = 1; 2531ca3e8d88SDave Plauger 2532ca3e8d88SDave Plauger if (ds->iotime < 1) 2533ca3e8d88SDave Plauger ds->iotime = 1; 2534ca3e8d88SDave Plauger iorate = (ds->nwrite * 100000ULL) / ds->iotime; 2535ca3e8d88SDave Plauger 2536ca3e8d88SDave Plauger compress_ratio = 100LL * ds->npages / btopr(ds->nwrite + 1); 2537ca3e8d88SDave Plauger 2538ca3e8d88SDave Plauger #define P(...) (p += p < e ? snprintf(p, e - p, __VA_ARGS__) : 0) 2539ca3e8d88SDave Plauger 2540ca3e8d88SDave Plauger P("Master cpu_seqid,%d\n", CPU->cpu_seqid); 2541ca3e8d88SDave Plauger P("Master cpu_id,%d\n", CPU->cpu_id); 2542ca3e8d88SDave Plauger P("dump_flags,0x%x\n", dumphdr->dump_flags); 2543ca3e8d88SDave Plauger P("dump_ioerr,%d\n", dump_ioerr); 2544ca3e8d88SDave Plauger 2545ca3e8d88SDave Plauger P("Helpers:\n"); 2546ca3e8d88SDave Plauger for (i = 0; i < ncpus; i++) { 2547ca3e8d88SDave Plauger if ((i & 15) == 0) 2548ca3e8d88SDave Plauger P(",,%03d,", i); 2549ca3e8d88SDave Plauger if (i == myid) 2550ca3e8d88SDave Plauger P(" M"); 2551ca3e8d88SDave Plauger else if (BT_TEST(cfg->helpermap, i)) 2552ca3e8d88SDave Plauger P("%4d", cpu_seq[i]->cpu_id); 2553ca3e8d88SDave Plauger else 2554ca3e8d88SDave Plauger P(" *"); 2555ca3e8d88SDave Plauger if ((i & 15) == 15) 2556ca3e8d88SDave Plauger P("\n"); 2557ca3e8d88SDave Plauger } 25587c478bd9Sstevel@tonic-gate 2559ca3e8d88SDave Plauger P("ncbuf_used,%d\n", cfg->ncbuf_used); 2560ca3e8d88SDave Plauger P("ncmap,%d\n", cfg->ncmap); 2561ca3e8d88SDave Plauger 2562ca3e8d88SDave Plauger P("Found %ldM ranges,%ld\n", (CBUF_MAPSIZE / DUMP_1MB), cfg->found4m); 2563ca3e8d88SDave Plauger P("Found small pages,%ld\n", cfg->foundsm); 2564ca3e8d88SDave Plauger 2565ca3e8d88SDave Plauger P("Compression level,%d\n", cfg->clevel); 2566ca3e8d88SDave Plauger P("Compression type,%s %s\n", cfg->clevel == 0 ? "serial" : "parallel", 2567ca3e8d88SDave Plauger cfg->clevel >= DUMP_CLEVEL_BZIP2 ? "bzip2" : "lzjb"); 2568ca3e8d88SDave Plauger P("Compression ratio,%d.%02d\n", compress_ratio / 100, compress_ratio % 2569ca3e8d88SDave Plauger 100); 2570ca3e8d88SDave Plauger P("nhelper_used,%d\n", cfg->nhelper_used); 2571ca3e8d88SDave Plauger 2572ca3e8d88SDave Plauger P("Dump I/O rate MBS,%d.%02d\n", iorate / 100, iorate % 100); 2573ca3e8d88SDave Plauger P("..total bytes,%lld\n", (u_longlong_t)ds->nwrite); 2574ca3e8d88SDave Plauger P("..total nsec,%lld\n", (u_longlong_t)ds->iotime); 2575ca3e8d88SDave Plauger P("dumpbuf.iosize,%ld\n", dumpbuf.iosize); 2576ca3e8d88SDave Plauger P("dumpbuf.size,%ld\n", dumpbuf.size); 2577ca3e8d88SDave Plauger 2578ca3e8d88SDave Plauger P("Dump pages/sec,%llu\n", (u_longlong_t)ds->npages / sec); 2579ca3e8d88SDave Plauger P("Dump pages,%llu\n", (u_longlong_t)ds->npages); 2580ca3e8d88SDave Plauger P("Dump time,%d\n", sec); 2581ca3e8d88SDave Plauger 2582ca3e8d88SDave Plauger if (ds->pages_mapped > 0) 2583ca3e8d88SDave Plauger P("per-cent map utilization,%d\n", (int)((100 * ds->pages_used) 2584ca3e8d88SDave Plauger / ds->pages_mapped)); 2585ca3e8d88SDave Plauger 2586ca3e8d88SDave Plauger P("\nPer-page metrics:\n"); 2587ca3e8d88SDave Plauger if (ds->npages > 0) { 2588ca3e8d88SDave Plauger for (hp = cfg->helper; hp != hpend; hp++) { 2589ca3e8d88SDave Plauger #define PERPAGE(x) ds->perpage.x += hp->perpage.x; 2590ca3e8d88SDave Plauger PERPAGES; 2591ca3e8d88SDave Plauger #undef PERPAGE 25927c478bd9Sstevel@tonic-gate } 2593ca3e8d88SDave Plauger #define PERPAGE(x) \ 2594ca3e8d88SDave Plauger P("%s nsec/page,%d\n", #x, (int)(ds->perpage.x / ds->npages)); 2595ca3e8d88SDave Plauger PERPAGES; 2596ca3e8d88SDave Plauger #undef PERPAGE 2597ca3e8d88SDave Plauger P("freebufq.empty,%d\n", (int)(ds->freebufq.empty / 2598ca3e8d88SDave Plauger ds->npages)); 2599ca3e8d88SDave Plauger P("helperq.empty,%d\n", (int)(ds->helperq.empty / 2600ca3e8d88SDave Plauger ds->npages)); 2601ca3e8d88SDave Plauger P("writerq.empty,%d\n", (int)(ds->writerq.empty / 2602ca3e8d88SDave Plauger ds->npages)); 2603ca3e8d88SDave Plauger P("mainq.empty,%d\n", (int)(ds->mainq.empty / ds->npages)); 2604ca3e8d88SDave Plauger 2605ca3e8d88SDave Plauger P("I/O wait nsec/page,%llu\n", (u_longlong_t)(ds->iowait / 2606ca3e8d88SDave Plauger ds->npages)); 26077c478bd9Sstevel@tonic-gate } 2608ca3e8d88SDave Plauger #undef P 2609ca3e8d88SDave Plauger if (p < e) 2610ca3e8d88SDave Plauger bzero(p, e - p); 2611ca3e8d88SDave Plauger return (p - buf); 26127c478bd9Sstevel@tonic-gate } 2613ca3e8d88SDave Plauger #endif /* COLLECT_METRICS */ 26147c478bd9Sstevel@tonic-gate 26157c478bd9Sstevel@tonic-gate /* 26167c478bd9Sstevel@tonic-gate * Dump the system. 26177c478bd9Sstevel@tonic-gate */ 26187c478bd9Sstevel@tonic-gate void 26197c478bd9Sstevel@tonic-gate dumpsys(void) 26207c478bd9Sstevel@tonic-gate { 2621ca3e8d88SDave Plauger dumpsync_t *ds = &dumpsync; 2622ca3e8d88SDave Plauger taskq_t *livetaskq = NULL; 26237c478bd9Sstevel@tonic-gate pfn_t pfn; 26247c478bd9Sstevel@tonic-gate pgcnt_t bitnum; 26257c478bd9Sstevel@tonic-gate proc_t *p; 2626ca3e8d88SDave Plauger helper_t *hp, *hpend = &dumpcfg.helper[dumpcfg.nhelper]; 2627ca3e8d88SDave Plauger cbuf_t *cp; 26287c478bd9Sstevel@tonic-gate pid_t npids, pidx; 26297c478bd9Sstevel@tonic-gate char *content; 26309dd77bc8SDave Plauger char *buf; 26319dd77bc8SDave Plauger size_t size; 2632ca3e8d88SDave Plauger int save_dump_clevel; 2633ca3e8d88SDave Plauger dumpmlw_t mlw; 2634ca3e8d88SDave Plauger dumpcsize_t datatag; 2635ca3e8d88SDave Plauger dumpdatahdr_t datahdr; 26367c478bd9Sstevel@tonic-gate 26377c478bd9Sstevel@tonic-gate if (dumpvp == NULL || dumphdr == NULL) { 26387c478bd9Sstevel@tonic-gate uprintf("skipping system dump - no dump device configured\n"); 2639ca3e8d88SDave Plauger if (panicstr) { 2640ca3e8d88SDave Plauger dumpcfg.helpers_wanted = 0; 2641ca3e8d88SDave Plauger dumpsys_spinunlock(&dumpcfg.helper_lock); 2642ca3e8d88SDave Plauger } 26437c478bd9Sstevel@tonic-gate return; 26447c478bd9Sstevel@tonic-gate } 2645ca3e8d88SDave Plauger dumpbuf.cur = dumpbuf.start; 2646ca3e8d88SDave Plauger 2647ca3e8d88SDave Plauger /* clear the sync variables */ 2648ca3e8d88SDave Plauger ASSERT(dumpcfg.nhelper > 0); 2649ca3e8d88SDave Plauger bzero(ds, sizeof (*ds)); 2650ca3e8d88SDave Plauger ds->dumpcpu = CPU->cpu_id; 26517c478bd9Sstevel@tonic-gate 26527c478bd9Sstevel@tonic-gate /* 26537c478bd9Sstevel@tonic-gate * Calculate the starting block for dump. If we're dumping on a 26547c478bd9Sstevel@tonic-gate * swap device, start 1/5 of the way in; otherwise, start at the 26557c478bd9Sstevel@tonic-gate * beginning. And never use the first page -- it may be a disk label. 26567c478bd9Sstevel@tonic-gate */ 26577c478bd9Sstevel@tonic-gate if (dumpvp->v_flag & VISSWAP) 26587c478bd9Sstevel@tonic-gate dumphdr->dump_start = P2ROUNDUP(dumpvp_size / 5, DUMP_OFFSET); 26597c478bd9Sstevel@tonic-gate else 26607c478bd9Sstevel@tonic-gate dumphdr->dump_start = DUMP_OFFSET; 26617c478bd9Sstevel@tonic-gate 2662ca3e8d88SDave Plauger dumphdr->dump_flags = DF_VALID | DF_COMPLETE | DF_LIVE | DF_COMPRESSED; 26637c478bd9Sstevel@tonic-gate dumphdr->dump_crashtime = gethrestime_sec(); 26647c478bd9Sstevel@tonic-gate dumphdr->dump_npages = 0; 26657c478bd9Sstevel@tonic-gate dumphdr->dump_nvtop = 0; 2666ca3e8d88SDave Plauger bzero(dumpcfg.bitmap, BT_SIZEOFMAP(dumpcfg.bitmapsize)); 26677c478bd9Sstevel@tonic-gate dump_timeleft = dump_timeout; 26687c478bd9Sstevel@tonic-gate 26697c478bd9Sstevel@tonic-gate if (panicstr) { 26707c478bd9Sstevel@tonic-gate dumphdr->dump_flags &= ~DF_LIVE; 2671da6c28aaSamw (void) VOP_DUMPCTL(dumpvp, DUMP_FREE, NULL, NULL); 2672da6c28aaSamw (void) VOP_DUMPCTL(dumpvp, DUMP_ALLOC, NULL, NULL); 26737c478bd9Sstevel@tonic-gate (void) vsnprintf(dumphdr->dump_panicstring, DUMP_PANICSIZE, 26747c478bd9Sstevel@tonic-gate panicstr, panicargs); 2675ca3e8d88SDave Plauger 26767c478bd9Sstevel@tonic-gate } 26777c478bd9Sstevel@tonic-gate 26787c478bd9Sstevel@tonic-gate if (dump_conflags & DUMP_ALL) 26797c478bd9Sstevel@tonic-gate content = "all"; 26807c478bd9Sstevel@tonic-gate else if (dump_conflags & DUMP_CURPROC) 26817c478bd9Sstevel@tonic-gate content = "kernel + curproc"; 26827c478bd9Sstevel@tonic-gate else 26837c478bd9Sstevel@tonic-gate content = "kernel"; 26847c478bd9Sstevel@tonic-gate uprintf("dumping to %s, offset %lld, content: %s\n", dumppath, 26857c478bd9Sstevel@tonic-gate dumphdr->dump_start, content); 26867c478bd9Sstevel@tonic-gate 2687ca3e8d88SDave Plauger /* Make sure nodename is current */ 2688ca3e8d88SDave Plauger bcopy(utsname.nodename, dumphdr->dump_utsname.nodename, SYS_NMLN); 2689ca3e8d88SDave Plauger 2690ca3e8d88SDave Plauger /* 2691ca3e8d88SDave Plauger * If this is a live dump, try to open a VCHR vnode for better 2692ca3e8d88SDave Plauger * performance. We must take care to flush the buffer cache 2693ca3e8d88SDave Plauger * first. 2694ca3e8d88SDave Plauger */ 2695ca3e8d88SDave Plauger if (!panicstr) { 2696ca3e8d88SDave Plauger vnode_t *cdev_vp, *cmn_cdev_vp; 2697ca3e8d88SDave Plauger 2698ca3e8d88SDave Plauger ASSERT(dumpbuf.cdev_vp == NULL); 2699ca3e8d88SDave Plauger cdev_vp = makespecvp(VTOS(dumpvp)->s_dev, VCHR); 2700ca3e8d88SDave Plauger if (cdev_vp != NULL) { 2701ca3e8d88SDave Plauger cmn_cdev_vp = common_specvp(cdev_vp); 2702ca3e8d88SDave Plauger if (VOP_OPEN(&cmn_cdev_vp, FREAD | FWRITE, kcred, NULL) 2703ca3e8d88SDave Plauger == 0) { 2704ca3e8d88SDave Plauger if (vn_has_cached_data(dumpvp)) 2705ca3e8d88SDave Plauger (void) pvn_vplist_dirty(dumpvp, 0, NULL, 2706ca3e8d88SDave Plauger B_INVAL | B_TRUNC, kcred); 2707ca3e8d88SDave Plauger dumpbuf.cdev_vp = cmn_cdev_vp; 2708ca3e8d88SDave Plauger } else { 2709ca3e8d88SDave Plauger VN_RELE(cdev_vp); 2710ca3e8d88SDave Plauger } 2711ca3e8d88SDave Plauger } 2712ca3e8d88SDave Plauger } 2713ca3e8d88SDave Plauger 2714d3d50737SRafael Vanoni /* 2715d3d50737SRafael Vanoni * Store a hires timestamp so we can look it up during debugging. 2716d3d50737SRafael Vanoni */ 2717d3d50737SRafael Vanoni lbolt_debug_entry(); 2718d3d50737SRafael Vanoni 27197c478bd9Sstevel@tonic-gate /* 27207c478bd9Sstevel@tonic-gate * Leave room for the message and ereport save areas and terminal dump 27217c478bd9Sstevel@tonic-gate * header. 27227c478bd9Sstevel@tonic-gate */ 2723ca3e8d88SDave Plauger dumpbuf.vp_limit = dumpvp_size - DUMP_LOGSIZE - DUMP_OFFSET - 2724ca3e8d88SDave Plauger DUMP_ERPTSIZE; 27257c478bd9Sstevel@tonic-gate 27267c478bd9Sstevel@tonic-gate /* 27277c478bd9Sstevel@tonic-gate * Write out the symbol table. It's no longer compressed, 27287c478bd9Sstevel@tonic-gate * so its 'size' and 'csize' are equal. 27297c478bd9Sstevel@tonic-gate */ 2730ca3e8d88SDave Plauger dumpbuf.vp_off = dumphdr->dump_ksyms = dumphdr->dump_start + PAGESIZE; 27317c478bd9Sstevel@tonic-gate dumphdr->dump_ksyms_size = dumphdr->dump_ksyms_csize = 27327c478bd9Sstevel@tonic-gate ksyms_snapshot(dumpvp_ksyms_write, NULL, LONG_MAX); 27337c478bd9Sstevel@tonic-gate 27347c478bd9Sstevel@tonic-gate /* 27357c478bd9Sstevel@tonic-gate * Write out the translation map. 27367c478bd9Sstevel@tonic-gate */ 27377c478bd9Sstevel@tonic-gate dumphdr->dump_map = dumpvp_flush(); 27387c478bd9Sstevel@tonic-gate dump_as(&kas); 2739ae115bc7Smrj dumphdr->dump_nvtop += dump_plat_addr(); 27407c478bd9Sstevel@tonic-gate 27417c478bd9Sstevel@tonic-gate /* 27427c478bd9Sstevel@tonic-gate * call into hat, which may have unmapped pages that also need to 27437c478bd9Sstevel@tonic-gate * be in the dump 27447c478bd9Sstevel@tonic-gate */ 27457c478bd9Sstevel@tonic-gate hat_dump(); 27467c478bd9Sstevel@tonic-gate 27477c478bd9Sstevel@tonic-gate if (dump_conflags & DUMP_ALL) { 27487c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 27497c478bd9Sstevel@tonic-gate 27507c478bd9Sstevel@tonic-gate for (npids = 0, p = practive; p != NULL; p = p->p_next) 2751ca3e8d88SDave Plauger dumpcfg.pids[npids++] = p->p_pid; 27527c478bd9Sstevel@tonic-gate 27537c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 27547c478bd9Sstevel@tonic-gate 27557c478bd9Sstevel@tonic-gate for (pidx = 0; pidx < npids; pidx++) 2756ca3e8d88SDave Plauger (void) dump_process(dumpcfg.pids[pidx]); 27577c478bd9Sstevel@tonic-gate 275886f21945SDave Plauger dump_init_memlist_walker(&mlw); 2759ca3e8d88SDave Plauger for (bitnum = 0; bitnum < dumpcfg.bitmapsize; bitnum++) { 27607c478bd9Sstevel@tonic-gate dump_timeleft = dump_timeout; 2761b280b2a3SStuart Maybee pfn = dump_bitnum_to_pfn(bitnum, &mlw); 2762b280b2a3SStuart Maybee /* 2763b280b2a3SStuart Maybee * Some hypervisors do not have all pages available to 2764b280b2a3SStuart Maybee * be accessed by the guest OS. Check for page 2765b280b2a3SStuart Maybee * accessibility. 2766b280b2a3SStuart Maybee */ 2767b280b2a3SStuart Maybee if (plat_hold_page(pfn, PLAT_HOLD_NO_LOCK, NULL) != 2768b280b2a3SStuart Maybee PLAT_HOLD_OK) 2769b280b2a3SStuart Maybee continue; 2770ca3e8d88SDave Plauger BT_SET(dumpcfg.bitmap, bitnum); 27717c478bd9Sstevel@tonic-gate } 2772ca3e8d88SDave Plauger dumphdr->dump_npages = dumpcfg.bitmapsize; 27737c478bd9Sstevel@tonic-gate dumphdr->dump_flags |= DF_ALL; 27747c478bd9Sstevel@tonic-gate 27757c478bd9Sstevel@tonic-gate } else if (dump_conflags & DUMP_CURPROC) { 27767c478bd9Sstevel@tonic-gate /* 27777c478bd9Sstevel@tonic-gate * Determine which pid is to be dumped. If we're panicking, we 27787c478bd9Sstevel@tonic-gate * dump the process associated with panic_thread (if any). If 27797c478bd9Sstevel@tonic-gate * this is a live dump, we dump the process associated with 27807c478bd9Sstevel@tonic-gate * curthread. 27817c478bd9Sstevel@tonic-gate */ 27827c478bd9Sstevel@tonic-gate npids = 0; 27837c478bd9Sstevel@tonic-gate if (panicstr) { 27847c478bd9Sstevel@tonic-gate if (panic_thread != NULL && 27857c478bd9Sstevel@tonic-gate panic_thread->t_procp != NULL && 27867c478bd9Sstevel@tonic-gate panic_thread->t_procp != &p0) { 2787ca3e8d88SDave Plauger dumpcfg.pids[npids++] = 27887c478bd9Sstevel@tonic-gate panic_thread->t_procp->p_pid; 27897c478bd9Sstevel@tonic-gate } 27907c478bd9Sstevel@tonic-gate } else { 2791ca3e8d88SDave Plauger dumpcfg.pids[npids++] = curthread->t_procp->p_pid; 27927c478bd9Sstevel@tonic-gate } 27937c478bd9Sstevel@tonic-gate 2794ca3e8d88SDave Plauger if (npids && dump_process(dumpcfg.pids[0]) == 0) 27957c478bd9Sstevel@tonic-gate dumphdr->dump_flags |= DF_CURPROC; 27967c478bd9Sstevel@tonic-gate else 27977c478bd9Sstevel@tonic-gate dumphdr->dump_flags |= DF_KERNEL; 27987c478bd9Sstevel@tonic-gate 27997c478bd9Sstevel@tonic-gate } else { 28007c478bd9Sstevel@tonic-gate dumphdr->dump_flags |= DF_KERNEL; 28017c478bd9Sstevel@tonic-gate } 28027c478bd9Sstevel@tonic-gate 28037c478bd9Sstevel@tonic-gate dumphdr->dump_hashmask = (1 << highbit(dumphdr->dump_nvtop - 1)) - 1; 28047c478bd9Sstevel@tonic-gate 28057c478bd9Sstevel@tonic-gate /* 28067c478bd9Sstevel@tonic-gate * Write out the pfn table. 28077c478bd9Sstevel@tonic-gate */ 28087c478bd9Sstevel@tonic-gate dumphdr->dump_pfn = dumpvp_flush(); 2809ca3e8d88SDave Plauger dump_init_memlist_walker(&mlw); 2810ca3e8d88SDave Plauger for (bitnum = 0; bitnum < dumpcfg.bitmapsize; bitnum++) { 28117c478bd9Sstevel@tonic-gate dump_timeleft = dump_timeout; 2812ca3e8d88SDave Plauger if (!BT_TEST(dumpcfg.bitmap, bitnum)) 28137c478bd9Sstevel@tonic-gate continue; 2814ca3e8d88SDave Plauger pfn = dump_bitnum_to_pfn(bitnum, &mlw); 28157c478bd9Sstevel@tonic-gate ASSERT(pfn != PFN_INVALID); 28167c478bd9Sstevel@tonic-gate dumpvp_write(&pfn, sizeof (pfn_t)); 28177c478bd9Sstevel@tonic-gate } 2818ae115bc7Smrj dump_plat_pfn(); 28197c478bd9Sstevel@tonic-gate 28207c478bd9Sstevel@tonic-gate /* 28217c478bd9Sstevel@tonic-gate * Write out all the pages. 2822ca3e8d88SDave Plauger * Map pages, copy them handling UEs, compress, and write them out. 2823ca3e8d88SDave Plauger * Cooperate with any helpers running on CPUs in panic_idle(). 28247c478bd9Sstevel@tonic-gate */ 28257c478bd9Sstevel@tonic-gate dumphdr->dump_data = dumpvp_flush(); 2826ca3e8d88SDave Plauger 2827ca3e8d88SDave Plauger bzero(dumpcfg.helpermap, BT_SIZEOFMAP(NCPU)); 2828ca3e8d88SDave Plauger ds->live = dumpcfg.clevel > 0 && 2829ca3e8d88SDave Plauger (dumphdr->dump_flags & DF_LIVE) != 0; 2830ca3e8d88SDave Plauger 2831ca3e8d88SDave Plauger save_dump_clevel = dumpcfg.clevel; 2832ca3e8d88SDave Plauger if (panicstr) 2833ca3e8d88SDave Plauger dumpsys_get_maxmem(); 2834ca3e8d88SDave Plauger else if (dumpcfg.clevel >= DUMP_CLEVEL_BZIP2) 2835ca3e8d88SDave Plauger dumpcfg.clevel = DUMP_CLEVEL_LZJB; 2836ca3e8d88SDave Plauger 2837ca3e8d88SDave Plauger dumpcfg.nhelper_used = 0; 2838ca3e8d88SDave Plauger for (hp = dumpcfg.helper; hp != hpend; hp++) { 2839ca3e8d88SDave Plauger if (hp->page == NULL) { 2840ca3e8d88SDave Plauger hp->helper = DONEHELPER; 28417c478bd9Sstevel@tonic-gate continue; 2842ca3e8d88SDave Plauger } 2843ca3e8d88SDave Plauger ++dumpcfg.nhelper_used; 2844ca3e8d88SDave Plauger hp->helper = FREEHELPER; 2845ca3e8d88SDave Plauger hp->taskqid = NULL; 2846ca3e8d88SDave Plauger hp->ds = ds; 2847ca3e8d88SDave Plauger bzero(&hp->perpage, sizeof (hp->perpage)); 2848ca3e8d88SDave Plauger if (dumpcfg.clevel >= DUMP_CLEVEL_BZIP2) 2849ca3e8d88SDave Plauger (void) BZ2_bzCompressReset(&hp->bzstream); 2850ca3e8d88SDave Plauger } 28517c478bd9Sstevel@tonic-gate 2852ca3e8d88SDave Plauger CQ_OPEN(freebufq); 2853ca3e8d88SDave Plauger CQ_OPEN(helperq); 2854ca3e8d88SDave Plauger 2855ca3e8d88SDave Plauger dumpcfg.ncbuf_used = 0; 2856ca3e8d88SDave Plauger for (cp = dumpcfg.cbuf; cp != &dumpcfg.cbuf[dumpcfg.ncbuf]; cp++) { 2857ca3e8d88SDave Plauger if (cp->buf != NULL) { 2858ca3e8d88SDave Plauger CQ_PUT(freebufq, cp, CBUF_FREEBUF); 2859ca3e8d88SDave Plauger ++dumpcfg.ncbuf_used; 28607c478bd9Sstevel@tonic-gate } 2861ca3e8d88SDave Plauger } 2862ca3e8d88SDave Plauger 2863ca3e8d88SDave Plauger for (cp = dumpcfg.cmap; cp != &dumpcfg.cmap[dumpcfg.ncmap]; cp++) 2864ca3e8d88SDave Plauger CQ_PUT(mainq, cp, CBUF_FREEMAP); 2865ca3e8d88SDave Plauger 2866ca3e8d88SDave Plauger ds->start = gethrtime(); 2867ca3e8d88SDave Plauger ds->iowaitts = ds->start; 2868ca3e8d88SDave Plauger 2869ca3e8d88SDave Plauger /* start helpers */ 2870ca3e8d88SDave Plauger if (ds->live) { 2871ca3e8d88SDave Plauger int n = dumpcfg.nhelper_used; 2872ca3e8d88SDave Plauger int pri = MINCLSYSPRI - 25; 2873ca3e8d88SDave Plauger 2874ca3e8d88SDave Plauger livetaskq = taskq_create("LiveDump", n, pri, n, n, 2875ca3e8d88SDave Plauger TASKQ_PREPOPULATE); 2876ca3e8d88SDave Plauger for (hp = dumpcfg.helper; hp != hpend; hp++) { 2877ca3e8d88SDave Plauger if (hp->page == NULL) 2878ca3e8d88SDave Plauger continue; 2879ca3e8d88SDave Plauger hp->helper = hp - dumpcfg.helper; 2880ca3e8d88SDave Plauger hp->taskqid = taskq_dispatch(livetaskq, 2881ca3e8d88SDave Plauger dumpsys_live_helper, (void *)hp, TQ_NOSLEEP); 28827c478bd9Sstevel@tonic-gate } 2883ca3e8d88SDave Plauger 2884ca3e8d88SDave Plauger } else { 28859dd77bc8SDave Plauger if (panicstr) 28869dd77bc8SDave Plauger kmem_dump_begin(); 2887ca3e8d88SDave Plauger dumpcfg.helpers_wanted = dumpcfg.clevel > 0; 2888ca3e8d88SDave Plauger dumpsys_spinunlock(&dumpcfg.helper_lock); 28897c478bd9Sstevel@tonic-gate } 28907c478bd9Sstevel@tonic-gate 2891ca3e8d88SDave Plauger /* run main task */ 2892ca3e8d88SDave Plauger dumpsys_main_task(ds); 2893ca3e8d88SDave Plauger 2894ca3e8d88SDave Plauger ds->elapsed = gethrtime() - ds->start; 2895ca3e8d88SDave Plauger if (ds->elapsed < 1) 2896ca3e8d88SDave Plauger ds->elapsed = 1; 2897ca3e8d88SDave Plauger 2898ca3e8d88SDave Plauger if (livetaskq != NULL) 2899ca3e8d88SDave Plauger taskq_destroy(livetaskq); 2900ca3e8d88SDave Plauger 2901ca3e8d88SDave Plauger if (ds->neednl) { 2902ca3e8d88SDave Plauger uprintf("\n"); 2903ca3e8d88SDave Plauger ds->neednl = 0; 2904ca3e8d88SDave Plauger } 2905ca3e8d88SDave Plauger 2906ca3e8d88SDave Plauger /* record actual pages dumped */ 2907ca3e8d88SDave Plauger dumphdr->dump_npages = ds->npages; 2908ca3e8d88SDave Plauger 2909ca3e8d88SDave Plauger /* platform-specific data */ 2910ca3e8d88SDave Plauger dumphdr->dump_npages += dump_plat_data(dumpcfg.cbuf[0].buf); 2911ca3e8d88SDave Plauger 2912ca3e8d88SDave Plauger /* note any errors by clearing DF_COMPLETE */ 2913ca3e8d88SDave Plauger if (dump_ioerr || ds->npages < dumphdr->dump_npages) 2914ca3e8d88SDave Plauger dumphdr->dump_flags &= ~DF_COMPLETE; 2915ca3e8d88SDave Plauger 2916ca3e8d88SDave Plauger /* end of stream blocks */ 2917ca3e8d88SDave Plauger datatag = 0; 2918ca3e8d88SDave Plauger dumpvp_write(&datatag, sizeof (datatag)); 2919ca3e8d88SDave Plauger 2920ca3e8d88SDave Plauger bzero(&datahdr, sizeof (datahdr)); 29219dd77bc8SDave Plauger 29229dd77bc8SDave Plauger /* buffer for metrics */ 29239dd77bc8SDave Plauger buf = dumpcfg.cbuf[0].buf; 29249dd77bc8SDave Plauger size = MIN(dumpcfg.cbuf[0].size, DUMP_OFFSET - sizeof (dumphdr_t) - 29259dd77bc8SDave Plauger sizeof (dumpdatahdr_t)); 29269dd77bc8SDave Plauger 29279dd77bc8SDave Plauger /* finish the kmem intercepts, collect kmem verbose info */ 29289dd77bc8SDave Plauger if (panicstr) { 29299dd77bc8SDave Plauger datahdr.dump_metrics = kmem_dump_finish(buf, size); 29309dd77bc8SDave Plauger buf += datahdr.dump_metrics; 29319dd77bc8SDave Plauger size -= datahdr.dump_metrics; 29329dd77bc8SDave Plauger } 29339dd77bc8SDave Plauger 2934f6e214c7SGavin Maltby /* record in the header whether this is a fault-management panic */ 2935f6e214c7SGavin Maltby if (panicstr) 2936f6e214c7SGavin Maltby dumphdr->dump_fm_panic = is_fm_panic(); 2937f6e214c7SGavin Maltby 29389dd77bc8SDave Plauger /* compression info in data header */ 2939ca3e8d88SDave Plauger datahdr.dump_datahdr_magic = DUMP_DATAHDR_MAGIC; 2940ca3e8d88SDave Plauger datahdr.dump_datahdr_version = DUMP_DATAHDR_VERSION; 2941ca3e8d88SDave Plauger datahdr.dump_maxcsize = CBUF_SIZE; 2942ca3e8d88SDave Plauger datahdr.dump_maxrange = CBUF_MAPSIZE / PAGESIZE; 2943ca3e8d88SDave Plauger datahdr.dump_nstreams = dumpcfg.nhelper_used; 2944ca3e8d88SDave Plauger datahdr.dump_clevel = dumpcfg.clevel; 2945ca3e8d88SDave Plauger #ifdef COLLECT_METRICS 2946ca3e8d88SDave Plauger if (dump_metrics_on) 29479dd77bc8SDave Plauger datahdr.dump_metrics += dumpsys_metrics(ds, buf, size); 2948ca3e8d88SDave Plauger #endif 2949ca3e8d88SDave Plauger datahdr.dump_data_csize = dumpvp_flush() - dumphdr->dump_data; 29507c478bd9Sstevel@tonic-gate 29517c478bd9Sstevel@tonic-gate /* 29527c478bd9Sstevel@tonic-gate * Write out the initial and terminal dump headers. 29537c478bd9Sstevel@tonic-gate */ 2954ca3e8d88SDave Plauger dumpbuf.vp_off = dumphdr->dump_start; 29557c478bd9Sstevel@tonic-gate dumpvp_write(dumphdr, sizeof (dumphdr_t)); 29567c478bd9Sstevel@tonic-gate (void) dumpvp_flush(); 29577c478bd9Sstevel@tonic-gate 2958ca3e8d88SDave Plauger dumpbuf.vp_limit = dumpvp_size; 2959ca3e8d88SDave Plauger dumpbuf.vp_off = dumpbuf.vp_limit - DUMP_OFFSET; 29607c478bd9Sstevel@tonic-gate dumpvp_write(dumphdr, sizeof (dumphdr_t)); 2961ca3e8d88SDave Plauger dumpvp_write(&datahdr, sizeof (dumpdatahdr_t)); 2962ca3e8d88SDave Plauger dumpvp_write(dumpcfg.cbuf[0].buf, datahdr.dump_metrics); 29637c478bd9Sstevel@tonic-gate 2964ca3e8d88SDave Plauger (void) dumpvp_flush(); 29657c478bd9Sstevel@tonic-gate 2966ca3e8d88SDave Plauger uprintf("\r%3d%% done: %llu pages dumped, ", 2967ca3e8d88SDave Plauger ds->percent_done, (u_longlong_t)ds->npages); 29687c478bd9Sstevel@tonic-gate 29697c478bd9Sstevel@tonic-gate if (dump_ioerr == 0) { 29707c478bd9Sstevel@tonic-gate uprintf("dump succeeded\n"); 29717c478bd9Sstevel@tonic-gate } else { 29727c478bd9Sstevel@tonic-gate uprintf("dump failed: error %d\n", dump_ioerr); 2973ca3e8d88SDave Plauger #ifdef DEBUG 2974ca3e8d88SDave Plauger if (panicstr) 29757c478bd9Sstevel@tonic-gate debug_enter("dump failed"); 2976ca3e8d88SDave Plauger #endif 29777c478bd9Sstevel@tonic-gate } 29787c478bd9Sstevel@tonic-gate 29797c478bd9Sstevel@tonic-gate /* 29807c478bd9Sstevel@tonic-gate * Write out all undelivered messages. This has to be the *last* 29817c478bd9Sstevel@tonic-gate * thing we do because the dump process itself emits messages. 29827c478bd9Sstevel@tonic-gate */ 29837c478bd9Sstevel@tonic-gate if (panicstr) { 2984f6e214c7SGavin Maltby dump_summary(); 29857c478bd9Sstevel@tonic-gate dump_ereports(); 29867c478bd9Sstevel@tonic-gate dump_messages(); 29877c478bd9Sstevel@tonic-gate } 29887c478bd9Sstevel@tonic-gate 29897c478bd9Sstevel@tonic-gate delay(2 * hz); /* let people see the 'done' message */ 29907c478bd9Sstevel@tonic-gate dump_timeleft = 0; 29917c478bd9Sstevel@tonic-gate dump_ioerr = 0; 2992ca3e8d88SDave Plauger 2993ca3e8d88SDave Plauger /* restore settings after live dump completes */ 2994ca3e8d88SDave Plauger if (!panicstr) { 2995ca3e8d88SDave Plauger dumpcfg.clevel = save_dump_clevel; 2996ca3e8d88SDave Plauger 2997ca3e8d88SDave Plauger /* release any VCHR open of the dump device */ 2998ca3e8d88SDave Plauger if (dumpbuf.cdev_vp != NULL) { 2999ca3e8d88SDave Plauger (void) VOP_CLOSE(dumpbuf.cdev_vp, FREAD | FWRITE, 1, 0, 3000ca3e8d88SDave Plauger kcred, NULL); 3001ca3e8d88SDave Plauger VN_RELE(dumpbuf.cdev_vp); 3002ca3e8d88SDave Plauger dumpbuf.cdev_vp = NULL; 3003ca3e8d88SDave Plauger } 3004ca3e8d88SDave Plauger } 30057c478bd9Sstevel@tonic-gate } 30067c478bd9Sstevel@tonic-gate 30077c478bd9Sstevel@tonic-gate /* 30087c478bd9Sstevel@tonic-gate * This function is called whenever the memory size, as represented 30097c478bd9Sstevel@tonic-gate * by the phys_install list, changes. 30107c478bd9Sstevel@tonic-gate */ 30117c478bd9Sstevel@tonic-gate void 30127c478bd9Sstevel@tonic-gate dump_resize() 30137c478bd9Sstevel@tonic-gate { 30147c478bd9Sstevel@tonic-gate mutex_enter(&dump_lock); 30157c478bd9Sstevel@tonic-gate dumphdr_init(); 30167c478bd9Sstevel@tonic-gate dumpbuf_resize(); 3017ca3e8d88SDave Plauger dump_update_clevel(); 30187c478bd9Sstevel@tonic-gate mutex_exit(&dump_lock); 30197c478bd9Sstevel@tonic-gate } 3020e7cbe64fSgw 3021e7cbe64fSgw /* 3022e7cbe64fSgw * This function allows for dynamic resizing of a dump area. It assumes that 3023e7cbe64fSgw * the underlying device has update its appropriate size(9P). 3024e7cbe64fSgw */ 3025e7cbe64fSgw int 3026e7cbe64fSgw dumpvp_resize() 3027e7cbe64fSgw { 3028e7cbe64fSgw int error; 3029e7cbe64fSgw vattr_t vattr; 3030e7cbe64fSgw 3031e7cbe64fSgw mutex_enter(&dump_lock); 3032e7cbe64fSgw vattr.va_mask = AT_SIZE; 3033e7cbe64fSgw if ((error = VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL)) != 0) { 3034e7cbe64fSgw mutex_exit(&dump_lock); 3035e7cbe64fSgw return (error); 3036e7cbe64fSgw } 3037e7cbe64fSgw 3038e7cbe64fSgw if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE) { 3039e7cbe64fSgw mutex_exit(&dump_lock); 3040e7cbe64fSgw return (ENOSPC); 3041e7cbe64fSgw } 3042e7cbe64fSgw 3043e7cbe64fSgw dumpvp_size = vattr.va_size & -DUMP_OFFSET; 3044e7cbe64fSgw mutex_exit(&dump_lock); 3045e7cbe64fSgw return (0); 3046e7cbe64fSgw } 3047f6e214c7SGavin Maltby 3048f6e214c7SGavin Maltby int 3049f6e214c7SGavin Maltby dump_set_uuid(const char *uuidstr) 3050f6e214c7SGavin Maltby { 3051f6e214c7SGavin Maltby const char *ptr; 3052f6e214c7SGavin Maltby int i; 3053f6e214c7SGavin Maltby 3054f6e214c7SGavin Maltby if (uuidstr == NULL || strnlen(uuidstr, 36 + 1) != 36) 3055f6e214c7SGavin Maltby return (EINVAL); 3056f6e214c7SGavin Maltby 3057f6e214c7SGavin Maltby /* uuid_parse is not common code so check manually */ 3058f6e214c7SGavin Maltby for (i = 0, ptr = uuidstr; i < 36; i++, ptr++) { 3059f6e214c7SGavin Maltby switch (i) { 3060f6e214c7SGavin Maltby case 8: 3061f6e214c7SGavin Maltby case 13: 3062f6e214c7SGavin Maltby case 18: 3063f6e214c7SGavin Maltby case 23: 3064f6e214c7SGavin Maltby if (*ptr != '-') 3065f6e214c7SGavin Maltby return (EINVAL); 3066f6e214c7SGavin Maltby break; 3067f6e214c7SGavin Maltby 3068f6e214c7SGavin Maltby default: 3069f6e214c7SGavin Maltby if (!isxdigit(*ptr)) 3070f6e214c7SGavin Maltby return (EINVAL); 3071f6e214c7SGavin Maltby break; 3072f6e214c7SGavin Maltby } 3073f6e214c7SGavin Maltby } 3074f6e214c7SGavin Maltby 3075f6e214c7SGavin Maltby if (dump_osimage_uuid[0] != '\0') 3076f6e214c7SGavin Maltby return (EALREADY); 3077f6e214c7SGavin Maltby 3078f6e214c7SGavin Maltby (void) strncpy(dump_osimage_uuid, uuidstr, 36 + 1); 3079f6e214c7SGavin Maltby 30800c2f09f2SBill Sommerfeld cmn_err(CE_CONT, "?This Solaris instance has UUID %s\n", 3081f6e214c7SGavin Maltby dump_osimage_uuid); 3082f6e214c7SGavin Maltby 3083f6e214c7SGavin Maltby return (0); 3084f6e214c7SGavin Maltby } 3085f6e214c7SGavin Maltby 3086f6e214c7SGavin Maltby const char * 3087f6e214c7SGavin Maltby dump_get_uuid(void) 3088f6e214c7SGavin Maltby { 3089f6e214c7SGavin Maltby return (dump_osimage_uuid[0] != '\0' ? dump_osimage_uuid : ""); 3090f6e214c7SGavin Maltby } 3091