kvm_para.h 3.6 KB

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