emulate.c 4.1 KB

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