desc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef __IDT_TEST__
  2. #define __IDT_TEST__
  3. #include <setjmp.h>
  4. void setup_idt(void);
  5. void setup_alt_stack(void);
  6. struct ex_regs {
  7. unsigned long rax, rcx, rdx, rbx;
  8. unsigned long dummy, rbp, rsi, rdi;
  9. #ifdef __x86_64__
  10. unsigned long r8, r9, r10, r11;
  11. unsigned long r12, r13, r14, r15;
  12. #endif
  13. unsigned long vector;
  14. unsigned long error_code;
  15. unsigned long rip;
  16. unsigned long cs;
  17. unsigned long rflags;
  18. };
  19. typedef void (*handler)(struct ex_regs *regs);
  20. typedef struct {
  21. u16 prev;
  22. u16 res1;
  23. u32 esp0;
  24. u16 ss0;
  25. u16 res2;
  26. u32 esp1;
  27. u16 ss1;
  28. u16 res3;
  29. u32 esp2;
  30. u16 ss2;
  31. u16 res4;
  32. u32 cr3;
  33. u32 eip;
  34. u32 eflags;
  35. u32 eax, ecx, edx, ebx, esp, ebp, esi, edi;
  36. u16 es;
  37. u16 res5;
  38. u16 cs;
  39. u16 res6;
  40. u16 ss;
  41. u16 res7;
  42. u16 ds;
  43. u16 res8;
  44. u16 fs;
  45. u16 res9;
  46. u16 gs;
  47. u16 res10;
  48. u16 ldt;
  49. u16 res11;
  50. u16 t:1;
  51. u16 res12:15;
  52. u16 iomap_base;
  53. } tss32_t;
  54. typedef struct __attribute__((packed)) {
  55. u32 res1;
  56. u64 rsp0;
  57. u64 rsp1;
  58. u64 rsp2;
  59. u64 res2;
  60. u64 ist1;
  61. u64 ist2;
  62. u64 ist3;
  63. u64 ist4;
  64. u64 ist5;
  65. u64 ist6;
  66. u64 ist7;
  67. u64 res3;
  68. u16 res4;
  69. u16 iomap_base;
  70. } tss64_t;
  71. #define ASM_TRY(catch) \
  72. "movl $0, %%gs:4 \n\t" \
  73. ".pushsection .data.ex \n\t" \
  74. ".quad 1111f, " catch "\n\t" \
  75. ".popsection \n\t" \
  76. "1111:"
  77. #define DB_VECTOR 1
  78. #define BP_VECTOR 3
  79. #define UD_VECTOR 6
  80. #define GP_VECTOR 13
  81. /*
  82. * selector 32-bit 64-bit
  83. * 0x00 NULL descriptor NULL descriptor
  84. * 0x08 ring-0 code segment (32-bit) ring-0 code segment (64-bit)
  85. * 0x10 ring-0 data segment (32-bit) ring-0 data segment (32/64-bit)
  86. * 0x18 ring-0 code segment (P=0) ring-0 code segment (64-bit, P=0)
  87. * 0x20 intr_alt_stack TSS ring-0 code segment (32-bit)
  88. * 0x28 ring-0 code segment (16-bit) same
  89. * 0x30 ring-0 data segment (16-bit) same
  90. * 0x38 (0x3b) ring-3 code segment (32-bit) same
  91. * 0x40 (0x43) ring-3 data segment (32-bit) ring-3 data segment (32/64-bit)
  92. * 0x48 (0x4b) **unused** ring-3 code segment (64-bit)
  93. * 0x50--0x78 free to use for test cases same
  94. * 0x80 primary TSS (CPU 0) same
  95. *
  96. * Note that the same segment can be used for 32-bit and 64-bit data segments
  97. * (the L bit is only defined for code segments)
  98. *
  99. * Selectors 0x08-0x10 and 0x3b-0x4b are set up for use with the SYSCALL
  100. * and SYSRET instructions.
  101. */
  102. #define KERNEL_CS 0x08
  103. #define KERNEL_DS 0x10
  104. #define NP_SEL 0x18
  105. #ifdef __x86_64__
  106. #define KERNEL_CS32 0x20
  107. #else
  108. #define TSS_INTR 0x20
  109. #endif
  110. #define KERNEL_CS16 0x28
  111. #define KERNEL_DS16 0x30
  112. #define USER_CS32 0x3b
  113. #define USER_DS 0x43
  114. #ifdef __x86_64__
  115. #define USER_CS64 0x4b
  116. #endif
  117. /* Synonyms */
  118. #define KERNEL_DS32 KERNEL_DS
  119. #define USER_DS32 USER_DS
  120. #ifdef __x86_64__
  121. #define KERNEL_CS64 KERNEL_CS
  122. #define USER_CS USER_CS64
  123. #define KERNEL_DS64 KERNEL_DS
  124. #define USER_DS64 USER_DS
  125. #else
  126. #define KERNEL_CS32 KERNEL_CS
  127. #define USER_CS USER_CS32
  128. #endif
  129. #define FIRST_SPARE_SEL 0x50
  130. #define TSS_MAIN 0x80
  131. typedef struct {
  132. unsigned short offset0;
  133. unsigned short selector;
  134. unsigned short ist : 3;
  135. unsigned short : 5;
  136. unsigned short type : 4;
  137. unsigned short : 1;
  138. unsigned short dpl : 2;
  139. unsigned short p : 1;
  140. unsigned short offset1;
  141. #ifdef __x86_64__
  142. unsigned offset2;
  143. unsigned reserved;
  144. #endif
  145. } idt_entry_t;
  146. typedef struct {
  147. u16 limit_low;
  148. u16 base_low;
  149. u8 base_middle;
  150. u8 access;
  151. u8 granularity;
  152. u8 base_high;
  153. } gdt_entry_t;
  154. extern idt_entry_t boot_idt[256];
  155. #ifndef __x86_64__
  156. extern gdt_entry_t gdt32[];
  157. extern tss32_t tss;
  158. extern tss32_t tss_intr;
  159. void set_gdt_task_gate(u16 tss_sel, u16 sel);
  160. void set_idt_task_gate(int vec, u16 sel);
  161. void set_intr_task_gate(int vec, void *fn);
  162. void setup_tss32(void);
  163. #else
  164. extern tss64_t tss;
  165. #endif
  166. unsigned exception_vector(void);
  167. unsigned exception_error_code(void);
  168. bool exception_rflags_rf(void);
  169. void set_idt_entry(int vec, void *addr, int dpl);
  170. void set_idt_sel(int vec, u16 sel);
  171. void set_idt_dpl(int vec, u16 dpl);
  172. void set_gdt_entry(int sel, u32 base, u32 limit, u8 access, u8 gran);
  173. void set_intr_alt_stack(int e, void *fn);
  174. void print_current_tss_info(void);
  175. handler handle_exception(u8 v, handler fn);
  176. bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data),
  177. void *data);
  178. void __set_exception_jmpbuf(jmp_buf *addr);
  179. #define set_exception_jmpbuf(jmpbuf) \
  180. (setjmp(jmpbuf) ? : (__set_exception_jmpbuf(&(jmpbuf)), 0))
  181. #endif