kvm_para.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2008
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #ifndef __POWERPC_KVM_PARA_H__
  20. #define __POWERPC_KVM_PARA_H__
  21. #include <linux/types.h>
  22. #include <linux/of.h>
  23. struct kvm_vcpu_arch_shared {
  24. __u64 sprg0;
  25. __u64 sprg1;
  26. __u64 sprg2;
  27. __u64 sprg3;
  28. __u64 srr0;
  29. __u64 srr1;
  30. __u64 dar;
  31. __u64 msr;
  32. __u32 dsisr;
  33. };
  34. #define KVM_SC_MAGIC_R0 0x4b564d21 /* "KVM!" */
  35. #define HC_VENDOR_KVM (42 << 16)
  36. #define HC_EV_SUCCESS 0
  37. #define HC_EV_UNIMPLEMENTED 12
  38. #ifdef __KERNEL__
  39. #ifdef CONFIG_KVM_GUEST
  40. static inline int kvm_para_available(void)
  41. {
  42. struct device_node *hyper_node;
  43. hyper_node = of_find_node_by_path("/hypervisor");
  44. if (!hyper_node)
  45. return 0;
  46. if (!of_device_is_compatible(hyper_node, "linux,kvm"))
  47. return 0;
  48. return 1;
  49. }
  50. extern unsigned long kvm_hypercall(unsigned long *in,
  51. unsigned long *out,
  52. unsigned long nr);
  53. #else
  54. static inline int kvm_para_available(void)
  55. {
  56. return 0;
  57. }
  58. static unsigned long kvm_hypercall(unsigned long *in,
  59. unsigned long *out,
  60. unsigned long nr)
  61. {
  62. return HC_EV_UNIMPLEMENTED;
  63. }
  64. #endif
  65. static inline long kvm_hypercall0_1(unsigned int nr, unsigned long *r2)
  66. {
  67. unsigned long in[8];
  68. unsigned long out[8];
  69. unsigned long r;
  70. r = kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  71. *r2 = out[0];
  72. return r;
  73. }
  74. static inline long kvm_hypercall0(unsigned int nr)
  75. {
  76. unsigned long in[8];
  77. unsigned long out[8];
  78. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  79. }
  80. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  81. {
  82. unsigned long in[8];
  83. unsigned long out[8];
  84. in[0] = p1;
  85. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  86. }
  87. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  88. unsigned long p2)
  89. {
  90. unsigned long in[8];
  91. unsigned long out[8];
  92. in[0] = p1;
  93. in[1] = p2;
  94. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  95. }
  96. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  97. unsigned long p2, unsigned long p3)
  98. {
  99. unsigned long in[8];
  100. unsigned long out[8];
  101. in[0] = p1;
  102. in[1] = p2;
  103. in[2] = p3;
  104. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  105. }
  106. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  107. unsigned long p2, unsigned long p3,
  108. unsigned long p4)
  109. {
  110. unsigned long in[8];
  111. unsigned long out[8];
  112. in[0] = p1;
  113. in[1] = p2;
  114. in[2] = p3;
  115. in[3] = p4;
  116. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  117. }
  118. static inline unsigned int kvm_arch_para_features(void)
  119. {
  120. unsigned long r;
  121. if (!kvm_para_available())
  122. return 0;
  123. if(kvm_hypercall0_1(KVM_HC_FEATURES, &r))
  124. return 0;
  125. return r;
  126. }
  127. #endif /* __KERNEL__ */
  128. #endif /* __POWERPC_KVM_PARA_H__ */