xref: /kvm-unit-tests/x86/taskswitch2.c (revision 3cdcf179421cbacf652152fede678161e82738ea)
1 #include "libcflat.h"
2 #include "desc.h"
3 #include "apic-defs.h"
4 #include "apic.h"
5 #include "processor.h"
6 #include "vm.h"
7 
8 #define MAIN_TSS_SEL (FIRST_SPARE_SEL + 0)
9 #define VM86_TSS_SEL (FIRST_SPARE_SEL + 8)
10 #define CONFORM_CS_SEL  (FIRST_SPARE_SEL + 16)
11 
12 static volatile int test_count;
13 static volatile unsigned int test_divider;
14 
15 static char *fault_addr;
16 static ulong fault_phys;
17 
18 static inline void io_delay(void)
19 {
20 }
21 
22 static void nmi_tss(void)
23 {
24 start:
25 	printf("NMI task is running\n");
26 	print_current_tss_info();
27 	test_count++;
28 	asm volatile ("iret");
29 	goto start;
30 }
31 
32 static void de_tss(void)
33 {
34 start:
35 	printf("DE task is running\n");
36 	print_current_tss_info();
37 	test_divider = 10;
38 	test_count++;
39 	asm volatile ("iret");
40 	goto start;
41 }
42 
43 static void of_tss(void)
44 {
45 start:
46 	printf("OF task is running\n");
47 	print_current_tss_info();
48 	test_count++;
49 	asm volatile ("iret");
50 	goto start;
51 }
52 
53 static void bp_tss(void)
54 {
55 start:
56 	printf("BP task is running\n");
57 	print_current_tss_info();
58 	test_count++;
59 	asm volatile ("iret");
60 	goto start;
61 }
62 
63 void do_pf_tss(ulong *error_code)
64 {
65 	printf("PF task is running %x %x\n", error_code, *(ulong*)error_code);
66 	print_current_tss_info();
67 	if (*(ulong*)error_code == 0x2) /* write access, not present */
68 		test_count++;
69 	install_pte(phys_to_virt(read_cr3()), 1, fault_addr,
70 		    fault_phys | PTE_PRESENT | PTE_WRITE, 0);
71 }
72 
73 extern void pf_tss(void);
74 
75 asm (
76 	"pf_tss: \n\t"
77 	"push %esp \n\t"
78 	"call do_pf_tss \n\t"
79 	"add $4, %esp \n\t"
80 	"iret\n\t"
81 	"jmp pf_tss\n\t"
82     );
83 
84 static void jmp_tss(void)
85 {
86 start:
87 	printf("JMP to task succeeded\n");
88 	print_current_tss_info();
89 	test_count++;
90 	asm volatile ("ljmp $" xstr(TSS_MAIN) ", $0");
91 	goto start;
92 }
93 
94 static void irq_tss(void)
95 {
96 start:
97 	printf("IRQ task is running\n");
98 	print_current_tss_info();
99 	test_count++;
100 	asm volatile ("iret");
101 	test_count++;
102 	printf("IRQ task restarts after iret.\n");
103 	goto start;
104 }
105 
106 static void user_tss(void)
107 {
108 start:
109 	test_count++;
110 	asm volatile ("iret");
111 	goto start;
112 }
113 
114 void test_kernel_mode_int()
115 {
116 	unsigned int res;
117 
118 	/* test that int $2 triggers task gate */
119 	test_count = 0;
120 	set_intr_task_gate(2, nmi_tss);
121 	printf("Triggering nmi 2\n");
122 	asm volatile ("int $2");
123 	printf("Return from nmi %d\n", test_count);
124 	report("NMI int $2", test_count == 1);
125 
126 	/* test that external NMI triggers task gate */
127 	test_count = 0;
128 	set_intr_task_gate(2, nmi_tss);
129 	printf("Triggering nmi through APIC\n");
130 	apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_NMI | APIC_INT_ASSERT, 0);
131 	io_delay();
132 	printf("Return from APIC nmi\n");
133 	report("NMI external", test_count == 1);
134 
135 	/* test that external interrupt triggesr task gate */
136 	test_count = 0;
137 	printf("Trigger IRQ from APIC\n");
138 	set_intr_task_gate(0xf0, irq_tss);
139 	irq_enable();
140 	apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL | APIC_DM_FIXED | APIC_INT_ASSERT | 0xf0, 0);
141 	io_delay();
142 	irq_disable();
143 	printf("Return from APIC IRQ\n");
144 	report("IRQ external", test_count == 1);
145 
146 	/* test that HW exception triggesr task gate */
147 	set_intr_task_gate(0, de_tss);
148 	printf("Try to devide by 0\n");
149 	asm volatile ("divl %3": "=a"(res)
150 		      : "d"(0), "a"(1500), "m"(test_divider));
151 	printf("Result is %d\n", res);
152 	report("DE exeption", res == 150);
153 
154 	/* test if call HW exeption DE by int $0 triggers task gate */
155 	test_count = 0;
156 	set_intr_task_gate(0, de_tss);
157 	printf("Call int 0\n");
158 	asm volatile ("int $0");
159 	printf("Return from int 0\n");
160 	report("int $0", test_count == 1);
161 
162 	/* test if HW exception OF triggers task gate */
163 	test_count = 0;
164 	set_intr_task_gate(4, of_tss);
165 	printf("Call into\n");
166 	asm volatile ("addb $127, %b0\ninto"::"a"(127));
167 	printf("Return from into\n");
168 	report("OF exeption", test_count);
169 
170 	/* test if HW exception BP triggers task gate */
171 	test_count = 0;
172 	set_intr_task_gate(3, bp_tss);
173 	printf("Call int 3\n");
174 	asm volatile ("int $3");
175 	printf("Return from int 3\n");
176 	report("BP exeption", test_count == 1);
177 
178 	/*
179 	 * test that PF triggers task gate and error code is placed on
180 	 * exception task's stack
181 	 */
182 	fault_addr = alloc_vpage();
183 	fault_phys = (ulong)virt_to_phys(alloc_page());
184 	test_count = 0;
185 	set_intr_task_gate(14, pf_tss);
186 	printf("Access unmapped page\n");
187 	*fault_addr = 0;
188 	printf("Return from pf tss\n");
189 	report("PF exeption", test_count == 1);
190 
191 	/* test that calling a task by lcall works */
192 	test_count = 0;
193 	set_intr_task_gate(0, irq_tss);
194 	printf("Calling task by lcall\n");
195 	/* hlt opcode is 0xf4 I use destination IP 0xf4f4f4f4 to catch
196 	   incorrect instruction length calculation */
197 	asm volatile("lcall $" xstr(TSS_INTR) ", $0xf4f4f4f4");
198 	printf("Return from call\n");
199 	report("lcall", test_count == 1);
200 
201 	/* call the same task again and check that it restarted after iret */
202 	test_count = 0;
203 	asm volatile("lcall $" xstr(TSS_INTR) ", $0xf4f4f4f4");
204 	report("lcall2", test_count == 2);
205 
206 	/* test that calling a task by ljmp works */
207 	test_count = 0;
208 	set_intr_task_gate(0, jmp_tss);
209 	printf("Jumping to a task by ljmp\n");
210 	asm volatile ("ljmp $" xstr(TSS_INTR) ", $0xf4f4f4f4");
211 	printf("Jump back succeeded\n");
212 	report("ljmp", test_count == 1);
213 }
214 
215 void test_vm86_switch(void)
216 {
217     static tss32_t main_tss;
218     static tss32_t vm86_tss;
219 
220     u8 *vm86_start;
221 
222     /* Write a 'ud2' instruction somewhere below 1 MB */
223     vm86_start = (void*) 0x42000;
224     vm86_start[0] = 0x0f;
225     vm86_start[1] = 0x0b;
226 
227     /* Main TSS */
228     set_gdt_entry(MAIN_TSS_SEL, (u32)&main_tss, sizeof(tss32_t) - 1, 0x89, 0);
229     ltr(MAIN_TSS_SEL);
230     main_tss = (tss32_t) {
231         .prev   = VM86_TSS_SEL,
232         .cr3    = read_cr3(),
233     };
234 
235     /* VM86 TSS (marked as busy, so we can iret to it) */
236     set_gdt_entry(VM86_TSS_SEL, (u32)&vm86_tss, sizeof(tss32_t) - 1, 0x8b, 0);
237     vm86_tss = (tss32_t) {
238         .eflags = 0x20002,
239         .cr3    = read_cr3(),
240         .eip    = (u32) vm86_start & 0x0f,
241         .cs     = (u32) vm86_start >> 4,
242         .ds     = 0x1234,
243         .es     = 0x2345,
244     };
245 
246     /* Setup task gate to main TSS for #UD */
247     set_idt_task_gate(6, MAIN_TSS_SEL);
248 
249     /* Jump into VM86 task with iret, #UD lets it come back immediately */
250     printf("Switch to VM86 task and back\n");
251     asm volatile(
252         "pushf\n"
253         "orw $0x4000, (%esp)\n"
254         "popf\n"
255         "iret\n"
256     );
257     report("VM86", 1);
258 }
259 
260 void test_conforming_switch(void)
261 {
262 	/* test lcall with conforming segment, cs.dpl != cs.rpl */
263 	test_count = 0;
264 
265 	tss_intr.cs = CONFORM_CS_SEL | 3;
266 	tss_intr.eip = (u32)user_tss;
267 	tss_intr.ds = tss_intr.gs = tss_intr.fs = tss_intr.ss = USER_DS;
268 	set_gdt_entry(CONFORM_CS_SEL, 0, 0xffffffff, 0x9f, 0xc0);
269 	asm volatile("lcall $" xstr(TSS_INTR) ", $0xf4f4f4f4");
270 	report("lcall with cs.rpl != cs.dpl", test_count == 1);
271 }
272 
273 int main()
274 {
275 	setup_vm();
276 	setup_idt();
277 	setup_tss32();
278 
279 	test_kernel_mode_int();
280 	test_vm86_switch();
281 	test_conforming_switch();
282 
283 	return report_summary();
284 }
285