kvm_host.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef __ARM_KVM_HOST_H__
  19. #define __ARM_KVM_HOST_H__
  20. #include <linux/types.h>
  21. #include <linux/kvm_types.h>
  22. #include <asm/kvm.h>
  23. #include <asm/kvm_asm.h>
  24. #include <asm/kvm_mmio.h>
  25. #include <asm/fpstate.h>
  26. #include <kvm/arm_arch_timer.h>
  27. #define __KVM_HAVE_ARCH_INTC_INITIALIZED
  28. #define KVM_USER_MEM_SLOTS 32
  29. #define KVM_HAVE_ONE_REG
  30. #define KVM_HALT_POLL_NS_DEFAULT 500000
  31. #define KVM_VCPU_MAX_FEATURES 2
  32. #include <kvm/arm_vgic.h>
  33. #ifdef CONFIG_ARM_GIC_V3
  34. #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
  35. #else
  36. #define KVM_MAX_VCPUS VGIC_V2_MAX_CPUS
  37. #endif
  38. #define KVM_REQ_SLEEP \
  39. KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  40. #define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)
  41. DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
  42. u32 *kvm_vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num, u32 mode);
  43. int __attribute_const__ kvm_target_cpu(void);
  44. int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  45. void kvm_reset_coprocs(struct kvm_vcpu *vcpu);
  46. struct kvm_arch {
  47. /* VTTBR value associated with below pgd and vmid */
  48. u64 vttbr;
  49. /* The last vcpu id that ran on each physical CPU */
  50. int __percpu *last_vcpu_ran;
  51. /*
  52. * Anything that is not used directly from assembly code goes
  53. * here.
  54. */
  55. /* The VMID generation used for the virt. memory system */
  56. u64 vmid_gen;
  57. u32 vmid;
  58. /* Stage-2 page table */
  59. pgd_t *pgd;
  60. /* Interrupt controller */
  61. struct vgic_dist vgic;
  62. int max_vcpus;
  63. };
  64. #define KVM_NR_MEM_OBJS 40
  65. /*
  66. * We don't want allocation failures within the mmu code, so we preallocate
  67. * enough memory for a single page fault in a cache.
  68. */
  69. struct kvm_mmu_memory_cache {
  70. int nobjs;
  71. void *objects[KVM_NR_MEM_OBJS];
  72. };
  73. struct kvm_vcpu_fault_info {
  74. u32 hsr; /* Hyp Syndrome Register */
  75. u32 hxfar; /* Hyp Data/Inst. Fault Address Register */
  76. u32 hpfar; /* Hyp IPA Fault Address Register */
  77. };
  78. /*
  79. * 0 is reserved as an invalid value.
  80. * Order should be kept in sync with the save/restore code.
  81. */
  82. enum vcpu_sysreg {
  83. __INVALID_SYSREG__,
  84. c0_MPIDR, /* MultiProcessor ID Register */
  85. c0_CSSELR, /* Cache Size Selection Register */
  86. c1_SCTLR, /* System Control Register */
  87. c1_ACTLR, /* Auxiliary Control Register */
  88. c1_CPACR, /* Coprocessor Access Control */
  89. c2_TTBR0, /* Translation Table Base Register 0 */
  90. c2_TTBR0_high, /* TTBR0 top 32 bits */
  91. c2_TTBR1, /* Translation Table Base Register 1 */
  92. c2_TTBR1_high, /* TTBR1 top 32 bits */
  93. c2_TTBCR, /* Translation Table Base Control R. */
  94. c3_DACR, /* Domain Access Control Register */
  95. c5_DFSR, /* Data Fault Status Register */
  96. c5_IFSR, /* Instruction Fault Status Register */
  97. c5_ADFSR, /* Auxilary Data Fault Status R */
  98. c5_AIFSR, /* Auxilary Instrunction Fault Status R */
  99. c6_DFAR, /* Data Fault Address Register */
  100. c6_IFAR, /* Instruction Fault Address Register */
  101. c7_PAR, /* Physical Address Register */
  102. c7_PAR_high, /* PAR top 32 bits */
  103. c9_L2CTLR, /* Cortex A15/A7 L2 Control Register */
  104. c10_PRRR, /* Primary Region Remap Register */
  105. c10_NMRR, /* Normal Memory Remap Register */
  106. c12_VBAR, /* Vector Base Address Register */
  107. c13_CID, /* Context ID Register */
  108. c13_TID_URW, /* Thread ID, User R/W */
  109. c13_TID_URO, /* Thread ID, User R/O */
  110. c13_TID_PRIV, /* Thread ID, Privileged */
  111. c14_CNTKCTL, /* Timer Control Register (PL1) */
  112. c10_AMAIR0, /* Auxilary Memory Attribute Indirection Reg0 */
  113. c10_AMAIR1, /* Auxilary Memory Attribute Indirection Reg1 */
  114. NR_CP15_REGS /* Number of regs (incl. invalid) */
  115. };
  116. struct kvm_cpu_context {
  117. struct kvm_regs gp_regs;
  118. struct vfp_hard_struct vfp;
  119. u32 cp15[NR_CP15_REGS];
  120. };
  121. typedef struct kvm_cpu_context kvm_cpu_context_t;
  122. struct kvm_vcpu_arch {
  123. struct kvm_cpu_context ctxt;
  124. int target; /* Processor target */
  125. DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
  126. /* The CPU type we expose to the VM */
  127. u32 midr;
  128. /* HYP trapping configuration */
  129. u32 hcr;
  130. /* Exception Information */
  131. struct kvm_vcpu_fault_info fault;
  132. /* Host FP context */
  133. kvm_cpu_context_t *host_cpu_context;
  134. /* VGIC state */
  135. struct vgic_cpu vgic_cpu;
  136. struct arch_timer_cpu timer_cpu;
  137. /*
  138. * Anything that is not used directly from assembly code goes
  139. * here.
  140. */
  141. /* vcpu power-off state */
  142. bool power_off;
  143. /* Don't run the guest (internal implementation need) */
  144. bool pause;
  145. /* IO related fields */
  146. struct kvm_decode mmio_decode;
  147. /* Cache some mmu pages needed inside spinlock regions */
  148. struct kvm_mmu_memory_cache mmu_page_cache;
  149. /* Detect first run of a vcpu */
  150. bool has_run_once;
  151. };
  152. struct kvm_vm_stat {
  153. ulong remote_tlb_flush;
  154. };
  155. struct kvm_vcpu_stat {
  156. u64 halt_successful_poll;
  157. u64 halt_attempted_poll;
  158. u64 halt_poll_invalid;
  159. u64 halt_wakeup;
  160. u64 hvc_exit_stat;
  161. u64 wfe_exit_stat;
  162. u64 wfi_exit_stat;
  163. u64 mmio_exit_user;
  164. u64 mmio_exit_kernel;
  165. u64 exits;
  166. };
  167. #define vcpu_cp15(v,r) (v)->arch.ctxt.cp15[r]
  168. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
  169. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  170. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  171. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  172. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  173. unsigned long kvm_call_hyp(void *hypfn, ...);
  174. void force_vm_exit(const cpumask_t *mask);
  175. #define KVM_ARCH_WANT_MMU_NOTIFIER
  176. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  177. int kvm_unmap_hva_range(struct kvm *kvm,
  178. unsigned long start, unsigned long end);
  179. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  180. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  181. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  182. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
  183. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  184. struct kvm_vcpu *kvm_arm_get_running_vcpu(void);
  185. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void);
  186. void kvm_arm_halt_guest(struct kvm *kvm);
  187. void kvm_arm_resume_guest(struct kvm *kvm);
  188. int kvm_arm_copy_coproc_indices(struct kvm_vcpu *vcpu, u64 __user *uindices);
  189. unsigned long kvm_arm_num_coproc_regs(struct kvm_vcpu *vcpu);
  190. int kvm_arm_coproc_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  191. int kvm_arm_coproc_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  192. int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  193. int exception_index);
  194. static inline void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run,
  195. int exception_index) {}
  196. static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr,
  197. unsigned long hyp_stack_ptr,
  198. unsigned long vector_ptr)
  199. {
  200. /*
  201. * Call initialization code, and switch to the full blown HYP
  202. * code. The init code doesn't need to preserve these
  203. * registers as r0-r3 are already callee saved according to
  204. * the AAPCS.
  205. * Note that we slightly misuse the prototype by casting the
  206. * stack pointer to a void *.
  207. * The PGDs are always passed as the third argument, in order
  208. * to be passed into r2-r3 to the init code (yes, this is
  209. * compliant with the PCS!).
  210. */
  211. kvm_call_hyp((void*)hyp_stack_ptr, vector_ptr, pgd_ptr);
  212. }
  213. static inline void __cpu_init_stage2(void)
  214. {
  215. kvm_call_hyp(__init_stage2_translation);
  216. }
  217. static inline int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)
  218. {
  219. return 0;
  220. }
  221. int kvm_perf_init(void);
  222. int kvm_perf_teardown(void);
  223. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
  224. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
  225. static inline void kvm_arch_hardware_unsetup(void) {}
  226. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  227. static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
  228. static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
  229. static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
  230. static inline void kvm_arm_init_debug(void) {}
  231. static inline void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) {}
  232. static inline void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) {}
  233. static inline void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) {}
  234. static inline bool kvm_arm_handle_step_debug(struct kvm_vcpu *vcpu,
  235. struct kvm_run *run)
  236. {
  237. return false;
  238. }
  239. int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
  240. struct kvm_device_attr *attr);
  241. int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
  242. struct kvm_device_attr *attr);
  243. int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
  244. struct kvm_device_attr *attr);
  245. /* All host FP/SIMD state is restored on guest exit, so nothing to save: */
  246. static inline void kvm_fpsimd_flush_cpu_state(void) {}
  247. static inline void kvm_arm_vhe_guest_enter(void) {}
  248. static inline void kvm_arm_vhe_guest_exit(void) {}
  249. static inline bool kvm_arm_harden_branch_predictor(void)
  250. {
  251. /* No way to detect it yet, pretend it is not there. */
  252. return false;
  253. }
  254. static inline void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu) {}
  255. static inline void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu) {}
  256. #endif /* __ARM_KVM_HOST_H__ */