kvm_para.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef __X86_KVM_PARA_H
  2. #define __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. /* xen binary-compatible interface. See xen headers for details */
  41. struct kvm_vcpu_time_info {
  42. uint32_t version;
  43. uint32_t pad0;
  44. uint64_t tsc_timestamp;
  45. uint64_t system_time;
  46. uint32_t tsc_to_system_mul;
  47. int8_t tsc_shift;
  48. int8_t pad[3];
  49. } __attribute__((__packed__)); /* 32 bytes */
  50. struct kvm_wall_clock {
  51. uint32_t wc_version;
  52. uint32_t wc_sec;
  53. uint32_t wc_nsec;
  54. } __attribute__((__packed__));
  55. extern void kvmclock_init(void);
  56. /* This instruction is vmcall. On non-VT architectures, it will generate a
  57. * trap that we will then rewrite to the appropriate instruction.
  58. */
  59. #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
  60. /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
  61. * instruction. The hypervisor may replace it with something else but only the
  62. * instructions are guaranteed to be supported.
  63. *
  64. * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
  65. * The hypercall number should be placed in rax and the return value will be
  66. * placed in rax. No other registers will be clobbered unless explicited
  67. * noted by the particular hypercall.
  68. */
  69. static inline long kvm_hypercall0(unsigned int nr)
  70. {
  71. long ret;
  72. asm volatile(KVM_HYPERCALL
  73. : "=a"(ret)
  74. : "a"(nr));
  75. return ret;
  76. }
  77. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  78. {
  79. long ret;
  80. asm volatile(KVM_HYPERCALL
  81. : "=a"(ret)
  82. : "a"(nr), "b"(p1));
  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. return ret;
  93. }
  94. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  95. unsigned long p2, unsigned long p3)
  96. {
  97. long ret;
  98. asm volatile(KVM_HYPERCALL
  99. : "=a"(ret)
  100. : "a"(nr), "b"(p1), "c"(p2), "d"(p3));
  101. return ret;
  102. }
  103. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  104. unsigned long p2, unsigned long p3,
  105. unsigned long p4)
  106. {
  107. long ret;
  108. asm volatile(KVM_HYPERCALL
  109. : "=a"(ret)
  110. : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4));
  111. return ret;
  112. }
  113. static inline int kvm_para_available(void)
  114. {
  115. unsigned int eax, ebx, ecx, edx;
  116. char signature[13];
  117. cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
  118. memcpy(signature + 0, &ebx, 4);
  119. memcpy(signature + 4, &ecx, 4);
  120. memcpy(signature + 8, &edx, 4);
  121. signature[12] = 0;
  122. if (strcmp(signature, "KVMKVMKVM") == 0)
  123. return 1;
  124. return 0;
  125. }
  126. static inline unsigned int kvm_arch_para_features(void)
  127. {
  128. return cpuid_eax(KVM_CPUID_FEATURES);
  129. }
  130. #endif
  131. #endif