xref: /onnv-gate/usr/src/uts/common/os/dumpsubr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/param.h>
31*0Sstevel@tonic-gate #include <sys/systm.h>
32*0Sstevel@tonic-gate #include <sys/vm.h>
33*0Sstevel@tonic-gate #include <sys/proc.h>
34*0Sstevel@tonic-gate #include <sys/file.h>
35*0Sstevel@tonic-gate #include <sys/conf.h>
36*0Sstevel@tonic-gate #include <sys/kmem.h>
37*0Sstevel@tonic-gate #include <sys/mem.h>
38*0Sstevel@tonic-gate #include <sys/mman.h>
39*0Sstevel@tonic-gate #include <sys/vnode.h>
40*0Sstevel@tonic-gate #include <sys/errno.h>
41*0Sstevel@tonic-gate #include <sys/memlist.h>
42*0Sstevel@tonic-gate #include <sys/dumphdr.h>
43*0Sstevel@tonic-gate #include <sys/dumpadm.h>
44*0Sstevel@tonic-gate #include <sys/ksyms.h>
45*0Sstevel@tonic-gate #include <sys/compress.h>
46*0Sstevel@tonic-gate #include <sys/stream.h>
47*0Sstevel@tonic-gate #include <sys/strsun.h>
48*0Sstevel@tonic-gate #include <sys/cmn_err.h>
49*0Sstevel@tonic-gate #include <sys/bitmap.h>
50*0Sstevel@tonic-gate #include <sys/modctl.h>
51*0Sstevel@tonic-gate #include <sys/utsname.h>
52*0Sstevel@tonic-gate #include <sys/systeminfo.h>
53*0Sstevel@tonic-gate #include <sys/vmem.h>
54*0Sstevel@tonic-gate #include <sys/log.h>
55*0Sstevel@tonic-gate #include <sys/var.h>
56*0Sstevel@tonic-gate #include <sys/debug.h>
57*0Sstevel@tonic-gate #include <sys/sunddi.h>
58*0Sstevel@tonic-gate #include <fs/fs_subr.h>
59*0Sstevel@tonic-gate #include <sys/fs/snode.h>
60*0Sstevel@tonic-gate #include <sys/ontrap.h>
61*0Sstevel@tonic-gate #include <sys/panic.h>
62*0Sstevel@tonic-gate #include <sys/dkio.h>
63*0Sstevel@tonic-gate #include <sys/vtoc.h>
64*0Sstevel@tonic-gate #include <sys/errorq.h>
65*0Sstevel@tonic-gate #include <sys/fm/util.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #include <vm/hat.h>
68*0Sstevel@tonic-gate #include <vm/as.h>
69*0Sstevel@tonic-gate #include <vm/page.h>
70*0Sstevel@tonic-gate #include <vm/seg.h>
71*0Sstevel@tonic-gate #include <vm/seg_kmem.h>
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate kmutex_t	dump_lock;	/* lock for dump configuration */
74*0Sstevel@tonic-gate dumphdr_t	*dumphdr;	/* dump header */
75*0Sstevel@tonic-gate int		dump_conflags = DUMP_KERNEL; /* dump configuration flags */
76*0Sstevel@tonic-gate vnode_t		*dumpvp;	/* dump device vnode pointer */
77*0Sstevel@tonic-gate u_offset_t	dumpvp_size;	/* size of dump device, in bytes */
78*0Sstevel@tonic-gate static u_offset_t dumpvp_limit;	/* maximum write offset */
79*0Sstevel@tonic-gate char		*dumppath;	/* pathname of dump device */
80*0Sstevel@tonic-gate int		dump_timeout = 120; /* timeout for dumping page during panic */
81*0Sstevel@tonic-gate int		dump_timeleft;	/* portion of dump_timeout remaining */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate #ifdef DEBUG
84*0Sstevel@tonic-gate int		dumpfaildebug = 1;	/* enter debugger if dump fails */
85*0Sstevel@tonic-gate #else
86*0Sstevel@tonic-gate int		dumpfaildebug = 0;
87*0Sstevel@tonic-gate #endif
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate static ulong_t	*dump_bitmap;	/* bitmap for marking pages to dump */
90*0Sstevel@tonic-gate static pgcnt_t	dump_bitmapsize; /* size of bitmap */
91*0Sstevel@tonic-gate static pid_t	*dump_pids;	/* list of process IDs at dump time */
92*0Sstevel@tonic-gate static int	dump_ioerr;	/* dump i/o error */
93*0Sstevel@tonic-gate static offset_t	dumpvp_off;	/* current dump device offset */
94*0Sstevel@tonic-gate static char	*dump_cmap;	/* VA for dump compression mapping */
95*0Sstevel@tonic-gate static char	*dumpbuf_cur, *dumpbuf_start, *dumpbuf_end;
96*0Sstevel@tonic-gate static char	*dump_cbuf;	/* compression buffer */
97*0Sstevel@tonic-gate static char	*dump_uebuf;	/* memory error detection buffer */
98*0Sstevel@tonic-gate static size_t	dumpbuf_size;	/* size of dumpbuf in bytes */
99*0Sstevel@tonic-gate static size_t	dumpbuf_limit = 1UL << 23;	/* 8MB */
100*0Sstevel@tonic-gate static size_t	dump_iosize;	/* device's best transfer size, if any */
101*0Sstevel@tonic-gate static uint64_t	dumpbuf_thresh = 1ULL << 30;	/* 1GB */
102*0Sstevel@tonic-gate static ulong_t	dumpbuf_mult = 8;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate  * The dump i/o buffer must be at least one page, at most xfer_size bytes, and
106*0Sstevel@tonic-gate  * should scale with physmem in between.  The transfer size passed in will
107*0Sstevel@tonic-gate  * either represent a global default (maxphys) or the best size for the device.
108*0Sstevel@tonic-gate  * Once the physical memory size exceeds dumpbuf_thresh (1GB by default), we
109*0Sstevel@tonic-gate  * increase the percentage of physical memory that dumpbuf can consume by a
110*0Sstevel@tonic-gate  * factor of dumpbuf_mult (8 by default) to improve large memory performance.
111*0Sstevel@tonic-gate  * The size of the dumpbuf i/o buffer is limited by dumpbuf_limit (8MB by
112*0Sstevel@tonic-gate  * default) because the dump performance saturates beyond a certain size.
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate static size_t
115*0Sstevel@tonic-gate dumpbuf_iosize(size_t xfer_size)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	pgcnt_t scale = physmem;
118*0Sstevel@tonic-gate 	size_t iosize;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if (scale >= dumpbuf_thresh / PAGESIZE) {
121*0Sstevel@tonic-gate 		scale *= dumpbuf_mult; /* increase scaling factor */
122*0Sstevel@tonic-gate 		iosize = MIN(xfer_size, scale) & PAGEMASK;
123*0Sstevel@tonic-gate 		if (dumpbuf_limit && iosize > dumpbuf_limit)
124*0Sstevel@tonic-gate 			iosize = MAX(PAGESIZE, dumpbuf_limit & PAGEMASK);
125*0Sstevel@tonic-gate 	} else
126*0Sstevel@tonic-gate 		iosize = MAX(PAGESIZE, MIN(xfer_size, scale) & PAGEMASK);
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	return (iosize);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate static void
132*0Sstevel@tonic-gate dumpbuf_resize(void)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	char *old_buf = dumpbuf_start;
135*0Sstevel@tonic-gate 	size_t old_size = dumpbuf_size;
136*0Sstevel@tonic-gate 	char *new_buf;
137*0Sstevel@tonic-gate 	size_t new_size;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if ((new_size = dumpbuf_iosize(MAX(dump_iosize, maxphys))) <= old_size)
142*0Sstevel@tonic-gate 		return; /* no need to reallocate buffer */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	new_buf = kmem_alloc(new_size, KM_SLEEP);
145*0Sstevel@tonic-gate 	dumpbuf_size = new_size;
146*0Sstevel@tonic-gate 	dumpbuf_start = new_buf;
147*0Sstevel@tonic-gate 	dumpbuf_end = new_buf + new_size;
148*0Sstevel@tonic-gate 	kmem_free(old_buf, old_size);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static void
152*0Sstevel@tonic-gate dumphdr_init(void)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	pgcnt_t npages = 0;
155*0Sstevel@tonic-gate 	struct memlist *mp;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (dumphdr == NULL) {
160*0Sstevel@tonic-gate 		dumphdr = kmem_zalloc(sizeof (dumphdr_t), KM_SLEEP);
161*0Sstevel@tonic-gate 		dumphdr->dump_magic = DUMP_MAGIC;
162*0Sstevel@tonic-gate 		dumphdr->dump_version = DUMP_VERSION;
163*0Sstevel@tonic-gate 		dumphdr->dump_wordsize = DUMP_WORDSIZE;
164*0Sstevel@tonic-gate 		dumphdr->dump_pageshift = PAGESHIFT;
165*0Sstevel@tonic-gate 		dumphdr->dump_pagesize = PAGESIZE;
166*0Sstevel@tonic-gate 		dumphdr->dump_utsname = utsname;
167*0Sstevel@tonic-gate 		(void) strcpy(dumphdr->dump_platform, platform);
168*0Sstevel@tonic-gate 		dump_cmap = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
169*0Sstevel@tonic-gate 		dumpbuf_size = dumpbuf_iosize(maxphys);
170*0Sstevel@tonic-gate 		dumpbuf_start = kmem_alloc(dumpbuf_size, KM_SLEEP);
171*0Sstevel@tonic-gate 		dumpbuf_end = dumpbuf_start + dumpbuf_size;
172*0Sstevel@tonic-gate 		dump_cbuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* compress buf */
173*0Sstevel@tonic-gate 		dump_uebuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* UE buf */
174*0Sstevel@tonic-gate 		dump_pids = kmem_alloc(v.v_proc * sizeof (pid_t), KM_SLEEP);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next)
178*0Sstevel@tonic-gate 		npages += mp->size >> PAGESHIFT;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	if (dump_bitmapsize != npages) {
181*0Sstevel@tonic-gate 		void *map = kmem_alloc(BT_SIZEOFMAP(npages), KM_SLEEP);
182*0Sstevel@tonic-gate 		kmem_free(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
183*0Sstevel@tonic-gate 		dump_bitmap = map;
184*0Sstevel@tonic-gate 		dump_bitmapsize = npages;
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate  * Establish a new dump device.
190*0Sstevel@tonic-gate  */
191*0Sstevel@tonic-gate int
192*0Sstevel@tonic-gate dumpinit(vnode_t *vp, char *name, int justchecking)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate 	vnode_t *cvp;
195*0Sstevel@tonic-gate 	vattr_t vattr;
196*0Sstevel@tonic-gate 	vnode_t *cdev_vp;
197*0Sstevel@tonic-gate 	int error = 0;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	dumphdr_init();
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	cvp = common_specvp(vp);
204*0Sstevel@tonic-gate 	if (cvp == dumpvp)
205*0Sstevel@tonic-gate 		return (0);
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	/*
208*0Sstevel@tonic-gate 	 * Determine whether this is a plausible dump device.  We want either:
209*0Sstevel@tonic-gate 	 * (1) a real device that's not mounted and has a cb_dump routine, or
210*0Sstevel@tonic-gate 	 * (2) a swapfile on some filesystem that has a vop_dump routine.
211*0Sstevel@tonic-gate 	 */
212*0Sstevel@tonic-gate 	if ((error = VOP_OPEN(&cvp, FREAD | FWRITE, kcred)) != 0)
213*0Sstevel@tonic-gate 		return (error);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE | AT_TYPE | AT_RDEV;
216*0Sstevel@tonic-gate 	if ((error = VOP_GETATTR(cvp, &vattr, 0, kcred)) == 0) {
217*0Sstevel@tonic-gate 		if (vattr.va_type == VBLK || vattr.va_type == VCHR) {
218*0Sstevel@tonic-gate 			if (devopsp[getmajor(vattr.va_rdev)]->
219*0Sstevel@tonic-gate 			    devo_cb_ops->cb_dump == nodev)
220*0Sstevel@tonic-gate 				error = ENOTSUP;
221*0Sstevel@tonic-gate 			else if (vfs_devismounted(vattr.va_rdev))
222*0Sstevel@tonic-gate 				error = EBUSY;
223*0Sstevel@tonic-gate 		} else {
224*0Sstevel@tonic-gate 			if (vn_matchopval(cvp, VOPNAME_DUMP, fs_nosys) ||
225*0Sstevel@tonic-gate 			    !IS_SWAPVP(cvp))
226*0Sstevel@tonic-gate 				error = ENOTSUP;
227*0Sstevel@tonic-gate 		}
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE)
231*0Sstevel@tonic-gate 		error = ENOSPC;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	if (error || justchecking) {
234*0Sstevel@tonic-gate 		(void) VOP_CLOSE(cvp, FREAD | FWRITE, 1, (offset_t)0, kcred);
235*0Sstevel@tonic-gate 		return (error);
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	VN_HOLD(cvp);
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	if (dumpvp != NULL)
241*0Sstevel@tonic-gate 		dumpfini();	/* unconfigure the old dump device */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	dumpvp = cvp;
244*0Sstevel@tonic-gate 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
245*0Sstevel@tonic-gate 	dumppath = kmem_alloc(strlen(name) + 1, KM_SLEEP);
246*0Sstevel@tonic-gate 	(void) strcpy(dumppath, name);
247*0Sstevel@tonic-gate 	dump_iosize = 0;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	/*
250*0Sstevel@tonic-gate 	 * If the dump device is a block device, attempt to open up the
251*0Sstevel@tonic-gate 	 * corresponding character device and determine its maximum transfer
252*0Sstevel@tonic-gate 	 * size.  We use this information to potentially resize dumpbuf to a
253*0Sstevel@tonic-gate 	 * larger and more optimal size for performing i/o to the dump device.
254*0Sstevel@tonic-gate 	 */
255*0Sstevel@tonic-gate 	if (cvp->v_type == VBLK &&
256*0Sstevel@tonic-gate 	    (cdev_vp = makespecvp(VTOS(cvp)->s_dev, VCHR)) != NULL) {
257*0Sstevel@tonic-gate 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred) == 0) {
258*0Sstevel@tonic-gate 			size_t blk_size;
259*0Sstevel@tonic-gate 			struct dk_cinfo dki;
260*0Sstevel@tonic-gate 			struct vtoc vtoc;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCGVTOC, (intptr_t)&vtoc,
263*0Sstevel@tonic-gate 			    FKIOCTL, kcred, NULL) == 0 && vtoc.v_sectorsz != 0)
264*0Sstevel@tonic-gate 				blk_size = vtoc.v_sectorsz;
265*0Sstevel@tonic-gate 			else
266*0Sstevel@tonic-gate 				blk_size = DEV_BSIZE;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCINFO, (intptr_t)&dki,
269*0Sstevel@tonic-gate 			    FKIOCTL, kcred, NULL) == 0) {
270*0Sstevel@tonic-gate 				dump_iosize = dki.dki_maxtransfer * blk_size;
271*0Sstevel@tonic-gate 				dumpbuf_resize();
272*0Sstevel@tonic-gate 			}
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0, kcred);
275*0Sstevel@tonic-gate 		}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		VN_RELE(cdev_vp);
278*0Sstevel@tonic-gate 	}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	cmn_err(CE_CONT, "?dump on %s size %llu MB\n", name, dumpvp_size >> 20);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	return (0);
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate void
286*0Sstevel@tonic-gate dumpfini(void)
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	kmem_free(dumppath, strlen(dumppath) + 1);
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	(void) VOP_CLOSE(dumpvp, FREAD | FWRITE, 1, (offset_t)0, kcred);
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	VN_RELE(dumpvp);
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	dumpvp = NULL;
297*0Sstevel@tonic-gate 	dumpvp_size = 0;
298*0Sstevel@tonic-gate 	dumppath = NULL;
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate static pfn_t
302*0Sstevel@tonic-gate dump_bitnum_to_pfn(pgcnt_t bitnum)
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate 	struct memlist *mp;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
307*0Sstevel@tonic-gate 		if (bitnum < (mp->size >> PAGESHIFT))
308*0Sstevel@tonic-gate 			return ((mp->address >> PAGESHIFT) + bitnum);
309*0Sstevel@tonic-gate 		bitnum -= mp->size >> PAGESHIFT;
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 	return (PFN_INVALID);
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate static pgcnt_t
315*0Sstevel@tonic-gate dump_pfn_to_bitnum(pfn_t pfn)
316*0Sstevel@tonic-gate {
317*0Sstevel@tonic-gate 	struct memlist *mp;
318*0Sstevel@tonic-gate 	pgcnt_t bitnum = 0;
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
321*0Sstevel@tonic-gate 		if (pfn >= (mp->address >> PAGESHIFT) &&
322*0Sstevel@tonic-gate 		    pfn < ((mp->address + mp->size) >> PAGESHIFT))
323*0Sstevel@tonic-gate 			return (bitnum + pfn - (mp->address >> PAGESHIFT));
324*0Sstevel@tonic-gate 		bitnum += mp->size >> PAGESHIFT;
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 	return ((pgcnt_t)-1);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate static offset_t
330*0Sstevel@tonic-gate dumpvp_flush(void)
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	size_t size = P2ROUNDUP(dumpbuf_cur - dumpbuf_start, PAGESIZE);
333*0Sstevel@tonic-gate 	int err;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	if (dumpvp_off + size > dumpvp_limit) {
336*0Sstevel@tonic-gate 		dump_ioerr = ENOSPC;
337*0Sstevel@tonic-gate 	} else if (size != 0) {
338*0Sstevel@tonic-gate 		if (panicstr)
339*0Sstevel@tonic-gate 			err = VOP_DUMP(dumpvp, dumpbuf_start,
340*0Sstevel@tonic-gate 			    lbtodb(dumpvp_off), btod(size));
341*0Sstevel@tonic-gate 		else
342*0Sstevel@tonic-gate 			err = vn_rdwr(UIO_WRITE, dumpvp, dumpbuf_start, size,
343*0Sstevel@tonic-gate 			    dumpvp_off, UIO_SYSSPACE, 0, dumpvp_limit,
344*0Sstevel@tonic-gate 			    kcred, 0);
345*0Sstevel@tonic-gate 		if (err && dump_ioerr == 0)
346*0Sstevel@tonic-gate 			dump_ioerr = err;
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 	dumpvp_off += size;
349*0Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
350*0Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
351*0Sstevel@tonic-gate 	return (dumpvp_off);
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate void
355*0Sstevel@tonic-gate dumpvp_write(const void *va, size_t size)
356*0Sstevel@tonic-gate {
357*0Sstevel@tonic-gate 	while (size != 0) {
358*0Sstevel@tonic-gate 		size_t len = MIN(size, dumpbuf_end - dumpbuf_cur);
359*0Sstevel@tonic-gate 		if (len == 0) {
360*0Sstevel@tonic-gate 			(void) dumpvp_flush();
361*0Sstevel@tonic-gate 		} else {
362*0Sstevel@tonic-gate 			bcopy(va, dumpbuf_cur, len);
363*0Sstevel@tonic-gate 			va = (char *)va + len;
364*0Sstevel@tonic-gate 			dumpbuf_cur += len;
365*0Sstevel@tonic-gate 			size -= len;
366*0Sstevel@tonic-gate 		}
367*0Sstevel@tonic-gate 	}
368*0Sstevel@tonic-gate }
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate /*ARGSUSED*/
371*0Sstevel@tonic-gate static void
372*0Sstevel@tonic-gate dumpvp_ksyms_write(const void *src, void *dst, size_t size)
373*0Sstevel@tonic-gate {
374*0Sstevel@tonic-gate 	dumpvp_write(src, size);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate  * Mark 'pfn' in the bitmap and dump its translation table entry.
379*0Sstevel@tonic-gate  */
380*0Sstevel@tonic-gate void
381*0Sstevel@tonic-gate dump_addpage(struct as *as, void *va, pfn_t pfn)
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
384*0Sstevel@tonic-gate 	pgcnt_t bitnum;
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
387*0Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
388*0Sstevel@tonic-gate 			dumphdr->dump_npages++;
389*0Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
390*0Sstevel@tonic-gate 		}
391*0Sstevel@tonic-gate 		dumphdr->dump_nvtop++;
392*0Sstevel@tonic-gate 		mem_vtop.m_as = as;
393*0Sstevel@tonic-gate 		mem_vtop.m_va = va;
394*0Sstevel@tonic-gate 		mem_vtop.m_pfn = pfn;
395*0Sstevel@tonic-gate 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
398*0Sstevel@tonic-gate }
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate /*
401*0Sstevel@tonic-gate  * Mark 'pfn' in the bitmap
402*0Sstevel@tonic-gate  */
403*0Sstevel@tonic-gate void
404*0Sstevel@tonic-gate dump_page(pfn_t pfn)
405*0Sstevel@tonic-gate {
406*0Sstevel@tonic-gate 	pgcnt_t bitnum;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
409*0Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
410*0Sstevel@tonic-gate 			dumphdr->dump_npages++;
411*0Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
412*0Sstevel@tonic-gate 		}
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
415*0Sstevel@tonic-gate }
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate  * Dump the <as, va, pfn> information for a given address space.
419*0Sstevel@tonic-gate  * SEGOP_DUMP() will call dump_addpage() for each page in the segment.
420*0Sstevel@tonic-gate  */
421*0Sstevel@tonic-gate static void
422*0Sstevel@tonic-gate dump_as(struct as *as)
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate 	struct seg *seg;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
427*0Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
428*0Sstevel@tonic-gate 		if (seg->s_as != as)
429*0Sstevel@tonic-gate 			break;
430*0Sstevel@tonic-gate 		if (seg->s_ops == NULL)
431*0Sstevel@tonic-gate 			continue;
432*0Sstevel@tonic-gate 		SEGOP_DUMP(seg);
433*0Sstevel@tonic-gate 	}
434*0Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (seg != NULL)
437*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid segment %p in address space %p",
438*0Sstevel@tonic-gate 		    (void *)seg, (void *)as);
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate static int
442*0Sstevel@tonic-gate dump_process(pid_t pid)
443*0Sstevel@tonic-gate {
444*0Sstevel@tonic-gate 	proc_t *p = sprlock(pid);
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 	if (p == NULL)
447*0Sstevel@tonic-gate 		return (-1);
448*0Sstevel@tonic-gate 	if (p->p_as != &kas) {
449*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
450*0Sstevel@tonic-gate 		dump_as(p->p_as);
451*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
452*0Sstevel@tonic-gate 	}
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	sprunlock(p);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	return (0);
457*0Sstevel@tonic-gate }
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate void
460*0Sstevel@tonic-gate dump_ereports(void)
461*0Sstevel@tonic-gate {
462*0Sstevel@tonic-gate 	u_offset_t dumpvp_start;
463*0Sstevel@tonic-gate 	erpt_dump_t ed;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL)
466*0Sstevel@tonic-gate 		return;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
469*0Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE);
470*0Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_ERPTSIZE;
471*0Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	fm_ereport_dump();
474*0Sstevel@tonic-gate 	if (panicstr)
475*0Sstevel@tonic-gate 		errorq_dump();
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	bzero(&ed, sizeof (ed)); /* indicate end of ereports */
478*0Sstevel@tonic-gate 	dumpvp_write(&ed, sizeof (ed));
479*0Sstevel@tonic-gate 	(void) dumpvp_flush();
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	if (!panicstr) {
482*0Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
483*0Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
484*0Sstevel@tonic-gate 		    B_INVAL | B_FORCE, kcred);
485*0Sstevel@tonic-gate 	}
486*0Sstevel@tonic-gate }
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate void
489*0Sstevel@tonic-gate dump_messages(void)
490*0Sstevel@tonic-gate {
491*0Sstevel@tonic-gate 	log_dump_t ld;
492*0Sstevel@tonic-gate 	mblk_t *mctl, *mdata;
493*0Sstevel@tonic-gate 	queue_t *q, *qlast;
494*0Sstevel@tonic-gate 	u_offset_t dumpvp_start;
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL || log_consq == NULL)
497*0Sstevel@tonic-gate 		return;
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
500*0Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_OFFSET;
501*0Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_LOGSIZE;
502*0Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	qlast = NULL;
505*0Sstevel@tonic-gate 	do {
506*0Sstevel@tonic-gate 		for (q = log_consq; q->q_next != qlast; q = q->q_next)
507*0Sstevel@tonic-gate 			continue;
508*0Sstevel@tonic-gate 		for (mctl = q->q_first; mctl != NULL; mctl = mctl->b_next) {
509*0Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
510*0Sstevel@tonic-gate 			mdata = mctl->b_cont;
511*0Sstevel@tonic-gate 			ld.ld_magic = LOG_MAGIC;
512*0Sstevel@tonic-gate 			ld.ld_msgsize = MBLKL(mctl->b_cont);
513*0Sstevel@tonic-gate 			ld.ld_csum = checksum32(mctl->b_rptr, MBLKL(mctl));
514*0Sstevel@tonic-gate 			ld.ld_msum = checksum32(mdata->b_rptr, MBLKL(mdata));
515*0Sstevel@tonic-gate 			dumpvp_write(&ld, sizeof (ld));
516*0Sstevel@tonic-gate 			dumpvp_write(mctl->b_rptr, MBLKL(mctl));
517*0Sstevel@tonic-gate 			dumpvp_write(mdata->b_rptr, MBLKL(mdata));
518*0Sstevel@tonic-gate 		}
519*0Sstevel@tonic-gate 	} while ((qlast = q) != log_consq);
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	ld.ld_magic = 0;		/* indicate end of messages */
522*0Sstevel@tonic-gate 	dumpvp_write(&ld, sizeof (ld));
523*0Sstevel@tonic-gate 	(void) dumpvp_flush();
524*0Sstevel@tonic-gate 	if (!panicstr) {
525*0Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
526*0Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
527*0Sstevel@tonic-gate 		    B_INVAL | B_FORCE, kcred);
528*0Sstevel@tonic-gate 	}
529*0Sstevel@tonic-gate }
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate static void
532*0Sstevel@tonic-gate dump_pagecopy(void *src, void *dst)
533*0Sstevel@tonic-gate {
534*0Sstevel@tonic-gate 	long *wsrc = (long *)src;
535*0Sstevel@tonic-gate 	long *wdst = (long *)dst;
536*0Sstevel@tonic-gate 	const ulong_t ncopies = PAGESIZE / sizeof (long);
537*0Sstevel@tonic-gate 	volatile int w = 0;
538*0Sstevel@tonic-gate 	volatile int ueoff = -1;
539*0Sstevel@tonic-gate 	on_trap_data_t otd;
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_EC)) {
542*0Sstevel@tonic-gate 		if (ueoff == -1) {
543*0Sstevel@tonic-gate 			uint64_t pa;
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 			ueoff = w * sizeof (long);
546*0Sstevel@tonic-gate 			pa = ptob((uint64_t)hat_getpfnum(kas.a_hat, src))
547*0Sstevel@tonic-gate 			    + ueoff;
548*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "memory error at PA 0x%08x.%08x",
549*0Sstevel@tonic-gate 			    (uint32_t)(pa >> 32), (uint32_t)pa);
550*0Sstevel@tonic-gate 		}
551*0Sstevel@tonic-gate #ifdef _LP64
552*0Sstevel@tonic-gate 		wdst[w++] = 0xbadecc00badecc;
553*0Sstevel@tonic-gate #else
554*0Sstevel@tonic-gate 		wdst[w++] = 0xbadecc;
555*0Sstevel@tonic-gate #endif
556*0Sstevel@tonic-gate 	}
557*0Sstevel@tonic-gate 	while (w < ncopies) {
558*0Sstevel@tonic-gate 		wdst[w] = wsrc[w];
559*0Sstevel@tonic-gate 		w++;
560*0Sstevel@tonic-gate 	}
561*0Sstevel@tonic-gate 	no_trap();
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate /*
565*0Sstevel@tonic-gate  * Dump the system.
566*0Sstevel@tonic-gate  */
567*0Sstevel@tonic-gate void
568*0Sstevel@tonic-gate dumpsys(void)
569*0Sstevel@tonic-gate {
570*0Sstevel@tonic-gate 	pfn_t pfn;
571*0Sstevel@tonic-gate 	pgcnt_t bitnum;
572*0Sstevel@tonic-gate 	int npages = 0;
573*0Sstevel@tonic-gate 	int percent_done = 0;
574*0Sstevel@tonic-gate 	uint32_t csize;
575*0Sstevel@tonic-gate 	u_offset_t total_csize = 0;
576*0Sstevel@tonic-gate 	int compress_ratio;
577*0Sstevel@tonic-gate 	proc_t *p;
578*0Sstevel@tonic-gate 	pid_t npids, pidx;
579*0Sstevel@tonic-gate 	char *content;
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL) {
582*0Sstevel@tonic-gate 		uprintf("skipping system dump - no dump device configured\n");
583*0Sstevel@tonic-gate 		return;
584*0Sstevel@tonic-gate 	}
585*0Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	/*
588*0Sstevel@tonic-gate 	 * Calculate the starting block for dump.  If we're dumping on a
589*0Sstevel@tonic-gate 	 * swap device, start 1/5 of the way in; otherwise, start at the
590*0Sstevel@tonic-gate 	 * beginning.  And never use the first page -- it may be a disk label.
591*0Sstevel@tonic-gate 	 */
592*0Sstevel@tonic-gate 	if (dumpvp->v_flag & VISSWAP)
593*0Sstevel@tonic-gate 		dumphdr->dump_start = P2ROUNDUP(dumpvp_size / 5, DUMP_OFFSET);
594*0Sstevel@tonic-gate 	else
595*0Sstevel@tonic-gate 		dumphdr->dump_start = DUMP_OFFSET;
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	dumphdr->dump_flags = DF_VALID | DF_COMPLETE | DF_LIVE;
598*0Sstevel@tonic-gate 	dumphdr->dump_crashtime = gethrestime_sec();
599*0Sstevel@tonic-gate 	dumphdr->dump_npages = 0;
600*0Sstevel@tonic-gate 	dumphdr->dump_nvtop = 0;
601*0Sstevel@tonic-gate 	bzero(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
602*0Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	if (panicstr) {
605*0Sstevel@tonic-gate 		dumphdr->dump_flags &= ~DF_LIVE;
606*0Sstevel@tonic-gate 		(void) VOP_DUMPCTL(dumpvp, DUMP_FREE, NULL);
607*0Sstevel@tonic-gate 		(void) VOP_DUMPCTL(dumpvp, DUMP_ALLOC, NULL);
608*0Sstevel@tonic-gate 		(void) vsnprintf(dumphdr->dump_panicstring, DUMP_PANICSIZE,
609*0Sstevel@tonic-gate 		    panicstr, panicargs);
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL)
613*0Sstevel@tonic-gate 		content = "all";
614*0Sstevel@tonic-gate 	else if (dump_conflags & DUMP_CURPROC)
615*0Sstevel@tonic-gate 		content = "kernel + curproc";
616*0Sstevel@tonic-gate 	else
617*0Sstevel@tonic-gate 		content = "kernel";
618*0Sstevel@tonic-gate 	uprintf("dumping to %s, offset %lld, content: %s\n", dumppath,
619*0Sstevel@tonic-gate 	    dumphdr->dump_start, content);
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	/*
622*0Sstevel@tonic-gate 	 * Leave room for the message and ereport save areas and terminal dump
623*0Sstevel@tonic-gate 	 * header.
624*0Sstevel@tonic-gate 	 */
625*0Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_LOGSIZE - DUMP_OFFSET - DUMP_ERPTSIZE;
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 	/*
628*0Sstevel@tonic-gate 	 * Write out the symbol table.  It's no longer compressed,
629*0Sstevel@tonic-gate 	 * so its 'size' and 'csize' are equal.
630*0Sstevel@tonic-gate 	 */
631*0Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_ksyms = dumphdr->dump_start + PAGESIZE;
632*0Sstevel@tonic-gate 	dumphdr->dump_ksyms_size = dumphdr->dump_ksyms_csize =
633*0Sstevel@tonic-gate 	    ksyms_snapshot(dumpvp_ksyms_write, NULL, LONG_MAX);
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	/*
636*0Sstevel@tonic-gate 	 * Write out the translation map.
637*0Sstevel@tonic-gate 	 */
638*0Sstevel@tonic-gate 	dumphdr->dump_map = dumpvp_flush();
639*0Sstevel@tonic-gate 	dump_as(&kas);
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	/*
642*0Sstevel@tonic-gate 	 * call into hat, which may have unmapped pages that also need to
643*0Sstevel@tonic-gate 	 * be in the dump
644*0Sstevel@tonic-gate 	 */
645*0Sstevel@tonic-gate 	hat_dump();
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL) {
648*0Sstevel@tonic-gate 		mutex_enter(&pidlock);
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 		for (npids = 0, p = practive; p != NULL; p = p->p_next)
651*0Sstevel@tonic-gate 			dump_pids[npids++] = p->p_pid;
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 		mutex_exit(&pidlock);
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 		for (pidx = 0; pidx < npids; pidx++)
656*0Sstevel@tonic-gate 			(void) dump_process(dump_pids[pidx]);
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 		for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
659*0Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
660*0Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
661*0Sstevel@tonic-gate 		}
662*0Sstevel@tonic-gate 		dumphdr->dump_npages = dump_bitmapsize;
663*0Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_ALL;
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	} else if (dump_conflags & DUMP_CURPROC) {
666*0Sstevel@tonic-gate 		/*
667*0Sstevel@tonic-gate 		 * Determine which pid is to be dumped.  If we're panicking, we
668*0Sstevel@tonic-gate 		 * dump the process associated with panic_thread (if any).  If
669*0Sstevel@tonic-gate 		 * this is a live dump, we dump the process associated with
670*0Sstevel@tonic-gate 		 * curthread.
671*0Sstevel@tonic-gate 		 */
672*0Sstevel@tonic-gate 		npids = 0;
673*0Sstevel@tonic-gate 		if (panicstr) {
674*0Sstevel@tonic-gate 			if (panic_thread != NULL &&
675*0Sstevel@tonic-gate 			    panic_thread->t_procp != NULL &&
676*0Sstevel@tonic-gate 			    panic_thread->t_procp != &p0) {
677*0Sstevel@tonic-gate 				dump_pids[npids++] =
678*0Sstevel@tonic-gate 				    panic_thread->t_procp->p_pid;
679*0Sstevel@tonic-gate 			}
680*0Sstevel@tonic-gate 		} else {
681*0Sstevel@tonic-gate 			dump_pids[npids++] = curthread->t_procp->p_pid;
682*0Sstevel@tonic-gate 		}
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 		if (npids && dump_process(dump_pids[0]) == 0)
685*0Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_CURPROC;
686*0Sstevel@tonic-gate 		else
687*0Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_KERNEL;
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	} else {
690*0Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_KERNEL;
691*0Sstevel@tonic-gate 	}
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 	dumphdr->dump_hashmask = (1 << highbit(dumphdr->dump_nvtop - 1)) - 1;
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	/*
696*0Sstevel@tonic-gate 	 * Write out the pfn table.
697*0Sstevel@tonic-gate 	 */
698*0Sstevel@tonic-gate 	dumphdr->dump_pfn = dumpvp_flush();
699*0Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
700*0Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
701*0Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
702*0Sstevel@tonic-gate 			continue;
703*0Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
704*0Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
705*0Sstevel@tonic-gate 		dumpvp_write(&pfn, sizeof (pfn_t));
706*0Sstevel@tonic-gate 	}
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	/*
709*0Sstevel@tonic-gate 	 * Write out all the pages.
710*0Sstevel@tonic-gate 	 */
711*0Sstevel@tonic-gate 	dumphdr->dump_data = dumpvp_flush();
712*0Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
713*0Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
714*0Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
715*0Sstevel@tonic-gate 			continue;
716*0Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
717*0Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 		/*
720*0Sstevel@tonic-gate 		 * Map in page frame 'pfn', scan it for UE's while copying
721*0Sstevel@tonic-gate 		 * the data to dump_uebuf, unmap it, compress dump_uebuf into
722*0Sstevel@tonic-gate 		 * dump_cbuf, and write out dump_cbuf.  The UE check ensures
723*0Sstevel@tonic-gate 		 * that we don't lose the whole dump because of a latent UE.
724*0Sstevel@tonic-gate 		 */
725*0Sstevel@tonic-gate 		hat_devload(kas.a_hat, dump_cmap, PAGESIZE, pfn, PROT_READ,
726*0Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST);
727*0Sstevel@tonic-gate 		dump_pagecopy(dump_cmap, dump_uebuf);
728*0Sstevel@tonic-gate 		hat_unload(kas.a_hat, dump_cmap, PAGESIZE, HAT_UNLOAD);
729*0Sstevel@tonic-gate 		csize = (uint32_t)compress(dump_uebuf, dump_cbuf, PAGESIZE);
730*0Sstevel@tonic-gate 		dumpvp_write(&csize, sizeof (uint32_t));
731*0Sstevel@tonic-gate 		dumpvp_write(dump_cbuf, csize);
732*0Sstevel@tonic-gate 		if (dump_ioerr) {
733*0Sstevel@tonic-gate 			dumphdr->dump_flags &= ~DF_COMPLETE;
734*0Sstevel@tonic-gate 			dumphdr->dump_npages = npages;
735*0Sstevel@tonic-gate 			break;
736*0Sstevel@tonic-gate 		}
737*0Sstevel@tonic-gate 		total_csize += csize;
738*0Sstevel@tonic-gate 		if (++npages * 100LL / dumphdr->dump_npages > percent_done) {
739*0Sstevel@tonic-gate 			uprintf("^\r%3d%% done", ++percent_done);
740*0Sstevel@tonic-gate 			if (!panicstr)
741*0Sstevel@tonic-gate 				delay(1);	/* let the output be sent */
742*0Sstevel@tonic-gate 		}
743*0Sstevel@tonic-gate 	}
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	(void) dumpvp_flush();
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	/*
748*0Sstevel@tonic-gate 	 * Write out the initial and terminal dump headers.
749*0Sstevel@tonic-gate 	 */
750*0Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_start;
751*0Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
752*0Sstevel@tonic-gate 	(void) dumpvp_flush();
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size;
755*0Sstevel@tonic-gate 	dumpvp_off = dumpvp_limit - DUMP_OFFSET;
756*0Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
757*0Sstevel@tonic-gate 	(void) dumpvp_flush();
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	compress_ratio = (int)(100LL * npages / (btopr(total_csize + 1)));
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	uprintf("\r%3d%% done: %d pages dumped, compression ratio %d.%02d, ",
762*0Sstevel@tonic-gate 	    percent_done, npages, compress_ratio / 100, compress_ratio % 100);
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	if (dump_ioerr == 0) {
765*0Sstevel@tonic-gate 		uprintf("dump succeeded\n");
766*0Sstevel@tonic-gate 	} else {
767*0Sstevel@tonic-gate 		uprintf("dump failed: error %d\n", dump_ioerr);
768*0Sstevel@tonic-gate 		if (panicstr && dumpfaildebug)
769*0Sstevel@tonic-gate 			debug_enter("dump failed");
770*0Sstevel@tonic-gate 	}
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	/*
773*0Sstevel@tonic-gate 	 * Write out all undelivered messages.  This has to be the *last*
774*0Sstevel@tonic-gate 	 * thing we do because the dump process itself emits messages.
775*0Sstevel@tonic-gate 	 */
776*0Sstevel@tonic-gate 	if (panicstr) {
777*0Sstevel@tonic-gate 		dump_ereports();
778*0Sstevel@tonic-gate 		dump_messages();
779*0Sstevel@tonic-gate 	}
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	delay(2 * hz);	/* let people see the 'done' message */
782*0Sstevel@tonic-gate 	dump_timeleft = 0;
783*0Sstevel@tonic-gate 	dump_ioerr = 0;
784*0Sstevel@tonic-gate }
785*0Sstevel@tonic-gate 
786*0Sstevel@tonic-gate /*
787*0Sstevel@tonic-gate  * This function is called whenever the memory size, as represented
788*0Sstevel@tonic-gate  * by the phys_install list, changes.
789*0Sstevel@tonic-gate  */
790*0Sstevel@tonic-gate void
791*0Sstevel@tonic-gate dump_resize()
792*0Sstevel@tonic-gate {
793*0Sstevel@tonic-gate 	mutex_enter(&dump_lock);
794*0Sstevel@tonic-gate 	dumphdr_init();
795*0Sstevel@tonic-gate 	dumpbuf_resize();
796*0Sstevel@tonic-gate 	mutex_exit(&dump_lock);
797*0Sstevel@tonic-gate }
798