blob: 25bd4ede722c72d3256eb5cce9a5cbc6eb3de058 [file] [log] [blame]
Alexander Graf0d8dc682009-10-30 05:47:11 +00001/*
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
49static 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 kvm_release_pfn_dirty(pte->pfn);
59}
60
61void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, u64 guest_ea, u64 ea_mask)
62{
63 int i;
64
65 dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%llx & 0x%llx\n",
66 vcpu->arch.hpte_cache_offset, guest_ea, ea_mask);
67 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
68
69 guest_ea &= ea_mask;
70 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
71 struct hpte_cache *pte;
72
73 pte = &vcpu->arch.hpte_cache[i];
74 if (!pte->host_va)
75 continue;
76
77 if ((pte->pte.eaddr & ea_mask) == guest_ea) {
78 invalidate_pte(pte);
79 }
80 }
81
82 /* Doing a complete flush -> start from scratch */
83 if (!ea_mask)
84 vcpu->arch.hpte_cache_offset = 0;
85}
86
87void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
88{
89 int i;
90
91 dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
92 vcpu->arch.hpte_cache_offset, guest_vp, vp_mask);
93 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
94
95 guest_vp &= vp_mask;
96 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
97 struct hpte_cache *pte;
98
99 pte = &vcpu->arch.hpte_cache[i];
100 if (!pte->host_va)
101 continue;
102
103 if ((pte->pte.vpage & vp_mask) == guest_vp) {
104 invalidate_pte(pte);
105 }
106 }
107}
108
109void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, u64 pa_start, u64 pa_end)
110{
111 int i;
112
113 dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%llx & 0x%llx\n",
114 vcpu->arch.hpte_cache_offset, guest_pa, pa_mask);
115 BUG_ON(vcpu->arch.hpte_cache_offset > HPTEG_CACHE_NUM);
116
117 for (i = 0; i < vcpu->arch.hpte_cache_offset; i++) {
118 struct hpte_cache *pte;
119
120 pte = &vcpu->arch.hpte_cache[i];
121 if (!pte->host_va)
122 continue;
123
124 if ((pte->pte.raddr >= pa_start) &&
125 (pte->pte.raddr < pa_end)) {
126 invalidate_pte(pte);
127 }
128 }
129}
130
131struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data)
132{
133 int i;
134 u64 guest_vp;
135
136 guest_vp = vcpu->arch.mmu.ea_to_vp(vcpu, ea, false);
137 for (i=0; i<vcpu->arch.hpte_cache_offset; i++) {
138 struct hpte_cache *pte;
139
140 pte = &vcpu->arch.hpte_cache[i];
141 if (!pte->host_va)
142 continue;
143
144 if (pte->pte.vpage == guest_vp)
145 return &pte->pte;
146 }
147
148 return NULL;
149}
150
151static int kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
152{
153 if (vcpu->arch.hpte_cache_offset == HPTEG_CACHE_NUM)
154 kvmppc_mmu_pte_flush(vcpu, 0, 0);
155
156 return vcpu->arch.hpte_cache_offset++;
157}
158
159/* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
160 * a hash, so we don't waste cycles on looping */
161static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
162{
163 return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
164 ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
165 ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
166 ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
167 ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
168 ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
169 ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
170 ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
171}
172
173
174static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
175{
176 struct kvmppc_sid_map *map;
177 u16 sid_map_mask;
178
179 if (vcpu->arch.msr & MSR_PR)
180 gvsid |= VSID_PR;
181
182 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
183 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
184 if (map->guest_vsid == gvsid) {
185 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
186 gvsid, map->host_vsid);
187 return map;
188 }
189
190 map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
191 if (map->guest_vsid == gvsid) {
192 dprintk_slb("SLB: Searching 0x%llx -> 0x%llx\n",
193 gvsid, map->host_vsid);
194 return map;
195 }
196
197 dprintk_slb("SLB: Searching 0x%llx -> not found\n", gvsid);
198 return NULL;
199}
200
201int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
202{
203 pfn_t hpaddr;
204 ulong hash, hpteg, va;
205 u64 vsid;
206 int ret;
207 int rflags = 0x192;
208 int vflags = 0;
209 int attempt = 0;
210 struct kvmppc_sid_map *map;
211
212 /* Get host physical address for gpa */
213 hpaddr = gfn_to_pfn(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
214 if (kvm_is_error_hva(hpaddr)) {
215 printk(KERN_INFO "Couldn't get guest page for gfn %llx!\n", orig_pte->eaddr);
216 return -EINVAL;
217 }
218 hpaddr <<= PAGE_SHIFT;
219#if PAGE_SHIFT == 12
220#elif PAGE_SHIFT == 16
221 hpaddr |= orig_pte->raddr & 0xf000;
222#else
223#error Unknown page size
224#endif
225
226 /* and write the mapping ea -> hpa into the pt */
227 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
228 map = find_sid_vsid(vcpu, vsid);
229 if (!map) {
230 kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
231 map = find_sid_vsid(vcpu, vsid);
232 }
233 BUG_ON(!map);
234
235 vsid = map->host_vsid;
236 va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
237
238 if (!orig_pte->may_write)
239 rflags |= HPTE_R_PP;
240 else
241 mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
242
243 if (!orig_pte->may_execute)
244 rflags |= HPTE_R_N;
245
246 hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
247
248map_again:
249 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
250
251 /* In case we tried normal mapping already, let's nuke old entries */
252 if (attempt > 1)
253 if (ppc_md.hpte_remove(hpteg) < 0)
254 return -1;
255
256 ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
257
258 if (ret < 0) {
259 /* If we couldn't map a primary PTE, try a secondary */
Alexander Graf0d8dc682009-10-30 05:47:11 +0000260 hash = ~hash;
Alexander Graf20a340a2010-02-19 11:00:46 +0100261 vflags ^= HPTE_V_SECONDARY;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000262 attempt++;
Alexander Graf0d8dc682009-10-30 05:47:11 +0000263 goto map_again;
264 } else {
265 int hpte_id = kvmppc_mmu_hpte_cache_next(vcpu);
266 struct hpte_cache *pte = &vcpu->arch.hpte_cache[hpte_id];
267
268 dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%lx (0x%llx) -> %lx\n",
269 ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
270 (rflags & HPTE_R_N) ? '-' : 'x',
271 orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
272
273 pte->slot = hpteg + (ret & 7);
274 pte->host_va = va;
275 pte->pte = *orig_pte;
276 pte->pfn = hpaddr >> PAGE_SHIFT;
277 }
278
279 return 0;
280}
281
282static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
283{
284 struct kvmppc_sid_map *map;
285 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
286 u16 sid_map_mask;
287 static int backwards_map = 0;
288
289 if (vcpu->arch.msr & MSR_PR)
290 gvsid |= VSID_PR;
291
292 /* We might get collisions that trap in preceding order, so let's
293 map them differently */
294
295 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
296 if (backwards_map)
297 sid_map_mask = SID_MAP_MASK - sid_map_mask;
298
299 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
300
301 /* Make sure we're taking the other map next time */
302 backwards_map = !backwards_map;
303
304 /* Uh-oh ... out of mappings. Let's flush! */
305 if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
306 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
307 memset(vcpu_book3s->sid_map, 0,
308 sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
309 kvmppc_mmu_pte_flush(vcpu, 0, 0);
310 kvmppc_mmu_flush_segments(vcpu);
311 }
312 map->host_vsid = vcpu_book3s->vsid_next++;
313
314 map->guest_vsid = gvsid;
315 map->valid = true;
316
317 return map;
318}
319
320static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
321{
322 int i;
323 int max_slb_size = 64;
324 int found_inval = -1;
325 int r;
326
327 if (!get_paca()->kvm_slb_max)
328 get_paca()->kvm_slb_max = 1;
329
330 /* Are we overwriting? */
331 for (i = 1; i < get_paca()->kvm_slb_max; i++) {
332 if (!(get_paca()->kvm_slb[i].esid & SLB_ESID_V))
333 found_inval = i;
334 else if ((get_paca()->kvm_slb[i].esid & ESID_MASK) == esid)
335 return i;
336 }
337
338 /* Found a spare entry that was invalidated before */
339 if (found_inval > 0)
340 return found_inval;
341
342 /* No spare invalid entry, so create one */
343
344 if (mmu_slb_size < 64)
345 max_slb_size = mmu_slb_size;
346
347 /* Overflowing -> purge */
348 if ((get_paca()->kvm_slb_max) == max_slb_size)
349 kvmppc_mmu_flush_segments(vcpu);
350
351 r = get_paca()->kvm_slb_max;
352 get_paca()->kvm_slb_max++;
353
354 return r;
355}
356
357int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
358{
359 u64 esid = eaddr >> SID_SHIFT;
360 u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
361 u64 slb_vsid = SLB_VSID_USER;
362 u64 gvsid;
363 int slb_index;
364 struct kvmppc_sid_map *map;
365
366 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
367
368 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
369 /* Invalidate an entry */
370 get_paca()->kvm_slb[slb_index].esid = 0;
371 return -ENOENT;
372 }
373
374 map = find_sid_vsid(vcpu, gvsid);
375 if (!map)
376 map = create_sid_map(vcpu, gvsid);
377
378 map->guest_esid = esid;
379
380 slb_vsid |= (map->host_vsid << 12);
381 slb_vsid &= ~SLB_VSID_KP;
382 slb_esid |= slb_index;
383
384 get_paca()->kvm_slb[slb_index].esid = slb_esid;
385 get_paca()->kvm_slb[slb_index].vsid = slb_vsid;
386
387 dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
388
389 return 0;
390}
391
392void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
393{
394 get_paca()->kvm_slb_max = 1;
395 get_paca()->kvm_slb[0].esid = 0;
396}
397
398void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
399{
400 kvmppc_mmu_pte_flush(vcpu, 0, 0);
401}