trap.c 9.8 KB

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