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