kvm_para.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #ifdef __KERNEL__
  44. #ifdef CONFIG_KVM_GUEST
  45. static inline int kvm_para_available(void)
  46. {
  47. struct device_node *hyper_node;
  48. hyper_node = of_find_node_by_path("/hypervisor");
  49. if (!hyper_node)
  50. return 0;
  51. if (!of_device_is_compatible(hyper_node, "linux,kvm"))
  52. return 0;
  53. return 1;
  54. }
  55. extern unsigned long kvm_hypercall(unsigned long *in,
  56. unsigned long *out,
  57. unsigned long nr);
  58. #else
  59. static inline int kvm_para_available(void)
  60. {
  61. return 0;
  62. }
  63. static unsigned long kvm_hypercall(unsigned long *in,
  64. unsigned long *out,
  65. unsigned long nr)
  66. {
  67. return HC_EV_UNIMPLEMENTED;
  68. }
  69. #endif
  70. static inline long kvm_hypercall0_1(unsigned int nr, unsigned long *r2)
  71. {
  72. unsigned long in[8];
  73. unsigned long out[8];
  74. unsigned long r;
  75. r = kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  76. *r2 = out[0];
  77. return r;
  78. }
  79. static inline long kvm_hypercall0(unsigned int nr)
  80. {
  81. unsigned long in[8];
  82. unsigned long out[8];
  83. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  84. }
  85. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  86. {
  87. unsigned long in[8];
  88. unsigned long out[8];
  89. in[0] = p1;
  90. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  91. }
  92. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  93. unsigned long p2)
  94. {
  95. unsigned long in[8];
  96. unsigned long out[8];
  97. in[0] = p1;
  98. in[1] = p2;
  99. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  100. }
  101. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  102. unsigned long p2, unsigned long p3)
  103. {
  104. unsigned long in[8];
  105. unsigned long out[8];
  106. in[0] = p1;
  107. in[1] = p2;
  108. in[2] = p3;
  109. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  110. }
  111. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  112. unsigned long p2, unsigned long p3,
  113. unsigned long p4)
  114. {
  115. unsigned long in[8];
  116. unsigned long out[8];
  117. in[0] = p1;
  118. in[1] = p2;
  119. in[2] = p3;
  120. in[3] = p4;
  121. return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
  122. }
  123. static inline unsigned int kvm_arch_para_features(void)
  124. {
  125. unsigned long r;
  126. if (!kvm_para_available())
  127. return 0;
  128. if(kvm_hypercall0_1(KVM_HC_FEATURES, &r))
  129. return 0;
  130. return r;
  131. }
  132. #endif /* __KERNEL__ */
  133. #endif /* __POWERPC_KVM_PARA_H__ */