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 /*
22f6e214c7SGavin Maltby * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23*c61ea566SGeorge Wilson * Delphix (c) 2012 by Delphix. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * Dump driver. Provides ioctls to get/set crash dump configuration.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/uio.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
417c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h>
427c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <vm/anon.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/conf.h>
477c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static dev_info_t *dump_devi;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate static int
dump_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)537c478bd9Sstevel@tonic-gate dump_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH)
567c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
577c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "dump", S_IFCHR, 0, DDI_PSEUDO, NULL) ==
587c478bd9Sstevel@tonic-gate DDI_FAILURE) {
597c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
607c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate dump_devi = devi;
637c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate static int
dump_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)677c478bd9Sstevel@tonic-gate dump_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH)
707c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
717c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
727c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
767c478bd9Sstevel@tonic-gate static int
dump_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)777c478bd9Sstevel@tonic-gate dump_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate switch (infocmd) {
807c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
817c478bd9Sstevel@tonic-gate *result = dump_devi;
827c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
837c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
847c478bd9Sstevel@tonic-gate *result = 0;
857c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
917c478bd9Sstevel@tonic-gate int
dump_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * cred,int * rvalp)927c478bd9Sstevel@tonic-gate dump_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate uint64_t size;
957c478bd9Sstevel@tonic-gate uint64_t dumpsize_in_pages;
967c478bd9Sstevel@tonic-gate int error = 0;
977c478bd9Sstevel@tonic-gate char *pathbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
98f6e214c7SGavin Maltby char uuidbuf[36 + 1];
99f6e214c7SGavin Maltby size_t len;
1007c478bd9Sstevel@tonic-gate vnode_t *vp;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate switch (cmd) {
1037c478bd9Sstevel@tonic-gate case DIOCGETDUMPSIZE:
1047c478bd9Sstevel@tonic-gate if (dump_conflags & DUMP_ALL)
1057c478bd9Sstevel@tonic-gate size = ptob((uint64_t)physmem) / DUMP_COMPRESS_RATIO;
1067c478bd9Sstevel@tonic-gate else {
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * We can't give a good answer for the DUMP_CURPROC
1097c478bd9Sstevel@tonic-gate * because we won't know which process to use until it
1107c478bd9Sstevel@tonic-gate * causes a panic. We'll therefore punt and give the
1117c478bd9Sstevel@tonic-gate * caller the size for the kernel.
1127c478bd9Sstevel@tonic-gate *
1137c478bd9Sstevel@tonic-gate * This kernel size equation takes care of the
1147c478bd9Sstevel@tonic-gate * boot time kernel footprint and also accounts
1157c478bd9Sstevel@tonic-gate * for availrmem changes due to user explicit locking.
1167c478bd9Sstevel@tonic-gate * Refer to common/vm/vm_page.c for an explanation
1177c478bd9Sstevel@tonic-gate * of these counters.
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate dumpsize_in_pages = (physinstalled - obp_pages -
1207c478bd9Sstevel@tonic-gate availrmem -
1217c478bd9Sstevel@tonic-gate anon_segkp_pages_locked -
1227c478bd9Sstevel@tonic-gate k_anoninfo.ani_mem_resv -
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);
176e7cbe64fSgw25295 if (vp->v_type == VBLK)
1777c478bd9Sstevel@tonic-gate error = dumpinit(vp, pathbuf, cmd == DIOCTRYDEV);
178e7cbe64fSgw25295 else
179e7cbe64fSgw25295 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
195f6e214c7SGavin Maltby case DIOCSETUUID:
196f6e214c7SGavin Maltby if ((error = copyinstr((char *)arg, uuidbuf, sizeof (uuidbuf),
197f6e214c7SGavin Maltby &len)) != 0)
198f6e214c7SGavin Maltby break;
199f6e214c7SGavin Maltby
200f6e214c7SGavin Maltby if (len != 37) {
201f6e214c7SGavin Maltby error = EINVAL;
202f6e214c7SGavin Maltby break;
203f6e214c7SGavin Maltby }
204f6e214c7SGavin Maltby
205f6e214c7SGavin Maltby error = dump_set_uuid(uuidbuf);
206f6e214c7SGavin Maltby break;
207f6e214c7SGavin Maltby
208f6e214c7SGavin Maltby case DIOCGETUUID:
209f6e214c7SGavin Maltby error = copyoutstr(dump_get_uuid(), (void *)arg, 37, NULL);
210f6e214c7SGavin Maltby break;
211f6e214c7SGavin Maltby
212*c61ea566SGeorge Wilson case DIOCRMDEV:
213*c61ea566SGeorge Wilson mutex_enter(&dump_lock);
214*c61ea566SGeorge Wilson if (dumpvp != NULL)
215*c61ea566SGeorge Wilson dumpfini();
216*c61ea566SGeorge Wilson mutex_exit(&dump_lock);
217*c61ea566SGeorge Wilson break;
218*c61ea566SGeorge Wilson
2197c478bd9Sstevel@tonic-gate default:
2207c478bd9Sstevel@tonic-gate error = ENXIO;
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate kmem_free(pathbuf, MAXPATHLEN);
2247c478bd9Sstevel@tonic-gate return (error);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate struct cb_ops dump_cb_ops = {
2287c478bd9Sstevel@tonic-gate nulldev, /* open */
2297c478bd9Sstevel@tonic-gate nulldev, /* close */
2307c478bd9Sstevel@tonic-gate nodev, /* strategy */
2317c478bd9Sstevel@tonic-gate nodev, /* print */
2327c478bd9Sstevel@tonic-gate nodev, /* dump */
2337c478bd9Sstevel@tonic-gate nodev, /* read */
2347c478bd9Sstevel@tonic-gate nodev, /* write */
2357c478bd9Sstevel@tonic-gate dump_ioctl, /* ioctl */
2367c478bd9Sstevel@tonic-gate nodev, /* devmap */
2377c478bd9Sstevel@tonic-gate nodev, /* mmap */
2387c478bd9Sstevel@tonic-gate nodev, /* segmap */
2397c478bd9Sstevel@tonic-gate nochpoll, /* poll */
2407c478bd9Sstevel@tonic-gate ddi_prop_op, /* prop_op */
2417c478bd9Sstevel@tonic-gate 0, /* streamtab */
2427c478bd9Sstevel@tonic-gate D_NEW|D_MP /* Driver compatibility flag */
2437c478bd9Sstevel@tonic-gate };
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate struct dev_ops dump_ops = {
2467c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
2477c478bd9Sstevel@tonic-gate 0, /* refcnt */
2487c478bd9Sstevel@tonic-gate dump_info, /* info */
2497c478bd9Sstevel@tonic-gate nulldev, /* identify */
2507c478bd9Sstevel@tonic-gate nulldev, /* probe */
2517c478bd9Sstevel@tonic-gate dump_attach, /* attach */
2527c478bd9Sstevel@tonic-gate dump_detach, /* detach */
2537c478bd9Sstevel@tonic-gate nodev, /* reset */
2547c478bd9Sstevel@tonic-gate &dump_cb_ops, /* driver operations */
25519397407SSherry Moore (struct bus_ops *)0, /* bus operations */
25619397407SSherry Moore NULL, /* power */
25719397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
2587c478bd9Sstevel@tonic-gate };
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
26119397407SSherry Moore &mod_driverops, "crash dump driver", &dump_ops,
2627c478bd9Sstevel@tonic-gate };
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2657c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL
2667c478bd9Sstevel@tonic-gate };
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate int
_init(void)2697c478bd9Sstevel@tonic-gate _init(void)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate int
_fini(void)2757c478bd9Sstevel@tonic-gate _fini(void)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2817c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2847c478bd9Sstevel@tonic-gate }
285