kvm_host.h 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  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 <c.dall@virtualopensystems.com>
  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/maple_tree.h>
  18. #include <linux/percpu.h>
  19. #include <linux/psci.h>
  20. #include <asm/arch_gicv3.h>
  21. #include <asm/barrier.h>
  22. #include <asm/cpufeature.h>
  23. #include <asm/cputype.h>
  24. #include <asm/daifflags.h>
  25. #include <asm/fpsimd.h>
  26. #include <asm/kvm.h>
  27. #include <asm/kvm_asm.h>
  28. #include <asm/vncr_mapping.h>
  29. #define __KVM_HAVE_ARCH_INTC_INITIALIZED
  30. #define KVM_HALT_POLL_NS_DEFAULT 500000
  31. #include <kvm/arm_vgic.h>
  32. #include <kvm/arm_arch_timer.h>
  33. #include <kvm/arm_pmu.h>
  34. #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
  35. #define KVM_VCPU_MAX_FEATURES 7
  36. #define KVM_VCPU_VALID_FEATURES (BIT(KVM_VCPU_MAX_FEATURES) - 1)
  37. #define KVM_REQ_SLEEP \
  38. KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  39. #define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)
  40. #define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2)
  41. #define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)
  42. #define KVM_REQ_RELOAD_GICv4 KVM_ARCH_REQ(4)
  43. #define KVM_REQ_RELOAD_PMU KVM_ARCH_REQ(5)
  44. #define KVM_REQ_SUSPEND KVM_ARCH_REQ(6)
  45. #define KVM_REQ_RESYNC_PMU_EL0 KVM_ARCH_REQ(7)
  46. #define KVM_REQ_NESTED_S2_UNMAP KVM_ARCH_REQ(8)
  47. #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  48. KVM_DIRTY_LOG_INITIALLY_SET)
  49. #define KVM_HAVE_MMU_RWLOCK
  50. /*
  51. * Mode of operation configurable with kvm-arm.mode early param.
  52. * See Documentation/admin-guide/kernel-parameters.txt for more information.
  53. */
  54. enum kvm_mode {
  55. KVM_MODE_DEFAULT,
  56. KVM_MODE_PROTECTED,
  57. KVM_MODE_NV,
  58. KVM_MODE_NONE,
  59. };
  60. #ifdef CONFIG_KVM
  61. enum kvm_mode kvm_get_mode(void);
  62. #else
  63. static inline enum kvm_mode kvm_get_mode(void) { return KVM_MODE_NONE; };
  64. #endif
  65. extern unsigned int __ro_after_init kvm_sve_max_vl;
  66. extern unsigned int __ro_after_init kvm_host_sve_max_vl;
  67. int __init kvm_arm_init_sve(void);
  68. u32 __attribute_const__ kvm_target_cpu(void);
  69. void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  70. void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
  71. struct kvm_hyp_memcache {
  72. phys_addr_t head;
  73. unsigned long nr_pages;
  74. };
  75. static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,
  76. phys_addr_t *p,
  77. phys_addr_t (*to_pa)(void *virt))
  78. {
  79. *p = mc->head;
  80. mc->head = to_pa(p);
  81. mc->nr_pages++;
  82. }
  83. static inline void *pop_hyp_memcache(struct kvm_hyp_memcache *mc,
  84. void *(*to_va)(phys_addr_t phys))
  85. {
  86. phys_addr_t *p = to_va(mc->head);
  87. if (!mc->nr_pages)
  88. return NULL;
  89. mc->head = *p;
  90. mc->nr_pages--;
  91. return p;
  92. }
  93. static inline int __topup_hyp_memcache(struct kvm_hyp_memcache *mc,
  94. unsigned long min_pages,
  95. void *(*alloc_fn)(void *arg),
  96. phys_addr_t (*to_pa)(void *virt),
  97. void *arg)
  98. {
  99. while (mc->nr_pages < min_pages) {
  100. phys_addr_t *p = alloc_fn(arg);
  101. if (!p)
  102. return -ENOMEM;
  103. push_hyp_memcache(mc, p, to_pa);
  104. }
  105. return 0;
  106. }
  107. static inline void __free_hyp_memcache(struct kvm_hyp_memcache *mc,
  108. void (*free_fn)(void *virt, void *arg),
  109. void *(*to_va)(phys_addr_t phys),
  110. void *arg)
  111. {
  112. while (mc->nr_pages)
  113. free_fn(pop_hyp_memcache(mc, to_va), arg);
  114. }
  115. void free_hyp_memcache(struct kvm_hyp_memcache *mc);
  116. int topup_hyp_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages);
  117. struct kvm_vmid {
  118. atomic64_t id;
  119. };
  120. struct kvm_s2_mmu {
  121. struct kvm_vmid vmid;
  122. /*
  123. * stage2 entry level table
  124. *
  125. * Two kvm_s2_mmu structures in the same VM can point to the same
  126. * pgd here. This happens when running a guest using a
  127. * translation regime that isn't affected by its own stage-2
  128. * translation, such as a non-VHE hypervisor running at vEL2, or
  129. * for vEL1/EL0 with vHCR_EL2.VM == 0. In that case, we use the
  130. * canonical stage-2 page tables.
  131. */
  132. phys_addr_t pgd_phys;
  133. struct kvm_pgtable *pgt;
  134. /*
  135. * VTCR value used on the host. For a non-NV guest (or a NV
  136. * guest that runs in a context where its own S2 doesn't
  137. * apply), its T0SZ value reflects that of the IPA size.
  138. *
  139. * For a shadow S2 MMU, T0SZ reflects the PARange exposed to
  140. * the guest.
  141. */
  142. u64 vtcr;
  143. /* The last vcpu id that ran on each physical CPU */
  144. int __percpu *last_vcpu_ran;
  145. #define KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT 0
  146. /*
  147. * Memory cache used to split
  148. * KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE worth of huge pages. It
  149. * is used to allocate stage2 page tables while splitting huge
  150. * pages. The choice of KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE
  151. * influences both the capacity of the split page cache, and
  152. * how often KVM reschedules. Be wary of raising CHUNK_SIZE
  153. * too high.
  154. *
  155. * Protected by kvm->slots_lock.
  156. */
  157. struct kvm_mmu_memory_cache split_page_cache;
  158. uint64_t split_page_chunk_size;
  159. struct kvm_arch *arch;
  160. /*
  161. * For a shadow stage-2 MMU, the virtual vttbr used by the
  162. * host to parse the guest S2.
  163. * This either contains:
  164. * - the virtual VTTBR programmed by the guest hypervisor with
  165. * CnP cleared
  166. * - The value 1 (VMID=0, BADDR=0, CnP=1) if invalid
  167. *
  168. * We also cache the full VTCR which gets used for TLB invalidation,
  169. * taking the ARM ARM's "Any of the bits in VTCR_EL2 are permitted
  170. * to be cached in a TLB" to the letter.
  171. */
  172. u64 tlb_vttbr;
  173. u64 tlb_vtcr;
  174. /*
  175. * true when this represents a nested context where virtual
  176. * HCR_EL2.VM == 1
  177. */
  178. bool nested_stage2_enabled;
  179. /*
  180. * true when this MMU needs to be unmapped before being used for a new
  181. * purpose.
  182. */
  183. bool pending_unmap;
  184. /*
  185. * 0: Nobody is currently using this, check vttbr for validity
  186. * >0: Somebody is actively using this.
  187. */
  188. atomic_t refcnt;
  189. };
  190. struct kvm_arch_memory_slot {
  191. };
  192. /**
  193. * struct kvm_smccc_features: Descriptor of the hypercall services exposed to the guests
  194. *
  195. * @std_bmap: Bitmap of standard secure service calls
  196. * @std_hyp_bmap: Bitmap of standard hypervisor service calls
  197. * @vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls
  198. */
  199. struct kvm_smccc_features {
  200. unsigned long std_bmap;
  201. unsigned long std_hyp_bmap;
  202. unsigned long vendor_hyp_bmap;
  203. };
  204. typedef unsigned int pkvm_handle_t;
  205. struct kvm_protected_vm {
  206. pkvm_handle_t handle;
  207. struct kvm_hyp_memcache teardown_mc;
  208. bool enabled;
  209. };
  210. struct kvm_mpidr_data {
  211. u64 mpidr_mask;
  212. DECLARE_FLEX_ARRAY(u16, cmpidr_to_idx);
  213. };
  214. static inline u16 kvm_mpidr_index(struct kvm_mpidr_data *data, u64 mpidr)
  215. {
  216. unsigned long index = 0, mask = data->mpidr_mask;
  217. unsigned long aff = mpidr & MPIDR_HWID_BITMASK;
  218. bitmap_gather(&index, &aff, &mask, fls(mask));
  219. return index;
  220. }
  221. struct kvm_sysreg_masks;
  222. enum fgt_group_id {
  223. __NO_FGT_GROUP__,
  224. HFGxTR_GROUP,
  225. HDFGRTR_GROUP,
  226. HDFGWTR_GROUP = HDFGRTR_GROUP,
  227. HFGITR_GROUP,
  228. HAFGRTR_GROUP,
  229. /* Must be last */
  230. __NR_FGT_GROUP_IDS__
  231. };
  232. struct kvm_arch {
  233. struct kvm_s2_mmu mmu;
  234. /*
  235. * Fine-Grained UNDEF, mimicking the FGT layout defined by the
  236. * architecture. We track them globally, as we present the
  237. * same feature-set to all vcpus.
  238. *
  239. * Index 0 is currently spare.
  240. */
  241. u64 fgu[__NR_FGT_GROUP_IDS__];
  242. /*
  243. * Stage 2 paging state for VMs with nested S2 using a virtual
  244. * VMID.
  245. */
  246. struct kvm_s2_mmu *nested_mmus;
  247. size_t nested_mmus_size;
  248. int nested_mmus_next;
  249. /* Interrupt controller */
  250. struct vgic_dist vgic;
  251. /* Timers */
  252. struct arch_timer_vm_data timer_data;
  253. /* Mandated version of PSCI */
  254. u32 psci_version;
  255. /* Protects VM-scoped configuration data */
  256. struct mutex config_lock;
  257. /*
  258. * If we encounter a data abort without valid instruction syndrome
  259. * information, report this to user space. User space can (and
  260. * should) opt in to this feature if KVM_CAP_ARM_NISV_TO_USER is
  261. * supported.
  262. */
  263. #define KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER 0
  264. /* Memory Tagging Extension enabled for the guest */
  265. #define KVM_ARCH_FLAG_MTE_ENABLED 1
  266. /* At least one vCPU has ran in the VM */
  267. #define KVM_ARCH_FLAG_HAS_RAN_ONCE 2
  268. /* The vCPU feature set for the VM is configured */
  269. #define KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED 3
  270. /* PSCI SYSTEM_SUSPEND enabled for the guest */
  271. #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED 4
  272. /* VM counter offset */
  273. #define KVM_ARCH_FLAG_VM_COUNTER_OFFSET 5
  274. /* Timer PPIs made immutable */
  275. #define KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE 6
  276. /* Initial ID reg values loaded */
  277. #define KVM_ARCH_FLAG_ID_REGS_INITIALIZED 7
  278. /* Fine-Grained UNDEF initialised */
  279. #define KVM_ARCH_FLAG_FGU_INITIALIZED 8
  280. unsigned long flags;
  281. /* VM-wide vCPU feature set */
  282. DECLARE_BITMAP(vcpu_features, KVM_VCPU_MAX_FEATURES);
  283. /* MPIDR to vcpu index mapping, optional */
  284. struct kvm_mpidr_data *mpidr_data;
  285. /*
  286. * VM-wide PMU filter, implemented as a bitmap and big enough for
  287. * up to 2^10 events (ARMv8.0) or 2^16 events (ARMv8.1+).
  288. */
  289. unsigned long *pmu_filter;
  290. struct arm_pmu *arm_pmu;
  291. cpumask_var_t supported_cpus;
  292. /* PMCR_EL0.N value for the guest */
  293. u8 pmcr_n;
  294. /* Iterator for idreg debugfs */
  295. u8 idreg_debugfs_iter;
  296. /* Hypercall features firmware registers' descriptor */
  297. struct kvm_smccc_features smccc_feat;
  298. struct maple_tree smccc_filter;
  299. /*
  300. * Emulated CPU ID registers per VM
  301. * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
  302. * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
  303. *
  304. * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
  305. * Atomic access to multiple idregs are guarded by kvm_arch.config_lock.
  306. */
  307. #define IDREG_IDX(id) (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
  308. #define KVM_ARM_ID_REG_NUM (IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1)
  309. u64 id_regs[KVM_ARM_ID_REG_NUM];
  310. u64 ctr_el0;
  311. /* Masks for VNCR-baked sysregs */
  312. struct kvm_sysreg_masks *sysreg_masks;
  313. /*
  314. * For an untrusted host VM, 'pkvm.handle' is used to lookup
  315. * the associated pKVM instance in the hypervisor.
  316. */
  317. struct kvm_protected_vm pkvm;
  318. };
  319. struct kvm_vcpu_fault_info {
  320. u64 esr_el2; /* Hyp Syndrom Register */
  321. u64 far_el2; /* Hyp Fault Address Register */
  322. u64 hpfar_el2; /* Hyp IPA Fault Address Register */
  323. u64 disr_el1; /* Deferred [SError] Status Register */
  324. };
  325. /*
  326. * VNCR() just places the VNCR_capable registers in the enum after
  327. * __VNCR_START__, and the value (after correction) to be an 8-byte offset
  328. * from the VNCR base. As we don't require the enum to be otherwise ordered,
  329. * we need the terrible hack below to ensure that we correctly size the
  330. * sys_regs array, no matter what.
  331. *
  332. * The __MAX__ macro has been lifted from Sean Eron Anderson's wonderful
  333. * treasure trove of bit hacks:
  334. * https://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
  335. */
  336. #define __MAX__(x,y) ((x) ^ (((x) ^ (y)) & -((x) < (y))))
  337. #define VNCR(r) \
  338. __before_##r, \
  339. r = __VNCR_START__ + ((VNCR_ ## r) / 8), \
  340. __after_##r = __MAX__(__before_##r - 1, r)
  341. enum vcpu_sysreg {
  342. __INVALID_SYSREG__, /* 0 is reserved as an invalid value */
  343. MPIDR_EL1, /* MultiProcessor Affinity Register */
  344. CLIDR_EL1, /* Cache Level ID Register */
  345. CSSELR_EL1, /* Cache Size Selection Register */
  346. TPIDR_EL0, /* Thread ID, User R/W */
  347. TPIDRRO_EL0, /* Thread ID, User R/O */
  348. TPIDR_EL1, /* Thread ID, Privileged */
  349. CNTKCTL_EL1, /* Timer Control Register (EL1) */
  350. PAR_EL1, /* Physical Address Register */
  351. MDCCINT_EL1, /* Monitor Debug Comms Channel Interrupt Enable Reg */
  352. OSLSR_EL1, /* OS Lock Status Register */
  353. DISR_EL1, /* Deferred Interrupt Status Register */
  354. /* Performance Monitors Registers */
  355. PMCR_EL0, /* Control Register */
  356. PMSELR_EL0, /* Event Counter Selection Register */
  357. PMEVCNTR0_EL0, /* Event Counter Register (0-30) */
  358. PMEVCNTR30_EL0 = PMEVCNTR0_EL0 + 30,
  359. PMCCNTR_EL0, /* Cycle Counter Register */
  360. PMEVTYPER0_EL0, /* Event Type Register (0-30) */
  361. PMEVTYPER30_EL0 = PMEVTYPER0_EL0 + 30,
  362. PMCCFILTR_EL0, /* Cycle Count Filter Register */
  363. PMCNTENSET_EL0, /* Count Enable Set Register */
  364. PMINTENSET_EL1, /* Interrupt Enable Set Register */
  365. PMOVSSET_EL0, /* Overflow Flag Status Set Register */
  366. PMUSERENR_EL0, /* User Enable Register */
  367. /* Pointer Authentication Registers in a strict increasing order. */
  368. APIAKEYLO_EL1,
  369. APIAKEYHI_EL1,
  370. APIBKEYLO_EL1,
  371. APIBKEYHI_EL1,
  372. APDAKEYLO_EL1,
  373. APDAKEYHI_EL1,
  374. APDBKEYLO_EL1,
  375. APDBKEYHI_EL1,
  376. APGAKEYLO_EL1,
  377. APGAKEYHI_EL1,
  378. /* Memory Tagging Extension registers */
  379. RGSR_EL1, /* Random Allocation Tag Seed Register */
  380. GCR_EL1, /* Tag Control Register */
  381. TFSRE0_EL1, /* Tag Fault Status Register (EL0) */
  382. POR_EL0, /* Permission Overlay Register 0 (EL0) */
  383. /* FP/SIMD/SVE */
  384. SVCR,
  385. FPMR,
  386. /* 32bit specific registers. */
  387. DACR32_EL2, /* Domain Access Control Register */
  388. IFSR32_EL2, /* Instruction Fault Status Register */
  389. FPEXC32_EL2, /* Floating-Point Exception Control Register */
  390. DBGVCR32_EL2, /* Debug Vector Catch Register */
  391. /* EL2 registers */
  392. SCTLR_EL2, /* System Control Register (EL2) */
  393. ACTLR_EL2, /* Auxiliary Control Register (EL2) */
  394. MDCR_EL2, /* Monitor Debug Configuration Register (EL2) */
  395. CPTR_EL2, /* Architectural Feature Trap Register (EL2) */
  396. HACR_EL2, /* Hypervisor Auxiliary Control Register */
  397. ZCR_EL2, /* SVE Control Register (EL2) */
  398. TTBR0_EL2, /* Translation Table Base Register 0 (EL2) */
  399. TTBR1_EL2, /* Translation Table Base Register 1 (EL2) */
  400. TCR_EL2, /* Translation Control Register (EL2) */
  401. SPSR_EL2, /* EL2 saved program status register */
  402. ELR_EL2, /* EL2 exception link register */
  403. AFSR0_EL2, /* Auxiliary Fault Status Register 0 (EL2) */
  404. AFSR1_EL2, /* Auxiliary Fault Status Register 1 (EL2) */
  405. ESR_EL2, /* Exception Syndrome Register (EL2) */
  406. FAR_EL2, /* Fault Address Register (EL2) */
  407. HPFAR_EL2, /* Hypervisor IPA Fault Address Register */
  408. MAIR_EL2, /* Memory Attribute Indirection Register (EL2) */
  409. AMAIR_EL2, /* Auxiliary Memory Attribute Indirection Register (EL2) */
  410. VBAR_EL2, /* Vector Base Address Register (EL2) */
  411. RVBAR_EL2, /* Reset Vector Base Address Register */
  412. CONTEXTIDR_EL2, /* Context ID Register (EL2) */
  413. CNTHCTL_EL2, /* Counter-timer Hypervisor Control register */
  414. SP_EL2, /* EL2 Stack Pointer */
  415. CNTHP_CTL_EL2,
  416. CNTHP_CVAL_EL2,
  417. CNTHV_CTL_EL2,
  418. CNTHV_CVAL_EL2,
  419. __VNCR_START__, /* Any VNCR-capable reg goes after this point */
  420. VNCR(SCTLR_EL1),/* System Control Register */
  421. VNCR(ACTLR_EL1),/* Auxiliary Control Register */
  422. VNCR(CPACR_EL1),/* Coprocessor Access Control */
  423. VNCR(ZCR_EL1), /* SVE Control */
  424. VNCR(TTBR0_EL1),/* Translation Table Base Register 0 */
  425. VNCR(TTBR1_EL1),/* Translation Table Base Register 1 */
  426. VNCR(TCR_EL1), /* Translation Control Register */
  427. VNCR(TCR2_EL1), /* Extended Translation Control Register */
  428. VNCR(ESR_EL1), /* Exception Syndrome Register */
  429. VNCR(AFSR0_EL1),/* Auxiliary Fault Status Register 0 */
  430. VNCR(AFSR1_EL1),/* Auxiliary Fault Status Register 1 */
  431. VNCR(FAR_EL1), /* Fault Address Register */
  432. VNCR(MAIR_EL1), /* Memory Attribute Indirection Register */
  433. VNCR(VBAR_EL1), /* Vector Base Address Register */
  434. VNCR(CONTEXTIDR_EL1), /* Context ID Register */
  435. VNCR(AMAIR_EL1),/* Aux Memory Attribute Indirection Register */
  436. VNCR(MDSCR_EL1),/* Monitor Debug System Control Register */
  437. VNCR(ELR_EL1),
  438. VNCR(SP_EL1),
  439. VNCR(SPSR_EL1),
  440. VNCR(TFSR_EL1), /* Tag Fault Status Register (EL1) */
  441. VNCR(VPIDR_EL2),/* Virtualization Processor ID Register */
  442. VNCR(VMPIDR_EL2),/* Virtualization Multiprocessor ID Register */
  443. VNCR(HCR_EL2), /* Hypervisor Configuration Register */
  444. VNCR(HSTR_EL2), /* Hypervisor System Trap Register */
  445. VNCR(VTTBR_EL2),/* Virtualization Translation Table Base Register */
  446. VNCR(VTCR_EL2), /* Virtualization Translation Control Register */
  447. VNCR(TPIDR_EL2),/* EL2 Software Thread ID Register */
  448. VNCR(HCRX_EL2), /* Extended Hypervisor Configuration Register */
  449. /* Permission Indirection Extension registers */
  450. VNCR(PIR_EL1), /* Permission Indirection Register 1 (EL1) */
  451. VNCR(PIRE0_EL1), /* Permission Indirection Register 0 (EL1) */
  452. VNCR(POR_EL1), /* Permission Overlay Register 1 (EL1) */
  453. VNCR(HFGRTR_EL2),
  454. VNCR(HFGWTR_EL2),
  455. VNCR(HFGITR_EL2),
  456. VNCR(HDFGRTR_EL2),
  457. VNCR(HDFGWTR_EL2),
  458. VNCR(HAFGRTR_EL2),
  459. VNCR(CNTVOFF_EL2),
  460. VNCR(CNTV_CVAL_EL0),
  461. VNCR(CNTV_CTL_EL0),
  462. VNCR(CNTP_CVAL_EL0),
  463. VNCR(CNTP_CTL_EL0),
  464. VNCR(ICH_HCR_EL2),
  465. NR_SYS_REGS /* Nothing after this line! */
  466. };
  467. struct kvm_sysreg_masks {
  468. struct {
  469. u64 res0;
  470. u64 res1;
  471. } mask[NR_SYS_REGS - __VNCR_START__];
  472. };
  473. struct kvm_cpu_context {
  474. struct user_pt_regs regs; /* sp = sp_el0 */
  475. u64 spsr_abt;
  476. u64 spsr_und;
  477. u64 spsr_irq;
  478. u64 spsr_fiq;
  479. struct user_fpsimd_state fp_regs;
  480. u64 sys_regs[NR_SYS_REGS];
  481. struct kvm_vcpu *__hyp_running_vcpu;
  482. /* This pointer has to be 4kB aligned. */
  483. u64 *vncr_array;
  484. };
  485. struct cpu_sve_state {
  486. __u64 zcr_el1;
  487. /*
  488. * Ordering is important since __sve_save_state/__sve_restore_state
  489. * relies on it.
  490. */
  491. __u32 fpsr;
  492. __u32 fpcr;
  493. /* Must be SVE_VQ_BYTES (128 bit) aligned. */
  494. __u8 sve_regs[];
  495. };
  496. /*
  497. * This structure is instantiated on a per-CPU basis, and contains
  498. * data that is:
  499. *
  500. * - tied to a single physical CPU, and
  501. * - either have a lifetime that does not extend past vcpu_put()
  502. * - or is an invariant for the lifetime of the system
  503. *
  504. * Use host_data_ptr(field) as a way to access a pointer to such a
  505. * field.
  506. */
  507. struct kvm_host_data {
  508. struct kvm_cpu_context host_ctxt;
  509. /*
  510. * Hyp VA.
  511. * sve_state is only used in pKVM and if system_supports_sve().
  512. */
  513. struct cpu_sve_state *sve_state;
  514. /* Used by pKVM only. */
  515. u64 fpmr;
  516. /* Ownership of the FP regs */
  517. enum {
  518. FP_STATE_FREE,
  519. FP_STATE_HOST_OWNED,
  520. FP_STATE_GUEST_OWNED,
  521. } fp_owner;
  522. /*
  523. * host_debug_state contains the host registers which are
  524. * saved and restored during world switches.
  525. */
  526. struct {
  527. /* {Break,watch}point registers */
  528. struct kvm_guest_debug_arch regs;
  529. /* Statistical profiling extension */
  530. u64 pmscr_el1;
  531. /* Self-hosted trace */
  532. u64 trfcr_el1;
  533. /* Values of trap registers for the host before guest entry. */
  534. u64 mdcr_el2;
  535. } host_debug_state;
  536. };
  537. struct kvm_host_psci_config {
  538. /* PSCI version used by host. */
  539. u32 version;
  540. u32 smccc_version;
  541. /* Function IDs used by host if version is v0.1. */
  542. struct psci_0_1_function_ids function_ids_0_1;
  543. bool psci_0_1_cpu_suspend_implemented;
  544. bool psci_0_1_cpu_on_implemented;
  545. bool psci_0_1_cpu_off_implemented;
  546. bool psci_0_1_migrate_implemented;
  547. };
  548. extern struct kvm_host_psci_config kvm_nvhe_sym(kvm_host_psci_config);
  549. #define kvm_host_psci_config CHOOSE_NVHE_SYM(kvm_host_psci_config)
  550. extern s64 kvm_nvhe_sym(hyp_physvirt_offset);
  551. #define hyp_physvirt_offset CHOOSE_NVHE_SYM(hyp_physvirt_offset)
  552. extern u64 kvm_nvhe_sym(hyp_cpu_logical_map)[NR_CPUS];
  553. #define hyp_cpu_logical_map CHOOSE_NVHE_SYM(hyp_cpu_logical_map)
  554. struct vcpu_reset_state {
  555. unsigned long pc;
  556. unsigned long r0;
  557. bool be;
  558. bool reset;
  559. };
  560. struct kvm_vcpu_arch {
  561. struct kvm_cpu_context ctxt;
  562. /*
  563. * Guest floating point state
  564. *
  565. * The architecture has two main floating point extensions,
  566. * the original FPSIMD and SVE. These have overlapping
  567. * register views, with the FPSIMD V registers occupying the
  568. * low 128 bits of the SVE Z registers. When the core
  569. * floating point code saves the register state of a task it
  570. * records which view it saved in fp_type.
  571. */
  572. void *sve_state;
  573. enum fp_type fp_type;
  574. unsigned int sve_max_vl;
  575. /* Stage 2 paging state used by the hardware on next switch */
  576. struct kvm_s2_mmu *hw_mmu;
  577. /* Values of trap registers for the guest. */
  578. u64 hcr_el2;
  579. u64 hcrx_el2;
  580. u64 mdcr_el2;
  581. /* Exception Information */
  582. struct kvm_vcpu_fault_info fault;
  583. /* Configuration flags, set once and for all before the vcpu can run */
  584. u8 cflags;
  585. /* Input flags to the hypervisor code, potentially cleared after use */
  586. u8 iflags;
  587. /* State flags for kernel bookkeeping, unused by the hypervisor code */
  588. u8 sflags;
  589. /*
  590. * Don't run the guest (internal implementation need).
  591. *
  592. * Contrary to the flags above, this is set/cleared outside of
  593. * a vcpu context, and thus cannot be mixed with the flags
  594. * themselves (or the flag accesses need to be made atomic).
  595. */
  596. bool pause;
  597. /*
  598. * We maintain more than a single set of debug registers to support
  599. * debugging the guest from the host and to maintain separate host and
  600. * guest state during world switches. vcpu_debug_state are the debug
  601. * registers of the vcpu as the guest sees them.
  602. *
  603. * external_debug_state contains the debug values we want to debug the
  604. * guest. This is set via the KVM_SET_GUEST_DEBUG ioctl.
  605. *
  606. * debug_ptr points to the set of debug registers that should be loaded
  607. * onto the hardware when running the guest.
  608. */
  609. struct kvm_guest_debug_arch *debug_ptr;
  610. struct kvm_guest_debug_arch vcpu_debug_state;
  611. struct kvm_guest_debug_arch external_debug_state;
  612. /* VGIC state */
  613. struct vgic_cpu vgic_cpu;
  614. struct arch_timer_cpu timer_cpu;
  615. struct kvm_pmu pmu;
  616. /*
  617. * Guest registers we preserve during guest debugging.
  618. *
  619. * These shadow registers are updated by the kvm_handle_sys_reg
  620. * trap handler if the guest accesses or updates them while we
  621. * are using guest debug.
  622. */
  623. struct {
  624. u32 mdscr_el1;
  625. bool pstate_ss;
  626. } guest_debug_preserved;
  627. /* vcpu power state */
  628. struct kvm_mp_state mp_state;
  629. spinlock_t mp_state_lock;
  630. /* Cache some mmu pages needed inside spinlock regions */
  631. struct kvm_mmu_memory_cache mmu_page_cache;
  632. /* Virtual SError ESR to restore when HCR_EL2.VSE is set */
  633. u64 vsesr_el2;
  634. /* Additional reset state */
  635. struct vcpu_reset_state reset_state;
  636. /* Guest PV state */
  637. struct {
  638. u64 last_steal;
  639. gpa_t base;
  640. } steal;
  641. /* Per-vcpu CCSIDR override or NULL */
  642. u32 *ccsidr;
  643. };
  644. /*
  645. * Each 'flag' is composed of a comma-separated triplet:
  646. *
  647. * - the flag-set it belongs to in the vcpu->arch structure
  648. * - the value for that flag
  649. * - the mask for that flag
  650. *
  651. * __vcpu_single_flag() builds such a triplet for a single-bit flag.
  652. * unpack_vcpu_flag() extract the flag value from the triplet for
  653. * direct use outside of the flag accessors.
  654. */
  655. #define __vcpu_single_flag(_set, _f) _set, (_f), (_f)
  656. #define __unpack_flag(_set, _f, _m) _f
  657. #define unpack_vcpu_flag(...) __unpack_flag(__VA_ARGS__)
  658. #define __build_check_flag(v, flagset, f, m) \
  659. do { \
  660. typeof(v->arch.flagset) *_fset; \
  661. \
  662. /* Check that the flags fit in the mask */ \
  663. BUILD_BUG_ON(HWEIGHT(m) != HWEIGHT((f) | (m))); \
  664. /* Check that the flags fit in the type */ \
  665. BUILD_BUG_ON((sizeof(*_fset) * 8) <= __fls(m)); \
  666. } while (0)
  667. #define __vcpu_get_flag(v, flagset, f, m) \
  668. ({ \
  669. __build_check_flag(v, flagset, f, m); \
  670. \
  671. READ_ONCE(v->arch.flagset) & (m); \
  672. })
  673. /*
  674. * Note that the set/clear accessors must be preempt-safe in order to
  675. * avoid nesting them with load/put which also manipulate flags...
  676. */
  677. #ifdef __KVM_NVHE_HYPERVISOR__
  678. /* the nVHE hypervisor is always non-preemptible */
  679. #define __vcpu_flags_preempt_disable()
  680. #define __vcpu_flags_preempt_enable()
  681. #else
  682. #define __vcpu_flags_preempt_disable() preempt_disable()
  683. #define __vcpu_flags_preempt_enable() preempt_enable()
  684. #endif
  685. #define __vcpu_set_flag(v, flagset, f, m) \
  686. do { \
  687. typeof(v->arch.flagset) *fset; \
  688. \
  689. __build_check_flag(v, flagset, f, m); \
  690. \
  691. fset = &v->arch.flagset; \
  692. __vcpu_flags_preempt_disable(); \
  693. if (HWEIGHT(m) > 1) \
  694. *fset &= ~(m); \
  695. *fset |= (f); \
  696. __vcpu_flags_preempt_enable(); \
  697. } while (0)
  698. #define __vcpu_clear_flag(v, flagset, f, m) \
  699. do { \
  700. typeof(v->arch.flagset) *fset; \
  701. \
  702. __build_check_flag(v, flagset, f, m); \
  703. \
  704. fset = &v->arch.flagset; \
  705. __vcpu_flags_preempt_disable(); \
  706. *fset &= ~(m); \
  707. __vcpu_flags_preempt_enable(); \
  708. } while (0)
  709. #define vcpu_get_flag(v, ...) __vcpu_get_flag((v), __VA_ARGS__)
  710. #define vcpu_set_flag(v, ...) __vcpu_set_flag((v), __VA_ARGS__)
  711. #define vcpu_clear_flag(v, ...) __vcpu_clear_flag((v), __VA_ARGS__)
  712. /* SVE exposed to guest */
  713. #define GUEST_HAS_SVE __vcpu_single_flag(cflags, BIT(0))
  714. /* SVE config completed */
  715. #define VCPU_SVE_FINALIZED __vcpu_single_flag(cflags, BIT(1))
  716. /* PTRAUTH exposed to guest */
  717. #define GUEST_HAS_PTRAUTH __vcpu_single_flag(cflags, BIT(2))
  718. /* KVM_ARM_VCPU_INIT completed */
  719. #define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(3))
  720. /* Exception pending */
  721. #define PENDING_EXCEPTION __vcpu_single_flag(iflags, BIT(0))
  722. /*
  723. * PC increment. Overlaps with EXCEPT_MASK on purpose so that it can't
  724. * be set together with an exception...
  725. */
  726. #define INCREMENT_PC __vcpu_single_flag(iflags, BIT(1))
  727. /* Target EL/MODE (not a single flag, but let's abuse the macro) */
  728. #define EXCEPT_MASK __vcpu_single_flag(iflags, GENMASK(3, 1))
  729. /* Helpers to encode exceptions with minimum fuss */
  730. #define __EXCEPT_MASK_VAL unpack_vcpu_flag(EXCEPT_MASK)
  731. #define __EXCEPT_SHIFT __builtin_ctzl(__EXCEPT_MASK_VAL)
  732. #define __vcpu_except_flags(_f) iflags, (_f << __EXCEPT_SHIFT), __EXCEPT_MASK_VAL
  733. /*
  734. * When PENDING_EXCEPTION is set, EXCEPT_MASK can take the following
  735. * values:
  736. *
  737. * For AArch32 EL1:
  738. */
  739. #define EXCEPT_AA32_UND __vcpu_except_flags(0)
  740. #define EXCEPT_AA32_IABT __vcpu_except_flags(1)
  741. #define EXCEPT_AA32_DABT __vcpu_except_flags(2)
  742. /* For AArch64: */
  743. #define EXCEPT_AA64_EL1_SYNC __vcpu_except_flags(0)
  744. #define EXCEPT_AA64_EL1_IRQ __vcpu_except_flags(1)
  745. #define EXCEPT_AA64_EL1_FIQ __vcpu_except_flags(2)
  746. #define EXCEPT_AA64_EL1_SERR __vcpu_except_flags(3)
  747. /* For AArch64 with NV: */
  748. #define EXCEPT_AA64_EL2_SYNC __vcpu_except_flags(4)
  749. #define EXCEPT_AA64_EL2_IRQ __vcpu_except_flags(5)
  750. #define EXCEPT_AA64_EL2_FIQ __vcpu_except_flags(6)
  751. #define EXCEPT_AA64_EL2_SERR __vcpu_except_flags(7)
  752. /* Guest debug is live */
  753. #define DEBUG_DIRTY __vcpu_single_flag(iflags, BIT(4))
  754. /* Save SPE context if active */
  755. #define DEBUG_STATE_SAVE_SPE __vcpu_single_flag(iflags, BIT(5))
  756. /* Save TRBE context if active */
  757. #define DEBUG_STATE_SAVE_TRBE __vcpu_single_flag(iflags, BIT(6))
  758. /* Physical CPU not in supported_cpus */
  759. #define ON_UNSUPPORTED_CPU __vcpu_single_flag(sflags, BIT(2))
  760. /* WFIT instruction trapped */
  761. #define IN_WFIT __vcpu_single_flag(sflags, BIT(3))
  762. /* vcpu system registers loaded on physical CPU */
  763. #define SYSREGS_ON_CPU __vcpu_single_flag(sflags, BIT(4))
  764. /* Software step state is Active-pending */
  765. #define DBG_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(5))
  766. /* PMUSERENR for the guest EL0 is on physical CPU */
  767. #define PMUSERENR_ON_CPU __vcpu_single_flag(sflags, BIT(6))
  768. /* WFI instruction trapped */
  769. #define IN_WFI __vcpu_single_flag(sflags, BIT(7))
  770. /* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */
  771. #define vcpu_sve_pffr(vcpu) (kern_hyp_va((vcpu)->arch.sve_state) + \
  772. sve_ffr_offset((vcpu)->arch.sve_max_vl))
  773. #define vcpu_sve_max_vq(vcpu) sve_vq_from_vl((vcpu)->arch.sve_max_vl)
  774. #define vcpu_sve_zcr_elx(vcpu) \
  775. (unlikely(is_hyp_ctxt(vcpu)) ? ZCR_EL2 : ZCR_EL1)
  776. #define vcpu_sve_state_size(vcpu) ({ \
  777. size_t __size_ret; \
  778. unsigned int __vcpu_vq; \
  779. \
  780. if (WARN_ON(!sve_vl_valid((vcpu)->arch.sve_max_vl))) { \
  781. __size_ret = 0; \
  782. } else { \
  783. __vcpu_vq = vcpu_sve_max_vq(vcpu); \
  784. __size_ret = SVE_SIG_REGS_SIZE(__vcpu_vq); \
  785. } \
  786. \
  787. __size_ret; \
  788. })
  789. #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
  790. KVM_GUESTDBG_USE_SW_BP | \
  791. KVM_GUESTDBG_USE_HW | \
  792. KVM_GUESTDBG_SINGLESTEP)
  793. #define vcpu_has_sve(vcpu) (system_supports_sve() && \
  794. vcpu_get_flag(vcpu, GUEST_HAS_SVE))
  795. #ifdef CONFIG_ARM64_PTR_AUTH
  796. #define vcpu_has_ptrauth(vcpu) \
  797. ((cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH) || \
  798. cpus_have_final_cap(ARM64_HAS_GENERIC_AUTH)) && \
  799. vcpu_get_flag(vcpu, GUEST_HAS_PTRAUTH))
  800. #else
  801. #define vcpu_has_ptrauth(vcpu) false
  802. #endif
  803. #define vcpu_on_unsupported_cpu(vcpu) \
  804. vcpu_get_flag(vcpu, ON_UNSUPPORTED_CPU)
  805. #define vcpu_set_on_unsupported_cpu(vcpu) \
  806. vcpu_set_flag(vcpu, ON_UNSUPPORTED_CPU)
  807. #define vcpu_clear_on_unsupported_cpu(vcpu) \
  808. vcpu_clear_flag(vcpu, ON_UNSUPPORTED_CPU)
  809. #define vcpu_gp_regs(v) (&(v)->arch.ctxt.regs)
  810. /*
  811. * Only use __vcpu_sys_reg/ctxt_sys_reg if you know you want the
  812. * memory backed version of a register, and not the one most recently
  813. * accessed by a running VCPU. For example, for userspace access or
  814. * for system registers that are never context switched, but only
  815. * emulated.
  816. *
  817. * Don't bother with VNCR-based accesses in the nVHE code, it has no
  818. * business dealing with NV.
  819. */
  820. static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r)
  821. {
  822. #if !defined (__KVM_NVHE_HYPERVISOR__)
  823. if (unlikely(cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) &&
  824. r >= __VNCR_START__ && ctxt->vncr_array))
  825. return &ctxt->vncr_array[r - __VNCR_START__];
  826. #endif
  827. return (u64 *)&ctxt->sys_regs[r];
  828. }
  829. #define __ctxt_sys_reg(c,r) \
  830. ({ \
  831. BUILD_BUG_ON(__builtin_constant_p(r) && \
  832. (r) >= NR_SYS_REGS); \
  833. ___ctxt_sys_reg(c, r); \
  834. })
  835. #define ctxt_sys_reg(c,r) (*__ctxt_sys_reg(c,r))
  836. u64 kvm_vcpu_sanitise_vncr_reg(const struct kvm_vcpu *, enum vcpu_sysreg);
  837. #define __vcpu_sys_reg(v,r) \
  838. (*({ \
  839. const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \
  840. u64 *__r = __ctxt_sys_reg(ctxt, (r)); \
  841. if (vcpu_has_nv((v)) && (r) >= __VNCR_START__) \
  842. *__r = kvm_vcpu_sanitise_vncr_reg((v), (r)); \
  843. __r; \
  844. }))
  845. u64 vcpu_read_sys_reg(const struct kvm_vcpu *vcpu, int reg);
  846. void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg);
  847. static inline bool __vcpu_read_sys_reg_from_cpu(int reg, u64 *val)
  848. {
  849. /*
  850. * *** VHE ONLY ***
  851. *
  852. * System registers listed in the switch are not saved on every
  853. * exit from the guest but are only saved on vcpu_put.
  854. *
  855. * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but
  856. * should never be listed below, because the guest cannot modify its
  857. * own MPIDR_EL1 and MPIDR_EL1 is accessed for VCPU A from VCPU B's
  858. * thread when emulating cross-VCPU communication.
  859. */
  860. if (!has_vhe())
  861. return false;
  862. switch (reg) {
  863. case SCTLR_EL1: *val = read_sysreg_s(SYS_SCTLR_EL12); break;
  864. case CPACR_EL1: *val = read_sysreg_s(SYS_CPACR_EL12); break;
  865. case TTBR0_EL1: *val = read_sysreg_s(SYS_TTBR0_EL12); break;
  866. case TTBR1_EL1: *val = read_sysreg_s(SYS_TTBR1_EL12); break;
  867. case TCR_EL1: *val = read_sysreg_s(SYS_TCR_EL12); break;
  868. case ESR_EL1: *val = read_sysreg_s(SYS_ESR_EL12); break;
  869. case AFSR0_EL1: *val = read_sysreg_s(SYS_AFSR0_EL12); break;
  870. case AFSR1_EL1: *val = read_sysreg_s(SYS_AFSR1_EL12); break;
  871. case FAR_EL1: *val = read_sysreg_s(SYS_FAR_EL12); break;
  872. case MAIR_EL1: *val = read_sysreg_s(SYS_MAIR_EL12); break;
  873. case VBAR_EL1: *val = read_sysreg_s(SYS_VBAR_EL12); break;
  874. case CONTEXTIDR_EL1: *val = read_sysreg_s(SYS_CONTEXTIDR_EL12);break;
  875. case TPIDR_EL0: *val = read_sysreg_s(SYS_TPIDR_EL0); break;
  876. case TPIDRRO_EL0: *val = read_sysreg_s(SYS_TPIDRRO_EL0); break;
  877. case TPIDR_EL1: *val = read_sysreg_s(SYS_TPIDR_EL1); break;
  878. case AMAIR_EL1: *val = read_sysreg_s(SYS_AMAIR_EL12); break;
  879. case CNTKCTL_EL1: *val = read_sysreg_s(SYS_CNTKCTL_EL12); break;
  880. case ELR_EL1: *val = read_sysreg_s(SYS_ELR_EL12); break;
  881. case SPSR_EL1: *val = read_sysreg_s(SYS_SPSR_EL12); break;
  882. case PAR_EL1: *val = read_sysreg_par(); break;
  883. case DACR32_EL2: *val = read_sysreg_s(SYS_DACR32_EL2); break;
  884. case IFSR32_EL2: *val = read_sysreg_s(SYS_IFSR32_EL2); break;
  885. case DBGVCR32_EL2: *val = read_sysreg_s(SYS_DBGVCR32_EL2); break;
  886. case ZCR_EL1: *val = read_sysreg_s(SYS_ZCR_EL12); break;
  887. default: return false;
  888. }
  889. return true;
  890. }
  891. static inline bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg)
  892. {
  893. /*
  894. * *** VHE ONLY ***
  895. *
  896. * System registers listed in the switch are not restored on every
  897. * entry to the guest but are only restored on vcpu_load.
  898. *
  899. * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but
  900. * should never be listed below, because the MPIDR should only be set
  901. * once, before running the VCPU, and never changed later.
  902. */
  903. if (!has_vhe())
  904. return false;
  905. switch (reg) {
  906. case SCTLR_EL1: write_sysreg_s(val, SYS_SCTLR_EL12); break;
  907. case CPACR_EL1: write_sysreg_s(val, SYS_CPACR_EL12); break;
  908. case TTBR0_EL1: write_sysreg_s(val, SYS_TTBR0_EL12); break;
  909. case TTBR1_EL1: write_sysreg_s(val, SYS_TTBR1_EL12); break;
  910. case TCR_EL1: write_sysreg_s(val, SYS_TCR_EL12); break;
  911. case ESR_EL1: write_sysreg_s(val, SYS_ESR_EL12); break;
  912. case AFSR0_EL1: write_sysreg_s(val, SYS_AFSR0_EL12); break;
  913. case AFSR1_EL1: write_sysreg_s(val, SYS_AFSR1_EL12); break;
  914. case FAR_EL1: write_sysreg_s(val, SYS_FAR_EL12); break;
  915. case MAIR_EL1: write_sysreg_s(val, SYS_MAIR_EL12); break;
  916. case VBAR_EL1: write_sysreg_s(val, SYS_VBAR_EL12); break;
  917. case CONTEXTIDR_EL1: write_sysreg_s(val, SYS_CONTEXTIDR_EL12);break;
  918. case TPIDR_EL0: write_sysreg_s(val, SYS_TPIDR_EL0); break;
  919. case TPIDRRO_EL0: write_sysreg_s(val, SYS_TPIDRRO_EL0); break;
  920. case TPIDR_EL1: write_sysreg_s(val, SYS_TPIDR_EL1); break;
  921. case AMAIR_EL1: write_sysreg_s(val, SYS_AMAIR_EL12); break;
  922. case CNTKCTL_EL1: write_sysreg_s(val, SYS_CNTKCTL_EL12); break;
  923. case ELR_EL1: write_sysreg_s(val, SYS_ELR_EL12); break;
  924. case SPSR_EL1: write_sysreg_s(val, SYS_SPSR_EL12); break;
  925. case PAR_EL1: write_sysreg_s(val, SYS_PAR_EL1); break;
  926. case DACR32_EL2: write_sysreg_s(val, SYS_DACR32_EL2); break;
  927. case IFSR32_EL2: write_sysreg_s(val, SYS_IFSR32_EL2); break;
  928. case DBGVCR32_EL2: write_sysreg_s(val, SYS_DBGVCR32_EL2); break;
  929. case ZCR_EL1: write_sysreg_s(val, SYS_ZCR_EL12); break;
  930. default: return false;
  931. }
  932. return true;
  933. }
  934. struct kvm_vm_stat {
  935. struct kvm_vm_stat_generic generic;
  936. };
  937. struct kvm_vcpu_stat {
  938. struct kvm_vcpu_stat_generic generic;
  939. u64 hvc_exit_stat;
  940. u64 wfe_exit_stat;
  941. u64 wfi_exit_stat;
  942. u64 mmio_exit_user;
  943. u64 mmio_exit_kernel;
  944. u64 signal_exits;
  945. u64 exits;
  946. };
  947. unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
  948. int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
  949. int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  950. int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
  951. unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu);
  952. int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices);
  953. int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
  954. struct kvm_vcpu_events *events);
  955. int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
  956. struct kvm_vcpu_events *events);
  957. void kvm_arm_halt_guest(struct kvm *kvm);
  958. void kvm_arm_resume_guest(struct kvm *kvm);
  959. #define vcpu_has_run_once(vcpu) !!rcu_access_pointer((vcpu)->pid)
  960. #ifndef __KVM_NVHE_HYPERVISOR__
  961. #define kvm_call_hyp_nvhe(f, ...) \
  962. ({ \
  963. struct arm_smccc_res res; \
  964. \
  965. arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f), \
  966. ##__VA_ARGS__, &res); \
  967. WARN_ON(res.a0 != SMCCC_RET_SUCCESS); \
  968. \
  969. res.a1; \
  970. })
  971. /*
  972. * The couple of isb() below are there to guarantee the same behaviour
  973. * on VHE as on !VHE, where the eret to EL1 acts as a context
  974. * synchronization event.
  975. */
  976. #define kvm_call_hyp(f, ...) \
  977. do { \
  978. if (has_vhe()) { \
  979. f(__VA_ARGS__); \
  980. isb(); \
  981. } else { \
  982. kvm_call_hyp_nvhe(f, ##__VA_ARGS__); \
  983. } \
  984. } while(0)
  985. #define kvm_call_hyp_ret(f, ...) \
  986. ({ \
  987. typeof(f(__VA_ARGS__)) ret; \
  988. \
  989. if (has_vhe()) { \
  990. ret = f(__VA_ARGS__); \
  991. isb(); \
  992. } else { \
  993. ret = kvm_call_hyp_nvhe(f, ##__VA_ARGS__); \
  994. } \
  995. \
  996. ret; \
  997. })
  998. #else /* __KVM_NVHE_HYPERVISOR__ */
  999. #define kvm_call_hyp(f, ...) f(__VA_ARGS__)
  1000. #define kvm_call_hyp_ret(f, ...) f(__VA_ARGS__)
  1001. #define kvm_call_hyp_nvhe(f, ...) f(__VA_ARGS__)
  1002. #endif /* __KVM_NVHE_HYPERVISOR__ */
  1003. int handle_exit(struct kvm_vcpu *vcpu, int exception_index);
  1004. void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index);
  1005. int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu);
  1006. int kvm_handle_cp14_32(struct kvm_vcpu *vcpu);
  1007. int kvm_handle_cp14_64(struct kvm_vcpu *vcpu);
  1008. int kvm_handle_cp15_32(struct kvm_vcpu *vcpu);
  1009. int kvm_handle_cp15_64(struct kvm_vcpu *vcpu);
  1010. int kvm_handle_sys_reg(struct kvm_vcpu *vcpu);
  1011. int kvm_handle_cp10_id(struct kvm_vcpu *vcpu);
  1012. void kvm_sys_regs_create_debugfs(struct kvm *kvm);
  1013. void kvm_reset_sys_regs(struct kvm_vcpu *vcpu);
  1014. int __init kvm_sys_reg_table_init(void);
  1015. struct sys_reg_desc;
  1016. int __init populate_sysreg_config(const struct sys_reg_desc *sr,
  1017. unsigned int idx);
  1018. int __init populate_nv_trap_config(void);
  1019. bool lock_all_vcpus(struct kvm *kvm);
  1020. void unlock_all_vcpus(struct kvm *kvm);
  1021. void kvm_calculate_traps(struct kvm_vcpu *vcpu);
  1022. /* MMIO helpers */
  1023. void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data);
  1024. unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);
  1025. int kvm_handle_mmio_return(struct kvm_vcpu *vcpu);
  1026. int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa);
  1027. /*
  1028. * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event,
  1029. * arrived in guest context. For arm64, any event that arrives while a vCPU is
  1030. * loaded is considered to be "in guest".
  1031. */
  1032. static inline bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu)
  1033. {
  1034. return IS_ENABLED(CONFIG_GUEST_PERF_EVENTS) && !!vcpu;
  1035. }
  1036. long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu);
  1037. gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu);
  1038. void kvm_update_stolen_time(struct kvm_vcpu *vcpu);
  1039. bool kvm_arm_pvtime_supported(void);
  1040. int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu,
  1041. struct kvm_device_attr *attr);
  1042. int kvm_arm_pvtime_get_attr(struct kvm_vcpu *vcpu,
  1043. struct kvm_device_attr *attr);
  1044. int kvm_arm_pvtime_has_attr(struct kvm_vcpu *vcpu,
  1045. struct kvm_device_attr *attr);
  1046. extern unsigned int __ro_after_init kvm_arm_vmid_bits;
  1047. int __init kvm_arm_vmid_alloc_init(void);
  1048. void __init kvm_arm_vmid_alloc_free(void);
  1049. void kvm_arm_vmid_update(struct kvm_vmid *kvm_vmid);
  1050. void kvm_arm_vmid_clear_active(void);
  1051. static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch)
  1052. {
  1053. vcpu_arch->steal.base = INVALID_GPA;
  1054. }
  1055. static inline bool kvm_arm_is_pvtime_enabled(struct kvm_vcpu_arch *vcpu_arch)
  1056. {
  1057. return (vcpu_arch->steal.base != INVALID_GPA);
  1058. }
  1059. void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 syndrome);
  1060. struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
  1061. DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
  1062. /*
  1063. * How we access per-CPU host data depends on the where we access it from,
  1064. * and the mode we're in:
  1065. *
  1066. * - VHE and nVHE hypervisor bits use their locally defined instance
  1067. *
  1068. * - the rest of the kernel use either the VHE or nVHE one, depending on
  1069. * the mode we're running in.
  1070. *
  1071. * Unless we're in protected mode, fully deprivileged, and the nVHE
  1072. * per-CPU stuff is exclusively accessible to the protected EL2 code.
  1073. * In this case, the EL1 code uses the *VHE* data as its private state
  1074. * (which makes sense in a way as there shouldn't be any shared state
  1075. * between the host and the hypervisor).
  1076. *
  1077. * Yes, this is all totally trivial. Shoot me now.
  1078. */
  1079. #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
  1080. #define host_data_ptr(f) (&this_cpu_ptr(&kvm_host_data)->f)
  1081. #else
  1082. #define host_data_ptr(f) \
  1083. (static_branch_unlikely(&kvm_protected_mode_initialized) ? \
  1084. &this_cpu_ptr(&kvm_host_data)->f : \
  1085. &this_cpu_ptr_hyp_sym(kvm_host_data)->f)
  1086. #endif
  1087. /* Check whether the FP regs are owned by the guest */
  1088. static inline bool guest_owns_fp_regs(void)
  1089. {
  1090. return *host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED;
  1091. }
  1092. /* Check whether the FP regs are owned by the host */
  1093. static inline bool host_owns_fp_regs(void)
  1094. {
  1095. return *host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED;
  1096. }
  1097. static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
  1098. {
  1099. /* The host's MPIDR is immutable, so let's set it up at boot time */
  1100. ctxt_sys_reg(cpu_ctxt, MPIDR_EL1) = read_cpuid_mpidr();
  1101. }
  1102. static inline bool kvm_system_needs_idmapped_vectors(void)
  1103. {
  1104. return cpus_have_final_cap(ARM64_SPECTRE_V3A);
  1105. }
  1106. static inline void kvm_arch_sync_events(struct kvm *kvm) {}
  1107. void kvm_arm_init_debug(void);
  1108. void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu);
  1109. void kvm_arm_setup_debug(struct kvm_vcpu *vcpu);
  1110. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu);
  1111. void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu);
  1112. #define kvm_vcpu_os_lock_enabled(vcpu) \
  1113. (!!(__vcpu_sys_reg(vcpu, OSLSR_EL1) & OSLSR_EL1_OSLK))
  1114. int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
  1115. struct kvm_device_attr *attr);
  1116. int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
  1117. struct kvm_device_attr *attr);
  1118. int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
  1119. struct kvm_device_attr *attr);
  1120. int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
  1121. struct kvm_arm_copy_mte_tags *copy_tags);
  1122. int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm,
  1123. struct kvm_arm_counter_offset *offset);
  1124. int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm,
  1125. struct reg_mask_range *range);
  1126. /* Guest/host FPSIMD coordination helpers */
  1127. int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
  1128. void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
  1129. void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu);
  1130. void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu);
  1131. void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu);
  1132. static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
  1133. {
  1134. return (!has_vhe() && attr->exclude_host);
  1135. }
  1136. /* Flags for host debug state */
  1137. void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu);
  1138. void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
  1139. #ifdef CONFIG_KVM
  1140. void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr);
  1141. void kvm_clr_pmu_events(u64 clr);
  1142. bool kvm_set_pmuserenr(u64 val);
  1143. #else
  1144. static inline void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr) {}
  1145. static inline void kvm_clr_pmu_events(u64 clr) {}
  1146. static inline bool kvm_set_pmuserenr(u64 val)
  1147. {
  1148. return false;
  1149. }
  1150. #endif
  1151. void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu);
  1152. void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu);
  1153. int __init kvm_set_ipa_limit(void);
  1154. u32 kvm_get_pa_bits(struct kvm *kvm);
  1155. #define __KVM_HAVE_ARCH_VM_ALLOC
  1156. struct kvm *kvm_arch_alloc_vm(void);
  1157. #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS
  1158. #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
  1159. #define kvm_vm_is_protected(kvm) (is_protected_kvm_enabled() && (kvm)->arch.pkvm.enabled)
  1160. #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
  1161. int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
  1162. bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
  1163. #define kvm_arm_vcpu_sve_finalized(vcpu) vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED)
  1164. #define kvm_has_mte(kvm) \
  1165. (system_supports_mte() && \
  1166. test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags))
  1167. #define kvm_supports_32bit_el0() \
  1168. (system_supports_32bit_el0() && \
  1169. !static_branch_unlikely(&arm64_mismatched_32bit_el0))
  1170. #define kvm_vm_has_ran_once(kvm) \
  1171. (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags))
  1172. static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
  1173. {
  1174. return test_bit(feature, ka->vcpu_features);
  1175. }
  1176. #define vcpu_has_feature(v, f) __vcpu_has_feature(&(v)->kvm->arch, (f))
  1177. #define kvm_vcpu_initialized(v) vcpu_get_flag(vcpu, VCPU_INITIALIZED)
  1178. int kvm_trng_call(struct kvm_vcpu *vcpu);
  1179. #ifdef CONFIG_KVM
  1180. extern phys_addr_t hyp_mem_base;
  1181. extern phys_addr_t hyp_mem_size;
  1182. void __init kvm_hyp_reserve(void);
  1183. #else
  1184. static inline void kvm_hyp_reserve(void) { }
  1185. #endif
  1186. void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
  1187. bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
  1188. static inline u64 *__vm_id_reg(struct kvm_arch *ka, u32 reg)
  1189. {
  1190. switch (reg) {
  1191. case sys_reg(3, 0, 0, 1, 0) ... sys_reg(3, 0, 0, 7, 7):
  1192. return &ka->id_regs[IDREG_IDX(reg)];
  1193. case SYS_CTR_EL0:
  1194. return &ka->ctr_el0;
  1195. default:
  1196. WARN_ON_ONCE(1);
  1197. return NULL;
  1198. }
  1199. }
  1200. #define kvm_read_vm_id_reg(kvm, reg) \
  1201. ({ u64 __val = *__vm_id_reg(&(kvm)->arch, reg); __val; })
  1202. void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
  1203. #define __expand_field_sign_unsigned(id, fld, val) \
  1204. ((u64)SYS_FIELD_VALUE(id, fld, val))
  1205. #define __expand_field_sign_signed(id, fld, val) \
  1206. ({ \
  1207. u64 __val = SYS_FIELD_VALUE(id, fld, val); \
  1208. sign_extend64(__val, id##_##fld##_WIDTH - 1); \
  1209. })
  1210. #define get_idreg_field_unsigned(kvm, id, fld) \
  1211. ({ \
  1212. u64 __val = kvm_read_vm_id_reg((kvm), SYS_##id); \
  1213. FIELD_GET(id##_##fld##_MASK, __val); \
  1214. })
  1215. #define get_idreg_field_signed(kvm, id, fld) \
  1216. ({ \
  1217. u64 __val = get_idreg_field_unsigned(kvm, id, fld); \
  1218. sign_extend64(__val, id##_##fld##_WIDTH - 1); \
  1219. })
  1220. #define get_idreg_field_enum(kvm, id, fld) \
  1221. get_idreg_field_unsigned(kvm, id, fld)
  1222. #define kvm_cmp_feat_signed(kvm, id, fld, op, limit) \
  1223. (get_idreg_field_signed((kvm), id, fld) op __expand_field_sign_signed(id, fld, limit))
  1224. #define kvm_cmp_feat_unsigned(kvm, id, fld, op, limit) \
  1225. (get_idreg_field_unsigned((kvm), id, fld) op __expand_field_sign_unsigned(id, fld, limit))
  1226. #define kvm_cmp_feat(kvm, id, fld, op, limit) \
  1227. (id##_##fld##_SIGNED ? \
  1228. kvm_cmp_feat_signed(kvm, id, fld, op, limit) : \
  1229. kvm_cmp_feat_unsigned(kvm, id, fld, op, limit))
  1230. #define kvm_has_feat(kvm, id, fld, limit) \
  1231. kvm_cmp_feat(kvm, id, fld, >=, limit)
  1232. #define kvm_has_feat_enum(kvm, id, fld, val) \
  1233. kvm_cmp_feat_unsigned(kvm, id, fld, ==, val)
  1234. #define kvm_has_feat_range(kvm, id, fld, min, max) \
  1235. (kvm_cmp_feat(kvm, id, fld, >=, min) && \
  1236. kvm_cmp_feat(kvm, id, fld, <=, max))
  1237. /* Check for a given level of PAuth support */
  1238. #define kvm_has_pauth(k, l) \
  1239. ({ \
  1240. bool pa, pi, pa3; \
  1241. \
  1242. pa = kvm_has_feat((k), ID_AA64ISAR1_EL1, APA, l); \
  1243. pa &= kvm_has_feat((k), ID_AA64ISAR1_EL1, GPA, IMP); \
  1244. pi = kvm_has_feat((k), ID_AA64ISAR1_EL1, API, l); \
  1245. pi &= kvm_has_feat((k), ID_AA64ISAR1_EL1, GPI, IMP); \
  1246. pa3 = kvm_has_feat((k), ID_AA64ISAR2_EL1, APA3, l); \
  1247. pa3 &= kvm_has_feat((k), ID_AA64ISAR2_EL1, GPA3, IMP); \
  1248. \
  1249. (pa + pi + pa3) == 1; \
  1250. })
  1251. #define kvm_has_fpmr(k) \
  1252. (system_supports_fpmr() && \
  1253. kvm_has_feat((k), ID_AA64PFR2_EL1, FPMR, IMP))
  1254. #endif /* __ARM64_KVM_HOST_H__ */