book3s_hv_tm_builtin.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. switch (instr & 0xfc0007ff) {
  26. case PPC_INST_RFID:
  27. /* XXX do we need to check for PR=0 here? */
  28. newmsr = vcpu->arch.shregs.srr1;
  29. /* should only get here for Sx -> T1 transition */
  30. if (!(MSR_TM_TRANSACTIONAL(newmsr) && (newmsr & MSR_TM)))
  31. return 0;
  32. newmsr = sanitize_msr(newmsr);
  33. vcpu->arch.shregs.msr = newmsr;
  34. vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
  35. vcpu->arch.regs.nip = vcpu->arch.shregs.srr0;
  36. return 1;
  37. case PPC_INST_RFEBB:
  38. /* check for PR=1 and arch 2.06 bit set in PCR */
  39. msr = vcpu->arch.shregs.msr;
  40. if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206))
  41. return 0;
  42. /* check EBB facility is available */
  43. if (!(vcpu->arch.hfscr & HFSCR_EBB) ||
  44. ((msr & MSR_PR) && !(mfspr(SPRN_FSCR) & FSCR_EBB)))
  45. return 0;
  46. bescr = mfspr(SPRN_BESCR);
  47. /* expect to see a S->T transition requested */
  48. if (((bescr >> 30) & 3) != 2)
  49. return 0;
  50. bescr &= ~BESCR_GE;
  51. if (instr & (1 << 11))
  52. bescr |= BESCR_GE;
  53. mtspr(SPRN_BESCR, bescr);
  54. msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
  55. vcpu->arch.shregs.msr = msr;
  56. vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
  57. vcpu->arch.regs.nip = mfspr(SPRN_EBBRR);
  58. return 1;
  59. case PPC_INST_MTMSRD:
  60. /* XXX do we need to check for PR=0 here? */
  61. rs = (instr >> 21) & 0x1f;
  62. newmsr = kvmppc_get_gpr(vcpu, rs);
  63. msr = vcpu->arch.shregs.msr;
  64. /* check this is a Sx -> T1 transition */
  65. if (!(MSR_TM_TRANSACTIONAL(newmsr) && (newmsr & MSR_TM)))
  66. return 0;
  67. /* mtmsrd doesn't change LE */
  68. newmsr = (newmsr & ~MSR_LE) | (msr & MSR_LE);
  69. newmsr = sanitize_msr(newmsr);
  70. vcpu->arch.shregs.msr = newmsr;
  71. return 1;
  72. case PPC_INST_TSR:
  73. /* we know the MSR has the TS field = S (0b01) here */
  74. msr = vcpu->arch.shregs.msr;
  75. /* check for PR=1 and arch 2.06 bit set in PCR */
  76. if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206))
  77. return 0;
  78. /* check for TM disabled in the HFSCR or MSR */
  79. if (!(vcpu->arch.hfscr & HFSCR_TM) || !(msr & MSR_TM))
  80. return 0;
  81. /* L=1 => tresume => set TS to T (0b10) */
  82. if (instr & (1 << 21))
  83. vcpu->arch.shregs.msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
  84. /* Set CR0 to 0b0010 */
  85. vcpu->arch.cr = (vcpu->arch.cr & 0x0fffffff) | 0x20000000;
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. /*
  91. * This is called when we are returning to a guest in TM transactional
  92. * state. We roll the guest state back to the checkpointed state.
  93. */
  94. void kvmhv_emulate_tm_rollback(struct kvm_vcpu *vcpu)
  95. {
  96. vcpu->arch.shregs.msr &= ~MSR_TS_MASK; /* go to N state */
  97. vcpu->arch.regs.nip = vcpu->arch.tfhar;
  98. copy_from_checkpoint(vcpu);
  99. vcpu->arch.cr = (vcpu->arch.cr & 0x0fffffff) | 0xa0000000;
  100. }