tod.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * TOD (Time Of Day) clock
  3. *
  4. * Copyright 2018 Red Hat, Inc.
  5. * Author(s): David Hildenbrand <david@redhat.com>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  8. * See the COPYING file in the top-level directory.
  9. */
  10. #include "qemu/osdep.h"
  11. #include "hw/s390x/tod.h"
  12. #include "qapi/error.h"
  13. #include "qemu/error-report.h"
  14. #include "qemu/module.h"
  15. #include "system/kvm.h"
  16. #include "system/tcg.h"
  17. #include "system/qtest.h"
  18. #include "migration/qemu-file-types.h"
  19. #include "migration/register.h"
  20. void s390_init_tod(void)
  21. {
  22. Object *obj;
  23. if (kvm_enabled()) {
  24. obj = object_new(TYPE_KVM_S390_TOD);
  25. } else if (tcg_enabled()) {
  26. obj = object_new(TYPE_QEMU_S390_TOD);
  27. } else if (qtest_enabled()) {
  28. return;
  29. } else {
  30. error_report("current accelerator not handled in s390_init_tod!");
  31. abort();
  32. }
  33. object_property_add_child(qdev_get_machine(), TYPE_S390_TOD, obj);
  34. object_unref(obj);
  35. qdev_realize(DEVICE(obj), NULL, &error_fatal);
  36. }
  37. S390TODState *s390_get_todstate(void)
  38. {
  39. static S390TODState *ts;
  40. if (!ts) {
  41. ts = S390_TOD(object_resolve_path_type("", TYPE_S390_TOD, NULL));
  42. }
  43. return ts;
  44. }
  45. #define S390_TOD_CLOCK_VALUE_MISSING 0x00
  46. #define S390_TOD_CLOCK_VALUE_PRESENT 0x01
  47. static void s390_tod_save(QEMUFile *f, void *opaque)
  48. {
  49. S390TODState *td = opaque;
  50. S390TODClass *tdc = S390_TOD_GET_CLASS(td);
  51. Error *err = NULL;
  52. S390TOD tod;
  53. tdc->get(td, &tod, &err);
  54. if (err) {
  55. warn_report_err(err);
  56. error_printf("Guest clock will not be migrated "
  57. "which could cause the guest to hang.");
  58. qemu_put_byte(f, S390_TOD_CLOCK_VALUE_MISSING);
  59. return;
  60. }
  61. qemu_put_byte(f, S390_TOD_CLOCK_VALUE_PRESENT);
  62. qemu_put_byte(f, tod.high);
  63. qemu_put_be64(f, tod.low);
  64. }
  65. static int s390_tod_load(QEMUFile *f, void *opaque, int version_id)
  66. {
  67. S390TODState *td = opaque;
  68. S390TODClass *tdc = S390_TOD_GET_CLASS(td);
  69. Error *err = NULL;
  70. S390TOD tod;
  71. if (qemu_get_byte(f) == S390_TOD_CLOCK_VALUE_MISSING) {
  72. warn_report("Guest clock was not migrated. This could "
  73. "cause the guest to hang.");
  74. return 0;
  75. }
  76. tod.high = qemu_get_byte(f);
  77. tod.low = qemu_get_be64(f);
  78. tdc->set(td, &tod, &err);
  79. if (err) {
  80. error_report_err(err);
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. static SaveVMHandlers savevm_tod = {
  86. .save_state = s390_tod_save,
  87. .load_state = s390_tod_load,
  88. };
  89. static void s390_tod_realize(DeviceState *dev, Error **errp)
  90. {
  91. S390TODState *td = S390_TOD(dev);
  92. /* Legacy migration interface */
  93. register_savevm_live("todclock", 0, 1, &savevm_tod, td);
  94. }
  95. static void s390_tod_class_init(ObjectClass *oc, void *data)
  96. {
  97. DeviceClass *dc = DEVICE_CLASS(oc);
  98. dc->desc = "TOD (Time Of Day) Clock";
  99. dc->realize = s390_tod_realize;
  100. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  101. /* We only have one TOD clock in the system attached to the machine */
  102. dc->user_creatable = false;
  103. }
  104. static const TypeInfo s390_tod_info = {
  105. .name = TYPE_S390_TOD,
  106. .parent = TYPE_DEVICE,
  107. .instance_size = sizeof(S390TODState),
  108. .class_init = s390_tod_class_init,
  109. .class_size = sizeof(S390TODClass),
  110. .abstract = true,
  111. };
  112. static void register_types(void)
  113. {
  114. type_register_static(&s390_tod_info);
  115. }
  116. type_init(register_types);