• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IBM Corp. 2007
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
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/mmu-44x.h>
28 #include <asm/kvm_ppc.h>
29 #include <asm/kvm_44x.h>
30 #include "timing.h"
31 
32 #include "44x_tlb.h"
33 
34 #ifndef PPC44x_TLBE_SIZE
35 #define PPC44x_TLBE_SIZE	PPC44x_TLB_4K
36 #endif
37 
38 #define PAGE_SIZE_4K (1<<12)
39 #define PAGE_MASK_4K (~(PAGE_SIZE_4K - 1))
40 
41 #define PPC44x_TLB_UATTR_MASK \
42 	(PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3)
43 #define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
44 #define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)
45 
46 #ifdef DEBUG
kvmppc_dump_tlbs(struct kvm_vcpu * vcpu)47 void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
48 {
49 	struct kvmppc_44x_tlbe *tlbe;
50 	int i;
51 
52 	printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
53 	printk("| %2s | %3s | %8s | %8s | %8s |\n",
54 			"nr", "tid", "word0", "word1", "word2");
55 
56 	for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
57 		tlbe = &vcpu_44x->guest_tlb[i];
58 		if (tlbe->word0 & PPC44x_TLB_VALID)
59 			printk(" G%2d |  %02X | %08X | %08X | %08X |\n",
60 			       i, tlbe->tid, tlbe->word0, tlbe->word1,
61 			       tlbe->word2);
62 	}
63 }
64 #endif
65 
kvmppc_44x_tlbie(unsigned int index)66 static inline void kvmppc_44x_tlbie(unsigned int index)
67 {
68 	/* 0 <= index < 64, so the V bit is clear and we can use the index as
69 	 * word0. */
70 	asm volatile(
71 		"tlbwe %[index], %[index], 0\n"
72 	:
73 	: [index] "r"(index)
74 	);
75 }
76 
kvmppc_44x_tlbre(unsigned int index,struct kvmppc_44x_tlbe * tlbe)77 static inline void kvmppc_44x_tlbre(unsigned int index,
78                                     struct kvmppc_44x_tlbe *tlbe)
79 {
80 	asm volatile(
81 		"tlbre %[word0], %[index], 0\n"
82 		"mfspr %[tid], %[sprn_mmucr]\n"
83 		"andi. %[tid], %[tid], 0xff\n"
84 		"tlbre %[word1], %[index], 1\n"
85 		"tlbre %[word2], %[index], 2\n"
86 		: [word0] "=r"(tlbe->word0),
87 		  [word1] "=r"(tlbe->word1),
88 		  [word2] "=r"(tlbe->word2),
89 		  [tid]   "=r"(tlbe->tid)
90 		: [index] "r"(index),
91 		  [sprn_mmucr] "i"(SPRN_MMUCR)
92 		: "cc"
93 	);
94 }
95 
kvmppc_44x_tlbwe(unsigned int index,struct kvmppc_44x_tlbe * stlbe)96 static inline void kvmppc_44x_tlbwe(unsigned int index,
97                                     struct kvmppc_44x_tlbe *stlbe)
98 {
99 	unsigned long tmp;
100 
101 	asm volatile(
102 		"mfspr %[tmp], %[sprn_mmucr]\n"
103 		"rlwimi %[tmp], %[tid], 0, 0xff\n"
104 		"mtspr %[sprn_mmucr], %[tmp]\n"
105 		"tlbwe %[word0], %[index], 0\n"
106 		"tlbwe %[word1], %[index], 1\n"
107 		"tlbwe %[word2], %[index], 2\n"
108 		: [tmp]   "=&r"(tmp)
109 		: [word0] "r"(stlbe->word0),
110 		  [word1] "r"(stlbe->word1),
111 		  [word2] "r"(stlbe->word2),
112 		  [tid]   "r"(stlbe->tid),
113 		  [index] "r"(index),
114 		  [sprn_mmucr] "i"(SPRN_MMUCR)
115 	);
116 }
117 
kvmppc_44x_tlb_shadow_attrib(u32 attrib,int usermode)118 static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
119 {
120 	/* We only care about the guest's permission and user bits. */
121 	attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK;
122 
123 	if (!usermode) {
124 		/* Guest is in supervisor mode, so we need to translate guest
125 		 * supervisor permissions into user permissions. */
126 		attrib &= ~PPC44x_TLB_USER_PERM_MASK;
127 		attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
128 	}
129 
130 	/* Make sure host can always access this memory. */
131 	attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;
132 
133 	/* WIMGE = 0b00100 */
134 	attrib |= PPC44x_TLB_M;
135 
136 	return attrib;
137 }
138 
139 /* Load shadow TLB back into hardware. */
kvmppc_44x_tlb_load(struct kvm_vcpu * vcpu)140 void kvmppc_44x_tlb_load(struct kvm_vcpu *vcpu)
141 {
142 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
143 	int i;
144 
145 	for (i = 0; i <= tlb_44x_hwater; i++) {
146 		struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
147 
148 		if (get_tlb_v(stlbe) && get_tlb_ts(stlbe))
149 			kvmppc_44x_tlbwe(i, stlbe);
150 	}
151 }
152 
kvmppc_44x_tlbe_set_modified(struct kvmppc_vcpu_44x * vcpu_44x,unsigned int i)153 static void kvmppc_44x_tlbe_set_modified(struct kvmppc_vcpu_44x *vcpu_44x,
154                                          unsigned int i)
155 {
156 	vcpu_44x->shadow_tlb_mod[i] = 1;
157 }
158 
159 /* Save hardware TLB to the vcpu, and invalidate all guest mappings. */
kvmppc_44x_tlb_put(struct kvm_vcpu * vcpu)160 void kvmppc_44x_tlb_put(struct kvm_vcpu *vcpu)
161 {
162 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
163 	int i;
164 
165 	for (i = 0; i <= tlb_44x_hwater; i++) {
166 		struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i];
167 
168 		if (vcpu_44x->shadow_tlb_mod[i])
169 			kvmppc_44x_tlbre(i, stlbe);
170 
171 		if (get_tlb_v(stlbe) && get_tlb_ts(stlbe))
172 			kvmppc_44x_tlbie(i);
173 	}
174 }
175 
176 
177 /* Search the guest TLB for a matching entry. */
kvmppc_44x_tlb_index(struct kvm_vcpu * vcpu,gva_t eaddr,unsigned int pid,unsigned int as)178 int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
179                          unsigned int as)
180 {
181 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
182 	int i;
183 
184 	/* XXX Replace loop with fancy data structures. */
185 	for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
186 		struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i];
187 		unsigned int tid;
188 
189 		if (eaddr < get_tlb_eaddr(tlbe))
190 			continue;
191 
192 		if (eaddr > get_tlb_end(tlbe))
193 			continue;
194 
195 		tid = get_tlb_tid(tlbe);
196 		if (tid && (tid != pid))
197 			continue;
198 
199 		if (!get_tlb_v(tlbe))
200 			continue;
201 
202 		if (get_tlb_ts(tlbe) != as)
203 			continue;
204 
205 		return i;
206 	}
207 
208 	return -1;
209 }
210 
kvmppc_44x_itlb_index(struct kvm_vcpu * vcpu,gva_t eaddr)211 int kvmppc_44x_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
212 {
213 	unsigned int as = !!(vcpu->arch.msr & MSR_IS);
214 
215 	return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
216 }
217 
kvmppc_44x_dtlb_index(struct kvm_vcpu * vcpu,gva_t eaddr)218 int kvmppc_44x_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
219 {
220 	unsigned int as = !!(vcpu->arch.msr & MSR_DS);
221 
222 	return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
223 }
224 
kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x * vcpu_44x,unsigned int stlb_index)225 static void kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x *vcpu_44x,
226                                       unsigned int stlb_index)
227 {
228 	struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[stlb_index];
229 
230 	if (!ref->page)
231 		return;
232 
233 	/* Discard from the TLB. */
234 	/* Note: we could actually invalidate a host mapping, if the host overwrote
235 	 * this TLB entry since we inserted a guest mapping. */
236 	kvmppc_44x_tlbie(stlb_index);
237 
238 	/* Now release the page. */
239 	if (ref->writeable)
240 		kvm_release_page_dirty(ref->page);
241 	else
242 		kvm_release_page_clean(ref->page);
243 
244 	ref->page = NULL;
245 
246 	/* XXX set tlb_44x_index to stlb_index? */
247 
248 	KVMTRACE_1D(STLB_INVAL, &vcpu_44x->vcpu, stlb_index, handler);
249 }
250 
kvmppc_core_destroy_mmu(struct kvm_vcpu * vcpu)251 void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
252 {
253 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
254 	int i;
255 
256 	for (i = 0; i <= tlb_44x_hwater; i++)
257 		kvmppc_44x_shadow_release(vcpu_44x, i);
258 }
259 
260 /**
261  * kvmppc_mmu_map -- create a host mapping for guest memory
262  *
263  * If the guest wanted a larger page than the host supports, only the first
264  * host page is mapped here and the rest are demand faulted.
265  *
266  * If the guest wanted a smaller page than the host page size, we map only the
267  * guest-size page (i.e. not a full host page mapping).
268  *
269  * Caller must ensure that the specified guest TLB entry is safe to insert into
270  * the shadow TLB.
271  */
kvmppc_mmu_map(struct kvm_vcpu * vcpu,u64 gvaddr,gpa_t gpaddr,u64 asid,u32 flags,u32 max_bytes,unsigned int gtlb_index)272 void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr, u64 asid,
273                     u32 flags, u32 max_bytes, unsigned int gtlb_index)
274 {
275 	struct kvmppc_44x_tlbe stlbe;
276 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
277 	struct kvmppc_44x_shadow_ref *ref;
278 	struct page *new_page;
279 	hpa_t hpaddr;
280 	gfn_t gfn;
281 	unsigned int victim;
282 
283 	/* Select TLB entry to clobber. Indirectly guard against races with the TLB
284 	 * miss handler by disabling interrupts. */
285 	local_irq_disable();
286 	victim = ++tlb_44x_index;
287 	if (victim > tlb_44x_hwater)
288 		victim = 0;
289 	tlb_44x_index = victim;
290 	local_irq_enable();
291 
292 	/* Get reference to new page. */
293 	gfn = gpaddr >> PAGE_SHIFT;
294 	new_page = gfn_to_page(vcpu->kvm, gfn);
295 	if (is_error_page(new_page)) {
296 		printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
297 		kvm_release_page_clean(new_page);
298 		return;
299 	}
300 	hpaddr = page_to_phys(new_page);
301 
302 	/* Invalidate any previous shadow mappings. */
303 	kvmppc_44x_shadow_release(vcpu_44x, victim);
304 
305 	/* XXX Make sure (va, size) doesn't overlap any other
306 	 * entries. 440x6 user manual says the result would be
307 	 * "undefined." */
308 
309 	/* XXX what about AS? */
310 
311 	/* Force TS=1 for all guest mappings. */
312 	stlbe.word0 = PPC44x_TLB_VALID | PPC44x_TLB_TS;
313 
314 	if (max_bytes >= PAGE_SIZE) {
315 		/* Guest mapping is larger than or equal to host page size. We can use
316 		 * a "native" host mapping. */
317 		stlbe.word0 |= (gvaddr & PAGE_MASK) | PPC44x_TLBE_SIZE;
318 	} else {
319 		/* Guest mapping is smaller than host page size. We must restrict the
320 		 * size of the mapping to be at most the smaller of the two, but for
321 		 * simplicity we fall back to a 4K mapping (this is probably what the
322 		 * guest is using anyways). */
323 		stlbe.word0 |= (gvaddr & PAGE_MASK_4K) | PPC44x_TLB_4K;
324 
325 		/* 'hpaddr' is a host page, which is larger than the mapping we're
326 		 * inserting here. To compensate, we must add the in-page offset to the
327 		 * sub-page. */
328 		hpaddr |= gpaddr & (PAGE_MASK ^ PAGE_MASK_4K);
329 	}
330 
331 	stlbe.word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
332 	stlbe.word2 = kvmppc_44x_tlb_shadow_attrib(flags,
333 	                                            vcpu->arch.msr & MSR_PR);
334 	stlbe.tid = !(asid & 0xff);
335 
336 	/* Keep track of the reference so we can properly release it later. */
337 	ref = &vcpu_44x->shadow_refs[victim];
338 	ref->page = new_page;
339 	ref->gtlb_index = gtlb_index;
340 	ref->writeable = !!(stlbe.word2 & PPC44x_TLB_UW);
341 	ref->tid = stlbe.tid;
342 
343 	/* Insert shadow mapping into hardware TLB. */
344 	kvmppc_44x_tlbe_set_modified(vcpu_44x, victim);
345 	kvmppc_44x_tlbwe(victim, &stlbe);
346 	KVMTRACE_5D(STLB_WRITE, vcpu, victim, stlbe.tid, stlbe.word0, stlbe.word1,
347 	            stlbe.word2, handler);
348 }
349 
350 /* For a particular guest TLB entry, invalidate the corresponding host TLB
351  * mappings and release the host pages. */
kvmppc_44x_invalidate(struct kvm_vcpu * vcpu,unsigned int gtlb_index)352 static void kvmppc_44x_invalidate(struct kvm_vcpu *vcpu,
353                                   unsigned int gtlb_index)
354 {
355 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
356 	int i;
357 
358 	for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
359 		struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
360 		if (ref->gtlb_index == gtlb_index)
361 			kvmppc_44x_shadow_release(vcpu_44x, i);
362 	}
363 }
364 
kvmppc_mmu_priv_switch(struct kvm_vcpu * vcpu,int usermode)365 void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
366 {
367 	vcpu->arch.shadow_pid = !usermode;
368 }
369 
kvmppc_set_pid(struct kvm_vcpu * vcpu,u32 new_pid)370 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid)
371 {
372 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
373 	int i;
374 
375 	if (unlikely(vcpu->arch.pid == new_pid))
376 		return;
377 
378 	vcpu->arch.pid = new_pid;
379 
380 	/* Guest userspace runs with TID=0 mappings and PID=0, to make sure it
381 	 * can't access guest kernel mappings (TID=1). When we switch to a new
382 	 * guest PID, which will also use host PID=0, we must discard the old guest
383 	 * userspace mappings. */
384 	for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
385 		struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
386 
387 		if (ref->tid == 0)
388 			kvmppc_44x_shadow_release(vcpu_44x, i);
389 	}
390 }
391 
tlbe_is_host_safe(const struct kvm_vcpu * vcpu,const struct kvmppc_44x_tlbe * tlbe)392 static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
393                              const struct kvmppc_44x_tlbe *tlbe)
394 {
395 	gpa_t gpa;
396 
397 	if (!get_tlb_v(tlbe))
398 		return 0;
399 
400 	/* Does it match current guest AS? */
401 	/* XXX what about IS != DS? */
402 	if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
403 		return 0;
404 
405 	gpa = get_tlb_raddr(tlbe);
406 	if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
407 		/* Mapping is not for RAM. */
408 		return 0;
409 
410 	return 1;
411 }
412 
kvmppc_44x_emul_tlbwe(struct kvm_vcpu * vcpu,u8 ra,u8 rs,u8 ws)413 int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
414 {
415 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
416 	struct kvmppc_44x_tlbe *tlbe;
417 	unsigned int gtlb_index;
418 
419 	gtlb_index = vcpu->arch.gpr[ra];
420 	if (gtlb_index > KVM44x_GUEST_TLB_SIZE) {
421 		printk("%s: index %d\n", __func__, gtlb_index);
422 		kvmppc_dump_vcpu(vcpu);
423 		return EMULATE_FAIL;
424 	}
425 
426 	tlbe = &vcpu_44x->guest_tlb[gtlb_index];
427 
428 	/* Invalidate shadow mappings for the about-to-be-clobbered TLB entry. */
429 	if (tlbe->word0 & PPC44x_TLB_VALID)
430 		kvmppc_44x_invalidate(vcpu, gtlb_index);
431 
432 	switch (ws) {
433 	case PPC44x_TLB_PAGEID:
434 		tlbe->tid = get_mmucr_stid(vcpu);
435 		tlbe->word0 = vcpu->arch.gpr[rs];
436 		break;
437 
438 	case PPC44x_TLB_XLAT:
439 		tlbe->word1 = vcpu->arch.gpr[rs];
440 		break;
441 
442 	case PPC44x_TLB_ATTRIB:
443 		tlbe->word2 = vcpu->arch.gpr[rs];
444 		break;
445 
446 	default:
447 		return EMULATE_FAIL;
448 	}
449 
450 	if (tlbe_is_host_safe(vcpu, tlbe)) {
451 		u64 asid;
452 		gva_t eaddr;
453 		gpa_t gpaddr;
454 		u32 flags;
455 		u32 bytes;
456 
457 		eaddr = get_tlb_eaddr(tlbe);
458 		gpaddr = get_tlb_raddr(tlbe);
459 
460 		/* Use the advertised page size to mask effective and real addrs. */
461 		bytes = get_tlb_bytes(tlbe);
462 		eaddr &= ~(bytes - 1);
463 		gpaddr &= ~(bytes - 1);
464 
465 		asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
466 		flags = tlbe->word2 & 0xffff;
467 
468 		kvmppc_mmu_map(vcpu, eaddr, gpaddr, asid, flags, bytes, gtlb_index);
469 	}
470 
471 	KVMTRACE_5D(GTLB_WRITE, vcpu, gtlb_index, tlbe->tid, tlbe->word0,
472 	            tlbe->word1, tlbe->word2, handler);
473 
474 	kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
475 	return EMULATE_DONE;
476 }
477 
kvmppc_44x_emul_tlbsx(struct kvm_vcpu * vcpu,u8 rt,u8 ra,u8 rb,u8 rc)478 int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
479 {
480 	u32 ea;
481 	int gtlb_index;
482 	unsigned int as = get_mmucr_sts(vcpu);
483 	unsigned int pid = get_mmucr_stid(vcpu);
484 
485 	ea = vcpu->arch.gpr[rb];
486 	if (ra)
487 		ea += vcpu->arch.gpr[ra];
488 
489 	gtlb_index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
490 	if (rc) {
491 		if (gtlb_index < 0)
492 			vcpu->arch.cr &= ~0x20000000;
493 		else
494 			vcpu->arch.cr |= 0x20000000;
495 	}
496 	vcpu->arch.gpr[rt] = gtlb_index;
497 
498 	kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
499 	return EMULATE_DONE;
500 }
501