kvm_host.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /* Mandated version of PSCI */
  64. u32 psci_version;
  65. };
  66. #define KVM_NR_MEM_OBJS 40
  67. /*
  68. * We don't want allocation failures within the mmu code, so we preallocate
  69. * enough memory for a single page fault in a cache.
  70. */
  71. struct kvm_mmu_memory_cache {
  72. int nobjs;
  73. void *objects[KVM_NR_MEM_OBJS];
  74. };
  75. struct kvm_vcpu_fault_info {
  76. u32 hsr; /* Hyp Syndrome Register */
  77. u32 hxfar; /* Hyp Data/Inst. Fault Address Register */
  78. u32 hpfar; /* Hyp IPA Fault Address Register */
  79. };
  80. /*
  81. * 0 is reserved as an invalid value.
  82. * Order should be kept in sync with the save/restore code.
  83. */
  84. enum vcpu_sysreg {
  85. __INVALID_SYSREG__,
  86. c0_MPIDR, /* MultiProcessor ID Register */
  87. c0_CSSELR, /* Cache Size Selection Register */
  88. c1_SCTLR, /* System Control Register */
  89. c1_ACTLR, /* Auxiliary Control Register */
  90. c1_CPACR, /* Coprocessor Access Control */
  91. c2_TTBR0, /* Translation Table Base Register 0 */
  92. c2_TTBR0_high, /* TTBR0 top 32 bits */
  93. c2_TTBR1, /* Translation Table Base Register 1 */
  94. c2_TTBR1_high, /* TTBR1 top 32 bits */
  95. c2_TTBCR, /* Translation Table Base Control R. */
  96. c3_DACR, /* Domain Access Control Register */
  97. c5_DFSR, /* Data Fault Status Register */
  98. c5_IFSR, /* Instruction Fault Status Register */
  99. c5_ADFSR, /* Auxilary Data Fault Status R */
  100. c5_AIFSR, /* Auxilary Instrunction Fault Status R */
  101. c6_DFAR, /* Data Fault Address Register */
  102. c6_IFAR, /* Instruction Fault Address Register */
  103. c7_PAR, /* Physical Address Register */
  104. c7_PAR_high, /* PAR top 32 bits */
  105. c9_L2CTLR, /* Cortex A15/A7 L2 Control Register */
  106. c10_PRRR, /* Primary Region Remap Register */
  107. c10_NMRR, /* Normal Memory Remap Register */
  108. c12_VBAR, /* Vector Base Address Register */
  109. c13_CID, /* Context ID Register */
  110. c13_TID_URW, /* Thread ID, User R/W */
  111. c13_TID_URO, /* Thread ID, User R/O */
  112. c13_TID_PRIV, /* Thread ID, Privileged */
  113. c14_CNTKCTL, /* Timer Control Register (PL1) */
  114. c10_AMAIR0, /* Auxilary Memory Attribute Indirection Reg0 */
  115. c10_AMAIR1, /* Auxilary Memory Attribute Indirection Reg1 */
  116. NR_CP15_REGS /* Number of regs (incl. invalid) */
  117. };
  118. struct kvm_cpu_context {
  119. struct kvm_regs gp_regs;
  120. struct vfp_hard_struct vfp;
  121. u32 cp15[NR_CP15_REGS];
  122. };
  123. typedef struct kvm_cpu_context kvm_cpu_context_t;
  124. struct kvm_vcpu_arch {
  125. struct kvm_cpu_context ctxt;
  126. int target; /* Processor target */
  127. DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
  128. /* The CPU type we expose to the VM */
  129. u32 midr;
  130. /* HYP trapping configuration */
  131. u32 hcr;
  132. /* Exception Information */
  133. struct kvm_vcpu_fault_info fault;
  134. /* Host FP context */
  135. kvm_cpu_context_t *host_cpu_context;
  136. /* VGIC state */
  137. struct vgic_cpu vgic_cpu;
  138. struct arch_timer_cpu timer_cpu;
  139. /*
  140. * Anything that is not used directly from assembly code goes
  141. * here.
  142. */
  143. /* vcpu power-off state */
  144. bool power_off;
  145. /* Don't run the guest (internal implementation need) */
  146. bool pause;
  147. /* IO related fields */
  148. struct kvm_decode mmio_decode;
  149. /* Cache some mmu pages needed inside spinlock regions */
  150. struct kvm_mmu_memory_cache mmu_page_cache;
  151. /* Detect first run of a vcpu */
  152. bool has_run_once;
  153. };
  154. struct kvm_vm_stat {
  155. ulong remote_tlb_flush;
  156. };
  157. struct kvm_vcpu_stat {
  158. u64 halt_successful_poll;
  159. u64 halt_attempted_poll;
  160. u64 halt_poll_invalid;
  161. u64 halt_wakeup;
  162. u64 hvc_exit_stat;
  163. u64 wfe_exit_stat;
  164. u64 wfi_exit_stat;
  165. u64 mmio_exit_user;
  166. u64 mmio_exit_kernel;
  167. u64 exits;
  168. };
  169. #define vcpu_cp15(v,r) (v)->arch.ctxt.cp15[r]
  170. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
  171. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  172. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  173. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  174. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  175. unsigned long kvm_call_hyp(void *hypfn, ...);
  176. void force_vm_exit(const cpumask_t *mask);
  177. #define KVM_ARCH_WANT_MMU_NOTIFIER
  178. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  179. int kvm_unmap_hva_range(struct kvm *kvm,
  180. unsigned long start, unsigned long end);
  181. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  182. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  183. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  184. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
  185. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  186. struct kvm_vcpu *kvm_arm_get_running_vcpu(void);
  187. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void);
  188. void kvm_arm_halt_guest(struct kvm *kvm);
  189. void kvm_arm_resume_guest(struct kvm *kvm);
  190. int kvm_arm_copy_coproc_indices(struct kvm_vcpu *vcpu, u64 __user *uindices);
  191. unsigned long kvm_arm_num_coproc_regs(struct kvm_vcpu *vcpu);
  192. int kvm_arm_coproc_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  193. int kvm_arm_coproc_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  194. int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  195. int exception_index);
  196. static inline void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run,
  197. int exception_index) {}
  198. static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr,
  199. unsigned long hyp_stack_ptr,
  200. unsigned long vector_ptr)
  201. {
  202. /*
  203. * Call initialization code, and switch to the full blown HYP
  204. * code. The init code doesn't need to preserve these
  205. * registers as r0-r3 are already callee saved according to
  206. * the AAPCS.
  207. * Note that we slightly misuse the prototype by casting the
  208. * stack pointer to a void *.
  209. * The PGDs are always passed as the third argument, in order
  210. * to be passed into r2-r3 to the init code (yes, this is
  211. * compliant with the PCS!).
  212. */
  213. kvm_call_hyp((void*)hyp_stack_ptr, vector_ptr, pgd_ptr);
  214. }
  215. static inline void __cpu_init_stage2(void)
  216. {
  217. kvm_call_hyp(__init_stage2_translation);
  218. }
  219. static inline int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)
  220. {
  221. return 0;
  222. }
  223. int kvm_perf_init(void);
  224. int kvm_perf_teardown(void);
  225. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
  226. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
  227. static inline void kvm_arch_hardware_unsetup(void) {}
  228. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  229. static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
  230. static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
  231. static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
  232. static inline void kvm_arm_init_debug(void) {}
  233. static inline void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) {}
  234. static inline void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) {}
  235. static inline void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) {}
  236. static inline bool kvm_arm_handle_step_debug(struct kvm_vcpu *vcpu,
  237. struct kvm_run *run)
  238. {
  239. return false;
  240. }
  241. int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
  242. struct kvm_device_attr *attr);
  243. int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
  244. struct kvm_device_attr *attr);
  245. int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
  246. struct kvm_device_attr *attr);
  247. /* All host FP/SIMD state is restored on guest exit, so nothing to save: */
  248. static inline void kvm_fpsimd_flush_cpu_state(void) {}
  249. static inline void kvm_arm_vhe_guest_enter(void) {}
  250. static inline void kvm_arm_vhe_guest_exit(void) {}
  251. static inline bool kvm_arm_harden_branch_predictor(void)
  252. {
  253. /* No way to detect it yet, pretend it is not there. */
  254. return false;
  255. }
  256. static inline void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu) {}
  257. static inline void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu) {}
  258. #endif /* __ARM_KVM_HOST_H__ */