1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * The driver portion of kmdb, which manages /dev/kmdb and passes requests along 31*0Sstevel@tonic-gate * to the kmdb misc module (kmdbmod). 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #include <sys/conf.h> 35*0Sstevel@tonic-gate #include <sys/stat.h> 36*0Sstevel@tonic-gate #include <sys/modctl.h> 37*0Sstevel@tonic-gate #include <sys/ddi.h> 38*0Sstevel@tonic-gate #include <sys/sunddi.h> 39*0Sstevel@tonic-gate #include <sys/open.h> 40*0Sstevel@tonic-gate #include <sys/kobj.h> 41*0Sstevel@tonic-gate #include <sys/kdi.h> 42*0Sstevel@tonic-gate #include <sys/policy.h> 43*0Sstevel@tonic-gate #include <sys/kobj_impl.h> 44*0Sstevel@tonic-gate #include <sys/kmdb.h> 45*0Sstevel@tonic-gate #include <sys/sysmacros.h> 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate #define KDRV_CFG_MAXLEN 2048 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate static dev_info_t *kdrv_dip; 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /*ARGSUSED*/ 52*0Sstevel@tonic-gate static int 53*0Sstevel@tonic-gate kdrv_open(dev_t *dev, int openflags, int otyp, cred_t *credp) 54*0Sstevel@tonic-gate { 55*0Sstevel@tonic-gate if (otyp != OTYP_CHR) 56*0Sstevel@tonic-gate return (EINVAL); 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate if (secpolicy_kmdb(credp) != 0) 59*0Sstevel@tonic-gate return (EPERM); 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate return (0); 62*0Sstevel@tonic-gate } 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /*ARGSUSED*/ 65*0Sstevel@tonic-gate static int 66*0Sstevel@tonic-gate kdrv_close(dev_t dev, int openflags, int otyp, cred_t *credp) 67*0Sstevel@tonic-gate { 68*0Sstevel@tonic-gate return (0); 69*0Sstevel@tonic-gate } 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate typedef struct kdrv_flags_map { 72*0Sstevel@tonic-gate const char *fm_name; 73*0Sstevel@tonic-gate int fm_defval; 74*0Sstevel@tonic-gate uint_t fm_flag; 75*0Sstevel@tonic-gate } kdrv_flags_map_t; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate static const kdrv_flags_map_t kdrv_flags_map[] = { 78*0Sstevel@tonic-gate { "kmdb-auto-entry", 1, KMDB_F_AUTO_ENTRY }, 79*0Sstevel@tonic-gate { "kmdb-trap-noswitch", 0, KMDB_F_TRAP_NOSWITCH }, 80*0Sstevel@tonic-gate { "kmdb-driver-debug", 0, KMDB_F_DRV_DEBUG }, 81*0Sstevel@tonic-gate { NULL } 82*0Sstevel@tonic-gate }; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate static int 85*0Sstevel@tonic-gate kdrv_activate(intptr_t arg) 86*0Sstevel@tonic-gate { 87*0Sstevel@tonic-gate uint_t flags; 88*0Sstevel@tonic-gate size_t memsz; 89*0Sstevel@tonic-gate char *cfg; 90*0Sstevel@tonic-gate size_t got; 91*0Sstevel@tonic-gate int i, rc; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate memsz = ddi_prop_get_int(DDI_DEV_T_ANY, kdrv_dip, 94*0Sstevel@tonic-gate DDI_PROP_DONTPASS, "kmdb-memseg-size", 0); 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate for (flags = 0, i = 0; kdrv_flags_map[i].fm_name != NULL; i++) { 97*0Sstevel@tonic-gate const kdrv_flags_map_t *fm = &kdrv_flags_map[i]; 98*0Sstevel@tonic-gate if (ddi_prop_get_int(DDI_DEV_T_ANY, kdrv_dip, DDI_PROP_DONTPASS, 99*0Sstevel@tonic-gate (char *)fm->fm_name, fm->fm_defval)) 100*0Sstevel@tonic-gate flags |= fm->fm_flag; 101*0Sstevel@tonic-gate } 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate cfg = kmem_alloc(KDRV_CFG_MAXLEN, KM_SLEEP); 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate if ((rc = copyinstr((caddr_t)arg, cfg, KDRV_CFG_MAXLEN, &got)) != 0) { 106*0Sstevel@tonic-gate kmem_free(cfg, KDRV_CFG_MAXLEN); 107*0Sstevel@tonic-gate return (rc == ENAMETOOLONG ? E2BIG : EFAULT); 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate rc = kctl_modload_activate(memsz, cfg, flags); 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate kmem_free(cfg, KDRV_CFG_MAXLEN); 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate return (rc); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate static int 118*0Sstevel@tonic-gate kdrv_deactivate(void) 119*0Sstevel@tonic-gate { 120*0Sstevel@tonic-gate return (kctl_deactivate()); 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate /*ARGSUSED*/ 124*0Sstevel@tonic-gate static int 125*0Sstevel@tonic-gate kdrv_ioctl(dev_t dev, int cmd, intptr_t arg, int flags, cred_t *credp, 126*0Sstevel@tonic-gate int *rvalp) 127*0Sstevel@tonic-gate { 128*0Sstevel@tonic-gate switch (cmd) { 129*0Sstevel@tonic-gate case KMDB_IOC_START: 130*0Sstevel@tonic-gate return (kdrv_activate(arg)); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate case KMDB_IOC_STOP: 133*0Sstevel@tonic-gate return (kdrv_deactivate()); 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate default: 136*0Sstevel@tonic-gate return (EINVAL); 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate /*ARGSUSED*/ 141*0Sstevel@tonic-gate static int 142*0Sstevel@tonic-gate kdrv_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 143*0Sstevel@tonic-gate { 144*0Sstevel@tonic-gate int error = DDI_SUCCESS; 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate switch (infocmd) { 147*0Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 148*0Sstevel@tonic-gate *result = kdrv_dip; 149*0Sstevel@tonic-gate break; 150*0Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 151*0Sstevel@tonic-gate *result = (void *)(uintptr_t)getminor((dev_t)arg); 152*0Sstevel@tonic-gate break; 153*0Sstevel@tonic-gate default: 154*0Sstevel@tonic-gate *result = NULL; 155*0Sstevel@tonic-gate error = DDI_FAILURE; 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate return (error); 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate static int 162*0Sstevel@tonic-gate kdrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 163*0Sstevel@tonic-gate { 164*0Sstevel@tonic-gate if (cmd != DDI_ATTACH) 165*0Sstevel@tonic-gate return (DDI_FAILURE); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate if (ddi_create_minor_node(dip, ddi_get_name(dip), S_IFCHR, 168*0Sstevel@tonic-gate ddi_get_instance(dip), DDI_PSEUDO, 0) != DDI_SUCCESS) 169*0Sstevel@tonic-gate return (DDI_FAILURE); 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate kdrv_dip = dip; 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate if (kctl_attach(dip) != 0) 174*0Sstevel@tonic-gate return (DDI_FAILURE); 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate return (DDI_SUCCESS); 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate static int 180*0Sstevel@tonic-gate kdrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 181*0Sstevel@tonic-gate { 182*0Sstevel@tonic-gate if (cmd != DDI_DETACH) 183*0Sstevel@tonic-gate return (DDI_FAILURE); 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate if (kctl_detach() == EBUSY) 186*0Sstevel@tonic-gate return (DDI_FAILURE); 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate return (DDI_SUCCESS); 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate static struct cb_ops kdrv_cb_ops = { 194*0Sstevel@tonic-gate kdrv_open, 195*0Sstevel@tonic-gate kdrv_close, 196*0Sstevel@tonic-gate nodev, /* not a block driver */ 197*0Sstevel@tonic-gate nodev, /* no print routine */ 198*0Sstevel@tonic-gate nodev, /* no dump routine */ 199*0Sstevel@tonic-gate nodev, /* no read routine */ 200*0Sstevel@tonic-gate nodev, /* no write routine */ 201*0Sstevel@tonic-gate kdrv_ioctl, 202*0Sstevel@tonic-gate nodev, /* no devmap routine */ 203*0Sstevel@tonic-gate nodev, /* no mmap routine */ 204*0Sstevel@tonic-gate nodev, /* no segmap routine */ 205*0Sstevel@tonic-gate nochpoll, /* no chpoll routine */ 206*0Sstevel@tonic-gate ddi_prop_op, 207*0Sstevel@tonic-gate 0, /* not a STREAMS driver */ 208*0Sstevel@tonic-gate D_NEW | D_MP, /* safe for multi-thread/multi-processor */ 209*0Sstevel@tonic-gate }; 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate static struct dev_ops kdrv_ops = { 212*0Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 213*0Sstevel@tonic-gate 0, /* devo_refcnt */ 214*0Sstevel@tonic-gate kdrv_getinfo, /* devo_getinfo */ 215*0Sstevel@tonic-gate nulldev, /* devo_identify */ 216*0Sstevel@tonic-gate nulldev, /* devo_probe */ 217*0Sstevel@tonic-gate kdrv_attach, /* devo_attach */ 218*0Sstevel@tonic-gate kdrv_detach, /* devo_detach */ 219*0Sstevel@tonic-gate nodev, /* devo_reset */ 220*0Sstevel@tonic-gate &kdrv_cb_ops, /* devo_cb_ops */ 221*0Sstevel@tonic-gate (struct bus_ops *)0, /* devo_bus_ops */ 222*0Sstevel@tonic-gate NULL, /* devo_power */ 223*0Sstevel@tonic-gate }; 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate static struct modldrv modldrv = { 226*0Sstevel@tonic-gate &mod_driverops, 227*0Sstevel@tonic-gate "kmdb driver %I%", 228*0Sstevel@tonic-gate &kdrv_ops 229*0Sstevel@tonic-gate }; 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate static struct modlinkage modlinkage = { 232*0Sstevel@tonic-gate MODREV_1, 233*0Sstevel@tonic-gate (void *)&modldrv, 234*0Sstevel@tonic-gate NULL 235*0Sstevel@tonic-gate }; 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate int 238*0Sstevel@tonic-gate _init(void) 239*0Sstevel@tonic-gate { 240*0Sstevel@tonic-gate return (mod_install(&modlinkage)); 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate int 244*0Sstevel@tonic-gate _info(struct modinfo *modinfop) 245*0Sstevel@tonic-gate { 246*0Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate int 250*0Sstevel@tonic-gate _fini(void) 251*0Sstevel@tonic-gate { 252*0Sstevel@tonic-gate return (mod_remove(&modlinkage)); 253*0Sstevel@tonic-gate } 254