exception.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Fault injection for both 32 and 64bit guests.
  4. *
  5. * Copyright (C) 2012,2013 - ARM Ltd
  6. * Author: Marc Zyngier <marc.zyngier@arm.com>
  7. *
  8. * Based on arch/arm/kvm/emulate.c
  9. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  10. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  11. */
  12. #include <hyp/adjust_pc.h>
  13. #include <linux/kvm_host.h>
  14. #include <asm/kvm_emulate.h>
  15. #include <asm/kvm_mmu.h>
  16. #include <asm/kvm_nested.h>
  17. #if !defined (__KVM_NVHE_HYPERVISOR__) && !defined (__KVM_VHE_HYPERVISOR__)
  18. #error Hypervisor code only!
  19. #endif
  20. static inline u64 __vcpu_read_sys_reg(const struct kvm_vcpu *vcpu, int reg)
  21. {
  22. u64 val;
  23. if (unlikely(vcpu_has_nv(vcpu)))
  24. return vcpu_read_sys_reg(vcpu, reg);
  25. else if (__vcpu_read_sys_reg_from_cpu(reg, &val))
  26. return val;
  27. return __vcpu_sys_reg(vcpu, reg);
  28. }
  29. static inline void __vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg)
  30. {
  31. if (unlikely(vcpu_has_nv(vcpu)))
  32. vcpu_write_sys_reg(vcpu, val, reg);
  33. else if (!__vcpu_write_sys_reg_to_cpu(val, reg))
  34. __vcpu_sys_reg(vcpu, reg) = val;
  35. }
  36. static void __vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long target_mode,
  37. u64 val)
  38. {
  39. if (unlikely(vcpu_has_nv(vcpu))) {
  40. if (target_mode == PSR_MODE_EL1h)
  41. vcpu_write_sys_reg(vcpu, val, SPSR_EL1);
  42. else
  43. vcpu_write_sys_reg(vcpu, val, SPSR_EL2);
  44. } else if (has_vhe()) {
  45. write_sysreg_el1(val, SYS_SPSR);
  46. } else {
  47. __vcpu_sys_reg(vcpu, SPSR_EL1) = val;
  48. }
  49. }
  50. static void __vcpu_write_spsr_abt(struct kvm_vcpu *vcpu, u64 val)
  51. {
  52. if (has_vhe())
  53. write_sysreg(val, spsr_abt);
  54. else
  55. vcpu->arch.ctxt.spsr_abt = val;
  56. }
  57. static void __vcpu_write_spsr_und(struct kvm_vcpu *vcpu, u64 val)
  58. {
  59. if (has_vhe())
  60. write_sysreg(val, spsr_und);
  61. else
  62. vcpu->arch.ctxt.spsr_und = val;
  63. }
  64. /*
  65. * This performs the exception entry at a given EL (@target_mode), stashing PC
  66. * and PSTATE into ELR and SPSR respectively, and compute the new PC/PSTATE.
  67. * The EL passed to this function *must* be a non-secure, privileged mode with
  68. * bit 0 being set (PSTATE.SP == 1).
  69. *
  70. * When an exception is taken, most PSTATE fields are left unchanged in the
  71. * handler. However, some are explicitly overridden (e.g. M[4:0]). Luckily all
  72. * of the inherited bits have the same position in the AArch64/AArch32 SPSR_ELx
  73. * layouts, so we don't need to shuffle these for exceptions from AArch32 EL0.
  74. *
  75. * For the SPSR_ELx layout for AArch64, see ARM DDI 0487E.a page C5-429.
  76. * For the SPSR_ELx layout for AArch32, see ARM DDI 0487E.a page C5-426.
  77. *
  78. * Here we manipulate the fields in order of the AArch64 SPSR_ELx layout, from
  79. * MSB to LSB.
  80. */
  81. static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode,
  82. enum exception_type type)
  83. {
  84. unsigned long sctlr, vbar, old, new, mode;
  85. u64 exc_offset;
  86. mode = *vcpu_cpsr(vcpu) & (PSR_MODE_MASK | PSR_MODE32_BIT);
  87. if (mode == target_mode)
  88. exc_offset = CURRENT_EL_SP_ELx_VECTOR;
  89. else if ((mode | PSR_MODE_THREAD_BIT) == target_mode)
  90. exc_offset = CURRENT_EL_SP_EL0_VECTOR;
  91. else if (!(mode & PSR_MODE32_BIT))
  92. exc_offset = LOWER_EL_AArch64_VECTOR;
  93. else
  94. exc_offset = LOWER_EL_AArch32_VECTOR;
  95. switch (target_mode) {
  96. case PSR_MODE_EL1h:
  97. vbar = __vcpu_read_sys_reg(vcpu, VBAR_EL1);
  98. sctlr = __vcpu_read_sys_reg(vcpu, SCTLR_EL1);
  99. __vcpu_write_sys_reg(vcpu, *vcpu_pc(vcpu), ELR_EL1);
  100. break;
  101. case PSR_MODE_EL2h:
  102. vbar = __vcpu_read_sys_reg(vcpu, VBAR_EL2);
  103. sctlr = __vcpu_read_sys_reg(vcpu, SCTLR_EL2);
  104. __vcpu_write_sys_reg(vcpu, *vcpu_pc(vcpu), ELR_EL2);
  105. break;
  106. default:
  107. /* Don't do that */
  108. BUG();
  109. }
  110. *vcpu_pc(vcpu) = vbar + exc_offset + type;
  111. old = *vcpu_cpsr(vcpu);
  112. new = 0;
  113. new |= (old & PSR_N_BIT);
  114. new |= (old & PSR_Z_BIT);
  115. new |= (old & PSR_C_BIT);
  116. new |= (old & PSR_V_BIT);
  117. if (kvm_has_mte(kern_hyp_va(vcpu->kvm)))
  118. new |= PSR_TCO_BIT;
  119. new |= (old & PSR_DIT_BIT);
  120. // PSTATE.UAO is set to zero upon any exception to AArch64
  121. // See ARM DDI 0487E.a, page D5-2579.
  122. // PSTATE.PAN is unchanged unless SCTLR_ELx.SPAN == 0b0
  123. // SCTLR_ELx.SPAN is RES1 when ARMv8.1-PAN is not implemented
  124. // See ARM DDI 0487E.a, page D5-2578.
  125. new |= (old & PSR_PAN_BIT);
  126. if (!(sctlr & SCTLR_EL1_SPAN))
  127. new |= PSR_PAN_BIT;
  128. // PSTATE.SS is set to zero upon any exception to AArch64
  129. // See ARM DDI 0487E.a, page D2-2452.
  130. // PSTATE.IL is set to zero upon any exception to AArch64
  131. // See ARM DDI 0487E.a, page D1-2306.
  132. // PSTATE.SSBS is set to SCTLR_ELx.DSSBS upon any exception to AArch64
  133. // See ARM DDI 0487E.a, page D13-3258
  134. if (sctlr & SCTLR_ELx_DSSBS)
  135. new |= PSR_SSBS_BIT;
  136. // PSTATE.BTYPE is set to zero upon any exception to AArch64
  137. // See ARM DDI 0487E.a, pages D1-2293 to D1-2294.
  138. new |= PSR_D_BIT;
  139. new |= PSR_A_BIT;
  140. new |= PSR_I_BIT;
  141. new |= PSR_F_BIT;
  142. new |= target_mode;
  143. *vcpu_cpsr(vcpu) = new;
  144. __vcpu_write_spsr(vcpu, target_mode, old);
  145. }
  146. /*
  147. * When an exception is taken, most CPSR fields are left unchanged in the
  148. * handler. However, some are explicitly overridden (e.g. M[4:0]).
  149. *
  150. * The SPSR/SPSR_ELx layouts differ, and the below is intended to work with
  151. * either format. Note: SPSR.J bit doesn't exist in SPSR_ELx, but this bit was
  152. * obsoleted by the ARMv7 virtualization extensions and is RES0.
  153. *
  154. * For the SPSR layout seen from AArch32, see:
  155. * - ARM DDI 0406C.d, page B1-1148
  156. * - ARM DDI 0487E.a, page G8-6264
  157. *
  158. * For the SPSR_ELx layout for AArch32 seen from AArch64, see:
  159. * - ARM DDI 0487E.a, page C5-426
  160. *
  161. * Here we manipulate the fields in order of the AArch32 SPSR_ELx layout, from
  162. * MSB to LSB.
  163. */
  164. static unsigned long get_except32_cpsr(struct kvm_vcpu *vcpu, u32 mode)
  165. {
  166. u32 sctlr = __vcpu_read_sys_reg(vcpu, SCTLR_EL1);
  167. unsigned long old, new;
  168. old = *vcpu_cpsr(vcpu);
  169. new = 0;
  170. new |= (old & PSR_AA32_N_BIT);
  171. new |= (old & PSR_AA32_Z_BIT);
  172. new |= (old & PSR_AA32_C_BIT);
  173. new |= (old & PSR_AA32_V_BIT);
  174. new |= (old & PSR_AA32_Q_BIT);
  175. // CPSR.IT[7:0] are set to zero upon any exception
  176. // See ARM DDI 0487E.a, section G1.12.3
  177. // See ARM DDI 0406C.d, section B1.8.3
  178. new |= (old & PSR_AA32_DIT_BIT);
  179. // CPSR.SSBS is set to SCTLR.DSSBS upon any exception
  180. // See ARM DDI 0487E.a, page G8-6244
  181. if (sctlr & BIT(31))
  182. new |= PSR_AA32_SSBS_BIT;
  183. // CPSR.PAN is unchanged unless SCTLR.SPAN == 0b0
  184. // SCTLR.SPAN is RES1 when ARMv8.1-PAN is not implemented
  185. // See ARM DDI 0487E.a, page G8-6246
  186. new |= (old & PSR_AA32_PAN_BIT);
  187. if (!(sctlr & BIT(23)))
  188. new |= PSR_AA32_PAN_BIT;
  189. // SS does not exist in AArch32, so ignore
  190. // CPSR.IL is set to zero upon any exception
  191. // See ARM DDI 0487E.a, page G1-5527
  192. new |= (old & PSR_AA32_GE_MASK);
  193. // CPSR.IT[7:0] are set to zero upon any exception
  194. // See prior comment above
  195. // CPSR.E is set to SCTLR.EE upon any exception
  196. // See ARM DDI 0487E.a, page G8-6245
  197. // See ARM DDI 0406C.d, page B4-1701
  198. if (sctlr & BIT(25))
  199. new |= PSR_AA32_E_BIT;
  200. // CPSR.A is unchanged upon an exception to Undefined, Supervisor
  201. // CPSR.A is set upon an exception to other modes
  202. // See ARM DDI 0487E.a, pages G1-5515 to G1-5516
  203. // See ARM DDI 0406C.d, page B1-1182
  204. new |= (old & PSR_AA32_A_BIT);
  205. if (mode != PSR_AA32_MODE_UND && mode != PSR_AA32_MODE_SVC)
  206. new |= PSR_AA32_A_BIT;
  207. // CPSR.I is set upon any exception
  208. // See ARM DDI 0487E.a, pages G1-5515 to G1-5516
  209. // See ARM DDI 0406C.d, page B1-1182
  210. new |= PSR_AA32_I_BIT;
  211. // CPSR.F is set upon an exception to FIQ
  212. // CPSR.F is unchanged upon an exception to other modes
  213. // See ARM DDI 0487E.a, pages G1-5515 to G1-5516
  214. // See ARM DDI 0406C.d, page B1-1182
  215. new |= (old & PSR_AA32_F_BIT);
  216. if (mode == PSR_AA32_MODE_FIQ)
  217. new |= PSR_AA32_F_BIT;
  218. // CPSR.T is set to SCTLR.TE upon any exception
  219. // See ARM DDI 0487E.a, page G8-5514
  220. // See ARM DDI 0406C.d, page B1-1181
  221. if (sctlr & BIT(30))
  222. new |= PSR_AA32_T_BIT;
  223. new |= mode;
  224. return new;
  225. }
  226. /*
  227. * Table taken from ARMv8 ARM DDI0487B-B, table G1-10.
  228. */
  229. static const u8 return_offsets[8][2] = {
  230. [0] = { 0, 0 }, /* Reset, unused */
  231. [1] = { 4, 2 }, /* Undefined */
  232. [2] = { 0, 0 }, /* SVC, unused */
  233. [3] = { 4, 4 }, /* Prefetch abort */
  234. [4] = { 8, 8 }, /* Data abort */
  235. [5] = { 0, 0 }, /* HVC, unused */
  236. [6] = { 4, 4 }, /* IRQ, unused */
  237. [7] = { 4, 4 }, /* FIQ, unused */
  238. };
  239. static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
  240. {
  241. unsigned long spsr = *vcpu_cpsr(vcpu);
  242. bool is_thumb = (spsr & PSR_AA32_T_BIT);
  243. u32 sctlr = __vcpu_read_sys_reg(vcpu, SCTLR_EL1);
  244. u32 return_address;
  245. *vcpu_cpsr(vcpu) = get_except32_cpsr(vcpu, mode);
  246. return_address = *vcpu_pc(vcpu);
  247. return_address += return_offsets[vect_offset >> 2][is_thumb];
  248. /* KVM only enters the ABT and UND modes, so only deal with those */
  249. switch(mode) {
  250. case PSR_AA32_MODE_ABT:
  251. __vcpu_write_spsr_abt(vcpu, host_spsr_to_spsr32(spsr));
  252. vcpu_gp_regs(vcpu)->compat_lr_abt = return_address;
  253. break;
  254. case PSR_AA32_MODE_UND:
  255. __vcpu_write_spsr_und(vcpu, host_spsr_to_spsr32(spsr));
  256. vcpu_gp_regs(vcpu)->compat_lr_und = return_address;
  257. break;
  258. }
  259. /* Branch to exception vector */
  260. if (sctlr & (1 << 13))
  261. vect_offset += 0xffff0000;
  262. else /* always have security exceptions */
  263. vect_offset += __vcpu_read_sys_reg(vcpu, VBAR_EL1);
  264. *vcpu_pc(vcpu) = vect_offset;
  265. }
  266. static void kvm_inject_exception(struct kvm_vcpu *vcpu)
  267. {
  268. if (vcpu_el1_is_32bit(vcpu)) {
  269. switch (vcpu_get_flag(vcpu, EXCEPT_MASK)) {
  270. case unpack_vcpu_flag(EXCEPT_AA32_UND):
  271. enter_exception32(vcpu, PSR_AA32_MODE_UND, 4);
  272. break;
  273. case unpack_vcpu_flag(EXCEPT_AA32_IABT):
  274. enter_exception32(vcpu, PSR_AA32_MODE_ABT, 12);
  275. break;
  276. case unpack_vcpu_flag(EXCEPT_AA32_DABT):
  277. enter_exception32(vcpu, PSR_AA32_MODE_ABT, 16);
  278. break;
  279. default:
  280. /* Err... */
  281. break;
  282. }
  283. } else {
  284. switch (vcpu_get_flag(vcpu, EXCEPT_MASK)) {
  285. case unpack_vcpu_flag(EXCEPT_AA64_EL1_SYNC):
  286. enter_exception64(vcpu, PSR_MODE_EL1h, except_type_sync);
  287. break;
  288. case unpack_vcpu_flag(EXCEPT_AA64_EL2_SYNC):
  289. enter_exception64(vcpu, PSR_MODE_EL2h, except_type_sync);
  290. break;
  291. case unpack_vcpu_flag(EXCEPT_AA64_EL2_IRQ):
  292. enter_exception64(vcpu, PSR_MODE_EL2h, except_type_irq);
  293. break;
  294. default:
  295. /*
  296. * Only EL1_SYNC and EL2_{SYNC,IRQ} makes
  297. * sense so far. Everything else gets silently
  298. * ignored.
  299. */
  300. break;
  301. }
  302. }
  303. }
  304. /*
  305. * Adjust the guest PC (and potentially exception state) depending on
  306. * flags provided by the emulation code.
  307. */
  308. void __kvm_adjust_pc(struct kvm_vcpu *vcpu)
  309. {
  310. if (vcpu_get_flag(vcpu, PENDING_EXCEPTION)) {
  311. kvm_inject_exception(vcpu);
  312. vcpu_clear_flag(vcpu, PENDING_EXCEPTION);
  313. vcpu_clear_flag(vcpu, EXCEPT_MASK);
  314. } else if (vcpu_get_flag(vcpu, INCREMENT_PC)) {
  315. kvm_skip_instr(vcpu);
  316. vcpu_clear_flag(vcpu, INCREMENT_PC);
  317. }
  318. }