trap.c 9.3 KB

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