1
0

plugin_supported_cpuid.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <errno.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "crosvm.h"
  12. typedef int (*crosvm_function)(struct crosvm*, uint32_t,
  13. struct kvm_cpuid_entry2*, uint32_t*);
  14. typedef int (*vcpu_function)(struct crosvm_vcpu*, uint32_t,
  15. struct kvm_cpuid_entry2*, uint32_t*);
  16. // Members of union should only differ by the pointer type of 1st arg.
  17. union cpuid_function {
  18. crosvm_function crosvm;
  19. vcpu_function vcpu;
  20. };
  21. int test_cpuid(void* crosvm, union cpuid_function funct, const char* name) {
  22. struct kvm_cpuid_entry2 cpuids[100];
  23. int n_entries = 0;
  24. int ret = funct.crosvm(crosvm, 1, cpuids, &n_entries);
  25. if (ret >= 0) {
  26. fprintf(stderr,
  27. "expected %s to fail with E2BIG\n", name);
  28. return ret;
  29. }
  30. ret = funct.crosvm(crosvm, 100, cpuids, &n_entries);
  31. if (ret < 0) {
  32. if (ret != -EINVAL) {
  33. fprintf(stderr, "unexpected failure of %s: %d\n", name, ret);
  34. } else {
  35. fprintf(stderr,
  36. "Query of %s failed with EINVAL (may be expected)\n",
  37. name, ret);
  38. }
  39. return ret;
  40. }
  41. if (n_entries <= 1) {
  42. fprintf(stderr,
  43. "unexpected number of cpuid entries from %s: %d\n",
  44. name, n_entries);
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. int main(int argc, char** argv) {
  50. struct crosvm* crosvm = NULL;
  51. int ret = crosvm_connect(&crosvm);
  52. if (ret) {
  53. fprintf(stderr, "failed to connect to crosvm: %d\n", ret);
  54. return 1;
  55. }
  56. struct crosvm_vcpu* vcpu = NULL;
  57. ret = crosvm_get_vcpu(crosvm, 0, &vcpu);
  58. if (ret) {
  59. fprintf(stderr, "failed to get vcpu #0: %d\n", ret);
  60. return 1;
  61. }
  62. union cpuid_function funct;
  63. funct.crosvm = crosvm_get_supported_cpuid;
  64. if (test_cpuid(crosvm, funct, "crosvm_get_supported_cpuid")) {
  65. return 1;
  66. }
  67. funct.crosvm = crosvm_get_emulated_cpuid;
  68. if (test_cpuid(crosvm, funct, "crosvm_get_emulated_cpuid")) {
  69. return 1;
  70. }
  71. ret = crosvm_start(crosvm);
  72. if (ret) {
  73. fprintf(stderr, "failed to start vm: %d\n", ret);
  74. return 1;
  75. }
  76. struct crosvm_vcpu_event evt = {0};
  77. ret = crosvm_vcpu_wait(vcpu, &evt);
  78. if (ret) {
  79. fprintf(stderr, "failed to wait for vm start: %d\n", ret);
  80. return 1;
  81. }
  82. if (evt.kind != CROSVM_VCPU_EVENT_KIND_INIT) {
  83. fprintf(stderr, "Got unexpected exit type: %d\n", evt.kind);
  84. return 1;
  85. }
  86. funct.vcpu = crosvm_get_hyperv_cpuid;
  87. ret = test_cpuid(vcpu, funct, "crosvm_get_hyperv_cpuid");
  88. // Older kernels don't support and return EINVAL, so allow this for now.
  89. if (ret && ret != -EINVAL) {
  90. fprintf(stderr, "Ignoring failure of crosvm_get_hyperv_cpuid\n");
  91. return 1;
  92. }
  93. return 0;
  94. }