kvm_para.h 3.4 KB

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