4 * Driver giving user-space access to the kernel's connection to the
7 * Copyright (c) 2005, Christian Limpach
8 * Copyright (c) 2005, Rusty Russell, IBM Corporation
10 * This file may be distributed separately from the Linux kernel, or
11 * incorporated into other software packages, subject to the following license:
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/cdefs.h>
38 #include <sys/errno.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/module.h>
48 #include <xen/xen-os.h>
50 #include <xen/hypervisor.h>
51 #include <xen/xenstore/xenstorevar.h>
52 #include <xen/xenstore/xenstore_internal.h>
54 struct xs_dev_transaction {
55 LIST_ENTRY(xs_dev_transaction) list;
56 struct xs_transaction handle;
60 /* In-progress transaction. */
61 LIST_HEAD(xdd_list_head, xs_dev_transaction) transactions;
63 /* Partial request. */
66 struct xsd_sockmsg msg;
67 char buffer[PAGE_SIZE];
71 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
72 char read_buffer[PAGE_SIZE];
73 unsigned int read_cons, read_prod;
77 xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
80 struct xs_dev_data *u;
82 error = devfs_get_cdevpriv((void **)&u);
86 while (u->read_prod == u->read_cons) {
87 error = tsleep(u, PCATCH, "xsdread", hz/10);
88 if (error && error != EWOULDBLOCK)
92 while (uio->uio_resid > 0) {
93 if (u->read_cons == u->read_prod)
95 error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
105 xs_queue_reply(struct xs_dev_data *u, char *data, unsigned int len)
109 for (i = 0; i < len; i++, u->read_prod++)
110 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
112 KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
113 ("xenstore reply too big"));
119 xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
122 struct xs_dev_data *u;
123 struct xs_dev_transaction *trans;
125 int len = uio->uio_resid;
127 error = devfs_get_cdevpriv((void **)&u);
131 if ((len + u->len) > sizeof(u->u.buffer))
134 error = uiomove(u->u.buffer + u->len, len, uio);
139 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
142 switch (u->u.msg.type) {
143 case XS_TRANSACTION_START:
144 case XS_TRANSACTION_END:
149 case XS_GET_DOMAIN_PATH:
154 error = xs_dev_request_and_reply(&u->u.msg, &reply);
156 if (u->u.msg.type == XS_TRANSACTION_START) {
157 trans = malloc(sizeof(*trans), M_XENSTORE,
159 trans->handle.id = strtoul(reply, NULL, 0);
160 LIST_INSERT_HEAD(&u->transactions, trans, list);
161 } else if (u->u.msg.type == XS_TRANSACTION_END) {
162 LIST_FOREACH(trans, &u->transactions, list)
163 if (trans->handle.id == u->u.msg.tx_id)
165 #if 0 /* XXX does this mean the list is empty? */
166 BUG_ON(&trans->list == &u->transactions);
168 LIST_REMOVE(trans, list);
169 free(trans, M_XENSTORE);
171 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
172 xs_queue_reply(u, (char *)reply, u->u.msg.len);
173 free(reply, M_XENSTORE);
189 xs_dev_dtor(void *arg)
191 struct xs_dev_data *u = arg;
192 struct xs_dev_transaction *trans, *tmp;
194 LIST_FOREACH_SAFE(trans, &u->transactions, list, tmp) {
195 xs_transaction_end(trans->handle, 1);
196 LIST_REMOVE(trans, list);
197 free(trans, M_XENSTORE);
204 xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
206 struct xs_dev_data *u;
209 u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
210 LIST_INIT(&u->transactions);
211 error = devfs_set_cdevpriv(u, xs_dev_dtor);
218 static struct cdevsw xs_dev_cdevsw = {
219 .d_version = D_VERSION,
220 .d_read = xs_dev_read,
221 .d_write = xs_dev_write,
222 .d_open = xs_dev_open,
226 /*------------------ Private Device Attachment Functions --------------------*/
228 * \brief Identify instances of this device type in the system.
230 * \param driver The driver performing this identify action.
231 * \param parent The NewBus parent device for any devices this method adds.
234 xs_dev_identify(driver_t *driver __unused, device_t parent)
237 * A single device instance for our driver is always present
238 * in a system operating under Xen.
240 BUS_ADD_CHILD(parent, 0, driver->name, 0);
244 * \brief Probe for the existence of the Xenstore device
246 * \param dev NewBus device_t for this instance.
248 * \return Always returns 0 indicating success.
251 xs_dev_probe(device_t dev)
254 device_set_desc(dev, "Xenstore user-space device");
259 * \brief Attach the Xenstore device.
261 * \param dev NewBus device_t for this instance.
263 * \return On success, 0. Otherwise an errno value indicating the
267 xs_dev_attach(device_t dev)
269 struct cdev *xs_cdev;
271 xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
272 UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
279 /*-------------------- Private Device Attachment Data -----------------------*/
280 static device_method_t xs_dev_methods[] = {
281 /* Device interface */
282 DEVMETHOD(device_identify, xs_dev_identify),
283 DEVMETHOD(device_probe, xs_dev_probe),
284 DEVMETHOD(device_attach, xs_dev_attach),
289 DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
290 devclass_t xs_dev_devclass;
292 DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, xs_dev_devclass,