kvm_ppc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_PPC_H__
  20. #define __POWERPC_KVM_PPC_H__
  21. /* This file exists just so we can dereference kvm_vcpu, avoiding nested header
  22. * dependencies. */
  23. #include <linux/mutex.h>
  24. #include <linux/timer.h>
  25. #include <linux/types.h>
  26. #include <linux/kvm_types.h>
  27. #include <linux/kvm_host.h>
  28. #include <linux/bug.h>
  29. #ifdef CONFIG_PPC_BOOK3S
  30. #include <asm/kvm_book3s.h>
  31. #else
  32. #include <asm/kvm_booke.h>
  33. #endif
  34. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  35. #include <asm/paca.h>
  36. #endif
  37. enum emulation_result {
  38. EMULATE_DONE, /* no further processing */
  39. EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
  40. EMULATE_DO_DCR, /* kvm_run filled with DCR request */
  41. EMULATE_FAIL, /* can't emulate this instruction */
  42. EMULATE_AGAIN, /* something went wrong. go again */
  43. };
  44. extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  45. extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  46. extern char kvmppc_handlers_start[];
  47. extern unsigned long kvmppc_handler_len;
  48. extern void kvmppc_handler_highmem(void);
  49. extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
  50. extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  51. unsigned int rt, unsigned int bytes,
  52. int is_bigendian);
  53. extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  54. unsigned int rt, unsigned int bytes,
  55. int is_bigendian);
  56. extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  57. u64 val, unsigned int bytes, int is_bigendian);
  58. extern int kvmppc_emulate_instruction(struct kvm_run *run,
  59. struct kvm_vcpu *vcpu);
  60. extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
  61. extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
  62. extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
  63. extern void kvmppc_decrementer_func(unsigned long data);
  64. extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
  65. extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
  66. extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
  67. /* Core-specific hooks */
  68. extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
  69. unsigned int gtlb_idx);
  70. extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
  71. extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
  72. extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
  73. extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
  74. extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
  75. extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
  76. extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
  77. gva_t eaddr);
  78. extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
  79. extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
  80. extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
  81. unsigned int id);
  82. extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
  83. extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
  84. extern int kvmppc_core_check_processor_compat(void);
  85. extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  86. struct kvm_translation *tr);
  87. extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  88. extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
  89. extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
  90. extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
  91. extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
  92. extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
  93. extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
  94. extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  95. struct kvm_interrupt *irq);
  96. extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
  97. struct kvm_interrupt *irq);
  98. extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
  99. extern int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  100. unsigned int op, int *advance);
  101. extern int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn,
  102. ulong val);
  103. extern int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn,
  104. ulong *val);
  105. extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
  106. extern int kvmppc_booke_init(void);
  107. extern void kvmppc_booke_exit(void);
  108. extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
  109. extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
  110. extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
  111. extern long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp);
  112. extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp);
  113. extern void kvmppc_free_hpt(struct kvm *kvm);
  114. extern long kvmppc_prepare_vrma(struct kvm *kvm,
  115. struct kvm_userspace_memory_region *mem);
  116. extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
  117. struct kvm_memory_slot *memslot, unsigned long porder);
  118. extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
  119. extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  120. struct kvm_create_spapr_tce *args);
  121. extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  122. unsigned long ioba, unsigned long tce);
  123. extern long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
  124. struct kvm_allocate_rma *rma);
  125. extern struct kvmppc_linear_info *kvm_alloc_rma(void);
  126. extern void kvm_release_rma(struct kvmppc_linear_info *ri);
  127. extern struct kvmppc_linear_info *kvm_alloc_hpt(void);
  128. extern void kvm_release_hpt(struct kvmppc_linear_info *li);
  129. extern int kvmppc_core_init_vm(struct kvm *kvm);
  130. extern void kvmppc_core_destroy_vm(struct kvm *kvm);
  131. extern void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  132. struct kvm_memory_slot *dont);
  133. extern int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  134. unsigned long npages);
  135. extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  136. struct kvm_memory_slot *memslot,
  137. struct kvm_userspace_memory_region *mem);
  138. extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
  139. struct kvm_userspace_memory_region *mem,
  140. struct kvm_memory_slot old);
  141. extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
  142. struct kvm_ppc_smmu_info *info);
  143. extern void kvmppc_core_flush_memslot(struct kvm *kvm,
  144. struct kvm_memory_slot *memslot);
  145. extern int kvmppc_bookehv_init(void);
  146. extern void kvmppc_bookehv_exit(void);
  147. extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
  148. extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
  149. /*
  150. * Cuts out inst bits with ordering according to spec.
  151. * That means the leftmost bit is zero. All given bits are included.
  152. */
  153. static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
  154. {
  155. u32 r;
  156. u32 mask;
  157. BUG_ON(msb > lsb);
  158. mask = (1 << (lsb - msb + 1)) - 1;
  159. r = (inst >> (63 - lsb)) & mask;
  160. return r;
  161. }
  162. /*
  163. * Replaces inst bits with ordering according to spec.
  164. */
  165. static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
  166. {
  167. u32 r;
  168. u32 mask;
  169. BUG_ON(msb > lsb);
  170. mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
  171. r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
  172. return r;
  173. }
  174. union kvmppc_one_reg {
  175. u32 wval;
  176. u64 dval;
  177. vector128 vval;
  178. u64 vsxval[2];
  179. struct {
  180. u64 addr;
  181. u64 length;
  182. } vpaval;
  183. };
  184. #define one_reg_size(id) \
  185. (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
  186. #define get_reg_val(id, reg) ({ \
  187. union kvmppc_one_reg __u; \
  188. switch (one_reg_size(id)) { \
  189. case 4: __u.wval = (reg); break; \
  190. case 8: __u.dval = (reg); break; \
  191. default: BUG(); \
  192. } \
  193. __u; \
  194. })
  195. #define set_reg_val(id, val) ({ \
  196. u64 __v; \
  197. switch (one_reg_size(id)) { \
  198. case 4: __v = (val).wval; break; \
  199. case 8: __v = (val).dval; break; \
  200. default: BUG(); \
  201. } \
  202. __v; \
  203. })
  204. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  205. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  206. void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  207. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  208. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
  209. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
  210. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
  211. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
  212. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
  213. #ifdef CONFIG_KVM_BOOK3S_64_HV
  214. static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
  215. {
  216. paca[cpu].kvm_hstate.xics_phys = addr;
  217. }
  218. extern void kvm_linear_init(void);
  219. #else
  220. static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
  221. {}
  222. static inline void kvm_linear_init(void)
  223. {}
  224. #endif
  225. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  226. struct kvm_config_tlb *cfg);
  227. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  228. struct kvm_dirty_tlb *cfg);
  229. long kvmppc_alloc_lpid(void);
  230. void kvmppc_claim_lpid(long lpid);
  231. void kvmppc_free_lpid(long lpid);
  232. void kvmppc_init_lpid(unsigned long nr_lpids);
  233. static inline void kvmppc_mmu_flush_icache(pfn_t pfn)
  234. {
  235. /* Clear i-cache for new pages */
  236. struct page *page;
  237. page = pfn_to_page(pfn);
  238. if (!test_bit(PG_arch_1, &page->flags)) {
  239. flush_dcache_icache_page(page);
  240. set_bit(PG_arch_1, &page->flags);
  241. }
  242. }
  243. /* Please call after prepare_to_enter. This function puts the lazy ee state
  244. back to normal mode, without actually enabling interrupts. */
  245. static inline void kvmppc_lazy_ee_enable(void)
  246. {
  247. #ifdef CONFIG_PPC64
  248. /* Only need to enable IRQs by hard enabling them after this */
  249. local_paca->irq_happened = 0;
  250. local_paca->soft_enabled = 1;
  251. #endif
  252. }
  253. static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
  254. {
  255. ulong ea;
  256. ulong msr_64bit = 0;
  257. ea = kvmppc_get_gpr(vcpu, rb);
  258. if (ra)
  259. ea += kvmppc_get_gpr(vcpu, ra);
  260. #if defined(CONFIG_PPC_BOOK3E_64)
  261. msr_64bit = MSR_CM;
  262. #elif defined(CONFIG_PPC_BOOK3S_64)
  263. msr_64bit = MSR_SF;
  264. #endif
  265. if (!(vcpu->arch.shared->msr & msr_64bit))
  266. ea = (uint32_t)ea;
  267. return ea;
  268. }
  269. #endif /* __POWERPC_KVM_PPC_H__ */