xen-common.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "qemu/osdep.h"
  11. #include "qemu/error-report.h"
  12. #include "qemu/module.h"
  13. #include "hw/xen/xen-legacy-backend.h"
  14. #include "chardev/char.h"
  15. #include "sysemu/accel.h"
  16. #include "sysemu/runstate.h"
  17. #include "migration/misc.h"
  18. #include "migration/global_state.h"
  19. //#define DEBUG_XEN
  20. #ifdef DEBUG_XEN
  21. #define DPRINTF(fmt, ...) \
  22. do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
  23. #else
  24. #define DPRINTF(fmt, ...) \
  25. do { } while (0)
  26. #endif
  27. xc_interface *xen_xc;
  28. xenforeignmemory_handle *xen_fmem;
  29. xendevicemodel_handle *xen_dmod;
  30. static int store_dev_info(int domid, Chardev *cs, const char *string)
  31. {
  32. struct xs_handle *xs = NULL;
  33. char *path = NULL;
  34. char *newpath = NULL;
  35. char *pts = NULL;
  36. int ret = -1;
  37. /* Only continue if we're talking to a pty. */
  38. if (!CHARDEV_IS_PTY(cs)) {
  39. return 0;
  40. }
  41. pts = cs->filename + 4;
  42. /* We now have everything we need to set the xenstore entry. */
  43. xs = xs_open(0);
  44. if (xs == NULL) {
  45. fprintf(stderr, "Could not contact XenStore\n");
  46. goto out;
  47. }
  48. path = xs_get_domain_path(xs, domid);
  49. if (path == NULL) {
  50. fprintf(stderr, "xs_get_domain_path() error\n");
  51. goto out;
  52. }
  53. newpath = realloc(path, (strlen(path) + strlen(string) +
  54. strlen("/tty") + 1));
  55. if (newpath == NULL) {
  56. fprintf(stderr, "realloc error\n");
  57. goto out;
  58. }
  59. path = newpath;
  60. strcat(path, string);
  61. strcat(path, "/tty");
  62. if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
  63. fprintf(stderr, "xs_write for '%s' fail", string);
  64. goto out;
  65. }
  66. ret = 0;
  67. out:
  68. free(path);
  69. xs_close(xs);
  70. return ret;
  71. }
  72. void xenstore_store_pv_console_info(int i, Chardev *chr)
  73. {
  74. if (i == 0) {
  75. store_dev_info(xen_domid, chr, "/console");
  76. } else {
  77. char buf[32];
  78. snprintf(buf, sizeof(buf), "/device/console/%d", i);
  79. store_dev_info(xen_domid, chr, buf);
  80. }
  81. }
  82. static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
  83. {
  84. char path[50];
  85. if (xs == NULL) {
  86. error_report("xenstore connection not initialized");
  87. exit(1);
  88. }
  89. snprintf(path, sizeof (path), "device-model/%u/state", xen_domid);
  90. /*
  91. * This call may fail when running restricted so don't make it fatal in
  92. * that case. Toolstacks should instead use QMP to listen for state changes.
  93. */
  94. if (!xs_write(xs, XBT_NULL, path, state, strlen(state)) &&
  95. !xen_domid_restrict) {
  96. error_report("error recording dm state");
  97. exit(1);
  98. }
  99. }
  100. static void xen_change_state_handler(void *opaque, int running,
  101. RunState state)
  102. {
  103. if (running) {
  104. /* record state running */
  105. xenstore_record_dm_state(xenstore, "running");
  106. }
  107. }
  108. static void xen_setup_post(MachineState *ms, AccelState *accel)
  109. {
  110. int rc;
  111. if (xen_domid_restrict) {
  112. rc = xen_restrict(xen_domid);
  113. if (rc < 0) {
  114. perror("xen: failed to restrict");
  115. exit(1);
  116. }
  117. }
  118. }
  119. static int xen_init(MachineState *ms)
  120. {
  121. xen_xc = xc_interface_open(0, 0, 0);
  122. if (xen_xc == NULL) {
  123. xen_pv_printf(NULL, 0, "can't open xen interface\n");
  124. return -1;
  125. }
  126. xen_fmem = xenforeignmemory_open(0, 0);
  127. if (xen_fmem == NULL) {
  128. xen_pv_printf(NULL, 0, "can't open xen fmem interface\n");
  129. xc_interface_close(xen_xc);
  130. return -1;
  131. }
  132. xen_dmod = xendevicemodel_open(0, 0);
  133. if (xen_dmod == NULL) {
  134. xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n");
  135. xenforeignmemory_close(xen_fmem);
  136. xc_interface_close(xen_xc);
  137. return -1;
  138. }
  139. qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
  140. return 0;
  141. }
  142. static void xen_accel_class_init(ObjectClass *oc, void *data)
  143. {
  144. AccelClass *ac = ACCEL_CLASS(oc);
  145. static GlobalProperty compat[] = {
  146. { "migration", "store-global-state", "off" },
  147. { "migration", "send-configuration", "off" },
  148. { "migration", "send-section-footer", "off" },
  149. };
  150. ac->name = "Xen";
  151. ac->init_machine = xen_init;
  152. ac->setup_post = xen_setup_post;
  153. ac->allowed = &xen_allowed;
  154. ac->compat_props = g_ptr_array_new();
  155. compat_props_add(ac->compat_props, compat, G_N_ELEMENTS(compat));
  156. }
  157. #define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen")
  158. static const TypeInfo xen_accel_type = {
  159. .name = TYPE_XEN_ACCEL,
  160. .parent = TYPE_ACCEL,
  161. .class_init = xen_accel_class_init,
  162. };
  163. static void xen_type_init(void)
  164. {
  165. type_register_static(&xen_accel_type);
  166. }
  167. type_init(xen_type_init);