xref: /illumos-gate/usr/src/uts/common/os/dumpsubr.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e7cbe64fSgw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/vm.h>
327c478bd9Sstevel@tonic-gate #include <sys/proc.h>
337c478bd9Sstevel@tonic-gate #include <sys/file.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/mem.h>
377c478bd9Sstevel@tonic-gate #include <sys/mman.h>
387c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
417c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
427c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h>
437c478bd9Sstevel@tonic-gate #include <sys/ksyms.h>
447c478bd9Sstevel@tonic-gate #include <sys/compress.h>
457c478bd9Sstevel@tonic-gate #include <sys/stream.h>
467c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
477c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
487c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
497c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
517c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
527c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/log.h>
547c478bd9Sstevel@tonic-gate #include <sys/var.h>
557c478bd9Sstevel@tonic-gate #include <sys/debug.h>
567c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
577c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
587c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
597c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
607c478bd9Sstevel@tonic-gate #include <sys/panic.h>
617c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
627c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
637c478bd9Sstevel@tonic-gate #include <sys/errorq.h>
647c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
65*e7cbe64fSgw #include <sys/fs/zfs.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <vm/hat.h>
687c478bd9Sstevel@tonic-gate #include <vm/as.h>
697c478bd9Sstevel@tonic-gate #include <vm/page.h>
707c478bd9Sstevel@tonic-gate #include <vm/seg.h>
717c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate kmutex_t	dump_lock;	/* lock for dump configuration */
747c478bd9Sstevel@tonic-gate dumphdr_t	*dumphdr;	/* dump header */
757c478bd9Sstevel@tonic-gate int		dump_conflags = DUMP_KERNEL; /* dump configuration flags */
767c478bd9Sstevel@tonic-gate vnode_t		*dumpvp;	/* dump device vnode pointer */
777c478bd9Sstevel@tonic-gate u_offset_t	dumpvp_size;	/* size of dump device, in bytes */
787c478bd9Sstevel@tonic-gate static u_offset_t dumpvp_limit;	/* maximum write offset */
797c478bd9Sstevel@tonic-gate char		*dumppath;	/* pathname of dump device */
807c478bd9Sstevel@tonic-gate int		dump_timeout = 120; /* timeout for dumping page during panic */
817c478bd9Sstevel@tonic-gate int		dump_timeleft;	/* portion of dump_timeout remaining */
82843e1988Sjohnlev int		dump_ioerr;	/* dump i/o error */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #ifdef DEBUG
857c478bd9Sstevel@tonic-gate int		dumpfaildebug = 1;	/* enter debugger if dump fails */
867c478bd9Sstevel@tonic-gate #else
877c478bd9Sstevel@tonic-gate int		dumpfaildebug = 0;
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static ulong_t	*dump_bitmap;	/* bitmap for marking pages to dump */
917c478bd9Sstevel@tonic-gate static pgcnt_t	dump_bitmapsize; /* size of bitmap */
927c478bd9Sstevel@tonic-gate static pid_t	*dump_pids;	/* list of process IDs at dump time */
937c478bd9Sstevel@tonic-gate static offset_t	dumpvp_off;	/* current dump device offset */
947c478bd9Sstevel@tonic-gate static char	*dump_cmap;	/* VA for dump compression mapping */
957c478bd9Sstevel@tonic-gate static char	*dumpbuf_cur, *dumpbuf_start, *dumpbuf_end;
967c478bd9Sstevel@tonic-gate static char	*dump_cbuf;	/* compression buffer */
977c478bd9Sstevel@tonic-gate static char	*dump_uebuf;	/* memory error detection buffer */
987c478bd9Sstevel@tonic-gate static size_t	dumpbuf_size;	/* size of dumpbuf in bytes */
997c478bd9Sstevel@tonic-gate static size_t	dumpbuf_limit = 1UL << 23;	/* 8MB */
1007c478bd9Sstevel@tonic-gate static size_t	dump_iosize;	/* device's best transfer size, if any */
1017c478bd9Sstevel@tonic-gate static uint64_t	dumpbuf_thresh = 1ULL << 30;	/* 1GB */
1027c478bd9Sstevel@tonic-gate static ulong_t	dumpbuf_mult = 8;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * The dump i/o buffer must be at least one page, at most xfer_size bytes, and
1067c478bd9Sstevel@tonic-gate  * should scale with physmem in between.  The transfer size passed in will
1077c478bd9Sstevel@tonic-gate  * either represent a global default (maxphys) or the best size for the device.
1087c478bd9Sstevel@tonic-gate  * Once the physical memory size exceeds dumpbuf_thresh (1GB by default), we
1097c478bd9Sstevel@tonic-gate  * increase the percentage of physical memory that dumpbuf can consume by a
1107c478bd9Sstevel@tonic-gate  * factor of dumpbuf_mult (8 by default) to improve large memory performance.
1117c478bd9Sstevel@tonic-gate  * The size of the dumpbuf i/o buffer is limited by dumpbuf_limit (8MB by
1127c478bd9Sstevel@tonic-gate  * default) because the dump performance saturates beyond a certain size.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate static size_t
1157c478bd9Sstevel@tonic-gate dumpbuf_iosize(size_t xfer_size)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	pgcnt_t scale = physmem;
1187c478bd9Sstevel@tonic-gate 	size_t iosize;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (scale >= dumpbuf_thresh / PAGESIZE) {
1217c478bd9Sstevel@tonic-gate 		scale *= dumpbuf_mult; /* increase scaling factor */
1227c478bd9Sstevel@tonic-gate 		iosize = MIN(xfer_size, scale) & PAGEMASK;
1237c478bd9Sstevel@tonic-gate 		if (dumpbuf_limit && iosize > dumpbuf_limit)
1247c478bd9Sstevel@tonic-gate 			iosize = MAX(PAGESIZE, dumpbuf_limit & PAGEMASK);
1257c478bd9Sstevel@tonic-gate 	} else
1267c478bd9Sstevel@tonic-gate 		iosize = MAX(PAGESIZE, MIN(xfer_size, scale) & PAGEMASK);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	return (iosize);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static void
1327c478bd9Sstevel@tonic-gate dumpbuf_resize(void)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	char *old_buf = dumpbuf_start;
1357c478bd9Sstevel@tonic-gate 	size_t old_size = dumpbuf_size;
1367c478bd9Sstevel@tonic-gate 	char *new_buf;
1377c478bd9Sstevel@tonic-gate 	size_t new_size;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if ((new_size = dumpbuf_iosize(MAX(dump_iosize, maxphys))) <= old_size)
1427c478bd9Sstevel@tonic-gate 		return; /* no need to reallocate buffer */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	new_buf = kmem_alloc(new_size, KM_SLEEP);
1457c478bd9Sstevel@tonic-gate 	dumpbuf_size = new_size;
1467c478bd9Sstevel@tonic-gate 	dumpbuf_start = new_buf;
1477c478bd9Sstevel@tonic-gate 	dumpbuf_end = new_buf + new_size;
1487c478bd9Sstevel@tonic-gate 	kmem_free(old_buf, old_size);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static void
1527c478bd9Sstevel@tonic-gate dumphdr_init(void)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	pgcnt_t npages = 0;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (dumphdr == NULL) {
1597c478bd9Sstevel@tonic-gate 		dumphdr = kmem_zalloc(sizeof (dumphdr_t), KM_SLEEP);
1607c478bd9Sstevel@tonic-gate 		dumphdr->dump_magic = DUMP_MAGIC;
1617c478bd9Sstevel@tonic-gate 		dumphdr->dump_version = DUMP_VERSION;
1627c478bd9Sstevel@tonic-gate 		dumphdr->dump_wordsize = DUMP_WORDSIZE;
1637c478bd9Sstevel@tonic-gate 		dumphdr->dump_pageshift = PAGESHIFT;
1647c478bd9Sstevel@tonic-gate 		dumphdr->dump_pagesize = PAGESIZE;
1657c478bd9Sstevel@tonic-gate 		dumphdr->dump_utsname = utsname;
1667c478bd9Sstevel@tonic-gate 		(void) strcpy(dumphdr->dump_platform, platform);
1677c478bd9Sstevel@tonic-gate 		dump_cmap = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
1687c478bd9Sstevel@tonic-gate 		dumpbuf_size = dumpbuf_iosize(maxphys);
1697c478bd9Sstevel@tonic-gate 		dumpbuf_start = kmem_alloc(dumpbuf_size, KM_SLEEP);
1707c478bd9Sstevel@tonic-gate 		dumpbuf_end = dumpbuf_start + dumpbuf_size;
1717c478bd9Sstevel@tonic-gate 		dump_cbuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* compress buf */
1727c478bd9Sstevel@tonic-gate 		dump_uebuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* UE buf */
1737c478bd9Sstevel@tonic-gate 		dump_pids = kmem_alloc(v.v_proc * sizeof (pid_t), KM_SLEEP);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
176843e1988Sjohnlev 	npages = num_phys_pages();
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (dump_bitmapsize != npages) {
1797c478bd9Sstevel@tonic-gate 		void *map = kmem_alloc(BT_SIZEOFMAP(npages), KM_SLEEP);
1807c478bd9Sstevel@tonic-gate 		kmem_free(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
1817c478bd9Sstevel@tonic-gate 		dump_bitmap = map;
1827c478bd9Sstevel@tonic-gate 		dump_bitmapsize = npages;
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Establish a new dump device.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate int
1907c478bd9Sstevel@tonic-gate dumpinit(vnode_t *vp, char *name, int justchecking)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	vnode_t *cvp;
1937c478bd9Sstevel@tonic-gate 	vattr_t vattr;
1947c478bd9Sstevel@tonic-gate 	vnode_t *cdev_vp;
1957c478bd9Sstevel@tonic-gate 	int error = 0;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	dumphdr_init();
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	cvp = common_specvp(vp);
2027c478bd9Sstevel@tonic-gate 	if (cvp == dumpvp)
2037c478bd9Sstevel@tonic-gate 		return (0);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * Determine whether this is a plausible dump device.  We want either:
2077c478bd9Sstevel@tonic-gate 	 * (1) a real device that's not mounted and has a cb_dump routine, or
2087c478bd9Sstevel@tonic-gate 	 * (2) a swapfile on some filesystem that has a vop_dump routine.
2097c478bd9Sstevel@tonic-gate 	 */
210da6c28aaSamw 	if ((error = VOP_OPEN(&cvp, FREAD | FWRITE, kcred, NULL)) != 0)
2117c478bd9Sstevel@tonic-gate 		return (error);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE | AT_TYPE | AT_RDEV;
214da6c28aaSamw 	if ((error = VOP_GETATTR(cvp, &vattr, 0, kcred, NULL)) == 0) {
2157c478bd9Sstevel@tonic-gate 		if (vattr.va_type == VBLK || vattr.va_type == VCHR) {
2167c478bd9Sstevel@tonic-gate 			if (devopsp[getmajor(vattr.va_rdev)]->
2177c478bd9Sstevel@tonic-gate 			    devo_cb_ops->cb_dump == nodev)
2187c478bd9Sstevel@tonic-gate 				error = ENOTSUP;
2197c478bd9Sstevel@tonic-gate 			else if (vfs_devismounted(vattr.va_rdev))
2207c478bd9Sstevel@tonic-gate 				error = EBUSY;
2217c478bd9Sstevel@tonic-gate 		} else {
2227c478bd9Sstevel@tonic-gate 			if (vn_matchopval(cvp, VOPNAME_DUMP, fs_nosys) ||
2237c478bd9Sstevel@tonic-gate 			    !IS_SWAPVP(cvp))
2247c478bd9Sstevel@tonic-gate 				error = ENOTSUP;
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE)
2297c478bd9Sstevel@tonic-gate 		error = ENOSPC;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (error || justchecking) {
232da6c28aaSamw 		(void) VOP_CLOSE(cvp, FREAD | FWRITE, 1, (offset_t)0,
233da6c28aaSamw 		    kcred, NULL);
2347c478bd9Sstevel@tonic-gate 		return (error);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	VN_HOLD(cvp);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (dumpvp != NULL)
2407c478bd9Sstevel@tonic-gate 		dumpfini();	/* unconfigure the old dump device */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	dumpvp = cvp;
2437c478bd9Sstevel@tonic-gate 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
2447c478bd9Sstevel@tonic-gate 	dumppath = kmem_alloc(strlen(name) + 1, KM_SLEEP);
2457c478bd9Sstevel@tonic-gate 	(void) strcpy(dumppath, name);
2467c478bd9Sstevel@tonic-gate 	dump_iosize = 0;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * If the dump device is a block device, attempt to open up the
2507c478bd9Sstevel@tonic-gate 	 * corresponding character device and determine its maximum transfer
2517c478bd9Sstevel@tonic-gate 	 * size.  We use this information to potentially resize dumpbuf to a
2527c478bd9Sstevel@tonic-gate 	 * larger and more optimal size for performing i/o to the dump device.
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	if (cvp->v_type == VBLK &&
2557c478bd9Sstevel@tonic-gate 	    (cdev_vp = makespecvp(VTOS(cvp)->s_dev, VCHR)) != NULL) {
256da6c28aaSamw 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
2577c478bd9Sstevel@tonic-gate 			size_t blk_size;
2587c478bd9Sstevel@tonic-gate 			struct dk_cinfo dki;
2597c478bd9Sstevel@tonic-gate 			struct vtoc vtoc;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCGVTOC, (intptr_t)&vtoc,
262da6c28aaSamw 			    FKIOCTL, kcred, NULL, NULL) == 0 &&
263da6c28aaSamw 			    vtoc.v_sectorsz != 0)
2647c478bd9Sstevel@tonic-gate 				blk_size = vtoc.v_sectorsz;
2657c478bd9Sstevel@tonic-gate 			else
2667c478bd9Sstevel@tonic-gate 				blk_size = DEV_BSIZE;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCINFO, (intptr_t)&dki,
269da6c28aaSamw 			    FKIOCTL, kcred, NULL, NULL) == 0) {
2707c478bd9Sstevel@tonic-gate 				dump_iosize = dki.dki_maxtransfer * blk_size;
2717c478bd9Sstevel@tonic-gate 				dumpbuf_resize();
2727c478bd9Sstevel@tonic-gate 			}
273*e7cbe64fSgw 			/*
274*e7cbe64fSgw 			 * If we are working with a zvol then call into
275*e7cbe64fSgw 			 * it to dumpify itself.
276*e7cbe64fSgw 			 */
277*e7cbe64fSgw 			if (strcmp(dki.dki_dname, ZVOL_DRIVER) == 0) {
278*e7cbe64fSgw 				if ((error = VOP_IOCTL(cdev_vp,
279*e7cbe64fSgw 				    DKIOCDUMPINIT, NULL, FKIOCTL, kcred,
280*e7cbe64fSgw 				    NULL, NULL)) != 0) {
281*e7cbe64fSgw 					dumpfini();
282*e7cbe64fSgw 				}
283*e7cbe64fSgw 			}
2847c478bd9Sstevel@tonic-gate 
285da6c28aaSamw 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
286da6c28aaSamw 			    kcred, NULL);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		VN_RELE(cdev_vp);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?dump on %s size %llu MB\n", name, dumpvp_size >> 20);
2937c478bd9Sstevel@tonic-gate 
294*e7cbe64fSgw 	return (error);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate void
2987c478bd9Sstevel@tonic-gate dumpfini(void)
2997c478bd9Sstevel@tonic-gate {
300*e7cbe64fSgw 	vattr_t vattr;
301*e7cbe64fSgw 	boolean_t is_zfs = B_FALSE;
302*e7cbe64fSgw 	vnode_t *cdev_vp;
3037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	kmem_free(dumppath, strlen(dumppath) + 1);
3067c478bd9Sstevel@tonic-gate 
307*e7cbe64fSgw 	/*
308*e7cbe64fSgw 	 * Determine if we are using zvols for our dump device
309*e7cbe64fSgw 	 */
310*e7cbe64fSgw 	vattr.va_mask = AT_RDEV;
311*e7cbe64fSgw 	if (VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL) == 0) {
312*e7cbe64fSgw 		is_zfs = (getmajor(vattr.va_rdev) ==
313*e7cbe64fSgw 		    ddi_name_to_major(ZFS_DRIVER)) ? B_TRUE : B_FALSE;
314*e7cbe64fSgw 	}
315*e7cbe64fSgw 
316*e7cbe64fSgw 	/*
317*e7cbe64fSgw 	 * If we have a zvol dump device then we call into zfs so
318*e7cbe64fSgw 	 * that it may have a chance to cleanup.
319*e7cbe64fSgw 	 */
320*e7cbe64fSgw 	if (is_zfs &&
321*e7cbe64fSgw 	    (cdev_vp = makespecvp(VTOS(dumpvp)->s_dev, VCHR)) != NULL) {
322*e7cbe64fSgw 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
323*e7cbe64fSgw 			(void) VOP_IOCTL(cdev_vp, DKIOCDUMPFINI, NULL, FKIOCTL,
324*e7cbe64fSgw 			    kcred, NULL, NULL);
325*e7cbe64fSgw 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
326*e7cbe64fSgw 			    kcred, NULL);
327*e7cbe64fSgw 		}
328*e7cbe64fSgw 		VN_RELE(cdev_vp);
329*e7cbe64fSgw 	}
330*e7cbe64fSgw 
331da6c28aaSamw 	(void) VOP_CLOSE(dumpvp, FREAD | FWRITE, 1, (offset_t)0, kcred, NULL);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	VN_RELE(dumpvp);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	dumpvp = NULL;
3367c478bd9Sstevel@tonic-gate 	dumpvp_size = 0;
3377c478bd9Sstevel@tonic-gate 	dumppath = NULL;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate static pfn_t
3417c478bd9Sstevel@tonic-gate dump_bitnum_to_pfn(pgcnt_t bitnum)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	struct memlist *mp;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3467c478bd9Sstevel@tonic-gate 		if (bitnum < (mp->size >> PAGESHIFT))
3477c478bd9Sstevel@tonic-gate 			return ((mp->address >> PAGESHIFT) + bitnum);
3487c478bd9Sstevel@tonic-gate 		bitnum -= mp->size >> PAGESHIFT;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	return (PFN_INVALID);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static pgcnt_t
3547c478bd9Sstevel@tonic-gate dump_pfn_to_bitnum(pfn_t pfn)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	struct memlist *mp;
3577c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum = 0;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3607c478bd9Sstevel@tonic-gate 		if (pfn >= (mp->address >> PAGESHIFT) &&
3617c478bd9Sstevel@tonic-gate 		    pfn < ((mp->address + mp->size) >> PAGESHIFT))
3627c478bd9Sstevel@tonic-gate 			return (bitnum + pfn - (mp->address >> PAGESHIFT));
3637c478bd9Sstevel@tonic-gate 		bitnum += mp->size >> PAGESHIFT;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	return ((pgcnt_t)-1);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate static offset_t
3697c478bd9Sstevel@tonic-gate dumpvp_flush(void)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	size_t size = P2ROUNDUP(dumpbuf_cur - dumpbuf_start, PAGESIZE);
3727c478bd9Sstevel@tonic-gate 	int err;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (dumpvp_off + size > dumpvp_limit) {
3757c478bd9Sstevel@tonic-gate 		dump_ioerr = ENOSPC;
3767c478bd9Sstevel@tonic-gate 	} else if (size != 0) {
3777c478bd9Sstevel@tonic-gate 		if (panicstr)
3787c478bd9Sstevel@tonic-gate 			err = VOP_DUMP(dumpvp, dumpbuf_start,
379da6c28aaSamw 			    lbtodb(dumpvp_off), btod(size), NULL);
3807c478bd9Sstevel@tonic-gate 		else
3817c478bd9Sstevel@tonic-gate 			err = vn_rdwr(UIO_WRITE, dumpvp, dumpbuf_start, size,
3827c478bd9Sstevel@tonic-gate 			    dumpvp_off, UIO_SYSSPACE, 0, dumpvp_limit,
3837c478bd9Sstevel@tonic-gate 			    kcred, 0);
3847c478bd9Sstevel@tonic-gate 		if (err && dump_ioerr == 0)
3857c478bd9Sstevel@tonic-gate 			dump_ioerr = err;
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	dumpvp_off += size;
3887c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
3897c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
3907c478bd9Sstevel@tonic-gate 	return (dumpvp_off);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate void
3947c478bd9Sstevel@tonic-gate dumpvp_write(const void *va, size_t size)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	while (size != 0) {
3977c478bd9Sstevel@tonic-gate 		size_t len = MIN(size, dumpbuf_end - dumpbuf_cur);
3987c478bd9Sstevel@tonic-gate 		if (len == 0) {
3997c478bd9Sstevel@tonic-gate 			(void) dumpvp_flush();
4007c478bd9Sstevel@tonic-gate 		} else {
4017c478bd9Sstevel@tonic-gate 			bcopy(va, dumpbuf_cur, len);
4027c478bd9Sstevel@tonic-gate 			va = (char *)va + len;
4037c478bd9Sstevel@tonic-gate 			dumpbuf_cur += len;
4047c478bd9Sstevel@tonic-gate 			size -= len;
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4107c478bd9Sstevel@tonic-gate static void
4117c478bd9Sstevel@tonic-gate dumpvp_ksyms_write(const void *src, void *dst, size_t size)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	dumpvp_write(src, size);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * Mark 'pfn' in the bitmap and dump its translation table entry.
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate void
4207c478bd9Sstevel@tonic-gate dump_addpage(struct as *as, void *va, pfn_t pfn)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
4237c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
4267c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
4277c478bd9Sstevel@tonic-gate 			dumphdr->dump_npages++;
4287c478bd9Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 		dumphdr->dump_nvtop++;
4317c478bd9Sstevel@tonic-gate 		mem_vtop.m_as = as;
4327c478bd9Sstevel@tonic-gate 		mem_vtop.m_va = va;
4337c478bd9Sstevel@tonic-gate 		mem_vtop.m_pfn = pfn;
4347c478bd9Sstevel@tonic-gate 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * Mark 'pfn' in the bitmap
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate void
4437c478bd9Sstevel@tonic-gate dump_page(pfn_t pfn)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
4487c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
4497c478bd9Sstevel@tonic-gate 			dumphdr->dump_npages++;
4507c478bd9Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate  * Dump the <as, va, pfn> information for a given address space.
4587c478bd9Sstevel@tonic-gate  * SEGOP_DUMP() will call dump_addpage() for each page in the segment.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate static void
4617c478bd9Sstevel@tonic-gate dump_as(struct as *as)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	struct seg *seg;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
4667c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
4677c478bd9Sstevel@tonic-gate 		if (seg->s_as != as)
4687c478bd9Sstevel@tonic-gate 			break;
4697c478bd9Sstevel@tonic-gate 		if (seg->s_ops == NULL)
4707c478bd9Sstevel@tonic-gate 			continue;
4717c478bd9Sstevel@tonic-gate 		SEGOP_DUMP(seg);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (seg != NULL)
4767c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid segment %p in address space %p",
4777c478bd9Sstevel@tonic-gate 		    (void *)seg, (void *)as);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static int
4817c478bd9Sstevel@tonic-gate dump_process(pid_t pid)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	proc_t *p = sprlock(pid);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (p == NULL)
4867c478bd9Sstevel@tonic-gate 		return (-1);
4877c478bd9Sstevel@tonic-gate 	if (p->p_as != &kas) {
4887c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4897c478bd9Sstevel@tonic-gate 		dump_as(p->p_as);
4907c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	sprunlock(p);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	return (0);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate void
4997c478bd9Sstevel@tonic-gate dump_ereports(void)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	u_offset_t dumpvp_start;
5027c478bd9Sstevel@tonic-gate 	erpt_dump_t ed;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL)
5057c478bd9Sstevel@tonic-gate 		return;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5087c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE);
5097c478bd9Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_ERPTSIZE;
5107c478bd9Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	fm_ereport_dump();
5137c478bd9Sstevel@tonic-gate 	if (panicstr)
5147c478bd9Sstevel@tonic-gate 		errorq_dump();
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	bzero(&ed, sizeof (ed)); /* indicate end of ereports */
5177c478bd9Sstevel@tonic-gate 	dumpvp_write(&ed, sizeof (ed));
5187c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (!panicstr) {
5217c478bd9Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
5227c478bd9Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
523da6c28aaSamw 		    B_INVAL | B_FORCE, kcred, NULL);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate void
5287c478bd9Sstevel@tonic-gate dump_messages(void)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	log_dump_t ld;
5317c478bd9Sstevel@tonic-gate 	mblk_t *mctl, *mdata;
5327c478bd9Sstevel@tonic-gate 	queue_t *q, *qlast;
5337c478bd9Sstevel@tonic-gate 	u_offset_t dumpvp_start;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL || log_consq == NULL)
5367c478bd9Sstevel@tonic-gate 		return;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5397c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_OFFSET;
5407c478bd9Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_LOGSIZE;
5417c478bd9Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	qlast = NULL;
5447c478bd9Sstevel@tonic-gate 	do {
5457c478bd9Sstevel@tonic-gate 		for (q = log_consq; q->q_next != qlast; q = q->q_next)
5467c478bd9Sstevel@tonic-gate 			continue;
5477c478bd9Sstevel@tonic-gate 		for (mctl = q->q_first; mctl != NULL; mctl = mctl->b_next) {
5487c478bd9Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
5497c478bd9Sstevel@tonic-gate 			mdata = mctl->b_cont;
5507c478bd9Sstevel@tonic-gate 			ld.ld_magic = LOG_MAGIC;
5517c478bd9Sstevel@tonic-gate 			ld.ld_msgsize = MBLKL(mctl->b_cont);
5527c478bd9Sstevel@tonic-gate 			ld.ld_csum = checksum32(mctl->b_rptr, MBLKL(mctl));
5537c478bd9Sstevel@tonic-gate 			ld.ld_msum = checksum32(mdata->b_rptr, MBLKL(mdata));
5547c478bd9Sstevel@tonic-gate 			dumpvp_write(&ld, sizeof (ld));
5557c478bd9Sstevel@tonic-gate 			dumpvp_write(mctl->b_rptr, MBLKL(mctl));
5567c478bd9Sstevel@tonic-gate 			dumpvp_write(mdata->b_rptr, MBLKL(mdata));
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 	} while ((qlast = q) != log_consq);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	ld.ld_magic = 0;		/* indicate end of messages */
5617c478bd9Sstevel@tonic-gate 	dumpvp_write(&ld, sizeof (ld));
5627c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
5637c478bd9Sstevel@tonic-gate 	if (!panicstr) {
5647c478bd9Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
5657c478bd9Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
566da6c28aaSamw 		    B_INVAL | B_FORCE, kcred, NULL);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate static void
5717c478bd9Sstevel@tonic-gate dump_pagecopy(void *src, void *dst)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	long *wsrc = (long *)src;
5747c478bd9Sstevel@tonic-gate 	long *wdst = (long *)dst;
5757c478bd9Sstevel@tonic-gate 	const ulong_t ncopies = PAGESIZE / sizeof (long);
5767c478bd9Sstevel@tonic-gate 	volatile int w = 0;
5777c478bd9Sstevel@tonic-gate 	volatile int ueoff = -1;
5787c478bd9Sstevel@tonic-gate 	on_trap_data_t otd;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_EC)) {
5817c478bd9Sstevel@tonic-gate 		if (ueoff == -1) {
5827c478bd9Sstevel@tonic-gate 			uint64_t pa;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 			ueoff = w * sizeof (long);
5857c478bd9Sstevel@tonic-gate 			pa = ptob((uint64_t)hat_getpfnum(kas.a_hat, src))
5867c478bd9Sstevel@tonic-gate 			    + ueoff;
5877c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "memory error at PA 0x%08x.%08x",
5887c478bd9Sstevel@tonic-gate 			    (uint32_t)(pa >> 32), (uint32_t)pa);
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate #ifdef _LP64
5917c478bd9Sstevel@tonic-gate 		wdst[w++] = 0xbadecc00badecc;
5927c478bd9Sstevel@tonic-gate #else
5937c478bd9Sstevel@tonic-gate 		wdst[w++] = 0xbadecc;
5947c478bd9Sstevel@tonic-gate #endif
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	while (w < ncopies) {
5977c478bd9Sstevel@tonic-gate 		wdst[w] = wsrc[w];
5987c478bd9Sstevel@tonic-gate 		w++;
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 	no_trap();
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate  * Dump the system.
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate void
6077c478bd9Sstevel@tonic-gate dumpsys(void)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	pfn_t pfn;
6107c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum;
6117c478bd9Sstevel@tonic-gate 	int npages = 0;
6127c478bd9Sstevel@tonic-gate 	int percent_done = 0;
6137c478bd9Sstevel@tonic-gate 	uint32_t csize;
6147c478bd9Sstevel@tonic-gate 	u_offset_t total_csize = 0;
6157c478bd9Sstevel@tonic-gate 	int compress_ratio;
6167c478bd9Sstevel@tonic-gate 	proc_t *p;
6177c478bd9Sstevel@tonic-gate 	pid_t npids, pidx;
6187c478bd9Sstevel@tonic-gate 	char *content;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL) {
6217c478bd9Sstevel@tonic-gate 		uprintf("skipping system dump - no dump device configured\n");
6227c478bd9Sstevel@tonic-gate 		return;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/*
6277c478bd9Sstevel@tonic-gate 	 * Calculate the starting block for dump.  If we're dumping on a
6287c478bd9Sstevel@tonic-gate 	 * swap device, start 1/5 of the way in; otherwise, start at the
6297c478bd9Sstevel@tonic-gate 	 * beginning.  And never use the first page -- it may be a disk label.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	if (dumpvp->v_flag & VISSWAP)
6327c478bd9Sstevel@tonic-gate 		dumphdr->dump_start = P2ROUNDUP(dumpvp_size / 5, DUMP_OFFSET);
6337c478bd9Sstevel@tonic-gate 	else
6347c478bd9Sstevel@tonic-gate 		dumphdr->dump_start = DUMP_OFFSET;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	dumphdr->dump_flags = DF_VALID | DF_COMPLETE | DF_LIVE;
6377c478bd9Sstevel@tonic-gate 	dumphdr->dump_crashtime = gethrestime_sec();
6387c478bd9Sstevel@tonic-gate 	dumphdr->dump_npages = 0;
6397c478bd9Sstevel@tonic-gate 	dumphdr->dump_nvtop = 0;
6407c478bd9Sstevel@tonic-gate 	bzero(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
6417c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	if (panicstr) {
6447c478bd9Sstevel@tonic-gate 		dumphdr->dump_flags &= ~DF_LIVE;
645da6c28aaSamw 		(void) VOP_DUMPCTL(dumpvp, DUMP_FREE, NULL, NULL);
646da6c28aaSamw 		(void) VOP_DUMPCTL(dumpvp, DUMP_ALLOC, NULL, NULL);
6477c478bd9Sstevel@tonic-gate 		(void) vsnprintf(dumphdr->dump_panicstring, DUMP_PANICSIZE,
6487c478bd9Sstevel@tonic-gate 		    panicstr, panicargs);
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL)
6527c478bd9Sstevel@tonic-gate 		content = "all";
6537c478bd9Sstevel@tonic-gate 	else if (dump_conflags & DUMP_CURPROC)
6547c478bd9Sstevel@tonic-gate 		content = "kernel + curproc";
6557c478bd9Sstevel@tonic-gate 	else
6567c478bd9Sstevel@tonic-gate 		content = "kernel";
6577c478bd9Sstevel@tonic-gate 	uprintf("dumping to %s, offset %lld, content: %s\n", dumppath,
6587c478bd9Sstevel@tonic-gate 	    dumphdr->dump_start, content);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Leave room for the message and ereport save areas and terminal dump
6627c478bd9Sstevel@tonic-gate 	 * header.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_LOGSIZE - DUMP_OFFSET - DUMP_ERPTSIZE;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * Write out the symbol table.  It's no longer compressed,
6687c478bd9Sstevel@tonic-gate 	 * so its 'size' and 'csize' are equal.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_ksyms = dumphdr->dump_start + PAGESIZE;
6717c478bd9Sstevel@tonic-gate 	dumphdr->dump_ksyms_size = dumphdr->dump_ksyms_csize =
6727c478bd9Sstevel@tonic-gate 	    ksyms_snapshot(dumpvp_ksyms_write, NULL, LONG_MAX);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * Write out the translation map.
6767c478bd9Sstevel@tonic-gate 	 */
6777c478bd9Sstevel@tonic-gate 	dumphdr->dump_map = dumpvp_flush();
6787c478bd9Sstevel@tonic-gate 	dump_as(&kas);
679ae115bc7Smrj 	dumphdr->dump_nvtop += dump_plat_addr();
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/*
6827c478bd9Sstevel@tonic-gate 	 * call into hat, which may have unmapped pages that also need to
6837c478bd9Sstevel@tonic-gate 	 * be in the dump
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	hat_dump();
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL) {
6887c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		for (npids = 0, p = practive; p != NULL; p = p->p_next)
6917c478bd9Sstevel@tonic-gate 			dump_pids[npids++] = p->p_pid;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		for (pidx = 0; pidx < npids; pidx++)
6967c478bd9Sstevel@tonic-gate 			(void) dump_process(dump_pids[pidx]);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
6997c478bd9Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
7007c478bd9Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		dumphdr->dump_npages = dump_bitmapsize;
7037c478bd9Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_ALL;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	} else if (dump_conflags & DUMP_CURPROC) {
7067c478bd9Sstevel@tonic-gate 		/*
7077c478bd9Sstevel@tonic-gate 		 * Determine which pid is to be dumped.  If we're panicking, we
7087c478bd9Sstevel@tonic-gate 		 * dump the process associated with panic_thread (if any).  If
7097c478bd9Sstevel@tonic-gate 		 * this is a live dump, we dump the process associated with
7107c478bd9Sstevel@tonic-gate 		 * curthread.
7117c478bd9Sstevel@tonic-gate 		 */
7127c478bd9Sstevel@tonic-gate 		npids = 0;
7137c478bd9Sstevel@tonic-gate 		if (panicstr) {
7147c478bd9Sstevel@tonic-gate 			if (panic_thread != NULL &&
7157c478bd9Sstevel@tonic-gate 			    panic_thread->t_procp != NULL &&
7167c478bd9Sstevel@tonic-gate 			    panic_thread->t_procp != &p0) {
7177c478bd9Sstevel@tonic-gate 				dump_pids[npids++] =
7187c478bd9Sstevel@tonic-gate 				    panic_thread->t_procp->p_pid;
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 		} else {
7217c478bd9Sstevel@tonic-gate 			dump_pids[npids++] = curthread->t_procp->p_pid;
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		if (npids && dump_process(dump_pids[0]) == 0)
7257c478bd9Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_CURPROC;
7267c478bd9Sstevel@tonic-gate 		else
7277c478bd9Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_KERNEL;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	} else {
7307c478bd9Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_KERNEL;
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	dumphdr->dump_hashmask = (1 << highbit(dumphdr->dump_nvtop - 1)) - 1;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/*
7367c478bd9Sstevel@tonic-gate 	 * Write out the pfn table.
7377c478bd9Sstevel@tonic-gate 	 */
7387c478bd9Sstevel@tonic-gate 	dumphdr->dump_pfn = dumpvp_flush();
7397c478bd9Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7407c478bd9Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7417c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7427c478bd9Sstevel@tonic-gate 			continue;
7437c478bd9Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7447c478bd9Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7457c478bd9Sstevel@tonic-gate 		dumpvp_write(&pfn, sizeof (pfn_t));
7467c478bd9Sstevel@tonic-gate 	}
747ae115bc7Smrj 	dump_plat_pfn();
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * Write out all the pages.
7517c478bd9Sstevel@tonic-gate 	 */
7527c478bd9Sstevel@tonic-gate 	dumphdr->dump_data = dumpvp_flush();
7537c478bd9Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7547c478bd9Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7557c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7567c478bd9Sstevel@tonic-gate 			continue;
7577c478bd9Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7587c478bd9Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		/*
7617c478bd9Sstevel@tonic-gate 		 * Map in page frame 'pfn', scan it for UE's while copying
7627c478bd9Sstevel@tonic-gate 		 * the data to dump_uebuf, unmap it, compress dump_uebuf into
7637c478bd9Sstevel@tonic-gate 		 * dump_cbuf, and write out dump_cbuf.  The UE check ensures
7647c478bd9Sstevel@tonic-gate 		 * that we don't lose the whole dump because of a latent UE.
7657c478bd9Sstevel@tonic-gate 		 */
7667c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, dump_cmap, PAGESIZE, pfn, PROT_READ,
7677c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST);
7687c478bd9Sstevel@tonic-gate 		dump_pagecopy(dump_cmap, dump_uebuf);
7697c478bd9Sstevel@tonic-gate 		hat_unload(kas.a_hat, dump_cmap, PAGESIZE, HAT_UNLOAD);
7707c478bd9Sstevel@tonic-gate 		csize = (uint32_t)compress(dump_uebuf, dump_cbuf, PAGESIZE);
7717c478bd9Sstevel@tonic-gate 		dumpvp_write(&csize, sizeof (uint32_t));
7727c478bd9Sstevel@tonic-gate 		dumpvp_write(dump_cbuf, csize);
7737c478bd9Sstevel@tonic-gate 		if (dump_ioerr) {
7747c478bd9Sstevel@tonic-gate 			dumphdr->dump_flags &= ~DF_COMPLETE;
7757c478bd9Sstevel@tonic-gate 			dumphdr->dump_npages = npages;
7767c478bd9Sstevel@tonic-gate 			break;
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 		total_csize += csize;
7797c478bd9Sstevel@tonic-gate 		if (++npages * 100LL / dumphdr->dump_npages > percent_done) {
7807c478bd9Sstevel@tonic-gate 			uprintf("^\r%3d%% done", ++percent_done);
7817c478bd9Sstevel@tonic-gate 			if (!panicstr)
7827c478bd9Sstevel@tonic-gate 				delay(1);	/* let the output be sent */
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	}
785ae115bc7Smrj 	dumphdr->dump_npages += dump_plat_data(dump_cbuf);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * Write out the initial and terminal dump headers.
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_start;
7937c478bd9Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
7947c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size;
7977c478bd9Sstevel@tonic-gate 	dumpvp_off = dumpvp_limit - DUMP_OFFSET;
7987c478bd9Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
7997c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	compress_ratio = (int)(100LL * npages / (btopr(total_csize + 1)));
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	uprintf("\r%3d%% done: %d pages dumped, compression ratio %d.%02d, ",
8047c478bd9Sstevel@tonic-gate 	    percent_done, npages, compress_ratio / 100, compress_ratio % 100);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	if (dump_ioerr == 0) {
8077c478bd9Sstevel@tonic-gate 		uprintf("dump succeeded\n");
8087c478bd9Sstevel@tonic-gate 	} else {
8097c478bd9Sstevel@tonic-gate 		uprintf("dump failed: error %d\n", dump_ioerr);
8107c478bd9Sstevel@tonic-gate 		if (panicstr && dumpfaildebug)
8117c478bd9Sstevel@tonic-gate 			debug_enter("dump failed");
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * Write out all undelivered messages.  This has to be the *last*
8167c478bd9Sstevel@tonic-gate 	 * thing we do because the dump process itself emits messages.
8177c478bd9Sstevel@tonic-gate 	 */
8187c478bd9Sstevel@tonic-gate 	if (panicstr) {
8197c478bd9Sstevel@tonic-gate 		dump_ereports();
8207c478bd9Sstevel@tonic-gate 		dump_messages();
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	delay(2 * hz);	/* let people see the 'done' message */
8247c478bd9Sstevel@tonic-gate 	dump_timeleft = 0;
8257c478bd9Sstevel@tonic-gate 	dump_ioerr = 0;
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate /*
8297c478bd9Sstevel@tonic-gate  * This function is called whenever the memory size, as represented
8307c478bd9Sstevel@tonic-gate  * by the phys_install list, changes.
8317c478bd9Sstevel@tonic-gate  */
8327c478bd9Sstevel@tonic-gate void
8337c478bd9Sstevel@tonic-gate dump_resize()
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 	mutex_enter(&dump_lock);
8367c478bd9Sstevel@tonic-gate 	dumphdr_init();
8377c478bd9Sstevel@tonic-gate 	dumpbuf_resize();
8387c478bd9Sstevel@tonic-gate 	mutex_exit(&dump_lock);
8397c478bd9Sstevel@tonic-gate }
840*e7cbe64fSgw 
841*e7cbe64fSgw /*
842*e7cbe64fSgw  * This function allows for dynamic resizing of a dump area. It assumes that
843*e7cbe64fSgw  * the underlying device has update its appropriate size(9P).
844*e7cbe64fSgw  */
845*e7cbe64fSgw int
846*e7cbe64fSgw dumpvp_resize()
847*e7cbe64fSgw {
848*e7cbe64fSgw 	int error;
849*e7cbe64fSgw 	vattr_t vattr;
850*e7cbe64fSgw 
851*e7cbe64fSgw 	mutex_enter(&dump_lock);
852*e7cbe64fSgw 	vattr.va_mask = AT_SIZE;
853*e7cbe64fSgw 	if ((error = VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL)) != 0) {
854*e7cbe64fSgw 		mutex_exit(&dump_lock);
855*e7cbe64fSgw 		return (error);
856*e7cbe64fSgw 	}
857*e7cbe64fSgw 
858*e7cbe64fSgw 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE) {
859*e7cbe64fSgw 		mutex_exit(&dump_lock);
860*e7cbe64fSgw 		return (ENOSPC);
861*e7cbe64fSgw 	}
862*e7cbe64fSgw 
863*e7cbe64fSgw 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
864*e7cbe64fSgw 	mutex_exit(&dump_lock);
865*e7cbe64fSgw 	return (0);
866*e7cbe64fSgw }
867