xref: /kvm-unit-tests/lib/x86/desc.c (revision d95bd8f6c804027984dec6468a57cb47de556d19)
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 		(((regs->rflags >> 16) & 1) << 8);
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 char vector;
177 
178     asm("movb %%gs:4, %0" : "=q"(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 bool exception_rflags_rf(void)
191 {
192     unsigned char rf_flag;
193 
194     asm("movb %%gs:5, %b0" : "=q"(rf_flag));
195     return rf_flag & 1;
196 }
197 
198 static char intr_alt_stack[4096];
199 
200 #ifndef __x86_64__
201 /*
202  * GDT, with 6 entries:
203  * 0x00 - NULL descriptor
204  * 0x08 - Code segment (ring 0)
205  * 0x10 - Data segment (ring 0)
206  * 0x18 - Not present code segment (ring 0)
207  * 0x20 - Code segment (ring 3)
208  * 0x28 - Data segment (ring 3)
209  * 0x30 - Interrupt task
210  * 0x38 to 0x78 - Free to use for test cases
211  * 0x80 - Primary task (CPU 0)
212  */
213 
214 void set_gdt_entry(int sel, u32 base,  u32 limit, u8 access, u8 gran)
215 {
216 	int num = sel >> 3;
217 
218 	/* Setup the descriptor base address */
219 	gdt32[num].base_low = (base & 0xFFFF);
220 	gdt32[num].base_middle = (base >> 16) & 0xFF;
221 	gdt32[num].base_high = (base >> 24) & 0xFF;
222 
223 	/* Setup the descriptor limits */
224 	gdt32[num].limit_low = (limit & 0xFFFF);
225 	gdt32[num].granularity = ((limit >> 16) & 0x0F);
226 
227 	/* Finally, set up the granularity and access flags */
228 	gdt32[num].granularity |= (gran & 0xF0);
229 	gdt32[num].access = access;
230 }
231 
232 void set_gdt_task_gate(u16 sel, u16 tss_sel)
233 {
234     set_gdt_entry(sel, tss_sel, 0, 0x85, 0); // task, present
235 }
236 
237 void set_idt_task_gate(int vec, u16 sel)
238 {
239     idt_entry_t *e = &boot_idt[vec];
240 
241     memset(e, 0, sizeof *e);
242 
243     e->selector = sel;
244     e->ist = 0;
245     e->type = 5;
246     e->dpl = 0;
247     e->p = 1;
248 }
249 
250 /*
251  * 0 - main task
252  * 1 - interrupt task
253  */
254 
255 tss32_t tss_intr;
256 
257 void setup_tss32(void)
258 {
259 	u16 desc_size = sizeof(tss32_t);
260 
261 	tss.cr3 = read_cr3();
262 	tss_intr.cr3 = read_cr3();
263 	tss_intr.ss0 = tss_intr.ss1 = tss_intr.ss2 = 0x10;
264 	tss_intr.esp = tss_intr.esp0 = tss_intr.esp1 = tss_intr.esp2 =
265 		(u32)intr_alt_stack + 4096;
266 	tss_intr.cs = 0x08;
267 	tss_intr.ds = tss_intr.es = tss_intr.fs = tss_intr.gs = tss_intr.ss = 0x10;
268 	tss_intr.iomap_base = (u16)desc_size;
269 	set_gdt_entry(TSS_INTR, (u32)&tss_intr, desc_size - 1, 0x89, 0x0f);
270 }
271 
272 void set_intr_task_gate(int e, void *fn)
273 {
274 	tss_intr.eip = (u32)fn;
275 	set_idt_task_gate(e, TSS_INTR);
276 }
277 
278 void setup_alt_stack(void)
279 {
280 	setup_tss32();
281 }
282 
283 void set_intr_alt_stack(int e, void *fn)
284 {
285 	set_intr_task_gate(e, fn);
286 }
287 
288 void print_current_tss_info(void)
289 {
290 	u16 tr = str();
291 
292 	if (tr != TSS_MAIN && tr != TSS_INTR)
293 		printf("Unknown TSS %x\n", tr);
294 	else
295 		printf("TR=%x (%s) Main TSS back link %x. Intr TSS back link %x\n",
296 		       tr, tr ? "interrupt" : "main", tss.prev, tss_intr.prev);
297 }
298 #else
299 void set_intr_alt_stack(int e, void *addr)
300 {
301 	set_idt_entry(e, addr, 0);
302 	boot_idt[e].ist = 1;
303 }
304 
305 void setup_alt_stack(void)
306 {
307 	tss.ist1 = (u64)intr_alt_stack + 4096;
308 }
309 #endif
310 
311 static bool exception;
312 static void *exception_return;
313 
314 static void exception_handler(struct ex_regs *regs)
315 {
316 	exception = true;
317 	regs->rip = (unsigned long)exception_return;
318 }
319 
320 bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data),
321 			void *data)
322 {
323 	handle_exception(ex, exception_handler);
324 	exception = false;
325 	trigger_func(data);
326 	handle_exception(ex, NULL);
327 	return exception;
328 }
329 
330 void set_exception_return(void *addr)
331 {
332 	exception_return = addr;
333 }
334