xref: /kvm-unit-tests/lib/x86/desc.c (revision 309ca07bee690831a3b33c14afb010e6ada4a590)
1 #include "libcflat.h"
2 #include "desc.h"
3 #include "processor.h"
4 
5 void set_idt_entry(int vec, void *addr, int dpl)
6 {
7     idt_entry_t *e = &boot_idt[vec];
8     memset(e, 0, sizeof *e);
9     e->offset0 = (unsigned long)addr;
10     e->selector = read_cs();
11     e->ist = 0;
12     e->type = 14;
13     e->dpl = dpl;
14     e->p = 1;
15     e->offset1 = (unsigned long)addr >> 16;
16 #ifdef __x86_64__
17     e->offset2 = (unsigned long)addr >> 32;
18 #endif
19 }
20 
21 void set_idt_sel(int vec, u16 sel)
22 {
23     idt_entry_t *e = &boot_idt[vec];
24     e->selector = sel;
25 }
26 
27 struct ex_record {
28     unsigned long rip;
29     unsigned long handler;
30 };
31 
32 extern struct ex_record exception_table_start, exception_table_end;
33 
34 static void check_exception_table(struct ex_regs *regs)
35 {
36     struct ex_record *ex;
37     unsigned ex_val;
38 
39     ex_val = regs->vector | (regs->error_code << 16);
40 
41     asm("mov %0, %%gs:4" : : "r"(ex_val));
42 
43     for (ex = &exception_table_start; ex != &exception_table_end; ++ex) {
44         if (ex->rip == regs->rip) {
45             regs->rip = ex->handler;
46             return;
47         }
48     }
49     printf("unhandled excecption %d\n", regs->vector);
50     exit(7);
51 }
52 
53 static void (*exception_handlers[32])(struct ex_regs *regs);
54 
55 
56 void handle_exception(u8 v, void (*func)(struct ex_regs *regs))
57 {
58 	if (v < 32)
59 		exception_handlers[v] = func;
60 }
61 
62 #ifndef __x86_64__
63 __attribute__((regparm(1)))
64 #endif
65 void do_handle_exception(struct ex_regs *regs)
66 {
67 	if (regs->vector < 32 && exception_handlers[regs->vector]) {
68 		exception_handlers[regs->vector](regs);
69 		return;
70 	}
71 	printf("unhandled cpu excecption %d\n", regs->vector);
72 	if (regs->vector == 14)
73 		printf("PF at %p addr %p\n", regs->rip, read_cr2());
74 	exit(7);
75 }
76 
77 #define EX(NAME, N) extern char NAME##_fault;	\
78 	asm (".pushsection .text \n\t"		\
79 	     #NAME"_fault: \n\t"		\
80 	     "push"W" $0 \n\t"			\
81 	     "push"W" $"#N" \n\t"		\
82 	     "jmp __handle_exception \n\t"	\
83 	     ".popsection")
84 
85 #define EX_E(NAME, N) extern char NAME##_fault;	\
86 	asm (".pushsection .text \n\t"		\
87 	     #NAME"_fault: \n\t"		\
88 	     "push"W" $"#N" \n\t"		\
89 	     "jmp __handle_exception \n\t"	\
90 	     ".popsection")
91 
92 EX(de, 0);
93 EX(db, 1);
94 EX(nmi, 2);
95 EX(bp, 3);
96 EX(of, 4);
97 EX(br, 5);
98 EX(ud, 6);
99 EX(nm, 7);
100 EX_E(df, 8);
101 EX_E(ts, 10);
102 EX_E(np, 11);
103 EX_E(ss, 12);
104 EX_E(gp, 13);
105 EX_E(pf, 14);
106 EX(mf, 16);
107 EX_E(ac, 17);
108 EX(mc, 18);
109 EX(xm, 19);
110 
111 asm (".pushsection .text \n\t"
112      "__handle_exception: \n\t"
113 #ifdef __x86_64__
114      "push %r15; push %r14; push %r13; push %r12 \n\t"
115      "push %r11; push %r10; push %r9; push %r8 \n\t"
116 #endif
117      "push %"R"di; push %"R"si; push %"R"bp; sub $"S", %"R"sp \n\t"
118      "push %"R"bx; push %"R"dx; push %"R"cx; push %"R"ax \n\t"
119 #ifdef __x86_64__
120      "mov %"R"sp, %"R"di \n\t"
121 #else
122      "mov %"R"sp, %"R"ax \n\t"
123 #endif
124      "call do_handle_exception \n\t"
125      "pop %"R"ax; pop %"R"cx; pop %"R"dx; pop %"R"bx \n\t"
126      "add $"S", %"R"sp; pop %"R"bp; pop %"R"si; pop %"R"di \n\t"
127 #ifdef __x86_64__
128      "pop %r8; pop %r9; pop %r10; pop %r11 \n\t"
129      "pop %r12; pop %r13; pop %r14; pop %r15 \n\t"
130 #endif
131      "add $"S", %"R"sp \n\t"
132      "add $"S", %"R"sp \n\t"
133      "iret"W" \n\t"
134      ".popsection");
135 
136 static void *idt_handlers[32] = {
137 	[0] = &de_fault,
138 	[1] = &db_fault,
139 	[2] = &nmi_fault,
140 	[3] = &bp_fault,
141 	[4] = &of_fault,
142 	[5] = &br_fault,
143 	[6] = &ud_fault,
144 	[7] = &nm_fault,
145 	[8] = &df_fault,
146 	[10] = &ts_fault,
147 	[11] = &np_fault,
148 	[12] = &ss_fault,
149 	[13] = &gp_fault,
150 	[14] = &pf_fault,
151 	[16] = &mf_fault,
152 	[17] = &ac_fault,
153 	[18] = &mc_fault,
154 	[19] = &xm_fault,
155 };
156 
157 void setup_idt(void)
158 {
159     int i;
160     static bool idt_initialized = false;
161 
162     if (idt_initialized) {
163         return;
164     }
165     idt_initialized = true;
166     for (i = 0; i < 32; i++)
167 	    if (idt_handlers[i])
168 		    set_idt_entry(i, idt_handlers[i], 0);
169     handle_exception(0, check_exception_table);
170     handle_exception(6, check_exception_table);
171     handle_exception(13, check_exception_table);
172 }
173 
174 unsigned exception_vector(void)
175 {
176     unsigned short vector;
177 
178     asm("mov %%gs:4, %0" : "=rm"(vector));
179     return vector;
180 }
181 
182 unsigned exception_error_code(void)
183 {
184     unsigned short error_code;
185 
186     asm("mov %%gs:6, %0" : "=rm"(error_code));
187     return error_code;
188 }
189 
190 static char intr_alt_stack[4096];
191 
192 #ifndef __x86_64__
193 /*
194  * GDT, with 6 entries:
195  * 0x00 - NULL descriptor
196  * 0x08 - Code segment (ring 0)
197  * 0x10 - Data segment (ring 0)
198  * 0x18 - Not present code segment (ring 0)
199  * 0x20 - Code segment (ring 3)
200  * 0x28 - Data segment (ring 3)
201  * 0x30 - Interrupt task
202  * 0x38 to 0x78 - Free to use for test cases
203  * 0x80 - Primary task (CPU 0)
204  */
205 
206 void set_gdt_entry(int sel, u32 base,  u32 limit, u8 access, u8 gran)
207 {
208 	int num = sel >> 3;
209 
210 	/* Setup the descriptor base address */
211 	gdt32[num].base_low = (base & 0xFFFF);
212 	gdt32[num].base_middle = (base >> 16) & 0xFF;
213 	gdt32[num].base_high = (base >> 24) & 0xFF;
214 
215 	/* Setup the descriptor limits */
216 	gdt32[num].limit_low = (limit & 0xFFFF);
217 	gdt32[num].granularity = ((limit >> 16) & 0x0F);
218 
219 	/* Finally, set up the granularity and access flags */
220 	gdt32[num].granularity |= (gran & 0xF0);
221 	gdt32[num].access = access;
222 }
223 
224 void set_gdt_task_gate(u16 sel, u16 tss_sel)
225 {
226     set_gdt_entry(sel, tss_sel, 0, 0x85, 0); // task, present
227 }
228 
229 void set_idt_task_gate(int vec, u16 sel)
230 {
231     idt_entry_t *e = &boot_idt[vec];
232 
233     memset(e, 0, sizeof *e);
234 
235     e->selector = sel;
236     e->ist = 0;
237     e->type = 5;
238     e->dpl = 0;
239     e->p = 1;
240 }
241 
242 /*
243  * 0 - main task
244  * 1 - interrupt task
245  */
246 
247 tss32_t tss_intr;
248 
249 void setup_tss32(void)
250 {
251 	u16 desc_size = sizeof(tss32_t);
252 
253 	tss.cr3 = read_cr3();
254 	tss_intr.cr3 = read_cr3();
255 	tss_intr.ss0 = tss_intr.ss1 = tss_intr.ss2 = 0x10;
256 	tss_intr.esp = tss_intr.esp0 = tss_intr.esp1 = tss_intr.esp2 =
257 		(u32)intr_alt_stack + 4096;
258 	tss_intr.cs = 0x08;
259 	tss_intr.ds = tss_intr.es = tss_intr.fs = tss_intr.gs = tss_intr.ss = 0x10;
260 	tss_intr.iomap_base = (u16)desc_size;
261 	set_gdt_entry(TSS_INTR, (u32)&tss_intr, desc_size - 1, 0x89, 0x0f);
262 }
263 
264 void set_intr_task_gate(int e, void *fn)
265 {
266 	tss_intr.eip = (u32)fn;
267 	set_idt_task_gate(e, TSS_INTR);
268 }
269 
270 void setup_alt_stack(void)
271 {
272 	setup_tss32();
273 }
274 
275 void set_intr_alt_stack(int e, void *fn)
276 {
277 	set_intr_task_gate(e, fn);
278 }
279 
280 void print_current_tss_info(void)
281 {
282 	u16 tr = str();
283 
284 	if (tr != TSS_MAIN && tr != TSS_INTR)
285 		printf("Unknown TSS %x\n", tr);
286 	else
287 		printf("TR=%x (%s) Main TSS back link %x. Intr TSS back link %x\n",
288 		       tr, tr ? "interrupt" : "main", tss.prev, tss_intr.prev);
289 }
290 #else
291 void set_intr_alt_stack(int e, void *addr)
292 {
293 	set_idt_entry(e, addr, 0);
294 	boot_idt[e].ist = 1;
295 }
296 
297 void setup_alt_stack(void)
298 {
299 	tss.ist1 = (u64)intr_alt_stack + 4096;
300 }
301 #endif
302 
303 static bool exception;
304 static void *exception_return;
305 
306 static void exception_handler(struct ex_regs *regs)
307 {
308 	exception = true;
309 	regs->rip = (unsigned long)exception_return;
310 }
311 
312 bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data),
313 			void *data)
314 {
315 	handle_exception(ex, exception_handler);
316 	exception = false;
317 	trigger_func(data);
318 	handle_exception(ex, NULL);
319 	return exception;
320 }
321 
322 void set_exception_return(void *addr)
323 {
324 	exception_return = addr;
325 }
326