kvm_para.h 3.5 KB

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