kvm_book3s.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 SUSE Linux Products GmbH 2009
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #ifndef __ASM_KVM_BOOK3S_H__
  20. #define __ASM_KVM_BOOK3S_H__
  21. #include <linux/types.h>
  22. #include <linux/kvm_host.h>
  23. #include <asm/kvm_book3s_asm.h>
  24. struct kvmppc_bat {
  25. u64 raw;
  26. u32 bepi;
  27. u32 bepi_mask;
  28. u32 brpn;
  29. u8 wimg;
  30. u8 pp;
  31. bool vs : 1;
  32. bool vp : 1;
  33. };
  34. struct kvmppc_sid_map {
  35. u64 guest_vsid;
  36. u64 guest_esid;
  37. u64 host_vsid;
  38. bool valid : 1;
  39. };
  40. #define SID_MAP_BITS 9
  41. #define SID_MAP_NUM (1 << SID_MAP_BITS)
  42. #define SID_MAP_MASK (SID_MAP_NUM - 1)
  43. #ifdef CONFIG_PPC_BOOK3S_64
  44. #define SID_CONTEXTS 1
  45. #else
  46. #define SID_CONTEXTS 128
  47. #define VSID_POOL_SIZE (SID_CONTEXTS * 16)
  48. #endif
  49. struct hpte_cache {
  50. struct hlist_node list_pte;
  51. struct hlist_node list_pte_long;
  52. struct hlist_node list_vpte;
  53. struct hlist_node list_vpte_long;
  54. struct rcu_head rcu_head;
  55. u64 host_va;
  56. u64 pfn;
  57. ulong slot;
  58. struct kvmppc_pte pte;
  59. };
  60. struct kvmppc_vcpu_book3s {
  61. struct kvm_vcpu vcpu;
  62. struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
  63. struct kvmppc_sid_map sid_map[SID_MAP_NUM];
  64. struct {
  65. u64 esid;
  66. u64 vsid;
  67. } slb_shadow[64];
  68. u8 slb_shadow_max;
  69. struct kvmppc_bat ibat[8];
  70. struct kvmppc_bat dbat[8];
  71. u64 hid[6];
  72. u64 gqr[8];
  73. u64 sdr1;
  74. u64 hior;
  75. u64 msr_mask;
  76. u64 vsid_next;
  77. #ifdef CONFIG_PPC_BOOK3S_32
  78. u32 vsid_pool[VSID_POOL_SIZE];
  79. #else
  80. u64 vsid_first;
  81. u64 vsid_max;
  82. #endif
  83. int context_id[SID_CONTEXTS];
  84. ulong prog_flags; /* flags to inject when giving a 700 trap */
  85. struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
  86. struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
  87. struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
  88. struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
  89. int hpte_cache_count;
  90. spinlock_t mmu_lock;
  91. };
  92. #define CONTEXT_HOST 0
  93. #define CONTEXT_GUEST 1
  94. #define CONTEXT_GUEST_END 2
  95. #define VSID_REAL 0x1fffffffffc00000ULL
  96. #define VSID_BAT 0x1fffffffffb00000ULL
  97. #define VSID_REAL_DR 0x2000000000000000ULL
  98. #define VSID_REAL_IR 0x4000000000000000ULL
  99. #define VSID_PR 0x8000000000000000ULL
  100. extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
  101. extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
  102. extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
  103. extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
  104. extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
  105. extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
  106. extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
  107. extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
  108. extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
  109. extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
  110. extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
  111. extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
  112. extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
  113. extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
  114. extern int kvmppc_mmu_hpte_sysinit(void);
  115. extern void kvmppc_mmu_hpte_sysexit(void);
  116. extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
  117. extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
  118. extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
  119. extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
  120. bool upper, u32 val);
  121. extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
  122. extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
  123. extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
  124. extern void kvmppc_handler_lowmem_trampoline(void);
  125. extern void kvmppc_handler_trampoline_enter(void);
  126. extern void kvmppc_rmcall(ulong srr0, ulong srr1);
  127. extern void kvmppc_load_up_fpu(void);
  128. extern void kvmppc_load_up_altivec(void);
  129. extern void kvmppc_load_up_vsx(void);
  130. extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
  131. extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
  132. static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
  133. {
  134. return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
  135. }
  136. static inline ulong dsisr(void)
  137. {
  138. ulong r;
  139. asm ( "mfdsisr %0 " : "=r" (r) );
  140. return r;
  141. }
  142. extern void kvm_return_point(void);
  143. static inline struct kvmppc_book3s_shadow_vcpu *to_svcpu(struct kvm_vcpu *vcpu);
  144. static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
  145. {
  146. if ( num < 14 ) {
  147. to_svcpu(vcpu)->gpr[num] = val;
  148. to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
  149. } else
  150. vcpu->arch.gpr[num] = val;
  151. }
  152. static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
  153. {
  154. if ( num < 14 )
  155. return to_svcpu(vcpu)->gpr[num];
  156. else
  157. return vcpu->arch.gpr[num];
  158. }
  159. static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
  160. {
  161. to_svcpu(vcpu)->cr = val;
  162. to_book3s(vcpu)->shadow_vcpu->cr = val;
  163. }
  164. static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
  165. {
  166. return to_svcpu(vcpu)->cr;
  167. }
  168. static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
  169. {
  170. to_svcpu(vcpu)->xer = val;
  171. to_book3s(vcpu)->shadow_vcpu->xer = val;
  172. }
  173. static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
  174. {
  175. return to_svcpu(vcpu)->xer;
  176. }
  177. static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
  178. {
  179. to_svcpu(vcpu)->ctr = val;
  180. }
  181. static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
  182. {
  183. return to_svcpu(vcpu)->ctr;
  184. }
  185. static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
  186. {
  187. to_svcpu(vcpu)->lr = val;
  188. }
  189. static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
  190. {
  191. return to_svcpu(vcpu)->lr;
  192. }
  193. static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
  194. {
  195. to_svcpu(vcpu)->pc = val;
  196. }
  197. static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
  198. {
  199. return to_svcpu(vcpu)->pc;
  200. }
  201. static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
  202. {
  203. ulong pc = kvmppc_get_pc(vcpu);
  204. struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
  205. /* Load the instruction manually if it failed to do so in the
  206. * exit path */
  207. if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
  208. kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
  209. return svcpu->last_inst;
  210. }
  211. static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
  212. {
  213. return to_svcpu(vcpu)->fault_dar;
  214. }
  215. /* Magic register values loaded into r3 and r4 before the 'sc' assembly
  216. * instruction for the OSI hypercalls */
  217. #define OSI_SC_MAGIC_R3 0x113724FA
  218. #define OSI_SC_MAGIC_R4 0x77810F9B
  219. #define INS_DCBZ 0x7c0007ec
  220. /* Also add subarch specific defines */
  221. #ifdef CONFIG_PPC_BOOK3S_32
  222. #include <asm/kvm_book3s_32.h>
  223. #else
  224. #include <asm/kvm_book3s_64.h>
  225. #endif
  226. #endif /* __ASM_KVM_BOOK3S_H__ */