trap.c 9.0 KB

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