2bb67e633de22fe0b2deaa15647d19d46fe494a0
[safe/jmp/linux-2.6] / arch / powerpc / kvm / book3s_32_mmu_host.c
1 /*
2  * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
3  *
4  * Authors:
5  *     Alexander Graf <agraf@suse.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 2, as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 #include <linux/kvm_host.h>
22
23 #include <asm/kvm_ppc.h>
24 #include <asm/kvm_book3s.h>
25 #include <asm/mmu-hash32.h>
26 #include <asm/machdep.h>
27 #include <asm/mmu_context.h>
28 #include <asm/hw_irq.h>
29
30 /* #define DEBUG_MMU */
31 /* #define DEBUG_SR */
32
33 #ifdef DEBUG_MMU
34 #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
35 #else
36 #define dprintk_mmu(a, ...) do { } while(0)
37 #endif
38
39 #ifdef DEBUG_SR
40 #define dprintk_sr(a, ...) printk(KERN_INFO a, __VA_ARGS__)
41 #else
42 #define dprintk_sr(a, ...) do { } while(0)
43 #endif
44
45 #if PAGE_SHIFT != 12
46 #error Unknown page size
47 #endif
48
49 #ifdef CONFIG_SMP
50 #error XXX need to grab mmu_hash_lock
51 #endif
52
53 #ifdef CONFIG_PTE_64BIT
54 #error Only 32 bit pages are supported for now
55 #endif
56
57 static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
58 {
59         volatile u32 *pteg;
60
61         dprintk_mmu("KVM: Flushing SPTE: 0x%llx (0x%llx) -> 0x%llx\n",
62                     pte->pte.eaddr, pte->pte.vpage, pte->host_va);
63
64         pteg = (u32*)pte->slot;
65
66         pteg[0] = 0;
67         asm volatile ("sync");
68         asm volatile ("tlbie %0" : : "r" (pte->pte.eaddr) : "memory");
69         asm volatile ("sync");
70         asm volatile ("tlbsync");
71
72         pte->host_va = 0;
73
74         if (pte->pte.may_write)
75                 kvm_release_pfn_dirty(pte->pfn);
76         else
77                 kvm_release_pfn_clean(pte->pfn);
78 }
79
80 void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
81 {
82         int i;
83
84         dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%x & 0x%x\n",
85                     vcpu->arch.hpte_cache_offset, guest_ea, ea_mask);
86         BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
87
88         guest_ea &= ea_mask;
89         for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
90                 struct hpte_cache *pte;
91
92                 pte = &vcpu->arch.hpte_cache[i];
93                 if (!pte->host_va)
94                         continue;
95
96                 if ((pte->pte.eaddr & ea_mask) == guest_ea) {
97                         invalidate_pte(vcpu, pte);
98                 }
99         }
100
101         /* Doing a complete flush -> start from scratch */
102         if (!ea_mask)
103                 vcpu->arch.hpte_cache_offset = 0;
104 }
105
106 void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
107 {
108         int i;
109
110         dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
111                     vcpu->arch.hpte_cache_offset, guest_vp, vp_mask);
112         BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
113
114         guest_vp &= vp_mask;
115         for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
116                 struct hpte_cache *pte;
117
118                 pte = &vcpu->arch.hpte_cache[i];
119                 if (!pte->host_va)
120                         continue;
121
122                 if ((pte->pte.vpage & vp_mask) == guest_vp) {
123                         invalidate_pte(vcpu, pte);
124                 }
125         }
126 }
127
128 void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
129 {
130         int i;
131
132         dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%llx & 0x%llx\n",
133                     vcpu->arch.hpte_cache_offset, pa_start, pa_end);
134         BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
135
136         for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
137                 struct hpte_cache *pte;
138
139                 pte = &vcpu->arch.hpte_cache[i];
140                 if (!pte->host_va)
141                         continue;
142
143                 if ((pte->pte.raddr >= pa_start) &&
144                     (pte->pte.raddr < pa_end)) {
145                         invalidate_pte(vcpu, pte);
146                 }
147         }
148 }
149
150 struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data)
151 {
152         int i;
153         u64 guest_vp;
154
155         guest_vp = vcpu->arch.mmu.ea_to_vp(vcpu, ea, false);
156         for (i=0; i<vcpu->arch.hpte_cache_offset; i++) {
157                 struct hpte_cache *pte;
158
159                 pte = &vcpu->arch.hpte_cache[i];
160                 if (!pte->host_va)
161                         continue;
162
163                 if (pte->pte.vpage == guest_vp)
164                         return &pte->pte;
165         }
166
167         return NULL;
168 }
169
170 static int kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
171 {
172         if (vcpu->arch.hpte_cache_offset == HPTEG_CACHE_NUM)
173                 kvmppc_mmu_pte_flush(vcpu, 0, 0);
174
175         return vcpu->arch.hpte_cache_offset++;
176 }
177
178 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
179  * a hash, so we don't waste cycles on looping */
180 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
181 {
182         return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
183                      ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
184                      ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
185                      ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
186                      ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
187                      ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
188                      ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
189                      ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
190 }
191
192
193 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
194 {
195         struct kvmppc_sid_map *map;
196         u16 sid_map_mask;
197
198         if (vcpu->arch.msr & MSR_PR)
199                 gvsid |= VSID_PR;
200
201         sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
202         map = &to_book3s(vcpu)->sid_map[sid_map_mask];
203         if (map->guest_vsid == gvsid) {
204                 dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
205                             gvsid, map->host_vsid);
206                 return map;
207         }
208
209         map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
210         if (map->guest_vsid == gvsid) {
211                 dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
212                             gvsid, map->host_vsid);
213                 return map;
214         }
215
216         dprintk_sr("SR: Searching 0x%llx -> not found\n", gvsid);
217         return NULL;
218 }
219
220 extern struct hash_pte *Hash;
221 extern unsigned long _SDR1;
222
223 static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr,
224                                 bool primary)
225 {
226         u32 page, hash, htabmask;
227         ulong pteg = (ulong)Hash;
228
229         page = (eaddr & ~ESID_MASK) >> 12;
230
231         hash = ((vsid ^ page) << 6);
232         if (!primary)
233                 hash = ~hash;
234
235         htabmask = ((_SDR1 & 0x1FF) << 16) | 0xFFC0;
236         hash &= htabmask;
237
238         pteg |= hash;
239
240         dprintk_mmu("htab: %p | hash: %x | htabmask: %x | pteg: %lx\n",
241                 Hash, hash, htabmask, pteg);
242
243         return (u32*)pteg;
244 }
245
246 extern char etext[];
247
248 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
249 {
250         pfn_t hpaddr;
251         u64 va;
252         u64 vsid;
253         struct kvmppc_sid_map *map;
254         volatile u32 *pteg;
255         u32 eaddr = orig_pte->eaddr;
256         u32 pteg0, pteg1;
257         register int rr = 0;
258         bool primary = false;
259         bool evict = false;
260         int hpte_id;
261         struct hpte_cache *pte;
262
263         /* Get host physical address for gpa */
264         hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
265         if (kvm_is_error_hva(hpaddr)) {
266                 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n",
267                                  orig_pte->eaddr);
268                 return -EINVAL;
269         }
270         hpaddr <<= PAGE_SHIFT;
271
272         /* and write the mapping ea -> hpa into the pt */
273         vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
274         map = find_sid_vsid(vcpu, vsid);
275         if (!map) {
276                 kvmppc_mmu_map_segment(vcpu, eaddr);
277                 map = find_sid_vsid(vcpu, vsid);
278         }
279         BUG_ON(!map);
280
281         vsid = map->host_vsid;
282         va = (vsid << SID_SHIFT) | (eaddr & ~ESID_MASK);
283
284 next_pteg:
285         if (rr == 16) {
286                 primary = !primary;
287                 evict = true;
288                 rr = 0;
289         }
290
291         pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);
292
293         /* not evicting yet */
294         if (!evict && (pteg[rr] & PTE_V)) {
295                 rr += 2;
296                 goto next_pteg;
297         }
298
299         dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);
300         dprintk_mmu("KVM:   %08x - %08x\n", pteg[0], pteg[1]);
301         dprintk_mmu("KVM:   %08x - %08x\n", pteg[2], pteg[3]);
302         dprintk_mmu("KVM:   %08x - %08x\n", pteg[4], pteg[5]);
303         dprintk_mmu("KVM:   %08x - %08x\n", pteg[6], pteg[7]);
304         dprintk_mmu("KVM:   %08x - %08x\n", pteg[8], pteg[9]);
305         dprintk_mmu("KVM:   %08x - %08x\n", pteg[10], pteg[11]);
306         dprintk_mmu("KVM:   %08x - %08x\n", pteg[12], pteg[13]);
307         dprintk_mmu("KVM:   %08x - %08x\n", pteg[14], pteg[15]);
308
309         pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |
310                 (primary ? 0 : PTE_SEC);
311         pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
312
313         if (orig_pte->may_write) {
314                 pteg1 |= PP_RWRW;
315                 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
316         } else {
317                 pteg1 |= PP_RWRX;
318         }
319
320         local_irq_disable();
321
322         if (pteg[rr]) {
323                 pteg[rr] = 0;
324                 asm volatile ("sync");
325         }
326         pteg[rr + 1] = pteg1;
327         pteg[rr] = pteg0;
328         asm volatile ("sync");
329
330         local_irq_enable();
331
332         dprintk_mmu("KVM: new PTEG: %p\n", pteg);
333         dprintk_mmu("KVM:   %08x - %08x\n", pteg[0], pteg[1]);
334         dprintk_mmu("KVM:   %08x - %08x\n", pteg[2], pteg[3]);
335         dprintk_mmu("KVM:   %08x - %08x\n", pteg[4], pteg[5]);
336         dprintk_mmu("KVM:   %08x - %08x\n", pteg[6], pteg[7]);
337         dprintk_mmu("KVM:   %08x - %08x\n", pteg[8], pteg[9]);
338         dprintk_mmu("KVM:   %08x - %08x\n", pteg[10], pteg[11]);
339         dprintk_mmu("KVM:   %08x - %08x\n", pteg[12], pteg[13]);
340         dprintk_mmu("KVM:   %08x - %08x\n", pteg[14], pteg[15]);
341
342
343         /* Now tell our Shadow PTE code about the new page */
344
345         hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
346         pte = &vcpu->arch.hpte_cache[hpte_id];
347
348         dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
349                     orig_pte->may_write ? 'w' : '-',
350                     orig_pte->may_execute ? 'x' : '-',
351                     orig_pte->eaddr, (ulong)pteg, va,
352                     orig_pte->vpage, hpaddr);
353
354         pte->slot = (ulong)&pteg[rr];
355         pte->host_va = va;
356         pte->pte = *orig_pte;
357         pte->pfn = hpaddr >> PAGE_SHIFT;
358
359         return 0;
360 }
361
362 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
363 {
364         struct kvmppc_sid_map *map;
365         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
366         u16 sid_map_mask;
367         static int backwards_map = 0;
368
369         if (vcpu->arch.msr & MSR_PR)
370                 gvsid |= VSID_PR;
371
372         /* We might get collisions that trap in preceding order, so let's
373            map them differently */
374
375         sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
376         if (backwards_map)
377                 sid_map_mask = SID_MAP_MASK - sid_map_mask;
378
379         map = &to_book3s(vcpu)->sid_map[sid_map_mask];
380
381         /* Make sure we're taking the other map next time */
382         backwards_map = !backwards_map;
383
384         /* Uh-oh ... out of mappings. Let's flush! */
385         if (vcpu_book3s->vsid_next >= vcpu_book3s->vsid_max) {
386                 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
387                 memset(vcpu_book3s->sid_map, 0,
388                        sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
389                 kvmppc_mmu_pte_flush(vcpu, 0, 0);
390                 kvmppc_mmu_flush_segments(vcpu);
391         }
392         map->host_vsid = vcpu_book3s->vsid_next;
393
394         /* Would have to be 111 to be completely aligned with the rest of
395            Linux, but that is just way too little space! */
396         vcpu_book3s->vsid_next+=1;
397
398         map->guest_vsid = gvsid;
399         map->valid = true;
400
401         return map;
402 }
403
404 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
405 {
406         u32 esid = eaddr >> SID_SHIFT;
407         u64 gvsid;
408         u32 sr;
409         struct kvmppc_sid_map *map;
410         struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
411
412         if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
413                 /* Invalidate an entry */
414                 svcpu->sr[esid] = SR_INVALID;
415                 return -ENOENT;
416         }
417
418         map = find_sid_vsid(vcpu, gvsid);
419         if (!map)
420                 map = create_sid_map(vcpu, gvsid);
421
422         map->guest_esid = esid;
423         sr = map->host_vsid | SR_KP;
424         svcpu->sr[esid] = sr;
425
426         dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);
427
428         return 0;
429 }
430
431 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
432 {
433         int i;
434         struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
435
436         dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));
437         for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)
438                 svcpu->sr[i] = SR_INVALID;
439 }
440
441 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
442 {
443         kvmppc_mmu_pte_flush(vcpu, 0, 0);
444         preempt_disable();
445         __destroy_context(to_book3s(vcpu)->context_id);
446         preempt_enable();
447 }
448
449 /* From mm/mmu_context_hash32.c */
450 #define CTX_TO_VSID(ctx) (((ctx) * (897 * 16)) & 0xffffff)
451
452 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
453 {
454         struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
455         int err;
456
457         err = __init_new_context();
458         if (err < 0)
459                 return -1;
460         vcpu3s->context_id = err;
461
462         vcpu3s->vsid_max = CTX_TO_VSID(vcpu3s->context_id + 1) - 1;
463         vcpu3s->vsid_first = CTX_TO_VSID(vcpu3s->context_id);
464
465 #if 0 /* XXX still doesn't guarantee uniqueness */
466         /* We could collide with the Linux vsid space because the vsid
467          * wraps around at 24 bits. We're safe if we do our own space
468          * though, so let's always set the highest bit. */
469
470         vcpu3s->vsid_max |= 0x00800000;
471         vcpu3s->vsid_first |= 0x00800000;
472 #endif
473         BUG_ON(vcpu3s->vsid_max < vcpu3s->vsid_first);
474
475         vcpu3s->vsid_next = vcpu3s->vsid_first;
476
477         return 0;
478 }