Skip to contentSkip to conversation

kvmppc refactor example

PATCH In review Liu Yu 2 participants 2 messages Sep 18, 2008 – Sep 29, 2008

Subsystems (2)
lore
Share & tools
lore

Liu Yu

author
#0 lore raw

PATCH 1/1 kvmppc refactor example

This patch gives a example of refactor, which is derived from x86. It aims at collecting comments.

I have not even tried to build it, for I have no 440 toolchain. I can test it on E500, but it needs some time to move E500 code base on it.

Hollis,

1. Do we need to move 44x_tlb.c into 44x.c?
so that some special struct definition can be moved from kvm_host.h to 44x.c/.h.

2. We could adopt the ops method like x86,
then kvm for different cores can be dynamicly inserted as a module, or adopt static compile method like current kvmppc.
How do you think of it.

---
 arch/powerpc/include/asm/kvm_host.h |   27 ++++++++++++-------
 arch/powerpc/kvm/44x.c              |   47 ++++++++++++++++++++++++++++++++
 arch/powerpc/kvm/44x_tlb.c          |   50 ++++++++++++++++++++--------------
 arch/powerpc/kvm/44x_tlb.h          |    5 +++
 arch/powerpc/kvm/booke_host.c       |   12 +++-----
 arch/powerpc/kvm/powerpc.c          |   26 +++--------------
 6 files changed, 107 insertions(+), 60 deletions(-)
 create mode 100644 arch/powerpc/kvm/44x.c
arch/powerpc/include/asm/kvm_host.h +17 -10
index df73351..930921c 100644
@@ -75,16 +75,6 @@ struct kvm_arch {
 };
 
 struct kvm_vcpu_arch {
-	/* Unmodified copy of the guest's TLB. */
-	struct kvmppc_44x_tlbe guest_tlb[PPC44x_TLB_SIZE];
-	/* TLB that's actually used when the guest is running. */
-	struct kvmppc_44x_tlbe shadow_tlb[PPC44x_TLB_SIZE];
-	/* Pages which are referenced in the shadow TLB. */
-	struct page *shadow_pages[PPC44x_TLB_SIZE];
-
-	/* Track which TLB entries we've modified in the current exit. */
-	u8 shadow_tlb_mod[PPC44x_TLB_SIZE];
-
 	u32 host_stack;
 	u32 host_pid;
 	u32 host_dbcr0;
@@ -156,6 +146,23 @@ struct kvm_vcpu_arch {
 	unsigned long pending_exceptions;
 };
 
+struct vcpu_44x {
+	struct kvm_vcpu vcpu;
+	/* Unmodified copy of the guest's TLB. */
+	struct kvmppc_44x_tlbe guest_tlb[PPC44x_TLB_SIZE];
+	/* TLB that's actually used when the guest is running. */
+	struct kvmppc_44x_tlbe shadow_tlb[PPC44x_TLB_SIZE];
+	/* Pages which are referenced in the shadow TLB. */
+	struct page *shadow_pages[PPC44x_TLB_SIZE];
+	/* Track which TLB entries we've modified in the current exit. */
+	u8 shadow_tlb_mod[PPC44x_TLB_SIZE];
+};
+
+struct kvm_powerpc_ops {
+	struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
+	void (*vcpu_free)(struct kvm_vcpu *vcpu);
+}
+
 struct kvm_guest_debug {
 	int enabled;
 	unsigned long bp[4];
arch/powerpc/kvm/44x.c new file +47 -0

47 lines not on this page · raw message

arch/powerpc/kvm/44x_tlb.c +29 -21

180 lines not on this page · raw message

arch/powerpc/kvm/44x_tlb.h +5 -0
index e5b0a76..6d82601 100644
@@ -90,4 +90,9 @@ static inline gpa_t tlb_xlate(struct kvmppc_44x_tlbe *tlbe, gva_t eaddr)
 	return get_tlb_raddr(tlbe) | (eaddr & pgmask);
 }
 
+static inline struct vcpu_44x *to_44x(struct kvm_vcpu *vcpu)
+{
+	return container_of(vcpu, struct vcpu_44x, vcpu);
+}
+
 #endif /* __KVM_POWERPC_TLB_H__ */
arch/powerpc/kvm/booke_host.c +4 -8
index b480341..6d45e7d 100644
@@ -19,13 +19,12 @@
 
 #include <linux/errno.h>
 #include <linux/kvm_host.h>
-#include <linux/module.h>
 #include <asm/cacheflush.h>
 #include <asm/kvm_ppc.h>
 
 unsigned long kvmppc_booke_handlers;
 
-static int kvmppc_booke_init(void)
+void kvmppc_booke_init(void)
 {
 	unsigned long ivor[16];
 	unsigned long max_ivor = 0;
@@ -69,15 +68,12 @@ static int kvmppc_booke_init(void)
 	}
 	flush_icache_range(kvmppc_booke_handlers,
 	                   kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
-
-	return kvm_init(NULL, sizeof(struct kvm_vcpu), THIS_MODULE);
 }
+EXPORT_SYMBOL_GPL(kvmppc_booke_init);
 
-static void __exit kvmppc_booke_exit(void)
+void __exit kvmppc_booke_exit(void)
 {
 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
-	kvm_exit();
 }
+EXPORT_SYMBOL_GPL(kvmppc_booke_exit);
 
-module_init(kvmppc_booke_init)
-module_exit(kvmppc_booke_exit)
arch/powerpc/kvm/powerpc.c +5 -21
index 90a6fc4..6d0e6c5 100644
@@ -29,6 +29,8 @@
 #include <asm/kvm_ppc.h>
 #include <asm/tlbflush.h>
 
+struct kvm_powerpc_ops *kvm_powerpc_ops;
+EXPORT_SYMBOL_GPL(kvm_powerpc_ops);
 
 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
 {
@@ -177,31 +179,12 @@ void kvm_arch_flush_shadow(struct kvm *kvm)
 
 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
 {
-	struct kvm_vcpu *vcpu;
-	int err;
-
-	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
-	if (!vcpu) {
-		err = -ENOMEM;
-		goto out;
-	}
-
-	err = kvm_vcpu_init(vcpu, kvm, id);
-	if (err)
-		goto free_vcpu;
-
-	return vcpu;
-
-free_vcpu:
-	kmem_cache_free(kvm_vcpu_cache, vcpu);
-out:
-	return ERR_PTR(err);
+	return kvm_powerpc_ops->vcpu_create(kvm, id);
 }
 
 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
 {
-	kvm_vcpu_uninit(vcpu);
-	kmem_cache_free(kvm_vcpu_cache, vcpu);
+	kvm_powerpc_ops->vcpu_free(vcpu);
 }
 
 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
@@ -542,6 +525,7 @@ long kvm_arch_vm_ioctl(struct file *filp,
 
 int kvm_arch_init(void *opaque)
 {
+	kvm_powerpc_ops = (struct kvm_powerpc_ops *)opaque;
 	return 0;
 }
 
Signature · 1 line
1.5.4
#1 lore raw
Quoted 2 lines · Liu Yu

This patch gives a example of refactor, which is derived from x86. It aims at collecting comments.

Thanks for sending this Yu. I needed some more extensive code refactoring work for another processor I'm looking at (I'll send patches shortly), but this approach is still needed to divide struct kvm_vcpu_arch. I will look at adapting this patch on top of the other refactoring I've done.

Quoted 7 lines

I have not even tried to build it, for I have no 440 toolchain. I can test it on E500, but it needs some time to move E500 code base on it.

Hollis,

1. Do we need to move 44x_tlb.c into 44x.c?
so that some special struct definition can be moved from kvm_host.h to 44x.c/.h.

Yeah, we definitely need a 44x.c, but I think also a 44x_tlb.c and 44x_emulate.c to keep it from growing too large.

Quoted 4 lines

2. We could adopt the ops method like x86,
then kvm for different cores can be dynamicly inserted as a module, or adopt static compile method like current kvmppc.
How do you think of it.

Well, unlike Intel/AMD, we can't have a single binary for any of our processors (440, e500, e500mc, future). Even e500 and e500mc can't both be supported in a single binary kernel, right?

Given that, I don't think function pointers are the right approach.

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center