xref: /onnv-gate/usr/src/uts/common/os/dumpsubr.c (revision 5331:3047ad28a67b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/vm.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/file.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/mem.h>
370Sstevel@tonic-gate #include <sys/mman.h>
380Sstevel@tonic-gate #include <sys/vnode.h>
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/memlist.h>
410Sstevel@tonic-gate #include <sys/dumphdr.h>
420Sstevel@tonic-gate #include <sys/dumpadm.h>
430Sstevel@tonic-gate #include <sys/ksyms.h>
440Sstevel@tonic-gate #include <sys/compress.h>
450Sstevel@tonic-gate #include <sys/stream.h>
460Sstevel@tonic-gate #include <sys/strsun.h>
470Sstevel@tonic-gate #include <sys/cmn_err.h>
480Sstevel@tonic-gate #include <sys/bitmap.h>
490Sstevel@tonic-gate #include <sys/modctl.h>
500Sstevel@tonic-gate #include <sys/utsname.h>
510Sstevel@tonic-gate #include <sys/systeminfo.h>
520Sstevel@tonic-gate #include <sys/vmem.h>
530Sstevel@tonic-gate #include <sys/log.h>
540Sstevel@tonic-gate #include <sys/var.h>
550Sstevel@tonic-gate #include <sys/debug.h>
560Sstevel@tonic-gate #include <sys/sunddi.h>
570Sstevel@tonic-gate #include <fs/fs_subr.h>
580Sstevel@tonic-gate #include <sys/fs/snode.h>
590Sstevel@tonic-gate #include <sys/ontrap.h>
600Sstevel@tonic-gate #include <sys/panic.h>
610Sstevel@tonic-gate #include <sys/dkio.h>
620Sstevel@tonic-gate #include <sys/vtoc.h>
630Sstevel@tonic-gate #include <sys/errorq.h>
640Sstevel@tonic-gate #include <sys/fm/util.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <vm/hat.h>
670Sstevel@tonic-gate #include <vm/as.h>
680Sstevel@tonic-gate #include <vm/page.h>
690Sstevel@tonic-gate #include <vm/seg.h>
700Sstevel@tonic-gate #include <vm/seg_kmem.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate kmutex_t	dump_lock;	/* lock for dump configuration */
730Sstevel@tonic-gate dumphdr_t	*dumphdr;	/* dump header */
740Sstevel@tonic-gate int		dump_conflags = DUMP_KERNEL; /* dump configuration flags */
750Sstevel@tonic-gate vnode_t		*dumpvp;	/* dump device vnode pointer */
760Sstevel@tonic-gate u_offset_t	dumpvp_size;	/* size of dump device, in bytes */
770Sstevel@tonic-gate static u_offset_t dumpvp_limit;	/* maximum write offset */
780Sstevel@tonic-gate char		*dumppath;	/* pathname of dump device */
790Sstevel@tonic-gate int		dump_timeout = 120; /* timeout for dumping page during panic */
800Sstevel@tonic-gate int		dump_timeleft;	/* portion of dump_timeout remaining */
815084Sjohnlev int		dump_ioerr;	/* dump i/o error */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #ifdef DEBUG
840Sstevel@tonic-gate int		dumpfaildebug = 1;	/* enter debugger if dump fails */
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate int		dumpfaildebug = 0;
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static ulong_t	*dump_bitmap;	/* bitmap for marking pages to dump */
900Sstevel@tonic-gate static pgcnt_t	dump_bitmapsize; /* size of bitmap */
910Sstevel@tonic-gate static pid_t	*dump_pids;	/* list of process IDs at dump time */
920Sstevel@tonic-gate static offset_t	dumpvp_off;	/* current dump device offset */
930Sstevel@tonic-gate static char	*dump_cmap;	/* VA for dump compression mapping */
940Sstevel@tonic-gate static char	*dumpbuf_cur, *dumpbuf_start, *dumpbuf_end;
950Sstevel@tonic-gate static char	*dump_cbuf;	/* compression buffer */
960Sstevel@tonic-gate static char	*dump_uebuf;	/* memory error detection buffer */
970Sstevel@tonic-gate static size_t	dumpbuf_size;	/* size of dumpbuf in bytes */
980Sstevel@tonic-gate static size_t	dumpbuf_limit = 1UL << 23;	/* 8MB */
990Sstevel@tonic-gate static size_t	dump_iosize;	/* device's best transfer size, if any */
1000Sstevel@tonic-gate static uint64_t	dumpbuf_thresh = 1ULL << 30;	/* 1GB */
1010Sstevel@tonic-gate static ulong_t	dumpbuf_mult = 8;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * The dump i/o buffer must be at least one page, at most xfer_size bytes, and
1050Sstevel@tonic-gate  * should scale with physmem in between.  The transfer size passed in will
1060Sstevel@tonic-gate  * either represent a global default (maxphys) or the best size for the device.
1070Sstevel@tonic-gate  * Once the physical memory size exceeds dumpbuf_thresh (1GB by default), we
1080Sstevel@tonic-gate  * increase the percentage of physical memory that dumpbuf can consume by a
1090Sstevel@tonic-gate  * factor of dumpbuf_mult (8 by default) to improve large memory performance.
1100Sstevel@tonic-gate  * The size of the dumpbuf i/o buffer is limited by dumpbuf_limit (8MB by
1110Sstevel@tonic-gate  * default) because the dump performance saturates beyond a certain size.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate static size_t
1140Sstevel@tonic-gate dumpbuf_iosize(size_t xfer_size)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	pgcnt_t scale = physmem;
1170Sstevel@tonic-gate 	size_t iosize;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (scale >= dumpbuf_thresh / PAGESIZE) {
1200Sstevel@tonic-gate 		scale *= dumpbuf_mult; /* increase scaling factor */
1210Sstevel@tonic-gate 		iosize = MIN(xfer_size, scale) & PAGEMASK;
1220Sstevel@tonic-gate 		if (dumpbuf_limit && iosize > dumpbuf_limit)
1230Sstevel@tonic-gate 			iosize = MAX(PAGESIZE, dumpbuf_limit & PAGEMASK);
1240Sstevel@tonic-gate 	} else
1250Sstevel@tonic-gate 		iosize = MAX(PAGESIZE, MIN(xfer_size, scale) & PAGEMASK);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return (iosize);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static void
1310Sstevel@tonic-gate dumpbuf_resize(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	char *old_buf = dumpbuf_start;
1340Sstevel@tonic-gate 	size_t old_size = dumpbuf_size;
1350Sstevel@tonic-gate 	char *new_buf;
1360Sstevel@tonic-gate 	size_t new_size;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if ((new_size = dumpbuf_iosize(MAX(dump_iosize, maxphys))) <= old_size)
1410Sstevel@tonic-gate 		return; /* no need to reallocate buffer */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	new_buf = kmem_alloc(new_size, KM_SLEEP);
1440Sstevel@tonic-gate 	dumpbuf_size = new_size;
1450Sstevel@tonic-gate 	dumpbuf_start = new_buf;
1460Sstevel@tonic-gate 	dumpbuf_end = new_buf + new_size;
1470Sstevel@tonic-gate 	kmem_free(old_buf, old_size);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static void
1510Sstevel@tonic-gate dumphdr_init(void)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	pgcnt_t npages = 0;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (dumphdr == NULL) {
1580Sstevel@tonic-gate 		dumphdr = kmem_zalloc(sizeof (dumphdr_t), KM_SLEEP);
1590Sstevel@tonic-gate 		dumphdr->dump_magic = DUMP_MAGIC;
1600Sstevel@tonic-gate 		dumphdr->dump_version = DUMP_VERSION;
1610Sstevel@tonic-gate 		dumphdr->dump_wordsize = DUMP_WORDSIZE;
1620Sstevel@tonic-gate 		dumphdr->dump_pageshift = PAGESHIFT;
1630Sstevel@tonic-gate 		dumphdr->dump_pagesize = PAGESIZE;
1640Sstevel@tonic-gate 		dumphdr->dump_utsname = utsname;
1650Sstevel@tonic-gate 		(void) strcpy(dumphdr->dump_platform, platform);
1660Sstevel@tonic-gate 		dump_cmap = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
1670Sstevel@tonic-gate 		dumpbuf_size = dumpbuf_iosize(maxphys);
1680Sstevel@tonic-gate 		dumpbuf_start = kmem_alloc(dumpbuf_size, KM_SLEEP);
1690Sstevel@tonic-gate 		dumpbuf_end = dumpbuf_start + dumpbuf_size;
1700Sstevel@tonic-gate 		dump_cbuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* compress buf */
1710Sstevel@tonic-gate 		dump_uebuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* UE buf */
1720Sstevel@tonic-gate 		dump_pids = kmem_alloc(v.v_proc * sizeof (pid_t), KM_SLEEP);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1755084Sjohnlev 	npages = num_phys_pages();
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (dump_bitmapsize != npages) {
1780Sstevel@tonic-gate 		void *map = kmem_alloc(BT_SIZEOFMAP(npages), KM_SLEEP);
1790Sstevel@tonic-gate 		kmem_free(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
1800Sstevel@tonic-gate 		dump_bitmap = map;
1810Sstevel@tonic-gate 		dump_bitmapsize = npages;
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Establish a new dump device.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate int
1890Sstevel@tonic-gate dumpinit(vnode_t *vp, char *name, int justchecking)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	vnode_t *cvp;
1920Sstevel@tonic-gate 	vattr_t vattr;
1930Sstevel@tonic-gate 	vnode_t *cdev_vp;
1940Sstevel@tonic-gate 	int error = 0;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	dumphdr_init();
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	cvp = common_specvp(vp);
2010Sstevel@tonic-gate 	if (cvp == dumpvp)
2020Sstevel@tonic-gate 		return (0);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Determine whether this is a plausible dump device.  We want either:
2060Sstevel@tonic-gate 	 * (1) a real device that's not mounted and has a cb_dump routine, or
2070Sstevel@tonic-gate 	 * (2) a swapfile on some filesystem that has a vop_dump routine.
2080Sstevel@tonic-gate 	 */
209*5331Samw 	if ((error = VOP_OPEN(&cvp, FREAD | FWRITE, kcred, NULL)) != 0)
2100Sstevel@tonic-gate 		return (error);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE | AT_TYPE | AT_RDEV;
213*5331Samw 	if ((error = VOP_GETATTR(cvp, &vattr, 0, kcred, NULL)) == 0) {
2140Sstevel@tonic-gate 		if (vattr.va_type == VBLK || vattr.va_type == VCHR) {
2150Sstevel@tonic-gate 			if (devopsp[getmajor(vattr.va_rdev)]->
2160Sstevel@tonic-gate 			    devo_cb_ops->cb_dump == nodev)
2170Sstevel@tonic-gate 				error = ENOTSUP;
2180Sstevel@tonic-gate 			else if (vfs_devismounted(vattr.va_rdev))
2190Sstevel@tonic-gate 				error = EBUSY;
2200Sstevel@tonic-gate 		} else {
2210Sstevel@tonic-gate 			if (vn_matchopval(cvp, VOPNAME_DUMP, fs_nosys) ||
2220Sstevel@tonic-gate 			    !IS_SWAPVP(cvp))
2230Sstevel@tonic-gate 				error = ENOTSUP;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE)
2280Sstevel@tonic-gate 		error = ENOSPC;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if (error || justchecking) {
231*5331Samw 		(void) VOP_CLOSE(cvp, FREAD | FWRITE, 1, (offset_t)0,
232*5331Samw 		    kcred, NULL);
2330Sstevel@tonic-gate 		return (error);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	VN_HOLD(cvp);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (dumpvp != NULL)
2390Sstevel@tonic-gate 		dumpfini();	/* unconfigure the old dump device */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	dumpvp = cvp;
2420Sstevel@tonic-gate 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
2430Sstevel@tonic-gate 	dumppath = kmem_alloc(strlen(name) + 1, KM_SLEEP);
2440Sstevel@tonic-gate 	(void) strcpy(dumppath, name);
2450Sstevel@tonic-gate 	dump_iosize = 0;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * If the dump device is a block device, attempt to open up the
2490Sstevel@tonic-gate 	 * corresponding character device and determine its maximum transfer
2500Sstevel@tonic-gate 	 * size.  We use this information to potentially resize dumpbuf to a
2510Sstevel@tonic-gate 	 * larger and more optimal size for performing i/o to the dump device.
2520Sstevel@tonic-gate 	 */
2530Sstevel@tonic-gate 	if (cvp->v_type == VBLK &&
2540Sstevel@tonic-gate 	    (cdev_vp = makespecvp(VTOS(cvp)->s_dev, VCHR)) != NULL) {
255*5331Samw 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
2560Sstevel@tonic-gate 			size_t blk_size;
2570Sstevel@tonic-gate 			struct dk_cinfo dki;
2580Sstevel@tonic-gate 			struct vtoc vtoc;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCGVTOC, (intptr_t)&vtoc,
261*5331Samw 			    FKIOCTL, kcred, NULL, NULL) == 0 &&
262*5331Samw 			    vtoc.v_sectorsz != 0)
2630Sstevel@tonic-gate 				blk_size = vtoc.v_sectorsz;
2640Sstevel@tonic-gate 			else
2650Sstevel@tonic-gate 				blk_size = DEV_BSIZE;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCINFO, (intptr_t)&dki,
268*5331Samw 			    FKIOCTL, kcred, NULL, NULL) == 0) {
2690Sstevel@tonic-gate 				dump_iosize = dki.dki_maxtransfer * blk_size;
2700Sstevel@tonic-gate 				dumpbuf_resize();
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 
273*5331Samw 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
274*5331Samw 			    kcred, NULL);
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		VN_RELE(cdev_vp);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	cmn_err(CE_CONT, "?dump on %s size %llu MB\n", name, dumpvp_size >> 20);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	return (0);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate void
2860Sstevel@tonic-gate dumpfini(void)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	kmem_free(dumppath, strlen(dumppath) + 1);
2910Sstevel@tonic-gate 
292*5331Samw 	(void) VOP_CLOSE(dumpvp, FREAD | FWRITE, 1, (offset_t)0, kcred, NULL);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	VN_RELE(dumpvp);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	dumpvp = NULL;
2970Sstevel@tonic-gate 	dumpvp_size = 0;
2980Sstevel@tonic-gate 	dumppath = NULL;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static pfn_t
3020Sstevel@tonic-gate dump_bitnum_to_pfn(pgcnt_t bitnum)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	struct memlist *mp;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3070Sstevel@tonic-gate 		if (bitnum < (mp->size >> PAGESHIFT))
3080Sstevel@tonic-gate 			return ((mp->address >> PAGESHIFT) + bitnum);
3090Sstevel@tonic-gate 		bitnum -= mp->size >> PAGESHIFT;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 	return (PFN_INVALID);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate static pgcnt_t
3150Sstevel@tonic-gate dump_pfn_to_bitnum(pfn_t pfn)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	struct memlist *mp;
3180Sstevel@tonic-gate 	pgcnt_t bitnum = 0;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3210Sstevel@tonic-gate 		if (pfn >= (mp->address >> PAGESHIFT) &&
3220Sstevel@tonic-gate 		    pfn < ((mp->address + mp->size) >> PAGESHIFT))
3230Sstevel@tonic-gate 			return (bitnum + pfn - (mp->address >> PAGESHIFT));
3240Sstevel@tonic-gate 		bitnum += mp->size >> PAGESHIFT;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 	return ((pgcnt_t)-1);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate static offset_t
3300Sstevel@tonic-gate dumpvp_flush(void)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	size_t size = P2ROUNDUP(dumpbuf_cur - dumpbuf_start, PAGESIZE);
3330Sstevel@tonic-gate 	int err;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (dumpvp_off + size > dumpvp_limit) {
3360Sstevel@tonic-gate 		dump_ioerr = ENOSPC;
3370Sstevel@tonic-gate 	} else if (size != 0) {
3380Sstevel@tonic-gate 		if (panicstr)
3390Sstevel@tonic-gate 			err = VOP_DUMP(dumpvp, dumpbuf_start,
340*5331Samw 			    lbtodb(dumpvp_off), btod(size), NULL);
3410Sstevel@tonic-gate 		else
3420Sstevel@tonic-gate 			err = vn_rdwr(UIO_WRITE, dumpvp, dumpbuf_start, size,
3430Sstevel@tonic-gate 			    dumpvp_off, UIO_SYSSPACE, 0, dumpvp_limit,
3440Sstevel@tonic-gate 			    kcred, 0);
3450Sstevel@tonic-gate 		if (err && dump_ioerr == 0)
3460Sstevel@tonic-gate 			dump_ioerr = err;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 	dumpvp_off += size;
3490Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
3500Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
3510Sstevel@tonic-gate 	return (dumpvp_off);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate void
3550Sstevel@tonic-gate dumpvp_write(const void *va, size_t size)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	while (size != 0) {
3580Sstevel@tonic-gate 		size_t len = MIN(size, dumpbuf_end - dumpbuf_cur);
3590Sstevel@tonic-gate 		if (len == 0) {
3600Sstevel@tonic-gate 			(void) dumpvp_flush();
3610Sstevel@tonic-gate 		} else {
3620Sstevel@tonic-gate 			bcopy(va, dumpbuf_cur, len);
3630Sstevel@tonic-gate 			va = (char *)va + len;
3640Sstevel@tonic-gate 			dumpbuf_cur += len;
3650Sstevel@tonic-gate 			size -= len;
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*ARGSUSED*/
3710Sstevel@tonic-gate static void
3720Sstevel@tonic-gate dumpvp_ksyms_write(const void *src, void *dst, size_t size)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	dumpvp_write(src, size);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * Mark 'pfn' in the bitmap and dump its translation table entry.
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate void
3810Sstevel@tonic-gate dump_addpage(struct as *as, void *va, pfn_t pfn)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
3840Sstevel@tonic-gate 	pgcnt_t bitnum;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
3870Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
3880Sstevel@tonic-gate 			dumphdr->dump_npages++;
3890Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 		dumphdr->dump_nvtop++;
3920Sstevel@tonic-gate 		mem_vtop.m_as = as;
3930Sstevel@tonic-gate 		mem_vtop.m_va = va;
3940Sstevel@tonic-gate 		mem_vtop.m_pfn = pfn;
3950Sstevel@tonic-gate 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate  * Mark 'pfn' in the bitmap
4020Sstevel@tonic-gate  */
4030Sstevel@tonic-gate void
4040Sstevel@tonic-gate dump_page(pfn_t pfn)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	pgcnt_t bitnum;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
4090Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
4100Sstevel@tonic-gate 			dumphdr->dump_npages++;
4110Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate  * Dump the <as, va, pfn> information for a given address space.
4190Sstevel@tonic-gate  * SEGOP_DUMP() will call dump_addpage() for each page in the segment.
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate static void
4220Sstevel@tonic-gate dump_as(struct as *as)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	struct seg *seg;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
4270Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
4280Sstevel@tonic-gate 		if (seg->s_as != as)
4290Sstevel@tonic-gate 			break;
4300Sstevel@tonic-gate 		if (seg->s_ops == NULL)
4310Sstevel@tonic-gate 			continue;
4320Sstevel@tonic-gate 		SEGOP_DUMP(seg);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (seg != NULL)
4370Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid segment %p in address space %p",
4380Sstevel@tonic-gate 		    (void *)seg, (void *)as);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate static int
4420Sstevel@tonic-gate dump_process(pid_t pid)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	proc_t *p = sprlock(pid);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (p == NULL)
4470Sstevel@tonic-gate 		return (-1);
4480Sstevel@tonic-gate 	if (p->p_as != &kas) {
4490Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4500Sstevel@tonic-gate 		dump_as(p->p_as);
4510Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	sprunlock(p);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	return (0);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate void
4600Sstevel@tonic-gate dump_ereports(void)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	u_offset_t dumpvp_start;
4630Sstevel@tonic-gate 	erpt_dump_t ed;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL)
4660Sstevel@tonic-gate 		return;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
4690Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE);
4700Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_ERPTSIZE;
4710Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	fm_ereport_dump();
4740Sstevel@tonic-gate 	if (panicstr)
4750Sstevel@tonic-gate 		errorq_dump();
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	bzero(&ed, sizeof (ed)); /* indicate end of ereports */
4780Sstevel@tonic-gate 	dumpvp_write(&ed, sizeof (ed));
4790Sstevel@tonic-gate 	(void) dumpvp_flush();
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if (!panicstr) {
4820Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
4830Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
484*5331Samw 		    B_INVAL | B_FORCE, kcred, NULL);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate void
4890Sstevel@tonic-gate dump_messages(void)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	log_dump_t ld;
4920Sstevel@tonic-gate 	mblk_t *mctl, *mdata;
4930Sstevel@tonic-gate 	queue_t *q, *qlast;
4940Sstevel@tonic-gate 	u_offset_t dumpvp_start;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL || log_consq == NULL)
4970Sstevel@tonic-gate 		return;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5000Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_OFFSET;
5010Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_LOGSIZE;
5020Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	qlast = NULL;
5050Sstevel@tonic-gate 	do {
5060Sstevel@tonic-gate 		for (q = log_consq; q->q_next != qlast; q = q->q_next)
5070Sstevel@tonic-gate 			continue;
5080Sstevel@tonic-gate 		for (mctl = q->q_first; mctl != NULL; mctl = mctl->b_next) {
5090Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
5100Sstevel@tonic-gate 			mdata = mctl->b_cont;
5110Sstevel@tonic-gate 			ld.ld_magic = LOG_MAGIC;
5120Sstevel@tonic-gate 			ld.ld_msgsize = MBLKL(mctl->b_cont);
5130Sstevel@tonic-gate 			ld.ld_csum = checksum32(mctl->b_rptr, MBLKL(mctl));
5140Sstevel@tonic-gate 			ld.ld_msum = checksum32(mdata->b_rptr, MBLKL(mdata));
5150Sstevel@tonic-gate 			dumpvp_write(&ld, sizeof (ld));
5160Sstevel@tonic-gate 			dumpvp_write(mctl->b_rptr, MBLKL(mctl));
5170Sstevel@tonic-gate 			dumpvp_write(mdata->b_rptr, MBLKL(mdata));
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 	} while ((qlast = q) != log_consq);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	ld.ld_magic = 0;		/* indicate end of messages */
5220Sstevel@tonic-gate 	dumpvp_write(&ld, sizeof (ld));
5230Sstevel@tonic-gate 	(void) dumpvp_flush();
5240Sstevel@tonic-gate 	if (!panicstr) {
5250Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
5260Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
527*5331Samw 		    B_INVAL | B_FORCE, kcred, NULL);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate static void
5320Sstevel@tonic-gate dump_pagecopy(void *src, void *dst)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	long *wsrc = (long *)src;
5350Sstevel@tonic-gate 	long *wdst = (long *)dst;
5360Sstevel@tonic-gate 	const ulong_t ncopies = PAGESIZE / sizeof (long);
5370Sstevel@tonic-gate 	volatile int w = 0;
5380Sstevel@tonic-gate 	volatile int ueoff = -1;
5390Sstevel@tonic-gate 	on_trap_data_t otd;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_EC)) {
5420Sstevel@tonic-gate 		if (ueoff == -1) {
5430Sstevel@tonic-gate 			uint64_t pa;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 			ueoff = w * sizeof (long);
5460Sstevel@tonic-gate 			pa = ptob((uint64_t)hat_getpfnum(kas.a_hat, src))
5470Sstevel@tonic-gate 			    + ueoff;
5480Sstevel@tonic-gate 			cmn_err(CE_WARN, "memory error at PA 0x%08x.%08x",
5490Sstevel@tonic-gate 			    (uint32_t)(pa >> 32), (uint32_t)pa);
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate #ifdef _LP64
5520Sstevel@tonic-gate 		wdst[w++] = 0xbadecc00badecc;
5530Sstevel@tonic-gate #else
5540Sstevel@tonic-gate 		wdst[w++] = 0xbadecc;
5550Sstevel@tonic-gate #endif
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	while (w < ncopies) {
5580Sstevel@tonic-gate 		wdst[w] = wsrc[w];
5590Sstevel@tonic-gate 		w++;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	no_trap();
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Dump the system.
5660Sstevel@tonic-gate  */
5670Sstevel@tonic-gate void
5680Sstevel@tonic-gate dumpsys(void)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate 	pfn_t pfn;
5710Sstevel@tonic-gate 	pgcnt_t bitnum;
5720Sstevel@tonic-gate 	int npages = 0;
5730Sstevel@tonic-gate 	int percent_done = 0;
5740Sstevel@tonic-gate 	uint32_t csize;
5750Sstevel@tonic-gate 	u_offset_t total_csize = 0;
5760Sstevel@tonic-gate 	int compress_ratio;
5770Sstevel@tonic-gate 	proc_t *p;
5780Sstevel@tonic-gate 	pid_t npids, pidx;
5790Sstevel@tonic-gate 	char *content;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL) {
5820Sstevel@tonic-gate 		uprintf("skipping system dump - no dump device configured\n");
5830Sstevel@tonic-gate 		return;
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * Calculate the starting block for dump.  If we're dumping on a
5890Sstevel@tonic-gate 	 * swap device, start 1/5 of the way in; otherwise, start at the
5900Sstevel@tonic-gate 	 * beginning.  And never use the first page -- it may be a disk label.
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	if (dumpvp->v_flag & VISSWAP)
5930Sstevel@tonic-gate 		dumphdr->dump_start = P2ROUNDUP(dumpvp_size / 5, DUMP_OFFSET);
5940Sstevel@tonic-gate 	else
5950Sstevel@tonic-gate 		dumphdr->dump_start = DUMP_OFFSET;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	dumphdr->dump_flags = DF_VALID | DF_COMPLETE | DF_LIVE;
5980Sstevel@tonic-gate 	dumphdr->dump_crashtime = gethrestime_sec();
5990Sstevel@tonic-gate 	dumphdr->dump_npages = 0;
6000Sstevel@tonic-gate 	dumphdr->dump_nvtop = 0;
6010Sstevel@tonic-gate 	bzero(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
6020Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if (panicstr) {
6050Sstevel@tonic-gate 		dumphdr->dump_flags &= ~DF_LIVE;
606*5331Samw 		(void) VOP_DUMPCTL(dumpvp, DUMP_FREE, NULL, NULL);
607*5331Samw 		(void) VOP_DUMPCTL(dumpvp, DUMP_ALLOC, NULL, NULL);
6080Sstevel@tonic-gate 		(void) vsnprintf(dumphdr->dump_panicstring, DUMP_PANICSIZE,
6090Sstevel@tonic-gate 		    panicstr, panicargs);
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL)
6130Sstevel@tonic-gate 		content = "all";
6140Sstevel@tonic-gate 	else if (dump_conflags & DUMP_CURPROC)
6150Sstevel@tonic-gate 		content = "kernel + curproc";
6160Sstevel@tonic-gate 	else
6170Sstevel@tonic-gate 		content = "kernel";
6180Sstevel@tonic-gate 	uprintf("dumping to %s, offset %lld, content: %s\n", dumppath,
6190Sstevel@tonic-gate 	    dumphdr->dump_start, content);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/*
6220Sstevel@tonic-gate 	 * Leave room for the message and ereport save areas and terminal dump
6230Sstevel@tonic-gate 	 * header.
6240Sstevel@tonic-gate 	 */
6250Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_LOGSIZE - DUMP_OFFSET - DUMP_ERPTSIZE;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * Write out the symbol table.  It's no longer compressed,
6290Sstevel@tonic-gate 	 * so its 'size' and 'csize' are equal.
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_ksyms = dumphdr->dump_start + PAGESIZE;
6320Sstevel@tonic-gate 	dumphdr->dump_ksyms_size = dumphdr->dump_ksyms_csize =
6330Sstevel@tonic-gate 	    ksyms_snapshot(dumpvp_ksyms_write, NULL, LONG_MAX);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/*
6360Sstevel@tonic-gate 	 * Write out the translation map.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	dumphdr->dump_map = dumpvp_flush();
6390Sstevel@tonic-gate 	dump_as(&kas);
6403446Smrj 	dumphdr->dump_nvtop += dump_plat_addr();
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/*
6430Sstevel@tonic-gate 	 * call into hat, which may have unmapped pages that also need to
6440Sstevel@tonic-gate 	 * be in the dump
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	hat_dump();
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL) {
6490Sstevel@tonic-gate 		mutex_enter(&pidlock);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		for (npids = 0, p = practive; p != NULL; p = p->p_next)
6520Sstevel@tonic-gate 			dump_pids[npids++] = p->p_pid;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		mutex_exit(&pidlock);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		for (pidx = 0; pidx < npids; pidx++)
6570Sstevel@tonic-gate 			(void) dump_process(dump_pids[pidx]);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
6600Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
6610Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 		dumphdr->dump_npages = dump_bitmapsize;
6640Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_ALL;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	} else if (dump_conflags & DUMP_CURPROC) {
6670Sstevel@tonic-gate 		/*
6680Sstevel@tonic-gate 		 * Determine which pid is to be dumped.  If we're panicking, we
6690Sstevel@tonic-gate 		 * dump the process associated with panic_thread (if any).  If
6700Sstevel@tonic-gate 		 * this is a live dump, we dump the process associated with
6710Sstevel@tonic-gate 		 * curthread.
6720Sstevel@tonic-gate 		 */
6730Sstevel@tonic-gate 		npids = 0;
6740Sstevel@tonic-gate 		if (panicstr) {
6750Sstevel@tonic-gate 			if (panic_thread != NULL &&
6760Sstevel@tonic-gate 			    panic_thread->t_procp != NULL &&
6770Sstevel@tonic-gate 			    panic_thread->t_procp != &p0) {
6780Sstevel@tonic-gate 				dump_pids[npids++] =
6790Sstevel@tonic-gate 				    panic_thread->t_procp->p_pid;
6800Sstevel@tonic-gate 			}
6810Sstevel@tonic-gate 		} else {
6820Sstevel@tonic-gate 			dump_pids[npids++] = curthread->t_procp->p_pid;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		if (npids && dump_process(dump_pids[0]) == 0)
6860Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_CURPROC;
6870Sstevel@tonic-gate 		else
6880Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_KERNEL;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	} else {
6910Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_KERNEL;
6920Sstevel@tonic-gate 	}
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	dumphdr->dump_hashmask = (1 << highbit(dumphdr->dump_nvtop - 1)) - 1;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * Write out the pfn table.
6980Sstevel@tonic-gate 	 */
6990Sstevel@tonic-gate 	dumphdr->dump_pfn = dumpvp_flush();
7000Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7010Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7020Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7030Sstevel@tonic-gate 			continue;
7040Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7050Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7060Sstevel@tonic-gate 		dumpvp_write(&pfn, sizeof (pfn_t));
7070Sstevel@tonic-gate 	}
7083446Smrj 	dump_plat_pfn();
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * Write out all the pages.
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 	dumphdr->dump_data = dumpvp_flush();
7140Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7150Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7160Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7170Sstevel@tonic-gate 			continue;
7180Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7190Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		/*
7220Sstevel@tonic-gate 		 * Map in page frame 'pfn', scan it for UE's while copying
7230Sstevel@tonic-gate 		 * the data to dump_uebuf, unmap it, compress dump_uebuf into
7240Sstevel@tonic-gate 		 * dump_cbuf, and write out dump_cbuf.  The UE check ensures
7250Sstevel@tonic-gate 		 * that we don't lose the whole dump because of a latent UE.
7260Sstevel@tonic-gate 		 */
7270Sstevel@tonic-gate 		hat_devload(kas.a_hat, dump_cmap, PAGESIZE, pfn, PROT_READ,
7280Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST);
7290Sstevel@tonic-gate 		dump_pagecopy(dump_cmap, dump_uebuf);
7300Sstevel@tonic-gate 		hat_unload(kas.a_hat, dump_cmap, PAGESIZE, HAT_UNLOAD);
7310Sstevel@tonic-gate 		csize = (uint32_t)compress(dump_uebuf, dump_cbuf, PAGESIZE);
7320Sstevel@tonic-gate 		dumpvp_write(&csize, sizeof (uint32_t));
7330Sstevel@tonic-gate 		dumpvp_write(dump_cbuf, csize);
7340Sstevel@tonic-gate 		if (dump_ioerr) {
7350Sstevel@tonic-gate 			dumphdr->dump_flags &= ~DF_COMPLETE;
7360Sstevel@tonic-gate 			dumphdr->dump_npages = npages;
7370Sstevel@tonic-gate 			break;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		total_csize += csize;
7400Sstevel@tonic-gate 		if (++npages * 100LL / dumphdr->dump_npages > percent_done) {
7410Sstevel@tonic-gate 			uprintf("^\r%3d%% done", ++percent_done);
7420Sstevel@tonic-gate 			if (!panicstr)
7430Sstevel@tonic-gate 				delay(1);	/* let the output be sent */
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 	}
7463446Smrj 	dumphdr->dump_npages += dump_plat_data(dump_cbuf);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	(void) dumpvp_flush();
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/*
7510Sstevel@tonic-gate 	 * Write out the initial and terminal dump headers.
7520Sstevel@tonic-gate 	 */
7530Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_start;
7540Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
7550Sstevel@tonic-gate 	(void) dumpvp_flush();
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size;
7580Sstevel@tonic-gate 	dumpvp_off = dumpvp_limit - DUMP_OFFSET;
7590Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
7600Sstevel@tonic-gate 	(void) dumpvp_flush();
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	compress_ratio = (int)(100LL * npages / (btopr(total_csize + 1)));
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	uprintf("\r%3d%% done: %d pages dumped, compression ratio %d.%02d, ",
7650Sstevel@tonic-gate 	    percent_done, npages, compress_ratio / 100, compress_ratio % 100);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (dump_ioerr == 0) {
7680Sstevel@tonic-gate 		uprintf("dump succeeded\n");
7690Sstevel@tonic-gate 	} else {
7700Sstevel@tonic-gate 		uprintf("dump failed: error %d\n", dump_ioerr);
7710Sstevel@tonic-gate 		if (panicstr && dumpfaildebug)
7720Sstevel@tonic-gate 			debug_enter("dump failed");
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * Write out all undelivered messages.  This has to be the *last*
7770Sstevel@tonic-gate 	 * thing we do because the dump process itself emits messages.
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	if (panicstr) {
7800Sstevel@tonic-gate 		dump_ereports();
7810Sstevel@tonic-gate 		dump_messages();
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	delay(2 * hz);	/* let people see the 'done' message */
7850Sstevel@tonic-gate 	dump_timeleft = 0;
7860Sstevel@tonic-gate 	dump_ioerr = 0;
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate  * This function is called whenever the memory size, as represented
7910Sstevel@tonic-gate  * by the phys_install list, changes.
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate void
7940Sstevel@tonic-gate dump_resize()
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	mutex_enter(&dump_lock);
7970Sstevel@tonic-gate 	dumphdr_init();
7980Sstevel@tonic-gate 	dumpbuf_resize();
7990Sstevel@tonic-gate 	mutex_exit(&dump_lock);
8000Sstevel@tonic-gate }
801