kvm_para.h 3.1 KB

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