xref: /kvm-unit-tests/lib/x86/desc.c (revision 148fc69ecb75ec89dcad4f75029a8c8b983b4040)
1 #include "libcflat.h"
2 #include "desc.h"
3 #include "processor.h"
4 #include <setjmp.h>
5 #include "apic-defs.h"
6 
7 /* Boot-related data structures */
8 
9 /* IDT and IDT descriptor */
10 idt_entry_t boot_idt[256] = {0};
11 
12 struct descriptor_table_ptr idt_descr = {
13 	.limit = sizeof(boot_idt) - 1,
14 	.base = (unsigned long)boot_idt,
15 };
16 
17 #ifndef __x86_64__
18 /* GDT, TSS and descriptors */
19 gdt_entry_t gdt[TSS_MAIN / 8 + MAX_TEST_CPUS * 2] = {
20 	{     0, 0, 0, .type_limit_flags = 0x0000}, /* 0x00 null */
21 	{0xffff, 0, 0, .type_limit_flags = 0xcf9b}, /* flat 32-bit code segment */
22 	{0xffff, 0, 0, .type_limit_flags = 0xcf93}, /* flat 32-bit data segment */
23 	{0xffff, 0, 0, .type_limit_flags = 0xcf1b}, /* flat 32-bit code segment, not present */
24 	{     0, 0, 0, .type_limit_flags = 0x0000}, /* TSS for task gates */
25 	{0xffff, 0, 0, .type_limit_flags = 0x8f9b}, /* 16-bit code segment */
26 	{0xffff, 0, 0, .type_limit_flags = 0x8f93}, /* 16-bit data segment */
27 	{0xffff, 0, 0, .type_limit_flags = 0xcffb}, /* 32-bit code segment (user) */
28 	{0xffff, 0, 0, .type_limit_flags = 0xcff3}, /* 32-bit data segment (user) */
29 };
30 
31 tss32_t tss[MAX_TEST_CPUS] = {0};
32 #else
33 gdt_entry_t gdt[TSS_MAIN / 8 + MAX_TEST_CPUS * 2] = {
34 	{     0, 0, 0, .type_limit_flags = 0x0000}, /* 0x00 null */
35 	{0xffff, 0, 0, .type_limit_flags = 0xaf9b}, /* 0x08 64-bit code segment */
36 	{0xffff, 0, 0, .type_limit_flags = 0xcf93}, /* 0x10 32/64-bit data segment */
37 	{0xffff, 0, 0, .type_limit_flags = 0xaf1b}, /* 0x18 64-bit code segment, not present */
38 	{0xffff, 0, 0, .type_limit_flags = 0xcf9b}, /* 0x20 32-bit code segment */
39 	{0xffff, 0, 0, .type_limit_flags = 0x8f9b}, /* 0x28 16-bit code segment */
40 	{0xffff, 0, 0, .type_limit_flags = 0x8f93}, /* 0x30 16-bit data segment */
41 	{0xffff, 0, 0, .type_limit_flags = 0xcffb}, /* 0x38 32-bit code segment (user) */
42 	{0xffff, 0, 0, .type_limit_flags = 0xcff3}, /* 0x40 32/64-bit data segment (user) */
43 	{0xffff, 0, 0, .type_limit_flags = 0xaffb}, /* 0x48 64-bit code segment (user) */
44 };
45 
46 tss64_t tss[MAX_TEST_CPUS] = {0};
47 #endif
48 
49 struct descriptor_table_ptr gdt_descr = {
50 	.limit = sizeof(gdt) - 1,
51 	.base = (unsigned long)gdt,
52 };
53 
54 #ifndef __x86_64__
55 __attribute__((regparm(1)))
56 #endif
57 void do_handle_exception(struct ex_regs *regs);
58 
59 void set_idt_entry(int vec, void *addr, int dpl)
60 {
61 	idt_entry_t *e = &boot_idt[vec];
62 	memset(e, 0, sizeof *e);
63 	e->offset0 = (unsigned long)addr;
64 	e->selector = read_cs();
65 	e->ist = 0;
66 	e->type = 14;
67 	e->dpl = dpl;
68 	e->p = 1;
69 	e->offset1 = (unsigned long)addr >> 16;
70 #ifdef __x86_64__
71 	e->offset2 = (unsigned long)addr >> 32;
72 #endif
73 }
74 
75 void set_idt_dpl(int vec, u16 dpl)
76 {
77 	idt_entry_t *e = &boot_idt[vec];
78 	e->dpl = dpl;
79 }
80 
81 void set_idt_sel(int vec, u16 sel)
82 {
83 	idt_entry_t *e = &boot_idt[vec];
84 	e->selector = sel;
85 }
86 
87 struct ex_record {
88 	unsigned long rip;
89 	unsigned long handler;
90 };
91 
92 extern struct ex_record exception_table_start, exception_table_end;
93 
94 const char* exception_mnemonic(int vector)
95 {
96 	switch(vector) {
97 	case 0: return "#DE";
98 	case 1: return "#DB";
99 	case 2: return "#NMI";
100 	case 3: return "#BP";
101 	case 4: return "#OF";
102 	case 5: return "#BR";
103 	case 6: return "#UD";
104 	case 7: return "#NM";
105 	case 8: return "#DF";
106 	case 10: return "#TS";
107 	case 11: return "#NP";
108 	case 12: return "#SS";
109 	case 13: return "#GP";
110 	case 14: return "#PF";
111 	case 16: return "#MF";
112 	case 17: return "#AC";
113 	case 18: return "#MC";
114 	case 19: return "#XM";
115 	default: return "#??";
116 	}
117 }
118 
119 void unhandled_exception(struct ex_regs *regs, bool cpu)
120 {
121 	printf("Unhandled %sexception %ld %s at ip %016lx\n",
122 	       cpu ? "cpu " : "", regs->vector,
123 	       exception_mnemonic(regs->vector), regs->rip);
124 	if (regs->vector == 14)
125 		printf("PF at %#lx addr %#lx\n", regs->rip, read_cr2());
126 
127 	printf("error_code=%04lx      rflags=%08lx      cs=%08lx\n"
128 	       "rax=%016lx rcx=%016lx rdx=%016lx rbx=%016lx\n"
129 	       "rbp=%016lx rsi=%016lx rdi=%016lx\n"
130 #ifdef __x86_64__
131 	       " r8=%016lx  r9=%016lx r10=%016lx r11=%016lx\n"
132 	       "r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n"
133 #endif
134 	       "cr0=%016lx cr2=%016lx cr3=%016lx cr4=%016lx\n"
135 #ifdef __x86_64__
136 	       "cr8=%016lx\n"
137 #endif
138 	       ,
139 	       regs->error_code, regs->rflags, regs->cs,
140 	       regs->rax, regs->rcx, regs->rdx, regs->rbx,
141 	       regs->rbp, regs->rsi, regs->rdi,
142 #ifdef __x86_64__
143 	       regs->r8, regs->r9, regs->r10, regs->r11,
144 	       regs->r12, regs->r13, regs->r14, regs->r15,
145 #endif
146 	       read_cr0(), read_cr2(), read_cr3(), read_cr4()
147 #ifdef __x86_64__
148 	       , read_cr8()
149 #endif
150 	);
151 	dump_frame_stack((void*) regs->rip, (void*) regs->rbp);
152 	abort();
153 }
154 
155 static void check_exception_table(struct ex_regs *regs)
156 {
157 	struct ex_record *ex;
158 	unsigned ex_val;
159 
160 	ex_val = regs->vector | (regs->error_code << 16) |
161 		(((regs->rflags >> 16) & 1) << 8);
162 	asm("mov %0, %%gs:4" : : "r"(ex_val));
163 
164 	for (ex = &exception_table_start; ex != &exception_table_end; ++ex) {
165 		if (ex->rip == regs->rip) {
166 			regs->rip = ex->handler;
167 			return;
168 		}
169 	}
170 	unhandled_exception(regs, false);
171 }
172 
173 static handler exception_handlers[32];
174 
175 handler handle_exception(u8 v, handler fn)
176 {
177 	handler old;
178 
179 	old = exception_handlers[v];
180 	if (v < 32)
181 		exception_handlers[v] = fn;
182 	return old;
183 }
184 
185 #ifndef __x86_64__
186 __attribute__((regparm(1)))
187 #endif
188 void do_handle_exception(struct ex_regs *regs)
189 {
190 	if (regs->vector < 32 && exception_handlers[regs->vector]) {
191 		exception_handlers[regs->vector](regs);
192 		return;
193 	}
194 	unhandled_exception(regs, true);
195 }
196 
197 #define EX(NAME, N) extern char NAME##_fault;	\
198 	asm (".pushsection .text \n\t"		\
199 	     #NAME"_fault: \n\t"		\
200 	     "push"W" $0 \n\t"			\
201 	     "push"W" $"#N" \n\t"		\
202 	     "jmp __handle_exception \n\t"	\
203 	     ".popsection")
204 
205 #define EX_E(NAME, N) extern char NAME##_fault;	\
206 	asm (".pushsection .text \n\t"		\
207 	     #NAME"_fault: \n\t"		\
208 	     "push"W" $"#N" \n\t"		\
209 	     "jmp __handle_exception \n\t"	\
210 	     ".popsection")
211 
212 EX(de, 0);
213 EX(db, 1);
214 EX(nmi, 2);
215 EX(bp, 3);
216 EX(of, 4);
217 EX(br, 5);
218 EX(ud, 6);
219 EX(nm, 7);
220 EX_E(df, 8);
221 EX_E(ts, 10);
222 EX_E(np, 11);
223 EX_E(ss, 12);
224 EX_E(gp, 13);
225 EX_E(pf, 14);
226 EX(mf, 16);
227 EX_E(ac, 17);
228 EX(mc, 18);
229 EX(xm, 19);
230 EX_E(cp, 21);
231 
232 asm (".pushsection .text \n\t"
233      "__handle_exception: \n\t"
234 #ifdef __x86_64__
235      "push %r15; push %r14; push %r13; push %r12 \n\t"
236      "push %r11; push %r10; push %r9; push %r8 \n\t"
237 #endif
238      "push %"R "di; push %"R "si; push %"R "bp; sub $"S", %"R "sp \n\t"
239      "push %"R "bx; push %"R "dx; push %"R "cx; push %"R "ax \n\t"
240 #ifdef __x86_64__
241      "mov %"R "sp, %"R "di \n\t"
242 #else
243      "mov %"R "sp, %"R "ax \n\t"
244 #endif
245      "call do_handle_exception \n\t"
246      "pop %"R "ax; pop %"R "cx; pop %"R "dx; pop %"R "bx \n\t"
247      "add $"S", %"R "sp; pop %"R "bp; pop %"R "si; pop %"R "di \n\t"
248 #ifdef __x86_64__
249      "pop %r8; pop %r9; pop %r10; pop %r11 \n\t"
250      "pop %r12; pop %r13; pop %r14; pop %r15 \n\t"
251 #endif
252      "add $"S", %"R "sp \n\t"
253      "add $"S", %"R "sp \n\t"
254      "iret"W" \n\t"
255      ".popsection");
256 
257 static void *idt_handlers[32] = {
258 	[0] = &de_fault,
259 	[1] = &db_fault,
260 	[2] = &nmi_fault,
261 	[3] = &bp_fault,
262 	[4] = &of_fault,
263 	[5] = &br_fault,
264 	[6] = &ud_fault,
265 	[7] = &nm_fault,
266 	[8] = &df_fault,
267 	[10] = &ts_fault,
268 	[11] = &np_fault,
269 	[12] = &ss_fault,
270 	[13] = &gp_fault,
271 	[14] = &pf_fault,
272 	[16] = &mf_fault,
273 	[17] = &ac_fault,
274 	[18] = &mc_fault,
275 	[19] = &xm_fault,
276 	[21] = &cp_fault,
277 };
278 
279 void setup_idt(void)
280 {
281 	int i;
282 	static bool idt_initialized = false;
283 
284 	if (idt_initialized)
285 		return;
286 
287 	idt_initialized = true;
288 	for (i = 0; i < 32; i++) {
289 		if (idt_handlers[i])
290 			set_idt_entry(i, idt_handlers[i], 0);
291 	}
292 	handle_exception(0, check_exception_table);
293 	handle_exception(6, check_exception_table);
294 	handle_exception(13, check_exception_table);
295 }
296 
297 unsigned exception_vector(void)
298 {
299 	unsigned char vector;
300 
301 	asm volatile("movb %%gs:4, %0" : "=q"(vector));
302 	return vector;
303 }
304 
305 int write_cr4_checking(unsigned long val)
306 {
307 	asm volatile(ASM_TRY("1f")
308 		"mov %0,%%cr4\n\t"
309 		"1:": : "r" (val));
310 	return exception_vector();
311 }
312 
313 unsigned exception_error_code(void)
314 {
315 	unsigned short error_code;
316 
317 	asm volatile("mov %%gs:6, %0" : "=r"(error_code));
318 	return error_code;
319 }
320 
321 bool exception_rflags_rf(void)
322 {
323 	unsigned char rf_flag;
324 
325 	asm volatile("movb %%gs:5, %b0" : "=q"(rf_flag));
326 	return rf_flag & 1;
327 }
328 
329 static char intr_alt_stack[4096];
330 
331 void set_gdt_entry(int sel, unsigned long base,  u32 limit, u8 type, u8 flags)
332 {
333 	gdt_entry_t *entry = &gdt[sel >> 3];
334 
335 	/* Setup the descriptor base address */
336 	entry->base1 = (base & 0xFFFF);
337 	entry->base2 = (base >> 16) & 0xFF;
338 	entry->base3 = (base >> 24) & 0xFF;
339 
340 	/* Setup the descriptor limits, type and flags */
341 	entry->limit1 = (limit & 0xFFFF);
342 	entry->type_limit_flags = ((limit & 0xF0000) >> 8) | ((flags & 0xF0) << 8) | type;
343 
344 #ifdef __x86_64__
345 	if (!entry->s) {
346 		struct system_desc64 *entry16 = (struct system_desc64 *)entry;
347 		entry16->zero = 0;
348 		entry16->base4 = base >> 32;
349 	}
350 #endif
351 }
352 
353 #ifndef __x86_64__
354 void set_gdt_task_gate(u16 sel, u16 tss_sel)
355 {
356 	set_gdt_entry(sel, tss_sel, 0, 0x85, 0); // task, present
357 }
358 
359 void set_idt_task_gate(int vec, u16 sel)
360 {
361 	idt_entry_t *e = &boot_idt[vec];
362 
363 	memset(e, 0, sizeof *e);
364 
365 	e->selector = sel;
366 	e->ist = 0;
367 	e->type = 5;
368 	e->dpl = 0;
369 	e->p = 1;
370 }
371 
372 /*
373  * 0 - main task
374  * 1 - interrupt task
375  */
376 
377 tss32_t tss_intr;
378 
379 void setup_tss32(void)
380 {
381 	u16 desc_size = sizeof(tss32_t);
382 
383 	tss[0].cr3 = read_cr3();
384 	tss_intr.cr3 = read_cr3();
385 	tss_intr.ss0 = tss_intr.ss1 = tss_intr.ss2 = 0x10;
386 	tss_intr.esp = tss_intr.esp0 = tss_intr.esp1 = tss_intr.esp2 =
387 		(u32)intr_alt_stack + 4096;
388 	tss_intr.cs = 0x08;
389 	tss_intr.ds = tss_intr.es = tss_intr.fs = tss_intr.ss = 0x10;
390 	tss_intr.gs = read_gs();
391 	tss_intr.iomap_base = (u16)desc_size;
392 	set_gdt_entry(TSS_INTR, (u32)&tss_intr, desc_size - 1, 0x89, 0);
393 }
394 
395 void set_intr_task_gate(int e, void *fn)
396 {
397 	tss_intr.eip = (u32)fn;
398 	set_idt_task_gate(e, TSS_INTR);
399 }
400 
401 void setup_alt_stack(void)
402 {
403 	setup_tss32();
404 }
405 
406 void set_intr_alt_stack(int e, void *fn)
407 {
408 	set_intr_task_gate(e, fn);
409 }
410 
411 void print_current_tss_info(void)
412 {
413 	u16 tr = str();
414 
415 	if (tr != TSS_MAIN && tr != TSS_INTR)
416 		printf("Unknown TSS %x\n", tr);
417 	else
418 		printf("TR=%x (%s) Main TSS back link %x. Intr TSS back link %x\n",
419 		       tr, tr ? "interrupt" : "main", tss[0].prev, tss_intr.prev);
420 }
421 #else
422 void set_intr_alt_stack(int e, void *addr)
423 {
424 	set_idt_entry(e, addr, 0);
425 	boot_idt[e].ist = 1;
426 }
427 
428 void setup_alt_stack(void)
429 {
430 	tss[0].ist1 = (u64)intr_alt_stack + 4096;
431 }
432 #endif
433 
434 static bool exception;
435 static jmp_buf *exception_jmpbuf;
436 
437 static void exception_handler_longjmp(void)
438 {
439 	longjmp(*exception_jmpbuf, 1);
440 }
441 
442 static void exception_handler(struct ex_regs *regs)
443 {
444 	/* longjmp must happen after iret, so do not do it now.  */
445 	exception = true;
446 	regs->rip = (unsigned long)&exception_handler_longjmp;
447 	regs->cs = read_cs();
448 }
449 
450 bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data),
451 			void *data)
452 {
453 	handler old;
454 	jmp_buf jmpbuf;
455 	int ret;
456 
457 	old = handle_exception(ex, exception_handler);
458 	ret = set_exception_jmpbuf(jmpbuf);
459 	if (ret == 0)
460 		trigger_func(data);
461 	handle_exception(ex, old);
462 	return ret;
463 }
464 
465 void __set_exception_jmpbuf(jmp_buf *addr)
466 {
467 	exception_jmpbuf = addr;
468 }
469 
470 gdt_entry_t *get_tss_descr(void)
471 {
472 	struct descriptor_table_ptr gdt_ptr;
473 	gdt_entry_t *gdt;
474 
475 	sgdt(&gdt_ptr);
476 	gdt = (gdt_entry_t *)gdt_ptr.base;
477 	return &gdt[str() / 8];
478 }
479 
480 unsigned long get_gdt_entry_base(gdt_entry_t *entry)
481 {
482 	unsigned long base;
483 	base = entry->base1 | ((u32)entry->base2 << 16) | ((u32)entry->base3 << 24);
484 #ifdef __x86_64__
485 	if (!entry->s) {
486 		base |= (u64)((struct system_desc64 *)entry)->base4 << 32;
487 	}
488 #endif
489 	return base;
490 }
491 
492 unsigned long get_gdt_entry_limit(gdt_entry_t *entry)
493 {
494 	unsigned long limit;
495 	limit = entry->limit1 | ((u32)entry->limit2 << 16);
496 	if (entry->g) {
497 		limit = (limit << 12) | 0xFFF;
498 	}
499 	return limit;
500 }
501