kvm_para.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef _ASM_X86_KVM_PARA_H
  2. #define _ASM_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. : "memory");
  61. return ret;
  62. }
  63. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  64. {
  65. long ret;
  66. asm volatile(KVM_HYPERCALL
  67. : "=a"(ret)
  68. : "a"(nr), "b"(p1)
  69. : "memory");
  70. return ret;
  71. }
  72. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  73. unsigned long p2)
  74. {
  75. long ret;
  76. asm volatile(KVM_HYPERCALL
  77. : "=a"(ret)
  78. : "a"(nr), "b"(p1), "c"(p2)
  79. : "memory");
  80. return ret;
  81. }
  82. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  83. unsigned long p2, unsigned long p3)
  84. {
  85. long ret;
  86. asm volatile(KVM_HYPERCALL
  87. : "=a"(ret)
  88. : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
  89. : "memory");
  90. return ret;
  91. }
  92. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  93. unsigned long p2, unsigned long p3,
  94. unsigned long p4)
  95. {
  96. long ret;
  97. asm volatile(KVM_HYPERCALL
  98. : "=a"(ret)
  99. : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
  100. : "memory");
  101. return ret;
  102. }
  103. static inline int kvm_para_available(void)
  104. {
  105. unsigned int eax, ebx, ecx, edx;
  106. char signature[13];
  107. cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
  108. memcpy(signature + 0, &ebx, 4);
  109. memcpy(signature + 4, &ecx, 4);
  110. memcpy(signature + 8, &edx, 4);
  111. signature[12] = 0;
  112. if (strcmp(signature, "KVMKVMKVM") == 0)
  113. return 1;
  114. return 0;
  115. }
  116. static inline unsigned int kvm_arch_para_features(void)
  117. {
  118. return cpuid_eax(KVM_CPUID_FEATURES);
  119. }
  120. #endif
  121. #endif /* _ASM_X86_KVM_PARA_H */