mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 25/40] KVM: MMU: Add for_each_shadow_entry(), a simpler alternative to walk_shadow()
Date: Thu, 26 Feb 2009 16:15:15 +0200	[thread overview]
Message-ID: <1235657730-27683-26-git-send-email-avi@redhat.com> (raw)
In-Reply-To: <1235657730-27683-1-git-send-email-avi@redhat.com>

Using a for_each loop style removes the need to write callback and nasty
casts.

Implement the walk_shadow() using the for_each_shadow_entry().

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 arch/x86/kvm/mmu.c |   69 +++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 5f03ec3..c669f2a 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -150,6 +150,20 @@ struct kvm_shadow_walk {
 		     u64 addr, u64 *spte, int level);
 };
 
+struct kvm_shadow_walk_iterator {
+	u64 addr;
+	hpa_t shadow_addr;
+	int level;
+	u64 *sptep;
+	unsigned index;
+};
+
+#define for_each_shadow_entry(_vcpu, _addr, _walker)    \
+	for (shadow_walk_init(&(_walker), _vcpu, _addr);	\
+	     shadow_walk_okay(&(_walker));			\
+	     shadow_walk_next(&(_walker)))
+
+
 struct kvm_unsync_walk {
 	int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
 };
@@ -1254,33 +1268,48 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 	return sp;
 }
 
+static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
+			     struct kvm_vcpu *vcpu, u64 addr)
+{
+	iterator->addr = addr;
+	iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
+	iterator->level = vcpu->arch.mmu.shadow_root_level;
+	if (iterator->level == PT32E_ROOT_LEVEL) {
+		iterator->shadow_addr
+			= vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
+		iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
+		--iterator->level;
+		if (!iterator->shadow_addr)
+			iterator->level = 0;
+	}
+}
+
+static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
+{
+	if (iterator->level < PT_PAGE_TABLE_LEVEL)
+		return false;
+	iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
+	iterator->sptep	= ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
+	return true;
+}
+
+static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
+{
+	iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
+	--iterator->level;
+}
+
 static int walk_shadow(struct kvm_shadow_walk *walker,
 		       struct kvm_vcpu *vcpu, u64 addr)
 {
-	hpa_t shadow_addr;
-	int level;
+	struct kvm_shadow_walk_iterator iterator;
 	int r;
-	u64 *sptep;
-	unsigned index;
-
-	shadow_addr = vcpu->arch.mmu.root_hpa;
-	level = vcpu->arch.mmu.shadow_root_level;
-	if (level == PT32E_ROOT_LEVEL) {
-		shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
-		shadow_addr &= PT64_BASE_ADDR_MASK;
-		if (!shadow_addr)
-			return 1;
-		--level;
-	}
 
-	while (level >= PT_PAGE_TABLE_LEVEL) {
-		index = SHADOW_PT_INDEX(addr, level);
-		sptep = ((u64 *)__va(shadow_addr)) + index;
-		r = walker->entry(walker, vcpu, addr, sptep, level);
+	for_each_shadow_entry(vcpu, addr, iterator) {
+		r = walker->entry(walker, vcpu, addr,
+				  iterator.sptep, iterator.level);
 		if (r)
 			return r;
-		shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
-		--level;
 	}
 	return 0;
 }
-- 
1.6.0.6


  parent reply	other threads:[~2009-02-26 14:20 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-26 14:14 [PATCH 00/40] KVM Updates for the 2.6.30 merge window (1/3) Avi Kivity
2009-02-26 14:14 ` [PATCH 01/40] KVM: SVM: Clean up VINTR setting Avi Kivity
2009-02-26 14:14 ` [PATCH 02/40] KVM: SVM: Move EFER and MSR constants to generic x86 code Avi Kivity
2009-02-26 14:14 ` [PATCH 03/40] KVM: SVM: Add helper functions for nested SVM Avi Kivity
2009-02-26 14:14 ` [PATCH 04/40] KVM: SVM: Implement GIF, clgi and stgi Avi Kivity
2009-02-26 14:14 ` [PATCH 05/40] KVM: SVM: Implement hsave Avi Kivity
2009-02-26 14:14 ` [PATCH 06/40] KVM: SVM: Add VMLOAD and VMSAVE handlers Avi Kivity
2009-02-26 14:14 ` [PATCH 07/40] KVM: SVM: Add VMRUN handler Avi Kivity
2009-02-26 14:14 ` [PATCH 08/40] KVM: SVM: Add VMEXIT handler and intercepts Avi Kivity
2009-02-26 14:14 ` [PATCH 09/40] KVM: SVM: Allow read access to MSR_VM_VR Avi Kivity
2009-02-26 14:15 ` [PATCH 10/40] KVM: SVM: Allow setting the SVME bit Avi Kivity
2009-02-26 14:15 ` [PATCH 11/40] KVM: SVM: Only allow setting of EFER_SVME when CPUID SVM is set Avi Kivity
2009-02-26 14:15 ` [PATCH 12/40] KVM: VMX: Support for injecting software exceptions Avi Kivity
2009-02-26 14:15 ` [PATCH 13/40] KVM: New guest debug interface Avi Kivity
2009-02-26 14:15 ` [PATCH 14/40] KVM: VMX: Allow single-stepping when uninterruptible Avi Kivity
2009-02-26 14:15 ` [PATCH 15/40] KVM: x86: Virtualize debug registers Avi Kivity
2009-02-26 14:15 ` [PATCH 16/40] KVM: x86: Wire-up hardware breakpoints for guest debugging Avi Kivity
2009-02-26 14:15 ` [PATCH 17/40] KVM: ia64: stack get/restore patch Avi Kivity
2009-02-26 14:15 ` [PATCH 18/40] KVM: Remove old kvm_guest_debug structs Avi Kivity
2009-02-26 14:15 ` [PATCH 19/40] KVM: ia64: Code cleanup Avi Kivity
2009-02-26 14:15 ` [PATCH 20/40] KVM: MMU: Inherit a shadow page's guest level count from vcpu setup Avi Kivity
2009-02-26 14:15 ` [PATCH 21/40] KVM: MMU: Segregate mmu pages created with different cr4.pge settings Avi Kivity
2009-02-26 14:15 ` [PATCH 22/40] KVM: MMU: Initialize a shadow page's global attribute from cr4.pge Avi Kivity
2009-02-26 14:15 ` [PATCH 23/40] KVM: Fix vmload and friends misinterpreted as lidt Avi Kivity
2009-02-26 14:15 ` [PATCH 24/40] KVM: Advertise guest debug capability per-arch Avi Kivity
2009-02-26 14:15 ` Avi Kivity [this message]
2009-02-26 14:15 ` [PATCH 26/40] KVM: MMU: Use for_each_shadow_entry() in __direct_map() Avi Kivity
2009-02-26 14:15 ` [PATCH 27/40] KVM: MMU: Replace walk_shadow() by for_each_shadow_entry() in fetch() Avi Kivity
2009-02-26 14:15 ` [PATCH 28/40] KVM: MMU: Replace walk_shadow() by for_each_shadow_entry() in invlpg() Avi Kivity
2009-02-26 14:15 ` [PATCH 29/40] KVM: MMU: Drop walk_shadow() Avi Kivity
2009-02-26 14:15 ` [PATCH 30/40] KVM: VMX: initialize TSC offset relative to vm creation time Avi Kivity
2009-02-26 14:15 ` [PATCH 31/40] KVM: introduce kvm_read_guest_virt, kvm_write_guest_virt Avi Kivity
2009-02-26 14:15 ` [PATCH 32/40] KVM: remove the vmap usage Avi Kivity
2009-02-26 14:15 ` [PATCH 33/40] KVM: Fallback support for MSR_VM_HSAVE_PA Avi Kivity
2009-02-26 14:15 ` [PATCH 34/40] KVM: PIT: provide an option to disable interrupt reinjection Avi Kivity
2009-02-26 14:15 ` [PATCH 35/40] KVM: Move struct kvm_pio_request into x86 kvm_host.h Avi Kivity
2009-02-26 14:15 ` [PATCH 36/40] KVM: ppc: move struct kvmppc_44x_tlbe into 44x-specific header Avi Kivity
2009-02-26 14:15 ` [PATCH 37/40] KVM: ppc: cosmetic changes to mmu hook names Avi Kivity
2009-02-26 14:15 ` [PATCH 38/40] KVM: ppc: small cosmetic changes to Book E DTLB miss handler Avi Kivity
2009-02-26 14:15 ` [PATCH 39/40] KVM: ppc: change kvmppc_mmu_map() parameters Avi Kivity
2009-02-26 14:15 ` [PATCH 40/40] KVM: ppc: turn tlb_xlate() into a per-core hook (and give it a better name) Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1235657730-27683-26-git-send-email-avi@redhat.com \
    --to=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®