xref: /kvm-unit-tests/x86/idt_test.c (revision 7d36db351752e29ad27eaafe3f102de7064e429b)
1 #include "libcflat.h"
2 #include "idt.h"
3 
4 int test_ud2(void)
5 {
6     asm volatile(ASM_TRY("1f")
7                  "ud2 \n\t"
8                  "1:" :);
9     return exception_vector();
10 }
11 
12 int test_gp(void)
13 {
14     unsigned long tmp;
15 
16     asm volatile("mov $0xffffffff, %0 \n\t"
17                  ASM_TRY("1f")
18 		 "mov %0, %%cr4\n\t"
19                  "1:"
20                  : "=a"(tmp));
21     return exception_vector();
22 }
23 
24 static int nr_fail, nr_test;
25 
26 static void report(int cond, const char *name)
27 {
28     ++nr_test;
29     if (!cond) {
30         ++nr_fail;
31         printf("%s: FAIL\n", name);
32     } else {
33         printf("%s: PASS\n", name);
34     }
35 }
36 
37 int main(void)
38 {
39     int r;
40 
41     printf("Starting IDT test\n");
42     setup_idt();
43     r = test_gp();
44     report(r == GP_VECTOR, "Testing #GP");
45     r = test_ud2();
46     report(r == UD_VECTOR, "Testing #UD");
47     printf("%d failures of %d tests\n", nr_fail, nr_test);
48     return !nr_fail ? 0 : 1;
49 }
50