kvm_host.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_PRIVATE_MEM_SLOTS 4
  30. #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
  31. #define KVM_HAVE_ONE_REG
  32. #define KVM_HALT_POLL_NS_DEFAULT 500000
  33. #define KVM_VCPU_MAX_FEATURES 2
  34. #include <kvm/arm_vgic.h>
  35. #ifdef CONFIG_ARM_GIC_V3
  36. #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
  37. #else
  38. #define KVM_MAX_VCPUS VGIC_V2_MAX_CPUS
  39. #endif
  40. #define KVM_REQ_VCPU_EXIT 8
  41. u32 *kvm_vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num, u32 mode);
  42. int __attribute_const__ kvm_target_cpu(void);
  43. int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  44. void kvm_reset_coprocs(struct kvm_vcpu *vcpu);
  45. struct kvm_arch {
  46. /* VTTBR value associated with below pgd and vmid */
  47. u64 vttbr;
  48. /* The last vcpu id that ran on each physical CPU */
  49. int __percpu *last_vcpu_ran;
  50. /* Timer */
  51. struct arch_timer_kvm timer;
  52. /*
  53. * Anything that is not used directly from assembly code goes
  54. * here.
  55. */
  56. /* The VMID generation used for the virt. memory system */
  57. u64 vmid_gen;
  58. u32 vmid;
  59. /* Stage-2 page table */
  60. pgd_t *pgd;
  61. /* Interrupt controller */
  62. struct vgic_dist vgic;
  63. int max_vcpus;
  64. /* Mandated version of PSCI */
  65. u32 psci_version;
  66. };
  67. #define KVM_NR_MEM_OBJS 40
  68. /*
  69. * We don't want allocation failures within the mmu code, so we preallocate
  70. * enough memory for a single page fault in a cache.
  71. */
  72. struct kvm_mmu_memory_cache {
  73. int nobjs;
  74. void *objects[KVM_NR_MEM_OBJS];
  75. };
  76. struct kvm_vcpu_fault_info {
  77. u32 hsr; /* Hyp Syndrome Register */
  78. u32 hxfar; /* Hyp Data/Inst. Fault Address Register */
  79. u32 hpfar; /* Hyp IPA Fault Address Register */
  80. };
  81. /*
  82. * 0 is reserved as an invalid value.
  83. * Order should be kept in sync with the save/restore code.
  84. */
  85. enum vcpu_sysreg {
  86. __INVALID_SYSREG__,
  87. c0_MPIDR, /* MultiProcessor ID Register */
  88. c0_CSSELR, /* Cache Size Selection Register */
  89. c1_SCTLR, /* System Control Register */
  90. c1_ACTLR, /* Auxiliary Control Register */
  91. c1_CPACR, /* Coprocessor Access Control */
  92. c2_TTBR0, /* Translation Table Base Register 0 */
  93. c2_TTBR0_high, /* TTBR0 top 32 bits */
  94. c2_TTBR1, /* Translation Table Base Register 1 */
  95. c2_TTBR1_high, /* TTBR1 top 32 bits */
  96. c2_TTBCR, /* Translation Table Base Control R. */
  97. c3_DACR, /* Domain Access Control Register */
  98. c5_DFSR, /* Data Fault Status Register */
  99. c5_IFSR, /* Instruction Fault Status Register */
  100. c5_ADFSR, /* Auxilary Data Fault Status R */
  101. c5_AIFSR, /* Auxilary Instrunction Fault Status R */
  102. c6_DFAR, /* Data Fault Address Register */
  103. c6_IFAR, /* Instruction Fault Address Register */
  104. c7_PAR, /* Physical Address Register */
  105. c7_PAR_high, /* PAR top 32 bits */
  106. c9_L2CTLR, /* Cortex A15/A7 L2 Control Register */
  107. c10_PRRR, /* Primary Region Remap Register */
  108. c10_NMRR, /* Normal Memory Remap Register */
  109. c12_VBAR, /* Vector Base Address Register */
  110. c13_CID, /* Context ID Register */
  111. c13_TID_URW, /* Thread ID, User R/W */
  112. c13_TID_URO, /* Thread ID, User R/O */
  113. c13_TID_PRIV, /* Thread ID, Privileged */
  114. c14_CNTKCTL, /* Timer Control Register (PL1) */
  115. c10_AMAIR0, /* Auxilary Memory Attribute Indirection Reg0 */
  116. c10_AMAIR1, /* Auxilary Memory Attribute Indirection Reg1 */
  117. NR_CP15_REGS /* Number of regs (incl. invalid) */
  118. };
  119. struct kvm_cpu_context {
  120. struct kvm_regs gp_regs;
  121. struct vfp_hard_struct vfp;
  122. u32 cp15[NR_CP15_REGS];
  123. };
  124. typedef struct kvm_cpu_context kvm_cpu_context_t;
  125. struct kvm_vcpu_arch {
  126. struct kvm_cpu_context ctxt;
  127. int target; /* Processor target */
  128. DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
  129. /* The CPU type we expose to the VM */
  130. u32 midr;
  131. /* HYP trapping configuration */
  132. u32 hcr;
  133. /* Interrupt related fields */
  134. u32 irq_lines; /* IRQ and FIQ levels */
  135. /* Exception Information */
  136. struct kvm_vcpu_fault_info fault;
  137. /* Host FP context */
  138. kvm_cpu_context_t *host_cpu_context;
  139. /* VGIC state */
  140. struct vgic_cpu vgic_cpu;
  141. struct arch_timer_cpu timer_cpu;
  142. /*
  143. * Anything that is not used directly from assembly code goes
  144. * here.
  145. */
  146. /* vcpu power-off state */
  147. bool power_off;
  148. /* Don't run the guest (internal implementation need) */
  149. bool pause;
  150. /* IO related fields */
  151. struct kvm_decode mmio_decode;
  152. /* Cache some mmu pages needed inside spinlock regions */
  153. struct kvm_mmu_memory_cache mmu_page_cache;
  154. /* Detect first run of a vcpu */
  155. bool has_run_once;
  156. };
  157. struct kvm_vm_stat {
  158. ulong remote_tlb_flush;
  159. };
  160. struct kvm_vcpu_stat {
  161. u64 halt_successful_poll;
  162. u64 halt_attempted_poll;
  163. u64 halt_poll_invalid;
  164. u64 halt_wakeup;
  165. u64 hvc_exit_stat;
  166. u64 wfe_exit_stat;
  167. u64 wfi_exit_stat;
  168. u64 mmio_exit_user;
  169. u64 mmio_exit_kernel;
  170. u64 exits;
  171. };
  172. #define vcpu_cp15(v,r) (v)->arch.ctxt.cp15[r]
  173. int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
  174. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  175. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  176. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  177. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  178. unsigned long kvm_call_hyp(void *hypfn, ...);
  179. void force_vm_exit(const cpumask_t *mask);
  180. #define KVM_ARCH_WANT_MMU_NOTIFIER
  181. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  182. int kvm_unmap_hva_range(struct kvm *kvm,
  183. unsigned long start, unsigned long end);
  184. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  185. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  186. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  187. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
  188. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  189. /* We do not have shadow page tables, hence the empty hooks */
  190. static inline void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,
  191. unsigned long address)
  192. {
  193. }
  194. struct kvm_vcpu *kvm_arm_get_running_vcpu(void);
  195. struct kvm_vcpu __percpu **kvm_get_running_vcpus(void);
  196. void kvm_arm_halt_guest(struct kvm *kvm);
  197. void kvm_arm_resume_guest(struct kvm *kvm);
  198. void kvm_arm_halt_vcpu(struct kvm_vcpu *vcpu);
  199. void kvm_arm_resume_vcpu(struct kvm_vcpu *vcpu);
  200. int kvm_arm_copy_coproc_indices(struct kvm_vcpu *vcpu, u64 __user *uindices);
  201. unsigned long kvm_arm_num_coproc_regs(struct kvm_vcpu *vcpu);
  202. int kvm_arm_coproc_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  203. int kvm_arm_coproc_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  204. int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
  205. int exception_index);
  206. static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr,
  207. unsigned long hyp_stack_ptr,
  208. unsigned long vector_ptr)
  209. {
  210. /*
  211. * Call initialization code, and switch to the full blown HYP
  212. * code. The init code doesn't need to preserve these
  213. * registers as r0-r3 are already callee saved according to
  214. * the AAPCS.
  215. * Note that we slightly misuse the prototype by casting the
  216. * stack pointer to a void *.
  217. * The PGDs are always passed as the third argument, in order
  218. * to be passed into r2-r3 to the init code (yes, this is
  219. * compliant with the PCS!).
  220. */
  221. kvm_call_hyp((void*)hyp_stack_ptr, vector_ptr, pgd_ptr);
  222. }
  223. static inline void __cpu_init_stage2(void)
  224. {
  225. kvm_call_hyp(__init_stage2_translation);
  226. }
  227. static inline void __cpu_reset_hyp_mode(unsigned long vector_ptr,
  228. phys_addr_t phys_idmap_start)
  229. {
  230. kvm_call_hyp((void *)virt_to_idmap(__kvm_hyp_reset), vector_ptr);
  231. }
  232. static inline int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)
  233. {
  234. return 0;
  235. }
  236. int kvm_perf_init(void);
  237. int kvm_perf_teardown(void);
  238. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
  239. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
  240. static inline void kvm_arch_hardware_unsetup(void) {}
  241. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  242. static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
  243. static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
  244. static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
  245. static inline void kvm_arm_init_debug(void) {}
  246. static inline void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) {}
  247. static inline void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) {}
  248. static inline void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) {}
  249. static inline int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
  250. struct kvm_device_attr *attr)
  251. {
  252. return -ENXIO;
  253. }
  254. static inline int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
  255. struct kvm_device_attr *attr)
  256. {
  257. return -ENXIO;
  258. }
  259. static inline int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
  260. struct kvm_device_attr *attr)
  261. {
  262. return -ENXIO;
  263. }
  264. static inline bool kvm_arm_harden_branch_predictor(void)
  265. {
  266. /* No way to detect it yet, pretend it is not there. */
  267. return false;
  268. }
  269. #define KVM_SSBD_UNKNOWN -1
  270. #define KVM_SSBD_FORCE_DISABLE 0
  271. #define KVM_SSBD_KERNEL 1
  272. #define KVM_SSBD_FORCE_ENABLE 2
  273. #define KVM_SSBD_MITIGATED 3
  274. static inline int kvm_arm_have_ssbd(void)
  275. {
  276. /* No way to detect it yet, pretend it is not there. */
  277. return KVM_SSBD_UNKNOWN;
  278. }
  279. #endif /* __ARM_KVM_HOST_H__ */