From: Sheng Yang <sheng@linux.intel.com>
To: Avi Kivity <avi@redhat.com>, Marcelo Tosatti <mtosatti@redhat.com>
Cc: kvm@vger.kernel.org, Sheng Yang <sheng@linux.intel.com>
Subject: [PATCH 1/2] test: Add IDT framework
Date: Thu, 10 Jun 2010 11:31:42 +0800 [thread overview]
Message-ID: <1276140703-30046-1-git-send-email-sheng@linux.intel.com> (raw)
Signed-off-by: Sheng Yang <sheng@linux.intel.com>
---
kvm/test/config-x86-common.mak | 2 +
kvm/test/config-x86_64.mak | 2 +-
kvm/test/lib/x86/idt.h | 19 +++++++++
kvm/test/x86/idt.c | 85 ++++++++++++++++++++++++++++++++++++++++
kvm/test/x86/idt_test.c | 48 ++++++++++++++++++++++
5 files changed, 155 insertions(+), 1 deletions(-)
create mode 100644 kvm/test/lib/x86/idt.h
create mode 100644 kvm/test/x86/idt.c
create mode 100644 kvm/test/x86/idt_test.c
diff --git a/kvm/test/config-x86-common.mak b/kvm/test/config-x86-common.mak
index c97de52..800b635 100644
--- a/kvm/test/config-x86-common.mak
+++ b/kvm/test/config-x86-common.mak
@@ -59,6 +59,8 @@ $(TEST_DIR)/realmode.o: bits = 32
$(TEST_DIR)/msr.flat: $(cstart.o) $(TEST_DIR)/msr.o
+$(TEST_DIR)/idt_test.flat: $(cstart.o) $(TEST_DIR)/idt.o $(TEST_DIR)/idt_test.o
+
arch_clean:
$(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat \
$(TEST_DIR)/.*.d $(TEST_DIR)/lib/.*.d $(TEST_DIR)/lib/*.o
diff --git a/kvm/test/config-x86_64.mak b/kvm/test/config-x86_64.mak
index d8fd2b5..f9cd121 100644
--- a/kvm/test/config-x86_64.mak
+++ b/kvm/test/config-x86_64.mak
@@ -5,6 +5,6 @@ ldarch = elf64-x86-64
CFLAGS += -D__x86_64__
tests = $(TEST_DIR)/access.flat $(TEST_DIR)/apic.flat \
- $(TEST_DIR)/emulator.flat
+ $(TEST_DIR)/emulator.flat $(TEST_DIR)/idt_test.flat
include config-x86-common.mak
diff --git a/kvm/test/lib/x86/idt.h b/kvm/test/lib/x86/idt.h
new file mode 100644
index 0000000..f449f01
--- /dev/null
+++ b/kvm/test/lib/x86/idt.h
@@ -0,0 +1,19 @@
+#ifndef __IDT_TEST__
+#define __IDT_TEST__
+
+void setup_idt(void);
+
+#define EXCEPTION_REGION_BEGIN(r, lb) \
+ asm volatile("pushq $"lb"\n\t" \
+ "mov $2, %0\n\t" \
+ : "=r"(r))
+#define EXCEPTION_REGION_END(r, lb) \
+ asm volatile("popq %%rdx\n\t" \
+ "mov $0, %0\n\t" \
+ lb":\n\t": \
+ "=r"(r) :: "%rdx")
+
+#define UD_VECTOR 6
+#define GP_VECTOR 13
+
+#endif
diff --git a/kvm/test/x86/idt.c b/kvm/test/x86/idt.c
new file mode 100644
index 0000000..e3172cb
--- /dev/null
+++ b/kvm/test/x86/idt.c
@@ -0,0 +1,85 @@
+#include "idt.h"
+
+typedef struct {
+ unsigned short offset0;
+ unsigned short selector;
+ unsigned short ist : 3;
+ unsigned short : 5;
+ unsigned short type : 4;
+ unsigned short : 1;
+ unsigned short dpl : 2;
+ unsigned short p : 1;
+ unsigned short offset1;
+ unsigned offset2;
+ unsigned reserved;
+} idt_entry_t;
+
+static idt_entry_t idt[256];
+
+typedef struct {
+ unsigned short limit;
+ unsigned long linear_addr;
+} __attribute__((packed)) descriptor_table_t;
+
+void lidt(idt_entry_t *idt, int nentries)
+{
+ descriptor_table_t dt;
+
+ dt.limit = nentries * sizeof(*idt) - 1;
+ dt.linear_addr = (unsigned long)idt;
+ asm volatile ("lidt %0" : : "m"(dt));
+}
+
+unsigned short read_cs()
+{
+ unsigned short r;
+
+ asm volatile ("mov %%cs, %0" : "=r"(r));
+ return r;
+}
+
+void memset(void *a, unsigned char v, int n)
+{
+ unsigned char *x = a;
+
+ while (n--)
+ *x++ = v;
+}
+
+void set_idt_entry(idt_entry_t *e, void *addr, int dpl)
+{
+ memset(e, 0, sizeof *e);
+ e->offset0 = (unsigned long)addr;
+ e->selector = read_cs();
+ e->ist = 0;
+ e->type = 14;
+ e->dpl = dpl;
+ e->p = 1;
+ e->offset1 = (unsigned long)addr >> 16;
+ e->offset2 = (unsigned long)addr >> 32;
+}
+
+void setup_idt(void)
+{
+ asm volatile (".section .text.ud \n\t"
+ "ud_fault: \n\t"
+ "mov 40(%%rsp), %%rax\n\t"
+ "mov %%rax, (%%rsp)\n\t"
+ "mov $6, %%rax\n\t"
+ "iretq \n\t"
+ ".section .text":);
+
+ asm volatile (".section .text.gp \n\t"
+ "gp_fault: \n\t"
+ "pop %%rax \n\t"
+ "mov 40(%%rsp), %%rax\n\t"
+ "mov %%rax, (%%rsp)\n\t"
+ "mov $13, %%rax\n\t"
+ "iretq \n\t"
+ ".section .text":);
+ extern char ud_fault, gp_fault;
+
+ lidt(idt, 256);
+ set_idt_entry(&idt[6], &ud_fault, 0);
+ set_idt_entry(&idt[13], &gp_fault, 0);
+}
diff --git a/kvm/test/x86/idt_test.c b/kvm/test/x86/idt_test.c
new file mode 100644
index 0000000..08f0da8
--- /dev/null
+++ b/kvm/test/x86/idt_test.c
@@ -0,0 +1,48 @@
+#include "libcflat.h"
+#include "idt.h"
+
+int test_ud2(void)
+{
+ int r;
+ EXCEPTION_REGION_BEGIN(r, "fixed_ud");
+ asm volatile("ud2;\n\t");
+ EXCEPTION_REGION_END(r, "fixed_ud");
+ return r;
+}
+
+int test_gp(void)
+{
+ int r;
+ EXCEPTION_REGION_BEGIN(r, "fixed_gp");
+ asm volatile("mov $0xffffffff, %%rax\n\t"
+ "mov %%rax, %%cr4\n\t":);
+ EXCEPTION_REGION_END(r, "fixed_gp");
+ return r;
+}
+
+int test(void)
+{
+ int r;
+
+ setup_idt();
+ printf("GP testing: ");
+ r = test_gp();
+ if (r == GP_VECTOR)
+ printf("Pass!\n");
+ else
+ printf("Fail!\n");
+ printf("UD testing: ");
+ r = test_ud2();
+ if (r == UD_VECTOR)
+ printf("Pass!\n");
+ else
+ printf("Fail!\n");
+ return 0;
+}
+
+int main(void)
+{
+ printf("start testing\n");
+ test();
+ return 0;
+}
--
1.7.0.1
next reply other threads:[~2010-06-10 3:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-10 3:31 Sheng Yang [this message]
2010-06-10 3:31 ` [PATCH 2/2] test: Add XSAVE unit test Sheng Yang
2010-06-10 9:49 ` [PATCH 1/2] test: Add IDT framework Avi Kivity
2010-06-10 9:55 ` Sheng Yang
2010-06-10 10:55 ` Avi Kivity
2010-06-10 13:32 ` 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=1276140703-30046-1-git-send-email-sheng@linux.intel.com \
--to=sheng@linux.intel.com \
--cc=avi@redhat.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