xref: /titanic_53/usr/src/uts/common/io/dump.c (revision f6e214c7418f43af38bd8c3a557e3d0a1d311cfa)
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*f6e214c7SGavin Maltby  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Dump driver.  Provides ioctls to get/set crash dump configuration.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
347c478bd9Sstevel@tonic-gate #include <sys/uio.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
407c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h>
417c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate #include <vm/anon.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/conf.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static dev_info_t *dump_devi;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int
527c478bd9Sstevel@tonic-gate dump_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
557c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
567c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "dump", S_IFCHR, 0, DDI_PSEUDO, NULL) ==
577c478bd9Sstevel@tonic-gate 	    DDI_FAILURE) {
587c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 	dump_devi = devi;
627c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int
667c478bd9Sstevel@tonic-gate dump_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
707c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
717c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
757c478bd9Sstevel@tonic-gate static int
767c478bd9Sstevel@tonic-gate dump_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	switch (infocmd) {
797c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
807c478bd9Sstevel@tonic-gate 		*result = dump_devi;
817c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
827c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
837c478bd9Sstevel@tonic-gate 		*result = 0;
847c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
907c478bd9Sstevel@tonic-gate int
917c478bd9Sstevel@tonic-gate dump_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	uint64_t size;
947c478bd9Sstevel@tonic-gate 	uint64_t dumpsize_in_pages;
957c478bd9Sstevel@tonic-gate 	int error = 0;
967c478bd9Sstevel@tonic-gate 	char *pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
97*f6e214c7SGavin Maltby 	char uuidbuf[36 + 1];
98*f6e214c7SGavin Maltby 	size_t len;
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 			    pages_locked -
1237c478bd9Sstevel@tonic-gate 			    pages_claimed -
1247c478bd9Sstevel@tonic-gate 			    pages_useclaim);
1257c478bd9Sstevel@tonic-gate 
126c151b7faSsp92102 			/*
127c151b7faSsp92102 			 * Protect against vm vagaries.
128c151b7faSsp92102 			 */
129c151b7faSsp92102 			if (dumpsize_in_pages > (uint64_t)physmem)
130c151b7faSsp92102 				dumpsize_in_pages = (uint64_t)physmem;
131c151b7faSsp92102 
1327c478bd9Sstevel@tonic-gate 			size = ptob(dumpsize_in_pages) / DUMP_COMPRESS_RATIO;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		if (copyout(&size, (void *)arg, sizeof (size)) < 0)
1357c478bd9Sstevel@tonic-gate 			error = EFAULT;
1367c478bd9Sstevel@tonic-gate 		break;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	case DIOCGETCONF:
1397c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1407c478bd9Sstevel@tonic-gate 		*rvalp = dump_conflags;
1417c478bd9Sstevel@tonic-gate 		if (dumpvp && !(dumpvp->v_flag & VISSWAP))
1427c478bd9Sstevel@tonic-gate 			*rvalp |= DUMP_EXCL;
1437c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1447c478bd9Sstevel@tonic-gate 		break;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	case DIOCSETCONF:
1477c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1487c478bd9Sstevel@tonic-gate 		if (arg == DUMP_KERNEL || arg == DUMP_ALL ||
1497c478bd9Sstevel@tonic-gate 		    arg == DUMP_CURPROC)
1507c478bd9Sstevel@tonic-gate 			dump_conflags = arg;
1517c478bd9Sstevel@tonic-gate 		else
1527c478bd9Sstevel@tonic-gate 			error = EINVAL;
1537c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1547c478bd9Sstevel@tonic-gate 		break;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	case DIOCGETDEV:
1577c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1587c478bd9Sstevel@tonic-gate 		if (dumppath == NULL) {
1597c478bd9Sstevel@tonic-gate 			mutex_exit(&dump_lock);
1607c478bd9Sstevel@tonic-gate 			error = ENODEV;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 		(void) strcpy(pathbuf, dumppath);
1647c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1657c478bd9Sstevel@tonic-gate 		error = copyoutstr(pathbuf, (void *)arg, MAXPATHLEN, NULL);
1667c478bd9Sstevel@tonic-gate 		break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	case DIOCSETDEV:
1697c478bd9Sstevel@tonic-gate 	case DIOCTRYDEV:
1707c478bd9Sstevel@tonic-gate 		if ((error = copyinstr((char *)arg, pathbuf, MAXPATHLEN,
1717c478bd9Sstevel@tonic-gate 		    NULL)) != 0 || (error = lookupname(pathbuf, UIO_SYSSPACE,
1727c478bd9Sstevel@tonic-gate 		    FOLLOW, NULLVPP, &vp)) != 0)
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
175e7cbe64fSgw25295 		if (vp->v_type == VBLK)
1767c478bd9Sstevel@tonic-gate 			error = dumpinit(vp, pathbuf, cmd == DIOCTRYDEV);
177e7cbe64fSgw25295 		else
178e7cbe64fSgw25295 			error = ENOTBLK;
1797c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1807c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1817c478bd9Sstevel@tonic-gate 		break;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	case DIOCDUMP:
1847c478bd9Sstevel@tonic-gate 		mutex_enter(&dump_lock);
1857c478bd9Sstevel@tonic-gate 		if (dumpvp == NULL)
1867c478bd9Sstevel@tonic-gate 			error = ENODEV;
1877c478bd9Sstevel@tonic-gate 		else if (dumpvp->v_flag & VISSWAP)
1887c478bd9Sstevel@tonic-gate 			error = EBUSY;
1897c478bd9Sstevel@tonic-gate 		else
1907c478bd9Sstevel@tonic-gate 			dumpsys();
1917c478bd9Sstevel@tonic-gate 		mutex_exit(&dump_lock);
1927c478bd9Sstevel@tonic-gate 		break;
1937c478bd9Sstevel@tonic-gate 
194*f6e214c7SGavin Maltby 	case DIOCSETUUID:
195*f6e214c7SGavin Maltby 		if ((error = copyinstr((char *)arg, uuidbuf, sizeof (uuidbuf),
196*f6e214c7SGavin Maltby 		    &len)) != 0)
197*f6e214c7SGavin Maltby 			break;
198*f6e214c7SGavin Maltby 
199*f6e214c7SGavin Maltby 		if (len != 37) {
200*f6e214c7SGavin Maltby 			error = EINVAL;
201*f6e214c7SGavin Maltby 			break;
202*f6e214c7SGavin Maltby 		}
203*f6e214c7SGavin Maltby 
204*f6e214c7SGavin Maltby 		error = dump_set_uuid(uuidbuf);
205*f6e214c7SGavin Maltby 		break;
206*f6e214c7SGavin Maltby 
207*f6e214c7SGavin Maltby 	case DIOCGETUUID:
208*f6e214c7SGavin Maltby 		error = copyoutstr(dump_get_uuid(), (void *)arg, 37, NULL);
209*f6e214c7SGavin Maltby 		break;
210*f6e214c7SGavin Maltby 
2117c478bd9Sstevel@tonic-gate 	default:
2127c478bd9Sstevel@tonic-gate 		error = ENXIO;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	kmem_free(pathbuf, MAXPATHLEN);
2167c478bd9Sstevel@tonic-gate 	return (error);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate struct cb_ops dump_cb_ops = {
2207c478bd9Sstevel@tonic-gate 	nulldev,		/* open */
2217c478bd9Sstevel@tonic-gate 	nulldev,		/* close */
2227c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
2237c478bd9Sstevel@tonic-gate 	nodev,			/* print */
2247c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
2257c478bd9Sstevel@tonic-gate 	nodev,			/* read */
2267c478bd9Sstevel@tonic-gate 	nodev,			/* write */
2277c478bd9Sstevel@tonic-gate 	dump_ioctl,		/* ioctl */
2287c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
2297c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
2307c478bd9Sstevel@tonic-gate 	nodev, 			/* segmap */
2317c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
2327c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
2337c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
2347c478bd9Sstevel@tonic-gate 	D_NEW|D_MP		/* Driver compatibility flag */
2357c478bd9Sstevel@tonic-gate };
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate struct dev_ops dump_ops = {
2387c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
2397c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
2407c478bd9Sstevel@tonic-gate 	dump_info,		/* info */
2417c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
2427c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
2437c478bd9Sstevel@tonic-gate 	dump_attach,		/* attach */
2447c478bd9Sstevel@tonic-gate 	dump_detach,		/* detach */
2457c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
2467c478bd9Sstevel@tonic-gate 	&dump_cb_ops,		/* driver operations */
24719397407SSherry Moore 	(struct bus_ops *)0,	/* bus operations */
24819397407SSherry Moore 	NULL,			/* power */
24919397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
2507c478bd9Sstevel@tonic-gate };
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
25319397407SSherry Moore 	&mod_driverops, "crash dump driver", &dump_ops,
2547c478bd9Sstevel@tonic-gate };
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2577c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
2587c478bd9Sstevel@tonic-gate };
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate int
2617c478bd9Sstevel@tonic-gate _init(void)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate int
2677c478bd9Sstevel@tonic-gate _fini(void)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate int
2737c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2767c478bd9Sstevel@tonic-gate }
277