kvm_para.h 4.3 KB

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