xref: /titanic_53/usr/src/uts/common/os/dumpsubr.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 /*
22ae115bc7Smrj  * Copyright 2007 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>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <vm/hat.h>
677c478bd9Sstevel@tonic-gate #include <vm/as.h>
687c478bd9Sstevel@tonic-gate #include <vm/page.h>
697c478bd9Sstevel@tonic-gate #include <vm/seg.h>
707c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate kmutex_t	dump_lock;	/* lock for dump configuration */
737c478bd9Sstevel@tonic-gate dumphdr_t	*dumphdr;	/* dump header */
747c478bd9Sstevel@tonic-gate int		dump_conflags = DUMP_KERNEL; /* dump configuration flags */
757c478bd9Sstevel@tonic-gate vnode_t		*dumpvp;	/* dump device vnode pointer */
767c478bd9Sstevel@tonic-gate u_offset_t	dumpvp_size;	/* size of dump device, in bytes */
777c478bd9Sstevel@tonic-gate static u_offset_t dumpvp_limit;	/* maximum write offset */
787c478bd9Sstevel@tonic-gate char		*dumppath;	/* pathname of dump device */
797c478bd9Sstevel@tonic-gate int		dump_timeout = 120; /* timeout for dumping page during panic */
807c478bd9Sstevel@tonic-gate int		dump_timeleft;	/* portion of dump_timeout remaining */
81843e1988Sjohnlev int		dump_ioerr;	/* dump i/o error */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #ifdef DEBUG
847c478bd9Sstevel@tonic-gate int		dumpfaildebug = 1;	/* enter debugger if dump fails */
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate int		dumpfaildebug = 0;
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static ulong_t	*dump_bitmap;	/* bitmap for marking pages to dump */
907c478bd9Sstevel@tonic-gate static pgcnt_t	dump_bitmapsize; /* size of bitmap */
917c478bd9Sstevel@tonic-gate static pid_t	*dump_pids;	/* list of process IDs at dump time */
927c478bd9Sstevel@tonic-gate static offset_t	dumpvp_off;	/* current dump device offset */
937c478bd9Sstevel@tonic-gate static char	*dump_cmap;	/* VA for dump compression mapping */
947c478bd9Sstevel@tonic-gate static char	*dumpbuf_cur, *dumpbuf_start, *dumpbuf_end;
957c478bd9Sstevel@tonic-gate static char	*dump_cbuf;	/* compression buffer */
967c478bd9Sstevel@tonic-gate static char	*dump_uebuf;	/* memory error detection buffer */
977c478bd9Sstevel@tonic-gate static size_t	dumpbuf_size;	/* size of dumpbuf in bytes */
987c478bd9Sstevel@tonic-gate static size_t	dumpbuf_limit = 1UL << 23;	/* 8MB */
997c478bd9Sstevel@tonic-gate static size_t	dump_iosize;	/* device's best transfer size, if any */
1007c478bd9Sstevel@tonic-gate static uint64_t	dumpbuf_thresh = 1ULL << 30;	/* 1GB */
1017c478bd9Sstevel@tonic-gate static ulong_t	dumpbuf_mult = 8;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * The dump i/o buffer must be at least one page, at most xfer_size bytes, and
1057c478bd9Sstevel@tonic-gate  * should scale with physmem in between.  The transfer size passed in will
1067c478bd9Sstevel@tonic-gate  * either represent a global default (maxphys) or the best size for the device.
1077c478bd9Sstevel@tonic-gate  * Once the physical memory size exceeds dumpbuf_thresh (1GB by default), we
1087c478bd9Sstevel@tonic-gate  * increase the percentage of physical memory that dumpbuf can consume by a
1097c478bd9Sstevel@tonic-gate  * factor of dumpbuf_mult (8 by default) to improve large memory performance.
1107c478bd9Sstevel@tonic-gate  * The size of the dumpbuf i/o buffer is limited by dumpbuf_limit (8MB by
1117c478bd9Sstevel@tonic-gate  * default) because the dump performance saturates beyond a certain size.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate static size_t
1147c478bd9Sstevel@tonic-gate dumpbuf_iosize(size_t xfer_size)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	pgcnt_t scale = physmem;
1177c478bd9Sstevel@tonic-gate 	size_t iosize;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (scale >= dumpbuf_thresh / PAGESIZE) {
1207c478bd9Sstevel@tonic-gate 		scale *= dumpbuf_mult; /* increase scaling factor */
1217c478bd9Sstevel@tonic-gate 		iosize = MIN(xfer_size, scale) & PAGEMASK;
1227c478bd9Sstevel@tonic-gate 		if (dumpbuf_limit && iosize > dumpbuf_limit)
1237c478bd9Sstevel@tonic-gate 			iosize = MAX(PAGESIZE, dumpbuf_limit & PAGEMASK);
1247c478bd9Sstevel@tonic-gate 	} else
1257c478bd9Sstevel@tonic-gate 		iosize = MAX(PAGESIZE, MIN(xfer_size, scale) & PAGEMASK);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	return (iosize);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static void
1317c478bd9Sstevel@tonic-gate dumpbuf_resize(void)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	char *old_buf = dumpbuf_start;
1347c478bd9Sstevel@tonic-gate 	size_t old_size = dumpbuf_size;
1357c478bd9Sstevel@tonic-gate 	char *new_buf;
1367c478bd9Sstevel@tonic-gate 	size_t new_size;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((new_size = dumpbuf_iosize(MAX(dump_iosize, maxphys))) <= old_size)
1417c478bd9Sstevel@tonic-gate 		return; /* no need to reallocate buffer */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	new_buf = kmem_alloc(new_size, KM_SLEEP);
1447c478bd9Sstevel@tonic-gate 	dumpbuf_size = new_size;
1457c478bd9Sstevel@tonic-gate 	dumpbuf_start = new_buf;
1467c478bd9Sstevel@tonic-gate 	dumpbuf_end = new_buf + new_size;
1477c478bd9Sstevel@tonic-gate 	kmem_free(old_buf, old_size);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static void
1517c478bd9Sstevel@tonic-gate dumphdr_init(void)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	pgcnt_t npages = 0;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (dumphdr == NULL) {
1587c478bd9Sstevel@tonic-gate 		dumphdr = kmem_zalloc(sizeof (dumphdr_t), KM_SLEEP);
1597c478bd9Sstevel@tonic-gate 		dumphdr->dump_magic = DUMP_MAGIC;
1607c478bd9Sstevel@tonic-gate 		dumphdr->dump_version = DUMP_VERSION;
1617c478bd9Sstevel@tonic-gate 		dumphdr->dump_wordsize = DUMP_WORDSIZE;
1627c478bd9Sstevel@tonic-gate 		dumphdr->dump_pageshift = PAGESHIFT;
1637c478bd9Sstevel@tonic-gate 		dumphdr->dump_pagesize = PAGESIZE;
1647c478bd9Sstevel@tonic-gate 		dumphdr->dump_utsname = utsname;
1657c478bd9Sstevel@tonic-gate 		(void) strcpy(dumphdr->dump_platform, platform);
1667c478bd9Sstevel@tonic-gate 		dump_cmap = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
1677c478bd9Sstevel@tonic-gate 		dumpbuf_size = dumpbuf_iosize(maxphys);
1687c478bd9Sstevel@tonic-gate 		dumpbuf_start = kmem_alloc(dumpbuf_size, KM_SLEEP);
1697c478bd9Sstevel@tonic-gate 		dumpbuf_end = dumpbuf_start + dumpbuf_size;
1707c478bd9Sstevel@tonic-gate 		dump_cbuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* compress buf */
1717c478bd9Sstevel@tonic-gate 		dump_uebuf = kmem_alloc(PAGESIZE, KM_SLEEP); /* UE buf */
1727c478bd9Sstevel@tonic-gate 		dump_pids = kmem_alloc(v.v_proc * sizeof (pid_t), KM_SLEEP);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
175843e1988Sjohnlev 	npages = num_phys_pages();
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (dump_bitmapsize != npages) {
1787c478bd9Sstevel@tonic-gate 		void *map = kmem_alloc(BT_SIZEOFMAP(npages), KM_SLEEP);
1797c478bd9Sstevel@tonic-gate 		kmem_free(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
1807c478bd9Sstevel@tonic-gate 		dump_bitmap = map;
1817c478bd9Sstevel@tonic-gate 		dump_bitmapsize = npages;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Establish a new dump device.
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate int
1897c478bd9Sstevel@tonic-gate dumpinit(vnode_t *vp, char *name, int justchecking)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	vnode_t *cvp;
1927c478bd9Sstevel@tonic-gate 	vattr_t vattr;
1937c478bd9Sstevel@tonic-gate 	vnode_t *cdev_vp;
1947c478bd9Sstevel@tonic-gate 	int error = 0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	dumphdr_init();
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	cvp = common_specvp(vp);
2017c478bd9Sstevel@tonic-gate 	if (cvp == dumpvp)
2027c478bd9Sstevel@tonic-gate 		return (0);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Determine whether this is a plausible dump device.  We want either:
2067c478bd9Sstevel@tonic-gate 	 * (1) a real device that's not mounted and has a cb_dump routine, or
2077c478bd9Sstevel@tonic-gate 	 * (2) a swapfile on some filesystem that has a vop_dump routine.
2087c478bd9Sstevel@tonic-gate 	 */
209*da6c28aaSamw 	if ((error = VOP_OPEN(&cvp, FREAD | FWRITE, kcred, NULL)) != 0)
2107c478bd9Sstevel@tonic-gate 		return (error);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE | AT_TYPE | AT_RDEV;
213*da6c28aaSamw 	if ((error = VOP_GETATTR(cvp, &vattr, 0, kcred, NULL)) == 0) {
2147c478bd9Sstevel@tonic-gate 		if (vattr.va_type == VBLK || vattr.va_type == VCHR) {
2157c478bd9Sstevel@tonic-gate 			if (devopsp[getmajor(vattr.va_rdev)]->
2167c478bd9Sstevel@tonic-gate 			    devo_cb_ops->cb_dump == nodev)
2177c478bd9Sstevel@tonic-gate 				error = ENOTSUP;
2187c478bd9Sstevel@tonic-gate 			else if (vfs_devismounted(vattr.va_rdev))
2197c478bd9Sstevel@tonic-gate 				error = EBUSY;
2207c478bd9Sstevel@tonic-gate 		} else {
2217c478bd9Sstevel@tonic-gate 			if (vn_matchopval(cvp, VOPNAME_DUMP, fs_nosys) ||
2227c478bd9Sstevel@tonic-gate 			    !IS_SWAPVP(cvp))
2237c478bd9Sstevel@tonic-gate 				error = ENOTSUP;
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE)
2287c478bd9Sstevel@tonic-gate 		error = ENOSPC;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (error || justchecking) {
231*da6c28aaSamw 		(void) VOP_CLOSE(cvp, FREAD | FWRITE, 1, (offset_t)0,
232*da6c28aaSamw 		    kcred, NULL);
2337c478bd9Sstevel@tonic-gate 		return (error);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	VN_HOLD(cvp);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (dumpvp != NULL)
2397c478bd9Sstevel@tonic-gate 		dumpfini();	/* unconfigure the old dump device */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	dumpvp = cvp;
2427c478bd9Sstevel@tonic-gate 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
2437c478bd9Sstevel@tonic-gate 	dumppath = kmem_alloc(strlen(name) + 1, KM_SLEEP);
2447c478bd9Sstevel@tonic-gate 	(void) strcpy(dumppath, name);
2457c478bd9Sstevel@tonic-gate 	dump_iosize = 0;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * If the dump device is a block device, attempt to open up the
2497c478bd9Sstevel@tonic-gate 	 * corresponding character device and determine its maximum transfer
2507c478bd9Sstevel@tonic-gate 	 * size.  We use this information to potentially resize dumpbuf to a
2517c478bd9Sstevel@tonic-gate 	 * larger and more optimal size for performing i/o to the dump device.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	if (cvp->v_type == VBLK &&
2547c478bd9Sstevel@tonic-gate 	    (cdev_vp = makespecvp(VTOS(cvp)->s_dev, VCHR)) != NULL) {
255*da6c28aaSamw 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
2567c478bd9Sstevel@tonic-gate 			size_t blk_size;
2577c478bd9Sstevel@tonic-gate 			struct dk_cinfo dki;
2587c478bd9Sstevel@tonic-gate 			struct vtoc vtoc;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCGVTOC, (intptr_t)&vtoc,
261*da6c28aaSamw 			    FKIOCTL, kcred, NULL, NULL) == 0 &&
262*da6c28aaSamw 			    vtoc.v_sectorsz != 0)
2637c478bd9Sstevel@tonic-gate 				blk_size = vtoc.v_sectorsz;
2647c478bd9Sstevel@tonic-gate 			else
2657c478bd9Sstevel@tonic-gate 				blk_size = DEV_BSIZE;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			if (VOP_IOCTL(cdev_vp, DKIOCINFO, (intptr_t)&dki,
268*da6c28aaSamw 			    FKIOCTL, kcred, NULL, NULL) == 0) {
2697c478bd9Sstevel@tonic-gate 				dump_iosize = dki.dki_maxtransfer * blk_size;
2707c478bd9Sstevel@tonic-gate 				dumpbuf_resize();
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 
273*da6c28aaSamw 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
274*da6c28aaSamw 			    kcred, NULL);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		VN_RELE(cdev_vp);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?dump on %s size %llu MB\n", name, dumpvp_size >> 20);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	return (0);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate void
2867c478bd9Sstevel@tonic-gate dumpfini(void)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dump_lock));
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	kmem_free(dumppath, strlen(dumppath) + 1);
2917c478bd9Sstevel@tonic-gate 
292*da6c28aaSamw 	(void) VOP_CLOSE(dumpvp, FREAD | FWRITE, 1, (offset_t)0, kcred, NULL);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	VN_RELE(dumpvp);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	dumpvp = NULL;
2977c478bd9Sstevel@tonic-gate 	dumpvp_size = 0;
2987c478bd9Sstevel@tonic-gate 	dumppath = NULL;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static pfn_t
3027c478bd9Sstevel@tonic-gate dump_bitnum_to_pfn(pgcnt_t bitnum)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct memlist *mp;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3077c478bd9Sstevel@tonic-gate 		if (bitnum < (mp->size >> PAGESHIFT))
3087c478bd9Sstevel@tonic-gate 			return ((mp->address >> PAGESHIFT) + bitnum);
3097c478bd9Sstevel@tonic-gate 		bitnum -= mp->size >> PAGESHIFT;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	return (PFN_INVALID);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static pgcnt_t
3157c478bd9Sstevel@tonic-gate dump_pfn_to_bitnum(pfn_t pfn)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	struct memlist *mp;
3187c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum = 0;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	for (mp = phys_install; mp != NULL; mp = mp->next) {
3217c478bd9Sstevel@tonic-gate 		if (pfn >= (mp->address >> PAGESHIFT) &&
3227c478bd9Sstevel@tonic-gate 		    pfn < ((mp->address + mp->size) >> PAGESHIFT))
3237c478bd9Sstevel@tonic-gate 			return (bitnum + pfn - (mp->address >> PAGESHIFT));
3247c478bd9Sstevel@tonic-gate 		bitnum += mp->size >> PAGESHIFT;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	return ((pgcnt_t)-1);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate static offset_t
3307c478bd9Sstevel@tonic-gate dumpvp_flush(void)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	size_t size = P2ROUNDUP(dumpbuf_cur - dumpbuf_start, PAGESIZE);
3337c478bd9Sstevel@tonic-gate 	int err;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (dumpvp_off + size > dumpvp_limit) {
3367c478bd9Sstevel@tonic-gate 		dump_ioerr = ENOSPC;
3377c478bd9Sstevel@tonic-gate 	} else if (size != 0) {
3387c478bd9Sstevel@tonic-gate 		if (panicstr)
3397c478bd9Sstevel@tonic-gate 			err = VOP_DUMP(dumpvp, dumpbuf_start,
340*da6c28aaSamw 			    lbtodb(dumpvp_off), btod(size), NULL);
3417c478bd9Sstevel@tonic-gate 		else
3427c478bd9Sstevel@tonic-gate 			err = vn_rdwr(UIO_WRITE, dumpvp, dumpbuf_start, size,
3437c478bd9Sstevel@tonic-gate 			    dumpvp_off, UIO_SYSSPACE, 0, dumpvp_limit,
3447c478bd9Sstevel@tonic-gate 			    kcred, 0);
3457c478bd9Sstevel@tonic-gate 		if (err && dump_ioerr == 0)
3467c478bd9Sstevel@tonic-gate 			dump_ioerr = err;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	dumpvp_off += size;
3497c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
3507c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
3517c478bd9Sstevel@tonic-gate 	return (dumpvp_off);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate void
3557c478bd9Sstevel@tonic-gate dumpvp_write(const void *va, size_t size)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	while (size != 0) {
3587c478bd9Sstevel@tonic-gate 		size_t len = MIN(size, dumpbuf_end - dumpbuf_cur);
3597c478bd9Sstevel@tonic-gate 		if (len == 0) {
3607c478bd9Sstevel@tonic-gate 			(void) dumpvp_flush();
3617c478bd9Sstevel@tonic-gate 		} else {
3627c478bd9Sstevel@tonic-gate 			bcopy(va, dumpbuf_cur, len);
3637c478bd9Sstevel@tonic-gate 			va = (char *)va + len;
3647c478bd9Sstevel@tonic-gate 			dumpbuf_cur += len;
3657c478bd9Sstevel@tonic-gate 			size -= len;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3717c478bd9Sstevel@tonic-gate static void
3727c478bd9Sstevel@tonic-gate dumpvp_ksyms_write(const void *src, void *dst, size_t size)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	dumpvp_write(src, size);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Mark 'pfn' in the bitmap and dump its translation table entry.
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate void
3817c478bd9Sstevel@tonic-gate dump_addpage(struct as *as, void *va, pfn_t pfn)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	mem_vtop_t mem_vtop;
3847c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
3877c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
3887c478bd9Sstevel@tonic-gate 			dumphdr->dump_npages++;
3897c478bd9Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		dumphdr->dump_nvtop++;
3927c478bd9Sstevel@tonic-gate 		mem_vtop.m_as = as;
3937c478bd9Sstevel@tonic-gate 		mem_vtop.m_va = va;
3947c478bd9Sstevel@tonic-gate 		mem_vtop.m_pfn = pfn;
3957c478bd9Sstevel@tonic-gate 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * Mark 'pfn' in the bitmap
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate void
4047c478bd9Sstevel@tonic-gate dump_page(pfn_t pfn)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
4097c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum)) {
4107c478bd9Sstevel@tonic-gate 			dumphdr->dump_npages++;
4117c478bd9Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * Dump the <as, va, pfn> information for a given address space.
4197c478bd9Sstevel@tonic-gate  * SEGOP_DUMP() will call dump_addpage() for each page in the segment.
4207c478bd9Sstevel@tonic-gate  */
4217c478bd9Sstevel@tonic-gate static void
4227c478bd9Sstevel@tonic-gate dump_as(struct as *as)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	struct seg *seg;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
4277c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
4287c478bd9Sstevel@tonic-gate 		if (seg->s_as != as)
4297c478bd9Sstevel@tonic-gate 			break;
4307c478bd9Sstevel@tonic-gate 		if (seg->s_ops == NULL)
4317c478bd9Sstevel@tonic-gate 			continue;
4327c478bd9Sstevel@tonic-gate 		SEGOP_DUMP(seg);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (seg != NULL)
4377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid segment %p in address space %p",
4387c478bd9Sstevel@tonic-gate 		    (void *)seg, (void *)as);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static int
4427c478bd9Sstevel@tonic-gate dump_process(pid_t pid)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	proc_t *p = sprlock(pid);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (p == NULL)
4477c478bd9Sstevel@tonic-gate 		return (-1);
4487c478bd9Sstevel@tonic-gate 	if (p->p_as != &kas) {
4497c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4507c478bd9Sstevel@tonic-gate 		dump_as(p->p_as);
4517c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	sprunlock(p);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (0);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate void
4607c478bd9Sstevel@tonic-gate dump_ereports(void)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	u_offset_t dumpvp_start;
4637c478bd9Sstevel@tonic-gate 	erpt_dump_t ed;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL)
4667c478bd9Sstevel@tonic-gate 		return;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
4697c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE);
4707c478bd9Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_ERPTSIZE;
4717c478bd9Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	fm_ereport_dump();
4747c478bd9Sstevel@tonic-gate 	if (panicstr)
4757c478bd9Sstevel@tonic-gate 		errorq_dump();
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	bzero(&ed, sizeof (ed)); /* indicate end of ereports */
4787c478bd9Sstevel@tonic-gate 	dumpvp_write(&ed, sizeof (ed));
4797c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (!panicstr) {
4827c478bd9Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
4837c478bd9Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
484*da6c28aaSamw 		    B_INVAL | B_FORCE, kcred, NULL);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate void
4897c478bd9Sstevel@tonic-gate dump_messages(void)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	log_dump_t ld;
4927c478bd9Sstevel@tonic-gate 	mblk_t *mctl, *mdata;
4937c478bd9Sstevel@tonic-gate 	queue_t *q, *qlast;
4947c478bd9Sstevel@tonic-gate 	u_offset_t dumpvp_start;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL || log_consq == NULL)
4977c478bd9Sstevel@tonic-gate 		return;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5007c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_OFFSET;
5017c478bd9Sstevel@tonic-gate 	dumpvp_start = dumpvp_limit - DUMP_LOGSIZE;
5027c478bd9Sstevel@tonic-gate 	dumpvp_off = dumpvp_start;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	qlast = NULL;
5057c478bd9Sstevel@tonic-gate 	do {
5067c478bd9Sstevel@tonic-gate 		for (q = log_consq; q->q_next != qlast; q = q->q_next)
5077c478bd9Sstevel@tonic-gate 			continue;
5087c478bd9Sstevel@tonic-gate 		for (mctl = q->q_first; mctl != NULL; mctl = mctl->b_next) {
5097c478bd9Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
5107c478bd9Sstevel@tonic-gate 			mdata = mctl->b_cont;
5117c478bd9Sstevel@tonic-gate 			ld.ld_magic = LOG_MAGIC;
5127c478bd9Sstevel@tonic-gate 			ld.ld_msgsize = MBLKL(mctl->b_cont);
5137c478bd9Sstevel@tonic-gate 			ld.ld_csum = checksum32(mctl->b_rptr, MBLKL(mctl));
5147c478bd9Sstevel@tonic-gate 			ld.ld_msum = checksum32(mdata->b_rptr, MBLKL(mdata));
5157c478bd9Sstevel@tonic-gate 			dumpvp_write(&ld, sizeof (ld));
5167c478bd9Sstevel@tonic-gate 			dumpvp_write(mctl->b_rptr, MBLKL(mctl));
5177c478bd9Sstevel@tonic-gate 			dumpvp_write(mdata->b_rptr, MBLKL(mdata));
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 	} while ((qlast = q) != log_consq);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	ld.ld_magic = 0;		/* indicate end of messages */
5227c478bd9Sstevel@tonic-gate 	dumpvp_write(&ld, sizeof (ld));
5237c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
5247c478bd9Sstevel@tonic-gate 	if (!panicstr) {
5257c478bd9Sstevel@tonic-gate 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
5267c478bd9Sstevel@tonic-gate 		    (size_t)(dumpvp_off - dumpvp_start),
527*da6c28aaSamw 		    B_INVAL | B_FORCE, kcred, NULL);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate static void
5327c478bd9Sstevel@tonic-gate dump_pagecopy(void *src, void *dst)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	long *wsrc = (long *)src;
5357c478bd9Sstevel@tonic-gate 	long *wdst = (long *)dst;
5367c478bd9Sstevel@tonic-gate 	const ulong_t ncopies = PAGESIZE / sizeof (long);
5377c478bd9Sstevel@tonic-gate 	volatile int w = 0;
5387c478bd9Sstevel@tonic-gate 	volatile int ueoff = -1;
5397c478bd9Sstevel@tonic-gate 	on_trap_data_t otd;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_EC)) {
5427c478bd9Sstevel@tonic-gate 		if (ueoff == -1) {
5437c478bd9Sstevel@tonic-gate 			uint64_t pa;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 			ueoff = w * sizeof (long);
5467c478bd9Sstevel@tonic-gate 			pa = ptob((uint64_t)hat_getpfnum(kas.a_hat, src))
5477c478bd9Sstevel@tonic-gate 			    + ueoff;
5487c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "memory error at PA 0x%08x.%08x",
5497c478bd9Sstevel@tonic-gate 			    (uint32_t)(pa >> 32), (uint32_t)pa);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate #ifdef _LP64
5527c478bd9Sstevel@tonic-gate 		wdst[w++] = 0xbadecc00badecc;
5537c478bd9Sstevel@tonic-gate #else
5547c478bd9Sstevel@tonic-gate 		wdst[w++] = 0xbadecc;
5557c478bd9Sstevel@tonic-gate #endif
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	while (w < ncopies) {
5587c478bd9Sstevel@tonic-gate 		wdst[w] = wsrc[w];
5597c478bd9Sstevel@tonic-gate 		w++;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	no_trap();
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Dump the system.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate void
5687c478bd9Sstevel@tonic-gate dumpsys(void)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	pfn_t pfn;
5717c478bd9Sstevel@tonic-gate 	pgcnt_t bitnum;
5727c478bd9Sstevel@tonic-gate 	int npages = 0;
5737c478bd9Sstevel@tonic-gate 	int percent_done = 0;
5747c478bd9Sstevel@tonic-gate 	uint32_t csize;
5757c478bd9Sstevel@tonic-gate 	u_offset_t total_csize = 0;
5767c478bd9Sstevel@tonic-gate 	int compress_ratio;
5777c478bd9Sstevel@tonic-gate 	proc_t *p;
5787c478bd9Sstevel@tonic-gate 	pid_t npids, pidx;
5797c478bd9Sstevel@tonic-gate 	char *content;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (dumpvp == NULL || dumphdr == NULL) {
5827c478bd9Sstevel@tonic-gate 		uprintf("skipping system dump - no dump device configured\n");
5837c478bd9Sstevel@tonic-gate 		return;
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 	dumpbuf_cur = dumpbuf_start;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	/*
5887c478bd9Sstevel@tonic-gate 	 * Calculate the starting block for dump.  If we're dumping on a
5897c478bd9Sstevel@tonic-gate 	 * swap device, start 1/5 of the way in; otherwise, start at the
5907c478bd9Sstevel@tonic-gate 	 * beginning.  And never use the first page -- it may be a disk label.
5917c478bd9Sstevel@tonic-gate 	 */
5927c478bd9Sstevel@tonic-gate 	if (dumpvp->v_flag & VISSWAP)
5937c478bd9Sstevel@tonic-gate 		dumphdr->dump_start = P2ROUNDUP(dumpvp_size / 5, DUMP_OFFSET);
5947c478bd9Sstevel@tonic-gate 	else
5957c478bd9Sstevel@tonic-gate 		dumphdr->dump_start = DUMP_OFFSET;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	dumphdr->dump_flags = DF_VALID | DF_COMPLETE | DF_LIVE;
5987c478bd9Sstevel@tonic-gate 	dumphdr->dump_crashtime = gethrestime_sec();
5997c478bd9Sstevel@tonic-gate 	dumphdr->dump_npages = 0;
6007c478bd9Sstevel@tonic-gate 	dumphdr->dump_nvtop = 0;
6017c478bd9Sstevel@tonic-gate 	bzero(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
6027c478bd9Sstevel@tonic-gate 	dump_timeleft = dump_timeout;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if (panicstr) {
6057c478bd9Sstevel@tonic-gate 		dumphdr->dump_flags &= ~DF_LIVE;
606*da6c28aaSamw 		(void) VOP_DUMPCTL(dumpvp, DUMP_FREE, NULL, NULL);
607*da6c28aaSamw 		(void) VOP_DUMPCTL(dumpvp, DUMP_ALLOC, NULL, NULL);
6087c478bd9Sstevel@tonic-gate 		(void) vsnprintf(dumphdr->dump_panicstring, DUMP_PANICSIZE,
6097c478bd9Sstevel@tonic-gate 		    panicstr, panicargs);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL)
6137c478bd9Sstevel@tonic-gate 		content = "all";
6147c478bd9Sstevel@tonic-gate 	else if (dump_conflags & DUMP_CURPROC)
6157c478bd9Sstevel@tonic-gate 		content = "kernel + curproc";
6167c478bd9Sstevel@tonic-gate 	else
6177c478bd9Sstevel@tonic-gate 		content = "kernel";
6187c478bd9Sstevel@tonic-gate 	uprintf("dumping to %s, offset %lld, content: %s\n", dumppath,
6197c478bd9Sstevel@tonic-gate 	    dumphdr->dump_start, content);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/*
6227c478bd9Sstevel@tonic-gate 	 * Leave room for the message and ereport save areas and terminal dump
6237c478bd9Sstevel@tonic-gate 	 * header.
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size - DUMP_LOGSIZE - DUMP_OFFSET - DUMP_ERPTSIZE;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * Write out the symbol table.  It's no longer compressed,
6297c478bd9Sstevel@tonic-gate 	 * so its 'size' and 'csize' are equal.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_ksyms = dumphdr->dump_start + PAGESIZE;
6327c478bd9Sstevel@tonic-gate 	dumphdr->dump_ksyms_size = dumphdr->dump_ksyms_csize =
6337c478bd9Sstevel@tonic-gate 	    ksyms_snapshot(dumpvp_ksyms_write, NULL, LONG_MAX);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * Write out the translation map.
6377c478bd9Sstevel@tonic-gate 	 */
6387c478bd9Sstevel@tonic-gate 	dumphdr->dump_map = dumpvp_flush();
6397c478bd9Sstevel@tonic-gate 	dump_as(&kas);
640ae115bc7Smrj 	dumphdr->dump_nvtop += dump_plat_addr();
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * call into hat, which may have unmapped pages that also need to
6447c478bd9Sstevel@tonic-gate 	 * be in the dump
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	hat_dump();
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (dump_conflags & DUMP_ALL) {
6497c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		for (npids = 0, p = practive; p != NULL; p = p->p_next)
6527c478bd9Sstevel@tonic-gate 			dump_pids[npids++] = p->p_pid;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		for (pidx = 0; pidx < npids; pidx++)
6577c478bd9Sstevel@tonic-gate 			(void) dump_process(dump_pids[pidx]);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
6607c478bd9Sstevel@tonic-gate 			dump_timeleft = dump_timeout;
6617c478bd9Sstevel@tonic-gate 			BT_SET(dump_bitmap, bitnum);
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		dumphdr->dump_npages = dump_bitmapsize;
6647c478bd9Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_ALL;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	} else if (dump_conflags & DUMP_CURPROC) {
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * Determine which pid is to be dumped.  If we're panicking, we
6697c478bd9Sstevel@tonic-gate 		 * dump the process associated with panic_thread (if any).  If
6707c478bd9Sstevel@tonic-gate 		 * this is a live dump, we dump the process associated with
6717c478bd9Sstevel@tonic-gate 		 * curthread.
6727c478bd9Sstevel@tonic-gate 		 */
6737c478bd9Sstevel@tonic-gate 		npids = 0;
6747c478bd9Sstevel@tonic-gate 		if (panicstr) {
6757c478bd9Sstevel@tonic-gate 			if (panic_thread != NULL &&
6767c478bd9Sstevel@tonic-gate 			    panic_thread->t_procp != NULL &&
6777c478bd9Sstevel@tonic-gate 			    panic_thread->t_procp != &p0) {
6787c478bd9Sstevel@tonic-gate 				dump_pids[npids++] =
6797c478bd9Sstevel@tonic-gate 				    panic_thread->t_procp->p_pid;
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 		} else {
6827c478bd9Sstevel@tonic-gate 			dump_pids[npids++] = curthread->t_procp->p_pid;
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		if (npids && dump_process(dump_pids[0]) == 0)
6867c478bd9Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_CURPROC;
6877c478bd9Sstevel@tonic-gate 		else
6887c478bd9Sstevel@tonic-gate 			dumphdr->dump_flags |= DF_KERNEL;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	} else {
6917c478bd9Sstevel@tonic-gate 		dumphdr->dump_flags |= DF_KERNEL;
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	dumphdr->dump_hashmask = (1 << highbit(dumphdr->dump_nvtop - 1)) - 1;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/*
6977c478bd9Sstevel@tonic-gate 	 * Write out the pfn table.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 	dumphdr->dump_pfn = dumpvp_flush();
7007c478bd9Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7017c478bd9Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7027c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7037c478bd9Sstevel@tonic-gate 			continue;
7047c478bd9Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7057c478bd9Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7067c478bd9Sstevel@tonic-gate 		dumpvp_write(&pfn, sizeof (pfn_t));
7077c478bd9Sstevel@tonic-gate 	}
708ae115bc7Smrj 	dump_plat_pfn();
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Write out all the pages.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	dumphdr->dump_data = dumpvp_flush();
7147c478bd9Sstevel@tonic-gate 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
7157c478bd9Sstevel@tonic-gate 		dump_timeleft = dump_timeout;
7167c478bd9Sstevel@tonic-gate 		if (!BT_TEST(dump_bitmap, bitnum))
7177c478bd9Sstevel@tonic-gate 			continue;
7187c478bd9Sstevel@tonic-gate 		pfn = dump_bitnum_to_pfn(bitnum);
7197c478bd9Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * Map in page frame 'pfn', scan it for UE's while copying
7237c478bd9Sstevel@tonic-gate 		 * the data to dump_uebuf, unmap it, compress dump_uebuf into
7247c478bd9Sstevel@tonic-gate 		 * dump_cbuf, and write out dump_cbuf.  The UE check ensures
7257c478bd9Sstevel@tonic-gate 		 * that we don't lose the whole dump because of a latent UE.
7267c478bd9Sstevel@tonic-gate 		 */
7277c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, dump_cmap, PAGESIZE, pfn, PROT_READ,
7287c478bd9Sstevel@tonic-gate 		    HAT_LOAD_NOCONSIST);
7297c478bd9Sstevel@tonic-gate 		dump_pagecopy(dump_cmap, dump_uebuf);
7307c478bd9Sstevel@tonic-gate 		hat_unload(kas.a_hat, dump_cmap, PAGESIZE, HAT_UNLOAD);
7317c478bd9Sstevel@tonic-gate 		csize = (uint32_t)compress(dump_uebuf, dump_cbuf, PAGESIZE);
7327c478bd9Sstevel@tonic-gate 		dumpvp_write(&csize, sizeof (uint32_t));
7337c478bd9Sstevel@tonic-gate 		dumpvp_write(dump_cbuf, csize);
7347c478bd9Sstevel@tonic-gate 		if (dump_ioerr) {
7357c478bd9Sstevel@tonic-gate 			dumphdr->dump_flags &= ~DF_COMPLETE;
7367c478bd9Sstevel@tonic-gate 			dumphdr->dump_npages = npages;
7377c478bd9Sstevel@tonic-gate 			break;
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 		total_csize += csize;
7407c478bd9Sstevel@tonic-gate 		if (++npages * 100LL / dumphdr->dump_npages > percent_done) {
7417c478bd9Sstevel@tonic-gate 			uprintf("^\r%3d%% done", ++percent_done);
7427c478bd9Sstevel@tonic-gate 			if (!panicstr)
7437c478bd9Sstevel@tonic-gate 				delay(1);	/* let the output be sent */
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
746ae115bc7Smrj 	dumphdr->dump_npages += dump_plat_data(dump_cbuf);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/*
7517c478bd9Sstevel@tonic-gate 	 * Write out the initial and terminal dump headers.
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 	dumpvp_off = dumphdr->dump_start;
7547c478bd9Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
7557c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	dumpvp_limit = dumpvp_size;
7587c478bd9Sstevel@tonic-gate 	dumpvp_off = dumpvp_limit - DUMP_OFFSET;
7597c478bd9Sstevel@tonic-gate 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
7607c478bd9Sstevel@tonic-gate 	(void) dumpvp_flush();
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	compress_ratio = (int)(100LL * npages / (btopr(total_csize + 1)));
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	uprintf("\r%3d%% done: %d pages dumped, compression ratio %d.%02d, ",
7657c478bd9Sstevel@tonic-gate 	    percent_done, npages, compress_ratio / 100, compress_ratio % 100);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (dump_ioerr == 0) {
7687c478bd9Sstevel@tonic-gate 		uprintf("dump succeeded\n");
7697c478bd9Sstevel@tonic-gate 	} else {
7707c478bd9Sstevel@tonic-gate 		uprintf("dump failed: error %d\n", dump_ioerr);
7717c478bd9Sstevel@tonic-gate 		if (panicstr && dumpfaildebug)
7727c478bd9Sstevel@tonic-gate 			debug_enter("dump failed");
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * Write out all undelivered messages.  This has to be the *last*
7777c478bd9Sstevel@tonic-gate 	 * thing we do because the dump process itself emits messages.
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	if (panicstr) {
7807c478bd9Sstevel@tonic-gate 		dump_ereports();
7817c478bd9Sstevel@tonic-gate 		dump_messages();
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	delay(2 * hz);	/* let people see the 'done' message */
7857c478bd9Sstevel@tonic-gate 	dump_timeleft = 0;
7867c478bd9Sstevel@tonic-gate 	dump_ioerr = 0;
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * This function is called whenever the memory size, as represented
7917c478bd9Sstevel@tonic-gate  * by the phys_install list, changes.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate void
7947c478bd9Sstevel@tonic-gate dump_resize()
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	mutex_enter(&dump_lock);
7977c478bd9Sstevel@tonic-gate 	dumphdr_init();
7987c478bd9Sstevel@tonic-gate 	dumpbuf_resize();
7997c478bd9Sstevel@tonic-gate 	mutex_exit(&dump_lock);
8007c478bd9Sstevel@tonic-gate }
801