vcpu_width_config.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vcpu_width_config - Test KVM_ARM_VCPU_INIT() with KVM_ARM_VCPU_EL1_32BIT.
  4. *
  5. * Copyright (c) 2022 Google LLC.
  6. *
  7. * This is a test that ensures that non-mixed-width vCPUs (all 64bit vCPUs
  8. * or all 32bit vcPUs) can be configured and mixed-width vCPUs cannot be
  9. * configured.
  10. */
  11. #include "kvm_util.h"
  12. #include "processor.h"
  13. #include "test_util.h"
  14. /*
  15. * Add a vCPU, run KVM_ARM_VCPU_INIT with @init0, and then
  16. * add another vCPU, and run KVM_ARM_VCPU_INIT with @init1.
  17. */
  18. static int add_init_2vcpus(struct kvm_vcpu_init *init0,
  19. struct kvm_vcpu_init *init1)
  20. {
  21. struct kvm_vcpu *vcpu0, *vcpu1;
  22. struct kvm_vm *vm;
  23. int ret;
  24. vm = vm_create_barebones();
  25. vcpu0 = __vm_vcpu_add(vm, 0);
  26. ret = __vcpu_ioctl(vcpu0, KVM_ARM_VCPU_INIT, init0);
  27. if (ret)
  28. goto free_exit;
  29. vcpu1 = __vm_vcpu_add(vm, 1);
  30. ret = __vcpu_ioctl(vcpu1, KVM_ARM_VCPU_INIT, init1);
  31. free_exit:
  32. kvm_vm_free(vm);
  33. return ret;
  34. }
  35. /*
  36. * Add two vCPUs, then run KVM_ARM_VCPU_INIT for one vCPU with @init0,
  37. * and run KVM_ARM_VCPU_INIT for another vCPU with @init1.
  38. */
  39. static int add_2vcpus_init_2vcpus(struct kvm_vcpu_init *init0,
  40. struct kvm_vcpu_init *init1)
  41. {
  42. struct kvm_vcpu *vcpu0, *vcpu1;
  43. struct kvm_vm *vm;
  44. int ret;
  45. vm = vm_create_barebones();
  46. vcpu0 = __vm_vcpu_add(vm, 0);
  47. vcpu1 = __vm_vcpu_add(vm, 1);
  48. ret = __vcpu_ioctl(vcpu0, KVM_ARM_VCPU_INIT, init0);
  49. if (ret)
  50. goto free_exit;
  51. ret = __vcpu_ioctl(vcpu1, KVM_ARM_VCPU_INIT, init1);
  52. free_exit:
  53. kvm_vm_free(vm);
  54. return ret;
  55. }
  56. /*
  57. * Tests that two 64bit vCPUs can be configured, two 32bit vCPUs can be
  58. * configured, and two mixed-width vCPUs cannot be configured.
  59. * Each of those three cases, configure vCPUs in two different orders.
  60. * The one is running KVM_CREATE_VCPU for 2 vCPUs, and then running
  61. * KVM_ARM_VCPU_INIT for them.
  62. * The other is running KVM_CREATE_VCPU and KVM_ARM_VCPU_INIT for a vCPU,
  63. * and then run those commands for another vCPU.
  64. */
  65. int main(void)
  66. {
  67. struct kvm_vcpu_init init0, init1;
  68. struct kvm_vm *vm;
  69. int ret;
  70. TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_EL1_32BIT));
  71. /* Get the preferred target type and copy that to init1 for later use */
  72. vm = vm_create_barebones();
  73. vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init0);
  74. kvm_vm_free(vm);
  75. init1 = init0;
  76. /* Test with 64bit vCPUs */
  77. ret = add_init_2vcpus(&init0, &init0);
  78. TEST_ASSERT(ret == 0,
  79. "Configuring 64bit EL1 vCPUs failed unexpectedly");
  80. ret = add_2vcpus_init_2vcpus(&init0, &init0);
  81. TEST_ASSERT(ret == 0,
  82. "Configuring 64bit EL1 vCPUs failed unexpectedly");
  83. /* Test with 32bit vCPUs */
  84. init0.features[0] = (1 << KVM_ARM_VCPU_EL1_32BIT);
  85. ret = add_init_2vcpus(&init0, &init0);
  86. TEST_ASSERT(ret == 0,
  87. "Configuring 32bit EL1 vCPUs failed unexpectedly");
  88. ret = add_2vcpus_init_2vcpus(&init0, &init0);
  89. TEST_ASSERT(ret == 0,
  90. "Configuring 32bit EL1 vCPUs failed unexpectedly");
  91. /* Test with mixed-width vCPUs */
  92. init0.features[0] = 0;
  93. init1.features[0] = (1 << KVM_ARM_VCPU_EL1_32BIT);
  94. ret = add_init_2vcpus(&init0, &init1);
  95. TEST_ASSERT(ret != 0,
  96. "Configuring mixed-width vCPUs worked unexpectedly");
  97. ret = add_2vcpus_init_2vcpus(&init0, &init1);
  98. TEST_ASSERT(ret != 0,
  99. "Configuring mixed-width vCPUs worked unexpectedly");
  100. return 0;
  101. }