set_sregs_test.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * KVM_SET_SREGS tests
  3. *
  4. * Copyright (C) 2018, Google LLC.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2.
  7. *
  8. * This is a regression test for the bug fixed by the following commit:
  9. * d3802286fa0f ("kvm: x86: Disallow illegal IA32_APIC_BASE MSR values")
  10. *
  11. * That bug allowed a user-mode program that called the KVM_SET_SREGS
  12. * ioctl to put a VCPU's local APIC into an invalid state.
  13. *
  14. */
  15. #define _GNU_SOURCE /* for program_invocation_short_name */
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include "test_util.h"
  22. #include "kvm_util.h"
  23. #include "x86.h"
  24. #define VCPU_ID 5
  25. int main(int argc, char *argv[])
  26. {
  27. struct kvm_sregs sregs;
  28. struct kvm_vm *vm;
  29. int rc;
  30. /* Tell stdout not to buffer its content */
  31. setbuf(stdout, NULL);
  32. /* Create VM */
  33. vm = vm_create_default(VCPU_ID, 0, NULL);
  34. vcpu_sregs_get(vm, VCPU_ID, &sregs);
  35. sregs.apic_base = 1 << 10;
  36. rc = _vcpu_sregs_set(vm, VCPU_ID, &sregs);
  37. TEST_ASSERT(rc, "Set IA32_APIC_BASE to %llx (invalid)",
  38. sregs.apic_base);
  39. sregs.apic_base = 1 << 11;
  40. rc = _vcpu_sregs_set(vm, VCPU_ID, &sregs);
  41. TEST_ASSERT(!rc, "Couldn't set IA32_APIC_BASE to %llx (valid)",
  42. sregs.apic_base);
  43. kvm_vm_free(vm);
  44. return 0;
  45. }