xref: /linux/arch/powerpc/kvm/book3s_64_mmu_host.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
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.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 = gfn_to_pfn(vcpu->kvm, 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 #if PAGE_SHIFT == 12
111 #elif PAGE_SHIFT == 16
112 	hpaddr |= orig_pte->raddr & 0xf000;
113 #else
114 #error Unknown page size
115 #endif
116 
117 	/* and write the mapping ea -> hpa into the pt */
118 	vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
119 	map = find_sid_vsid(vcpu, vsid);
120 	if (!map) {
121 		ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
122 		WARN_ON(ret < 0);
123 		map = find_sid_vsid(vcpu, vsid);
124 	}
125 	if (!map) {
126 		printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
127 				vsid, orig_pte->eaddr);
128 		WARN_ON(true);
129 		return -EINVAL;
130 	}
131 
132 	vsid = map->host_vsid;
133 	va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
134 
135 	if (!orig_pte->may_write)
136 		rflags |= HPTE_R_PP;
137 	else
138 		mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
139 
140 	if (!orig_pte->may_execute)
141 		rflags |= HPTE_R_N;
142 
143 	hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
144 
145 map_again:
146 	hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
147 
148 	/* In case we tried normal mapping already, let's nuke old entries */
149 	if (attempt > 1)
150 		if (ppc_md.hpte_remove(hpteg) < 0)
151 			return -1;
152 
153 	ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
154 
155 	if (ret < 0) {
156 		/* If we couldn't map a primary PTE, try a secondary */
157 		hash = ~hash;
158 		vflags ^= HPTE_V_SECONDARY;
159 		attempt++;
160 		goto map_again;
161 	} else {
162 		struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
163 
164 		dprintk_mmu("KVM: %c%c Map 0x%lx: [%lx] 0x%lx (0x%llx) -> %lx\n",
165 			    ((rflags & HPTE_R_PP) == 3) ? '-' : 'w',
166 			    (rflags & HPTE_R_N) ? '-' : 'x',
167 			    orig_pte->eaddr, hpteg, va, orig_pte->vpage, hpaddr);
168 
169 		/* The ppc_md code may give us a secondary entry even though we
170 		   asked for a primary. Fix up. */
171 		if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
172 			hash = ~hash;
173 			hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
174 		}
175 
176 		pte->slot = hpteg + (ret & 7);
177 		pte->host_va = va;
178 		pte->pte = *orig_pte;
179 		pte->pfn = hpaddr >> PAGE_SHIFT;
180 
181 		kvmppc_mmu_hpte_cache_map(vcpu, pte);
182 	}
183 
184 	return 0;
185 }
186 
187 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
188 {
189 	struct kvmppc_sid_map *map;
190 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
191 	u16 sid_map_mask;
192 	static int backwards_map = 0;
193 
194 	if (vcpu->arch.msr & MSR_PR)
195 		gvsid |= VSID_PR;
196 
197 	/* We might get collisions that trap in preceding order, so let's
198 	   map them differently */
199 
200 	sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
201 	if (backwards_map)
202 		sid_map_mask = SID_MAP_MASK - sid_map_mask;
203 
204 	map = &to_book3s(vcpu)->sid_map[sid_map_mask];
205 
206 	/* Make sure we're taking the other map next time */
207 	backwards_map = !backwards_map;
208 
209 	/* Uh-oh ... out of mappings. Let's flush! */
210 	if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
211 		vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
212 		memset(vcpu_book3s->sid_map, 0,
213 		       sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
214 		kvmppc_mmu_pte_flush(vcpu, 0, 0);
215 		kvmppc_mmu_flush_segments(vcpu);
216 	}
217 	map->host_vsid = vcpu_book3s->vsid_next++;
218 
219 	map->guest_vsid = gvsid;
220 	map->valid = true;
221 
222 	dprintk_slb("SLB: New mapping at %d: 0x%llx -> 0x%llx\n",
223 		    sid_map_mask, gvsid, map->host_vsid);
224 
225 	return map;
226 }
227 
228 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
229 {
230 	int i;
231 	int max_slb_size = 64;
232 	int found_inval = -1;
233 	int r;
234 
235 	if (!to_svcpu(vcpu)->slb_max)
236 		to_svcpu(vcpu)->slb_max = 1;
237 
238 	/* Are we overwriting? */
239 	for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
240 		if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
241 			found_inval = i;
242 		else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
243 			return i;
244 	}
245 
246 	/* Found a spare entry that was invalidated before */
247 	if (found_inval > 0)
248 		return found_inval;
249 
250 	/* No spare invalid entry, so create one */
251 
252 	if (mmu_slb_size < 64)
253 		max_slb_size = mmu_slb_size;
254 
255 	/* Overflowing -> purge */
256 	if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
257 		kvmppc_mmu_flush_segments(vcpu);
258 
259 	r = to_svcpu(vcpu)->slb_max;
260 	to_svcpu(vcpu)->slb_max++;
261 
262 	return r;
263 }
264 
265 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
266 {
267 	u64 esid = eaddr >> SID_SHIFT;
268 	u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
269 	u64 slb_vsid = SLB_VSID_USER;
270 	u64 gvsid;
271 	int slb_index;
272 	struct kvmppc_sid_map *map;
273 
274 	slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
275 
276 	if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
277 		/* Invalidate an entry */
278 		to_svcpu(vcpu)->slb[slb_index].esid = 0;
279 		return -ENOENT;
280 	}
281 
282 	map = find_sid_vsid(vcpu, gvsid);
283 	if (!map)
284 		map = create_sid_map(vcpu, gvsid);
285 
286 	map->guest_esid = esid;
287 
288 	slb_vsid |= (map->host_vsid << 12);
289 	slb_vsid &= ~SLB_VSID_KP;
290 	slb_esid |= slb_index;
291 
292 	to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
293 	to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
294 
295 	dprintk_slb("slbmte %#llx, %#llx\n", slb_vsid, slb_esid);
296 
297 	return 0;
298 }
299 
300 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
301 {
302 	to_svcpu(vcpu)->slb_max = 1;
303 	to_svcpu(vcpu)->slb[0].esid = 0;
304 }
305 
306 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
307 {
308 	kvmppc_mmu_hpte_destroy(vcpu);
309 	__destroy_context(to_book3s(vcpu)->context_id);
310 }
311 
312 int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
313 {
314 	struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
315 	int err;
316 
317 	err = __init_new_context();
318 	if (err < 0)
319 		return -1;
320 	vcpu3s->context_id = err;
321 
322 	vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1;
323 	vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS;
324 	vcpu3s->vsid_next = vcpu3s->vsid_first;
325 
326 	kvmppc_mmu_hpte_init(vcpu);
327 
328 	return 0;
329 }
330