inject_fault.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <linux/kvm_host.h>
  13. #include <asm/kvm_emulate.h>
  14. #include <asm/esr.h>
  15. static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr)
  16. {
  17. unsigned long cpsr = *vcpu_cpsr(vcpu);
  18. bool is_aarch32 = vcpu_mode_is_32bit(vcpu);
  19. u64 esr = 0;
  20. kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
  21. vcpu_write_sys_reg(vcpu, addr, FAR_EL1);
  22. /*
  23. * Build an {i,d}abort, depending on the level and the
  24. * instruction set. Report an external synchronous abort.
  25. */
  26. if (kvm_vcpu_trap_il_is32bit(vcpu))
  27. esr |= ESR_ELx_IL;
  28. /*
  29. * Here, the guest runs in AArch64 mode when in EL1. If we get
  30. * an AArch32 fault, it means we managed to trap an EL0 fault.
  31. */
  32. if (is_aarch32 || (cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
  33. esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT);
  34. else
  35. esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT);
  36. if (!is_iabt)
  37. esr |= ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT;
  38. vcpu_write_sys_reg(vcpu, esr | ESR_ELx_FSC_EXTABT, ESR_EL1);
  39. }
  40. static void inject_undef64(struct kvm_vcpu *vcpu)
  41. {
  42. u64 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
  43. kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
  44. /*
  45. * Build an unknown exception, depending on the instruction
  46. * set.
  47. */
  48. if (kvm_vcpu_trap_il_is32bit(vcpu))
  49. esr |= ESR_ELx_IL;
  50. vcpu_write_sys_reg(vcpu, esr, ESR_EL1);
  51. }
  52. #define DFSR_FSC_EXTABT_LPAE 0x10
  53. #define DFSR_FSC_EXTABT_nLPAE 0x08
  54. #define DFSR_LPAE BIT(9)
  55. #define TTBCR_EAE BIT(31)
  56. static void inject_undef32(struct kvm_vcpu *vcpu)
  57. {
  58. kvm_pend_exception(vcpu, EXCEPT_AA32_UND);
  59. }
  60. /*
  61. * Modelled after TakeDataAbortException() and TakePrefetchAbortException
  62. * pseudocode.
  63. */
  64. static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt, u32 addr)
  65. {
  66. u64 far;
  67. u32 fsr;
  68. /* Give the guest an IMPLEMENTATION DEFINED exception */
  69. if (vcpu_read_sys_reg(vcpu, TCR_EL1) & TTBCR_EAE) {
  70. fsr = DFSR_LPAE | DFSR_FSC_EXTABT_LPAE;
  71. } else {
  72. /* no need to shuffle FS[4] into DFSR[10] as its 0 */
  73. fsr = DFSR_FSC_EXTABT_nLPAE;
  74. }
  75. far = vcpu_read_sys_reg(vcpu, FAR_EL1);
  76. if (is_pabt) {
  77. kvm_pend_exception(vcpu, EXCEPT_AA32_IABT);
  78. far &= GENMASK(31, 0);
  79. far |= (u64)addr << 32;
  80. vcpu_write_sys_reg(vcpu, fsr, IFSR32_EL2);
  81. } else { /* !iabt */
  82. kvm_pend_exception(vcpu, EXCEPT_AA32_DABT);
  83. far &= GENMASK(63, 32);
  84. far |= addr;
  85. vcpu_write_sys_reg(vcpu, fsr, ESR_EL1);
  86. }
  87. vcpu_write_sys_reg(vcpu, far, FAR_EL1);
  88. }
  89. /**
  90. * kvm_inject_dabt - inject a data abort into the guest
  91. * @vcpu: The VCPU to receive the data abort
  92. * @addr: The address to report in the DFAR
  93. *
  94. * It is assumed that this code is called from the VCPU thread and that the
  95. * VCPU therefore is not currently executing guest code.
  96. */
  97. void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
  98. {
  99. if (vcpu_el1_is_32bit(vcpu))
  100. inject_abt32(vcpu, false, addr);
  101. else
  102. inject_abt64(vcpu, false, addr);
  103. }
  104. /**
  105. * kvm_inject_pabt - inject a prefetch abort into the guest
  106. * @vcpu: The VCPU to receive the prefetch abort
  107. * @addr: The address to report in the DFAR
  108. *
  109. * It is assumed that this code is called from the VCPU thread and that the
  110. * VCPU therefore is not currently executing guest code.
  111. */
  112. void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
  113. {
  114. if (vcpu_el1_is_32bit(vcpu))
  115. inject_abt32(vcpu, true, addr);
  116. else
  117. inject_abt64(vcpu, true, addr);
  118. }
  119. void kvm_inject_size_fault(struct kvm_vcpu *vcpu)
  120. {
  121. unsigned long addr, esr;
  122. addr = kvm_vcpu_get_fault_ipa(vcpu);
  123. addr |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0);
  124. if (kvm_vcpu_trap_is_iabt(vcpu))
  125. kvm_inject_pabt(vcpu, addr);
  126. else
  127. kvm_inject_dabt(vcpu, addr);
  128. /*
  129. * If AArch64 or LPAE, set FSC to 0 to indicate an Address
  130. * Size Fault at level 0, as if exceeding PARange.
  131. *
  132. * Non-LPAE guests will only get the external abort, as there
  133. * is no way to to describe the ASF.
  134. */
  135. if (vcpu_el1_is_32bit(vcpu) &&
  136. !(vcpu_read_sys_reg(vcpu, TCR_EL1) & TTBCR_EAE))
  137. return;
  138. esr = vcpu_read_sys_reg(vcpu, ESR_EL1);
  139. esr &= ~GENMASK_ULL(5, 0);
  140. vcpu_write_sys_reg(vcpu, esr, ESR_EL1);
  141. }
  142. /**
  143. * kvm_inject_undefined - inject an undefined instruction into the guest
  144. * @vcpu: The vCPU in which to inject the exception
  145. *
  146. * It is assumed that this code is called from the VCPU thread and that the
  147. * VCPU therefore is not currently executing guest code.
  148. */
  149. void kvm_inject_undefined(struct kvm_vcpu *vcpu)
  150. {
  151. if (vcpu_el1_is_32bit(vcpu))
  152. inject_undef32(vcpu);
  153. else
  154. inject_undef64(vcpu);
  155. }
  156. void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 esr)
  157. {
  158. vcpu_set_vsesr(vcpu, esr & ESR_ELx_ISS_MASK);
  159. *vcpu_hcr(vcpu) |= HCR_VSE;
  160. }
  161. /**
  162. * kvm_inject_vabt - inject an async abort / SError into the guest
  163. * @vcpu: The VCPU to receive the exception
  164. *
  165. * It is assumed that this code is called from the VCPU thread and that the
  166. * VCPU therefore is not currently executing guest code.
  167. *
  168. * Systems with the RAS Extensions specify an imp-def ESR (ISV/IDS = 1) with
  169. * the remaining ISS all-zeros so that this error is not interpreted as an
  170. * uncategorized RAS error. Without the RAS Extensions we can't specify an ESR
  171. * value, so the CPU generates an imp-def value.
  172. */
  173. void kvm_inject_vabt(struct kvm_vcpu *vcpu)
  174. {
  175. kvm_set_sei_esr(vcpu, ESR_ELx_ISV);
  176. }