blob: 2760d7acd3715e684b6e760e3a229a113f3e7221 [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 */
36_GLOBAL(kvmppc_save_tm)
37 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/*
160 * Restore transactional state and TM-related registers.
Simon Guo6f597c62018-05-23 15:01:48 +0800161 * Called with:
162 * - r3 pointing to the vcpu struct.
163 * - r4 is the guest MSR with desired TS bits:
164 * For HV KVM, it is VCPU_MSR
165 * For PR KVM, it is provided by caller
Simon Guo009c8722018-05-23 15:01:47 +0800166 * This potentially modifies all checkpointed registers.
Simon Guo6f597c62018-05-23 15:01:48 +0800167 * It restores r1, r2 from the PACA.
Simon Guo009c8722018-05-23 15:01:47 +0800168 */
169_GLOBAL(kvmppc_restore_tm)
170 mflr r0
171 std r0, PPC_LR_STKOFF(r1)
172
173 /* Turn on TM/FP/VSX/VMX so we can restore them. */
174 mfmsr r5
175 li r6, MSR_TM >> 32
176 sldi r6, r6, 32
177 or r5, r5, r6
178 ori r5, r5, MSR_FP
179 oris r5, r5, (MSR_VEC | MSR_VSX)@h
180 mtmsrd r5
181
182 /*
183 * The user may change these outside of a transaction, so they must
184 * always be context switched.
185 */
Simon Guo6f597c62018-05-23 15:01:48 +0800186 ld r5, VCPU_TFHAR(r3)
187 ld r6, VCPU_TFIAR(r3)
188 ld r7, VCPU_TEXASR(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800189 mtspr SPRN_TFHAR, r5
190 mtspr SPRN_TFIAR, r6
191 mtspr SPRN_TEXASR, r7
192
Simon Guo6f597c62018-05-23 15:01:48 +0800193 mr r5, r4
Simon Guo009c8722018-05-23 15:01:47 +0800194 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
195 beqlr /* TM not active in guest */
Simon Guo6f597c62018-05-23 15:01:48 +0800196 std r1, HSTATE_SCRATCH2(r13)
Simon Guo009c8722018-05-23 15:01:47 +0800197
198 /* Make sure the failure summary is set, otherwise we'll program check
199 * when we trechkpt. It's possible that this might have been not set
200 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
201 * host.
202 */
203 oris r7, r7, (TEXASR_FS)@h
204 mtspr SPRN_TEXASR, r7
205
206 /*
207 * We need to load up the checkpointed state for the guest.
208 * We need to do this early as it will blow away any GPRs, VSRs and
209 * some SPRs.
210 */
211
Simon Guo6f597c62018-05-23 15:01:48 +0800212 mr r31, r3
Simon Guo009c8722018-05-23 15:01:47 +0800213 addi r3, r31, VCPU_FPRS_TM
214 bl load_fp_state
215 addi r3, r31, VCPU_VRS_TM
216 bl load_vr_state
Simon Guo6f597c62018-05-23 15:01:48 +0800217 mr r3, r31
218 lwz r7, VCPU_VRSAVE_TM(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800219 mtspr SPRN_VRSAVE, r7
220
Simon Guo6f597c62018-05-23 15:01:48 +0800221 ld r5, VCPU_LR_TM(r3)
222 lwz r6, VCPU_CR_TM(r3)
223 ld r7, VCPU_CTR_TM(r3)
224 ld r8, VCPU_AMR_TM(r3)
225 ld r9, VCPU_TAR_TM(r3)
226 ld r10, VCPU_XER_TM(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800227 mtlr r5
228 mtcr r6
229 mtctr r7
230 mtspr SPRN_AMR, r8
231 mtspr SPRN_TAR, r9
232 mtxer r10
233
234 /*
235 * Load up PPR and DSCR values but don't put them in the actual SPRs
236 * till the last moment to avoid running with userspace PPR and DSCR for
237 * too long.
238 */
Simon Guo6f597c62018-05-23 15:01:48 +0800239 ld r29, VCPU_DSCR_TM(r3)
240 ld r30, VCPU_PPR_TM(r3)
Simon Guo009c8722018-05-23 15:01:47 +0800241
242 std r2, PACATMSCRATCH(r13) /* Save TOC */
243
244 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
245 li r5, 0
246 mtmsrd r5, 1
247
248 /* Load GPRs r0-r28 */
249 reg = 0
250 .rept 29
251 ld reg, VCPU_GPRS_TM(reg)(r31)
252 reg = reg + 1
253 .endr
254
255 mtspr SPRN_DSCR, r29
256 mtspr SPRN_PPR, r30
257
258 /* Load final GPRs */
259 ld 29, VCPU_GPRS_TM(29)(r31)
260 ld 30, VCPU_GPRS_TM(30)(r31)
261 ld 31, VCPU_GPRS_TM(31)(r31)
262
263 /* TM checkpointed state is now setup. All GPRs are now volatile. */
264 TRECHKPT
265
266 /* Now let's get back the state we need. */
267 HMT_MEDIUM
268 GET_PACA(r13)
269#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
270 ld r29, HSTATE_DSCR(r13)
271 mtspr SPRN_DSCR, r29
Simon Guo009c8722018-05-23 15:01:47 +0800272#endif
Simon Guo6f597c62018-05-23 15:01:48 +0800273 ld r1, HSTATE_SCRATCH2(r13)
Simon Guo009c8722018-05-23 15:01:47 +0800274 ld r2, PACATMSCRATCH(r13)
275
276 /* Set the MSR RI since we have our registers back. */
277 li r5, MSR_RI
278 mtmsrd r5, 1
279 ld r0, PPC_LR_STKOFF(r1)
280 mtlr r0
281 blr
282#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */