kvm_host.h 49 KB

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