1 #include "trap.h"
2 #include "gate.h"
3 #include <common/kprint.h>
4 #include <debug/traceback/traceback.h>
5 #include <process/process.h>
6 #include <process/ptrace.h>
7 #include <sched/sched.h>
8 
9 extern void ignore_int();
10 
11 // 0 #DE 除法错误
do_divide_error(struct pt_regs * regs,unsigned long error_code)12 void do_divide_error(struct pt_regs *regs, unsigned long error_code)
13 {
14     // kerror("do_divide_error(0)");
15     kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\t pid=%d\n", error_code,
16            regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid);
17     traceback(regs);
18     current_pcb->state = PROC_STOPPED;
19     sched();
20 }
21 
22 // 1 #DB 调试异常
do_debug(struct pt_regs * regs,unsigned long error_code)23 void do_debug(struct pt_regs *regs, unsigned long error_code)
24 {
25     printk("[ ");
26     printk_color(RED, BLACK, "ERROR / TRAP");
27     printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d, pid:%d\n", error_code, regs->rsp, regs->rip,
28            proc_current_cpu_id, current_pcb->pid);
29 
30     while (1)
31         hlt();
32 }
33 
34 // 2 不可屏蔽中断
do_nmi(struct pt_regs * regs,unsigned long error_code)35 void do_nmi(struct pt_regs *regs, unsigned long error_code)
36 {
37 
38     printk("[ ");
39     printk_color(BLUE, BLACK, "INT");
40     printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip,
41            proc_current_cpu_id);
42 
43     while (1)
44         hlt();
45 }
46 
47 // 3 #BP 断点异常
do_int3(struct pt_regs * regs,unsigned long error_code)48 void do_int3(struct pt_regs *regs, unsigned long error_code)
49 {
50 
51     printk("[ ");
52     printk_color(YELLOW, BLACK, "TRAP");
53     printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip,
54            proc_current_cpu_id);
55 
56     while (1)
57         hlt();
58 }
59 
60 // 4 #OF 溢出异常
do_overflow(struct pt_regs * regs,unsigned long error_code)61 void do_overflow(struct pt_regs *regs, unsigned long error_code)
62 {
63 
64     printk("[ ");
65     printk_color(YELLOW, BLACK, "TRAP");
66     printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
67            regs->rip, proc_current_cpu_id);
68 
69     current_pcb->state = PROC_STOPPED;
70     sched();
71 }
72 
73 // 5 #BR 越界异常
do_bounds(struct pt_regs * regs,unsigned long error_code)74 void do_bounds(struct pt_regs *regs, unsigned long error_code)
75 {
76 
77     kerror("do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip,
78            proc_current_cpu_id);
79 
80     while (1)
81         hlt();
82 }
83 
84 // 6 #UD 无效/未定义的机器码
do_undefined_opcode(struct pt_regs * regs,unsigned long error_code)85 void do_undefined_opcode(struct pt_regs *regs, unsigned long error_code)
86 {
87 
88     kerror("do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d, pid:%ld", error_code,
89            regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid);
90     traceback(regs);
91     current_pcb->state = PROC_STOPPED;
92     sched();
93 }
94 
95 // 7 #NM 设备异常(FPU不存在)
do_dev_not_avaliable(struct pt_regs * regs,unsigned long error_code)96 void do_dev_not_avaliable(struct pt_regs *regs, unsigned long error_code)
97 {
98 
99     kerror("do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
100            regs->rip, proc_current_cpu_id);
101 
102     current_pcb->state = PROC_STOPPED;
103     sched();
104 }
105 
106 // 8 #DF 双重错误
do_double_fault(struct pt_regs * regs,unsigned long error_code)107 void do_double_fault(struct pt_regs *regs, unsigned long error_code)
108 {
109 
110     printk("[ ");
111     printk_color(RED, BLACK, "Terminate");
112     printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
113            regs->rip, proc_current_cpu_id);
114     traceback(regs);
115     current_pcb->state = PROC_STOPPED;
116     sched();
117 }
118 
119 // 9 协处理器越界(保留)
do_coprocessor_segment_overrun(struct pt_regs * regs,unsigned long error_code)120 void do_coprocessor_segment_overrun(struct pt_regs *regs, unsigned long error_code)
121 {
122 
123     kerror("do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code,
124            regs->rsp, regs->rip, proc_current_cpu_id);
125 
126     current_pcb->state = PROC_STOPPED;
127     sched();
128 }
129 
130 // 10 #TS 无效的TSS段
do_invalid_TSS(struct pt_regs * regs,unsigned long error_code)131 void do_invalid_TSS(struct pt_regs *regs, unsigned long error_code)
132 {
133 
134     printk("[");
135     printk_color(RED, BLACK, "ERROR");
136     printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
137            regs->rip, proc_current_cpu_id);
138 
139     printk_color(YELLOW, BLACK, "Information:\n");
140     // 解析错误码
141     if (error_code & 0x01)
142         printk("The exception occurred during delivery of an event external to the program.\n");
143 
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 
154     printk("Segment Selector Index:%10x\n", error_code & 0xfff8);
155 
156     printk("\n");
157 
158     current_pcb->state = PROC_STOPPED;
159     sched();
160 }
161 
162 // 11 #NP 段不存在
do_segment_not_exists(struct pt_regs * regs,unsigned long error_code)163 void do_segment_not_exists(struct pt_regs *regs, unsigned long error_code)
164 {
165 
166     kerror("do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
167            regs->rip, proc_current_cpu_id);
168 
169     current_pcb->state = PROC_STOPPED;
170     sched();
171 }
172 
173 // 12 #SS SS段错误
do_stack_segment_fault(struct pt_regs * regs,unsigned long error_code)174 void do_stack_segment_fault(struct pt_regs *regs, unsigned long error_code)
175 {
176 
177     kerror("do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
178            regs->rip, proc_current_cpu_id);
179     traceback(regs);
180     current_pcb->state = PROC_STOPPED;
181     sched();
182 }
183 
184 // 13 #GP 通用保护性异常
do_general_protection(struct pt_regs * regs,unsigned long error_code)185 void do_general_protection(struct pt_regs *regs, unsigned long error_code)
186 {
187 
188     kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\tpid=%ld\n", error_code,
189            regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid);
190     if (error_code & 0x01)
191         printk_color(RED, BLACK,
192                      "The exception occurred during delivery of an event external to the program,such as an interrupt "
193                      "or an earlier exception.\n");
194 
195     if (error_code & 0x02)
196         printk_color(RED, BLACK, "Refers to a gate descriptor in the IDT;\n");
197     else
198         printk_color(RED, BLACK, "Refers to a descriptor in the GDT or the current LDT;\n");
199 
200     if ((error_code & 0x02) == 0)
201         if (error_code & 0x04)
202             printk_color(RED, BLACK, "Refers to a segment or gate descriptor in the LDT;\n");
203         else
204             printk_color(RED, BLACK, "Refers to a descriptor in the current GDT;\n");
205 
206     printk_color(RED, BLACK, "Segment Selector Index:%#010x\n", error_code & 0xfff8);
207     traceback(regs);
208     current_pcb->state = PROC_STOPPED;
209     sched();
210 }
211 
212 // 14 #PF 页故障
do_page_fault(struct pt_regs * regs,unsigned long error_code)213 void do_page_fault(struct pt_regs *regs, unsigned long error_code)
214 {
215 
216     unsigned long cr2 = 0;
217 
218     __asm__ __volatile__("movq	%%cr2,	%0" : "=r"(cr2)::"memory");
219 
220     kerror("do_page_fault(14),Error code :%#018lx,RSP:%#018lx, RBP=%#018lx, RIP:%#018lx CPU:%d, pid=%d\n", error_code,
221            regs->rsp, regs->rbp, regs->rip, proc_current_cpu_id, current_pcb->pid);
222     kerror("regs->rax = %#018lx\n", regs->rax);
223     if (!(error_code & 0x01))
224         printk_color(RED, BLACK, "Page Not-Present,\t");
225 
226     if (error_code & 0x02)
227         printk_color(RED, BLACK, "Write Cause Fault,\t");
228     else
229         printk_color(RED, BLACK, "Read Cause Fault,\t");
230 
231     if (error_code & 0x04)
232         printk_color(RED, BLACK, "Fault in user(3)\t");
233     else
234         printk_color(RED, BLACK, "Fault in supervisor(0,1,2)\t");
235 
236     if (error_code & 0x08)
237         printk_color(RED, BLACK, ",Reserved Bit Cause Fault\t");
238 
239     if (error_code & 0x10)
240         printk_color(RED, BLACK, ",Instruction fetch Cause Fault");
241 
242     printk_color(RED, BLACK, "\n");
243 
244     printk_color(RED, BLACK, "CR2:%#018lx\n", cr2);
245 
246     traceback(regs);
247     current_pcb->state = PROC_STOPPED;
248     sched();
249 }
250 
251 // 15 Intel保留,请勿使用
252 
253 // 16 #MF x87FPU错误
do_x87_FPU_error(struct pt_regs * regs,unsigned long error_code)254 void do_x87_FPU_error(struct pt_regs *regs, unsigned long error_code)
255 {
256 
257     kerror("do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
258            regs->rip, proc_current_cpu_id);
259 
260     while (1)
261         hlt();
262 }
263 
264 // 17 #AC 对齐检测
do_alignment_check(struct pt_regs * regs,unsigned long error_code)265 void do_alignment_check(struct pt_regs *regs, unsigned long error_code)
266 {
267 
268     kerror("do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
269            regs->rip, proc_current_cpu_id);
270 
271     current_pcb->state = PROC_STOPPED;
272     sched();
273 }
274 
275 // 18 #MC 机器检测
do_machine_check(struct pt_regs * regs,unsigned long error_code)276 void do_machine_check(struct pt_regs *regs, unsigned long error_code)
277 {
278 
279     kerror("do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
280            regs->rip, proc_current_cpu_id);
281 
282     current_pcb->state = PROC_STOPPED;
283     sched();
284 }
285 
286 // 19 #XM SIMD浮点异常
do_SIMD_exception(struct pt_regs * regs,unsigned long error_code)287 void do_SIMD_exception(struct pt_regs *regs, unsigned long error_code)
288 {
289 
290     kerror("do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
291            regs->rip, proc_current_cpu_id);
292 
293     current_pcb->state = PROC_STOPPED;
294     sched();
295 }
296 
297 // 20 #VE 虚拟化异常
do_virtualization_exception(struct pt_regs * regs,unsigned long error_code)298 void do_virtualization_exception(struct pt_regs *regs, unsigned long error_code)
299 {
300 
301     kerror("do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code,
302            regs->rsp, regs->rip, proc_current_cpu_id);
303 
304     current_pcb->state = PROC_STOPPED;
305     sched();
306 }
307 
308 // 21-21 Intel保留,请勿使用
309 
310 /**
311  * @brief 当系统收到未知的中断时,执行此处理函数
312  *
313  * @param regs
314  * @param error_code
315  */
ignore_int_handler(struct pt_regs * regs,unsigned long error_code)316 void ignore_int_handler(struct pt_regs *regs, unsigned long error_code)
317 {
318     kwarn("Unknown interrupt or fault at RIP.\n");
319 }
320 
sys_vector_init()321 void sys_vector_init()
322 {
323     // 将idt重置为新的ignore_int入点(此前在head.S中有设置,
324     // 但是那个不完整,某些版本的编译器的输出,在真机运行时会破坏进程执行环境,从而导致#GP
325     for (int i = 0; i < 256; ++i)
326         set_intr_gate(i, 0, ignore_int);
327 
328     set_trap_gate(0, 0, divide_error);
329     set_trap_gate(1, 0, debug);
330     set_intr_gate(2, 0, nmi);
331     set_system_trap_gate(3, 0, int3);
332     set_system_trap_gate(4, 0, overflow);
333     set_system_trap_gate(5, 0, bounds);
334     set_trap_gate(6, 0, undefined_opcode);
335     set_trap_gate(7, 0, dev_not_avaliable);
336     set_trap_gate(8, 0, double_fault);
337     set_trap_gate(9, 0, coprocessor_segment_overrun);
338     set_trap_gate(10, 0, invalid_TSS);
339     set_trap_gate(11, 0, segment_not_exists);
340     set_trap_gate(12, 0, stack_segment_fault);
341     set_trap_gate(13, 0, general_protection);
342     set_trap_gate(14, 0, page_fault);
343     // 中断号15由Intel保留,不能使用
344     set_trap_gate(16, 0, x87_FPU_error);
345     set_trap_gate(17, 0, alignment_check);
346     set_trap_gate(18, 0, machine_check);
347     set_trap_gate(19, 0, SIMD_exception);
348     set_trap_gate(20, 0, virtualization_exception);
349     // 中断号21-31由Intel保留,不能使用
350 
351     // 32-255为用户自定义中断内部
352 }