xref: /openbmc/linux/arch/powerpc/kvm/book3s_32_mmu.c (revision 1fa6ac37)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14  *
15  * Copyright SUSE Linux Products GmbH 2009
16  *
17  * Authors: Alexander Graf <agraf@suse.de>
18  */
19 
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/highmem.h>
25 
26 #include <asm/tlbflush.h>
27 #include <asm/kvm_ppc.h>
28 #include <asm/kvm_book3s.h>
29 
30 /* #define DEBUG_MMU */
31 /* #define DEBUG_MMU_PTE */
32 /* #define DEBUG_MMU_PTE_IP 0xfff14c40 */
33 
34 #ifdef DEBUG_MMU
35 #define dprintk(X...) printk(KERN_INFO X)
36 #else
37 #define dprintk(X...) do { } while(0)
38 #endif
39 
40 #ifdef DEBUG_MMU_PTE
41 #define dprintk_pte(X...) printk(KERN_INFO X)
42 #else
43 #define dprintk_pte(X...) do { } while(0)
44 #endif
45 
46 #define PTEG_FLAG_ACCESSED	0x00000100
47 #define PTEG_FLAG_DIRTY		0x00000080
48 #ifndef SID_SHIFT
49 #define SID_SHIFT		28
50 #endif
51 
52 static inline bool check_debug_ip(struct kvm_vcpu *vcpu)
53 {
54 #ifdef DEBUG_MMU_PTE_IP
55 	return vcpu->arch.pc == DEBUG_MMU_PTE_IP;
56 #else
57 	return true;
58 #endif
59 }
60 
61 static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
62 					  struct kvmppc_pte *pte, bool data);
63 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
64 					     u64 *vsid);
65 
66 static struct kvmppc_sr *find_sr(struct kvmppc_vcpu_book3s *vcpu_book3s, gva_t eaddr)
67 {
68 	return &vcpu_book3s->sr[(eaddr >> 28) & 0xf];
69 }
70 
71 static u64 kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu *vcpu, gva_t eaddr,
72 					 bool data)
73 {
74 	u64 vsid;
75 	struct kvmppc_pte pte;
76 
77 	if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &pte, data))
78 		return pte.vpage;
79 
80 	kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
81 	return (((u64)eaddr >> 12) & 0xffff) | (vsid << 16);
82 }
83 
84 static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu *vcpu)
85 {
86 	kvmppc_set_msr(vcpu, 0);
87 }
88 
89 static hva_t kvmppc_mmu_book3s_32_get_pteg(struct kvmppc_vcpu_book3s *vcpu_book3s,
90 				      struct kvmppc_sr *sre, gva_t eaddr,
91 				      bool primary)
92 {
93 	u32 page, hash, pteg, htabmask;
94 	hva_t r;
95 
96 	page = (eaddr & 0x0FFFFFFF) >> 12;
97 	htabmask = ((vcpu_book3s->sdr1 & 0x1FF) << 16) | 0xFFC0;
98 
99 	hash = ((sre->vsid ^ page) << 6);
100 	if (!primary)
101 		hash = ~hash;
102 	hash &= htabmask;
103 
104 	pteg = (vcpu_book3s->sdr1 & 0xffff0000) | hash;
105 
106 	dprintk("MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
107 		vcpu_book3s->vcpu.arch.pc, eaddr, vcpu_book3s->sdr1, pteg,
108 		sre->vsid);
109 
110 	r = gfn_to_hva(vcpu_book3s->vcpu.kvm, pteg >> PAGE_SHIFT);
111 	if (kvm_is_error_hva(r))
112 		return r;
113 	return r | (pteg & ~PAGE_MASK);
114 }
115 
116 static u32 kvmppc_mmu_book3s_32_get_ptem(struct kvmppc_sr *sre, gva_t eaddr,
117 				    bool primary)
118 {
119 	return ((eaddr & 0x0fffffff) >> 22) | (sre->vsid << 7) |
120 	       (primary ? 0 : 0x40) | 0x80000000;
121 }
122 
123 static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu *vcpu, gva_t eaddr,
124 					  struct kvmppc_pte *pte, bool data)
125 {
126 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
127 	struct kvmppc_bat *bat;
128 	int i;
129 
130 	for (i = 0; i < 8; i++) {
131 		if (data)
132 			bat = &vcpu_book3s->dbat[i];
133 		else
134 			bat = &vcpu_book3s->ibat[i];
135 
136 		if (vcpu->arch.msr & MSR_PR) {
137 			if (!bat->vp)
138 				continue;
139 		} else {
140 			if (!bat->vs)
141 				continue;
142 		}
143 
144 		if (check_debug_ip(vcpu))
145 		{
146 			dprintk_pte("%cBAT %02d: 0x%lx - 0x%x (0x%x)\n",
147 				    data ? 'd' : 'i', i, eaddr, bat->bepi,
148 				    bat->bepi_mask);
149 		}
150 		if ((eaddr & bat->bepi_mask) == bat->bepi) {
151 			u64 vsid;
152 			kvmppc_mmu_book3s_32_esid_to_vsid(vcpu,
153 				eaddr >> SID_SHIFT, &vsid);
154 			vsid <<= 16;
155 			pte->vpage = (((u64)eaddr >> 12) & 0xffff) | vsid;
156 
157 			pte->raddr = bat->brpn | (eaddr & ~bat->bepi_mask);
158 			pte->may_read = bat->pp;
159 			pte->may_write = bat->pp > 1;
160 			pte->may_execute = true;
161 			if (!pte->may_read) {
162 				printk(KERN_INFO "BAT is not readable!\n");
163 				continue;
164 			}
165 			if (!pte->may_write) {
166 				/* let's treat r/o BATs as not-readable for now */
167 				dprintk_pte("BAT is read-only!\n");
168 				continue;
169 			}
170 
171 			return 0;
172 		}
173 	}
174 
175 	return -ENOENT;
176 }
177 
178 static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu *vcpu, gva_t eaddr,
179 				     struct kvmppc_pte *pte, bool data,
180 				     bool primary)
181 {
182 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
183 	struct kvmppc_sr *sre;
184 	hva_t ptegp;
185 	u32 pteg[16];
186 	u32 ptem = 0;
187 	int i;
188 	int found = 0;
189 
190 	sre = find_sr(vcpu_book3s, eaddr);
191 
192 	dprintk_pte("SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr >> 28,
193 		    sre->vsid, sre->raw);
194 
195 	pte->vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
196 
197 	ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu_book3s, sre, eaddr, primary);
198 	if (kvm_is_error_hva(ptegp)) {
199 		printk(KERN_INFO "KVM: Invalid PTEG!\n");
200 		goto no_page_found;
201 	}
202 
203 	ptem = kvmppc_mmu_book3s_32_get_ptem(sre, eaddr, primary);
204 
205 	if(copy_from_user(pteg, (void __user *)ptegp, sizeof(pteg))) {
206 		printk(KERN_ERR "KVM: Can't copy data from 0x%lx!\n", ptegp);
207 		goto no_page_found;
208 	}
209 
210 	for (i=0; i<16; i+=2) {
211 		if (ptem == pteg[i]) {
212 			u8 pp;
213 
214 			pte->raddr = (pteg[i+1] & ~(0xFFFULL)) | (eaddr & 0xFFF);
215 			pp = pteg[i+1] & 3;
216 
217 			if ((sre->Kp &&  (vcpu->arch.msr & MSR_PR)) ||
218 			    (sre->Ks && !(vcpu->arch.msr & MSR_PR)))
219 				pp |= 4;
220 
221 			pte->may_write = false;
222 			pte->may_read = false;
223 			pte->may_execute = true;
224 			switch (pp) {
225 				case 0:
226 				case 1:
227 				case 2:
228 				case 6:
229 					pte->may_write = true;
230 				case 3:
231 				case 5:
232 				case 7:
233 					pte->may_read = true;
234 					break;
235 			}
236 
237 			if ( !pte->may_read )
238 				continue;
239 
240 			dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
241 				    pteg[i], pteg[i+1], pp);
242 			found = 1;
243 			break;
244 		}
245 	}
246 
247 	/* Update PTE C and A bits, so the guest's swapper knows we used the
248 	   page */
249 	if (found) {
250 		u32 oldpte = pteg[i+1];
251 
252 		if (pte->may_read)
253 			pteg[i+1] |= PTEG_FLAG_ACCESSED;
254 		if (pte->may_write)
255 			pteg[i+1] |= PTEG_FLAG_DIRTY;
256 		else
257 			dprintk_pte("KVM: Mapping read-only page!\n");
258 
259 		/* Write back into the PTEG */
260 		if (pteg[i+1] != oldpte)
261 			copy_to_user((void __user *)ptegp, pteg, sizeof(pteg));
262 
263 		return 0;
264 	}
265 
266 no_page_found:
267 
268 	if (check_debug_ip(vcpu)) {
269 		dprintk_pte("KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
270 			    to_book3s(vcpu)->sdr1, ptegp);
271 		for (i=0; i<16; i+=2) {
272 			dprintk_pte("   %02d: 0x%x - 0x%x (0x%llx)\n",
273 				    i, pteg[i], pteg[i+1], ptem);
274 		}
275 	}
276 
277 	return -ENOENT;
278 }
279 
280 static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
281 				      struct kvmppc_pte *pte, bool data)
282 {
283 	int r;
284 
285 	pte->eaddr = eaddr;
286 	r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data);
287 	if (r < 0)
288 	       r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, true);
289 	if (r < 0)
290 	       r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data, false);
291 
292 	return r;
293 }
294 
295 
296 static u32 kvmppc_mmu_book3s_32_mfsrin(struct kvm_vcpu *vcpu, u32 srnum)
297 {
298 	return to_book3s(vcpu)->sr[srnum].raw;
299 }
300 
301 static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
302 					ulong value)
303 {
304 	struct kvmppc_sr *sre;
305 
306 	sre = &to_book3s(vcpu)->sr[srnum];
307 
308 	/* Flush any left-over shadows from the previous SR */
309 
310 	/* XXX Not necessary? */
311 	/* kvmppc_mmu_pte_flush(vcpu, ((u64)sre->vsid) << 28, 0xf0000000ULL); */
312 
313 	/* And then put in the new SR */
314 	sre->raw = value;
315 	sre->vsid = (value & 0x0fffffff);
316 	sre->valid = (value & 0x80000000) ? false : true;
317 	sre->Ks = (value & 0x40000000) ? true : false;
318 	sre->Kp = (value & 0x20000000) ? true : false;
319 	sre->nx = (value & 0x10000000) ? true : false;
320 
321 	/* Map the new segment */
322 	kvmppc_mmu_map_segment(vcpu, srnum << SID_SHIFT);
323 }
324 
325 static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu *vcpu, ulong ea, bool large)
326 {
327 	kvmppc_mmu_pte_flush(vcpu, ea, 0x0FFFF000);
328 }
329 
330 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
331 					     u64 *vsid)
332 {
333 	ulong ea = esid << SID_SHIFT;
334 	struct kvmppc_sr *sr;
335 	u64 gvsid = esid;
336 
337 	if (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
338 		sr = find_sr(to_book3s(vcpu), ea);
339 		if (sr->valid)
340 			gvsid = sr->vsid;
341 	}
342 
343 	/* In case we only have one of MSR_IR or MSR_DR set, let's put
344 	   that in the real-mode context (and hope RM doesn't access
345 	   high memory) */
346 	switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
347 	case 0:
348 		*vsid = VSID_REAL | esid;
349 		break;
350 	case MSR_IR:
351 		*vsid = VSID_REAL_IR | gvsid;
352 		break;
353 	case MSR_DR:
354 		*vsid = VSID_REAL_DR | gvsid;
355 		break;
356 	case MSR_DR|MSR_IR:
357 		if (!sr->valid)
358 			return -1;
359 
360 		*vsid = sr->vsid;
361 		break;
362 	default:
363 		BUG();
364 	}
365 
366 	if (vcpu->arch.msr & MSR_PR)
367 		*vsid |= VSID_PR;
368 
369 	return 0;
370 }
371 
372 static bool kvmppc_mmu_book3s_32_is_dcbz32(struct kvm_vcpu *vcpu)
373 {
374 	return true;
375 }
376 
377 
378 void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu)
379 {
380 	struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
381 
382 	mmu->mtsrin = kvmppc_mmu_book3s_32_mtsrin;
383 	mmu->mfsrin = kvmppc_mmu_book3s_32_mfsrin;
384 	mmu->xlate = kvmppc_mmu_book3s_32_xlate;
385 	mmu->reset_msr = kvmppc_mmu_book3s_32_reset_msr;
386 	mmu->tlbie = kvmppc_mmu_book3s_32_tlbie;
387 	mmu->esid_to_vsid = kvmppc_mmu_book3s_32_esid_to_vsid;
388 	mmu->ea_to_vp = kvmppc_mmu_book3s_32_ea_to_vp;
389 	mmu->is_dcbz32 = kvmppc_mmu_book3s_32_is_dcbz32;
390 
391 	mmu->slbmte = NULL;
392 	mmu->slbmfee = NULL;
393 	mmu->slbmfev = NULL;
394 	mmu->slbie = NULL;
395 	mmu->slbia = NULL;
396 }
397