7188c11ed7d124fefb6edea196a0f0e45ea1a095
[safe/jmp/linux-2.6] / arch / powerpc / kvm / book3s_64_slb.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 #define SHADOW_SLB_ESID(num)    (SLBSHADOW_SAVEAREA + (num * 0x10))
21 #define SHADOW_SLB_VSID(num)    (SLBSHADOW_SAVEAREA + (num * 0x10) + 0x8)
22 #define UNBOLT_SLB_ENTRY(num) \
23         ld      r9, SHADOW_SLB_ESID(num)(r12); \
24         /* Invalid? Skip. */; \
25         rldicl. r0, r9, 37, 63; \
26         beq     slb_entry_skip_ ## num; \
27         xoris   r9, r9, SLB_ESID_V@h; \
28         std     r9, SHADOW_SLB_ESID(num)(r12); \
29   slb_entry_skip_ ## num:
30
31 #define REBOLT_SLB_ENTRY(num) \
32         ld      r10, SHADOW_SLB_ESID(num)(r11); \
33         cmpdi   r10, 0; \
34         beq     slb_exit_skip_ ## num; \
35         oris    r10, r10, SLB_ESID_V@h; \
36         ld      r9, SHADOW_SLB_VSID(num)(r11); \
37         slbmte  r9, r10; \
38         std     r10, SHADOW_SLB_ESID(num)(r11); \
39 slb_exit_skip_ ## num:
40
41 /******************************************************************************
42  *                                                                            *
43  *                               Entry code                                   *
44  *                                                                            *
45  *****************************************************************************/
46
47 .global kvmppc_handler_trampoline_enter
48 kvmppc_handler_trampoline_enter:
49
50         /* Required state:
51          *
52          * MSR = ~IR|DR
53          * R13 = PACA
54          * R1 = host R1
55          * R2 = host R2
56          * R9 = guest IP
57          * R10 = guest MSR
58          * all other GPRS = free
59          * PACA[KVM_CR] = guest CR
60          * PACA[KVM_XER] = guest XER
61          */
62
63         mtsrr0  r9
64         mtsrr1  r10
65
66         /* Remove LPAR shadow entries */
67
68 #if SLB_NUM_BOLTED == 3
69
70         ld      r12, PACA_SLBSHADOWPTR(r13)
71
72         /* Save off the first entry so we can slbie it later */
73         ld      r10, SHADOW_SLB_ESID(0)(r12)
74         ld      r11, SHADOW_SLB_VSID(0)(r12)
75
76         /* Remove bolted entries */
77         UNBOLT_SLB_ENTRY(0)
78         UNBOLT_SLB_ENTRY(1)
79         UNBOLT_SLB_ENTRY(2)
80         
81 #else
82 #error unknown number of bolted entries
83 #endif
84
85         /* Flush SLB */
86
87         slbia
88
89         /* r0 = esid & ESID_MASK */
90         rldicr  r10, r10, 0, 35
91         /* r0 |= CLASS_BIT(VSID) */
92         rldic   r12, r11, 56 - 36, 36
93         or      r10, r10, r12
94         slbie   r10
95
96         isync
97
98         /* Fill SLB with our shadow */
99
100         lbz     r12, PACA_KVM_SLB_MAX(r13)
101         mulli   r12, r12, 16
102         addi    r12, r12, PACA_KVM_SLB
103         add     r12, r12, r13
104
105         /* for (r11 = kvm_slb; r11 < kvm_slb + kvm_slb_size; r11+=slb_entry) */
106         li      r11, PACA_KVM_SLB
107         add     r11, r11, r13
108
109 slb_loop_enter:
110
111         ld      r10, 0(r11)
112
113         rldicl. r0, r10, 37, 63
114         beq     slb_loop_enter_skip
115
116         ld      r9, 8(r11)
117         slbmte  r9, r10
118
119 slb_loop_enter_skip:
120         addi    r11, r11, 16
121         cmpd    cr0, r11, r12
122         blt     slb_loop_enter
123
124 slb_do_enter:
125
126         /* Enter guest */
127
128         ld      r0, (PACA_KVM_R0)(r13)
129         ld      r1, (PACA_KVM_R1)(r13)
130         ld      r2, (PACA_KVM_R2)(r13)
131         ld      r3, (PACA_KVM_R3)(r13)
132         ld      r4, (PACA_KVM_R4)(r13)
133         ld      r5, (PACA_KVM_R5)(r13)
134         ld      r6, (PACA_KVM_R6)(r13)
135         ld      r7, (PACA_KVM_R7)(r13)
136         ld      r8, (PACA_KVM_R8)(r13)
137         ld      r9, (PACA_KVM_R9)(r13)
138         ld      r10, (PACA_KVM_R10)(r13)
139         ld      r12, (PACA_KVM_R12)(r13)
140
141         lwz     r11, (PACA_KVM_CR)(r13)
142         mtcr    r11
143
144         ld      r11, (PACA_KVM_XER)(r13)
145         mtxer   r11
146
147         ld      r11, (PACA_KVM_R11)(r13)
148         ld      r13, (PACA_KVM_R13)(r13)
149
150         RFI
151 kvmppc_handler_trampoline_enter_end:
152
153
154
155 /******************************************************************************
156  *                                                                            *
157  *                               Exit code                                    *
158  *                                                                            *
159  *****************************************************************************/
160
161 .global kvmppc_handler_trampoline_exit
162 kvmppc_handler_trampoline_exit:
163
164         /* Register usage at this point:
165          *
166          * SPRG_SCRATCH0     = guest R13
167          * R12               = exit handler id
168          * R13               = PACA
169          * PACA.KVM.SCRATCH0 = guest R12
170          * PACA.KVM.SCRATCH1 = guest CR
171          *
172          */
173
174         /* Save registers */
175
176         std     r0, PACA_KVM_R0(r13)
177         std     r1, PACA_KVM_R1(r13)
178         std     r2, PACA_KVM_R2(r13)
179         std     r3, PACA_KVM_R3(r13)
180         std     r4, PACA_KVM_R4(r13)
181         std     r5, PACA_KVM_R5(r13)
182         std     r6, PACA_KVM_R6(r13)
183         std     r7, PACA_KVM_R7(r13)
184         std     r8, PACA_KVM_R8(r13)
185         std     r9, PACA_KVM_R9(r13)
186         std     r10, PACA_KVM_R10(r13)
187         std     r11, PACA_KVM_R11(r13)
188
189         /* Restore R1/R2 so we can handle faults */
190         ld      r1, PACA_KVM_HOST_R1(r13)
191         ld      r2, PACA_KVM_HOST_R2(r13)
192
193         /* Save guest PC and MSR in GPRs */
194         mfsrr0  r3
195         mfsrr1  r4
196
197         /* Get scratch'ed off registers */
198         mfspr   r9, SPRN_SPRG_SCRATCH0
199         std     r9, PACA_KVM_R13(r13)
200
201         ld      r8, PACA_KVM_SCRATCH0(r13)
202         std     r8, PACA_KVM_R12(r13)
203
204         lwz     r7, PACA_KVM_SCRATCH1(r13)
205         stw     r7, PACA_KVM_CR(r13)
206
207         /* Save more register state  */
208
209         mfxer   r6
210         stw     r6, PACA_KVM_XER(r13)
211
212         mfdar   r5
213         mfdsisr r6
214
215         /* Unset guest state */
216         li      r9, 0
217         stb     r9, PACA_KVM_IN_GUEST(r13)
218
219         /*
220          * In order for us to easily get the last instruction,
221          * we got the #vmexit at, we exploit the fact that the
222          * virtual layout is still the same here, so we can just
223          * ld from the guest's PC address
224          */
225
226         /* We only load the last instruction when it's safe */
227         cmpwi   r12, BOOK3S_INTERRUPT_DATA_STORAGE
228         beq     ld_last_inst
229         cmpwi   r12, BOOK3S_INTERRUPT_PROGRAM
230         beq     ld_last_inst
231
232         b       no_ld_last_inst
233
234 ld_last_inst:
235         /* Save off the guest instruction we're at */
236         /*    1) enable paging for data */
237         mfmsr   r9
238         ori     r11, r9, MSR_DR                 /* Enable paging for data */
239         mtmsr   r11
240         /*    2) fetch the instruction */
241         /* XXX implement PACA_KVM_IN_GUEST=2 path to safely jump over this */
242         lwz     r0, 0(r3)
243         /*    3) disable paging again */
244         mtmsr   r9
245
246 no_ld_last_inst:
247
248         /* Restore bolted entries from the shadow and fix it along the way */
249
250         /* We don't store anything in entry 0, so we don't need to take care of it */
251         slbia
252         isync
253
254 #if SLB_NUM_BOLTED == 3
255
256         ld      r11, PACA_SLBSHADOWPTR(r13)
257
258         REBOLT_SLB_ENTRY(0)
259         REBOLT_SLB_ENTRY(1)
260         REBOLT_SLB_ENTRY(2)
261         
262 #else
263 #error unknown number of bolted entries
264 #endif
265
266 slb_do_exit:
267
268         /* Register usage at this point:
269          *
270          * R0         = guest last inst
271          * R1         = host R1
272          * R2         = host R2
273          * R3         = guest PC
274          * R4         = guest MSR
275          * R5         = guest DAR
276          * R6         = guest DSISR
277          * R12        = exit handler id
278          * R13        = PACA
279          * PACA.KVM.* = guest *
280          *
281          */
282
283         /* RFI into the highmem handler */
284         mfmsr   r7
285         ori     r7, r7, MSR_IR|MSR_DR|MSR_RI    /* Enable paging */
286         mtsrr1  r7
287         ld      r8, PACA_KVM_VMHANDLER(r13)     /* Highmem handler address */
288         mtsrr0  r8
289
290         RFI
291 kvmppc_handler_trampoline_exit_end:
292