kvm_para.h 3.9 KB

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