kvm_para.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. struct kvm_vcpu_arch_shared {
  23. __u64 scratch1;
  24. __u64 scratch2;
  25. __u64 scratch3;
  26. __u64 critical; /* Guest may not get interrupts if == r1 */
  27. __u64 sprg0;
  28. __u64 sprg1;
  29. __u64 sprg2;
  30. __u64 sprg3;
  31. __u64 srr0;
  32. __u64 srr1;
  33. __u64 dar;
  34. __u64 msr;
  35. __u32 dsisr;
  36. __u32 int_pending; /* Tells the guest if we have an interrupt */
  37. __u32 sr[16];
  38. };
  39. #define KVM_SC_MAGIC_R0 0x4b564d21 /* "KVM!" */
  40. #define HC_VENDOR_KVM (42 << 16)
  41. #define HC_EV_SUCCESS 0
  42. #define HC_EV_UNIMPLEMENTED 12
  43. #define KVM_FEATURE_MAGIC_PAGE 1
  44. #define KVM_MAGIC_FEAT_SR (1 << 0)
  45. #ifdef __KERNEL__
  46. #ifdef CONFIG_KVM_GUEST
  47. #include <linux/of.h>
  48. static inline int kvm_para_available(void)
  49. {
  50. struct device_node *hyper_node;
  51. hyper_node = of_find_node_by_path("/hypervisor");
  52. if (!hyper_node)
  53. return 0;
  54. if (!of_device_is_compatible(hyper_node, "linux,kvm"))
  55. return 0;
  56. return 1;
  57. }
  58. extern unsigned long kvm_hypercall(unsigned long *in,
  59. unsigned long *out,
  60. unsigned long nr);
  61. #else
  62. static inline int kvm_para_available(void)
  63. {
  64. return 0;
  65. }
  66. static unsigned long kvm_hypercall(unsigned long *in,
  67. unsigned long *out,
  68. unsigned long nr)
  69. {
  70. return HC_EV_UNIMPLEMENTED;
  71. }
  72. #endif
  73. static inline long kvm_hypercall0_1(unsigned int nr, unsigned long *r2)
  74. {
  75. unsigned long in[8];
  76. unsigned long out[8];
  77. unsigned long r;
  78. r = kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  79. *r2 = out[0];
  80. return r;
  81. }
  82. static inline long kvm_hypercall0(unsigned int nr)
  83. {
  84. unsigned long in[8];
  85. unsigned long out[8];
  86. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  87. }
  88. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  89. {
  90. unsigned long in[8];
  91. unsigned long out[8];
  92. in[0] = p1;
  93. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  94. }
  95. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  96. unsigned long p2)
  97. {
  98. unsigned long in[8];
  99. unsigned long out[8];
  100. in[0] = p1;
  101. in[1] = p2;
  102. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  103. }
  104. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  105. unsigned long p2, unsigned long p3)
  106. {
  107. unsigned long in[8];
  108. unsigned long out[8];
  109. in[0] = p1;
  110. in[1] = p2;
  111. in[2] = p3;
  112. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  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. unsigned long in[8];
  119. unsigned long out[8];
  120. in[0] = p1;
  121. in[1] = p2;
  122. in[2] = p3;
  123. in[3] = p4;
  124. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  125. }
  126. static inline unsigned int kvm_arch_para_features(void)
  127. {
  128. unsigned long r;
  129. if (!kvm_para_available())
  130. return 0;
  131. if(kvm_hypercall0_1(KVM_HC_FEATURES, &r))
  132. return 0;
  133. return r;
  134. }
  135. #endif /* __KERNEL__ */
  136. #endif /* __POWERPC_KVM_PARA_H__ */