blob: 4a68dd4050a4cc1e28265be4baf69f9ac88cbbea [file] [log] [blame]
Simon Guo009c8722018-05-23 15:01:47 +08001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Derived from book3s_hv_rmhandlers.S, which is:
12 *
13 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
14 *
15 */
16
17#include <asm/reg.h>
18#include <asm/ppc_asm.h>
19#include <asm/asm-offsets.h>
20#include <asm/export.h>
21#include <asm/tm.h>
22#include <asm/cputable.h>
23
24#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
25#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
26
27/*
28 * Save transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +080029 * Called with:
30 * - r3 pointing to the vcpu struct
31 * - r4 points to the MSR with current TS bits:
32 * (For HV KVM, it is VCPU_MSR ; For PR KVM, it is host MSR).
Simon Guo009c8722018-05-23 15:01:47 +080033 * This can modify all checkpointed registers, but
Simon Guo6f597c62018-05-23 15:01:48 +080034 * restores r1, r2 before exit.
Simon Guo009c8722018-05-23 15:01:47 +080035 */
Simon Guocaa3be92018-05-23 15:01:50 +080036_GLOBAL(__kvmppc_save_tm)
Simon Guo009c8722018-05-23 15:01:47 +080037 mflr r0
38 std r0, PPC_LR_STKOFF(r1)
39
40 /* Turn on TM. */
41 mfmsr r8
42 li r0, 1
43 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
Simon Guo7f386af2018-05-23 15:01:49 +080044 ori r8, r8, MSR_FP
45 oris r8, r8, (MSR_VEC | MSR_VSX)@h
Simon Guo009c8722018-05-23 15:01:47 +080046 mtmsrd r8
47
Simon Guo6f597c62018-05-23 15:01:48 +080048 rldicl. r4, r4, 64 - MSR_TS_S_LG, 62
Simon Guo009c8722018-05-23 15:01:47 +080049 beq 1f /* TM not active in guest. */
Simon Guo009c8722018-05-23 15:01:47 +080050
Simon Guo6f597c62018-05-23 15:01:48 +080051 std r1, HSTATE_SCRATCH2(r13)
52 std r3, HSTATE_SCRATCH1(r13)
Simon Guo009c8722018-05-23 15:01:47 +080053
54#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
55BEGIN_FTR_SECTION
56 /* Emulation of the treclaim instruction needs TEXASR before treclaim */
57 mfspr r6, SPRN_TEXASR
Simon Guo6f597c62018-05-23 15:01:48 +080058 std r6, VCPU_ORIG_TEXASR(r3)
Simon Guo009c8722018-05-23 15:01:47 +080059END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
60#endif
61
62 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
63 li r5, 0
64 mtmsrd r5, 1
65
Simon Guo6f597c62018-05-23 15:01:48 +080066 li r3, TM_CAUSE_KVM_RESCHED
67
Simon Guo009c8722018-05-23 15:01:47 +080068 /* All GPRs are volatile at this point. */
69 TRECLAIM(R3)
70
71 /* Temporarily store r13 and r9 so we have some regs to play with */
72 SET_SCRATCH0(r13)
73 GET_PACA(r13)
74 std r9, PACATMSCRATCH(r13)
Simon Guo6f597c62018-05-23 15:01:48 +080075 ld r9, HSTATE_SCRATCH1(r13)
Simon Guo009c8722018-05-23 15:01:47 +080076
77 /* Get a few more GPRs free. */
78 std r29, VCPU_GPRS_TM(29)(r9)
79 std r30, VCPU_GPRS_TM(30)(r9)
80 std r31, VCPU_GPRS_TM(31)(r9)
81
82 /* Save away PPR and DSCR soon so don't run with user values. */
83 mfspr r31, SPRN_PPR
84 HMT_MEDIUM
85 mfspr r30, SPRN_DSCR
86#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
87 ld r29, HSTATE_DSCR(r13)
88 mtspr SPRN_DSCR, r29
89#endif
90
91 /* Save all but r9, r13 & r29-r31 */
92 reg = 0
93 .rept 29
94 .if (reg != 9) && (reg != 13)
95 std reg, VCPU_GPRS_TM(reg)(r9)
96 .endif
97 reg = reg + 1
98 .endr
99 /* ... now save r13 */
100 GET_SCRATCH0(r4)
101 std r4, VCPU_GPRS_TM(13)(r9)
102 /* ... and save r9 */
103 ld r4, PACATMSCRATCH(r13)
104 std r4, VCPU_GPRS_TM(9)(r9)
105
106 /* Reload stack pointer and TOC. */
Simon Guo6f597c62018-05-23 15:01:48 +0800107 ld r1, HSTATE_SCRATCH2(r13)
Simon Guo009c8722018-05-23 15:01:47 +0800108 ld r2, PACATOC(r13)
109
110 /* Set MSR RI now we have r1 and r13 back. */
111 li r5, MSR_RI
112 mtmsrd r5, 1
113
114 /* Save away checkpinted SPRs. */
115 std r31, VCPU_PPR_TM(r9)
116 std r30, VCPU_DSCR_TM(r9)
117 mflr r5
118 mfcr r6
119 mfctr r7
120 mfspr r8, SPRN_AMR
121 mfspr r10, SPRN_TAR
122 mfxer r11
123 std r5, VCPU_LR_TM(r9)
124 stw r6, VCPU_CR_TM(r9)
125 std r7, VCPU_CTR_TM(r9)
126 std r8, VCPU_AMR_TM(r9)
127 std r10, VCPU_TAR_TM(r9)
128 std r11, VCPU_XER_TM(r9)
129
130 /* Restore r12 as trap number. */
131 lwz r12, VCPU_TRAP(r9)
132
133 /* Save FP/VSX. */
134 addi r3, r9, VCPU_FPRS_TM
135 bl store_fp_state
136 addi r3, r9, VCPU_VRS_TM
137 bl store_vr_state
138 mfspr r6, SPRN_VRSAVE
139 stw r6, VCPU_VRSAVE_TM(r9)
1401:
141 /*
142 * We need to save these SPRs after the treclaim so that the software
143 * error code is recorded correctly in the TEXASR. Also the user may
144 * change these outside of a transaction, so they must always be
145 * context switched.
146 */
147 mfspr r7, SPRN_TEXASR
148 std r7, VCPU_TEXASR(r9)
14911:
150 mfspr r5, SPRN_TFHAR
151 mfspr r6, SPRN_TFIAR
152 std r5, VCPU_TFHAR(r9)
153 std r6, VCPU_TFIAR(r9)
154
155 ld r0, PPC_LR_STKOFF(r1)
156 mtlr r0
157 blr
158
159/*
Simon Guocaa3be92018-05-23 15:01:50 +0800160 * _kvmppc_save_tm_pr() is a wrapper around __kvmppc_save_tm(), so that it can
161 * be invoked from C function by PR KVM only.
162 */
163_GLOBAL(_kvmppc_save_tm_pr)
164 mflr r5
165 std r5, PPC_LR_STKOFF(r1)
166 stdu r1, -SWITCH_FRAME_SIZE(r1)
167 SAVE_NVGPRS(r1)
168
169 /* save MSR since TM/math bits might be impacted
170 * by __kvmppc_save_tm().
171 */
172 mfmsr r5
173 SAVE_GPR(5, r1)
174
175 /* also save DSCR/CR so that it can be recovered later */
176 mfspr r6, SPRN_DSCR
177 SAVE_GPR(6, r1)
178
179 mfcr r7
180 stw r7, _CCR(r1)
181
182 bl __kvmppc_save_tm
183
184 ld r7, _CCR(r1)
185 mtcr r7
186
187 REST_GPR(6, r1)
188 mtspr SPRN_DSCR, r6
189
190 /* need preserve current MSR's MSR_TS bits */
191 REST_GPR(5, r1)
192 mfmsr r6
193 rldicl r6, r6, 64 - MSR_TS_S_LG, 62
194 rldimi r5, r6, MSR_TS_S_LG, 63 - MSR_TS_T_LG
195 mtmsrd r5
196
197 REST_NVGPRS(r1)
198 addi r1, r1, SWITCH_FRAME_SIZE
199 ld r5, PPC_LR_STKOFF(r1)
200 mtlr r5
201 blr
202
203EXPORT_SYMBOL_GPL(_kvmppc_save_tm_pr);
204
205/*
Simon Guo009c8722018-05-23 15:01:47 +0800206 * Restore transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +0800207 * Called with:
208 * - r3 pointing to the vcpu struct.
209 * - r4 is the guest MSR with desired TS bits:
210 * For HV KVM, it is VCPU_MSR
211 * For PR KVM, it is provided by caller
Simon Guo009c8722018-05-23 15:01:47 +0800212 * This potentially modifies all checkpointed registers.
Simon Guo6f597c62018-05-23 15:01:48 +0800213 * It restores r1, r2 from the PACA.
Simon Guo009c8722018-05-23 15:01:47 +0800214 */
Simon Guocaa3be92018-05-23 15:01:50 +0800215_GLOBAL(__kvmppc_restore_tm)
Simon Guo009c8722018-05-23 15:01:47 +0800216 mflr r0
217 std r0, PPC_LR_STKOFF(r1)
218
219 /* Turn on TM/FP/VSX/VMX so we can restore them. */
220 mfmsr r5
221 li r6, MSR_TM >> 32
222 sldi r6, r6, 32
223 or r5, r5, r6
224 ori r5, r5, MSR_FP
225 oris r5, r5, (MSR_VEC | MSR_VSX)@h
226 mtmsrd r5
227
228 /*
229 * The user may change these outside of a transaction, so they must
230 * always be context switched.
231 */
Simon Guo6f597c62018-05-23 15:01:48 +0800232 ld r5, VCPU_TFHAR(r3)
233 ld r6, VCPU_TFIAR(r3)
234 ld r7, VCPU_TEXASR(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800235 mtspr SPRN_TFHAR, r5
236 mtspr SPRN_TFIAR, r6
237 mtspr SPRN_TEXASR, r7
238
Simon Guo6f597c62018-05-23 15:01:48 +0800239 mr r5, r4
Simon Guo009c8722018-05-23 15:01:47 +0800240 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
241 beqlr /* TM not active in guest */
Simon Guo6f597c62018-05-23 15:01:48 +0800242 std r1, HSTATE_SCRATCH2(r13)
Simon Guo009c8722018-05-23 15:01:47 +0800243
244 /* Make sure the failure summary is set, otherwise we'll program check
245 * when we trechkpt. It's possible that this might have been not set
246 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
247 * host.
248 */
249 oris r7, r7, (TEXASR_FS)@h
250 mtspr SPRN_TEXASR, r7
251
252 /*
253 * We need to load up the checkpointed state for the guest.
254 * We need to do this early as it will blow away any GPRs, VSRs and
255 * some SPRs.
256 */
257
Simon Guo6f597c62018-05-23 15:01:48 +0800258 mr r31, r3
Simon Guo009c8722018-05-23 15:01:47 +0800259 addi r3, r31, VCPU_FPRS_TM
260 bl load_fp_state
261 addi r3, r31, VCPU_VRS_TM
262 bl load_vr_state
Simon Guo6f597c62018-05-23 15:01:48 +0800263 mr r3, r31
264 lwz r7, VCPU_VRSAVE_TM(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800265 mtspr SPRN_VRSAVE, r7
266
Simon Guo6f597c62018-05-23 15:01:48 +0800267 ld r5, VCPU_LR_TM(r3)
268 lwz r6, VCPU_CR_TM(r3)
269 ld r7, VCPU_CTR_TM(r3)
270 ld r8, VCPU_AMR_TM(r3)
271 ld r9, VCPU_TAR_TM(r3)
272 ld r10, VCPU_XER_TM(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800273 mtlr r5
274 mtcr r6
275 mtctr r7
276 mtspr SPRN_AMR, r8
277 mtspr SPRN_TAR, r9
278 mtxer r10
279
280 /*
281 * Load up PPR and DSCR values but don't put them in the actual SPRs
282 * till the last moment to avoid running with userspace PPR and DSCR for
283 * too long.
284 */
Simon Guo6f597c62018-05-23 15:01:48 +0800285 ld r29, VCPU_DSCR_TM(r3)
286 ld r30, VCPU_PPR_TM(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800287
288 std r2, PACATMSCRATCH(r13) /* Save TOC */
289
290 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
291 li r5, 0
292 mtmsrd r5, 1
293
294 /* Load GPRs r0-r28 */
295 reg = 0
296 .rept 29
297 ld reg, VCPU_GPRS_TM(reg)(r31)
298 reg = reg + 1
299 .endr
300
301 mtspr SPRN_DSCR, r29
302 mtspr SPRN_PPR, r30
303
304 /* Load final GPRs */
305 ld 29, VCPU_GPRS_TM(29)(r31)
306 ld 30, VCPU_GPRS_TM(30)(r31)
307 ld 31, VCPU_GPRS_TM(31)(r31)
308
309 /* TM checkpointed state is now setup. All GPRs are now volatile. */
310 TRECHKPT
311
312 /* Now let's get back the state we need. */
313 HMT_MEDIUM
314 GET_PACA(r13)
315#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
316 ld r29, HSTATE_DSCR(r13)
317 mtspr SPRN_DSCR, r29
Simon Guo009c8722018-05-23 15:01:47 +0800318#endif
Simon Guo6f597c62018-05-23 15:01:48 +0800319 ld r1, HSTATE_SCRATCH2(r13)
Simon Guo009c8722018-05-23 15:01:47 +0800320 ld r2, PACATMSCRATCH(r13)
321
322 /* Set the MSR RI since we have our registers back. */
323 li r5, MSR_RI
324 mtmsrd r5, 1
325 ld r0, PPC_LR_STKOFF(r1)
326 mtlr r0
327 blr
Simon Guocaa3be92018-05-23 15:01:50 +0800328
329/*
330 * _kvmppc_restore_tm_pr() is a wrapper around __kvmppc_restore_tm(), so that it
331 * can be invoked from C function by PR KVM only.
332 */
333_GLOBAL(_kvmppc_restore_tm_pr)
334 mflr r5
335 std r5, PPC_LR_STKOFF(r1)
336 stdu r1, -SWITCH_FRAME_SIZE(r1)
337 SAVE_NVGPRS(r1)
338
339 /* save MSR to avoid TM/math bits change */
340 mfmsr r5
341 SAVE_GPR(5, r1)
342
343 /* also save DSCR/CR so that it can be recovered later */
344 mfspr r6, SPRN_DSCR
345 SAVE_GPR(6, r1)
346
347 mfcr r7
348 stw r7, _CCR(r1)
349
350 bl __kvmppc_restore_tm
351
352 ld r7, _CCR(r1)
353 mtcr r7
354
355 REST_GPR(6, r1)
356 mtspr SPRN_DSCR, r6
357
358 /* need preserve current MSR's MSR_TS bits */
359 REST_GPR(5, r1)
360 mfmsr r6
361 rldicl r6, r6, 64 - MSR_TS_S_LG, 62
362 rldimi r5, r6, MSR_TS_S_LG, 63 - MSR_TS_T_LG
363 mtmsrd r5
364
365 REST_NVGPRS(r1)
366 addi r1, r1, SWITCH_FRAME_SIZE
367 ld r5, PPC_LR_STKOFF(r1)
368 mtlr r5
369 blr
370
371EXPORT_SYMBOL_GPL(_kvmppc_restore_tm_pr);
Simon Guo009c8722018-05-23 15:01:47 +0800372#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */