2
0

xen-common.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2014 Citrix Systems UK Ltd.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. *
  7. * Contributions after 2012-01-13 are licensed under the terms of the
  8. * GNU GPL, version 2 or (at your option) any later version.
  9. */
  10. #include "hw/xen/xen_backend.h"
  11. #include "qmp-commands.h"
  12. #include "sysemu/char.h"
  13. #include "sysemu/accel.h"
  14. //#define DEBUG_XEN
  15. #ifdef DEBUG_XEN
  16. #define DPRINTF(fmt, ...) \
  17. do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
  18. #else
  19. #define DPRINTF(fmt, ...) \
  20. do { } while (0)
  21. #endif
  22. static int store_dev_info(int domid, CharDriverState *cs, const char *string)
  23. {
  24. struct xs_handle *xs = NULL;
  25. char *path = NULL;
  26. char *newpath = NULL;
  27. char *pts = NULL;
  28. int ret = -1;
  29. /* Only continue if we're talking to a pty. */
  30. if (strncmp(cs->filename, "pty:", 4)) {
  31. return 0;
  32. }
  33. pts = cs->filename + 4;
  34. /* We now have everything we need to set the xenstore entry. */
  35. xs = xs_open(0);
  36. if (xs == NULL) {
  37. fprintf(stderr, "Could not contact XenStore\n");
  38. goto out;
  39. }
  40. path = xs_get_domain_path(xs, domid);
  41. if (path == NULL) {
  42. fprintf(stderr, "xs_get_domain_path() error\n");
  43. goto out;
  44. }
  45. newpath = realloc(path, (strlen(path) + strlen(string) +
  46. strlen("/tty") + 1));
  47. if (newpath == NULL) {
  48. fprintf(stderr, "realloc error\n");
  49. goto out;
  50. }
  51. path = newpath;
  52. strcat(path, string);
  53. strcat(path, "/tty");
  54. if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
  55. fprintf(stderr, "xs_write for '%s' fail", string);
  56. goto out;
  57. }
  58. ret = 0;
  59. out:
  60. free(path);
  61. xs_close(xs);
  62. return ret;
  63. }
  64. void xenstore_store_pv_console_info(int i, CharDriverState *chr)
  65. {
  66. if (i == 0) {
  67. store_dev_info(xen_domid, chr, "/console");
  68. } else {
  69. char buf[32];
  70. snprintf(buf, sizeof(buf), "/device/console/%d", i);
  71. store_dev_info(xen_domid, chr, buf);
  72. }
  73. }
  74. static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
  75. {
  76. char path[50];
  77. if (xs == NULL) {
  78. fprintf(stderr, "xenstore connection not initialized\n");
  79. exit(1);
  80. }
  81. snprintf(path, sizeof (path), "device-model/%u/state", xen_domid);
  82. if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
  83. fprintf(stderr, "error recording dm state\n");
  84. exit(1);
  85. }
  86. }
  87. static void xen_change_state_handler(void *opaque, int running,
  88. RunState state)
  89. {
  90. if (running) {
  91. /* record state running */
  92. xenstore_record_dm_state(xenstore, "running");
  93. }
  94. }
  95. static int xen_init(MachineState *ms)
  96. {
  97. xen_xc = xen_xc_interface_open(0, 0, 0);
  98. if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
  99. xen_be_printf(NULL, 0, "can't open xen interface\n");
  100. return -1;
  101. }
  102. qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
  103. return 0;
  104. }
  105. static void xen_accel_class_init(ObjectClass *oc, void *data)
  106. {
  107. AccelClass *ac = ACCEL_CLASS(oc);
  108. ac->name = "Xen";
  109. ac->init_machine = xen_init;
  110. ac->allowed = &xen_allowed;
  111. }
  112. #define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen")
  113. static const TypeInfo xen_accel_type = {
  114. .name = TYPE_XEN_ACCEL,
  115. .parent = TYPE_ACCEL,
  116. .class_init = xen_accel_class_init,
  117. };
  118. static void xen_type_init(void)
  119. {
  120. type_register_static(&xen_accel_type);
  121. }
  122. type_init(xen_type_init);