aarch32.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hyp portion of the (not much of an) Emulation layer for 32bit 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/kvm_hyp.h>
  15. /*
  16. * stolen from arch/arm/kernel/opcodes.c
  17. *
  18. * condition code lookup table
  19. * index into the table is test code: EQ, NE, ... LT, GT, AL, NV
  20. *
  21. * bit position in short is condition code: NZCV
  22. */
  23. static const unsigned short cc_map[16] = {
  24. 0xF0F0, /* EQ == Z set */
  25. 0x0F0F, /* NE */
  26. 0xCCCC, /* CS == C set */
  27. 0x3333, /* CC */
  28. 0xFF00, /* MI == N set */
  29. 0x00FF, /* PL */
  30. 0xAAAA, /* VS == V set */
  31. 0x5555, /* VC */
  32. 0x0C0C, /* HI == C set && Z clear */
  33. 0xF3F3, /* LS == C clear || Z set */
  34. 0xAA55, /* GE == (N==V) */
  35. 0x55AA, /* LT == (N!=V) */
  36. 0x0A05, /* GT == (!Z && (N==V)) */
  37. 0xF5FA, /* LE == (Z || (N!=V)) */
  38. 0xFFFF, /* AL always */
  39. 0 /* NV */
  40. };
  41. /*
  42. * Check if a trapped instruction should have been executed or not.
  43. */
  44. bool kvm_condition_valid32(const struct kvm_vcpu *vcpu)
  45. {
  46. unsigned long cpsr;
  47. u32 cpsr_cond;
  48. int cond;
  49. /*
  50. * These are the exception classes that could fire with a
  51. * conditional instruction.
  52. */
  53. switch (kvm_vcpu_trap_get_class(vcpu)) {
  54. case ESR_ELx_EC_CP15_32:
  55. case ESR_ELx_EC_CP15_64:
  56. case ESR_ELx_EC_CP14_MR:
  57. case ESR_ELx_EC_CP14_LS:
  58. case ESR_ELx_EC_FP_ASIMD:
  59. case ESR_ELx_EC_CP10_ID:
  60. case ESR_ELx_EC_CP14_64:
  61. case ESR_ELx_EC_SVC32:
  62. break;
  63. default:
  64. return true;
  65. }
  66. /* Is condition field valid? */
  67. cond = kvm_vcpu_get_condition(vcpu);
  68. if (cond == 0xE)
  69. return true;
  70. cpsr = *vcpu_cpsr(vcpu);
  71. if (cond < 0) {
  72. /* This can happen in Thumb mode: examine IT state. */
  73. unsigned long it;
  74. it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
  75. /* it == 0 => unconditional. */
  76. if (it == 0)
  77. return true;
  78. /* The cond for this insn works out as the top 4 bits. */
  79. cond = (it >> 4);
  80. }
  81. cpsr_cond = cpsr >> 28;
  82. if (!((cc_map[cond] >> cpsr_cond) & 1))
  83. return false;
  84. return true;
  85. }
  86. /**
  87. * kvm_adjust_itstate - adjust ITSTATE when emulating instructions in IT-block
  88. * @vcpu: The VCPU pointer
  89. *
  90. * When exceptions occur while instructions are executed in Thumb IF-THEN
  91. * blocks, the ITSTATE field of the CPSR is not advanced (updated), so we have
  92. * to do this little bit of work manually. The fields map like this:
  93. *
  94. * IT[7:0] -> CPSR[26:25],CPSR[15:10]
  95. */
  96. static void kvm_adjust_itstate(struct kvm_vcpu *vcpu)
  97. {
  98. unsigned long itbits, cond;
  99. unsigned long cpsr = *vcpu_cpsr(vcpu);
  100. bool is_arm = !(cpsr & PSR_AA32_T_BIT);
  101. if (is_arm || !(cpsr & PSR_AA32_IT_MASK))
  102. return;
  103. cond = (cpsr & 0xe000) >> 13;
  104. itbits = (cpsr & 0x1c00) >> (10 - 2);
  105. itbits |= (cpsr & (0x3 << 25)) >> 25;
  106. /* Perform ITAdvance (see page A2-52 in ARM DDI 0406C) */
  107. if ((itbits & 0x7) == 0)
  108. itbits = cond = 0;
  109. else
  110. itbits = (itbits << 1) & 0x1f;
  111. cpsr &= ~PSR_AA32_IT_MASK;
  112. cpsr |= cond << 13;
  113. cpsr |= (itbits & 0x1c) << (10 - 2);
  114. cpsr |= (itbits & 0x3) << 25;
  115. *vcpu_cpsr(vcpu) = cpsr;
  116. }
  117. /**
  118. * kvm_skip_instr32 - skip a trapped instruction and proceed to the next
  119. * @vcpu: The vcpu pointer
  120. */
  121. void kvm_skip_instr32(struct kvm_vcpu *vcpu)
  122. {
  123. u32 pc = *vcpu_pc(vcpu);
  124. bool is_thumb;
  125. is_thumb = !!(*vcpu_cpsr(vcpu) & PSR_AA32_T_BIT);
  126. if (is_thumb && !kvm_vcpu_trap_il_is32bit(vcpu))
  127. pc += 2;
  128. else
  129. pc += 4;
  130. *vcpu_pc(vcpu) = pc;
  131. kvm_adjust_itstate(vcpu);
  132. }