book3s_hv_tm_builtin.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /*
  15. * This handles the cases where the guest is in real suspend mode
  16. * and we want to get back to the guest without dooming the transaction.
  17. * The caller has checked that the guest is in real-suspend mode
  18. * (MSR[TS] = S and the fake-suspend flag is not set).
  19. */
  20. int kvmhv_p9_tm_emulation_early(struct kvm_vcpu *vcpu)
  21. {
  22. u32 instr = vcpu->arch.emul_inst;
  23. u64 newmsr, msr, bescr;
  24. int rs;
  25. /*
  26. * rfid, rfebb, and mtmsrd encode bit 31 = 0 since it's a reserved bit
  27. * in these instructions, so masking bit 31 out doesn't change these
  28. * instructions. For the tsr. instruction if bit 31 = 0 then it is per
  29. * ISA an invalid form, however P9 UM, in section 4.6.10 Book II Invalid
  30. * Forms, informs specifically that ignoring bit 31 is an acceptable way
  31. * to handle TM-related invalid forms that have bit 31 = 0. Moreover,
  32. * for emulation purposes both forms (w/ and wo/ bit 31 set) can
  33. * generate a softpatch interrupt. Hence both forms are handled below
  34. * for tsr. to make them behave the same way.
  35. */
  36. switch (instr & PO_XOP_OPCODE_MASK) {
  37. case PPC_INST_RFID:
  38. /* XXX do we need to check for PR=0 here? */
  39. newmsr = vcpu->arch.shregs.srr1;
  40. /* should only get here for Sx -> T1 transition */
  41. if (!(MSR_TM_TRANSACTIONAL(newmsr) && (newmsr & MSR_TM)))
  42. return 0;
  43. newmsr = sanitize_msr(newmsr);
  44. vcpu->arch.shregs.msr = newmsr;
  45. vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
  46. vcpu->arch.regs.nip = vcpu->arch.shregs.srr0;
  47. return 1;
  48. case PPC_INST_RFEBB:
  49. /* check for PR=1 and arch 2.06 bit set in PCR */
  50. msr = vcpu->arch.shregs.msr;
  51. if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206))
  52. return 0;
  53. /* check EBB facility is available */
  54. if (!(vcpu->arch.hfscr & HFSCR_EBB) ||
  55. ((msr & MSR_PR) && !(mfspr(SPRN_FSCR) & FSCR_EBB)))
  56. return 0;
  57. bescr = mfspr(SPRN_BESCR);
  58. /* expect to see a S->T transition requested */
  59. if (((bescr >> 30) & 3) != 2)
  60. return 0;
  61. bescr &= ~BESCR_GE;
  62. if (instr & (1 << 11))
  63. bescr |= BESCR_GE;
  64. mtspr(SPRN_BESCR, bescr);
  65. msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
  66. vcpu->arch.shregs.msr = msr;
  67. vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
  68. vcpu->arch.regs.nip = mfspr(SPRN_EBBRR);
  69. return 1;
  70. case PPC_INST_MTMSRD:
  71. /* XXX do we need to check for PR=0 here? */
  72. rs = (instr >> 21) & 0x1f;
  73. newmsr = kvmppc_get_gpr(vcpu, rs);
  74. msr = vcpu->arch.shregs.msr;
  75. /* check this is a Sx -> T1 transition */
  76. if (!(MSR_TM_TRANSACTIONAL(newmsr) && (newmsr & MSR_TM)))
  77. return 0;
  78. /* mtmsrd doesn't change LE */
  79. newmsr = (newmsr & ~MSR_LE) | (msr & MSR_LE);
  80. newmsr = sanitize_msr(newmsr);
  81. vcpu->arch.shregs.msr = newmsr;
  82. return 1;
  83. /* ignore bit 31, see comment above */
  84. case (PPC_INST_TSR & PO_XOP_OPCODE_MASK):
  85. /* we know the MSR has the TS field = S (0b01) here */
  86. msr = vcpu->arch.shregs.msr;
  87. /* check for PR=1 and arch 2.06 bit set in PCR */
  88. if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206))
  89. return 0;
  90. /* check for TM disabled in the HFSCR or MSR */
  91. if (!(vcpu->arch.hfscr & HFSCR_TM) || !(msr & MSR_TM))
  92. return 0;
  93. /* L=1 => tresume => set TS to T (0b10) */
  94. if (instr & (1 << 21))
  95. vcpu->arch.shregs.msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
  96. /* Set CR0 to 0b0010 */
  97. vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & 0x0fffffff) |
  98. 0x20000000;
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. /*
  104. * This is called when we are returning to a guest in TM transactional
  105. * state. We roll the guest state back to the checkpointed state.
  106. */
  107. void kvmhv_emulate_tm_rollback(struct kvm_vcpu *vcpu)
  108. {
  109. vcpu->arch.shregs.msr &= ~MSR_TS_MASK; /* go to N state */
  110. vcpu->arch.regs.nip = vcpu->arch.tfhar;
  111. copy_from_checkpoint(vcpu);
  112. vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & 0x0fffffff) | 0xa0000000;
  113. }