kvm_para.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /*
  23. * Additions to this struct must only occur at the end, and should be
  24. * accompanied by a KVM_MAGIC_FEAT flag to advertise that they are present
  25. * (albeit not necessarily relevant to the current target hardware platform).
  26. *
  27. * Struct fields are always 32 or 64 bit aligned, depending on them being 32
  28. * or 64 bit wide respectively.
  29. *
  30. * See Documentation/virtual/kvm/ppc-pv.txt
  31. */
  32. struct kvm_vcpu_arch_shared {
  33. __u64 scratch1;
  34. __u64 scratch2;
  35. __u64 scratch3;
  36. __u64 critical; /* Guest may not get interrupts if == r1 */
  37. __u64 sprg0;
  38. __u64 sprg1;
  39. __u64 sprg2;
  40. __u64 sprg3;
  41. __u64 srr0;
  42. __u64 srr1;
  43. __u64 dar; /* dear on BookE */
  44. __u64 msr;
  45. __u32 dsisr;
  46. __u32 int_pending; /* Tells the guest if we have an interrupt */
  47. __u32 sr[16];
  48. __u32 mas0;
  49. __u32 mas1;
  50. __u64 mas7_3;
  51. __u64 mas2;
  52. __u32 mas4;
  53. __u32 mas6;
  54. __u32 esr;
  55. __u32 pir;
  56. /*
  57. * SPRG4-7 are user-readable, so we can only keep these consistent
  58. * between the shared area and the real registers when there's an
  59. * intervening exit to KVM. This also applies to SPRG3 on some
  60. * chips.
  61. *
  62. * This suffices for access by guest userspace, since in PR-mode
  63. * KVM, an exit must occur when changing the guest's MSR[PR].
  64. * If the guest kernel writes to SPRG3-7 via the shared area, it
  65. * must also use the shared area for reading while in kernel space.
  66. */
  67. __u64 sprg4;
  68. __u64 sprg5;
  69. __u64 sprg6;
  70. __u64 sprg7;
  71. };
  72. #define KVM_SC_MAGIC_R0 0x4b564d21 /* "KVM!" */
  73. #define KVM_HCALL_TOKEN(num) _EV_HCALL_TOKEN(EV_KVM_VENDOR_ID, num)
  74. #include <asm/epapr_hcalls.h>
  75. #define KVM_FEATURE_MAGIC_PAGE 1
  76. #define KVM_MAGIC_FEAT_SR (1 << 0)
  77. /* MASn, ESR, PIR, and high SPRGs */
  78. #define KVM_MAGIC_FEAT_MAS0_TO_SPRG7 (1 << 1)
  79. #ifdef __KERNEL__
  80. #ifdef CONFIG_KVM_GUEST
  81. #include <linux/of.h>
  82. static inline int kvm_para_available(void)
  83. {
  84. struct device_node *hyper_node;
  85. hyper_node = of_find_node_by_path("/hypervisor");
  86. if (!hyper_node)
  87. return 0;
  88. if (!of_device_is_compatible(hyper_node, "linux,kvm"))
  89. return 0;
  90. return 1;
  91. }
  92. extern unsigned long kvm_hypercall(unsigned long *in,
  93. unsigned long *out,
  94. unsigned long nr);
  95. #else
  96. static inline int kvm_para_available(void)
  97. {
  98. return 0;
  99. }
  100. static unsigned long kvm_hypercall(unsigned long *in,
  101. unsigned long *out,
  102. unsigned long nr)
  103. {
  104. return EV_UNIMPLEMENTED;
  105. }
  106. #endif
  107. static inline long kvm_hypercall0_1(unsigned int nr, unsigned long *r2)
  108. {
  109. unsigned long in[8];
  110. unsigned long out[8];
  111. unsigned long r;
  112. r = kvm_hypercall(in, out, KVM_HCALL_TOKEN(nr));
  113. *r2 = out[0];
  114. return r;
  115. }
  116. static inline long kvm_hypercall0(unsigned int nr)
  117. {
  118. unsigned long in[8];
  119. unsigned long out[8];
  120. return kvm_hypercall(in, out, KVM_HCALL_TOKEN(nr));
  121. }
  122. static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
  123. {
  124. unsigned long in[8];
  125. unsigned long out[8];
  126. in[0] = p1;
  127. return kvm_hypercall(in, out, KVM_HCALL_TOKEN(nr));
  128. }
  129. static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
  130. unsigned long p2)
  131. {
  132. unsigned long in[8];
  133. unsigned long out[8];
  134. in[0] = p1;
  135. in[1] = p2;
  136. return kvm_hypercall(in, out, KVM_HCALL_TOKEN(nr));
  137. }
  138. static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
  139. unsigned long p2, unsigned long p3)
  140. {
  141. unsigned long in[8];
  142. unsigned long out[8];
  143. in[0] = p1;
  144. in[1] = p2;
  145. in[2] = p3;
  146. return kvm_hypercall(in, out, KVM_HCALL_TOKEN(nr));
  147. }
  148. static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
  149. unsigned long p2, unsigned long p3,
  150. unsigned long p4)
  151. {
  152. unsigned long in[8];
  153. unsigned long out[8];
  154. in[0] = p1;
  155. in[1] = p2;
  156. in[2] = p3;
  157. in[3] = p4;
  158. return kvm_hypercall(in, out, KVM_HCALL_TOKEN(nr));
  159. }
  160. static inline unsigned int kvm_arch_para_features(void)
  161. {
  162. unsigned long r;
  163. if (!kvm_para_available())
  164. return 0;
  165. if(kvm_hypercall0_1(KVM_HC_FEATURES, &r))
  166. return 0;
  167. return r;
  168. }
  169. static inline bool kvm_check_and_clear_guest_paused(void)
  170. {
  171. return false;
  172. }
  173. #endif /* __KERNEL__ */
  174. #endif /* __POWERPC_KVM_PARA_H__ */