10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52191Sszhou * Common Development and Distribution License (the "License").
62191Sszhou * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * The driver portion of kmdb, which manages /dev/kmdb and passes requests along
290Sstevel@tonic-gate * to the kmdb misc module (kmdbmod).
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/modctl.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/open.h>
380Sstevel@tonic-gate #include <sys/kobj.h>
390Sstevel@tonic-gate #include <sys/kdi.h>
400Sstevel@tonic-gate #include <sys/policy.h>
410Sstevel@tonic-gate #include <sys/kobj_impl.h>
420Sstevel@tonic-gate #include <sys/kmdb.h>
430Sstevel@tonic-gate #include <sys/sysmacros.h>
442191Sszhou #include <sys/consdev.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #define KDRV_CFG_MAXLEN 2048
470Sstevel@tonic-gate
480Sstevel@tonic-gate static dev_info_t *kdrv_dip;
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*ARGSUSED*/
510Sstevel@tonic-gate static int
kdrv_open(dev_t * dev,int openflags,int otyp,cred_t * credp)520Sstevel@tonic-gate kdrv_open(dev_t *dev, int openflags, int otyp, cred_t *credp)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate if (otyp != OTYP_CHR)
550Sstevel@tonic-gate return (EINVAL);
560Sstevel@tonic-gate
570Sstevel@tonic-gate if (secpolicy_kmdb(credp) != 0)
580Sstevel@tonic-gate return (EPERM);
590Sstevel@tonic-gate
600Sstevel@tonic-gate return (0);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*ARGSUSED*/
640Sstevel@tonic-gate static int
kdrv_close(dev_t dev,int openflags,int otyp,cred_t * credp)650Sstevel@tonic-gate kdrv_close(dev_t dev, int openflags, int otyp, cred_t *credp)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate return (0);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
700Sstevel@tonic-gate typedef struct kdrv_flags_map {
710Sstevel@tonic-gate const char *fm_name;
720Sstevel@tonic-gate int fm_defval;
730Sstevel@tonic-gate uint_t fm_flag;
740Sstevel@tonic-gate } kdrv_flags_map_t;
750Sstevel@tonic-gate
760Sstevel@tonic-gate static const kdrv_flags_map_t kdrv_flags_map[] = {
770Sstevel@tonic-gate { "kmdb-auto-entry", 1, KMDB_F_AUTO_ENTRY },
780Sstevel@tonic-gate { "kmdb-trap-noswitch", 0, KMDB_F_TRAP_NOSWITCH },
790Sstevel@tonic-gate { "kmdb-driver-debug", 0, KMDB_F_DRV_DEBUG },
800Sstevel@tonic-gate { NULL }
810Sstevel@tonic-gate };
820Sstevel@tonic-gate
830Sstevel@tonic-gate static int
kdrv_activate(intptr_t arg)840Sstevel@tonic-gate kdrv_activate(intptr_t arg)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate uint_t flags;
870Sstevel@tonic-gate size_t memsz;
880Sstevel@tonic-gate char *cfg;
890Sstevel@tonic-gate size_t got;
900Sstevel@tonic-gate int i, rc;
910Sstevel@tonic-gate
923446Smrj #if defined(__x86)
932191Sszhou if (cons_polledio == NULL) {
942191Sszhou cmn_err(CE_NOTE, "kmdb not supported: no console polled I/O");
952191Sszhou return (ENOTSUP);
962191Sszhou }
972191Sszhou #endif
982191Sszhou
990Sstevel@tonic-gate memsz = ddi_prop_get_int(DDI_DEV_T_ANY, kdrv_dip,
1000Sstevel@tonic-gate DDI_PROP_DONTPASS, "kmdb-memseg-size", 0);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate for (flags = 0, i = 0; kdrv_flags_map[i].fm_name != NULL; i++) {
1030Sstevel@tonic-gate const kdrv_flags_map_t *fm = &kdrv_flags_map[i];
1040Sstevel@tonic-gate if (ddi_prop_get_int(DDI_DEV_T_ANY, kdrv_dip, DDI_PROP_DONTPASS,
1050Sstevel@tonic-gate (char *)fm->fm_name, fm->fm_defval))
1060Sstevel@tonic-gate flags |= fm->fm_flag;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate cfg = kmem_alloc(KDRV_CFG_MAXLEN, KM_SLEEP);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if ((rc = copyinstr((caddr_t)arg, cfg, KDRV_CFG_MAXLEN, &got)) != 0) {
1120Sstevel@tonic-gate kmem_free(cfg, KDRV_CFG_MAXLEN);
1130Sstevel@tonic-gate return (rc == ENAMETOOLONG ? E2BIG : EFAULT);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate rc = kctl_modload_activate(memsz, cfg, flags);
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate kmem_free(cfg, KDRV_CFG_MAXLEN);
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate return (rc);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate static int
kdrv_deactivate(void)1240Sstevel@tonic-gate kdrv_deactivate(void)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate return (kctl_deactivate());
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*ARGSUSED*/
1300Sstevel@tonic-gate static int
kdrv_ioctl(dev_t dev,int cmd,intptr_t arg,int flags,cred_t * credp,int * rvalp)1310Sstevel@tonic-gate kdrv_ioctl(dev_t dev, int cmd, intptr_t arg, int flags, cred_t *credp,
1320Sstevel@tonic-gate int *rvalp)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate switch (cmd) {
1350Sstevel@tonic-gate case KMDB_IOC_START:
1360Sstevel@tonic-gate return (kdrv_activate(arg));
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate case KMDB_IOC_STOP:
1390Sstevel@tonic-gate return (kdrv_deactivate());
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate default:
1420Sstevel@tonic-gate return (EINVAL);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /*ARGSUSED*/
1470Sstevel@tonic-gate static int
kdrv_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1480Sstevel@tonic-gate kdrv_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate int error = DDI_SUCCESS;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate switch (infocmd) {
1530Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
1540Sstevel@tonic-gate *result = kdrv_dip;
1550Sstevel@tonic-gate break;
1560Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
1570Sstevel@tonic-gate *result = (void *)(uintptr_t)getminor((dev_t)arg);
1580Sstevel@tonic-gate break;
1590Sstevel@tonic-gate default:
1600Sstevel@tonic-gate *result = NULL;
1610Sstevel@tonic-gate error = DDI_FAILURE;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate return (error);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate static int
kdrv_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1680Sstevel@tonic-gate kdrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate if (cmd != DDI_ATTACH)
1710Sstevel@tonic-gate return (DDI_FAILURE);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (ddi_create_minor_node(dip, ddi_get_name(dip), S_IFCHR,
1740Sstevel@tonic-gate ddi_get_instance(dip), DDI_PSEUDO, 0) != DDI_SUCCESS)
1750Sstevel@tonic-gate return (DDI_FAILURE);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate kdrv_dip = dip;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if (kctl_attach(dip) != 0)
1800Sstevel@tonic-gate return (DDI_FAILURE);
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate return (DDI_SUCCESS);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate static int
kdrv_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1860Sstevel@tonic-gate kdrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate if (cmd != DDI_DETACH)
1890Sstevel@tonic-gate return (DDI_FAILURE);
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate if (kctl_detach() == EBUSY)
1920Sstevel@tonic-gate return (DDI_FAILURE);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate return (DDI_SUCCESS);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate static struct cb_ops kdrv_cb_ops = {
2000Sstevel@tonic-gate kdrv_open,
2010Sstevel@tonic-gate kdrv_close,
2020Sstevel@tonic-gate nodev, /* not a block driver */
2030Sstevel@tonic-gate nodev, /* no print routine */
2040Sstevel@tonic-gate nodev, /* no dump routine */
2050Sstevel@tonic-gate nodev, /* no read routine */
2060Sstevel@tonic-gate nodev, /* no write routine */
2070Sstevel@tonic-gate kdrv_ioctl,
2080Sstevel@tonic-gate nodev, /* no devmap routine */
2090Sstevel@tonic-gate nodev, /* no mmap routine */
2100Sstevel@tonic-gate nodev, /* no segmap routine */
2110Sstevel@tonic-gate nochpoll, /* no chpoll routine */
2120Sstevel@tonic-gate ddi_prop_op,
2130Sstevel@tonic-gate 0, /* not a STREAMS driver */
2140Sstevel@tonic-gate D_NEW | D_MP, /* safe for multi-thread/multi-processor */
2150Sstevel@tonic-gate };
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate static struct dev_ops kdrv_ops = {
2180Sstevel@tonic-gate DEVO_REV, /* devo_rev */
2190Sstevel@tonic-gate 0, /* devo_refcnt */
2200Sstevel@tonic-gate kdrv_getinfo, /* devo_getinfo */
2210Sstevel@tonic-gate nulldev, /* devo_identify */
2220Sstevel@tonic-gate nulldev, /* devo_probe */
2230Sstevel@tonic-gate kdrv_attach, /* devo_attach */
2240Sstevel@tonic-gate kdrv_detach, /* devo_detach */
2250Sstevel@tonic-gate nodev, /* devo_reset */
2260Sstevel@tonic-gate &kdrv_cb_ops, /* devo_cb_ops */
2270Sstevel@tonic-gate (struct bus_ops *)0, /* devo_bus_ops */
2280Sstevel@tonic-gate NULL, /* devo_power */
229*7656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* devo_quiesce */
2300Sstevel@tonic-gate };
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate static struct modldrv modldrv = {
2330Sstevel@tonic-gate &mod_driverops,
234*7656SSherry.Moore@Sun.COM "kmdb driver",
2350Sstevel@tonic-gate &kdrv_ops
2360Sstevel@tonic-gate };
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate static struct modlinkage modlinkage = {
2390Sstevel@tonic-gate MODREV_1,
2400Sstevel@tonic-gate (void *)&modldrv,
2410Sstevel@tonic-gate NULL
2420Sstevel@tonic-gate };
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate int
_init(void)2450Sstevel@tonic-gate _init(void)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate return (mod_install(&modlinkage));
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2510Sstevel@tonic-gate _info(struct modinfo *modinfop)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate int
_fini(void)2570Sstevel@tonic-gate _fini(void)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate return (mod_remove(&modlinkage));
2600Sstevel@tonic-gate }
261