All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xiao Guangrong <guangrong.xiao@linux.intel.com>
To: pbonzini@redhat.com
Cc: gleb@kernel.org, mtosatti@redhat.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, dmatlack@google.com,
	Xiao Guangrong <guangrong.xiao@linux.intel.com>
Subject: [PATCH v2 06/15] KVM: MTRR: clean up mtrr default type
Date: Mon, 15 Jun 2015 16:55:26 +0800	[thread overview]
Message-ID: <1434358535-19923-7-git-send-email-guangrong.xiao@linux.intel.com> (raw)
In-Reply-To: <1434358535-19923-1-git-send-email-guangrong.xiao@linux.intel.com>

Drop kvm_mtrr->enable, omit the decode/code workload and get rid of
all the hard code

Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
---
 arch/x86/include/asm/kvm_host.h |  3 +--
 arch/x86/kvm/mtrr.c             | 40 ++++++++++++++++++++++++++++------------
 2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index fe9cbe4..8d43006 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -345,8 +345,7 @@ enum {
 struct kvm_mtrr {
 	struct mtrr_var_range var_ranges[KVM_NR_VAR_MTRR];
 	mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION];
-	unsigned char enabled;
-	mtrr_type def_type;
+	u64 deftype;
 };
 
 struct kvm_vcpu_arch {
diff --git a/arch/x86/kvm/mtrr.c b/arch/x86/kvm/mtrr.c
index ce061ff..191de64 100644
--- a/arch/x86/kvm/mtrr.c
+++ b/arch/x86/kvm/mtrr.c
@@ -22,6 +22,10 @@
 #include "cpuid.h"
 #include "mmu.h"
 
+#define IA32_MTRR_DEF_TYPE_E		(1ULL << 11)
+#define IA32_MTRR_DEF_TYPE_FE		(1ULL << 10)
+#define IA32_MTRR_DEF_TYPE_TYPE_MASK	(0xff)
+
 static bool msr_mtrr_valid(unsigned msr)
 {
 	switch (msr) {
@@ -101,10 +105,24 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 }
 EXPORT_SYMBOL_GPL(kvm_mtrr_valid);
 
+static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
+{
+	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
+}
+
+static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
+{
+	return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
+}
+
+static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
+{
+	return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
+}
+
 static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
 {
 	struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
-	unsigned char mtrr_enabled = mtrr_state->enabled;
 	gfn_t start, end, mask;
 	int index;
 	bool is_fixed = true;
@@ -113,7 +131,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
 	      !kvm_arch_has_noncoherent_dma(vcpu->kvm))
 		return;
 
-	if (!(mtrr_enabled & 0x2) && msr != MSR_MTRRdefType)
+	if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
 		return;
 
 	switch (msr) {
@@ -152,7 +170,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
 		end = ((start & mask) | ~mask) + 1;
 	}
 
-	if (is_fixed && !(mtrr_enabled & 0x1))
+	if (is_fixed && !fixed_mtrr_is_enabled(mtrr_state))
 		return;
 
 	kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
@@ -165,10 +183,9 @@ int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 	if (!kvm_mtrr_valid(vcpu, msr, data))
 		return 1;
 
-	if (msr == MSR_MTRRdefType) {
-		vcpu->arch.mtrr_state.def_type = data;
-		vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
-	} else if (msr == MSR_MTRRfix64K_00000)
+	if (msr == MSR_MTRRdefType)
+		vcpu->arch.mtrr_state.deftype = data;
+	else if (msr == MSR_MTRRfix64K_00000)
 		p[0] = data;
 	else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
 		p[1 + msr - MSR_MTRRfix16K_80000] = data;
@@ -215,8 +232,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		return 1;
 
 	if (msr == MSR_MTRRdefType)
-		*pdata = vcpu->arch.mtrr_state.def_type +
-			 (vcpu->arch.mtrr_state.enabled << 10);
+		*pdata = vcpu->arch.mtrr_state.deftype;
 	else if (msr == MSR_MTRRfix64K_00000)
 		*pdata = p[0];
 	else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
@@ -255,14 +271,14 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
 	int i, num_var_ranges = KVM_NR_VAR_MTRR;
 
 	/* MTRR is completely disabled, use UC for all of physical memory. */
-	if (!(mtrr_state->enabled & 0x2))
+	if (!mtrr_is_enabled(mtrr_state))
 		return MTRR_TYPE_UNCACHABLE;
 
 	/* Make end inclusive end, instead of exclusive */
 	end--;
 
 	/* Look in fixed ranges. Just return the type as per start */
-	if ((mtrr_state->enabled & 0x1) && (start < 0x100000)) {
+	if (fixed_mtrr_is_enabled(mtrr_state) && (start < 0x100000)) {
 		int idx;
 
 		if (start < 0x80000) {
@@ -330,7 +346,7 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
 	if (prev_match != 0xFF)
 		return prev_match;
 
-	return mtrr_state->def_type;
+	return mtrr_default_type(mtrr_state);
 }
 
 u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
-- 
2.1.0


  parent reply	other threads:[~2015-06-15  9:00 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-15  8:55 [PATCH v2 00/15] vMTRR bugfix and optimization Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 01/15] KVM: x86: fix CR0.CD virtualization Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 02/15] KVM: x86: move MTRR related code to a separate file Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 03/15] KVM: MTRR: handle MSR_MTRRcap in kvm_mtrr_get_msr Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 04/15] KVM: MTRR: remove mtrr_state.have_fixed Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 05/15] KVM: MTRR: exactly define the size of variable MTRRs Xiao Guangrong
2015-06-15  8:55 ` Xiao Guangrong [this message]
2015-06-15  8:55 ` [PATCH v2 07/15] KVM: MTRR: do not split 64 bits MSR content Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 08/15] KVM: MTRR: improve kvm_mtrr_get_guest_memory_type Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 09/15] KVM: MTRR: introduce fixed_mtrr_segment table Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 10/15] KVM: MTRR: introduce var_mtrr_range Xiao Guangrong
2015-06-17 15:38   ` Paolo Bonzini
2015-06-15  8:55 ` [PATCH v2 11/15] KVM: MTRR: sort variable MTRRs Xiao Guangrong
2015-06-17 15:40   ` Paolo Bonzini
2015-06-17 16:11   ` Paolo Bonzini
2015-06-22 11:24     ` Paolo Bonzini
2015-06-22 11:24       ` Paolo Bonzini
2015-06-23  2:29       ` Xiao Guangrong
2015-06-23  2:29         ` Xiao Guangrong
2015-06-23  8:00         ` Paolo Bonzini
2015-06-23  8:27           ` Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 12/15] KVM: MTRR: introduce fixed_mtrr_addr_* functions Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 13/15] KVM: MTRR: introduce mtrr_for_each_mem_type Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 14/15] KVM: MTRR: simplify kvm_mtrr_get_guest_memory_type Xiao Guangrong
2015-06-15  8:55 ` [PATCH v2 15/15] KVM: MTRR: do not map huage page for non-consistent range Xiao Guangrong

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=1434358535-19923-7-git-send-email-guangrong.xiao@linux.intel.com \
    --to=guangrong.xiao@linux.intel.com \
    --cc=dmatlack@google.com \
    --cc=gleb@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.