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