kvm_host.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <[email protected]>
  5. *
  6. * Derived from arch/arm/include/asm/kvm_host.h:
  7. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  8. * Author: Christoffer Dall <[email protected]>
  9. */
  10. #ifndef __ARM64_KVM_HOST_H__
  11. #define __ARM64_KVM_HOST_H__
  12. #include <linux/arm-smccc.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/types.h>
  15. #include <linux/jump_label.h>
  16. #include <linux/kvm_types.h>
  17. #include <linux/percpu.h>
  18. #include <linux/psci.h>
  19. #include <asm/arch_gicv3.h>
  20. #include <asm/barrier.h>
  21. #include <asm/cpufeature.h>
  22. #include <asm/cputype.h>
  23. #include <asm/daifflags.h>
  24. #include <asm/fpsimd.h>
  25. #include <asm/kvm.h>
  26. #include <asm/kvm_asm.h>
  27. #define __KVM_HAVE_ARCH_INTC_INITIALIZED
  28. #define KVM_HALT_POLL_NS_DEFAULT 500000
  29. #include <kvm/arm_vgic.h>
  30. #include <kvm/arm_arch_timer.h>
  31. #include <kvm/arm_pmu.h>
  32. #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
  33. #define KVM_VCPU_MAX_FEATURES 7
  34. #define KVM_REQ_SLEEP \
  35. KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  36. #define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)
  37. #define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2)
  38. #define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)
  39. #define KVM_REQ_RELOAD_GICv4 KVM_ARCH_REQ(4)
  40. #define KVM_REQ_RELOAD_PMU KVM_ARCH_REQ(5)
  41. #define KVM_REQ_SUSPEND KVM_ARCH_REQ(6)
  42. #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  43. KVM_DIRTY_LOG_INITIALLY_SET)
  44. #define KVM_HAVE_MMU_RWLOCK
  45. /*
  46. * Mode of operation configurable with kvm-arm.mode early param.
  47. * See Documentation/admin-guide/kernel-parameters.txt for more information.
  48. */
  49. enum kvm_mode {
  50. KVM_MODE_DEFAULT,
  51. KVM_MODE_PROTECTED,
  52. KVM_MODE_NONE,
  53. };
  54. enum kvm_mode kvm_get_mode(void);
  55. DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
  56. extern unsigned int kvm_sve_max_vl;
  57. extern unsigned int kvm_host_sve_max_vl;
  58. int kvm_arm_init_sve(void);
  59. u32 __attribute_const__ kvm_target_cpu(void);
  60. int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  61. void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
  62. struct kvm_hyp_memcache {
  63. phys_addr_t head;
  64. unsigned long nr_pages;
  65. };
  66. static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,
  67. phys_addr_t *p,
  68. phys_addr_t (*to_pa)(void *virt))
  69. {
  70. *p = mc->head;
  71. mc->head = to_pa(p);
  72. mc->nr_pages++;
  73. }
  74. static inline void *pop_hyp_memcache(struct kvm_hyp_memcache *mc,
  75. void *(*to_va)(phys_addr_t phys))
  76. {
  77. phys_addr_t *p = to_va(mc->head);
  78. if (!mc->nr_pages)
  79. return NULL;
  80. mc->head = *p;
  81. mc->nr_pages--;
  82. return p;
  83. }
  84. static inline int __topup_hyp_memcache(struct kvm_hyp_memcache *mc,
  85. unsigned long min_pages,
  86. void *(*alloc_fn)(void *arg),
  87. phys_addr_t (*to_pa)(void *virt),
  88. void *arg)
  89. {
  90. while (mc->nr_pages < min_pages) {
  91. phys_addr_t *p = alloc_fn(arg);
  92. if (!p)
  93. return -ENOMEM;
  94. push_hyp_memcache(mc, p, to_pa);
  95. }
  96. return 0;
  97. }
  98. static inline void __free_hyp_memcache(struct kvm_hyp_memcache *mc,
  99. void (*free_fn)(void *virt, void *arg),
  100. void *(*to_va)(phys_addr_t phys),
  101. void *arg)
  102. {
  103. while (mc->nr_pages)
  104. free_fn(pop_hyp_memcache(mc, to_va), arg);
  105. }
  106. void free_hyp_memcache(struct kvm_hyp_memcache *mc, struct kvm *kvm);
  107. void free_hyp_stage2_memcache(struct kvm_hyp_memcache *mc, struct kvm *kvm);
  108. int topup_hyp_memcache(struct kvm_vcpu *vcpu);
  109. struct kvm_vmid {
  110. atomic64_t id;
  111. };
  112. struct kvm_s2_mmu {
  113. struct kvm_vmid vmid;
  114. /*
  115. * stage2 entry level table
  116. *
  117. * Two kvm_s2_mmu structures in the same VM can point to the same
  118. * pgd here. This happens when running a guest using a
  119. * translation regime that isn't affected by its own stage-2
  120. * translation, such as a non-VHE hypervisor running at vEL2, or
  121. * for vEL1/EL0 with vHCR_EL2.VM == 0. In that case, we use the
  122. * canonical stage-2 page tables.
  123. */
  124. phys_addr_t pgd_phys;
  125. struct kvm_pgtable *pgt;
  126. /* The last vcpu id that ran on each physical CPU */
  127. int __percpu *last_vcpu_ran;
  128. struct kvm_arch *arch;
  129. };
  130. struct kvm_arch_memory_slot {
  131. };
  132. /**
  133. * struct kvm_smccc_features: Descriptor of the hypercall services exposed to the guests
  134. *
  135. * @std_bmap: Bitmap of standard secure service calls
  136. * @std_hyp_bmap: Bitmap of standard hypervisor service calls
  137. * @vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls
  138. */
  139. struct kvm_smccc_features {
  140. unsigned long std_bmap;
  141. unsigned long std_hyp_bmap;
  142. unsigned long vendor_hyp_bmap;
  143. };
  144. struct kvm_pinned_page {
  145. struct rb_node node;
  146. struct page *page;
  147. u64 ipa;
  148. };
  149. typedef unsigned int pkvm_handle_t;
  150. struct kvm_protected_vm {
  151. pkvm_handle_t handle;
  152. struct kvm_hyp_memcache teardown_mc;
  153. struct kvm_hyp_memcache teardown_stage2_mc;
  154. struct rb_root pinned_pages;
  155. gpa_t pvmfw_load_addr;
  156. bool enabled;
  157. };
  158. struct kvm_arch {
  159. struct kvm_s2_mmu mmu;
  160. /* VTCR_EL2 value for this VM */
  161. u64 vtcr;
  162. /* Interrupt controller */
  163. struct vgic_dist vgic;
  164. /* Mandated version of PSCI */
  165. u32 psci_version;
  166. #ifndef __GENKSYMS__
  167. /* Protects VM-scoped configuration data */
  168. struct mutex config_lock;
  169. #endif
  170. /*
  171. * If we encounter a data abort without valid instruction syndrome
  172. * information, report this to user space. User space can (and
  173. * should) opt in to this feature if KVM_CAP_ARM_NISV_TO_USER is
  174. * supported.
  175. */
  176. #define KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER 0
  177. /* Memory Tagging Extension enabled for the guest */
  178. #define KVM_ARCH_FLAG_MTE_ENABLED 1
  179. /* At least one vCPU has ran in the VM */
  180. #define KVM_ARCH_FLAG_HAS_RAN_ONCE 2
  181. /*
  182. * The following two bits are used to indicate the guest's EL1
  183. * register width configuration. A value of KVM_ARCH_FLAG_EL1_32BIT
  184. * bit is valid only when KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED is set.
  185. * Otherwise, the guest's EL1 register width has not yet been
  186. * determined yet.
  187. */
  188. #define KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED 3
  189. #define KVM_ARCH_FLAG_EL1_32BIT 4
  190. /* PSCI SYSTEM_SUSPEND enabled for the guest */
  191. #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED 5
  192. /* Guest has bought into the MMIO guard extension */
  193. #define KVM_ARCH_FLAG_MMIO_GUARD 6
  194. unsigned long flags;
  195. /*
  196. * VM-wide PMU filter, implemented as a bitmap and big enough for
  197. * up to 2^10 events (ARMv8.0) or 2^16 events (ARMv8.1+).
  198. */
  199. unsigned long *pmu_filter;
  200. struct arm_pmu *arm_pmu;
  201. cpumask_var_t supported_cpus;
  202. u8 pfr0_csv2;
  203. u8 pfr0_csv3;
  204. /* Hypercall features firmware registers' descriptor */
  205. struct kvm_smccc_features smccc_feat;
  206. /*
  207. * For an untrusted host VM, 'pkvm.handle' is used to lookup
  208. * the associated pKVM instance in the hypervisor.
  209. */
  210. struct kvm_protected_vm pkvm;
  211. };
  212. struct kvm_vcpu_fault_info {
  213. u64 esr_el2; /* Hyp Syndrom Register */
  214. u64 far_el2; /* Hyp Fault Address Register */
  215. u64 hpfar_el2; /* Hyp IPA Fault Address Register */
  216. u64 disr_el1; /* Deferred [SError] Status Register */
  217. };
  218. enum vcpu_sysreg {
  219. __INVALID_SYSREG__, /* 0 is reserved as an invalid value */
  220. MPIDR_EL1, /* MultiProcessor Affinity Register */
  221. CSSELR_EL1, /* Cache Size Selection Register */
  222. SCTLR_EL1, /* System Control Register */
  223. ACTLR_EL1, /* Auxiliary Control Register */
  224. CPACR_EL1, /* Coprocessor Access Control */
  225. ZCR_EL1, /* SVE Control */
  226. TTBR0_EL1, /* Translation Table Base Register 0 */
  227. TTBR1_EL1, /* Translation Table Base Register 1 */
  228. TCR_EL1, /* Translation Control Register */
  229. ESR_EL1, /* Exception Syndrome Register */
  230. AFSR0_EL1, /* Auxiliary Fault Status Register 0 */
  231. AFSR1_EL1, /* Auxiliary Fault Status Register 1 */
  232. FAR_EL1, /* Fault Address Register */
  233. MAIR_EL1, /* Memory Attribute Indirection Register */
  234. VBAR_EL1, /* Vector Base Address Register */
  235. CONTEXTIDR_EL1, /* Context ID Register */
  236. TPIDR_EL0, /* Thread ID, User R/W */
  237. TPIDRRO_EL0, /* Thread ID, User R/O */
  238. TPIDR_EL1, /* Thread ID, Privileged */
  239. AMAIR_EL1, /* Aux Memory Attribute Indirection Register */
  240. CNTKCTL_EL1, /* Timer Control Register (EL1) */
  241. PAR_EL1, /* Physical Address Register */
  242. MDSCR_EL1, /* Monitor Debug System Control Register */
  243. MDCCINT_EL1, /* Monitor Debug Comms Channel Interrupt Enable Reg */
  244. OSLSR_EL1, /* OS Lock Status Register */
  245. DISR_EL1, /* Deferred Interrupt Status Register */
  246. /* Performance Monitors Registers */
  247. PMCR_EL0, /* Control Register */
  248. PMSELR_EL0, /* Event Counter Selection Register */
  249. PMEVCNTR0_EL0, /* Event Counter Register (0-30) */
  250. PMEVCNTR30_EL0 = PMEVCNTR0_EL0 + 30,
  251. PMCCNTR_EL0, /* Cycle Counter Register */
  252. PMEVTYPER0_EL0, /* Event Type Register (0-30) */
  253. PMEVTYPER30_EL0 = PMEVTYPER0_EL0 + 30,
  254. PMCCFILTR_EL0, /* Cycle Count Filter Register */
  255. PMCNTENSET_EL0, /* Count Enable Set Register */
  256. PMINTENSET_EL1, /* Interrupt Enable Set Register */
  257. PMOVSSET_EL0, /* Overflow Flag Status Set Register */
  258. PMUSERENR_EL0, /* User Enable Register */
  259. /* Pointer Authentication Registers in a strict increasing order. */
  260. APIAKEYLO_EL1,
  261. APIAKEYHI_EL1,
  262. APIBKEYLO_EL1,
  263. APIBKEYHI_EL1,
  264. APDAKEYLO_EL1,
  265. APDAKEYHI_EL1,
  266. APDBKEYLO_EL1,
  267. APDBKEYHI_EL1,
  268. APGAKEYLO_EL1,
  269. APGAKEYHI_EL1,
  270. ELR_EL1,
  271. SP_EL1,
  272. SPSR_EL1,
  273. CNTVOFF_EL2,
  274. CNTV_CVAL_EL0,
  275. CNTV_CTL_EL0,
  276. CNTP_CVAL_EL0,
  277. CNTP_CTL_EL0,
  278. /* Memory Tagging Extension registers */
  279. RGSR_EL1, /* Random Allocation Tag Seed Register */
  280. GCR_EL1, /* Tag Control Register */
  281. TFSR_EL1, /* Tag Fault Status Register (EL1) */
  282. TFSRE0_EL1, /* Tag Fault Status Register (EL0) */
  283. /* 32bit specific registers. Keep them at the end of the range */
  284. DACR32_EL2, /* Domain Access Control Register */
  285. IFSR32_EL2, /* Instruction Fault Status Register */
  286. FPEXC32_EL2, /* Floating-Point Exception Control Register */
  287. DBGVCR32_EL2, /* Debug Vector Catch Register */
  288. NR_SYS_REGS /* Nothing after this line! */
  289. };
  290. struct kvm_cpu_context {
  291. struct user_pt_regs regs; /* sp = sp_el0 */
  292. u64 spsr_abt;
  293. u64 spsr_und;
  294. u64 spsr_irq;
  295. u64 spsr_fiq;
  296. struct user_fpsimd_state fp_regs;
  297. u64 sys_regs[NR_SYS_REGS];
  298. #ifdef __GENKSYMS__
  299. struct kvm_vcpu *__hyp_running_vcpu;
  300. #else
  301. void *__hyp_running_vcpu;
  302. #endif
  303. };
  304. struct kvm_host_data {
  305. struct kvm_cpu_context host_ctxt;
  306. };
  307. struct kvm_host_psci_config {
  308. /* PSCI version used by host. */
  309. u32 version;
  310. u32 smccc_version;
  311. /* Function IDs used by host if version is v0.1. */
  312. struct psci_0_1_function_ids function_ids_0_1;
  313. bool psci_0_1_cpu_suspend_implemented;
  314. bool psci_0_1_cpu_on_implemented;
  315. bool psci_0_1_cpu_off_implemented;
  316. bool psci_0_1_migrate_implemented;
  317. };
  318. extern struct kvm_host_psci_config kvm_nvhe_sym(kvm_host_psci_config);
  319. #define kvm_host_psci_config CHOOSE_NVHE_SYM(kvm_host_psci_config)
  320. extern s64 kvm_nvhe_sym(hyp_physvirt_offset);
  321. #define hyp_physvirt_offset CHOOSE_NVHE_SYM(hyp_physvirt_offset)
  322. extern u64 kvm_nvhe_sym(hyp_cpu_logical_map)[NR_CPUS];
  323. #define hyp_cpu_logical_map CHOOSE_NVHE_SYM(hyp_cpu_logical_map)
  324. enum pkvm_iommu_pm_event {
  325. PKVM_IOMMU_PM_SUSPEND,
  326. PKVM_IOMMU_PM_RESUME,
  327. };
  328. struct pkvm_iommu_ops;
  329. struct pkvm_iommu_driver {
  330. const struct pkvm_iommu_ops *ops;
  331. struct list_head list;
  332. atomic_t state;
  333. };
  334. int pkvm_iommu_driver_init(u64 drv, void *data, size_t size);
  335. int pkvm_iommu_register(struct device *dev, u64 drv, phys_addr_t pa,
  336. size_t size, struct device *parent, u8 flags);
  337. int pkvm_iommu_suspend(struct device *dev);
  338. int pkvm_iommu_resume(struct device *dev);
  339. /*
  340. * Reject future calls to pkvm_iommu_driver_init() and pkvm_iommu_register()
  341. * and report errors if found. Incase of errors pKVM can take proper actions
  342. * as erasing pvmfw.
  343. */
  344. int pkvm_iommu_finalize(int err);
  345. bool pkvm_iommu_finalized(void);
  346. struct vcpu_reset_state {
  347. unsigned long pc;
  348. unsigned long r0;
  349. bool be;
  350. bool reset;
  351. };
  352. struct kvm_vcpu_arch {
  353. struct kvm_cpu_context ctxt;
  354. /* Guest floating point state */
  355. void *sve_state;
  356. unsigned int sve_max_vl;
  357. u64 svcr;
  358. /* Stage 2 paging state used by the hardware on next switch */
  359. struct kvm_s2_mmu *hw_mmu;
  360. /* Values of trap registers for the guest. */
  361. u64 hcr_el2;
  362. u64 mdcr_el2;
  363. u64 cptr_el2;
  364. /* Values of trap registers for the host before guest entry. */
  365. u64 mdcr_el2_host;
  366. /* Exception Information */
  367. struct kvm_vcpu_fault_info fault;
  368. /* Ownership of the FP regs */
  369. enum {
  370. FP_STATE_FREE,
  371. FP_STATE_HOST_OWNED,
  372. FP_STATE_GUEST_OWNED,
  373. } fp_state;
  374. /* Configuration flags, set once and for all before the vcpu can run */
  375. u8 cflags;
  376. /* Input flags to the hypervisor code, potentially cleared after use */
  377. u8 iflags;
  378. /* State flags for kernel bookkeeping, unused by the hypervisor code */
  379. u8 sflags;
  380. /*
  381. * Don't run the guest (internal implementation need).
  382. *
  383. * Contrary to the flags above, this is set/cleared outside of
  384. * a vcpu context, and thus cannot be mixed with the flags
  385. * themselves (or the flag accesses need to be made atomic).
  386. */
  387. bool pause;
  388. /*
  389. * We maintain more than a single set of debug registers to support
  390. * debugging the guest from the host and to maintain separate host and
  391. * guest state during world switches. vcpu_debug_state are the debug
  392. * registers of the vcpu as the guest sees them. host_debug_state are
  393. * the host registers which are saved and restored during
  394. * world switches. external_debug_state contains the debug
  395. * values we want to debug the guest. This is set via the
  396. * KVM_SET_GUEST_DEBUG ioctl.
  397. *
  398. * debug_ptr points to the set of debug registers that should be loaded
  399. * onto the hardware when running the guest.
  400. */
  401. struct kvm_guest_debug_arch *debug_ptr;
  402. struct kvm_guest_debug_arch vcpu_debug_state;
  403. struct kvm_guest_debug_arch external_debug_state;
  404. struct user_fpsimd_state *host_fpsimd_state; /* hyp VA */
  405. struct {
  406. /* {Break,watch}point registers */
  407. struct kvm_guest_debug_arch regs;
  408. /* Statistical profiling extension */
  409. u64 pmscr_el1;
  410. /* Self-hosted trace */
  411. u64 trfcr_el1;
  412. } host_debug_state;
  413. /* VGIC state */
  414. struct vgic_cpu vgic_cpu;
  415. struct arch_timer_cpu timer_cpu;
  416. struct kvm_pmu pmu;
  417. /*
  418. * Guest registers we preserve during guest debugging.
  419. *
  420. * These shadow registers are updated by the kvm_handle_sys_reg
  421. * trap handler if the guest accesses or updates them while we
  422. * are using guest debug.
  423. */
  424. struct {
  425. u32 mdscr_el1;
  426. bool pstate_ss;
  427. } guest_debug_preserved;
  428. /* vcpu power state */
  429. struct kvm_mp_state mp_state;
  430. #ifndef __GENKSYMS__
  431. spinlock_t mp_state_lock;
  432. #endif
  433. union {
  434. /* Cache some mmu pages needed inside spinlock regions */
  435. struct kvm_mmu_memory_cache mmu_page_cache;
  436. /* Pages to be donated to pkvm/EL2 if it runs out */
  437. struct kvm_hyp_memcache pkvm_memcache;
  438. };
  439. /* Target CPU and feature flags */
  440. int target;
  441. DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
  442. /* Virtual SError ESR to restore when HCR_EL2.VSE is set */
  443. u64 vsesr_el2;
  444. /* Additional reset state */
  445. struct vcpu_reset_state reset_state;
  446. /* Guest PV state */
  447. struct {
  448. u64 last_steal;
  449. gpa_t base;
  450. } steal;
  451. };
  452. /*
  453. * Each 'flag' is composed of a comma-separated triplet:
  454. *
  455. * - the flag-set it belongs to in the vcpu->arch structure
  456. * - the value for that flag
  457. * - the mask for that flag
  458. *
  459. * __vcpu_single_flag() builds such a triplet for a single-bit flag.
  460. * unpack_vcpu_flag() extract the flag value from the triplet for
  461. * direct use outside of the flag accessors.
  462. */
  463. #define __vcpu_single_flag(_set, _f) _set, (_f), (_f)
  464. #define __unpack_flag(_set, _f, _m) _f
  465. #define unpack_vcpu_flag(...) __unpack_flag(__VA_ARGS__)
  466. #define __build_check_flag(v, flagset, f, m) \
  467. do { \
  468. typeof(v->arch.flagset) *_fset; \
  469. \
  470. /* Check that the flags fit in the mask */ \
  471. BUILD_BUG_ON(HWEIGHT(m) != HWEIGHT((f) | (m))); \
  472. /* Check that the flags fit in the type */ \
  473. BUILD_BUG_ON((sizeof(*_fset) * 8) <= __fls(m)); \
  474. } while (0)
  475. #define __vcpu_get_flag(v, flagset, f, m) \
  476. ({ \
  477. __build_check_flag(v, flagset, f, m); \
  478. \
  479. READ_ONCE(v->arch.flagset) & (m); \
  480. })
  481. /*
  482. * Note that the set/clear accessors must be preempt-safe in order to
  483. * avoid nesting them with load/put which also manipulate flags...
  484. */
  485. #ifdef __KVM_NVHE_HYPERVISOR__
  486. /* the nVHE hypervisor is always non-preemptible */
  487. #define __vcpu_flags_preempt_disable()
  488. #define __vcpu_flags_preempt_enable()
  489. #else
  490. #define __vcpu_flags_preempt_disable() preempt_disable()
  491. #define __vcpu_flags_preempt_enable() preempt_enable()
  492. #endif
  493. #define __vcpu_set_flag(v, flagset, f, m) \
  494. do { \
  495. typeof(v->arch.flagset) *fset; \
  496. \
  497. __build_check_flag(v, flagset, f, m); \
  498. \
  499. fset = &v->arch.flagset; \
  500. __vcpu_flags_preempt_disable(); \
  501. if (HWEIGHT(m) > 1) \
  502. *fset &= ~(m); \
  503. *fset |= (f); \
  504. __vcpu_flags_preempt_enable(); \
  505. } while (0)
  506. #define __vcpu_clear_flag(v, flagset, f, m) \
  507. do { \
  508. typeof(v->arch.flagset) *fset; \
  509. \
  510. __build_check_flag(v, flagset, f, m); \
  511. \
  512. fset = &v->arch.flagset; \
  513. __vcpu_flags_preempt_disable(); \
  514. *fset &= ~(m); \
  515. __vcpu_flags_preempt_enable(); \
  516. } while (0)
  517. #define __vcpu_copy_flag(vt, vs, flagset, f, m) \
  518. do { \
  519. typeof(vs->arch.flagset) tmp, val; \
  520. \
  521. __build_check_flag(vs, flagset, f, m); \
  522. \
  523. __vcpu_flags_preempt_disable(); \
  524. val = READ_ONCE(vs->arch.flagset); \
  525. val &= (m); \
  526. tmp = READ_ONCE(vt->arch.flagset); \
  527. tmp &= ~(m); \
  528. tmp |= val; \
  529. WRITE_ONCE(vt->arch.flagset, tmp); \
  530. __vcpu_flags_preempt_enable(); \
  531. } while (0)
  532. #define vcpu_get_flag(v, ...) __vcpu_get_flag((v), __VA_ARGS__)
  533. #define vcpu_set_flag(v, ...) __vcpu_set_flag((v), __VA_ARGS__)
  534. #define vcpu_clear_flag(v, ...) __vcpu_clear_flag((v), __VA_ARGS__)
  535. #define vcpu_copy_flag(vt, vs,...) __vcpu_copy_flag((vt), (vs), __VA_ARGS__)
  536. /* SVE exposed to guest */
  537. #define GUEST_HAS_SVE __vcpu_single_flag(cflags, BIT(0))
  538. /* SVE config completed */
  539. #define VCPU_SVE_FINALIZED __vcpu_single_flag(cflags, BIT(1))
  540. /* PTRAUTH exposed to guest */
  541. #define GUEST_HAS_PTRAUTH __vcpu_single_flag(cflags, BIT(2))
  542. /* Exception pending */
  543. #define PENDING_EXCEPTION __vcpu_single_flag(iflags, BIT(0))
  544. /*
  545. * PC increment. Overlaps with EXCEPT_MASK on purpose so that it can't
  546. * be set together with an exception...
  547. */
  548. #define INCREMENT_PC __vcpu_single_flag(iflags, BIT(1))
  549. /* Target EL/MODE (not a single flag, but let's abuse the macro) */
  550. #define EXCEPT_MASK __vcpu_single_flag(iflags, GENMASK(3, 1))
  551. /* Cover both PENDING_EXCEPTION and EXCEPT_MASK for global operations */
  552. #define PC_UPDATE_REQ __vcpu_single_flag(iflags, GENMASK(3, 0))
  553. /* Helpers to encode exceptions with minimum fuss */
  554. #define __EXCEPT_MASK_VAL unpack_vcpu_flag(EXCEPT_MASK)
  555. #define __EXCEPT_SHIFT __builtin_ctzl(__EXCEPT_MASK_VAL)
  556. #define __vcpu_except_flags(_f) iflags, (_f << __EXCEPT_SHIFT), __EXCEPT_MASK_VAL
  557. /*
  558. * When PENDING_EXCEPTION is set, EXCEPT_MASK can take the following
  559. * values:
  560. *
  561. * For AArch32 EL1:
  562. */
  563. #define EXCEPT_AA32_UND __vcpu_except_flags(0)
  564. #define EXCEPT_AA32_IABT __vcpu_except_flags(1)
  565. #define EXCEPT_AA32_DABT __vcpu_except_flags(2)
  566. /* For AArch64: */
  567. #define EXCEPT_AA64_EL1_SYNC __vcpu_except_flags(0)
  568. #define EXCEPT_AA64_EL1_IRQ __vcpu_except_flags(1)
  569. #define EXCEPT_AA64_EL1_FIQ __vcpu_except_flags(2)
  570. #define EXCEPT_AA64_EL1_SERR __vcpu_except_flags(3)
  571. /* For AArch64 with NV (one day): */
  572. #define EXCEPT_AA64_EL2_SYNC __vcpu_except_flags(4)
  573. #define EXCEPT_AA64_EL2_IRQ __vcpu_except_flags(5)
  574. #define EXCEPT_AA64_EL2_FIQ __vcpu_except_flags(6)
  575. #define EXCEPT_AA64_EL2_SERR __vcpu_except_flags(7)
  576. /* Guest debug is live */
  577. #define DEBUG_DIRTY __vcpu_single_flag(iflags, BIT(4))
  578. /* Save SPE context if active */
  579. #define DEBUG_STATE_SAVE_SPE __vcpu_single_flag(iflags, BIT(5))
  580. /* Save TRBE context if active */
  581. #define DEBUG_STATE_SAVE_TRBE __vcpu_single_flag(iflags, BIT(6))
  582. /* pKVM host vcpu state is dirty, needs resync */
  583. #define PKVM_HOST_STATE_DIRTY __vcpu_single_flag(iflags, BIT(7))
  584. /* SVE enabled for host EL0 */
  585. #define HOST_SVE_ENABLED __vcpu_single_flag(sflags, BIT(0))
  586. /* SME enabled for EL0 */
  587. #define HOST_SME_ENABLED __vcpu_single_flag(sflags, BIT(1))
  588. /* Physical CPU not in supported_cpus */
  589. #define ON_UNSUPPORTED_CPU __vcpu_single_flag(sflags, BIT(2))
  590. /* WFIT instruction trapped */
  591. #define IN_WFIT __vcpu_single_flag(sflags, BIT(3))
  592. /* vcpu system registers loaded on physical CPU */
  593. #define SYSREGS_ON_CPU __vcpu_single_flag(sflags, BIT(4))
  594. /* Software step state is Active-pending */
  595. #define DBG_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(5))
  596. /* WFI instruction trapped */
  597. #define IN_WFI __vcpu_single_flag(sflags, BIT(7))
  598. /* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */
  599. #define vcpu_sve_pffr(vcpu) (kern_hyp_va((vcpu)->arch.sve_state) + \
  600. sve_ffr_offset((vcpu)->arch.sve_max_vl))
  601. #define vcpu_sve_max_vq(vcpu) sve_vq_from_vl((vcpu)->arch.sve_max_vl)
  602. #define vcpu_sve_state_size(vcpu) ({ \
  603. size_t __size_ret; \
  604. unsigned int __vcpu_vq; \
  605. \
  606. if (WARN_ON(!sve_vl_valid((vcpu)->arch.sve_max_vl))) { \
  607. __size_ret = 0; \
  608. } else { \
  609. __vcpu_vq = vcpu_sve_max_vq(vcpu); \
  610. __size_ret = SVE_SIG_REGS_SIZE(__vcpu_vq); \
  611. } \
  612. \
  613. __size_ret; \
  614. })
  615. #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
  616. KVM_GUESTDBG_USE_SW_BP | \
  617. KVM_GUESTDBG_USE_HW | \
  618. KVM_GUESTDBG_SINGLESTEP)
  619. #define vcpu_has_sve(vcpu) (system_supports_sve() && \
  620. vcpu_get_flag(vcpu, GUEST_HAS_SVE))
  621. #ifdef CONFIG_ARM64_PTR_AUTH
  622. #define vcpu_has_ptrauth(vcpu) \
  623. ((cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH) || \
  624. cpus_have_final_cap(ARM64_HAS_GENERIC_AUTH)) && \
  625. vcpu_get_flag(vcpu, GUEST_HAS_PTRAUTH))
  626. #else
  627. #define vcpu_has_ptrauth(vcpu) false
  628. #endif
  629. #define vcpu_on_unsupported_cpu(vcpu) \
  630. vcpu_get_flag(vcpu, ON_UNSUPPORTED_CPU)
  631. #define vcpu_set_on_unsupported_cpu(vcpu) \
  632. vcpu_set_flag(vcpu, ON_UNSUPPORTED_CPU)
  633. #define vcpu_clear_on_unsupported_cpu(vcpu) \
  634. vcpu_clear_flag(vcpu, ON_UNSUPPORTED_CPU)
  635. #define vcpu_gp_regs(v) (&(v)->arch.ctxt.regs)
  636. /*
  637. * Only use __vcpu_sys_reg/ctxt_sys_reg if you know you want the
  638. * memory backed version of a register, and not the one most recently
  639. * accessed by a running VCPU. For example, for userspace access or
  640. * for system registers that are never context switched, but only
  641. * emulated.
  642. */
  643. #define __ctxt_sys_reg(c,r) (&(c)->sys_regs[(r)])
  644. #define ctxt_sys_reg(c,r) (*__ctxt_sys_reg(c,r))
  645. #define __vcpu_sys_reg(v,r) (ctxt_sys_reg(&(v)->arch.ctxt, (r)))
  646. static inline bool __vcpu_read_sys_reg_from_cpu(int reg, u64 *val)
  647. {
  648. /*
  649. * *** VHE ONLY ***
  650. *
  651. * System registers listed in the switch are not saved on every
  652. * exit from the guest but are only saved on vcpu_put.
  653. *
  654. * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but
  655. * should never be listed below, because the guest cannot modify its
  656. * own MPIDR_EL1 and MPIDR_EL1 is accessed for VCPU A from VCPU B's
  657. * thread when emulating cross-VCPU communication.
  658. */
  659. if (!has_vhe())
  660. return false;
  661. switch (reg) {
  662. case CSSELR_EL1: *val = read_sysreg_s(SYS_CSSELR_EL1); break;
  663. case SCTLR_EL1: *val = read_sysreg_s(SYS_SCTLR_EL12); break;
  664. case CPACR_EL1: *val = read_sysreg_s(SYS_CPACR_EL12); break;
  665. case TTBR0_EL1: *val = read_sysreg_s(SYS_TTBR0_EL12); break;
  666. case TTBR1_EL1: *val = read_sysreg_s(SYS_TTBR1_EL12); break;
  667. case TCR_EL1: *val = read_sysreg_s(SYS_TCR_EL12); break;
  668. case ESR_EL1: *val = read_sysreg_s(SYS_ESR_EL12); break;
  669. case AFSR0_EL1: *val = read_sysreg_s(SYS_AFSR0_EL12); break;
  670. case AFSR1_EL1: *val = read_sysreg_s(SYS_AFSR1_EL12); break;
  671. case FAR_EL1: *val = read_sysreg_s(SYS_FAR_EL12); break;
  672. case MAIR_EL1: *val = read_sysreg_s(SYS_MAIR_EL12); break;
  673. case VBAR_EL1: *val = read_sysreg_s(SYS_VBAR_EL12); break;
  674. case CONTEXTIDR_EL1: *val = read_sysreg_s(SYS_CONTEXTIDR_EL12);break;
  675. case TPIDR_EL0: *val = read_sysreg_s(SYS_TPIDR_EL0); break;
  676. case TPIDRRO_EL0: *val = read_sysreg_s(SYS_TPIDRRO_EL0); break;
  677. case TPIDR_EL1: *val = read_sysreg_s(SYS_TPIDR_EL1); break;
  678. case AMAIR_EL1: *val = read_sysreg_s(SYS_AMAIR_EL12); break;
  679. case CNTKCTL_EL1: *val = read_sysreg_s(SYS_CNTKCTL_EL12); break;
  680. case ELR_EL1: *val = read_sysreg_s(SYS_ELR_EL12); break;
  681. case PAR_EL1: *val = read_sysreg_par(); break;
  682. case DACR32_EL2: *val = read_sysreg_s(SYS_DACR32_EL2); break;
  683. case IFSR32_EL2: *val = read_sysreg_s(SYS_IFSR32_EL2); break;
  684. case DBGVCR32_EL2: *val = read_sysreg_s(SYS_DBGVCR32_EL2); break;
  685. default: return false;
  686. }
  687. return true;
  688. }
  689. static inline bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg)
  690. {
  691. /*
  692. * *** VHE ONLY ***
  693. *
  694. * System registers listed in the switch are not restored on every
  695. * entry to the guest but are only restored on vcpu_load.
  696. *
  697. * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but
  698. * should never be listed below, because the MPIDR should only be set
  699. * once, before running the VCPU, and never changed later.
  700. */
  701. if (!has_vhe())
  702. return false;
  703. switch (reg) {
  704. case CSSELR_EL1: write_sysreg_s(val, SYS_CSSELR_EL1); break;
  705. case SCTLR_EL1: write_sysreg_s(val, SYS_SCTLR_EL12); break;
  706. case CPACR_EL1: write_sysreg_s(val, SYS_CPACR_EL12); break;
  707. case TTBR0_EL1: write_sysreg_s(val, SYS_TTBR0_EL12); break;
  708. case TTBR1_EL1: write_sysreg_s(val, SYS_TTBR1_EL12); break;
  709. case TCR_EL1: write_sysreg_s(val, SYS_TCR_EL12); break;
  710. case ESR_EL1: write_sysreg_s(val, SYS_ESR_EL12); break;
  711. case AFSR0_EL1: write_sysreg_s(val, SYS_AFSR0_EL12); break;
  712. case AFSR1_EL1: write_sysreg_s(val, SYS_AFSR1_EL12); break;
  713. case FAR_EL1: write_sysreg_s(val, SYS_FAR_EL12); break;
  714. case MAIR_EL1: write_sysreg_s(val, SYS_MAIR_EL12); break;
  715. case VBAR_EL1: write_sysreg_s(val, SYS_VBAR_EL12); break;
  716. case CONTEXTIDR_EL1: write_sysreg_s(val, SYS_CONTEXTIDR_EL12);break;
  717. case TPIDR_EL0: write_sysreg_s(val, SYS_TPIDR_EL0); break;
  718. case TPIDRRO_EL0: write_sysreg_s(val, SYS_TPIDRRO_EL0); break;
  719. case TPIDR_EL1: write_sysreg_s(val, SYS_TPIDR_EL1); break;
  720. case AMAIR_EL1: write_sysreg_s(val, SYS_AMAIR_EL12); break;
  721. case CNTKCTL_EL1: write_sysreg_s(val, SYS_CNTKCTL_EL12); break;
  722. case ELR_EL1: write_sysreg_s(val, SYS_ELR_EL12); break;
  723. case PAR_EL1: write_sysreg_s(val, SYS_PAR_EL1); break;
  724. case DACR32_EL2: write_sysreg_s(val, SYS_DACR32_EL2); break;
  725. case IFSR32_EL2: write_sysreg_s(val, SYS_IFSR32_EL2); break;
  726. case DBGVCR32_EL2: write_sysreg_s(val, SYS_DBGVCR32_EL2); break;
  727. default: return false;
  728. }
  729. return true;
  730. }
  731. #define vcpu_read_sys_reg(__vcpu, reg) \
  732. ({ \
  733. u64 __val = 0x8badf00d8badf00d; \
  734. \
  735. /* SYSREGS_ON_CPU is only used in VHE */ \
  736. ((!is_nvhe_hyp_code() && \
  737. vcpu_get_flag(__vcpu, SYSREGS_ON_CPU) && \
  738. __vcpu_read_sys_reg_from_cpu(reg, &__val))) ? \
  739. __val \
  740. : \
  741. ctxt_sys_reg(&__vcpu->arch.ctxt, reg); \
  742. })
  743. #define vcpu_write_sys_reg(__vcpu, __val, reg) \
  744. do { \
  745. /* SYSREGS_ON_CPU is only used in VHE */ \
  746. if (is_nvhe_hyp_code() || \
  747. !vcpu_get_flag(__vcpu, SYSREGS_ON_CPU) || \
  748. !__vcpu_write_sys_reg_to_cpu(__val, reg)) \
  749. ctxt_sys_reg(&__vcpu->arch.ctxt, reg) = __val; \
  750. } while (0)
  751. struct kvm_vm_stat {
  752. struct kvm_vm_stat_generic generic;
  753. atomic64_t protected_hyp_mem;
  754. atomic64_t protected_shared_mem;
  755. };
  756. struct kvm_vcpu_stat {
  757. struct kvm_vcpu_stat_generic generic;
  758. u64 hvc_exit_stat;
  759. u64 wfe_exit_stat;
  760. u64 wfi_exit_stat;
  761. u64 mmio_exit_user;
  762. u64 mmio_exit_kernel;
  763. u64 signal_exits;
  764. u64 exits;
  765. };
  766. void kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
  767. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  768. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  769. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  770. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  771. unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu);
  772. int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices);
  773. int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
  774. struct kvm_vcpu_events *events);
  775. int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
  776. struct kvm_vcpu_events *events);
  777. #define KVM_ARCH_WANT_MMU_NOTIFIER
  778. void kvm_arm_halt_guest(struct kvm *kvm);
  779. void kvm_arm_resume_guest(struct kvm *kvm);
  780. #define vcpu_has_run_once(vcpu) !!rcu_access_pointer((vcpu)->pid)
  781. #ifndef __KVM_NVHE_HYPERVISOR__
  782. #define kvm_call_hyp_nvhe(f, ...) \
  783. ({ \
  784. struct arm_smccc_res res; \
  785. \
  786. arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f), \
  787. ##__VA_ARGS__, &res); \
  788. WARN_ON(res.a0 != SMCCC_RET_SUCCESS); \
  789. \
  790. res.a1; \
  791. })
  792. /*
  793. * The couple of isb() below are there to guarantee the same behaviour
  794. * on VHE as on !VHE, where the eret to EL1 acts as a context
  795. * synchronization event.
  796. */
  797. #define kvm_call_hyp(f, ...) \
  798. do { \
  799. if (has_vhe()) { \
  800. f(__VA_ARGS__); \
  801. isb(); \
  802. } else { \
  803. kvm_call_hyp_nvhe(f, ##__VA_ARGS__); \
  804. } \
  805. } while(0)
  806. #define kvm_call_hyp_ret(f, ...) \
  807. ({ \
  808. typeof(f(__VA_ARGS__)) ret; \
  809. \
  810. if (has_vhe()) { \
  811. ret = f(__VA_ARGS__); \
  812. isb(); \
  813. } else { \
  814. ret = kvm_call_hyp_nvhe(f, ##__VA_ARGS__); \
  815. } \
  816. \
  817. ret; \
  818. })
  819. #else /* __KVM_NVHE_HYPERVISOR__ */
  820. #define kvm_call_hyp(f, ...) f(__VA_ARGS__)
  821. #define kvm_call_hyp_ret(f, ...) f(__VA_ARGS__)
  822. #define kvm_call_hyp_nvhe(f, ...) f(__VA_ARGS__)
  823. #endif /* __KVM_NVHE_HYPERVISOR__ */
  824. void force_vm_exit(const cpumask_t *mask);
  825. int handle_exit(struct kvm_vcpu *vcpu, int exception_index);
  826. void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index);
  827. int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu);
  828. int kvm_handle_cp14_32(struct kvm_vcpu *vcpu);
  829. int kvm_handle_cp14_64(struct kvm_vcpu *vcpu);
  830. int kvm_handle_cp15_32(struct kvm_vcpu *vcpu);
  831. int kvm_handle_cp15_64(struct kvm_vcpu *vcpu);
  832. int kvm_handle_sys_reg(struct kvm_vcpu *vcpu);
  833. int kvm_handle_cp10_id(struct kvm_vcpu *vcpu);
  834. void kvm_reset_sys_regs(struct kvm_vcpu *vcpu);
  835. int kvm_sys_reg_table_init(void);
  836. /* MMIO helpers */
  837. void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data);
  838. unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);
  839. int kvm_handle_mmio_return(struct kvm_vcpu *vcpu);
  840. int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa);
  841. /*
  842. * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event,
  843. * arrived in guest context. For arm64, any event that arrives while a vCPU is
  844. * loaded is considered to be "in guest".
  845. */
  846. static inline bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu)
  847. {
  848. return IS_ENABLED(CONFIG_GUEST_PERF_EVENTS) && !!vcpu;
  849. }
  850. long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu);
  851. gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu);
  852. void kvm_update_stolen_time(struct kvm_vcpu *vcpu);
  853. bool kvm_arm_pvtime_supported(void);
  854. int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu,
  855. struct kvm_device_attr *attr);
  856. int kvm_arm_pvtime_get_attr(struct kvm_vcpu *vcpu,
  857. struct kvm_device_attr *attr);
  858. int kvm_arm_pvtime_has_attr(struct kvm_vcpu *vcpu,
  859. struct kvm_device_attr *attr);
  860. extern unsigned int kvm_arm_vmid_bits;
  861. int kvm_arm_vmid_alloc_init(void);
  862. void kvm_arm_vmid_alloc_free(void);
  863. void kvm_arm_vmid_update(struct kvm_vmid *kvm_vmid);
  864. void kvm_arm_vmid_clear_active(void);
  865. static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch)
  866. {
  867. vcpu_arch->steal.base = GPA_INVALID;
  868. }
  869. static inline bool kvm_arm_is_pvtime_enabled(struct kvm_vcpu_arch *vcpu_arch)
  870. {
  871. return (vcpu_arch->steal.base != GPA_INVALID);
  872. }
  873. void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 syndrome);
  874. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
  875. DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
  876. static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
  877. {
  878. /* The host's MPIDR is immutable, so let's set it up at boot time */
  879. ctxt_sys_reg(cpu_ctxt, MPIDR_EL1) = read_cpuid_mpidr();
  880. }
  881. static inline bool kvm_system_needs_idmapped_vectors(void)
  882. {
  883. return cpus_have_const_cap(ARM64_SPECTRE_V3A);
  884. }
  885. void kvm_arm_vcpu_ptrauth_trap(struct kvm_vcpu *vcpu);
  886. static inline void kvm_arch_hardware_unsetup(void) {}
  887. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  888. static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
  889. void kvm_arm_init_debug(void);
  890. void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu);
  891. void kvm_arm_setup_debug(struct kvm_vcpu *vcpu);
  892. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu);
  893. void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu);
  894. #define __vcpu_save_guest_debug_regs(vcpu) \
  895. do { \
  896. u64 val = vcpu_read_sys_reg(vcpu, MDSCR_EL1); \
  897. \
  898. (vcpu)->arch.guest_debug_preserved.mdscr_el1 = val; \
  899. } while(0)
  900. #define __vcpu_restore_guest_debug_regs(vcpu) \
  901. do { \
  902. u64 val = (vcpu)->arch.guest_debug_preserved.mdscr_el1; \
  903. \
  904. vcpu_write_sys_reg(vcpu, val, MDSCR_EL1); \
  905. } while (0)
  906. #define kvm_vcpu_os_lock_enabled(vcpu) \
  907. (!!(__vcpu_sys_reg(vcpu, OSLSR_EL1) & SYS_OSLSR_OSLK))
  908. #define kvm_vcpu_needs_debug_regs(vcpu) \
  909. ((vcpu)->guest_debug || kvm_vcpu_os_lock_enabled(vcpu))
  910. int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
  911. struct kvm_device_attr *attr);
  912. int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
  913. struct kvm_device_attr *attr);
  914. int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
  915. struct kvm_device_attr *attr);
  916. long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
  917. struct kvm_arm_copy_mte_tags *copy_tags);
  918. /* Guest/host FPSIMD coordination helpers */
  919. int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
  920. void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
  921. void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu);
  922. void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu);
  923. void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu);
  924. static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
  925. {
  926. return (!has_vhe() && attr->exclude_host);
  927. }
  928. /* Flags for host debug state */
  929. void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu);
  930. void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
  931. #ifdef CONFIG_KVM
  932. void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
  933. void kvm_clr_pmu_events(u32 clr);
  934. #else
  935. static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
  936. static inline void kvm_clr_pmu_events(u32 clr) {}
  937. #endif
  938. void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu);
  939. void kvm_vcpu_put_sysregs_vhe(struct kvm_vcpu *vcpu);
  940. int kvm_set_ipa_limit(void);
  941. #define __KVM_HAVE_ARCH_VM_ALLOC
  942. struct kvm *kvm_arch_alloc_vm(void);
  943. #define kvm_vm_is_protected(kvm) ((kvm)->arch.pkvm.enabled)
  944. void kvm_init_protected_traps(struct kvm_vcpu *vcpu);
  945. int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
  946. bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
  947. #define kvm_arm_vcpu_sve_finalized(vcpu) vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED)
  948. #define kvm_has_mte(kvm) \
  949. (system_supports_mte() && \
  950. test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags))
  951. #define kvm_supports_32bit_el0() \
  952. (system_supports_32bit_el0() && \
  953. !static_branch_unlikely(&arm64_mismatched_32bit_el0))
  954. int kvm_trng_call(struct kvm_vcpu *vcpu);
  955. #ifdef CONFIG_KVM
  956. extern phys_addr_t hyp_mem_base;
  957. extern phys_addr_t hyp_mem_size;
  958. void __init kvm_hyp_reserve(void);
  959. #else
  960. static inline void kvm_hyp_reserve(void) { }
  961. #endif
  962. void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
  963. bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
  964. #endif /* __ARM64_KVM_HOST_H__ */