svm_util.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2020, Red Hat, Inc.
  4. */
  5. #ifndef SELFTEST_KVM_SVM_UTILS_H
  6. #define SELFTEST_KVM_SVM_UTILS_H
  7. #include <asm/svm.h>
  8. #include <stdint.h>
  9. #include "svm.h"
  10. #include "processor.h"
  11. struct svm_test_data {
  12. /* VMCB */
  13. struct vmcb *vmcb; /* gva */
  14. void *vmcb_hva;
  15. uint64_t vmcb_gpa;
  16. /* host state-save area */
  17. struct vmcb_save_area *save_area; /* gva */
  18. void *save_area_hva;
  19. uint64_t save_area_gpa;
  20. /* MSR-Bitmap */
  21. void *msr; /* gva */
  22. void *msr_hva;
  23. uint64_t msr_gpa;
  24. /* NPT */
  25. uint64_t ncr3_gpa;
  26. };
  27. static inline void vmmcall(void)
  28. {
  29. /*
  30. * Stuff RAX and RCX with "safe" values to make sure L0 doesn't handle
  31. * it as a valid hypercall (e.g. Hyper-V L2 TLB flush) as the intended
  32. * use of this function is to exit to L1 from L2. Clobber all other
  33. * GPRs as L1 doesn't correctly preserve them during vmexits.
  34. */
  35. __asm__ __volatile__("push %%rbp; vmmcall; pop %%rbp"
  36. : : "a"(0xdeadbeef), "c"(0xbeefdead)
  37. : "rbx", "rdx", "rsi", "rdi", "r8", "r9",
  38. "r10", "r11", "r12", "r13", "r14", "r15");
  39. }
  40. #define stgi() \
  41. __asm__ __volatile__( \
  42. "stgi\n" \
  43. )
  44. #define clgi() \
  45. __asm__ __volatile__( \
  46. "clgi\n" \
  47. )
  48. struct svm_test_data *vcpu_alloc_svm(struct kvm_vm *vm, vm_vaddr_t *p_svm_gva);
  49. void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rsp);
  50. void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
  51. static inline bool kvm_cpu_has_npt(void)
  52. {
  53. return kvm_cpu_has(X86_FEATURE_NPT);
  54. }
  55. void vm_enable_npt(struct kvm_vm *vm);
  56. int open_sev_dev_path_or_exit(void);
  57. #endif /* SELFTEST_KVM_SVM_UTILS_H */