kvm_book3s.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #ifdef CONFIG_PPC_BOOK3S_64
  55. struct hlist_node list_vpte_64k;
  56. #endif
  57. struct rcu_head rcu_head;
  58. u64 host_vpn;
  59. u64 pfn;
  60. ulong slot;
  61. struct kvmppc_pte pte;
  62. int pagesize;
  63. };
  64. /*
  65. * Struct for a virtual core.
  66. * Note: entry_exit_map combines a bitmap of threads that have entered
  67. * in the bottom 8 bits and a bitmap of threads that have exited in the
  68. * next 8 bits. This is so that we can atomically set the entry bit
  69. * iff the exit map is 0 without taking a lock.
  70. */
  71. struct kvmppc_vcore {
  72. int n_runnable;
  73. int num_threads;
  74. int entry_exit_map;
  75. int napping_threads;
  76. int first_vcpuid;
  77. u16 pcpu;
  78. u16 last_cpu;
  79. u8 vcore_state;
  80. u8 in_guest;
  81. struct kvmppc_vcore *master_vcore;
  82. struct kvm_vcpu *runnable_threads[MAX_SMT_THREADS];
  83. struct list_head preempt_list;
  84. spinlock_t lock;
  85. struct swait_queue_head wq;
  86. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  87. u64 stolen_tb;
  88. u64 preempt_tb;
  89. struct kvm_vcpu *runner;
  90. struct kvm *kvm;
  91. u64 tb_offset; /* guest timebase - host timebase */
  92. ulong lpcr;
  93. u32 arch_compat;
  94. ulong pcr;
  95. ulong dpdes; /* doorbell state (POWER8) */
  96. ulong vtb; /* virtual timebase */
  97. ulong conferring_threads;
  98. unsigned int halt_poll_ns;
  99. };
  100. struct kvmppc_vcpu_book3s {
  101. struct kvmppc_sid_map sid_map[SID_MAP_NUM];
  102. struct {
  103. u64 esid;
  104. u64 vsid;
  105. } slb_shadow[64];
  106. u8 slb_shadow_max;
  107. struct kvmppc_bat ibat[8];
  108. struct kvmppc_bat dbat[8];
  109. u64 hid[6];
  110. u64 gqr[8];
  111. u64 sdr1;
  112. u64 hior;
  113. u64 msr_mask;
  114. u64 vtb;
  115. #ifdef CONFIG_PPC_BOOK3S_32
  116. u32 vsid_pool[VSID_POOL_SIZE];
  117. u32 vsid_next;
  118. #else
  119. u64 proto_vsid_first;
  120. u64 proto_vsid_max;
  121. u64 proto_vsid_next;
  122. #endif
  123. int context_id[SID_CONTEXTS];
  124. bool hior_explicit; /* HIOR is set by ioctl, not PVR */
  125. struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
  126. struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
  127. struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
  128. struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
  129. #ifdef CONFIG_PPC_BOOK3S_64
  130. struct hlist_head hpte_hash_vpte_64k[HPTEG_HASH_NUM_VPTE_64K];
  131. #endif
  132. int hpte_cache_count;
  133. spinlock_t mmu_lock;
  134. };
  135. #define VSID_REAL 0x07ffffffffc00000ULL
  136. #define VSID_BAT 0x07ffffffffb00000ULL
  137. #define VSID_64K 0x0800000000000000ULL
  138. #define VSID_1T 0x1000000000000000ULL
  139. #define VSID_REAL_DR 0x2000000000000000ULL
  140. #define VSID_REAL_IR 0x4000000000000000ULL
  141. #define VSID_PR 0x8000000000000000ULL
  142. extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
  143. extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
  144. extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
  145. extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
  146. extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
  147. extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
  148. extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
  149. extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
  150. bool iswrite);
  151. extern void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
  152. extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
  153. extern void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong eaddr, ulong seg_size);
  154. extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
  155. extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run,
  156. struct kvm_vcpu *vcpu, unsigned long addr,
  157. unsigned long status);
  158. extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
  159. unsigned long slb_v, unsigned long valid);
  160. extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
  161. extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
  162. extern void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte);
  163. extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
  164. extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
  165. extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
  166. extern int kvmppc_mmu_hpte_sysinit(void);
  167. extern void kvmppc_mmu_hpte_sysexit(void);
  168. extern int kvmppc_mmu_hv_init(void);
  169. extern int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hc);
  170. /* XXX remove this export when load_last_inst() is generic */
  171. extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
  172. extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
  173. extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
  174. unsigned int vec);
  175. extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
  176. extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
  177. bool upper, u32 val);
  178. extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
  179. extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
  180. extern kvm_pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa,
  181. bool writing, bool *writable);
  182. extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
  183. unsigned long *rmap, long pte_index, int realmode);
  184. extern void kvmppc_update_rmap_change(unsigned long *rmap, unsigned long psize);
  185. extern void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
  186. unsigned long pte_index);
  187. void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
  188. unsigned long pte_index);
  189. extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
  190. unsigned long *nb_ret);
  191. extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr,
  192. unsigned long gpa, bool dirty);
  193. extern long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
  194. long pte_index, unsigned long pteh, unsigned long ptel,
  195. pgd_t *pgdir, bool realmode, unsigned long *idx_ret);
  196. extern long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
  197. unsigned long pte_index, unsigned long avpn,
  198. unsigned long *hpret);
  199. extern long kvmppc_hv_get_dirty_log(struct kvm *kvm,
  200. struct kvm_memory_slot *memslot, unsigned long *map);
  201. extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr,
  202. unsigned long mask);
  203. extern void kvmppc_set_fscr(struct kvm_vcpu *vcpu, u64 fscr);
  204. extern void kvmppc_entry_trampoline(void);
  205. extern void kvmppc_hv_entry_trampoline(void);
  206. extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
  207. extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
  208. extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
  209. extern void kvmppc_pr_init_default_hcalls(struct kvm *kvm);
  210. extern int kvmppc_hcall_impl_pr(unsigned long cmd);
  211. extern int kvmppc_hcall_impl_hv_realmode(unsigned long cmd);
  212. extern void kvmppc_copy_to_svcpu(struct kvmppc_book3s_shadow_vcpu *svcpu,
  213. struct kvm_vcpu *vcpu);
  214. extern void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu,
  215. struct kvmppc_book3s_shadow_vcpu *svcpu);
  216. extern int kvm_irq_bypass;
  217. static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
  218. {
  219. return vcpu->arch.book3s;
  220. }
  221. /* Also add subarch specific defines */
  222. #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
  223. #include <asm/kvm_book3s_32.h>
  224. #endif
  225. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  226. #include <asm/kvm_book3s_64.h>
  227. #endif
  228. static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
  229. {
  230. vcpu->arch.gpr[num] = val;
  231. }
  232. static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
  233. {
  234. return vcpu->arch.gpr[num];
  235. }
  236. static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
  237. {
  238. vcpu->arch.cr = val;
  239. }
  240. static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
  241. {
  242. return vcpu->arch.cr;
  243. }
  244. static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, ulong val)
  245. {
  246. vcpu->arch.xer = val;
  247. }
  248. static inline ulong kvmppc_get_xer(struct kvm_vcpu *vcpu)
  249. {
  250. return vcpu->arch.xer;
  251. }
  252. static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
  253. {
  254. vcpu->arch.ctr = val;
  255. }
  256. static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
  257. {
  258. return vcpu->arch.ctr;
  259. }
  260. static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
  261. {
  262. vcpu->arch.lr = val;
  263. }
  264. static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
  265. {
  266. return vcpu->arch.lr;
  267. }
  268. static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
  269. {
  270. vcpu->arch.pc = val;
  271. }
  272. static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
  273. {
  274. return vcpu->arch.pc;
  275. }
  276. static inline u64 kvmppc_get_msr(struct kvm_vcpu *vcpu);
  277. static inline bool kvmppc_need_byteswap(struct kvm_vcpu *vcpu)
  278. {
  279. return (kvmppc_get_msr(vcpu) & MSR_LE) != (MSR_KERNEL & MSR_LE);
  280. }
  281. static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
  282. {
  283. return vcpu->arch.fault_dar;
  284. }
  285. static inline bool is_kvmppc_resume_guest(int r)
  286. {
  287. return (r == RESUME_GUEST || r == RESUME_GUEST_NV);
  288. }
  289. static inline bool is_kvmppc_hv_enabled(struct kvm *kvm);
  290. static inline bool kvmppc_supports_magic_page(struct kvm_vcpu *vcpu)
  291. {
  292. /* Only PR KVM supports the magic page */
  293. return !is_kvmppc_hv_enabled(vcpu->kvm);
  294. }
  295. extern int kvmppc_h_logical_ci_load(struct kvm_vcpu *vcpu);
  296. extern int kvmppc_h_logical_ci_store(struct kvm_vcpu *vcpu);
  297. /* Magic register values loaded into r3 and r4 before the 'sc' assembly
  298. * instruction for the OSI hypercalls */
  299. #define OSI_SC_MAGIC_R3 0x113724FA
  300. #define OSI_SC_MAGIC_R4 0x77810F9B
  301. #define INS_DCBZ 0x7c0007ec
  302. /* TO = 31 for unconditional trap */
  303. #define INS_TW 0x7fe00008
  304. /* LPIDs we support with this build -- runtime limit may be lower */
  305. #define KVMPPC_NR_LPIDS (LPID_RSVD + 1)
  306. #define SPLIT_HACK_MASK 0xff000000
  307. #define SPLIT_HACK_OFFS 0xfb000000
  308. #endif /* __ASM_KVM_BOOK3S_H__ */