blob: c54b0e30cf3f1c7331f90e78e80e71a88ee6d20c [file] [log] [blame]
Alexander Graf29eb61b2009-10-30 05:47:07 +00001/*
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 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
23#include <asm/page.h>
24#include <asm/asm-offsets.h>
25#include <asm/exception-64s.h>
26
Alexander Grafb79fcdf2010-04-16 00:11:47 +020027#if defined(CONFIG_PPC_BOOK3S_64)
Alexander Graf29eb61b2009-10-30 05:47:07 +000028
Alexander Grafb79fcdf2010-04-16 00:11:47 +020029#define ULONG_SIZE 8
30#define FUNC(name) GLUE(.,name)
Alexander Graf29eb61b2009-10-30 05:47:07 +000031
Paul Mackerras3c42bf82011-06-29 00:20:58 +000032#define GET_SHADOW_VCPU_R13
Alexander Grafb79fcdf2010-04-16 00:11:47 +020033
34#define DISABLE_INTERRUPTS \
35 mfmsr r0; \
36 rldicl r0,r0,48,1; \
37 rotldi r0,r0,16; \
38 mtmsrd r0,1; \
39
40#elif defined(CONFIG_PPC_BOOK3S_32)
41
42#define ULONG_SIZE 4
43#define FUNC(name) name
44
Paul Mackerras3c42bf82011-06-29 00:20:58 +000045#define GET_SHADOW_VCPU_R13 \
46 lwz r13, (THREAD + THREAD_KVM_SVCPU)(r2)
Alexander Grafb79fcdf2010-04-16 00:11:47 +020047
48#define DISABLE_INTERRUPTS \
49 mfmsr r0; \
50 rlwinm r0,r0,0,17,15; \
51 mtmsr r0; \
52
53#endif /* CONFIG_PPC_BOOK3S_XX */
54
55
56#define VCPU_GPR(n) (VCPU_GPRS + (n * ULONG_SIZE))
Alexander Graf97c4cfb2010-01-04 22:19:25 +010057#define VCPU_LOAD_NVGPRS(vcpu) \
Alexander Grafb79fcdf2010-04-16 00:11:47 +020058 PPC_LL r14, VCPU_GPR(r14)(vcpu); \
59 PPC_LL r15, VCPU_GPR(r15)(vcpu); \
60 PPC_LL r16, VCPU_GPR(r16)(vcpu); \
61 PPC_LL r17, VCPU_GPR(r17)(vcpu); \
62 PPC_LL r18, VCPU_GPR(r18)(vcpu); \
63 PPC_LL r19, VCPU_GPR(r19)(vcpu); \
64 PPC_LL r20, VCPU_GPR(r20)(vcpu); \
65 PPC_LL r21, VCPU_GPR(r21)(vcpu); \
66 PPC_LL r22, VCPU_GPR(r22)(vcpu); \
67 PPC_LL r23, VCPU_GPR(r23)(vcpu); \
68 PPC_LL r24, VCPU_GPR(r24)(vcpu); \
69 PPC_LL r25, VCPU_GPR(r25)(vcpu); \
70 PPC_LL r26, VCPU_GPR(r26)(vcpu); \
71 PPC_LL r27, VCPU_GPR(r27)(vcpu); \
72 PPC_LL r28, VCPU_GPR(r28)(vcpu); \
73 PPC_LL r29, VCPU_GPR(r29)(vcpu); \
74 PPC_LL r30, VCPU_GPR(r30)(vcpu); \
75 PPC_LL r31, VCPU_GPR(r31)(vcpu); \
Alexander Graf97c4cfb2010-01-04 22:19:25 +010076
Alexander Graf29eb61b2009-10-30 05:47:07 +000077/*****************************************************************************
78 * *
79 * Guest entry / exit code that is in kernel module memory (highmem) *
80 * *
81 ****************************************************************************/
82
83/* Registers:
84 * r3: kvm_run pointer
85 * r4: vcpu pointer
86 */
Paul Mackerrasdf6909e52011-06-29 00:19:50 +000087_GLOBAL(__kvmppc_vcpu_run)
Alexander Graf29eb61b2009-10-30 05:47:07 +000088
89kvm_start_entry:
90 /* Write correct stack frame */
Alexander Grafb79fcdf2010-04-16 00:11:47 +020091 mflr r0
92 PPC_STL r0,PPC_LR_STKOFF(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +000093
94 /* Save host state to the stack */
Alexander Grafb79fcdf2010-04-16 00:11:47 +020095 PPC_STLU r1, -SWITCH_FRAME_SIZE(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +000096
97 /* Save r3 (kvm_run) and r4 (vcpu) */
98 SAVE_2GPRS(3, r1)
99
100 /* Save non-volatile registers (r14 - r31) */
101 SAVE_NVGPRS(r1)
102
103 /* Save LR */
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200104 PPC_STL r0, _LINK(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000105
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100106 /* Load non-volatile guest state from the vcpu */
107 VCPU_LOAD_NVGPRS(r4)
108
Alexander Graf29eb61b2009-10-30 05:47:07 +0000109kvm_start_lightweight:
110
Paul Mackerras3c42bf82011-06-29 00:20:58 +0000111 GET_SHADOW_VCPU_R13
112 PPC_LL r3, VCPU_HIGHMEM_HANDLER(r4)
113 PPC_STL r3, HSTATE_VMHANDLER(r13)
114
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200115 PPC_LL r10, VCPU_SHADOW_MSR(r4) /* r10 = vcpu->arch.shadow_msr */
Alexander Graf29eb61b2009-10-30 05:47:07 +0000116
Alexander Graf7e57cba2010-01-08 02:58:03 +0100117 DISABLE_INTERRUPTS
Alexander Graf29eb61b2009-10-30 05:47:07 +0000118
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200119#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf29eb61b2009-10-30 05:47:07 +0000120 /* Some guests may need to have dcbz set to 32 byte length.
121 *
122 * Usually we ensure that by patching the guest's instructions
123 * to trap on dcbz and emulate it in the hypervisor.
124 *
125 * If we can, we should tell the CPU to use 32 byte dcbz though,
126 * because that's a lot faster.
127 */
128
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200129 PPC_LL r3, VCPU_HFLAGS(r4)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000130 rldicl. r3, r3, 0, 63 /* CR = ((r3 & 1) == 0) */
131 beq no_dcbz32_on
132
133 mfspr r3,SPRN_HID5
134 ori r3, r3, 0x80 /* XXX HID5_dcbz32 = 0x80 */
135 mtspr SPRN_HID5,r3
136
137no_dcbz32_on:
Alexander Graf29eb61b2009-10-30 05:47:07 +0000138
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200139#endif /* CONFIG_PPC_BOOK3S_64 */
140
141 PPC_LL r6, VCPU_RMCALL(r4)
Alexander Graf021ec9c2010-01-08 02:58:06 +0100142 mtctr r6
Alexander Graf29eb61b2009-10-30 05:47:07 +0000143
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200144 PPC_LL r3, VCPU_TRAMPOLINE_ENTER(r4)
Alexander Graf021ec9c2010-01-08 02:58:06 +0100145 LOAD_REG_IMMEDIATE(r4, MSR_KERNEL & ~(MSR_IR | MSR_DR))
Alexander Graf7e57cba2010-01-08 02:58:03 +0100146
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200147 /* Jump to segment patching handler and into our guest */
Alexander Graf021ec9c2010-01-08 02:58:06 +0100148 bctr
Alexander Graf29eb61b2009-10-30 05:47:07 +0000149
150/*
151 * This is the handler in module memory. It gets jumped at from the
152 * lowmem trampoline code, so it's basically the guest exit code.
153 *
154 */
155
156.global kvmppc_handler_highmem
157kvmppc_handler_highmem:
158
159 /*
160 * Register usage at this point:
161 *
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200162 * R1 = host R1
163 * R2 = host R2
164 * R12 = exit handler id
165 * R13 = PACA
166 * SVCPU.* = guest *
Alexander Graf29eb61b2009-10-30 05:47:07 +0000167 *
168 */
169
Alexander Graf7e57cba2010-01-08 02:58:03 +0100170 /* R7 = vcpu */
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200171 PPC_LL r7, GPR4(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000172
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200173#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf29eb61b2009-10-30 05:47:07 +0000174
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200175 PPC_LL r5, VCPU_HFLAGS(r7)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000176 rldicl. r5, r5, 0, 63 /* CR = ((r5 & 1) == 0) */
177 beq no_dcbz32_off
178
Alexander Grafd35feb22010-01-08 02:58:08 +0100179 li r4, 0
Alexander Graf29eb61b2009-10-30 05:47:07 +0000180 mfspr r5,SPRN_HID5
Alexander Grafd35feb22010-01-08 02:58:08 +0100181 rldimi r5,r4,6,56
Alexander Graf29eb61b2009-10-30 05:47:07 +0000182 mtspr SPRN_HID5,r5
183
184no_dcbz32_off:
185
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200186#endif /* CONFIG_PPC_BOOK3S_64 */
Alexander Graf29eb61b2009-10-30 05:47:07 +0000187
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200188 PPC_STL r14, VCPU_GPR(r14)(r7)
189 PPC_STL r15, VCPU_GPR(r15)(r7)
190 PPC_STL r16, VCPU_GPR(r16)(r7)
191 PPC_STL r17, VCPU_GPR(r17)(r7)
192 PPC_STL r18, VCPU_GPR(r18)(r7)
193 PPC_STL r19, VCPU_GPR(r19)(r7)
194 PPC_STL r20, VCPU_GPR(r20)(r7)
195 PPC_STL r21, VCPU_GPR(r21)(r7)
196 PPC_STL r22, VCPU_GPR(r22)(r7)
197 PPC_STL r23, VCPU_GPR(r23)(r7)
198 PPC_STL r24, VCPU_GPR(r24)(r7)
199 PPC_STL r25, VCPU_GPR(r25)(r7)
200 PPC_STL r26, VCPU_GPR(r26)(r7)
201 PPC_STL r27, VCPU_GPR(r27)(r7)
202 PPC_STL r28, VCPU_GPR(r28)(r7)
203 PPC_STL r29, VCPU_GPR(r29)(r7)
204 PPC_STL r30, VCPU_GPR(r30)(r7)
205 PPC_STL r31, VCPU_GPR(r31)(r7)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000206
Alexander Graf29eb61b2009-10-30 05:47:07 +0000207 /* Restore host msr -> SRR1 */
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200208 PPC_LL r6, VCPU_HOST_MSR(r7)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000209
210 /*
211 * For some interrupts, we need to call the real Linux
212 * handler, so it can do work for us. This has to happen
213 * as if the interrupt arrived from the kernel though,
214 * so let's fake it here where most state is restored.
215 *
216 * Call Linux for hardware interrupts/decrementer
217 * r3 = address of interrupt handler (exit reason)
218 */
219
Alexander Graf7e57cba2010-01-08 02:58:03 +0100220 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Alexander Graf29eb61b2009-10-30 05:47:07 +0000221 beq call_linux_handler
Alexander Graf7e57cba2010-01-08 02:58:03 +0100222 cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
Alexander Graf29eb61b2009-10-30 05:47:07 +0000223 beq call_linux_handler
Alexander Graf7fdaec92010-04-20 02:49:47 +0200224 cmpwi r12, BOOK3S_INTERRUPT_PERFMON
225 beq call_linux_handler
Alexander Graf29eb61b2009-10-30 05:47:07 +0000226
Alexander Grafbc909232010-01-08 02:58:05 +0100227 /* Back to EE=1 */
228 mtmsr r6
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200229 sync
Alexander Grafbc909232010-01-08 02:58:05 +0100230 b kvm_return_point
Alexander Graf29eb61b2009-10-30 05:47:07 +0000231
232call_linux_handler:
233
234 /*
235 * If we land here we need to jump back to the handler we
236 * came from.
237 *
238 * We have a page that we can access from real mode, so let's
239 * jump back to that and use it as a trampoline to get back into the
240 * interrupt handler!
241 *
242 * R3 still contains the exit code,
Alexander Grafbc909232010-01-08 02:58:05 +0100243 * R5 VCPU_HOST_RETIP and
244 * R6 VCPU_HOST_MSR
Alexander Graf29eb61b2009-10-30 05:47:07 +0000245 */
246
Alexander Grafbc909232010-01-08 02:58:05 +0100247 /* Restore host IP -> SRR0 */
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200248 PPC_LL r5, VCPU_HOST_RETIP(r7)
Alexander Grafbc909232010-01-08 02:58:05 +0100249
250 /* XXX Better move to a safe function?
251 * What if we get an HTAB flush in between mtsrr0 and mtsrr1? */
252
Alexander Graf7e57cba2010-01-08 02:58:03 +0100253 mtlr r12
Alexander Graf29eb61b2009-10-30 05:47:07 +0000254
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200255 PPC_LL r4, VCPU_TRAMPOLINE_LOWMEM(r7)
Alexander Graf7e57cba2010-01-08 02:58:03 +0100256 mtsrr0 r4
257 LOAD_REG_IMMEDIATE(r3, MSR_KERNEL & ~(MSR_IR | MSR_DR))
258 mtsrr1 r3
Alexander Graf29eb61b2009-10-30 05:47:07 +0000259
260 RFI
261
262.global kvm_return_point
263kvm_return_point:
264
265 /* Jump back to lightweight entry if we're supposed to */
266 /* go back into the guest */
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100267
268 /* Pass the exit number as 3rd argument to kvmppc_handle_exit */
Alexander Graf7e57cba2010-01-08 02:58:03 +0100269 mr r5, r12
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100270
Alexander Graf29eb61b2009-10-30 05:47:07 +0000271 /* Restore r3 (kvm_run) and r4 (vcpu) */
272 REST_2GPRS(3, r1)
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200273 bl FUNC(kvmppc_handle_exit)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000274
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100275 /* If RESUME_GUEST, get back in the loop */
Alexander Graf29eb61b2009-10-30 05:47:07 +0000276 cmpwi r3, RESUME_GUEST
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100277 beq kvm_loop_lightweight
Alexander Graf29eb61b2009-10-30 05:47:07 +0000278
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100279 cmpwi r3, RESUME_GUEST_NV
280 beq kvm_loop_heavyweight
Alexander Graf29eb61b2009-10-30 05:47:07 +0000281
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100282kvm_exit_loop:
Alexander Graf29eb61b2009-10-30 05:47:07 +0000283
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200284 PPC_LL r4, _LINK(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000285 mtlr r4
286
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100287 /* Restore non-volatile host registers (r14 - r31) */
288 REST_NVGPRS(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000289
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100290 addi r1, r1, SWITCH_FRAME_SIZE
291 blr
292
293kvm_loop_heavyweight:
294
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200295 PPC_LL r4, _LINK(r1)
296 PPC_STL r4, (PPC_LR_STKOFF + SWITCH_FRAME_SIZE)(r1)
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100297
298 /* Load vcpu and cpu_run */
Alexander Graf29eb61b2009-10-30 05:47:07 +0000299 REST_2GPRS(3, r1)
300
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100301 /* Load non-volatile guest state from the vcpu */
302 VCPU_LOAD_NVGPRS(r4)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000303
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100304 /* Jump back into the beginning of this function */
305 b kvm_start_lightweight
Alexander Graf29eb61b2009-10-30 05:47:07 +0000306
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100307kvm_loop_lightweight:
Alexander Graf29eb61b2009-10-30 05:47:07 +0000308
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100309 /* We'll need the vcpu pointer */
310 REST_GPR(4, r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000311
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100312 /* Jump back into the beginning of this function */
313 b kvm_start_lightweight