Path: blob/master/arch/powerpc/kvm/book3s_hv_tm_builtin.c
26451 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright 2017 Paul Mackerras, IBM Corp. <[email protected]>3*/45#include <linux/kvm_host.h>67#include <asm/kvm_ppc.h>8#include <asm/kvm_book3s.h>9#include <asm/kvm_book3s_64.h>10#include <asm/reg.h>11#include <asm/ppc-opcode.h>1213/*14* This handles the cases where the guest is in real suspend mode15* and we want to get back to the guest without dooming the transaction.16* The caller has checked that the guest is in real-suspend mode17* (MSR[TS] = S and the fake-suspend flag is not set).18*/19int kvmhv_p9_tm_emulation_early(struct kvm_vcpu *vcpu)20{21u32 instr = vcpu->arch.emul_inst;22u64 newmsr, msr, bescr;23int rs;2425/*26* rfid, rfebb, and mtmsrd encode bit 31 = 0 since it's a reserved bit27* in these instructions, so masking bit 31 out doesn't change these28* instructions. For the tsr. instruction if bit 31 = 0 then it is per29* ISA an invalid form, however P9 UM, in section 4.6.10 Book II Invalid30* Forms, informs specifically that ignoring bit 31 is an acceptable way31* to handle TM-related invalid forms that have bit 31 = 0. Moreover,32* for emulation purposes both forms (w/ and wo/ bit 31 set) can33* generate a softpatch interrupt. Hence both forms are handled below34* for tsr. to make them behave the same way.35*/36switch (instr & PO_XOP_OPCODE_MASK) {37case PPC_INST_RFID:38/* XXX do we need to check for PR=0 here? */39newmsr = vcpu->arch.shregs.srr1;40/* should only get here for Sx -> T1 transition */41if (!(MSR_TM_TRANSACTIONAL(newmsr) && (newmsr & MSR_TM)))42return 0;43newmsr = sanitize_msr(newmsr);44vcpu->arch.shregs.msr = newmsr;45vcpu->arch.cfar = vcpu->arch.regs.nip - 4;46vcpu->arch.regs.nip = vcpu->arch.shregs.srr0;47return 1;4849case PPC_INST_RFEBB:50/* check for PR=1 and arch 2.06 bit set in PCR */51msr = vcpu->arch.shregs.msr;52if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206))53return 0;54/* check EBB facility is available */55if (!(vcpu->arch.hfscr & HFSCR_EBB) ||56((msr & MSR_PR) && !(mfspr(SPRN_FSCR) & FSCR_EBB)))57return 0;58bescr = mfspr(SPRN_BESCR);59/* expect to see a S->T transition requested */60if (((bescr >> 30) & 3) != 2)61return 0;62bescr &= ~BESCR_GE;63if (instr & (1 << 11))64bescr |= BESCR_GE;65mtspr(SPRN_BESCR, bescr);66msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;67vcpu->arch.shregs.msr = msr;68vcpu->arch.cfar = vcpu->arch.regs.nip - 4;69vcpu->arch.regs.nip = mfspr(SPRN_EBBRR);70return 1;7172case PPC_INST_MTMSRD:73/* XXX do we need to check for PR=0 here? */74rs = (instr >> 21) & 0x1f;75newmsr = kvmppc_get_gpr(vcpu, rs);76msr = vcpu->arch.shregs.msr;77/* check this is a Sx -> T1 transition */78if (!(MSR_TM_TRANSACTIONAL(newmsr) && (newmsr & MSR_TM)))79return 0;80/* mtmsrd doesn't change LE */81newmsr = (newmsr & ~MSR_LE) | (msr & MSR_LE);82newmsr = sanitize_msr(newmsr);83vcpu->arch.shregs.msr = newmsr;84return 1;8586/* ignore bit 31, see comment above */87case (PPC_INST_TSR & PO_XOP_OPCODE_MASK):88/* we know the MSR has the TS field = S (0b01) here */89msr = vcpu->arch.shregs.msr;90/* check for PR=1 and arch 2.06 bit set in PCR */91if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206))92return 0;93/* check for TM disabled in the HFSCR or MSR */94if (!(vcpu->arch.hfscr & HFSCR_TM) || !(msr & MSR_TM))95return 0;96/* L=1 => tresume => set TS to T (0b10) */97if (instr & (1 << 21))98vcpu->arch.shregs.msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;99/* Set CR0 to 0b0010 */100vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & 0x0fffffff) |1010x20000000;102return 1;103}104105return 0;106}107108/*109* This is called when we are returning to a guest in TM transactional110* state. We roll the guest state back to the checkpointed state.111*/112void kvmhv_emulate_tm_rollback(struct kvm_vcpu *vcpu)113{114vcpu->arch.shregs.msr &= ~MSR_TS_MASK; /* go to N state */115vcpu->arch.regs.nip = vcpu->arch.tfhar;116copy_from_checkpoint(vcpu);117vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & 0x0fffffff) | 0xa0000000;118}119120121