xref: /titanic_53/usr/src/uts/common/io/dump.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c151b7faSsp92102  * Common Development and Distribution License (the "License").
6c151b7faSsp92102  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e7cbe64fSgw25295  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Dump driver.  Provides ioctls to get/set crash dump configuration.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
367c478bd9Sstevel@tonic-gate #include <sys/uio.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
427c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h>
437c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <vm/anon.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/conf.h>
487c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
497c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static dev_info_t *dump_devi;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static int
547c478bd9Sstevel@tonic-gate dump_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
587c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "dump", S_IFCHR, 0, DDI_PSEUDO, NULL) ==
597c478bd9Sstevel@tonic-gate 	    DDI_FAILURE) {
607c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
617c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	dump_devi = devi;
647c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static int
687c478bd9Sstevel@tonic-gate dump_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
717c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
727c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
777c478bd9Sstevel@tonic-gate static int
787c478bd9Sstevel@tonic-gate dump_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	switch (infocmd) {
817c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
827c478bd9Sstevel@tonic-gate 		*result = dump_devi;
837c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
847c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
857c478bd9Sstevel@tonic-gate 		*result = 0;
867c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
927c478bd9Sstevel@tonic-gate int
937c478bd9Sstevel@tonic-gate dump_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	uint64_t size;
967c478bd9Sstevel@tonic-gate 	uint64_t dumpsize_in_pages;
977c478bd9Sstevel@tonic-gate 	int error = 0;
987c478bd9Sstevel@tonic-gate 	char *pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
997c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	switch (cmd) {
1027c478bd9Sstevel@tonic-gate 	case DIOCGETDUMPSIZE:
1037c478bd9Sstevel@tonic-gate 		if (dump_conflags & DUMP_ALL)
1047c478bd9Sstevel@tonic-gate 			size = ptob((uint64_t)physmem) / DUMP_COMPRESS_RATIO;
1057c478bd9Sstevel@tonic-gate 		else {
1067c478bd9Sstevel@tonic-gate 			/*
1077c478bd9Sstevel@tonic-gate 			 * We can't give a good answer for the DUMP_CURPROC
1087c478bd9Sstevel@tonic-gate 			 * because we won't know which process to use until it
1097c478bd9Sstevel@tonic-gate 			 * causes a panic.  We'll therefore punt and give the
1107c478bd9Sstevel@tonic-gate 			 * caller the size for the kernel.
1117c478bd9Sstevel@tonic-gate 			 *
1127c478bd9Sstevel@tonic-gate 			 * This kernel size equation takes care of the
1137c478bd9Sstevel@tonic-gate 			 * boot time kernel footprint and also accounts
1147c478bd9Sstevel@tonic-gate 			 * for availrmem changes due to user explicit locking.
1157c478bd9Sstevel@tonic-gate 			 * Refer to common/vm/vm_page.c for an explanation
1167c478bd9Sstevel@tonic-gate 			 * of these counters.
1177c478bd9Sstevel@tonic-gate 			 */
1187c478bd9Sstevel@tonic-gate 			dumpsize_in_pages = (physinstalled - obp_pages -
1197c478bd9Sstevel@tonic-gate 						availrmem -
1207c478bd9Sstevel@tonic-gate 						anon_segkp_pages_locked -
1217c478bd9Sstevel@tonic-gate 						k_anoninfo.ani_mem_resv -
1227c478bd9Sstevel@tonic-gate 						segvn_pages_locked -
1237c478bd9Sstevel@tonic-gate 						pages_locked -
1247c478bd9Sstevel@tonic-gate 						pages_claimed -
1257c478bd9Sstevel@tonic-gate 						pages_useclaim);
1267c478bd9Sstevel@tonic-gate 
127c151b7faSsp92102 			/*
128c151b7faSsp92102 			 * Protect against vm vagaries.
129c151b7faSsp92102 			 */
130c151b7faSsp92102 			if (dumpsize_in_pages > (uint64_t)physmem)
131c151b7faSsp92102 				dumpsize_in_pages = (uint64_t)physmem;
132c151b7faSsp92102 
1337c478bd9Sstevel@tonic-gate 			size = ptob(dumpsize_in_pages) / DUMP_COMPRESS_RATIO;
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 		if (copyout(&size, (void *)arg, sizeof (size)) < 0)
1367c478bd9Sstevel@tonic-gate 			error = EFAULT;
1377c478bd9Sstevel@tonic-gate 		break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	case DIOCGETCONF:
1407c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1417c478bd9Sstevel@tonic-gate 		*rvalp = dump_conflags;
1427c478bd9Sstevel@tonic-gate 		if (dumpvp && !(dumpvp->v_flag & VISSWAP))
1437c478bd9Sstevel@tonic-gate 			*rvalp |= DUMP_EXCL;
1447c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1457c478bd9Sstevel@tonic-gate 		break;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	case DIOCSETCONF:
1487c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1497c478bd9Sstevel@tonic-gate 		if (arg == DUMP_KERNEL || arg == DUMP_ALL ||
1507c478bd9Sstevel@tonic-gate 		    arg == DUMP_CURPROC)
1517c478bd9Sstevel@tonic-gate 			dump_conflags = arg;
1527c478bd9Sstevel@tonic-gate 		else
1537c478bd9Sstevel@tonic-gate 			error = EINVAL;
1547c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1557c478bd9Sstevel@tonic-gate 		break;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	case DIOCGETDEV:
1587c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1597c478bd9Sstevel@tonic-gate 		if (dumppath == NULL) {
1607c478bd9Sstevel@tonic-gate 			mutex_exit(&dump_lock);
1617c478bd9Sstevel@tonic-gate 			error = ENODEV;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 		(void) strcpy(pathbuf, dumppath);
1657c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1667c478bd9Sstevel@tonic-gate 		error = copyoutstr(pathbuf, (void *)arg, MAXPATHLEN, NULL);
1677c478bd9Sstevel@tonic-gate 		break;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	case DIOCSETDEV:
1707c478bd9Sstevel@tonic-gate 	case DIOCTRYDEV:
1717c478bd9Sstevel@tonic-gate 		if ((error = copyinstr((char *)arg, pathbuf, MAXPATHLEN,
1727c478bd9Sstevel@tonic-gate 		    NULL)) != 0 || (error = lookupname(pathbuf, UIO_SYSSPACE,
1737c478bd9Sstevel@tonic-gate 		    FOLLOW, NULLVPP, &vp)) != 0)
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
176*e7cbe64fSgw25295 		if (vp->v_type == VBLK)
1777c478bd9Sstevel@tonic-gate 			error = dumpinit(vp, pathbuf, cmd == DIOCTRYDEV);
178*e7cbe64fSgw25295 		else
179*e7cbe64fSgw25295 			error = ENOTBLK;
1807c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1817c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1827c478bd9Sstevel@tonic-gate 		break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	case DIOCDUMP:
1857c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1867c478bd9Sstevel@tonic-gate 		if (dumpvp == NULL)
1877c478bd9Sstevel@tonic-gate 			error = ENODEV;
1887c478bd9Sstevel@tonic-gate 		else if (dumpvp->v_flag & VISSWAP)
1897c478bd9Sstevel@tonic-gate 			error = EBUSY;
1907c478bd9Sstevel@tonic-gate 		else
1917c478bd9Sstevel@tonic-gate 			dumpsys();
1927c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1937c478bd9Sstevel@tonic-gate 		break;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	default:
1967c478bd9Sstevel@tonic-gate 		error = ENXIO;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	kmem_free(pathbuf, MAXPATHLEN);
2007c478bd9Sstevel@tonic-gate 	return (error);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate struct cb_ops dump_cb_ops = {
2047c478bd9Sstevel@tonic-gate 	nulldev,		/* open */
2057c478bd9Sstevel@tonic-gate 	nulldev,		/* close */
2067c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
2077c478bd9Sstevel@tonic-gate 	nodev,			/* print */
2087c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
2097c478bd9Sstevel@tonic-gate 	nodev,			/* read */
2107c478bd9Sstevel@tonic-gate 	nodev,			/* write */
2117c478bd9Sstevel@tonic-gate 	dump_ioctl,		/* ioctl */
2127c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
2137c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
2147c478bd9Sstevel@tonic-gate 	nodev, 			/* segmap */
2157c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
2167c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
2177c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
2187c478bd9Sstevel@tonic-gate 	D_NEW|D_MP		/* Driver compatibility flag */
2197c478bd9Sstevel@tonic-gate };
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate struct dev_ops dump_ops = {
2227c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
2237c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
2247c478bd9Sstevel@tonic-gate 	dump_info,		/* info */
2257c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
2267c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
2277c478bd9Sstevel@tonic-gate 	dump_attach,		/* attach */
2287c478bd9Sstevel@tonic-gate 	dump_detach,		/* detach */
2297c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
2307c478bd9Sstevel@tonic-gate 	&dump_cb_ops,		/* driver operations */
2317c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations */
2327c478bd9Sstevel@tonic-gate };
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2357c478bd9Sstevel@tonic-gate 	&mod_driverops, "crash dump driver %I%", &dump_ops,
2367c478bd9Sstevel@tonic-gate };
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2397c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
2407c478bd9Sstevel@tonic-gate };
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate int
2437c478bd9Sstevel@tonic-gate _init(void)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate int
2497c478bd9Sstevel@tonic-gate _fini(void)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate int
2557c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2587c478bd9Sstevel@tonic-gate }
259