1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2017 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/kvm_host.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <asm/kvm_ppc.h>
11*4882a593Smuzhiyun #include <asm/kvm_book3s.h>
12*4882a593Smuzhiyun #include <asm/kvm_book3s_64.h>
13*4882a593Smuzhiyun #include <asm/reg.h>
14*4882a593Smuzhiyun #include <asm/ppc-opcode.h>
15*4882a593Smuzhiyun
emulate_tx_failure(struct kvm_vcpu * vcpu,u64 failure_cause)16*4882a593Smuzhiyun static void emulate_tx_failure(struct kvm_vcpu *vcpu, u64 failure_cause)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun u64 texasr, tfiar;
19*4882a593Smuzhiyun u64 msr = vcpu->arch.shregs.msr;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun tfiar = vcpu->arch.regs.nip & ~0x3ull;
22*4882a593Smuzhiyun texasr = (failure_cause << 56) | TEXASR_ABORT | TEXASR_FS | TEXASR_EXACT;
23*4882a593Smuzhiyun if (MSR_TM_SUSPENDED(vcpu->arch.shregs.msr))
24*4882a593Smuzhiyun texasr |= TEXASR_SUSP;
25*4882a593Smuzhiyun if (msr & MSR_PR) {
26*4882a593Smuzhiyun texasr |= TEXASR_PR;
27*4882a593Smuzhiyun tfiar |= 1;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun vcpu->arch.tfiar = tfiar;
30*4882a593Smuzhiyun /* Preserve ROT and TL fields of existing TEXASR */
31*4882a593Smuzhiyun vcpu->arch.texasr = (vcpu->arch.texasr & 0x3ffffff) | texasr;
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun * This gets called on a softpatch interrupt on POWER9 DD2.2 processors.
36*4882a593Smuzhiyun * We expect to find a TM-related instruction to be emulated. The
37*4882a593Smuzhiyun * instruction image is in vcpu->arch.emul_inst. If the guest was in
38*4882a593Smuzhiyun * TM suspended or transactional state, the checkpointed state has been
39*4882a593Smuzhiyun * reclaimed and is in the vcpu struct. The CPU is in virtual mode in
40*4882a593Smuzhiyun * host context.
41*4882a593Smuzhiyun */
kvmhv_p9_tm_emulation(struct kvm_vcpu * vcpu)42*4882a593Smuzhiyun int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun u32 instr = vcpu->arch.emul_inst;
45*4882a593Smuzhiyun u64 msr = vcpu->arch.shregs.msr;
46*4882a593Smuzhiyun u64 newmsr, bescr;
47*4882a593Smuzhiyun int ra, rs;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun * rfid, rfebb, and mtmsrd encode bit 31 = 0 since it's a reserved bit
51*4882a593Smuzhiyun * in these instructions, so masking bit 31 out doesn't change these
52*4882a593Smuzhiyun * instructions. For treclaim., tsr., and trechkpt. instructions if bit
53*4882a593Smuzhiyun * 31 = 0 then they are per ISA invalid forms, however P9 UM, in section
54*4882a593Smuzhiyun * 4.6.10 Book II Invalid Forms, informs specifically that ignoring bit
55*4882a593Smuzhiyun * 31 is an acceptable way to handle these invalid forms that have
56*4882a593Smuzhiyun * bit 31 = 0. Moreover, for emulation purposes both forms (w/ and wo/
57*4882a593Smuzhiyun * bit 31 set) can generate a softpatch interrupt. Hence both forms
58*4882a593Smuzhiyun * are handled below for these instructions so they behave the same way.
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun switch (instr & PO_XOP_OPCODE_MASK) {
61*4882a593Smuzhiyun case PPC_INST_RFID:
62*4882a593Smuzhiyun /* XXX do we need to check for PR=0 here? */
63*4882a593Smuzhiyun newmsr = vcpu->arch.shregs.srr1;
64*4882a593Smuzhiyun /* should only get here for Sx -> T1 transition */
65*4882a593Smuzhiyun WARN_ON_ONCE(!(MSR_TM_SUSPENDED(msr) &&
66*4882a593Smuzhiyun MSR_TM_TRANSACTIONAL(newmsr) &&
67*4882a593Smuzhiyun (newmsr & MSR_TM)));
68*4882a593Smuzhiyun newmsr = sanitize_msr(newmsr);
69*4882a593Smuzhiyun vcpu->arch.shregs.msr = newmsr;
70*4882a593Smuzhiyun vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
71*4882a593Smuzhiyun vcpu->arch.regs.nip = vcpu->arch.shregs.srr0;
72*4882a593Smuzhiyun return RESUME_GUEST;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun case PPC_INST_RFEBB:
75*4882a593Smuzhiyun if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206)) {
76*4882a593Smuzhiyun /* generate an illegal instruction interrupt */
77*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
78*4882a593Smuzhiyun return RESUME_GUEST;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun /* check EBB facility is available */
81*4882a593Smuzhiyun if (!(vcpu->arch.hfscr & HFSCR_EBB)) {
82*4882a593Smuzhiyun /* generate an illegal instruction interrupt */
83*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
84*4882a593Smuzhiyun return RESUME_GUEST;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun if ((msr & MSR_PR) && !(vcpu->arch.fscr & FSCR_EBB)) {
87*4882a593Smuzhiyun /* generate a facility unavailable interrupt */
88*4882a593Smuzhiyun vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
89*4882a593Smuzhiyun ((u64)FSCR_EBB_LG << 56);
90*4882a593Smuzhiyun kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_FAC_UNAVAIL);
91*4882a593Smuzhiyun return RESUME_GUEST;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun bescr = vcpu->arch.bescr;
94*4882a593Smuzhiyun /* expect to see a S->T transition requested */
95*4882a593Smuzhiyun WARN_ON_ONCE(!(MSR_TM_SUSPENDED(msr) &&
96*4882a593Smuzhiyun ((bescr >> 30) & 3) == 2));
97*4882a593Smuzhiyun bescr &= ~BESCR_GE;
98*4882a593Smuzhiyun if (instr & (1 << 11))
99*4882a593Smuzhiyun bescr |= BESCR_GE;
100*4882a593Smuzhiyun vcpu->arch.bescr = bescr;
101*4882a593Smuzhiyun msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
102*4882a593Smuzhiyun vcpu->arch.shregs.msr = msr;
103*4882a593Smuzhiyun vcpu->arch.cfar = vcpu->arch.regs.nip - 4;
104*4882a593Smuzhiyun vcpu->arch.regs.nip = vcpu->arch.ebbrr;
105*4882a593Smuzhiyun return RESUME_GUEST;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun case PPC_INST_MTMSRD:
108*4882a593Smuzhiyun /* XXX do we need to check for PR=0 here? */
109*4882a593Smuzhiyun rs = (instr >> 21) & 0x1f;
110*4882a593Smuzhiyun newmsr = kvmppc_get_gpr(vcpu, rs);
111*4882a593Smuzhiyun /* check this is a Sx -> T1 transition */
112*4882a593Smuzhiyun WARN_ON_ONCE(!(MSR_TM_SUSPENDED(msr) &&
113*4882a593Smuzhiyun MSR_TM_TRANSACTIONAL(newmsr) &&
114*4882a593Smuzhiyun (newmsr & MSR_TM)));
115*4882a593Smuzhiyun /* mtmsrd doesn't change LE */
116*4882a593Smuzhiyun newmsr = (newmsr & ~MSR_LE) | (msr & MSR_LE);
117*4882a593Smuzhiyun newmsr = sanitize_msr(newmsr);
118*4882a593Smuzhiyun vcpu->arch.shregs.msr = newmsr;
119*4882a593Smuzhiyun return RESUME_GUEST;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* ignore bit 31, see comment above */
122*4882a593Smuzhiyun case (PPC_INST_TSR & PO_XOP_OPCODE_MASK):
123*4882a593Smuzhiyun /* check for PR=1 and arch 2.06 bit set in PCR */
124*4882a593Smuzhiyun if ((msr & MSR_PR) && (vcpu->arch.vcore->pcr & PCR_ARCH_206)) {
125*4882a593Smuzhiyun /* generate an illegal instruction interrupt */
126*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
127*4882a593Smuzhiyun return RESUME_GUEST;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun /* check for TM disabled in the HFSCR or MSR */
130*4882a593Smuzhiyun if (!(vcpu->arch.hfscr & HFSCR_TM)) {
131*4882a593Smuzhiyun /* generate an illegal instruction interrupt */
132*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
133*4882a593Smuzhiyun return RESUME_GUEST;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun if (!(msr & MSR_TM)) {
136*4882a593Smuzhiyun /* generate a facility unavailable interrupt */
137*4882a593Smuzhiyun vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
138*4882a593Smuzhiyun ((u64)FSCR_TM_LG << 56);
139*4882a593Smuzhiyun kvmppc_book3s_queue_irqprio(vcpu,
140*4882a593Smuzhiyun BOOK3S_INTERRUPT_FAC_UNAVAIL);
141*4882a593Smuzhiyun return RESUME_GUEST;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun /* Set CR0 to indicate previous transactional state */
144*4882a593Smuzhiyun vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & 0x0fffffff) |
145*4882a593Smuzhiyun (((msr & MSR_TS_MASK) >> MSR_TS_S_LG) << 29);
146*4882a593Smuzhiyun /* L=1 => tresume, L=0 => tsuspend */
147*4882a593Smuzhiyun if (instr & (1 << 21)) {
148*4882a593Smuzhiyun if (MSR_TM_SUSPENDED(msr))
149*4882a593Smuzhiyun msr = (msr & ~MSR_TS_MASK) | MSR_TS_T;
150*4882a593Smuzhiyun } else {
151*4882a593Smuzhiyun if (MSR_TM_TRANSACTIONAL(msr))
152*4882a593Smuzhiyun msr = (msr & ~MSR_TS_MASK) | MSR_TS_S;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun vcpu->arch.shregs.msr = msr;
155*4882a593Smuzhiyun return RESUME_GUEST;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /* ignore bit 31, see comment above */
158*4882a593Smuzhiyun case (PPC_INST_TRECLAIM & PO_XOP_OPCODE_MASK):
159*4882a593Smuzhiyun /* check for TM disabled in the HFSCR or MSR */
160*4882a593Smuzhiyun if (!(vcpu->arch.hfscr & HFSCR_TM)) {
161*4882a593Smuzhiyun /* generate an illegal instruction interrupt */
162*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
163*4882a593Smuzhiyun return RESUME_GUEST;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun if (!(msr & MSR_TM)) {
166*4882a593Smuzhiyun /* generate a facility unavailable interrupt */
167*4882a593Smuzhiyun vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
168*4882a593Smuzhiyun ((u64)FSCR_TM_LG << 56);
169*4882a593Smuzhiyun kvmppc_book3s_queue_irqprio(vcpu,
170*4882a593Smuzhiyun BOOK3S_INTERRUPT_FAC_UNAVAIL);
171*4882a593Smuzhiyun return RESUME_GUEST;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun /* If no transaction active, generate TM bad thing */
174*4882a593Smuzhiyun if (!MSR_TM_ACTIVE(msr)) {
175*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGTM);
176*4882a593Smuzhiyun return RESUME_GUEST;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun /* If failure was not previously recorded, recompute TEXASR */
179*4882a593Smuzhiyun if (!(vcpu->arch.orig_texasr & TEXASR_FS)) {
180*4882a593Smuzhiyun ra = (instr >> 16) & 0x1f;
181*4882a593Smuzhiyun if (ra)
182*4882a593Smuzhiyun ra = kvmppc_get_gpr(vcpu, ra) & 0xff;
183*4882a593Smuzhiyun emulate_tx_failure(vcpu, ra);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun copy_from_checkpoint(vcpu);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* Set CR0 to indicate previous transactional state */
189*4882a593Smuzhiyun vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & 0x0fffffff) |
190*4882a593Smuzhiyun (((msr & MSR_TS_MASK) >> MSR_TS_S_LG) << 29);
191*4882a593Smuzhiyun vcpu->arch.shregs.msr &= ~MSR_TS_MASK;
192*4882a593Smuzhiyun return RESUME_GUEST;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* ignore bit 31, see comment above */
195*4882a593Smuzhiyun case (PPC_INST_TRECHKPT & PO_XOP_OPCODE_MASK):
196*4882a593Smuzhiyun /* XXX do we need to check for PR=0 here? */
197*4882a593Smuzhiyun /* check for TM disabled in the HFSCR or MSR */
198*4882a593Smuzhiyun if (!(vcpu->arch.hfscr & HFSCR_TM)) {
199*4882a593Smuzhiyun /* generate an illegal instruction interrupt */
200*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
201*4882a593Smuzhiyun return RESUME_GUEST;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun if (!(msr & MSR_TM)) {
204*4882a593Smuzhiyun /* generate a facility unavailable interrupt */
205*4882a593Smuzhiyun vcpu->arch.fscr = (vcpu->arch.fscr & ~(0xffull << 56)) |
206*4882a593Smuzhiyun ((u64)FSCR_TM_LG << 56);
207*4882a593Smuzhiyun kvmppc_book3s_queue_irqprio(vcpu,
208*4882a593Smuzhiyun BOOK3S_INTERRUPT_FAC_UNAVAIL);
209*4882a593Smuzhiyun return RESUME_GUEST;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun /* If transaction active or TEXASR[FS] = 0, bad thing */
212*4882a593Smuzhiyun if (MSR_TM_ACTIVE(msr) || !(vcpu->arch.texasr & TEXASR_FS)) {
213*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGTM);
214*4882a593Smuzhiyun return RESUME_GUEST;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun copy_to_checkpoint(vcpu);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /* Set CR0 to indicate previous transactional state */
220*4882a593Smuzhiyun vcpu->arch.regs.ccr = (vcpu->arch.regs.ccr & 0x0fffffff) |
221*4882a593Smuzhiyun (((msr & MSR_TS_MASK) >> MSR_TS_S_LG) << 29);
222*4882a593Smuzhiyun vcpu->arch.shregs.msr = msr | MSR_TS_S;
223*4882a593Smuzhiyun return RESUME_GUEST;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /* What should we do here? We didn't recognize the instruction */
227*4882a593Smuzhiyun kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
228*4882a593Smuzhiyun pr_warn_ratelimited("Unrecognized TM-related instruction %#x for emulation", instr);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun return RESUME_GUEST;
231*4882a593Smuzhiyun }
232