Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6.git] / arch / powerpc / kvm / book3s_64_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
32 #ifdef DEBUG_MMU
33 #define dprintk(X...) printk(KERN_INFO X)
34 #else
35 #define dprintk(X...) do { } while(0)
36 #endif
37
38 static void kvmppc_mmu_book3s_64_reset_msr(struct kvm_vcpu *vcpu)
39 {
40         kvmppc_set_msr(vcpu, MSR_SF);
41 }
42
43 static struct kvmppc_slb *kvmppc_mmu_book3s_64_find_slbe(
44                                 struct kvmppc_vcpu_book3s *vcpu_book3s,
45                                 gva_t eaddr)
46 {
47         int i;
48         u64 esid = GET_ESID(eaddr);
49         u64 esid_1t = GET_ESID_1T(eaddr);
50
51         for (i = 0; i < vcpu_book3s->slb_nr; i++) {
52                 u64 cmp_esid = esid;
53
54                 if (!vcpu_book3s->slb[i].valid)
55                         continue;
56
57                 if (vcpu_book3s->slb[i].large)
58                         cmp_esid = esid_1t;
59
60                 if (vcpu_book3s->slb[i].esid == cmp_esid)
61                         return &vcpu_book3s->slb[i];
62         }
63
64         dprintk("KVM: No SLB entry found for 0x%lx [%llx | %llx]\n",
65                 eaddr, esid, esid_1t);
66         for (i = 0; i < vcpu_book3s->slb_nr; i++) {
67             if (vcpu_book3s->slb[i].vsid)
68                 dprintk("  %d: %c%c %llx %llx\n", i,
69                         vcpu_book3s->slb[i].valid ? 'v' : ' ',
70                         vcpu_book3s->slb[i].large ? 'l' : ' ',
71                         vcpu_book3s->slb[i].esid,
72                         vcpu_book3s->slb[i].vsid);
73         }
74
75         return NULL;
76 }
77
78 static u64 kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
79                                          bool data)
80 {
81         struct kvmppc_slb *slb;
82
83         slb = kvmppc_mmu_book3s_64_find_slbe(to_book3s(vcpu), eaddr);
84         if (!slb)
85                 return 0;
86
87         if (slb->large)
88                 return (((u64)eaddr >> 12) & 0xfffffff) |
89                        (((u64)slb->vsid) << 28);
90
91         return (((u64)eaddr >> 12) & 0xffff) | (((u64)slb->vsid) << 16);
92 }
93
94 static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb *slbe)
95 {
96         return slbe->large ? 24 : 12;
97 }
98
99 static u32 kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb *slbe, gva_t eaddr)
100 {
101         int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
102         return ((eaddr & 0xfffffff) >> p);
103 }
104
105 static hva_t kvmppc_mmu_book3s_64_get_pteg(
106                                 struct kvmppc_vcpu_book3s *vcpu_book3s,
107                                 struct kvmppc_slb *slbe, gva_t eaddr,
108                                 bool second)
109 {
110         u64 hash, pteg, htabsize;
111         u32 page;
112         hva_t r;
113
114         page = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
115         htabsize = ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1);
116
117         hash = slbe->vsid ^ page;
118         if (second)
119                 hash = ~hash;
120         hash &= ((1ULL << 39ULL) - 1ULL);
121         hash &= htabsize;
122         hash <<= 7ULL;
123
124         pteg = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL;
125         pteg |= hash;
126
127         dprintk("MMU: page=0x%x sdr1=0x%llx pteg=0x%llx vsid=0x%llx\n",
128                 page, vcpu_book3s->sdr1, pteg, slbe->vsid);
129
130         r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
131         if (kvm_is_error_hva(r))
132                 return r;
133         return r | (pteg & ~PAGE_MASK);
134 }
135
136 static u64 kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb *slbe, gva_t eaddr)
137 {
138         int p = kvmppc_mmu_book3s_64_get_pagesize(slbe);
139         u64 avpn;
140
141         avpn = kvmppc_mmu_book3s_64_get_page(slbe, eaddr);
142         avpn |= slbe->vsid << (28 - p);
143
144         if (p < 24)
145                 avpn >>= ((80 - p) - 56) - 8;
146         else
147                 avpn <<= 8;
148
149         return avpn;
150 }
151
152 static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
153                                 struct kvmppc_pte *gpte, bool data)
154 {
155         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
156         struct kvmppc_slb *slbe;
157         hva_t ptegp;
158         u64 pteg[16];
159         u64 avpn = 0;
160         int i;
161         u8 key = 0;
162         bool found = false;
163         bool perm_err = false;
164         int second = 0;
165
166         slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu_book3s, eaddr);
167         if (!slbe)
168                 goto no_seg_found;
169
170 do_second:
171         ptegp = kvmppc_mmu_book3s_64_get_pteg(vcpu_book3s, slbe, eaddr, second);
172         if (kvm_is_error_hva(ptegp))
173                 goto no_page_found;
174
175         avpn = kvmppc_mmu_book3s_64_get_avpn(slbe, eaddr);
176
177         if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
178                 printk(KERN_ERR "KVM can't copy data from 0x%lx!\n", ptegp);
179                 goto no_page_found;
180         }
181
182         if ((vcpu->arch.msr & MSR_PR) && slbe->Kp)
183                 key = 4;
184         else if (!(vcpu->arch.msr & MSR_PR) && slbe->Ks)
185                 key = 4;
186
187         for (i=0; i<16; i+=2) {
188                 u64 v = pteg[i];
189                 u64 r = pteg[i+1];
190
191                 /* Valid check */
192                 if (!(v & HPTE_V_VALID))
193                         continue;
194                 /* Hash check */
195                 if ((v & HPTE_V_SECONDARY) != second)
196                         continue;
197
198                 /* AVPN compare */
199                 if (HPTE_V_AVPN_VAL(avpn) == HPTE_V_AVPN_VAL(v)) {
200                         u8 pp = (r & HPTE_R_PP) | key;
201                         int eaddr_mask = 0xFFF;
202
203                         gpte->eaddr = eaddr;
204                         gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu,
205                                                                     eaddr,
206                                                                     data);
207                         if (slbe->large)
208                                 eaddr_mask = 0xFFFFFF;
209                         gpte->raddr = (r & HPTE_R_RPN) | (eaddr & eaddr_mask);
210                         gpte->may_execute = ((r & HPTE_R_N) ? false : true);
211                         gpte->may_read = false;
212                         gpte->may_write = false;
213
214                         switch (pp) {
215                         case 0:
216                         case 1:
217                         case 2:
218                         case 6:
219                                 gpte->may_write = true;
220                                 /* fall through */
221                         case 3:
222                         case 5:
223                         case 7:
224                                 gpte->may_read = true;
225                                 break;
226                         }
227
228                         if (!gpte->may_read) {
229                                 perm_err = true;
230                                 continue;
231                         }
232
233                         dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
234                                 "-> 0x%llx\n",
235                                 eaddr, avpn, gpte->vpage, gpte->raddr);
236                         found = true;
237                         break;
238                 }
239         }
240
241         /* Update PTE R and C bits, so the guest's swapper knows we used the
242          * page */
243         if (found) {
244                 u32 oldr = pteg[i+1];
245
246                 if (gpte->may_read) {
247                         /* Set the accessed flag */
248                         pteg[i+1] |= HPTE_R_R;
249                 }
250                 if (gpte->may_write) {
251                         /* Set the dirty flag */
252                         pteg[i+1] |= HPTE_R_C;
253                 } else {
254                         dprintk("KVM: Mapping read-only page!\n");
255                 }
256
257                 /* Write back into the PTEG */
258                 if (pteg[i+1] != oldr)
259                         copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
260
261                 return 0;
262         } else {
263                 dprintk("KVM MMU: No PTE found (ea=0x%lx sdr1=0x%llx "
264                         "ptegp=0x%lx)\n",
265                         eaddr, to_book3s(vcpu)->sdr1, ptegp);
266                 for (i = 0; i < 16; i += 2)
267                         dprintk("   %02d: 0x%llx - 0x%llx (0x%llx)\n",
268                                 i, pteg[i], pteg[i+1], avpn);
269
270                 if (!second) {
271                         second = HPTE_V_SECONDARY;
272                         goto do_second;
273                 }
274         }
275
276
277 no_page_found:
278
279
280         if (perm_err)
281                 return -EPERM;
282
283         return -ENOENT;
284
285 no_seg_found:
286
287         dprintk("KVM MMU: Trigger segment fault\n");
288         return -EINVAL;
289 }
290
291 static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu *vcpu, u64 rs, u64 rb)
292 {
293         struct kvmppc_vcpu_book3s *vcpu_book3s;
294         u64 esid, esid_1t;
295         int slb_nr;
296         struct kvmppc_slb *slbe;
297
298         dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs, rb);
299
300         vcpu_book3s = to_book3s(vcpu);
301
302         esid = GET_ESID(rb);
303         esid_1t = GET_ESID_1T(rb);
304         slb_nr = rb & 0xfff;
305
306         if (slb_nr > vcpu_book3s->slb_nr)
307                 return;
308
309         slbe = &vcpu_book3s->slb[slb_nr];
310
311         slbe->large = (rs & SLB_VSID_L) ? 1 : 0;
312         slbe->esid  = slbe->large ? esid_1t : esid;
313         slbe->vsid  = rs >> 12;
314         slbe->valid = (rb & SLB_ESID_V) ? 1 : 0;
315         slbe->Ks    = (rs & SLB_VSID_KS) ? 1 : 0;
316         slbe->Kp    = (rs & SLB_VSID_KP) ? 1 : 0;
317         slbe->nx    = (rs & SLB_VSID_N) ? 1 : 0;
318         slbe->class = (rs & SLB_VSID_C) ? 1 : 0;
319
320         slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
321         slbe->origv = rs;
322
323         /* Map the new segment */
324         kvmppc_mmu_map_segment(vcpu, esid << SID_SHIFT);
325 }
326
327 static u64 kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu *vcpu, u64 slb_nr)
328 {
329         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
330         struct kvmppc_slb *slbe;
331
332         if (slb_nr > vcpu_book3s->slb_nr)
333                 return 0;
334
335         slbe = &vcpu_book3s->slb[slb_nr];
336
337         return slbe->orige;
338 }
339
340 static u64 kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu *vcpu, u64 slb_nr)
341 {
342         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
343         struct kvmppc_slb *slbe;
344
345         if (slb_nr > vcpu_book3s->slb_nr)
346                 return 0;
347
348         slbe = &vcpu_book3s->slb[slb_nr];
349
350         return slbe->origv;
351 }
352
353 static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu *vcpu, u64 ea)
354 {
355         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
356         struct kvmppc_slb *slbe;
357
358         dprintk("KVM MMU: slbie(0x%llx)\n", ea);
359
360         slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu_book3s, ea);
361
362         if (!slbe)
363                 return;
364
365         dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea, slbe->esid);
366
367         slbe->valid = false;
368
369         kvmppc_mmu_map_segment(vcpu, ea);
370 }
371
372 static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)
373 {
374         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
375         int i;
376
377         dprintk("KVM MMU: slbia()\n");
378
379         for (i = 1; i < vcpu_book3s->slb_nr; i++)
380                 vcpu_book3s->slb[i].valid = false;
381
382         if (vcpu->arch.msr & MSR_IR) {
383                 kvmppc_mmu_flush_segments(vcpu);
384                 kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
385         }
386 }
387
388 static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
389                                         ulong value)
390 {
391         u64 rb = 0, rs = 0;
392
393         /* ESID = srnum */
394         rb |= (srnum & 0xf) << 28;
395         /* Set the valid bit */
396         rb |= 1 << 27;
397         /* Index = ESID */
398         rb |= srnum;
399
400         /* VSID = VSID */
401         rs |= (value & 0xfffffff) << 12;
402         /* flags = flags */
403         rs |= ((value >> 27) & 0xf) << 9;
404
405         kvmppc_mmu_book3s_64_slbmte(vcpu, rs, rb);
406 }
407
408 static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va,
409                                        bool large)
410 {
411         u64 mask = 0xFFFFFFFFFULL;
412
413         dprintk("KVM MMU: tlbie(0x%lx)\n", va);
414
415         if (large)
416                 mask = 0xFFFFFF000ULL;
417         kvmppc_mmu_pte_vflush(vcpu, va >> 12, mask);
418 }
419
420 static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, u64 esid,
421                                              u64 *vsid)
422 {
423         switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
424         case 0:
425                 *vsid = (VSID_REAL >> 16) | esid;
426                 break;
427         case MSR_IR:
428                 *vsid = (VSID_REAL_IR >> 16) | esid;
429                 break;
430         case MSR_DR:
431                 *vsid = (VSID_REAL_DR >> 16) | esid;
432                 break;
433         case MSR_DR|MSR_IR:
434         {
435                 ulong ea;
436                 struct kvmppc_slb *slb;
437                 ea = esid << SID_SHIFT;
438                 slb = kvmppc_mmu_book3s_64_find_slbe(to_book3s(vcpu), ea);
439                 if (slb)
440                         *vsid = slb->vsid;
441                 else
442                         return -ENOENT;
443
444                 break;
445         }
446         default:
447                 BUG();
448                 break;
449         }
450
451         return 0;
452 }
453
454 static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu *vcpu)
455 {
456         return (to_book3s(vcpu)->hid[5] & 0x80);
457 }
458
459 void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu)
460 {
461         struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
462
463         mmu->mfsrin = NULL;
464         mmu->mtsrin = kvmppc_mmu_book3s_64_mtsrin;
465         mmu->slbmte = kvmppc_mmu_book3s_64_slbmte;
466         mmu->slbmfee = kvmppc_mmu_book3s_64_slbmfee;
467         mmu->slbmfev = kvmppc_mmu_book3s_64_slbmfev;
468         mmu->slbie = kvmppc_mmu_book3s_64_slbie;
469         mmu->slbia = kvmppc_mmu_book3s_64_slbia;
470         mmu->xlate = kvmppc_mmu_book3s_64_xlate;
471         mmu->reset_msr = kvmppc_mmu_book3s_64_reset_msr;
472         mmu->tlbie = kvmppc_mmu_book3s_64_tlbie;
473         mmu->esid_to_vsid = kvmppc_mmu_book3s_64_esid_to_vsid;
474         mmu->ea_to_vp = kvmppc_mmu_book3s_64_ea_to_vp;
475         mmu->is_dcbz32 = kvmppc_mmu_book3s_64_is_dcbz32;
476
477         vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
478 }