1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * Copyright SUSE Linux Products GmbH 2010
6 * Authors: Alexander Graf <agraf@suse.de>
9 /* Real mode helpers */
11 #include <asm/asm-compat.h>
12 #include <asm/feature-fixups.h>
14 #if defined(CONFIG_PPC_BOOK3S_64)
16 #define GET_SHADOW_VCPU(reg) \
19 #elif defined(CONFIG_PPC_BOOK3S_32)
21 #define GET_SHADOW_VCPU(reg) \
23 lwz reg, (THREAD + THREAD_KVM_SVCPU)(reg); \
28 /* Disable for nested KVM */
29 #define USE_QUICK_LAST_INST
32 /* Get helper functions for subarch specific functionality */
34 #if defined(CONFIG_PPC_BOOK3S_64)
35 #include "book3s_64_slb.S"
36 #elif defined(CONFIG_PPC_BOOK3S_32)
37 #include "book3s_32_sr.S"
40 /******************************************************************************
44 *****************************************************************************/
46 .global kvmppc_handler_trampoline_enter
47 kvmppc_handler_trampoline_enter:
54 * R4 = guest shadow MSR
55 * R5 = normal host MSR
56 * R6 = current host MSR (EE, IR, DR off)
57 * LR = highmem guest exit code
58 * all other volatile GPRS = free
59 * SVCPU[CR] = guest CR
60 * SVCPU[XER] = guest XER
61 * SVCPU[CTR] = guest CTR
62 * SVCPU[LR] = guest LR
65 /* r3 = shadow vcpu */
68 /* Save guest exit handler address and MSR */
70 PPC_STL r0, HSTATE_VMHANDLER(r3)
71 PPC_STL r5, HSTATE_HOST_MSR(r3)
73 /* Save R1/R2 in the PACA (64-bit) or shadow_vcpu (32-bit) */
74 PPC_STL r1, HSTATE_HOST_R1(r3)
75 PPC_STL r2, HSTATE_HOST_R2(r3)
77 /* Activate guest mode, so faults get handled by KVM */
78 li r11, KVM_GUEST_MODE_GUEST
79 stb r11, HSTATE_IN_GUEST(r3)
81 /* Switch to guest segment. This is subarch specific. */
84 #ifdef CONFIG_PPC_BOOK3S_64
88 std r8, HSTATE_HOST_FSCR(r13)
89 /* Set FSCR during guest execution */
90 ld r9, SVCPU_SHADOW_FSCR(r13)
92 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
94 /* Some guests may need to have dcbz set to 32 byte length.
96 * Usually we ensure that by patching the guest's instructions
97 * to trap on dcbz and emulate it in the hypervisor.
99 * If we can, we should tell the CPU to use 32 byte dcbz though,
100 * because that's a lot faster.
102 lbz r0, HSTATE_RESTORE_HID5(r3)
107 ori r0, r0, 0x80 /* XXX HID5_dcbz32 = 0x80 */
111 #endif /* CONFIG_PPC_BOOK3S_64 */
115 PPC_LL r8, SVCPU_CTR(r3)
116 PPC_LL r9, SVCPU_LR(r3)
117 lwz r10, SVCPU_CR(r3)
118 PPC_LL r11, SVCPU_XER(r3)
125 /* Move SRR0 and SRR1 into the respective regs */
126 PPC_LL r9, SVCPU_PC(r3)
127 /* First clear RI in our current MSR value */
131 PPC_LL r0, SVCPU_R0(r3)
132 PPC_LL r1, SVCPU_R1(r3)
133 PPC_LL r2, SVCPU_R2(r3)
134 PPC_LL r5, SVCPU_R5(r3)
135 PPC_LL r7, SVCPU_R7(r3)
136 PPC_LL r8, SVCPU_R8(r3)
137 PPC_LL r10, SVCPU_R10(r3)
138 PPC_LL r11, SVCPU_R11(r3)
139 PPC_LL r12, SVCPU_R12(r3)
140 PPC_LL r13, SVCPU_R13(r3)
146 PPC_LL r4, SVCPU_R4(r3)
147 PPC_LL r6, SVCPU_R6(r3)
148 PPC_LL r9, SVCPU_R9(r3)
149 PPC_LL r3, (SVCPU_R3)(r3)
152 kvmppc_handler_trampoline_enter_end:
156 /******************************************************************************
160 *****************************************************************************/
162 .global kvmppc_interrupt_pr
164 /* 64-bit entry. Register usage at this point:
166 * SPRG_SCRATCH0 = guest R13
167 * R12 = (guest CR << 32) | exit handler id
169 * HSTATE.SCRATCH0 = guest R12
170 * HSTATE.SCRATCH1 = guest CTR if RELOCATABLE
173 /* Match 32-bit entry */
174 #ifdef CONFIG_RELOCATABLE
175 std r9, HSTATE_SCRATCH2(r13)
176 ld r9, HSTATE_SCRATCH1(r13)
178 ld r9, HSTATE_SCRATCH2(r13)
180 rotldi r12, r12, 32 /* Flip R12 halves for stw */
181 stw r12, HSTATE_SCRATCH1(r13) /* CR is now in the low half */
182 srdi r12, r12, 32 /* shift trap into low half */
185 .global kvmppc_handler_trampoline_exit
186 kvmppc_handler_trampoline_exit:
187 /* Register usage at this point:
189 * SPRG_SCRATCH0 = guest R13
190 * R12 = exit handler id
191 * R13 = shadow vcpu (32-bit) or PACA (64-bit)
192 * HSTATE.SCRATCH0 = guest R12
193 * HSTATE.SCRATCH1 = guest CR
198 PPC_STL r0, SVCPU_R0(r13)
199 PPC_STL r1, SVCPU_R1(r13)
200 PPC_STL r2, SVCPU_R2(r13)
201 PPC_STL r3, SVCPU_R3(r13)
202 PPC_STL r4, SVCPU_R4(r13)
203 PPC_STL r5, SVCPU_R5(r13)
204 PPC_STL r6, SVCPU_R6(r13)
205 PPC_STL r7, SVCPU_R7(r13)
206 PPC_STL r8, SVCPU_R8(r13)
207 PPC_STL r9, SVCPU_R9(r13)
208 PPC_STL r10, SVCPU_R10(r13)
209 PPC_STL r11, SVCPU_R11(r13)
211 /* Restore R1/R2 so we can handle faults */
212 PPC_LL r1, HSTATE_HOST_R1(r13)
213 PPC_LL r2, HSTATE_HOST_R2(r13)
215 /* Save guest PC and MSR */
225 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
230 PPC_STL r3, SVCPU_PC(r13)
231 PPC_STL r4, SVCPU_SHADOW_SRR1(r13)
233 /* Get scratch'ed off registers */
235 PPC_LL r8, HSTATE_SCRATCH0(r13)
236 lwz r7, HSTATE_SCRATCH1(r13)
238 PPC_STL r9, SVCPU_R13(r13)
239 PPC_STL r8, SVCPU_R12(r13)
240 stw r7, SVCPU_CR(r13)
242 /* Save more register state */
250 PPC_STL r5, SVCPU_XER(r13)
251 PPC_STL r6, SVCPU_FAULT_DAR(r13)
252 stw r7, SVCPU_FAULT_DSISR(r13)
253 PPC_STL r8, SVCPU_CTR(r13)
254 PPC_STL r9, SVCPU_LR(r13)
257 * In order for us to easily get the last instruction,
258 * we got the #vmexit at, we exploit the fact that the
259 * virtual layout is still the same here, so we can just
260 * ld from the guest's PC address
263 /* We only load the last instruction when it's safe */
264 cmpwi r12, BOOK3S_INTERRUPT_DATA_STORAGE
266 cmpwi r12, BOOK3S_INTERRUPT_PROGRAM
268 cmpwi r12, BOOK3S_INTERRUPT_SYSCALL
269 beq ld_last_prev_inst
270 cmpwi r12, BOOK3S_INTERRUPT_ALIGNMENT
274 cmpwi r12, BOOK3S_INTERRUPT_H_EMUL_ASSIST
276 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
278 cmpwi r12, BOOK3S_INTERRUPT_FAC_UNAVAIL
280 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
289 /* Save off the guest instruction we're at */
291 /* In case lwz faults */
292 li r0, KVM_INST_FETCH_FAILED
294 #ifdef USE_QUICK_LAST_INST
296 /* Set guest mode to 'jump over instruction' so if lwz faults
297 * we'll just continue at the next IP. */
298 li r9, KVM_GUEST_MODE_SKIP
299 stb r9, HSTATE_IN_GUEST(r13)
301 /* 1) enable paging for data */
303 ori r11, r9, MSR_DR /* Enable paging for data */
306 /* 2) fetch the instruction */
308 /* 3) disable paging again */
313 stw r0, SVCPU_LAST_INST(r13)
317 /* Unset guest mode */
318 li r9, KVM_GUEST_MODE_NONE
319 stb r9, HSTATE_IN_GUEST(r13)
321 /* Switch back to host MMU */
324 #ifdef CONFIG_PPC_BOOK3S_64
326 lbz r5, HSTATE_RESTORE_HID5(r13)
338 /* Save guest FSCR on a FAC_UNAVAIL interrupt */
339 cmpwi r12, BOOK3S_INTERRUPT_FAC_UNAVAIL
342 std r7, SVCPU_SHADOW_FSCR(r13)
344 /* Restore host FSCR */
345 ld r8, HSTATE_HOST_FSCR(r13)
347 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
349 #endif /* CONFIG_PPC_BOOK3S_64 */
352 * For some interrupts, we need to call the real Linux
353 * handler, so it can do work for us. This has to happen
354 * as if the interrupt arrived from the kernel though,
355 * so let's fake it here where most state is restored.
357 * Having set up SRR0/1 with the address where we want
358 * to continue with relocation on (potentially in module
359 * space), we either just go straight there with rfi[d],
360 * or we jump to an interrupt handler if there is an
361 * interrupt to be handled first. In the latter case,
362 * the rfi[d] at the end of the interrupt handler will
363 * get us back to where we want to continue.
366 /* Register usage at this point:
370 * R10 = raw exit handler id
371 * R12 = exit handler id
372 * R13 = shadow vcpu (32-bit) or PACA (64-bit)
377 PPC_LL r6, HSTATE_HOST_MSR(r13)
378 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
380 * We don't want to change MSR[TS] bits via rfi here.
381 * The actual TM handling logic will be in host with
382 * recovered DR/IR bits after HSTATE_VMHANDLER.
383 * And MSR_TM can be enabled in HOST_MSR so rfid may
384 * not suppress this change and can lead to exception.
385 * Manually set MSR to prevent TS state change here.
388 rldicl r7, r7, 64 - MSR_TS_S_LG, 62
389 rldimi r6, r7, MSR_TS_S_LG, 63 - MSR_TS_T_LG
391 PPC_LL r8, HSTATE_VMHANDLER(r13)
398 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
400 1: /* Restore host msr -> SRR1 */
402 /* Load highmem handler address */
405 /* RFI into the highmem handler, or jump to interrupt handler */
406 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
407 beqa BOOK3S_INTERRUPT_EXTERNAL
408 cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
409 beqa BOOK3S_INTERRUPT_DECREMENTER
410 cmpwi r12, BOOK3S_INTERRUPT_PERFMON
411 beqa BOOK3S_INTERRUPT_PERFMON
412 cmpwi r12, BOOK3S_INTERRUPT_DOORBELL
413 beqa BOOK3S_INTERRUPT_DOORBELL
416 kvmppc_handler_trampoline_exit_end: