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 528cdc3d7Sszhou * Common Development and Distribution License (the "License"). 628cdc3d7Sszhou * 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*19397407SSherry Moore * 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 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * The driver portion of kmdb, which manages /dev/kmdb and passes requests along 297c478bd9Sstevel@tonic-gate * to the kmdb misc module (kmdbmod). 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/conf.h> 337c478bd9Sstevel@tonic-gate #include <sys/stat.h> 347c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 357c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 377c478bd9Sstevel@tonic-gate #include <sys/open.h> 387c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 397c478bd9Sstevel@tonic-gate #include <sys/kdi.h> 407c478bd9Sstevel@tonic-gate #include <sys/policy.h> 417c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h> 427c478bd9Sstevel@tonic-gate #include <sys/kmdb.h> 437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 4428cdc3d7Sszhou #include <sys/consdev.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #define KDRV_CFG_MAXLEN 2048 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate static dev_info_t *kdrv_dip; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 517c478bd9Sstevel@tonic-gate static int 527c478bd9Sstevel@tonic-gate kdrv_open(dev_t *dev, int openflags, int otyp, cred_t *credp) 537c478bd9Sstevel@tonic-gate { 547c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 557c478bd9Sstevel@tonic-gate return (EINVAL); 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate if (secpolicy_kmdb(credp) != 0) 587c478bd9Sstevel@tonic-gate return (EPERM); 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate return (0); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 647c478bd9Sstevel@tonic-gate static int 657c478bd9Sstevel@tonic-gate kdrv_close(dev_t dev, int openflags, int otyp, cred_t *credp) 667c478bd9Sstevel@tonic-gate { 677c478bd9Sstevel@tonic-gate return (0); 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate typedef struct kdrv_flags_map { 717c478bd9Sstevel@tonic-gate const char *fm_name; 727c478bd9Sstevel@tonic-gate int fm_defval; 737c478bd9Sstevel@tonic-gate uint_t fm_flag; 747c478bd9Sstevel@tonic-gate } kdrv_flags_map_t; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static const kdrv_flags_map_t kdrv_flags_map[] = { 777c478bd9Sstevel@tonic-gate { "kmdb-auto-entry", 1, KMDB_F_AUTO_ENTRY }, 787c478bd9Sstevel@tonic-gate { "kmdb-trap-noswitch", 0, KMDB_F_TRAP_NOSWITCH }, 797c478bd9Sstevel@tonic-gate { "kmdb-driver-debug", 0, KMDB_F_DRV_DEBUG }, 807c478bd9Sstevel@tonic-gate { NULL } 817c478bd9Sstevel@tonic-gate }; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static int 847c478bd9Sstevel@tonic-gate kdrv_activate(intptr_t arg) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate uint_t flags; 877c478bd9Sstevel@tonic-gate size_t memsz; 887c478bd9Sstevel@tonic-gate char *cfg; 897c478bd9Sstevel@tonic-gate size_t got; 907c478bd9Sstevel@tonic-gate int i, rc; 917c478bd9Sstevel@tonic-gate 92ae115bc7Smrj #if defined(__x86) 9328cdc3d7Sszhou if (cons_polledio == NULL) { 9428cdc3d7Sszhou cmn_err(CE_NOTE, "kmdb not supported: no console polled I/O"); 9528cdc3d7Sszhou return (ENOTSUP); 9628cdc3d7Sszhou } 9728cdc3d7Sszhou #endif 9828cdc3d7Sszhou 997c478bd9Sstevel@tonic-gate memsz = ddi_prop_get_int(DDI_DEV_T_ANY, kdrv_dip, 1007c478bd9Sstevel@tonic-gate DDI_PROP_DONTPASS, "kmdb-memseg-size", 0); 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate for (flags = 0, i = 0; kdrv_flags_map[i].fm_name != NULL; i++) { 1037c478bd9Sstevel@tonic-gate const kdrv_flags_map_t *fm = &kdrv_flags_map[i]; 1047c478bd9Sstevel@tonic-gate if (ddi_prop_get_int(DDI_DEV_T_ANY, kdrv_dip, DDI_PROP_DONTPASS, 1057c478bd9Sstevel@tonic-gate (char *)fm->fm_name, fm->fm_defval)) 1067c478bd9Sstevel@tonic-gate flags |= fm->fm_flag; 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate cfg = kmem_alloc(KDRV_CFG_MAXLEN, KM_SLEEP); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate if ((rc = copyinstr((caddr_t)arg, cfg, KDRV_CFG_MAXLEN, &got)) != 0) { 1127c478bd9Sstevel@tonic-gate kmem_free(cfg, KDRV_CFG_MAXLEN); 1137c478bd9Sstevel@tonic-gate return (rc == ENAMETOOLONG ? E2BIG : EFAULT); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate rc = kctl_modload_activate(memsz, cfg, flags); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate kmem_free(cfg, KDRV_CFG_MAXLEN); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate return (rc); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate static int 1247c478bd9Sstevel@tonic-gate kdrv_deactivate(void) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate return (kctl_deactivate()); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1307c478bd9Sstevel@tonic-gate static int 1317c478bd9Sstevel@tonic-gate kdrv_ioctl(dev_t dev, int cmd, intptr_t arg, int flags, cred_t *credp, 1327c478bd9Sstevel@tonic-gate int *rvalp) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate switch (cmd) { 1357c478bd9Sstevel@tonic-gate case KMDB_IOC_START: 1367c478bd9Sstevel@tonic-gate return (kdrv_activate(arg)); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate case KMDB_IOC_STOP: 1397c478bd9Sstevel@tonic-gate return (kdrv_deactivate()); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate default: 1427c478bd9Sstevel@tonic-gate return (EINVAL); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1477c478bd9Sstevel@tonic-gate static int 1487c478bd9Sstevel@tonic-gate kdrv_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1497c478bd9Sstevel@tonic-gate { 1507c478bd9Sstevel@tonic-gate int error = DDI_SUCCESS; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate switch (infocmd) { 1537c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1547c478bd9Sstevel@tonic-gate *result = kdrv_dip; 1557c478bd9Sstevel@tonic-gate break; 1567c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1577c478bd9Sstevel@tonic-gate *result = (void *)(uintptr_t)getminor((dev_t)arg); 1587c478bd9Sstevel@tonic-gate break; 1597c478bd9Sstevel@tonic-gate default: 1607c478bd9Sstevel@tonic-gate *result = NULL; 1617c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate return (error); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate static int 1687c478bd9Sstevel@tonic-gate kdrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH) 1717c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, ddi_get_name(dip), S_IFCHR, 1747c478bd9Sstevel@tonic-gate ddi_get_instance(dip), DDI_PSEUDO, 0) != DDI_SUCCESS) 1757c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate kdrv_dip = dip; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (kctl_attach(dip) != 0) 1807c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate static int 1867c478bd9Sstevel@tonic-gate kdrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) 1897c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate if (kctl_detach() == EBUSY) 1927c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate static struct cb_ops kdrv_cb_ops = { 2007c478bd9Sstevel@tonic-gate kdrv_open, 2017c478bd9Sstevel@tonic-gate kdrv_close, 2027c478bd9Sstevel@tonic-gate nodev, /* not a block driver */ 2037c478bd9Sstevel@tonic-gate nodev, /* no print routine */ 2047c478bd9Sstevel@tonic-gate nodev, /* no dump routine */ 2057c478bd9Sstevel@tonic-gate nodev, /* no read routine */ 2067c478bd9Sstevel@tonic-gate nodev, /* no write routine */ 2077c478bd9Sstevel@tonic-gate kdrv_ioctl, 2087c478bd9Sstevel@tonic-gate nodev, /* no devmap routine */ 2097c478bd9Sstevel@tonic-gate nodev, /* no mmap routine */ 2107c478bd9Sstevel@tonic-gate nodev, /* no segmap routine */ 2117c478bd9Sstevel@tonic-gate nochpoll, /* no chpoll routine */ 2127c478bd9Sstevel@tonic-gate ddi_prop_op, 2137c478bd9Sstevel@tonic-gate 0, /* not a STREAMS driver */ 2147c478bd9Sstevel@tonic-gate D_NEW | D_MP, /* safe for multi-thread/multi-processor */ 2157c478bd9Sstevel@tonic-gate }; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate static struct dev_ops kdrv_ops = { 2187c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 2197c478bd9Sstevel@tonic-gate 0, /* devo_refcnt */ 2207c478bd9Sstevel@tonic-gate kdrv_getinfo, /* devo_getinfo */ 2217c478bd9Sstevel@tonic-gate nulldev, /* devo_identify */ 2227c478bd9Sstevel@tonic-gate nulldev, /* devo_probe */ 2237c478bd9Sstevel@tonic-gate kdrv_attach, /* devo_attach */ 2247c478bd9Sstevel@tonic-gate kdrv_detach, /* devo_detach */ 2257c478bd9Sstevel@tonic-gate nodev, /* devo_reset */ 2267c478bd9Sstevel@tonic-gate &kdrv_cb_ops, /* devo_cb_ops */ 2277c478bd9Sstevel@tonic-gate (struct bus_ops *)0, /* devo_bus_ops */ 2287c478bd9Sstevel@tonic-gate NULL, /* devo_power */ 229*19397407SSherry Moore ddi_quiesce_not_needed, /* devo_quiesce */ 2307c478bd9Sstevel@tonic-gate }; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 2337c478bd9Sstevel@tonic-gate &mod_driverops, 234*19397407SSherry Moore "kmdb driver", 2357c478bd9Sstevel@tonic-gate &kdrv_ops 2367c478bd9Sstevel@tonic-gate }; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 2397c478bd9Sstevel@tonic-gate MODREV_1, 2407c478bd9Sstevel@tonic-gate (void *)&modldrv, 2417c478bd9Sstevel@tonic-gate NULL 2427c478bd9Sstevel@tonic-gate }; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate int 2457c478bd9Sstevel@tonic-gate _init(void) 2467c478bd9Sstevel@tonic-gate { 2477c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate int 2517c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 2527c478bd9Sstevel@tonic-gate { 2537c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate int 2577c478bd9Sstevel@tonic-gate _fini(void) 2587c478bd9Sstevel@tonic-gate { 2597c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 2607c478bd9Sstevel@tonic-gate } 261