]> Pileus Git - ~andy/linux/blob - arch/powerpc/kvm/book3s_64_mmu_host.c
KVM: PPC: Magic Page Book3s support
[~andy/linux] / 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 #include <linux/hash.h>
24
25 #include <asm/kvm_ppc.h>
26 #include <asm/kvm_book3s.h>
27 #include <asm/mmu-hash64.h>
28 #include <asm/machdep.h>
29 #include <asm/mmu_context.h>
30 #include <asm/hw_irq.h>
31
32 #define PTE_SIZE 12
33 #define VSID_ALL 0
34
35 /* #define DEBUG_MMU */
36 /* #define DEBUG_SLB */
37
38 #ifdef DEBUG_MMU
39 #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
40 #else
41 #define dprintk_mmu(a, ...) do { } while(0)
42 #endif
43
44 #ifdef DEBUG_SLB
45 #define dprintk_slb(a, ...) printk(KERN_INFO a, __VA_ARGS__)
46 #else
47 #define dprintk_slb(a, ...) do { } while(0)
48 #endif
49
50 void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
51 {
52         ppc_md.hpte_invalidate(pte->slot, pte->host_va,
53                                MMU_PAGE_4K, MMU_SEGSIZE_256M,
54                                false);
55 }
56
57 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
58  * a hash, so we don't waste cycles on looping */
59 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
60 {
61         return hash_64(gvsid, SID_MAP_BITS);
62 }
63
64 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
65 {
66         struct kvmppc_sid_map *map;
67         u16 sid_map_mask;
68
69         if (vcpu->arch.shared->msr & MSR_PR)
70                 gvsid |= VSID_PR;
71
72         sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
73         map = &to_book3s(vcpu)->sid_map[sid_map_mask];
74         if (map->guest_vsid == gvsid) {
75                 dprintk_slb("SLB: Searching: 0x%llx -> 0x%llx\n",
76                             gvsid, map->host_vsid);
77                 return map;
78         }
79
80         map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
81         if (map->guest_vsid == gvsid) {
82                 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
83                             gvsid, map->host_vsid);
84                 return map;
85         }
86
87         dprintk_slb("SLB: Searching %d/%d: 0x%llx -> not found\n",
88                     sid_map_mask, SID_MAP_MASK - sid_map_mask, gvsid);
89         return NULL;
90 }
91
92 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
93 {
94         pfn_t hpaddr;
95         ulong hash, hpteg, va;
96         u64 vsid;
97         int ret;
98         int rflags = 0x192;
99         int vflags = 0;
100         int attempt = 0;
101         struct kvmppc_sid_map *map;
102
103         /* Get host physical address for gpa */
104         hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
105         if (kvm_is_error_hva(hpaddr)) {
106                 printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
107                 return -EINVAL;
108         }
109         hpaddr <<= PAGE_SHIFT;
110         hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
111
112         /* and write the mapping ea -> hpa into the pt */
113         vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
114         map = find_sid_vsid(vcpu, vsid);
115         if (!map) {
116                 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
117                 WARN_ON(ret < 0);
118                 map = find_sid_vsid(vcpu, vsid);
119         }
120         if (!map) {
121                 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
122                                 vsid, orig_pte->eaddr);
123                 WARN_ON(true);
124                 return -EINVAL;
125         }
126
127         vsid = map->host_vsid;
128         va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
129
130         if (!orig_pte->may_write)
131                 rflags |= HPTE_R_PP;
132         else
133                 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
134
135         if (!orig_pte->may_execute)
136                 rflags |= HPTE_R_N;
137
138         hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
139
140 map_again:
141         hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
142
143         /* In case we tried normal mapping already, let's nuke old entries */
144         if (attempt > 1)
145                 if (ppc_md.hpte_remove(hpteg) < 0)
146                         return -1;
147
148         ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
149
150         if (ret < 0) {
151                 /* If we couldn't map a primary PTE, try a secondary */
152                 hash = ~hash;
153                 vflags ^= HPTE_V_SECONDARY;
154                 attempt++;
155                 goto map_again;
156         } else {
157                 struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
158
159                 dprintk_mmu("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx\n",
160                             ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
161                             (rflags & HPTE_R_N) ? '-' : 'x',
162                             orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
163
164                 /* The ppc_md code may give us a secondary entry even though we
165                    asked for a primary. Fix up. */
166                 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
167                         hash = ~hash;
168                         hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
169                 }
170
171                 pte->slot = hpteg + (ret & 7);
172                 pte->host_va = va;
173                 pte->pte = *orig_pte;
174                 pte->pfn = hpaddr >> PAGE_SHIFT;
175
176                 kvmppc_mmu_hpte_cache_map(vcpu, pte);
177         }
178
179         return 0;
180 }
181
182 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
183 {
184         struct kvmppc_sid_map *map;
185         struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
186         u16 sid_map_mask;
187         static int backwards_map = 0;
188
189         if (vcpu->arch.shared->msr & MSR_PR)
190                 gvsid |= VSID_PR;
191
192         /* We might get collisions that trap in preceding order, so let's
193            map them differently */
194
195         sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
196         if (backwards_map)
197                 sid_map_mask = SID_MAP_MASK - sid_map_mask;
198
199         map = &to_book3s(vcpu)->sid_map[sid_map_mask];
200
201         /* Make sure we're taking the other map next time */
202         backwards_map = !backwards_map;
203
204         /* Uh-oh ... out of mappings. Let's flush! */
205         if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
206                 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
207                 memset(vcpu_book3s->sid_map, 0,
208                        sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
209                 kvmppc_mmu_pte_flush(vcpu, 0, 0);
210                 kvmppc_mmu_flush_segments(vcpu);
211         }
212         map->host_vsid = vcpu_book3s->vsid_next++;
213
214         map->guest_vsid = gvsid;
215         map->valid = true;
216
217         dprintk_slb("SLB: New mapping at %d: 0x%llx -> 0x%llx\n",
218                     sid_map_mask, gvsid, map->host_vsid);
219
220         return map;
221 }
222
223 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
224 {
225         int i;
226         int max_slb_size = 64;
227         int found_inval = -1;
228         int r;
229
230         if (!to_svcpu(vcpu)->slb_max)
231                 to_svcpu(vcpu)->slb_max = 1;
232
233         /* Are we overwriting? */
234         for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
235                 if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
236                         found_inval = i;
237                 else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
238                         return i;
239         }
240
241         /* Found a spare entry that was invalidated before */
242         if (found_inval > 0)
243                 return found_inval;
244
245         /* No spare invalid entry, so create one */
246
247         if (mmu_slb_size < 64)
248                 max_slb_size = mmu_slb_size;
249
250         /* Overflowing -> purge */
251         if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
252                 kvmppc_mmu_flush_segments(vcpu);
253
254         r = to_svcpu(vcpu)->slb_max;
255         to_svcpu(vcpu)->slb_max++;
256
257         return r;
258 }
259
260 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
261 {
262         u64 esid = eaddr >> SID_SHIFT;
263         u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
264         u64 slb_vsid = SLB_VSID_USER;
265         u64 gvsid;
266         int slb_index;
267         struct kvmppc_sid_map *map;
268
269         slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
270
271         if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
272                 /* Invalidate an entry */
273                 to_svcpu(vcpu)->slb[slb_index].esid = 0;
274                 return -ENOENT;
275         }
276
277         map = find_sid_vsid(vcpu, gvsid);
278         if (!map)
279                 map = create_sid_map(vcpu, gvsid);
280
281         map->guest_esid = esid;
282
283         slb_vsid |= (map->host_vsid << 12);
284         slb_vsid &= ~SLB_VSID_KP;
285         slb_esid |= slb_index;
286
287         to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
288         to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
289
290         dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
291
292         return 0;
293 }
294
295 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
296 {
297         to_svcpu(vcpu)->slb_max = 1;
298         to_svcpu(vcpu)->slb[0].esid = 0;
299 }
300
301 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
302 {
303         kvmppc_mmu_hpte_destroy(vcpu);
304         __destroy_context(to_book3s(vcpu)->context_id);
305 }
306
307 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
308 {
309         struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
310         int err;
311
312         err = __init_new_context();
313         if (err < 0)
314                 return -1;
315         vcpu3s->context_id = err;
316
317         vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
318         vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
319         vcpu3s->vsid_next = vcpu3s->vsid_first;
320
321         kvmppc_mmu_hpte_init(vcpu);
322
323         return 0;
324 }