aarch32.c 3.4 KB

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