Unit tests for big real mode patchset
cover Unit tests for big real mode patchset
This series adds unit tests for the big real mode kernel patchset.
Avi Kivity (12):
realmode: test cpuid
emulator: test leave
emulator: test long-mode lidt/lgdt
emulator: check for mov null, %ss
realmode: test for ss base used with esp/ebp base register
realmode: test sigt/sgdt
realmode: test lahf
realmode: test movsx/movzx
emulator: test enter
realmode: test bswap
emulator: test lldt
emulator: test ltr
x86/emulator.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- x86/realmode.c | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 1 deletion(-)
Signature · 1 line
1.7.11
PATCH 1/12 realmode: test cpuid
--- x86/realmode.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
x86/realmode.c +15 -0
@@ -1287,6 +1287,20 @@ void test_jcxz(void)report ("jecxz short 2" ,R_AX , outregs.eax ==0x1234 ); } +static void test_cpuid (void ) +{ +MK_INSN (cpuid,"cpuid" ); +unsigned function =0x1234 ; +unsigned eax, ebx, ecx, edx; + + inregs.eax = eax = function; + asm("cpuid" :"+a" (eax),"=b" (ebx),"=c" (ecx),"=d" (edx)); +exec_in_big_real_mode (&insn_cpuid); +report ("cpuid" ,R_AX |R_BX |R_CX |R_DX , + outregs.eax == eax && outregs.ebx == ebx + && outregs.ecx == ecx && outregs.edx == edx); +} +void realmode_start (void ) {test_null ();
@@ -1320,6 +1334,7 @@ void realmode_start(void)test_das ();test_lds_lss ();test_jcxz (); +test_cpuid ();exit (0 ); }
Signature · 1 line
1.7.11
PATCH 2/12 emulator: test leave
In reply to Avi Kivity--- x86/emulator.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
x86/emulator.c +12 -1
@@ -213,7 +213,7 @@ void test_push(void *mem)void test_pop (void *mem) { -unsigned long tmp; +unsigned long tmp, tmp3;unsigned long *stack_top = mem +4096 ;unsigned long memw =0x123456789abcdeful ;static unsigned long tmp2;
@@ -256,6 +256,17 @@ void test_pop(void *mem) : [tmp]"=&r"(tmp) : [stack_top]"r"(stack_top) : "memory"); report("ret" ,1 ); + + stack_top[-1 ] =0x778899 ; + asmvolatile ("mov %%rsp, %[tmp] \n\t" +"mov %%rbp, %[tmp3] \n\t" +"mov %[stack_top], %%rbp \n\t" +"leave \n\t" +"xchg %%rsp, %[tmp] \n\t" +"xchg %%rbp, %[tmp3]" + : [tmp]"=&r" (tmp), [tmp3]"=&r" (tmp3) : [stack_top]"r" (stack_top-1 ) + :"memory" ); +report ("leave" , tmp == (ulong )stack_top && tmp3 ==0x778899 ); }void test_ljmp (void *mem)
Signature · 1 line
1.7.11
PATCH 3/12 emulator: test long-mode lidt/lgdt
In reply to Avi KivityA bug in kvm caused it to fail on long-mode lidt/lgdt. This patch tests for the failure.
However, kvm doesn't allow lidt/lgdt from mmio, so the test is disabled.
--- x86/emulator.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
x86/emulator.c +30 -0
@@ -718,6 +718,35 @@ static void test_crosspage_mmio(volatile uint8_t *mem)report ("cross-page mmio write" , mem[4095 ] ==0xaa && mem[4096 ] ==0x88 ); } +static void test_lgdt_lidt (volatile uint8_t *mem) +{ +struct descriptor_table_ptr orig, fresh = {}; + +sgdt (&orig); + *(struct descriptor_table_ptr *)mem = (struct descriptor_table_ptr ) { + .limit =0xf234 , + .base =0x12345678abcd , + }; +cli (); + asmvolatile ("lgdt %0" : :"m" (*(struct descriptor_table_ptr *)mem)); +sgdt (&fresh); +lgdt (&orig); +sti (); +report ("lgdt (long address)" , orig.limit == fresh.limit && orig.base == fresh.base); + +sidt (&orig); + *(struct descriptor_table_ptr *)mem = (struct descriptor_table_ptr ) { + .limit =0x432f , + .base =0xdbca87654321 , + }; +cli (); + asmvolatile ("lidt %0" : :"m" (*(struct descriptor_table_ptr *)mem)); +sidt (&fresh); +lidt (&orig); +sti (); +report ("lidt (long address)" , orig.limit == fresh.limit && orig.base == fresh.base); +} +int main () {void *mem;
@@ -768,6 +797,7 @@ int main()test_mmx (mem);test_rip_relative (mem, insn_ram);test_shld_shrd (mem); +//test_lgdt_lidt(mem); test_mmx_movq_mf (mem, insn_page, alt_insn_page, insn_ram);
Signature · 1 line
1.7.11
PATCH 4/12 emulator: check for mov null, %ss
In reply to Avi KivityBut check that rpl matches cpl.
--- x86/emulator.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
x86/emulator.c +28 -0
@@ -747,6 +747,33 @@ static void test_lgdt_lidt(volatile uint8_t *mem)report ("lidt (long address)" , orig.limit == fresh.limit && orig.base == fresh.base); } +static void ss_bad_rpl (struct ex_regs *regs) +{ +extern char ss_bad_rpl_cont; + + ++exceptions; + regs->rip = (ulong)&ss_bad_rpl_cont; +} + +static void test_sreg (volatile uint16_t *mem) +{ +u16 ss =read_ss (); + +// check for null segment load + *mem =0 ; + asmvolatile ("mov %0, %%ss" : :"m" (*mem)); +report ("mov null, %ss" ,read_ss () ==0 ); + +// check for exception when ss.rpl != cpl on null segment load + exceptions =0 ; +handle_exception (GP_VECTOR , ss_bad_rpl); + *mem =3 ; + asmvolatile ("mov %0, %%ss; ss_bad_rpl_cont:" : :"m" (*mem)); +report ("mov null, %ss (with ss.rpl != cpl)" , exceptions ==1 &&read_ss () ==0 ); +handle_exception (GP_VECTOR ,0 ); +write_ss (ss); +} +int main () {void *mem;
@@ -798,6 +825,7 @@ int main()test_rip_relative (mem, insn_ram);test_shld_shrd (mem);//test_lgdt_lidt(mem); +test_sreg (mem);test_mmx_movq_mf (mem, insn_page, alt_insn_page, insn_ram);
Signature · 1 line
1.7.11
PATCH 5/12 realmode: test for ss base used with esp/ebp base register
In reply to Avi Kivity--- x86/realmode.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
x86/realmode.c +18 -0
@@ -1301,6 +1301,23 @@ static void test_cpuid(void) && outregs.ecx == ecx && outregs.edx == edx); } +static void test_ss_base_for_esp_ebp (void ) +{ +MK_INSN (ssrel1,"mov %ss, %ax; mov %bx, %ss; movl (%ebp), %ebx; mov %ax, %ss" ); +MK_INSN (ssrel2,"mov %ss, %ax; mov %bx, %ss; movl (%ebp,%edi,8), %ebx; mov %ax, %ss" ); +static unsigned array[] = {0x12345678 ,0 ,0 ,0 ,0x87654321 }; + + inregs.ebx =1 ; + inregs.ebp = (unsigned )array; +exec_in_big_real_mode (&insn_ssrel1); +report ("ss relative addressing (1)" ,R_AX |R_BX , outregs.ebx ==0x87654321 ); + inregs.ebx =1 ; + inregs.ebp = (unsigned )array; + inregs.edi =0 ; +exec_in_big_real_mode (&insn_ssrel2); +report ("ss relative addressing (2)" ,R_AX |R_BX , outregs.ebx ==0x87654321 ); +} +void realmode_start (void ) {test_null ();
@@ -1335,6 +1352,7 @@ void realmode_start(void)test_lds_lss ();test_jcxz ();test_cpuid (); +test_ss_base_for_esp_ebp ();exit (0 ); }
Signature · 1 line
1.7.11
PATCH 6/12 realmode: test sigt/sgdt
In reply to Avi Kivity--- x86/realmode.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
x86/realmode.c +18 -0
@@ -1318,6 +1318,23 @@ static void test_ss_base_for_esp_ebp(void)report ("ss relative addressing (2)" ,R_AX |R_BX , outregs.ebx ==0x87654321 ); } +static void test_sgdt_sidt (void ) +{ +MK_INSN (sgdt,"sgdtw (%eax)" ); +MK_INSN (sidt,"sidtw (%eax)" ); +unsigned x, y; + + inregs.eax = (unsigned )&y; + asmvolatile ("sgdtw %0" :"=m" (x)); +exec_in_big_real_mode (&insn_sgdt); +report ("sgdt" ,0 , x == y); + + inregs.eax = (unsigned )&y; + asmvolatile ("sidtw %0" :"=m" (x)); +exec_in_big_real_mode (&insn_sidt); +report ("sidt" ,0 , x == y); +} +void realmode_start (void ) {test_null ();
@@ -1353,6 +1370,7 @@ void realmode_start(void)test_jcxz ();test_cpuid ();test_ss_base_for_esp_ebp (); +test_sgdt_sidt ();exit (0 ); }
Signature · 1 line
1.7.11
PATCH 7/12 realmode: test lahf
In reply to Avi Kivity--- x86/realmode.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
x86/realmode.c +10 -0
@@ -1335,6 +1335,15 @@ static void test_sgdt_sidt(void)report ("sidt" ,0 , x == y); } +static void test_lahf (void ) +{ +MK_INSN (lahf,"pushfw; mov %al, (%esp); popfw; lahf" ); + + inregs.eax =0xc7 ; +exec_in_big_real_mode (&insn_lahf); +report ("lahf" ,R_AX , (outregs.eax >>8 ) == inregs.eax); +} +void realmode_start (void ) {test_null ();
@@ -1371,6 +1380,7 @@ void realmode_start(void)test_cpuid ();test_ss_base_for_esp_ebp ();test_sgdt_sidt (); +test_lahf ();exit (0 ); }
Signature · 1 line
1.7.11
PATCH 8/12 realmode: test movsx/movzx
In reply to Avi Kivity--- x86/realmode.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
x86/realmode.c +13 -0
@@ -1344,6 +1344,18 @@ static void test_lahf(void)report ("lahf" ,R_AX , (outregs.eax >>8 ) == inregs.eax); } +static void test_movzx_movsx (void ) +{ +MK_INSN (movsx,"movsx %al, %ebx" ); +MK_INSN (movzx,"movzx %al, %ebx" ); + + inregs.eax =0x1234569c ; +exec_in_big_real_mode (&insn_movsx); +report ("movsx" ,R_BX , outregs.ebx == (signed char )inregs.eax); +exec_in_big_real_mode (&insn_movzx); +report ("movzx" ,R_BX , outregs.ebx == (unsigned char )inregs.eax); +} +void realmode_start (void ) {test_null ();
@@ -1381,6 +1393,7 @@ void realmode_start(void)test_ss_base_for_esp_ebp ();test_sgdt_sidt ();test_lahf (); +test_movzx_movsx ();exit (0 ); }
Signature · 1 line
1.7.11
PATCH 9/12 emulator: test enter
In reply to Avi Kivity--- x86/emulator.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
x86/emulator.c +14 -1
@@ -213,7 +213,7 @@ void test_push(void *mem)void test_pop (void *mem) { -unsigned long tmp, tmp3; +unsigned long tmp, tmp3, rsp, rbp;unsigned long *stack_top = mem +4096 ;unsigned long memw =0x123456789abcdeful ;static unsigned long tmp2;
@@ -267,6 +267,19 @@ void test_pop(void *mem) : [tmp]"=&r"(tmp), [tmp3]"=&r"(tmp3) : [stack_top]"r"(stack_top-1) : "memory"); report("leave" , tmp == (ulong )stack_top && tmp3 ==0x778899 ); + + rbp =0xaa55aa55bb66bb66ULL ; + rsp = (unsigned long )stack_top; + asmvolatile ("xchg %%rsp, %[rsp] \n\t" +"xchg %%rbp, %[rbp] \n\t" +"enter $0x1238, $0 \n\t" +"xchg %%rsp, %[rsp] \n\t" +"xchg %%rbp, %[rbp]" + : [rsp]"+a" (rsp), [rbp]"+b" (rbp) : :"memory" ); +report ("enter" , + rsp == (unsigned long )stack_top -8 -0x1238 + && rbp == (unsigned long )stack_top -8 + && stack_top[-1 ] ==0xaa55aa55bb66bb66ULL ); }void test_ljmp (void *mem)
Signature · 1 line
1.7.11
PATCH 10/12 realmode: test bswap
In reply to Avi Kivity--- x86/realmode.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
x86/realmode.c +10 -0
@@ -1356,6 +1356,15 @@ static void test_movzx_movsx(void)report ("movzx" ,R_BX , outregs.ebx == (unsigned char )inregs.eax); } +static void test_bswap (void ) +{ +MK_INSN (bswap,"bswap %ecx" ); + + inregs.ecx =0x12345678 ; +exec_in_big_real_mode (&insn_bswap); +report ("bswap" ,R_CX , outregs.ecx ==0x78563412 ); +} +void realmode_start (void ) {test_null ();
@@ -1394,6 +1403,7 @@ void realmode_start(void)test_sgdt_sidt ();test_lahf ();test_movzx_movsx (); +test_bswap ();exit (0 ); }
Signature · 1 line
1.7.11
PATCH 11/12 emulator: test lldt
In reply to Avi Kivity--- x86/emulator.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
x86/emulator.c +17 -0
@@ -787,6 +787,22 @@ static void test_sreg(volatile uint16_t *mem)write_ss (ss); } +static void test_lldt (volatile uint16_t *mem) +{ +u64 gdt[] = {0 ,0x0000f82000000ffffull /* ldt descriptor */ }; +struct descriptor_table_ptr gdt_ptr = { .limit =0xffff , .base = (ulong)&gdt }; +struct descriptor_table_ptr orig_gdt; + +cli (); +sgdt (&orig_gdt); +lgdt (&gdt_ptr); + *mem =0x8 ; + asmvolatile ("lldt %0" : :"m" (*mem)); +lgdt (&orig_gdt); +sti (); +report ("lldt" ,sldt () == *mem); +} +int main () {void *mem;
@@ -839,6 +855,7 @@ int main()test_shld_shrd (mem);//test_lgdt_lidt(mem); test_sreg (mem); +test_lldt (mem);test_mmx_movq_mf (mem, insn_page, alt_insn_page, insn_ram);
Signature · 1 line
1.7.11
PATCH 12/12 emulator: test ltr
In reply to Avi Kivity--- x86/emulator.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
x86/emulator.c +17 -0
@@ -803,6 +803,22 @@ static void test_lldt(volatile uint16_t *mem)report ("lldt" ,sldt () == *mem); } +static void test_ltr (volatile uint16_t *mem) +{ +struct descriptor_table_ptr gdt_ptr; +uint64_t *gdt, *trp; +uint16_t tr =str (); +uint64_t busy_mask = (uint64_t )1 <<41 ; + +sgdt (&gdt_ptr); + gdt = (uint64_t *)gdt_ptr.base; + trp = &gdt[tr >>3 ]; + *trp &= ~busy_mask; + *mem = tr; + asmvolatile ("ltr %0" : :"m" (*mem) :"memory" ); +report ("ltr" ,str () == tr && (*trp & busy_mask)); +} +int main () {void *mem;
@@ -856,6 +872,7 @@ int main()//test_lgdt_lidt(mem); test_sreg (mem);test_lldt (mem); +test_ltr (mem);test_mmx_movq_mf (mem, insn_page, alt_insn_page, insn_ram);
Signature · 1 line
1.7.11