kvm_para.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef __X86_KVM_PARA_H
  2. #define __X86_KVM_PARA_H
  3. /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
  4. * should be used to determine that a VM is running under KVM.
  5. */
  6. #define KVM_CPUID_SIGNATURE 0x40000000
  7. /* This CPUID returns a feature bitmap in eax. Before enabling a particular
  8. * paravirtualization, the appropriate feature bit should be checked.
  9. */
  10. #define KVM_CPUID_FEATURES 0x40000001
  11. #define KVM_FEATURE_CLOCKSOURCE 0
  12. #define KVM_FEATURE_NOP_IO_DELAY 1
  13. #define KVM_FEATURE_MMU_OP 2
  14. #define MSR_KVM_WALL_CLOCK 0x11
  15. #define MSR_KVM_SYSTEM_TIME 0x12
  16. #define KVM_MAX_MMU_OP_BATCH 32
  17. /* Operations for KVM_HC_MMU_OP */
  18. #define KVM_MMU_OP_WRITE_PTE 1
  19. #define KVM_MMU_OP_FLUSH_TLB 2
  20. #define KVM_MMU_OP_RELEASE_PT 3
  21. /* Payload for KVM_HC_MMU_OP */
  22. struct kvm_mmu_op_header {
  23. __u32 op;
  24. __u32 pad;
  25. };
  26. struct kvm_mmu_op_write_pte {
  27. struct kvm_mmu_op_header header;
  28. __u64 pte_phys;
  29. __u64 pte_val;
  30. };
  31. struct kvm_mmu_op_flush_tlb {
  32. struct kvm_mmu_op_header header;
  33. };
  34. struct kvm_mmu_op_release_pt {
  35. struct kvm_mmu_op_header header;
  36. __u64 pt_phys;
  37. };
  38. #ifdef __KERNEL__
  39. #include <asm/processor.h>
  40. extern void kvmclock_init(void);
  41. /* This instruction is vmcall. On non-VT architectures, it will generate a
  42. * trap that we will then rewrite to the appropriate instruction.
  43. */
  44. #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
  45. /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
  46. * instruction. The hypervisor may replace it with something else but only the
  47. * instructions are guaranteed to be supported.
  48. *
  49. * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
  50. * The hypercall number should be placed in rax and the return value will be
  51. * placed in rax. No other registers will be clobbered unless explicited
  52. * noted by the particular hypercall.
  53. */
  54. static inline long kvm_hypercall0(unsigned int nr)
  55. {
  56. long ret;
  57. asm volatile(KVM_HYPERCALL
  58. : "=a"(ret)
  59. : "a"(nr));
  60. return ret;
  61. }
  62. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  63. {
  64. long ret;
  65. asm volatile(KVM_HYPERCALL
  66. : "=a"(ret)
  67. : "a"(nr), "b"(p1));
  68. return ret;
  69. }
  70. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  71. unsigned long p2)
  72. {
  73. long ret;
  74. asm volatile(KVM_HYPERCALL
  75. : "=a"(ret)
  76. : "a"(nr), "b"(p1), "c"(p2));
  77. return ret;
  78. }
  79. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  80. unsigned long p2, unsigned long p3)
  81. {
  82. long ret;
  83. asm volatile(KVM_HYPERCALL
  84. : "=a"(ret)
  85. : "a"(nr), "b"(p1), "c"(p2), "d"(p3));
  86. return ret;
  87. }
  88. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  89. unsigned long p2, unsigned long p3,
  90. unsigned long p4)
  91. {
  92. long ret;
  93. asm volatile(KVM_HYPERCALL
  94. : "=a"(ret)
  95. : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4));
  96. return ret;
  97. }
  98. static inline int kvm_para_available(void)
  99. {
  100. unsigned int eax, ebx, ecx, edx;
  101. char signature[13];
  102. cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
  103. memcpy(signature + 0, &ebx, 4);
  104. memcpy(signature + 4, &ecx, 4);
  105. memcpy(signature + 8, &edx, 4);
  106. signature[12] = 0;
  107. if (strcmp(signature, "KVMKVMKVM") == 0)
  108. return 1;
  109. return 0;
  110. }
  111. static inline unsigned int kvm_arch_para_features(void)
  112. {
  113. return cpuid_eax(KVM_CPUID_FEATURES);
  114. }
  115. #endif
  116. #endif