book3s_hv_tm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2017 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2, as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kvm_host.h>
  9. #include <asm/kvm_ppc.h>
  10. #include <asm/kvm_book3s.h>
  11. #include <asm/kvm_book3s_64.h>
  12. #include <asm/reg.h>
  13. #include <asm/ppc-opcode.h>
  14. static void emulate_tx_failure(struct kvm_vcpu *vcpu, u64 failure_cause)
  15. {
  16. u64 texasr, tfiar;
  17. u64 msr = vcpu->arch.shregs.msr;
  18. tfiar = vcpu->arch.regs.nip & ~0x3ull;
  19. texasr = (failure_cause << 56) | TEXASR_ABORT | TEXASR_FS | TEXASR_EXACT;
  20. if (MSR_TM_SUSPENDED(vcpu->arch.shregs.msr))
  21. texasr |= TEXASR_SUSP;
  22. if (msr & MSR_PR) {
  23. texasr |= TEXASR_PR;
  24. tfiar |= 1;
  25. }
  26. vcpu->arch.tfiar = tfiar;
  27. /* Preserve ROT and TL fields of existing TEXASR */
  28. vcpu->arch.texasr = (vcpu->arch.texasr & 0x3ffffff) | texasr;
  29. }
  30. /*
  31. * This gets called on a softpatch interrupt on POWER9 DD2.2 processors.
  32. * We expect to find a TM-related instruction to be emulated. The
  33. * instruction image is in vcpu->arch.emul_inst. If the guest was in
  34. * TM suspended or transactional state, the checkpointed state has been
  35. * reclaimed and is in the vcpu struct. The CPU is in virtual mode in
  36. * host context.
  37. */
  38. int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)
  39. {
  40. u32 instr = vcpu->arch.emul_inst;
  41. u64 msr = vcpu->arch.shregs.msr;
  42. u64 newmsr, bescr;
  43. int ra, rs;
  44. switch (instr & 0xfc0007ff) {
  45. case PPC_INST_RFID:
  46. /* XXX do we need to check for PR=0 here? */
  47. newmsr = vcpu->arch.shregs.srr1;
  48. /* should only get here for Sx -> T1 transition */
  49. WARN_ON_ONCE(!(MSR_TM_SUSPENDED(msr) &&
  50. MSR_TM_TRANSACTIONAL(newmsr) &&
  51. (newmsr & MSR_TM)));
  52. newmsr = sanitize_msr(newmsr);
  53. vcpu->arch.shregs.msr = newmsr;
  54. vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
  55. vcpu->arch.regs.nip = vcpu->arch.shregs.srr0;
  56. return RESUME_GUEST;
  57. case PPC_INST_RFEBB:
  58. if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206)) {
  59. /* generate an illegal instruction interrupt */
  60. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  61. return RESUME_GUEST;
  62. }
  63. /* check EBB facility is available */
  64. if (!(vcpu->arch.hfscr & HFSCR_EBB)) {
  65. /* generate an illegal instruction interrupt */
  66. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  67. return RESUME_GUEST;
  68. }
  69. if ((msr & MSR_PR) && !(vcpu->arch.fscr & FSCR_EBB)) {
  70. /* generate a facility unavailable interrupt */
  71. vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
  72. ((u64)FSCR_EBB_LG << 56);
  73. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_FAC_UNAVAIL);
  74. return RESUME_GUEST;
  75. }
  76. bescr = vcpu->arch.bescr;
  77. /* expect to see a S->T transition requested */
  78. WARN_ON_ONCE(!(MSR_TM_SUSPENDED(msr) &&
  79. ((bescr >> 30) & 3) == 2));
  80. bescr &= ~BESCR_GE;
  81. if (instr & (1 << 11))
  82. bescr |= BESCR_GE;
  83. vcpu->arch.bescr = bescr;
  84. msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
  85. vcpu->arch.shregs.msr = msr;
  86. vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
  87. vcpu->arch.regs.nip = vcpu->arch.ebbrr;
  88. return RESUME_GUEST;
  89. case PPC_INST_MTMSRD:
  90. /* XXX do we need to check for PR=0 here? */
  91. rs = (instr >> 21) & 0x1f;
  92. newmsr = kvmppc_get_gpr(vcpu, rs);
  93. /* check this is a Sx -> T1 transition */
  94. WARN_ON_ONCE(!(MSR_TM_SUSPENDED(msr) &&
  95. MSR_TM_TRANSACTIONAL(newmsr) &&
  96. (newmsr & MSR_TM)));
  97. /* mtmsrd doesn't change LE */
  98. newmsr = (newmsr & ~MSR_LE) | (msr & MSR_LE);
  99. newmsr = sanitize_msr(newmsr);
  100. vcpu->arch.shregs.msr = newmsr;
  101. return RESUME_GUEST;
  102. case PPC_INST_TSR:
  103. /* check for PR=1 and arch 2.06 bit set in PCR */
  104. if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206)) {
  105. /* generate an illegal instruction interrupt */
  106. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  107. return RESUME_GUEST;
  108. }
  109. /* check for TM disabled in the HFSCR or MSR */
  110. if (!(vcpu->arch.hfscr & HFSCR_TM)) {
  111. /* generate an illegal instruction interrupt */
  112. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  113. return RESUME_GUEST;
  114. }
  115. if (!(msr & MSR_TM)) {
  116. /* generate a facility unavailable interrupt */
  117. vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
  118. ((u64)FSCR_TM_LG << 56);
  119. kvmppc_book3s_queue_irqprio(vcpu,
  120. BOOK3S_INTERRUPT_FAC_UNAVAIL);
  121. return RESUME_GUEST;
  122. }
  123. /* Set CR0 to indicate previous transactional state */
  124. vcpu->arch.cr = (vcpu->arch.cr & 0x0fffffff) |
  125. (((msr & MSR_TS_MASK) >> MSR_TS_S_LG) << 28);
  126. /* L=1 => tresume, L=0 => tsuspend */
  127. if (instr & (1 << 21)) {
  128. if (MSR_TM_SUSPENDED(msr))
  129. msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
  130. } else {
  131. if (MSR_TM_TRANSACTIONAL(msr))
  132. msr = (msr & ~MSR_TS_MASK) | MSR_TS_S;
  133. }
  134. vcpu->arch.shregs.msr = msr;
  135. return RESUME_GUEST;
  136. case PPC_INST_TRECLAIM:
  137. /* check for TM disabled in the HFSCR or MSR */
  138. if (!(vcpu->arch.hfscr & HFSCR_TM)) {
  139. /* generate an illegal instruction interrupt */
  140. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  141. return RESUME_GUEST;
  142. }
  143. if (!(msr & MSR_TM)) {
  144. /* generate a facility unavailable interrupt */
  145. vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
  146. ((u64)FSCR_TM_LG << 56);
  147. kvmppc_book3s_queue_irqprio(vcpu,
  148. BOOK3S_INTERRUPT_FAC_UNAVAIL);
  149. return RESUME_GUEST;
  150. }
  151. /* If no transaction active, generate TM bad thing */
  152. if (!MSR_TM_ACTIVE(msr)) {
  153. kvmppc_core_queue_program(vcpu, SRR1_PROGTM);
  154. return RESUME_GUEST;
  155. }
  156. /* If failure was not previously recorded, recompute TEXASR */
  157. if (!(vcpu->arch.orig_texasr & TEXASR_FS)) {
  158. ra = (instr >> 16) & 0x1f;
  159. if (ra)
  160. ra = kvmppc_get_gpr(vcpu, ra) & 0xff;
  161. emulate_tx_failure(vcpu, ra);
  162. }
  163. copy_from_checkpoint(vcpu);
  164. /* Set CR0 to indicate previous transactional state */
  165. vcpu->arch.cr = (vcpu->arch.cr & 0x0fffffff) |
  166. (((msr & MSR_TS_MASK) >> MSR_TS_S_LG) << 28);
  167. vcpu->arch.shregs.msr &= ~MSR_TS_MASK;
  168. return RESUME_GUEST;
  169. case PPC_INST_TRECHKPT:
  170. /* XXX do we need to check for PR=0 here? */
  171. /* check for TM disabled in the HFSCR or MSR */
  172. if (!(vcpu->arch.hfscr & HFSCR_TM)) {
  173. /* generate an illegal instruction interrupt */
  174. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  175. return RESUME_GUEST;
  176. }
  177. if (!(msr & MSR_TM)) {
  178. /* generate a facility unavailable interrupt */
  179. vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
  180. ((u64)FSCR_TM_LG << 56);
  181. kvmppc_book3s_queue_irqprio(vcpu,
  182. BOOK3S_INTERRUPT_FAC_UNAVAIL);
  183. return RESUME_GUEST;
  184. }
  185. /* If transaction active or TEXASR[FS] = 0, bad thing */
  186. if (MSR_TM_ACTIVE(msr) || !(vcpu->arch.texasr & TEXASR_FS)) {
  187. kvmppc_core_queue_program(vcpu, SRR1_PROGTM);
  188. return RESUME_GUEST;
  189. }
  190. copy_to_checkpoint(vcpu);
  191. /* Set CR0 to indicate previous transactional state */
  192. vcpu->arch.cr = (vcpu->arch.cr & 0x0fffffff) |
  193. (((msr & MSR_TS_MASK) >> MSR_TS_S_LG) << 28);
  194. vcpu->arch.shregs.msr = msr | MSR_TS_S;
  195. return RESUME_GUEST;
  196. }
  197. /* What should we do here? We didn't recognize the instruction */
  198. WARN_ON_ONCE(1);
  199. return RESUME_GUEST;
  200. }