2
0

xen-common.c 3.8 KB

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