inject_fault.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Fault injection for both 32 and 64bit guests.
  3. *
  4. * Copyright (C) 2012,2013 - ARM Ltd
  5. * Author: Marc Zyngier <marc.zyngier@arm.com>
  6. *
  7. * Based on arch/arm/kvm/emulate.c
  8. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  9. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <linux/kvm_host.h>
  24. #include <asm/kvm_emulate.h>
  25. #include <asm/esr.h>
  26. #define PSTATE_FAULT_BITS_64 (PSR_MODE_EL1h | PSR_A_BIT | PSR_F_BIT | \
  27. PSR_I_BIT | PSR_D_BIT)
  28. #define EL1_EXCEPT_SYNC_OFFSET 0x200
  29. static void prepare_fault32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
  30. {
  31. unsigned long cpsr;
  32. unsigned long new_spsr_value = *vcpu_cpsr(vcpu);
  33. bool is_thumb = (new_spsr_value & COMPAT_PSR_T_BIT);
  34. u32 return_offset = (is_thumb) ? 4 : 0;
  35. u32 sctlr = vcpu_cp15(vcpu, c1_SCTLR);
  36. cpsr = mode | COMPAT_PSR_I_BIT;
  37. if (sctlr & (1 << 30))
  38. cpsr |= COMPAT_PSR_T_BIT;
  39. if (sctlr & (1 << 25))
  40. cpsr |= COMPAT_PSR_E_BIT;
  41. *vcpu_cpsr(vcpu) = cpsr;
  42. /* Note: These now point to the banked copies */
  43. *vcpu_spsr(vcpu) = new_spsr_value;
  44. *vcpu_reg32(vcpu, 14) = *vcpu_pc(vcpu) + return_offset;
  45. /* Branch to exception vector */
  46. if (sctlr & (1 << 13))
  47. vect_offset += 0xffff0000;
  48. else /* always have security exceptions */
  49. vect_offset += vcpu_cp15(vcpu, c12_VBAR);
  50. *vcpu_pc(vcpu) = vect_offset;
  51. }
  52. static void inject_undef32(struct kvm_vcpu *vcpu)
  53. {
  54. prepare_fault32(vcpu, COMPAT_PSR_MODE_UND, 4);
  55. }
  56. /*
  57. * Modelled after TakeDataAbortException() and TakePrefetchAbortException
  58. * pseudocode.
  59. */
  60. static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt,
  61. unsigned long addr)
  62. {
  63. u32 vect_offset;
  64. u32 *far, *fsr;
  65. bool is_lpae;
  66. if (is_pabt) {
  67. vect_offset = 12;
  68. far = &vcpu_cp15(vcpu, c6_IFAR);
  69. fsr = &vcpu_cp15(vcpu, c5_IFSR);
  70. } else { /* !iabt */
  71. vect_offset = 16;
  72. far = &vcpu_cp15(vcpu, c6_DFAR);
  73. fsr = &vcpu_cp15(vcpu, c5_DFSR);
  74. }
  75. prepare_fault32(vcpu, COMPAT_PSR_MODE_ABT | COMPAT_PSR_A_BIT, vect_offset);
  76. *far = addr;
  77. /* Give the guest an IMPLEMENTATION DEFINED exception */
  78. is_lpae = (vcpu_cp15(vcpu, c2_TTBCR) >> 31);
  79. if (is_lpae)
  80. *fsr = 1 << 9 | 0x34;
  81. else
  82. *fsr = 0x14;
  83. }
  84. static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr)
  85. {
  86. unsigned long cpsr = *vcpu_cpsr(vcpu);
  87. bool is_aarch32;
  88. u32 esr = 0;
  89. is_aarch32 = vcpu_mode_is_32bit(vcpu);
  90. *vcpu_spsr(vcpu) = cpsr;
  91. *vcpu_elr_el1(vcpu) = *vcpu_pc(vcpu);
  92. *vcpu_cpsr(vcpu) = PSTATE_FAULT_BITS_64;
  93. *vcpu_pc(vcpu) = vcpu_sys_reg(vcpu, VBAR_EL1) + EL1_EXCEPT_SYNC_OFFSET;
  94. vcpu_sys_reg(vcpu, FAR_EL1) = addr;
  95. /*
  96. * Build an {i,d}abort, depending on the level and the
  97. * instruction set. Report an external synchronous abort.
  98. */
  99. if (kvm_vcpu_trap_il_is32bit(vcpu))
  100. esr |= ESR_ELx_IL;
  101. /*
  102. * Here, the guest runs in AArch64 mode when in EL1. If we get
  103. * an AArch32 fault, it means we managed to trap an EL0 fault.
  104. */
  105. if (is_aarch32 || (cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
  106. esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT);
  107. else
  108. esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT);
  109. if (!is_iabt)
  110. esr |= ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT;
  111. vcpu_sys_reg(vcpu, ESR_EL1) = esr | ESR_ELx_FSC_EXTABT;
  112. }
  113. static void inject_undef64(struct kvm_vcpu *vcpu)
  114. {
  115. unsigned long cpsr = *vcpu_cpsr(vcpu);
  116. u32 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
  117. *vcpu_spsr(vcpu) = cpsr;
  118. *vcpu_elr_el1(vcpu) = *vcpu_pc(vcpu);
  119. *vcpu_cpsr(vcpu) = PSTATE_FAULT_BITS_64;
  120. *vcpu_pc(vcpu) = vcpu_sys_reg(vcpu, VBAR_EL1) + EL1_EXCEPT_SYNC_OFFSET;
  121. /*
  122. * Build an unknown exception, depending on the instruction
  123. * set.
  124. */
  125. if (kvm_vcpu_trap_il_is32bit(vcpu))
  126. esr |= ESR_ELx_IL;
  127. vcpu_sys_reg(vcpu, ESR_EL1) = esr;
  128. }
  129. /**
  130. * kvm_inject_dabt - inject a data abort into the guest
  131. * @vcpu: The VCPU to receive the undefined exception
  132. * @addr: The address to report in the DFAR
  133. *
  134. * It is assumed that this code is called from the VCPU thread and that the
  135. * VCPU therefore is not currently executing guest code.
  136. */
  137. void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
  138. {
  139. if (!(vcpu->arch.hcr_el2 & HCR_RW))
  140. inject_abt32(vcpu, false, addr);
  141. else
  142. inject_abt64(vcpu, false, addr);
  143. }
  144. /**
  145. * kvm_inject_pabt - inject a prefetch abort into the guest
  146. * @vcpu: The VCPU to receive the undefined exception
  147. * @addr: The address to report in the DFAR
  148. *
  149. * It is assumed that this code is called from the VCPU thread and that the
  150. * VCPU therefore is not currently executing guest code.
  151. */
  152. void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
  153. {
  154. if (!(vcpu->arch.hcr_el2 & HCR_RW))
  155. inject_abt32(vcpu, true, addr);
  156. else
  157. inject_abt64(vcpu, true, addr);
  158. }
  159. /**
  160. * kvm_inject_undefined - inject an undefined instruction into the guest
  161. *
  162. * It is assumed that this code is called from the VCPU thread and that the
  163. * VCPU therefore is not currently executing guest code.
  164. */
  165. void kvm_inject_undefined(struct kvm_vcpu *vcpu)
  166. {
  167. if (!(vcpu->arch.hcr_el2 & HCR_RW))
  168. inject_undef32(vcpu);
  169. else
  170. inject_undef64(vcpu);
  171. }