1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * Derived from book3s_hv_rmhandlers.S, which is:
6 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
10 #include <asm/ppc_asm.h>
11 #include <asm/asm-offsets.h>
12 #include <asm/export.h>
14 #include <asm/cputable.h>
16 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
17 #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
20 * Save transactional state and TM-related registers.
22 * - r3 pointing to the vcpu struct
23 * - r4 containing the MSR with current TS bits:
24 * (For HV KVM, it is VCPU_MSR ; For PR KVM, it is host MSR).
25 * - r5 containing a flag indicating that non-volatile registers
27 * If r5 == 0, this can modify all checkpointed registers, but
28 * restores r1, r2 before exit. If r5 != 0, this restores the
29 * MSR TM/FP/VEC/VSX bits to their state on entry.
31 _GLOBAL(__kvmppc_save_tm)
33 std r0, PPC_LR_STKOFF(r1)
34 stdu r1, -SWITCH_FRAME_SIZE(r1)
43 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
45 oris r8, r8, (MSR_VEC | MSR_VSX)@h
48 rldicl. r4, r4, 64 - MSR_TS_S_LG, 62
49 beq 1f /* TM not active in guest. */
51 std r1, HSTATE_SCRATCH2(r13)
52 std r3, HSTATE_SCRATCH1(r13)
54 /* Save CR on the stack - even if r5 == 0 we need to get cr7 back. */
58 /* Save DSCR so we can restore it to avoid running with user value */
63 * We are going to do treclaim., which will modify all checkpointed
64 * registers. Save the non-volatile registers on the stack if
65 * preservation of non-volatile state has been requested.
70 /* MSR[TS] will be 0 (non-transactional) once we do treclaim. */
72 rldimi r10, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
73 SAVE_GPR(10, r1) /* final MSR value */
75 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
77 /* Emulation of the treclaim instruction needs TEXASR before treclaim */
79 std r6, VCPU_ORIG_TEXASR(r3)
80 END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
83 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
87 li r3, TM_CAUSE_KVM_RESCHED
89 /* All GPRs are volatile at this point. */
92 /* Temporarily store r13 and r9 so we have some regs to play with */
95 std r9, PACATMSCRATCH(r13)
96 ld r9, HSTATE_SCRATCH1(r13)
98 /* Save away PPR soon so we don't run with user value. */
99 std r0, VCPU_GPRS_TM(0)(r9)
103 /* Reload stack pointer. */
104 std r1, VCPU_GPRS_TM(1)(r9)
105 ld r1, HSTATE_SCRATCH2(r13)
107 /* Set MSR RI now we have r1 and r13 back. */
108 std r2, VCPU_GPRS_TM(2)(r9)
112 /* Reload TOC pointer. */
115 /* Save all but r0-r2, r9 & r13 */
118 .if (reg != 9) && (reg != 13)
119 std reg, VCPU_GPRS_TM(reg)(r9)
123 /* ... now save r13 */
125 std r4, VCPU_GPRS_TM(13)(r9)
126 /* ... and save r9 */
127 ld r4, PACATMSCRATCH(r13)
128 std r4, VCPU_GPRS_TM(9)(r9)
130 /* Restore host DSCR and CR values, after saving guest values */
133 stw r6, VCPU_CR_TM(r9)
134 std r7, VCPU_DSCR_TM(r9)
140 /* Save away checkpointed SPRs. */
141 std r0, VCPU_PPR_TM(r9)
147 std r5, VCPU_LR_TM(r9)
148 std r7, VCPU_CTR_TM(r9)
149 std r8, VCPU_AMR_TM(r9)
150 std r10, VCPU_TAR_TM(r9)
151 std r11, VCPU_XER_TM(r9)
154 addi r3, r9, VCPU_FPRS_TM
156 addi r3, r9, VCPU_VRS_TM
158 mfspr r6, SPRN_VRSAVE
159 stw r6, VCPU_VRSAVE_TM(r9)
161 /* Restore non-volatile registers if requested to */
167 * We need to save these SPRs after the treclaim so that the software
168 * error code is recorded correctly in the TEXASR. Also the user may
169 * change these outside of a transaction, so they must always be
172 mfspr r7, SPRN_TEXASR
173 std r7, VCPU_TEXASR(r9)
176 std r5, VCPU_TFHAR(r9)
177 std r6, VCPU_TFIAR(r9)
179 /* Restore MSR state if requested */
183 addi r1, r1, SWITCH_FRAME_SIZE
184 ld r0, PPC_LR_STKOFF(r1)
189 * _kvmppc_save_tm_pr() is a wrapper around __kvmppc_save_tm(), so that it can
190 * be invoked from C function by PR KVM only.
192 _GLOBAL(_kvmppc_save_tm_pr)
194 std r0, PPC_LR_STKOFF(r1)
195 stdu r1, -PPC_MIN_STKFRM(r1)
198 std r8, PPC_MIN_STKFRM-8(r1)
200 li r5, 1 /* preserve non-volatile registers */
203 ld r8, PPC_MIN_STKFRM-8(r1)
206 addi r1, r1, PPC_MIN_STKFRM
207 ld r0, PPC_LR_STKOFF(r1)
211 EXPORT_SYMBOL_GPL(_kvmppc_save_tm_pr);
214 * Restore transactional state and TM-related registers.
216 * - r3 pointing to the vcpu struct.
217 * - r4 is the guest MSR with desired TS bits:
218 * For HV KVM, it is VCPU_MSR
219 * For PR KVM, it is provided by caller
220 * - r5 containing a flag indicating that non-volatile registers
222 * If r5 == 0, this potentially modifies all checkpointed registers, but
223 * restores r1, r2 from the PACA before exit.
224 * If r5 != 0, this restores the MSR TM/FP/VEC/VSX bits to their state on entry.
226 _GLOBAL(__kvmppc_restore_tm)
228 std r0, PPC_LR_STKOFF(r1)
232 /* Turn on TM/FP/VSX/VMX so we can restore them. */
239 oris r5, r5, (MSR_VEC | MSR_VSX)@h
243 * The user may change these outside of a transaction, so they must
244 * always be context switched.
246 ld r5, VCPU_TFHAR(r3)
247 ld r6, VCPU_TFIAR(r3)
248 ld r7, VCPU_TEXASR(r3)
251 mtspr SPRN_TEXASR, r7
254 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
255 beq 9f /* TM not active in guest */
257 /* Make sure the failure summary is set, otherwise we'll program check
258 * when we trechkpt. It's possible that this might have been not set
259 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
262 oris r7, r7, (TEXASR_FS)@h
263 mtspr SPRN_TEXASR, r7
266 * Make a stack frame and save non-volatile registers if requested.
268 stdu r1, -SWITCH_FRAME_SIZE(r1)
269 std r1, HSTATE_SCRATCH2(r13)
280 /* MSR[TS] will be 1 (suspended) once we do trechkpt */
282 rldimi r10, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
283 SAVE_GPR(10, r1) /* final MSR value */
286 * We need to load up the checkpointed state for the guest.
287 * We need to do this early as it will blow away any GPRs, VSRs and
292 addi r3, r31, VCPU_FPRS_TM
294 addi r3, r31, VCPU_VRS_TM
297 lwz r7, VCPU_VRSAVE_TM(r3)
298 mtspr SPRN_VRSAVE, r7
300 ld r5, VCPU_LR_TM(r3)
301 lwz r6, VCPU_CR_TM(r3)
302 ld r7, VCPU_CTR_TM(r3)
303 ld r8, VCPU_AMR_TM(r3)
304 ld r9, VCPU_TAR_TM(r3)
305 ld r10, VCPU_XER_TM(r3)
314 * Load up PPR and DSCR values but don't put them in the actual SPRs
315 * till the last moment to avoid running with userspace PPR and DSCR for
318 ld r29, VCPU_DSCR_TM(r3)
319 ld r30, VCPU_PPR_TM(r3)
321 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
325 /* Load GPRs r0-r28 */
328 ld reg, VCPU_GPRS_TM(reg)(r31)
335 /* Load final GPRs */
336 ld 29, VCPU_GPRS_TM(29)(r31)
337 ld 30, VCPU_GPRS_TM(30)(r31)
338 ld 31, VCPU_GPRS_TM(31)(r31)
340 /* TM checkpointed state is now setup. All GPRs are now volatile. */
343 /* Now let's get back the state we need. */
346 ld r1, HSTATE_SCRATCH2(r13)
350 /* Set the MSR RI since we have our registers back. */
354 /* Restore TOC pointer and CR */
359 /* Restore non-volatile registers if requested to. */
364 5: addi r1, r1, SWITCH_FRAME_SIZE
365 ld r0, PPC_LR_STKOFF(r1)
368 9: /* Restore MSR bits if requested */
374 * _kvmppc_restore_tm_pr() is a wrapper around __kvmppc_restore_tm(), so that it
375 * can be invoked from C function by PR KVM only.
377 _GLOBAL(_kvmppc_restore_tm_pr)
379 std r0, PPC_LR_STKOFF(r1)
380 stdu r1, -PPC_MIN_STKFRM(r1)
382 /* save TAR so that it can be recovered later */
384 std r8, PPC_MIN_STKFRM-8(r1)
387 bl __kvmppc_restore_tm
389 ld r8, PPC_MIN_STKFRM-8(r1)
392 addi r1, r1, PPC_MIN_STKFRM
393 ld r0, PPC_LR_STKOFF(r1)
397 EXPORT_SYMBOL_GPL(_kvmppc_restore_tm_pr);
398 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */