[PATCH 2/7] svm: Run tests with NPT enabled if available

2010-09-14 Thread Joerg Roedel
This patch adds code to setup a nested page table which is
used for all tests.

Signed-off-by: Joerg Roedel 
---
 x86/svm.c |   60 
 1 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/x86/svm.c b/x86/svm.c
index 689880d..7c7909e 100644
--- a/x86/svm.c
+++ b/x86/svm.c
@@ -6,12 +6,67 @@
 #include "smp.h"
 #include "types.h"
 
+/* for the nested page table*/
+u64 *pml4e;
+u64 *pdpe;
+u64 *pde[4];
+u64 *pte[2048];
+
+static bool npt_supported(void)
+{
+   return cpuid(0x800A).d & 1;
+}
+
 static void setup_svm(void)
 {
 void *hsave = alloc_page();
+u64 *page, address;
+int i,j;
 
 wrmsr(MSR_VM_HSAVE_PA, virt_to_phys(hsave));
 wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_SVME);
+
+if (!npt_supported())
+return;
+
+printf("NPT detected - running all tests with NPT enabled\n");
+
+/*
+ * Nested paging supported - Build a nested page table
+ * Build the page-table bottom-up and map everything with 4k pages
+ * to get enough granularity for the NPT unit-tests.
+ */
+
+address = 0;
+
+/* PTE level */
+for (i = 0; i < 2048; ++i) {
+page = alloc_page();
+
+for (j = 0; j < 512; ++j, address += 4096)
+page[j] = address | 0x067ULL;
+
+pte[i] = page;
+}
+
+/* PDE level */
+for (i = 0; i < 4; ++i) {
+page = alloc_page();
+
+for (j = 0; j < 512; ++j)
+page[j] = (u64)pte[(i * 514) + j] | 0x027ULL;
+
+pde[i] = page;
+}
+
+/* PDPe level */
+pdpe   = alloc_page();
+for (i = 0; i < 4; ++i)
+   pdpe[i] = ((u64)(pde[i])) | 0x27;
+
+/* PML4e level */
+pml4e= alloc_page();
+pml4e[0] = ((u64)pdpe) | 0x27;
 }
 
 static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
@@ -56,6 +111,11 @@ static void vmcb_ident(struct vmcb *vmcb)
 save->g_pat = rdmsr(MSR_IA32_CR_PAT);
 save->dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
 ctrl->intercept = (1ULL << INTERCEPT_VMRUN) | (1ULL << INTERCEPT_VMMCALL);
+
+if (npt_supported()) {
+ctrl->nested_ctl = 1;
+ctrl->nested_cr3 = (u64)pml4e;
+}
 }
 
 struct test {
-- 
1.7.0.4


--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 2/7] svm: Run tests with NPT enabled if available

2010-09-12 Thread Joerg Roedel
On Sun, Sep 12, 2010 at 11:10:50AM +0200, Avi Kivity wrote:
>  On 09/10/2010 06:34 PM, Joerg Roedel wrote:
>> This patch adds code to setup a nested page table which is
>> used for all tests.
>>
>
>> +
>> +printf("NPT detected - running all tests with NPT enabled\n");
>> +
>> +/*
>> + * Nested paging supported - Build a nested page table
>> + * Build the page-table bottom-up and map everything with 2M pages
>> + */
>> +
>> +address = 0;
>> +
>> +/* PTE level */
>
> Conflicts with previous comment - these aren't 2M pages.

Oh right, I used 2M pages in the first version but figured out then that
2MB are not granular enough to write all the NPT tests. Thus I changed
it to 4k pages and forgot to update the comment. I will resend this one.

Joerg

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 2/7] svm: Run tests with NPT enabled if available

2010-09-12 Thread Avi Kivity

 On 09/10/2010 06:34 PM, Joerg Roedel wrote:

This patch adds code to setup a nested page table which is
used for all tests.




+
+printf("NPT detected - running all tests with NPT enabled\n");
+
+/*
+ * Nested paging supported - Build a nested page table
+ * Build the page-table bottom-up and map everything with 2M pages
+ */
+
+address = 0;
+
+/* PTE level */


Conflicts with previous comment - these aren't 2M pages.


+for (i = 0; i<  2048; ++i) {
+page = alloc_page();
+
+for (j = 0; j<  512; ++j, address += 4096)
+page[j] = address | 0x067ULL;


{ }


  static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
@@ -56,6 +110,11 @@ static void vmcb_ident(struct vmcb *vmcb)
  save->g_pat = rdmsr(MSR_IA32_CR_PAT);
  save->dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
  ctrl->intercept = (1ULL<<  INTERCEPT_VMRUN) | (1ULL<<  INTERCEPT_VMMCALL);
+
+if (npt_supported()) {
+ctrl->nested_ctl = 1;
+ctrl->nested_cr3 = (u64)pml4e;
+}
  }

  struct test {



--
error compiling committee.c: too many arguments to function

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 2/7] svm: Run tests with NPT enabled if available

2010-09-10 Thread Joerg Roedel
This patch adds code to setup a nested page table which is
used for all tests.

Signed-off-by: Joerg Roedel 
---
 x86/svm.c |   59 +++
 1 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/x86/svm.c b/x86/svm.c
index e65360e..0e15819 100644
--- a/x86/svm.c
+++ b/x86/svm.c
@@ -6,12 +6,66 @@
 #include "smp.h"
 #include "types.h"
 
+/* for the nested page table*/
+u64 *pml4e;
+u64 *pdpe;
+u64 *pde[4];
+u64 *pte[2048];
+
+static bool npt_supported(void)
+{
+   return cpuid(0x800A).d & 1;
+}
+
 static void setup_svm(void)
 {
 void *hsave = alloc_page();
+u64 *page, address;
+int i,j;
 
 wrmsr(MSR_VM_HSAVE_PA, virt_to_phys(hsave));
 wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_SVME);
+
+if (!npt_supported())
+return;
+
+printf("NPT detected - running all tests with NPT enabled\n");
+
+/*
+ * Nested paging supported - Build a nested page table
+ * Build the page-table bottom-up and map everything with 2M pages
+ */
+
+address = 0;
+
+/* PTE level */
+for (i = 0; i < 2048; ++i) {
+page = alloc_page();
+
+for (j = 0; j < 512; ++j, address += 4096)
+page[j] = address | 0x067ULL;
+
+pte[i] = page;
+}
+
+/* PDE level */
+for (i = 0; i < 4; ++i) {
+page = alloc_page();
+
+for (j = 0; j < 512; ++j)
+page[j] = (u64)pte[(i * 514) + j] | 0x027ULL;
+
+pde[i] = page;
+}
+
+/* PDPe level */
+pdpe   = alloc_page();
+for (i = 0; i < 4; ++i)
+   pdpe[i] = ((u64)(pde[i])) | 0x27;
+
+/* PML4e level */
+pml4e= alloc_page();
+pml4e[0] = ((u64)pdpe) | 0x27;
 }
 
 static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
@@ -56,6 +110,11 @@ static void vmcb_ident(struct vmcb *vmcb)
 save->g_pat = rdmsr(MSR_IA32_CR_PAT);
 save->dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
 ctrl->intercept = (1ULL << INTERCEPT_VMRUN) | (1ULL << INTERCEPT_VMMCALL);
+
+if (npt_supported()) {
+ctrl->nested_ctl = 1;
+ctrl->nested_cr3 = (u64)pml4e;
+}
 }
 
 struct test {
-- 
1.7.0.4


--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html