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