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>
48 #include <sys/sysctl.h>
51 #include <xen/xen-os.h>
53 #include <xen/hypervisor.h>
54 #include <xen/xenstore/xenstorevar.h>
55 #include <xen/xenstore/xenstore_internal.h>
57 static unsigned int max_pending_watches = 1000;
59 struct xs_dev_transaction {
60 LIST_ENTRY(xs_dev_transaction) list;
61 struct xs_transaction handle;
65 LIST_ENTRY(xs_dev_watch) list;
66 struct xs_watch watch;
68 struct xs_dev_data *user;
72 /* In-progress transaction. */
73 LIST_HEAD(, xs_dev_transaction) transactions;
76 LIST_HEAD(, xs_dev_watch) watches;
78 /* Partial request. */
81 struct xsd_sockmsg msg;
82 char buffer[PAGE_SIZE];
86 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
87 char read_buffer[PAGE_SIZE];
88 unsigned int read_cons, read_prod;
90 /* Serializes writes to the read buffer. */
93 /* Polling structure (for reads only ATM). */
94 struct selinfo ev_rsel;
98 xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
102 for (i = 0; i < len; i++, u->read_prod++)
103 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
105 KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
106 ("xenstore reply too big"));
109 selwakeup(&u->ev_rsel);
113 xs_dev_error_to_string(int error)
117 for (i = 0; i < nitems(xsd_errors); i++)
118 if (xsd_errors[i].errnum == error)
119 return (xsd_errors[i].errstring);
125 xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
127 struct xsd_sockmsg msg;
136 payload = xs_dev_error_to_string(error);
138 payload = xs_dev_error_to_string(EINVAL);
139 KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));
141 msg.len = strlen(payload) + 1;
144 xs_queue_reply(u, (char *)&msg, sizeof(msg));
145 xs_queue_reply(u, payload, msg.len);
146 mtx_unlock(&u->lock);
150 xs_dev_watch_message_parse_string(const char **p, const char *end,
151 const char **string_r)
155 nul = memchr(*p, 0, end - *p);
166 xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
167 const char **token_r)
172 p = (const char *)msg + sizeof(*msg);
174 KASSERT(p <= end, ("payload overflow"));
176 error = xs_dev_watch_message_parse_string(&p, end, path_r);
179 error = xs_dev_watch_message_parse_string(&p, end, token_r);
186 static struct xs_dev_watch *
187 xs_dev_find_watch(struct xs_dev_data *u, const char *token)
189 struct xs_dev_watch *watch;
191 LIST_FOREACH(watch, &u->watches, list)
192 if (strcmp(watch->token, token) == 0)
199 xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
201 struct xs_dev_watch *dwatch;
202 struct xsd_sockmsg msg;
205 dwatch = (struct xs_dev_watch *)watch->callback_data;
206 msg.type = XS_WATCH_EVENT;
207 msg.req_id = msg.tx_id = 0;
208 msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;
210 payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
211 strcpy(payload, vec[XS_WATCH_PATH]);
212 strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
213 mtx_lock(&dwatch->user->lock);
214 xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
215 xs_queue_reply(dwatch->user, payload, msg.len);
216 mtx_unlock(&dwatch->user->lock);
217 free(payload, M_XENSTORE);
220 static struct xs_dev_transaction *
221 xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
223 struct xs_dev_transaction *trans;
225 LIST_FOREACH(trans, &u->transactions, list)
226 if (trans->handle.id == tx_id)
233 xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
236 struct xs_dev_data *u;
238 error = devfs_get_cdevpriv((void **)&u);
242 while (u->read_prod == u->read_cons) {
243 error = tsleep(u, PCATCH, "xsdread", hz/10);
244 if (error && error != EWOULDBLOCK)
248 while (uio->uio_resid > 0) {
249 if (u->read_cons == u->read_prod)
251 error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
261 xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
264 const char *wpath, *wtoken;
265 struct xs_dev_data *u;
266 struct xs_dev_transaction *trans;
267 struct xs_dev_watch *watch;
269 static const char *ok = "OK";
270 int len = uio->uio_resid;
272 error = devfs_get_cdevpriv((void **)&u);
276 if ((len + u->len) > sizeof(u->u.buffer))
279 error = uiomove(u->u.buffer + u->len, len, uio);
284 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
287 switch (u->u.msg.type) {
288 case XS_TRANSACTION_START:
289 case XS_TRANSACTION_END:
294 case XS_GET_DOMAIN_PATH:
299 /* Check that this transaction id is not hijacked. */
300 if (u->u.msg.tx_id != 0 &&
301 xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
305 error = xs_dev_request_and_reply(&u->u.msg, &reply);
307 if (u->u.msg.type == XS_TRANSACTION_START) {
308 trans = malloc(sizeof(*trans), M_XENSTORE,
310 trans->handle.id = strtoul(reply, NULL, 0);
311 LIST_INSERT_HEAD(&u->transactions, trans, list);
312 } else if (u->u.msg.type == XS_TRANSACTION_END) {
313 trans = xs_dev_find_transaction(u,
315 KASSERT(trans != NULL,
316 ("Unable to find transaction"));
317 LIST_REMOVE(trans, list);
318 free(trans, M_XENSTORE);
321 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
322 xs_queue_reply(u, (char *)reply, u->u.msg.len);
323 mtx_unlock(&u->lock);
324 free(reply, M_XENSTORE);
329 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
332 if (xs_dev_find_watch(u, wtoken) != NULL) {
337 watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
338 watch->watch.node = strdup(wpath, M_XENSTORE);
339 watch->watch.callback = xs_dev_watch_cb;
340 watch->watch.callback_data = (uintptr_t)watch;
341 watch->watch.max_pending = max_pending_watches;
342 watch->token = strdup(wtoken, M_XENSTORE);
345 error = xs_register_watch(&watch->watch);
347 free(watch->token, M_XENSTORE);
348 free(watch->watch.node, M_XENSTORE);
349 free(watch, M_XENSTORE);
353 LIST_INSERT_HEAD(&u->watches, watch, list);
354 u->u.msg.len = sizeof(ok);
356 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
357 xs_queue_reply(u, ok, sizeof(ok));
358 mtx_unlock(&u->lock);
362 error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
365 watch = xs_dev_find_watch(u, wtoken);
371 LIST_REMOVE(watch, list);
372 xs_unregister_watch(&watch->watch);
373 free(watch->watch.node, M_XENSTORE);
374 free(watch->token, M_XENSTORE);
375 free(watch, M_XENSTORE);
376 u->u.msg.len = sizeof(ok);
378 xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
379 xs_queue_reply(u, ok, sizeof(ok));
380 mtx_unlock(&u->lock);
388 xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);
390 /* Reset the write buffer. */
397 xs_dev_poll(struct cdev *dev, int events, struct thread *td)
399 struct xs_dev_data *u;
402 error = devfs_get_cdevpriv((void **)&u);
406 /* we can always write */
407 mask = events & (POLLOUT | POLLWRNORM);
409 if (events & (POLLIN | POLLRDNORM)) {
410 if (u->read_cons != u->read_prod) {
411 mask |= events & (POLLIN | POLLRDNORM);
413 /* Record that someone is waiting */
414 selrecord(td, &u->ev_rsel);
422 xs_dev_dtor(void *arg)
424 struct xs_dev_data *u = arg;
425 struct xs_dev_transaction *trans, *tmpt;
426 struct xs_dev_watch *watch, *tmpw;
428 seldrain(&u->ev_rsel);
430 LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
431 xs_transaction_end(trans->handle, 1);
432 LIST_REMOVE(trans, list);
433 free(trans, M_XENSTORE);
436 LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
437 LIST_REMOVE(watch, list);
438 xs_unregister_watch(&watch->watch);
439 free(watch->watch.node, M_XENSTORE);
440 free(watch->token, M_XENSTORE);
441 free(watch, M_XENSTORE);
443 mtx_destroy(&u->lock);
449 xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
451 struct xs_dev_data *u;
454 u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
455 mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
456 LIST_INIT(&u->transactions);
457 LIST_INIT(&u->watches);
458 error = devfs_set_cdevpriv(u, xs_dev_dtor);
465 static struct cdevsw xs_dev_cdevsw = {
466 .d_version = D_VERSION,
467 .d_read = xs_dev_read,
468 .d_write = xs_dev_write,
469 .d_open = xs_dev_open,
470 .d_poll = xs_dev_poll,
474 /*------------------ Private Device Attachment Functions --------------------*/
476 * \brief Identify instances of this device type in the system.
478 * \param driver The driver performing this identify action.
479 * \param parent The NewBus parent device for any devices this method adds.
482 xs_dev_identify(driver_t *driver __unused, device_t parent)
485 * A single device instance for our driver is always present
486 * in a system operating under Xen.
488 BUS_ADD_CHILD(parent, 0, driver->name, 0);
492 * \brief Probe for the existence of the Xenstore device
494 * \param dev NewBus device_t for this instance.
496 * \return Always returns 0 indicating success.
499 xs_dev_probe(device_t dev)
502 device_set_desc(dev, "Xenstore user-space device");
507 * \brief Attach the Xenstore device.
509 * \param dev NewBus device_t for this instance.
511 * \return On success, 0. Otherwise an errno value indicating the
515 xs_dev_attach(device_t dev)
517 struct cdev *xs_cdev;
518 struct sysctl_ctx_list *sysctl_ctx;
519 struct sysctl_oid *sysctl_tree;
521 sysctl_ctx = device_get_sysctl_ctx(dev);
522 sysctl_tree = device_get_sysctl_tree(dev);
523 if (sysctl_ctx == NULL || sysctl_tree == NULL)
526 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
527 "max_pending_watch_events", CTLFLAG_RW, &max_pending_watches, 0,
528 "maximum amount of pending watch events to be delivered");
530 xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
531 UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
538 /*-------------------- Private Device Attachment Data -----------------------*/
539 static device_method_t xs_dev_methods[] = {
540 /* Device interface */
541 DEVMETHOD(device_identify, xs_dev_identify),
542 DEVMETHOD(device_probe, xs_dev_probe),
543 DEVMETHOD(device_attach, xs_dev_attach),
548 DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
549 devclass_t xs_dev_devclass;
551 DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, xs_dev_devclass,