xref: /titanic_41/usr/src/uts/common/os/dumpsubr.c (revision d5ace9454616652a717c9831d949dffa319381f9)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/vm.h>
31 #include <sys/proc.h>
32 #include <sys/file.h>
33 #include <sys/conf.h>
34 #include <sys/kmem.h>
35 #include <sys/mem.h>
36 #include <sys/mman.h>
37 #include <sys/vnode.h>
38 #include <sys/errno.h>
39 #include <sys/memlist.h>
40 #include <sys/dumphdr.h>
41 #include <sys/dumpadm.h>
42 #include <sys/ksyms.h>
43 #include <sys/compress.h>
44 #include <sys/stream.h>
45 #include <sys/strsun.h>
46 #include <sys/cmn_err.h>
47 #include <sys/bitmap.h>
48 #include <sys/modctl.h>
49 #include <sys/utsname.h>
50 #include <sys/systeminfo.h>
51 #include <sys/vmem.h>
52 #include <sys/log.h>
53 #include <sys/var.h>
54 #include <sys/debug.h>
55 #include <sys/sunddi.h>
56 #include <fs/fs_subr.h>
57 #include <sys/fs/snode.h>
58 #include <sys/ontrap.h>
59 #include <sys/panic.h>
60 #include <sys/dkio.h>
61 #include <sys/vtoc.h>
62 #include <sys/errorq.h>
63 #include <sys/fm/util.h>
64 #include <sys/fs/zfs.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, NULL)) != 0)
210 		return (error);
211 
212 	vattr.va_mask = AT_SIZE | AT_TYPE | AT_RDEV;
213 	if ((error = VOP_GETATTR(cvp, &vattr, 0, kcred, NULL)) == 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,
232 		    kcred, NULL);
233 		return (error);
234 	}
235 
236 	VN_HOLD(cvp);
237 
238 	if (dumpvp != NULL)
239 		dumpfini();	/* unconfigure the old dump device */
240 
241 	dumpvp = cvp;
242 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
243 	dumppath = kmem_alloc(strlen(name) + 1, KM_SLEEP);
244 	(void) strcpy(dumppath, name);
245 	dump_iosize = 0;
246 
247 	/*
248 	 * If the dump device is a block device, attempt to open up the
249 	 * corresponding character device and determine its maximum transfer
250 	 * size.  We use this information to potentially resize dumpbuf to a
251 	 * larger and more optimal size for performing i/o to the dump device.
252 	 */
253 	if (cvp->v_type == VBLK &&
254 	    (cdev_vp = makespecvp(VTOS(cvp)->s_dev, VCHR)) != NULL) {
255 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
256 			size_t blk_size;
257 			struct dk_cinfo dki;
258 			struct extvtoc vtoc;
259 
260 			if (VOP_IOCTL(cdev_vp, DKIOCGEXTVTOC, (intptr_t)&vtoc,
261 			    FKIOCTL, kcred, NULL, NULL) == 0 &&
262 			    vtoc.v_sectorsz != 0)
263 				blk_size = vtoc.v_sectorsz;
264 			else
265 				blk_size = DEV_BSIZE;
266 
267 			if (VOP_IOCTL(cdev_vp, DKIOCINFO, (intptr_t)&dki,
268 			    FKIOCTL, kcred, NULL, NULL) == 0) {
269 				dump_iosize = dki.dki_maxtransfer * blk_size;
270 				dumpbuf_resize();
271 			}
272 			/*
273 			 * If we are working with a zvol then call into
274 			 * it to dumpify itself.
275 			 */
276 			if (strcmp(dki.dki_dname, ZVOL_DRIVER) == 0) {
277 				if ((error = VOP_IOCTL(cdev_vp,
278 				    DKIOCDUMPINIT, NULL, FKIOCTL, kcred,
279 				    NULL, NULL)) != 0) {
280 					dumpfini();
281 				}
282 			}
283 
284 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
285 			    kcred, NULL);
286 		}
287 
288 		VN_RELE(cdev_vp);
289 	}
290 
291 	cmn_err(CE_CONT, "?dump on %s size %llu MB\n", name, dumpvp_size >> 20);
292 
293 	return (error);
294 }
295 
296 void
297 dumpfini(void)
298 {
299 	vattr_t vattr;
300 	boolean_t is_zfs = B_FALSE;
301 	vnode_t *cdev_vp;
302 	ASSERT(MUTEX_HELD(&dump_lock));
303 
304 	kmem_free(dumppath, strlen(dumppath) + 1);
305 
306 	/*
307 	 * Determine if we are using zvols for our dump device
308 	 */
309 	vattr.va_mask = AT_RDEV;
310 	if (VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL) == 0) {
311 		is_zfs = (getmajor(vattr.va_rdev) ==
312 		    ddi_name_to_major(ZFS_DRIVER)) ? B_TRUE : B_FALSE;
313 	}
314 
315 	/*
316 	 * If we have a zvol dump device then we call into zfs so
317 	 * that it may have a chance to cleanup.
318 	 */
319 	if (is_zfs &&
320 	    (cdev_vp = makespecvp(VTOS(dumpvp)->s_dev, VCHR)) != NULL) {
321 		if (VOP_OPEN(&cdev_vp, FREAD | FWRITE, kcred, NULL) == 0) {
322 			(void) VOP_IOCTL(cdev_vp, DKIOCDUMPFINI, NULL, FKIOCTL,
323 			    kcred, NULL, NULL);
324 			(void) VOP_CLOSE(cdev_vp, FREAD | FWRITE, 1, 0,
325 			    kcred, NULL);
326 		}
327 		VN_RELE(cdev_vp);
328 	}
329 
330 	(void) VOP_CLOSE(dumpvp, FREAD | FWRITE, 1, (offset_t)0, kcred, NULL);
331 
332 	VN_RELE(dumpvp);
333 
334 	dumpvp = NULL;
335 	dumpvp_size = 0;
336 	dumppath = NULL;
337 }
338 
339 static pfn_t
340 dump_bitnum_to_pfn(pgcnt_t bitnum)
341 {
342 	struct memlist *mp;
343 
344 	for (mp = phys_install; mp != NULL; mp = mp->next) {
345 		if (bitnum < (mp->size >> PAGESHIFT))
346 			return ((mp->address >> PAGESHIFT) + bitnum);
347 		bitnum -= mp->size >> PAGESHIFT;
348 	}
349 	return (PFN_INVALID);
350 }
351 
352 static pgcnt_t
353 dump_pfn_to_bitnum(pfn_t pfn)
354 {
355 	struct memlist *mp;
356 	pgcnt_t bitnum = 0;
357 
358 	for (mp = phys_install; mp != NULL; mp = mp->next) {
359 		if (pfn >= (mp->address >> PAGESHIFT) &&
360 		    pfn < ((mp->address + mp->size) >> PAGESHIFT))
361 			return (bitnum + pfn - (mp->address >> PAGESHIFT));
362 		bitnum += mp->size >> PAGESHIFT;
363 	}
364 	return ((pgcnt_t)-1);
365 }
366 
367 static offset_t
368 dumpvp_flush(void)
369 {
370 	size_t size = P2ROUNDUP(dumpbuf_cur - dumpbuf_start, PAGESIZE);
371 	int err;
372 
373 	if (dumpvp_off + size > dumpvp_limit) {
374 		dump_ioerr = ENOSPC;
375 	} else if (size != 0) {
376 		if (panicstr)
377 			err = VOP_DUMP(dumpvp, dumpbuf_start,
378 			    lbtodb(dumpvp_off), btod(size), NULL);
379 		else
380 			err = vn_rdwr(UIO_WRITE, dumpvp, dumpbuf_start, size,
381 			    dumpvp_off, UIO_SYSSPACE, 0, dumpvp_limit,
382 			    kcred, 0);
383 		if (err && dump_ioerr == 0)
384 			dump_ioerr = err;
385 	}
386 	dumpvp_off += size;
387 	dumpbuf_cur = dumpbuf_start;
388 	dump_timeleft = dump_timeout;
389 	return (dumpvp_off);
390 }
391 
392 void
393 dumpvp_write(const void *va, size_t size)
394 {
395 	while (size != 0) {
396 		size_t len = MIN(size, dumpbuf_end - dumpbuf_cur);
397 		if (len == 0) {
398 			(void) dumpvp_flush();
399 		} else {
400 			bcopy(va, dumpbuf_cur, len);
401 			va = (char *)va + len;
402 			dumpbuf_cur += len;
403 			size -= len;
404 		}
405 	}
406 }
407 
408 /*ARGSUSED*/
409 static void
410 dumpvp_ksyms_write(const void *src, void *dst, size_t size)
411 {
412 	dumpvp_write(src, size);
413 }
414 
415 /*
416  * Mark 'pfn' in the bitmap and dump its translation table entry.
417  */
418 void
419 dump_addpage(struct as *as, void *va, pfn_t pfn)
420 {
421 	mem_vtop_t mem_vtop;
422 	pgcnt_t bitnum;
423 
424 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
425 		if (!BT_TEST(dump_bitmap, bitnum)) {
426 			dumphdr->dump_npages++;
427 			BT_SET(dump_bitmap, bitnum);
428 		}
429 		dumphdr->dump_nvtop++;
430 		mem_vtop.m_as = as;
431 		mem_vtop.m_va = va;
432 		mem_vtop.m_pfn = pfn;
433 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
434 	}
435 	dump_timeleft = dump_timeout;
436 }
437 
438 /*
439  * Mark 'pfn' in the bitmap
440  */
441 void
442 dump_page(pfn_t pfn)
443 {
444 	pgcnt_t bitnum;
445 
446 	if ((bitnum = dump_pfn_to_bitnum(pfn)) != (pgcnt_t)-1) {
447 		if (!BT_TEST(dump_bitmap, bitnum)) {
448 			dumphdr->dump_npages++;
449 			BT_SET(dump_bitmap, bitnum);
450 		}
451 	}
452 	dump_timeleft = dump_timeout;
453 }
454 
455 /*
456  * Dump the <as, va, pfn> information for a given address space.
457  * SEGOP_DUMP() will call dump_addpage() for each page in the segment.
458  */
459 static void
460 dump_as(struct as *as)
461 {
462 	struct seg *seg;
463 
464 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
465 	for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
466 		if (seg->s_as != as)
467 			break;
468 		if (seg->s_ops == NULL)
469 			continue;
470 		SEGOP_DUMP(seg);
471 	}
472 	AS_LOCK_EXIT(as, &as->a_lock);
473 
474 	if (seg != NULL)
475 		cmn_err(CE_WARN, "invalid segment %p in address space %p",
476 		    (void *)seg, (void *)as);
477 }
478 
479 static int
480 dump_process(pid_t pid)
481 {
482 	proc_t *p = sprlock(pid);
483 
484 	if (p == NULL)
485 		return (-1);
486 	if (p->p_as != &kas) {
487 		mutex_exit(&p->p_lock);
488 		dump_as(p->p_as);
489 		mutex_enter(&p->p_lock);
490 	}
491 
492 	sprunlock(p);
493 
494 	return (0);
495 }
496 
497 void
498 dump_ereports(void)
499 {
500 	u_offset_t dumpvp_start;
501 	erpt_dump_t ed;
502 
503 	if (dumpvp == NULL || dumphdr == NULL)
504 		return;
505 
506 	dumpbuf_cur = dumpbuf_start;
507 	dumpvp_limit = dumpvp_size - (DUMP_OFFSET + DUMP_LOGSIZE);
508 	dumpvp_start = dumpvp_limit - DUMP_ERPTSIZE;
509 	dumpvp_off = dumpvp_start;
510 
511 	fm_ereport_dump();
512 	if (panicstr)
513 		errorq_dump();
514 
515 	bzero(&ed, sizeof (ed)); /* indicate end of ereports */
516 	dumpvp_write(&ed, sizeof (ed));
517 	(void) dumpvp_flush();
518 
519 	if (!panicstr) {
520 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
521 		    (size_t)(dumpvp_off - dumpvp_start),
522 		    B_INVAL | B_FORCE, kcred, NULL);
523 	}
524 }
525 
526 void
527 dump_messages(void)
528 {
529 	log_dump_t ld;
530 	mblk_t *mctl, *mdata;
531 	queue_t *q, *qlast;
532 	u_offset_t dumpvp_start;
533 
534 	if (dumpvp == NULL || dumphdr == NULL || log_consq == NULL)
535 		return;
536 
537 	dumpbuf_cur = dumpbuf_start;
538 	dumpvp_limit = dumpvp_size - DUMP_OFFSET;
539 	dumpvp_start = dumpvp_limit - DUMP_LOGSIZE;
540 	dumpvp_off = dumpvp_start;
541 
542 	qlast = NULL;
543 	do {
544 		for (q = log_consq; q->q_next != qlast; q = q->q_next)
545 			continue;
546 		for (mctl = q->q_first; mctl != NULL; mctl = mctl->b_next) {
547 			dump_timeleft = dump_timeout;
548 			mdata = mctl->b_cont;
549 			ld.ld_magic = LOG_MAGIC;
550 			ld.ld_msgsize = MBLKL(mctl->b_cont);
551 			ld.ld_csum = checksum32(mctl->b_rptr, MBLKL(mctl));
552 			ld.ld_msum = checksum32(mdata->b_rptr, MBLKL(mdata));
553 			dumpvp_write(&ld, sizeof (ld));
554 			dumpvp_write(mctl->b_rptr, MBLKL(mctl));
555 			dumpvp_write(mdata->b_rptr, MBLKL(mdata));
556 		}
557 	} while ((qlast = q) != log_consq);
558 
559 	ld.ld_magic = 0;		/* indicate end of messages */
560 	dumpvp_write(&ld, sizeof (ld));
561 	(void) dumpvp_flush();
562 	if (!panicstr) {
563 		(void) VOP_PUTPAGE(dumpvp, dumpvp_start,
564 		    (size_t)(dumpvp_off - dumpvp_start),
565 		    B_INVAL | B_FORCE, kcred, NULL);
566 	}
567 }
568 
569 static void
570 dump_pagecopy(void *src, void *dst)
571 {
572 	long *wsrc = (long *)src;
573 	long *wdst = (long *)dst;
574 	const ulong_t ncopies = PAGESIZE / sizeof (long);
575 	volatile int w = 0;
576 	volatile int ueoff = -1;
577 	on_trap_data_t otd;
578 
579 	if (on_trap(&otd, OT_DATA_EC)) {
580 		if (ueoff == -1) {
581 			uint64_t pa;
582 
583 			ueoff = w * sizeof (long);
584 			pa = ptob((uint64_t)hat_getpfnum(kas.a_hat, src))
585 			    + ueoff;
586 			cmn_err(CE_WARN, "memory error at PA 0x%08x.%08x",
587 			    (uint32_t)(pa >> 32), (uint32_t)pa);
588 		}
589 #ifdef _LP64
590 		wdst[w++] = 0xbadecc00badecc;
591 #else
592 		wdst[w++] = 0xbadecc;
593 #endif
594 	}
595 	while (w < ncopies) {
596 		wdst[w] = wsrc[w];
597 		w++;
598 	}
599 	no_trap();
600 }
601 
602 /*
603  * Dump the system.
604  */
605 void
606 dumpsys(void)
607 {
608 	pfn_t pfn;
609 	pgcnt_t bitnum;
610 	int npages = 0;
611 	int percent_done = 0;
612 	uint32_t csize;
613 	u_offset_t total_csize = 0;
614 	int compress_ratio;
615 	proc_t *p;
616 	pid_t npids, pidx;
617 	char *content;
618 
619 	if (dumpvp == NULL || dumphdr == NULL) {
620 		uprintf("skipping system dump - no dump device configured\n");
621 		return;
622 	}
623 	dumpbuf_cur = dumpbuf_start;
624 
625 	/*
626 	 * Calculate the starting block for dump.  If we're dumping on a
627 	 * swap device, start 1/5 of the way in; otherwise, start at the
628 	 * beginning.  And never use the first page -- it may be a disk label.
629 	 */
630 	if (dumpvp->v_flag & VISSWAP)
631 		dumphdr->dump_start = P2ROUNDUP(dumpvp_size / 5, DUMP_OFFSET);
632 	else
633 		dumphdr->dump_start = DUMP_OFFSET;
634 
635 	dumphdr->dump_flags = DF_VALID | DF_COMPLETE | DF_LIVE;
636 	dumphdr->dump_crashtime = gethrestime_sec();
637 	dumphdr->dump_npages = 0;
638 	dumphdr->dump_nvtop = 0;
639 	bzero(dump_bitmap, BT_SIZEOFMAP(dump_bitmapsize));
640 	dump_timeleft = dump_timeout;
641 
642 	if (panicstr) {
643 		dumphdr->dump_flags &= ~DF_LIVE;
644 		(void) VOP_DUMPCTL(dumpvp, DUMP_FREE, NULL, NULL);
645 		(void) VOP_DUMPCTL(dumpvp, DUMP_ALLOC, NULL, NULL);
646 		(void) vsnprintf(dumphdr->dump_panicstring, DUMP_PANICSIZE,
647 		    panicstr, panicargs);
648 	}
649 
650 	if (dump_conflags & DUMP_ALL)
651 		content = "all";
652 	else if (dump_conflags & DUMP_CURPROC)
653 		content = "kernel + curproc";
654 	else
655 		content = "kernel";
656 	uprintf("dumping to %s, offset %lld, content: %s\n", dumppath,
657 	    dumphdr->dump_start, content);
658 
659 	/*
660 	 * Leave room for the message and ereport save areas and terminal dump
661 	 * header.
662 	 */
663 	dumpvp_limit = dumpvp_size - DUMP_LOGSIZE - DUMP_OFFSET - DUMP_ERPTSIZE;
664 
665 	/*
666 	 * Write out the symbol table.  It's no longer compressed,
667 	 * so its 'size' and 'csize' are equal.
668 	 */
669 	dumpvp_off = dumphdr->dump_ksyms = dumphdr->dump_start + PAGESIZE;
670 	dumphdr->dump_ksyms_size = dumphdr->dump_ksyms_csize =
671 	    ksyms_snapshot(dumpvp_ksyms_write, NULL, LONG_MAX);
672 
673 	/*
674 	 * Write out the translation map.
675 	 */
676 	dumphdr->dump_map = dumpvp_flush();
677 	dump_as(&kas);
678 	dumphdr->dump_nvtop += dump_plat_addr();
679 
680 	/*
681 	 * call into hat, which may have unmapped pages that also need to
682 	 * be in the dump
683 	 */
684 	hat_dump();
685 
686 	if (dump_conflags & DUMP_ALL) {
687 		mutex_enter(&pidlock);
688 
689 		for (npids = 0, p = practive; p != NULL; p = p->p_next)
690 			dump_pids[npids++] = p->p_pid;
691 
692 		mutex_exit(&pidlock);
693 
694 		for (pidx = 0; pidx < npids; pidx++)
695 			(void) dump_process(dump_pids[pidx]);
696 
697 		for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
698 			dump_timeleft = dump_timeout;
699 			BT_SET(dump_bitmap, bitnum);
700 		}
701 		dumphdr->dump_npages = dump_bitmapsize;
702 		dumphdr->dump_flags |= DF_ALL;
703 
704 	} else if (dump_conflags & DUMP_CURPROC) {
705 		/*
706 		 * Determine which pid is to be dumped.  If we're panicking, we
707 		 * dump the process associated with panic_thread (if any).  If
708 		 * this is a live dump, we dump the process associated with
709 		 * curthread.
710 		 */
711 		npids = 0;
712 		if (panicstr) {
713 			if (panic_thread != NULL &&
714 			    panic_thread->t_procp != NULL &&
715 			    panic_thread->t_procp != &p0) {
716 				dump_pids[npids++] =
717 				    panic_thread->t_procp->p_pid;
718 			}
719 		} else {
720 			dump_pids[npids++] = curthread->t_procp->p_pid;
721 		}
722 
723 		if (npids && dump_process(dump_pids[0]) == 0)
724 			dumphdr->dump_flags |= DF_CURPROC;
725 		else
726 			dumphdr->dump_flags |= DF_KERNEL;
727 
728 	} else {
729 		dumphdr->dump_flags |= DF_KERNEL;
730 	}
731 
732 	dumphdr->dump_hashmask = (1 << highbit(dumphdr->dump_nvtop - 1)) - 1;
733 
734 	/*
735 	 * Write out the pfn table.
736 	 */
737 	dumphdr->dump_pfn = dumpvp_flush();
738 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
739 		dump_timeleft = dump_timeout;
740 		if (!BT_TEST(dump_bitmap, bitnum))
741 			continue;
742 		pfn = dump_bitnum_to_pfn(bitnum);
743 		ASSERT(pfn != PFN_INVALID);
744 		dumpvp_write(&pfn, sizeof (pfn_t));
745 	}
746 	dump_plat_pfn();
747 
748 	/*
749 	 * Write out all the pages.
750 	 */
751 	dumphdr->dump_data = dumpvp_flush();
752 	for (bitnum = 0; bitnum < dump_bitmapsize; bitnum++) {
753 		dump_timeleft = dump_timeout;
754 		if (!BT_TEST(dump_bitmap, bitnum))
755 			continue;
756 		pfn = dump_bitnum_to_pfn(bitnum);
757 		ASSERT(pfn != PFN_INVALID);
758 
759 		/*
760 		 * Map in page frame 'pfn', scan it for UE's while copying
761 		 * the data to dump_uebuf, unmap it, compress dump_uebuf into
762 		 * dump_cbuf, and write out dump_cbuf.  The UE check ensures
763 		 * that we don't lose the whole dump because of a latent UE.
764 		 */
765 		hat_devload(kas.a_hat, dump_cmap, PAGESIZE, pfn, PROT_READ,
766 		    HAT_LOAD_NOCONSIST);
767 		dump_pagecopy(dump_cmap, dump_uebuf);
768 		hat_unload(kas.a_hat, dump_cmap, PAGESIZE, HAT_UNLOAD);
769 		csize = (uint32_t)compress(dump_uebuf, dump_cbuf, PAGESIZE);
770 		dumpvp_write(&csize, sizeof (uint32_t));
771 		dumpvp_write(dump_cbuf, csize);
772 		if (dump_ioerr) {
773 			dumphdr->dump_flags &= ~DF_COMPLETE;
774 			dumphdr->dump_npages = npages;
775 			break;
776 		}
777 		total_csize += csize;
778 		if (++npages * 100LL / dumphdr->dump_npages > percent_done) {
779 			uprintf("^\r%3d%% done", ++percent_done);
780 			if (!panicstr)
781 				delay(1);	/* let the output be sent */
782 		}
783 	}
784 	dumphdr->dump_npages += dump_plat_data(dump_cbuf);
785 
786 	(void) dumpvp_flush();
787 
788 	/*
789 	 * Write out the initial and terminal dump headers.
790 	 */
791 	dumpvp_off = dumphdr->dump_start;
792 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
793 	(void) dumpvp_flush();
794 
795 	dumpvp_limit = dumpvp_size;
796 	dumpvp_off = dumpvp_limit - DUMP_OFFSET;
797 	dumpvp_write(dumphdr, sizeof (dumphdr_t));
798 	(void) dumpvp_flush();
799 
800 	compress_ratio = (int)(100LL * npages / (btopr(total_csize + 1)));
801 
802 	uprintf("\r%3d%% done: %d pages dumped, compression ratio %d.%02d, ",
803 	    percent_done, npages, compress_ratio / 100, compress_ratio % 100);
804 
805 	if (dump_ioerr == 0) {
806 		uprintf("dump succeeded\n");
807 	} else {
808 		uprintf("dump failed: error %d\n", dump_ioerr);
809 		if (panicstr && dumpfaildebug)
810 			debug_enter("dump failed");
811 	}
812 
813 	/*
814 	 * Write out all undelivered messages.  This has to be the *last*
815 	 * thing we do because the dump process itself emits messages.
816 	 */
817 	if (panicstr) {
818 		dump_ereports();
819 		dump_messages();
820 	}
821 
822 	delay(2 * hz);	/* let people see the 'done' message */
823 	dump_timeleft = 0;
824 	dump_ioerr = 0;
825 }
826 
827 /*
828  * This function is called whenever the memory size, as represented
829  * by the phys_install list, changes.
830  */
831 void
832 dump_resize()
833 {
834 	mutex_enter(&dump_lock);
835 	dumphdr_init();
836 	dumpbuf_resize();
837 	mutex_exit(&dump_lock);
838 }
839 
840 /*
841  * This function allows for dynamic resizing of a dump area. It assumes that
842  * the underlying device has update its appropriate size(9P).
843  */
844 int
845 dumpvp_resize()
846 {
847 	int error;
848 	vattr_t vattr;
849 
850 	mutex_enter(&dump_lock);
851 	vattr.va_mask = AT_SIZE;
852 	if ((error = VOP_GETATTR(dumpvp, &vattr, 0, kcred, NULL)) != 0) {
853 		mutex_exit(&dump_lock);
854 		return (error);
855 	}
856 
857 	if (error == 0 && vattr.va_size < 2 * DUMP_LOGSIZE + DUMP_ERPTSIZE) {
858 		mutex_exit(&dump_lock);
859 		return (ENOSPC);
860 	}
861 
862 	dumpvp_size = vattr.va_size & -DUMP_OFFSET;
863 	mutex_exit(&dump_lock);
864 	return (0);
865 }
866