kvm_host.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #define __KVM_HAVE_ARCH_INTC_INITIALIZED
  29. #define KVM_USER_MEM_SLOTS 32
  30. #define KVM_PRIVATE_MEM_SLOTS 4
  31. #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
  32. #define KVM_HALT_POLL_NS_DEFAULT 500000
  33. #include <kvm/arm_vgic.h>
  34. #include <kvm/arm_arch_timer.h>
  35. #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
  36. #define KVM_VCPU_MAX_FEATURES 3
  37. int __attribute_const__ kvm_target_cpu(void);
  38. int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  39. int kvm_arch_dev_ioctl_check_extension(long ext);
  40. struct kvm_arch {
  41. /* The VMID generation used for the virt. memory system */
  42. u64 vmid_gen;
  43. u32 vmid;
  44. /* 1-level 2nd stage table and lock */
  45. spinlock_t pgd_lock;
  46. pgd_t *pgd;
  47. /* VTTBR value associated with above pgd and vmid */
  48. u64 vttbr;
  49. /* The maximum number of vCPUs depends on the used GIC model */
  50. int max_vcpus;
  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. u32 mdcr_el2;
  83. /* Exception Information */
  84. struct kvm_vcpu_fault_info fault;
  85. /* Guest debug state */
  86. u64 debug_flags;
  87. /*
  88. * We maintain more than a single set of debug registers to support
  89. * debugging the guest from the host and to maintain separate host and
  90. * guest state during world switches. vcpu_debug_state are the debug
  91. * registers of the vcpu as the guest sees them. host_debug_state are
  92. * the host registers which are saved and restored during
  93. * world switches. external_debug_state contains the debug
  94. * values we want to debug the guest. This is set via the
  95. * KVM_SET_GUEST_DEBUG ioctl.
  96. *
  97. * debug_ptr points to the set of debug registers that should be loaded
  98. * onto the hardware when running the guest.
  99. */
  100. struct kvm_guest_debug_arch *debug_ptr;
  101. struct kvm_guest_debug_arch vcpu_debug_state;
  102. struct kvm_guest_debug_arch external_debug_state;
  103. /* Pointer to host CPU context */
  104. kvm_cpu_context_t *host_cpu_context;
  105. struct kvm_guest_debug_arch host_debug_state;
  106. /* VGIC state */
  107. struct vgic_cpu vgic_cpu;
  108. struct arch_timer_cpu timer_cpu;
  109. /*
  110. * Anything that is not used directly from assembly code goes
  111. * here.
  112. */
  113. /*
  114. * Guest registers we preserve during guest debugging.
  115. *
  116. * These shadow registers are updated by the kvm_handle_sys_reg
  117. * trap handler if the guest accesses or updates them while we
  118. * are using guest debug.
  119. */
  120. struct {
  121. u32 mdscr_el1;
  122. } guest_debug_preserved;
  123. /* vcpu power-off state */
  124. bool power_off;
  125. /* Don't run the guest (internal implementation need) */
  126. bool pause;
  127. /* IO related fields */
  128. struct kvm_decode mmio_decode;
  129. /* Interrupt related fields */
  130. u64 irq_lines; /* IRQ and FIQ levels */
  131. /* Cache some mmu pages needed inside spinlock regions */
  132. struct kvm_mmu_memory_cache mmu_page_cache;
  133. /* Target CPU and feature flags */
  134. int target;
  135. DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
  136. /* Detect first run of a vcpu */
  137. bool has_run_once;
  138. };
  139. #define vcpu_gp_regs(v) (&(v)->arch.ctxt.gp_regs)
  140. #define vcpu_sys_reg(v,r) ((v)->arch.ctxt.sys_regs[(r)])
  141. /*
  142. * CP14 and CP15 live in the same array, as they are backed by the
  143. * same system registers.
  144. */
  145. #define vcpu_cp14(v,r) ((v)->arch.ctxt.copro[(r)])
  146. #define vcpu_cp15(v,r) ((v)->arch.ctxt.copro[(r)])
  147. #ifdef CONFIG_CPU_BIG_ENDIAN
  148. #define vcpu_cp15_64_high(v,r) vcpu_cp15((v),(r))
  149. #define vcpu_cp15_64_low(v,r) vcpu_cp15((v),(r) + 1)
  150. #else
  151. #define vcpu_cp15_64_high(v,r) vcpu_cp15((v),(r) + 1)
  152. #define vcpu_cp15_64_low(v,r) vcpu_cp15((v),(r))
  153. #endif
  154. struct kvm_vm_stat {
  155. u32 remote_tlb_flush;
  156. };
  157. struct kvm_vcpu_stat {
  158. u32 halt_successful_poll;
  159. u32 halt_attempted_poll;
  160. u32 halt_wakeup;
  161. };
  162. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
  163. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  164. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  165. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  166. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  167. #define KVM_ARCH_WANT_MMU_NOTIFIER
  168. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  169. int kvm_unmap_hva_range(struct kvm *kvm,
  170. unsigned long start, unsigned long end);
  171. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  172. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
  173. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  174. /* We do not have shadow page tables, hence the empty hooks */
  175. static inline void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
  176. unsigned long address)
  177. {
  178. }
  179. struct kvm_vcpu *kvm_arm_get_running_vcpu(void);
  180. struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void);
  181. u64 kvm_call_hyp(void *hypfn, ...);
  182. void force_vm_exit(const cpumask_t *mask);
  183. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
  184. int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  185. int exception_index);
  186. int kvm_perf_init(void);
  187. int kvm_perf_teardown(void);
  188. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
  189. static inline void __cpu_init_hyp_mode(phys_addr_t boot_pgd_ptr,
  190. phys_addr_t pgd_ptr,
  191. unsigned long hyp_stack_ptr,
  192. unsigned long vector_ptr)
  193. {
  194. /*
  195. * Call initialization code, and switch to the full blown
  196. * HYP code.
  197. */
  198. kvm_call_hyp((void *)boot_pgd_ptr, pgd_ptr,
  199. hyp_stack_ptr, vector_ptr);
  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. void kvm_arm_init_debug(void);
  207. void kvm_arm_setup_debug(struct kvm_vcpu *vcpu);
  208. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu);
  209. void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu);
  210. #endif /* __ARM64_KVM_HOST_H__ */