task switch and event injection tests
cover task switch and event injection tests
Long time ago I wrote a mini guest to test various interesting task switch and even injection scenarios. This is the port of the test cases to unit test framework.
Gleb Natapov (16):
Move idt.c into lib code.
Make access.c use library functions.
Remove duplicated idt code from apic test. Remove unused function from apic test. Rename idt.[ch] into desc.[ch]
Specify correct operand length for ltr and str. Move irq_(enable|disable) into library code. Add another task switch test.
Move vm.[ch] info library code.
Fix mmu on 32 bit.
Set WP bit in CR0 to make write protection work. Fix exception handling on i386 arch.
Move handle_irq() from apic test into library code. Add handle_exception() interface.
Move invlpg() into library code.
Add even injection test.
config-i386.mak | 3 +- config-x86-common.mak | 24 ++-- lib/x86/desc.c | 393 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/desc.h | 51 +++++++ lib/x86/idt.h | 19 --- lib/x86/isr.c | 99 +++++++++++++ lib/x86/isr.h | 14 ++ lib/x86/processor.h | 18 ++- lib/x86/vm.c | 256 ++++++++++++++++++++++++++++++++ lib/x86/vm.h | 47 ++++++ x86/access.c | 97 +------------ x86/apic.c | 156 +------------------- x86/emulator.c | 2 +- x86/eventinj.c | 372 ++++++++++++++++++++++++++++++++++++++++++++++ x86/idt.c | 148 ------------------- x86/idt_test.c | 2 +- x86/taskswitch2.c | 185 +++++++++++++++++++++++ x86/vm.c | 229 ---------------------------- x86/vm.h | 46 ------ x86/xsave.c | 2 +- 20 files changed, 1464 insertions(+), 699 deletions(-) create mode 100644 lib/x86/desc.c create mode 100644 lib/x86/desc.h delete mode 100644 lib/x86/idt.h create mode 100644 lib/x86/isr.c create mode 100644 lib/x86/isr.h create mode 100644 lib/x86/vm.c create mode 100644 lib/x86/vm.h create mode 100644 x86/eventinj.c delete mode 100644 x86/idt.c create mode 100644 x86/taskswitch2.c delete mode 100644 x86/vm.c delete mode 100644 x86/vm.h
Signature · 1 line
1.7.2.3
PATCH 1/16 Move idt.c into lib code.
Make it compilable in 32 and 64 bit mode.
--- config-x86-common.mak | 7 +- lib/x86/idt.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/idt.h | 1 + x86/idt.c | 148 --------------------------------------------- 4 files changed, 164 insertions(+), 151 deletions(-) create mode 100644 lib/x86/idt.c delete mode 100644 x86/idt.c
config-x86-common.mak +4 -3
@@ -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 new file +159 -0
159 lines not on this page · raw message
lib/x86/idt.h +1 -0
@@ -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
PATCH 2/16 Make access.c use library functions.
In reply to Gleb Natapovaccess.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
@@ -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; - - asmvolatile ("mov %%cr0, %0" :"=r" (cr0)); - -return cr0; -} - -static void write_cr0 (unsigned long cr0) -{ - asmvolatile ("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; - asmvolatile ("lidt %0" : :"m" (dt)); -} - -unsigned short read_cs () -{ -unsigned short r; - - asmvolatile ("mov %%cs, %0" :"=r" (r)); -return r; -} - -unsigned long long rdmsr (unsigned index) -{ -unsigned a, d; - - asmvolatile ("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 ; - - asmvolatile ("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; - - asmvolatile ("mov %%cr3, %0" :"=r" (cr3)); -return cr3; -} -void invlpg (void *addr) { asmvolatile ("invlpg (%0)" : :"r" (addr));
Signature · 1 line
1.7.2.3
PATCH 3/16 Remove duplicated idt code from apic test.
In reply to Gleb NatapovUse library idt code instead.
--- x86/apic.c | 49 +++++++++++-------------------------------------- 1 files changed, 11 insertions(+), 38 deletions(-)
x86/apic.c +11 -38
@@ -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 ); asmvolatile ("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
PATCH 4/16 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
@@ -78,11 +78,6 @@ asm (static int g_fail;static int g_tests; -static void outb (unsigned char data,unsigned short port) -{ - asmvolatile ("out %0, %1" : :"a" (data),"d" (port)); -} -static void report (const char *msg,int pass) { ++g_tests;
Signature · 1 line
1.7.2.3
PATCH 5/16 Rename idt.[ch] into desc.[ch]
In reply to Gleb NatapovAll descriptor related code will go there.
--- config-x86-common.mak | 2 +- lib/x86/desc.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/desc.h | 20 ++++++ lib/x86/idt.c | 159 ------------------------------------------------- lib/x86/idt.h | 20 ------ x86/access.c | 2 +- x86/apic.c | 2 +- x86/emulator.c | 2 +- x86/idt_test.c | 2 +- x86/xsave.c | 2 +- 10 files changed, 185 insertions(+), 185 deletions(-) create mode 100644 lib/x86/desc.c create mode 100644 lib/x86/desc.h delete mode 100644 lib/x86/idt.c delete mode 100644 lib/x86/idt.h
config-x86-common.mak +1 -1
@@ -11,7 +11,7 @@ cflatobjs += \ cflatobjs += lib/x86/fwcfg.o cflatobjs += lib/x86/apic.o cflatobjs += lib/x86/atomic.o -cflatobjs += lib/x86/idt.o +cflatobjs += lib/x86/desc.o $(libcflat): LDFLAGS += -nostdlib $(libcflat): CFLAGS += -ffreestanding -I lib
lib/x86/desc.c new file +159 -0
159 lines not on this page · raw message
lib/x86/desc.h new file +20 -0
@@ -0,0 +1,20 @@ +#ifndef __IDT_TEST__ +#define __IDT_TEST__ + +void setup_idt (void ); + +#define ASM_TRY (catch) \ + "movl $0, %%gs:4 \n\t" \ + ".pushsection .data.ex \n\t" \ + ".quad 1111f, " catch "\n\t" \ + ".popsection \n\t" \ + "1111:" + +#define UD_VECTOR 6 +#define GP_VECTOR 13 + +unsigned exception_vector (void ); +unsigned exception_error_code (void ); +void set_idt_entry (int vec,void *addr,int dpl); + +#endif
lib/x86/idt.c deleted +0 -159
159 lines not on this page · raw message
lib/x86/idt.h deleted +0 -20
@@ -1,20 +0,0 @@ -#ifndef __IDT_TEST__ -#define __IDT_TEST__ - -void setup_idt (void ); - -#define ASM_TRY (catch) \ - "movl $0, %%gs:4 \n\t" \ - ".pushsection .data.ex \n\t" \ - ".quad 1111f, " catch "\n\t" \ - ".popsection \n\t" \ - "1111:" - -#define UD_VECTOR 6 -#define GP_VECTOR 13 - -unsigned exception_vector (void ); -unsigned exception_error_code (void ); -void set_idt_entry (int vec,void *addr,int dpl); - -#endif
x86/access.c +1 -1
@@ -1,6 +1,6 @@#include "libcflat.h" -#include "idt.h" +#include "desc.h" #include "processor.h" #define smp_id () 0
x86/apic.c +1 -1
@@ -2,7 +2,7 @@#include "apic.h" #include "vm.h" #include "smp.h" -#include "idt.h" +#include "desc.h" typedef struct { ulong regs[sizeof(ulong)*2];
x86/emulator.c +1 -1
@@ -1,7 +1,7 @@#include "ioram.h" #include "vm.h" #include "libcflat.h" -#include "idt.h" +#include "desc.h" #define memset __builtin_memset#define TESTDEV_IO_PORT 0xe0
x86/idt_test.c +1 -1
@@ -1,5 +1,5 @@#include "libcflat.h" -#include "idt.h" +#include "desc.h" int test_ud2 (void ) {
x86/xsave.c +1 -1
@@ -1,5 +1,5 @@#include "libcflat.h" -#include "idt.h" +#include "desc.h" #ifdef __x86_64__#define uint64_t unsigned long
Signature · 1 line
1.7.2.3
Re: [PATCH unit-tests 05/16] Rename idt.[ch] into desc.[ch]
Quoted 2 lines · Gleb Natapov
All descriptor related code will go there.
In the future, please set diff.rename = true in your global git configuration so renames look nicer.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 6/16 Specify correct operand length for ltr and str.
In reply to Gleb Natapov--- lib/x86/processor.h | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
lib/x86/processor.h +2 -2
@@ -193,13 +193,13 @@ static inline u16 sldt(void)static inline void ltr (unsigned val) { - asmvolatile ("ltr %0" : :"rm" (val)); + asmvolatile ("ltr %w0" : :"rm" (val)); }static inline u16 str (void ) {u16 val; - asmvolatile ("str %0" :"=rm" (val)); + asmvolatile ("str %w0" :"=rm" (val));return val; }
Signature · 1 line
1.7.2.3
Re: [PATCH unit-tests 06/16] Specify correct operand length for ltr and str.
Quoted 10 lines · Gleb Natapov
Signed-off-by Gleb Natapov<[email protected]>--- lib/x86/processor.h | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)lib/x86/processor.h +0
-0
lib/x86/processor.h static inline u16 sldt(void) · line 197
Quoted 6 lines
lib/x86/processor.h +1
-1@@ -193,13 +193,13 @@ static inline u16 sldt(void)static inline void ltr (unsigned val) { - asmvolatile ("ltr %0" : :"rm" (val)); + asmvolatile ("ltr %w0" : :"rm" (val)); }
Is this really needed? And isn't this done better by declaring 'u16 val'?
lib/x86/processor.h static inline u16 sldt(void) · line 205
Quoted 9 lines
lib/x86/processor.h +1
-1static inline u16 str (void ) {u16 val; - asmvolatile ("str %0" :"=rm" (val)); + asmvolatile ("str %w0" :"=rm" (val));return val; }
This looks completely unneeded, since val is already a u16.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH unit-tests 06/16] Specify correct operand length for ltr and str.
Quoted 30 lines · Avi Kivity
Quoted 10 lines · Gleb Natapov
Signed-off-by Gleb Natapov<[email protected]>--- lib/x86/processor.h | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)lib/x86/processor.h +0
-0lib/x86/processor.h static inline u16 sldt(void) · line 197
Quoted 6 lines
lib/x86/processor.h +1
-1@@ -193,13 +193,13 @@ static inline u16 sldt(void)static inline void ltr (unsigned val) { - asmvolatile ("ltr %0" : :"rm" (val)); + asmvolatile ("ltr %w0" : :"rm" (val)); }Is this really needed? And isn't this done better by declaring 'u16 val'?
lib/x86/processor.h static inline u16 sldt(void) · line 205
Quoted 9 lines
lib/x86/processor.h +1
-1static inline u16 str (void ) {u16 val; - asmvolatile ("str %0" :"=rm" (val)); + asmvolatile ("str %w0" :"=rm" (val));return val; }
Quoted 2 lines
This looks completely unneeded, since val is already a u16.
IIRC I got errors from assembler. Will recheck.
--
Gleb.
Re: [PATCH unit-tests 06/16] Specify correct operand length for ltr and str.
Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Avi Kivity
Quoted 10 lines · Gleb Natapov
Signed-off-by Gleb Natapov<[email protected]>--- lib/x86/processor.h | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)lib/x86/processor.h +0
-0lib/x86/processor.h static inline u16 sldt(void) · line 197
Quoted 6 lines
lib/x86/processor.h +1
-1@@ -193,13 +193,13 @@ static inline u16 sldt(void)static inline void ltr (unsigned val) { - asmvolatile ("ltr %0" : :"rm" (val)); + asmvolatile ("ltr %w0" : :"rm" (val)); }Is this really needed? And isn't this done better by declaring 'u16 val'?
Quoted 9 lines
lib/x86/processor.h +1
-1static inline u16 str (void ) {u16 val; - asmvolatile ("str %0" :"=rm" (val)); + asmvolatile ("str %w0" :"=rm" (val));return val; }
Quoted 4 lines
Quoted 2 lines
This looks completely unneeded, since val is already a u16.
IIRC I got errors from assembler. Will recheck.
You are correct. The error was only about ltr. Changing unsigned to u16 fixes it.
--
Gleb.
Re: [PATCH unit-tests 06/16] Specify correct operand length for ltr and str.
Quoted 8 lines · Gleb Natapov
Quoted 5 lines
Quoted 3 lines
This looks completely unneeded, since val is already a u16.
IIRC I got errors from assembler. Will recheck.
You are correct. The error was only about ltr. Changing unsigned to u16 fixes it.
Ok, I'll fix it. Applied all the other patches.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 7/16 Move irq_(enable|disable) into library code.
In reply to Gleb Natapov--- lib/x86/processor.h | 10 ++++++++++ x86/apic.c | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-)
lib/x86/processor.h +10 -0
@@ -280,4 +280,14 @@ static inline void wrtsc(u64 tsc) asm volatile("wrmsr" : : "a"(a), "d"(d), "c"(0x10)); } +static inline void irq_disable (void ) +{ + asmvolatile ("cli" ); +} + +static inline void irq_enable (void ) +{ + asmvolatile ("sti" ); +} +#endif
x86/apic.c +0 -10
@@ -134,16 +134,6 @@ static void handle_irq(unsigned vec, void (*func)(isr_regs_t *regs))#endif } -static void irq_disable (void ) -{ - asmvolatile ("cli" ); -} - -static void irq_enable (void ) -{ - asmvolatile ("sti" ); -} -static void eoi (void ) {apic_write (APIC_EOI ,0 );
Signature · 1 line
1.7.2.3
PATCH 8/16 Add another task switch test.
In reply to Gleb NatapovIt has more test cases then existing one. These test cases were written when I worked on fixing task switch emulation code. Most of them check for previously existing issue.
--- config-i386.mak | 3 +- lib/x86/desc.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/desc.h | 13 ++++ x86/taskswitch2.c | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 372 insertions(+), 1 deletions(-) create mode 100644 x86/taskswitch2.c
config-i386.mak +2 -1
@@ -5,8 +5,9 @@ ldarch = elf32-i386 CFLAGS += -D__i386__ CFLAGS += -I $(KERNELDIR)/include -tests = $(TEST_DIR)/taskswitch.flat +tests = $(TEST_DIR)/taskswitch.flat $(TEST_DIR)/taskswitch2.flat include config-x86-common.mak $(TEST_DIR)/taskswitch.elf: $(cstart.o) $(TEST_DIR)/taskswitch.o +$(TEST_DIR)/taskswitch2.elf: $(cstart.o) $(TEST_DIR)/taskswitch2.o
lib/x86/desc.c +172 -0
181 lines not on this page · raw message
lib/x86/desc.h +13 -0
@@ -2,6 +2,13 @@#define __IDT_TEST__void setup_idt (void ); +#ifndef __x86_64__ +void setup_gdt (void ); +void setup_tss32 (void ); +#else +static inline void setup_gdt (void ){} +static inline void setup_tss32 (void ){} +#endif #define ASM_TRY (catch) \ "movl $0, %%gs:4 \n\t" \
@@ -13,8 +20,14 @@ void setup_idt(void);#define UD_VECTOR 6#define GP_VECTOR 13 +#define TSS_MAIN 0x20 +#define TSS_INTR 0x28 +unsigned exception_vector (void );unsigned exception_error_code (void );void set_idt_entry (int vec,void *addr,int dpl); +void set_gdt_entry (int num,u32 base,u32 limit,u8 access,u8 gran); +void set_intr_task_gate (int e,void *fn); +void print_current_tss_info (void );#endif
x86/taskswitch2.c new file +185 -0
185 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH 9/16 Move vm.[ch] info library code.
In reply to Gleb Natapov--- config-x86-common.mak | 14 ++-- lib/x86/vm.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/vm.h | 46 ++++++++++ x86/vm.c | 229 ------------------------------------------------- x86/vm.h | 46 ---------- 5 files changed, 281 insertions(+), 283 deletions(-) create mode 100644 lib/x86/vm.c create mode 100644 lib/x86/vm.h delete mode 100644 x86/vm.c delete mode 100644 x86/vm.h
config-x86-common.mak +6 -8
@@ -8,6 +8,7 @@ cflatobjs += \ lib/x86/io.o \ lib/x86/smp.o +cflatobjs += lib/x86/vm.o cflatobjs += lib/x86/fwcfg.o cflatobjs += lib/x86/apic.o cflatobjs += lib/x86/atomic.o
@@ -43,21 +44,19 @@ $(TEST_DIR)/access.elf: $(cstart.o) $(TEST_DIR)/access.o $(TEST_DIR)/hypercall.elf: $(cstart.o) $(TEST_DIR)/hypercall.o -$(TEST_DIR)/sieve.elf: $(cstart.o) $(TEST_DIR)/sieve.o \ - $(TEST_DIR)/vm.o +$(TEST_DIR)/sieve.elf: $(cstart.o) $(TEST_DIR)/sieve.o $(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)/emulator.elf: $(cstart.o) $(TEST_DIR)/emulator.o $(TEST_DIR)/port80.elf: $(cstart.o) $(TEST_DIR)/port80.o $(TEST_DIR)/tsc.elf: $(cstart.o) $(TEST_DIR)/tsc.o -$(TEST_DIR)/apic.elf: $(cstart.o) $(TEST_DIR)/apic.o $(TEST_DIR)/vm.o +$(TEST_DIR)/apic.elf: $(cstart.o) $(TEST_DIR)/apic.o $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o $(CC) -m32 -nostdlib -o $@ -Wl,-T,$(TEST_DIR)/realmode.lds $^
@@ -70,10 +69,9 @@ $(TEST_DIR)/idt_test.elf: $(cstart.o) $(TEST_DIR)/idt_test.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 +$(TEST_DIR)/rmap_chain.elf: $(cstart.o) $(TEST_DIR)/rmap_chain.o -$(TEST_DIR)/svm.elf: $(cstart.o) $(TEST_DIR)/vm.o +$(TEST_DIR)/svm.elf: $(cstart.o) $(TEST_DIR)/kvmclock_test.elf: $(cstart.o) $(TEST_DIR)/kvmclock.o \ $(TEST_DIR)/kvmclock_test.o
lib/x86/vm.c new file +229 -0
229 lines not on this page · raw message
lib/x86/vm.h new file +46 -0
46 lines not on this page · raw message
x86/vm.c deleted +0 -229
229 lines not on this page · raw message
x86/vm.h deleted +0 -46
46 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH 10/16 Fix mmu on 32 bit.
In reply to Gleb NatapovThe way virtual memory is setup now does not leave space for vmalloc on 32bit system. Make a hole in address space from 2G to 3G for vmalloc on 32 bit.
--- lib/x86/vm.c | 42 +++++++++++++++++++++++++++++++----------- 1 files changed, 31 insertions(+), 11 deletions(-)
lib/x86/vm.c +31 -11
@@ -135,23 +135,43 @@ struct ljmp {unsigned short seg; }; +static void setup_mmu_range (unsigned long *cr3,unsigned long start, +unsigned long len) +{ +u64 max = (u64 )len + (u64 )start; +u64 phys = start; + +while (phys +LARGE_PAGE_SIZE <= max) { +install_large_page (cr3, phys, (void *)(ulong )phys); + phys +=LARGE_PAGE_SIZE ; + } +while (phys +PAGE_SIZE <= max) { +install_page (cr3, phys, (void *)(ulong )phys); + phys +=PAGE_SIZE ; + } +} +static void setup_mmu (unsigned long len) {unsigned long *cr3 =alloc_page (); -unsigned long phys =0 ; +memset (cr3,0 ,PAGE_SIZE ); + +#ifdef __x86_64__if (len < (1ul <<32 )) - len =1ul <<32 ;/* map mmio 1:1 */ + len = (1ul <<32 );/* map mmio 1:1 */ + +setup_mmu_range (cr3,0 , len); +#else +if (len > (1ul <<31 )) + len = (1ul <<31 ); + +/* 0 - 2G memory, 2G-3G valloc area, 3G-4G mmio */ +setup_mmu_range (cr3,0 , len); +setup_mmu_range (cr3,3ul <<30 , (1ul <<30 )); + vfree_top = (void *)(3ul <<30 ); +#endif -memset (cr3,0 ,PAGE_SIZE ); -while (phys +LARGE_PAGE_SIZE <= len) { -install_large_page (cr3, phys, (void *)phys); - phys +=LARGE_PAGE_SIZE ; - } -while (phys +PAGE_SIZE <= len) { -install_page (cr3, phys, (void *)phys); - phys +=PAGE_SIZE ; - }write_cr3 (virt_to_phys (cr3));#ifndef __x86_64__write_cr4 (X86_CR4_PSE );
Signature · 1 line
1.7.2.3
PATCH 11/16 Set WP bit in CR0 to make write protection work.
In reply to Gleb Natapov--- lib/x86/vm.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
lib/x86/vm.c +2 -1
@@ -9,6 +9,7 @@#endif #define X86_CR0_PE 0x00000001 +#define X86_CR0_WP 0x00010000#define X86_CR0_PG 0x80000000#define X86_CR4_PSE 0x00000010static void *free =0 ;
@@ -176,7 +177,7 @@ static void setup_mmu(unsigned long len)#ifndef __x86_64__write_cr4 (X86_CR4_PSE );#endif -write_cr0 (X86_CR0_PG |X86_CR0_PE ); +write_cr0 (X86_CR0_PG |X86_CR0_PE |X86_CR0_WP );printf ("paging enabled\n" );printf ("cr0 = %x\n" ,read_cr0 ());
Signature · 1 line
1.7.2.3
PATCH 12/16 Fix exception handling on i386 arch.
In reply to Gleb NatapovPass parameter to exception handler in correct register.
--- lib/x86/desc.c | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
lib/x86/desc.c +7 -0
@@ -111,6 +111,9 @@ struct ex_record {extern struct ex_record exception_table_start, exception_table_end; +#ifndef __x86_64__ +__attribute__((regparm(1))) +#endif void do_handle_exception (struct ex_regs *regs) {struct ex_record *ex;
@@ -162,7 +165,11 @@ asm (".pushsection .text \n\t"#endif "push %"R"di; push %"R"si; push %"R"bp; sub $"S", %"R"sp \n\t" "push %"R"bx; push %"R"dx; push %"R"cx; push %"R"ax \n\t" +#ifdef __x86_64__ "mov %"R"sp, %"R"di \n\t" +#else + "mov %"R"sp, %"R"ax \n\t" +#endif"call do_handle_exception \n\t" "pop %" R "ax; pop %" R "cx; pop %" R "dx; pop %" R "bx \n\t" "add $" S ", %" R "sp; pop %" R "bp; pop %" R "si; pop %" R "di \n\t"
Signature · 1 line
1.7.2.3
PATCH 13/16 Move handle_irq() from apic test into library code.
In reply to Gleb Natapov--- config-x86-common.mak | 1 + lib/x86/isr.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/isr.h | 14 +++++++ x86/apic.c | 100 +------------------------------------------------ 4 files changed, 115 insertions(+), 99 deletions(-) create mode 100644 lib/x86/isr.c create mode 100644 lib/x86/isr.h
config-x86-common.mak +1 -0
@@ -13,6 +13,7 @@ cflatobjs += lib/x86/fwcfg.o cflatobjs += lib/x86/apic.o cflatobjs += lib/x86/atomic.o cflatobjs += lib/x86/desc.o +cflatobjs += lib/x86/isr.o $(libcflat): LDFLAGS += -nostdlib $(libcflat): CFLAGS += -ffreestanding -I lib
lib/x86/isr.c new file +99 -0
99 lines not on this page · raw message
lib/x86/isr.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef __ISR_TEST__ +#define __ISR_TEST__ + +typedef struct { +ulong regs[sizeof (ulong)*2 ]; +ulong func; +ulong rip; +ulong cs; +ulong rflags; +}isr_regs_t ; + +void handle_irq (unsigned vec,void (*func)(isr_regs_t *regs)); + +#endif
x86/apic.c +1 -99
112 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH 14/16 Add handle_exception() interface.
In reply to Gleb NatapovProvide easy way for test to hook its own exception handler.
--- lib/x86/desc.c | 122 +++++++++++++++++++++++++++++++++++++++---------------- lib/x86/desc.h | 15 +++++++ 2 files changed, 101 insertions(+), 36 deletions(-)
lib/x86/desc.c +88 -38
@@ -90,19 +90,6 @@ void set_idt_entry(int vec, void *addr, int dpl)#endif } -struct ex_regs { -unsigned long rax, rcx, rdx, rbx; -unsigned long dummy, rbp, rsi, rdi; -#ifdef __x86_64__ -unsigned long r8, r9, r10, r11; -unsigned long r12, r13, r14, r15; -#endif -unsigned long vector; -unsigned long error_code; -unsigned long rip; -unsigned long cs; -unsigned long rflags; -};struct ex_record {unsigned long rip;
@@ -111,10 +98,7 @@ struct ex_record {extern struct ex_record exception_table_start, exception_table_end; -#ifndef __x86_64__ -__attribute__((regparm(1))) -#endif -void do_handle_exception (struct ex_regs *regs) +static void check_exception_table (struct ex_regs *regs) {struct ex_record *ex;unsigned ex_val;
@@ -129,10 +113,34 @@ void do_handle_exception(struct ex_regs *regs)return ; } } -printf ("unhandled excecption\n" ); +printf ("unhandled excecption %d\n" , regs->vector);exit (7 ); } +static void (*exception_handlers[32 ])(struct ex_regs *regs); + + +void handle_exception (u8 v,void (*func)(struct ex_regs *regs)) +{ +if (v <32 ) + exception_handlers[v] = func; +} + +#ifndef __x86_64__ +__attribute__((regparm (1 ))) +#endif +void do_handle_exception (struct ex_regs *regs) +{ +if (regs->vector <32 && exception_handlers[regs->vector]) { + exception_handlers[regs->vector](regs); +return ; + } +printf ("unhandled cpu excecption %d\n" , regs->vector); +if (regs->vector ==14 ) +printf ("PF at %p addr %p\n" , regs->rip,read_cr2 ()); +exit (7 ); +} +#ifdef __x86_64__# define R "r"# define W "q"
@@ -143,22 +151,42 @@ void do_handle_exception(struct ex_regs *regs)# define S "4"#endif -asm (".pushsection .text \n\t" -"ud_fault: \n\t" -"push" W " $0 \n\t" -"push" W " $6 \n\t" -"jmp handle_exception \n\t" - -"gp_fault: \n\t" -"push" W " $13 \n\t" -"jmp handle_exception \n\t" - -"de_fault: \n\t" -"push" W " $0 \n\t" -"push" W " $0 \n\t" -"jmp handle_exception \n\t" +#define EX (NAME ,N ) extern char NAME##_fault; \ + asm (".pushsection .text \n\t" \ + #NAME"_fault: \n\t" \ + "push"W" $0 \n\t" \ + "push"W" $"#N" \n\t" \ + "jmp __handle_exception \n\t" \ + ".popsection") + +#define EX_E (NAME ,N ) extern char NAME##_fault; \ + asm (".pushsection .text \n\t" \ + #NAME"_fault: \n\t" \ + "push"W" $"#N" \n\t" \ + "jmp __handle_exception \n\t" \ + ".popsection") + +EX (de,0 ); +EX (db,1 ); +EX (nmi,2 ); +EX (bp,3 ); +EX (of,4 ); +EX (br,5 ); +EX (ud,6 ); +EX (nm,7 ); +EX_E (df,8 ); +EX_E (ts,10 ); +EX_E (np,11 ); +EX_E (ss,12 ); +EX_E (gp,13 ); +EX_E (pf,14 ); +EX (mf,16 ); +EX_E (ac,17 ); +EX (mc,18 ); +EX (xm,19 ); -"handle_exception: \n\t" +asm (".pushsection .text \n\t" +"__handle_exception: \n\t" #ifdef __x86_64__"push %r15; push %r14; push %r13; push %r12 \n\t" "push %r11; push %r10; push %r9; push %r8 \n\t"
@@ -182,15 +210,37 @@ asm (".pushsection .text \n\t""iret" W " \n\t" ".popsection"); +static void *idt_handlers[32 ] = { + [0 ] = &de_fault, + [1 ] = &db_fault, + [2 ] = &nmi_fault, + [3 ] = &bp_fault, + [4 ] = &of_fault, + [5 ] = &br_fault, + [6 ] = &ud_fault, + [7 ] = &nm_fault, + [8 ] = &df_fault, + [10 ] = &ts_fault, + [11 ] = &np_fault, + [12 ] = &ss_fault, + [13 ] = &gp_fault, + [14 ] = &pf_fault, + [16 ] = &mf_fault, + [17 ] = &ac_fault, + [18 ] = &mc_fault, + [19 ] = &xm_fault, +};void setup_idt (void ) { -extern char ud_fault, gp_fault, de_fault; - +int i;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 ); +for (i =0 ; i <32 ; i++) +if (idt_handlers[i]) +set_idt_entry (i, idt_handlers[i],0 ); +handle_exception (0 , check_exception_table); +handle_exception (6 , check_exception_table); +handle_exception (13 , check_exception_table); }unsigned exception_vector (void )
lib/x86/desc.h +15 -0
@@ -10,6 +10,20 @@ static inline void setup_gdt(void){}static inline void setup_tss32 (void ){}#endif +struct ex_regs { +unsigned long rax, rcx, rdx, rbx; +unsigned long dummy, rbp, rsi, rdi; +#ifdef __x86_64__ +unsigned long r8, r9, r10, r11; +unsigned long r12, r13, r14, r15; +#endif +unsigned long vector; +unsigned long error_code; +unsigned long rip; +unsigned long cs; +unsigned long rflags; +}; +#define ASM_TRY (catch) \ "movl $0, %%gs:4 \n\t" \ ".pushsection .data.ex \n\t" \
@@ -29,5 +43,6 @@ void set_idt_entry(int vec, void *addr, int dpl);void set_gdt_entry (int num,u32 base,u32 limit,u8 access,u8 gran);void set_intr_task_gate (int e,void *fn);void print_current_tss_info (void ); +void handle_exception (u8 v,void (*func)(struct ex_regs *regs));#endif
Signature · 1 line
1.7.2.3
PATCH 15/16 Move invlpg() into library code.
In reply to Gleb Natapov--- lib/x86/processor.h | 4 ++++ x86/access.c | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-)
lib/x86/processor.h +4 -0
@@ -290,4 +290,8 @@ static inline void irq_enable(void) asm volatile("sti"); } +static inline void invlpg (void *va) +{ + asmvolatile ("invlpg (%0)" ::"r" (va) :"memory" ); +}#endif
x86/access.c +0 -5
@@ -199,11 +199,6 @@ int ac_test_bump(ac_test_t *at)return ret; } -void invlpg (void *addr) -{ - asmvolatile ("invlpg (%0)" : :"r" (addr)); -} -pt_element_t ac_test_alloc_pt (ac_pool_t *pool) {pt_element_t ret = pool->pt_pool + pool->pt_pool_current;
Signature · 1 line
1.7.2.3
PATCH 16/16 Add even injection test.
In reply to Gleb NatapovAdd various event injection test. Those tests use testdev to unmap pages from shadow pages/ept tables which make it possible to test rare scenarios.
--- config-x86-common.mak | 4 +- lib/x86/desc.c | 7 +- lib/x86/desc.h | 3 + lib/x86/vm.c | 6 + lib/x86/vm.h | 1 + x86/eventinj.c | 372 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 391 insertions(+), 2 deletions(-) create mode 100644 x86/eventinj.c
config-x86-common.mak +3 -1
@@ -33,7 +33,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \ $(TEST_DIR)/smptest.flat $(TEST_DIR)/port80.flat \ $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \ $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \ - $(TEST_DIR)/kvmclock_test.flat + $(TEST_DIR)/kvmclock_test.flat $(TEST_DIR)/eventinj.flat tests_and_config = $(TEST_DIR)/*.flat $(TEST_DIR)/unittests.cfg
@@ -77,6 +77,8 @@ $(TEST_DIR)/svm.elf: $(cstart.o) $(TEST_DIR)/kvmclock_test.elf: $(cstart.o) $(TEST_DIR)/kvmclock.o \ $(TEST_DIR)/kvmclock_test.o +$(TEST_DIR)/eventinj.elf: $(cstart.o) $(TEST_DIR)/eventinj.o + arch_clean: $(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf \ $(TEST_DIR)/.*.d $(TEST_DIR)/lib/.*.d $(TEST_DIR)/lib/*.o
lib/x86/desc.c +6 -1
@@ -62,7 +62,7 @@ typedef struct {u16 iomap_base; } tss32_t; -static idt_entry_t idt[256 ]; +static idt_entry_t idt[256] __attribute__((aligned(4096)));void load_lidt (idt_entry_t *idt,int nentries) {
@@ -90,6 +90,11 @@ void set_idt_entry(int vec, void *addr, int dpl)#endif } +void set_idt_sel (int vec,u16 sel) +{ +idt_entry_t *e = &idt[vec]; + e->selector = sel; +}struct ex_record {unsigned long rip;
lib/x86/desc.h +3 -0
@@ -37,9 +37,12 @@ struct ex_regs {#define TSS_MAIN 0x20#define TSS_INTR 0x28 +#define NP_SEL 0x18 +unsigned exception_vector (void );unsigned exception_error_code (void );void set_idt_entry (int vec,void *addr,int dpl); +void set_idt_sel (int vec,u16 sel);void set_gdt_entry (int num,u32 base,u32 limit,u8 access,u8 gran);void set_intr_task_gate (int e,void *fn);void print_current_tss_info (void );
lib/x86/vm.c +6 -0
@@ -248,3 +248,9 @@ void *vmap(unsigned long long phys, unsigned long size) }return mem; } + +void *alloc_vpage (void ) +{ + vfree_top -=PAGE_SIZE ; +return vfree_top; +}
lib/x86/vm.h +1 -0
@@ -20,6 +20,7 @@ void setup_vm();void *vmalloc (unsigned long size);void vfree (void *mem);void *vmap (unsigned long long phys,unsigned long size); +void *alloc_vpage (void ); void install_pte(unsigned long *cr3, int pte_level,
x86/eventinj.c new file +372 -0
372 lines not on this page · raw message
Signature · 1 line
1.7.2.3