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>
47 #include <sys/selinfo.h>
50 #include <xen/xen-os.h>
52 #include <xen/hypervisor.h>
53 #include <xen/xenstore/xenstorevar.h>
54 #include <xen/xenstore/xenstore_internal.h>
56 struct xs_dev_transaction {
57 LIST_ENTRY(xs_dev_transaction) list;
58 struct xs_transaction handle;
62 LIST_ENTRY(xs_dev_watch) list;
63 struct xs_watch watch;
65 struct xs_dev_data *user;
69 /* In-progress transaction. */
70 LIST_HEAD(, xs_dev_transaction) transactions;
73 LIST_HEAD(, xs_dev_watch) watches;
75 /* Partial request. */
78 struct xsd_sockmsg msg;
79 char buffer[PAGE_SIZE];
83 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
84 char read_buffer[PAGE_SIZE];
85 unsigned int read_cons, read_prod;
87 /* Serializes writes to the read buffer. */
90 /* Polling structure (for reads only ATM). */
91 struct selinfo ev_rsel;
95 xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
99 for (i = 0; i < len; i++, u->read_prod++)
100 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
102 KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
103 ("xenstore reply too big"));
106 selwakeup(&u->ev_rsel);
110 xs_dev_error_to_string(int error)
114 for (i = 0; i < nitems(xsd_errors); i++)
115 if (xsd_errors[i].errnum == error)
116 return (xsd_errors[i].errstring);
122 xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
124 struct xsd_sockmsg msg;
133 payload = xs_dev_error_to_string(error);
135 payload = xs_dev_error_to_string(EINVAL);
136 KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));
138 msg.len = strlen(payload) + 1;
141 xs_queue_reply(u, (char *)&msg, sizeof(msg));
142 xs_queue_reply(u, payload, msg.len);
143 mtx_unlock(&u->lock);
147 xs_dev_watch_message_parse_string(const char **p, const char *end,
148 const char **string_r)
152 nul = memchr(*p, 0, end - *p);
163 xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
164 const char **token_r)
169 p = (const char *)msg + sizeof(*msg);
171 KASSERT(p <= end, ("payload overflow"));
173 error = xs_dev_watch_message_parse_string(&p, end, path_r);
176 error = xs_dev_watch_message_parse_string(&p, end, token_r);
183 static struct xs_dev_watch *
184 xs_dev_find_watch(struct xs_dev_data *u, const char *token)
186 struct xs_dev_watch *watch;
188 LIST_FOREACH(watch, &u->watches, list)
189 if (strcmp(watch->token, token) == 0)
196 xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
198 struct xs_dev_watch *dwatch;
199 struct xsd_sockmsg msg;
202 dwatch = (struct xs_dev_watch *)watch->callback_data;
203 msg.type = XS_WATCH_EVENT;
204 msg.req_id = msg.tx_id = 0;
205 msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;
207 payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
208 strcpy(payload, vec[XS_WATCH_PATH]);
209 strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
210 mtx_lock(&dwatch->user->lock);
211 xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
212 xs_queue_reply(dwatch->user, payload, msg.len);
213 mtx_unlock(&dwatch->user->lock);
214 free(payload, M_XENSTORE);
217 static struct xs_dev_transaction *
218 xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
220 struct xs_dev_transaction *trans;
222 LIST_FOREACH(trans, &u->transactions, list)
223 if (trans->handle.id == tx_id)
230 xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
233 struct xs_dev_data *u;
235 error = devfs_get_cdevpriv((void **)&u);
239 while (u->read_prod == u->read_cons) {
240 error = tsleep(u, PCATCH, "xsdread", hz/10);
241 if (error && error != EWOULDBLOCK)
245 while (uio->uio_resid > 0) {
246 if (u->read_cons == u->read_prod)
248 error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
258 xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
261 const char *wpath, *wtoken;
262 struct xs_dev_data *u;
263 struct xs_dev_transaction *trans;
264 struct xs_dev_watch *watch;
266 static const char *ok = "OK";
267 int len = uio->uio_resid;
269 error = devfs_get_cdevpriv((void **)&u);
273 if ((len + u->len) > sizeof(u->u.buffer))
276 error = uiomove(u->u.buffer + u->len, len, uio);
281 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
284 switch (u->u.msg.type) {
285 case XS_TRANSACTION_START:
286 case XS_TRANSACTION_END:
291 case XS_GET_DOMAIN_PATH:
296 /* Check that this transaction id is not hijacked. */
297 if (u->u.msg.tx_id != 0 &&
298 xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
302 error = xs_dev_request_and_reply(&u->u.msg, &reply);
304 if (u->u.msg.type == XS_TRANSACTION_START) {
305 trans = malloc(sizeof(*trans), M_XENSTORE,
307 trans->handle.id = strtoul(reply, NULL, 0);
308 LIST_INSERT_HEAD(&u->transactions, trans, list);
309 } else if (u->u.msg.type == XS_TRANSACTION_END) {
310 trans = xs_dev_find_transaction(u,
312 KASSERT(trans != NULL,
313 ("Unable to find transaction"));
314 LIST_REMOVE(trans, list);
315 free(trans, M_XENSTORE);
318 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
319 xs_queue_reply(u, (char *)reply, u->u.msg.len);
320 mtx_unlock(&u->lock);
321 free(reply, M_XENSTORE);
326 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
329 if (xs_dev_find_watch(u, wtoken) != NULL) {
334 watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
335 watch->watch.node = strdup(wpath, M_XENSTORE);
336 watch->watch.callback = xs_dev_watch_cb;
337 watch->watch.callback_data = (uintptr_t)watch;
338 watch->token = strdup(wtoken, M_XENSTORE);
341 error = xs_register_watch(&watch->watch);
343 free(watch->token, M_XENSTORE);
344 free(watch->watch.node, M_XENSTORE);
345 free(watch, M_XENSTORE);
349 LIST_INSERT_HEAD(&u->watches, watch, list);
350 u->u.msg.len = sizeof(ok);
352 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
353 xs_queue_reply(u, ok, sizeof(ok));
354 mtx_unlock(&u->lock);
358 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
361 watch = xs_dev_find_watch(u, wtoken);
367 LIST_REMOVE(watch, list);
368 xs_unregister_watch(&watch->watch);
369 free(watch->watch.node, M_XENSTORE);
370 free(watch->token, M_XENSTORE);
371 free(watch, M_XENSTORE);
372 u->u.msg.len = sizeof(ok);
374 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
375 xs_queue_reply(u, ok, sizeof(ok));
376 mtx_unlock(&u->lock);
384 xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);
386 /* Reset the write buffer. */
393 xs_dev_poll(struct cdev *dev, int events, struct thread *td)
395 struct xs_dev_data *u;
398 error = devfs_get_cdevpriv((void **)&u);
402 /* we can always write */
403 mask = events & (POLLOUT | POLLWRNORM);
405 if (events & (POLLIN | POLLRDNORM)) {
406 if (u->read_cons != u->read_prod) {
407 mask |= events & (POLLIN | POLLRDNORM);
409 /* Record that someone is waiting */
410 selrecord(td, &u->ev_rsel);
418 xs_dev_dtor(void *arg)
420 struct xs_dev_data *u = arg;
421 struct xs_dev_transaction *trans, *tmpt;
422 struct xs_dev_watch *watch, *tmpw;
424 seldrain(&u->ev_rsel);
426 LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
427 xs_transaction_end(trans->handle, 1);
428 LIST_REMOVE(trans, list);
429 free(trans, M_XENSTORE);
432 LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
433 LIST_REMOVE(watch, list);
434 xs_unregister_watch(&watch->watch);
435 free(watch->watch.node, M_XENSTORE);
436 free(watch->token, M_XENSTORE);
437 free(watch, M_XENSTORE);
439 mtx_destroy(&u->lock);
445 xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
447 struct xs_dev_data *u;
450 u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
451 mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
452 LIST_INIT(&u->transactions);
453 LIST_INIT(&u->watches);
454 error = devfs_set_cdevpriv(u, xs_dev_dtor);
461 static struct cdevsw xs_dev_cdevsw = {
462 .d_version = D_VERSION,
463 .d_read = xs_dev_read,
464 .d_write = xs_dev_write,
465 .d_open = xs_dev_open,
466 .d_poll = xs_dev_poll,
470 /*------------------ Private Device Attachment Functions --------------------*/
472 * \brief Identify instances of this device type in the system.
474 * \param driver The driver performing this identify action.
475 * \param parent The NewBus parent device for any devices this method adds.
478 xs_dev_identify(driver_t *driver __unused, device_t parent)
481 * A single device instance for our driver is always present
482 * in a system operating under Xen.
484 BUS_ADD_CHILD(parent, 0, driver->name, 0);
488 * \brief Probe for the existence of the Xenstore device
490 * \param dev NewBus device_t for this instance.
492 * \return Always returns 0 indicating success.
495 xs_dev_probe(device_t dev)
498 device_set_desc(dev, "Xenstore user-space device");
503 * \brief Attach the Xenstore device.
505 * \param dev NewBus device_t for this instance.
507 * \return On success, 0. Otherwise an errno value indicating the
511 xs_dev_attach(device_t dev)
513 struct cdev *xs_cdev;
515 xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
516 UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
523 /*-------------------- Private Device Attachment Data -----------------------*/
524 static device_method_t xs_dev_methods[] = {
525 /* Device interface */
526 DEVMETHOD(device_identify, xs_dev_identify),
527 DEVMETHOD(device_probe, xs_dev_probe),
528 DEVMETHOD(device_attach, xs_dev_attach),
533 DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
534 devclass_t xs_dev_devclass;
536 DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, xs_dev_devclass,