target-i386: Move breakpoint related functions to new file
[qemu/ar7.git] / hw / xen / xen_backend.c
blob2510e2e4ff454ea3b65d9cdbff2822c8e7b4f50c
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 struct xs_handle *xenstore = NULL;
49 const char *xen_protocol;
51 /* private */
52 static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
53 static int debug = 0;
55 /* ------------------------------------------------------------- */
57 int xenstore_write_str(const char *base, const char *node, const char *val)
59 char abspath[XEN_BUFSIZE];
61 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
62 if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
63 return -1;
65 return 0;
68 char *xenstore_read_str(const char *base, const char *node)
70 char abspath[XEN_BUFSIZE];
71 unsigned int len;
72 char *str, *ret = NULL;
74 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
75 str = xs_read(xenstore, 0, abspath, &len);
76 if (str != NULL) {
77 /* move to qemu-allocated memory to make sure
78 * callers can savely g_free() stuff. */
79 ret = g_strdup(str);
80 free(str);
82 return ret;
85 int xenstore_write_int(const char *base, const char *node, int ival)
87 char val[12];
89 snprintf(val, sizeof(val), "%d", ival);
90 return xenstore_write_str(base, node, val);
93 int xenstore_write_int64(const char *base, const char *node, int64_t ival)
95 char val[21];
97 snprintf(val, sizeof(val), "%"PRId64, ival);
98 return xenstore_write_str(base, node, val);
101 int xenstore_read_int(const char *base, const char *node, int *ival)
103 char *val;
104 int rc = -1;
106 val = xenstore_read_str(base, node);
107 if (val && 1 == sscanf(val, "%d", ival)) {
108 rc = 0;
110 g_free(val);
111 return rc;
114 int xenstore_read_uint64(const char *base, const char *node, uint64_t *uval)
116 char *val;
117 int rc = -1;
119 val = xenstore_read_str(base, node);
120 if (val && 1 == sscanf(val, "%"SCNu64, uval)) {
121 rc = 0;
123 g_free(val);
124 return rc;
127 int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
129 return xenstore_write_str(xendev->be, node, val);
132 int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
134 return xenstore_write_int(xendev->be, node, ival);
137 int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
139 return xenstore_write_int64(xendev->be, node, ival);
142 char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
144 return xenstore_read_str(xendev->be, node);
147 int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
149 return xenstore_read_int(xendev->be, node, ival);
152 char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
154 return xenstore_read_str(xendev->fe, node);
157 int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
159 return xenstore_read_int(xendev->fe, node, ival);
162 int xenstore_read_fe_uint64(struct XenDevice *xendev, const char *node, uint64_t *uval)
164 return xenstore_read_uint64(xendev->fe, node, uval);
167 /* ------------------------------------------------------------- */
169 const char *xenbus_strstate(enum xenbus_state state)
171 static const char *const name[] = {
172 [ XenbusStateUnknown ] = "Unknown",
173 [ XenbusStateInitialising ] = "Initialising",
174 [ XenbusStateInitWait ] = "InitWait",
175 [ XenbusStateInitialised ] = "Initialised",
176 [ XenbusStateConnected ] = "Connected",
177 [ XenbusStateClosing ] = "Closing",
178 [ XenbusStateClosed ] = "Closed",
180 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
183 int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
185 int rc;
187 rc = xenstore_write_be_int(xendev, "state", state);
188 if (rc < 0) {
189 return rc;
191 xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
192 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
193 xendev->be_state = state;
194 return 0;
197 /* ------------------------------------------------------------- */
199 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
201 struct XenDevice *xendev;
203 QTAILQ_FOREACH(xendev, &xendevs, next) {
204 if (xendev->dom != dom) {
205 continue;
207 if (xendev->dev != dev) {
208 continue;
210 if (strcmp(xendev->type, type) != 0) {
211 continue;
213 return xendev;
215 return NULL;
219 * get xen backend device, allocate a new one if it doesn't exist.
221 static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
222 struct XenDevOps *ops)
224 struct XenDevice *xendev;
226 xendev = xen_be_find_xendev(type, dom, dev);
227 if (xendev) {
228 return xendev;
231 /* init new xendev */
232 xendev = g_malloc0(ops->size);
233 xendev->type = type;
234 xendev->dom = dom;
235 xendev->dev = dev;
236 xendev->ops = ops;
238 snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
239 xendev->type, xendev->dom, xendev->dev);
240 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
241 xendev->type, xendev->dev);
243 xendev->debug = debug;
244 xendev->local_port = -1;
246 xendev->evtchndev = xen_xc_evtchn_open(NULL, 0);
247 if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
248 xen_be_printf(NULL, 0, "can't open evtchn device\n");
249 g_free(xendev);
250 return NULL;
252 fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
254 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
255 xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
256 if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
257 xen_be_printf(NULL, 0, "can't open gnttab device\n");
258 xc_evtchn_close(xendev->evtchndev);
259 g_free(xendev);
260 return NULL;
262 } else {
263 xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE;
266 QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
268 if (xendev->ops->alloc) {
269 xendev->ops->alloc(xendev);
272 return xendev;
276 * release xen backend device.
278 static struct XenDevice *xen_be_del_xendev(int dom, int dev)
280 struct XenDevice *xendev, *xnext;
283 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
284 * we save the next pointer in xnext because we might free xendev.
286 xnext = xendevs.tqh_first;
287 while (xnext) {
288 xendev = xnext;
289 xnext = xendev->next.tqe_next;
291 if (xendev->dom != dom) {
292 continue;
294 if (xendev->dev != dev && dev != -1) {
295 continue;
298 if (xendev->ops->free) {
299 xendev->ops->free(xendev);
302 if (xendev->fe) {
303 char token[XEN_BUFSIZE];
304 snprintf(token, sizeof(token), "fe:%p", xendev);
305 xs_unwatch(xenstore, xendev->fe, token);
306 g_free(xendev->fe);
309 if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
310 xc_evtchn_close(xendev->evtchndev);
312 if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
313 xc_gnttab_close(xendev->gnttabdev);
316 QTAILQ_REMOVE(&xendevs, xendev, next);
317 g_free(xendev);
319 return NULL;
323 * Sync internal data structures on xenstore updates.
324 * Node specifies the changed field. node = NULL means
325 * update all fields (used for initialization).
327 static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
329 if (node == NULL || strcmp(node, "online") == 0) {
330 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
331 xendev->online = 0;
335 if (node) {
336 xen_be_printf(xendev, 2, "backend update: %s\n", node);
337 if (xendev->ops->backend_changed) {
338 xendev->ops->backend_changed(xendev, node);
343 static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
345 int fe_state;
347 if (node == NULL || strcmp(node, "state") == 0) {
348 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
349 fe_state = XenbusStateUnknown;
351 if (xendev->fe_state != fe_state) {
352 xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
353 xenbus_strstate(xendev->fe_state),
354 xenbus_strstate(fe_state));
356 xendev->fe_state = fe_state;
358 if (node == NULL || strcmp(node, "protocol") == 0) {
359 g_free(xendev->protocol);
360 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
361 if (xendev->protocol) {
362 xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol);
366 if (node) {
367 xen_be_printf(xendev, 2, "frontend update: %s\n", node);
368 if (xendev->ops->frontend_changed) {
369 xendev->ops->frontend_changed(xendev, node);
374 /* ------------------------------------------------------------- */
375 /* Check for possible state transitions and perform them. */
378 * Initial xendev setup. Read frontend path, register watch for it.
379 * Should succeed once xend finished setting up the backend device.
381 * Also sets initial state (-> Initializing) when done. Which
382 * only affects the xendev->be_state variable as xenbus should
383 * already be put into that state by xend.
385 static int xen_be_try_setup(struct XenDevice *xendev)
387 char token[XEN_BUFSIZE];
388 int be_state;
390 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
391 xen_be_printf(xendev, 0, "reading backend state failed\n");
392 return -1;
395 if (be_state != XenbusStateInitialising) {
396 xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
397 xenbus_strstate(be_state));
398 return -1;
401 xendev->fe = xenstore_read_be_str(xendev, "frontend");
402 if (xendev->fe == NULL) {
403 xen_be_printf(xendev, 0, "reading frontend path failed\n");
404 return -1;
407 /* setup frontend watch */
408 snprintf(token, sizeof(token), "fe:%p", xendev);
409 if (!xs_watch(xenstore, xendev->fe, token)) {
410 xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
411 xendev->fe);
412 return -1;
414 xen_be_set_state(xendev, XenbusStateInitialising);
416 xen_be_backend_changed(xendev, NULL);
417 xen_be_frontend_changed(xendev, NULL);
418 return 0;
422 * Try initialize xendev. Prepare everything the backend can do
423 * without synchronizing with the frontend. Fakes hotplug-status. No
424 * hotplug involved here because this is about userspace drivers, thus
425 * there are kernel backend devices which could invoke hotplug.
427 * Goes to InitWait on success.
429 static int xen_be_try_init(struct XenDevice *xendev)
431 int rc = 0;
433 if (!xendev->online) {
434 xen_be_printf(xendev, 1, "not online\n");
435 return -1;
438 if (xendev->ops->init) {
439 rc = xendev->ops->init(xendev);
441 if (rc != 0) {
442 xen_be_printf(xendev, 1, "init() failed\n");
443 return rc;
446 xenstore_write_be_str(xendev, "hotplug-status", "connected");
447 xen_be_set_state(xendev, XenbusStateInitWait);
448 return 0;
452 * Try to initialise xendev. Depends on the frontend being ready
453 * for it (shared ring and evtchn info in xenstore, state being
454 * Initialised or Connected).
456 * Goes to Connected on success.
458 static int xen_be_try_initialise(struct XenDevice *xendev)
460 int rc = 0;
462 if (xendev->fe_state != XenbusStateInitialised &&
463 xendev->fe_state != XenbusStateConnected) {
464 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
465 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
466 } else {
467 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
468 return -1;
472 if (xendev->ops->initialise) {
473 rc = xendev->ops->initialise(xendev);
475 if (rc != 0) {
476 xen_be_printf(xendev, 0, "initialise() failed\n");
477 return rc;
480 xen_be_set_state(xendev, XenbusStateConnected);
481 return 0;
485 * Try to let xendev know that it is connected. Depends on the
486 * frontend being Connected. Note that this may be called more
487 * than once since the backend state is not modified.
489 static void xen_be_try_connected(struct XenDevice *xendev)
491 if (!xendev->ops->connected) {
492 return;
495 if (xendev->fe_state != XenbusStateConnected) {
496 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
497 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
498 } else {
499 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
500 return;
504 xendev->ops->connected(xendev);
508 * Teardown connection.
510 * Goes to Closed when done.
512 static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
514 if (xendev->be_state != XenbusStateClosing &&
515 xendev->be_state != XenbusStateClosed &&
516 xendev->ops->disconnect) {
517 xendev->ops->disconnect(xendev);
519 if (xendev->be_state != state) {
520 xen_be_set_state(xendev, state);
525 * Try to reset xendev, for reconnection by another frontend instance.
527 static int xen_be_try_reset(struct XenDevice *xendev)
529 if (xendev->fe_state != XenbusStateInitialising) {
530 return -1;
533 xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
534 xen_be_set_state(xendev, XenbusStateInitialising);
535 return 0;
539 * state change dispatcher function
541 void xen_be_check_state(struct XenDevice *xendev)
543 int rc = 0;
545 /* frontend may request shutdown from almost anywhere */
546 if (xendev->fe_state == XenbusStateClosing ||
547 xendev->fe_state == XenbusStateClosed) {
548 xen_be_disconnect(xendev, xendev->fe_state);
549 return;
552 /* check for possible backend state transitions */
553 for (;;) {
554 switch (xendev->be_state) {
555 case XenbusStateUnknown:
556 rc = xen_be_try_setup(xendev);
557 break;
558 case XenbusStateInitialising:
559 rc = xen_be_try_init(xendev);
560 break;
561 case XenbusStateInitWait:
562 rc = xen_be_try_initialise(xendev);
563 break;
564 case XenbusStateConnected:
565 /* xendev->be_state doesn't change */
566 xen_be_try_connected(xendev);
567 rc = -1;
568 break;
569 case XenbusStateClosed:
570 rc = xen_be_try_reset(xendev);
571 break;
572 default:
573 rc = -1;
575 if (rc != 0) {
576 break;
581 /* ------------------------------------------------------------- */
583 static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
585 struct XenDevice *xendev;
586 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
587 char **dev = NULL;
588 unsigned int cdev, j;
590 /* setup watch */
591 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
592 snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
593 if (!xs_watch(xenstore, path, token)) {
594 xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path);
595 return -1;
598 /* look for backends */
599 dev = xs_directory(xenstore, 0, path, &cdev);
600 if (!dev) {
601 return 0;
603 for (j = 0; j < cdev; j++) {
604 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
605 if (xendev == NULL) {
606 continue;
608 xen_be_check_state(xendev);
610 free(dev);
611 return 0;
614 static void xenstore_update_be(char *watch, char *type, int dom,
615 struct XenDevOps *ops)
617 struct XenDevice *xendev;
618 char path[XEN_BUFSIZE], *bepath;
619 unsigned int len, dev;
621 len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
622 if (strncmp(path, watch, len) != 0) {
623 return;
625 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
626 strcpy(path, "");
627 if (sscanf(watch+len, "/%u", &dev) != 1) {
628 dev = -1;
631 if (dev == -1) {
632 return;
635 xendev = xen_be_get_xendev(type, dom, dev, ops);
636 if (xendev != NULL) {
637 bepath = xs_read(xenstore, 0, xendev->be, &len);
638 if (bepath == NULL) {
639 xen_be_del_xendev(dom, dev);
640 } else {
641 free(bepath);
642 xen_be_backend_changed(xendev, path);
643 xen_be_check_state(xendev);
648 static void xenstore_update_fe(char *watch, struct XenDevice *xendev)
650 char *node;
651 unsigned int len;
653 len = strlen(xendev->fe);
654 if (strncmp(xendev->fe, watch, len) != 0) {
655 return;
657 if (watch[len] != '/') {
658 return;
660 node = watch + len + 1;
662 xen_be_frontend_changed(xendev, node);
663 xen_be_check_state(xendev);
666 static void xenstore_update(void *unused)
668 char **vec = NULL;
669 intptr_t type, ops, ptr;
670 unsigned int dom, count;
672 vec = xs_read_watch(xenstore, &count);
673 if (vec == NULL) {
674 goto cleanup;
677 if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR,
678 &type, &dom, &ops) == 3) {
679 xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops);
681 if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
682 xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
685 cleanup:
686 free(vec);
689 static void xen_be_evtchn_event(void *opaque)
691 struct XenDevice *xendev = opaque;
692 evtchn_port_t port;
694 port = xc_evtchn_pending(xendev->evtchndev);
695 if (port != xendev->local_port) {
696 xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
697 port, xendev->local_port);
698 return;
700 xc_evtchn_unmask(xendev->evtchndev, port);
702 if (xendev->ops->event) {
703 xendev->ops->event(xendev);
707 /* -------------------------------------------------------------------- */
709 int xen_be_init(void)
711 xenstore = xs_daemon_open();
712 if (!xenstore) {
713 xen_be_printf(NULL, 0, "can't connect to xenstored\n");
714 return -1;
717 qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
719 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
720 /* Check if xen_init() have been called */
721 goto err;
723 return 0;
725 err:
726 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
727 xs_daemon_close(xenstore);
728 xenstore = NULL;
730 return -1;
733 int xen_be_register(const char *type, struct XenDevOps *ops)
735 return xenstore_scan(type, xen_domid, ops);
738 int xen_be_bind_evtchn(struct XenDevice *xendev)
740 if (xendev->local_port != -1) {
741 return 0;
743 xendev->local_port = xc_evtchn_bind_interdomain
744 (xendev->evtchndev, xendev->dom, xendev->remote_port);
745 if (xendev->local_port == -1) {
746 xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
747 return -1;
749 xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
750 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
751 xen_be_evtchn_event, NULL, xendev);
752 return 0;
755 void xen_be_unbind_evtchn(struct XenDevice *xendev)
757 if (xendev->local_port == -1) {
758 return;
760 qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
761 xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
762 xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
763 xendev->local_port = -1;
766 int xen_be_send_notify(struct XenDevice *xendev)
768 return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
772 * msg_level:
773 * 0 == errors (stderr + logfile).
774 * 1 == informative debug messages (logfile only).
775 * 2 == noisy debug messages (logfile only).
776 * 3 == will flood your log (logfile only).
778 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
780 va_list args;
782 if (xendev) {
783 if (msg_level > xendev->debug) {
784 return;
786 qemu_log("xen be: %s: ", xendev->name);
787 if (msg_level == 0) {
788 fprintf(stderr, "xen be: %s: ", xendev->name);
790 } else {
791 if (msg_level > debug) {
792 return;
794 qemu_log("xen be core: ");
795 if (msg_level == 0) {
796 fprintf(stderr, "xen be core: ");
799 va_start(args, fmt);
800 qemu_log_vprintf(fmt, args);
801 va_end(args);
802 if (msg_level == 0) {
803 va_start(args, fmt);
804 vfprintf(stderr, fmt, args);
805 va_end(args);
807 qemu_log_flush();