kvm_para.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 MSR_KVM_WALL_CLOCK 0x11
  13. #define MSR_KVM_SYSTEM_TIME 0x12
  14. #ifdef __KERNEL__
  15. #include <asm/processor.h>
  16. /* xen binary-compatible interface. See xen headers for details */
  17. struct kvm_vcpu_time_info {
  18. uint32_t version;
  19. uint32_t pad0;
  20. uint64_t tsc_timestamp;
  21. uint64_t system_time;
  22. uint32_t tsc_to_system_mul;
  23. int8_t tsc_shift;
  24. int8_t pad[3];
  25. } __attribute__((__packed__)); /* 32 bytes */
  26. struct kvm_wall_clock {
  27. uint32_t wc_version;
  28. uint32_t wc_sec;
  29. uint32_t wc_nsec;
  30. } __attribute__((__packed__));
  31. extern void kvmclock_init(void);
  32. /* This instruction is vmcall. On non-VT architectures, it will generate a
  33. * trap that we will then rewrite to the appropriate instruction.
  34. */
  35. #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
  36. /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
  37. * instruction. The hypervisor may replace it with something else but only the
  38. * instructions are guaranteed to be supported.
  39. *
  40. * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
  41. * The hypercall number should be placed in rax and the return value will be
  42. * placed in rax. No other registers will be clobbered unless explicited
  43. * noted by the particular hypercall.
  44. */
  45. static inline long kvm_hypercall0(unsigned int nr)
  46. {
  47. long ret;
  48. asm volatile(KVM_HYPERCALL
  49. : "=a"(ret)
  50. : "a"(nr));
  51. return ret;
  52. }
  53. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  54. {
  55. long ret;
  56. asm volatile(KVM_HYPERCALL
  57. : "=a"(ret)
  58. : "a"(nr), "b"(p1));
  59. return ret;
  60. }
  61. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  62. unsigned long p2)
  63. {
  64. long ret;
  65. asm volatile(KVM_HYPERCALL
  66. : "=a"(ret)
  67. : "a"(nr), "b"(p1), "c"(p2));
  68. return ret;
  69. }
  70. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  71. unsigned long p2, unsigned long p3)
  72. {
  73. long ret;
  74. asm volatile(KVM_HYPERCALL
  75. : "=a"(ret)
  76. : "a"(nr), "b"(p1), "c"(p2), "d"(p3));
  77. return ret;
  78. }
  79. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  80. unsigned long p2, unsigned long p3,
  81. unsigned long p4)
  82. {
  83. long ret;
  84. asm volatile(KVM_HYPERCALL
  85. : "=a"(ret)
  86. : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4));
  87. return ret;
  88. }
  89. static inline int kvm_para_available(void)
  90. {
  91. unsigned int eax, ebx, ecx, edx;
  92. char signature[13];
  93. cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
  94. memcpy(signature + 0, &ebx, 4);
  95. memcpy(signature + 4, &ecx, 4);
  96. memcpy(signature + 8, &edx, 4);
  97. signature[12] = 0;
  98. if (strcmp(signature, "KVMKVMKVM") == 0)
  99. return 1;
  100. return 0;
  101. }
  102. static inline unsigned int kvm_arch_para_features(void)
  103. {
  104. return cpuid_eax(KVM_CPUID_FEATURES);
  105. }
  106. #endif
  107. #endif