trap.c 9.2 KB

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