KVM: PPC: Convert u64 -> ulong
[safe/jmp/linux-2.6] / arch / powerpc / kvm / book3s_64_mmu_host.c
1 /*
2  * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
3  *
4  * Authors:
5  *     Alexander Graf <agraf@suse.de>
6  *     Kevin Wolf <mail@kevin-wolf.de>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License, version 2, as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #include <linux/kvm_host.h>
23
24 #include <asm/kvm_ppc.h>
25 #include <asm/kvm_book3s.h>
26 #include <asm/mmu-hash64.h>
27 #include <asm/machdep.h>
28 #include <asm/mmu_context.h>
29 #include <asm/hw_irq.h>
30
31 #define PTE_SIZE 12
32 #define VSID_ALL 0
33
34 /* #define DEBUG_MMU */
35 /* #define DEBUG_SLB */
36
37 #ifdef DEBUG_MMU
38 #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
39 #else
40 #define dprintk_mmu(a, ...) do { } while(0)
41 #endif
42
43 #ifdef DEBUG_SLB
44 #define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
45 #else
46 #define dprintk_slb(a, ...) do { } while(0)
47 #endif
48
49 static void invalidate_pte(struct hpte_cache *pte)
50 {
51         dprintk_mmu("KVM: Flushing SPT %d: 0x%llx (0x%llx) -> 0x%llx\n",
52                     i, pte->pte.eaddr, pte->pte.vpage, pte->host_va);
53
54         ppc_md.hpte_invalidate(pte->slot, pte->host_va,
55                                MMU_PAGE_4K, MMU_SEGSIZE_256M,
56                                false);
57         pte->host_va = 0;
58
59         if (pte->pte.may_write)
60                 kvm_release_pfn_dirty(pte->pfn);
61         else
62                 kvm_release_pfn_clean(pte->pfn);
63 }
64
65 void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
66 {
67         int i;
68
69         dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%llx & 0x%llx\n",
70                     vcpu->arch.hpte_cache_offset, guest_ea, ea_mask);
71         BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
72
73         guest_ea &= ea_mask;
74         for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
75                 struct hpte_cache *pte;
76
77                 pte = &vcpu->arch.hpte_cache[i];
78                 if (!pte->host_va)
79                         continue;
80
81                 if ((pte->pte.eaddr & ea_mask) == guest_ea) {
82                         invalidate_pte(pte);
83                 }
84         }
85
86         /* Doing a complete flush -> start from scratch */
87         if (!ea_mask)
88                 vcpu->arch.hpte_cache_offset = 0;
89 }
90
91 void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
92 {
93         int i;
94
95         dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
96                     vcpu->arch.hpte_cache_offset, guest_vp, vp_mask);
97         BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
98
99         guest_vp &= vp_mask;
100         for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
101                 struct hpte_cache *pte;
102
103                 pte = &vcpu->arch.hpte_cache[i];
104                 if (!pte->host_va)
105                         continue;
106
107                 if ((pte->pte.vpage & vp_mask) == guest_vp) {
108                         invalidate_pte(pte);
109                 }
110         }
111 }
112
113 void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
114 {
115         int i;
116
117         dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%llx & 0x%llx\n",
118                     vcpu->arch.hpte_cache_offset, guest_pa, pa_mask);
119         BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
120
121         for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
122                 struct hpte_cache *pte;
123
124                 pte = &vcpu->arch.hpte_cache[i];
125                 if (!pte->host_va)
126                         continue;
127
128                 if ((pte->pte.raddr >= pa_start) &&
129                     (pte->pte.raddr < pa_end)) {
130                         invalidate_pte(pte);
131                 }
132         }
133 }
134
135 struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data)
136 {
137         int i;
138         u64 guest_vp;
139
140         guest_vp = vcpu->arch.mmu.ea_to_vp(vcpu, ea, false);
141         for (i=0; i<vcpu->arch.hpte_cache_offset; i++) {
142                 struct hpte_cache *pte;
143
144                 pte = &vcpu->arch.hpte_cache[i];
145                 if (!pte->host_va)
146                         continue;
147
148                 if (pte->pte.vpage == guest_vp)
149                         return &pte->pte;
150         }
151
152         return NULL;
153 }
154
155 static int kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
156 {
157         if (vcpu->arch.hpte_cache_offset == HPTEG_CACHE_NUM)
158                 kvmppc_mmu_pte_flush(vcpu, 0, 0);
159
160         return vcpu->arch.hpte_cache_offset++;
161 }
162
163 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
164  * a hash, so we don't waste cycles on looping */
165 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
166 {
167         return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
168                      ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
169                      ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
170                      ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
171                      ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
172                      ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
173                      ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
174                      ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
175 }
176
177
178 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
179 {
180         struct kvmppc_sid_map *map;
181         u16 sid_map_mask;
182
183         if (vcpu->arch.msr & MSR_PR)
184                 gvsid |= VSID_PR;
185
186         sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
187         map = &to_book3s(vcpu)->sid_map[sid_map_mask];
188         if (map->guest_vsid == gvsid) {
189                 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
190                             gvsid, map->host_vsid);
191                 return map;
192         }
193
194         map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
195         if (map->guest_vsid == gvsid) {
196                 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
197                             gvsid, map->host_vsid);
198                 return map;
199         }
200
201         dprintk_slb("SLB: Searching 0x%llx -> not found\n", gvsid);
202         return NULL;
203 }
204
205 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
206 {
207         pfn_t hpaddr;
208         ulong hash, hpteg, va;
209         u64 vsid;
210         int ret;
211         int rflags = 0x192;
212         int vflags = 0;
213         int attempt = 0;
214         struct kvmppc_sid_map *map;
215
216         /* Get host physical address for gpa */
217         hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
218         if (kvm_is_error_hva(hpaddr)) {
219                 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
220                 return -EINVAL;
221         }
222         hpaddr <<= PAGE_SHIFT;
223 #if PAGE_SHIFT == 12
224 #elif PAGE_SHIFT == 16
225         hpaddr |= orig_pte->raddr & 0xf000;
226 #else
227 #error Unknown page size
228 #endif
229
230         /* and write the mapping ea -> hpa into the pt */
231         vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
232         map = find_sid_vsid(vcpu, vsid);
233         if (!map) {
234                 kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
235                 map = find_sid_vsid(vcpu, vsid);
236         }
237         BUG_ON(!map);
238
239         vsid = map->host_vsid;
240         va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
241
242         if (!orig_pte->may_write)
243                 rflags |= HPTE_R_PP;
244         else
245                 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
246
247         if (!orig_pte->may_execute)
248                 rflags |= HPTE_R_N;
249
250         hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
251
252 map_again:
253         hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
254
255         /* In case we tried normal mapping already, let's nuke old entries */
256         if (attempt > 1)
257                 if (ppc_md.hpte_remove(hpteg) < 0)
258                         return -1;
259
260         ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
261
262         if (ret < 0) {
263                 /* If we couldn't map a primary PTE, try a secondary */
264                 hash = ~hash;
265                 vflags ^= HPTE_V_SECONDARY;
266                 attempt++;
267                 goto map_again;
268         } else {
269                 int hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
270                 struct hpte_cache *pte = &vcpu->arch.hpte_cache[hpte_id];
271
272                 dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%lx (0x%llx) -> %lx\n",
273                             ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
274                             (rflags & HPTE_R_N) ? '-' : 'x',
275                             orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
276
277                 /* The ppc_md code may give us a secondary entry even though we
278                    asked for a primary. Fix up. */
279                 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
280                         hash = ~hash;
281                         hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
282                 }
283
284                 pte->slot = hpteg + (ret & 7);
285                 pte->host_va = va;
286                 pte->pte = *orig_pte;
287                 pte->pfn = hpaddr >> PAGE_SHIFT;
288         }
289
290         return 0;
291 }
292
293 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
294 {
295         struct kvmppc_sid_map *map;
296         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
297         u16 sid_map_mask;
298         static int backwards_map = 0;
299
300         if (vcpu->arch.msr & MSR_PR)
301                 gvsid |= VSID_PR;
302
303         /* We might get collisions that trap in preceding order, so let's
304            map them differently */
305
306         sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
307         if (backwards_map)
308                 sid_map_mask = SID_MAP_MASK - sid_map_mask;
309
310         map = &to_book3s(vcpu)->sid_map[sid_map_mask];
311
312         /* Make sure we're taking the other map next time */
313         backwards_map = !backwards_map;
314
315         /* Uh-oh ... out of mappings. Let's flush! */
316         if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
317                 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
318                 memset(vcpu_book3s->sid_map, 0,
319                        sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
320                 kvmppc_mmu_pte_flush(vcpu, 0, 0);
321                 kvmppc_mmu_flush_segments(vcpu);
322         }
323         map->host_vsid = vcpu_book3s->vsid_next++;
324
325         map->guest_vsid = gvsid;
326         map->valid = true;
327
328         return map;
329 }
330
331 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
332 {
333         int i;
334         int max_slb_size = 64;
335         int found_inval = -1;
336         int r;
337
338         if (!to_svcpu(vcpu)->slb_max)
339                 to_svcpu(vcpu)->slb_max = 1;
340
341         /* Are we overwriting? */
342         for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
343                 if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
344                         found_inval = i;
345                 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
346                         return i;
347         }
348
349         /* Found a spare entry that was invalidated before */
350         if (found_inval > 0)
351                 return found_inval;
352
353         /* No spare invalid entry, so create one */
354
355         if (mmu_slb_size < 64)
356                 max_slb_size = mmu_slb_size;
357
358         /* Overflowing -> purge */
359         if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
360                 kvmppc_mmu_flush_segments(vcpu);
361
362         r = to_svcpu(vcpu)->slb_max;
363         to_svcpu(vcpu)->slb_max++;
364
365         return r;
366 }
367
368 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
369 {
370         u64 esid = eaddr >> SID_SHIFT;
371         u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
372         u64 slb_vsid = SLB_VSID_USER;
373         u64 gvsid;
374         int slb_index;
375         struct kvmppc_sid_map *map;
376
377         slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
378
379         if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
380                 /* Invalidate an entry */
381                 to_svcpu(vcpu)->slb[slb_index].esid = 0;
382                 return -ENOENT;
383         }
384
385         map = find_sid_vsid(vcpu, gvsid);
386         if (!map)
387                 map = create_sid_map(vcpu, gvsid);
388
389         map->guest_esid = esid;
390
391         slb_vsid |= (map->host_vsid << 12);
392         slb_vsid &= ~SLB_VSID_KP;
393         slb_esid |= slb_index;
394
395         to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
396         to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
397
398         dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
399
400         return 0;
401 }
402
403 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
404 {
405         to_svcpu(vcpu)->slb_max = 1;
406         to_svcpu(vcpu)->slb[0].esid = 0;
407 }
408
409 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
410 {
411         kvmppc_mmu_pte_flush(vcpu, 0, 0);
412         __destroy_context(to_book3s(vcpu)->context_id);
413 }
414
415 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
416 {
417         struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
418         int err;
419
420         err = __init_new_context();
421         if (err < 0)
422                 return -1;
423         vcpu3s->context_id = err;
424
425         vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
426         vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
427         vcpu3s->vsid_next = vcpu3s->vsid_first;
428
429         return 0;
430 }