public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: prasadjoshi.linux@gmail.com
To: prasadjoshi.linux@gmail.com
Cc: kvm@vger.kernel.org, mtosatti@redhat.com
Subject: [PATCH kvm-unittest] x86/svm: add svm lock tests
Date: Sun, 21 Apr 2013 14:13:38 +0530	[thread overview]
Message-ID: <1366533818-2187-1-git-send-email-prasadjoshi.linux@gmail.com> (raw)

From: Prasad Joshi <prasadjoshi.linux@gmail.com>

Signed-off-by: Prasad Joshi <prasadjoshi.linux@gmail.com>
---
 lib/x86/msr.h |   10 ++++++++++
 x86/svm.c     |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)

diff --git a/lib/x86/msr.h b/lib/x86/msr.h
index 509a421..0e602d0 100644
--- a/lib/x86/msr.h
+++ b/lib/x86/msr.h
@@ -402,5 +402,15 @@
 #define MSR_VM_CR                       0xc0010114
 #define MSR_VM_IGNNE                    0xc0010115
 #define MSR_VM_HSAVE_PA                 0xc0010117
+#define MSR_SVM_KEY			0xc0010118
+
+/* VM CR bits: */
+#define _VM_CR_DPD		0 /* disable internal debugging */
+#define _VM_CR_R_INIT		1 /* non-interrupted INIT causes #SX exception */
+#define _VM_CR_DIS_A20M		2 /* disable A20 masking */
+#define _VM_CR_LOCK		3 /* svm lock */
+#define _VM_CR_SVMDIS		4 /* disable svm */
+
+#define VM_CR_LOCK		(1 << _VM_CR_LOCK)
 
 #endif /* _ASM_X86_MSR_INDEX_H */
diff --git a/x86/svm.c b/x86/svm.c
index d51e7ec..6d39ff1 100644
--- a/x86/svm.c
+++ b/x86/svm.c
@@ -241,6 +241,63 @@ static bool null_check(struct test *test)
     return test->vmcb->control.exit_code == SVM_EXIT_VMMCALL;
 }
 
+static bool svm_lock_supported(void)
+{
+	/*
+	 * Support for SVM-Lock is indicated by EDX bit 2 as returned by CPUID
+	 * function 8000_000Ah.
+	 */
+	return (cpuid(0x8000000A).d & 4);
+}
+
+static void svm_lock_test(struct test *test)
+{
+	const int no_tests = 6;
+	const u64 svm_key  = 0x1234567890;
+
+	test->scratch = no_tests;
+
+	/* reading SVM_KEY must always return 0 */
+	if (!rdmsr(MSR_SVM_KEY))
+		test->scratch--;
+
+	/* set the key */
+	wrmsr(MSR_SVM_KEY, svm_key);
+
+	/* reading SVM_KEY must always return 0 */
+	if (!rdmsr(MSR_SVM_KEY))
+		test->scratch--;
+
+	/* set SVM lock */
+	wrmsr(MSR_VM_CR, rdmsr(MSR_VM_CR) | VM_CR_LOCK);
+	if (rdmsr(MSR_VM_CR) & VM_CR_LOCK) {
+		/* lock is set */
+		test->scratch--;
+	}
+
+	/* compare with 0 key */
+	wrmsr(MSR_SVM_KEY, 0);
+	if (rdmsr(MSR_VM_CR) & VM_CR_LOCK)
+		test->scratch--;
+
+	/* compare with incorrect key */
+	wrmsr(MSR_SVM_KEY, 0x12);
+	if (rdmsr(MSR_VM_CR) & VM_CR_LOCK)
+		test->scratch--;
+
+	/* compare with correct key */
+	wrmsr(MSR_SVM_KEY, svm_key);
+	if (!(rdmsr(MSR_VM_CR) & VM_CR_LOCK)) {
+		/* lock no longer set */
+		test->scratch--;
+	}
+}
+
+static bool svm_lock_check(struct test *test)
+{
+	return !test->scratch;
+}
+
 static void prepare_no_vmrun_int(struct test *test)
 {
     test->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMRUN);
@@ -746,6 +803,8 @@ static bool lat_svm_insn_check(struct test *test)
 static struct test tests[] = {
     { "null", default_supported, default_prepare, null_test,
       default_finished, null_check },
+    { "SVM Lock", svm_lock_supported, default_prepare, svm_lock_test,
+      default_finished, svm_lock_check },
     { "vmrun", default_supported, default_prepare, test_vmrun,
        default_finished, check_vmrun },
     { "vmrun intercept check", default_supported, prepare_no_vmrun_int,
-- 
1.7.10.4


                 reply	other threads:[~2013-04-21  8:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1366533818-2187-1-git-send-email-prasadjoshi.linux@gmail.com \
    --to=prasadjoshi.linux@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox