aarch32.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * (not much of an) Emulation layer for 32bit 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/kvm_hyp.h>
  26. /*
  27. * stolen from arch/arm/kernel/opcodes.c
  28. *
  29. * condition code lookup table
  30. * index into the table is test code: EQ, NE, ... LT, GT, AL, NV
  31. *
  32. * bit position in short is condition code: NZCV
  33. */
  34. static const unsigned short cc_map[16] = {
  35. 0xF0F0, /* EQ == Z set */
  36. 0x0F0F, /* NE */
  37. 0xCCCC, /* CS == C set */
  38. 0x3333, /* CC */
  39. 0xFF00, /* MI == N set */
  40. 0x00FF, /* PL */
  41. 0xAAAA, /* VS == V set */
  42. 0x5555, /* VC */
  43. 0x0C0C, /* HI == C set && Z clear */
  44. 0xF3F3, /* LS == C clear || Z set */
  45. 0xAA55, /* GE == (N==V) */
  46. 0x55AA, /* LT == (N!=V) */
  47. 0x0A05, /* GT == (!Z && (N==V)) */
  48. 0xF5FA, /* LE == (Z || (N!=V)) */
  49. 0xFFFF, /* AL always */
  50. 0 /* NV */
  51. };
  52. /*
  53. * Check if a trapped instruction should have been executed or not.
  54. */
  55. bool __hyp_text kvm_condition_valid32(const struct kvm_vcpu *vcpu)
  56. {
  57. unsigned long cpsr;
  58. u32 cpsr_cond;
  59. int cond;
  60. /* Top two bits non-zero? Unconditional. */
  61. if (kvm_vcpu_get_hsr(vcpu) >> 30)
  62. return true;
  63. /* Is condition field valid? */
  64. cond = kvm_vcpu_get_condition(vcpu);
  65. if (cond == 0xE)
  66. return true;
  67. cpsr = *vcpu_cpsr(vcpu);
  68. if (cond < 0) {
  69. /* This can happen in Thumb mode: examine IT state. */
  70. unsigned long it;
  71. it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
  72. /* it == 0 => unconditional. */
  73. if (it == 0)
  74. return true;
  75. /* The cond for this insn works out as the top 4 bits. */
  76. cond = (it >> 4);
  77. }
  78. cpsr_cond = cpsr >> 28;
  79. if (!((cc_map[cond] >> cpsr_cond) & 1))
  80. return false;
  81. return true;
  82. }
  83. /**
  84. * adjust_itstate - adjust ITSTATE when emulating instructions in IT-block
  85. * @vcpu: The VCPU pointer
  86. *
  87. * When exceptions occur while instructions are executed in Thumb IF-THEN
  88. * blocks, the ITSTATE field of the CPSR is not advanced (updated), so we have
  89. * to do this little bit of work manually. The fields map like this:
  90. *
  91. * IT[7:0] -> CPSR[26:25],CPSR[15:10]
  92. */
  93. static void __hyp_text kvm_adjust_itstate(struct kvm_vcpu *vcpu)
  94. {
  95. unsigned long itbits, cond;
  96. unsigned long cpsr = *vcpu_cpsr(vcpu);
  97. bool is_arm = !(cpsr & PSR_AA32_T_BIT);
  98. if (is_arm || !(cpsr & PSR_AA32_IT_MASK))
  99. return;
  100. cond = (cpsr & 0xe000) >> 13;
  101. itbits = (cpsr & 0x1c00) >> (10 - 2);
  102. itbits |= (cpsr & (0x3 << 25)) >> 25;
  103. /* Perform ITAdvance (see page A2-52 in ARM DDI 0406C) */
  104. if ((itbits & 0x7) == 0)
  105. itbits = cond = 0;
  106. else
  107. itbits = (itbits << 1) & 0x1f;
  108. cpsr &= ~PSR_AA32_IT_MASK;
  109. cpsr |= cond << 13;
  110. cpsr |= (itbits & 0x1c) << (10 - 2);
  111. cpsr |= (itbits & 0x3) << 25;
  112. *vcpu_cpsr(vcpu) = cpsr;
  113. }
  114. /**
  115. * kvm_skip_instr - skip a trapped instruction and proceed to the next
  116. * @vcpu: The vcpu pointer
  117. */
  118. void __hyp_text kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr)
  119. {
  120. bool is_thumb;
  121. is_thumb = !!(*vcpu_cpsr(vcpu) & PSR_AA32_T_BIT);
  122. if (is_thumb && !is_wide_instr)
  123. *vcpu_pc(vcpu) += 2;
  124. else
  125. *vcpu_pc(vcpu) += 4;
  126. kvm_adjust_itstate(vcpu);
  127. }
  128. /*
  129. * Table taken from ARMv8 ARM DDI0487B-B, table G1-10.
  130. */
  131. static const u8 return_offsets[8][2] = {
  132. [0] = { 0, 0 }, /* Reset, unused */
  133. [1] = { 4, 2 }, /* Undefined */
  134. [2] = { 0, 0 }, /* SVC, unused */
  135. [3] = { 4, 4 }, /* Prefetch abort */
  136. [4] = { 8, 8 }, /* Data abort */
  137. [5] = { 0, 0 }, /* HVC, unused */
  138. [6] = { 4, 4 }, /* IRQ, unused */
  139. [7] = { 4, 4 }, /* FIQ, unused */
  140. };
  141. static void prepare_fault32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
  142. {
  143. unsigned long cpsr;
  144. unsigned long new_spsr_value = *vcpu_cpsr(vcpu);
  145. bool is_thumb = (new_spsr_value & PSR_AA32_T_BIT);
  146. u32 return_offset = return_offsets[vect_offset >> 2][is_thumb];
  147. u32 sctlr = vcpu_cp15(vcpu, c1_SCTLR);
  148. cpsr = mode | PSR_AA32_I_BIT;
  149. if (sctlr & (1 << 30))
  150. cpsr |= PSR_AA32_T_BIT;
  151. if (sctlr & (1 << 25))
  152. cpsr |= PSR_AA32_E_BIT;
  153. *vcpu_cpsr(vcpu) = cpsr;
  154. /* Note: These now point to the banked copies */
  155. vcpu_write_spsr(vcpu, new_spsr_value);
  156. *vcpu_reg32(vcpu, 14) = *vcpu_pc(vcpu) + return_offset;
  157. /* Branch to exception vector */
  158. if (sctlr & (1 << 13))
  159. vect_offset += 0xffff0000;
  160. else /* always have security exceptions */
  161. vect_offset += vcpu_cp15(vcpu, c12_VBAR);
  162. *vcpu_pc(vcpu) = vect_offset;
  163. }
  164. void kvm_inject_undef32(struct kvm_vcpu *vcpu)
  165. {
  166. prepare_fault32(vcpu, PSR_AA32_MODE_UND, 4);
  167. }
  168. /*
  169. * Modelled after TakeDataAbortException() and TakePrefetchAbortException
  170. * pseudocode.
  171. */
  172. static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt,
  173. unsigned long addr)
  174. {
  175. u32 vect_offset;
  176. u32 *far, *fsr;
  177. bool is_lpae;
  178. if (is_pabt) {
  179. vect_offset = 12;
  180. far = &vcpu_cp15(vcpu, c6_IFAR);
  181. fsr = &vcpu_cp15(vcpu, c5_IFSR);
  182. } else { /* !iabt */
  183. vect_offset = 16;
  184. far = &vcpu_cp15(vcpu, c6_DFAR);
  185. fsr = &vcpu_cp15(vcpu, c5_DFSR);
  186. }
  187. prepare_fault32(vcpu, PSR_AA32_MODE_ABT | PSR_AA32_A_BIT, vect_offset);
  188. *far = addr;
  189. /* Give the guest an IMPLEMENTATION DEFINED exception */
  190. is_lpae = (vcpu_cp15(vcpu, c2_TTBCR) >> 31);
  191. if (is_lpae)
  192. *fsr = 1 << 9 | 0x34;
  193. else
  194. *fsr = 0x14;
  195. }
  196. void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr)
  197. {
  198. inject_abt32(vcpu, false, addr);
  199. }
  200. void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr)
  201. {
  202. inject_abt32(vcpu, true, addr);
  203. }