1 #include "libcflat.h" 2 #include "desc.h" 3 #include "processor.h" 4 #include <setjmp.h> 5 6 void set_idt_entry(int vec, void *addr, int dpl) 7 { 8 idt_entry_t *e = &boot_idt[vec]; 9 memset(e, 0, sizeof *e); 10 e->offset0 = (unsigned long)addr; 11 e->selector = read_cs(); 12 e->ist = 0; 13 e->type = 14; 14 e->dpl = dpl; 15 e->p = 1; 16 e->offset1 = (unsigned long)addr >> 16; 17 #ifdef __x86_64__ 18 e->offset2 = (unsigned long)addr >> 32; 19 #endif 20 } 21 22 void set_idt_dpl(int vec, u16 dpl) 23 { 24 idt_entry_t *e = &boot_idt[vec]; 25 e->dpl = dpl; 26 } 27 28 void set_idt_sel(int vec, u16 sel) 29 { 30 idt_entry_t *e = &boot_idt[vec]; 31 e->selector = sel; 32 } 33 34 struct ex_record { 35 unsigned long rip; 36 unsigned long handler; 37 }; 38 39 extern struct ex_record exception_table_start, exception_table_end; 40 41 static const char* exception_mnemonic(int vector) 42 { 43 switch(vector) { 44 case 0: return "#DE"; 45 case 1: return "#DB"; 46 case 2: return "#NMI"; 47 case 3: return "#BP"; 48 case 4: return "#OF"; 49 case 5: return "#BR"; 50 case 6: return "#UD"; 51 case 7: return "#NM"; 52 case 8: return "#DF"; 53 case 10: return "#TS"; 54 case 11: return "#NP"; 55 case 12: return "#SS"; 56 case 13: return "#GP"; 57 case 14: return "#PF"; 58 case 16: return "#MF"; 59 case 17: return "#AC"; 60 case 18: return "#MC"; 61 case 19: return "#XM"; 62 default: return "#??"; 63 } 64 } 65 66 void unhandled_exception(struct ex_regs *regs, bool cpu) 67 { 68 printf("Unhandled %sexception %ld %s at ip %016lx\n", 69 cpu ? "cpu " : "", regs->vector, 70 exception_mnemonic(regs->vector), regs->rip); 71 if (regs->vector == 14) 72 printf("PF at %#lx addr %#lx\n", regs->rip, read_cr2()); 73 74 printf("error_code=%04lx rflags=%08lx cs=%08lx\n" 75 "rax=%016lx rcx=%016lx rdx=%016lx rbx=%016lx\n" 76 "rbp=%016lx rsi=%016lx rdi=%016lx\n" 77 #ifdef __x86_64__ 78 " r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n" 79 "r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n" 80 #endif 81 "cr0=%016lx cr2=%016lx cr3=%016lx cr4=%016lx\n" 82 #ifdef __x86_64__ 83 "cr8=%016lx\n" 84 #endif 85 , 86 regs->error_code, regs->rflags, regs->cs, 87 regs->rax, regs->rcx, regs->rdx, regs->rbx, 88 regs->rbp, regs->rsi, regs->rdi, 89 #ifdef __x86_64__ 90 regs->r8, regs->r9, regs->r10, regs->r11, 91 regs->r12, regs->r13, regs->r14, regs->r15, 92 #endif 93 read_cr0(), read_cr2(), read_cr3(), read_cr4() 94 #ifdef __x86_64__ 95 , read_cr8() 96 #endif 97 ); 98 dump_frame_stack((void*) regs->rip, (void*) regs->rbp); 99 abort(); 100 } 101 102 static void check_exception_table(struct ex_regs *regs) 103 { 104 struct ex_record *ex; 105 unsigned ex_val; 106 107 ex_val = regs->vector | (regs->error_code << 16) | 108 (((regs->rflags >> 16) & 1) << 8); 109 asm("mov %0, %%gs:4" : : "r"(ex_val)); 110 111 for (ex = &exception_table_start; ex != &exception_table_end; ++ex) { 112 if (ex->rip == regs->rip) { 113 regs->rip = ex->handler; 114 return; 115 } 116 } 117 unhandled_exception(regs, false); 118 } 119 120 static handler exception_handlers[32]; 121 122 handler handle_exception(u8 v, handler fn) 123 { 124 handler old; 125 126 old = exception_handlers[v]; 127 if (v < 32) 128 exception_handlers[v] = fn; 129 return old; 130 } 131 132 #ifndef __x86_64__ 133 __attribute__((regparm(1))) 134 #endif 135 void do_handle_exception(struct ex_regs *regs) 136 { 137 if (regs->vector < 32 && exception_handlers[regs->vector]) { 138 exception_handlers[regs->vector](regs); 139 return; 140 } 141 unhandled_exception(regs, true); 142 } 143 144 #define EX(NAME, N) extern char NAME##_fault; \ 145 asm (".pushsection .text \n\t" \ 146 #NAME"_fault: \n\t" \ 147 "push"W" $0 \n\t" \ 148 "push"W" $"#N" \n\t" \ 149 "jmp __handle_exception \n\t" \ 150 ".popsection") 151 152 #define EX_E(NAME, N) extern char NAME##_fault; \ 153 asm (".pushsection .text \n\t" \ 154 #NAME"_fault: \n\t" \ 155 "push"W" $"#N" \n\t" \ 156 "jmp __handle_exception \n\t" \ 157 ".popsection") 158 159 EX(de, 0); 160 EX(db, 1); 161 EX(nmi, 2); 162 EX(bp, 3); 163 EX(of, 4); 164 EX(br, 5); 165 EX(ud, 6); 166 EX(nm, 7); 167 EX_E(df, 8); 168 EX_E(ts, 10); 169 EX_E(np, 11); 170 EX_E(ss, 12); 171 EX_E(gp, 13); 172 EX_E(pf, 14); 173 EX(mf, 16); 174 EX_E(ac, 17); 175 EX(mc, 18); 176 EX(xm, 19); 177 178 asm (".pushsection .text \n\t" 179 "__handle_exception: \n\t" 180 #ifdef __x86_64__ 181 "push %r15; push %r14; push %r13; push %r12 \n\t" 182 "push %r11; push %r10; push %r9; push %r8 \n\t" 183 #endif 184 "push %"R "di; push %"R "si; push %"R "bp; sub $"S", %"R "sp \n\t" 185 "push %"R "bx; push %"R "dx; push %"R "cx; push %"R "ax \n\t" 186 #ifdef __x86_64__ 187 "mov %"R "sp, %"R "di \n\t" 188 #else 189 "mov %"R "sp, %"R "ax \n\t" 190 #endif 191 "call do_handle_exception \n\t" 192 "pop %"R "ax; pop %"R "cx; pop %"R "dx; pop %"R "bx \n\t" 193 "add $"S", %"R "sp; pop %"R "bp; pop %"R "si; pop %"R "di \n\t" 194 #ifdef __x86_64__ 195 "pop %r8; pop %r9; pop %r10; pop %r11 \n\t" 196 "pop %r12; pop %r13; pop %r14; pop %r15 \n\t" 197 #endif 198 "add $"S", %"R "sp \n\t" 199 "add $"S", %"R "sp \n\t" 200 "iret"W" \n\t" 201 ".popsection"); 202 203 static void *idt_handlers[32] = { 204 [0] = &de_fault, 205 [1] = &db_fault, 206 [2] = &nmi_fault, 207 [3] = &bp_fault, 208 [4] = &of_fault, 209 [5] = &br_fault, 210 [6] = &ud_fault, 211 [7] = &nm_fault, 212 [8] = &df_fault, 213 [10] = &ts_fault, 214 [11] = &np_fault, 215 [12] = &ss_fault, 216 [13] = &gp_fault, 217 [14] = &pf_fault, 218 [16] = &mf_fault, 219 [17] = &ac_fault, 220 [18] = &mc_fault, 221 [19] = &xm_fault, 222 }; 223 224 void setup_idt(void) 225 { 226 int i; 227 static bool idt_initialized = false; 228 229 if (idt_initialized) { 230 return; 231 } 232 idt_initialized = true; 233 for (i = 0; i < 32; i++) 234 if (idt_handlers[i]) 235 set_idt_entry(i, idt_handlers[i], 0); 236 handle_exception(0, check_exception_table); 237 handle_exception(6, check_exception_table); 238 handle_exception(13, check_exception_table); 239 } 240 241 unsigned exception_vector(void) 242 { 243 unsigned char vector; 244 245 asm("movb %%gs:4, %0" : "=q"(vector)); 246 return vector; 247 } 248 249 unsigned exception_error_code(void) 250 { 251 unsigned short error_code; 252 253 asm("mov %%gs:6, %0" : "=rm"(error_code)); 254 return error_code; 255 } 256 257 bool exception_rflags_rf(void) 258 { 259 unsigned char rf_flag; 260 261 asm("movb %%gs:5, %b0" : "=q"(rf_flag)); 262 return rf_flag & 1; 263 } 264 265 static char intr_alt_stack[4096]; 266 267 #ifndef __x86_64__ 268 void set_gdt_entry(int sel, u32 base, u32 limit, u8 access, u8 gran) 269 { 270 int num = sel >> 3; 271 272 /* Setup the descriptor base address */ 273 gdt32[num].base_low = (base & 0xFFFF); 274 gdt32[num].base_middle = (base >> 16) & 0xFF; 275 gdt32[num].base_high = (base >> 24) & 0xFF; 276 277 /* Setup the descriptor limits */ 278 gdt32[num].limit_low = (limit & 0xFFFF); 279 gdt32[num].granularity = ((limit >> 16) & 0x0F); 280 281 /* Finally, set up the granularity and access flags */ 282 gdt32[num].granularity |= (gran & 0xF0); 283 gdt32[num].access = access; 284 } 285 286 void set_gdt_task_gate(u16 sel, u16 tss_sel) 287 { 288 set_gdt_entry(sel, tss_sel, 0, 0x85, 0); // task, present 289 } 290 291 void set_idt_task_gate(int vec, u16 sel) 292 { 293 idt_entry_t *e = &boot_idt[vec]; 294 295 memset(e, 0, sizeof *e); 296 297 e->selector = sel; 298 e->ist = 0; 299 e->type = 5; 300 e->dpl = 0; 301 e->p = 1; 302 } 303 304 /* 305 * 0 - main task 306 * 1 - interrupt task 307 */ 308 309 tss32_t tss_intr; 310 311 void setup_tss32(void) 312 { 313 u16 desc_size = sizeof(tss32_t); 314 315 tss.cr3 = read_cr3(); 316 tss_intr.cr3 = read_cr3(); 317 tss_intr.ss0 = tss_intr.ss1 = tss_intr.ss2 = 0x10; 318 tss_intr.esp = tss_intr.esp0 = tss_intr.esp1 = tss_intr.esp2 = 319 (u32)intr_alt_stack + 4096; 320 tss_intr.cs = 0x08; 321 tss_intr.ds = tss_intr.es = tss_intr.fs = tss_intr.gs = tss_intr.ss = 0x10; 322 tss_intr.iomap_base = (u16)desc_size; 323 set_gdt_entry(TSS_INTR, (u32)&tss_intr, desc_size - 1, 0x89, 0x0f); 324 } 325 326 void set_intr_task_gate(int e, void *fn) 327 { 328 tss_intr.eip = (u32)fn; 329 set_idt_task_gate(e, TSS_INTR); 330 } 331 332 void setup_alt_stack(void) 333 { 334 setup_tss32(); 335 } 336 337 void set_intr_alt_stack(int e, void *fn) 338 { 339 set_intr_task_gate(e, fn); 340 } 341 342 void print_current_tss_info(void) 343 { 344 u16 tr = str(); 345 346 if (tr != TSS_MAIN && tr != TSS_INTR) 347 printf("Unknown TSS %x\n", tr); 348 else 349 printf("TR=%x (%s) Main TSS back link %x. Intr TSS back link %x\n", 350 tr, tr ? "interrupt" : "main", tss.prev, tss_intr.prev); 351 } 352 #else 353 void set_intr_alt_stack(int e, void *addr) 354 { 355 set_idt_entry(e, addr, 0); 356 boot_idt[e].ist = 1; 357 } 358 359 void setup_alt_stack(void) 360 { 361 tss.ist1 = (u64)intr_alt_stack + 4096; 362 } 363 #endif 364 365 static bool exception; 366 static jmp_buf *exception_jmpbuf; 367 368 static void exception_handler_longjmp(void) 369 { 370 longjmp(*exception_jmpbuf, 1); 371 } 372 373 static void exception_handler(struct ex_regs *regs) 374 { 375 /* longjmp must happen after iret, so do not do it now. */ 376 exception = true; 377 regs->rip = (unsigned long)&exception_handler_longjmp; 378 regs->cs = read_cs(); 379 } 380 381 bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data), 382 void *data) 383 { 384 handler old; 385 jmp_buf jmpbuf; 386 int ret; 387 388 old = handle_exception(ex, exception_handler); 389 ret = set_exception_jmpbuf(jmpbuf); 390 if (ret == 0) 391 trigger_func(data); 392 handle_exception(ex, old); 393 return ret; 394 } 395 396 void __set_exception_jmpbuf(jmp_buf *addr) 397 { 398 exception_jmpbuf = addr; 399 } 400