Skip to contentSkip to conversation

Refactor TLB codes about 44x to general

PATCH In review Zhang Wei 1 participant 1 message Jan 25, 2008

Subsystems (2)
lore
Share & tools
lore

Zhang Wei

author
#0 lore raw

PATCH 4/5 Refactor TLB codes about 44x to general

---
 arch/powerpc/kvm/44x_tlb.c    |  309 +++++++++++++++++------------------------
 arch/powerpc/kvm/44x_tlb.h    |    5 -
 arch/powerpc/kvm/Makefile     |    2 +-
 arch/powerpc/kvm/emulate.c    |    4 +-
 arch/powerpc/kvm/powerpc.c    |   34 ++---
 arch/powerpc/kvm/tlb.c        |  226 ++++++++++++++++++++++++++++++
 arch/powerpc/kvm/tlb.h        |   54 +++++++
 include/asm-powerpc/kvm_asm.h |    2 +-
 8 files changed, 426 insertions(+), 210 deletions(-)
 create mode 100644 arch/powerpc/kvm/tlb.c
 create mode 100644 arch/powerpc/kvm/tlb.h
arch/powerpc/kvm/44x_tlb.c +128 -181
index 241fa0e..599ec6a 100644
@@ -15,200 +15,147 @@
  * Copyright IBM Corp. 2007
  *
  * Authors: Hollis Blanchard <[email protected]>
+ *
+ * The original 44x_tlb.c is moved to tlb.c.
+ *
+ * The file implements some TLB operations on PowerPC 44x processor.
+ *
+ * Copyright (C) 2008 Freescale Semiconductor, Inc.
+ *
+ * Zhang Wei <[email protected]>
  */
 
 #include <linux/types.h>
 #include <linux/string.h>
 #include <linux/kvm_host.h>
 
+#include "tlb.h"
 #include "44x_tlb.h"
 
-#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)
-
-static unsigned int tlb_index = 1;
-
-static u32 tlb_shadow_attrib(u32 attrib, int usermode)
-{
-	/* XXX remove mask when Linux is fixed */
-	attrib &= 0xf03f;
-
-	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;
-
-	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)
-{
-	int i;
-
-	/* XXX Replace loop with fancy data structures. */
-	for (i = 0; i < PPC44x_TLB_SIZE; i++) {
-		struct tlbe *tlbe = &vcpu->arch.guest_tlb[i];
-		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;
-}
-
-struct tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
-{
-	unsigned int as = !!(vcpu->arch.guest_msr & MSR_IS);
-        unsigned int index;
-
-	index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
-        if (index = -1)
-                return NULL;
-        return &vcpu->arch.guest_tlb[index];
-}
-
-struct tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu, gva_t eaddr)
-{
-	unsigned int as = !!(vcpu->arch.guest_msr & MSR_DS);
-        unsigned int index;
-
-	index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as);
-        if (index = -1)
-                return NULL;
-        return &vcpu->arch.guest_tlb[index];
-}
-
 /* 440 host */
-
-static hpa_t kvmppc_gpa_to_hpa(struct kvm *kvm, gfn_t gfn)
+/* TLB helper functions */
+void kvmppc_set_tlbe(struct tlbe *tlbes, u32 entry, u32 tid, gva_t ep, gpa_t rp, size_t size, u32 flags)
 {
-	struct page *page;
-
-	down_write(&current->mm->mmap_sem);
-	page = gfn_to_page(kvm, gfn);
-	up_write(&current->mm->mmap_sem);
-
-	if (is_error_page(page)) {
-		printk(KERN_ERR "Couldn't get guest page!\n");
-		kvm_release_page_clean(page);
-		return (hpa_t)-1;
-	}
-
-	return page_to_phys(page);
+	int bit;
+	struct tlbe *tlbe = tlbes + entry;
+
+	tlbe->tid = tid & 0xff;
+	tlbe->word0 = (ep & PPC44x_TLB_EPN_MASK) |
+					((__ilog2(size >> 10) / 2) << 4);
+	tlbe->word1 = (((u32)rp) & PPC44x_TLB_RPN_MASK) |
+					((rp >> 32) & PPC44x_TLB_ERPN_MASK);
+	tlbe->word2 = 0;
+	for_each_bit(bit, &flags, sizeof(u32) * BITS_PER_BYTE)
+		switch (BIT(bit)) {
+		case TLBE_VALID:
+			tlbe->word0 |= PPC44x_TLB_VALID;
+			break;
+		case TLBE_TS:
+			tlbe->word0 |= PPC44x_TLB_TS;
+			break;
+		case TLBE_U0:
+			tlbe->word2 |= PPC44x_TLB_U0;
+			break;
+		case TLBE_U1:
+			tlbe->word2 |= PPC44x_TLB_U1;
+			break;
+		case TLBE_U2:
+			tlbe->word2 |= PPC44x_TLB_U2;
+			break;
+		case TLBE_U3:
+			tlbe->word2 |= PPC44x_TLB_U3;
+			break;
+		case TLBE_W:
+			tlbe->word2 |= PPC44x_TLB_W;
+			break;
+		case TLBE_I:
+			tlbe->word2 |= PPC44x_TLB_I;
+			break;
+		case TLBE_M:
+			tlbe->word2 |= PPC44x_TLB_M;
+			break;
+		case TLBE_G:
+			tlbe->word2 |= PPC44x_TLB_G;
+			break;
+		case TLBE_E:
+			tlbe->word2 |= PPC44x_TLB_E;
+			break;
+		case TLBE_UX:
+			tlbe->word2 |= PPC44x_TLB_UX;
+			break;
+		case TLBE_SX:
+			tlbe->word2 |= PPC44x_TLB_SX;
+			break;
+		case TLBE_UW:
+			tlbe->word2 |= PPC44x_TLB_UW;
+			break;
+		case TLBE_SW:
+			tlbe->word2 |= PPC44x_TLB_SW;
+			break;
+		case TLBE_UR:
+			tlbe->word2 |= PPC44x_TLB_UR;
+			break;
+		case TLBE_SR:
+			tlbe->word2 |= PPC44x_TLB_SR;
+			break;
+		}
 }
 
-/* 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, gfn_t gfn, u64 asid,
-                    u32 flags)
+u32 flags_to_tlbe(const struct tlbe *tlbe)
 {
-	struct tlbe *stlbe;
-	hpa_t hpaddr;
-	unsigned int victim;
-
-	/* Future optimization: don't overwrite the TLB entry containing the
-	 * current PC (or stack?). */
-	do {
-		victim = tlb_index++;
-		if (tlb_index >= PPC44x_TLB_SIZE)
-			tlb_index = 0;
-	} while (victim = vcpu->arch.trampoline_tlbe);
-
-	stlbe = &vcpu->arch.shadow_tlb[victim];
-
-	/* Determine the host physical address to put into the hardware TLB. */
-	hpaddr = kvmppc_gpa_to_hpa(vcpu->kvm, gfn);
-	if (hpaddr = (hpa_t)-1)
-		return;
-
-	/* XXX Make sure (va, size) doesn't overlap any other
-	 * entries. 440x6 user manual says the result would be
-	 * "undefined." */
-
-	/* XXX what about AS? */
-
-	stlbe->tid = asid & 0xff;
-
-	/* Force TS=1 for all guest mappings. */
-	/* For now we hardcode 4KB mappings, but it will be important to
-	 * use host large pages in the future. */
-	stlbe->word0 = (gvaddr & PAGE_MASK) | PPC44x_TLB_VALID | PPC44x_TLB_TS
-	               | PPC44x_TLB_4K;
-
-	stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf);
-	stlbe->word2 = tlb_shadow_attrib(flags, vcpu->arch.guest_msr & MSR_PR);
-
-#if 0
-	printk("stlbe %2d: %08x %08x %08x %08x\n", victim,
-	       stlbe->tid, stlbe->word0,
-	       stlbe->word1, stlbe->word2);
-#endif
+	int bit;
+	u32 r = 0;
+	u32 flags = tlbe->word2 & 0xffff;
+
+	for_each_bit(bit, &flags, sizeof(u32) * BITS_PER_BYTE)
+		switch (BIT(bit)) {
+		case PPC44x_TLB_U0:
+			r |= TLBE_U0;
+			break;
+		case PPC44x_TLB_U1:
+			r |= TLBE_U1;
+			break;
+		case PPC44x_TLB_U2:
+			r |= TLBE_U2;
+			break;
+		case PPC44x_TLB_U3:
+			r |= TLBE_U3;
+			break;
+		case PPC44x_TLB_W:
+			r |= TLBE_W;
+			break;
+		case PPC44x_TLB_I:
+			r |= TLBE_I;
+			break;
+		case PPC44x_TLB_M:
+			r |= TLBE_M;
+			break;
+		case PPC44x_TLB_G:
+			r |= TLBE_G;
+			break;
+		case PPC44x_TLB_E:
+			r |= TLBE_E;
+			break;
+		case PPC44x_TLB_UX:
+			r |= TLBE_UX;
+			break;
+		case PPC44x_TLB_UW:
+			r |= TLBE_UW;
+			break;
+		case PPC44x_TLB_UR:
+			r |= TLBE_UR;
+			break;
+		case PPC44x_TLB_SX:
+			r |= TLBE_SX;
+			break;
+		case PPC44x_TLB_SW:
+			r |= TLBE_SW;
+			break;
+		case PPC44x_TLB_SR:
+			r |= TLBE_SR;
+			break;
+		}
+	return r;
 }
 
-void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, u64 eaddr, u64 asid)
-{
-	unsigned int pid = asid & 0xff;
-	int i;
-	unsigned long track[2] = {0, 0};
-
-	/* XXX Replace loop with fancy data structures. */
-	for (i = 0; i < PPC44x_TLB_SIZE; i++) {
-		struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
-		unsigned int tid;
-
-		if (eaddr < get_tlb_eaddr(stlbe))
-			continue;
-
-		if (eaddr > get_tlb_end(stlbe))
-			continue;
-
-		tid = get_tlb_tid(stlbe);
-		if (tid && (tid != pid))
-			continue;
 
-		set_bit(i, track);
-		stlbe->word0 = 0;
-	}
-
-#if 0
-	printk("%s: %016llx %016llx inv %lx %lx\n", __func__, eaddr, asid,
-	       track[1], track[0]);
-#endif
-}
-
-/* Invalidate all mappings, so that when they fault back in they will get the
- * proper permission bits. */
-void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
-{
-	int i;
-
-	/* XXX Replace loop with fancy data structures. */
-	for (i = 0; i < PPC44x_TLB_SIZE; i++) {
-		if (i = vcpu->arch.trampoline_tlbe)
-			continue;
-		vcpu->arch.shadow_tlb[i].word0 = 0;
-	}
-}
arch/powerpc/kvm/44x_tlb.h +0 -5
index 2ccd46b..e2b5836 100644
@@ -23,11 +23,6 @@
 #include <linux/kvm_host.h>
 #include <asm/mmu-44x.h>
 
-extern int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr,
-                                unsigned int pid, unsigned int as);
-extern struct tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu, gva_t eaddr);
-extern struct tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu, gva_t eaddr);
-
 /* TLB helper functions */
 static inline unsigned int get_tlb_size(const struct tlbe *tlbe)
 {
arch/powerpc/kvm/Makefile +1 -1
index 9d672d1..b342ef3 100644
@@ -9,7 +9,7 @@ common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o)
 kvm-objs := $(common-objs)
 obj-$(CONFIG_KVM) += kvm.o
 
-kvm-powerpc-objs := powerpc.o emulate.o 44x_tlb.o booke_interrupts.o
+kvm-powerpc-objs := powerpc.o emulate.o 44x_tlb.o booke_interrupts.o tlb.o
 obj-$(CONFIG_KVM_POWERPC) += kvm-powerpc.o
 
 AFLAGS_booke_interrupts.o := -I$(obj)
arch/powerpc/kvm/emulate.c +2 -2
index b4b1442..ad4a26a 100644
@@ -27,7 +27,7 @@
 #include <asm/time.h>
 #include <asm/byteorder.h>
 
-#include "44x_tlb.h"
+#include "tlb.h"
 
 #define DCRN_CPR0_CFGADDR	0xc
 #define DCRN_CPR0_CFGDATA	0xd
@@ -561,7 +561,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
 			if (ra)
 				ea += vcpu->arch.gpr[ra];
 
-			index = kvmppc_44x_tlb_index(vcpu, ea, pid, as);
+			index = kvmppc_tlb_index(vcpu, ea, pid, as);
 			if (rc) {
 				if (index < 0)
 					vcpu->arch.cr &= ~0x20000000;
arch/powerpc/kvm/powerpc.c +14 -20
index 9866b3c..ebe26a1 100644
@@ -27,7 +27,7 @@
 #include <asm/cputable.h>
 #include <asm/cacheflush.h>
 
-#include "44x_tlb.h"
+#include "tlb.h"
 
 struct kvm_stats_debugfs_item debugfs_entries[] = {
 	{ NULL }
@@ -219,7 +219,7 @@ int kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		gfn_t gfn;
 
 		/* Check the guest TLB. */
-		gtlbe = kvmppc_44x_dtlb_search(vcpu, eaddr);
+		gtlbe = kvmppc_dtlb_search(vcpu, eaddr);
 		if (!gtlbe) {
 			/* The guest didn't have a mapping for it. */
 			kvmppc_sync_interrupt(vcpu, exit_nr);
@@ -263,7 +263,7 @@ int kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		r = RESUME_GUEST;
 
 		/* Check the guest TLB. */
-		gtlbe = kvmppc_44x_itlb_search(vcpu, eaddr);
+		gtlbe = kvmppc_itlb_search(vcpu, eaddr);
 		if (!gtlbe) {
 			/* The guest didn't have a mapping for it. */
 			kvmppc_sync_interrupt(vcpu, exit_nr);
@@ -555,11 +555,10 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 
 	/* Insert mapping for the trampoline. */
 	tlbe = &vcpu->arch.shadow_tlb[vcpu->arch.trampoline_tlbe];
-	tlbe->tid = 0;
-	tlbe->word0 = (unsigned long)vcpu->arch.trampoline
-	              | VCPU_TLB_PGSZ | PPC44x_TLB_VALID;
-	tlbe->word1 = __pa(handlers);
-	tlbe->word2 = PPC44x_TLB_SX|PPC44x_TLB_SW|PPC44x_TLB_SR;
+	kvmppc_set_tlbe(vcpu->arch.shadow_tlb, vcpu->arch.trampoline_tlbe,
+			0, (unsigned long)vcpu->arch.trampoline, __pa(handlers),
+			0x10000, /* 64K */
+			TLBE_VALID | TLBE_SX | TLBE_SW | TLBE_SR);
 	printk("tlb[%d]: %x %x %x %x\n", vcpu->arch.trampoline_tlbe,
 	       tlbe->tid,
 	       tlbe->word0,
@@ -580,19 +579,14 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
 {
-	struct tlbe *tlbe = &vcpu->arch.guest_tlb[0];
+	kvmppc_set_tlbe(vcpu->arch.guest_tlb, 0, 0,
+			0, 0, 0x1000000, /* ep = 0, rp = 0, size = 16MB */
+			TLBE_VALID | TLBE_SX | TLBE_SW | TLBE_SR);
 
-	tlbe->tid = 0;
-	tlbe->word0 = PPC44x_TLB_16M | PPC44x_TLB_VALID;
-	tlbe->word1 = 0;
-	tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR;
-
-	tlbe++;
-	tlbe->tid = 0;
-	tlbe->word0 = 0xef600000 | PPC44x_TLB_4K | PPC44x_TLB_VALID;
-	tlbe->word1 = 0xef600000;
-	tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR
-	              | PPC44x_TLB_I | PPC44x_TLB_G;
+	kvmppc_set_tlbe(vcpu->arch.guest_tlb, 1, 0,
+			0xef600000, 0xef600000, 4096,
+			TLBE_VALID | TLBE_SX | TLBE_SW | TLBE_SR
+			| TLBE_I | TLBE_G);
 
 	vcpu->arch.pc = 0;
 	vcpu->arch.guest_msr = 0;
arch/powerpc/kvm/tlb.c new file +226 -0

226 lines not on this page · raw message

arch/powerpc/kvm/tlb.h new file +54 -0

54 lines not on this page · raw message

include/asm-powerpc/kvm_asm.h +1 -1
index 3c5da1d..6bd9878 100644
@@ -23,7 +23,6 @@
 /* IVPR must be 64KiB-aligned. */
 #define VCPU_SIZE_ORDER	4
 #define VCPU_SIZE_LOG	(VCPU_SIZE_ORDER + 12)
-#define VCPU_TLB_PGSZ	PPC44x_TLB_64K
 #define VCPU_SIZE_BYTES	(1<<VCPU_SIZE_LOG)
 
 #define BOOKE_INTERRUPT_CRITICAL 0
@@ -59,6 +58,7 @@
 
 #if defined(CONFIG_KVM_POWERPC_440)
 #define KVMPPC_HOST_TLB_SIZE PPC44x_TLB_SIZE
+#define VCPU_TLB_PGSZ	PPC44x_TLB_64K
 #endif
 
 #endif /* __POWERPC_KVM_ASM_H__ */
Signature · 11 lines
1.5.2


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
kvm-ppc-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-ppc-devel