Skip to contentSkip to conversation

Move idt.c into lib code.

PATCH In review Gleb Natapov 2 participants 6 messages Dec 14, 2010 – Dec 21, 2010 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Move idt.c into lib code.

Make it compilable in 32 and 64 bit mode.

---
 config-x86-common.mak |    7 +-
 lib/x86/idt.c         |   32 ++++-------
 lib/x86/idt.h         |    1 +
 x86/idt.c             |  148 -------------------------------------------------
 4 files changed, 16 insertions(+), 172 deletions(-)
 delete mode 100644 x86/idt.c
config-x86-common.mak +4 -3
index c5508b3..2269c4a 100644
@@ -11,6 +11,7 @@ cflatobjs += \
 cflatobjs += lib/x86/fwcfg.o
 cflatobjs += lib/x86/apic.o
 cflatobjs += lib/x86/atomic.o
+cflatobjs += lib/x86/idt.o
 
 $(libcflat): LDFLAGS += -nostdlib
 $(libcflat): CFLAGS += -ffreestanding -I lib
@@ -50,7 +51,7 @@ $(TEST_DIR)/vmexit.elf: $(cstart.o) $(TEST_DIR)/vmexit.o
 $(TEST_DIR)/smptest.elf: $(cstart.o) $(TEST_DIR)/smptest.o
 
 $(TEST_DIR)/emulator.elf: $(cstart.o) $(TEST_DIR)/emulator.o \
-			   $(TEST_DIR)/vm.o $(TEST_DIR)/idt.o
+			   $(TEST_DIR)/vm.o
 
 $(TEST_DIR)/port80.elf: $(cstart.o) $(TEST_DIR)/port80.o
 
@@ -65,9 +66,9 @@ $(TEST_DIR)/realmode.o: bits = 32
 
 $(TEST_DIR)/msr.elf: $(cstart.o) $(TEST_DIR)/msr.o
 
-$(TEST_DIR)/idt_test.elf: $(cstart.o) $(TEST_DIR)/idt.o $(TEST_DIR)/idt_test.o
+$(TEST_DIR)/idt_test.elf: $(cstart.o) $(TEST_DIR)/idt_test.o
 
-$(TEST_DIR)/xsave.elf: $(cstart.o) $(TEST_DIR)/idt.o $(TEST_DIR)/xsave.o
+$(TEST_DIR)/xsave.elf: $(cstart.o) $(TEST_DIR)/xsave.o
 
 $(TEST_DIR)/rmap_chain.elf: $(cstart.o) $(TEST_DIR)/rmap_chain.o \
 			     $(TEST_DIR)/vm.o
lib/x86/idt.c +11 -21
index ed2f4b0..b3e47d4 100644
@@ -1,5 +1,6 @@
 #include "idt.h"
 #include "libcflat.h"
+#include "processor.h"
 
 typedef struct {
     unsigned short offset0;
@@ -19,30 +20,19 @@ typedef struct {
 
 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)
+void load_lidt(idt_entry_t *idt, int nentries)
 {
-    descriptor_table_t dt;
+    struct descriptor_table_ptr dt;
 
     dt.limit = nentries * sizeof(*idt) - 1;
-    dt.linear_addr = (unsigned long)idt;
+    dt.base = (unsigned long)idt;
+    lidt(&dt);
     asm volatile ("lidt %0" : : "m"(dt));
 }
 
-unsigned short read_cs()
-{
-    unsigned short r;
-
-    asm volatile ("mov %%cs, %0" : "=r"(r));
-    return r;
-}
-
-void set_idt_entry(idt_entry_t *e, void *addr, int dpl)
+void set_idt_entry(int vec, void *addr, int dpl)
 {
+    idt_entry_t *e = &idt[vec];
     memset(e, 0, sizeof *e);
     e->offset0 = (unsigned long)addr;
     e->selector = read_cs();
@@ -146,10 +136,10 @@ void setup_idt(void)
 {
     extern char ud_fault, gp_fault, de_fault;
 
-    lidt(idt, 256);
-    set_idt_entry(&idt[0], &de_fault, 0);
-    set_idt_entry(&idt[6], &ud_fault, 0);
-    set_idt_entry(&idt[13], &gp_fault, 0);
+    load_lidt(idt, 256);
+    set_idt_entry(0, &de_fault, 0);
+    set_idt_entry(6, &ud_fault, 0);
+    set_idt_entry(13, &gp_fault, 0);
 }
 
 unsigned exception_vector(void)
lib/x86/idt.h +1 -0
index 6babcb4..81b8944 100644
@@ -15,5 +15,6 @@ void setup_idt(void);
 
 unsigned exception_vector(void);
 unsigned exception_error_code(void);
+void set_idt_entry(int vec, void *addr, int dpl);
 
 #endif
x86/idt.c deleted +0 -148

148 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 2/4 Make access.c use library functions.

access.c has functions that are provided by library code. Remove them and use library functions instead.

---
 x86/access.c |   92 +++------------------------------------------------------
 1 files changed, 5 insertions(+), 87 deletions(-)
x86/access.c +5 -87
index 067565b..df943d9 100644
@@ -1,5 +1,7 @@
 
 #include "libcflat.h"
+#include "idt.h"
+#include "processor.h"
 
 #define smp_id() 0
 
@@ -98,34 +100,6 @@ static inline void *va(pt_element_t phys)
     return (void *)phys;
 }
 
-static unsigned long read_cr0()
-{
-    unsigned long cr0;
-
-    asm volatile ("mov %%cr0, %0" : "=r"(cr0));
-
-    return cr0;
-}
-
-static void write_cr0(unsigned long cr0)
-{
-    asm volatile ("mov %0, %%cr0" : : "r"(cr0));
-}
-
-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;
-
 typedef struct {
     pt_element_t pt_pool;
     unsigned pt_pool_size;
@@ -143,7 +117,6 @@ typedef struct {
     pt_element_t ignore_pde;
     int expected_fault;
     unsigned expected_error;
-    idt_entry_t idt[256];
 } ac_test_t;
 
 typedef struct {
@@ -154,51 +127,6 @@ typedef struct {
 
 static void ac_test_show(ac_test_t *at);
 
-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;
-}
-
-unsigned long long rdmsr(unsigned index)
-{
-    unsigned a, d;
-
-    asm volatile("rdmsr" : "=a"(a), "=d"(d) : "c"(index));
-    return ((unsigned long long)d << 32) | a;
-}
-
-void wrmsr(unsigned index, unsigned long long val)
-{
-    unsigned a = val, d = val >> 32;
-
-    asm volatile("wrmsr" : : "a"(a), "d"(d), "c"(index));
-}
-
-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 set_cr0_wp(int wp)
 {
     unsigned long cr0 = read_cr0();
@@ -222,13 +150,11 @@ void set_efer_nx(int nx)
 
 static void ac_env_int(ac_pool_t *pool)
 {
-    static idt_entry_t idt[256];
+    setup_idt();
 
-    memset(idt, 0, sizeof(idt));
-    lidt(idt, 256);
     extern char page_fault, kernel_entry;
-    set_idt_entry(&idt[14], &page_fault, 0);
-    set_idt_entry(&idt[0x20], &kernel_entry, 3);
+    set_idt_entry(14, &page_fault, 0);
+    set_idt_entry(0x20, &kernel_entry, 3);
 
     pool->pt_pool = 33 * 1024 * 1024;
     pool->pt_pool_size = 120 * 1024 * 1024 - pool->pt_pool;
@@ -273,14 +199,6 @@ int ac_test_bump(ac_test_t *at)
     return ret;
 }
 
-unsigned long read_cr3()
-{
-    unsigned long cr3;
-
-    asm volatile ("mov %%cr3, %0" : "=r"(cr3));
-    return cr3;
-}
-
 void invlpg(void *addr)
 {
     asm volatile ("invlpg (%0)" : : "r"(addr));
Signature · 1 line
1.7.2.3
#2 lore raw

PATCH 3/4 Remove duplicated idt code from apic test.

In reply to Gleb Natapov

Use library idt code instead.

---
 x86/apic.c |   49 +++++++++++--------------------------------------
 1 files changed, 11 insertions(+), 38 deletions(-)
x86/apic.c +11 -38
index 2207040..6d06f9f 100644
@@ -2,22 +2,7 @@
 #include "apic.h"
 #include "vm.h"
 #include "smp.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;
-#ifdef __x86_64__
-    unsigned offset2;
-    unsigned reserved;
-#endif
-} idt_entry_t;
+#include "idt.h"
 
 typedef struct {
     ulong regs[sizeof(ulong)*2];
@@ -90,8 +75,6 @@ asm (
 #endif
     );
 
-static idt_entry_t *idt = 0;
-
 static int g_fail;
 static int g_tests;
 
@@ -128,22 +111,12 @@ void test_enable_x2apic(void)
     }
 }
 
-static void set_idt_entry(unsigned vec, void (*func)(isr_regs_t *regs))
+static void handle_irq(unsigned vec, void (*func)(isr_regs_t *regs))
 {
     u8 *thunk = vmalloc(50);
-    ulong ptr = (ulong)thunk;
-    idt_entry_t ent = {
-        .offset0 = ptr,
-        .selector = read_cs(),
-        .ist = 0,
-        .type = 14,
-        .dpl = 0,
-        .p = 1,
-        .offset1 = ptr >> 16,
-#ifdef __x86_64__
-        .offset2 = ptr >> 32,
-#endif
-    };
+
+    set_idt_entry(vec, thunk, 0);
+
 #ifdef __x86_64__
     /* sub $8, %rsp */
     *thunk++ = 0x48; *thunk++ = 0x83; *thunk++ = 0xec; *thunk++ = 0x08;
@@ -164,7 +137,6 @@ static void set_idt_entry(unsigned vec, void (*func)(isr_regs_t *regs))
     *thunk ++ = 0xe9;
     *(u32 *)thunk = (ulong)isr_entry_point - (ulong)(thunk + 4);
 #endif
-    idt[vec] = ent;
 }
 
 static void irq_disable(void)
@@ -194,7 +166,7 @@ static void test_self_ipi(void)
 {
     int vec = 0xf1;
 
-    set_idt_entry(vec, self_ipi_isr);
+    handle_irq(vec, self_ipi_isr);
     irq_enable();
     apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL | APIC_DM_FIXED | vec,
                    0);
@@ -234,7 +206,7 @@ static void ioapic_isr_77(isr_regs_t *regs)
 
 static void test_ioapic_intr(void)
 {
-    set_idt_entry(0x77, ioapic_isr_77);
+    handle_irq(0x77, ioapic_isr_77);
     set_ioapic_redir(0x10, 0x77);
     toggle_irq_line(0x10);
     asm volatile ("nop");
@@ -262,8 +234,8 @@ static void ioapic_isr_66(isr_regs_t *regs)
 
 static void test_ioapic_simultaneous(void)
 {
-    set_idt_entry(0x78, ioapic_isr_78);
-    set_idt_entry(0x66, ioapic_isr_66);
+    handle_irq(0x78, ioapic_isr_78);
+    handle_irq(0x66, ioapic_isr_66);
     set_ioapic_redir(0x10, 0x78);
     set_ioapic_redir(0x11, 0x66);
     irq_disable();
@@ -323,7 +295,7 @@ static void test_sti_nmi(void)
 	return;
     }
 
-    set_idt_entry(2, nmi_handler);
+    handle_irq(2, nmi_handler);
     on_cpu(1, update_cr3, (void *)read_cr3());
 
     sti_loop_active = 1;
@@ -343,6 +315,7 @@ int main()
 {
     setup_vm();
     smp_init();
+    setup_idt();
 
     test_lapic_existence();
 
Signature · 1 line
1.7.2.3
#3 lore raw

PATCH 4/4 Remove unused function from apic test.

In reply to Gleb Natapov
---
 x86/apic.c |    5 -----
 1 files changed, 0 insertions(+), 5 deletions(-)
x86/apic.c +0 -5
index 6d06f9f..bcb9fc1 100644
@@ -78,11 +78,6 @@ asm (
 static int g_fail;
 static int g_tests;
 
-static void outb(unsigned char data, unsigned short port)
-{
-    asm volatile ("out %0, %1" : : "a"(data), "d"(port));
-}
-
 static void report(const char *msg, int pass)
 {
     ++g_tests;
Signature · 1 line
1.7.2.3
#4 lore raw
In reply to Gleb Natapov

lib/x86/idt.c

Quoted 4 lines · Gleb Natapov
lib/x86/idt.c +0 -0
index ed2f4b0..b3e47d4 100644

There's no such file on current kvm-unit-test repo. What did you generate against?

#5 lore raw
Quoted 8 lines · Marcelo Tosatti

lib/x86/idt.c

Quoted 4 lines · Gleb Natapov
lib/x86/idt.c +0 -0
index ed2f4b0..b3e47d4 100644

There's no such file on current kvm-unit-test repo. What did you generate against?

The patch should have moved it from x86/ to lib/x86 but something went wrong with my commit. Will check.

--
Gleb.