desc.c 8.7 KB

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