KVM: PPC: Name generic 64-bit code generic
[linux-2.6.git] / arch / powerpc / kvm / book3s_32_mmu.c
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 <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/highmem.h>
25
26 #include <asm/tlbflush.h>
27 #include <asm/kvm_ppc.h>
28 #include <asm/kvm_book3s.h>
29
30 /* #define DEBUG_MMU */
31 /* #define DEBUG_MMU_PTE */
32 /* #define DEBUG_MMU_PTE_IP 0xfff14c40 */
33
34 #ifdef DEBUG_MMU
35 #define dprintk(X...) printk(KERN_INFO X)
36 #else
37 #define dprintk(X...) do { } while(0)
38 #endif
39
40 #ifdef DEBUG_MMU_PTE
41 #define dprintk_pte(X...) printk(KERN_INFO X)
42 #else
43 #define dprintk_pte(X...) do { } while(0)
44 #endif
45
46 #define PTEG_FLAG_ACCESSED      0x00000100
47 #define PTEG_FLAG_DIRTY         0x00000080
48
49 static inline bool check_debug_ip(struct kvm_vcpu *vcpu)
50 {
51 #ifdef DEBUG_MMU_PTE_IP
52         return vcpu->arch.pc == DEBUG_MMU_PTE_IP;
53 #else
54         return true;
55 #endif
56 }
57
58 static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
59                                           struct kvmppc_pte *pte, bool data);
60 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, u64 esid,
61                                              u64 *vsid);
62
63 static struct kvmppc_sr *find_sr(struct kvmppc_vcpu_book3s *vcpu_book3s, gva_t eaddr)
64 {
65         return &vcpu_book3s->sr[(eaddr >> 28) & 0xf];
66 }
67
68 static u64 kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
69                                          bool data)
70 {
71         u64 vsid;
72         struct kvmppc_pte pte;
73
74         if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data))
75                 return pte.vpage;
76
77         kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
78         return (((u64)eaddr >> 12) & 0xffff) | (vsid << 16);
79 }
80
81 static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu *vcpu)
82 {
83         kvmppc_set_msr(vcpu, 0);
84 }
85
86 static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvmppc_vcpu_book3s *vcpu_book3s,
87                                       struct kvmppc_sr *sre, gva_t eaddr,
88                                       bool primary)
89 {
90         u32 page, hash, pteg, htabmask;
91         hva_t r;
92
93         page = (eaddr & 0x0FFFFFFF) >> 12;
94         htabmask = ((vcpu_book3s->sdr1 & 0x1FF) << 16) | 0xFFC0;
95
96         hash = ((sre->vsid ^ page) << 6);
97         if (!primary)
98                 hash = ~hash;
99         hash &= htabmask;
100
101         pteg = (vcpu_book3s->sdr1 & 0xffff0000) | hash;
102
103         dprintk("MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
104                 vcpu_book3s->vcpu.arch.pc, eaddr, vcpu_book3s->sdr1, pteg,
105                 sre->vsid);
106
107         r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
108         if (kvm_is_error_hva(r))
109                 return r;
110         return r | (pteg & ~PAGE_MASK);
111 }
112
113 static u32 kvmppc_mmu_book3s_32_get_ptem(struct kvmppc_sr *sre, gva_t eaddr,
114                                     bool primary)
115 {
116         return ((eaddr & 0x0fffffff) >> 22) | (sre->vsid << 7) |
117                (primary ? 0 : 0x40) | 0x80000000;
118 }
119
120 static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
121                                           struct kvmppc_pte *pte, bool data)
122 {
123         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
124         struct kvmppc_bat *bat;
125         int i;
126
127         for (i = 0; i < 8; i++) {
128                 if (data)
129                         bat = &vcpu_book3s->dbat[i];
130                 else
131                         bat = &vcpu_book3s->ibat[i];
132
133                 if (vcpu->arch.msr & MSR_PR) {
134                         if (!bat->vp)
135                                 continue;
136                 } else {
137                         if (!bat->vs)
138                                 continue;
139                 }
140
141                 if (check_debug_ip(vcpu))
142                 {
143                         dprintk_pte("%cBAT %02d: 0x%lx - 0x%x (0x%x)\n",
144                                     data ? 'd' : 'i', i, eaddr, bat->bepi,
145                                     bat->bepi_mask);
146                 }
147                 if ((eaddr & bat->bepi_mask) == bat->bepi) {
148                         u64 vsid;
149                         kvmppc_mmu_book3s_32_esid_to_vsid(vcpu,
150                                 eaddr >> SID_SHIFT, &vsid);
151                         vsid <<= 16;
152                         pte->vpage = (((u64)eaddr >> 12) & 0xffff) | vsid;
153
154                         pte->raddr = bat->brpn | (eaddr & ~bat->bepi_mask);
155                         pte->may_read = bat->pp;
156                         pte->may_write = bat->pp > 1;
157                         pte->may_execute = true;
158                         if (!pte->may_read) {
159                                 printk(KERN_INFO "BAT is not readable!\n");
160                                 continue;
161                         }
162                         if (!pte->may_write) {
163                                 /* let's treat r/o BATs as not-readable for now */
164                                 dprintk_pte("BAT is read-only!\n");
165                                 continue;
166                         }
167
168                         return 0;
169                 }
170         }
171
172         return -ENOENT;
173 }
174
175 static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr,
176                                      struct kvmppc_pte *pte, bool data,
177                                      bool primary)
178 {
179         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
180         struct kvmppc_sr *sre;
181         hva_t ptegp;
182         u32 pteg[16];
183         u64 ptem = 0;
184         int i;
185         int found = 0;
186
187         sre = find_sr(vcpu_book3s, eaddr);
188
189         dprintk_pte("SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr >> 28,
190                     sre->vsid, sre->raw);
191
192         pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
193
194         ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu_book3s, sre, eaddr, primary);
195         if (kvm_is_error_hva(ptegp)) {
196                 printk(KERN_INFO "KVM: Invalid PTEG!\n");
197                 goto no_page_found;
198         }
199
200         ptem = kvmppc_mmu_book3s_32_get_ptem(sre, eaddr, primary);
201
202         if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
203                 printk(KERN_ERR "KVM: Can't copy data from 0x%lx!\n", ptegp);
204                 goto no_page_found;
205         }
206
207         for (i=0; i<16; i+=2) {
208                 if (ptem == pteg[i]) {
209                         u8 pp;
210
211                         pte->raddr = (pteg[i+1] & ~(0xFFFULL)) | (eaddr & 0xFFF);
212                         pp = pteg[i+1] & 3;
213
214                         if ((sre->Kp &&  (vcpu->arch.msr & MSR_PR)) ||
215                             (sre->Ks && !(vcpu->arch.msr & MSR_PR)))
216                                 pp |= 4;
217
218                         pte->may_write = false;
219                         pte->may_read = false;
220                         pte->may_execute = true;
221                         switch (pp) {
222                                 case 0:
223                                 case 1:
224                                 case 2:
225                                 case 6:
226                                         pte->may_write = true;
227                                 case 3:
228                                 case 5:
229                                 case 7:
230                                         pte->may_read = true;
231                                         break;
232                         }
233
234                         if ( !pte->may_read )
235                                 continue;
236
237                         dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
238                                     pteg[i], pteg[i+1], pp);
239                         found = 1;
240                         break;
241                 }
242         }
243
244         /* Update PTE C and A bits, so the guest's swapper knows we used the
245            page */
246         if (found) {
247                 u32 oldpte = pteg[i+1];
248
249                 if (pte->may_read)
250                         pteg[i+1] |= PTEG_FLAG_ACCESSED;
251                 if (pte->may_write)
252                         pteg[i+1] |= PTEG_FLAG_DIRTY;
253                 else
254                         dprintk_pte("KVM: Mapping read-only page!\n");
255
256                 /* Write back into the PTEG */
257                 if (pteg[i+1] != oldpte)
258                         copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
259
260                 return 0;
261         }
262
263 no_page_found:
264
265         if (check_debug_ip(vcpu)) {
266                 dprintk_pte("KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
267                             to_book3s(vcpu)->sdr1, ptegp);
268                 for (i=0; i<16; i+=2) {
269                         dprintk_pte("   %02d: 0x%x - 0x%x (0x%llx)\n",
270                                     i, pteg[i], pteg[i+1], ptem);
271                 }
272         }
273
274         return -ENOENT;
275 }
276
277 static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
278                                       struct kvmppc_pte *pte, bool data)
279 {
280         int r;
281
282         pte->eaddr = eaddr;
283         r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data);
284         if (r < 0)
285                r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, true);
286         if (r < 0)
287                r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, false);
288
289         return r;
290 }
291
292
293 static u32 kvmppc_mmu_book3s_32_mfsrin(struct kvm_vcpu *vcpu, u32 srnum)
294 {
295         return to_book3s(vcpu)->sr[srnum].raw;
296 }
297
298 static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
299                                         ulong value)
300 {
301         struct kvmppc_sr *sre;
302
303         sre = &to_book3s(vcpu)->sr[srnum];
304
305         /* Flush any left-over shadows from the previous SR */
306
307         /* XXX Not necessary? */
308         /* kvmppc_mmu_pte_flush(vcpu, ((u64)sre->vsid) << 28, 0xf0000000ULL); */
309
310         /* And then put in the new SR */
311         sre->raw = value;
312         sre->vsid = (value & 0x0fffffff);
313         sre->valid = (value & 0x80000000) ? false : true;
314         sre->Ks = (value & 0x40000000) ? true : false;
315         sre->Kp = (value & 0x20000000) ? true : false;
316         sre->nx = (value & 0x10000000) ? true : false;
317
318         /* Map the new segment */
319         kvmppc_mmu_map_segment(vcpu, srnum << SID_SHIFT);
320 }
321
322 static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu *vcpu, ulong ea, bool large)
323 {
324         kvmppc_mmu_pte_flush(vcpu, ea, 0x0FFFF000);
325 }
326
327 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, u64 esid,
328                                              u64 *vsid)
329 {
330         /* In case we only have one of MSR_IR or MSR_DR set, let's put
331            that in the real-mode context (and hope RM doesn't access
332            high memory) */
333         switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
334         case 0:
335                 *vsid = (VSID_REAL >> 16) | esid;
336                 break;
337         case MSR_IR:
338                 *vsid = (VSID_REAL_IR >> 16) | esid;
339                 break;
340         case MSR_DR:
341                 *vsid = (VSID_REAL_DR >> 16) | esid;
342                 break;
343         case MSR_DR|MSR_IR:
344         {
345                 ulong ea = esid << SID_SHIFT;
346                 struct kvmppc_sr *sr = find_sr(to_book3s(vcpu), ea);
347
348                 if (!sr->valid)
349                         return -1;
350
351                 *vsid = sr->vsid;
352                 break;
353         }
354         default:
355                 BUG();
356         }
357
358         if (vcpu->arch.msr & MSR_PR)
359                 *vsid |= VSID_PR;
360
361         return 0;
362 }
363
364 static bool kvmppc_mmu_book3s_32_is_dcbz32(struct kvm_vcpu *vcpu)
365 {
366         return true;
367 }
368
369
370 void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu)
371 {
372         struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
373
374         mmu->mtsrin = kvmppc_mmu_book3s_32_mtsrin;
375         mmu->mfsrin = kvmppc_mmu_book3s_32_mfsrin;
376         mmu->xlate = kvmppc_mmu_book3s_32_xlate;
377         mmu->reset_msr = kvmppc_mmu_book3s_32_reset_msr;
378         mmu->tlbie = kvmppc_mmu_book3s_32_tlbie;
379         mmu->esid_to_vsid = kvmppc_mmu_book3s_32_esid_to_vsid;
380         mmu->ea_to_vp = kvmppc_mmu_book3s_32_ea_to_vp;
381         mmu->is_dcbz32 = kvmppc_mmu_book3s_32_is_dcbz32;
382
383         mmu->slbmte = NULL;
384         mmu->slbmfee = NULL;
385         mmu->slbmfev = NULL;
386         mmu->slbie = NULL;
387         mmu->slbia = NULL;
388 }