emulate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/esr.h>
  25. #include <asm/kvm_emulate.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. static int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
  53. {
  54. u32 esr = kvm_vcpu_get_hsr(vcpu);
  55. if (esr & ESR_ELx_CV)
  56. return (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
  57. return -1;
  58. }
  59. /*
  60. * Check if a trapped instruction should have been executed or not.
  61. */
  62. bool kvm_condition_valid32(const struct kvm_vcpu *vcpu)
  63. {
  64. unsigned long cpsr;
  65. u32 cpsr_cond;
  66. int cond;
  67. /* Top two bits non-zero? Unconditional. */
  68. if (kvm_vcpu_get_hsr(vcpu) >> 30)
  69. return true;
  70. /* Is condition field valid? */
  71. cond = kvm_vcpu_get_condition(vcpu);
  72. if (cond == 0xE)
  73. return true;
  74. cpsr = *vcpu_cpsr(vcpu);
  75. if (cond < 0) {
  76. /* This can happen in Thumb mode: examine IT state. */
  77. unsigned long it;
  78. it = ((cpsr >> 8) & 0xFC) | ((cpsr >> 25) & 0x3);
  79. /* it == 0 => unconditional. */
  80. if (it == 0)
  81. return true;
  82. /* The cond for this insn works out as the top 4 bits. */
  83. cond = (it >> 4);
  84. }
  85. cpsr_cond = cpsr >> 28;
  86. if (!((cc_map[cond] >> cpsr_cond) & 1))
  87. return false;
  88. return true;
  89. }
  90. /**
  91. * adjust_itstate - adjust ITSTATE when emulating instructions in IT-block
  92. * @vcpu: The VCPU pointer
  93. *
  94. * When exceptions occur while instructions are executed in Thumb IF-THEN
  95. * blocks, the ITSTATE field of the CPSR is not advanced (updated), so we have
  96. * to do this little bit of work manually. The fields map like this:
  97. *
  98. * IT[7:0] -> CPSR[26:25],CPSR[15:10]
  99. */
  100. static void kvm_adjust_itstate(struct kvm_vcpu *vcpu)
  101. {
  102. unsigned long itbits, cond;
  103. unsigned long cpsr = *vcpu_cpsr(vcpu);
  104. bool is_arm = !(cpsr & COMPAT_PSR_T_BIT);
  105. BUG_ON(is_arm && (cpsr & COMPAT_PSR_IT_MASK));
  106. if (!(cpsr & COMPAT_PSR_IT_MASK))
  107. return;
  108. cond = (cpsr & 0xe000) >> 13;
  109. itbits = (cpsr & 0x1c00) >> (10 - 2);
  110. itbits |= (cpsr & (0x3 << 25)) >> 25;
  111. /* Perform ITAdvance (see page A2-52 in ARM DDI 0406C) */
  112. if ((itbits & 0x7) == 0)
  113. itbits = cond = 0;
  114. else
  115. itbits = (itbits << 1) & 0x1f;
  116. cpsr &= ~COMPAT_PSR_IT_MASK;
  117. cpsr |= cond << 13;
  118. cpsr |= (itbits & 0x1c) << (10 - 2);
  119. cpsr |= (itbits & 0x3) << 25;
  120. *vcpu_cpsr(vcpu) = cpsr;
  121. }
  122. /**
  123. * kvm_skip_instr - skip a trapped instruction and proceed to the next
  124. * @vcpu: The vcpu pointer
  125. */
  126. void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr)
  127. {
  128. bool is_thumb;
  129. is_thumb = !!(*vcpu_cpsr(vcpu) & COMPAT_PSR_T_BIT);
  130. if (is_thumb && !is_wide_instr)
  131. *vcpu_pc(vcpu) += 2;
  132. else
  133. *vcpu_pc(vcpu) += 4;
  134. kvm_adjust_itstate(vcpu);
  135. }