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>
47 #include <sys/sysctl.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 static unsigned int max_pending_watches = 1000;
58 struct xs_dev_transaction {
59 LIST_ENTRY(xs_dev_transaction) list;
60 struct xs_transaction handle;
64 LIST_ENTRY(xs_dev_watch) list;
65 struct xs_watch watch;
67 struct xs_dev_data *user;
71 /* In-progress transaction. */
72 LIST_HEAD(, xs_dev_transaction) transactions;
75 LIST_HEAD(, xs_dev_watch) watches;
77 /* Partial request. */
80 struct xsd_sockmsg msg;
81 char buffer[PAGE_SIZE];
85 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
86 char read_buffer[PAGE_SIZE];
87 unsigned int read_cons, read_prod;
89 /* Serializes writes to the read buffer. */
92 /* Polling structure (for reads only ATM). */
93 struct selinfo ev_rsel;
97 xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
101 for (i = 0; i < len; i++, u->read_prod++)
102 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
104 KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
105 ("xenstore reply too big"));
108 selwakeup(&u->ev_rsel);
112 xs_dev_error_to_string(int error)
116 for (i = 0; i < nitems(xsd_errors); i++)
117 if (xsd_errors[i].errnum == error)
118 return (xsd_errors[i].errstring);
124 xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
126 struct xsd_sockmsg msg;
134 payload = xs_dev_error_to_string(error);
136 payload = xs_dev_error_to_string(EINVAL);
137 KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));
139 msg.len = strlen(payload) + 1;
142 xs_queue_reply(u, (char *)&msg, sizeof(msg));
143 xs_queue_reply(u, payload, msg.len);
144 mtx_unlock(&u->lock);
148 xs_dev_watch_message_parse_string(const char **p, const char *end,
149 const char **string_r)
153 nul = memchr(*p, 0, end - *p);
164 xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
165 const char **token_r)
170 p = (const char *)msg + sizeof(*msg);
172 KASSERT(p <= end, ("payload overflow"));
174 error = xs_dev_watch_message_parse_string(&p, end, path_r);
177 error = xs_dev_watch_message_parse_string(&p, end, token_r);
184 static struct xs_dev_watch *
185 xs_dev_find_watch(struct xs_dev_data *u, const char *token)
187 struct xs_dev_watch *watch;
189 LIST_FOREACH(watch, &u->watches, list)
190 if (strcmp(watch->token, token) == 0)
197 xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
199 struct xs_dev_watch *dwatch;
200 struct xsd_sockmsg msg;
203 dwatch = (struct xs_dev_watch *)watch->callback_data;
204 msg.type = XS_WATCH_EVENT;
205 msg.req_id = msg.tx_id = 0;
206 msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;
208 payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
209 strcpy(payload, vec[XS_WATCH_PATH]);
210 strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
211 mtx_lock(&dwatch->user->lock);
212 xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
213 xs_queue_reply(dwatch->user, payload, msg.len);
214 mtx_unlock(&dwatch->user->lock);
215 free(payload, M_XENSTORE);
218 static struct xs_dev_transaction *
219 xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
221 struct xs_dev_transaction *trans;
223 LIST_FOREACH(trans, &u->transactions, list)
224 if (trans->handle.id == tx_id)
231 xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
234 struct xs_dev_data *u;
236 error = devfs_get_cdevpriv((void **)&u);
240 while (u->read_prod == u->read_cons) {
241 error = tsleep(u, PCATCH, "xsdread", hz/10);
242 if (error && error != EWOULDBLOCK)
246 while (uio->uio_resid > 0) {
247 if (u->read_cons == u->read_prod)
249 error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
259 xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
262 const char *wpath, *wtoken;
263 struct xs_dev_data *u;
264 struct xs_dev_transaction *trans;
265 struct xs_dev_watch *watch;
267 static const char *ok = "OK";
268 int len = uio->uio_resid;
270 error = devfs_get_cdevpriv((void **)&u);
274 if ((len + u->len) > sizeof(u->u.buffer))
277 error = uiomove(u->u.buffer + u->len, len, uio);
282 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
285 switch (u->u.msg.type) {
286 case XS_TRANSACTION_START:
287 case XS_TRANSACTION_END:
292 case XS_GET_DOMAIN_PATH:
297 /* Check that this transaction id is not hijacked. */
298 if (u->u.msg.tx_id != 0 &&
299 xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
303 error = xs_dev_request_and_reply(&u->u.msg, &reply);
305 if (u->u.msg.type == XS_TRANSACTION_START) {
306 trans = malloc(sizeof(*trans), M_XENSTORE,
308 trans->handle.id = strtoul(reply, NULL, 0);
309 LIST_INSERT_HEAD(&u->transactions, trans, list);
310 } else if (u->u.msg.type == XS_TRANSACTION_END) {
311 trans = xs_dev_find_transaction(u,
313 KASSERT(trans != NULL,
314 ("Unable to find transaction"));
315 LIST_REMOVE(trans, list);
316 free(trans, M_XENSTORE);
319 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
320 xs_queue_reply(u, (char *)reply, u->u.msg.len);
321 mtx_unlock(&u->lock);
322 free(reply, M_XENSTORE);
327 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
330 if (xs_dev_find_watch(u, wtoken) != NULL) {
335 watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
336 watch->watch.node = strdup(wpath, M_XENSTORE);
337 watch->watch.callback = xs_dev_watch_cb;
338 watch->watch.callback_data = (uintptr_t)watch;
339 watch->watch.max_pending = max_pending_watches;
340 watch->token = strdup(wtoken, M_XENSTORE);
343 error = xs_register_watch(&watch->watch);
345 free(watch->token, M_XENSTORE);
346 free(watch->watch.node, M_XENSTORE);
347 free(watch, M_XENSTORE);
351 LIST_INSERT_HEAD(&u->watches, watch, list);
352 u->u.msg.len = sizeof(ok);
354 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
355 xs_queue_reply(u, ok, sizeof(ok));
356 mtx_unlock(&u->lock);
360 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
363 watch = xs_dev_find_watch(u, wtoken);
369 LIST_REMOVE(watch, list);
370 xs_unregister_watch(&watch->watch);
371 free(watch->watch.node, M_XENSTORE);
372 free(watch->token, M_XENSTORE);
373 free(watch, M_XENSTORE);
374 u->u.msg.len = sizeof(ok);
376 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
377 xs_queue_reply(u, ok, sizeof(ok));
378 mtx_unlock(&u->lock);
386 xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);
388 /* Reset the write buffer. */
395 xs_dev_poll(struct cdev *dev, int events, struct thread *td)
397 struct xs_dev_data *u;
400 error = devfs_get_cdevpriv((void **)&u);
404 /* we can always write */
405 mask = events & (POLLOUT | POLLWRNORM);
407 if (events & (POLLIN | POLLRDNORM)) {
408 if (u->read_cons != u->read_prod) {
409 mask |= events & (POLLIN | POLLRDNORM);
411 /* Record that someone is waiting */
412 selrecord(td, &u->ev_rsel);
420 xs_dev_dtor(void *arg)
422 struct xs_dev_data *u = arg;
423 struct xs_dev_transaction *trans, *tmpt;
424 struct xs_dev_watch *watch, *tmpw;
426 seldrain(&u->ev_rsel);
428 LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
429 xs_transaction_end(trans->handle, 1);
430 LIST_REMOVE(trans, list);
431 free(trans, M_XENSTORE);
434 LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
435 LIST_REMOVE(watch, list);
436 xs_unregister_watch(&watch->watch);
437 free(watch->watch.node, M_XENSTORE);
438 free(watch->token, M_XENSTORE);
439 free(watch, M_XENSTORE);
441 mtx_destroy(&u->lock);
447 xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
449 struct xs_dev_data *u;
452 u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
453 mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
454 LIST_INIT(&u->transactions);
455 LIST_INIT(&u->watches);
456 error = devfs_set_cdevpriv(u, xs_dev_dtor);
463 static struct cdevsw xs_dev_cdevsw = {
464 .d_version = D_VERSION,
465 .d_read = xs_dev_read,
466 .d_write = xs_dev_write,
467 .d_open = xs_dev_open,
468 .d_poll = xs_dev_poll,
472 /*------------------ Private Device Attachment Functions --------------------*/
474 * \brief Identify instances of this device type in the system.
476 * \param driver The driver performing this identify action.
477 * \param parent The NewBus parent device for any devices this method adds.
480 xs_dev_identify(driver_t *driver __unused, device_t parent)
483 * A single device instance for our driver is always present
484 * in a system operating under Xen.
486 BUS_ADD_CHILD(parent, 0, driver->name, 0);
490 * \brief Probe for the existence of the Xenstore device
492 * \param dev NewBus device_t for this instance.
494 * \return Always returns 0 indicating success.
497 xs_dev_probe(device_t dev)
500 device_set_desc(dev, "Xenstore user-space device");
505 * \brief Attach the Xenstore device.
507 * \param dev NewBus device_t for this instance.
509 * \return On success, 0. Otherwise an errno value indicating the
513 xs_dev_attach(device_t dev)
515 struct cdev *xs_cdev;
516 struct sysctl_ctx_list *sysctl_ctx;
517 struct sysctl_oid *sysctl_tree;
519 sysctl_ctx = device_get_sysctl_ctx(dev);
520 sysctl_tree = device_get_sysctl_tree(dev);
521 if (sysctl_ctx == NULL || sysctl_tree == NULL)
524 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
525 "max_pending_watch_events", CTLFLAG_RW, &max_pending_watches, 0,
526 "maximum amount of pending watch events to be delivered");
528 xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
529 UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
536 /*-------------------- Private Device Attachment Data -----------------------*/
537 static device_method_t xs_dev_methods[] = {
538 /* Device interface */
539 DEVMETHOD(device_identify, xs_dev_identify),
540 DEVMETHOD(device_probe, xs_dev_probe),
541 DEVMETHOD(device_attach, xs_dev_attach),
546 DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
547 devclass_t xs_dev_devclass;
549 DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, xs_dev_devclass,