kvm_emulate.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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_sync(struct kvm_vcpu *vcpu, u64 esr);
  41. int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
  42. int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
  43. int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
  44. void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
  45. static inline int kvm_inject_sea_dabt(struct kvm_vcpu *vcpu, u64 addr)
  46. {
  47. return kvm_inject_sea(vcpu, false, addr);
  48. }
  49. static inline int kvm_inject_sea_iabt(struct kvm_vcpu *vcpu, u64 addr)
  50. {
  51. return kvm_inject_sea(vcpu, true, addr);
  52. }
  53. static inline int kvm_inject_serror(struct kvm_vcpu *vcpu)
  54. {
  55. /*
  56. * ESR_ELx.ISV (later renamed to IDS) indicates whether or not
  57. * ESR_ELx.ISS contains IMPLEMENTATION DEFINED syndrome information.
  58. *
  59. * Set the bit when injecting an SError w/o an ESR to indicate ISS
  60. * does not follow the architected format.
  61. */
  62. return kvm_inject_serror_esr(vcpu, ESR_ELx_ISV);
  63. }
  64. void kvm_vcpu_wfi(struct kvm_vcpu *vcpu);
  65. void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu);
  66. int kvm_inject_nested_sync(struct kvm_vcpu *vcpu, u64 esr_el2);
  67. int kvm_inject_nested_irq(struct kvm_vcpu *vcpu);
  68. int kvm_inject_nested_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
  69. int kvm_inject_nested_serror(struct kvm_vcpu *vcpu, u64 esr);
  70. static inline void kvm_inject_nested_sve_trap(struct kvm_vcpu *vcpu)
  71. {
  72. u64 esr = FIELD_PREP(ESR_ELx_EC_MASK, ESR_ELx_EC_SVE) |
  73. ESR_ELx_IL;
  74. kvm_inject_nested_sync(vcpu, esr);
  75. }
  76. #if defined(__KVM_VHE_HYPERVISOR__) || defined(__KVM_NVHE_HYPERVISOR__)
  77. static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
  78. {
  79. return !(vcpu->arch.hcr_el2 & HCR_RW);
  80. }
  81. #else
  82. static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
  83. {
  84. return vcpu_has_feature(vcpu, KVM_ARM_VCPU_EL1_32BIT);
  85. }
  86. #endif
  87. static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
  88. {
  89. if (!vcpu_has_run_once(vcpu))
  90. vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
  91. /*
  92. * For non-FWB CPUs, we trap VM ops (HCR_EL2.TVM) until M+C
  93. * get set in SCTLR_EL1 such that we can detect when the guest
  94. * MMU gets turned on and do the necessary cache maintenance
  95. * then.
  96. */
  97. if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
  98. vcpu->arch.hcr_el2 |= HCR_TVM;
  99. }
  100. static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
  101. {
  102. return (unsigned long *)&vcpu->arch.hcr_el2;
  103. }
  104. static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
  105. {
  106. return vcpu->arch.vsesr_el2;
  107. }
  108. static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
  109. {
  110. vcpu->arch.vsesr_el2 = vsesr;
  111. }
  112. static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
  113. {
  114. return (unsigned long *)&vcpu_gp_regs(vcpu)->pc;
  115. }
  116. static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
  117. {
  118. return (unsigned long *)&vcpu_gp_regs(vcpu)->pstate;
  119. }
  120. static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
  121. {
  122. return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
  123. }
  124. static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
  125. {
  126. if (vcpu_mode_is_32bit(vcpu))
  127. return kvm_condition_valid32(vcpu);
  128. return true;
  129. }
  130. static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
  131. {
  132. *vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT;
  133. }
  134. /*
  135. * vcpu_get_reg and vcpu_set_reg should always be passed a register number
  136. * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
  137. * AArch32 with banked registers.
  138. */
  139. static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
  140. u8 reg_num)
  141. {
  142. return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs[reg_num];
  143. }
  144. static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
  145. unsigned long val)
  146. {
  147. if (reg_num != 31)
  148. vcpu_gp_regs(vcpu)->regs[reg_num] = val;
  149. }
  150. static inline bool vcpu_is_el2_ctxt(const struct kvm_cpu_context *ctxt)
  151. {
  152. switch (ctxt->regs.pstate & (PSR_MODE32_BIT | PSR_MODE_MASK)) {
  153. case PSR_MODE_EL2h:
  154. case PSR_MODE_EL2t:
  155. return true;
  156. default:
  157. return false;
  158. }
  159. }
  160. static inline bool vcpu_is_el2(const struct kvm_vcpu *vcpu)
  161. {
  162. return vcpu_is_el2_ctxt(&vcpu->arch.ctxt);
  163. }
  164. static inline bool vcpu_el2_e2h_is_set(const struct kvm_vcpu *vcpu)
  165. {
  166. return (!cpus_have_final_cap(ARM64_HAS_HCR_NV1) ||
  167. (__vcpu_sys_reg(vcpu, HCR_EL2) & HCR_E2H));
  168. }
  169. static inline bool vcpu_el2_tge_is_set(const struct kvm_vcpu *vcpu)
  170. {
  171. return ctxt_sys_reg(&vcpu->arch.ctxt, HCR_EL2) & HCR_TGE;
  172. }
  173. static inline bool vcpu_el2_amo_is_set(const struct kvm_vcpu *vcpu)
  174. {
  175. /*
  176. * DDI0487L.b Known Issue D22105
  177. *
  178. * When executing at EL2 and HCR_EL2.{E2H,TGE} = {1, 0} it is
  179. * IMPLEMENTATION DEFINED whether the effective value of HCR_EL2.AMO
  180. * is the value programmed or 1.
  181. *
  182. * Make the implementation choice of treating the effective value as 1 as
  183. * we cannot subsequently catch changes to TGE or AMO that would
  184. * otherwise lead to the SError becoming deliverable.
  185. */
  186. if (vcpu_is_el2(vcpu) && vcpu_el2_e2h_is_set(vcpu) && !vcpu_el2_tge_is_set(vcpu))
  187. return true;
  188. return ctxt_sys_reg(&vcpu->arch.ctxt, HCR_EL2) & HCR_AMO;
  189. }
  190. static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
  191. {
  192. bool e2h, tge;
  193. u64 hcr;
  194. if (!vcpu_has_nv(vcpu))
  195. return false;
  196. hcr = __vcpu_sys_reg(vcpu, HCR_EL2);
  197. e2h = (hcr & HCR_E2H);
  198. tge = (hcr & HCR_TGE);
  199. /*
  200. * We are in a hypervisor context if the vcpu mode is EL2 or
  201. * E2H and TGE bits are set. The latter means we are in the user space
  202. * of the VHE kernel. ARMv8.1 ARM describes this as 'InHost'
  203. *
  204. * Note that the HCR_EL2.{E2H,TGE}={0,1} isn't really handled in the
  205. * rest of the KVM code, and will result in a misbehaving guest.
  206. */
  207. return vcpu_is_el2(vcpu) || (e2h && tge) || tge;
  208. }
  209. static inline bool vcpu_is_host_el0(const struct kvm_vcpu *vcpu)
  210. {
  211. return is_hyp_ctxt(vcpu) && !vcpu_is_el2(vcpu);
  212. }
  213. static inline bool is_nested_ctxt(struct kvm_vcpu *vcpu)
  214. {
  215. return vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu);
  216. }
  217. static inline bool vserror_state_is_nested(struct kvm_vcpu *vcpu)
  218. {
  219. if (!is_nested_ctxt(vcpu))
  220. return false;
  221. return vcpu_el2_amo_is_set(vcpu) ||
  222. (__vcpu_sys_reg(vcpu, HCRX_EL2) & HCRX_EL2_TMEA);
  223. }
  224. /*
  225. * The layout of SPSR for an AArch32 state is different when observed from an
  226. * AArch64 SPSR_ELx or an AArch32 SPSR_*. This function generates the AArch32
  227. * view given an AArch64 view.
  228. *
  229. * In ARM DDI 0487E.a see:
  230. *
  231. * - The AArch64 view (SPSR_EL2) in section C5.2.18, page C5-426
  232. * - The AArch32 view (SPSR_abt) in section G8.2.126, page G8-6256
  233. * - The AArch32 view (SPSR_und) in section G8.2.132, page G8-6280
  234. *
  235. * Which show the following differences:
  236. *
  237. * | Bit | AA64 | AA32 | Notes |
  238. * +-----+------+------+-----------------------------|
  239. * | 24 | DIT | J | J is RES0 in ARMv8 |
  240. * | 21 | SS | DIT | SS doesn't exist in AArch32 |
  241. *
  242. * ... and all other bits are (currently) common.
  243. */
  244. static inline unsigned long host_spsr_to_spsr32(unsigned long spsr)
  245. {
  246. const unsigned long overlap = BIT(24) | BIT(21);
  247. unsigned long dit = !!(spsr & PSR_AA32_DIT_BIT);
  248. spsr &= ~overlap;
  249. spsr |= dit << 21;
  250. return spsr;
  251. }
  252. static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
  253. {
  254. u32 mode;
  255. if (vcpu_mode_is_32bit(vcpu)) {
  256. mode = *vcpu_cpsr(vcpu) & PSR_AA32_MODE_MASK;
  257. return mode > PSR_AA32_MODE_USR;
  258. }
  259. mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
  260. return mode != PSR_MODE_EL0t;
  261. }
  262. static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
  263. {
  264. return vcpu->arch.fault.esr_el2;
  265. }
  266. static inline bool guest_hyp_wfx_traps_enabled(const struct kvm_vcpu *vcpu)
  267. {
  268. u64 esr = kvm_vcpu_get_esr(vcpu);
  269. bool is_wfe = !!(esr & ESR_ELx_WFx_ISS_WFE);
  270. u64 hcr_el2 = __vcpu_sys_reg(vcpu, HCR_EL2);
  271. if (!vcpu_has_nv(vcpu) || vcpu_is_el2(vcpu))
  272. return false;
  273. return ((is_wfe && (hcr_el2 & HCR_TWE)) ||
  274. (!is_wfe && (hcr_el2 & HCR_TWI)));
  275. }
  276. static __always_inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
  277. {
  278. u64 esr = kvm_vcpu_get_esr(vcpu);
  279. if (esr & ESR_ELx_CV)
  280. return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
  281. return -1;
  282. }
  283. static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
  284. {
  285. return vcpu->arch.fault.far_el2;
  286. }
  287. static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
  288. {
  289. u64 hpfar = vcpu->arch.fault.hpfar_el2;
  290. if (unlikely(!(hpfar & HPFAR_EL2_NS)))
  291. return INVALID_GPA;
  292. return FIELD_GET(HPFAR_EL2_FIPA, hpfar) << 12;
  293. }
  294. static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
  295. {
  296. return vcpu->arch.fault.disr_el1;
  297. }
  298. static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
  299. {
  300. return kvm_vcpu_get_esr(vcpu) & ESR_ELx_xVC_IMM_MASK;
  301. }
  302. static __always_inline bool kvm_vcpu_dabt_isvalid(const struct kvm_vcpu *vcpu)
  303. {
  304. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_ISV);
  305. }
  306. static inline unsigned long kvm_vcpu_dabt_iss_nisv_sanitized(const struct kvm_vcpu *vcpu)
  307. {
  308. return kvm_vcpu_get_esr(vcpu) & (ESR_ELx_CM | ESR_ELx_WNR | ESR_ELx_FSC);
  309. }
  310. static inline bool kvm_vcpu_dabt_issext(const struct kvm_vcpu *vcpu)
  311. {
  312. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SSE);
  313. }
  314. static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
  315. {
  316. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_SF);
  317. }
  318. static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu)
  319. {
  320. return (kvm_vcpu_get_esr(vcpu) & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT;
  321. }
  322. static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu)
  323. {
  324. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_S1PTW);
  325. }
  326. /* Always check for S1PTW *before* using this. */
  327. static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu)
  328. {
  329. return kvm_vcpu_get_esr(vcpu) & ESR_ELx_WNR;
  330. }
  331. static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu)
  332. {
  333. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_CM);
  334. }
  335. static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)
  336. {
  337. return 1 << ((kvm_vcpu_get_esr(vcpu) & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT);
  338. }
  339. /* This one is not specific to Data Abort */
  340. static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu)
  341. {
  342. return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_IL);
  343. }
  344. static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
  345. {
  346. return ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
  347. }
  348. static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
  349. {
  350. return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW;
  351. }
  352. static inline bool kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu *vcpu)
  353. {
  354. return kvm_vcpu_trap_is_iabt(vcpu) && !kvm_vcpu_abt_iss1tw(vcpu);
  355. }
  356. static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
  357. {
  358. return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC;
  359. }
  360. static inline
  361. bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
  362. {
  363. return esr_fsc_is_permission_fault(kvm_vcpu_get_esr(vcpu));
  364. }
  365. static inline
  366. bool kvm_vcpu_trap_is_translation_fault(const struct kvm_vcpu *vcpu)
  367. {
  368. return esr_fsc_is_translation_fault(kvm_vcpu_get_esr(vcpu));
  369. }
  370. static inline
  371. u64 kvm_vcpu_trap_get_perm_fault_granule(const struct kvm_vcpu *vcpu)
  372. {
  373. unsigned long esr = kvm_vcpu_get_esr(vcpu);
  374. BUG_ON(!esr_fsc_is_permission_fault(esr));
  375. return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(esr & ESR_ELx_FSC_LEVEL));
  376. }
  377. static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
  378. {
  379. switch (kvm_vcpu_trap_get_fault(vcpu)) {
  380. case ESR_ELx_FSC_EXTABT:
  381. case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3):
  382. case ESR_ELx_FSC_SECC:
  383. case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3):
  384. return true;
  385. default:
  386. return false;
  387. }
  388. }
  389. static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
  390. {
  391. u64 esr = kvm_vcpu_get_esr(vcpu);
  392. return ESR_ELx_SYS64_ISS_RT(esr);
  393. }
  394. static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  395. {
  396. if (kvm_vcpu_abt_iss1tw(vcpu)) {
  397. /*
  398. * Only a permission fault on a S1PTW should be
  399. * considered as a write. Otherwise, page tables baked
  400. * in a read-only memslot will result in an exception
  401. * being delivered in the guest.
  402. *
  403. * The drawback is that we end-up faulting twice if the
  404. * guest is using any of HW AF/DB: a translation fault
  405. * to map the page containing the PT (read only at
  406. * first), then a permission fault to allow the flags
  407. * to be set.
  408. */
  409. return kvm_vcpu_trap_is_permission_fault(vcpu);
  410. }
  411. if (kvm_vcpu_trap_is_iabt(vcpu))
  412. return false;
  413. return kvm_vcpu_dabt_iswrite(vcpu);
  414. }
  415. static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
  416. {
  417. return __vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
  418. }
  419. static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
  420. {
  421. if (vcpu_mode_is_32bit(vcpu)) {
  422. *vcpu_cpsr(vcpu) |= PSR_AA32_E_BIT;
  423. } else {
  424. enum vcpu_sysreg r;
  425. u64 sctlr;
  426. r = vcpu_has_nv(vcpu) ? SCTLR_EL2 : SCTLR_EL1;
  427. sctlr = vcpu_read_sys_reg(vcpu, r);
  428. sctlr |= SCTLR_ELx_EE;
  429. vcpu_write_sys_reg(vcpu, sctlr, r);
  430. }
  431. }
  432. static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
  433. {
  434. enum vcpu_sysreg r;
  435. u64 bit;
  436. if (vcpu_mode_is_32bit(vcpu))
  437. return !!(*vcpu_cpsr(vcpu) & PSR_AA32_E_BIT);
  438. r = is_hyp_ctxt(vcpu) ? SCTLR_EL2 : SCTLR_EL1;
  439. bit = vcpu_mode_priv(vcpu) ? SCTLR_ELx_EE : SCTLR_EL1_E0E;
  440. return vcpu_read_sys_reg(vcpu, r) & bit;
  441. }
  442. static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
  443. unsigned long data,
  444. unsigned int len)
  445. {
  446. if (kvm_vcpu_is_be(vcpu)) {
  447. switch (len) {
  448. case 1:
  449. return data & 0xff;
  450. case 2:
  451. return be16_to_cpu(data & 0xffff);
  452. case 4:
  453. return be32_to_cpu(data & 0xffffffff);
  454. default:
  455. return be64_to_cpu(data);
  456. }
  457. } else {
  458. switch (len) {
  459. case 1:
  460. return data & 0xff;
  461. case 2:
  462. return le16_to_cpu(data & 0xffff);
  463. case 4:
  464. return le32_to_cpu(data & 0xffffffff);
  465. default:
  466. return le64_to_cpu(data);
  467. }
  468. }
  469. return data; /* Leave LE untouched */
  470. }
  471. static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
  472. unsigned long data,
  473. unsigned int len)
  474. {
  475. if (kvm_vcpu_is_be(vcpu)) {
  476. switch (len) {
  477. case 1:
  478. return data & 0xff;
  479. case 2:
  480. return cpu_to_be16(data & 0xffff);
  481. case 4:
  482. return cpu_to_be32(data & 0xffffffff);
  483. default:
  484. return cpu_to_be64(data);
  485. }
  486. } else {
  487. switch (len) {
  488. case 1:
  489. return data & 0xff;
  490. case 2:
  491. return cpu_to_le16(data & 0xffff);
  492. case 4:
  493. return cpu_to_le32(data & 0xffffffff);
  494. default:
  495. return cpu_to_le64(data);
  496. }
  497. }
  498. return data; /* Leave LE untouched */
  499. }
  500. static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)
  501. {
  502. WARN_ON(vcpu_get_flag(vcpu, PENDING_EXCEPTION));
  503. vcpu_set_flag(vcpu, INCREMENT_PC);
  504. }
  505. #define kvm_pend_exception(v, e) \
  506. do { \
  507. WARN_ON(vcpu_get_flag((v), INCREMENT_PC)); \
  508. vcpu_set_flag((v), PENDING_EXCEPTION); \
  509. vcpu_set_flag((v), e); \
  510. } while (0)
  511. /*
  512. * Returns a 'sanitised' view of CPTR_EL2, translating from nVHE to the VHE
  513. * format if E2H isn't set.
  514. */
  515. static inline u64 vcpu_sanitised_cptr_el2(const struct kvm_vcpu *vcpu)
  516. {
  517. u64 cptr = __vcpu_sys_reg(vcpu, CPTR_EL2);
  518. if (!vcpu_el2_e2h_is_set(vcpu))
  519. cptr = translate_cptr_el2_to_cpacr_el1(cptr);
  520. return cptr;
  521. }
  522. static inline bool ____cptr_xen_trap_enabled(const struct kvm_vcpu *vcpu,
  523. unsigned int xen)
  524. {
  525. switch (xen) {
  526. case 0b00:
  527. case 0b10:
  528. return true;
  529. case 0b01:
  530. return vcpu_el2_tge_is_set(vcpu) && !vcpu_is_el2(vcpu);
  531. case 0b11:
  532. default:
  533. return false;
  534. }
  535. }
  536. #define __guest_hyp_cptr_xen_trap_enabled(vcpu, xen) \
  537. (!vcpu_has_nv(vcpu) ? false : \
  538. ____cptr_xen_trap_enabled(vcpu, \
  539. SYS_FIELD_GET(CPACR_EL1, xen, \
  540. vcpu_sanitised_cptr_el2(vcpu))))
  541. static inline bool guest_hyp_fpsimd_traps_enabled(const struct kvm_vcpu *vcpu)
  542. {
  543. return __guest_hyp_cptr_xen_trap_enabled(vcpu, FPEN);
  544. }
  545. static inline bool guest_hyp_sve_traps_enabled(const struct kvm_vcpu *vcpu)
  546. {
  547. return __guest_hyp_cptr_xen_trap_enabled(vcpu, ZEN);
  548. }
  549. static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
  550. {
  551. struct kvm *kvm = vcpu->kvm;
  552. if (cpus_have_final_cap(ARM64_HAS_HCX)) {
  553. /*
  554. * In general, all HCRX_EL2 bits are gated by a feature.
  555. * The only reason we can set SMPME without checking any
  556. * feature is that its effects are not directly observable
  557. * from the guest.
  558. */
  559. vcpu->arch.hcrx_el2 = HCRX_EL2_SMPME;
  560. if (kvm_has_feat(kvm, ID_AA64ISAR2_EL1, MOPS, IMP))
  561. vcpu->arch.hcrx_el2 |= (HCRX_EL2_MSCEn | HCRX_EL2_MCE2);
  562. if (kvm_has_tcr2(kvm))
  563. vcpu->arch.hcrx_el2 |= HCRX_EL2_TCR2En;
  564. if (kvm_has_fpmr(kvm))
  565. vcpu->arch.hcrx_el2 |= HCRX_EL2_EnFPM;
  566. if (kvm_has_sctlr2(kvm))
  567. vcpu->arch.hcrx_el2 |= HCRX_EL2_SCTLR2En;
  568. if (kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64))
  569. vcpu->arch.hcrx_el2 |= HCRX_EL2_EnALS;
  570. if (kvm_has_feat(kvm, ID_AA64ISAR1_EL1, LS64, LS64_V))
  571. vcpu->arch.hcrx_el2 |= HCRX_EL2_EnASR;
  572. }
  573. }
  574. #endif /* __ARM64_KVM_EMULATE_H__ */