xref: /onnv-gate/usr/src/uts/common/os/dumpsubr.c (revision 10588:dc03f981ea18)
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  */
217563SPrasad.Singamsetty@Sun.COM 
220Sstevel@tonic-gate /*
239889SLarry.Liu@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/vm.h>
310Sstevel@tonic-gate #include <sys/proc.h>
320Sstevel@tonic-gate #include <sys/file.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/mem.h>
360Sstevel@tonic-gate #include <sys/mman.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/memlist.h>
400Sstevel@tonic-gate #include <sys/dumphdr.h>
410Sstevel@tonic-gate #include <sys/dumpadm.h>
420Sstevel@tonic-gate #include <sys/ksyms.h>
430Sstevel@tonic-gate #include <sys/compress.h>
440Sstevel@tonic-gate #include <sys/stream.h>
450Sstevel@tonic-gate #include <sys/strsun.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/bitmap.h>
480Sstevel@tonic-gate #include <sys/modctl.h>
490Sstevel@tonic-gate #include <sys/utsname.h>
500Sstevel@tonic-gate #include <sys/systeminfo.h>
510Sstevel@tonic-gate #include <sys/vmem.h>
520Sstevel@tonic-gate #include <sys/log.h>
530Sstevel@tonic-gate #include <sys/var.h>
540Sstevel@tonic-gate #include <sys/debug.h>
550Sstevel@tonic-gate #include <sys/sunddi.h>
560Sstevel@tonic-gate #include <fs/fs_subr.h>
570Sstevel@tonic-gate #include <sys/fs/snode.h>
580Sstevel@tonic-gate #include <sys/ontrap.h>
590Sstevel@tonic-gate #include <sys/panic.h>
600Sstevel@tonic-gate #include <sys/dkio.h>
610Sstevel@tonic-gate #include <sys/vtoc.h>
620Sstevel@tonic-gate #include <sys/errorq.h>
630Sstevel@tonic-gate #include <sys/fm/util.h>
646423Sgw25295 #include <sys/fs/zfs.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 	 */
2095331Samw 	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;
2135331Samw 	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;
220*10588SEric.Taylor@Sun.COM 			if (strcmp(ddi_driver_name(VTOS(cvp)->s_dip),
221*10588SEric.Taylor@Sun.COM 			    ZFS_DRIVER) == 0 &&
222*10588SEric.Taylor@Sun.COM 			    IS_SWAPVP(common_specvp(cvp)))
223*10588SEric.Taylor@Sun.COM 					error = EBUSY;
2240Sstevel@tonic-gate 		} else {
2250Sstevel@tonic-gate 			if (vn_matchopval(cvp, VOPNAME_DUMP, fs_nosys) ||
2260Sstevel@tonic-gate 			    !IS_SWAPVP(cvp))
2270Sstevel@tonic-gate 				error = ENOTSUP;
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE)
2320Sstevel@tonic-gate 		error = ENOSPC;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (error || justchecking) {
2355331Samw 		(void) VOP_CLOSE(cvp, FREAD | FWRITE, 1, (offset_t)0,
2365331Samw 		    kcred, NULL);
2370Sstevel@tonic-gate 		return (error);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	VN_HOLD(cvp);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (dumpvp != NULL)
2430Sstevel@tonic-gate 		dumpfini();	/* unconfigure the old dump device */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	dumpvp = cvp;
2460Sstevel@tonic-gate 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
2470Sstevel@tonic-gate 	dumppath = kmem_alloc(strlen(name) + 1, KM_SLEEP);
2480Sstevel@tonic-gate 	(void) strcpy(dumppath, name);
2490Sstevel@tonic-gate 	dump_iosize = 0;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * If the dump device is a block device, attempt to open up the
2530Sstevel@tonic-gate 	 * corresponding character device and determine its maximum transfer
2540Sstevel@tonic-gate 	 * size.  We use this information to potentially resize dumpbuf to a
2550Sstevel@tonic-gate 	 * larger and more optimal size for performing i/o to the dump device.
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	if (cvp->v_type == VBLK &&
2580Sstevel@tonic-gate 	    (cdev_vp = makespecvp(VTOS(cvp)->s_dev, VCHR)) != NULL) {
2595331Samw 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
2600Sstevel@tonic-gate 			size_t blk_size;
2610Sstevel@tonic-gate 			struct dk_cinfo dki;
2629889SLarry.Liu@Sun.COM 			struct dk_minfo minf;
2630Sstevel@tonic-gate 
2649889SLarry.Liu@Sun.COM 			if (VOP_IOCTL(cdev_vp, DKIOCGMEDIAINFO,
2659889SLarry.Liu@Sun.COM 			    (intptr_t)&minf, FKIOCTL, kcred, NULL, NULL)
2669889SLarry.Liu@Sun.COM 			    == 0 && minf.dki_lbsize != 0)
2679889SLarry.Liu@Sun.COM 				blk_size = minf.dki_lbsize;
2680Sstevel@tonic-gate 			else
2690Sstevel@tonic-gate 				blk_size = DEV_BSIZE;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCINFO, (intptr_t)&dki,
2725331Samw 			    FKIOCTL, kcred, NULL, NULL) == 0) {
2730Sstevel@tonic-gate 				dump_iosize = dki.dki_maxtransfer * blk_size;
2740Sstevel@tonic-gate 				dumpbuf_resize();
2750Sstevel@tonic-gate 			}
2766423Sgw25295 			/*
277*10588SEric.Taylor@Sun.COM 			 * If we are working with a zvol then dumpify it
278*10588SEric.Taylor@Sun.COM 			 * if it's not being used as swap.
2796423Sgw25295 			 */
2806423Sgw25295 			if (strcmp(dki.dki_dname, ZVOL_DRIVER) == 0) {
281*10588SEric.Taylor@Sun.COM 				if (IS_SWAPVP(common_specvp(cvp)))
282*10588SEric.Taylor@Sun.COM 					error = EBUSY;
283*10588SEric.Taylor@Sun.COM 				else if ((error = VOP_IOCTL(cdev_vp,
2846423Sgw25295 				    DKIOCDUMPINIT, NULL, FKIOCTL, kcred,
285*10588SEric.Taylor@Sun.COM 				    NULL, NULL)) != 0)
2866423Sgw25295 					dumpfini();
2876423Sgw25295 			}
2880Sstevel@tonic-gate 
2895331Samw 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
2905331Samw 			    kcred, NULL);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		VN_RELE(cdev_vp);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	cmn_err(CE_CONT, "?dump on %s size %llu MB\n", name, dumpvp_size >> 20);
2970Sstevel@tonic-gate 
2986423Sgw25295 	return (error);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate void
3020Sstevel@tonic-gate dumpfini(void)
3030Sstevel@tonic-gate {
3046423Sgw25295 	vattr_t vattr;
3056423Sgw25295 	boolean_t is_zfs = B_FALSE;
3066423Sgw25295 	vnode_t *cdev_vp;
3070Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	kmem_free(dumppath, strlen(dumppath) + 1);
3100Sstevel@tonic-gate 
3116423Sgw25295 	/*
3126423Sgw25295 	 * Determine if we are using zvols for our dump device
3136423Sgw25295 	 */
3146423Sgw25295 	vattr.va_mask = AT_RDEV;
3156423Sgw25295 	if (VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL) == 0) {
3166423Sgw25295 		is_zfs = (getmajor(vattr.va_rdev) ==
3176423Sgw25295 		    ddi_name_to_major(ZFS_DRIVER)) ? B_TRUE : B_FALSE;
3186423Sgw25295 	}
3196423Sgw25295 
3206423Sgw25295 	/*
3216423Sgw25295 	 * If we have a zvol dump device then we call into zfs so
3226423Sgw25295 	 * that it may have a chance to cleanup.
3236423Sgw25295 	 */
3246423Sgw25295 	if (is_zfs &&
3256423Sgw25295 	    (cdev_vp = makespecvp(VTOS(dumpvp)->s_dev, VCHR)) != NULL) {
3266423Sgw25295 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
3276423Sgw25295 			(void) VOP_IOCTL(cdev_vp, DKIOCDUMPFINI, NULL, FKIOCTL,
3286423Sgw25295 			    kcred, NULL, NULL);
3296423Sgw25295 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
3306423Sgw25295 			    kcred, NULL);
3316423Sgw25295 		}
3326423Sgw25295 		VN_RELE(cdev_vp);
3336423Sgw25295 	}
3346423Sgw25295 
3355331Samw 	(void) VOP_CLOSE(dumpvp, FREAD | FWRITE, 1, (offset_t)0, kcred, NULL);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	VN_RELE(dumpvp);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	dumpvp = NULL;
3400Sstevel@tonic-gate 	dumpvp_size = 0;
3410Sstevel@tonic-gate 	dumppath = NULL;
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static pfn_t
3450Sstevel@tonic-gate dump_bitnum_to_pfn(pgcnt_t bitnum)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	struct memlist *mp;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3500Sstevel@tonic-gate 		if (bitnum < (mp->size >> PAGESHIFT))
3510Sstevel@tonic-gate 			return ((mp->address >> PAGESHIFT) + bitnum);
3520Sstevel@tonic-gate 		bitnum -= mp->size >> PAGESHIFT;
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	return (PFN_INVALID);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate static pgcnt_t
3580Sstevel@tonic-gate dump_pfn_to_bitnum(pfn_t pfn)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	struct memlist *mp;
3610Sstevel@tonic-gate 	pgcnt_t bitnum = 0;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3640Sstevel@tonic-gate 		if (pfn >= (mp->address >> PAGESHIFT) &&
3650Sstevel@tonic-gate 		    pfn < ((mp->address + mp->size) >> PAGESHIFT))
3660Sstevel@tonic-gate 			return (bitnum + pfn - (mp->address >> PAGESHIFT));
3670Sstevel@tonic-gate 		bitnum += mp->size >> PAGESHIFT;
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 	return ((pgcnt_t)-1);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static offset_t
3730Sstevel@tonic-gate dumpvp_flush(void)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	size_t size = P2ROUNDUP(dumpbuf_cur - dumpbuf_start, PAGESIZE);
3760Sstevel@tonic-gate 	int err;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (dumpvp_off + size > dumpvp_limit) {
3790Sstevel@tonic-gate 		dump_ioerr = ENOSPC;
3800Sstevel@tonic-gate 	} else if (size != 0) {
3810Sstevel@tonic-gate 		if (panicstr)
3820Sstevel@tonic-gate 			err = VOP_DUMP(dumpvp, dumpbuf_start,
3835331Samw 			    lbtodb(dumpvp_off), btod(size), NULL);
3840Sstevel@tonic-gate 		else
3850Sstevel@tonic-gate 			err = vn_rdwr(UIO_WRITE, dumpvp, dumpbuf_start, size,
3860Sstevel@tonic-gate 			    dumpvp_off, UIO_SYSSPACE, 0, dumpvp_limit,
3870Sstevel@tonic-gate 			    kcred, 0);
3880Sstevel@tonic-gate 		if (err && dump_ioerr == 0)
3890Sstevel@tonic-gate 			dump_ioerr = err;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 	dumpvp_off += size;
3920Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
3930Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
3940Sstevel@tonic-gate 	return (dumpvp_off);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate void
3980Sstevel@tonic-gate dumpvp_write(const void *va, size_t size)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	while (size != 0) {
4010Sstevel@tonic-gate 		size_t len = MIN(size, dumpbuf_end - dumpbuf_cur);
4020Sstevel@tonic-gate 		if (len == 0) {
4030Sstevel@tonic-gate 			(void) dumpvp_flush();
4040Sstevel@tonic-gate 		} else {
4050Sstevel@tonic-gate 			bcopy(va, dumpbuf_cur, len);
4060Sstevel@tonic-gate 			va = (char *)va + len;
4070Sstevel@tonic-gate 			dumpbuf_cur += len;
4080Sstevel@tonic-gate 			size -= len;
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*ARGSUSED*/
4140Sstevel@tonic-gate static void
4150Sstevel@tonic-gate dumpvp_ksyms_write(const void *src, void *dst, size_t size)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	dumpvp_write(src, size);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * Mark 'pfn' in the bitmap and dump its translation table entry.
4220Sstevel@tonic-gate  */
4230Sstevel@tonic-gate void
4240Sstevel@tonic-gate dump_addpage(struct as *as, void *va, pfn_t pfn)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
4270Sstevel@tonic-gate 	pgcnt_t bitnum;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
4300Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
4310Sstevel@tonic-gate 			dumphdr->dump_npages++;
4320Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 		dumphdr->dump_nvtop++;
4350Sstevel@tonic-gate 		mem_vtop.m_as = as;
4360Sstevel@tonic-gate 		mem_vtop.m_va = va;
4370Sstevel@tonic-gate 		mem_vtop.m_pfn = pfn;
4380Sstevel@tonic-gate 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * Mark 'pfn' in the bitmap
4450Sstevel@tonic-gate  */
4460Sstevel@tonic-gate void
4470Sstevel@tonic-gate dump_page(pfn_t pfn)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	pgcnt_t bitnum;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
4520Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
4530Sstevel@tonic-gate 			dumphdr->dump_npages++;
4540Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Dump the <as, va, pfn> information for a given address space.
4620Sstevel@tonic-gate  * SEGOP_DUMP() will call dump_addpage() for each page in the segment.
4630Sstevel@tonic-gate  */
4640Sstevel@tonic-gate static void
4650Sstevel@tonic-gate dump_as(struct as *as)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	struct seg *seg;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
4700Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
4710Sstevel@tonic-gate 		if (seg->s_as != as)
4720Sstevel@tonic-gate 			break;
4730Sstevel@tonic-gate 		if (seg->s_ops == NULL)
4740Sstevel@tonic-gate 			continue;
4750Sstevel@tonic-gate 		SEGOP_DUMP(seg);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if (seg != NULL)
4800Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid segment %p in address space %p",
4810Sstevel@tonic-gate 		    (void *)seg, (void *)as);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate static int
4850Sstevel@tonic-gate dump_process(pid_t pid)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	proc_t *p = sprlock(pid);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (p == NULL)
4900Sstevel@tonic-gate 		return (-1);
4910Sstevel@tonic-gate 	if (p->p_as != &kas) {
4920Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4930Sstevel@tonic-gate 		dump_as(p->p_as);
4940Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	sprunlock(p);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	return (0);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate void
5030Sstevel@tonic-gate dump_ereports(void)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	u_offset_t dumpvp_start;
5060Sstevel@tonic-gate 	erpt_dump_t ed;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL)
5090Sstevel@tonic-gate 		return;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5120Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE);
5130Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_ERPTSIZE;
5140Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	fm_ereport_dump();
5170Sstevel@tonic-gate 	if (panicstr)
5180Sstevel@tonic-gate 		errorq_dump();
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	bzero(&ed, sizeof (ed)); /* indicate end of ereports */
5210Sstevel@tonic-gate 	dumpvp_write(&ed, sizeof (ed));
5220Sstevel@tonic-gate 	(void) dumpvp_flush();
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (!panicstr) {
5250Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
5260Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
5275331Samw 		    B_INVAL | B_FORCE, kcred, NULL);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate void
5320Sstevel@tonic-gate dump_messages(void)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	log_dump_t ld;
5350Sstevel@tonic-gate 	mblk_t *mctl, *mdata;
5360Sstevel@tonic-gate 	queue_t *q, *qlast;
5370Sstevel@tonic-gate 	u_offset_t dumpvp_start;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL || log_consq == NULL)
5400Sstevel@tonic-gate 		return;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5430Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_OFFSET;
5440Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_LOGSIZE;
5450Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	qlast = NULL;
5480Sstevel@tonic-gate 	do {
5490Sstevel@tonic-gate 		for (q = log_consq; q->q_next != qlast; q = q->q_next)
5500Sstevel@tonic-gate 			continue;
5510Sstevel@tonic-gate 		for (mctl = q->q_first; mctl != NULL; mctl = mctl->b_next) {
5520Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
5530Sstevel@tonic-gate 			mdata = mctl->b_cont;
5540Sstevel@tonic-gate 			ld.ld_magic = LOG_MAGIC;
5550Sstevel@tonic-gate 			ld.ld_msgsize = MBLKL(mctl->b_cont);
5560Sstevel@tonic-gate 			ld.ld_csum = checksum32(mctl->b_rptr, MBLKL(mctl));
5570Sstevel@tonic-gate 			ld.ld_msum = checksum32(mdata->b_rptr, MBLKL(mdata));
5580Sstevel@tonic-gate 			dumpvp_write(&ld, sizeof (ld));
5590Sstevel@tonic-gate 			dumpvp_write(mctl->b_rptr, MBLKL(mctl));
5600Sstevel@tonic-gate 			dumpvp_write(mdata->b_rptr, MBLKL(mdata));
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 	} while ((qlast = q) != log_consq);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	ld.ld_magic = 0;		/* indicate end of messages */
5650Sstevel@tonic-gate 	dumpvp_write(&ld, sizeof (ld));
5660Sstevel@tonic-gate 	(void) dumpvp_flush();
5670Sstevel@tonic-gate 	if (!panicstr) {
5680Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
5690Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
5705331Samw 		    B_INVAL | B_FORCE, kcred, NULL);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate static void
5750Sstevel@tonic-gate dump_pagecopy(void *src, void *dst)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate 	long *wsrc = (long *)src;
5780Sstevel@tonic-gate 	long *wdst = (long *)dst;
5790Sstevel@tonic-gate 	const ulong_t ncopies = PAGESIZE / sizeof (long);
5800Sstevel@tonic-gate 	volatile int w = 0;
5810Sstevel@tonic-gate 	volatile int ueoff = -1;
5820Sstevel@tonic-gate 	on_trap_data_t otd;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_EC)) {
5850Sstevel@tonic-gate 		if (ueoff == -1) {
5860Sstevel@tonic-gate 			uint64_t pa;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 			ueoff = w * sizeof (long);
5890Sstevel@tonic-gate 			pa = ptob((uint64_t)hat_getpfnum(kas.a_hat, src))
5900Sstevel@tonic-gate 			    + ueoff;
5910Sstevel@tonic-gate 			cmn_err(CE_WARN, "memory error at PA 0x%08x.%08x",
5920Sstevel@tonic-gate 			    (uint32_t)(pa >> 32), (uint32_t)pa);
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate #ifdef _LP64
5950Sstevel@tonic-gate 		wdst[w++] = 0xbadecc00badecc;
5960Sstevel@tonic-gate #else
5970Sstevel@tonic-gate 		wdst[w++] = 0xbadecc;
5980Sstevel@tonic-gate #endif
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 	while (w < ncopies) {
6010Sstevel@tonic-gate 		wdst[w] = wsrc[w];
6020Sstevel@tonic-gate 		w++;
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	no_trap();
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate  * Dump the system.
6090Sstevel@tonic-gate  */
6100Sstevel@tonic-gate void
6110Sstevel@tonic-gate dumpsys(void)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	pfn_t pfn;
6140Sstevel@tonic-gate 	pgcnt_t bitnum;
6150Sstevel@tonic-gate 	int npages = 0;
6160Sstevel@tonic-gate 	int percent_done = 0;
6170Sstevel@tonic-gate 	uint32_t csize;
6180Sstevel@tonic-gate 	u_offset_t total_csize = 0;
6190Sstevel@tonic-gate 	int compress_ratio;
6200Sstevel@tonic-gate 	proc_t *p;
6210Sstevel@tonic-gate 	pid_t npids, pidx;
6220Sstevel@tonic-gate 	char *content;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL) {
6250Sstevel@tonic-gate 		uprintf("skipping system dump - no dump device configured\n");
6260Sstevel@tonic-gate 		return;
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/*
6310Sstevel@tonic-gate 	 * Calculate the starting block for dump.  If we're dumping on a
6320Sstevel@tonic-gate 	 * swap device, start 1/5 of the way in; otherwise, start at the
6330Sstevel@tonic-gate 	 * beginning.  And never use the first page -- it may be a disk label.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	if (dumpvp->v_flag & VISSWAP)
6360Sstevel@tonic-gate 		dumphdr->dump_start = P2ROUNDUP(dumpvp_size / 5, DUMP_OFFSET);
6370Sstevel@tonic-gate 	else
6380Sstevel@tonic-gate 		dumphdr->dump_start = DUMP_OFFSET;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	dumphdr->dump_flags = DF_VALID | DF_COMPLETE | DF_LIVE;
6410Sstevel@tonic-gate 	dumphdr->dump_crashtime = gethrestime_sec();
6420Sstevel@tonic-gate 	dumphdr->dump_npages = 0;
6430Sstevel@tonic-gate 	dumphdr->dump_nvtop = 0;
6440Sstevel@tonic-gate 	bzero(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
6450Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (panicstr) {
6480Sstevel@tonic-gate 		dumphdr->dump_flags &= ~DF_LIVE;
6495331Samw 		(void) VOP_DUMPCTL(dumpvp, DUMP_FREE, NULL, NULL);
6505331Samw 		(void) VOP_DUMPCTL(dumpvp, DUMP_ALLOC, NULL, NULL);
6510Sstevel@tonic-gate 		(void) vsnprintf(dumphdr->dump_panicstring, DUMP_PANICSIZE,
6520Sstevel@tonic-gate 		    panicstr, panicargs);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL)
6560Sstevel@tonic-gate 		content = "all";
6570Sstevel@tonic-gate 	else if (dump_conflags & DUMP_CURPROC)
6580Sstevel@tonic-gate 		content = "kernel + curproc";
6590Sstevel@tonic-gate 	else
6600Sstevel@tonic-gate 		content = "kernel";
6610Sstevel@tonic-gate 	uprintf("dumping to %s, offset %lld, content: %s\n", dumppath,
6620Sstevel@tonic-gate 	    dumphdr->dump_start, content);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/*
6650Sstevel@tonic-gate 	 * Leave room for the message and ereport save areas and terminal dump
6660Sstevel@tonic-gate 	 * header.
6670Sstevel@tonic-gate 	 */
6680Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_LOGSIZE - DUMP_OFFSET - DUMP_ERPTSIZE;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/*
6710Sstevel@tonic-gate 	 * Write out the symbol table.  It's no longer compressed,
6720Sstevel@tonic-gate 	 * so its 'size' and 'csize' are equal.
6730Sstevel@tonic-gate 	 */
6740Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_ksyms = dumphdr->dump_start + PAGESIZE;
6750Sstevel@tonic-gate 	dumphdr->dump_ksyms_size = dumphdr->dump_ksyms_csize =
6760Sstevel@tonic-gate 	    ksyms_snapshot(dumpvp_ksyms_write, NULL, LONG_MAX);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * Write out the translation map.
6800Sstevel@tonic-gate 	 */
6810Sstevel@tonic-gate 	dumphdr->dump_map = dumpvp_flush();
6820Sstevel@tonic-gate 	dump_as(&kas);
6833446Smrj 	dumphdr->dump_nvtop += dump_plat_addr();
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/*
6860Sstevel@tonic-gate 	 * call into hat, which may have unmapped pages that also need to
6870Sstevel@tonic-gate 	 * be in the dump
6880Sstevel@tonic-gate 	 */
6890Sstevel@tonic-gate 	hat_dump();
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL) {
6920Sstevel@tonic-gate 		mutex_enter(&pidlock);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		for (npids = 0, p = practive; p != NULL; p = p->p_next)
6950Sstevel@tonic-gate 			dump_pids[npids++] = p->p_pid;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		mutex_exit(&pidlock);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		for (pidx = 0; pidx < npids; pidx++)
7000Sstevel@tonic-gate 			(void) dump_process(dump_pids[pidx]);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7030Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
7040Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
7050Sstevel@tonic-gate 		}
7060Sstevel@tonic-gate 		dumphdr->dump_npages = dump_bitmapsize;
7070Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_ALL;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	} else if (dump_conflags & DUMP_CURPROC) {
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * Determine which pid is to be dumped.  If we're panicking, we
7120Sstevel@tonic-gate 		 * dump the process associated with panic_thread (if any).  If
7130Sstevel@tonic-gate 		 * this is a live dump, we dump the process associated with
7140Sstevel@tonic-gate 		 * curthread.
7150Sstevel@tonic-gate 		 */
7160Sstevel@tonic-gate 		npids = 0;
7170Sstevel@tonic-gate 		if (panicstr) {
7180Sstevel@tonic-gate 			if (panic_thread != NULL &&
7190Sstevel@tonic-gate 			    panic_thread->t_procp != NULL &&
7200Sstevel@tonic-gate 			    panic_thread->t_procp != &p0) {
7210Sstevel@tonic-gate 				dump_pids[npids++] =
7220Sstevel@tonic-gate 				    panic_thread->t_procp->p_pid;
7230Sstevel@tonic-gate 			}
7240Sstevel@tonic-gate 		} else {
7250Sstevel@tonic-gate 			dump_pids[npids++] = curthread->t_procp->p_pid;
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		if (npids && dump_process(dump_pids[0]) == 0)
7290Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_CURPROC;
7300Sstevel@tonic-gate 		else
7310Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_KERNEL;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	} else {
7340Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_KERNEL;
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	dumphdr->dump_hashmask = (1 << highbit(dumphdr->dump_nvtop - 1)) - 1;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	/*
7400Sstevel@tonic-gate 	 * Write out the pfn table.
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	dumphdr->dump_pfn = dumpvp_flush();
7430Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7440Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7450Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7460Sstevel@tonic-gate 			continue;
7470Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7480Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7490Sstevel@tonic-gate 		dumpvp_write(&pfn, sizeof (pfn_t));
7500Sstevel@tonic-gate 	}
7513446Smrj 	dump_plat_pfn();
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	/*
7540Sstevel@tonic-gate 	 * Write out all the pages.
7550Sstevel@tonic-gate 	 */
7560Sstevel@tonic-gate 	dumphdr->dump_data = dumpvp_flush();
7570Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7580Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7590Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7600Sstevel@tonic-gate 			continue;
7610Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7620Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		/*
7650Sstevel@tonic-gate 		 * Map in page frame 'pfn', scan it for UE's while copying
7660Sstevel@tonic-gate 		 * the data to dump_uebuf, unmap it, compress dump_uebuf into
7670Sstevel@tonic-gate 		 * dump_cbuf, and write out dump_cbuf.  The UE check ensures
7680Sstevel@tonic-gate 		 * that we don't lose the whole dump because of a latent UE.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 		hat_devload(kas.a_hat, dump_cmap, PAGESIZE, pfn, PROT_READ,
7710Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST);
7720Sstevel@tonic-gate 		dump_pagecopy(dump_cmap, dump_uebuf);
7730Sstevel@tonic-gate 		hat_unload(kas.a_hat, dump_cmap, PAGESIZE, HAT_UNLOAD);
7740Sstevel@tonic-gate 		csize = (uint32_t)compress(dump_uebuf, dump_cbuf, PAGESIZE);
7750Sstevel@tonic-gate 		dumpvp_write(&csize, sizeof (uint32_t));
7760Sstevel@tonic-gate 		dumpvp_write(dump_cbuf, csize);
7770Sstevel@tonic-gate 		if (dump_ioerr) {
7780Sstevel@tonic-gate 			dumphdr->dump_flags &= ~DF_COMPLETE;
7790Sstevel@tonic-gate 			dumphdr->dump_npages = npages;
7800Sstevel@tonic-gate 			break;
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 		total_csize += csize;
7830Sstevel@tonic-gate 		if (++npages * 100LL / dumphdr->dump_npages > percent_done) {
7840Sstevel@tonic-gate 			uprintf("^\r%3d%% done", ++percent_done);
7850Sstevel@tonic-gate 			if (!panicstr)
7860Sstevel@tonic-gate 				delay(1);	/* let the output be sent */
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 	}
7893446Smrj 	dumphdr->dump_npages += dump_plat_data(dump_cbuf);
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	(void) dumpvp_flush();
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	/*
7940Sstevel@tonic-gate 	 * Write out the initial and terminal dump headers.
7950Sstevel@tonic-gate 	 */
7960Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_start;
7970Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
7980Sstevel@tonic-gate 	(void) dumpvp_flush();
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size;
8010Sstevel@tonic-gate 	dumpvp_off = dumpvp_limit - DUMP_OFFSET;
8020Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
8030Sstevel@tonic-gate 	(void) dumpvp_flush();
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	compress_ratio = (int)(100LL * npages / (btopr(total_csize + 1)));
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	uprintf("\r%3d%% done: %d pages dumped, compression ratio %d.%02d, ",
8080Sstevel@tonic-gate 	    percent_done, npages, compress_ratio / 100, compress_ratio % 100);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (dump_ioerr == 0) {
8110Sstevel@tonic-gate 		uprintf("dump succeeded\n");
8120Sstevel@tonic-gate 	} else {
8130Sstevel@tonic-gate 		uprintf("dump failed: error %d\n", dump_ioerr);
8140Sstevel@tonic-gate 		if (panicstr && dumpfaildebug)
8150Sstevel@tonic-gate 			debug_enter("dump failed");
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	/*
8190Sstevel@tonic-gate 	 * Write out all undelivered messages.  This has to be the *last*
8200Sstevel@tonic-gate 	 * thing we do because the dump process itself emits messages.
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	if (panicstr) {
8230Sstevel@tonic-gate 		dump_ereports();
8240Sstevel@tonic-gate 		dump_messages();
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	delay(2 * hz);	/* let people see the 'done' message */
8280Sstevel@tonic-gate 	dump_timeleft = 0;
8290Sstevel@tonic-gate 	dump_ioerr = 0;
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate  * This function is called whenever the memory size, as represented
8340Sstevel@tonic-gate  * by the phys_install list, changes.
8350Sstevel@tonic-gate  */
8360Sstevel@tonic-gate void
8370Sstevel@tonic-gate dump_resize()
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	mutex_enter(&dump_lock);
8400Sstevel@tonic-gate 	dumphdr_init();
8410Sstevel@tonic-gate 	dumpbuf_resize();
8420Sstevel@tonic-gate 	mutex_exit(&dump_lock);
8430Sstevel@tonic-gate }
8446423Sgw25295 
8456423Sgw25295 /*
8466423Sgw25295  * This function allows for dynamic resizing of a dump area. It assumes that
8476423Sgw25295  * the underlying device has update its appropriate size(9P).
8486423Sgw25295  */
8496423Sgw25295 int
8506423Sgw25295 dumpvp_resize()
8516423Sgw25295 {
8526423Sgw25295 	int error;
8536423Sgw25295 	vattr_t vattr;
8546423Sgw25295 
8556423Sgw25295 	mutex_enter(&dump_lock);
8566423Sgw25295 	vattr.va_mask = AT_SIZE;
8576423Sgw25295 	if ((error = VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL)) != 0) {
8586423Sgw25295 		mutex_exit(&dump_lock);
8596423Sgw25295 		return (error);
8606423Sgw25295 	}
8616423Sgw25295 
8626423Sgw25295 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE) {
8636423Sgw25295 		mutex_exit(&dump_lock);
8646423Sgw25295 		return (ENOSPC);
8656423Sgw25295 	}
8666423Sgw25295 
8676423Sgw25295 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
8686423Sgw25295 	mutex_exit(&dump_lock);
8696423Sgw25295 	return (0);
8706423Sgw25295 }
871