plugin_vm_state.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2018 The ChromiumOS Authors
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. */
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "crosvm.h"
  11. int main(int argc, char** argv) {
  12. struct crosvm *crosvm;
  13. int ret = crosvm_connect(&crosvm);
  14. if (ret) {
  15. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  16. return 1;
  17. }
  18. struct kvm_pic_state pic_state;
  19. ret = crosvm_get_pic_state(crosvm, false, &pic_state);
  20. if (ret < 0) {
  21. fprintf(stderr, "failed to get initial PIC1 state: %d\n", ret);
  22. return 1;
  23. }
  24. if (pic_state.auto_eoi) {
  25. fprintf(stderr, "unexpected value of auto_eoi flag\n");
  26. return 1;
  27. }
  28. pic_state.auto_eoi = true;
  29. ret = crosvm_set_pic_state(crosvm, false, &pic_state);
  30. if (ret < 0) {
  31. fprintf(stderr, "failed to update PIC1 state: %d\n", ret);
  32. return 1;
  33. }
  34. ret = crosvm_get_pic_state(crosvm, false, &pic_state);
  35. if (ret < 0) {
  36. fprintf(stderr, "failed to get updated PIC1 state: %d\n", ret);
  37. return 1;
  38. }
  39. if (!pic_state.auto_eoi) {
  40. fprintf(stderr, "unexpected value of auto_eoi flag after update\n");
  41. return 1;
  42. }
  43. // Test retrieving and setting IOAPIC state.
  44. struct kvm_ioapic_state ioapic_state;
  45. ret = crosvm_get_ioapic_state(crosvm, &ioapic_state);
  46. if (ret < 0) {
  47. fprintf(stderr, "failed to get initial PIC1 state: %d\n", ret);
  48. return 1;
  49. }
  50. fprintf(stderr, "IOAPIC ID: %d\n", ioapic_state.id);
  51. if (ioapic_state.id != 0) {
  52. fprintf(stderr, "unexpected value of IOAPIC ID: %d\n", ioapic_state.id);
  53. return 1;
  54. }
  55. ioapic_state.id = 1;
  56. ret = crosvm_set_ioapic_state(crosvm, &ioapic_state);
  57. if (ret < 0) {
  58. fprintf(stderr, "failed to update PIC1 state: %d\n", ret);
  59. return 1;
  60. }
  61. ret = crosvm_get_ioapic_state(crosvm, &ioapic_state);
  62. if (ret < 0) {
  63. fprintf(stderr, "failed to get updated PIC1 state: %d\n", ret);
  64. return 1;
  65. }
  66. if (ioapic_state.id != 1) {
  67. fprintf(stderr, "unexpected value of IOAPIC ID after update: %d\n",
  68. ioapic_state.id);
  69. return 1;
  70. }
  71. // Test retrieving and setting PIT state.
  72. struct kvm_pit_state2 pit_state;
  73. ret = crosvm_get_pit_state(crosvm, &pit_state);
  74. if (ret < 0) {
  75. fprintf(stderr, "failed to get initial PIT state: %d\n", ret);
  76. return 1;
  77. }
  78. if (pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY) {
  79. fprintf(stderr, "unexpected value of KVM_PIT_FLAGS_HPET_LEGACY flag\n");
  80. return 1;
  81. }
  82. pit_state.flags |= KVM_PIT_FLAGS_HPET_LEGACY;
  83. ret = crosvm_set_pit_state(crosvm, &pit_state);
  84. if (ret < 0) {
  85. fprintf(stderr, "failed to update PIT state: %d\n", ret);
  86. return 1;
  87. }
  88. ret = crosvm_get_pit_state(crosvm, &pit_state);
  89. if (ret < 0) {
  90. fprintf(stderr, "failed to get updated PIT state: %d\n", ret);
  91. return 1;
  92. }
  93. if (!(pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY)) {
  94. fprintf(stderr,
  95. "unexpected value of KVM_PIT_FLAGS_HPET_LEGACY after update\n");
  96. return 1;
  97. }
  98. // Test retrieving and setting clock state.
  99. struct kvm_clock_data clock_data = { .clock = 0, .flags = -1U, };
  100. ret = crosvm_get_clock(crosvm, &clock_data);
  101. if (ret < 0) {
  102. fprintf(stderr, "failed to get initial clock state: %d\n", ret);
  103. return 1;
  104. }
  105. if (clock_data.clock == 0 || clock_data.flags != 0) {
  106. fprintf(stderr, "invalid clock data returned (%llu, %u)\n",
  107. clock_data.clock, clock_data.flags);
  108. }
  109. clock_data.clock += 10000000;
  110. ret = crosvm_set_clock(crosvm, &clock_data);
  111. if (ret < 0) {
  112. fprintf(stderr, "failed to update clock: %d\n", ret);
  113. return 1;
  114. }
  115. clock_data.flags = -1U;
  116. ret = crosvm_set_clock(crosvm, &clock_data);
  117. if (ret >= 0) {
  118. fprintf(stderr, "unexpected success updating clock\n");
  119. return 1;
  120. }
  121. return 0;
  122. }