44x_tlb.c 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright IBM Corp. 2007
 *
 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
 */

#include <linux/types.h>
#include <linux/string.h>
22
#include <linux/kvm.h>
23 24
#include <linux/kvm_host.h>
#include <linux/highmem.h>
25 26

#include <asm/tlbflush.h>
27 28
#include <asm/mmu-44x.h>
#include <asm/kvm_ppc.h>
29
#include <asm/kvm_44x.h>
30 31 32

#include "44x_tlb.h"

33 34 35 36 37 38 39
#ifndef PPC44x_TLBE_SIZE
#define PPC44x_TLBE_SIZE	PPC44x_TLB_4K
#endif

#define PAGE_SIZE_4K (1<<12)
#define PAGE_MASK_4K (~(PAGE_SIZE_4K - 1))

40 41
#define PPC44x_TLB_UATTR_MASK \
	(PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3)
42 43 44
#define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW)
#define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW)

45 46 47 48 49 50 51 52 53 54
#ifdef DEBUG
void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
{
	struct kvmppc_44x_tlbe *tlbe;
	int i;

	printk("vcpu %d TLB dump:\n", vcpu->vcpu_id);
	printk("| %2s | %3s | %8s | %8s | %8s |\n",
			"nr", "tid", "word0", "word1", "word2");

55
	for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
56
		tlbe = &vcpu_44x->guest_tlb[i];
57 58 59 60 61 62 63 64
		if (tlbe->word0 & PPC44x_TLB_VALID)
			printk(" G%2d |  %02X | %08X | %08X | %08X |\n",
			       i, tlbe->tid, tlbe->word0, tlbe->word1,
			       tlbe->word2);
	}
}
#endif

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static inline void kvmppc_44x_tlbie(unsigned int index)
{
	/* 0 <= index < 64, so the V bit is clear and we can use the index as
	 * word0. */
	asm volatile(
		"tlbwe %[index], %[index], 0\n"
	:
	: [index] "r"(index)
	);
}

static inline void kvmppc_44x_tlbwe(unsigned int index,
                                    struct kvmppc_44x_tlbe *stlbe)
{
	unsigned long tmp;

	asm volatile(
		"mfspr %[tmp], %[sprn_mmucr]\n"
		"rlwimi %[tmp], %[tid], 0, 0xff\n"
		"mtspr %[sprn_mmucr], %[tmp]\n"
		"tlbwe %[word0], %[index], 0\n"
		"tlbwe %[word1], %[index], 1\n"
		"tlbwe %[word2], %[index], 2\n"
		: [tmp]   "=&r"(tmp)
		: [word0] "r"(stlbe->word0),
		  [word1] "r"(stlbe->word1),
		  [word2] "r"(stlbe->word2),
		  [tid]   "r"(stlbe->tid),
		  [index] "r"(index),
		  [sprn_mmucr] "i"(SPRN_MMUCR)
	);
}

98 99
static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode)
{
100 101
	/* We only care about the guest's permission and user bits. */
	attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK;
102 103 104 105 106 107 108 109 110 111 112

	if (!usermode) {
		/* Guest is in supervisor mode, so we need to translate guest
		 * supervisor permissions into user permissions. */
		attrib &= ~PPC44x_TLB_USER_PERM_MASK;
		attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3;
	}

	/* Make sure host can always access this memory. */
	attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW;

113 114 115
	/* WIMGE = 0b00100 */
	attrib |= PPC44x_TLB_M;

116 117 118 119 120 121 122
	return attrib;
}

/* Search the guest TLB for a matching entry. */
int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid,
                         unsigned int as)
{
123
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
124 125 126
	int i;

	/* XXX Replace loop with fancy data structures. */
127
	for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) {
128
		struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i];
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		unsigned int tid;

		if (eaddr < get_tlb_eaddr(tlbe))
			continue;

		if (eaddr > get_tlb_end(tlbe))
			continue;

		tid = get_tlb_tid(tlbe);
		if (tid && (tid != pid))
			continue;

		if (!get_tlb_v(tlbe))
			continue;

		if (get_tlb_ts(tlbe) != as)
			continue;

		return i;
	}

	return -1;
}

153
int kvmppc_44x_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
154 155 156
{
	unsigned int as = !!(vcpu->arch.msr & MSR_IS);

157
	return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
158 159
}

160
int kvmppc_44x_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
161 162 163
{
	unsigned int as = !!(vcpu->arch.msr & MSR_DS);

164
	return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
165 166
}

167 168
static void kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x *vcpu_44x,
                                      unsigned int stlb_index)
169
{
170
	struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[stlb_index];
171

172 173
	if (!ref->page)
		return;
174

175 176 177 178
	/* Discard from the TLB. */
	/* Note: we could actually invalidate a host mapping, if the host overwrote
	 * this TLB entry since we inserted a guest mapping. */
	kvmppc_44x_tlbie(stlb_index);
179

180 181 182 183 184
	/* Now release the page. */
	if (ref->writeable)
		kvm_release_page_dirty(ref->page);
	else
		kvm_release_page_clean(ref->page);
185

186 187 188 189 190
	ref->page = NULL;

	/* XXX set tlb_44x_index to stlb_index? */

	KVMTRACE_1D(STLB_INVAL, &vcpu_44x->vcpu, stlb_index, handler);
191 192
}

193
void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu)
194
{
195
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
196
	int i;
197

198 199
	for (i = 0; i <= tlb_44x_hwater; i++)
		kvmppc_44x_shadow_release(vcpu_44x, i);
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213 214
/**
 * kvmppc_mmu_map -- create a host mapping for guest memory
 *
 * If the guest wanted a larger page than the host supports, only the first
 * host page is mapped here and the rest are demand faulted.
 *
 * If the guest wanted a smaller page than the host page size, we map only the
 * guest-size page (i.e. not a full host page mapping).
 *
 * Caller must ensure that the specified guest TLB entry is safe to insert into
 * the shadow TLB.
 */
void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr, u64 asid,
215
                    u32 flags, u32 max_bytes, unsigned int gtlb_index)
216
{
217
	struct kvmppc_44x_tlbe stlbe;
218
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
219
	struct kvmppc_44x_shadow_ref *ref;
220 221
	struct page *new_page;
	hpa_t hpaddr;
222
	gfn_t gfn;
223 224
	unsigned int victim;

225 226 227 228 229 230 231 232
	/* Select TLB entry to clobber. Indirectly guard against races with the TLB
	 * miss handler by disabling interrupts. */
	local_irq_disable();
	victim = ++tlb_44x_index;
	if (victim > tlb_44x_hwater)
		victim = 0;
	tlb_44x_index = victim;
	local_irq_enable();
233 234

	/* Get reference to new page. */
235
	gfn = gpaddr >> PAGE_SHIFT;
236 237
	new_page = gfn_to_page(vcpu->kvm, gfn);
	if (is_error_page(new_page)) {
H
Hollis Blanchard 已提交
238
		printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
239 240 241 242 243
		kvm_release_page_clean(new_page);
		return;
	}
	hpaddr = page_to_phys(new_page);

244 245
	/* Invalidate any previous shadow mappings. */
	kvmppc_44x_shadow_release(vcpu_44x, victim);
246 247 248 249 250 251 252 253

	/* XXX Make sure (va, size) doesn't overlap any other
	 * entries. 440x6 user manual says the result would be
	 * "undefined." */

	/* XXX what about AS? */

	/* Force TS=1 for all guest mappings. */
254
	stlbe.word0 = PPC44x_TLB_VALID | PPC44x_TLB_TS;
255 256 257 258

	if (max_bytes >= PAGE_SIZE) {
		/* Guest mapping is larger than or equal to host page size. We can use
		 * a "native" host mapping. */
259
		stlbe.word0 |= (gvaddr & PAGE_MASK) | PPC44x_TLBE_SIZE;
260 261 262 263 264
	} else {
		/* Guest mapping is smaller than host page size. We must restrict the
		 * size of the mapping to be at most the smaller of the two, but for
		 * simplicity we fall back to a 4K mapping (this is probably what the
		 * guest is using anyways). */
265
		stlbe.word0 |= (gvaddr & PAGE_MASK_4K) | PPC44x_TLB_4K;
266 267 268 269 270 271 272

		/* 'hpaddr' is a host page, which is larger than the mapping we're
		 * inserting here. To compensate, we must add the in-page offset to the
		 * sub-page. */
		hpaddr |= gpaddr & (PAGE_MASK ^ PAGE_MASK_4K);
	}

273 274
	stlbe.word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
	stlbe.word2 = kvmppc_44x_tlb_shadow_attrib(flags,
275
	                                            vcpu->arch.msr & MSR_PR);
276 277 278 279 280 281 282 283 284 285 286 287 288
	stlbe.tid = !(asid & 0xff);

	/* Keep track of the reference so we can properly release it later. */
	ref = &vcpu_44x->shadow_refs[victim];
	ref->page = new_page;
	ref->gtlb_index = gtlb_index;
	ref->writeable = !!(stlbe.word2 & PPC44x_TLB_UW);
	ref->tid = stlbe.tid;

	/* Insert shadow mapping into hardware TLB. */
	kvmppc_44x_tlbwe(victim, &stlbe);
	KVMTRACE_5D(STLB_WRITE, vcpu, victim, stlbe.tid, stlbe.word0, stlbe.word1,
	            stlbe.word2, handler);
289 290
}

291 292 293 294
/* For a particular guest TLB entry, invalidate the corresponding host TLB
 * mappings and release the host pages. */
static void kvmppc_44x_invalidate(struct kvm_vcpu *vcpu,
                                  unsigned int gtlb_index)
295
{
296
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
297 298
	int i;

299 300 301 302
	for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
		struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];
		if (ref->gtlb_index == gtlb_index)
			kvmppc_44x_shadow_release(vcpu_44x, i);
303 304 305 306
	}
}

void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
307 308 309 310 311
{
	vcpu->arch.shadow_pid = !usermode;
}

void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid)
312
{
313
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
314 315
	int i;

316 317 318 319 320 321 322 323 324
	if (unlikely(vcpu->arch.pid == new_pid))
		return;

	vcpu->arch.pid = new_pid;

	/* Guest userspace runs with TID=0 mappings and PID=0, to make sure it
	 * can't access guest kernel mappings (TID=1). When we switch to a new
	 * guest PID, which will also use host PID=0, we must discard the old guest
	 * userspace mappings. */
325 326 327 328 329
	for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) {
		struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i];

		if (ref->tid == 0)
			kvmppc_44x_shadow_release(vcpu_44x, i);
330 331
	}
}
332 333

static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
334
                             const struct kvmppc_44x_tlbe *tlbe)
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
{
	gpa_t gpa;

	if (!get_tlb_v(tlbe))
		return 0;

	/* Does it match current guest AS? */
	/* XXX what about IS != DS? */
	if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
		return 0;

	gpa = get_tlb_raddr(tlbe);
	if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
		/* Mapping is not for RAM. */
		return 0;

	return 1;
}

354
int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws)
355
{
356
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
357
	struct kvmppc_44x_tlbe *tlbe;
358
	unsigned int gtlb_index;
359

360 361 362
	gtlb_index = vcpu->arch.gpr[ra];
	if (gtlb_index > KVM44x_GUEST_TLB_SIZE) {
		printk("%s: index %d\n", __func__, gtlb_index);
363 364 365 366
		kvmppc_dump_vcpu(vcpu);
		return EMULATE_FAIL;
	}

367
	tlbe = &vcpu_44x->guest_tlb[gtlb_index];
368

369 370 371
	/* Invalidate shadow mappings for the about-to-be-clobbered TLB entry. */
	if (tlbe->word0 & PPC44x_TLB_VALID)
		kvmppc_44x_invalidate(vcpu, gtlb_index);
372 373 374

	switch (ws) {
	case PPC44x_TLB_PAGEID:
375
		tlbe->tid = get_mmucr_stid(vcpu);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
		tlbe->word0 = vcpu->arch.gpr[rs];
		break;

	case PPC44x_TLB_XLAT:
		tlbe->word1 = vcpu->arch.gpr[rs];
		break;

	case PPC44x_TLB_ATTRIB:
		tlbe->word2 = vcpu->arch.gpr[rs];
		break;

	default:
		return EMULATE_FAIL;
	}

	if (tlbe_is_host_safe(vcpu, tlbe)) {
392 393
		u64 asid;
		gva_t eaddr;
394 395 396 397
		gpa_t gpaddr;
		u32 flags;
		u32 bytes;

398
		eaddr = get_tlb_eaddr(tlbe);
399 400 401 402 403 404 405
		gpaddr = get_tlb_raddr(tlbe);

		/* Use the advertised page size to mask effective and real addrs. */
		bytes = get_tlb_bytes(tlbe);
		eaddr &= ~(bytes - 1);
		gpaddr &= ~(bytes - 1);

406 407 408
		asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid;
		flags = tlbe->word2 & 0xffff;

409
		kvmppc_mmu_map(vcpu, eaddr, gpaddr, asid, flags, bytes, gtlb_index);
410 411
	}

412 413
	KVMTRACE_5D(GTLB_WRITE, vcpu, gtlb_index, tlbe->tid, tlbe->word0,
	            tlbe->word1, tlbe->word2, handler);
414 415 416 417

	return EMULATE_DONE;
}

418
int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc)
419 420
{
	u32 ea;
421
	int gtlb_index;
422 423 424 425 426 427 428
	unsigned int as = get_mmucr_sts(vcpu);
	unsigned int pid = get_mmucr_stid(vcpu);

	ea = vcpu->arch.gpr[rb];
	if (ra)
		ea += vcpu->arch.gpr[ra];

429
	gtlb_index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
430
	if (rc) {
431
		if (gtlb_index < 0)
432 433 434 435
			vcpu->arch.cr &= ~0x20000000;
		else
			vcpu->arch.cr |= 0x20000000;
	}
436
	vcpu->arch.gpr[rt] = gtlb_index;
437 438 439

	return EMULATE_DONE;
}