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 #include "trace_pr.h"
31 
32 #define PTE_SIZE 12
33 
34 void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
35 {
36 	ppc_md.hpte_invalidate(pte->slot, pte->host_vpn,
37 			       pte->pagesize, pte->pagesize, MMU_SEGSIZE_256M,
38 			       false);
39 }
40 
41 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
42  * a hash, so we don't waste cycles on looping */
43 static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
44 {
45 	return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
46 		     ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
47 		     ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
48 		     ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
49 		     ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
50 		     ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
51 		     ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
52 		     ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
53 }
54 
55 
56 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
57 {
58 	struct kvmppc_sid_map *map;
59 	u16 sid_map_mask;
60 
61 	if (kvmppc_get_msr(vcpu) & MSR_PR)
62 		gvsid |= VSID_PR;
63 
64 	sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
65 	map = &to_book3s(vcpu)->sid_map[sid_map_mask];
66 	if (map->valid && (map->guest_vsid == gvsid)) {
67 		trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
68 		return map;
69 	}
70 
71 	map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
72 	if (map->valid && (map->guest_vsid == gvsid)) {
73 		trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
74 		return map;
75 	}
76 
77 	trace_kvm_book3s_slb_fail(sid_map_mask, gvsid);
78 	return NULL;
79 }
80 
81 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte,
82 			bool iswrite)
83 {
84 	unsigned long vpn;
85 	pfn_t hpaddr;
86 	ulong hash, hpteg;
87 	u64 vsid;
88 	int ret;
89 	int rflags = 0x192;
90 	int vflags = 0;
91 	int attempt = 0;
92 	struct kvmppc_sid_map *map;
93 	int r = 0;
94 	int hpsize = MMU_PAGE_4K;
95 	bool writable;
96 	unsigned long mmu_seq;
97 	struct kvm *kvm = vcpu->kvm;
98 	struct hpte_cache *cpte;
99 	unsigned long gfn = orig_pte->raddr >> PAGE_SHIFT;
100 	unsigned long pfn;
101 
102 	/* used to check for invalidations in progress */
103 	mmu_seq = kvm->mmu_notifier_seq;
104 	smp_rmb();
105 
106 	/* Get host physical address for gpa */
107 	pfn = kvmppc_gfn_to_pfn(vcpu, gfn, iswrite, &writable);
108 	if (is_error_noslot_pfn(pfn)) {
109 		printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", gfn);
110 		r = -EINVAL;
111 		goto out;
112 	}
113 	hpaddr = pfn << PAGE_SHIFT;
114 
115 	/* and write the mapping ea -> hpa into the pt */
116 	vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
117 	map = find_sid_vsid(vcpu, vsid);
118 	if (!map) {
119 		ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
120 		WARN_ON(ret < 0);
121 		map = find_sid_vsid(vcpu, vsid);
122 	}
123 	if (!map) {
124 		printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
125 				vsid, orig_pte->eaddr);
126 		WARN_ON(true);
127 		r = -EINVAL;
128 		goto out;
129 	}
130 
131 	vpn = hpt_vpn(orig_pte->eaddr, map->host_vsid, MMU_SEGSIZE_256M);
132 
133 	kvm_set_pfn_accessed(pfn);
134 	if (!orig_pte->may_write || !writable)
135 		rflags |= PP_RXRX;
136 	else {
137 		mark_page_dirty(vcpu->kvm, gfn);
138 		kvm_set_pfn_dirty(pfn);
139 	}
140 
141 	if (!orig_pte->may_execute)
142 		rflags |= HPTE_R_N;
143 	else
144 		kvmppc_mmu_flush_icache(pfn);
145 
146 	/*
147 	 * Use 64K pages if possible; otherwise, on 64K page kernels,
148 	 * we need to transfer 4 more bits from guest real to host real addr.
149 	 */
150 	if (vsid & VSID_64K)
151 		hpsize = MMU_PAGE_64K;
152 	else
153 		hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
154 
155 	hash = hpt_hash(vpn, mmu_psize_defs[hpsize].shift, MMU_SEGSIZE_256M);
156 
157 	cpte = kvmppc_mmu_hpte_cache_next(vcpu);
158 
159 	spin_lock(&kvm->mmu_lock);
160 	if (!cpte || mmu_notifier_retry(kvm, mmu_seq)) {
161 		r = -EAGAIN;
162 		goto out_unlock;
163 	}
164 
165 map_again:
166 	hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
167 
168 	/* In case we tried normal mapping already, let's nuke old entries */
169 	if (attempt > 1)
170 		if (ppc_md.hpte_remove(hpteg) < 0) {
171 			r = -1;
172 			goto out_unlock;
173 		}
174 
175 	ret = ppc_md.hpte_insert(hpteg, vpn, hpaddr, rflags, vflags,
176 				 hpsize, hpsize, MMU_SEGSIZE_256M);
177 
178 	if (ret < 0) {
179 		/* If we couldn't map a primary PTE, try a secondary */
180 		hash = ~hash;
181 		vflags ^= HPTE_V_SECONDARY;
182 		attempt++;
183 		goto map_again;
184 	} else {
185 		trace_kvm_book3s_64_mmu_map(rflags, hpteg,
186 					    vpn, hpaddr, orig_pte);
187 
188 		/* The ppc_md code may give us a secondary entry even though we
189 		   asked for a primary. Fix up. */
190 		if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
191 			hash = ~hash;
192 			hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
193 		}
194 
195 		cpte->slot = hpteg + (ret & 7);
196 		cpte->host_vpn = vpn;
197 		cpte->pte = *orig_pte;
198 		cpte->pfn = pfn;
199 		cpte->pagesize = hpsize;
200 
201 		kvmppc_mmu_hpte_cache_map(vcpu, cpte);
202 		cpte = NULL;
203 	}
204 
205 out_unlock:
206 	spin_unlock(&kvm->mmu_lock);
207 	kvm_release_pfn_clean(pfn);
208 	if (cpte)
209 		kvmppc_mmu_hpte_cache_free(cpte);
210 
211 out:
212 	return r;
213 }
214 
215 void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
216 {
217 	u64 mask = 0xfffffffffULL;
218 	u64 vsid;
219 
220 	vcpu->arch.mmu.esid_to_vsid(vcpu, pte->eaddr >> SID_SHIFT, &vsid);
221 	if (vsid & VSID_64K)
222 		mask = 0xffffffff0ULL;
223 	kvmppc_mmu_pte_vflush(vcpu, pte->vpage, mask);
224 }
225 
226 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
227 {
228 	struct kvmppc_sid_map *map;
229 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
230 	u16 sid_map_mask;
231 	static int backwards_map = 0;
232 
233 	if (kvmppc_get_msr(vcpu) & MSR_PR)
234 		gvsid |= VSID_PR;
235 
236 	/* We might get collisions that trap in preceding order, so let's
237 	   map them differently */
238 
239 	sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
240 	if (backwards_map)
241 		sid_map_mask = SID_MAP_MASK - sid_map_mask;
242 
243 	map = &to_book3s(vcpu)->sid_map[sid_map_mask];
244 
245 	/* Make sure we're taking the other map next time */
246 	backwards_map = !backwards_map;
247 
248 	/* Uh-oh ... out of mappings. Let's flush! */
249 	if (vcpu_book3s->proto_vsid_next == vcpu_book3s->proto_vsid_max) {
250 		vcpu_book3s->proto_vsid_next = vcpu_book3s->proto_vsid_first;
251 		memset(vcpu_book3s->sid_map, 0,
252 		       sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
253 		kvmppc_mmu_pte_flush(vcpu, 0, 0);
254 		kvmppc_mmu_flush_segments(vcpu);
255 	}
256 	map->host_vsid = vsid_scramble(vcpu_book3s->proto_vsid_next++, 256M);
257 
258 	map->guest_vsid = gvsid;
259 	map->valid = true;
260 
261 	trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid);
262 
263 	return map;
264 }
265 
266 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
267 {
268 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
269 	int i;
270 	int max_slb_size = 64;
271 	int found_inval = -1;
272 	int r;
273 
274 	/* Are we overwriting? */
275 	for (i = 0; i < svcpu->slb_max; i++) {
276 		if (!(svcpu->slb[i].esid & SLB_ESID_V))
277 			found_inval = i;
278 		else if ((svcpu->slb[i].esid & ESID_MASK) == esid) {
279 			r = i;
280 			goto out;
281 		}
282 	}
283 
284 	/* Found a spare entry that was invalidated before */
285 	if (found_inval >= 0) {
286 		r = found_inval;
287 		goto out;
288 	}
289 
290 	/* No spare invalid entry, so create one */
291 
292 	if (mmu_slb_size < 64)
293 		max_slb_size = mmu_slb_size;
294 
295 	/* Overflowing -> purge */
296 	if ((svcpu->slb_max) == max_slb_size)
297 		kvmppc_mmu_flush_segments(vcpu);
298 
299 	r = svcpu->slb_max;
300 	svcpu->slb_max++;
301 
302 out:
303 	svcpu_put(svcpu);
304 	return r;
305 }
306 
307 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
308 {
309 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
310 	u64 esid = eaddr >> SID_SHIFT;
311 	u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
312 	u64 slb_vsid = SLB_VSID_USER;
313 	u64 gvsid;
314 	int slb_index;
315 	struct kvmppc_sid_map *map;
316 	int r = 0;
317 
318 	slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
319 
320 	if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
321 		/* Invalidate an entry */
322 		svcpu->slb[slb_index].esid = 0;
323 		r = -ENOENT;
324 		goto out;
325 	}
326 
327 	map = find_sid_vsid(vcpu, gvsid);
328 	if (!map)
329 		map = create_sid_map(vcpu, gvsid);
330 
331 	map->guest_esid = esid;
332 
333 	slb_vsid |= (map->host_vsid << 12);
334 	slb_vsid &= ~SLB_VSID_KP;
335 	slb_esid |= slb_index;
336 
337 #ifdef CONFIG_PPC_64K_PAGES
338 	/* Set host segment base page size to 64K if possible */
339 	if (gvsid & VSID_64K)
340 		slb_vsid |= mmu_psize_defs[MMU_PAGE_64K].sllp;
341 #endif
342 
343 	svcpu->slb[slb_index].esid = slb_esid;
344 	svcpu->slb[slb_index].vsid = slb_vsid;
345 
346 	trace_kvm_book3s_slbmte(slb_vsid, slb_esid);
347 
348 out:
349 	svcpu_put(svcpu);
350 	return r;
351 }
352 
353 void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong ea, ulong seg_size)
354 {
355 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
356 	ulong seg_mask = -seg_size;
357 	int i;
358 
359 	for (i = 0; i < svcpu->slb_max; i++) {
360 		if ((svcpu->slb[i].esid & SLB_ESID_V) &&
361 		    (svcpu->slb[i].esid & seg_mask) == ea) {
362 			/* Invalidate this entry */
363 			svcpu->slb[i].esid = 0;
364 		}
365 	}
366 
367 	svcpu_put(svcpu);
368 }
369 
370 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
371 {
372 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
373 	svcpu->slb_max = 0;
374 	svcpu->slb[0].esid = 0;
375 	svcpu_put(svcpu);
376 }
377 
378 void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu)
379 {
380 	kvmppc_mmu_hpte_destroy(vcpu);
381 	__destroy_context(to_book3s(vcpu)->context_id[0]);
382 }
383 
384 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
385 {
386 	struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
387 	int err;
388 
389 	err = __init_new_context();
390 	if (err < 0)
391 		return -1;
392 	vcpu3s->context_id[0] = err;
393 
394 	vcpu3s->proto_vsid_max = ((u64)(vcpu3s->context_id[0] + 1)
395 				  << ESID_BITS) - 1;
396 	vcpu3s->proto_vsid_first = (u64)vcpu3s->context_id[0] << ESID_BITS;
397 	vcpu3s->proto_vsid_next = vcpu3s->proto_vsid_first;
398 
399 	kvmppc_mmu_hpte_init(vcpu);
400 
401 	return 0;
402 }
403