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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/cdefs.h>
37 #include <sys/errno.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/selinfo.h>
49 #include <xen/xen-os.h>
51 #include <xen/hypervisor.h>
52 #include <xen/xenstore/xenstorevar.h>
53 #include <xen/xenstore/xenstore_internal.h>
55 struct xs_dev_transaction {
56 LIST_ENTRY(xs_dev_transaction) list;
57 struct xs_transaction handle;
61 LIST_ENTRY(xs_dev_watch) list;
62 struct xs_watch watch;
64 struct xs_dev_data *user;
68 /* In-progress transaction. */
69 LIST_HEAD(, xs_dev_transaction) transactions;
72 LIST_HEAD(, xs_dev_watch) watches;
74 /* Partial request. */
77 struct xsd_sockmsg msg;
78 char buffer[PAGE_SIZE];
82 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
83 char read_buffer[PAGE_SIZE];
84 unsigned int read_cons, read_prod;
86 /* Serializes writes to the read buffer. */
89 /* Polling structure (for reads only ATM). */
90 struct selinfo ev_rsel;
94 xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
98 for (i = 0; i < len; i++, u->read_prod++)
99 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
101 KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
102 ("xenstore reply too big"));
105 selwakeup(&u->ev_rsel);
109 xs_dev_error_to_string(int error)
113 for (i = 0; i < nitems(xsd_errors); i++)
114 if (xsd_errors[i].errnum == error)
115 return (xsd_errors[i].errstring);
121 xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
123 struct xsd_sockmsg msg;
131 payload = xs_dev_error_to_string(error);
133 payload = xs_dev_error_to_string(EINVAL);
134 KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));
136 msg.len = strlen(payload) + 1;
139 xs_queue_reply(u, (char *)&msg, sizeof(msg));
140 xs_queue_reply(u, payload, msg.len);
141 mtx_unlock(&u->lock);
145 xs_dev_watch_message_parse_string(const char **p, const char *end,
146 const char **string_r)
150 nul = memchr(*p, 0, end - *p);
161 xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
162 const char **token_r)
167 p = (const char *)msg + sizeof(*msg);
169 KASSERT(p <= end, ("payload overflow"));
171 error = xs_dev_watch_message_parse_string(&p, end, path_r);
174 error = xs_dev_watch_message_parse_string(&p, end, token_r);
181 static struct xs_dev_watch *
182 xs_dev_find_watch(struct xs_dev_data *u, const char *token)
184 struct xs_dev_watch *watch;
186 LIST_FOREACH(watch, &u->watches, list)
187 if (strcmp(watch->token, token) == 0)
194 xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
196 struct xs_dev_watch *dwatch;
197 struct xsd_sockmsg msg;
200 dwatch = (struct xs_dev_watch *)watch->callback_data;
201 msg.type = XS_WATCH_EVENT;
202 msg.req_id = msg.tx_id = 0;
203 msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;
205 payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
206 strcpy(payload, vec[XS_WATCH_PATH]);
207 strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
208 mtx_lock(&dwatch->user->lock);
209 xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
210 xs_queue_reply(dwatch->user, payload, msg.len);
211 mtx_unlock(&dwatch->user->lock);
212 free(payload, M_XENSTORE);
215 static struct xs_dev_transaction *
216 xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
218 struct xs_dev_transaction *trans;
220 LIST_FOREACH(trans, &u->transactions, list)
221 if (trans->handle.id == tx_id)
228 xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
231 struct xs_dev_data *u;
233 error = devfs_get_cdevpriv((void **)&u);
237 while (u->read_prod == u->read_cons) {
238 error = tsleep(u, PCATCH, "xsdread", hz/10);
239 if (error && error != EWOULDBLOCK)
243 while (uio->uio_resid > 0) {
244 if (u->read_cons == u->read_prod)
246 error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
256 xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
259 const char *wpath, *wtoken;
260 struct xs_dev_data *u;
261 struct xs_dev_transaction *trans;
262 struct xs_dev_watch *watch;
264 static const char *ok = "OK";
265 int len = uio->uio_resid;
267 error = devfs_get_cdevpriv((void **)&u);
271 if ((len + u->len) > sizeof(u->u.buffer))
274 error = uiomove(u->u.buffer + u->len, len, uio);
279 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
282 switch (u->u.msg.type) {
283 case XS_TRANSACTION_START:
284 case XS_TRANSACTION_END:
289 case XS_GET_DOMAIN_PATH:
294 /* Check that this transaction id is not hijacked. */
295 if (u->u.msg.tx_id != 0 &&
296 xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
300 error = xs_dev_request_and_reply(&u->u.msg, &reply);
302 if (u->u.msg.type == XS_TRANSACTION_START) {
303 trans = malloc(sizeof(*trans), M_XENSTORE,
305 trans->handle.id = strtoul(reply, NULL, 0);
306 LIST_INSERT_HEAD(&u->transactions, trans, list);
307 } else if (u->u.msg.type == XS_TRANSACTION_END) {
308 trans = xs_dev_find_transaction(u,
310 KASSERT(trans != NULL,
311 ("Unable to find transaction"));
312 LIST_REMOVE(trans, list);
313 free(trans, M_XENSTORE);
316 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
317 xs_queue_reply(u, (char *)reply, u->u.msg.len);
318 mtx_unlock(&u->lock);
319 free(reply, M_XENSTORE);
324 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
327 if (xs_dev_find_watch(u, wtoken) != NULL) {
332 watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
333 watch->watch.node = strdup(wpath, M_XENSTORE);
334 watch->watch.callback = xs_dev_watch_cb;
335 watch->watch.callback_data = (uintptr_t)watch;
336 watch->token = strdup(wtoken, M_XENSTORE);
339 error = xs_register_watch(&watch->watch);
341 free(watch->token, M_XENSTORE);
342 free(watch->watch.node, M_XENSTORE);
343 free(watch, M_XENSTORE);
347 LIST_INSERT_HEAD(&u->watches, watch, list);
348 u->u.msg.len = sizeof(ok);
350 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
351 xs_queue_reply(u, ok, sizeof(ok));
352 mtx_unlock(&u->lock);
356 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
359 watch = xs_dev_find_watch(u, wtoken);
365 LIST_REMOVE(watch, list);
366 xs_unregister_watch(&watch->watch);
367 free(watch->watch.node, M_XENSTORE);
368 free(watch->token, M_XENSTORE);
369 free(watch, M_XENSTORE);
370 u->u.msg.len = sizeof(ok);
372 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
373 xs_queue_reply(u, ok, sizeof(ok));
374 mtx_unlock(&u->lock);
382 xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);
384 /* Reset the write buffer. */
391 xs_dev_poll(struct cdev *dev, int events, struct thread *td)
393 struct xs_dev_data *u;
396 error = devfs_get_cdevpriv((void **)&u);
400 /* we can always write */
401 mask = events & (POLLOUT | POLLWRNORM);
403 if (events & (POLLIN | POLLRDNORM)) {
404 if (u->read_cons != u->read_prod) {
405 mask |= events & (POLLIN | POLLRDNORM);
407 /* Record that someone is waiting */
408 selrecord(td, &u->ev_rsel);
416 xs_dev_dtor(void *arg)
418 struct xs_dev_data *u = arg;
419 struct xs_dev_transaction *trans, *tmpt;
420 struct xs_dev_watch *watch, *tmpw;
422 seldrain(&u->ev_rsel);
424 LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
425 xs_transaction_end(trans->handle, 1);
426 LIST_REMOVE(trans, list);
427 free(trans, M_XENSTORE);
430 LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
431 LIST_REMOVE(watch, list);
432 xs_unregister_watch(&watch->watch);
433 free(watch->watch.node, M_XENSTORE);
434 free(watch->token, M_XENSTORE);
435 free(watch, M_XENSTORE);
437 mtx_destroy(&u->lock);
443 xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
445 struct xs_dev_data *u;
448 u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
449 mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
450 LIST_INIT(&u->transactions);
451 LIST_INIT(&u->watches);
452 error = devfs_set_cdevpriv(u, xs_dev_dtor);
459 static struct cdevsw xs_dev_cdevsw = {
460 .d_version = D_VERSION,
461 .d_read = xs_dev_read,
462 .d_write = xs_dev_write,
463 .d_open = xs_dev_open,
464 .d_poll = xs_dev_poll,
468 /*------------------ Private Device Attachment Functions --------------------*/
470 * \brief Identify instances of this device type in the system.
472 * \param driver The driver performing this identify action.
473 * \param parent The NewBus parent device for any devices this method adds.
476 xs_dev_identify(driver_t *driver __unused, device_t parent)
479 * A single device instance for our driver is always present
480 * in a system operating under Xen.
482 BUS_ADD_CHILD(parent, 0, driver->name, 0);
486 * \brief Probe for the existence of the Xenstore device
488 * \param dev NewBus device_t for this instance.
490 * \return Always returns 0 indicating success.
493 xs_dev_probe(device_t dev)
496 device_set_desc(dev, "Xenstore user-space device");
501 * \brief Attach the Xenstore device.
503 * \param dev NewBus device_t for this instance.
505 * \return On success, 0. Otherwise an errno value indicating the
509 xs_dev_attach(device_t dev)
511 struct cdev *xs_cdev;
513 xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
514 UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
521 /*-------------------- Private Device Attachment Data -----------------------*/
522 static device_method_t xs_dev_methods[] = {
523 /* Device interface */
524 DEVMETHOD(device_identify, xs_dev_identify),
525 DEVMETHOD(device_probe, xs_dev_probe),
526 DEVMETHOD(device_attach, xs_dev_attach),
531 DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
532 devclass_t xs_dev_devclass;
534 DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, xs_dev_devclass,