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