kvm_para.h 2.6 KB

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