KVM: PPC: Use PACA backed shadow vcpu
[safe/jmp/linux-2.6] / arch / powerpc / kvm / book3s_64_interrupts.S
1 /*
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
27 #define KVMPPC_HANDLE_EXIT .kvmppc_handle_exit
28 #define ULONG_SIZE 8
29 #define VCPU_GPR(n)     (VCPU_GPRS + (n * ULONG_SIZE))
30
31 .macro DISABLE_INTERRUPTS
32        mfmsr   r0
33        rldicl  r0,r0,48,1
34        rotldi  r0,r0,16
35        mtmsrd  r0,1
36 .endm
37
38 #define VCPU_LOAD_NVGPRS(vcpu) \
39         ld      r14, VCPU_GPR(r14)(vcpu); \
40         ld      r15, VCPU_GPR(r15)(vcpu); \
41         ld      r16, VCPU_GPR(r16)(vcpu); \
42         ld      r17, VCPU_GPR(r17)(vcpu); \
43         ld      r18, VCPU_GPR(r18)(vcpu); \
44         ld      r19, VCPU_GPR(r19)(vcpu); \
45         ld      r20, VCPU_GPR(r20)(vcpu); \
46         ld      r21, VCPU_GPR(r21)(vcpu); \
47         ld      r22, VCPU_GPR(r22)(vcpu); \
48         ld      r23, VCPU_GPR(r23)(vcpu); \
49         ld      r24, VCPU_GPR(r24)(vcpu); \
50         ld      r25, VCPU_GPR(r25)(vcpu); \
51         ld      r26, VCPU_GPR(r26)(vcpu); \
52         ld      r27, VCPU_GPR(r27)(vcpu); \
53         ld      r28, VCPU_GPR(r28)(vcpu); \
54         ld      r29, VCPU_GPR(r29)(vcpu); \
55         ld      r30, VCPU_GPR(r30)(vcpu); \
56         ld      r31, VCPU_GPR(r31)(vcpu); \
57
58 /*****************************************************************************
59  *                                                                           *
60  *     Guest entry / exit code that is in kernel module memory (highmem)     *
61  *                                                                           *
62  ****************************************************************************/
63
64 /* Registers:
65  *  r3: kvm_run pointer
66  *  r4: vcpu pointer
67  */
68 _GLOBAL(__kvmppc_vcpu_entry)
69
70 kvm_start_entry:
71         /* Write correct stack frame */
72         mflr    r0
73         std     r0,16(r1)
74
75         /* Save host state to the stack */
76         stdu    r1, -SWITCH_FRAME_SIZE(r1)
77
78         /* Save r3 (kvm_run) and r4 (vcpu) */
79         SAVE_2GPRS(3, r1)
80
81         /* Save non-volatile registers (r14 - r31) */
82         SAVE_NVGPRS(r1)
83
84         /* Save LR */
85         std     r0, _LINK(r1)
86
87         /* Load non-volatile guest state from the vcpu */
88         VCPU_LOAD_NVGPRS(r4)
89
90         /* Save R1/R2 in the PACA */
91         std     r1, PACA_KVM_HOST_R1(r13)
92         std     r2, PACA_KVM_HOST_R2(r13)
93
94         /* XXX swap in/out on load? */
95         ld      r3, VCPU_HIGHMEM_HANDLER(r4)
96         std     r3, PACA_KVM_VMHANDLER(r13)
97
98         ld      r3, VCPU_TRAMPOLINE_ENTER(r4)
99         std     r3, PACA_KVM_RMHANDLER(r13)
100
101 kvm_start_lightweight:
102
103         ld      r9, VCPU_PC(r4)                 /* r9 = vcpu->arch.pc */
104         ld      r10, VCPU_SHADOW_MSR(r4)        /* r10 = vcpu->arch.shadow_msr */
105
106         /* Load some guest state in the respective registers */
107         ld      r3, VCPU_CTR(r4)        /* r3 = vcpu->arch.ctr */
108         mtctr   r3                      /* CTR = r3 */
109
110         ld      r3, VCPU_LR(r4)         /* r3 = vcpu->arch.lr */
111         mtlr    r3                      /* LR = r3 */
112
113         DISABLE_INTERRUPTS
114
115         /* Some guests may need to have dcbz set to 32 byte length.
116          *
117          * Usually we ensure that by patching the guest's instructions
118          * to trap on dcbz and emulate it in the hypervisor.
119          *
120          * If we can, we should tell the CPU to use 32 byte dcbz though,
121          * because that's a lot faster.
122          */
123
124         ld      r3, VCPU_HFLAGS(r4)
125         rldicl. r3, r3, 0, 63           /* CR = ((r3 & 1) == 0) */
126         beq     no_dcbz32_on
127
128         mfspr   r3,SPRN_HID5
129         ori     r3, r3, 0x80            /* XXX HID5_dcbz32 = 0x80 */
130         mtspr   SPRN_HID5,r3
131
132 no_dcbz32_on:
133
134         /* This sets the Magic value for the trampoline */
135
136         /* XXX this needs to move into a safe function, so we can
137            be sure we don't get any interrupts */
138
139         li      r11, 1
140         stb     r11, PACA_KVM_IN_GUEST(r13)
141
142         ld      r3, PACA_KVM_RMHANDLER(r13)
143         mtsrr0  r3
144
145         LOAD_REG_IMMEDIATE(r3, MSR_KERNEL & ~(MSR_IR | MSR_DR))
146         mtsrr1  r3
147
148         /* Jump to SLB patching handlder and into our guest */
149         RFI
150
151 /*
152  * This is the handler in module memory. It gets jumped at from the
153  * lowmem trampoline code, so it's basically the guest exit code.
154  *
155  */
156
157 .global kvmppc_handler_highmem
158 kvmppc_handler_highmem:
159
160         /*
161          * Register usage at this point:
162          *
163          * R0         = guest last inst
164          * R1         = host R1
165          * R2         = host R2
166          * R3         = guest PC
167          * R4         = guest MSR
168          * R5         = guest DAR
169          * R6         = guest DSISR
170          * R13        = PACA
171          * PACA.KVM.* = guest *
172          *
173          */
174
175         /* R7 = vcpu */
176         ld      r7, GPR4(r1)
177
178         /* Now save the guest state */
179
180         stw     r0, VCPU_LAST_INST(r7)
181
182         std     r3, VCPU_PC(r7)
183         std     r4, VCPU_SHADOW_MSR(r7)
184         std     r5, VCPU_FAULT_DEAR(r7)
185         std     r6, VCPU_FAULT_DSISR(r7)
186
187         ld      r5, VCPU_HFLAGS(r7)
188         rldicl. r5, r5, 0, 63           /* CR = ((r5 & 1) == 0) */
189         beq     no_dcbz32_off
190
191         mfspr   r5,SPRN_HID5
192         rldimi  r5,r5,6,56
193         mtspr   SPRN_HID5,r5
194
195 no_dcbz32_off:
196
197         std     r14, VCPU_GPR(r14)(r7)
198         std     r15, VCPU_GPR(r15)(r7)
199         std     r16, VCPU_GPR(r16)(r7)
200         std     r17, VCPU_GPR(r17)(r7)
201         std     r18, VCPU_GPR(r18)(r7)
202         std     r19, VCPU_GPR(r19)(r7)
203         std     r20, VCPU_GPR(r20)(r7)
204         std     r21, VCPU_GPR(r21)(r7)
205         std     r22, VCPU_GPR(r22)(r7)
206         std     r23, VCPU_GPR(r23)(r7)
207         std     r24, VCPU_GPR(r24)(r7)
208         std     r25, VCPU_GPR(r25)(r7)
209         std     r26, VCPU_GPR(r26)(r7)
210         std     r27, VCPU_GPR(r27)(r7)
211         std     r28, VCPU_GPR(r28)(r7)
212         std     r29, VCPU_GPR(r29)(r7)
213         std     r30, VCPU_GPR(r30)(r7)
214         std     r31, VCPU_GPR(r31)(r7)
215
216         /* Save guest CTR */
217         mfctr   r5
218         std     r5, VCPU_CTR(r7)
219
220         /* Save guest LR */
221         mflr    r5
222         std     r5, VCPU_LR(r7)
223
224         /* XXX convert to safe function call */
225
226         /* Restore host msr -> SRR1 */
227         ld      r6, VCPU_HOST_MSR(r7)
228         mtsrr1  r6
229
230         /* Restore host IP -> SRR0 */
231         ld      r5, VCPU_HOST_RETIP(r7)
232         mtsrr0  r5
233
234         /*
235          * For some interrupts, we need to call the real Linux
236          * handler, so it can do work for us. This has to happen
237          * as if the interrupt arrived from the kernel though,
238          * so let's fake it here where most state is restored.
239          *
240          * Call Linux for hardware interrupts/decrementer
241          * r3 = address of interrupt handler (exit reason)
242          */
243
244         cmpwi   r12, BOOK3S_INTERRUPT_EXTERNAL
245         beq     call_linux_handler
246         cmpwi   r12, BOOK3S_INTERRUPT_DECREMENTER
247         beq     call_linux_handler
248
249         /* Back to Interruptable Mode! (goto kvm_return_point) */
250         RFI
251
252 call_linux_handler:
253
254         /*
255          * If we land here we need to jump back to the handler we
256          * came from.
257          *
258          * We have a page that we can access from real mode, so let's
259          * jump back to that and use it as a trampoline to get back into the
260          * interrupt handler!
261          *
262          * R3 still contains the exit code,
263          * R6 VCPU_HOST_RETIP and
264          * R7 VCPU_HOST_MSR
265          */
266
267         mtlr    r12
268
269         ld      r4, VCPU_TRAMPOLINE_LOWMEM(r7)
270         mtsrr0  r4
271         LOAD_REG_IMMEDIATE(r3, MSR_KERNEL & ~(MSR_IR | MSR_DR))
272         mtsrr1  r3
273
274         RFI
275
276 .global kvm_return_point
277 kvm_return_point:
278
279         /* Jump back to lightweight entry if we're supposed to */
280         /* go back into the guest */
281
282         /* Pass the exit number as 3rd argument to kvmppc_handle_exit */
283         mr      r5, r12
284
285         /* Restore r3 (kvm_run) and r4 (vcpu) */
286         REST_2GPRS(3, r1)
287         bl      KVMPPC_HANDLE_EXIT
288
289         /* If RESUME_GUEST, get back in the loop */
290         cmpwi   r3, RESUME_GUEST
291         beq     kvm_loop_lightweight
292
293         cmpwi   r3, RESUME_GUEST_NV
294         beq     kvm_loop_heavyweight
295
296 kvm_exit_loop:
297
298         ld      r4, _LINK(r1)
299         mtlr    r4
300
301         /* Restore non-volatile host registers (r14 - r31) */
302         REST_NVGPRS(r1)
303
304         addi    r1, r1, SWITCH_FRAME_SIZE
305         blr
306
307 kvm_loop_heavyweight:
308
309         ld      r4, _LINK(r1)
310         std     r4, (16 + SWITCH_FRAME_SIZE)(r1)
311
312         /* Load vcpu and cpu_run */
313         REST_2GPRS(3, r1)
314
315         /* Load non-volatile guest state from the vcpu */
316         VCPU_LOAD_NVGPRS(r4)
317
318         /* Jump back into the beginning of this function */
319         b       kvm_start_lightweight
320
321 kvm_loop_lightweight:
322
323         /* We'll need the vcpu pointer */
324         REST_GPR(4, r1)
325
326         /* Jump back into the beginning of this function */
327         b       kvm_start_lightweight
328