xref: /illumos-gate/usr/src/uts/i86xpv/io/domcaps.c (revision bbd6719318c24a8a2364080d8a139444e9944311)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This rather uninspiring device enables userland to discover if
31  * the current kernel is actually a dom0 or other domain e.g. domU.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/file.h>
36 #include <sys/errno.h>
37 #include <sys/open.h>
38 #include <sys/cred.h>
39 #include <sys/conf.h>
40 #include <sys/stat.h>
41 #include <sys/modctl.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 #include <sys/hypervisor.h>
45 #include <sys/sysmacros.h>
46 
47 #include <sys/domcaps_impl.h>
48 
49 static dev_info_t *domcaps_devi;
50 
51 /*ARGSUSED*/
52 static int
53 domcaps_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
54 {
55 	if (getminor((dev_t)arg) != DOMCAPS_MINOR)
56 		return (DDI_FAILURE);
57 
58 	switch (cmd) {
59 	case DDI_INFO_DEVT2DEVINFO:
60 		*result = domcaps_devi;
61 		break;
62 	case DDI_INFO_DEVT2INSTANCE:
63 		*result = 0;
64 		break;
65 	default:
66 		return (DDI_FAILURE);
67 	}
68 
69 	return (DDI_SUCCESS);
70 }
71 
72 static int
73 domcaps_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
74 {
75 	if (cmd != DDI_ATTACH)
76 		return (DDI_FAILURE);
77 
78 	if (ddi_create_minor_node(devi, ddi_get_name(devi), S_IFCHR,
79 	    ddi_get_instance(devi), DDI_PSEUDO, 0) != DDI_SUCCESS)
80 		return (DDI_FAILURE);
81 
82 	domcaps_devi = devi;
83 	ddi_report_dev(devi);
84 	return (DDI_SUCCESS);
85 }
86 
87 static int
88 domcaps_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
89 {
90 	if (cmd != DDI_DETACH)
91 		return (DDI_FAILURE);
92 	ddi_remove_minor_node(devi, NULL);
93 	domcaps_devi = NULL;
94 	return (DDI_SUCCESS);
95 }
96 
97 /*ARGSUSED1*/
98 static int
99 domcaps_open(dev_t *dev, int flag, int otyp, cred_t *cr)
100 {
101 	return (getminor(*dev) == DOMCAPS_MINOR ? 0 : ENXIO);
102 }
103 
104 /*ARGSUSED*/
105 static int
106 domcaps_read(dev_t dev, uio_t *uio, cred_t *cr)
107 {
108 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
109 		static char data[] = "control_d\n";
110 		size_t nbytes;
111 
112 		if (uio->uio_loffset > sizeof (data))
113 			return (0);
114 		nbytes = MIN(uio->uio_resid, sizeof (data) - uio->uio_loffset);
115 
116 		return (uiomove(data + uio->uio_loffset, nbytes,
117 		    UIO_READ, uio));
118 	}
119 
120 	return (0);
121 }
122 
123 static struct cb_ops domcaps_cb_ops = {
124 	domcaps_open,
125 	nulldev,	/* close */
126 	nodev,		/* strategy */
127 	nodev,		/* print */
128 	nodev,		/* dump */
129 	domcaps_read,
130 	nodev,		/* write */
131 	nodev,		/* ioctl */
132 	nodev,		/* devmap */
133 	nodev,		/* mmap */
134 	nodev,		/* segmap */
135 	nochpoll,	/* poll */
136 	ddi_prop_op,
137 	NULL,
138 	D_64BIT | D_MP,
139 	CB_REV,
140 	NULL,
141 	NULL
142 };
143 
144 static struct dev_ops domcaps_dv_ops = {
145 	DEVO_REV,
146 	0,
147 	domcaps_getinfo,
148 	nulldev,	/* identify */
149 	nulldev,	/* probe */
150 	domcaps_attach,
151 	domcaps_detach,
152 	nodev,		/* reset */
153 	&domcaps_cb_ops,
154 	NULL,		/* struct bus_ops */
155 	NULL		/* power */
156 };
157 
158 static struct modldrv modldrv = {
159 	&mod_driverops,
160 	"hypervisor capabilities driver %I%",
161 	&domcaps_dv_ops
162 };
163 
164 static struct modlinkage modl = {
165 	MODREV_1,
166 	{
167 		(void *)&modldrv,
168 		NULL		/* null termination */
169 	}
170 };
171 
172 int
173 _init(void)
174 {
175 	return (mod_install(&modl));
176 }
177 
178 int
179 _fini(void)
180 {
181 	return (mod_remove(&modl));
182 }
183 
184 int
185 _info(struct modinfo *modinfo)
186 {
187 	return (mod_info(&modl, modinfo));
188 }
189