trap.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "trap.h"
  2. #include "gate.h"
  3. #include "../process/ptrace.h"
  4. #include "../common/kprint.h"
  5. void sys_vector_init()
  6. {
  7. set_trap_gate(0, 1, &divide_error);
  8. set_trap_gate(1, 1, &debug);
  9. set_intr_gate(2, 1, &nmi);
  10. set_system_trap_gate(3, 1, &int3);
  11. set_system_trap_gate(4, 1, &overflow);
  12. set_system_trap_gate(5, 1, &bounds);
  13. set_trap_gate(6, 1, &undefined_opcode);
  14. set_trap_gate(7, 1, &dev_not_avaliable);
  15. set_trap_gate(8, 1, &double_fault);
  16. set_trap_gate(9, 1, &coprocessor_segment_overrun);
  17. set_trap_gate(10, 1, &invalid_TSS);
  18. set_trap_gate(11, 1, &segment_not_exists);
  19. set_trap_gate(12, 1, &stack_segment_fault);
  20. set_trap_gate(13, 1, &general_protection);
  21. set_trap_gate(14, 1, &page_fault);
  22. // 中断号15由Intel保留,不能使用
  23. set_trap_gate(16, 1, &x87_FPU_error);
  24. set_trap_gate(17, 1, &alignment_check);
  25. set_trap_gate(18, 1, &machine_check);
  26. set_trap_gate(19, 1, &SIMD_exception);
  27. set_trap_gate(20, 1, &virtualization_exception);
  28. // 中断号21-31由Intel保留,不能使用
  29. // 32-255为用户自定义中断内部
  30. /*
  31. set_trap_gate(0, 1, divide_error);
  32. set_trap_gate(1, 1, debug);
  33. set_intr_gate(2, 1, nmi);
  34. set_system_trap_gate(3, 1, int3);
  35. set_system_trap_gate(4, 1, overflow);
  36. set_system_trap_gate(5, 1, bounds);
  37. set_trap_gate(6, 1, undefined_opcode);
  38. set_trap_gate(7, 1, dev_not_avaliable);
  39. set_trap_gate(8, 1, double_fault);
  40. set_trap_gate(9, 1, coprocessor_segment_overrun);
  41. set_trap_gate(10, 1, invalid_TSS);
  42. set_trap_gate(11, 1, segment_not_exists);
  43. set_trap_gate(12, 1, stack_segment_fault);
  44. set_trap_gate(13, 1, general_protection);
  45. set_trap_gate(14, 1, page_fault);
  46. // 中断号15由Intel保留,不能使用
  47. set_trap_gate(16, 1, x87_FPU_error);
  48. set_trap_gate(17, 1, alignment_check);
  49. set_trap_gate(18, 1, machine_check);
  50. set_trap_gate(19, 1, SIMD_exception);
  51. set_trap_gate(20, 1, virtualization_exception);
  52. */
  53. }
  54. // 0 #DE 除法错误
  55. void do_divide_error(struct pt_regs *regs, unsigned long error_code)
  56. {
  57. kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  58. while (1)
  59. ;
  60. }
  61. // 1 #DB 调试异常
  62. void do_debug(struct pt_regs *regs, unsigned long error_code)
  63. {
  64. printk("[ ");
  65. printk_color(RED, BLACK, "ERROR / TRAP");
  66. printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  67. while (1)
  68. ;
  69. }
  70. // 2 不可屏蔽中断
  71. void do_nmi(struct pt_regs *regs, unsigned long error_code)
  72. {
  73. printk("[ ");
  74. printk_color(BLUE, BLACK, "INT");
  75. printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  76. while (1)
  77. ;
  78. }
  79. // 3 #BP 断点异常
  80. void do_int3(struct pt_regs *regs, unsigned long error_code)
  81. {
  82. printk("[ ");
  83. printk_color(YELLOW, BLACK, "TRAP");
  84. printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  85. while (1)
  86. ;
  87. }
  88. // 4 #OF 溢出异常
  89. void do_overflow(struct pt_regs *regs, unsigned long error_code)
  90. {
  91. printk("[ ");
  92. printk_color(YELLOW, BLACK, "TRAP");
  93. printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  94. while (1)
  95. ;
  96. }
  97. // 5 #BR 越界异常
  98. void do_bounds(struct pt_regs *regs, unsigned long error_code)
  99. {
  100. kerror("do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  101. while (1)
  102. ;
  103. }
  104. // 6 #UD 无效/未定义的机器码
  105. void do_undefined_opcode(struct pt_regs *regs, unsigned long error_code)
  106. {
  107. kerror("do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx", error_code, regs->rsp, regs->rip);
  108. while (1)
  109. ;
  110. }
  111. // 7 #NM 设备异常(FPU不存在)
  112. void do_dev_not_avaliable(struct pt_regs *regs, unsigned long error_code)
  113. {
  114. kerror("do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  115. while (1)
  116. ;
  117. }
  118. // 8 #DF 双重错误
  119. void do_double_fault(struct pt_regs *regs, unsigned long error_code)
  120. {
  121. printk("[ ");
  122. printk_color(RED, BLACK, "Terminate");
  123. printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  124. while (1)
  125. ;
  126. }
  127. // 9 协处理器越界(保留)
  128. void do_coprocessor_segment_overrun(struct pt_regs *regs, unsigned long error_code)
  129. {
  130. kerror("do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  131. while (1)
  132. ;
  133. }
  134. // 10 #TS 无效的TSS段
  135. void do_invalid_TSS(struct pt_regs *regs, unsigned long error_code)
  136. {
  137. printk("[");
  138. printk_color(RED, BLACK, "ERROR");
  139. printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  140. printk_color(YELLOW, BLACK, "Information:\n");
  141. // 解析错误码
  142. if (error_code & 0x01)
  143. printk("The exception occurred during delivery of an event external to the program.\n");
  144. if (error_code & 0x02)
  145. printk("Refers to a descriptor in the IDT.\n");
  146. else
  147. {
  148. if (error_code & 0x04)
  149. printk("Refers to a descriptor in the current LDT.\n");
  150. else
  151. printk("Refers to a descriptor in the GDT.\n");
  152. }
  153. printk("Segment Selector Index:%10x\n", error_code & 0xfff8);
  154. printk("\n");
  155. while (1)
  156. ;
  157. }
  158. // 11 #NP 段不存在
  159. void do_segment_not_exists(struct pt_regs *regs, unsigned long error_code)
  160. {
  161. kerror("do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  162. while (1)
  163. ;
  164. }
  165. // 12 #SS SS段错误
  166. void do_stack_segment_fault(struct pt_regs *regs, unsigned long error_code)
  167. {
  168. kerror("do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  169. while (1)
  170. ;
  171. }
  172. // 13 #GP 通用保护性异常
  173. void do_general_protection(struct pt_regs *regs, unsigned long error_code)
  174. {
  175. kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  176. if (error_code & 0x01)
  177. printk_color(RED, BLACK, "The exception occurred during delivery of an event external to the program,such as an interrupt or an earlier exception.\n");
  178. if (error_code & 0x02)
  179. printk_color(RED, BLACK, "Refers to a gate descriptor in the IDT;\n");
  180. else
  181. printk_color(RED, BLACK, "Refers to a descriptor in the GDT or the current LDT;\n");
  182. if ((error_code & 0x02) == 0)
  183. if (error_code & 0x04)
  184. printk_color(RED, BLACK, "Refers to a segment or gate descriptor in the LDT;\n");
  185. else
  186. printk_color(RED, BLACK, "Refers to a descriptor in the current GDT;\n");
  187. printk_color(RED, BLACK, "Segment Selector Index:%#010x\n", error_code & 0xfff8);
  188. while (1)
  189. ;
  190. }
  191. // 14 #PF 页故障
  192. void do_page_fault(struct pt_regs *regs, unsigned long error_code)
  193. {
  194. unsigned long cr2 = 0;
  195. // 先保存cr2寄存器的值,避免由于再次触发页故障而丢失值
  196. // cr2存储着触发异常的线性地址
  197. __asm__ __volatile__("movq %%cr2, %0"
  198. : "=r"(cr2)::"memory");
  199. kerror("do_page_fault(14),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\tCR2:%#18lx\n", error_code, regs->rsp, regs->rip, cr2);
  200. printk_color(YELLOW, BLACK, "Information:\n");
  201. if (!(error_code & 0x01))
  202. printk("Page does not exist.\n");
  203. if (error_code & 0x02)
  204. printk("Fault occurred during operation: writing\n");
  205. else
  206. printk("Fault occurred during operation: reading\n");
  207. if (error_code & 0x04)
  208. printk("Fault in user level(3).\n");
  209. else
  210. printk("Fault in supervisor level(0,1,2).\n");
  211. if (error_code & 0x08)
  212. printk("Reserved bit caused the fault.\n");
  213. if (error_code & 0x10)
  214. printk("Fault occurred during fetching instruction.\n");
  215. while (1)
  216. ;
  217. }
  218. // 15 Intel保留,请勿使用
  219. // 16 #MF x87FPU错误
  220. void do_x87_FPU_error(struct pt_regs *regs, unsigned long error_code)
  221. {
  222. kerror("do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  223. while (1)
  224. ;
  225. }
  226. // 17 #AC 对齐检测
  227. void do_alignment_check(struct pt_regs *regs, unsigned long error_code)
  228. {
  229. kerror("do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  230. while (1)
  231. ;
  232. }
  233. // 18 #MC 机器检测
  234. void do_machine_check(struct pt_regs *regs, unsigned long error_code)
  235. {
  236. kerror("do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  237. while (1)
  238. ;
  239. }
  240. // 19 #XM SIMD浮点异常
  241. void do_SIMD_exception(struct pt_regs *regs, unsigned long error_code)
  242. {
  243. kerror("do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  244. while (1)
  245. ;
  246. }
  247. // 20 #VE 虚拟化异常
  248. void do_virtualization_exception(struct pt_regs *regs, unsigned long error_code)
  249. {
  250. kerror("do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
  251. while (1)
  252. ;
  253. }
  254. // 21-21 Intel保留,请勿使用