kvm_host.h 8.8 KB

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