kvm_ppc.h 11 KB

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