xref: /titanic_44/usr/src/uts/i86xpv/io/balloon_drv.c (revision 3289e1bdec732dbdf04517907516fd0d00f6a6f8)
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  * A simple wrapper around the balloon kernel thread to allow userland
31  * programs access to the balloon status.
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 #include <sys/balloon_impl.h>
47 
48 static dev_info_t *balloon_devi;
49 
50 /*ARGSUSED*/
51 static int
52 balloon_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
53 {
54 	if (getminor((dev_t)arg) != BALLOON_MINOR)
55 		return (DDI_FAILURE);
56 
57 	switch (cmd) {
58 	case DDI_INFO_DEVT2DEVINFO:
59 		*result = balloon_devi;
60 		break;
61 	case DDI_INFO_DEVT2INSTANCE:
62 		*result = 0;
63 		break;
64 	default:
65 		return (DDI_FAILURE);
66 	}
67 
68 	return (DDI_SUCCESS);
69 }
70 
71 static int
72 balloon_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
73 {
74 	if (cmd != DDI_ATTACH)
75 		return (DDI_FAILURE);
76 
77 	if (ddi_create_minor_node(devi, ddi_get_name(devi), S_IFCHR,
78 	    ddi_get_instance(devi), DDI_PSEUDO, 0) != DDI_SUCCESS)
79 		return (DDI_FAILURE);
80 
81 	balloon_devi = devi;
82 	ddi_report_dev(devi);
83 	return (DDI_SUCCESS);
84 }
85 
86 static int
87 balloon_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
88 {
89 	if (cmd != DDI_DETACH)
90 		return (DDI_FAILURE);
91 	ddi_remove_minor_node(devi, NULL);
92 	balloon_devi = NULL;
93 	return (DDI_SUCCESS);
94 }
95 
96 /*ARGSUSED1*/
97 static int
98 balloon_open(dev_t *dev, int flag, int otyp, cred_t *cr)
99 {
100 	return (getminor(*dev) == BALLOON_MINOR ? 0 : ENXIO);
101 }
102 
103 /*
104  * When asked for one of the balloon values, we simply query the balloon thread.
105  */
106 /*ARGSUSED*/
107 static int
108 balloon_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr,
109     int *rval_p)
110 {
111 	int rval = 0;
112 	size_t value;
113 
114 	switch (cmd) {
115 	case BLN_IOCTL_CURRENT:
116 	case BLN_IOCTL_TARGET:
117 	case BLN_IOCTL_LOW:
118 	case BLN_IOCTL_HIGH:
119 	case BLN_IOCTL_LIMIT:
120 		value = balloon_values(cmd);
121 		if (ddi_copyout((void *)&value, (void *)arg, sizeof (value),
122 		    mode))
123 			return (EFAULT);
124 		break;
125 	default:
126 		rval = EINVAL;
127 		break;
128 	}
129 	return (rval);
130 }
131 
132 static struct cb_ops balloon_cb_ops = {
133 	balloon_open,
134 	nulldev,	/* close */
135 	nodev,		/* strategy */
136 	nodev,		/* print */
137 	nodev,		/* dump */
138 	nodev,		/* read */
139 	nodev,		/* write */
140 	balloon_ioctl,	/* ioctl */
141 	nodev,		/* devmap */
142 	nodev,		/* mmap */
143 	nodev,		/* segmap */
144 	nochpoll,	/* poll */
145 	ddi_prop_op,
146 	NULL,
147 	D_64BIT | D_MP,
148 	CB_REV,
149 	NULL,
150 	NULL
151 };
152 
153 static struct dev_ops balloon_dv_ops = {
154 	DEVO_REV,
155 	0,
156 	balloon_getinfo,
157 	nulldev,	/* identify */
158 	nulldev,	/* probe */
159 	balloon_attach,
160 	balloon_detach,
161 	nodev,		/* reset */
162 	&balloon_cb_ops,
163 	NULL,		/* struct bus_ops */
164 	NULL		/* power */
165 };
166 
167 static struct modldrv modldrv = {
168 	&mod_driverops,
169 	"balloon driver 1.1",
170 	&balloon_dv_ops
171 };
172 
173 static struct modlinkage modl = {
174 	MODREV_1,
175 	{
176 		(void *)&modldrv,
177 		NULL		/* null termination */
178 	}
179 };
180 
181 int
182 _init(void)
183 {
184 	return (mod_install(&modl));
185 }
186 
187 int
188 _fini(void)
189 {
190 	return (mod_remove(&modl));
191 }
192 
193 int
194 _info(struct modinfo *modinfo)
195 {
196 	return (mod_info(&modl, modinfo));
197 }
198