kvm_emulate.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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/kvm_emulate.h
  7. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  8. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  9. */
  10. #ifndef __ARM64_KVM_EMULATE_H__
  11. #define __ARM64_KVM_EMULATE_H__
  12. #include <linux/bitfield.h>
  13. #include <linux/kvm_host.h>
  14. #include <asm/debug-monitors.h>
  15. #include <asm/esr.h>
  16. #include <asm/kvm_arm.h>
  17. #include <asm/kvm_hyp.h>
  18. #include <asm/kvm_nested.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/cputype.h>
  21. #include <asm/virt.h>
  22. #define CURRENT_EL_SP_EL0_VECTOR 0x0
  23. #define CURRENT_EL_SP_ELx_VECTOR 0x200
  24. #define LOWER_EL_AArch64_VECTOR 0x400
  25. #define LOWER_EL_AArch32_VECTOR 0x600
  26. enum exception_type {
  27. except_type_sync = 0,
  28. except_type_irq = 0x80,
  29. except_type_fiq = 0x100,
  30. except_type_serror = 0x180,
  31. };
  32. #define kvm_exception_type_names \
  33. { except_type_sync, "SYNC" }, \
  34. { except_type_irq, "IRQ" }, \
  35. { except_type_fiq, "FIQ" }, \
  36. { except_type_serror, "SERROR" }
  37. bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
  38. void kvm_skip_instr32(struct kvm_vcpu *vcpu);
  39. void kvm_inject_undefined(struct kvm_vcpu *vcpu);
  40. void kvm_inject_vabt(struct kvm_vcpu *vcpu);
  41. void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr);
  42. void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr);
  43. void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
  44. void kvm_vcpu_wfi(struct kvm_vcpu *vcpu);
  45. void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu);
  46. int kvm_inject_nested_sync(struct kvm_vcpu *vcpu, u64 esr_el2);
  47. int kvm_inject_nested_irq(struct kvm_vcpu *vcpu);
  48. static inline void kvm_inject_nested_sve_trap(struct kvm_vcpu *vcpu)
  49. {
  50. u64 esr = FIELD_PREP(ESR_ELx_EC_MASK, ESR_ELx_EC_SVE) |
  51. ESR_ELx_IL;
  52. kvm_inject_nested_sync(vcpu, esr);
  53. }
  54. #if defined(__KVM_VHE_HYPERVISOR__) || defined(__KVM_NVHE_HYPERVISOR__)
  55. static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
  56. {
  57. return !(vcpu->arch.hcr_el2 & HCR_RW);
  58. }
  59. #else
  60. static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
  61. {
  62. return vcpu_has_feature(vcpu, KVM_ARM_VCPU_EL1_32BIT);
  63. }
  64. #endif
  65. static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
  66. {
  67. if (!vcpu_has_run_once(vcpu))
  68. vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
  69. /*
  70. * For non-FWB CPUs, we trap VM ops (HCR_EL2.TVM) until M+C
  71. * get set in SCTLR_EL1 such that we can detect when the guest
  72. * MMU gets turned on and do the necessary cache maintenance
  73. * then.
  74. */
  75. if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
  76. vcpu->arch.hcr_el2 |= HCR_TVM;
  77. }
  78. static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
  79. {
  80. return (unsigned long *)&vcpu->arch.hcr_el2;
  81. }
  82. static inline void vcpu_clear_wfx_traps(struct kvm_vcpu *vcpu)
  83. {
  84. vcpu->arch.hcr_el2 &= ~HCR_TWE;
  85. if (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
  86. vcpu->kvm->arch.vgic.nassgireq)
  87. vcpu->arch.hcr_el2 &= ~HCR_TWI;
  88. else
  89. vcpu->arch.hcr_el2 |= HCR_TWI;
  90. }
  91. static inline void vcpu_set_wfx_traps(struct kvm_vcpu *vcpu)
  92. {
  93. vcpu->arch.hcr_el2 |= HCR_TWE;
  94. vcpu->arch.hcr_el2 |= HCR_TWI;
  95. }
  96. static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
  97. {
  98. return vcpu->arch.vsesr_el2;
  99. }
  100. static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
  101. {
  102. vcpu->arch.vsesr_el2 = vsesr;
  103. }
  104. static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
  105. {
  106. return (unsigned long *)&vcpu_gp_regs(vcpu)->pc;
  107. }
  108. static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
  109. {
  110. return (unsigned long *)&vcpu_gp_regs(vcpu)->pstate;
  111. }
  112. static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
  113. {
  114. return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
  115. }
  116. static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
  117. {
  118. if (vcpu_mode_is_32bit(vcpu))
  119. return kvm_condition_valid32(vcpu);
  120. return true;
  121. }
  122. static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
  123. {
  124. *vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT;
  125. }
  126. /*
  127. * vcpu_get_reg and vcpu_set_reg should always be passed a register number
  128. * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
  129. * AArch32 with banked registers.
  130. */
  131. static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
  132. u8 reg_num)
  133. {
  134. return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs[reg_num];
  135. }
  136. static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
  137. unsigned long val)
  138. {
  139. if (reg_num != 31)
  140. vcpu_gp_regs(vcpu)->regs[reg_num] = val;
  141. }
  142. static inline bool vcpu_is_el2_ctxt(const struct kvm_cpu_context *ctxt)
  143. {
  144. switch (ctxt->regs.pstate & (PSR_MODE32_BIT | PSR_MODE_MASK)) {
  145. case PSR_MODE_EL2h:
  146. case PSR_MODE_EL2t:
  147. return true;
  148. default:
  149. return false;
  150. }
  151. }
  152. static inline bool vcpu_is_el2(const struct kvm_vcpu *vcpu)
  153. {
  154. return vcpu_is_el2_ctxt(&vcpu->arch.ctxt);
  155. }
  156. static inline bool __vcpu_el2_e2h_is_set(const struct kvm_cpu_context *ctxt)
  157. {
  158. return (!cpus_have_final_cap(ARM64_HAS_HCR_NV1) ||
  159. (ctxt_sys_reg(ctxt, HCR_EL2) & HCR_E2H));
  160. }
  161. static inline bool vcpu_el2_e2h_is_set(const struct kvm_vcpu *vcpu)
  162. {
  163. return __vcpu_el2_e2h_is_set(&vcpu->arch.ctxt);
  164. }
  165. static inline bool __vcpu_el2_tge_is_set(const struct kvm_cpu_context *ctxt)
  166. {
  167. return ctxt_sys_reg(ctxt, HCR_EL2) & HCR_TGE;
  168. }
  169. static inline bool vcpu_el2_tge_is_set(const struct kvm_vcpu *vcpu)
  170. {
  171. return __vcpu_el2_tge_is_set(&vcpu->arch.ctxt);
  172. }
  173. static inline bool __is_hyp_ctxt(const struct kvm_cpu_context *ctxt)
  174. {
  175. /*
  176. * We are in a hypervisor context if the vcpu mode is EL2 or
  177. * E2H and TGE bits are set. The latter means we are in the user space
  178. * of the VHE kernel. ARMv8.1 ARM describes this as 'InHost'
  179. *
  180. * Note that the HCR_EL2.{E2H,TGE}={0,1} isn't really handled in the
  181. * rest of the KVM code, and will result in a misbehaving guest.
  182. */
  183. return vcpu_is_el2_ctxt(ctxt) ||
  184. (__vcpu_el2_e2h_is_set(ctxt) && __vcpu_el2_tge_is_set(ctxt)) ||
  185. __vcpu_el2_tge_is_set(ctxt);
  186. }
  187. static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
  188. {
  189. return vcpu_has_nv(vcpu) && __is_hyp_ctxt(&vcpu->arch.ctxt);
  190. }
  191. /*
  192. * The layout of SPSR for an AArch32 state is different when observed from an
  193. * AArch64 SPSR_ELx or an AArch32 SPSR_*. This function generates the AArch32
  194. * view given an AArch64 view.
  195. *
  196. * In ARM DDI 0487E.a see:
  197. *
  198. * - The AArch64 view (SPSR_EL2) in section C5.2.18, page C5-426
  199. * - The AArch32 view (SPSR_abt) in section G8.2.126, page G8-6256
  200. * - The AArch32 view (SPSR_und) in section G8.2.132, page G8-6280
  201. *
  202. * Which show the following differences:
  203. *
  204. * | Bit | AA64 | AA32 | Notes |
  205. * +-----+------+------+-----------------------------|
  206. * | 24 | DIT | J | J is RES0 in ARMv8 |
  207. * | 21 | SS | DIT | SS doesn't exist in AArch32 |
  208. *
  209. * ... and all other bits are (currently) common.
  210. */
  211. static inline unsigned long host_spsr_to_spsr32(unsigned long spsr)
  212. {
  213. const unsigned long overlap = BIT(24) | BIT(21);
  214. unsigned long dit = !!(spsr & PSR_AA32_DIT_BIT);
  215. spsr &= ~overlap;
  216. spsr |= dit << 21;
  217. return spsr;
  218. }
  219. static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
  220. {
  221. u32 mode;
  222. if (vcpu_mode_is_32bit(vcpu)) {
  223. mode = *vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK;
  224. return mode > PSR_AA32_MODE_USR;
  225. }
  226. mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
  227. return mode != PSR_MODE_EL0t;
  228. }
  229. static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
  230. {
  231. return vcpu->arch.fault.esr_el2;
  232. }
  233. static __always_inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
  234. {
  235. u64 esr = kvm_vcpu_get_esr(vcpu);
  236. if (esr & ESR_ELx_CV)
  237. return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
  238. return -1;
  239. }
  240. static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
  241. {
  242. return vcpu->arch.fault.far_el2;
  243. }
  244. static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
  245. {
  246. return ((phys_addr_t)vcpu->arch.fault.hpfar_el2 & HPFAR_MASK) << 8;
  247. }
  248. static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
  249. {
  250. return vcpu->arch.fault.disr_el1;
  251. }
  252. static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
  253. {
  254. return kvm_vcpu_get_esr(vcpu) & ESR_ELx_xVC_IMM_MASK;
  255. }
  256. static __always_inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
  257. {
  258. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_ISV);
  259. }
  260. static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu *vcpu)
  261. {
  262. return kvm_vcpu_get_esr(vcpu) & (ESR_ELx_CM | ESR_ELx_WNR | ESR_ELx_FSC);
  263. }
  264. static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
  265. {
  266. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SSE);
  267. }
  268. static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
  269. {
  270. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SF);
  271. }
  272. static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
  273. {
  274. return (kvm_vcpu_get_esr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
  275. }
  276. static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu)
  277. {
  278. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_S1PTW);
  279. }
  280. /* Always check for S1PTW *before* using this. */
  281. static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
  282. {
  283. return kvm_vcpu_get_esr(vcpu) & ESR_ELx_WNR;
  284. }
  285. static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu)
  286. {
  287. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_CM);
  288. }
  289. static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
  290. {
  291. return 1 << ((kvm_vcpu_get_esr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
  292. }
  293. /* This one is not specific to Data Abort */
  294. static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
  295. {
  296. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_IL);
  297. }
  298. static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
  299. {
  300. return ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
  301. }
  302. static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
  303. {
  304. return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW;
  305. }
  306. static inline bool kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu *vcpu)
  307. {
  308. return kvm_vcpu_trap_is_iabt(vcpu) && !kvm_vcpu_abt_iss1tw(vcpu);
  309. }
  310. static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
  311. {
  312. return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC;
  313. }
  314. static inline
  315. bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
  316. {
  317. return esr_fsc_is_permission_fault(kvm_vcpu_get_esr(vcpu));
  318. }
  319. static inline
  320. bool kvm_vcpu_trap_is_translation_fault(const struct kvm_vcpu *vcpu)
  321. {
  322. return esr_fsc_is_translation_fault(kvm_vcpu_get_esr(vcpu));
  323. }
  324. static inline
  325. u64 kvm_vcpu_trap_get_perm_fault_granule(const struct kvm_vcpu *vcpu)
  326. {
  327. unsigned long esr = kvm_vcpu_get_esr(vcpu);
  328. BUG_ON(!esr_fsc_is_permission_fault(esr));
  329. return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(esr & ESR_ELx_FSC_LEVEL));
  330. }
  331. static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
  332. {
  333. switch (kvm_vcpu_trap_get_fault(vcpu)) {
  334. case ESR_ELx_FSC_EXTABT:
  335. case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3):
  336. case ESR_ELx_FSC_SECC:
  337. case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3):
  338. return true;
  339. default:
  340. return false;
  341. }
  342. }
  343. static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
  344. {
  345. u64 esr = kvm_vcpu_get_esr(vcpu);
  346. return ESR_ELx_SYS64_ISS_RT(esr);
  347. }
  348. static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  349. {
  350. if (kvm_vcpu_abt_iss1tw(vcpu)) {
  351. /*
  352. * Only a permission fault on a S1PTW should be
  353. * considered as a write. Otherwise, page tables baked
  354. * in a read-only memslot will result in an exception
  355. * being delivered in the guest.
  356. *
  357. * The drawback is that we end-up faulting twice if the
  358. * guest is using any of HW AF/DB: a translation fault
  359. * to map the page containing the PT (read only at
  360. * first), then a permission fault to allow the flags
  361. * to be set.
  362. */
  363. return kvm_vcpu_trap_is_permission_fault(vcpu);
  364. }
  365. if (kvm_vcpu_trap_is_iabt(vcpu))
  366. return false;
  367. return kvm_vcpu_dabt_iswrite(vcpu);
  368. }
  369. static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
  370. {
  371. return __vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
  372. }
  373. static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
  374. {
  375. if (vcpu_mode_is_32bit(vcpu)) {
  376. *vcpu_cpsr(vcpu) |= PSR_AA32_E_BIT;
  377. } else {
  378. u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1);
  379. sctlr |= SCTLR_ELx_EE;
  380. vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1);
  381. }
  382. }
  383. static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
  384. {
  385. if (vcpu_mode_is_32bit(vcpu))
  386. return !!(*vcpu_cpsr(vcpu) & PSR_AA32_E_BIT);
  387. if (vcpu_mode_priv(vcpu))
  388. return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & SCTLR_ELx_EE);
  389. else
  390. return !!(vcpu_read_sys_reg(vcpu, SCTLR_EL1) & SCTLR_EL1_E0E);
  391. }
  392. static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
  393. unsigned long data,
  394. unsigned int len)
  395. {
  396. if (kvm_vcpu_is_be(vcpu)) {
  397. switch (len) {
  398. case 1:
  399. return data & 0xff;
  400. case 2:
  401. return be16_to_cpu(data & 0xffff);
  402. case 4:
  403. return be32_to_cpu(data & 0xffffffff);
  404. default:
  405. return be64_to_cpu(data);
  406. }
  407. } else {
  408. switch (len) {
  409. case 1:
  410. return data & 0xff;
  411. case 2:
  412. return le16_to_cpu(data & 0xffff);
  413. case 4:
  414. return le32_to_cpu(data & 0xffffffff);
  415. default:
  416. return le64_to_cpu(data);
  417. }
  418. }
  419. return data; /* Leave LE untouched */
  420. }
  421. static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
  422. unsigned long data,
  423. unsigned int len)
  424. {
  425. if (kvm_vcpu_is_be(vcpu)) {
  426. switch (len) {
  427. case 1:
  428. return data & 0xff;
  429. case 2:
  430. return cpu_to_be16(data & 0xffff);
  431. case 4:
  432. return cpu_to_be32(data & 0xffffffff);
  433. default:
  434. return cpu_to_be64(data);
  435. }
  436. } else {
  437. switch (len) {
  438. case 1:
  439. return data & 0xff;
  440. case 2:
  441. return cpu_to_le16(data & 0xffff);
  442. case 4:
  443. return cpu_to_le32(data & 0xffffffff);
  444. default:
  445. return cpu_to_le64(data);
  446. }
  447. }
  448. return data; /* Leave LE untouched */
  449. }
  450. static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)
  451. {
  452. WARN_ON(vcpu_get_flag(vcpu, PENDING_EXCEPTION));
  453. vcpu_set_flag(vcpu, INCREMENT_PC);
  454. }
  455. #define kvm_pend_exception(v, e) \
  456. do { \
  457. WARN_ON(vcpu_get_flag((v), INCREMENT_PC)); \
  458. vcpu_set_flag((v), PENDING_EXCEPTION); \
  459. vcpu_set_flag((v), e); \
  460. } while (0)
  461. #define __build_check_all_or_none(r, bits) \
  462. BUILD_BUG_ON(((r) & (bits)) && ((r) & (bits)) != (bits))
  463. #define __cpacr_to_cptr_clr(clr, set) \
  464. ({ \
  465. u64 cptr = 0; \
  466. \
  467. if ((set) & CPACR_ELx_FPEN) \
  468. cptr |= CPTR_EL2_TFP; \
  469. if ((set) & CPACR_ELx_ZEN) \
  470. cptr |= CPTR_EL2_TZ; \
  471. if ((set) & CPACR_ELx_SMEN) \
  472. cptr |= CPTR_EL2_TSM; \
  473. if ((clr) & CPACR_ELx_TTA) \
  474. cptr |= CPTR_EL2_TTA; \
  475. if ((clr) & CPTR_EL2_TAM) \
  476. cptr |= CPTR_EL2_TAM; \
  477. if ((clr) & CPTR_EL2_TCPAC) \
  478. cptr |= CPTR_EL2_TCPAC; \
  479. \
  480. cptr; \
  481. })
  482. #define __cpacr_to_cptr_set(clr, set) \
  483. ({ \
  484. u64 cptr = 0; \
  485. \
  486. if ((clr) & CPACR_ELx_FPEN) \
  487. cptr |= CPTR_EL2_TFP; \
  488. if ((clr) & CPACR_ELx_ZEN) \
  489. cptr |= CPTR_EL2_TZ; \
  490. if ((clr) & CPACR_ELx_SMEN) \
  491. cptr |= CPTR_EL2_TSM; \
  492. if ((set) & CPACR_ELx_TTA) \
  493. cptr |= CPTR_EL2_TTA; \
  494. if ((set) & CPTR_EL2_TAM) \
  495. cptr |= CPTR_EL2_TAM; \
  496. if ((set) & CPTR_EL2_TCPAC) \
  497. cptr |= CPTR_EL2_TCPAC; \
  498. \
  499. cptr; \
  500. })
  501. #define cpacr_clear_set(clr, set) \
  502. do { \
  503. BUILD_BUG_ON((set) & CPTR_VHE_EL2_RES0); \
  504. BUILD_BUG_ON((clr) & CPACR_ELx_E0POE); \
  505. __build_check_all_or_none((clr), CPACR_ELx_FPEN); \
  506. __build_check_all_or_none((set), CPACR_ELx_FPEN); \
  507. __build_check_all_or_none((clr), CPACR_ELx_ZEN); \
  508. __build_check_all_or_none((set), CPACR_ELx_ZEN); \
  509. __build_check_all_or_none((clr), CPACR_ELx_SMEN); \
  510. __build_check_all_or_none((set), CPACR_ELx_SMEN); \
  511. \
  512. if (has_vhe() || has_hvhe()) \
  513. sysreg_clear_set(cpacr_el1, clr, set); \
  514. else \
  515. sysreg_clear_set(cptr_el2, \
  516. __cpacr_to_cptr_clr(clr, set), \
  517. __cpacr_to_cptr_set(clr, set));\
  518. } while (0)
  519. static __always_inline void kvm_write_cptr_el2(u64 val)
  520. {
  521. if (has_vhe() || has_hvhe())
  522. write_sysreg(val, cpacr_el1);
  523. else
  524. write_sysreg(val, cptr_el2);
  525. }
  526. static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
  527. {
  528. u64 val;
  529. if (has_vhe()) {
  530. val = (CPACR_ELx_FPEN | CPACR_EL1_ZEN_EL1EN);
  531. if (cpus_have_final_cap(ARM64_SME))
  532. val |= CPACR_EL1_SMEN_EL1EN;
  533. } else if (has_hvhe()) {
  534. val = CPACR_ELx_FPEN;
  535. if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs())
  536. val |= CPACR_ELx_ZEN;
  537. if (cpus_have_final_cap(ARM64_SME))
  538. val |= CPACR_ELx_SMEN;
  539. } else {
  540. val = CPTR_NVHE_EL2_RES1;
  541. if (vcpu_has_sve(vcpu) && guest_owns_fp_regs())
  542. val |= CPTR_EL2_TZ;
  543. if (cpus_have_final_cap(ARM64_SME))
  544. val &= ~CPTR_EL2_TSM;
  545. }
  546. return val;
  547. }
  548. static __always_inline void kvm_reset_cptr_el2(struct kvm_vcpu *vcpu)
  549. {
  550. u64 val = kvm_get_reset_cptr_el2(vcpu);
  551. kvm_write_cptr_el2(val);
  552. }
  553. /*
  554. * Returns a 'sanitised' view of CPTR_EL2, translating from nVHE to the VHE
  555. * format if E2H isn't set.
  556. */
  557. static inline u64 vcpu_sanitised_cptr_el2(const struct kvm_vcpu *vcpu)
  558. {
  559. u64 cptr = __vcpu_sys_reg(vcpu, CPTR_EL2);
  560. if (!vcpu_el2_e2h_is_set(vcpu))
  561. cptr = translate_cptr_el2_to_cpacr_el1(cptr);
  562. return cptr;
  563. }
  564. static inline bool ____cptr_xen_trap_enabled(const struct kvm_vcpu *vcpu,
  565. unsigned int xen)
  566. {
  567. switch (xen) {
  568. case 0b00:
  569. case 0b10:
  570. return true;
  571. case 0b01:
  572. return vcpu_el2_tge_is_set(vcpu) && !vcpu_is_el2(vcpu);
  573. case 0b11:
  574. default:
  575. return false;
  576. }
  577. }
  578. #define __guest_hyp_cptr_xen_trap_enabled(vcpu, xen) \
  579. (!vcpu_has_nv(vcpu) ? false : \
  580. ____cptr_xen_trap_enabled(vcpu, \
  581. SYS_FIELD_GET(CPACR_ELx, xen, \
  582. vcpu_sanitised_cptr_el2(vcpu))))
  583. static inline bool guest_hyp_fpsimd_traps_enabled(const struct kvm_vcpu *vcpu)
  584. {
  585. return __guest_hyp_cptr_xen_trap_enabled(vcpu, FPEN);
  586. }
  587. static inline bool guest_hyp_sve_traps_enabled(const struct kvm_vcpu *vcpu)
  588. {
  589. return __guest_hyp_cptr_xen_trap_enabled(vcpu, ZEN);
  590. }
  591. #endif /* __ARM64_KVM_EMULATE_H__ */