xen-common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "hw/xen/xen_backend.h"
  12. #include "qmp-commands.h"
  13. #include "chardev/char.h"
  14. #include "sysemu/accel.h"
  15. #include "migration/migration.h"
  16. //#define DEBUG_XEN
  17. #ifdef DEBUG_XEN
  18. #define DPRINTF(fmt, ...) \
  19. do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
  20. #else
  21. #define DPRINTF(fmt, ...) \
  22. do { } while (0)
  23. #endif
  24. xc_interface *xen_xc;
  25. xenforeignmemory_handle *xen_fmem;
  26. xendevicemodel_handle *xen_dmod;
  27. static int store_dev_info(int domid, Chardev *cs, const char *string)
  28. {
  29. struct xs_handle *xs = NULL;
  30. char *path = NULL;
  31. char *newpath = NULL;
  32. char *pts = NULL;
  33. int ret = -1;
  34. /* Only continue if we're talking to a pty. */
  35. if (!CHARDEV_IS_PTY(cs)) {
  36. return 0;
  37. }
  38. pts = cs->filename + 4;
  39. /* We now have everything we need to set the xenstore entry. */
  40. xs = xs_open(0);
  41. if (xs == NULL) {
  42. fprintf(stderr, "Could not contact XenStore\n");
  43. goto out;
  44. }
  45. path = xs_get_domain_path(xs, domid);
  46. if (path == NULL) {
  47. fprintf(stderr, "xs_get_domain_path() error\n");
  48. goto out;
  49. }
  50. newpath = realloc(path, (strlen(path) + strlen(string) +
  51. strlen("/tty") + 1));
  52. if (newpath == NULL) {
  53. fprintf(stderr, "realloc error\n");
  54. goto out;
  55. }
  56. path = newpath;
  57. strcat(path, string);
  58. strcat(path, "/tty");
  59. if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
  60. fprintf(stderr, "xs_write for '%s' fail", string);
  61. goto out;
  62. }
  63. ret = 0;
  64. out:
  65. free(path);
  66. xs_close(xs);
  67. return ret;
  68. }
  69. void xenstore_store_pv_console_info(int i, Chardev *chr)
  70. {
  71. if (i == 0) {
  72. store_dev_info(xen_domid, chr, "/console");
  73. } else {
  74. char buf[32];
  75. snprintf(buf, sizeof(buf), "/device/console/%d", i);
  76. store_dev_info(xen_domid, chr, buf);
  77. }
  78. }
  79. static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
  80. {
  81. char path[50];
  82. if (xs == NULL) {
  83. fprintf(stderr, "xenstore connection not initialized\n");
  84. exit(1);
  85. }
  86. snprintf(path, sizeof (path), "device-model/%u/state", xen_domid);
  87. if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
  88. fprintf(stderr, "error recording dm state\n");
  89. exit(1);
  90. }
  91. }
  92. static void xen_change_state_handler(void *opaque, int running,
  93. RunState state)
  94. {
  95. if (running) {
  96. /* record state running */
  97. xenstore_record_dm_state(xenstore, "running");
  98. }
  99. }
  100. static int xen_init(MachineState *ms)
  101. {
  102. xen_xc = xc_interface_open(0, 0, 0);
  103. if (xen_xc == NULL) {
  104. xen_pv_printf(NULL, 0, "can't open xen interface\n");
  105. return -1;
  106. }
  107. xen_fmem = xenforeignmemory_open(0, 0);
  108. if (xen_fmem == NULL) {
  109. xen_pv_printf(NULL, 0, "can't open xen fmem interface\n");
  110. xc_interface_close(xen_xc);
  111. return -1;
  112. }
  113. xen_dmod = xendevicemodel_open(0, 0);
  114. if (xen_dmod == NULL) {
  115. xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n");
  116. xenforeignmemory_close(xen_fmem);
  117. xc_interface_close(xen_xc);
  118. return -1;
  119. }
  120. qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
  121. global_state_set_optional();
  122. savevm_skip_configuration();
  123. savevm_skip_section_footers();
  124. return 0;
  125. }
  126. static void xen_accel_class_init(ObjectClass *oc, void *data)
  127. {
  128. AccelClass *ac = ACCEL_CLASS(oc);
  129. ac->name = "Xen";
  130. ac->init_machine = xen_init;
  131. ac->allowed = &xen_allowed;
  132. }
  133. #define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen")
  134. static const TypeInfo xen_accel_type = {
  135. .name = TYPE_XEN_ACCEL,
  136. .parent = TYPE_ACCEL,
  137. .class_init = xen_accel_class_init,
  138. };
  139. static void xen_type_init(void)
  140. {
  141. type_register_static(&xen_accel_type);
  142. }
  143. type_init(xen_type_init);