acpi: add rules to compile ASL source
[qemu.git] / hw / xen / xen_backend.c
blobd82ce5d8a6831c297b61f110c8193792fd446f6e
1 /*
2 * xen backend driver infrastructure
3 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; under version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
22 * TODO: add some xenbus / xenstore concepts overview here.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/signal.h>
37 #include "hw/hw.h"
38 #include "sysemu/char.h"
39 #include "qemu/log.h"
40 #include "hw/xen/xen_backend.h"
42 #include <xen/grant_table.h>
44 /* ------------------------------------------------------------- */
46 /* public */
47 XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
48 XenGnttab xen_xcg = XC_HANDLER_INITIAL_VALUE;
49 struct xs_handle *xenstore = NULL;
50 const char *xen_protocol;
52 /* private */
53 static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
54 static int debug = 0;
56 /* ------------------------------------------------------------- */
58 int xenstore_write_str(const char *base, const char *node, const char *val)
60 char abspath[XEN_BUFSIZE];
62 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
63 if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
64 return -1;
66 return 0;
69 char *xenstore_read_str(const char *base, const char *node)
71 char abspath[XEN_BUFSIZE];
72 unsigned int len;
73 char *str, *ret = NULL;
75 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
76 str = xs_read(xenstore, 0, abspath, &len);
77 if (str != NULL) {
78 /* move to qemu-allocated memory to make sure
79 * callers can savely g_free() stuff. */
80 ret = g_strdup(str);
81 free(str);
83 return ret;
86 int xenstore_write_int(const char *base, const char *node, int ival)
88 char val[12];
90 snprintf(val, sizeof(val), "%d", ival);
91 return xenstore_write_str(base, node, val);
94 int xenstore_write_int64(const char *base, const char *node, int64_t ival)
96 char val[21];
98 snprintf(val, sizeof(val), "%"PRId64, ival);
99 return xenstore_write_str(base, node, val);
102 int xenstore_read_int(const char *base, const char *node, int *ival)
104 char *val;
105 int rc = -1;
107 val = xenstore_read_str(base, node);
108 if (val && 1 == sscanf(val, "%d", ival)) {
109 rc = 0;
111 g_free(val);
112 return rc;
115 int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
117 return xenstore_write_str(xendev->be, node, val);
120 int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
122 return xenstore_write_int(xendev->be, node, ival);
125 int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
127 return xenstore_write_int64(xendev->be, node, ival);
130 char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
132 return xenstore_read_str(xendev->be, node);
135 int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
137 return xenstore_read_int(xendev->be, node, ival);
140 char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
142 return xenstore_read_str(xendev->fe, node);
145 int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
147 return xenstore_read_int(xendev->fe, node, ival);
150 /* ------------------------------------------------------------- */
152 const char *xenbus_strstate(enum xenbus_state state)
154 static const char *const name[] = {
155 [ XenbusStateUnknown ] = "Unknown",
156 [ XenbusStateInitialising ] = "Initialising",
157 [ XenbusStateInitWait ] = "InitWait",
158 [ XenbusStateInitialised ] = "Initialised",
159 [ XenbusStateConnected ] = "Connected",
160 [ XenbusStateClosing ] = "Closing",
161 [ XenbusStateClosed ] = "Closed",
163 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
166 int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
168 int rc;
170 rc = xenstore_write_be_int(xendev, "state", state);
171 if (rc < 0) {
172 return rc;
174 xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
175 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
176 xendev->be_state = state;
177 return 0;
180 /* ------------------------------------------------------------- */
182 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
184 struct XenDevice *xendev;
186 QTAILQ_FOREACH(xendev, &xendevs, next) {
187 if (xendev->dom != dom) {
188 continue;
190 if (xendev->dev != dev) {
191 continue;
193 if (strcmp(xendev->type, type) != 0) {
194 continue;
196 return xendev;
198 return NULL;
202 * get xen backend device, allocate a new one if it doesn't exist.
204 static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
205 struct XenDevOps *ops)
207 struct XenDevice *xendev;
208 char *dom0;
210 xendev = xen_be_find_xendev(type, dom, dev);
211 if (xendev) {
212 return xendev;
215 /* init new xendev */
216 xendev = g_malloc0(ops->size);
217 xendev->type = type;
218 xendev->dom = dom;
219 xendev->dev = dev;
220 xendev->ops = ops;
222 dom0 = xs_get_domain_path(xenstore, 0);
223 snprintf(xendev->be, sizeof(xendev->be), "%s/backend/%s/%d/%d",
224 dom0, xendev->type, xendev->dom, xendev->dev);
225 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
226 xendev->type, xendev->dev);
227 free(dom0);
229 xendev->debug = debug;
230 xendev->local_port = -1;
232 xendev->evtchndev = xen_xc_evtchn_open(NULL, 0);
233 if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
234 xen_be_printf(NULL, 0, "can't open evtchn device\n");
235 g_free(xendev);
236 return NULL;
238 fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
240 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
241 xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
242 if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
243 xen_be_printf(NULL, 0, "can't open gnttab device\n");
244 xc_evtchn_close(xendev->evtchndev);
245 g_free(xendev);
246 return NULL;
248 } else {
249 xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE;
252 QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
254 if (xendev->ops->alloc) {
255 xendev->ops->alloc(xendev);
258 return xendev;
262 * release xen backend device.
264 static struct XenDevice *xen_be_del_xendev(int dom, int dev)
266 struct XenDevice *xendev, *xnext;
269 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
270 * we save the next pointer in xnext because we might free xendev.
272 xnext = xendevs.tqh_first;
273 while (xnext) {
274 xendev = xnext;
275 xnext = xendev->next.tqe_next;
277 if (xendev->dom != dom) {
278 continue;
280 if (xendev->dev != dev && dev != -1) {
281 continue;
284 if (xendev->ops->free) {
285 xendev->ops->free(xendev);
288 if (xendev->fe) {
289 char token[XEN_BUFSIZE];
290 snprintf(token, sizeof(token), "fe:%p", xendev);
291 xs_unwatch(xenstore, xendev->fe, token);
292 g_free(xendev->fe);
295 if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
296 xc_evtchn_close(xendev->evtchndev);
298 if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
299 xc_gnttab_close(xendev->gnttabdev);
302 QTAILQ_REMOVE(&xendevs, xendev, next);
303 g_free(xendev);
305 return NULL;
309 * Sync internal data structures on xenstore updates.
310 * Node specifies the changed field. node = NULL means
311 * update all fields (used for initialization).
313 static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
315 if (node == NULL || strcmp(node, "online") == 0) {
316 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
317 xendev->online = 0;
321 if (node) {
322 xen_be_printf(xendev, 2, "backend update: %s\n", node);
323 if (xendev->ops->backend_changed) {
324 xendev->ops->backend_changed(xendev, node);
329 static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
331 int fe_state;
333 if (node == NULL || strcmp(node, "state") == 0) {
334 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
335 fe_state = XenbusStateUnknown;
337 if (xendev->fe_state != fe_state) {
338 xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
339 xenbus_strstate(xendev->fe_state),
340 xenbus_strstate(fe_state));
342 xendev->fe_state = fe_state;
344 if (node == NULL || strcmp(node, "protocol") == 0) {
345 g_free(xendev->protocol);
346 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
347 if (xendev->protocol) {
348 xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol);
352 if (node) {
353 xen_be_printf(xendev, 2, "frontend update: %s\n", node);
354 if (xendev->ops->frontend_changed) {
355 xendev->ops->frontend_changed(xendev, node);
360 /* ------------------------------------------------------------- */
361 /* Check for possible state transitions and perform them. */
364 * Initial xendev setup. Read frontend path, register watch for it.
365 * Should succeed once xend finished setting up the backend device.
367 * Also sets initial state (-> Initializing) when done. Which
368 * only affects the xendev->be_state variable as xenbus should
369 * already be put into that state by xend.
371 static int xen_be_try_setup(struct XenDevice *xendev)
373 char token[XEN_BUFSIZE];
374 int be_state;
376 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
377 xen_be_printf(xendev, 0, "reading backend state failed\n");
378 return -1;
381 if (be_state != XenbusStateInitialising) {
382 xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
383 xenbus_strstate(be_state));
384 return -1;
387 xendev->fe = xenstore_read_be_str(xendev, "frontend");
388 if (xendev->fe == NULL) {
389 xen_be_printf(xendev, 0, "reading frontend path failed\n");
390 return -1;
393 /* setup frontend watch */
394 snprintf(token, sizeof(token), "fe:%p", xendev);
395 if (!xs_watch(xenstore, xendev->fe, token)) {
396 xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
397 xendev->fe);
398 return -1;
400 xen_be_set_state(xendev, XenbusStateInitialising);
402 xen_be_backend_changed(xendev, NULL);
403 xen_be_frontend_changed(xendev, NULL);
404 return 0;
408 * Try initialize xendev. Prepare everything the backend can do
409 * without synchronizing with the frontend. Fakes hotplug-status. No
410 * hotplug involved here because this is about userspace drivers, thus
411 * there are kernel backend devices which could invoke hotplug.
413 * Goes to InitWait on success.
415 static int xen_be_try_init(struct XenDevice *xendev)
417 int rc = 0;
419 if (!xendev->online) {
420 xen_be_printf(xendev, 1, "not online\n");
421 return -1;
424 if (xendev->ops->init) {
425 rc = xendev->ops->init(xendev);
427 if (rc != 0) {
428 xen_be_printf(xendev, 1, "init() failed\n");
429 return rc;
432 xenstore_write_be_str(xendev, "hotplug-status", "connected");
433 xen_be_set_state(xendev, XenbusStateInitWait);
434 return 0;
438 * Try to initialise xendev. Depends on the frontend being ready
439 * for it (shared ring and evtchn info in xenstore, state being
440 * Initialised or Connected).
442 * Goes to Connected on success.
444 static int xen_be_try_initialise(struct XenDevice *xendev)
446 int rc = 0;
448 if (xendev->fe_state != XenbusStateInitialised &&
449 xendev->fe_state != XenbusStateConnected) {
450 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
451 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
452 } else {
453 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
454 return -1;
458 if (xendev->ops->initialise) {
459 rc = xendev->ops->initialise(xendev);
461 if (rc != 0) {
462 xen_be_printf(xendev, 0, "initialise() failed\n");
463 return rc;
466 xen_be_set_state(xendev, XenbusStateConnected);
467 return 0;
471 * Try to let xendev know that it is connected. Depends on the
472 * frontend being Connected. Note that this may be called more
473 * than once since the backend state is not modified.
475 static void xen_be_try_connected(struct XenDevice *xendev)
477 if (!xendev->ops->connected) {
478 return;
481 if (xendev->fe_state != XenbusStateConnected) {
482 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
483 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
484 } else {
485 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
486 return;
490 xendev->ops->connected(xendev);
494 * Teardown connection.
496 * Goes to Closed when done.
498 static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
500 if (xendev->be_state != XenbusStateClosing &&
501 xendev->be_state != XenbusStateClosed &&
502 xendev->ops->disconnect) {
503 xendev->ops->disconnect(xendev);
505 if (xendev->be_state != state) {
506 xen_be_set_state(xendev, state);
511 * Try to reset xendev, for reconnection by another frontend instance.
513 static int xen_be_try_reset(struct XenDevice *xendev)
515 if (xendev->fe_state != XenbusStateInitialising) {
516 return -1;
519 xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
520 xen_be_set_state(xendev, XenbusStateInitialising);
521 return 0;
525 * state change dispatcher function
527 void xen_be_check_state(struct XenDevice *xendev)
529 int rc = 0;
531 /* frontend may request shutdown from almost anywhere */
532 if (xendev->fe_state == XenbusStateClosing ||
533 xendev->fe_state == XenbusStateClosed) {
534 xen_be_disconnect(xendev, xendev->fe_state);
535 return;
538 /* check for possible backend state transitions */
539 for (;;) {
540 switch (xendev->be_state) {
541 case XenbusStateUnknown:
542 rc = xen_be_try_setup(xendev);
543 break;
544 case XenbusStateInitialising:
545 rc = xen_be_try_init(xendev);
546 break;
547 case XenbusStateInitWait:
548 rc = xen_be_try_initialise(xendev);
549 break;
550 case XenbusStateConnected:
551 /* xendev->be_state doesn't change */
552 xen_be_try_connected(xendev);
553 rc = -1;
554 break;
555 case XenbusStateClosed:
556 rc = xen_be_try_reset(xendev);
557 break;
558 default:
559 rc = -1;
561 if (rc != 0) {
562 break;
567 /* ------------------------------------------------------------- */
569 static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
571 struct XenDevice *xendev;
572 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
573 char **dev = NULL, *dom0;
574 unsigned int cdev, j;
576 /* setup watch */
577 dom0 = xs_get_domain_path(xenstore, 0);
578 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
579 snprintf(path, sizeof(path), "%s/backend/%s/%d", dom0, type, dom);
580 free(dom0);
581 if (!xs_watch(xenstore, path, token)) {
582 xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path);
583 return -1;
586 /* look for backends */
587 dev = xs_directory(xenstore, 0, path, &cdev);
588 if (!dev) {
589 return 0;
591 for (j = 0; j < cdev; j++) {
592 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
593 if (xendev == NULL) {
594 continue;
596 xen_be_check_state(xendev);
598 free(dev);
599 return 0;
602 static void xenstore_update_be(char *watch, char *type, int dom,
603 struct XenDevOps *ops)
605 struct XenDevice *xendev;
606 char path[XEN_BUFSIZE], *dom0, *bepath;
607 unsigned int len, dev;
609 dom0 = xs_get_domain_path(xenstore, 0);
610 len = snprintf(path, sizeof(path), "%s/backend/%s/%d", dom0, type, dom);
611 free(dom0);
612 if (strncmp(path, watch, len) != 0) {
613 return;
615 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
616 strcpy(path, "");
617 if (sscanf(watch+len, "/%u", &dev) != 1) {
618 dev = -1;
621 if (dev == -1) {
622 return;
625 xendev = xen_be_get_xendev(type, dom, dev, ops);
626 if (xendev != NULL) {
627 bepath = xs_read(xenstore, 0, xendev->be, &len);
628 if (bepath == NULL) {
629 xen_be_del_xendev(dom, dev);
630 } else {
631 free(bepath);
632 xen_be_backend_changed(xendev, path);
633 xen_be_check_state(xendev);
638 static void xenstore_update_fe(char *watch, struct XenDevice *xendev)
640 char *node;
641 unsigned int len;
643 len = strlen(xendev->fe);
644 if (strncmp(xendev->fe, watch, len) != 0) {
645 return;
647 if (watch[len] != '/') {
648 return;
650 node = watch + len + 1;
652 xen_be_frontend_changed(xendev, node);
653 xen_be_check_state(xendev);
656 static void xenstore_update(void *unused)
658 char **vec = NULL;
659 intptr_t type, ops, ptr;
660 unsigned int dom, count;
662 vec = xs_read_watch(xenstore, &count);
663 if (vec == NULL) {
664 goto cleanup;
667 if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR,
668 &type, &dom, &ops) == 3) {
669 xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops);
671 if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
672 xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
675 cleanup:
676 free(vec);
679 static void xen_be_evtchn_event(void *opaque)
681 struct XenDevice *xendev = opaque;
682 evtchn_port_t port;
684 port = xc_evtchn_pending(xendev->evtchndev);
685 if (port != xendev->local_port) {
686 xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
687 port, xendev->local_port);
688 return;
690 xc_evtchn_unmask(xendev->evtchndev, port);
692 if (xendev->ops->event) {
693 xendev->ops->event(xendev);
697 /* -------------------------------------------------------------------- */
699 int xen_be_init(void)
701 xenstore = xs_daemon_open();
702 if (!xenstore) {
703 xen_be_printf(NULL, 0, "can't connect to xenstored\n");
704 return -1;
707 if (qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL) < 0) {
708 goto err;
711 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
712 /* Check if xen_init() have been called */
713 goto err;
715 return 0;
717 err:
718 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
719 xs_daemon_close(xenstore);
720 xenstore = NULL;
722 return -1;
725 int xen_be_register(const char *type, struct XenDevOps *ops)
727 return xenstore_scan(type, xen_domid, ops);
730 int xen_be_bind_evtchn(struct XenDevice *xendev)
732 if (xendev->local_port != -1) {
733 return 0;
735 xendev->local_port = xc_evtchn_bind_interdomain
736 (xendev->evtchndev, xendev->dom, xendev->remote_port);
737 if (xendev->local_port == -1) {
738 xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
739 return -1;
741 xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
742 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
743 xen_be_evtchn_event, NULL, xendev);
744 return 0;
747 void xen_be_unbind_evtchn(struct XenDevice *xendev)
749 if (xendev->local_port == -1) {
750 return;
752 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
753 xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
754 xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
755 xendev->local_port = -1;
758 int xen_be_send_notify(struct XenDevice *xendev)
760 return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
764 * msg_level:
765 * 0 == errors (stderr + logfile).
766 * 1 == informative debug messages (logfile only).
767 * 2 == noisy debug messages (logfile only).
768 * 3 == will flood your log (logfile only).
770 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
772 va_list args;
774 if (xendev) {
775 if (msg_level > xendev->debug) {
776 return;
778 qemu_log("xen be: %s: ", xendev->name);
779 if (msg_level == 0) {
780 fprintf(stderr, "xen be: %s: ", xendev->name);
782 } else {
783 if (msg_level > debug) {
784 return;
786 qemu_log("xen be core: ");
787 if (msg_level == 0) {
788 fprintf(stderr, "xen be core: ");
791 va_start(args, fmt);
792 qemu_log_vprintf(fmt, args);
793 va_end(args);
794 if (msg_level == 0) {
795 va_start(args, fmt);
796 vfprintf(stderr, fmt, args);
797 va_end(args);
799 qemu_log_flush();