kvm_para.h 3.5 KB

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