kvm_host.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * Derived from arch/arm/include/asm/kvm_host.h:
  6. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  7. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARM64_KVM_HOST_H__
  22. #define __ARM64_KVM_HOST_H__
  23. #include <linux/types.h>
  24. #include <linux/kvm_types.h>
  25. #include <asm/kvm.h>
  26. #include <asm/kvm_asm.h>
  27. #include <asm/kvm_mmio.h>
  28. #if defined(CONFIG_KVM_ARM_MAX_VCPUS)
  29. #define KVM_MAX_VCPUS CONFIG_KVM_ARM_MAX_VCPUS
  30. #else
  31. #define KVM_MAX_VCPUS 0
  32. #endif
  33. #define KVM_USER_MEM_SLOTS 32
  34. #define KVM_PRIVATE_MEM_SLOTS 4
  35. #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
  36. #include <kvm/arm_vgic.h>
  37. #include <kvm/arm_arch_timer.h>
  38. #define KVM_VCPU_MAX_FEATURES 3
  39. int __attribute_const__ kvm_target_cpu(void);
  40. int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  41. int kvm_arch_dev_ioctl_check_extension(long ext);
  42. struct kvm_arch {
  43. /* The VMID generation used for the virt. memory system */
  44. u64 vmid_gen;
  45. u32 vmid;
  46. /* 1-level 2nd stage table and lock */
  47. spinlock_t pgd_lock;
  48. pgd_t *pgd;
  49. /* VTTBR value associated with above pgd and vmid */
  50. u64 vttbr;
  51. /* Interrupt controller */
  52. struct vgic_dist vgic;
  53. /* Timer */
  54. struct arch_timer_kvm timer;
  55. };
  56. #define KVM_NR_MEM_OBJS 40
  57. /*
  58. * We don't want allocation failures within the mmu code, so we preallocate
  59. * enough memory for a single page fault in a cache.
  60. */
  61. struct kvm_mmu_memory_cache {
  62. int nobjs;
  63. void *objects[KVM_NR_MEM_OBJS];
  64. };
  65. struct kvm_vcpu_fault_info {
  66. u32 esr_el2; /* Hyp Syndrom Register */
  67. u64 far_el2; /* Hyp Fault Address Register */
  68. u64 hpfar_el2; /* Hyp IPA Fault Address Register */
  69. };
  70. struct kvm_cpu_context {
  71. struct kvm_regs gp_regs;
  72. union {
  73. u64 sys_regs[NR_SYS_REGS];
  74. u32 copro[NR_COPRO_REGS];
  75. };
  76. };
  77. typedef struct kvm_cpu_context kvm_cpu_context_t;
  78. struct kvm_vcpu_arch {
  79. struct kvm_cpu_context ctxt;
  80. /* HYP configuration */
  81. u64 hcr_el2;
  82. /* Exception Information */
  83. struct kvm_vcpu_fault_info fault;
  84. /* Debug state */
  85. u64 debug_flags;
  86. /* Pointer to host CPU context */
  87. kvm_cpu_context_t *host_cpu_context;
  88. /* VGIC state */
  89. struct vgic_cpu vgic_cpu;
  90. struct arch_timer_cpu timer_cpu;
  91. /*
  92. * Anything that is not used directly from assembly code goes
  93. * here.
  94. */
  95. /* dcache set/way operation pending */
  96. int last_pcpu;
  97. cpumask_t require_dcache_flush;
  98. /* Don't run the guest */
  99. bool pause;
  100. /* IO related fields */
  101. struct kvm_decode mmio_decode;
  102. /* Interrupt related fields */
  103. u64 irq_lines; /* IRQ and FIQ levels */
  104. /* Cache some mmu pages needed inside spinlock regions */
  105. struct kvm_mmu_memory_cache mmu_page_cache;
  106. /* Target CPU and feature flags */
  107. int target;
  108. DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
  109. /* Detect first run of a vcpu */
  110. bool has_run_once;
  111. };
  112. #define vcpu_gp_regs(v) (&(v)->arch.ctxt.gp_regs)
  113. #define vcpu_sys_reg(v,r) ((v)->arch.ctxt.sys_regs[(r)])
  114. /*
  115. * CP14 and CP15 live in the same array, as they are backed by the
  116. * same system registers.
  117. */
  118. #define vcpu_cp14(v,r) ((v)->arch.ctxt.copro[(r)])
  119. #define vcpu_cp15(v,r) ((v)->arch.ctxt.copro[(r)])
  120. #ifdef CONFIG_CPU_BIG_ENDIAN
  121. #define vcpu_cp15_64_high(v,r) vcpu_cp15((v),(r))
  122. #define vcpu_cp15_64_low(v,r) vcpu_cp15((v),(r) + 1)
  123. #else
  124. #define vcpu_cp15_64_high(v,r) vcpu_cp15((v),(r) + 1)
  125. #define vcpu_cp15_64_low(v,r) vcpu_cp15((v),(r))
  126. #endif
  127. struct kvm_vm_stat {
  128. u32 remote_tlb_flush;
  129. };
  130. struct kvm_vcpu_stat {
  131. u32 halt_wakeup;
  132. };
  133. int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
  134. const struct kvm_vcpu_init *init);
  135. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
  136. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  137. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  138. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  139. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  140. #define KVM_ARCH_WANT_MMU_NOTIFIER
  141. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  142. int kvm_unmap_hva_range(struct kvm *kvm,
  143. unsigned long start, unsigned long end);
  144. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  145. /* We do not have shadow page tables, hence the empty hooks */
  146. static inline int kvm_age_hva(struct kvm *kvm, unsigned long start,
  147. unsigned long end)
  148. {
  149. return 0;
  150. }
  151. static inline int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  152. {
  153. return 0;
  154. }
  155. static inline void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
  156. unsigned long address)
  157. {
  158. }
  159. struct kvm_vcpu *kvm_arm_get_running_vcpu(void);
  160. struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void);
  161. u64 kvm_call_hyp(void *hypfn, ...);
  162. int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  163. int exception_index);
  164. int kvm_perf_init(void);
  165. int kvm_perf_teardown(void);
  166. static inline void __cpu_init_hyp_mode(phys_addr_t boot_pgd_ptr,
  167. phys_addr_t pgd_ptr,
  168. unsigned long hyp_stack_ptr,
  169. unsigned long vector_ptr)
  170. {
  171. /*
  172. * Call initialization code, and switch to the full blown
  173. * HYP code.
  174. */
  175. kvm_call_hyp((void *)boot_pgd_ptr, pgd_ptr,
  176. hyp_stack_ptr, vector_ptr);
  177. }
  178. struct vgic_sr_vectors {
  179. void *save_vgic;
  180. void *restore_vgic;
  181. };
  182. static inline void vgic_arch_setup(const struct vgic_params *vgic)
  183. {
  184. extern struct vgic_sr_vectors __vgic_sr_vectors;
  185. switch(vgic->type)
  186. {
  187. case VGIC_V2:
  188. __vgic_sr_vectors.save_vgic = __save_vgic_v2_state;
  189. __vgic_sr_vectors.restore_vgic = __restore_vgic_v2_state;
  190. break;
  191. #ifdef CONFIG_ARM_GIC_V3
  192. case VGIC_V3:
  193. __vgic_sr_vectors.save_vgic = __save_vgic_v3_state;
  194. __vgic_sr_vectors.restore_vgic = __restore_vgic_v3_state;
  195. break;
  196. #endif
  197. default:
  198. BUG();
  199. }
  200. }
  201. static inline void kvm_arch_hardware_disable(void) {}
  202. static inline void kvm_arch_hardware_unsetup(void) {}
  203. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  204. static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
  205. static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
  206. #endif /* __ARM64_KVM_HOST_H__ */