kvm_host.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
  4. */
  5. #ifndef __ASM_LOONGARCH_KVM_HOST_H__
  6. #define __ASM_LOONGARCH_KVM_HOST_H__
  7. #include <linux/cpumask.h>
  8. #include <linux/hrtimer.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kvm.h>
  11. #include <linux/kvm_types.h>
  12. #include <linux/mutex.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/threads.h>
  16. #include <linux/types.h>
  17. #include <asm/inst.h>
  18. #include <asm/kvm_mmu.h>
  19. #include <asm/kvm_ipi.h>
  20. #include <asm/kvm_eiointc.h>
  21. #include <asm/kvm_pch_pic.h>
  22. #include <asm/loongarch.h>
  23. #define __KVM_HAVE_ARCH_INTC_INITIALIZED
  24. /* Loongarch KVM register ids */
  25. #define KVM_GET_IOC_CSR_IDX(id) ((id & KVM_CSR_IDX_MASK) >> LOONGARCH_REG_SHIFT)
  26. #define KVM_GET_IOC_CPUCFG_IDX(id) ((id & KVM_CPUCFG_IDX_MASK) >> LOONGARCH_REG_SHIFT)
  27. #define KVM_MAX_VCPUS 256
  28. #define KVM_MAX_CPUCFG_REGS 21
  29. #define KVM_HALT_POLL_NS_DEFAULT 500000
  30. #define KVM_REQ_TLB_FLUSH_GPA KVM_ARCH_REQ(0)
  31. #define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(1)
  32. #define KVM_REQ_PMU KVM_ARCH_REQ(2)
  33. #define KVM_REQ_AUX_LOAD KVM_ARCH_REQ(3)
  34. #define KVM_GUESTDBG_SW_BP_MASK \
  35. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)
  36. #define KVM_GUESTDBG_VALID_MASK \
  37. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP | KVM_GUESTDBG_SINGLESTEP)
  38. #define KVM_DIRTY_LOG_MANUAL_CAPS \
  39. (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | KVM_DIRTY_LOG_INITIALLY_SET)
  40. struct kvm_vm_stat {
  41. struct kvm_vm_stat_generic generic;
  42. u64 pages;
  43. u64 hugepages;
  44. };
  45. struct kvm_vcpu_stat {
  46. struct kvm_vcpu_stat_generic generic;
  47. u64 int_exits;
  48. u64 idle_exits;
  49. u64 cpucfg_exits;
  50. u64 signal_exits;
  51. u64 hypercall_exits;
  52. u64 ipi_read_exits;
  53. u64 ipi_write_exits;
  54. u64 eiointc_read_exits;
  55. u64 eiointc_write_exits;
  56. u64 pch_pic_read_exits;
  57. u64 pch_pic_write_exits;
  58. };
  59. #define KVM_MEM_HUGEPAGE_CAPABLE (1UL << 0)
  60. #define KVM_MEM_HUGEPAGE_INCAPABLE (1UL << 1)
  61. struct kvm_arch_memory_slot {
  62. unsigned long flags;
  63. };
  64. #define HOST_MAX_PMNUM 16
  65. struct kvm_context {
  66. unsigned long vpid_cache;
  67. struct kvm_vcpu *last_vcpu;
  68. /* Host PMU CSR */
  69. u64 perf_ctrl[HOST_MAX_PMNUM];
  70. u64 perf_cntr[HOST_MAX_PMNUM];
  71. };
  72. struct kvm_world_switch {
  73. int (*exc_entry)(void);
  74. int (*enter_guest)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  75. unsigned long page_order;
  76. };
  77. #define MAX_PGTABLE_LEVELS 4
  78. /*
  79. * Physical CPUID is used for interrupt routing, there are different
  80. * definitions about physical cpuid on different hardwares.
  81. *
  82. * For LOONGARCH_CSR_CPUID register, max CPUID size if 512
  83. * For IPI hardware, max destination CPUID size 1024
  84. * For eiointc interrupt controller, max destination CPUID size is 256
  85. * For msgint interrupt controller, max supported CPUID size is 65536
  86. *
  87. * Currently max CPUID is defined as 256 for KVM hypervisor, in future
  88. * it will be expanded to 4096, including 16 packages at most. And every
  89. * package supports at most 256 vcpus
  90. */
  91. #define KVM_MAX_PHYID 256
  92. struct kvm_phyid_info {
  93. struct kvm_vcpu *vcpu;
  94. bool enabled;
  95. };
  96. struct kvm_phyid_map {
  97. int max_phyid;
  98. struct kvm_phyid_info phys_map[KVM_MAX_PHYID];
  99. };
  100. struct kvm_arch {
  101. /* Guest physical mm */
  102. kvm_pte_t *pgd;
  103. unsigned long gpa_size;
  104. unsigned long invalid_ptes[MAX_PGTABLE_LEVELS];
  105. unsigned int pte_shifts[MAX_PGTABLE_LEVELS];
  106. unsigned int root_level;
  107. spinlock_t phyid_map_lock;
  108. struct kvm_phyid_map *phyid_map;
  109. /* Enabled PV features */
  110. unsigned long pv_features;
  111. /* Supported KVM features */
  112. unsigned long kvm_features;
  113. s64 time_offset;
  114. struct kvm_context __percpu *vmcs;
  115. struct loongarch_ipi *ipi;
  116. struct loongarch_eiointc *eiointc;
  117. struct loongarch_pch_pic *pch_pic;
  118. };
  119. #define CSR_MAX_NUMS 0x800
  120. struct loongarch_csrs {
  121. unsigned long csrs[CSR_MAX_NUMS];
  122. };
  123. /* Resume Flags */
  124. #define RESUME_HOST 0
  125. #define RESUME_GUEST 1
  126. enum emulation_result {
  127. EMULATE_DONE, /* no further processing */
  128. EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
  129. EMULATE_DO_IOCSR, /* handle IOCSR request */
  130. EMULATE_FAIL, /* can't emulate this instruction */
  131. EMULATE_EXCEPT, /* A guest exception has been generated */
  132. };
  133. #define KVM_LARCH_FPU (0x1 << 0)
  134. #define KVM_LARCH_LSX (0x1 << 1)
  135. #define KVM_LARCH_LASX (0x1 << 2)
  136. #define KVM_LARCH_LBT (0x1 << 3)
  137. #define KVM_LARCH_PMU (0x1 << 4)
  138. #define KVM_LARCH_SWCSR_LATEST (0x1 << 5)
  139. #define KVM_LARCH_HWCSR_USABLE (0x1 << 6)
  140. #define LOONGARCH_PV_FEAT_UPDATED BIT_ULL(63)
  141. #define LOONGARCH_PV_FEAT_MASK (BIT(KVM_FEATURE_IPI) | \
  142. BIT(KVM_FEATURE_PREEMPT) | \
  143. BIT(KVM_FEATURE_STEAL_TIME) | \
  144. BIT(KVM_FEATURE_USER_HCALL) | \
  145. BIT(KVM_FEATURE_VIRT_EXTIOI))
  146. struct kvm_vcpu_arch {
  147. /*
  148. * Switch pointer-to-function type to unsigned long
  149. * for loading the value into register directly.
  150. */
  151. unsigned long host_eentry;
  152. unsigned long guest_eentry;
  153. /* Pointers stored here for easy accessing from assembly code */
  154. int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  155. /* GPA (=HVA) of PGD for secondary mmu */
  156. unsigned long kvm_pgd;
  157. /* Host registers preserved across guest mode execution */
  158. unsigned long host_sp;
  159. unsigned long host_tp;
  160. unsigned long host_pgd;
  161. /* Host CSRs are used when handling exits from guest */
  162. unsigned long badi;
  163. unsigned long badv;
  164. unsigned long host_ecfg;
  165. unsigned long host_estat;
  166. unsigned long host_percpu;
  167. /* GPRs */
  168. unsigned long gprs[32];
  169. unsigned long pc;
  170. /* Which auxiliary state is loaded (KVM_LARCH_*) */
  171. unsigned int aux_inuse;
  172. unsigned int aux_ldtype;
  173. /* FPU state */
  174. struct loongarch_fpu fpu FPU_ALIGN;
  175. struct loongarch_lbt lbt;
  176. /* CSR state */
  177. struct loongarch_csrs *csr;
  178. /* Guest max PMU CSR id */
  179. int max_pmu_csrid;
  180. /* GPR used as IO source/target */
  181. u32 io_gpr;
  182. /* KVM register to control count timer */
  183. u32 count_ctl;
  184. struct hrtimer swtimer;
  185. /* Bitmask of intr that are pending */
  186. unsigned long irq_pending;
  187. /* Bitmask of pending intr to be cleared */
  188. unsigned long irq_clear;
  189. /* Bitmask of exceptions that are pending */
  190. unsigned long exception_pending;
  191. unsigned int esubcode;
  192. /* Cache for pages needed inside spinlock regions */
  193. struct kvm_mmu_memory_cache mmu_page_cache;
  194. /* vcpu's vpid */
  195. u64 vpid;
  196. gpa_t flush_gpa;
  197. /* Frequency of stable timer in Hz */
  198. u64 timer_mhz;
  199. ktime_t expire;
  200. /* Last CPU the vCPU state was loaded on */
  201. int last_sched_cpu;
  202. /* mp state */
  203. struct kvm_mp_state mp_state;
  204. /* ipi state */
  205. struct ipi_state ipi_state;
  206. /* cpucfg */
  207. u32 cpucfg[KVM_MAX_CPUCFG_REGS];
  208. /* paravirt steal time */
  209. struct {
  210. u64 guest_addr;
  211. u64 last_steal;
  212. struct gfn_to_hva_cache cache;
  213. u8 preempted;
  214. } st;
  215. };
  216. static inline unsigned long readl_sw_gcsr(struct loongarch_csrs *csr, int reg)
  217. {
  218. return csr->csrs[reg];
  219. }
  220. static inline void writel_sw_gcsr(struct loongarch_csrs *csr, int reg, unsigned long val)
  221. {
  222. csr->csrs[reg] = val;
  223. }
  224. static inline bool kvm_guest_has_msgint(struct kvm_vcpu_arch *arch)
  225. {
  226. return arch->cpucfg[1] & CPUCFG1_MSGINT;
  227. }
  228. static inline bool kvm_guest_has_fpu(struct kvm_vcpu_arch *arch)
  229. {
  230. return arch->cpucfg[2] & CPUCFG2_FP;
  231. }
  232. static inline bool kvm_guest_has_lsx(struct kvm_vcpu_arch *arch)
  233. {
  234. return arch->cpucfg[2] & CPUCFG2_LSX;
  235. }
  236. static inline bool kvm_guest_has_lasx(struct kvm_vcpu_arch *arch)
  237. {
  238. return arch->cpucfg[2] & CPUCFG2_LASX;
  239. }
  240. static inline bool kvm_guest_has_lbt(struct kvm_vcpu_arch *arch)
  241. {
  242. return arch->cpucfg[2] & (CPUCFG2_X86BT | CPUCFG2_ARMBT | CPUCFG2_MIPSBT);
  243. }
  244. static inline bool kvm_guest_has_pmu(struct kvm_vcpu_arch *arch)
  245. {
  246. return arch->cpucfg[6] & CPUCFG6_PMP;
  247. }
  248. static inline int kvm_get_pmu_num(struct kvm_vcpu_arch *arch)
  249. {
  250. return (arch->cpucfg[6] & CPUCFG6_PMNUM) >> CPUCFG6_PMNUM_SHIFT;
  251. }
  252. /* Check whether KVM support this feature (VMM may disable it) */
  253. static inline bool kvm_vm_support(struct kvm_arch *arch, int feature)
  254. {
  255. return !!(arch->kvm_features & BIT_ULL(feature));
  256. }
  257. bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu);
  258. /* Debug: dump vcpu state */
  259. int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu);
  260. /* MMU handling */
  261. void kvm_flush_tlb_all(void);
  262. void kvm_flush_tlb_gpa(struct kvm_vcpu *vcpu, unsigned long gpa);
  263. int kvm_handle_mm_fault(struct kvm_vcpu *vcpu, unsigned long badv, bool write, int ecode);
  264. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end, bool blockable);
  265. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
  266. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  267. static inline void update_pc(struct kvm_vcpu_arch *arch)
  268. {
  269. arch->pc += 4;
  270. }
  271. /*
  272. * kvm_is_ifetch_fault() - Find whether a TLBL exception is due to ifetch fault.
  273. * @vcpu: Virtual CPU.
  274. *
  275. * Returns: Whether the TLBL exception was likely due to an instruction
  276. * fetch fault rather than a data load fault.
  277. */
  278. static inline bool kvm_is_ifetch_fault(struct kvm_vcpu_arch *arch)
  279. {
  280. return arch->pc == arch->badv;
  281. }
  282. /* Misc */
  283. static inline void kvm_arch_hardware_unsetup(void) {}
  284. static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {}
  285. static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
  286. static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
  287. static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
  288. static inline void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) {}
  289. void kvm_check_vpid(struct kvm_vcpu *vcpu);
  290. enum hrtimer_restart kvm_swtimer_wakeup(struct hrtimer *timer);
  291. void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, const struct kvm_memory_slot *memslot);
  292. void kvm_init_vmcs(struct kvm *kvm);
  293. void kvm_exc_entry(void);
  294. int kvm_enter_guest(struct kvm_run *run, struct kvm_vcpu *vcpu);
  295. extern unsigned long vpid_mask;
  296. extern const unsigned long kvm_exception_size;
  297. extern const unsigned long kvm_enter_guest_size;
  298. extern struct kvm_world_switch *kvm_loongarch_ops;
  299. #define SW_GCSR (1 << 0)
  300. #define HW_GCSR (1 << 1)
  301. #define INVALID_GCSR (1 << 2)
  302. int get_gcsr_flag(int csr);
  303. void set_hw_gcsr(int csr_id, unsigned long val);
  304. #endif /* __ASM_LOONGARCH_KVM_HOST_H__ */